c0cd1520db6ef1036678046fc5c8ada174155c49
[git/git.git] / apply.c
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.
21 //
22 // --check turns on checking that the working tree matches the
23 // files that are being modified, but doesn't apply the patch
24 // --stat does just a diffstat, and doesn't actually apply
25 // --show-files shows the directory changes
26 //
27 static int merge_patch = 1;
28 static int check_index = 0;
29 static int write_index = 0;
30 static int diffstat = 0;
31 static int check = 0;
32 static int apply = 1;
33 static int show_files = 0;
34 static const char apply_usage[] = "git-apply [--stat] [--check] [--show-files] <patch>";
35
36 /*
37 * For "diff-stat" like behaviour, we keep track of the biggest change
38 * we've seen, and the longest filename. That allows us to do simple
39 * scaling.
40 */
41 static int max_change, max_len;
42
43 /*
44 * Various "current state", notably line numbers and what
45 * file (and how) we're patching right now.. The "is_xxxx"
46 * things are flags, where -1 means "don't know yet".
47 */
48 static int linenr = 1;
49
50 struct fragment {
51 unsigned long oldpos, oldlines;
52 unsigned long newpos, newlines;
53 const char *patch;
54 int size;
55 struct fragment *next;
56 };
57
58 struct patch {
59 char *new_name, *old_name, *def_name;
60 unsigned int old_mode, new_mode;
61 int is_rename, is_copy, is_new, is_delete;
62 int lines_added, lines_deleted;
63 struct fragment *fragments;
64 char *result;
65 unsigned long resultsize;
66 struct patch *next;
67 };
68
69 #define CHUNKSIZE (8192)
70 #define SLOP (16)
71
72 static void *read_patch_file(int fd, unsigned long *sizep)
73 {
74 unsigned long size = 0, alloc = CHUNKSIZE;
75 void *buffer = xmalloc(alloc);
76
77 for (;;) {
78 int nr = alloc - size;
79 if (nr < 1024) {
80 alloc += CHUNKSIZE;
81 buffer = xrealloc(buffer, alloc);
82 nr = alloc - size;
83 }
84 nr = read(fd, buffer + size, nr);
85 if (!nr)
86 break;
87 if (nr < 0) {
88 if (errno == EAGAIN)
89 continue;
90 die("git-apply: read returned %s", strerror(errno));
91 }
92 size += nr;
93 }
94 *sizep = size;
95
96 /*
97 * Make sure that we have some slop in the buffer
98 * so that we can do speculative "memcmp" etc, and
99 * see to it that it is NUL-filled.
100 */
101 if (alloc < size + SLOP)
102 buffer = xrealloc(buffer, size + SLOP);
103 memset(buffer + size, 0, SLOP);
104 return buffer;
105 }
106
107 static unsigned long linelen(const char *buffer, unsigned long size)
108 {
109 unsigned long len = 0;
110 while (size--) {
111 len++;
112 if (*buffer++ == '\n')
113 break;
114 }
115 return len;
116 }
117
118 static int is_dev_null(const char *str)
119 {
120 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
121 }
122
123 #define TERM_SPACE 1
124 #define TERM_TAB 2
125
126 static int name_terminate(const char *name, int namelen, int c, int terminate)
127 {
128 if (c == ' ' && !(terminate & TERM_SPACE))
129 return 0;
130 if (c == '\t' && !(terminate & TERM_TAB))
131 return 0;
132
133 return 1;
134 }
135
136 static char * find_name(const char *line, char *def, int p_value, int terminate)
137 {
138 int len;
139 const char *start = line;
140 char *name;
141
142 for (;;) {
143 char c = *line;
144
145 if (isspace(c)) {
146 if (c == '\n')
147 break;
148 if (name_terminate(start, line-start, c, terminate))
149 break;
150 }
151 line++;
152 if (c == '/' && !--p_value)
153 start = line;
154 }
155 if (!start)
156 return def;
157 len = line - start;
158 if (!len)
159 return def;
160
161 /*
162 * Generally we prefer the shorter name, especially
163 * if the other one is just a variation of that with
164 * something else tacked on to the end (ie "file.orig"
165 * or "file~").
166 */
167 if (def) {
168 int deflen = strlen(def);
169 if (deflen < len && !strncmp(start, def, deflen))
170 return def;
171 }
172
173 name = xmalloc(len + 1);
174 memcpy(name, start, len);
175 name[len] = 0;
176 free(def);
177 return name;
178 }
179
180 /*
181 * Get the name etc info from the --/+++ lines of a traditional patch header
182 *
183 * NOTE! This hardcodes "-p1" behaviour in filename detection.
184 *
185 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
186 * files, we can happily check the index for a match, but for creating a
187 * new file we should try to match whatever "patch" does. I have no idea.
188 */
189 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
190 {
191 int p_value = 1;
192 char *name;
193
194 first += 4; // skip "--- "
195 second += 4; // skip "+++ "
196 if (is_dev_null(first)) {
197 patch->is_new = 1;
198 patch->is_delete = 0;
199 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
200 patch->new_name = name;
201 } else if (is_dev_null(second)) {
202 patch->is_new = 0;
203 patch->is_delete = 1;
204 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
205 patch->old_name = name;
206 } else {
207 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
208 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
209 patch->old_name = patch->new_name = name;
210 }
211 if (!name)
212 die("unable to find filename in patch at line %d", linenr);
213 }
214
215 static int gitdiff_hdrend(const char *line, struct patch *patch)
216 {
217 return -1;
218 }
219
220 /*
221 * We're anal about diff header consistency, to make
222 * sure that we don't end up having strange ambiguous
223 * patches floating around.
224 *
225 * As a result, gitdiff_{old|new}name() will check
226 * their names against any previous information, just
227 * to make sure..
228 */
229 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
230 {
231 int len;
232 const char *name;
233
234 if (!orig_name && !isnull)
235 return find_name(line, NULL, 1, 0);
236
237 name = "/dev/null";
238 len = 9;
239 if (orig_name) {
240 name = orig_name;
241 len = strlen(name);
242 if (isnull)
243 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
244 }
245
246 if (*name == '/')
247 goto absolute_path;
248
249 for (;;) {
250 char c = *line++;
251 if (c == '\n')
252 break;
253 if (c != '/')
254 continue;
255 absolute_path:
256 if (memcmp(line, name, len) || line[len] != '\n')
257 break;
258 return orig_name;
259 }
260 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
261 return NULL;
262 }
263
264 static int gitdiff_oldname(const char *line, struct patch *patch)
265 {
266 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
267 return 0;
268 }
269
270 static int gitdiff_newname(const char *line, struct patch *patch)
271 {
272 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
273 return 0;
274 }
275
276 static int gitdiff_oldmode(const char *line, struct patch *patch)
277 {
278 patch->old_mode = strtoul(line, NULL, 8);
279 return 0;
280 }
281
282 static int gitdiff_newmode(const char *line, struct patch *patch)
283 {
284 patch->new_mode = strtoul(line, NULL, 8);
285 return 0;
286 }
287
288 static int gitdiff_delete(const char *line, struct patch *patch)
289 {
290 patch->is_delete = 1;
291 patch->old_name = patch->def_name;
292 return gitdiff_oldmode(line, patch);
293 }
294
295 static int gitdiff_newfile(const char *line, struct patch *patch)
296 {
297 patch->is_new = 1;
298 patch->new_name = patch->def_name;
299 return gitdiff_newmode(line, patch);
300 }
301
302 static int gitdiff_copysrc(const char *line, struct patch *patch)
303 {
304 patch->is_copy = 1;
305 patch->old_name = find_name(line, NULL, 0, 0);
306 return 0;
307 }
308
309 static int gitdiff_copydst(const char *line, struct patch *patch)
310 {
311 patch->is_copy = 1;
312 patch->new_name = find_name(line, NULL, 0, 0);
313 return 0;
314 }
315
316 static int gitdiff_renamesrc(const char *line, struct patch *patch)
317 {
318 patch->is_rename = 1;
319 patch->old_name = find_name(line, NULL, 0, 0);
320 return 0;
321 }
322
323 static int gitdiff_renamedst(const char *line, struct patch *patch)
324 {
325 patch->is_rename = 1;
326 patch->new_name = find_name(line, NULL, 0, 0);
327 return 0;
328 }
329
330 static int gitdiff_similarity(const char *line, struct patch *patch)
331 {
332 return 0;
333 }
334
335 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
336 {
337 return 0;
338 }
339
340 /*
341 * This is normal for a diff that doesn't change anything: we'll fall through
342 * into the next diff. Tell the parser to break out.
343 */
344 static int gitdiff_unrecognized(const char *line, struct patch *patch)
345 {
346 return -1;
347 }
348
349 static char *git_header_name(char *line)
350 {
351 int len;
352 char *name, *second;
353
354 /*
355 * Find the first '/'
356 */
357 name = line;
358 for (;;) {
359 char c = *name++;
360 if (c == '\n')
361 return NULL;
362 if (c == '/')
363 break;
364 }
365
366 /*
367 * We don't accept absolute paths (/dev/null) as possibly valid
368 */
369 if (name == line+1)
370 return NULL;
371
372 /*
373 * Accept a name only if it shows up twice, exactly the same
374 * form.
375 */
376 for (len = 0 ; ; len++) {
377 char c = name[len];
378
379 switch (c) {
380 default:
381 continue;
382 case '\n':
383 break;
384 case '\t': case ' ':
385 second = name+len;
386 for (;;) {
387 char c = *second++;
388 if (c == '\n')
389 return NULL;
390 if (c == '/')
391 break;
392 }
393 if (second[len] == '\n' && !memcmp(name, second, len)) {
394 char *ret = xmalloc(len + 1);
395 memcpy(ret, name, len);
396 ret[len] = 0;
397 return ret;
398 }
399 }
400 }
401 return NULL;
402 }
403
404 /* Verify that we recognize the lines following a git header */
405 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
406 {
407 unsigned long offset;
408
409 /* A git diff has explicit new/delete information, so we don't guess */
410 patch->is_new = 0;
411 patch->is_delete = 0;
412
413 /*
414 * Some things may not have the old name in the
415 * rest of the headers anywhere (pure mode changes,
416 * or removing or adding empty files), so we get
417 * the default name from the header.
418 */
419 patch->def_name = git_header_name(line + strlen("diff --git "));
420
421 line += len;
422 size -= len;
423 linenr++;
424 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
425 static const struct opentry {
426 const char *str;
427 int (*fn)(const char *, struct patch *);
428 } optable[] = {
429 { "@@ -", gitdiff_hdrend },
430 { "--- ", gitdiff_oldname },
431 { "+++ ", gitdiff_newname },
432 { "old mode ", gitdiff_oldmode },
433 { "new mode ", gitdiff_newmode },
434 { "deleted file mode ", gitdiff_delete },
435 { "new file mode ", gitdiff_newfile },
436 { "copy from ", gitdiff_copysrc },
437 { "copy to ", gitdiff_copydst },
438 { "rename old ", gitdiff_renamesrc },
439 { "rename new ", gitdiff_renamedst },
440 { "rename from ", gitdiff_renamesrc },
441 { "rename to ", gitdiff_renamedst },
442 { "similarity index ", gitdiff_similarity },
443 { "dissimilarity index ", gitdiff_dissimilarity },
444 { "", gitdiff_unrecognized },
445 };
446 int i;
447
448 len = linelen(line, size);
449 if (!len || line[len-1] != '\n')
450 break;
451 for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
452 const struct opentry *p = optable + i;
453 int oplen = strlen(p->str);
454 if (len < oplen || memcmp(p->str, line, oplen))
455 continue;
456 if (p->fn(line + oplen, patch) < 0)
457 return offset;
458 break;
459 }
460 }
461
462 return offset;
463 }
464
465 static int parse_num(const char *line, unsigned long *p)
466 {
467 char *ptr;
468
469 if (!isdigit(*line))
470 return 0;
471 *p = strtoul(line, &ptr, 10);
472 return ptr - line;
473 }
474
475 static int parse_range(const char *line, int len, int offset, const char *expect,
476 unsigned long *p1, unsigned long *p2)
477 {
478 int digits, ex;
479
480 if (offset < 0 || offset >= len)
481 return -1;
482 line += offset;
483 len -= offset;
484
485 digits = parse_num(line, p1);
486 if (!digits)
487 return -1;
488
489 offset += digits;
490 line += digits;
491 len -= digits;
492
493 *p2 = *p1;
494 if (*line == ',') {
495 digits = parse_num(line+1, p2);
496 if (!digits)
497 return -1;
498
499 offset += digits+1;
500 line += digits+1;
501 len -= digits+1;
502 }
503
504 ex = strlen(expect);
505 if (ex > len)
506 return -1;
507 if (memcmp(line, expect, ex))
508 return -1;
509
510 return offset + ex;
511 }
512
513 /*
514 * Parse a unified diff fragment header of the
515 * form "@@ -a,b +c,d @@"
516 */
517 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
518 {
519 int offset;
520
521 if (!len || line[len-1] != '\n')
522 return -1;
523
524 /* Figure out the number of lines in a fragment */
525 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
526 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
527
528 return offset;
529 }
530
531 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
532 {
533 unsigned long offset, len;
534
535 patch->is_rename = patch->is_copy = 0;
536 patch->is_new = patch->is_delete = -1;
537 patch->old_mode = patch->new_mode = 0;
538 patch->old_name = patch->new_name = NULL;
539 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
540 unsigned long nextlen;
541
542 len = linelen(line, size);
543 if (!len)
544 break;
545
546 /* Testing this early allows us to take a few shortcuts.. */
547 if (len < 6)
548 continue;
549
550 /*
551 * Make sure we don't find any unconnected patch fragmants.
552 * That's a sign that we didn't find a header, and that a
553 * patch has become corrupted/broken up.
554 */
555 if (!memcmp("@@ -", line, 4)) {
556 struct fragment dummy;
557 if (parse_fragment_header(line, len, &dummy) < 0)
558 continue;
559 error("patch fragment without header at line %d: %.*s", linenr, len-1, line);
560 }
561
562 if (size < len + 6)
563 break;
564
565 /*
566 * Git patch? It might not have a real patch, just a rename
567 * or mode change, so we handle that specially
568 */
569 if (!memcmp("diff --git ", line, 11)) {
570 int git_hdr_len = parse_git_header(line, len, size, patch);
571 if (git_hdr_len <= len)
572 continue;
573 if (!patch->old_name && !patch->new_name)
574 die("git diff header lacks filename information (line %d)", linenr);
575 *hdrsize = git_hdr_len;
576 return offset;
577 }
578
579 /** --- followed by +++ ? */
580 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
581 continue;
582
583 /*
584 * We only accept unified patches, so we want it to
585 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
586 * minimum
587 */
588 nextlen = linelen(line + len, size - len);
589 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
590 continue;
591
592 /* Ok, we'll consider it a patch */
593 parse_traditional_patch(line, line+len, patch);
594 *hdrsize = len + nextlen;
595 linenr += 2;
596 return offset;
597 }
598 return -1;
599 }
600
601 /*
602 * Parse a unified diff. Note that this really needs
603 * to parse each fragment separately, since the only
604 * way to know the difference between a "---" that is
605 * part of a patch, and a "---" that starts the next
606 * patch is to look at the line counts..
607 */
608 static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
609 {
610 int added, deleted;
611 int len = linelen(line, size), offset;
612 unsigned long oldlines, newlines;
613
614 offset = parse_fragment_header(line, len, fragment);
615 if (offset < 0)
616 return -1;
617 oldlines = fragment->oldlines;
618 newlines = fragment->newlines;
619
620 if (patch->is_new < 0) {
621 patch->is_new = !oldlines;
622 if (!oldlines)
623 patch->old_name = NULL;
624 }
625 if (patch->is_delete < 0) {
626 patch->is_delete = !newlines;
627 if (!newlines)
628 patch->new_name = NULL;
629 }
630
631 if (patch->is_new != !oldlines)
632 return error("new file depends on old contents");
633 if (patch->is_delete != !newlines) {
634 if (newlines)
635 return error("deleted file still has contents");
636 fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
637 }
638
639 /* Parse the thing.. */
640 line += len;
641 size -= len;
642 linenr++;
643 added = deleted = 0;
644 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
645 if (!oldlines && !newlines)
646 break;
647 len = linelen(line, size);
648 if (!len || line[len-1] != '\n')
649 return -1;
650 switch (*line) {
651 default:
652 return -1;
653 case ' ':
654 oldlines--;
655 newlines--;
656 break;
657 case '-':
658 deleted++;
659 oldlines--;
660 break;
661 case '+':
662 added++;
663 newlines--;
664 break;
665 /* We allow "\ No newline at end of file" */
666 case '\\':
667 if (len < 12 || memcmp(line, "\\ No newline", 12))
668 return -1;
669 break;
670 }
671 }
672 patch->lines_added += added;
673 patch->lines_deleted += deleted;
674 return offset;
675 }
676
677 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
678 {
679 unsigned long offset = 0;
680 struct fragment **fragp = &patch->fragments;
681
682 while (size > 4 && !memcmp(line, "@@ -", 4)) {
683 struct fragment *fragment;
684 int len;
685
686 fragment = xmalloc(sizeof(*fragment));
687 memset(fragment, 0, sizeof(*fragment));
688 len = parse_fragment(line, size, patch, fragment);
689 if (len <= 0)
690 die("corrupt patch at line %d", linenr);
691
692 fragment->patch = line;
693 fragment->size = len;
694
695 *fragp = fragment;
696 fragp = &fragment->next;
697
698 offset += len;
699 line += len;
700 size -= len;
701 }
702 return offset;
703 }
704
705 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
706 {
707 int hdrsize, patchsize;
708 int offset = find_header(buffer, size, &hdrsize, patch);
709
710 if (offset < 0)
711 return offset;
712
713 patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
714
715 return offset + hdrsize + patchsize;
716 }
717
718 const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
719 const char minuses[]= "----------------------------------------------------------------------";
720
721 static void show_stats(struct patch *patch)
722 {
723 char *name = patch->old_name;
724 int len, max, add, del, total;
725
726 if (!name)
727 name = patch->new_name;
728
729 /*
730 * "scale" the filename
731 */
732 len = strlen(name);
733 max = max_len;
734 if (max > 50)
735 max = 50;
736 if (len > max)
737 name += len - max;
738 len = max;
739
740 /*
741 * scale the add/delete
742 */
743 max = max_change;
744 if (max + len > 70)
745 max = 70 - len;
746
747 add = patch->lines_added;
748 del = patch->lines_deleted;
749 total = add + del;
750
751 total = (total * max + max_change / 2) / max_change;
752 add = (add * max + max_change / 2) / max_change;
753 del = total - add;
754 printf(" %-*s |%5d %.*s%.*s\n",
755 len, name, patch->lines_added + patch->lines_deleted,
756 add, pluses, del, minuses);
757 }
758
759 static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
760 {
761 int fd;
762 unsigned long got;
763
764 switch (st->st_mode & S_IFMT) {
765 case S_IFLNK:
766 return readlink(path, buf, size);
767 case S_IFREG:
768 fd = open(path, O_RDONLY);
769 if (fd < 0)
770 return error("unable to open %s", path);
771 got = 0;
772 for (;;) {
773 int ret = read(fd, buf + got, size - got);
774 if (ret < 0) {
775 if (errno == EAGAIN)
776 continue;
777 break;
778 }
779 if (!ret)
780 break;
781 got += ret;
782 }
783 close(fd);
784 return got;
785
786 default:
787 return -1;
788 }
789 }
790
791 static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
792 {
793 int i;
794 unsigned long start, backwards, forwards;
795
796 if (fragsize > size)
797 return -1;
798
799 start = 0;
800 if (line > 1) {
801 unsigned long offset = 0;
802 i = line-1;
803 while (offset + fragsize <= size) {
804 if (buf[offset++] == '\n') {
805 start = offset;
806 if (!--i)
807 break;
808 }
809 }
810 }
811
812 /* Exact line number? */
813 if (!memcmp(buf + start, fragment, fragsize))
814 return start;
815
816 /*
817 * There's probably some smart way to do this, but I'll leave
818 * that to the smart and beautiful people. I'm simple and stupid.
819 */
820 backwards = start;
821 forwards = start;
822 for (i = 0; ; i++) {
823 unsigned long try;
824 int n;
825
826 /* "backward" */
827 if (i & 1) {
828 if (!backwards) {
829 if (forwards + fragsize > size)
830 break;
831 continue;
832 }
833 do {
834 --backwards;
835 } while (backwards && buf[backwards-1] != '\n');
836 try = backwards;
837 } else {
838 while (forwards + fragsize <= size) {
839 if (buf[forwards++] == '\n')
840 break;
841 }
842 try = forwards;
843 }
844
845 if (try + fragsize > size)
846 continue;
847 if (memcmp(buf + try, fragment, fragsize))
848 continue;
849 n = (i >> 1)+1;
850 if (i & 1)
851 n = -n;
852 fprintf(stderr, "Fragment applied at offset %d\n", n);
853 return try;
854 }
855
856 /*
857 * We should start searching forward and backward.
858 */
859 return -1;
860 }
861
862 struct buffer_desc {
863 char *buffer;
864 unsigned long size;
865 unsigned long alloc;
866 };
867
868 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
869 {
870 char *buf = desc->buffer;
871 const char *patch = frag->patch;
872 int offset, size = frag->size;
873 char *old = xmalloc(size);
874 char *new = xmalloc(size);
875 int oldsize = 0, newsize = 0;
876
877 while (size > 0) {
878 int len = linelen(patch, size);
879 int plen;
880
881 if (!len)
882 break;
883
884 /*
885 * "plen" is how much of the line we should use for
886 * the actual patch data. Normally we just remove the
887 * first character on the line, but if the line is
888 * followed by "\ No newline", then we also remove the
889 * last one (which is the newline, of course).
890 */
891 plen = len-1;
892 if (len > size && patch[len] == '\\')
893 plen--;
894 switch (*patch) {
895 case ' ':
896 case '-':
897 memcpy(old + oldsize, patch + 1, plen);
898 oldsize += plen;
899 if (*patch == '-')
900 break;
901 /* Fall-through for ' ' */
902 case '+':
903 memcpy(new + newsize, patch + 1, plen);
904 newsize += plen;
905 break;
906 case '@': case '\\':
907 /* Ignore it, we already handled it */
908 break;
909 default:
910 return -1;
911 }
912 patch += len;
913 size -= len;
914 }
915
916 offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
917 if (offset >= 0) {
918 int diff = newsize - oldsize;
919 unsigned long size = desc->size + diff;
920 unsigned long alloc = desc->alloc;
921
922 if (size > alloc) {
923 alloc = size + 8192;
924 desc->alloc = alloc;
925 buf = xrealloc(buf, alloc);
926 desc->buffer = buf;
927 }
928 desc->size = size;
929 memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
930 memcpy(buf + offset, new, newsize);
931 offset = 0;
932 }
933
934 free(old);
935 free(new);
936 return offset;
937 }
938
939 static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
940 {
941 struct fragment *frag = patch->fragments;
942
943 while (frag) {
944 if (apply_one_fragment(desc, frag) < 0)
945 return error("patch failed: %s:%d", patch->old_name, frag->oldpos);
946 frag = frag->next;
947 }
948 return 0;
949 }
950
951 static int apply_data(struct patch *patch, struct stat *st)
952 {
953 char *buf;
954 unsigned long size, alloc;
955 struct buffer_desc desc;
956
957 size = 0;
958 alloc = 0;
959 buf = NULL;
960 if (patch->old_name) {
961 size = st->st_size;
962 alloc = size + 8192;
963 buf = xmalloc(alloc);
964 if (read_old_data(st, patch->old_name, buf, alloc) != size)
965 return error("read of %s failed", patch->old_name);
966 }
967
968 desc.size = size;
969 desc.alloc = alloc;
970 desc.buffer = buf;
971 if (apply_fragments(&desc, patch) < 0)
972 return -1;
973 patch->result = desc.buffer;
974 patch->resultsize = desc.size;
975
976 if (patch->is_delete && patch->resultsize)
977 return error("removal patch leaves file contents");
978
979 return 0;
980 }
981
982 static int check_patch(struct patch *patch)
983 {
984 struct stat st;
985 const char *old_name = patch->old_name;
986 const char *new_name = patch->new_name;
987
988 if (old_name) {
989 int changed;
990
991 if (lstat(old_name, &st) < 0)
992 return error("%s: %s", old_name, strerror(errno));
993 if (check_index) {
994 int pos = cache_name_pos(old_name, strlen(old_name));
995 if (pos < 0)
996 return error("%s: does not exist in index", old_name);
997 changed = ce_match_stat(active_cache[pos], &st);
998 if (changed)
999 return error("%s: does not match index", old_name);
1000 }
1001 if (patch->is_new < 0)
1002 patch->is_new = 0;
1003 st.st_mode = ntohl(create_ce_mode(st.st_mode));
1004 if (!patch->old_mode)
1005 patch->old_mode = st.st_mode;
1006 if ((st.st_mode ^ patch->old_mode) & S_IFMT)
1007 return error("%s: wrong type", old_name);
1008 if (st.st_mode != patch->old_mode)
1009 fprintf(stderr, "warning: %s has type %o, expected %o\n",
1010 old_name, st.st_mode, patch->old_mode);
1011 }
1012
1013 if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1014 if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1015 return error("%s: already exists in index", new_name);
1016 if (!lstat(new_name, &st))
1017 return error("%s: already exists in working directory", new_name);
1018 if (errno != ENOENT)
1019 return error("%s: %s", new_name, strerror(errno));
1020 if (!patch->new_mode)
1021 patch->new_mode = S_IFREG | 0644;
1022 }
1023
1024 if (new_name && old_name) {
1025 int same = !strcmp(old_name, new_name);
1026 if (!patch->new_mode)
1027 patch->new_mode = patch->old_mode;
1028 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
1029 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1030 patch->new_mode, new_name, patch->old_mode,
1031 same ? "" : " of ", same ? "" : old_name);
1032 }
1033
1034 if (apply_data(patch, &st) < 0)
1035 return error("%s: patch does not apply", old_name);
1036 return 0;
1037 }
1038
1039 static int check_patch_list(struct patch *patch)
1040 {
1041 int error = 0;
1042
1043 for (;patch ; patch = patch->next)
1044 error |= check_patch(patch);
1045 return error;
1046 }
1047
1048 static void show_file(int c, unsigned int mode, const char *name)
1049 {
1050 printf("%c %o %s\n", c, mode, name);
1051 }
1052
1053 static void show_file_list(struct patch *patch)
1054 {
1055 for (;patch ; patch = patch->next) {
1056 if (patch->is_rename) {
1057 show_file('-', patch->old_mode, patch->old_name);
1058 show_file('+', patch->new_mode, patch->new_name);
1059 continue;
1060 }
1061 if (patch->is_copy || patch->is_new) {
1062 show_file('+', patch->new_mode, patch->new_name);
1063 continue;
1064 }
1065 if (patch->is_delete) {
1066 show_file('-', patch->old_mode, patch->old_name);
1067 continue;
1068 }
1069 if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) {
1070 printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name);
1071 continue;
1072 }
1073 printf("M %o %s\n", patch->old_mode, patch->old_name);
1074 }
1075 }
1076
1077 static void stat_patch_list(struct patch *patch)
1078 {
1079 int files, adds, dels;
1080
1081 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
1082 files++;
1083 adds += patch->lines_added;
1084 dels += patch->lines_deleted;
1085 show_stats(patch);
1086 }
1087
1088 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
1089 }
1090
1091 static void patch_stats(struct patch *patch)
1092 {
1093 int lines = patch->lines_added + patch->lines_deleted;
1094
1095 if (lines > max_change)
1096 max_change = lines;
1097 if (patch->old_name) {
1098 int len = strlen(patch->old_name);
1099 if (len > max_len)
1100 max_len = len;
1101 }
1102 if (patch->new_name) {
1103 int len = strlen(patch->new_name);
1104 if (len > max_len)
1105 max_len = len;
1106 }
1107 }
1108
1109 static void remove_file(struct patch *patch)
1110 {
1111 if (write_index) {
1112 if (remove_file_from_cache(patch->old_name) < 0)
1113 die("unable to remove %s from index", patch->old_name);
1114 }
1115 unlink(patch->old_name);
1116 }
1117
1118 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
1119 {
1120 struct stat st;
1121 struct cache_entry *ce;
1122 int namelen = strlen(path);
1123 unsigned ce_size = cache_entry_size(namelen);
1124
1125 if (!write_index)
1126 return;
1127
1128 ce = xmalloc(ce_size);
1129 memset(ce, 0, ce_size);
1130 memcpy(ce->name, path, namelen);
1131 ce->ce_mode = create_ce_mode(mode);
1132 ce->ce_flags = htons(namelen);
1133 if (lstat(path, &st) < 0)
1134 die("unable to stat newly created file %s", path);
1135 fill_stat_cache_info(ce, &st);
1136 if (write_sha1_file(buf, size, "blob", ce->sha1) < 0)
1137 die("unable to create backing store for newly created file %s", path);
1138 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
1139 die("unable to add cache entry for %s", path);
1140 }
1141
1142 static void create_file(struct patch *patch)
1143 {
1144 const char *path = patch->new_name;
1145 unsigned mode = patch->new_mode;
1146 unsigned long size = patch->resultsize;
1147 char *buf = patch->result;
1148
1149 if (!mode)
1150 mode = S_IFREG | 0644;
1151 if (S_ISREG(mode)) {
1152 int fd;
1153 mode = (mode & 0100) ? 0777 : 0666;
1154 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
1155 if (fd < 0)
1156 die("unable to create file %s (%s)", path, strerror(errno));
1157 if (write(fd, buf, size) != size)
1158 die("unable to write file %s", path);
1159 close(fd);
1160 add_index_file(path, mode, buf, size);
1161 return;
1162 }
1163 if (S_ISLNK(mode)) {
1164 if (size && buf[size-1] == '\n')
1165 size--;
1166 buf[size] = 0;
1167 if (symlink(buf, path) < 0)
1168 die("unable to write symlink %s", path);
1169 add_index_file(path, mode, buf, size);
1170 return;
1171 }
1172 die("unable to write file mode %o", mode);
1173 }
1174
1175 static void write_out_one_result(struct patch *patch)
1176 {
1177 if (patch->is_delete > 0) {
1178 remove_file(patch);
1179 return;
1180 }
1181 if (patch->is_new > 0 || patch->is_copy) {
1182 create_file(patch);
1183 return;
1184 }
1185 /*
1186 * Rename or modification boils down to the same
1187 * thing: remove the old, write the new
1188 */
1189 remove_file(patch);
1190 create_file(patch);
1191 }
1192
1193 static void write_out_results(struct patch *list)
1194 {
1195 if (!list)
1196 die("No changes");
1197
1198 while (list) {
1199 write_out_one_result(list);
1200 list = list->next;
1201 }
1202 }
1203
1204 static struct cache_file cache_file;
1205
1206 static int apply_patch(int fd)
1207 {
1208 int newfd;
1209 unsigned long offset, size;
1210 char *buffer = read_patch_file(fd, &size);
1211 struct patch *list = NULL, **listp = &list;
1212
1213 if (!buffer)
1214 return -1;
1215 offset = 0;
1216 while (size > 0) {
1217 struct patch *patch;
1218 int nr;
1219
1220 patch = xmalloc(sizeof(*patch));
1221 memset(patch, 0, sizeof(*patch));
1222 nr = parse_chunk(buffer + offset, size, patch);
1223 if (nr < 0)
1224 break;
1225 patch_stats(patch);
1226 *listp = patch;
1227 listp = &patch->next;
1228 offset += nr;
1229 size -= nr;
1230 }
1231
1232 newfd = -1;
1233 write_index = check_index && apply;
1234 if (write_index)
1235 newfd = hold_index_file_for_update(&cache_file, get_index_file());
1236 if (check_index) {
1237 if (read_cache() < 0)
1238 die("unable to read index file");
1239 }
1240
1241 if ((check || apply) && check_patch_list(list) < 0)
1242 exit(1);
1243
1244 if (apply)
1245 write_out_results(list);
1246
1247 if (write_index) {
1248 if (write_cache(newfd, active_cache, active_nr) ||
1249 commit_index_file(&cache_file))
1250 die("Unable to write new cachefile");
1251 }
1252
1253 if (show_files)
1254 show_file_list(list);
1255
1256 if (diffstat)
1257 stat_patch_list(list);
1258
1259 free(buffer);
1260 return 0;
1261 }
1262
1263 int main(int argc, char **argv)
1264 {
1265 int i;
1266 int read_stdin = 1;
1267
1268 for (i = 1; i < argc; i++) {
1269 const char *arg = argv[i];
1270 int fd;
1271
1272 if (!strcmp(arg, "-")) {
1273 apply_patch(0);
1274 read_stdin = 0;
1275 continue;
1276 }
1277 if (!strcmp(arg, "--no-merge")) {
1278 merge_patch = 0;
1279 continue;
1280 }
1281 if (!strcmp(arg, "--stat")) {
1282 apply = 0;
1283 diffstat = 1;
1284 continue;
1285 }
1286 if (!strcmp(arg, "--check")) {
1287 apply = 0;
1288 check = 1;
1289 continue;
1290 }
1291 if (!strcmp(arg, "--index")) {
1292 check_index = 1;
1293 continue;
1294 }
1295 if (!strcmp(arg, "--show-files")) {
1296 show_files = 1;
1297 continue;
1298 }
1299 fd = open(arg, O_RDONLY);
1300 if (fd < 0)
1301 usage(apply_usage);
1302 read_stdin = 0;
1303 apply_patch(fd);
1304 close(fd);
1305 }
1306 if (read_stdin)
1307 apply_patch(0);
1308 return 0;
1309 }