]> Kevux Git Server - rit/commit
contrib/diff-highlight: do not highlight identical pairs
authorJeff King <peff@peff.net>
Tue, 17 Mar 2026 23:02:23 +0000 (19:02 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Mar 2026 05:29:14 +0000 (22:29 -0700)
commit598f40c4b3de30d8f7c0666823a9d90884b78bee
tree7e1bc16c2f77254573ddf32718ee97bde3b293b2
parent7ff1e8dc1e1680510c96e69965b3fa81372c5037
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>
contrib/diff-highlight/DiffHighlight.pm
contrib/diff-highlight/t/t9400-diff-highlight.sh