Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle quoted filenames in diff header #1222

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,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 @@ -382,13 +391,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 @@ -551,6 +562,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