[PATCH] Test case portability fix.
[git/git.git] / apply.c
CommitLineData
c1bb9350
LT
1/*
2 * apply.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This applies patches on top of some (arbitrary) version of the SCM.
7 *
8 * NOTE! It does all its work in the index file, and only cares about
9 * the files in the working directory if you tell it to "merge" the
10 * patch apply.
11 *
12 * Even when merging it always takes the source from the index, and
13 * uses the working tree as a "branch" for a 3-way merge.
14 */
15#include <ctype.h>
16
17#include "cache.h"
18
19// We default to the merge behaviour, since that's what most people would
20// expect
21static int merge_patch = 1;
22static const char apply_usage[] = "git-apply <patch>";
23
a4acb0eb
LT
24/*
25 * Various "current state", notably line numbers and what
26 * file (and how) we're patching right now.. The "is_xxxx"
27 * things are flags, where -1 means "don't know yet".
28 */
46979f56 29static int linenr = 1;
a4acb0eb
LT
30static int old_mode, new_mode;
31static char *old_name, *new_name, *def_name;
32static int is_rename, is_copy, is_new, is_delete;
46979f56 33
c1bb9350 34#define CHUNKSIZE (8192)
a4acb0eb 35#define SLOP (16)
c1bb9350
LT
36
37static void *read_patch_file(int fd, unsigned long *sizep)
38{
39 unsigned long size = 0, alloc = CHUNKSIZE;
40 void *buffer = xmalloc(alloc);
41
42 for (;;) {
43 int nr = alloc - size;
44 if (nr < 1024) {
45 alloc += CHUNKSIZE;
46 buffer = xrealloc(buffer, alloc);
47 nr = alloc - size;
48 }
49 nr = read(fd, buffer + size, nr);
50 if (!nr)
51 break;
52 if (nr < 0) {
53 if (errno == EAGAIN)
54 continue;
55 die("git-apply: read returned %s", strerror(errno));
56 }
57 size += nr;
58 }
59 *sizep = size;
a4acb0eb
LT
60
61 /*
62 * Make sure that we have some slop in the buffer
63 * so that we can do speculative "memcmp" etc, and
64 * see to it that it is NUL-filled.
65 */
66 if (alloc < size + SLOP)
67 buffer = xrealloc(buffer, size + SLOP);
68 memset(buffer + size, 0, SLOP);
c1bb9350
LT
69 return buffer;
70}
71
72static unsigned long linelen(char *buffer, unsigned long size)
73{
74 unsigned long len = 0;
75 while (size--) {
76 len++;
77 if (*buffer++ == '\n')
78 break;
79 }
80 return len;
81}
82
a4acb0eb
LT
83static int is_dev_null(const char *str)
84{
85 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
86}
87
9a4a100e
LT
88#define TERM_EXIST 1
89#define TERM_SPACE 2
90#define TERM_TAB 4
91
92static int name_terminate(const char *name, int namelen, int c, int terminate)
93{
94 if (c == ' ' && !(terminate & TERM_SPACE))
95 return 0;
96 if (c == '\t' && !(terminate & TERM_TAB))
97 return 0;
98
99 /*
100 * Do we want an existing name? Return false and
101 * continue if it's not there.
102 */
103 if (terminate & TERM_EXIST)
104 return cache_name_pos(name, namelen) >= 0;
105
106 return 1;
107}
108
109static char * find_name(const char *line, char *def, int p_value, int terminate)
c1bb9350 110{
a4acb0eb
LT
111 int len;
112 const char *start = line;
113 char *name;
114
c1bb9350 115 for (;;) {
a4acb0eb 116 char c = *line;
9a4a100e
LT
117
118 if (isspace(c)) {
119 if (c == '\n')
120 break;
121 if (name_terminate(start, line-start, c, terminate))
122 break;
123 }
a4acb0eb
LT
124 line++;
125 if (c == '/' && !--p_value)
126 start = line;
127 }
128 if (!start)
129 return def;
130 len = line - start;
131 if (!len)
132 return def;
133
134 /*
135 * Generally we prefer the shorter name, especially
136 * if the other one is just a variation of that with
137 * something else tacked on to the end (ie "file.orig"
138 * or "file~").
139 */
140 if (def) {
141 int deflen = strlen(def);
142 if (deflen < len && !strncmp(start, def, deflen))
143 return def;
c1bb9350 144 }
a4acb0eb
LT
145
146 name = xmalloc(len + 1);
147 memcpy(name, start, len);
148 name[len] = 0;
149 free(def);
150 return name;
151}
152
153/*
154 * Get the name etc info from the --/+++ lines of a traditional patch header
155 *
156 * NOTE! This hardcodes "-p1" behaviour in filename detection.
9a4a100e
LT
157 *
158 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
159 * files, we can happily check the index for a match, but for creating a
160 * new file we should try to match whatever "patch" does. I have no idea.
a4acb0eb 161 */
bba0f401 162static void parse_traditional_patch(const char *first, const char *second)
a4acb0eb
LT
163{
164 int p_value = 1;
165 char *name;
166
167 first += 4; // skip "--- "
168 second += 4; // skip "+++ "
169 if (is_dev_null(first)) {
170 is_new = 1;
9a4a100e
LT
171 name = find_name(second, def_name, p_value, TERM_SPACE | TERM_TAB);
172 new_name = name;
a4acb0eb
LT
173 } else if (is_dev_null(second)) {
174 is_delete = 1;
9a4a100e
LT
175 name = find_name(first, def_name, p_value, TERM_EXIST | TERM_SPACE | TERM_TAB);
176 old_name = name;
a4acb0eb 177 } else {
9a4a100e
LT
178 name = find_name(first, def_name, p_value, TERM_EXIST | TERM_SPACE | TERM_TAB);
179 name = find_name(second, name, p_value, TERM_EXIST | TERM_SPACE | TERM_TAB);
180 old_name = new_name = name;
a4acb0eb
LT
181 }
182 if (!name)
183 die("unable to find filename in patch at line %d", linenr);
a4acb0eb
LT
184}
185
186static int gitdiff_hdrend(const char *line)
187{
188 return -1;
189}
190
1e3f6b6e
LT
191/*
192 * We're anal about diff header consistency, to make
193 * sure that we don't end up having strange ambiguous
194 * patches floating around.
195 *
196 * As a result, gitdiff_{old|new}name() will check
197 * their names against any previous information, just
198 * to make sure..
199 */
200static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
201{
202 int len;
203 const char *name;
204
205 if (!orig_name && !isnull)
206 return find_name(line, NULL, 1, 0);
207
208 name = "/dev/null";
209 len = 9;
210 if (orig_name) {
211 name = orig_name;
212 len = strlen(name);
213 if (isnull)
214 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
215 }
216
217 if (*name == '/')
218 goto absolute_path;
219
220 for (;;) {
221 char c = *line++;
222 if (c == '\n')
223 break;
224 if (c != '/')
225 continue;
226absolute_path:
227 if (memcmp(line, name, len) || line[len] != '\n')
228 break;
229 return orig_name;
230 }
231 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
232 return NULL;
233}
234
a4acb0eb
LT
235static int gitdiff_oldname(const char *line)
236{
1e3f6b6e 237 old_name = gitdiff_verify_name(line, is_new, old_name, "old");
a4acb0eb
LT
238 return 0;
239}
240
241static int gitdiff_newname(const char *line)
242{
1e3f6b6e 243 new_name = gitdiff_verify_name(line, is_delete, new_name, "new");
a4acb0eb
LT
244 return 0;
245}
246
247static int gitdiff_oldmode(const char *line)
248{
249 old_mode = strtoul(line, NULL, 8);
250 return 0;
251}
252
253static int gitdiff_newmode(const char *line)
254{
255 new_mode = strtoul(line, NULL, 8);
256 return 0;
257}
258
259static int gitdiff_delete(const char *line)
260{
261 is_delete = 1;
262 return gitdiff_oldmode(line);
263}
264
265static int gitdiff_newfile(const char *line)
266{
267 is_new = 1;
268 return gitdiff_newmode(line);
269}
270
271static int gitdiff_copysrc(const char *line)
272{
273 is_copy = 1;
9a4a100e 274 old_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
275 return 0;
276}
277
278static int gitdiff_copydst(const char *line)
279{
280 is_copy = 1;
9a4a100e 281 new_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
282 return 0;
283}
284
285static int gitdiff_renamesrc(const char *line)
286{
287 is_rename = 1;
9a4a100e 288 old_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
289 return 0;
290}
291
292static int gitdiff_renamedst(const char *line)
293{
294 is_rename = 1;
9a4a100e 295 new_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
296 return 0;
297}
298
299static int gitdiff_similarity(const char *line)
300{
301 return 0;
c1bb9350
LT
302}
303
9a4a100e
LT
304/*
305 * This is normal for a diff that doesn't change anything: we'll fall through
306 * into the next diff. Tell the parser to break out.
307 */
308static int gitdiff_unrecognized(const char *line)
309{
310 return -1;
311}
312
c1bb9350 313/* Verify that we recognize the lines following a git header */
a4acb0eb 314static int parse_git_header(char *line, int len, unsigned int size)
c1bb9350 315{
a4acb0eb
LT
316 unsigned long offset;
317
318 /* A git diff has explicit new/delete information, so we don't guess */
319 is_new = 0;
320 is_delete = 0;
321
322 line += len;
323 size -= len;
324 linenr++;
325 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
326 static const struct opentry {
327 const char *str;
328 int (*fn)(const char *);
329 } optable[] = {
330 { "@@ -", gitdiff_hdrend },
331 { "--- ", gitdiff_oldname },
332 { "+++ ", gitdiff_newname },
333 { "old mode ", gitdiff_oldmode },
334 { "new mode ", gitdiff_newmode },
335 { "deleted file mode ", gitdiff_delete },
336 { "new file mode ", gitdiff_newfile },
337 { "copy from ", gitdiff_copysrc },
338 { "copy to ", gitdiff_copydst },
339 { "rename from ", gitdiff_renamesrc },
340 { "rename to ", gitdiff_renamedst },
341 { "similarity index ", gitdiff_similarity },
9a4a100e 342 { "", gitdiff_unrecognized },
a4acb0eb
LT
343 };
344 int i;
c1bb9350 345
c1bb9350 346 len = linelen(line, size);
a4acb0eb 347 if (!len || line[len-1] != '\n')
c1bb9350 348 break;
a4acb0eb
LT
349 for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
350 const struct opentry *p = optable + i;
351 int oplen = strlen(p->str);
352 if (len < oplen || memcmp(p->str, line, oplen))
353 continue;
354 if (p->fn(line + oplen) < 0)
355 return offset;
9a4a100e 356 break;
a4acb0eb 357 }
c1bb9350
LT
358 }
359
a4acb0eb 360 return offset;
c1bb9350
LT
361}
362
46979f56
LT
363static int parse_num(const char *line, int len, int offset, const char *expect, unsigned long *p)
364{
365 char *ptr;
366 int digits, ex;
367
368 if (offset < 0 || offset >= len)
369 return -1;
370 line += offset;
371 len -= offset;
372
373 if (!isdigit(*line))
374 return -1;
375 *p = strtoul(line, &ptr, 10);
376
377 digits = ptr - line;
378
379 offset += digits;
380 line += digits;
381 len -= digits;
382
383 ex = strlen(expect);
384 if (ex > len)
385 return -1;
386 if (memcmp(line, expect, ex))
387 return -1;
388
389 return offset + ex;
390}
391
392/*
393 * Parse a unified diff fragment header of the
394 * form "@@ -a,b +c,d @@"
395 */
396static int parse_fragment_header(char *line, int len, unsigned long *pos)
397{
398 int offset;
399
400 if (!len || line[len-1] != '\n')
401 return -1;
402
403 /* Figure out the number of lines in a fragment */
404 offset = parse_num(line, len, 4, ",", pos);
405 offset = parse_num(line, len, offset, " +", pos+1);
406 offset = parse_num(line, len, offset, ",", pos+2);
407 offset = parse_num(line, len, offset, " @@", pos+3);
408
409 return offset;
410}
411
c1bb9350
LT
412static int find_header(char *line, unsigned long size, int *hdrsize)
413{
414 unsigned long offset, len;
415
a4acb0eb
LT
416 is_rename = is_copy = 0;
417 is_new = is_delete = -1;
881b0765 418 old_mode = new_mode = 0;
a4acb0eb 419 def_name = old_name = new_name = NULL;
46979f56 420 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
c1bb9350
LT
421 unsigned long nextlen;
422
423 len = linelen(line, size);
424 if (!len)
425 break;
426
427 /* Testing this early allows us to take a few shortcuts.. */
428 if (len < 6)
429 continue;
46979f56
LT
430
431 /*
432 * Make sure we don't find any unconnected patch fragmants.
433 * That's a sign that we didn't find a header, and that a
434 * patch has become corrupted/broken up.
435 */
436 if (!memcmp("@@ -", line, 4)) {
437 unsigned long pos[4];
438 if (parse_fragment_header(line, len, pos) < 0)
439 continue;
440 error("patch fragment without header at line %d: %.*s", linenr, len-1, line);
441 }
442
c1bb9350
LT
443 if (size < len + 6)
444 break;
445
446 /*
447 * Git patch? It might not have a real patch, just a rename
448 * or mode change, so we handle that specially
449 */
450 if (!memcmp("diff --git ", line, 11)) {
a4acb0eb 451 int git_hdr_len = parse_git_header(line, len, size);
c1bb9350
LT
452 if (git_hdr_len < 0)
453 continue;
454
a4acb0eb 455 *hdrsize = git_hdr_len;
c1bb9350
LT
456 return offset;
457 }
458
459 /** --- followed by +++ ? */
460 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
461 continue;
462
463 /*
464 * We only accept unified patches, so we want it to
465 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
466 * minimum
467 */
468 nextlen = linelen(line + len, size - len);
469 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
470 continue;
471
472 /* Ok, we'll consider it a patch */
a4acb0eb 473 parse_traditional_patch(line, line+len);
c1bb9350 474 *hdrsize = len + nextlen;
46979f56 475 linenr += 2;
c1bb9350
LT
476 return offset;
477 }
478 return -1;
479}
480
c1bb9350
LT
481/*
482 * Parse a unified diff. Note that this really needs
483 * to parse each fragment separately, since the only
484 * way to know the difference between a "---" that is
485 * part of a patch, and a "---" that starts the next
486 * patch is to look at the line counts..
487 */
488static int apply_fragment(char *line, unsigned long size)
489{
490 int len = linelen(line, size), offset;
46979f56 491 unsigned long pos[4], oldlines, newlines;
c1bb9350 492
46979f56 493 offset = parse_fragment_header(line, len, pos);
c1bb9350
LT
494 if (offset < 0)
495 return -1;
46979f56
LT
496 oldlines = pos[1];
497 newlines = pos[3];
c1bb9350 498
a4acb0eb
LT
499 if (is_new < 0 && (pos[0] || oldlines))
500 is_new = 0;
501 if (is_delete < 0 && (pos[1] || newlines))
502 is_delete = 0;
503
c1bb9350
LT
504 /* Parse the thing.. */
505 line += len;
506 size -= len;
46979f56
LT
507 linenr++;
508 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
c1bb9350
LT
509 if (!oldlines && !newlines)
510 break;
511 len = linelen(line, size);
512 if (!len || line[len-1] != '\n')
513 return -1;
514 switch (*line) {
515 default:
516 return -1;
517 case ' ':
518 oldlines--;
519 newlines--;
520 break;
521 case '-':
522 oldlines--;
523 break;
524 case '+':
525 newlines--;
526 break;
527 }
528 }
529 return offset;
530}
531
532static int apply_single_patch(char *line, unsigned long size)
533{
534 unsigned long offset = 0;
535
536 while (size > 4 && !memcmp(line, "@@ -", 4)) {
537 int len = apply_fragment(line, size);
538 if (len <= 0)
46979f56 539 die("corrupt patch at line %d", linenr);
c1bb9350
LT
540
541printf("applying fragment:\n%.*s\n\n", len, line);
542
543 offset += len;
544 line += len;
545 size -= len;
546 }
547 return offset;
548}
549
550static int apply_chunk(char *buffer, unsigned long size)
551{
552 int hdrsize, patchsize;
553 int offset = find_header(buffer, size, &hdrsize);
554 char *header, *patch;
555
556 if (offset < 0)
557 return offset;
558 header = buffer + offset;
559
560printf("Found header:\n%.*s\n\n", hdrsize, header);
a4acb0eb
LT
561printf("Rename: %d\n", is_rename);
562printf("Copy: %d\n", is_copy);
563printf("New: %d\n", is_new);
564printf("Delete: %d\n", is_delete);
9a4a100e
LT
565printf("Mode: %o:%o\n", old_mode, new_mode);
566printf("Name: '%s':'%s'\n", old_name, new_name);
567
568 if (old_name && cache_name_pos(old_name, strlen(old_name)) < 0)
569 die("file %s does not exist", old_name);
570 if (new_name && (is_new | is_rename | is_copy)) {
571 if (cache_name_pos(new_name, strlen(new_name)) >= 0)
572 die("file %s already exists", new_name);
573 }
c1bb9350
LT
574
575 patch = header + hdrsize;
576 patchsize = apply_single_patch(patch, size - offset - hdrsize);
577
578 return offset + hdrsize + patchsize;
579}
580
581static int apply_patch(int fd)
582{
583 unsigned long offset, size;
584 char *buffer = read_patch_file(fd, &size);
585
586 if (!buffer)
587 return -1;
588 offset = 0;
589 while (size > 0) {
590 int nr = apply_chunk(buffer + offset, size);
591 if (nr < 0)
592 break;
593 offset += nr;
594 size -= nr;
595 }
596 free(buffer);
597 return 0;
598}
599
600int main(int argc, char **argv)
601{
602 int i;
4dfdbe10 603 int read_stdin = 1;
c1bb9350
LT
604
605 if (read_cache() < 0)
606 die("unable to read index file");
607
608 for (i = 1; i < argc; i++) {
609 const char *arg = argv[i];
610 int fd;
611
612 if (!strcmp(arg, "-")) {
613 apply_patch(0);
4dfdbe10 614 read_stdin = 0;
c1bb9350
LT
615 continue;
616 }
617 if (!strcmp(arg, "--no-merge")) {
618 merge_patch = 0;
619 continue;
620 }
621 fd = open(arg, O_RDONLY);
622 if (fd < 0)
623 usage(apply_usage);
4dfdbe10 624 read_stdin = 0;
c1bb9350
LT
625 apply_patch(fd);
626 close(fd);
627 }
4dfdbe10
LT
628 if (read_stdin)
629 apply_patch(0);
c1bb9350
LT
630 return 0;
631}