{"thread":{"id":"65286","subject":"[PATCH] contrib/diff-highlight: do not highlight identical pairs","startedAt":"2026-03-17T23:02:30Z","lastAt":"2026-03-17T23:02:30Z","messageCount":1,"participants":["Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"539270","messageId":"20260317230223.GA716496@coredump.intra.peff.net","threadId":"65286","inReplyTo":null,"subject":"[PATCH] contrib/diff-highlight: do not highlight identical pairs","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2026-03-17T23:02:23Z","receivedAt":"2026-03-17T23:02:30Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We pair lines for highlighting based on their position in the hunk. So\nwe should never see two identical lines paired, like:\n\n  -one\n  -two\n  +one\n  +something else\n\nwhich would pair -one/+one, because that implies that the diff could\neasily be shrunk by turning line \"one\" into context.\n\nBut there is (at least) one exception: removing a newline at the end of\na file will produce a diff like:\n\n  -foo\n  +foo\n  \\No newline at end of file\n\nAnd we will pair those two lines. As a result, we end up marking the\nwhole line, including the newline, as the shared prefix. And there's an\nempty suffix.\n\nThe most obvious bug here is that when we try to print the highlighted\nlines, we remove the trailing newline from the suffix, but do not bother\nwith the prefix (under the assumption that there had to be a difference\n_somewhere_ in the line, and thus the prefix would not eat all the way\nup to the newline). And so you get an extra line like:\n\n  -foo\n\n  +foo\n\n  \\No newline at end of file\n\nThis is obviously ugly, but also causes interactive.diffFilter to\n(rightly) complain that the input and output do not match their lines\n1-to-1.\n\nThis could easily be fixed by chomping the prefix, too, but I think the\nproblem is deeper. For one, I suspect some of the other logic gets\nconfused by forming an array with zero-indexed element \"3\" in a\n3-element array. But more importantly, we try not to highlight whole\nlines, as there's nothing interesting to show there. So let's catch this\nearly in is_pair_interesting() and bail to our usual passthrough\nstrategy.\n\nReported-by: Scott Baker <scott@perturb.org>\nSigned-off-by: Jeff King <peff@peff.net>\n---\nIt would perhaps make more sense for diff-highlight to chomp all\nincoming lines, then do its comparisons, and then add a newline back on\noutput. That's a bigger change, so I punted on it for now.\n\n contrib/diff-highlight/DiffHighlight.pm          | 12 ++++++++++++\n contrib/diff-highlight/t/t9400-diff-highlight.sh | 11 +++++++++++\n 2 files changed, 23 insertions(+)\n\ndiff --git a/contrib/diff-highlight/DiffHighlight.pm b/contrib/diff-highlight/DiffHighlight.pm\nindex 3d061bc0b7..f0607a4b68 100644\n--- a/contrib/diff-highlight/DiffHighlight.pm\n+++ b/contrib/diff-highlight/DiffHighlight.pm\n@@ -273,6 +273,18 @@ sub highlight_line {\n # or suffix (disregarding boring bits like whitespace and colorization).\n sub is_pair_interesting {\n \tmy ($a, $pa, $sa, $b, $pb, $sb) = @_;\n+\n+\t# We hit this case if the prefix consumed the entire line, meaning\n+\t# that two lines are identical. This generally shouldn't happen,\n+\t# since it implies the diff isn't minimal (you could shrink the hunk by\n+\t# making this a context line). But you can see it when the line\n+\t# content is the same, but the trailing newline is dropped, like:\n+\t#\n+\t#   -foo\n+\t#   +foo\n+\t#   \\No newline at end of file\n+\treturn 0 if $pa == @$a || $pb == @$b;\n+\n \tmy $prefix_a = join('', @$a[0..($pa-1)]);\n \tmy $prefix_b = join('', @$b[0..($pb-1)]);\n \tmy $suffix_a = join('', @$a[($sa+1)..$#$a]);\ndiff --git a/contrib/diff-highlight/t/t9400-diff-highlight.sh b/contrib/diff-highlight/t/t9400-diff-highlight.sh\nindex dee296739c..2a9b68cf3b 100755\n--- a/contrib/diff-highlight/t/t9400-diff-highlight.sh\n+++ b/contrib/diff-highlight/t/t9400-diff-highlight.sh\n@@ -340,4 +340,15 @@ test_expect_success 'diff-highlight handles --graph with leading dash' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'highlight diff that removes final newline' '\n+\tprintf \"content\\n\" >a &&\n+\tprintf \"content\" >b &&\n+\tdh_test a b <<-\\EOF\n+\t@@ -1 +1 @@\n+\t-content\n+\t+content\n+\t\\ No newline at end of file\n+\tEOF\n+'\n+\n test_done\n-- \n2.53.0.930.g4fb07a7d1b\n"}]}