mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-29 15:13:54 +00:00
Preliminary Git support!
This is something that I've long wanted to add. It uses libgit2 as an optional dependency.
This commit is contained in:
parent
e835fe8fe3
commit
90d4684de4
91
Cargo.lock
generated
91
Cargo.lock
generated
@ -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"
|
||||
|
@ -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
|
@ -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",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
83
src/dir.rs
83
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: Path,
|
||||
git: Option<Git>,
|
||||
}
|
||||
|
||||
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<Git, git2::Error> {
|
||||
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<Git, ()> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
fn status(&self, _: &Path) -> String {
|
||||
panic!("Tried to access a Git repo without Git support!");
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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};
|
||||
|
@ -265,6 +265,7 @@ fn columns(matches: &getopts::Matches) -> Result<Vec<Column>, Misfire> {
|
||||
columns.push(Group);
|
||||
}
|
||||
|
||||
columns.push(GitStatus);
|
||||
columns.push(FileName);
|
||||
Ok(columns)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user