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..aba4e13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,11 @@ getopts = "0.1.4" natord = "1.0.6" number_prefix = "0.2.1" users = "0.2.1" + +[features] +default = [ "git" ] +git = [ "git2" ] + +[dependencies.git2] +version = "0.1.12" +optional = true \ No newline at end of file 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. 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 ff92bf7..e22b092 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. @@ -8,8 +12,9 @@ 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, + git: Option, } impl Dir { @@ -20,6 +25,7 @@ impl Dir { fs::readdir(&path).map(|paths| Dir { contents: paths, path: path.clone(), + git: Git::scan(&path).ok(), }) } @@ -42,4 +48,106 @@ 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) + } + + /// 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, 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(), + } + } +} + +/// Container of Git statuses for all the files in this folder's Git repository. +#[cfg(feature="git")] +struct Git { + statuses: Vec<(Vec, git2::Status)>, +} + +#[cfg(feature="git")] +impl Git { + + /// 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)).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_vec()); + 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_vec())) + .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 { + 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 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/file.rs b/src/file.rs index 9982494..e6470d4 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(), } } @@ -113,7 +114,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, }; @@ -369,6 +370,15 @@ impl<'a> File<'a> { vec![] // No source files if there's no extension, either! } } + + fn git_status(&self) -> Cell { + 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 } + } } /// 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..f103699 100644 --- a/src/options.rs +++ b/src/options.rs @@ -265,6 +265,10 @@ fn columns(matches: &getopts::Matches) -> Result, Misfire> { columns.push(Group); } + if cfg!(feature="git") { + columns.push(GitStatus); + } + columns.push(FileName); Ok(columns) }