From 38c6718c86086ec2f8e57bf73dcf76a33eb901fc Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Mon, 11 Jul 2022 21:38:36 -0700 Subject: [PATCH] Improve handling of /dev/null paths when there are two CLI argsuments When git calls us, we always know the file name. If we're called with two arguments and one is /dev/null, use the other for language detection and display. --- CHANGELOG.md | 5 +++++ src/display/style.rs | 17 +++++++++++------ src/main.rs | 10 ++++------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b0efff67..e550835d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ TypeScript. Fixed an issue with inline mode where there was a blank line between every line. +### Command Line Interface + +Fixed language detection and filename display when one path was +`/dev/null` and difftastic was invoked with two arguments. + ## 0.30 (released 4th July 2022) ### Parsing diff --git a/src/display/style.rs b/src/display/style.rs index 40be8f783..1a9bdd200 100644 --- a/src/display/style.rs +++ b/src/display/style.rs @@ -380,19 +380,24 @@ pub fn header( display_options.use_color, display_options.background_color, ); + let lhs_path_pretty = apply_header_color( + lhs_display_path, + display_options.use_color, + display_options.background_color, + ); if hunk_num == 1 && lhs_display_path != rhs_display_path && display_options.in_vcs { - let lhs_path_pretty = apply_header_color( - lhs_display_path, - display_options.use_color, - display_options.background_color, - ); let renamed = format!("Renamed {} to {}", lhs_path_pretty, rhs_path_pretty,); format!( "{}\n{} --- {}{}", renamed, rhs_path_pretty, divider, language_name ) } else { - format!("{} --- {}{}", rhs_path_pretty, divider, language_name) + let path_pretty = if lhs_display_path == "/dev/null" { + rhs_path_pretty + } else { + lhs_path_pretty + }; + format!("{} --- {}{}", path_pretty, divider, language_name) } } diff --git a/src/main.rs b/src/main.rs index dbb6ba625..476493a18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -242,16 +242,14 @@ fn diff_file_content( rhs_src.pop(); } - // TODO: take a Path directly instead. - let guess_path = Path::new(&rhs_display_path); - // Take the larger of the two files when guessing the // language. This is useful when we've added or removed a whole // file. - let guess_src = if lhs_src.len() > rhs_src.len() { - &lhs_src + let (guess_src, guess_path) = if lhs_src.len() > rhs_src.len() { + // TODO: take a Path directly instead. + (&lhs_src, Path::new(&lhs_display_path)) } else { - &rhs_src + (&rhs_src, Path::new(&rhs_display_path)) }; let language = language_override.or_else(|| guess(guess_path, guess_src)); let lang_config = language.map(tsp::from_language);