summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2018-05-06 11:04:44 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2018-05-06 11:22:48 +0200
commit3eb79d63ceda959a7b2c1ae46ea34593656d1488 (patch)
treed313086744ca2e4db2e7639786abf1757824532d
parent04834077753e2fedc476f13d3fe1cd494aa4e93b (diff)
Compute pathspec from absolute.strip_prefix(workdir)
-rw-r--r--src/main.rs14
1 files changed, 5 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 8e3f4d46..7a314126 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -230,16 +230,12 @@ fn print_file<P: AsRef<Path>>(
}
fn get_git_diff(filename: &str) -> Option<LineChanges> {
- let repo = Repository::discover(Path::new(&filename)).ok()?;
- let workdir = repo.workdir()?;
- let absolute_file_path = workdir.join(Path::new(&filename));
- let relative_file_path = absolute_file_path.strip_prefix(workdir).ok()?;
+ let repo = Repository::discover(&filename).ok()?;
+ let path_absolute = fs::canonicalize(&filename).ok()?;
+ let path_relative_to_repo = path_absolute.strip_prefix(repo.workdir()?).ok()?;
let mut diff_options = DiffOptions::new();
- let pathspec = format!("*{}", relative_file_path.display())
- .into_c_string()
- .ok()?;
- // GIT pathspec uses relative path
+ let pathspec = path_relative_to_repo.into_c_string().ok()?;
diff_options.pathspec(pathspec);
diff_options.context_lines(0);
@@ -261,7 +257,7 @@ fn get_git_diff(filename: &str) -> Option<LineChanges> {
Some(&mut |delta, hunk| {
let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));
- if relative_file_path != path {
+ if path_relative_to_repo != path {
return false;
}