From 90d4684de4d64fadcd4025c2c86e5c82857f6cd0 Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 27 Jan 2015 15:01:17 +0000 Subject: [PATCH] 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) } -- GitLab