Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-03-26 00:37:12 +00:00
|
|
|
|
|
|
|
use git2;
|
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
use file;
|
2015-03-26 00:37:12 +00:00
|
|
|
|
|
|
|
/// Container of Git statuses for all the files in this folder's Git repository.
|
|
|
|
pub struct Git {
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
statuses: Vec<(PathBuf, git2::Status)>,
|
2015-03-26 00:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Git {
|
|
|
|
|
|
|
|
/// Discover a Git repository on or above this directory, scanning it for
|
|
|
|
/// the files' statuses if one is found.
|
|
|
|
pub fn scan(path: &Path) -> Result<Git, git2::Error> {
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
let repo = try!(git2::Repository::discover(path));
|
2015-03-26 00:37:12 +00:00
|
|
|
let workdir = match repo.workdir() {
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
Some(w) => w,
|
2015-03-26 00:37:12 +00:00
|
|
|
None => return Ok(Git { statuses: vec![] }), // bare repo
|
|
|
|
};
|
|
|
|
|
|
|
|
let statuses = try!(repo.statuses(None)).iter()
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
.map(|e| (workdir.join(Path::new(e.path().unwrap())), e.status()))
|
2015-03-26 00:37:12 +00:00
|
|
|
.collect();
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
|
2015-03-26 00:37:12 +00:00
|
|
|
Ok(Git { statuses: statuses })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the status for the file at the given path, if present.
|
2015-05-11 22:28:01 +00:00
|
|
|
pub fn status(&self, path: &Path) -> file::Git {
|
2015-03-26 00:37:12 +00:00
|
|
|
let status = self.statuses.iter()
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
.find(|p| p.0.as_path() == path);
|
2015-03-26 00:37:12 +00:00
|
|
|
match status {
|
2015-05-11 22:28:01 +00:00
|
|
|
Some(&(_, s)) => file::Git { staged: index_status(s), unstaged: working_tree_status(s) },
|
|
|
|
None => file::Git { staged: file::GitStatus::NotModified, unstaged: file::GitStatus::NotModified }
|
2015-03-26 00:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2015-05-11 22:28:01 +00:00
|
|
|
pub fn dir_status(&self, dir: &Path) -> file::Git {
|
2015-03-26 00:37:12 +00:00
|
|
|
let s = self.statuses.iter()
|
Use new io + path + fs libraries (LOTS OF CHANGES)
Exa now uses the new IO, Path, and Filesystem libraries that have been out for a while now.
Unfortunately, the new libraries don't *entirely* cover the range of the old libraries just yet: in particular, to become more cross-platform, the data in `UnstableFileStat` isn't available in the Unix `MetadataExt` yet. Much of this is contained in rust-lang/rfcs#1044 (which is due to be implemented in rust-lang/rust#14711), but it's not *entirely* there yet.
As such, this commits a serious loss of functionality: no symlink viewing, no hard links or blocks, or users or groups. Also, some of the code could now be optimised. I just wanted to commit this to sort out most of the 'teething problems' of having a different path system in advance.
Here's an example problem that took ages to fix for you, just because you read this far: when I first got exa to compile, it worked mostly fine, except calling `exa` by itself didn't list the current directory. I traced where the command-line options were being generated, to where files and directories were sorted, to where the threads were spawned... and the problem turned out to be that it was using the full path as the file name, rather than just the last component, and these paths happened to begin with `.`, so it thought they were dotfiles.
2015-04-23 12:00:34 +00:00
|
|
|
.filter(|p| p.0.starts_with(dir))
|
2015-03-26 00:37:12 +00:00
|
|
|
.fold(git2::Status::empty(), |a, b| a | b.1);
|
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
file::Git { staged: index_status(s), unstaged: working_tree_status(s) }
|
2015-03-26 00:37:12 +00:00
|
|
|
}
|
2015-05-11 22:28:01 +00:00
|
|
|
}
|
2015-03-26 00:37:12 +00:00
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
/// The character to display if the file has been modified, but not staged.
|
|
|
|
fn working_tree_status(status: git2::Status) -> file::GitStatus {
|
|
|
|
match status {
|
|
|
|
s if s.contains(git2::STATUS_WT_NEW) => file::GitStatus::New,
|
|
|
|
s if s.contains(git2::STATUS_WT_MODIFIED) => file::GitStatus::Modified,
|
|
|
|
s if s.contains(git2::STATUS_WT_DELETED) => file::GitStatus::Deleted,
|
|
|
|
s if s.contains(git2::STATUS_WT_RENAMED) => file::GitStatus::Renamed,
|
|
|
|
s if s.contains(git2::STATUS_WT_TYPECHANGE) => file::GitStatus::TypeChange,
|
|
|
|
_ => file::GitStatus::NotModified,
|
2015-03-26 00:37:12 +00:00
|
|
|
}
|
2015-05-11 22:28:01 +00:00
|
|
|
}
|
2015-03-26 00:37:12 +00:00
|
|
|
|
2015-05-11 22:28:01 +00:00
|
|
|
/// The character to display if the file has been modified, and the change
|
|
|
|
/// has been staged.
|
|
|
|
fn index_status(status: git2::Status) -> file::GitStatus {
|
|
|
|
match status {
|
|
|
|
s if s.contains(git2::STATUS_INDEX_NEW) => file::GitStatus::New,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_MODIFIED) => file::GitStatus::Modified,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_DELETED) => file::GitStatus::Deleted,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_RENAMED) => file::GitStatus::Renamed,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => file::GitStatus::TypeChange,
|
|
|
|
_ => file::GitStatus::NotModified,
|
2015-03-26 00:37:12 +00:00
|
|
|
}
|
|
|
|
}
|