Skip to content

Commit

Permalink
Only strip expected git-added "a/" and "b/" prefixes. (#124)
Browse files Browse the repository at this point in the history
Fixes #120
  • Loading branch information
dandavison committed Apr 17, 2020
1 parent a53ef55 commit 0f7d484
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ pub fn get_file_path_from_file_meta_line(line: &str, git_diff_name: bool) -> Str
} else {
match line.split(' ').nth(1) {
Some("/dev/null") => "/dev/null",
Some(path) => {
if git_diff_name {
&path[2..]
} else {
path.split('\t').next().unwrap_or("")
}
Some(path) if git_diff_name && (path.starts_with("a/") || path.starts_with("b/")) => {
&path[2..]
}
Some(path) if git_diff_name => path,
Some(path) => path.split('\t').next().unwrap_or(""),
_ => "",
}
.to_string()
Expand Down Expand Up @@ -119,6 +117,20 @@ mod tests {
);
}

// We should only strip the prefixes if they are "a/" or "b/". This will be correct except for
// the case of a user with `diff.noprefix = true` who has directories named "a" or "b", which
// is an irresolvable ambiguity. Ideally one would only strip the prefixes if we have confirmed
// that we are looking at something like
//
// --- a/src/parse.rs
// +++ b/src/parse.rs
//
// as opposed to something like
//
// --- a/src/parse.rs
// +++ sibling_of_a/src/parse.rs
//
// but we don't attempt that currently.
#[test]
fn test_get_file_path_from_git_file_meta_line() {
assert_eq!(
Expand All @@ -129,8 +141,20 @@ mod tests {
get_file_path_from_file_meta_line("+++ b/src/delta.rs", true),
"src/delta.rs"
);
assert_eq!(
get_file_path_from_file_meta_line("--- src/delta.rs", true),
"src/delta.rs"
);
assert_eq!(
get_file_path_from_file_meta_line("+++ src/delta.rs", true),
"src/delta.rs"
);
}

// We should not strip the prefix unless it's If the user has `diff.noprefix = true` then
#[test]
fn test_get_file_path_from_git_file_meta_line_under_diff_noprefix() {}

#[test]
fn test_get_file_path_from_file_meta_line() {
assert_eq!(
Expand Down

0 comments on commit 0f7d484

Please sign in to comment.