If you are using --color-words[=<regex>]
, make sure to use Git 2.32 (Q2 2021) or more recent: the word-diff mode has been taught to work better with a word regexp that can match an empty string.
See commit 0324e8f (04 May 2021) by Phillip Wood (phillipwood
).
(Merged by Junio C Hamano -- gitster
-- in commit 65c1891, 14 May 2021)
word diff
: handle zero length matches
Signed-off-by: Phillip Wood
If find_word_boundaries()
encounters a zero length match (which can be caused by matching a newline or using '*
' instead of '+
' in the regex) we stop splitting the input into words which generates an inaccurate diff.
To fix this increment the start point when there is a zero length match and try a new match.
This is safe as posix regular expressions always return the longest available match so a zero length match means there are no longer matches available from the current position.
Commit bf82940 ("color-words
: enable REG_NEWLINE
to help user", 2009-01-17, Git v1.6.2-rc0 -- merge) prevented matching newlines in negated character classes but it is still possible for the user to have an explicit newline match in the regex which could cause a zero length match.
One could argue that having explicit newline matches or using '*
' rather than '+
' are user errors but it seems to be better to work round them than produce inaccurate diffs.