]> git.ipfire.org Git - thirdparty/git.git/commit - diff.c
diff --color-moved: avoid false short line matches and bad zebra coloring
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Thu, 9 Dec 2021 10:30:00 +0000 (10:30 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Dec 2021 21:24:05 +0000 (13:24 -0800)
commiteb893525041a53b968a46000ed5cb66ffc725853
treecae4052094860f5ad8b06c4e6534b3d37790f65b
parenteb315457f65e1d6b77cde3933b6a175e649fb34b
diff --color-moved: avoid false short line matches and bad zebra coloring

When marking moved lines it is possible for a block of potential
matched lines to extend past a change in sign when there is a sequence
of added lines whose text matches the text of a sequence of deleted
and added lines. Most of the time either `match` will be NULL or
`pmb_advance_or_null()` will fail when the loop encounters a change of
sign but there are corner cases where `match` is non-NULL and
`pmb_advance_or_null()` successfully advances the moved block despite
the change in sign.

One consequence of this is highlighting a short line as moved when it
should not be. For example

-moved line  # Correctly highlighted as moved
+short line  # Wrongly highlighted as moved
 context
+moved line  # Correctly highlighted as moved
+short line
 context
-short line

The other consequence is coloring a moved addition following a moved
deletion in the wrong color. In the example below the first "+moved
line 3" should be highlighted as newMoved not newMovedAlternate.

-moved line 1 # Correctly highlighted as oldMoved
-moved line 2 # Correctly highlighted as oldMovedAlternate
+moved line 3 # Wrongly highlighted as newMovedAlternate
 context      # Everything else is highlighted correctly
+moved line 2
+moved line 3
 context
+moved line 1
-moved line 3

These false matches are more likely when using --color-moved-ws with
the exception of --color-moved-ws=allow-indentation-change which ties
the sign of the current whitespace delta to the sign of the line to
avoid this problem. The fix is to check that the sign of the new line
being matched is the same as the sign of the line that started the
block of potential matches.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4015-diff-whitespace.sh