From 3eb79d63ceda959a7b2c1ae46ea34593656d1488 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 6 May 2018 11:04:44 +0200 Subject: [PATCH] Compute pathspec from absolute.strip_prefix(workdir) --- src/main.rs | 14 +++++--------- 1 file 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>( } fn get_git_diff(filename: &str) -> Option { - 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 { 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; }