combine-diff.c: Fix output when changes are exactly 3 lines apart

When a deletion is followed by exactly 3 (or whatever the number of context lines) unchanged lines, followed by another change, the combined diff output would hide the first deletion, resulting in a malformed diff. This happened because the 3 lines before each change are painted interesting, but also marked as no_pre_delete to prevent showing deletes that were previously marked as uninteresting. This behaviour was introduced in c86fbe53 (diff -c/--cc: do not include uninteresting deletion before leading context). However, as a side effect, this could also mark deletes that were already interesting as no_pre_delete. This would happen only if the delete was exactly 3 lines away from the next change, since lines farther away would not be touched by the "paint three lines before the change" code and lines closer would be painted by the "merge two adjacent hunks" code instead, which does not set the no_pre_delete flag. This commit fixes this problem by only setting the no_pre_delete flag for changes that were previously uninteresting. Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthijs Kooijman committed May 15, 2013 at 19:42 UTC aac385717a62bfa6889151d191e08b9725587912
2 files changed +53 -2
combine-diff.c
+5 -2
@@ -344,8 +344,11 @@ static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
344 unsigned long k;
345
346 /* Paint a few lines before the first interesting line. */
347 - while (j < i)
348 - sline[j++].flag |= mark | no_pre_delete;
347 + while (j < i) {
348 + if (!(sline[j].flag & mark))
349 + sline[j].flag |= no_pre_delete;
350 + sline[j++].flag |= mark;
351 + }
352
353 again:
354 /* we know up to i is to be included. where does the
t/t4038-diff-combined.sh
+48
@@ -89,4 +89,52 @@ test_expect_success 'diagnose truncated file' '
89 grep "diff --cc file" out
90 '
91
92 +# Test for a bug reported at
93 +# http://thread.gmane.org/gmane.comp.version-control.git/224410
94 +# where a delete lines were missing from combined diff output when they
95 +# occurred exactly before the context lines of a later change.
96 +test_expect_success 'combine diff missing delete bug' '
97 + git commit -m initial --allow-empty &&
98 + cat <<-\EOF >test &&
99 + 1
100 + 2
101 + 3
102 + 4
103 + EOF
104 + git add test &&
105 + git commit -a -m side1 &&
106 + git checkout -B side1 &&
107 + git checkout HEAD^ &&
108 + cat <<-\EOF >test &&
109 + 0
110 + 1
111 + 2
112 + 3
113 + 4modified
114 + EOF
115 + git add test &&
116 + git commit -m side2 &&
117 + git branch -f side2 &&
118 + test_must_fail git merge --no-commit side1 &&
119 + cat <<-\EOF >test &&
120 + 1
121 + 2
122 + 3
123 + 4modified
124 + EOF
125 + git add test &&
126 + git commit -a -m merge &&
127 + git diff-tree -c -p HEAD >actual.tmp &&
128 + sed -e "1,/^@@@/d" < actual.tmp >actual &&
129 + tr -d Q <<-\EOF >expected &&
130 + - 0
131 + 1
132 + 2
133 + 3
134 + -4
135 + +4modified
136 + EOF
137 + test_cmp expected actual
138 +'
139 +
140 test_done