From e835fe8fe352751e6ca03c1edf31705c3abe61f4 Mon Sep 17 00:00:00 2001 From: Ben S Date: Mon, 26 Jan 2015 17:26:11 +0000 Subject: [PATCH 1/6] Make all fields of Dir private --- src/dir.rs | 9 +++++++-- src/file.rs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index ff92bf7..0f50224 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -8,8 +8,8 @@ use file::File; /// check the existence of surrounding files, then highlight themselves /// accordingly. (See `File#get_source_files`) pub struct Dir { - pub contents: Vec, - pub path: Path, + contents: Vec, + path: Path, } impl Dir { @@ -42,4 +42,9 @@ impl Dir { pub fn contains(&self, path: &Path) -> bool { self.contents.contains(path) } + + /// Append a path onto the path specified by this directory. + pub fn join(&self, child: Path) -> Path { + self.path.join(child) + } } diff --git a/src/file.rs b/src/file.rs index 9982494..b225897 100644 --- a/src/file.rs +++ b/src/file.rs @@ -113,7 +113,7 @@ impl<'a> File<'a> { if let Ok(path) = fs::readlink(&self.path) { let target_path = match self.dir { - Some(dir) => dir.path.join(path), + Some(dir) => dir.join(path), None => path, }; From 90d4684de4d64fadcd4025c2c86e5c82857f6cd0 Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 27 Jan 2015 15:01:17 +0000 Subject: [PATCH 2/6] Preliminary Git support! This is something that I've long wanted to add. It uses libgit2 as an optional dependency. --- Cargo.lock | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++++ src/column.rs | 4 +++ src/dir.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++- src/file.rs | 6 ++++ src/main.rs | 3 ++ src/options.rs | 1 + 7 files changed, 194 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index f6e9215..0d14356 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "ansi_term 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "git2 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "natord 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "number_prefix 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "users 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -18,11 +19,68 @@ dependencies = [ "regex_macros 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bitflags" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "getopts" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "git2" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libgit2-sys 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libgit2-sys" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libssh2-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libressl-pnacl-sys" +version = "2.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pnacl-build-helper 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libssh2-sys" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libz-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libz-sys" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pkg-config 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "matches" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "natord" version = "1.0.6" @@ -33,6 +91,25 @@ name = "number_prefix" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "openssl-sys" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libressl-pnacl-sys 2.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pkg-config" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pnacl-build-helper" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "regex" version = "0.1.10" @@ -46,6 +123,20 @@ dependencies = [ "regex 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rustc-serialize" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "url" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "users" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 466105d..7d60fdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,10 @@ getopts = "0.1.4" natord = "1.0.6" number_prefix = "0.2.1" users = "0.2.1" + +[features] +git = [ "git2" ] + +[dependencies.git2] +version = "0.1.12" +optional = true \ No newline at end of file diff --git a/src/column.rs b/src/column.rs index 8e97aac..210e69e 100644 --- a/src/column.rs +++ b/src/column.rs @@ -12,6 +12,8 @@ pub enum Column { Group, HardLinks, Inode, + + GitStatus, } #[derive(PartialEq, Debug, Copy)] @@ -37,6 +39,7 @@ impl Column { Column::HardLinks => Alignment::Right, Column::Inode => Alignment::Right, Column::Blocks => Alignment::Right, + Column::GitStatus => Alignment::Right, _ => Alignment::Left, } } @@ -53,6 +56,7 @@ impl Column { Column::Group => "Group", Column::HardLinks => "Links", Column::Inode => "inode", + Column::GitStatus => "Git", } } } diff --git a/src/dir.rs b/src/dir.rs index 0f50224..29f4106 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -1,5 +1,9 @@ use std::io::{fs, IoResult}; -use file::File; +use file::{File, GREY}; + +#[cfg(feature="git")] use ansi_term::ANSIString; +#[cfg(feature="git")] use ansi_term::Colour::*; +#[cfg(feature="git")] use git2; /// A **Dir** provides a cached list of the file paths in a directory that's /// being listed. @@ -10,6 +14,7 @@ use file::File; pub struct Dir { contents: Vec, path: Path, + git: Option, } impl Dir { @@ -20,6 +25,7 @@ impl Dir { fs::readdir(&path).map(|paths| Dir { contents: paths, path: path.clone(), + git: Git::new(&path).ok(), }) } @@ -47,4 +53,79 @@ impl Dir { pub fn join(&self, child: Path) -> Path { self.path.join(child) } + + /// Return whether there's a Git repository on or above this directory. + pub fn has_git_repo(&self) -> bool { + self.git.is_some() + } + + /// Get a string describing the Git status of the given file. + pub fn git_status(&self, path: &Path) -> String { + match self.git { + Some(ref git) => git.status(path), + None => GREY.paint("--").to_string(), + } + } +} + +#[cfg(feature="git")] +struct Git { + statuses: Vec<(String, git2::Status)>, +} + +#[cfg(feature="git")] +impl Git { + fn new(path: &Path) -> Result { + let repo = try!(git2::Repository::discover(path)); + let statuses = try!(repo.statuses(None)); + + Ok(Git { statuses: statuses.iter().map(|e| (e.path().unwrap().to_string(), e.status())).collect() }) + } + + /// Get the status for the file at the given path, if present. + fn status(&self, path: &Path) -> String { + match self.statuses.iter().find(|&&(ref p, _)| path.as_str().unwrap() == p.as_slice()) { + Some(&(_, s)) => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)), + None => GREY.paint("--").to_string(), + } + } + + /// The character to display if the file has been modified, but not staged. + fn working_tree_status(status: git2::Status) -> ANSIString<'static> { + match status { + s if s.contains(git2::STATUS_WT_NEW) => Green.paint("A"), + s if s.contains(git2::STATUS_WT_MODIFIED) => Blue.paint("M"), + s if s.contains(git2::STATUS_WT_DELETED) => Red.paint("D"), + s if s.contains(git2::STATUS_WT_RENAMED) => Yellow.paint("R"), + s if s.contains(git2::STATUS_WT_TYPECHANGE) => Purple.paint("T"), + _ => GREY.paint("-"), + } + } + + /// The character to display if the file has been modified, and the change + /// has been staged. + fn index_status(status: git2::Status) -> ANSIString<'static> { + match status { + s if s.contains(git2::STATUS_INDEX_NEW) => Green.paint("A"), + s if s.contains(git2::STATUS_INDEX_MODIFIED) => Blue.paint("M"), + s if s.contains(git2::STATUS_INDEX_DELETED) => Red.paint("D"), + s if s.contains(git2::STATUS_INDEX_RENAMED) => Yellow.paint("R"), + s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => Purple.paint("T"), + _ => GREY.paint("-"), + } + } +} + +#[cfg(not(feature="git"))] +struct Git; + +#[cfg(not(feature="git"))] +impl Git { + fn new(_: &Path) -> Result { + Err(()) + } + + fn status(&self, _: &Path) -> String { + panic!("Tried to access a Git repo without Git support!"); + } } diff --git a/src/file.rs b/src/file.rs index b225897..060b4f5 100644 --- a/src/file.rs +++ b/src/file.rs @@ -79,6 +79,7 @@ impl<'a> File<'a> { Blocks => self.blocks(), User => self.user(users_cache), Group => self.group(users_cache), + GitStatus => self.git_status(), } } @@ -369,6 +370,11 @@ impl<'a> File<'a> { vec![] // No source files if there's no extension, either! } } + + fn git_status(&self) -> Cell { + let status = self.dir.map(|d| d.git_status(&self.path)).unwrap_or("NO".to_string()); + Cell { text: status, length: 2 } + } } /// Extract an extension from a string, if one is present, in lowercase. diff --git a/src/main.rs b/src/main.rs index 5fb36fe..8f33d35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,9 @@ extern crate number_prefix; extern crate unicode; extern crate users; +#[cfg(feature="git")] +extern crate git2; + use std::io::FileType; use std::io::fs; use std::os::{args, set_exit_status}; diff --git a/src/options.rs b/src/options.rs index 2cb45e4..9a1638c 100644 --- a/src/options.rs +++ b/src/options.rs @@ -265,6 +265,7 @@ fn columns(matches: &getopts::Matches) -> Result, Misfire> { columns.push(Group); } + columns.push(GitStatus); columns.push(FileName); Ok(columns) } From 1d0cc329eb4a6c9aa26c9c2b3ef3c486a1eaf7da Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 27 Jan 2015 15:30:55 +0000 Subject: [PATCH 3/6] Don't even show the column without the feature --- src/dir.rs | 12 +++++++++--- src/options.rs | 5 ++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 29f4106..579dfc9 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -25,7 +25,7 @@ impl Dir { fs::readdir(&path).map(|paths| Dir { contents: paths, path: path.clone(), - git: Git::new(&path).ok(), + git: Git::scan(&path).ok(), }) } @@ -68,6 +68,7 @@ impl Dir { } } +/// Container of Git statuses for all the files in this folder's Git repository. #[cfg(feature="git")] struct Git { statuses: Vec<(String, git2::Status)>, @@ -75,7 +76,10 @@ struct Git { #[cfg(feature="git")] impl Git { - fn new(path: &Path) -> Result { + + /// Discover a Git repository on or above this directory, scanning it for + /// the files' statuses if one is found. + fn scan(path: &Path) -> Result { let repo = try!(git2::Repository::discover(path)); let statuses = try!(repo.statuses(None)); @@ -121,11 +125,13 @@ struct Git; #[cfg(not(feature="git"))] impl Git { - fn new(_: &Path) -> Result { + fn scan(_: &Path) -> Result { + // Don't do anything without Git support Err(()) } fn status(&self, _: &Path) -> String { + // The Err above means that this should never happen panic!("Tried to access a Git repo without Git support!"); } } diff --git a/src/options.rs b/src/options.rs index 9a1638c..f103699 100644 --- a/src/options.rs +++ b/src/options.rs @@ -265,7 +265,10 @@ fn columns(matches: &getopts::Matches) -> Result, Misfire> { columns.push(Group); } - columns.push(GitStatus); + if cfg!(feature="git") { + columns.push(GitStatus); + } + columns.push(FileName); Ok(columns) } From f6cbfc7276326d7e9e49ae3c857c9a9d3b9222b5 Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 27 Jan 2015 15:59:22 +0000 Subject: [PATCH 4/6] Support Git by default --- Cargo.toml | 1 + README.md | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 7d60fdd..aba4e13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ number_prefix = "0.2.1" users = "0.2.1" [features] +default = [ "git" ] git = [ "git2" ] [dependencies.git2] diff --git a/README.md b/README.md index 1c3a633..5640235 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,5 @@ You can sort by **name**, **size**, **ext**, **inode**, or **none**. ## Installation exa is written in [Rust](http://www.rust-lang.org). You'll have to use the nightly -- I try to keep it up to date with the latest version when possible. Once you have it set up, a simple `cargo build` will pull in all the dependencies and compile exa. + +exa depends on [libgit2](https://github.com/alexcrichton/git2-rs) for certain features. If you're unable to compile libgit2, you can opt out of Git support by passing `--no-default-features` to Cargo. From f794f5eda6bfdd975ea331c437e8c744118c0f3a Mon Sep 17 00:00:00 2001 From: Ben S Date: Wed, 28 Jan 2015 10:43:19 +0000 Subject: [PATCH 5/6] Implement Git status for directories --- src/dir.rs | 26 +++++++++++++++++++++----- src/file.rs | 6 +++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 579dfc9..1591086 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -60,10 +60,11 @@ impl Dir { } /// Get a string describing the Git status of the given file. - pub fn git_status(&self, path: &Path) -> String { - match self.git { - Some(ref git) => git.status(path), - None => GREY.paint("--").to_string(), + pub fn git_status(&self, path: &Path, prefix_lookup: bool) -> String { + match (&self.git, prefix_lookup) { + (&Some(ref git), false) => git.status(path), + (&Some(ref git), true) => git.dir_status(path), + (&None, _) => GREY.paint("--").to_string(), } } } @@ -88,12 +89,27 @@ impl Git { /// Get the status for the file at the given path, if present. fn status(&self, path: &Path) -> String { - match self.statuses.iter().find(|&&(ref p, _)| path.as_str().unwrap() == p.as_slice()) { + let status = self.statuses.iter() + .find(|p| p.0 == path.as_str().unwrap()); + + match status { Some(&(_, s)) => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)), None => GREY.paint("--").to_string(), } } + /// Get the combined status for all the files whose paths begin with the + /// path that gets passed in. This is used for getting the status of + /// directories, which don't really have an 'official' status. + fn dir_status(&self, dir: &Path) -> String { + let status = self.statuses.iter() + .filter(|p| p.0.starts_with(dir.as_str().unwrap())) + .fold(git2::Status::empty(), |a, b| a | b.1); + match status { + s => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)), + } + } + /// The character to display if the file has been modified, but not staged. fn working_tree_status(status: git2::Status) -> ANSIString<'static> { match status { diff --git a/src/file.rs b/src/file.rs index 060b4f5..e6470d4 100644 --- a/src/file.rs +++ b/src/file.rs @@ -372,7 +372,11 @@ impl<'a> File<'a> { } fn git_status(&self) -> Cell { - let status = self.dir.map(|d| d.git_status(&self.path)).unwrap_or("NO".to_string()); + let status = match self.dir { + Some(d) => d.git_status(&self.path, self.stat.kind == io::FileType::Directory), + None => GREY.paint("--").to_string(), + }; + Cell { text: status, length: 2 } } } From 04b2483d1fd6230960f77c98420999b3e823242b Mon Sep 17 00:00:00 2001 From: Ben S Date: Wed, 28 Jan 2015 15:49:17 +0000 Subject: [PATCH 6/6] Compare vectors, not strings This is good for two reasons: 1) shorter code! 2) it won't fail if any of the filenames aren't valid UTF-8. --- src/dir.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 1591086..e22b092 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -72,7 +72,7 @@ impl Dir { /// Container of Git statuses for all the files in this folder's Git repository. #[cfg(feature="git")] struct Git { - statuses: Vec<(String, git2::Status)>, + statuses: Vec<(Vec, git2::Status)>, } #[cfg(feature="git")] @@ -82,16 +82,16 @@ impl Git { /// the files' statuses if one is found. fn scan(path: &Path) -> Result { let repo = try!(git2::Repository::discover(path)); - let statuses = try!(repo.statuses(None)); - - Ok(Git { statuses: statuses.iter().map(|e| (e.path().unwrap().to_string(), e.status())).collect() }) + let statuses = try!(repo.statuses(None)).iter() + .map(|e| (e.path_bytes().to_vec(), e.status())) + .collect(); + Ok(Git { statuses: statuses }) } /// Get the status for the file at the given path, if present. fn status(&self, path: &Path) -> String { let status = self.statuses.iter() - .find(|p| p.0 == path.as_str().unwrap()); - + .find(|p| p.0 == path.as_vec()); match status { Some(&(_, s)) => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)), None => GREY.paint("--").to_string(), @@ -103,7 +103,7 @@ impl Git { /// directories, which don't really have an 'official' status. fn dir_status(&self, dir: &Path) -> String { let status = self.statuses.iter() - .filter(|p| p.0.starts_with(dir.as_str().unwrap())) + .filter(|p| p.0.starts_with(dir.as_vec())) .fold(git2::Status::empty(), |a, b| a | b.1); match status { s => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)),