mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-24 22:48:37 +01:00
Compute pathspec from absolute.strip_prefix(workdir)
This commit is contained in:
parent
0483407775
commit
3eb79d63ce
14
src/main.rs
14
src/main.rs
@ -230,16 +230,12 @@ fn print_file<P: AsRef<Path>>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_git_diff(filename: &str) -> Option<LineChanges> {
|
fn get_git_diff(filename: &str) -> Option<LineChanges> {
|
||||||
let repo = Repository::discover(Path::new(&filename)).ok()?;
|
let repo = Repository::discover(&filename).ok()?;
|
||||||
let workdir = repo.workdir()?;
|
let path_absolute = fs::canonicalize(&filename).ok()?;
|
||||||
let absolute_file_path = workdir.join(Path::new(&filename));
|
let path_relative_to_repo = path_absolute.strip_prefix(repo.workdir()?).ok()?;
|
||||||
let relative_file_path = absolute_file_path.strip_prefix(workdir).ok()?;
|
|
||||||
|
|
||||||
let mut diff_options = DiffOptions::new();
|
let mut diff_options = DiffOptions::new();
|
||||||
let pathspec = format!("*{}", relative_file_path.display())
|
let pathspec = path_relative_to_repo.into_c_string().ok()?;
|
||||||
.into_c_string()
|
|
||||||
.ok()?;
|
|
||||||
// GIT pathspec uses relative path
|
|
||||||
diff_options.pathspec(pathspec);
|
diff_options.pathspec(pathspec);
|
||||||
diff_options.context_lines(0);
|
diff_options.context_lines(0);
|
||||||
|
|
||||||
@ -261,7 +257,7 @@ fn get_git_diff(filename: &str) -> Option<LineChanges> {
|
|||||||
Some(&mut |delta, hunk| {
|
Some(&mut |delta, hunk| {
|
||||||
let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));
|
let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));
|
||||||
|
|
||||||
if relative_file_path != path {
|
if path_relative_to_repo != path {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user