Skip to content

Commit aac3857

Browse files
matthijskooijmangitster
authored andcommitted
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 c86fbe5 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1599999 commit aac3857

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

combine-diff.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,11 @@ static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
344344
unsigned long k;
345345

346346
/* 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+
}
349352

350353
again:
351354
/* we know up to i is to be included. where does the

t/t4038-diff-combined.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,52 @@ test_expect_success 'diagnose truncated file' '
8989
grep "diff --cc file" out
9090
'
9191

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+
92140
test_done

0 commit comments

Comments
 (0)