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;
|
|
|
|
|
2016-04-16 17:59:25 +00:00
|
|
|
use fs::fields as f;
|
2015-03-26 00:37:12 +00:00
|
|
|
|
2015-06-08 20:33:39 +00:00
|
|
|
|
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> {
|
2017-08-19 09:06:07 +00:00
|
|
|
info!("Scanning for Git repository under {:?}", path);
|
2017-08-26 22:53:47 +00:00
|
|
|
|
2017-03-26 16:35:50 +00:00
|
|
|
let repo = 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
|
|
|
|
};
|
|
|
|
|
2017-03-26 16:35:50 +00:00
|
|
|
let statuses = repo.statuses(None)?.iter()
|
2017-08-26 22:53:47 +00:00
|
|
|
.map(|e| (workdir.join(Path::new(e.path().unwrap())), e.status()))
|
|
|
|
.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.
|
2016-04-16 17:59:25 +00:00
|
|
|
pub fn status(&self, path: &Path) -> f::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 {
|
2016-04-16 17:59:25 +00:00
|
|
|
Some(&(_, s)) => f::Git { staged: index_status(s), unstaged: working_tree_status(s) },
|
|
|
|
None => f::Git { staged: f::GitStatus::NotModified, unstaged: f::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.
|
2016-04-16 17:59:25 +00:00
|
|
|
pub fn dir_status(&self, dir: &Path) -> f::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);
|
|
|
|
|
2016-04-16 17:59:25 +00:00
|
|
|
f::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.
|
2016-04-16 17:59:25 +00:00
|
|
|
fn working_tree_status(status: git2::Status) -> f::GitStatus {
|
2015-05-11 22:28:01 +00:00
|
|
|
match status {
|
2016-04-16 17:59:25 +00:00
|
|
|
s if s.contains(git2::STATUS_WT_NEW) => f::GitStatus::New,
|
|
|
|
s if s.contains(git2::STATUS_WT_MODIFIED) => f::GitStatus::Modified,
|
|
|
|
s if s.contains(git2::STATUS_WT_DELETED) => f::GitStatus::Deleted,
|
|
|
|
s if s.contains(git2::STATUS_WT_RENAMED) => f::GitStatus::Renamed,
|
|
|
|
s if s.contains(git2::STATUS_WT_TYPECHANGE) => f::GitStatus::TypeChange,
|
|
|
|
_ => f::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.
|
2016-04-16 17:59:25 +00:00
|
|
|
fn index_status(status: git2::Status) -> f::GitStatus {
|
2015-05-11 22:28:01 +00:00
|
|
|
match status {
|
2016-04-16 17:59:25 +00:00
|
|
|
s if s.contains(git2::STATUS_INDEX_NEW) => f::GitStatus::New,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_MODIFIED) => f::GitStatus::Modified,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_DELETED) => f::GitStatus::Deleted,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_RENAMED) => f::GitStatus::Renamed,
|
|
|
|
s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => f::GitStatus::TypeChange,
|
|
|
|
_ => f::GitStatus::NotModified,
|
2015-03-26 00:37:12 +00:00
|
|
|
}
|
|
|
|
}
|