From c6fdfb9f567f60bd5b8cf30b48c742b5a3f27ac7 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 6 May 2024 22:30:04 -0400 Subject: [PATCH] Implement --diff-args --- src/cli.rs | 16 ++++++++++++---- src/config.rs | 2 ++ src/options/set.rs | 1 + src/subcommands/diff.rs | 10 ++++++---- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 723ea42c0..5f7fad6e9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -317,6 +317,16 @@ pub struct Opt { #[arg(long = "detect-dark-light", value_enum, default_value_t = DetectDarkLight::default())] pub detect_dark_light: DetectDarkLight, + #[arg(long = "diff-args", default_value = "", value_name = "STRING")] + /// Arguments to pass to `git diff` when using delta to diff two files. + /// + /// E.g. `delta --diff-args=-U999 file_1 file_2` is equivalent to + /// `git diff -U999 --no-index --color file_1 file_2 | delta`. + /// + /// However, if you use process substitution instead of real file paths, it falls back to `diff -u` instead of `git + /// diff`. + pub diff_args: String, + #[arg(long = "diff-highlight")] /// Emulate diff-highlight. /// @@ -1101,12 +1111,10 @@ pub struct Opt { /// Deprecated: use --true-color. pub _24_bit_color: Option, - /// First file to be compared when delta is being used in diff mode - /// - /// `delta file_1 file_2` is equivalent to `diff -u file_1 file_2 | delta`. + /// First file to be compared when delta is being used to diff two files. pub minus_file: Option, - /// Second file to be compared when delta is being used in diff mode. + /// Second file to be compared when delta is being used to diff two files. pub plus_file: Option, #[arg(skip)] diff --git a/src/config.rs b/src/config.rs index 767892890..fde08f14f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -53,6 +53,7 @@ pub struct Config { pub cwd_relative_to_repo_root: Option, pub decorations_width: cli::Width, pub default_language: String, + pub diff_args: String, pub diff_stat_align_width: usize, pub error_exit_code: i32, pub file_added_label: String, @@ -294,6 +295,7 @@ impl From for Config { cwd_relative_to_repo_root, decorations_width: opt.computed.decorations_width, default_language: opt.default_language, + diff_args: opt.diff_args, diff_stat_align_width: opt.diff_stat_align_width, error_exit_code: 2, // Use 2 for error because diff uses 0 and 1 for non-error. file_added_label, diff --git a/src/options/set.rs b/src/options/set.rs index f6d293e9b..e80cd2d29 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -140,6 +140,7 @@ pub fn set_options( commit_regex, commit_style, default_language, + diff_args, diff_stat_align_width, file_added_label, file_copied_label, diff --git a/src/subcommands/diff.rs b/src/subcommands/diff.rs index ddaf44d88..f432f4612 100644 --- a/src/subcommands/diff.rs +++ b/src/subcommands/diff.rs @@ -24,12 +24,14 @@ pub fn diff( |f: &Path| f.starts_with("/proc/self/fd/") || f.starts_with("/dev/fd/"); let diff_cmd = if via_process_substitution(minus_file) || via_process_substitution(plus_file) { - ["diff", "-u", "--"].as_slice() + format!("diff -u {} --", config.diff_args) } else { - ["git", "diff", "--no-index", "--color", "--"].as_slice() + format!("git diff --no-index --color {} --", config.diff_args) }; - let diff_bin = diff_cmd[0]; + let mut diff_cmd = diff_cmd.split_whitespace(); + let diff_bin = diff_cmd.next().unwrap(); + let diff_path = match grep_cli::resolve_binary(PathBuf::from(diff_bin)) { Ok(path) => path, Err(err) => { @@ -39,7 +41,7 @@ pub fn diff( }; let diff_process = process::Command::new(diff_path) - .args(&diff_cmd[1..]) + .args(diff_cmd) .args([minus_file, plus_file]) .stdout(process::Stdio::piped()) .spawn();