[PATCH] diff-highlight: support range-diff output
- From
- gremat via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 24, 2026, 08:49 UTC
- Message-ID
- <pull.2320.git.1795510197963.gitgitgadget@gmail.com>
From: gremat <gremat@nextron-systems.com>
The output of "git range-diff" is a diff of diffs: each line carries an outer marker (how the patch changed between versions) followed by an inner marker (what the patch itself does), and the whole thing is indented by four columns below a commit pair header such as
1: abc1234 ! 1: def5678 subject
diff-highlight currently does not highlight anything in such output. It neither strips the indentation nor recognizes the "@@" hunk header that range-diff colors differently, and even when a removed/added pair is found, is_pair_interesting() rejects it because the common prefix contains the second marker.
Teach diff-highlight to:
- recognize the range-diff commit pair header, and strip the
four-column indentation of the following lines the same way we
strip a --graph prefix; - accept color codes between "@@" and the following space when
looking for a hunk header; - accept either the outer or the inner marker as the one forming a
removed/added pair in is_pair_interesting(); - pair up lines that share the same outer marker but form a
removed/added pair in the inner diff (e.g. "+-old" and "++new",
when a new version of a patch starts modifying a line), falling
back to that when the outer hunk cannot be paired line by line.Apart from the more lenient hunk header match, non-range-diff input is not affected, as the new behavior is guarded by the range-diff header having been seen.
Signed-off-by: gremat <gremat@nextron-systems.com>
---
diff-highlight: support range-diff outputPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2320%2Fgremat%2Ffeat%2Fdiff-highlight-support-range-diff-output-v1 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2320/gremat/feat/diff-highlight-support-range-diff-output-v1 Pull-Request: https://github.com/gitgitgadget/git/pull/2320
contrib/diff-highlight/DiffHighlight.pm | 77 ++++++++++++++++--- .../diff-highlight/t/t9400-diff-highlight.sh | 63 +++++++++++++++ 2 files changed, 129 insertions(+), 11 deletions(-)
diff --git a/contrib/diff-highlight/DiffHighlight.pm b/contrib/diff-highlight/DiffHighlight.pm index abe457882e..0e45d0fbf2 100644 --- a/contrib/diff-highlight/DiffHighlight.pm +++ b/contrib/diff-highlight/DiffHighlight.pm @@ -21,8 +21,10 @@ my $BORING = qr/$COLOR|\s/; my @removed; my @added; +my @context; my $in_hunk; my $graph_indent = 0; +my $range_diff; our $line_cb = sub { print @_ }; our $flush_cb = sub { local $| = 1 }; @@ -72,10 +74,20 @@ sub handle_line { # queued. flush(); $graph_indent = visible_width($graph_prefix); + $range_diff = 0; + + # match a range-diff commit pair header like "1: abc1234 ! 1: def5678"; + # the diff-of-diffs that follows is indented by 4 columns, which we + # strip just like a graph prefix + } elsif (/^$COLOR*\s*(?:-+|\d+):\s+(?:-+|[0-9a-f]+)$BORING*[<>=!]$BORING*(?:-+|\d+):/) { + flush(); + $graph_indent = 4; + $range_diff = 1; } elsif ($graph_indent) { if (length($_) < $graph_indent) { $graph_indent = 0; + $range_diff = 0; } else { $_ = visible_substr($_, $graph_indent); } @@ -83,14 +95,22 @@ sub handle_line { if (!$in_hunk) { $line_cb->($orig); - $in_hunk = /^$COLOR*\@\@ /; + $in_hunk = /^$COLOR*\@\@$COLOR* /; } elsif (/^$COLOR*-/) { + flush() if @context; push @removed, $orig; } elsif (/^$COLOR*\+/) { + flush() if @context; push @added, $orig; } + # in range-diff output, an unchanged line of the inner diff may still be + # part of an inner removed/added pair + elsif ($range_diff && /^$COLOR* $COLOR*[-+]/) { + flush() if @removed || @added; + push @context, $orig; + } else { flush(); $line_cb->($orig); @@ -114,8 +134,34 @@ sub flush { # Flush any queued hunk (this can happen when there is no trailing # context in the final diff of the input). show_hunk(\@removed, \@added); + show_inner(\@context); @removed = (); @added = (); + @context = (); +} + +# In range-diff output, lines with the same outer marker can still form a +# removed/added pair in the inner diff, e.g. "+-old" and "++new". Pair those +# up among the given lines, which all share one outer marker. +sub show_inner { + my (@rm, @add); + for my $line (@{$_[0]}) { + local $_ = visible_substr($line, $graph_indent); + if (/^$COLOR*[-+ ]$COLOR*-/) { + if (@add) { + show_hunk(\@rm, \@add, 1); + @rm = @add = (); + } + push @rm, $line; + } elsif (/^$COLOR*[-+ ]$COLOR*\+/) { + push @add, $line; + } else { + show_hunk(\@rm, \@add, 1); + @rm = @add = (); + $line_cb->($line); + } + } + show_hunk(\@rm, \@add, 1); } sub highlight_stdin { @@ -149,20 +195,23 @@ sub color_config { } sub show_hunk { - my ($a, $b) = @_; + my ($a, $b, $inner) = @_; # If one side is empty, then there is nothing to compare or highlight. - if (!@$a || !@$b) { - $line_cb->(@$a, @$b); - return; - } - + # # If we have mismatched numbers of lines on each side, we could try to # be clever and match up similar lines. But for now we are simple and # stupid, and only handle multi-line hunks that remove and add the same # number of lines. - if (@$a != @$b) { - $line_cb->(@$a, @$b); + # + # For range-diff, fall back to pairing lines within the inner diff. + if (!@$a || !@$b || @$a != @$b) { + if ($range_diff && !$inner) { + show_inner($a); + show_inner($b); + } else { + $line_cb->(@$a, @$b); + } return; } @@ -319,8 +368,14 @@ sub is_pair_interesting { my $suffix_a = join('', @$a[($sa+1)..$#$a]); my $suffix_b = join('', @$b[($sb+1)..$#$b]); - return visible_substr($prefix_a, $graph_indent) !~ /^$COLOR*-$BORING*$/ || - visible_substr($prefix_b, $graph_indent) !~ /^$COLOR*\+$BORING*$/ || + # range-diff lines carry two markers (outer and inner); either may be + # the one that makes this a removed/added pair + my ($rm, $add) = $range_diff ? + (qr/-$COLOR*[-+ ]|[-+ ]$COLOR*-/, qr/\+$COLOR*[-+ ]|[-+ ]$COLOR*\+/) : + (qr/-/, qr/\+/); + + return visible_substr($prefix_a, $graph_indent) !~ /^$COLOR*(?:$rm)$BORING*$/ || + visible_substr($prefix_b, $graph_indent) !~ /^$COLOR*(?:$add)$BORING*$/ || $suffix_a !~ /^$BORING*$/ || $suffix_b !~ /^$BORING*$/; } diff --git a/contrib/diff-highlight/t/t9400-diff-highlight.sh b/contrib/diff-highlight/t/t9400-diff-highlight.sh index b38fe2196a..ada90a5666 100755 --- a/contrib/diff-highlight/t/t9400-diff-highlight.sh +++ b/contrib/diff-highlight/t/t9400-diff-highlight.sh @@ -378,4 +378,67 @@ test_expect_success 'configure normal/highlight colors' ' EOF ' +dh_range_diff_setup () { + git checkout --orphan rd-base && + git rm -rf --cached . >/dev/null && + test_write_lines 1 2 3 4 5 6 >file && + git add file && + git commit -m base && + + git checkout -b rd-v1 && + test_write_lines 1 "2 x" 3 4 5 6 >file && + git commit -a -m change && + + git checkout -b rd-v2 rd-base && + test_write_lines 1 "2 x" 3 "4 old" 5 6 >file && + git commit -a -m change && + + git checkout -b rd-v3 rd-base && + test_write_lines 1 "2 x" 3 "4 new" 5 6 >file && + git commit -a -m change +} + +test_expect_success 'diff-highlight highlights range-diff pair across versions' ' + dh_range_diff_setup && + git range-diff --creation-factor=999 rd-base rd-v2 rd-v3 | + "$DIFF_HIGHLIGHT" >actual.raw && + sed -n "/^ *@@ file/,\$p" actual.raw | test_decode_color >actual && + cat >expect <<-\EOF && + @@ file + +2 x + 3 + -4 + -+4 <REVERSE>old<NOREVERSE> + ++4 <REVERSE>new<NOREVERSE> + 5 + 6 + EOF + test_cmp expect actual +' + +test_expect_success 'diff-highlight highlights range-diff pair within one version' ' + git range-diff --creation-factor=999 rd-base rd-v1 rd-v2 | + "$DIFF_HIGHLIGHT" >actual.raw && + sed -n "/^ *@@ file/,\$p" actual.raw | test_decode_color >actual && + cat >expect <<-\EOF && + @@ file + -2<REVERSE><NOREVERSE> + +2<REVERSE> x<NOREVERSE> + 3 + - 4 + +-4<REVERSE><NOREVERSE> + ++4<REVERSE> old<NOREVERSE> + 5 + + 6 + EOF + test_cmp expect actual +' + +test_expect_success 'diff-highlight highlights colored range-diff' ' + git range-diff --color --creation-factor=999 rd-base rd-v2 rd-v3 | + "$DIFF_HIGHLIGHT" | test_decode_color >actual && + grep "+4 <REVERSE>old<NOREVERSE>" actual && + grep "+4 <REVERSE>new<NOREVERSE>" actual +' + test_done base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
-- gitgitgadget