Skip to content

Commit

Permalink
Implement --diff-args
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed May 7, 2024
1 parent 97839c6 commit c6fdfb9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -1101,12 +1111,10 @@ pub struct Opt {
/// Deprecated: use --true-color.
pub _24_bit_color: Option<String>,

/// 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<PathBuf>,

/// 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<PathBuf>,

#[arg(skip)]
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct Config {
pub cwd_relative_to_repo_root: Option<String>,
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,
Expand Down Expand Up @@ -294,6 +295,7 @@ impl From<cli::Opt> 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,
Expand Down
1 change: 1 addition & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions src/subcommands/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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();
Expand Down

0 comments on commit c6fdfb9

Please sign in to comment.