Skip to content

Commit b810cbb

Browse files
committed
diff --cc: a lost line at the beginning of the file is shown incorrectly
When combine-diff inspected the diff from one parent to the merge result, it misinterpreted a header in the form @@ -l,k +0,0 @@. This hunk header means that K lines were removed from the beginning of the file, so the lost lines must be queued to the sline that represents the first line of the merge result, but we incremented our pointer incorrectly and ended up queuing it to the second line, which in turn made the lossage appear _after_ the first line. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 55d5d5b commit b810cbb

File tree

2 files changed

+93
-7
lines changed

2 files changed

+93
-7
lines changed

combine-diff.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,22 @@ static void consume_line(void *state_, char *line, unsigned long len)
164164
&state->nb, &state->nn))
165165
return;
166166
state->lno = state->nb;
167-
if (!state->nb)
168-
/* @@ -1,2 +0,0 @@ to remove the
169-
* first two lines...
170-
*/
171-
state->nb = 1;
172-
if (state->nn == 0)
167+
if (state->nn == 0) {
173168
/* @@ -X,Y +N,0 @@ removed Y lines
174169
* that would have come *after* line N
175170
* in the result. Our lost buckets hang
176171
* to the line after the removed lines,
172+
*
173+
* Note that this is correct even when N == 0,
174+
* in which case the hunk removes the first
175+
* line in the file.
177176
*/
178177
state->lost_bucket = &state->sline[state->nb];
179-
else
178+
if (!state->nb)
179+
state->nb = 1;
180+
} else {
180181
state->lost_bucket = &state->sline[state->nb-1];
182+
}
181183
if (!state->sline[state->nb-1].p_lno)
182184
state->sline[state->nb-1].p_lno =
183185
xcalloc(state->num_parent,

t/t4038-diff-combined.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/sh
2+
3+
test_description='combined diff'
4+
5+
. ./test-lib.sh
6+
7+
setup_helper () {
8+
one=$1 branch=$2 side=$3 &&
9+
10+
git branch $side $branch &&
11+
for l in $one two three fyra
12+
do
13+
echo $l
14+
done >file &&
15+
git add file &&
16+
test_tick &&
17+
git commit -m $branch &&
18+
git checkout $side &&
19+
for l in $one two three quatro
20+
do
21+
echo $l
22+
done >file &&
23+
git add file &&
24+
test_tick &&
25+
git commit -m $side &&
26+
test_must_fail git merge $branch &&
27+
for l in $one three four
28+
do
29+
echo $l
30+
done >file &&
31+
git add file &&
32+
test_tick &&
33+
git commit -m "merge $branch into $side"
34+
}
35+
36+
verify_helper () {
37+
it=$1 &&
38+
39+
# Ignore lines that were removed only from the other parent
40+
sed -e '
41+
1,/^@@@/d
42+
/^ -/d
43+
s/^\(.\)./\1/
44+
' "$it" >"$it.actual.1" &&
45+
sed -e '
46+
1,/^@@@/d
47+
/^- /d
48+
s/^.\(.\)/\1/
49+
' "$it" >"$it.actual.2" &&
50+
51+
git diff "$it^" "$it" -- | sed -e '1,/^@@/d' >"$it.expect.1" &&
52+
test_cmp "$it.expect.1" "$it.actual.1" &&
53+
54+
git diff "$it^2" "$it" -- | sed -e '1,/^@@/d' >"$it.expect.2" &&
55+
test_cmp "$it.expect.2" "$it.actual.2"
56+
}
57+
58+
test_expect_success setup '
59+
>file &&
60+
git add file &&
61+
test_tick &&
62+
git commit -m initial &&
63+
64+
git branch withone &&
65+
git branch sansone &&
66+
67+
git checkout withone &&
68+
setup_helper one withone sidewithone &&
69+
70+
git checkout sansone &&
71+
setup_helper "" sansone sidesansone
72+
'
73+
74+
test_expect_success 'check combined output (1)' '
75+
git show sidewithone -- >sidewithone &&
76+
verify_helper sidewithone
77+
'
78+
79+
test_expect_failure 'check combined output (2)' '
80+
git show sidesansone -- >sidesansone &&
81+
verify_helper sidesansone
82+
'
83+
84+
test_done

0 commit comments

Comments
 (0)