apply: force matching at the beginning.
When there is no leading context, the patch must match at the beginning of preimage; otherwise there is a "patch adds these lines while the other lines were added to the original file" conflict. This is the opposite of match_end fix earlier in this series. Unlike matching at the end case, we can additionally check the preimage line number recorded in the patch, so the change is not symmetrical with the earlier one. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
May 24, 2006 at 13:19 UTC
65aadb92a1ce9605fa2f412b51de91781a3ef3d6
2 files changed
+27
-6
apply.c
+8
-5
@@ -1333,7 +1333,7 @@ static int apply_line(char *output, const char *patch, int plen)
1333
1334
static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1335
{
1336
- int match_end;
1336
+ int match_beginning, match_end;
1337
char *buf = desc->buffer;
1338
const char *patch = frag->patch;
1339
int offset, size = frag->size;
@@ -1398,9 +1398,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1398
trailing = frag->trailing;
1399
1400
/*
1401
- * If we don't have any trailing data in the patch,
1402
- * we want it to match at the end of the file.
1401
+ * If we don't have any leading/trailing data in the patch,
1402
+ * we want it to match at the beginning/end of the file.
1403
*/
1404
+ match_beginning = !leading && (frag->oldpos == 1);
1405
match_end = !trailing;
1406
1407
lines = 0;
@@ -1409,6 +1410,8 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1410
offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
1411
if (match_end && offset + oldsize != desc->size)
1412
offset = -1;
1413
+ if (match_beginning && offset)
1414
+ offset = -1;
1415
if (offset >= 0) {
1416
int diff = newsize - oldsize;
1417
unsigned long size = desc->size + diff;
@@ -1438,8 +1441,8 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1441
/* Am I at my context limits? */
1442
if ((leading <= p_context) && (trailing <= p_context))
1443
break;
1441
- if (match_end) {
1442
- match_end = 0;
1444
+ if (match_beginning || match_end) {
1445
+ match_beginning = match_end = 0;
1446
continue;
1447
}
1448
/* Reduce the number of context lines
t/t4113-apply-ending.sh
+19
-1
@@ -29,7 +29,25 @@ test_expect_success setup \
29
30
# test
31
32
-test_expect_failure apply \
32
+test_expect_failure 'apply at the end' \
33
'git-apply --index test-patch'
34
35
+cat >test-patch <<\EOF
36
+diff a/file b/file
37
+--- a/file
38
++++ b/file
39
+@@ -1,2 +1,3 @@
40
++a
41
+ b
42
+ c
43
+EOF
44
+
45
+echo >file 'a
46
+b
47
+c'
48
+git-update-index file
49
+
50
+test_expect_failure 'apply at the beginning' \
51
+ 'git-apply --index test-patch'
52
+
53
test_done