From ebcd9467329e87228e0136482e94028ce025079c Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 10 Oct 2021 00:03:07 -0700 Subject: [PATCH] cli: make default diff format configurable This change adds a `diff.format` config option, which can be set to "git", "color-words", or "summary". Closes #33. --- src/commands.rs | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 7f5492092..a6873fdea 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -652,6 +652,13 @@ With the `--from` and/or `--to` options, shows the difference from/to the given .conflicts_with("summary") .help("Show a Git-format diff"), ) + .arg( + Arg::with_name("color-words") + .long("color-words") + .conflicts_with("summary") + .conflicts_with("git") + .help("Show a word-level diff with changes indicated only by color"), + ) .arg( Arg::with_name("revision") .long("revision") @@ -1481,13 +1488,38 @@ fn cmd_diff( let repo = repo_command.repo(); let matcher = matcher_from_values(ui, repo.working_copy_path(), sub_matches.values_of("paths"))?; - if sub_matches.is_present("summary") { - let summary = from_tree.diff_summary(&to_tree, matcher.as_ref()); - show_diff_summary(ui, repo.working_copy_path(), &summary)?; - } else if sub_matches.is_present("git") { - show_git_diff(ui, repo, from_tree.diff(&to_tree, matcher.as_ref()))?; - } else { - show_diff(ui, repo, from_tree.diff(&to_tree, matcher.as_ref()))?; + enum Format { + Summary, + Git, + ColorWords, + } + let format = { + if sub_matches.is_present("summary") { + Format::Summary + } else if sub_matches.is_present("git") { + Format::Git + } else if sub_matches.is_present("color-words") { + Format::ColorWords + } else { + match ui.settings().config().get_str("diff.format") { + Ok(value) if &value == "summary" => Format::Summary, + Ok(value) if &value == "git" => Format::Git, + Ok(value) if &value == "color-words" => Format::ColorWords, + _ => Format::ColorWords, + } + } + }; + match format { + Format::Summary => { + let summary = from_tree.diff_summary(&to_tree, matcher.as_ref()); + show_diff_summary(ui, repo.working_copy_path(), &summary)?; + } + Format::Git => { + show_git_diff(ui, repo, from_tree.diff(&to_tree, matcher.as_ref()))?; + } + Format::ColorWords => { + show_diff(ui, repo, from_tree.diff(&to_tree, matcher.as_ref()))?; + } } Ok(()) }