Merge branch 'git-status'

This commit is contained in:
Ben S 2015-01-28 15:55:46 +00:00
commit e86f0e12bf
8 changed files with 234 additions and 4 deletions

91
Cargo.lock generated
View File

@ -4,6 +4,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"ansi_term 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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]] [[package]]
name = "getopts" name = "getopts"
version = "0.1.4" version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "natord" name = "natord"
version = "1.0.6" version = "1.0.6"
@ -33,6 +91,25 @@ name = "number_prefix"
version = "0.2.1" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "regex" name = "regex"
version = "0.1.10" version = "0.1.10"
@ -46,6 +123,20 @@ dependencies = [
"regex 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "users" name = "users"
version = "0.2.1" version = "0.2.1"

View File

@ -12,3 +12,11 @@ getopts = "0.1.4"
natord = "1.0.6" natord = "1.0.6"
number_prefix = "0.2.1" number_prefix = "0.2.1"
users = "0.2.1" users = "0.2.1"
[features]
default = [ "git" ]
git = [ "git2" ]
[dependencies.git2]
version = "0.1.12"
optional = true

View File

@ -31,3 +31,5 @@ You can sort by **name**, **size**, **ext**, **inode**, or **none**.
## Installation ## 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 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.

View File

@ -12,6 +12,8 @@ pub enum Column {
Group, Group,
HardLinks, HardLinks,
Inode, Inode,
GitStatus,
} }
#[derive(PartialEq, Debug, Copy)] #[derive(PartialEq, Debug, Copy)]
@ -37,6 +39,7 @@ impl Column {
Column::HardLinks => Alignment::Right, Column::HardLinks => Alignment::Right,
Column::Inode => Alignment::Right, Column::Inode => Alignment::Right,
Column::Blocks => Alignment::Right, Column::Blocks => Alignment::Right,
Column::GitStatus => Alignment::Right,
_ => Alignment::Left, _ => Alignment::Left,
} }
} }
@ -53,6 +56,7 @@ impl Column {
Column::Group => "Group", Column::Group => "Group",
Column::HardLinks => "Links", Column::HardLinks => "Links",
Column::Inode => "inode", Column::Inode => "inode",
Column::GitStatus => "Git",
} }
} }
} }

View File

@ -1,5 +1,9 @@
use std::io::{fs, IoResult}; 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 /// A **Dir** provides a cached list of the file paths in a directory that's
/// being listed. /// being listed.
@ -8,8 +12,9 @@ use file::File;
/// check the existence of surrounding files, then highlight themselves /// check the existence of surrounding files, then highlight themselves
/// accordingly. (See `File#get_source_files`) /// accordingly. (See `File#get_source_files`)
pub struct Dir { pub struct Dir {
pub contents: Vec<Path>, contents: Vec<Path>,
pub path: Path, path: Path,
git: Option<Git>,
} }
impl Dir { impl Dir {
@ -20,6 +25,7 @@ impl Dir {
fs::readdir(&path).map(|paths| Dir { fs::readdir(&path).map(|paths| Dir {
contents: paths, contents: paths,
path: path.clone(), path: path.clone(),
git: Git::scan(&path).ok(),
}) })
} }
@ -42,4 +48,106 @@ impl Dir {
pub fn contains(&self, path: &Path) -> bool { pub fn contains(&self, path: &Path) -> bool {
self.contents.contains(path) 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<u8>, 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<Git, git2::Error> {
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<Git, ()> {
// 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!");
}
} }

View File

@ -79,6 +79,7 @@ impl<'a> File<'a> {
Blocks => self.blocks(), Blocks => self.blocks(),
User => self.user(users_cache), User => self.user(users_cache),
Group => self.group(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) { if let Ok(path) = fs::readlink(&self.path) {
let target_path = match self.dir { let target_path = match self.dir {
Some(dir) => dir.path.join(path), Some(dir) => dir.join(path),
None => path, None => path,
}; };
@ -369,6 +370,15 @@ impl<'a> File<'a> {
vec![] // No source files if there's no extension, either! 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. /// Extract an extension from a string, if one is present, in lowercase.

View File

@ -5,6 +5,9 @@ extern crate number_prefix;
extern crate unicode; extern crate unicode;
extern crate users; extern crate users;
#[cfg(feature="git")]
extern crate git2;
use std::io::FileType; use std::io::FileType;
use std::io::fs; use std::io::fs;
use std::os::{args, set_exit_status}; use std::os::{args, set_exit_status};

View File

@ -265,6 +265,10 @@ fn columns(matches: &getopts::Matches) -> Result<Vec<Column>, Misfire> {
columns.push(Group); columns.push(Group);
} }
if cfg!(feature="git") {
columns.push(GitStatus);
}
columns.push(FileName); columns.push(FileName);
Ok(columns) Ok(columns)
} }