Skip to content

Commit

Permalink
Handle quoted filenames in diff header
Browse files Browse the repository at this point in the history
Otherwise the file extension would return 'rs"' which does not
match any language.
  • Loading branch information
th1000s authored and dandavison committed Nov 15, 2022
1 parent b5ed58d commit afa7a1a
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ fn get_repeated_file_path_from_diff_line(line: &str) -> Option<String> {
None
}

fn remove_surrounding_quotes(path: &str) -> &str {
if path.starts_with('"') && path.ends_with('"') {
// Indexing into the UTF-8 string is safe because of the previous test
&path[1..path.len() - 1]
} else {
path
}
}

fn _parse_file_path(s: &str, git_diff_name: bool) -> String {
// It appears that, if the file name contains a space, git appends a tab
// character in the diff metadata lines, e.g.
Expand All @@ -380,13 +389,15 @@ fn _parse_file_path(s: &str, git_diff_name: bool) -> String {
// index·d00491f..0cfbf08·100644␊
// ---·a/a·b├──┤␊
// +++·b/c·d├──┤␊
match s.strip_suffix('\t').unwrap_or(s) {
let path = match s.strip_suffix('\t').unwrap_or(s) {
path if path == "/dev/null" => "/dev/null",
path if git_diff_name && DIFF_PREFIXES.iter().any(|s| path.starts_with(s)) => &path[2..],
path if git_diff_name => path,
path => path.split('\t').next().unwrap_or(""),
}
.to_string()
};
// When a path contains non-ASCII characters, a backslash, or a quote then it is quoted,
// so remove these quotes. Characters may also be escaped, but these are left as-is.
remove_surrounding_quotes(path).to_string()
}

pub fn get_file_change_description_from_file_paths(
Expand Down Expand Up @@ -549,6 +560,11 @@ mod tests {
parse_diff_header_line("+++ src/delta.rs", true),
("src/delta.rs".to_string(), FileEvent::Change)
);

assert_eq!(
parse_diff_header_line("+++ \".\\delta.rs\"", true),
(".\\delta.rs".to_string(), FileEvent::Change)
);
}

#[test]
Expand Down

0 comments on commit afa7a1a

Please sign in to comment.