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