From 932255237a7e6dcd30edfcc36a9b842ecf2e741b Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Fri, 1 Mar 2024 15:43:24 +0100 Subject: [PATCH 1/2] t4034: extract out `diff_with_opts` The current `word_diff` function in t4034 uses the arguments provided to obtain the diff and compare them. Let's rename this to `diff_with_opts` and add a new function `word_diff` which is specific to the `--word-diff=` option. This function will act as a wrapper around `diff_with_opts`. In the following commit, when we introduce a config option for `--word-diff`, we'd want to test the existing tests also with the config option and at this point we can expand `word_diff` to test both config and command line options. Signed-off-by: Karthik Nayak --- t/t4034-diff-words.sh | 54 +++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index 74586f3813c..4f70aa6e9ff 100755 --- a/t/t4034-diff-words.sh +++ b/t/t4034-diff-words.sh @@ -51,7 +51,7 @@ cat >expect.non-whitespace-is-word <<-EOF aeff = aeff * ( aaa ) EOF -word_diff () { +diff_with_opts () { pre=$(git rev-parse --short $(git hash-object pre)) && post=$(git rev-parse --short $(git hash-object post)) && test_must_fail git diff --no-index "$@" pre post >output && @@ -60,6 +60,10 @@ word_diff () { test_cmp expected output.decrypted } +word_diff () { + diff_with_opts "--word-diff=$1" $(echo "$@" | cut -d' ' -s -f 2-) +} + test_language_driver () { lang=$1 test_expect_success "diff driver '$lang'" ' @@ -67,11 +71,11 @@ test_language_driver () { "$TEST_DIRECTORY/t4034/'"$lang"'/post" \ "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . && echo "* diff='"$lang"'" >.gitattributes && - word_diff --color-words + diff_with_opts --color-words ' test_expect_success "diff driver '$lang' in Islandic" ' LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \ - word_diff --color-words + diff_with_opts --color-words ' } @@ -81,7 +85,7 @@ test_expect_success setup ' git config diff.color.func magenta ' -test_expect_success 'set up pre and post with runs of whitespace' ' +test_expect_success 'setup pre and post with runs of whitespace' ' cp pre.simple pre && cp post.simple post ' @@ -101,9 +105,9 @@ test_expect_success 'word diff with runs of whitespace' ' aeff = aeff * ( aaa ) EOF - word_diff --color-words && - word_diff --word-diff=color && - word_diff --color --word-diff=color + diff_with_opts --color-words && + word_diff color && + diff_with_opts --color --word-diff=color ' test_expect_success '--word-diff=porcelain' ' @@ -127,7 +131,7 @@ test_expect_success '--word-diff=porcelain' ' +aeff = aeff * ( aaa ) ~ EOF - word_diff --word-diff=porcelain + word_diff porcelain ' test_expect_success '--word-diff=plain' ' @@ -145,8 +149,8 @@ test_expect_success '--word-diff=plain' ' {+aeff = aeff * ( aaa )+} EOF - word_diff --word-diff=plain && - word_diff --word-diff=plain --no-color + diff_with_opts --word-diff=plain && + word_diff plain --no-color ' test_expect_success '--word-diff=plain --color' ' @@ -164,7 +168,7 @@ test_expect_success '--word-diff=plain --color' ' {+aeff = aeff * ( aaa )+} EOF - word_diff --word-diff=plain --color + word_diff plain --color ' test_expect_success 'word diff without context' ' @@ -181,17 +185,17 @@ test_expect_success 'word diff without context' ' aeff = aeff * ( aaa ) EOF - word_diff --color-words --unified=0 + diff_with_opts --color-words --unified=0 ' test_expect_success 'word diff with a regular expression' ' cp expect.letter-runs-are-words expect && - word_diff --color-words="[a-z]+" + diff_with_opts --color-words="[a-z]+" ' test_expect_success 'word diff with zero length matches' ' cp expect.letter-runs-are-words expect && - word_diff --color-words="[a-z${LF}]*" + diff_with_opts --color-words="[a-z${LF}]*" ' test_expect_success 'set up a diff driver' ' @@ -204,12 +208,12 @@ test_expect_success 'set up a diff driver' ' test_expect_success 'option overrides .gitattributes' ' cp expect.letter-runs-are-words expect && - word_diff --color-words="[a-z]+" + diff_with_opts --color-words="[a-z]+" ' test_expect_success 'use regex supplied by driver' ' cp expect.non-whitespace-is-word expect && - word_diff --color-words + diff_with_opts --color-words ' test_expect_success 'set up diff.wordRegex option' ' @@ -218,7 +222,7 @@ test_expect_success 'set up diff.wordRegex option' ' test_expect_success 'command-line overrides config' ' cp expect.letter-runs-are-words expect && - word_diff --color-words="[a-z]+" + diff_with_opts --color-words="[a-z]+" ' test_expect_success 'command-line overrides config: --word-diff-regex' ' @@ -236,12 +240,12 @@ test_expect_success 'command-line overrides config: --word-diff-regex' ' {+aeff = aeff * ( aaa+} ) EOF - word_diff --color --word-diff-regex="[a-z]+" + diff_with_opts --color --word-diff-regex="[a-z]+" ' test_expect_success '.gitattributes override config' ' cp expect.non-whitespace-is-word expect && - word_diff --color-words + diff_with_opts --color-words ' test_expect_success 'setup: remove diff driver regex' ' @@ -263,7 +267,7 @@ test_expect_success 'use configured regex' ' aeff = aeff * ( aaa ) EOF - word_diff --color-words + diff_with_opts --color-words ' test_expect_success 'test parsing words for newline' ' @@ -279,7 +283,7 @@ test_expect_success 'test parsing words for newline' ' @@ -1 +1 @@ aaa (aaa) aaa EOF - word_diff --color-words="a+" + diff_with_opts --color-words="a+" ' test_expect_success 'test when words are only removed at the end' ' @@ -295,7 +299,7 @@ test_expect_success 'test when words are only removed at the end' ' @@ -1 +1 @@ (: EOF - word_diff --color-words=. + diff_with_opts --color-words=. ' test_expect_success '--word-diff=none' ' @@ -312,7 +316,7 @@ test_expect_success '--word-diff=none' ' -(: +( EOF - word_diff --word-diff=plain --word-diff=none + word_diff plain --word-diff=none ' test_expect_success 'unset default driver' ' @@ -363,7 +367,7 @@ test_expect_success 'word-diff with diff.sbe' ' [-b-]{+c+} EOF test_config diff.suppress-blank-empty true && - word_diff --word-diff=plain + word_diff plain ' test_expect_success 'word-diff with no newline at EOF' ' @@ -379,7 +383,7 @@ test_expect_success 'word-diff with no newline at EOF' ' @@ -1 +1 @@ a a [-a-]{+ab+} a a EOF - word_diff --word-diff=plain + word_diff plain ' test_expect_success 'setup history with two files' ' -- GitLab From 8c0b49440d3fa4de583abdb7a2f10c92280b6c5b Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Wed, 28 Feb 2024 15:23:25 +0100 Subject: [PATCH 2/2] diff: add 'diff.wordDiff' config option The git-diff(1) command supports the `--word-diff` which allows the users to specify how to delimit word diffs. Provide this option also as a config param 'diff.wordDiff'. Signed-off-by: Karthik Nayak --- Documentation/config/diff.txt | 4 +++ diff.c | 50 +++++++++++++++++++++++++++-------- t/t4034-diff-words.sh | 11 ++++++-- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Documentation/config/diff.txt b/Documentation/config/diff.txt index bd5ae0c3378..00459beee23 100644 --- a/Documentation/config/diff.txt +++ b/Documentation/config/diff.txt @@ -225,3 +225,7 @@ diff.colorMovedWS:: When moved lines are colored using e.g. the `diff.colorMoved` setting, this option controls the `` how spaces are treated for details of valid modes see '--color-moved-ws' in linkgit:git-diff[1]. + +diff.wordDiff:: + Show a word diff, using the `` to delimit changed words. + For details of valid modes see '--word-diff' in linkgit:git-diff[1]. diff --git a/diff.c b/diff.c index e50def45383..050d83ef858 100644 --- a/diff.c +++ b/diff.c @@ -69,6 +69,7 @@ static int diff_dirstat_permille_default = 30; static struct diff_options default_diff_options; static long diff_algorithm; static unsigned ws_error_highlight_default = WSEH_NEW; +static int diff_word_diff_default; static char diff_colors[][COLOR_MAXLEN] = { GIT_COLOR_RESET, @@ -209,6 +210,23 @@ int git_config_rename(const char *var, const char *value) return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; } +static int parse_word_diff_value(const char *value) +{ + if (!value) + return -1; + else if (!strcmp(value, "plain")) + return DIFF_WORDS_PLAIN; + else if (!strcmp(value, "color")) { + return DIFF_WORDS_COLOR; + } + else if (!strcmp(value, "porcelain")) + return DIFF_WORDS_PORCELAIN; + else if (!strcmp(value, "none")) + return DIFF_WORDS_NONE; + + return -1; +} + long parse_algorithm_value(const char *value) { if (!value) @@ -452,6 +470,18 @@ int git_diff_ui_config(const char *var, const char *value, return 0; } + if (!strcmp(var, "diff.worddiff")) { + if (!value) + return config_error_nonbool(var); + diff_word_diff_default = parse_word_diff_value(value); + if (diff_word_diff_default < 0) + return error(_("unknown value for config '%s': %s"), + var, value); + if (diff_word_diff_default == DIFF_WORDS_COLOR) + diff_use_color_default = 1; + return 0; + } + if (git_color_config(var, value, cb) < 0) return -1; @@ -4724,6 +4754,7 @@ void repo_diff_setup(struct repository *r, struct diff_options *options) options->use_color = diff_use_color_default; options->detect_rename = diff_detect_rename_default; options->xdl_opts |= diff_algorithm; + options->word_diff |= diff_word_diff_default; if (diff_indent_heuristic) DIFF_XDL_SET(options, INDENT_HEURISTIC); @@ -5504,21 +5535,18 @@ static int diff_opt_word_diff(const struct option *opt, const char *arg, int unset) { struct diff_options *options = opt->value; + int value; BUG_ON_OPT_NEG(unset); + if (arg) { - if (!strcmp(arg, "plain")) - options->word_diff = DIFF_WORDS_PLAIN; - else if (!strcmp(arg, "color")) { - options->use_color = 1; - options->word_diff = DIFF_WORDS_COLOR; - } - else if (!strcmp(arg, "porcelain")) - options->word_diff = DIFF_WORDS_PORCELAIN; - else if (!strcmp(arg, "none")) - options->word_diff = DIFF_WORDS_NONE; - else + value = parse_word_diff_value(arg); + if (value < 0) return error(_("bad --word-diff argument: %s"), arg); + if (value == DIFF_WORDS_COLOR) + options->use_color = 1; + + options->word_diff = value; } else { if (options->word_diff == DIFF_WORDS_NONE) options->word_diff = DIFF_WORDS_PLAIN; diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index 4f70aa6e9ff..2cc391c3394 100755 --- a/t/t4034-diff-words.sh +++ b/t/t4034-diff-words.sh @@ -56,12 +56,19 @@ diff_with_opts () { post=$(git rev-parse --short $(git hash-object post)) && test_must_fail git diff --no-index "$@" pre post >output && test_decode_color output.decrypted && - sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected + sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected && test_cmp expected output.decrypted } word_diff () { - diff_with_opts "--word-diff=$1" $(echo "$@" | cut -d' ' -s -f 2-) + # Capture the rest of the arguments to passthrough. + rest=$(echo "$@" | cut -d' ' -s -f 2-) && + # Test via the config route. + git config diff.wordDiff $1 && + diff_with_opts $rest && + git config --unset diff.wordDiff && + # Test via the command option route. + diff_with_opts "--word-diff=$1" $rest } test_language_driver () { -- GitLab