Skip to content

Commit 248b6c0

Browse files
committed
Merge branch 'maint'
* maint: Trailing whitespace and no newline fix diff --cc: a lost line at the beginning of the file is shown incorrectly combine-diff.c: fix performance problem when folding common deleted lines
2 parents 2a679c7 + 735c674 commit 248b6c0

File tree

4 files changed

+118
-18
lines changed

4 files changed

+118
-18
lines changed

combine-diff.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct lline {
8080
/* Lines surviving in the merge result */
8181
struct sline {
8282
struct lline *lost_head, **lost_tail;
83+
struct lline *next_lost;
8384
char *bol;
8485
int len;
8586
/* bit 0 up to (N-1) are on if the parent has this line (i.e.
@@ -121,18 +122,12 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
121122

122123
/* Check to see if we can squash things */
123124
if (sline->lost_head) {
124-
struct lline *last_one = NULL;
125-
/* We cannot squash it with earlier one */
126-
for (lline = sline->lost_head;
127-
lline;
128-
lline = lline->next)
129-
if (lline->parent_map & this_mask)
130-
last_one = lline;
131-
lline = last_one ? last_one->next : sline->lost_head;
125+
lline = sline->next_lost;
132126
while (lline) {
133127
if (lline->len == len &&
134128
!memcmp(lline->line, line, len)) {
135129
lline->parent_map |= this_mask;
130+
sline->next_lost = lline->next;
136131
return;
137132
}
138133
lline = lline->next;
@@ -147,6 +142,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
147142
lline->line[len] = 0;
148143
*sline->lost_tail = lline;
149144
sline->lost_tail = &lline->next;
145+
sline->next_lost = NULL;
150146
}
151147

152148
struct combine_diff_state {
@@ -168,25 +164,28 @@ static void consume_line(void *state_, char *line, unsigned long len)
168164
&state->nb, &state->nn))
169165
return;
170166
state->lno = state->nb;
171-
if (!state->nb)
172-
/* @@ -1,2 +0,0 @@ to remove the
173-
* first two lines...
174-
*/
175-
state->nb = 1;
176-
if (state->nn == 0)
167+
if (state->nn == 0) {
177168
/* @@ -X,Y +N,0 @@ removed Y lines
178169
* that would have come *after* line N
179170
* in the result. Our lost buckets hang
180171
* 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.
181176
*/
182177
state->lost_bucket = &state->sline[state->nb];
183-
else
178+
if (!state->nb)
179+
state->nb = 1;
180+
} else {
184181
state->lost_bucket = &state->sline[state->nb-1];
182+
}
185183
if (!state->sline[state->nb-1].p_lno)
186184
state->sline[state->nb-1].p_lno =
187185
xcalloc(state->num_parent,
188186
sizeof(unsigned long));
189187
state->sline[state->nb-1].p_lno[state->n] = state->ob;
188+
state->lost_bucket->next_lost = state->lost_bucket->lost_head;
190189
return;
191190
}
192191
if (!state->lost_bucket)

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

t/t4124-apply-ws-rule.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ do
148148
done
149149
done
150150

151+
create_patch () {
152+
sed -e "s/_/ /" <<-\EOF
153+
diff --git a/target b/target
154+
index e69de29..8bd6648 100644
155+
--- a/target
156+
+++ b/target
157+
@@ -0,0 +1 @@
158+
+A line with trailing whitespace and no newline_
159+
\ No newline at end of file
160+
EOF
161+
}
162+
163+
test_expect_success 'trailing whitespace & no newline at the end of file' '
164+
>target &&
165+
create_patch | git apply --whitespace=fix - &&
166+
grep "newline$" target
167+
'
168+
151169
test_done

ws.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
261261
/*
262262
* Strip trailing whitespace
263263
*/
264-
if ((ws_rule & WS_TRAILING_SPACE) &&
265-
(2 <= len && isspace(src[len-2]))) {
266-
if (src[len - 1] == '\n') {
264+
if (ws_rule & WS_TRAILING_SPACE) {
265+
if (1 < len && src[len - 1] == '\n') {
267266
add_nl_to_tail = 1;
268267
len--;
269268
if (1 < len && src[len - 1] == '\r') {

0 commit comments

Comments
 (0)