git-am: interactive should fail gracefully.
[git/dkf.git] / apply.c
blob155fbe84da8b6d8dfb231fe508b68a36d52ffc60
1 /*
2 * apply.c
4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
8 */
9 #include <ctype.h>
10 #include <fnmatch.h>
11 #include "cache.h"
13 // --check turns on checking that the working tree matches the
14 // files that are being modified, but doesn't apply the patch
15 // --stat does just a diffstat, and doesn't actually apply
16 // --show-files shows the directory changes
17 // --show-index-info shows the old and new index info for paths if available.
19 static int check_index = 0;
20 static int write_index = 0;
21 static int diffstat = 0;
22 static int summary = 0;
23 static int check = 0;
24 static int apply = 1;
25 static int show_files = 0;
26 static int show_index_info = 0;
27 static const char apply_usage[] =
28 "git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] <patch>...";
31 * For "diff-stat" like behaviour, we keep track of the biggest change
32 * we've seen, and the longest filename. That allows us to do simple
33 * scaling.
35 static int max_change, max_len;
38 * Various "current state", notably line numbers and what
39 * file (and how) we're patching right now.. The "is_xxxx"
40 * things are flags, where -1 means "don't know yet".
42 static int linenr = 1;
44 struct fragment {
45 unsigned long oldpos, oldlines;
46 unsigned long newpos, newlines;
47 const char *patch;
48 int size;
49 struct fragment *next;
52 struct patch {
53 char *new_name, *old_name, *def_name;
54 unsigned int old_mode, new_mode;
55 int is_rename, is_copy, is_new, is_delete;
56 int lines_added, lines_deleted;
57 int score;
58 struct fragment *fragments;
59 char *result;
60 unsigned long resultsize;
61 char old_sha1_prefix[41];
62 char new_sha1_prefix[41];
63 struct patch *next;
66 #define CHUNKSIZE (8192)
67 #define SLOP (16)
69 static void *read_patch_file(int fd, unsigned long *sizep)
71 unsigned long size = 0, alloc = CHUNKSIZE;
72 void *buffer = xmalloc(alloc);
74 for (;;) {
75 int nr = alloc - size;
76 if (nr < 1024) {
77 alloc += CHUNKSIZE;
78 buffer = xrealloc(buffer, alloc);
79 nr = alloc - size;
81 nr = read(fd, buffer + size, nr);
82 if (!nr)
83 break;
84 if (nr < 0) {
85 if (errno == EAGAIN)
86 continue;
87 die("git-apply: read returned %s", strerror(errno));
89 size += nr;
91 *sizep = size;
94 * Make sure that we have some slop in the buffer
95 * so that we can do speculative "memcmp" etc, and
96 * see to it that it is NUL-filled.
98 if (alloc < size + SLOP)
99 buffer = xrealloc(buffer, size + SLOP);
100 memset(buffer + size, 0, SLOP);
101 return buffer;
104 static unsigned long linelen(const char *buffer, unsigned long size)
106 unsigned long len = 0;
107 while (size--) {
108 len++;
109 if (*buffer++ == '\n')
110 break;
112 return len;
115 static int is_dev_null(const char *str)
117 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
120 #define TERM_SPACE 1
121 #define TERM_TAB 2
123 static int name_terminate(const char *name, int namelen, int c, int terminate)
125 if (c == ' ' && !(terminate & TERM_SPACE))
126 return 0;
127 if (c == '\t' && !(terminate & TERM_TAB))
128 return 0;
130 return 1;
133 static char * find_name(const char *line, char *def, int p_value, int terminate)
135 int len;
136 const char *start = line;
137 char *name;
139 for (;;) {
140 char c = *line;
142 if (isspace(c)) {
143 if (c == '\n')
144 break;
145 if (name_terminate(start, line-start, c, terminate))
146 break;
148 line++;
149 if (c == '/' && !--p_value)
150 start = line;
152 if (!start)
153 return def;
154 len = line - start;
155 if (!len)
156 return def;
159 * Generally we prefer the shorter name, especially
160 * if the other one is just a variation of that with
161 * something else tacked on to the end (ie "file.orig"
162 * or "file~").
164 if (def) {
165 int deflen = strlen(def);
166 if (deflen < len && !strncmp(start, def, deflen))
167 return def;
170 name = xmalloc(len + 1);
171 memcpy(name, start, len);
172 name[len] = 0;
173 free(def);
174 return name;
178 * Get the name etc info from the --/+++ lines of a traditional patch header
180 * NOTE! This hardcodes "-p1" behaviour in filename detection.
182 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
183 * files, we can happily check the index for a match, but for creating a
184 * new file we should try to match whatever "patch" does. I have no idea.
186 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
188 int p_value = 1;
189 char *name;
191 first += 4; // skip "--- "
192 second += 4; // skip "+++ "
193 if (is_dev_null(first)) {
194 patch->is_new = 1;
195 patch->is_delete = 0;
196 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
197 patch->new_name = name;
198 } else if (is_dev_null(second)) {
199 patch->is_new = 0;
200 patch->is_delete = 1;
201 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
202 patch->old_name = name;
203 } else {
204 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
205 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
206 patch->old_name = patch->new_name = name;
208 if (!name)
209 die("unable to find filename in patch at line %d", linenr);
212 static int gitdiff_hdrend(const char *line, struct patch *patch)
214 return -1;
218 * We're anal about diff header consistency, to make
219 * sure that we don't end up having strange ambiguous
220 * patches floating around.
222 * As a result, gitdiff_{old|new}name() will check
223 * their names against any previous information, just
224 * to make sure..
226 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
228 int len;
229 const char *name;
231 if (!orig_name && !isnull)
232 return find_name(line, NULL, 1, 0);
234 name = "/dev/null";
235 len = 9;
236 if (orig_name) {
237 name = orig_name;
238 len = strlen(name);
239 if (isnull)
240 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
243 if (*name == '/')
244 goto absolute_path;
246 for (;;) {
247 char c = *line++;
248 if (c == '\n')
249 break;
250 if (c != '/')
251 continue;
252 absolute_path:
253 if (memcmp(line, name, len) || line[len] != '\n')
254 break;
255 return orig_name;
257 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
258 return NULL;
261 static int gitdiff_oldname(const char *line, struct patch *patch)
263 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
264 return 0;
267 static int gitdiff_newname(const char *line, struct patch *patch)
269 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
270 return 0;
273 static int gitdiff_oldmode(const char *line, struct patch *patch)
275 patch->old_mode = strtoul(line, NULL, 8);
276 return 0;
279 static int gitdiff_newmode(const char *line, struct patch *patch)
281 patch->new_mode = strtoul(line, NULL, 8);
282 return 0;
285 static int gitdiff_delete(const char *line, struct patch *patch)
287 patch->is_delete = 1;
288 patch->old_name = patch->def_name;
289 return gitdiff_oldmode(line, patch);
292 static int gitdiff_newfile(const char *line, struct patch *patch)
294 patch->is_new = 1;
295 patch->new_name = patch->def_name;
296 return gitdiff_newmode(line, patch);
299 static int gitdiff_copysrc(const char *line, struct patch *patch)
301 patch->is_copy = 1;
302 patch->old_name = find_name(line, NULL, 0, 0);
303 return 0;
306 static int gitdiff_copydst(const char *line, struct patch *patch)
308 patch->is_copy = 1;
309 patch->new_name = find_name(line, NULL, 0, 0);
310 return 0;
313 static int gitdiff_renamesrc(const char *line, struct patch *patch)
315 patch->is_rename = 1;
316 patch->old_name = find_name(line, NULL, 0, 0);
317 return 0;
320 static int gitdiff_renamedst(const char *line, struct patch *patch)
322 patch->is_rename = 1;
323 patch->new_name = find_name(line, NULL, 0, 0);
324 return 0;
327 static int gitdiff_similarity(const char *line, struct patch *patch)
329 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
330 patch->score = 0;
331 return 0;
334 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
336 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
337 patch->score = 0;
338 return 0;
341 static int gitdiff_index(const char *line, struct patch *patch)
343 /* index line is N hexadecimal, "..", N hexadecimal,
344 * and optional space with octal mode.
346 const char *ptr, *eol;
347 int len;
349 ptr = strchr(line, '.');
350 if (!ptr || ptr[1] != '.' || 40 <= ptr - line)
351 return 0;
352 len = ptr - line;
353 memcpy(patch->old_sha1_prefix, line, len);
354 patch->old_sha1_prefix[len] = 0;
356 line = ptr + 2;
357 ptr = strchr(line, ' ');
358 eol = strchr(line, '\n');
360 if (!ptr || eol < ptr)
361 ptr = eol;
362 len = ptr - line;
364 if (40 <= len)
365 return 0;
366 memcpy(patch->new_sha1_prefix, line, len);
367 patch->new_sha1_prefix[len] = 0;
368 if (*ptr == ' ')
369 patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
370 return 0;
374 * This is normal for a diff that doesn't change anything: we'll fall through
375 * into the next diff. Tell the parser to break out.
377 static int gitdiff_unrecognized(const char *line, struct patch *patch)
379 return -1;
382 static char *git_header_name(char *line)
384 int len;
385 char *name, *second;
388 * Find the first '/'
390 name = line;
391 for (;;) {
392 char c = *name++;
393 if (c == '\n')
394 return NULL;
395 if (c == '/')
396 break;
400 * We don't accept absolute paths (/dev/null) as possibly valid
402 if (name == line+1)
403 return NULL;
406 * Accept a name only if it shows up twice, exactly the same
407 * form.
409 for (len = 0 ; ; len++) {
410 char c = name[len];
412 switch (c) {
413 default:
414 continue;
415 case '\n':
416 return NULL;
417 case '\t': case ' ':
418 second = name+len;
419 for (;;) {
420 char c = *second++;
421 if (c == '\n')
422 return NULL;
423 if (c == '/')
424 break;
426 if (second[len] == '\n' && !memcmp(name, second, len)) {
427 char *ret = xmalloc(len + 1);
428 memcpy(ret, name, len);
429 ret[len] = 0;
430 return ret;
434 return NULL;
437 /* Verify that we recognize the lines following a git header */
438 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
440 unsigned long offset;
442 /* A git diff has explicit new/delete information, so we don't guess */
443 patch->is_new = 0;
444 patch->is_delete = 0;
447 * Some things may not have the old name in the
448 * rest of the headers anywhere (pure mode changes,
449 * or removing or adding empty files), so we get
450 * the default name from the header.
452 patch->def_name = git_header_name(line + strlen("diff --git "));
454 line += len;
455 size -= len;
456 linenr++;
457 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
458 static const struct opentry {
459 const char *str;
460 int (*fn)(const char *, struct patch *);
461 } optable[] = {
462 { "@@ -", gitdiff_hdrend },
463 { "--- ", gitdiff_oldname },
464 { "+++ ", gitdiff_newname },
465 { "old mode ", gitdiff_oldmode },
466 { "new mode ", gitdiff_newmode },
467 { "deleted file mode ", gitdiff_delete },
468 { "new file mode ", gitdiff_newfile },
469 { "copy from ", gitdiff_copysrc },
470 { "copy to ", gitdiff_copydst },
471 { "rename old ", gitdiff_renamesrc },
472 { "rename new ", gitdiff_renamedst },
473 { "rename from ", gitdiff_renamesrc },
474 { "rename to ", gitdiff_renamedst },
475 { "similarity index ", gitdiff_similarity },
476 { "dissimilarity index ", gitdiff_dissimilarity },
477 { "index ", gitdiff_index },
478 { "", gitdiff_unrecognized },
480 int i;
482 len = linelen(line, size);
483 if (!len || line[len-1] != '\n')
484 break;
485 for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
486 const struct opentry *p = optable + i;
487 int oplen = strlen(p->str);
488 if (len < oplen || memcmp(p->str, line, oplen))
489 continue;
490 if (p->fn(line + oplen, patch) < 0)
491 return offset;
492 break;
496 return offset;
499 static int parse_num(const char *line, unsigned long *p)
501 char *ptr;
503 if (!isdigit(*line))
504 return 0;
505 *p = strtoul(line, &ptr, 10);
506 return ptr - line;
509 static int parse_range(const char *line, int len, int offset, const char *expect,
510 unsigned long *p1, unsigned long *p2)
512 int digits, ex;
514 if (offset < 0 || offset >= len)
515 return -1;
516 line += offset;
517 len -= offset;
519 digits = parse_num(line, p1);
520 if (!digits)
521 return -1;
523 offset += digits;
524 line += digits;
525 len -= digits;
527 *p2 = *p1;
528 if (*line == ',') {
529 digits = parse_num(line+1, p2);
530 if (!digits)
531 return -1;
533 offset += digits+1;
534 line += digits+1;
535 len -= digits+1;
538 ex = strlen(expect);
539 if (ex > len)
540 return -1;
541 if (memcmp(line, expect, ex))
542 return -1;
544 return offset + ex;
548 * Parse a unified diff fragment header of the
549 * form "@@ -a,b +c,d @@"
551 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
553 int offset;
555 if (!len || line[len-1] != '\n')
556 return -1;
558 /* Figure out the number of lines in a fragment */
559 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
560 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
562 return offset;
565 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
567 unsigned long offset, len;
569 patch->is_rename = patch->is_copy = 0;
570 patch->is_new = patch->is_delete = -1;
571 patch->old_mode = patch->new_mode = 0;
572 patch->old_name = patch->new_name = NULL;
573 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
574 unsigned long nextlen;
576 len = linelen(line, size);
577 if (!len)
578 break;
580 /* Testing this early allows us to take a few shortcuts.. */
581 if (len < 6)
582 continue;
585 * Make sure we don't find any unconnected patch fragmants.
586 * That's a sign that we didn't find a header, and that a
587 * patch has become corrupted/broken up.
589 if (!memcmp("@@ -", line, 4)) {
590 struct fragment dummy;
591 if (parse_fragment_header(line, len, &dummy) < 0)
592 continue;
593 error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
596 if (size < len + 6)
597 break;
600 * Git patch? It might not have a real patch, just a rename
601 * or mode change, so we handle that specially
603 if (!memcmp("diff --git ", line, 11)) {
604 int git_hdr_len = parse_git_header(line, len, size, patch);
605 if (git_hdr_len <= len)
606 continue;
607 if (!patch->old_name && !patch->new_name) {
608 if (!patch->def_name)
609 die("git diff header lacks filename information (line %d)", linenr);
610 patch->old_name = patch->new_name = patch->def_name;
612 *hdrsize = git_hdr_len;
613 return offset;
616 /** --- followed by +++ ? */
617 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
618 continue;
621 * We only accept unified patches, so we want it to
622 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
623 * minimum
625 nextlen = linelen(line + len, size - len);
626 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
627 continue;
629 /* Ok, we'll consider it a patch */
630 parse_traditional_patch(line, line+len, patch);
631 *hdrsize = len + nextlen;
632 linenr += 2;
633 return offset;
635 return -1;
639 * Parse a unified diff. Note that this really needs
640 * to parse each fragment separately, since the only
641 * way to know the difference between a "---" that is
642 * part of a patch, and a "---" that starts the next
643 * patch is to look at the line counts..
645 static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
647 int added, deleted;
648 int len = linelen(line, size), offset;
649 unsigned long oldlines, newlines;
651 offset = parse_fragment_header(line, len, fragment);
652 if (offset < 0)
653 return -1;
654 oldlines = fragment->oldlines;
655 newlines = fragment->newlines;
657 if (patch->is_new < 0) {
658 patch->is_new = !oldlines;
659 if (!oldlines)
660 patch->old_name = NULL;
662 if (patch->is_delete < 0) {
663 patch->is_delete = !newlines;
664 if (!newlines)
665 patch->new_name = NULL;
668 if (patch->is_new != !oldlines)
669 return error("new file depends on old contents");
670 if (patch->is_delete != !newlines) {
671 if (newlines)
672 return error("deleted file still has contents");
673 fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
676 /* Parse the thing.. */
677 line += len;
678 size -= len;
679 linenr++;
680 added = deleted = 0;
681 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
682 if (!oldlines && !newlines)
683 break;
684 len = linelen(line, size);
685 if (!len || line[len-1] != '\n')
686 return -1;
687 switch (*line) {
688 default:
689 return -1;
690 case ' ':
691 oldlines--;
692 newlines--;
693 break;
694 case '-':
695 deleted++;
696 oldlines--;
697 break;
698 case '+':
699 added++;
700 newlines--;
701 break;
703 /* We allow "\ No newline at end of file". Depending
704 * on locale settings when the patch was produced we
705 * don't know what this line looks like. The only
706 * thing we do know is that it begins with "\ ".
707 * Checking for 12 is just for sanity check -- any
708 * l10n of "\ No newline..." is at least that long.
710 case '\\':
711 if (len < 12 || memcmp(line, "\\ ", 2))
712 return -1;
713 break;
716 /* If a fragment ends with an incomplete line, we failed to include
717 * it in the above loop because we hit oldlines == newlines == 0
718 * before seeing it.
720 if (12 < size && !memcmp(line, "\\ ", 2))
721 offset += linelen(line, size);
723 patch->lines_added += added;
724 patch->lines_deleted += deleted;
725 return offset;
728 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
730 unsigned long offset = 0;
731 struct fragment **fragp = &patch->fragments;
733 while (size > 4 && !memcmp(line, "@@ -", 4)) {
734 struct fragment *fragment;
735 int len;
737 fragment = xmalloc(sizeof(*fragment));
738 memset(fragment, 0, sizeof(*fragment));
739 len = parse_fragment(line, size, patch, fragment);
740 if (len <= 0)
741 die("corrupt patch at line %d", linenr);
743 fragment->patch = line;
744 fragment->size = len;
746 *fragp = fragment;
747 fragp = &fragment->next;
749 offset += len;
750 line += len;
751 size -= len;
753 return offset;
756 static inline int metadata_changes(struct patch *patch)
758 return patch->is_rename > 0 ||
759 patch->is_copy > 0 ||
760 patch->is_new > 0 ||
761 patch->is_delete ||
762 (patch->old_mode && patch->new_mode &&
763 patch->old_mode != patch->new_mode);
766 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
768 int hdrsize, patchsize;
769 int offset = find_header(buffer, size, &hdrsize, patch);
771 if (offset < 0)
772 return offset;
774 patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
776 if (!patchsize && !metadata_changes(patch))
777 die("patch with only garbage at line %d", linenr);
779 return offset + hdrsize + patchsize;
782 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
783 static const char minuses[]= "----------------------------------------------------------------------";
785 static void show_stats(struct patch *patch)
787 const char *prefix = "";
788 char *name = patch->new_name;
789 int len, max, add, del, total;
791 if (!name)
792 name = patch->old_name;
795 * "scale" the filename
797 len = strlen(name);
798 max = max_len;
799 if (max > 50)
800 max = 50;
801 if (len > max) {
802 char *slash;
803 prefix = "...";
804 max -= 3;
805 name += len - max;
806 slash = strchr(name, '/');
807 if (slash)
808 name = slash;
810 len = max;
813 * scale the add/delete
815 max = max_change;
816 if (max + len > 70)
817 max = 70 - len;
819 add = patch->lines_added;
820 del = patch->lines_deleted;
821 total = add + del;
823 if (max_change > 0) {
824 total = (total * max + max_change / 2) / max_change;
825 add = (add * max + max_change / 2) / max_change;
826 del = total - add;
828 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
829 len, name, patch->lines_added + patch->lines_deleted,
830 add, pluses, del, minuses);
833 static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
835 int fd;
836 unsigned long got;
838 switch (st->st_mode & S_IFMT) {
839 case S_IFLNK:
840 return readlink(path, buf, size);
841 case S_IFREG:
842 fd = open(path, O_RDONLY);
843 if (fd < 0)
844 return error("unable to open %s", path);
845 got = 0;
846 for (;;) {
847 int ret = read(fd, buf + got, size - got);
848 if (ret < 0) {
849 if (errno == EAGAIN)
850 continue;
851 break;
853 if (!ret)
854 break;
855 got += ret;
857 close(fd);
858 return got;
860 default:
861 return -1;
865 static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
867 int i;
868 unsigned long start, backwards, forwards;
870 if (fragsize > size)
871 return -1;
873 start = 0;
874 if (line > 1) {
875 unsigned long offset = 0;
876 i = line-1;
877 while (offset + fragsize <= size) {
878 if (buf[offset++] == '\n') {
879 start = offset;
880 if (!--i)
881 break;
886 /* Exact line number? */
887 if (!memcmp(buf + start, fragment, fragsize))
888 return start;
891 * There's probably some smart way to do this, but I'll leave
892 * that to the smart and beautiful people. I'm simple and stupid.
894 backwards = start;
895 forwards = start;
896 for (i = 0; ; i++) {
897 unsigned long try;
898 int n;
900 /* "backward" */
901 if (i & 1) {
902 if (!backwards) {
903 if (forwards + fragsize > size)
904 break;
905 continue;
907 do {
908 --backwards;
909 } while (backwards && buf[backwards-1] != '\n');
910 try = backwards;
911 } else {
912 while (forwards + fragsize <= size) {
913 if (buf[forwards++] == '\n')
914 break;
916 try = forwards;
919 if (try + fragsize > size)
920 continue;
921 if (memcmp(buf + try, fragment, fragsize))
922 continue;
923 n = (i >> 1)+1;
924 if (i & 1)
925 n = -n;
926 return try;
930 * We should start searching forward and backward.
932 return -1;
935 struct buffer_desc {
936 char *buffer;
937 unsigned long size;
938 unsigned long alloc;
941 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
943 char *buf = desc->buffer;
944 const char *patch = frag->patch;
945 int offset, size = frag->size;
946 char *old = xmalloc(size);
947 char *new = xmalloc(size);
948 int oldsize = 0, newsize = 0;
950 while (size > 0) {
951 int len = linelen(patch, size);
952 int plen;
954 if (!len)
955 break;
958 * "plen" is how much of the line we should use for
959 * the actual patch data. Normally we just remove the
960 * first character on the line, but if the line is
961 * followed by "\ No newline", then we also remove the
962 * last one (which is the newline, of course).
964 plen = len-1;
965 if (len < size && patch[len] == '\\')
966 plen--;
967 switch (*patch) {
968 case ' ':
969 case '-':
970 memcpy(old + oldsize, patch + 1, plen);
971 oldsize += plen;
972 if (*patch == '-')
973 break;
974 /* Fall-through for ' ' */
975 case '+':
976 memcpy(new + newsize, patch + 1, plen);
977 newsize += plen;
978 break;
979 case '@': case '\\':
980 /* Ignore it, we already handled it */
981 break;
982 default:
983 return -1;
985 patch += len;
986 size -= len;
989 offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
990 if (offset >= 0) {
991 int diff = newsize - oldsize;
992 unsigned long size = desc->size + diff;
993 unsigned long alloc = desc->alloc;
995 if (size > alloc) {
996 alloc = size + 8192;
997 desc->alloc = alloc;
998 buf = xrealloc(buf, alloc);
999 desc->buffer = buf;
1001 desc->size = size;
1002 memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1003 memcpy(buf + offset, new, newsize);
1004 offset = 0;
1007 free(old);
1008 free(new);
1009 return offset;
1012 static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1014 struct fragment *frag = patch->fragments;
1016 while (frag) {
1017 if (apply_one_fragment(desc, frag) < 0)
1018 return error("patch failed: %s:%ld", patch->old_name, frag->oldpos);
1019 frag = frag->next;
1021 return 0;
1024 static int apply_data(struct patch *patch, struct stat *st)
1026 char *buf;
1027 unsigned long size, alloc;
1028 struct buffer_desc desc;
1030 size = 0;
1031 alloc = 0;
1032 buf = NULL;
1033 if (patch->old_name) {
1034 size = st->st_size;
1035 alloc = size + 8192;
1036 buf = xmalloc(alloc);
1037 if (read_old_data(st, patch->old_name, buf, alloc) != size)
1038 return error("read of %s failed", patch->old_name);
1041 desc.size = size;
1042 desc.alloc = alloc;
1043 desc.buffer = buf;
1044 if (apply_fragments(&desc, patch) < 0)
1045 return -1;
1046 patch->result = desc.buffer;
1047 patch->resultsize = desc.size;
1049 if (patch->is_delete && patch->resultsize)
1050 return error("removal patch leaves file contents");
1052 return 0;
1055 static int check_patch(struct patch *patch)
1057 struct stat st;
1058 const char *old_name = patch->old_name;
1059 const char *new_name = patch->new_name;
1061 if (old_name) {
1062 int changed;
1063 int stat_ret = lstat(old_name, &st);
1065 if (check_index) {
1066 int pos = cache_name_pos(old_name, strlen(old_name));
1067 if (pos < 0)
1068 return error("%s: does not exist in index",
1069 old_name);
1070 if (stat_ret < 0) {
1071 struct checkout costate;
1072 if (errno != ENOENT)
1073 return error("%s: %s", old_name,
1074 strerror(errno));
1075 /* checkout */
1076 costate.base_dir = "";
1077 costate.base_dir_len = 0;
1078 costate.force = 0;
1079 costate.quiet = 0;
1080 costate.not_new = 0;
1081 costate.refresh_cache = 1;
1082 if (checkout_entry(active_cache[pos],
1083 &costate) ||
1084 lstat(old_name, &st))
1085 return -1;
1088 changed = ce_match_stat(active_cache[pos], &st);
1089 if (changed)
1090 return error("%s: does not match index",
1091 old_name);
1093 else if (stat_ret < 0)
1094 return error("%s: %s", old_name, strerror(errno));
1096 if (patch->is_new < 0)
1097 patch->is_new = 0;
1098 st.st_mode = ntohl(create_ce_mode(st.st_mode));
1099 if (!patch->old_mode)
1100 patch->old_mode = st.st_mode;
1101 if ((st.st_mode ^ patch->old_mode) & S_IFMT)
1102 return error("%s: wrong type", old_name);
1103 if (st.st_mode != patch->old_mode)
1104 fprintf(stderr, "warning: %s has type %o, expected %o\n",
1105 old_name, st.st_mode, patch->old_mode);
1108 if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1109 if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1110 return error("%s: already exists in index", new_name);
1111 if (!lstat(new_name, &st))
1112 return error("%s: already exists in working directory", new_name);
1113 if (errno != ENOENT)
1114 return error("%s: %s", new_name, strerror(errno));
1115 if (!patch->new_mode) {
1116 if (patch->is_new)
1117 patch->new_mode = S_IFREG | 0644;
1118 else
1119 patch->new_mode = patch->old_mode;
1123 if (new_name && old_name) {
1124 int same = !strcmp(old_name, new_name);
1125 if (!patch->new_mode)
1126 patch->new_mode = patch->old_mode;
1127 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
1128 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1129 patch->new_mode, new_name, patch->old_mode,
1130 same ? "" : " of ", same ? "" : old_name);
1133 if (apply_data(patch, &st) < 0)
1134 return error("%s: patch does not apply", old_name);
1135 return 0;
1138 static int check_patch_list(struct patch *patch)
1140 int error = 0;
1142 for (;patch ; patch = patch->next)
1143 error |= check_patch(patch);
1144 return error;
1147 static void show_file(int c, unsigned int mode, const char *name)
1149 printf("%c %o %s\n", c, mode, name);
1152 static void show_file_list(struct patch *patch)
1154 for (;patch ; patch = patch->next) {
1155 if (patch->is_rename) {
1156 show_file('-', patch->old_mode, patch->old_name);
1157 show_file('+', patch->new_mode, patch->new_name);
1158 continue;
1160 if (patch->is_copy || patch->is_new) {
1161 show_file('+', patch->new_mode, patch->new_name);
1162 continue;
1164 if (patch->is_delete) {
1165 show_file('-', patch->old_mode, patch->old_name);
1166 continue;
1168 if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) {
1169 printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name);
1170 continue;
1172 printf("M %o %s\n", patch->old_mode, patch->old_name);
1176 static inline int is_null_sha1(const unsigned char *sha1)
1178 return !memcmp(sha1, null_sha1, 20);
1181 static void show_index_list(struct patch *list)
1183 struct patch *patch;
1185 /* Once we start supporting the reverse patch, it may be
1186 * worth showing the new sha1 prefix, but until then...
1188 for (patch = list; patch; patch = patch->next) {
1189 const unsigned char *sha1_ptr;
1190 unsigned char sha1[20];
1191 const char *name;
1193 name = patch->old_name ? patch->old_name : patch->new_name;
1194 if (patch->is_new)
1195 sha1_ptr = null_sha1;
1196 else if (get_sha1(patch->old_sha1_prefix, sha1))
1197 die("sha1 information is lacking or useless (%s).",
1198 name);
1199 else
1200 sha1_ptr = sha1;
1201 printf("%06o %s %s\n",patch->old_mode,
1202 sha1_to_hex(sha1_ptr), name);
1206 static void stat_patch_list(struct patch *patch)
1208 int files, adds, dels;
1210 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
1211 files++;
1212 adds += patch->lines_added;
1213 dels += patch->lines_deleted;
1214 show_stats(patch);
1217 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
1220 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
1222 if (mode)
1223 printf(" %s mode %06o %s\n", newdelete, mode, name);
1224 else
1225 printf(" %s %s\n", newdelete, name);
1228 static void show_mode_change(struct patch *p, int show_name)
1230 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
1231 if (show_name)
1232 printf(" mode change %06o => %06o %s\n",
1233 p->old_mode, p->new_mode, p->new_name);
1234 else
1235 printf(" mode change %06o => %06o\n",
1236 p->old_mode, p->new_mode);
1240 static void show_rename_copy(struct patch *p)
1242 const char *renamecopy = p->is_rename ? "rename" : "copy";
1243 const char *old, *new;
1245 /* Find common prefix */
1246 old = p->old_name;
1247 new = p->new_name;
1248 while (1) {
1249 const char *slash_old, *slash_new;
1250 slash_old = strchr(old, '/');
1251 slash_new = strchr(new, '/');
1252 if (!slash_old ||
1253 !slash_new ||
1254 slash_old - old != slash_new - new ||
1255 memcmp(old, new, slash_new - new))
1256 break;
1257 old = slash_old + 1;
1258 new = slash_new + 1;
1260 /* p->old_name thru old is the common prefix, and old and new
1261 * through the end of names are renames
1263 if (old != p->old_name)
1264 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1265 (int)(old - p->old_name), p->old_name,
1266 old, new, p->score);
1267 else
1268 printf(" %s %s => %s (%d%%)\n", renamecopy,
1269 p->old_name, p->new_name, p->score);
1270 show_mode_change(p, 0);
1273 static void summary_patch_list(struct patch *patch)
1275 struct patch *p;
1277 for (p = patch; p; p = p->next) {
1278 if (p->is_new)
1279 show_file_mode_name("create", p->new_mode, p->new_name);
1280 else if (p->is_delete)
1281 show_file_mode_name("delete", p->old_mode, p->old_name);
1282 else {
1283 if (p->is_rename || p->is_copy)
1284 show_rename_copy(p);
1285 else {
1286 if (p->score) {
1287 printf(" rewrite %s (%d%%)\n",
1288 p->new_name, p->score);
1289 show_mode_change(p, 0);
1291 else
1292 show_mode_change(p, 1);
1298 static void patch_stats(struct patch *patch)
1300 int lines = patch->lines_added + patch->lines_deleted;
1302 if (lines > max_change)
1303 max_change = lines;
1304 if (patch->old_name) {
1305 int len = strlen(patch->old_name);
1306 if (len > max_len)
1307 max_len = len;
1309 if (patch->new_name) {
1310 int len = strlen(patch->new_name);
1311 if (len > max_len)
1312 max_len = len;
1316 static void remove_file(struct patch *patch)
1318 if (write_index) {
1319 if (remove_file_from_cache(patch->old_name) < 0)
1320 die("unable to remove %s from index", patch->old_name);
1322 unlink(patch->old_name);
1325 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
1327 struct stat st;
1328 struct cache_entry *ce;
1329 int namelen = strlen(path);
1330 unsigned ce_size = cache_entry_size(namelen);
1332 if (!write_index)
1333 return;
1335 ce = xmalloc(ce_size);
1336 memset(ce, 0, ce_size);
1337 memcpy(ce->name, path, namelen);
1338 ce->ce_mode = create_ce_mode(mode);
1339 ce->ce_flags = htons(namelen);
1340 if (lstat(path, &st) < 0)
1341 die("unable to stat newly created file %s", path);
1342 fill_stat_cache_info(ce, &st);
1343 if (write_sha1_file(buf, size, "blob", ce->sha1) < 0)
1344 die("unable to create backing store for newly created file %s", path);
1345 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
1346 die("unable to add cache entry for %s", path);
1349 static void create_subdirectories(const char *path)
1351 int len = strlen(path);
1352 char *buf = xmalloc(len + 1);
1353 const char *slash = path;
1355 while ((slash = strchr(slash+1, '/')) != NULL) {
1356 len = slash - path;
1357 memcpy(buf, path, len);
1358 buf[len] = 0;
1359 if (mkdir(buf, 0777) < 0) {
1360 if (errno != EEXIST)
1361 break;
1364 free(buf);
1367 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
1369 int fd;
1371 if (S_ISLNK(mode))
1372 return symlink(buf, path);
1373 fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, (mode & 0100) ? 0777 : 0666);
1374 if (fd < 0)
1375 return -1;
1376 while (size) {
1377 int written = write(fd, buf, size);
1378 if (written < 0) {
1379 if (errno == EINTR || errno == EAGAIN)
1380 continue;
1381 die("writing file %s: %s", path, strerror(errno));
1383 if (!written)
1384 die("out of space writing file %s", path);
1385 buf += written;
1386 size -= written;
1388 if (close(fd) < 0)
1389 die("closing file %s: %s", path, strerror(errno));
1390 return 0;
1394 * We optimistically assume that the directories exist,
1395 * which is true 99% of the time anyway. If they don't,
1396 * we create them and try again.
1398 static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size)
1400 if (!try_create_file(path, mode, buf, size))
1401 return;
1403 if (errno == ENOENT) {
1404 create_subdirectories(path);
1405 if (!try_create_file(path, mode, buf, size))
1406 return;
1409 if (errno == EEXIST) {
1410 unsigned int nr = getpid();
1412 for (;;) {
1413 const char *newpath;
1414 newpath = mkpath("%s~%u", path, nr);
1415 if (!try_create_file(newpath, mode, buf, size)) {
1416 if (!rename(newpath, path))
1417 return;
1418 unlink(newpath);
1419 break;
1421 if (errno != EEXIST)
1422 break;
1425 die("unable to write file %s mode %o", path, mode);
1428 static void create_file(struct patch *patch)
1430 const char *path = patch->new_name;
1431 unsigned mode = patch->new_mode;
1432 unsigned long size = patch->resultsize;
1433 char *buf = patch->result;
1435 if (!mode)
1436 mode = S_IFREG | 0644;
1437 create_one_file(path, mode, buf, size);
1438 add_index_file(path, mode, buf, size);
1441 static void write_out_one_result(struct patch *patch)
1443 if (patch->is_delete > 0) {
1444 remove_file(patch);
1445 return;
1447 if (patch->is_new > 0 || patch->is_copy) {
1448 create_file(patch);
1449 return;
1452 * Rename or modification boils down to the same
1453 * thing: remove the old, write the new
1455 remove_file(patch);
1456 create_file(patch);
1459 static void write_out_results(struct patch *list, int skipped_patch)
1461 if (!list && !skipped_patch)
1462 die("No changes");
1464 while (list) {
1465 write_out_one_result(list);
1466 list = list->next;
1470 static struct cache_file cache_file;
1472 static struct excludes {
1473 struct excludes *next;
1474 const char *path;
1475 } *excludes;
1477 static int use_patch(struct patch *p)
1479 const char *pathname = p->new_name ? p->new_name : p->old_name;
1480 struct excludes *x = excludes;
1481 while (x) {
1482 if (fnmatch(x->path, pathname, 0) == 0)
1483 return 0;
1484 x = x->next;
1486 return 1;
1489 static int apply_patch(int fd)
1491 int newfd;
1492 unsigned long offset, size;
1493 char *buffer = read_patch_file(fd, &size);
1494 struct patch *list = NULL, **listp = &list;
1495 int skipped_patch = 0;
1497 if (!buffer)
1498 return -1;
1499 offset = 0;
1500 while (size > 0) {
1501 struct patch *patch;
1502 int nr;
1504 patch = xmalloc(sizeof(*patch));
1505 memset(patch, 0, sizeof(*patch));
1506 nr = parse_chunk(buffer + offset, size, patch);
1507 if (nr < 0)
1508 break;
1509 if (use_patch(patch)) {
1510 patch_stats(patch);
1511 *listp = patch;
1512 listp = &patch->next;
1513 } else {
1514 /* perhaps free it a bit better? */
1515 free(patch);
1516 skipped_patch++;
1518 offset += nr;
1519 size -= nr;
1522 newfd = -1;
1523 write_index = check_index && apply;
1524 if (write_index)
1525 newfd = hold_index_file_for_update(&cache_file, get_index_file());
1526 if (check_index) {
1527 if (read_cache() < 0)
1528 die("unable to read index file");
1531 if ((check || apply) && check_patch_list(list) < 0)
1532 exit(1);
1534 if (apply)
1535 write_out_results(list, skipped_patch);
1537 if (write_index) {
1538 if (write_cache(newfd, active_cache, active_nr) ||
1539 commit_index_file(&cache_file))
1540 die("Unable to write new cachefile");
1543 if (show_files)
1544 show_file_list(list);
1546 if (show_index_info)
1547 show_index_list(list);
1549 if (diffstat)
1550 stat_patch_list(list);
1552 if (summary)
1553 summary_patch_list(list);
1555 free(buffer);
1556 return 0;
1559 int main(int argc, char **argv)
1561 int i;
1562 int read_stdin = 1;
1564 for (i = 1; i < argc; i++) {
1565 const char *arg = argv[i];
1566 int fd;
1568 if (!strcmp(arg, "-")) {
1569 apply_patch(0);
1570 read_stdin = 0;
1571 continue;
1573 if (!strncmp(arg, "--exclude=", 10)) {
1574 struct excludes *x = xmalloc(sizeof(*x));
1575 x->path = arg + 10;
1576 x->next = excludes;
1577 excludes = x;
1578 continue;
1580 if (!strcmp(arg, "--stat")) {
1581 apply = 0;
1582 diffstat = 1;
1583 continue;
1585 if (!strcmp(arg, "--summary")) {
1586 apply = 0;
1587 summary = 1;
1588 continue;
1590 if (!strcmp(arg, "--check")) {
1591 apply = 0;
1592 check = 1;
1593 continue;
1595 if (!strcmp(arg, "--index")) {
1596 check_index = 1;
1597 continue;
1599 if (!strcmp(arg, "--apply")) {
1600 apply = 1;
1601 continue;
1603 if (!strcmp(arg, "--show-files")) {
1604 show_files = 1;
1605 continue;
1607 if (!strcmp(arg, "--show-index-info")) {
1608 apply = 0;
1609 show_index_info = 1;
1610 continue;
1612 fd = open(arg, O_RDONLY);
1613 if (fd < 0)
1614 usage(apply_usage);
1615 read_stdin = 0;
1616 apply_patch(fd);
1617 close(fd);
1619 if (read_stdin)
1620 apply_patch(0);
1621 return 0;