contrib/diff-highlight: do not highlight identical pairs

We pair lines for highlighting based on their position in the hunk. So we should never see two identical lines paired, like: -one -two +one +something else which would pair -one/+one, because that implies that the diff could easily be shrunk by turning line "one" into context. But there is (at least) one exception: removing a newline at the end of a file will produce a diff like: -foo +foo \No newline at end of file And we will pair those two lines. As a result, we end up marking the whole line, including the newline, as the shared prefix. And there's an empty suffix. The most obvious bug here is that when we try to print the highlighted lines, we remove the trailing newline from the suffix, but do not bother with the prefix (under the assumption that there had to be a difference _somewhere_ in the line, and thus the prefix would not eat all the way up to the newline). And so you get an extra line like: -foo +foo \No newline at end of file This is obviously ugly, but also causes interactive.diffFilter to (rightly) complain that the input and output do not match their lines 1-to-1. This could easily be fixed by chomping the prefix, too, but I think the problem is deeper. For one, I suspect some of the other logic gets confused by forming an array with zero-indexed element "3" in a 3-element array. But more importantly, we try not to highlight whole lines, as there's nothing interesting to show there. So let's catch this early in is_pair_interesting() and bail to our usual passthrough strategy. Reported-by: Scott Baker <scott@perturb.org> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 17, 2026 at 19:02 UTC 598f40c4b3de30d8f7c0666823a9d90884b78bee
2 files changed +23
contrib/diff-highlight/DiffHighlight.pm
+12
@@ -273,6 +273,18 @@ sub highlight_line {
273 # or suffix (disregarding boring bits like whitespace and colorization).
274 sub is_pair_interesting {
275 my ($a, $pa, $sa, $b, $pb, $sb) = @_;
276 +
277 + # We hit this case if the prefix consumed the entire line, meaning
278 + # that two lines are identical. This generally shouldn't happen,
279 + # since it implies the diff isn't minimal (you could shrink the hunk by
280 + # making this a context line). But you can see it when the line
281 + # content is the same, but the trailing newline is dropped, like:
282 + #
283 + # -foo
284 + # +foo
285 + # \No newline at end of file
286 + return 0 if $pa == @$a || $pb == @$b;
287 +
288 my $prefix_a = join('', @$a[0..($pa-1)]);
289 my $prefix_b = join('', @$b[0..($pb-1)]);
290 my $suffix_a = join('', @$a[($sa+1)..$#$a]);
contrib/diff-highlight/t/t9400-diff-highlight.sh
+11
@@ -340,4 +340,15 @@ test_expect_success 'diff-highlight handles --graph with leading dash' '
340 test_cmp expect actual
341 '
342
343 +test_expect_success 'highlight diff that removes final newline' '
344 + printf "content\n" >a &&
345 + printf "content" >b &&
346 + dh_test a b <<-\EOF
347 + @@ -1 +1 @@
348 + -content
349 + +content
350 + \ No newline at end of file
351 + EOF
352 +'
353 +
354 test_done