@matheusbsbs / matheus-bagliano / commits / 1bf1a859ae
Fork 0

apply: treat EOF as proper context.

Catalin noticed that we do not treat end-of-file condition shown in the patch text as the patch context. This causes a patch that appends at the end of the file to cleanly apply even if something else has been appended to the file. If this happened in the middle, we would refuse by saying that the file has conflicting modifications. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 23, 2006 at 19:08 UTC 1bf1a859ae980a73552c76fe20a5952a763ec81e
1 file changed +14
apply.c
+14
@@ -1333,6 +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;
1337 char *buf = desc->buffer;
1338 const char *patch = frag->patch;
1339 int offset, size = frag->size;
@@ -1395,10 +1396,19 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1396 newlines = new;
1397 leading = frag->leading;
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.
1403 + */
1404 + match_end = !trailing;
1405 +
1406 lines = 0;
1407 pos = frag->newpos;
1408 for (;;) {
1409 offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
1410 + if (match_end && offset + oldsize != desc->size)
1411 + offset = -1;
1412 if (offset >= 0) {
1413 int diff = newsize - oldsize;
1414 unsigned long size = desc->size + diff;
@@ -1428,6 +1438,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1438 /* Am I at my context limits? */
1439 if ((leading <= p_context) && (trailing <= p_context))
1440 break;
1441 + if (match_end) {
1442 + match_end = 0;
1443 + continue;
1444 + }
1445 /* Reduce the number of context lines
1446 * Reduce both leading and trailing if they are equal
1447 * otherwise just reduce the larger context.