diff-highlight: avoid highlighting combined diffs

The algorithm in diff-highlight only understands how to look at two sides of a diff; it cannot correctly handle combined diffs with multiple preimages. Often highlighting does not trigger at all for these diffs because the line counts do not match up. E.g., if we see: - ours -theirs ++resolved we would not bother highlighting; it naively looks like a single line went away, and then a separate hunk added another single line. But of course there are exceptions. E.g., if the other side deleted the line, we might see: - ours ++resolved which looks like we dropped " ours" and added "+resolved". This is only a small highlighting glitch (we highlight the space and the "+" along with the content), but it's also the tip of the iceberg. Even if we learned to find the true content here (by noticing we are in a 3-way combined diff and marking _two_ characters from the front of the line as uninteresting), there are other more complicated cases where we really do need to handle a 3-way hunk. Let's just punt for now; we can recognize combined diffs by the presence of extra "@" symbols in the hunk header, and treat them as non-diff content. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 31, 2016 at 01:05 UTC 3dbfe2b8ae94cbdae5f3d32581aedaa5510fdc87
2 files changed +38 -1
contrib/diff-highlight/diff-highlight
+1 -1
@@ -36,7 +36,7 @@ $SIG{PIPE} = 'DEFAULT';
36 while (<>) {
37 if (!$in_hunk) {
38 print;
39 - $in_hunk = /^$GRAPH*$COLOR*\@/;
39 + $in_hunk = /^$GRAPH*$COLOR*\@\@ /;
40 }
41 elsif (/^$GRAPH*$COLOR*-/) {
42 push @removed, $_;
contrib/diff-highlight/t/t9400-diff-highlight.sh
+37
@@ -256,4 +256,41 @@ test_expect_success 'diff-highlight works with the --graph option' '
256 test_cmp graph.exp graph.act
257 '
258
259 +# Most combined diffs won't meet diff-highlight's line-number filter. So we
260 +# create one here where one side drops a line and the other modifies it. That
261 +# should result in a diff like:
262 +#
263 +# - modified content
264 +# ++resolved content
265 +#
266 +# which naively looks like one side added "+resolved".
267 +test_expect_success 'diff-highlight ignores combined diffs' '
268 + echo "content" >file &&
269 + git add file &&
270 + git commit -m base &&
271 +
272 + >file &&
273 + git commit -am master &&
274 +
275 + git checkout -b other HEAD^ &&
276 + echo "modified content" >file &&
277 + git commit -am other &&
278 +
279 + test_must_fail git merge master &&
280 + echo "resolved content" >file &&
281 + git commit -am resolved &&
282 +
283 + cat >expect <<-\EOF &&
284 + --- a/file
285 + +++ b/file
286 + @@@ -1,1 -1,0 +1,1 @@@
287 + - modified content
288 + ++resolved content
289 + EOF
290 +
291 + git show -c | "$DIFF_HIGHLIGHT" >actual.raw &&
292 + sed -n "/^---/,\$p" <actual.raw >actual &&
293 + test_cmp expect actual
294 +'
295 +
296 test_done