builtin/apply: make apply_all_patches() return 128 or 1 on error
[git/git-svn.git] / builtin / apply.c
blob5530ba13ecc72247fadea0f34fd2b7756f72d497
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 "cache.h"
10 #include "lockfile.h"
11 #include "cache-tree.h"
12 #include "quote.h"
13 #include "blob.h"
14 #include "delta.h"
15 #include "builtin.h"
16 #include "string-list.h"
17 #include "dir.h"
18 #include "diff.h"
19 #include "parse-options.h"
20 #include "xdiff-interface.h"
21 #include "ll-merge.h"
22 #include "rerere.h"
23 #include "apply.h"
25 static const char * const apply_usage[] = {
26 N_("git apply [<options>] [<patch>...]"),
27 NULL
30 static void set_default_whitespace_mode(struct apply_state *state)
32 if (!state->whitespace_option && !apply_default_whitespace)
33 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
37 * This represents one "hunk" from a patch, starting with
38 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
39 * patch text is pointed at by patch, and its byte length
40 * is stored in size. leading and trailing are the number
41 * of context lines.
43 struct fragment {
44 unsigned long leading, trailing;
45 unsigned long oldpos, oldlines;
46 unsigned long newpos, newlines;
48 * 'patch' is usually borrowed from buf in apply_patch(),
49 * but some codepaths store an allocated buffer.
51 const char *patch;
52 unsigned free_patch:1,
53 rejected:1;
54 int size;
55 int linenr;
56 struct fragment *next;
60 * When dealing with a binary patch, we reuse "leading" field
61 * to store the type of the binary hunk, either deflated "delta"
62 * or deflated "literal".
64 #define binary_patch_method leading
65 #define BINARY_DELTA_DEFLATED 1
66 #define BINARY_LITERAL_DEFLATED 2
69 * This represents a "patch" to a file, both metainfo changes
70 * such as creation/deletion, filemode and content changes represented
71 * as a series of fragments.
73 struct patch {
74 char *new_name, *old_name, *def_name;
75 unsigned int old_mode, new_mode;
76 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
77 int rejected;
78 unsigned ws_rule;
79 int lines_added, lines_deleted;
80 int score;
81 unsigned int is_toplevel_relative:1;
82 unsigned int inaccurate_eof:1;
83 unsigned int is_binary:1;
84 unsigned int is_copy:1;
85 unsigned int is_rename:1;
86 unsigned int recount:1;
87 unsigned int conflicted_threeway:1;
88 unsigned int direct_to_threeway:1;
89 struct fragment *fragments;
90 char *result;
91 size_t resultsize;
92 char old_sha1_prefix[41];
93 char new_sha1_prefix[41];
94 struct patch *next;
96 /* three-way fallback result */
97 struct object_id threeway_stage[3];
100 static void free_fragment_list(struct fragment *list)
102 while (list) {
103 struct fragment *next = list->next;
104 if (list->free_patch)
105 free((char *)list->patch);
106 free(list);
107 list = next;
111 static void free_patch(struct patch *patch)
113 free_fragment_list(patch->fragments);
114 free(patch->def_name);
115 free(patch->old_name);
116 free(patch->new_name);
117 free(patch->result);
118 free(patch);
121 static void free_patch_list(struct patch *list)
123 while (list) {
124 struct patch *next = list->next;
125 free_patch(list);
126 list = next;
131 * A line in a file, len-bytes long (includes the terminating LF,
132 * except for an incomplete line at the end if the file ends with
133 * one), and its contents hashes to 'hash'.
135 struct line {
136 size_t len;
137 unsigned hash : 24;
138 unsigned flag : 8;
139 #define LINE_COMMON 1
140 #define LINE_PATCHED 2
144 * This represents a "file", which is an array of "lines".
146 struct image {
147 char *buf;
148 size_t len;
149 size_t nr;
150 size_t alloc;
151 struct line *line_allocated;
152 struct line *line;
155 static uint32_t hash_line(const char *cp, size_t len)
157 size_t i;
158 uint32_t h;
159 for (i = 0, h = 0; i < len; i++) {
160 if (!isspace(cp[i])) {
161 h = h * 3 + (cp[i] & 0xff);
164 return h;
168 * Compare lines s1 of length n1 and s2 of length n2, ignoring
169 * whitespace difference. Returns 1 if they match, 0 otherwise
171 static int fuzzy_matchlines(const char *s1, size_t n1,
172 const char *s2, size_t n2)
174 const char *last1 = s1 + n1 - 1;
175 const char *last2 = s2 + n2 - 1;
176 int result = 0;
178 /* ignore line endings */
179 while ((*last1 == '\r') || (*last1 == '\n'))
180 last1--;
181 while ((*last2 == '\r') || (*last2 == '\n'))
182 last2--;
184 /* skip leading whitespaces, if both begin with whitespace */
185 if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
186 while (isspace(*s1) && (s1 <= last1))
187 s1++;
188 while (isspace(*s2) && (s2 <= last2))
189 s2++;
191 /* early return if both lines are empty */
192 if ((s1 > last1) && (s2 > last2))
193 return 1;
194 while (!result) {
195 result = *s1++ - *s2++;
197 * Skip whitespace inside. We check for whitespace on
198 * both buffers because we don't want "a b" to match
199 * "ab"
201 if (isspace(*s1) && isspace(*s2)) {
202 while (isspace(*s1) && s1 <= last1)
203 s1++;
204 while (isspace(*s2) && s2 <= last2)
205 s2++;
208 * If we reached the end on one side only,
209 * lines don't match
211 if (
212 ((s2 > last2) && (s1 <= last1)) ||
213 ((s1 > last1) && (s2 <= last2)))
214 return 0;
215 if ((s1 > last1) && (s2 > last2))
216 break;
219 return !result;
222 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
224 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
225 img->line_allocated[img->nr].len = len;
226 img->line_allocated[img->nr].hash = hash_line(bol, len);
227 img->line_allocated[img->nr].flag = flag;
228 img->nr++;
232 * "buf" has the file contents to be patched (read from various sources).
233 * attach it to "image" and add line-based index to it.
234 * "image" now owns the "buf".
236 static void prepare_image(struct image *image, char *buf, size_t len,
237 int prepare_linetable)
239 const char *cp, *ep;
241 memset(image, 0, sizeof(*image));
242 image->buf = buf;
243 image->len = len;
245 if (!prepare_linetable)
246 return;
248 ep = image->buf + image->len;
249 cp = image->buf;
250 while (cp < ep) {
251 const char *next;
252 for (next = cp; next < ep && *next != '\n'; next++)
254 if (next < ep)
255 next++;
256 add_line_info(image, cp, next - cp, 0);
257 cp = next;
259 image->line = image->line_allocated;
262 static void clear_image(struct image *image)
264 free(image->buf);
265 free(image->line_allocated);
266 memset(image, 0, sizeof(*image));
269 /* fmt must contain _one_ %s and no other substitution */
270 static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
272 struct strbuf sb = STRBUF_INIT;
274 if (patch->old_name && patch->new_name &&
275 strcmp(patch->old_name, patch->new_name)) {
276 quote_c_style(patch->old_name, &sb, NULL, 0);
277 strbuf_addstr(&sb, " => ");
278 quote_c_style(patch->new_name, &sb, NULL, 0);
279 } else {
280 const char *n = patch->new_name;
281 if (!n)
282 n = patch->old_name;
283 quote_c_style(n, &sb, NULL, 0);
285 fprintf(output, fmt, sb.buf);
286 fputc('\n', output);
287 strbuf_release(&sb);
290 #define SLOP (16)
292 static int read_patch_file(struct strbuf *sb, int fd)
294 if (strbuf_read(sb, fd, 0) < 0)
295 return error_errno("git apply: failed to read");
298 * Make sure that we have some slop in the buffer
299 * so that we can do speculative "memcmp" etc, and
300 * see to it that it is NUL-filled.
302 strbuf_grow(sb, SLOP);
303 memset(sb->buf + sb->len, 0, SLOP);
304 return 0;
307 static unsigned long linelen(const char *buffer, unsigned long size)
309 unsigned long len = 0;
310 while (size--) {
311 len++;
312 if (*buffer++ == '\n')
313 break;
315 return len;
318 static int is_dev_null(const char *str)
320 return skip_prefix(str, "/dev/null", &str) && isspace(*str);
323 #define TERM_SPACE 1
324 #define TERM_TAB 2
326 static int name_terminate(int c, int terminate)
328 if (c == ' ' && !(terminate & TERM_SPACE))
329 return 0;
330 if (c == '\t' && !(terminate & TERM_TAB))
331 return 0;
333 return 1;
336 /* remove double slashes to make --index work with such filenames */
337 static char *squash_slash(char *name)
339 int i = 0, j = 0;
341 if (!name)
342 return NULL;
344 while (name[i]) {
345 if ((name[j++] = name[i++]) == '/')
346 while (name[i] == '/')
347 i++;
349 name[j] = '\0';
350 return name;
353 static char *find_name_gnu(struct apply_state *state,
354 const char *line,
355 const char *def,
356 int p_value)
358 struct strbuf name = STRBUF_INIT;
359 char *cp;
362 * Proposed "new-style" GNU patch/diff format; see
363 * http://marc.info/?l=git&m=112927316408690&w=2
365 if (unquote_c_style(&name, line, NULL)) {
366 strbuf_release(&name);
367 return NULL;
370 for (cp = name.buf; p_value; p_value--) {
371 cp = strchr(cp, '/');
372 if (!cp) {
373 strbuf_release(&name);
374 return NULL;
376 cp++;
379 strbuf_remove(&name, 0, cp - name.buf);
380 if (state->root.len)
381 strbuf_insert(&name, 0, state->root.buf, state->root.len);
382 return squash_slash(strbuf_detach(&name, NULL));
385 static size_t sane_tz_len(const char *line, size_t len)
387 const char *tz, *p;
389 if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
390 return 0;
391 tz = line + len - strlen(" +0500");
393 if (tz[1] != '+' && tz[1] != '-')
394 return 0;
396 for (p = tz + 2; p != line + len; p++)
397 if (!isdigit(*p))
398 return 0;
400 return line + len - tz;
403 static size_t tz_with_colon_len(const char *line, size_t len)
405 const char *tz, *p;
407 if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
408 return 0;
409 tz = line + len - strlen(" +08:00");
411 if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
412 return 0;
413 p = tz + 2;
414 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
415 !isdigit(*p++) || !isdigit(*p++))
416 return 0;
418 return line + len - tz;
421 static size_t date_len(const char *line, size_t len)
423 const char *date, *p;
425 if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
426 return 0;
427 p = date = line + len - strlen("72-02-05");
429 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
430 !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
431 !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */
432 return 0;
434 if (date - line >= strlen("19") &&
435 isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */
436 date -= strlen("19");
438 return line + len - date;
441 static size_t short_time_len(const char *line, size_t len)
443 const char *time, *p;
445 if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
446 return 0;
447 p = time = line + len - strlen(" 07:01:32");
449 /* Permit 1-digit hours? */
450 if (*p++ != ' ' ||
451 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
452 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
453 !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
454 return 0;
456 return line + len - time;
459 static size_t fractional_time_len(const char *line, size_t len)
461 const char *p;
462 size_t n;
464 /* Expected format: 19:41:17.620000023 */
465 if (!len || !isdigit(line[len - 1]))
466 return 0;
467 p = line + len - 1;
469 /* Fractional seconds. */
470 while (p > line && isdigit(*p))
471 p--;
472 if (*p != '.')
473 return 0;
475 /* Hours, minutes, and whole seconds. */
476 n = short_time_len(line, p - line);
477 if (!n)
478 return 0;
480 return line + len - p + n;
483 static size_t trailing_spaces_len(const char *line, size_t len)
485 const char *p;
487 /* Expected format: ' ' x (1 or more) */
488 if (!len || line[len - 1] != ' ')
489 return 0;
491 p = line + len;
492 while (p != line) {
493 p--;
494 if (*p != ' ')
495 return line + len - (p + 1);
498 /* All spaces! */
499 return len;
502 static size_t diff_timestamp_len(const char *line, size_t len)
504 const char *end = line + len;
505 size_t n;
508 * Posix: 2010-07-05 19:41:17
509 * GNU: 2010-07-05 19:41:17.620000023 -0500
512 if (!isdigit(end[-1]))
513 return 0;
515 n = sane_tz_len(line, end - line);
516 if (!n)
517 n = tz_with_colon_len(line, end - line);
518 end -= n;
520 n = short_time_len(line, end - line);
521 if (!n)
522 n = fractional_time_len(line, end - line);
523 end -= n;
525 n = date_len(line, end - line);
526 if (!n) /* No date. Too bad. */
527 return 0;
528 end -= n;
530 if (end == line) /* No space before date. */
531 return 0;
532 if (end[-1] == '\t') { /* Success! */
533 end--;
534 return line + len - end;
536 if (end[-1] != ' ') /* No space before date. */
537 return 0;
539 /* Whitespace damage. */
540 end -= trailing_spaces_len(line, end - line);
541 return line + len - end;
544 static char *find_name_common(struct apply_state *state,
545 const char *line,
546 const char *def,
547 int p_value,
548 const char *end,
549 int terminate)
551 int len;
552 const char *start = NULL;
554 if (p_value == 0)
555 start = line;
556 while (line != end) {
557 char c = *line;
559 if (!end && isspace(c)) {
560 if (c == '\n')
561 break;
562 if (name_terminate(c, terminate))
563 break;
565 line++;
566 if (c == '/' && !--p_value)
567 start = line;
569 if (!start)
570 return squash_slash(xstrdup_or_null(def));
571 len = line - start;
572 if (!len)
573 return squash_slash(xstrdup_or_null(def));
576 * Generally we prefer the shorter name, especially
577 * if the other one is just a variation of that with
578 * something else tacked on to the end (ie "file.orig"
579 * or "file~").
581 if (def) {
582 int deflen = strlen(def);
583 if (deflen < len && !strncmp(start, def, deflen))
584 return squash_slash(xstrdup(def));
587 if (state->root.len) {
588 char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
589 return squash_slash(ret);
592 return squash_slash(xmemdupz(start, len));
595 static char *find_name(struct apply_state *state,
596 const char *line,
597 char *def,
598 int p_value,
599 int terminate)
601 if (*line == '"') {
602 char *name = find_name_gnu(state, line, def, p_value);
603 if (name)
604 return name;
607 return find_name_common(state, line, def, p_value, NULL, terminate);
610 static char *find_name_traditional(struct apply_state *state,
611 const char *line,
612 char *def,
613 int p_value)
615 size_t len;
616 size_t date_len;
618 if (*line == '"') {
619 char *name = find_name_gnu(state, line, def, p_value);
620 if (name)
621 return name;
624 len = strchrnul(line, '\n') - line;
625 date_len = diff_timestamp_len(line, len);
626 if (!date_len)
627 return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
628 len -= date_len;
630 return find_name_common(state, line, def, p_value, line + len, 0);
633 static int count_slashes(const char *cp)
635 int cnt = 0;
636 char ch;
638 while ((ch = *cp++))
639 if (ch == '/')
640 cnt++;
641 return cnt;
645 * Given the string after "--- " or "+++ ", guess the appropriate
646 * p_value for the given patch.
648 static int guess_p_value(struct apply_state *state, const char *nameline)
650 char *name, *cp;
651 int val = -1;
653 if (is_dev_null(nameline))
654 return -1;
655 name = find_name_traditional(state, nameline, NULL, 0);
656 if (!name)
657 return -1;
658 cp = strchr(name, '/');
659 if (!cp)
660 val = 0;
661 else if (state->prefix) {
663 * Does it begin with "a/$our-prefix" and such? Then this is
664 * very likely to apply to our directory.
666 if (!strncmp(name, state->prefix, state->prefix_length))
667 val = count_slashes(state->prefix);
668 else {
669 cp++;
670 if (!strncmp(cp, state->prefix, state->prefix_length))
671 val = count_slashes(state->prefix) + 1;
674 free(name);
675 return val;
679 * Does the ---/+++ line have the POSIX timestamp after the last HT?
680 * GNU diff puts epoch there to signal a creation/deletion event. Is
681 * this such a timestamp?
683 static int has_epoch_timestamp(const char *nameline)
686 * We are only interested in epoch timestamp; any non-zero
687 * fraction cannot be one, hence "(\.0+)?" in the regexp below.
688 * For the same reason, the date must be either 1969-12-31 or
689 * 1970-01-01, and the seconds part must be "00".
691 const char stamp_regexp[] =
692 "^(1969-12-31|1970-01-01)"
694 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
696 "([-+][0-2][0-9]:?[0-5][0-9])\n";
697 const char *timestamp = NULL, *cp, *colon;
698 static regex_t *stamp;
699 regmatch_t m[10];
700 int zoneoffset;
701 int hourminute;
702 int status;
704 for (cp = nameline; *cp != '\n'; cp++) {
705 if (*cp == '\t')
706 timestamp = cp + 1;
708 if (!timestamp)
709 return 0;
710 if (!stamp) {
711 stamp = xmalloc(sizeof(*stamp));
712 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
713 warning(_("Cannot prepare timestamp regexp %s"),
714 stamp_regexp);
715 return 0;
719 status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
720 if (status) {
721 if (status != REG_NOMATCH)
722 warning(_("regexec returned %d for input: %s"),
723 status, timestamp);
724 return 0;
727 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
728 if (*colon == ':')
729 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
730 else
731 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
732 if (timestamp[m[3].rm_so] == '-')
733 zoneoffset = -zoneoffset;
736 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
737 * (west of GMT) or 1970-01-01 (east of GMT)
739 if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
740 (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
741 return 0;
743 hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
744 strtol(timestamp + 14, NULL, 10) -
745 zoneoffset);
747 return ((zoneoffset < 0 && hourminute == 1440) ||
748 (0 <= zoneoffset && !hourminute));
752 * Get the name etc info from the ---/+++ lines of a traditional patch header
754 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
755 * files, we can happily check the index for a match, but for creating a
756 * new file we should try to match whatever "patch" does. I have no idea.
758 static void parse_traditional_patch(struct apply_state *state,
759 const char *first,
760 const char *second,
761 struct patch *patch)
763 char *name;
765 first += 4; /* skip "--- " */
766 second += 4; /* skip "+++ " */
767 if (!state->p_value_known) {
768 int p, q;
769 p = guess_p_value(state, first);
770 q = guess_p_value(state, second);
771 if (p < 0) p = q;
772 if (0 <= p && p == q) {
773 state->p_value = p;
774 state->p_value_known = 1;
777 if (is_dev_null(first)) {
778 patch->is_new = 1;
779 patch->is_delete = 0;
780 name = find_name_traditional(state, second, NULL, state->p_value);
781 patch->new_name = name;
782 } else if (is_dev_null(second)) {
783 patch->is_new = 0;
784 patch->is_delete = 1;
785 name = find_name_traditional(state, first, NULL, state->p_value);
786 patch->old_name = name;
787 } else {
788 char *first_name;
789 first_name = find_name_traditional(state, first, NULL, state->p_value);
790 name = find_name_traditional(state, second, first_name, state->p_value);
791 free(first_name);
792 if (has_epoch_timestamp(first)) {
793 patch->is_new = 1;
794 patch->is_delete = 0;
795 patch->new_name = name;
796 } else if (has_epoch_timestamp(second)) {
797 patch->is_new = 0;
798 patch->is_delete = 1;
799 patch->old_name = name;
800 } else {
801 patch->old_name = name;
802 patch->new_name = xstrdup_or_null(name);
805 if (!name)
806 die(_("unable to find filename in patch at line %d"), state->linenr);
809 static int gitdiff_hdrend(struct apply_state *state,
810 const char *line,
811 struct patch *patch)
813 return -1;
817 * We're anal about diff header consistency, to make
818 * sure that we don't end up having strange ambiguous
819 * patches floating around.
821 * As a result, gitdiff_{old|new}name() will check
822 * their names against any previous information, just
823 * to make sure..
825 #define DIFF_OLD_NAME 0
826 #define DIFF_NEW_NAME 1
828 static void gitdiff_verify_name(struct apply_state *state,
829 const char *line,
830 int isnull,
831 char **name,
832 int side)
834 if (!*name && !isnull) {
835 *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
836 return;
839 if (*name) {
840 int len = strlen(*name);
841 char *another;
842 if (isnull)
843 die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
844 *name, state->linenr);
845 another = find_name(state, line, NULL, state->p_value, TERM_TAB);
846 if (!another || memcmp(another, *name, len + 1))
847 die((side == DIFF_NEW_NAME) ?
848 _("git apply: bad git-diff - inconsistent new filename on line %d") :
849 _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
850 free(another);
851 } else {
852 /* expect "/dev/null" */
853 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
854 die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
858 static int gitdiff_oldname(struct apply_state *state,
859 const char *line,
860 struct patch *patch)
862 gitdiff_verify_name(state, line,
863 patch->is_new, &patch->old_name,
864 DIFF_OLD_NAME);
865 return 0;
868 static int gitdiff_newname(struct apply_state *state,
869 const char *line,
870 struct patch *patch)
872 gitdiff_verify_name(state, line,
873 patch->is_delete, &patch->new_name,
874 DIFF_NEW_NAME);
875 return 0;
878 static int gitdiff_oldmode(struct apply_state *state,
879 const char *line,
880 struct patch *patch)
882 patch->old_mode = strtoul(line, NULL, 8);
883 return 0;
886 static int gitdiff_newmode(struct apply_state *state,
887 const char *line,
888 struct patch *patch)
890 patch->new_mode = strtoul(line, NULL, 8);
891 return 0;
894 static int gitdiff_delete(struct apply_state *state,
895 const char *line,
896 struct patch *patch)
898 patch->is_delete = 1;
899 free(patch->old_name);
900 patch->old_name = xstrdup_or_null(patch->def_name);
901 return gitdiff_oldmode(state, line, patch);
904 static int gitdiff_newfile(struct apply_state *state,
905 const char *line,
906 struct patch *patch)
908 patch->is_new = 1;
909 free(patch->new_name);
910 patch->new_name = xstrdup_or_null(patch->def_name);
911 return gitdiff_newmode(state, line, patch);
914 static int gitdiff_copysrc(struct apply_state *state,
915 const char *line,
916 struct patch *patch)
918 patch->is_copy = 1;
919 free(patch->old_name);
920 patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
921 return 0;
924 static int gitdiff_copydst(struct apply_state *state,
925 const char *line,
926 struct patch *patch)
928 patch->is_copy = 1;
929 free(patch->new_name);
930 patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
931 return 0;
934 static int gitdiff_renamesrc(struct apply_state *state,
935 const char *line,
936 struct patch *patch)
938 patch->is_rename = 1;
939 free(patch->old_name);
940 patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
941 return 0;
944 static int gitdiff_renamedst(struct apply_state *state,
945 const char *line,
946 struct patch *patch)
948 patch->is_rename = 1;
949 free(patch->new_name);
950 patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
951 return 0;
954 static int gitdiff_similarity(struct apply_state *state,
955 const char *line,
956 struct patch *patch)
958 unsigned long val = strtoul(line, NULL, 10);
959 if (val <= 100)
960 patch->score = val;
961 return 0;
964 static int gitdiff_dissimilarity(struct apply_state *state,
965 const char *line,
966 struct patch *patch)
968 unsigned long val = strtoul(line, NULL, 10);
969 if (val <= 100)
970 patch->score = val;
971 return 0;
974 static int gitdiff_index(struct apply_state *state,
975 const char *line,
976 struct patch *patch)
979 * index line is N hexadecimal, "..", N hexadecimal,
980 * and optional space with octal mode.
982 const char *ptr, *eol;
983 int len;
985 ptr = strchr(line, '.');
986 if (!ptr || ptr[1] != '.' || 40 < ptr - line)
987 return 0;
988 len = ptr - line;
989 memcpy(patch->old_sha1_prefix, line, len);
990 patch->old_sha1_prefix[len] = 0;
992 line = ptr + 2;
993 ptr = strchr(line, ' ');
994 eol = strchrnul(line, '\n');
996 if (!ptr || eol < ptr)
997 ptr = eol;
998 len = ptr - line;
1000 if (40 < len)
1001 return 0;
1002 memcpy(patch->new_sha1_prefix, line, len);
1003 patch->new_sha1_prefix[len] = 0;
1004 if (*ptr == ' ')
1005 patch->old_mode = strtoul(ptr+1, NULL, 8);
1006 return 0;
1010 * This is normal for a diff that doesn't change anything: we'll fall through
1011 * into the next diff. Tell the parser to break out.
1013 static int gitdiff_unrecognized(struct apply_state *state,
1014 const char *line,
1015 struct patch *patch)
1017 return -1;
1021 * Skip p_value leading components from "line"; as we do not accept
1022 * absolute paths, return NULL in that case.
1024 static const char *skip_tree_prefix(struct apply_state *state,
1025 const char *line,
1026 int llen)
1028 int nslash;
1029 int i;
1031 if (!state->p_value)
1032 return (llen && line[0] == '/') ? NULL : line;
1034 nslash = state->p_value;
1035 for (i = 0; i < llen; i++) {
1036 int ch = line[i];
1037 if (ch == '/' && --nslash <= 0)
1038 return (i == 0) ? NULL : &line[i + 1];
1040 return NULL;
1044 * This is to extract the same name that appears on "diff --git"
1045 * line. We do not find and return anything if it is a rename
1046 * patch, and it is OK because we will find the name elsewhere.
1047 * We need to reliably find name only when it is mode-change only,
1048 * creation or deletion of an empty file. In any of these cases,
1049 * both sides are the same name under a/ and b/ respectively.
1051 static char *git_header_name(struct apply_state *state,
1052 const char *line,
1053 int llen)
1055 const char *name;
1056 const char *second = NULL;
1057 size_t len, line_len;
1059 line += strlen("diff --git ");
1060 llen -= strlen("diff --git ");
1062 if (*line == '"') {
1063 const char *cp;
1064 struct strbuf first = STRBUF_INIT;
1065 struct strbuf sp = STRBUF_INIT;
1067 if (unquote_c_style(&first, line, &second))
1068 goto free_and_fail1;
1070 /* strip the a/b prefix including trailing slash */
1071 cp = skip_tree_prefix(state, first.buf, first.len);
1072 if (!cp)
1073 goto free_and_fail1;
1074 strbuf_remove(&first, 0, cp - first.buf);
1077 * second points at one past closing dq of name.
1078 * find the second name.
1080 while ((second < line + llen) && isspace(*second))
1081 second++;
1083 if (line + llen <= second)
1084 goto free_and_fail1;
1085 if (*second == '"') {
1086 if (unquote_c_style(&sp, second, NULL))
1087 goto free_and_fail1;
1088 cp = skip_tree_prefix(state, sp.buf, sp.len);
1089 if (!cp)
1090 goto free_and_fail1;
1091 /* They must match, otherwise ignore */
1092 if (strcmp(cp, first.buf))
1093 goto free_and_fail1;
1094 strbuf_release(&sp);
1095 return strbuf_detach(&first, NULL);
1098 /* unquoted second */
1099 cp = skip_tree_prefix(state, second, line + llen - second);
1100 if (!cp)
1101 goto free_and_fail1;
1102 if (line + llen - cp != first.len ||
1103 memcmp(first.buf, cp, first.len))
1104 goto free_and_fail1;
1105 return strbuf_detach(&first, NULL);
1107 free_and_fail1:
1108 strbuf_release(&first);
1109 strbuf_release(&sp);
1110 return NULL;
1113 /* unquoted first name */
1114 name = skip_tree_prefix(state, line, llen);
1115 if (!name)
1116 return NULL;
1119 * since the first name is unquoted, a dq if exists must be
1120 * the beginning of the second name.
1122 for (second = name; second < line + llen; second++) {
1123 if (*second == '"') {
1124 struct strbuf sp = STRBUF_INIT;
1125 const char *np;
1127 if (unquote_c_style(&sp, second, NULL))
1128 goto free_and_fail2;
1130 np = skip_tree_prefix(state, sp.buf, sp.len);
1131 if (!np)
1132 goto free_and_fail2;
1134 len = sp.buf + sp.len - np;
1135 if (len < second - name &&
1136 !strncmp(np, name, len) &&
1137 isspace(name[len])) {
1138 /* Good */
1139 strbuf_remove(&sp, 0, np - sp.buf);
1140 return strbuf_detach(&sp, NULL);
1143 free_and_fail2:
1144 strbuf_release(&sp);
1145 return NULL;
1150 * Accept a name only if it shows up twice, exactly the same
1151 * form.
1153 second = strchr(name, '\n');
1154 if (!second)
1155 return NULL;
1156 line_len = second - name;
1157 for (len = 0 ; ; len++) {
1158 switch (name[len]) {
1159 default:
1160 continue;
1161 case '\n':
1162 return NULL;
1163 case '\t': case ' ':
1165 * Is this the separator between the preimage
1166 * and the postimage pathname? Again, we are
1167 * only interested in the case where there is
1168 * no rename, as this is only to set def_name
1169 * and a rename patch has the names elsewhere
1170 * in an unambiguous form.
1172 if (!name[len + 1])
1173 return NULL; /* no postimage name */
1174 second = skip_tree_prefix(state, name + len + 1,
1175 line_len - (len + 1));
1176 if (!second)
1177 return NULL;
1179 * Does len bytes starting at "name" and "second"
1180 * (that are separated by one HT or SP we just
1181 * found) exactly match?
1183 if (second[len] == '\n' && !strncmp(name, second, len))
1184 return xmemdupz(name, len);
1189 /* Verify that we recognize the lines following a git header */
1190 static int parse_git_header(struct apply_state *state,
1191 const char *line,
1192 int len,
1193 unsigned int size,
1194 struct patch *patch)
1196 unsigned long offset;
1198 /* A git diff has explicit new/delete information, so we don't guess */
1199 patch->is_new = 0;
1200 patch->is_delete = 0;
1203 * Some things may not have the old name in the
1204 * rest of the headers anywhere (pure mode changes,
1205 * or removing or adding empty files), so we get
1206 * the default name from the header.
1208 patch->def_name = git_header_name(state, line, len);
1209 if (patch->def_name && state->root.len) {
1210 char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1211 free(patch->def_name);
1212 patch->def_name = s;
1215 line += len;
1216 size -= len;
1217 state->linenr++;
1218 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1219 static const struct opentry {
1220 const char *str;
1221 int (*fn)(struct apply_state *, const char *, struct patch *);
1222 } optable[] = {
1223 { "@@ -", gitdiff_hdrend },
1224 { "--- ", gitdiff_oldname },
1225 { "+++ ", gitdiff_newname },
1226 { "old mode ", gitdiff_oldmode },
1227 { "new mode ", gitdiff_newmode },
1228 { "deleted file mode ", gitdiff_delete },
1229 { "new file mode ", gitdiff_newfile },
1230 { "copy from ", gitdiff_copysrc },
1231 { "copy to ", gitdiff_copydst },
1232 { "rename old ", gitdiff_renamesrc },
1233 { "rename new ", gitdiff_renamedst },
1234 { "rename from ", gitdiff_renamesrc },
1235 { "rename to ", gitdiff_renamedst },
1236 { "similarity index ", gitdiff_similarity },
1237 { "dissimilarity index ", gitdiff_dissimilarity },
1238 { "index ", gitdiff_index },
1239 { "", gitdiff_unrecognized },
1241 int i;
1243 len = linelen(line, size);
1244 if (!len || line[len-1] != '\n')
1245 break;
1246 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1247 const struct opentry *p = optable + i;
1248 int oplen = strlen(p->str);
1249 if (len < oplen || memcmp(p->str, line, oplen))
1250 continue;
1251 if (p->fn(state, line + oplen, patch) < 0)
1252 return offset;
1253 break;
1257 return offset;
1260 static int parse_num(const char *line, unsigned long *p)
1262 char *ptr;
1264 if (!isdigit(*line))
1265 return 0;
1266 *p = strtoul(line, &ptr, 10);
1267 return ptr - line;
1270 static int parse_range(const char *line, int len, int offset, const char *expect,
1271 unsigned long *p1, unsigned long *p2)
1273 int digits, ex;
1275 if (offset < 0 || offset >= len)
1276 return -1;
1277 line += offset;
1278 len -= offset;
1280 digits = parse_num(line, p1);
1281 if (!digits)
1282 return -1;
1284 offset += digits;
1285 line += digits;
1286 len -= digits;
1288 *p2 = 1;
1289 if (*line == ',') {
1290 digits = parse_num(line+1, p2);
1291 if (!digits)
1292 return -1;
1294 offset += digits+1;
1295 line += digits+1;
1296 len -= digits+1;
1299 ex = strlen(expect);
1300 if (ex > len)
1301 return -1;
1302 if (memcmp(line, expect, ex))
1303 return -1;
1305 return offset + ex;
1308 static void recount_diff(const char *line, int size, struct fragment *fragment)
1310 int oldlines = 0, newlines = 0, ret = 0;
1312 if (size < 1) {
1313 warning("recount: ignore empty hunk");
1314 return;
1317 for (;;) {
1318 int len = linelen(line, size);
1319 size -= len;
1320 line += len;
1322 if (size < 1)
1323 break;
1325 switch (*line) {
1326 case ' ': case '\n':
1327 newlines++;
1328 /* fall through */
1329 case '-':
1330 oldlines++;
1331 continue;
1332 case '+':
1333 newlines++;
1334 continue;
1335 case '\\':
1336 continue;
1337 case '@':
1338 ret = size < 3 || !starts_with(line, "@@ ");
1339 break;
1340 case 'd':
1341 ret = size < 5 || !starts_with(line, "diff ");
1342 break;
1343 default:
1344 ret = -1;
1345 break;
1347 if (ret) {
1348 warning(_("recount: unexpected line: %.*s"),
1349 (int)linelen(line, size), line);
1350 return;
1352 break;
1354 fragment->oldlines = oldlines;
1355 fragment->newlines = newlines;
1359 * Parse a unified diff fragment header of the
1360 * form "@@ -a,b +c,d @@"
1362 static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1364 int offset;
1366 if (!len || line[len-1] != '\n')
1367 return -1;
1369 /* Figure out the number of lines in a fragment */
1370 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1371 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1373 return offset;
1377 * Find file diff header
1379 * Returns:
1380 * -1 if no header was found
1381 * -128 in case of error
1382 * the size of the header in bytes (called "offset") otherwise
1384 static int find_header(struct apply_state *state,
1385 const char *line,
1386 unsigned long size,
1387 int *hdrsize,
1388 struct patch *patch)
1390 unsigned long offset, len;
1392 patch->is_toplevel_relative = 0;
1393 patch->is_rename = patch->is_copy = 0;
1394 patch->is_new = patch->is_delete = -1;
1395 patch->old_mode = patch->new_mode = 0;
1396 patch->old_name = patch->new_name = NULL;
1397 for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1398 unsigned long nextlen;
1400 len = linelen(line, size);
1401 if (!len)
1402 break;
1404 /* Testing this early allows us to take a few shortcuts.. */
1405 if (len < 6)
1406 continue;
1409 * Make sure we don't find any unconnected patch fragments.
1410 * That's a sign that we didn't find a header, and that a
1411 * patch has become corrupted/broken up.
1413 if (!memcmp("@@ -", line, 4)) {
1414 struct fragment dummy;
1415 if (parse_fragment_header(line, len, &dummy) < 0)
1416 continue;
1417 error(_("patch fragment without header at line %d: %.*s"),
1418 state->linenr, (int)len-1, line);
1419 return -128;
1422 if (size < len + 6)
1423 break;
1426 * Git patch? It might not have a real patch, just a rename
1427 * or mode change, so we handle that specially
1429 if (!memcmp("diff --git ", line, 11)) {
1430 int git_hdr_len = parse_git_header(state, line, len, size, patch);
1431 if (git_hdr_len <= len)
1432 continue;
1433 if (!patch->old_name && !patch->new_name) {
1434 if (!patch->def_name) {
1435 error(Q_("git diff header lacks filename information when removing "
1436 "%d leading pathname component (line %d)",
1437 "git diff header lacks filename information when removing "
1438 "%d leading pathname components (line %d)",
1439 state->p_value),
1440 state->p_value, state->linenr);
1441 return -128;
1443 patch->old_name = xstrdup(patch->def_name);
1444 patch->new_name = xstrdup(patch->def_name);
1446 if (!patch->is_delete && !patch->new_name) {
1447 error("git diff header lacks filename information "
1448 "(line %d)", state->linenr);
1449 return -128;
1451 patch->is_toplevel_relative = 1;
1452 *hdrsize = git_hdr_len;
1453 return offset;
1456 /* --- followed by +++ ? */
1457 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1458 continue;
1461 * We only accept unified patches, so we want it to
1462 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1463 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1465 nextlen = linelen(line + len, size - len);
1466 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1467 continue;
1469 /* Ok, we'll consider it a patch */
1470 parse_traditional_patch(state, line, line+len, patch);
1471 *hdrsize = len + nextlen;
1472 state->linenr += 2;
1473 return offset;
1475 return -1;
1478 static void record_ws_error(struct apply_state *state,
1479 unsigned result,
1480 const char *line,
1481 int len,
1482 int linenr)
1484 char *err;
1486 if (!result)
1487 return;
1489 state->whitespace_error++;
1490 if (state->squelch_whitespace_errors &&
1491 state->squelch_whitespace_errors < state->whitespace_error)
1492 return;
1494 err = whitespace_error_string(result);
1495 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1496 state->patch_input_file, linenr, err, len, line);
1497 free(err);
1500 static void check_whitespace(struct apply_state *state,
1501 const char *line,
1502 int len,
1503 unsigned ws_rule)
1505 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1507 record_ws_error(state, result, line + 1, len - 2, state->linenr);
1511 * Parse a unified diff. Note that this really needs to parse each
1512 * fragment separately, since the only way to know the difference
1513 * between a "---" that is part of a patch, and a "---" that starts
1514 * the next patch is to look at the line counts..
1516 static int parse_fragment(struct apply_state *state,
1517 const char *line,
1518 unsigned long size,
1519 struct patch *patch,
1520 struct fragment *fragment)
1522 int added, deleted;
1523 int len = linelen(line, size), offset;
1524 unsigned long oldlines, newlines;
1525 unsigned long leading, trailing;
1527 offset = parse_fragment_header(line, len, fragment);
1528 if (offset < 0)
1529 return -1;
1530 if (offset > 0 && patch->recount)
1531 recount_diff(line + offset, size - offset, fragment);
1532 oldlines = fragment->oldlines;
1533 newlines = fragment->newlines;
1534 leading = 0;
1535 trailing = 0;
1537 /* Parse the thing.. */
1538 line += len;
1539 size -= len;
1540 state->linenr++;
1541 added = deleted = 0;
1542 for (offset = len;
1543 0 < size;
1544 offset += len, size -= len, line += len, state->linenr++) {
1545 if (!oldlines && !newlines)
1546 break;
1547 len = linelen(line, size);
1548 if (!len || line[len-1] != '\n')
1549 return -1;
1550 switch (*line) {
1551 default:
1552 return -1;
1553 case '\n': /* newer GNU diff, an empty context line */
1554 case ' ':
1555 oldlines--;
1556 newlines--;
1557 if (!deleted && !added)
1558 leading++;
1559 trailing++;
1560 if (!state->apply_in_reverse &&
1561 state->ws_error_action == correct_ws_error)
1562 check_whitespace(state, line, len, patch->ws_rule);
1563 break;
1564 case '-':
1565 if (state->apply_in_reverse &&
1566 state->ws_error_action != nowarn_ws_error)
1567 check_whitespace(state, line, len, patch->ws_rule);
1568 deleted++;
1569 oldlines--;
1570 trailing = 0;
1571 break;
1572 case '+':
1573 if (!state->apply_in_reverse &&
1574 state->ws_error_action != nowarn_ws_error)
1575 check_whitespace(state, line, len, patch->ws_rule);
1576 added++;
1577 newlines--;
1578 trailing = 0;
1579 break;
1582 * We allow "\ No newline at end of file". Depending
1583 * on locale settings when the patch was produced we
1584 * don't know what this line looks like. The only
1585 * thing we do know is that it begins with "\ ".
1586 * Checking for 12 is just for sanity check -- any
1587 * l10n of "\ No newline..." is at least that long.
1589 case '\\':
1590 if (len < 12 || memcmp(line, "\\ ", 2))
1591 return -1;
1592 break;
1595 if (oldlines || newlines)
1596 return -1;
1597 if (!deleted && !added)
1598 return -1;
1600 fragment->leading = leading;
1601 fragment->trailing = trailing;
1604 * If a fragment ends with an incomplete line, we failed to include
1605 * it in the above loop because we hit oldlines == newlines == 0
1606 * before seeing it.
1608 if (12 < size && !memcmp(line, "\\ ", 2))
1609 offset += linelen(line, size);
1611 patch->lines_added += added;
1612 patch->lines_deleted += deleted;
1614 if (0 < patch->is_new && oldlines)
1615 return error(_("new file depends on old contents"));
1616 if (0 < patch->is_delete && newlines)
1617 return error(_("deleted file still has contents"));
1618 return offset;
1622 * We have seen "diff --git a/... b/..." header (or a traditional patch
1623 * header). Read hunks that belong to this patch into fragments and hang
1624 * them to the given patch structure.
1626 * The (fragment->patch, fragment->size) pair points into the memory given
1627 * by the caller, not a copy, when we return.
1629 * Returns:
1630 * -1 in case of error,
1631 * the number of bytes in the patch otherwise.
1633 static int parse_single_patch(struct apply_state *state,
1634 const char *line,
1635 unsigned long size,
1636 struct patch *patch)
1638 unsigned long offset = 0;
1639 unsigned long oldlines = 0, newlines = 0, context = 0;
1640 struct fragment **fragp = &patch->fragments;
1642 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1643 struct fragment *fragment;
1644 int len;
1646 fragment = xcalloc(1, sizeof(*fragment));
1647 fragment->linenr = state->linenr;
1648 len = parse_fragment(state, line, size, patch, fragment);
1649 if (len <= 0) {
1650 free(fragment);
1651 return error(_("corrupt patch at line %d"), state->linenr);
1653 fragment->patch = line;
1654 fragment->size = len;
1655 oldlines += fragment->oldlines;
1656 newlines += fragment->newlines;
1657 context += fragment->leading + fragment->trailing;
1659 *fragp = fragment;
1660 fragp = &fragment->next;
1662 offset += len;
1663 line += len;
1664 size -= len;
1668 * If something was removed (i.e. we have old-lines) it cannot
1669 * be creation, and if something was added it cannot be
1670 * deletion. However, the reverse is not true; --unified=0
1671 * patches that only add are not necessarily creation even
1672 * though they do not have any old lines, and ones that only
1673 * delete are not necessarily deletion.
1675 * Unfortunately, a real creation/deletion patch do _not_ have
1676 * any context line by definition, so we cannot safely tell it
1677 * apart with --unified=0 insanity. At least if the patch has
1678 * more than one hunk it is not creation or deletion.
1680 if (patch->is_new < 0 &&
1681 (oldlines || (patch->fragments && patch->fragments->next)))
1682 patch->is_new = 0;
1683 if (patch->is_delete < 0 &&
1684 (newlines || (patch->fragments && patch->fragments->next)))
1685 patch->is_delete = 0;
1687 if (0 < patch->is_new && oldlines)
1688 return error(_("new file %s depends on old contents"), patch->new_name);
1689 if (0 < patch->is_delete && newlines)
1690 return error(_("deleted file %s still has contents"), patch->old_name);
1691 if (!patch->is_delete && !newlines && context)
1692 fprintf_ln(stderr,
1693 _("** warning: "
1694 "file %s becomes empty but is not deleted"),
1695 patch->new_name);
1697 return offset;
1700 static inline int metadata_changes(struct patch *patch)
1702 return patch->is_rename > 0 ||
1703 patch->is_copy > 0 ||
1704 patch->is_new > 0 ||
1705 patch->is_delete ||
1706 (patch->old_mode && patch->new_mode &&
1707 patch->old_mode != patch->new_mode);
1710 static char *inflate_it(const void *data, unsigned long size,
1711 unsigned long inflated_size)
1713 git_zstream stream;
1714 void *out;
1715 int st;
1717 memset(&stream, 0, sizeof(stream));
1719 stream.next_in = (unsigned char *)data;
1720 stream.avail_in = size;
1721 stream.next_out = out = xmalloc(inflated_size);
1722 stream.avail_out = inflated_size;
1723 git_inflate_init(&stream);
1724 st = git_inflate(&stream, Z_FINISH);
1725 git_inflate_end(&stream);
1726 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1727 free(out);
1728 return NULL;
1730 return out;
1734 * Read a binary hunk and return a new fragment; fragment->patch
1735 * points at an allocated memory that the caller must free, so
1736 * it is marked as "->free_patch = 1".
1738 static struct fragment *parse_binary_hunk(struct apply_state *state,
1739 char **buf_p,
1740 unsigned long *sz_p,
1741 int *status_p,
1742 int *used_p)
1745 * Expect a line that begins with binary patch method ("literal"
1746 * or "delta"), followed by the length of data before deflating.
1747 * a sequence of 'length-byte' followed by base-85 encoded data
1748 * should follow, terminated by a newline.
1750 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1751 * and we would limit the patch line to 66 characters,
1752 * so one line can fit up to 13 groups that would decode
1753 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1754 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1756 int llen, used;
1757 unsigned long size = *sz_p;
1758 char *buffer = *buf_p;
1759 int patch_method;
1760 unsigned long origlen;
1761 char *data = NULL;
1762 int hunk_size = 0;
1763 struct fragment *frag;
1765 llen = linelen(buffer, size);
1766 used = llen;
1768 *status_p = 0;
1770 if (starts_with(buffer, "delta ")) {
1771 patch_method = BINARY_DELTA_DEFLATED;
1772 origlen = strtoul(buffer + 6, NULL, 10);
1774 else if (starts_with(buffer, "literal ")) {
1775 patch_method = BINARY_LITERAL_DEFLATED;
1776 origlen = strtoul(buffer + 8, NULL, 10);
1778 else
1779 return NULL;
1781 state->linenr++;
1782 buffer += llen;
1783 while (1) {
1784 int byte_length, max_byte_length, newsize;
1785 llen = linelen(buffer, size);
1786 used += llen;
1787 state->linenr++;
1788 if (llen == 1) {
1789 /* consume the blank line */
1790 buffer++;
1791 size--;
1792 break;
1795 * Minimum line is "A00000\n" which is 7-byte long,
1796 * and the line length must be multiple of 5 plus 2.
1798 if ((llen < 7) || (llen-2) % 5)
1799 goto corrupt;
1800 max_byte_length = (llen - 2) / 5 * 4;
1801 byte_length = *buffer;
1802 if ('A' <= byte_length && byte_length <= 'Z')
1803 byte_length = byte_length - 'A' + 1;
1804 else if ('a' <= byte_length && byte_length <= 'z')
1805 byte_length = byte_length - 'a' + 27;
1806 else
1807 goto corrupt;
1808 /* if the input length was not multiple of 4, we would
1809 * have filler at the end but the filler should never
1810 * exceed 3 bytes
1812 if (max_byte_length < byte_length ||
1813 byte_length <= max_byte_length - 4)
1814 goto corrupt;
1815 newsize = hunk_size + byte_length;
1816 data = xrealloc(data, newsize);
1817 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1818 goto corrupt;
1819 hunk_size = newsize;
1820 buffer += llen;
1821 size -= llen;
1824 frag = xcalloc(1, sizeof(*frag));
1825 frag->patch = inflate_it(data, hunk_size, origlen);
1826 frag->free_patch = 1;
1827 if (!frag->patch)
1828 goto corrupt;
1829 free(data);
1830 frag->size = origlen;
1831 *buf_p = buffer;
1832 *sz_p = size;
1833 *used_p = used;
1834 frag->binary_patch_method = patch_method;
1835 return frag;
1837 corrupt:
1838 free(data);
1839 *status_p = -1;
1840 error(_("corrupt binary patch at line %d: %.*s"),
1841 state->linenr-1, llen-1, buffer);
1842 return NULL;
1846 * Returns:
1847 * -1 in case of error,
1848 * the length of the parsed binary patch otherwise
1850 static int parse_binary(struct apply_state *state,
1851 char *buffer,
1852 unsigned long size,
1853 struct patch *patch)
1856 * We have read "GIT binary patch\n"; what follows is a line
1857 * that says the patch method (currently, either "literal" or
1858 * "delta") and the length of data before deflating; a
1859 * sequence of 'length-byte' followed by base-85 encoded data
1860 * follows.
1862 * When a binary patch is reversible, there is another binary
1863 * hunk in the same format, starting with patch method (either
1864 * "literal" or "delta") with the length of data, and a sequence
1865 * of length-byte + base-85 encoded data, terminated with another
1866 * empty line. This data, when applied to the postimage, produces
1867 * the preimage.
1869 struct fragment *forward;
1870 struct fragment *reverse;
1871 int status;
1872 int used, used_1;
1874 forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
1875 if (!forward && !status)
1876 /* there has to be one hunk (forward hunk) */
1877 return error(_("unrecognized binary patch at line %d"), state->linenr-1);
1878 if (status)
1879 /* otherwise we already gave an error message */
1880 return status;
1882 reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
1883 if (reverse)
1884 used += used_1;
1885 else if (status) {
1887 * Not having reverse hunk is not an error, but having
1888 * a corrupt reverse hunk is.
1890 free((void*) forward->patch);
1891 free(forward);
1892 return status;
1894 forward->next = reverse;
1895 patch->fragments = forward;
1896 patch->is_binary = 1;
1897 return used;
1900 static void prefix_one(struct apply_state *state, char **name)
1902 char *old_name = *name;
1903 if (!old_name)
1904 return;
1905 *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
1906 free(old_name);
1909 static void prefix_patch(struct apply_state *state, struct patch *p)
1911 if (!state->prefix || p->is_toplevel_relative)
1912 return;
1913 prefix_one(state, &p->new_name);
1914 prefix_one(state, &p->old_name);
1918 * include/exclude
1921 static void add_name_limit(struct apply_state *state,
1922 const char *name,
1923 int exclude)
1925 struct string_list_item *it;
1927 it = string_list_append(&state->limit_by_name, name);
1928 it->util = exclude ? NULL : (void *) 1;
1931 static int use_patch(struct apply_state *state, struct patch *p)
1933 const char *pathname = p->new_name ? p->new_name : p->old_name;
1934 int i;
1936 /* Paths outside are not touched regardless of "--include" */
1937 if (0 < state->prefix_length) {
1938 int pathlen = strlen(pathname);
1939 if (pathlen <= state->prefix_length ||
1940 memcmp(state->prefix, pathname, state->prefix_length))
1941 return 0;
1944 /* See if it matches any of exclude/include rule */
1945 for (i = 0; i < state->limit_by_name.nr; i++) {
1946 struct string_list_item *it = &state->limit_by_name.items[i];
1947 if (!wildmatch(it->string, pathname, 0, NULL))
1948 return (it->util != NULL);
1952 * If we had any include, a path that does not match any rule is
1953 * not used. Otherwise, we saw bunch of exclude rules (or none)
1954 * and such a path is used.
1956 return !state->has_include;
1960 * Read the patch text in "buffer" that extends for "size" bytes; stop
1961 * reading after seeing a single patch (i.e. changes to a single file).
1962 * Create fragments (i.e. patch hunks) and hang them to the given patch.
1964 * Returns:
1965 * -1 if no header was found or parse_binary() failed,
1966 * -128 on another error,
1967 * the number of bytes consumed otherwise,
1968 * so that the caller can call us again for the next patch.
1970 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
1972 int hdrsize, patchsize;
1973 int offset = find_header(state, buffer, size, &hdrsize, patch);
1975 if (offset < 0)
1976 return offset;
1978 prefix_patch(state, patch);
1980 if (!use_patch(state, patch))
1981 patch->ws_rule = 0;
1982 else
1983 patch->ws_rule = whitespace_rule(patch->new_name
1984 ? patch->new_name
1985 : patch->old_name);
1987 patchsize = parse_single_patch(state,
1988 buffer + offset + hdrsize,
1989 size - offset - hdrsize,
1990 patch);
1992 if (patchsize < 0)
1993 return -128;
1995 if (!patchsize) {
1996 static const char git_binary[] = "GIT binary patch\n";
1997 int hd = hdrsize + offset;
1998 unsigned long llen = linelen(buffer + hd, size - hd);
2000 if (llen == sizeof(git_binary) - 1 &&
2001 !memcmp(git_binary, buffer + hd, llen)) {
2002 int used;
2003 state->linenr++;
2004 used = parse_binary(state, buffer + hd + llen,
2005 size - hd - llen, patch);
2006 if (used < 0)
2007 return -1;
2008 if (used)
2009 patchsize = used + llen;
2010 else
2011 patchsize = 0;
2013 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2014 static const char *binhdr[] = {
2015 "Binary files ",
2016 "Files ",
2017 NULL,
2019 int i;
2020 for (i = 0; binhdr[i]; i++) {
2021 int len = strlen(binhdr[i]);
2022 if (len < size - hd &&
2023 !memcmp(binhdr[i], buffer + hd, len)) {
2024 state->linenr++;
2025 patch->is_binary = 1;
2026 patchsize = llen;
2027 break;
2032 /* Empty patch cannot be applied if it is a text patch
2033 * without metadata change. A binary patch appears
2034 * empty to us here.
2036 if ((state->apply || state->check) &&
2037 (!patch->is_binary && !metadata_changes(patch))) {
2038 error(_("patch with only garbage at line %d"), state->linenr);
2039 return -128;
2043 return offset + hdrsize + patchsize;
2046 #define swap(a,b) myswap((a),(b),sizeof(a))
2048 #define myswap(a, b, size) do { \
2049 unsigned char mytmp[size]; \
2050 memcpy(mytmp, &a, size); \
2051 memcpy(&a, &b, size); \
2052 memcpy(&b, mytmp, size); \
2053 } while (0)
2055 static void reverse_patches(struct patch *p)
2057 for (; p; p = p->next) {
2058 struct fragment *frag = p->fragments;
2060 swap(p->new_name, p->old_name);
2061 swap(p->new_mode, p->old_mode);
2062 swap(p->is_new, p->is_delete);
2063 swap(p->lines_added, p->lines_deleted);
2064 swap(p->old_sha1_prefix, p->new_sha1_prefix);
2066 for (; frag; frag = frag->next) {
2067 swap(frag->newpos, frag->oldpos);
2068 swap(frag->newlines, frag->oldlines);
2073 static const char pluses[] =
2074 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2075 static const char minuses[]=
2076 "----------------------------------------------------------------------";
2078 static void show_stats(struct apply_state *state, struct patch *patch)
2080 struct strbuf qname = STRBUF_INIT;
2081 char *cp = patch->new_name ? patch->new_name : patch->old_name;
2082 int max, add, del;
2084 quote_c_style(cp, &qname, NULL, 0);
2087 * "scale" the filename
2089 max = state->max_len;
2090 if (max > 50)
2091 max = 50;
2093 if (qname.len > max) {
2094 cp = strchr(qname.buf + qname.len + 3 - max, '/');
2095 if (!cp)
2096 cp = qname.buf + qname.len + 3 - max;
2097 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2100 if (patch->is_binary) {
2101 printf(" %-*s | Bin\n", max, qname.buf);
2102 strbuf_release(&qname);
2103 return;
2106 printf(" %-*s |", max, qname.buf);
2107 strbuf_release(&qname);
2110 * scale the add/delete
2112 max = max + state->max_change > 70 ? 70 - max : state->max_change;
2113 add = patch->lines_added;
2114 del = patch->lines_deleted;
2116 if (state->max_change > 0) {
2117 int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2118 add = (add * max + state->max_change / 2) / state->max_change;
2119 del = total - add;
2121 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2122 add, pluses, del, minuses);
2125 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2127 switch (st->st_mode & S_IFMT) {
2128 case S_IFLNK:
2129 if (strbuf_readlink(buf, path, st->st_size) < 0)
2130 return error(_("unable to read symlink %s"), path);
2131 return 0;
2132 case S_IFREG:
2133 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2134 return error(_("unable to open or read %s"), path);
2135 convert_to_git(path, buf->buf, buf->len, buf, 0);
2136 return 0;
2137 default:
2138 return -1;
2143 * Update the preimage, and the common lines in postimage,
2144 * from buffer buf of length len. If postlen is 0 the postimage
2145 * is updated in place, otherwise it's updated on a new buffer
2146 * of length postlen
2149 static void update_pre_post_images(struct image *preimage,
2150 struct image *postimage,
2151 char *buf,
2152 size_t len, size_t postlen)
2154 int i, ctx, reduced;
2155 char *new, *old, *fixed;
2156 struct image fixed_preimage;
2159 * Update the preimage with whitespace fixes. Note that we
2160 * are not losing preimage->buf -- apply_one_fragment() will
2161 * free "oldlines".
2163 prepare_image(&fixed_preimage, buf, len, 1);
2164 assert(postlen
2165 ? fixed_preimage.nr == preimage->nr
2166 : fixed_preimage.nr <= preimage->nr);
2167 for (i = 0; i < fixed_preimage.nr; i++)
2168 fixed_preimage.line[i].flag = preimage->line[i].flag;
2169 free(preimage->line_allocated);
2170 *preimage = fixed_preimage;
2173 * Adjust the common context lines in postimage. This can be
2174 * done in-place when we are shrinking it with whitespace
2175 * fixing, but needs a new buffer when ignoring whitespace or
2176 * expanding leading tabs to spaces.
2178 * We trust the caller to tell us if the update can be done
2179 * in place (postlen==0) or not.
2181 old = postimage->buf;
2182 if (postlen)
2183 new = postimage->buf = xmalloc(postlen);
2184 else
2185 new = old;
2186 fixed = preimage->buf;
2188 for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2189 size_t l_len = postimage->line[i].len;
2190 if (!(postimage->line[i].flag & LINE_COMMON)) {
2191 /* an added line -- no counterparts in preimage */
2192 memmove(new, old, l_len);
2193 old += l_len;
2194 new += l_len;
2195 continue;
2198 /* a common context -- skip it in the original postimage */
2199 old += l_len;
2201 /* and find the corresponding one in the fixed preimage */
2202 while (ctx < preimage->nr &&
2203 !(preimage->line[ctx].flag & LINE_COMMON)) {
2204 fixed += preimage->line[ctx].len;
2205 ctx++;
2209 * preimage is expected to run out, if the caller
2210 * fixed addition of trailing blank lines.
2212 if (preimage->nr <= ctx) {
2213 reduced++;
2214 continue;
2217 /* and copy it in, while fixing the line length */
2218 l_len = preimage->line[ctx].len;
2219 memcpy(new, fixed, l_len);
2220 new += l_len;
2221 fixed += l_len;
2222 postimage->line[i].len = l_len;
2223 ctx++;
2226 if (postlen
2227 ? postlen < new - postimage->buf
2228 : postimage->len < new - postimage->buf)
2229 die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2230 (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2232 /* Fix the length of the whole thing */
2233 postimage->len = new - postimage->buf;
2234 postimage->nr -= reduced;
2237 static int line_by_line_fuzzy_match(struct image *img,
2238 struct image *preimage,
2239 struct image *postimage,
2240 unsigned long try,
2241 int try_lno,
2242 int preimage_limit)
2244 int i;
2245 size_t imgoff = 0;
2246 size_t preoff = 0;
2247 size_t postlen = postimage->len;
2248 size_t extra_chars;
2249 char *buf;
2250 char *preimage_eof;
2251 char *preimage_end;
2252 struct strbuf fixed;
2253 char *fixed_buf;
2254 size_t fixed_len;
2256 for (i = 0; i < preimage_limit; i++) {
2257 size_t prelen = preimage->line[i].len;
2258 size_t imglen = img->line[try_lno+i].len;
2260 if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2261 preimage->buf + preoff, prelen))
2262 return 0;
2263 if (preimage->line[i].flag & LINE_COMMON)
2264 postlen += imglen - prelen;
2265 imgoff += imglen;
2266 preoff += prelen;
2270 * Ok, the preimage matches with whitespace fuzz.
2272 * imgoff now holds the true length of the target that
2273 * matches the preimage before the end of the file.
2275 * Count the number of characters in the preimage that fall
2276 * beyond the end of the file and make sure that all of them
2277 * are whitespace characters. (This can only happen if
2278 * we are removing blank lines at the end of the file.)
2280 buf = preimage_eof = preimage->buf + preoff;
2281 for ( ; i < preimage->nr; i++)
2282 preoff += preimage->line[i].len;
2283 preimage_end = preimage->buf + preoff;
2284 for ( ; buf < preimage_end; buf++)
2285 if (!isspace(*buf))
2286 return 0;
2289 * Update the preimage and the common postimage context
2290 * lines to use the same whitespace as the target.
2291 * If whitespace is missing in the target (i.e.
2292 * if the preimage extends beyond the end of the file),
2293 * use the whitespace from the preimage.
2295 extra_chars = preimage_end - preimage_eof;
2296 strbuf_init(&fixed, imgoff + extra_chars);
2297 strbuf_add(&fixed, img->buf + try, imgoff);
2298 strbuf_add(&fixed, preimage_eof, extra_chars);
2299 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2300 update_pre_post_images(preimage, postimage,
2301 fixed_buf, fixed_len, postlen);
2302 return 1;
2305 static int match_fragment(struct apply_state *state,
2306 struct image *img,
2307 struct image *preimage,
2308 struct image *postimage,
2309 unsigned long try,
2310 int try_lno,
2311 unsigned ws_rule,
2312 int match_beginning, int match_end)
2314 int i;
2315 char *fixed_buf, *buf, *orig, *target;
2316 struct strbuf fixed;
2317 size_t fixed_len, postlen;
2318 int preimage_limit;
2320 if (preimage->nr + try_lno <= img->nr) {
2322 * The hunk falls within the boundaries of img.
2324 preimage_limit = preimage->nr;
2325 if (match_end && (preimage->nr + try_lno != img->nr))
2326 return 0;
2327 } else if (state->ws_error_action == correct_ws_error &&
2328 (ws_rule & WS_BLANK_AT_EOF)) {
2330 * This hunk extends beyond the end of img, and we are
2331 * removing blank lines at the end of the file. This
2332 * many lines from the beginning of the preimage must
2333 * match with img, and the remainder of the preimage
2334 * must be blank.
2336 preimage_limit = img->nr - try_lno;
2337 } else {
2339 * The hunk extends beyond the end of the img and
2340 * we are not removing blanks at the end, so we
2341 * should reject the hunk at this position.
2343 return 0;
2346 if (match_beginning && try_lno)
2347 return 0;
2349 /* Quick hash check */
2350 for (i = 0; i < preimage_limit; i++)
2351 if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2352 (preimage->line[i].hash != img->line[try_lno + i].hash))
2353 return 0;
2355 if (preimage_limit == preimage->nr) {
2357 * Do we have an exact match? If we were told to match
2358 * at the end, size must be exactly at try+fragsize,
2359 * otherwise try+fragsize must be still within the preimage,
2360 * and either case, the old piece should match the preimage
2361 * exactly.
2363 if ((match_end
2364 ? (try + preimage->len == img->len)
2365 : (try + preimage->len <= img->len)) &&
2366 !memcmp(img->buf + try, preimage->buf, preimage->len))
2367 return 1;
2368 } else {
2370 * The preimage extends beyond the end of img, so
2371 * there cannot be an exact match.
2373 * There must be one non-blank context line that match
2374 * a line before the end of img.
2376 char *buf_end;
2378 buf = preimage->buf;
2379 buf_end = buf;
2380 for (i = 0; i < preimage_limit; i++)
2381 buf_end += preimage->line[i].len;
2383 for ( ; buf < buf_end; buf++)
2384 if (!isspace(*buf))
2385 break;
2386 if (buf == buf_end)
2387 return 0;
2391 * No exact match. If we are ignoring whitespace, run a line-by-line
2392 * fuzzy matching. We collect all the line length information because
2393 * we need it to adjust whitespace if we match.
2395 if (state->ws_ignore_action == ignore_ws_change)
2396 return line_by_line_fuzzy_match(img, preimage, postimage,
2397 try, try_lno, preimage_limit);
2399 if (state->ws_error_action != correct_ws_error)
2400 return 0;
2403 * The hunk does not apply byte-by-byte, but the hash says
2404 * it might with whitespace fuzz. We weren't asked to
2405 * ignore whitespace, we were asked to correct whitespace
2406 * errors, so let's try matching after whitespace correction.
2408 * While checking the preimage against the target, whitespace
2409 * errors in both fixed, we count how large the corresponding
2410 * postimage needs to be. The postimage prepared by
2411 * apply_one_fragment() has whitespace errors fixed on added
2412 * lines already, but the common lines were propagated as-is,
2413 * which may become longer when their whitespace errors are
2414 * fixed.
2417 /* First count added lines in postimage */
2418 postlen = 0;
2419 for (i = 0; i < postimage->nr; i++) {
2420 if (!(postimage->line[i].flag & LINE_COMMON))
2421 postlen += postimage->line[i].len;
2425 * The preimage may extend beyond the end of the file,
2426 * but in this loop we will only handle the part of the
2427 * preimage that falls within the file.
2429 strbuf_init(&fixed, preimage->len + 1);
2430 orig = preimage->buf;
2431 target = img->buf + try;
2432 for (i = 0; i < preimage_limit; i++) {
2433 size_t oldlen = preimage->line[i].len;
2434 size_t tgtlen = img->line[try_lno + i].len;
2435 size_t fixstart = fixed.len;
2436 struct strbuf tgtfix;
2437 int match;
2439 /* Try fixing the line in the preimage */
2440 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2442 /* Try fixing the line in the target */
2443 strbuf_init(&tgtfix, tgtlen);
2444 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2447 * If they match, either the preimage was based on
2448 * a version before our tree fixed whitespace breakage,
2449 * or we are lacking a whitespace-fix patch the tree
2450 * the preimage was based on already had (i.e. target
2451 * has whitespace breakage, the preimage doesn't).
2452 * In either case, we are fixing the whitespace breakages
2453 * so we might as well take the fix together with their
2454 * real change.
2456 match = (tgtfix.len == fixed.len - fixstart &&
2457 !memcmp(tgtfix.buf, fixed.buf + fixstart,
2458 fixed.len - fixstart));
2460 /* Add the length if this is common with the postimage */
2461 if (preimage->line[i].flag & LINE_COMMON)
2462 postlen += tgtfix.len;
2464 strbuf_release(&tgtfix);
2465 if (!match)
2466 goto unmatch_exit;
2468 orig += oldlen;
2469 target += tgtlen;
2474 * Now handle the lines in the preimage that falls beyond the
2475 * end of the file (if any). They will only match if they are
2476 * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2477 * false).
2479 for ( ; i < preimage->nr; i++) {
2480 size_t fixstart = fixed.len; /* start of the fixed preimage */
2481 size_t oldlen = preimage->line[i].len;
2482 int j;
2484 /* Try fixing the line in the preimage */
2485 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2487 for (j = fixstart; j < fixed.len; j++)
2488 if (!isspace(fixed.buf[j]))
2489 goto unmatch_exit;
2491 orig += oldlen;
2495 * Yes, the preimage is based on an older version that still
2496 * has whitespace breakages unfixed, and fixing them makes the
2497 * hunk match. Update the context lines in the postimage.
2499 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2500 if (postlen < postimage->len)
2501 postlen = 0;
2502 update_pre_post_images(preimage, postimage,
2503 fixed_buf, fixed_len, postlen);
2504 return 1;
2506 unmatch_exit:
2507 strbuf_release(&fixed);
2508 return 0;
2511 static int find_pos(struct apply_state *state,
2512 struct image *img,
2513 struct image *preimage,
2514 struct image *postimage,
2515 int line,
2516 unsigned ws_rule,
2517 int match_beginning, int match_end)
2519 int i;
2520 unsigned long backwards, forwards, try;
2521 int backwards_lno, forwards_lno, try_lno;
2524 * If match_beginning or match_end is specified, there is no
2525 * point starting from a wrong line that will never match and
2526 * wander around and wait for a match at the specified end.
2528 if (match_beginning)
2529 line = 0;
2530 else if (match_end)
2531 line = img->nr - preimage->nr;
2534 * Because the comparison is unsigned, the following test
2535 * will also take care of a negative line number that can
2536 * result when match_end and preimage is larger than the target.
2538 if ((size_t) line > img->nr)
2539 line = img->nr;
2541 try = 0;
2542 for (i = 0; i < line; i++)
2543 try += img->line[i].len;
2546 * There's probably some smart way to do this, but I'll leave
2547 * that to the smart and beautiful people. I'm simple and stupid.
2549 backwards = try;
2550 backwards_lno = line;
2551 forwards = try;
2552 forwards_lno = line;
2553 try_lno = line;
2555 for (i = 0; ; i++) {
2556 if (match_fragment(state, img, preimage, postimage,
2557 try, try_lno, ws_rule,
2558 match_beginning, match_end))
2559 return try_lno;
2561 again:
2562 if (backwards_lno == 0 && forwards_lno == img->nr)
2563 break;
2565 if (i & 1) {
2566 if (backwards_lno == 0) {
2567 i++;
2568 goto again;
2570 backwards_lno--;
2571 backwards -= img->line[backwards_lno].len;
2572 try = backwards;
2573 try_lno = backwards_lno;
2574 } else {
2575 if (forwards_lno == img->nr) {
2576 i++;
2577 goto again;
2579 forwards += img->line[forwards_lno].len;
2580 forwards_lno++;
2581 try = forwards;
2582 try_lno = forwards_lno;
2586 return -1;
2589 static void remove_first_line(struct image *img)
2591 img->buf += img->line[0].len;
2592 img->len -= img->line[0].len;
2593 img->line++;
2594 img->nr--;
2597 static void remove_last_line(struct image *img)
2599 img->len -= img->line[--img->nr].len;
2603 * The change from "preimage" and "postimage" has been found to
2604 * apply at applied_pos (counts in line numbers) in "img".
2605 * Update "img" to remove "preimage" and replace it with "postimage".
2607 static void update_image(struct apply_state *state,
2608 struct image *img,
2609 int applied_pos,
2610 struct image *preimage,
2611 struct image *postimage)
2614 * remove the copy of preimage at offset in img
2615 * and replace it with postimage
2617 int i, nr;
2618 size_t remove_count, insert_count, applied_at = 0;
2619 char *result;
2620 int preimage_limit;
2623 * If we are removing blank lines at the end of img,
2624 * the preimage may extend beyond the end.
2625 * If that is the case, we must be careful only to
2626 * remove the part of the preimage that falls within
2627 * the boundaries of img. Initialize preimage_limit
2628 * to the number of lines in the preimage that falls
2629 * within the boundaries.
2631 preimage_limit = preimage->nr;
2632 if (preimage_limit > img->nr - applied_pos)
2633 preimage_limit = img->nr - applied_pos;
2635 for (i = 0; i < applied_pos; i++)
2636 applied_at += img->line[i].len;
2638 remove_count = 0;
2639 for (i = 0; i < preimage_limit; i++)
2640 remove_count += img->line[applied_pos + i].len;
2641 insert_count = postimage->len;
2643 /* Adjust the contents */
2644 result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2645 memcpy(result, img->buf, applied_at);
2646 memcpy(result + applied_at, postimage->buf, postimage->len);
2647 memcpy(result + applied_at + postimage->len,
2648 img->buf + (applied_at + remove_count),
2649 img->len - (applied_at + remove_count));
2650 free(img->buf);
2651 img->buf = result;
2652 img->len += insert_count - remove_count;
2653 result[img->len] = '\0';
2655 /* Adjust the line table */
2656 nr = img->nr + postimage->nr - preimage_limit;
2657 if (preimage_limit < postimage->nr) {
2659 * NOTE: this knows that we never call remove_first_line()
2660 * on anything other than pre/post image.
2662 REALLOC_ARRAY(img->line, nr);
2663 img->line_allocated = img->line;
2665 if (preimage_limit != postimage->nr)
2666 memmove(img->line + applied_pos + postimage->nr,
2667 img->line + applied_pos + preimage_limit,
2668 (img->nr - (applied_pos + preimage_limit)) *
2669 sizeof(*img->line));
2670 memcpy(img->line + applied_pos,
2671 postimage->line,
2672 postimage->nr * sizeof(*img->line));
2673 if (!state->allow_overlap)
2674 for (i = 0; i < postimage->nr; i++)
2675 img->line[applied_pos + i].flag |= LINE_PATCHED;
2676 img->nr = nr;
2680 * Use the patch-hunk text in "frag" to prepare two images (preimage and
2681 * postimage) for the hunk. Find lines that match "preimage" in "img" and
2682 * replace the part of "img" with "postimage" text.
2684 static int apply_one_fragment(struct apply_state *state,
2685 struct image *img, struct fragment *frag,
2686 int inaccurate_eof, unsigned ws_rule,
2687 int nth_fragment)
2689 int match_beginning, match_end;
2690 const char *patch = frag->patch;
2691 int size = frag->size;
2692 char *old, *oldlines;
2693 struct strbuf newlines;
2694 int new_blank_lines_at_end = 0;
2695 int found_new_blank_lines_at_end = 0;
2696 int hunk_linenr = frag->linenr;
2697 unsigned long leading, trailing;
2698 int pos, applied_pos;
2699 struct image preimage;
2700 struct image postimage;
2702 memset(&preimage, 0, sizeof(preimage));
2703 memset(&postimage, 0, sizeof(postimage));
2704 oldlines = xmalloc(size);
2705 strbuf_init(&newlines, size);
2707 old = oldlines;
2708 while (size > 0) {
2709 char first;
2710 int len = linelen(patch, size);
2711 int plen;
2712 int added_blank_line = 0;
2713 int is_blank_context = 0;
2714 size_t start;
2716 if (!len)
2717 break;
2720 * "plen" is how much of the line we should use for
2721 * the actual patch data. Normally we just remove the
2722 * first character on the line, but if the line is
2723 * followed by "\ No newline", then we also remove the
2724 * last one (which is the newline, of course).
2726 plen = len - 1;
2727 if (len < size && patch[len] == '\\')
2728 plen--;
2729 first = *patch;
2730 if (state->apply_in_reverse) {
2731 if (first == '-')
2732 first = '+';
2733 else if (first == '+')
2734 first = '-';
2737 switch (first) {
2738 case '\n':
2739 /* Newer GNU diff, empty context line */
2740 if (plen < 0)
2741 /* ... followed by '\No newline'; nothing */
2742 break;
2743 *old++ = '\n';
2744 strbuf_addch(&newlines, '\n');
2745 add_line_info(&preimage, "\n", 1, LINE_COMMON);
2746 add_line_info(&postimage, "\n", 1, LINE_COMMON);
2747 is_blank_context = 1;
2748 break;
2749 case ' ':
2750 if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2751 ws_blank_line(patch + 1, plen, ws_rule))
2752 is_blank_context = 1;
2753 case '-':
2754 memcpy(old, patch + 1, plen);
2755 add_line_info(&preimage, old, plen,
2756 (first == ' ' ? LINE_COMMON : 0));
2757 old += plen;
2758 if (first == '-')
2759 break;
2760 /* Fall-through for ' ' */
2761 case '+':
2762 /* --no-add does not add new lines */
2763 if (first == '+' && state->no_add)
2764 break;
2766 start = newlines.len;
2767 if (first != '+' ||
2768 !state->whitespace_error ||
2769 state->ws_error_action != correct_ws_error) {
2770 strbuf_add(&newlines, patch + 1, plen);
2772 else {
2773 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2775 add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2776 (first == '+' ? 0 : LINE_COMMON));
2777 if (first == '+' &&
2778 (ws_rule & WS_BLANK_AT_EOF) &&
2779 ws_blank_line(patch + 1, plen, ws_rule))
2780 added_blank_line = 1;
2781 break;
2782 case '@': case '\\':
2783 /* Ignore it, we already handled it */
2784 break;
2785 default:
2786 if (state->apply_verbosely)
2787 error(_("invalid start of line: '%c'"), first);
2788 applied_pos = -1;
2789 goto out;
2791 if (added_blank_line) {
2792 if (!new_blank_lines_at_end)
2793 found_new_blank_lines_at_end = hunk_linenr;
2794 new_blank_lines_at_end++;
2796 else if (is_blank_context)
2798 else
2799 new_blank_lines_at_end = 0;
2800 patch += len;
2801 size -= len;
2802 hunk_linenr++;
2804 if (inaccurate_eof &&
2805 old > oldlines && old[-1] == '\n' &&
2806 newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2807 old--;
2808 strbuf_setlen(&newlines, newlines.len - 1);
2811 leading = frag->leading;
2812 trailing = frag->trailing;
2815 * A hunk to change lines at the beginning would begin with
2816 * @@ -1,L +N,M @@
2817 * but we need to be careful. -U0 that inserts before the second
2818 * line also has this pattern.
2820 * And a hunk to add to an empty file would begin with
2821 * @@ -0,0 +N,M @@
2823 * In other words, a hunk that is (frag->oldpos <= 1) with or
2824 * without leading context must match at the beginning.
2826 match_beginning = (!frag->oldpos ||
2827 (frag->oldpos == 1 && !state->unidiff_zero));
2830 * A hunk without trailing lines must match at the end.
2831 * However, we simply cannot tell if a hunk must match end
2832 * from the lack of trailing lines if the patch was generated
2833 * with unidiff without any context.
2835 match_end = !state->unidiff_zero && !trailing;
2837 pos = frag->newpos ? (frag->newpos - 1) : 0;
2838 preimage.buf = oldlines;
2839 preimage.len = old - oldlines;
2840 postimage.buf = newlines.buf;
2841 postimage.len = newlines.len;
2842 preimage.line = preimage.line_allocated;
2843 postimage.line = postimage.line_allocated;
2845 for (;;) {
2847 applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2848 ws_rule, match_beginning, match_end);
2850 if (applied_pos >= 0)
2851 break;
2853 /* Am I at my context limits? */
2854 if ((leading <= state->p_context) && (trailing <= state->p_context))
2855 break;
2856 if (match_beginning || match_end) {
2857 match_beginning = match_end = 0;
2858 continue;
2862 * Reduce the number of context lines; reduce both
2863 * leading and trailing if they are equal otherwise
2864 * just reduce the larger context.
2866 if (leading >= trailing) {
2867 remove_first_line(&preimage);
2868 remove_first_line(&postimage);
2869 pos--;
2870 leading--;
2872 if (trailing > leading) {
2873 remove_last_line(&preimage);
2874 remove_last_line(&postimage);
2875 trailing--;
2879 if (applied_pos >= 0) {
2880 if (new_blank_lines_at_end &&
2881 preimage.nr + applied_pos >= img->nr &&
2882 (ws_rule & WS_BLANK_AT_EOF) &&
2883 state->ws_error_action != nowarn_ws_error) {
2884 record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
2885 found_new_blank_lines_at_end);
2886 if (state->ws_error_action == correct_ws_error) {
2887 while (new_blank_lines_at_end--)
2888 remove_last_line(&postimage);
2891 * We would want to prevent write_out_results()
2892 * from taking place in apply_patch() that follows
2893 * the callchain led us here, which is:
2894 * apply_patch->check_patch_list->check_patch->
2895 * apply_data->apply_fragments->apply_one_fragment
2897 if (state->ws_error_action == die_on_ws_error)
2898 state->apply = 0;
2901 if (state->apply_verbosely && applied_pos != pos) {
2902 int offset = applied_pos - pos;
2903 if (state->apply_in_reverse)
2904 offset = 0 - offset;
2905 fprintf_ln(stderr,
2906 Q_("Hunk #%d succeeded at %d (offset %d line).",
2907 "Hunk #%d succeeded at %d (offset %d lines).",
2908 offset),
2909 nth_fragment, applied_pos + 1, offset);
2913 * Warn if it was necessary to reduce the number
2914 * of context lines.
2916 if ((leading != frag->leading) ||
2917 (trailing != frag->trailing))
2918 fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
2919 " to apply fragment at %d"),
2920 leading, trailing, applied_pos+1);
2921 update_image(state, img, applied_pos, &preimage, &postimage);
2922 } else {
2923 if (state->apply_verbosely)
2924 error(_("while searching for:\n%.*s"),
2925 (int)(old - oldlines), oldlines);
2928 out:
2929 free(oldlines);
2930 strbuf_release(&newlines);
2931 free(preimage.line_allocated);
2932 free(postimage.line_allocated);
2934 return (applied_pos < 0);
2937 static int apply_binary_fragment(struct apply_state *state,
2938 struct image *img,
2939 struct patch *patch)
2941 struct fragment *fragment = patch->fragments;
2942 unsigned long len;
2943 void *dst;
2945 if (!fragment)
2946 return error(_("missing binary patch data for '%s'"),
2947 patch->new_name ?
2948 patch->new_name :
2949 patch->old_name);
2951 /* Binary patch is irreversible without the optional second hunk */
2952 if (state->apply_in_reverse) {
2953 if (!fragment->next)
2954 return error("cannot reverse-apply a binary patch "
2955 "without the reverse hunk to '%s'",
2956 patch->new_name
2957 ? patch->new_name : patch->old_name);
2958 fragment = fragment->next;
2960 switch (fragment->binary_patch_method) {
2961 case BINARY_DELTA_DEFLATED:
2962 dst = patch_delta(img->buf, img->len, fragment->patch,
2963 fragment->size, &len);
2964 if (!dst)
2965 return -1;
2966 clear_image(img);
2967 img->buf = dst;
2968 img->len = len;
2969 return 0;
2970 case BINARY_LITERAL_DEFLATED:
2971 clear_image(img);
2972 img->len = fragment->size;
2973 img->buf = xmemdupz(fragment->patch, img->len);
2974 return 0;
2976 return -1;
2980 * Replace "img" with the result of applying the binary patch.
2981 * The binary patch data itself in patch->fragment is still kept
2982 * but the preimage prepared by the caller in "img" is freed here
2983 * or in the helper function apply_binary_fragment() this calls.
2985 static int apply_binary(struct apply_state *state,
2986 struct image *img,
2987 struct patch *patch)
2989 const char *name = patch->old_name ? patch->old_name : patch->new_name;
2990 unsigned char sha1[20];
2993 * For safety, we require patch index line to contain
2994 * full 40-byte textual SHA1 for old and new, at least for now.
2996 if (strlen(patch->old_sha1_prefix) != 40 ||
2997 strlen(patch->new_sha1_prefix) != 40 ||
2998 get_sha1_hex(patch->old_sha1_prefix, sha1) ||
2999 get_sha1_hex(patch->new_sha1_prefix, sha1))
3000 return error("cannot apply binary patch to '%s' "
3001 "without full index line", name);
3003 if (patch->old_name) {
3005 * See if the old one matches what the patch
3006 * applies to.
3008 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3009 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3010 return error("the patch applies to '%s' (%s), "
3011 "which does not match the "
3012 "current contents.",
3013 name, sha1_to_hex(sha1));
3015 else {
3016 /* Otherwise, the old one must be empty. */
3017 if (img->len)
3018 return error("the patch applies to an empty "
3019 "'%s' but it is not empty", name);
3022 get_sha1_hex(patch->new_sha1_prefix, sha1);
3023 if (is_null_sha1(sha1)) {
3024 clear_image(img);
3025 return 0; /* deletion patch */
3028 if (has_sha1_file(sha1)) {
3029 /* We already have the postimage */
3030 enum object_type type;
3031 unsigned long size;
3032 char *result;
3034 result = read_sha1_file(sha1, &type, &size);
3035 if (!result)
3036 return error("the necessary postimage %s for "
3037 "'%s' cannot be read",
3038 patch->new_sha1_prefix, name);
3039 clear_image(img);
3040 img->buf = result;
3041 img->len = size;
3042 } else {
3044 * We have verified buf matches the preimage;
3045 * apply the patch data to it, which is stored
3046 * in the patch->fragments->{patch,size}.
3048 if (apply_binary_fragment(state, img, patch))
3049 return error(_("binary patch does not apply to '%s'"),
3050 name);
3052 /* verify that the result matches */
3053 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3054 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3055 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3056 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3059 return 0;
3062 static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3064 struct fragment *frag = patch->fragments;
3065 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3066 unsigned ws_rule = patch->ws_rule;
3067 unsigned inaccurate_eof = patch->inaccurate_eof;
3068 int nth = 0;
3070 if (patch->is_binary)
3071 return apply_binary(state, img, patch);
3073 while (frag) {
3074 nth++;
3075 if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3076 error(_("patch failed: %s:%ld"), name, frag->oldpos);
3077 if (!state->apply_with_reject)
3078 return -1;
3079 frag->rejected = 1;
3081 frag = frag->next;
3083 return 0;
3086 static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3088 if (S_ISGITLINK(mode)) {
3089 strbuf_grow(buf, 100);
3090 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3091 } else {
3092 enum object_type type;
3093 unsigned long sz;
3094 char *result;
3096 result = read_sha1_file(sha1, &type, &sz);
3097 if (!result)
3098 return -1;
3099 /* XXX read_sha1_file NUL-terminates */
3100 strbuf_attach(buf, result, sz, sz + 1);
3102 return 0;
3105 static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3107 if (!ce)
3108 return 0;
3109 return read_blob_object(buf, ce->sha1, ce->ce_mode);
3112 static struct patch *in_fn_table(struct apply_state *state, const char *name)
3114 struct string_list_item *item;
3116 if (name == NULL)
3117 return NULL;
3119 item = string_list_lookup(&state->fn_table, name);
3120 if (item != NULL)
3121 return (struct patch *)item->util;
3123 return NULL;
3127 * item->util in the filename table records the status of the path.
3128 * Usually it points at a patch (whose result records the contents
3129 * of it after applying it), but it could be PATH_WAS_DELETED for a
3130 * path that a previously applied patch has already removed, or
3131 * PATH_TO_BE_DELETED for a path that a later patch would remove.
3133 * The latter is needed to deal with a case where two paths A and B
3134 * are swapped by first renaming A to B and then renaming B to A;
3135 * moving A to B should not be prevented due to presence of B as we
3136 * will remove it in a later patch.
3138 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3139 #define PATH_WAS_DELETED ((struct patch *) -1)
3141 static int to_be_deleted(struct patch *patch)
3143 return patch == PATH_TO_BE_DELETED;
3146 static int was_deleted(struct patch *patch)
3148 return patch == PATH_WAS_DELETED;
3151 static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3153 struct string_list_item *item;
3156 * Always add new_name unless patch is a deletion
3157 * This should cover the cases for normal diffs,
3158 * file creations and copies
3160 if (patch->new_name != NULL) {
3161 item = string_list_insert(&state->fn_table, patch->new_name);
3162 item->util = patch;
3166 * store a failure on rename/deletion cases because
3167 * later chunks shouldn't patch old names
3169 if ((patch->new_name == NULL) || (patch->is_rename)) {
3170 item = string_list_insert(&state->fn_table, patch->old_name);
3171 item->util = PATH_WAS_DELETED;
3175 static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3178 * store information about incoming file deletion
3180 while (patch) {
3181 if ((patch->new_name == NULL) || (patch->is_rename)) {
3182 struct string_list_item *item;
3183 item = string_list_insert(&state->fn_table, patch->old_name);
3184 item->util = PATH_TO_BE_DELETED;
3186 patch = patch->next;
3190 static int checkout_target(struct index_state *istate,
3191 struct cache_entry *ce, struct stat *st)
3193 struct checkout costate;
3195 memset(&costate, 0, sizeof(costate));
3196 costate.base_dir = "";
3197 costate.refresh_cache = 1;
3198 costate.istate = istate;
3199 if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3200 return error(_("cannot checkout %s"), ce->name);
3201 return 0;
3204 static struct patch *previous_patch(struct apply_state *state,
3205 struct patch *patch,
3206 int *gone)
3208 struct patch *previous;
3210 *gone = 0;
3211 if (patch->is_copy || patch->is_rename)
3212 return NULL; /* "git" patches do not depend on the order */
3214 previous = in_fn_table(state, patch->old_name);
3215 if (!previous)
3216 return NULL;
3218 if (to_be_deleted(previous))
3219 return NULL; /* the deletion hasn't happened yet */
3221 if (was_deleted(previous))
3222 *gone = 1;
3224 return previous;
3227 static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3229 if (S_ISGITLINK(ce->ce_mode)) {
3230 if (!S_ISDIR(st->st_mode))
3231 return -1;
3232 return 0;
3234 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3237 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3239 static int load_patch_target(struct apply_state *state,
3240 struct strbuf *buf,
3241 const struct cache_entry *ce,
3242 struct stat *st,
3243 const char *name,
3244 unsigned expected_mode)
3246 if (state->cached || state->check_index) {
3247 if (read_file_or_gitlink(ce, buf))
3248 return error(_("failed to read %s"), name);
3249 } else if (name) {
3250 if (S_ISGITLINK(expected_mode)) {
3251 if (ce)
3252 return read_file_or_gitlink(ce, buf);
3253 else
3254 return SUBMODULE_PATCH_WITHOUT_INDEX;
3255 } else if (has_symlink_leading_path(name, strlen(name))) {
3256 return error(_("reading from '%s' beyond a symbolic link"), name);
3257 } else {
3258 if (read_old_data(st, name, buf))
3259 return error(_("failed to read %s"), name);
3262 return 0;
3266 * We are about to apply "patch"; populate the "image" with the
3267 * current version we have, from the working tree or from the index,
3268 * depending on the situation e.g. --cached/--index. If we are
3269 * applying a non-git patch that incrementally updates the tree,
3270 * we read from the result of a previous diff.
3272 static int load_preimage(struct apply_state *state,
3273 struct image *image,
3274 struct patch *patch, struct stat *st,
3275 const struct cache_entry *ce)
3277 struct strbuf buf = STRBUF_INIT;
3278 size_t len;
3279 char *img;
3280 struct patch *previous;
3281 int status;
3283 previous = previous_patch(state, patch, &status);
3284 if (status)
3285 return error(_("path %s has been renamed/deleted"),
3286 patch->old_name);
3287 if (previous) {
3288 /* We have a patched copy in memory; use that. */
3289 strbuf_add(&buf, previous->result, previous->resultsize);
3290 } else {
3291 status = load_patch_target(state, &buf, ce, st,
3292 patch->old_name, patch->old_mode);
3293 if (status < 0)
3294 return status;
3295 else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3297 * There is no way to apply subproject
3298 * patch without looking at the index.
3299 * NEEDSWORK: shouldn't this be flagged
3300 * as an error???
3302 free_fragment_list(patch->fragments);
3303 patch->fragments = NULL;
3304 } else if (status) {
3305 return error(_("failed to read %s"), patch->old_name);
3309 img = strbuf_detach(&buf, &len);
3310 prepare_image(image, img, len, !patch->is_binary);
3311 return 0;
3314 static int three_way_merge(struct image *image,
3315 char *path,
3316 const unsigned char *base,
3317 const unsigned char *ours,
3318 const unsigned char *theirs)
3320 mmfile_t base_file, our_file, their_file;
3321 mmbuffer_t result = { NULL };
3322 int status;
3324 read_mmblob(&base_file, base);
3325 read_mmblob(&our_file, ours);
3326 read_mmblob(&their_file, theirs);
3327 status = ll_merge(&result, path,
3328 &base_file, "base",
3329 &our_file, "ours",
3330 &their_file, "theirs", NULL);
3331 free(base_file.ptr);
3332 free(our_file.ptr);
3333 free(their_file.ptr);
3334 if (status < 0 || !result.ptr) {
3335 free(result.ptr);
3336 return -1;
3338 clear_image(image);
3339 image->buf = result.ptr;
3340 image->len = result.size;
3342 return status;
3346 * When directly falling back to add/add three-way merge, we read from
3347 * the current contents of the new_name. In no cases other than that
3348 * this function will be called.
3350 static int load_current(struct apply_state *state,
3351 struct image *image,
3352 struct patch *patch)
3354 struct strbuf buf = STRBUF_INIT;
3355 int status, pos;
3356 size_t len;
3357 char *img;
3358 struct stat st;
3359 struct cache_entry *ce;
3360 char *name = patch->new_name;
3361 unsigned mode = patch->new_mode;
3363 if (!patch->is_new)
3364 die("BUG: patch to %s is not a creation", patch->old_name);
3366 pos = cache_name_pos(name, strlen(name));
3367 if (pos < 0)
3368 return error(_("%s: does not exist in index"), name);
3369 ce = active_cache[pos];
3370 if (lstat(name, &st)) {
3371 if (errno != ENOENT)
3372 return error(_("%s: %s"), name, strerror(errno));
3373 if (checkout_target(&the_index, ce, &st))
3374 return -1;
3376 if (verify_index_match(ce, &st))
3377 return error(_("%s: does not match index"), name);
3379 status = load_patch_target(state, &buf, ce, &st, name, mode);
3380 if (status < 0)
3381 return status;
3382 else if (status)
3383 return -1;
3384 img = strbuf_detach(&buf, &len);
3385 prepare_image(image, img, len, !patch->is_binary);
3386 return 0;
3389 static int try_threeway(struct apply_state *state,
3390 struct image *image,
3391 struct patch *patch,
3392 struct stat *st,
3393 const struct cache_entry *ce)
3395 unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3396 struct strbuf buf = STRBUF_INIT;
3397 size_t len;
3398 int status;
3399 char *img;
3400 struct image tmp_image;
3402 /* No point falling back to 3-way merge in these cases */
3403 if (patch->is_delete ||
3404 S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3405 return -1;
3407 /* Preimage the patch was prepared for */
3408 if (patch->is_new)
3409 write_sha1_file("", 0, blob_type, pre_sha1);
3410 else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3411 read_blob_object(&buf, pre_sha1, patch->old_mode))
3412 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3414 fprintf(stderr, "Falling back to three-way merge...\n");
3416 img = strbuf_detach(&buf, &len);
3417 prepare_image(&tmp_image, img, len, 1);
3418 /* Apply the patch to get the post image */
3419 if (apply_fragments(state, &tmp_image, patch) < 0) {
3420 clear_image(&tmp_image);
3421 return -1;
3423 /* post_sha1[] is theirs */
3424 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3425 clear_image(&tmp_image);
3427 /* our_sha1[] is ours */
3428 if (patch->is_new) {
3429 if (load_current(state, &tmp_image, patch))
3430 return error("cannot read the current contents of '%s'",
3431 patch->new_name);
3432 } else {
3433 if (load_preimage(state, &tmp_image, patch, st, ce))
3434 return error("cannot read the current contents of '%s'",
3435 patch->old_name);
3437 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3438 clear_image(&tmp_image);
3440 /* in-core three-way merge between post and our using pre as base */
3441 status = three_way_merge(image, patch->new_name,
3442 pre_sha1, our_sha1, post_sha1);
3443 if (status < 0) {
3444 fprintf(stderr, "Failed to fall back on three-way merge...\n");
3445 return status;
3448 if (status) {
3449 patch->conflicted_threeway = 1;
3450 if (patch->is_new)
3451 oidclr(&patch->threeway_stage[0]);
3452 else
3453 hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3454 hashcpy(patch->threeway_stage[1].hash, our_sha1);
3455 hashcpy(patch->threeway_stage[2].hash, post_sha1);
3456 fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3457 } else {
3458 fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3460 return 0;
3463 static int apply_data(struct apply_state *state, struct patch *patch,
3464 struct stat *st, const struct cache_entry *ce)
3466 struct image image;
3468 if (load_preimage(state, &image, patch, st, ce) < 0)
3469 return -1;
3471 if (patch->direct_to_threeway ||
3472 apply_fragments(state, &image, patch) < 0) {
3473 /* Note: with --reject, apply_fragments() returns 0 */
3474 if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
3475 return -1;
3477 patch->result = image.buf;
3478 patch->resultsize = image.len;
3479 add_to_fn_table(state, patch);
3480 free(image.line_allocated);
3482 if (0 < patch->is_delete && patch->resultsize)
3483 return error(_("removal patch leaves file contents"));
3485 return 0;
3489 * If "patch" that we are looking at modifies or deletes what we have,
3490 * we would want it not to lose any local modification we have, either
3491 * in the working tree or in the index.
3493 * This also decides if a non-git patch is a creation patch or a
3494 * modification to an existing empty file. We do not check the state
3495 * of the current tree for a creation patch in this function; the caller
3496 * check_patch() separately makes sure (and errors out otherwise) that
3497 * the path the patch creates does not exist in the current tree.
3499 static int check_preimage(struct apply_state *state,
3500 struct patch *patch,
3501 struct cache_entry **ce,
3502 struct stat *st)
3504 const char *old_name = patch->old_name;
3505 struct patch *previous = NULL;
3506 int stat_ret = 0, status;
3507 unsigned st_mode = 0;
3509 if (!old_name)
3510 return 0;
3512 assert(patch->is_new <= 0);
3513 previous = previous_patch(state, patch, &status);
3515 if (status)
3516 return error(_("path %s has been renamed/deleted"), old_name);
3517 if (previous) {
3518 st_mode = previous->new_mode;
3519 } else if (!state->cached) {
3520 stat_ret = lstat(old_name, st);
3521 if (stat_ret && errno != ENOENT)
3522 return error(_("%s: %s"), old_name, strerror(errno));
3525 if (state->check_index && !previous) {
3526 int pos = cache_name_pos(old_name, strlen(old_name));
3527 if (pos < 0) {
3528 if (patch->is_new < 0)
3529 goto is_new;
3530 return error(_("%s: does not exist in index"), old_name);
3532 *ce = active_cache[pos];
3533 if (stat_ret < 0) {
3534 if (checkout_target(&the_index, *ce, st))
3535 return -1;
3537 if (!state->cached && verify_index_match(*ce, st))
3538 return error(_("%s: does not match index"), old_name);
3539 if (state->cached)
3540 st_mode = (*ce)->ce_mode;
3541 } else if (stat_ret < 0) {
3542 if (patch->is_new < 0)
3543 goto is_new;
3544 return error(_("%s: %s"), old_name, strerror(errno));
3547 if (!state->cached && !previous)
3548 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3550 if (patch->is_new < 0)
3551 patch->is_new = 0;
3552 if (!patch->old_mode)
3553 patch->old_mode = st_mode;
3554 if ((st_mode ^ patch->old_mode) & S_IFMT)
3555 return error(_("%s: wrong type"), old_name);
3556 if (st_mode != patch->old_mode)
3557 warning(_("%s has type %o, expected %o"),
3558 old_name, st_mode, patch->old_mode);
3559 if (!patch->new_mode && !patch->is_delete)
3560 patch->new_mode = st_mode;
3561 return 0;
3563 is_new:
3564 patch->is_new = 1;
3565 patch->is_delete = 0;
3566 free(patch->old_name);
3567 patch->old_name = NULL;
3568 return 0;
3572 #define EXISTS_IN_INDEX 1
3573 #define EXISTS_IN_WORKTREE 2
3575 static int check_to_create(struct apply_state *state,
3576 const char *new_name,
3577 int ok_if_exists)
3579 struct stat nst;
3581 if (state->check_index &&
3582 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3583 !ok_if_exists)
3584 return EXISTS_IN_INDEX;
3585 if (state->cached)
3586 return 0;
3588 if (!lstat(new_name, &nst)) {
3589 if (S_ISDIR(nst.st_mode) || ok_if_exists)
3590 return 0;
3592 * A leading component of new_name might be a symlink
3593 * that is going to be removed with this patch, but
3594 * still pointing at somewhere that has the path.
3595 * In such a case, path "new_name" does not exist as
3596 * far as git is concerned.
3598 if (has_symlink_leading_path(new_name, strlen(new_name)))
3599 return 0;
3601 return EXISTS_IN_WORKTREE;
3602 } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3603 return error("%s: %s", new_name, strerror(errno));
3605 return 0;
3608 static uintptr_t register_symlink_changes(struct apply_state *state,
3609 const char *path,
3610 uintptr_t what)
3612 struct string_list_item *ent;
3614 ent = string_list_lookup(&state->symlink_changes, path);
3615 if (!ent) {
3616 ent = string_list_insert(&state->symlink_changes, path);
3617 ent->util = (void *)0;
3619 ent->util = (void *)(what | ((uintptr_t)ent->util));
3620 return (uintptr_t)ent->util;
3623 static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3625 struct string_list_item *ent;
3627 ent = string_list_lookup(&state->symlink_changes, path);
3628 if (!ent)
3629 return 0;
3630 return (uintptr_t)ent->util;
3633 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3635 for ( ; patch; patch = patch->next) {
3636 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3637 (patch->is_rename || patch->is_delete))
3638 /* the symlink at patch->old_name is removed */
3639 register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3641 if (patch->new_name && S_ISLNK(patch->new_mode))
3642 /* the symlink at patch->new_name is created or remains */
3643 register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3647 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3649 do {
3650 unsigned int change;
3652 while (--name->len && name->buf[name->len] != '/')
3653 ; /* scan backwards */
3654 if (!name->len)
3655 break;
3656 name->buf[name->len] = '\0';
3657 change = check_symlink_changes(state, name->buf);
3658 if (change & APPLY_SYMLINK_IN_RESULT)
3659 return 1;
3660 if (change & APPLY_SYMLINK_GOES_AWAY)
3662 * This cannot be "return 0", because we may
3663 * see a new one created at a higher level.
3665 continue;
3667 /* otherwise, check the preimage */
3668 if (state->check_index) {
3669 struct cache_entry *ce;
3671 ce = cache_file_exists(name->buf, name->len, ignore_case);
3672 if (ce && S_ISLNK(ce->ce_mode))
3673 return 1;
3674 } else {
3675 struct stat st;
3676 if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3677 return 1;
3679 } while (1);
3680 return 0;
3683 static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3685 int ret;
3686 struct strbuf name = STRBUF_INIT;
3688 assert(*name_ != '\0');
3689 strbuf_addstr(&name, name_);
3690 ret = path_is_beyond_symlink_1(state, &name);
3691 strbuf_release(&name);
3693 return ret;
3696 static void die_on_unsafe_path(struct patch *patch)
3698 const char *old_name = NULL;
3699 const char *new_name = NULL;
3700 if (patch->is_delete)
3701 old_name = patch->old_name;
3702 else if (!patch->is_new && !patch->is_copy)
3703 old_name = patch->old_name;
3704 if (!patch->is_delete)
3705 new_name = patch->new_name;
3707 if (old_name && !verify_path(old_name))
3708 die(_("invalid path '%s'"), old_name);
3709 if (new_name && !verify_path(new_name))
3710 die(_("invalid path '%s'"), new_name);
3714 * Check and apply the patch in-core; leave the result in patch->result
3715 * for the caller to write it out to the final destination.
3717 static int check_patch(struct apply_state *state, struct patch *patch)
3719 struct stat st;
3720 const char *old_name = patch->old_name;
3721 const char *new_name = patch->new_name;
3722 const char *name = old_name ? old_name : new_name;
3723 struct cache_entry *ce = NULL;
3724 struct patch *tpatch;
3725 int ok_if_exists;
3726 int status;
3728 patch->rejected = 1; /* we will drop this after we succeed */
3730 status = check_preimage(state, patch, &ce, &st);
3731 if (status)
3732 return status;
3733 old_name = patch->old_name;
3736 * A type-change diff is always split into a patch to delete
3737 * old, immediately followed by a patch to create new (see
3738 * diff.c::run_diff()); in such a case it is Ok that the entry
3739 * to be deleted by the previous patch is still in the working
3740 * tree and in the index.
3742 * A patch to swap-rename between A and B would first rename A
3743 * to B and then rename B to A. While applying the first one,
3744 * the presence of B should not stop A from getting renamed to
3745 * B; ask to_be_deleted() about the later rename. Removal of
3746 * B and rename from A to B is handled the same way by asking
3747 * was_deleted().
3749 if ((tpatch = in_fn_table(state, new_name)) &&
3750 (was_deleted(tpatch) || to_be_deleted(tpatch)))
3751 ok_if_exists = 1;
3752 else
3753 ok_if_exists = 0;
3755 if (new_name &&
3756 ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3757 int err = check_to_create(state, new_name, ok_if_exists);
3759 if (err && state->threeway) {
3760 patch->direct_to_threeway = 1;
3761 } else switch (err) {
3762 case 0:
3763 break; /* happy */
3764 case EXISTS_IN_INDEX:
3765 return error(_("%s: already exists in index"), new_name);
3766 break;
3767 case EXISTS_IN_WORKTREE:
3768 return error(_("%s: already exists in working directory"),
3769 new_name);
3770 default:
3771 return err;
3774 if (!patch->new_mode) {
3775 if (0 < patch->is_new)
3776 patch->new_mode = S_IFREG | 0644;
3777 else
3778 patch->new_mode = patch->old_mode;
3782 if (new_name && old_name) {
3783 int same = !strcmp(old_name, new_name);
3784 if (!patch->new_mode)
3785 patch->new_mode = patch->old_mode;
3786 if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3787 if (same)
3788 return error(_("new mode (%o) of %s does not "
3789 "match old mode (%o)"),
3790 patch->new_mode, new_name,
3791 patch->old_mode);
3792 else
3793 return error(_("new mode (%o) of %s does not "
3794 "match old mode (%o) of %s"),
3795 patch->new_mode, new_name,
3796 patch->old_mode, old_name);
3800 if (!state->unsafe_paths)
3801 die_on_unsafe_path(patch);
3804 * An attempt to read from or delete a path that is beyond a
3805 * symbolic link will be prevented by load_patch_target() that
3806 * is called at the beginning of apply_data() so we do not
3807 * have to worry about a patch marked with "is_delete" bit
3808 * here. We however need to make sure that the patch result
3809 * is not deposited to a path that is beyond a symbolic link
3810 * here.
3812 if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3813 return error(_("affected file '%s' is beyond a symbolic link"),
3814 patch->new_name);
3816 if (apply_data(state, patch, &st, ce) < 0)
3817 return error(_("%s: patch does not apply"), name);
3818 patch->rejected = 0;
3819 return 0;
3822 static int check_patch_list(struct apply_state *state, struct patch *patch)
3824 int err = 0;
3826 prepare_symlink_changes(state, patch);
3827 prepare_fn_table(state, patch);
3828 while (patch) {
3829 if (state->apply_verbosely)
3830 say_patch_name(stderr,
3831 _("Checking patch %s..."), patch);
3832 err |= check_patch(state, patch);
3833 patch = patch->next;
3835 return err;
3838 /* This function tries to read the sha1 from the current index */
3839 static int get_current_sha1(const char *path, unsigned char *sha1)
3841 int pos;
3843 if (read_cache() < 0)
3844 return -1;
3845 pos = cache_name_pos(path, strlen(path));
3846 if (pos < 0)
3847 return -1;
3848 hashcpy(sha1, active_cache[pos]->sha1);
3849 return 0;
3852 static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3855 * A usable gitlink patch has only one fragment (hunk) that looks like:
3856 * @@ -1 +1 @@
3857 * -Subproject commit <old sha1>
3858 * +Subproject commit <new sha1>
3859 * or
3860 * @@ -1 +0,0 @@
3861 * -Subproject commit <old sha1>
3862 * for a removal patch.
3864 struct fragment *hunk = p->fragments;
3865 static const char heading[] = "-Subproject commit ";
3866 char *preimage;
3868 if (/* does the patch have only one hunk? */
3869 hunk && !hunk->next &&
3870 /* is its preimage one line? */
3871 hunk->oldpos == 1 && hunk->oldlines == 1 &&
3872 /* does preimage begin with the heading? */
3873 (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3874 starts_with(++preimage, heading) &&
3875 /* does it record full SHA-1? */
3876 !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
3877 preimage[sizeof(heading) + 40 - 1] == '\n' &&
3878 /* does the abbreviated name on the index line agree with it? */
3879 starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3880 return 0; /* it all looks fine */
3882 /* we may have full object name on the index line */
3883 return get_sha1_hex(p->old_sha1_prefix, sha1);
3886 /* Build an index that contains the just the files needed for a 3way merge */
3887 static void build_fake_ancestor(struct patch *list, const char *filename)
3889 struct patch *patch;
3890 struct index_state result = { NULL };
3891 static struct lock_file lock;
3893 /* Once we start supporting the reverse patch, it may be
3894 * worth showing the new sha1 prefix, but until then...
3896 for (patch = list; patch; patch = patch->next) {
3897 unsigned char sha1[20];
3898 struct cache_entry *ce;
3899 const char *name;
3901 name = patch->old_name ? patch->old_name : patch->new_name;
3902 if (0 < patch->is_new)
3903 continue;
3905 if (S_ISGITLINK(patch->old_mode)) {
3906 if (!preimage_sha1_in_gitlink_patch(patch, sha1))
3907 ; /* ok, the textual part looks sane */
3908 else
3909 die("sha1 information is lacking or useless for submodule %s",
3910 name);
3911 } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
3912 ; /* ok */
3913 } else if (!patch->lines_added && !patch->lines_deleted) {
3914 /* mode-only change: update the current */
3915 if (get_current_sha1(patch->old_name, sha1))
3916 die("mode change for %s, which is not "
3917 "in current HEAD", name);
3918 } else
3919 die("sha1 information is lacking or useless "
3920 "(%s).", name);
3922 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
3923 if (!ce)
3924 die(_("make_cache_entry failed for path '%s'"), name);
3925 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
3926 die ("Could not add %s to temporary index", name);
3929 hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
3930 if (write_locked_index(&result, &lock, COMMIT_LOCK))
3931 die ("Could not write temporary index to %s", filename);
3933 discard_index(&result);
3936 static void stat_patch_list(struct apply_state *state, struct patch *patch)
3938 int files, adds, dels;
3940 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
3941 files++;
3942 adds += patch->lines_added;
3943 dels += patch->lines_deleted;
3944 show_stats(state, patch);
3947 print_stat_summary(stdout, files, adds, dels);
3950 static void numstat_patch_list(struct apply_state *state,
3951 struct patch *patch)
3953 for ( ; patch; patch = patch->next) {
3954 const char *name;
3955 name = patch->new_name ? patch->new_name : patch->old_name;
3956 if (patch->is_binary)
3957 printf("-\t-\t");
3958 else
3959 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
3960 write_name_quoted(name, stdout, state->line_termination);
3964 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
3966 if (mode)
3967 printf(" %s mode %06o %s\n", newdelete, mode, name);
3968 else
3969 printf(" %s %s\n", newdelete, name);
3972 static void show_mode_change(struct patch *p, int show_name)
3974 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
3975 if (show_name)
3976 printf(" mode change %06o => %06o %s\n",
3977 p->old_mode, p->new_mode, p->new_name);
3978 else
3979 printf(" mode change %06o => %06o\n",
3980 p->old_mode, p->new_mode);
3984 static void show_rename_copy(struct patch *p)
3986 const char *renamecopy = p->is_rename ? "rename" : "copy";
3987 const char *old, *new;
3989 /* Find common prefix */
3990 old = p->old_name;
3991 new = p->new_name;
3992 while (1) {
3993 const char *slash_old, *slash_new;
3994 slash_old = strchr(old, '/');
3995 slash_new = strchr(new, '/');
3996 if (!slash_old ||
3997 !slash_new ||
3998 slash_old - old != slash_new - new ||
3999 memcmp(old, new, slash_new - new))
4000 break;
4001 old = slash_old + 1;
4002 new = slash_new + 1;
4004 /* p->old_name thru old is the common prefix, and old and new
4005 * through the end of names are renames
4007 if (old != p->old_name)
4008 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4009 (int)(old - p->old_name), p->old_name,
4010 old, new, p->score);
4011 else
4012 printf(" %s %s => %s (%d%%)\n", renamecopy,
4013 p->old_name, p->new_name, p->score);
4014 show_mode_change(p, 0);
4017 static void summary_patch_list(struct patch *patch)
4019 struct patch *p;
4021 for (p = patch; p; p = p->next) {
4022 if (p->is_new)
4023 show_file_mode_name("create", p->new_mode, p->new_name);
4024 else if (p->is_delete)
4025 show_file_mode_name("delete", p->old_mode, p->old_name);
4026 else {
4027 if (p->is_rename || p->is_copy)
4028 show_rename_copy(p);
4029 else {
4030 if (p->score) {
4031 printf(" rewrite %s (%d%%)\n",
4032 p->new_name, p->score);
4033 show_mode_change(p, 0);
4035 else
4036 show_mode_change(p, 1);
4042 static void patch_stats(struct apply_state *state, struct patch *patch)
4044 int lines = patch->lines_added + patch->lines_deleted;
4046 if (lines > state->max_change)
4047 state->max_change = lines;
4048 if (patch->old_name) {
4049 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4050 if (!len)
4051 len = strlen(patch->old_name);
4052 if (len > state->max_len)
4053 state->max_len = len;
4055 if (patch->new_name) {
4056 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4057 if (!len)
4058 len = strlen(patch->new_name);
4059 if (len > state->max_len)
4060 state->max_len = len;
4064 static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4066 if (state->update_index) {
4067 if (remove_file_from_cache(patch->old_name) < 0)
4068 die(_("unable to remove %s from index"), patch->old_name);
4070 if (!state->cached) {
4071 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4072 remove_path(patch->old_name);
4077 static void add_index_file(struct apply_state *state,
4078 const char *path,
4079 unsigned mode,
4080 void *buf,
4081 unsigned long size)
4083 struct stat st;
4084 struct cache_entry *ce;
4085 int namelen = strlen(path);
4086 unsigned ce_size = cache_entry_size(namelen);
4088 if (!state->update_index)
4089 return;
4091 ce = xcalloc(1, ce_size);
4092 memcpy(ce->name, path, namelen);
4093 ce->ce_mode = create_ce_mode(mode);
4094 ce->ce_flags = create_ce_flags(0);
4095 ce->ce_namelen = namelen;
4096 if (S_ISGITLINK(mode)) {
4097 const char *s;
4099 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4100 get_sha1_hex(s, ce->sha1))
4101 die(_("corrupt patch for submodule %s"), path);
4102 } else {
4103 if (!state->cached) {
4104 if (lstat(path, &st) < 0)
4105 die_errno(_("unable to stat newly created file '%s'"),
4106 path);
4107 fill_stat_cache_info(ce, &st);
4109 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
4110 die(_("unable to create backing store for newly created file %s"), path);
4112 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4113 die(_("unable to add cache entry for %s"), path);
4116 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4118 int fd;
4119 struct strbuf nbuf = STRBUF_INIT;
4121 if (S_ISGITLINK(mode)) {
4122 struct stat st;
4123 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4124 return 0;
4125 return mkdir(path, 0777);
4128 if (has_symlinks && S_ISLNK(mode))
4129 /* Although buf:size is counted string, it also is NUL
4130 * terminated.
4132 return symlink(buf, path);
4134 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4135 if (fd < 0)
4136 return -1;
4138 if (convert_to_working_tree(path, buf, size, &nbuf)) {
4139 size = nbuf.len;
4140 buf = nbuf.buf;
4142 write_or_die(fd, buf, size);
4143 strbuf_release(&nbuf);
4145 if (close(fd) < 0)
4146 die_errno(_("closing file '%s'"), path);
4147 return 0;
4151 * We optimistically assume that the directories exist,
4152 * which is true 99% of the time anyway. If they don't,
4153 * we create them and try again.
4155 static void create_one_file(struct apply_state *state,
4156 char *path,
4157 unsigned mode,
4158 const char *buf,
4159 unsigned long size)
4161 if (state->cached)
4162 return;
4163 if (!try_create_file(path, mode, buf, size))
4164 return;
4166 if (errno == ENOENT) {
4167 if (safe_create_leading_directories(path))
4168 return;
4169 if (!try_create_file(path, mode, buf, size))
4170 return;
4173 if (errno == EEXIST || errno == EACCES) {
4174 /* We may be trying to create a file where a directory
4175 * used to be.
4177 struct stat st;
4178 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4179 errno = EEXIST;
4182 if (errno == EEXIST) {
4183 unsigned int nr = getpid();
4185 for (;;) {
4186 char newpath[PATH_MAX];
4187 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4188 if (!try_create_file(newpath, mode, buf, size)) {
4189 if (!rename(newpath, path))
4190 return;
4191 unlink_or_warn(newpath);
4192 break;
4194 if (errno != EEXIST)
4195 break;
4196 ++nr;
4199 die_errno(_("unable to write file '%s' mode %o"), path, mode);
4202 static void add_conflicted_stages_file(struct apply_state *state,
4203 struct patch *patch)
4205 int stage, namelen;
4206 unsigned ce_size, mode;
4207 struct cache_entry *ce;
4209 if (!state->update_index)
4210 return;
4211 namelen = strlen(patch->new_name);
4212 ce_size = cache_entry_size(namelen);
4213 mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4215 remove_file_from_cache(patch->new_name);
4216 for (stage = 1; stage < 4; stage++) {
4217 if (is_null_oid(&patch->threeway_stage[stage - 1]))
4218 continue;
4219 ce = xcalloc(1, ce_size);
4220 memcpy(ce->name, patch->new_name, namelen);
4221 ce->ce_mode = create_ce_mode(mode);
4222 ce->ce_flags = create_ce_flags(stage);
4223 ce->ce_namelen = namelen;
4224 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4225 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4226 die(_("unable to add cache entry for %s"), patch->new_name);
4230 static void create_file(struct apply_state *state, struct patch *patch)
4232 char *path = patch->new_name;
4233 unsigned mode = patch->new_mode;
4234 unsigned long size = patch->resultsize;
4235 char *buf = patch->result;
4237 if (!mode)
4238 mode = S_IFREG | 0644;
4239 create_one_file(state, path, mode, buf, size);
4241 if (patch->conflicted_threeway)
4242 add_conflicted_stages_file(state, patch);
4243 else
4244 add_index_file(state, path, mode, buf, size);
4247 /* phase zero is to remove, phase one is to create */
4248 static void write_out_one_result(struct apply_state *state,
4249 struct patch *patch,
4250 int phase)
4252 if (patch->is_delete > 0) {
4253 if (phase == 0)
4254 remove_file(state, patch, 1);
4255 return;
4257 if (patch->is_new > 0 || patch->is_copy) {
4258 if (phase == 1)
4259 create_file(state, patch);
4260 return;
4263 * Rename or modification boils down to the same
4264 * thing: remove the old, write the new
4266 if (phase == 0)
4267 remove_file(state, patch, patch->is_rename);
4268 if (phase == 1)
4269 create_file(state, patch);
4272 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4274 FILE *rej;
4275 char namebuf[PATH_MAX];
4276 struct fragment *frag;
4277 int cnt = 0;
4278 struct strbuf sb = STRBUF_INIT;
4280 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4281 if (!frag->rejected)
4282 continue;
4283 cnt++;
4286 if (!cnt) {
4287 if (state->apply_verbosely)
4288 say_patch_name(stderr,
4289 _("Applied patch %s cleanly."), patch);
4290 return 0;
4293 /* This should not happen, because a removal patch that leaves
4294 * contents are marked "rejected" at the patch level.
4296 if (!patch->new_name)
4297 die(_("internal error"));
4299 /* Say this even without --verbose */
4300 strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4301 "Applying patch %%s with %d rejects...",
4302 cnt),
4303 cnt);
4304 say_patch_name(stderr, sb.buf, patch);
4305 strbuf_release(&sb);
4307 cnt = strlen(patch->new_name);
4308 if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4309 cnt = ARRAY_SIZE(namebuf) - 5;
4310 warning(_("truncating .rej filename to %.*s.rej"),
4311 cnt - 1, patch->new_name);
4313 memcpy(namebuf, patch->new_name, cnt);
4314 memcpy(namebuf + cnt, ".rej", 5);
4316 rej = fopen(namebuf, "w");
4317 if (!rej)
4318 return error(_("cannot open %s: %s"), namebuf, strerror(errno));
4320 /* Normal git tools never deal with .rej, so do not pretend
4321 * this is a git patch by saying --git or giving extended
4322 * headers. While at it, maybe please "kompare" that wants
4323 * the trailing TAB and some garbage at the end of line ;-).
4325 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4326 patch->new_name, patch->new_name);
4327 for (cnt = 1, frag = patch->fragments;
4328 frag;
4329 cnt++, frag = frag->next) {
4330 if (!frag->rejected) {
4331 fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4332 continue;
4334 fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4335 fprintf(rej, "%.*s", frag->size, frag->patch);
4336 if (frag->patch[frag->size-1] != '\n')
4337 fputc('\n', rej);
4339 fclose(rej);
4340 return -1;
4343 static int write_out_results(struct apply_state *state, struct patch *list)
4345 int phase;
4346 int errs = 0;
4347 struct patch *l;
4348 struct string_list cpath = STRING_LIST_INIT_DUP;
4350 for (phase = 0; phase < 2; phase++) {
4351 l = list;
4352 while (l) {
4353 if (l->rejected)
4354 errs = 1;
4355 else {
4356 write_out_one_result(state, l, phase);
4357 if (phase == 1) {
4358 if (write_out_one_reject(state, l))
4359 errs = 1;
4360 if (l->conflicted_threeway) {
4361 string_list_append(&cpath, l->new_name);
4362 errs = 1;
4366 l = l->next;
4370 if (cpath.nr) {
4371 struct string_list_item *item;
4373 string_list_sort(&cpath);
4374 for_each_string_list_item(item, &cpath)
4375 fprintf(stderr, "U %s\n", item->string);
4376 string_list_clear(&cpath, 0);
4378 rerere(0);
4381 return errs;
4384 static struct lock_file lock_file;
4386 #define INACCURATE_EOF (1<<0)
4387 #define RECOUNT (1<<1)
4390 * Try to apply a patch.
4392 * Returns:
4393 * -128 if a bad error happened (like patch unreadable)
4394 * -1 if patch did not apply and user cannot deal with it
4395 * 0 if the patch applied
4396 * 1 if the patch did not apply but user might fix it
4398 static int apply_patch(struct apply_state *state,
4399 int fd,
4400 const char *filename,
4401 int options)
4403 size_t offset;
4404 struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4405 struct patch *list = NULL, **listp = &list;
4406 int skipped_patch = 0;
4407 int res = 0;
4409 state->patch_input_file = filename;
4410 if (read_patch_file(&buf, fd) < 0)
4411 return -128;
4412 offset = 0;
4413 while (offset < buf.len) {
4414 struct patch *patch;
4415 int nr;
4417 patch = xcalloc(1, sizeof(*patch));
4418 patch->inaccurate_eof = !!(options & INACCURATE_EOF);
4419 patch->recount = !!(options & RECOUNT);
4420 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4421 if (nr < 0) {
4422 free_patch(patch);
4423 if (nr == -128) {
4424 res = -128;
4425 goto end;
4427 break;
4429 if (state->apply_in_reverse)
4430 reverse_patches(patch);
4431 if (use_patch(state, patch)) {
4432 patch_stats(state, patch);
4433 *listp = patch;
4434 listp = &patch->next;
4436 else {
4437 if (state->apply_verbosely)
4438 say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4439 free_patch(patch);
4440 skipped_patch++;
4442 offset += nr;
4445 if (!list && !skipped_patch) {
4446 error(_("unrecognized input"));
4447 res = -128;
4448 goto end;
4451 if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4452 state->apply = 0;
4454 state->update_index = state->check_index && state->apply;
4455 if (state->update_index && state->newfd < 0)
4456 state->newfd = hold_locked_index(state->lock_file, 1);
4458 if (state->check_index && read_cache() < 0) {
4459 error(_("unable to read index file"));
4460 res = -128;
4461 goto end;
4464 if ((state->check || state->apply) &&
4465 check_patch_list(state, list) < 0 &&
4466 !state->apply_with_reject) {
4467 res = -1;
4468 goto end;
4471 if (state->apply && write_out_results(state, list)) {
4472 /* with --3way, we still need to write the index out */
4473 res = state->apply_with_reject ? -1 : 1;
4474 goto end;
4477 if (state->fake_ancestor)
4478 build_fake_ancestor(list, state->fake_ancestor);
4480 if (state->diffstat)
4481 stat_patch_list(state, list);
4483 if (state->numstat)
4484 numstat_patch_list(state, list);
4486 if (state->summary)
4487 summary_patch_list(list);
4489 end:
4490 free_patch_list(list);
4491 strbuf_release(&buf);
4492 string_list_clear(&state->fn_table, 0);
4493 return res;
4496 static int option_parse_exclude(const struct option *opt,
4497 const char *arg, int unset)
4499 struct apply_state *state = opt->value;
4500 add_name_limit(state, arg, 1);
4501 return 0;
4504 static int option_parse_include(const struct option *opt,
4505 const char *arg, int unset)
4507 struct apply_state *state = opt->value;
4508 add_name_limit(state, arg, 0);
4509 state->has_include = 1;
4510 return 0;
4513 static int option_parse_p(const struct option *opt,
4514 const char *arg,
4515 int unset)
4517 struct apply_state *state = opt->value;
4518 state->p_value = atoi(arg);
4519 state->p_value_known = 1;
4520 return 0;
4523 static int option_parse_space_change(const struct option *opt,
4524 const char *arg, int unset)
4526 struct apply_state *state = opt->value;
4527 if (unset)
4528 state->ws_ignore_action = ignore_ws_none;
4529 else
4530 state->ws_ignore_action = ignore_ws_change;
4531 return 0;
4534 static int option_parse_whitespace(const struct option *opt,
4535 const char *arg, int unset)
4537 struct apply_state *state = opt->value;
4538 state->whitespace_option = arg;
4539 if (parse_whitespace_option(state, arg))
4540 exit(1);
4541 return 0;
4544 static int option_parse_directory(const struct option *opt,
4545 const char *arg, int unset)
4547 struct apply_state *state = opt->value;
4548 strbuf_reset(&state->root);
4549 strbuf_addstr(&state->root, arg);
4550 strbuf_complete(&state->root, '/');
4551 return 0;
4554 static int apply_all_patches(struct apply_state *state,
4555 int argc,
4556 const char **argv,
4557 int options)
4559 int i;
4560 int res;
4561 int errs = 0;
4562 int read_stdin = 1;
4564 for (i = 0; i < argc; i++) {
4565 const char *arg = argv[i];
4566 int fd;
4568 if (!strcmp(arg, "-")) {
4569 res = apply_patch(state, 0, "<stdin>", options);
4570 if (res < 0)
4571 goto end;
4572 errs |= res;
4573 read_stdin = 0;
4574 continue;
4575 } else if (0 < state->prefix_length)
4576 arg = prefix_filename(state->prefix,
4577 state->prefix_length,
4578 arg);
4580 fd = open(arg, O_RDONLY);
4581 if (fd < 0) {
4582 error(_("can't open patch '%s': %s"), arg, strerror(errno));
4583 res = -128;
4584 goto end;
4586 read_stdin = 0;
4587 set_default_whitespace_mode(state);
4588 res = apply_patch(state, fd, arg, options);
4589 close(fd);
4590 if (res < 0)
4591 goto end;
4592 errs |= res;
4594 set_default_whitespace_mode(state);
4595 if (read_stdin) {
4596 res = apply_patch(state, 0, "<stdin>", options);
4597 if (res < 0)
4598 goto end;
4599 errs |= res;
4602 if (state->whitespace_error) {
4603 if (state->squelch_whitespace_errors &&
4604 state->squelch_whitespace_errors < state->whitespace_error) {
4605 int squelched =
4606 state->whitespace_error - state->squelch_whitespace_errors;
4607 warning(Q_("squelched %d whitespace error",
4608 "squelched %d whitespace errors",
4609 squelched),
4610 squelched);
4612 if (state->ws_error_action == die_on_ws_error) {
4613 error(Q_("%d line adds whitespace errors.",
4614 "%d lines add whitespace errors.",
4615 state->whitespace_error),
4616 state->whitespace_error);
4617 res = -128;
4618 goto end;
4620 if (state->applied_after_fixing_ws && state->apply)
4621 warning("%d line%s applied after"
4622 " fixing whitespace errors.",
4623 state->applied_after_fixing_ws,
4624 state->applied_after_fixing_ws == 1 ? "" : "s");
4625 else if (state->whitespace_error)
4626 warning(Q_("%d line adds whitespace errors.",
4627 "%d lines add whitespace errors.",
4628 state->whitespace_error),
4629 state->whitespace_error);
4632 if (state->update_index) {
4633 res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
4634 if (res) {
4635 error(_("Unable to write new index file"));
4636 res = -128;
4637 goto end;
4639 state->newfd = -1;
4642 return !!errs;
4644 end:
4645 if (state->newfd >= 0) {
4646 rollback_lock_file(state->lock_file);
4647 state->newfd = -1;
4650 return (res == -1 ? 1 : 128);
4653 int cmd_apply(int argc, const char **argv, const char *prefix)
4655 int force_apply = 0;
4656 int options = 0;
4657 int ret;
4658 struct apply_state state;
4660 struct option builtin_apply_options[] = {
4661 { OPTION_CALLBACK, 0, "exclude", &state, N_("path"),
4662 N_("don't apply changes matching the given path"),
4663 0, option_parse_exclude },
4664 { OPTION_CALLBACK, 0, "include", &state, N_("path"),
4665 N_("apply changes matching the given path"),
4666 0, option_parse_include },
4667 { OPTION_CALLBACK, 'p', NULL, &state, N_("num"),
4668 N_("remove <num> leading slashes from traditional diff paths"),
4669 0, option_parse_p },
4670 OPT_BOOL(0, "no-add", &state.no_add,
4671 N_("ignore additions made by the patch")),
4672 OPT_BOOL(0, "stat", &state.diffstat,
4673 N_("instead of applying the patch, output diffstat for the input")),
4674 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
4675 OPT_NOOP_NOARG(0, "binary"),
4676 OPT_BOOL(0, "numstat", &state.numstat,
4677 N_("show number of added and deleted lines in decimal notation")),
4678 OPT_BOOL(0, "summary", &state.summary,
4679 N_("instead of applying the patch, output a summary for the input")),
4680 OPT_BOOL(0, "check", &state.check,
4681 N_("instead of applying the patch, see if the patch is applicable")),
4682 OPT_BOOL(0, "index", &state.check_index,
4683 N_("make sure the patch is applicable to the current index")),
4684 OPT_BOOL(0, "cached", &state.cached,
4685 N_("apply a patch without touching the working tree")),
4686 OPT_BOOL(0, "unsafe-paths", &state.unsafe_paths,
4687 N_("accept a patch that touches outside the working area")),
4688 OPT_BOOL(0, "apply", &force_apply,
4689 N_("also apply the patch (use with --stat/--summary/--check)")),
4690 OPT_BOOL('3', "3way", &state.threeway,
4691 N_( "attempt three-way merge if a patch does not apply")),
4692 OPT_FILENAME(0, "build-fake-ancestor", &state.fake_ancestor,
4693 N_("build a temporary index based on embedded index information")),
4694 /* Think twice before adding "--nul" synonym to this */
4695 OPT_SET_INT('z', NULL, &state.line_termination,
4696 N_("paths are separated with NUL character"), '\0'),
4697 OPT_INTEGER('C', NULL, &state.p_context,
4698 N_("ensure at least <n> lines of context match")),
4699 { OPTION_CALLBACK, 0, "whitespace", &state, N_("action"),
4700 N_("detect new or modified lines that have whitespace errors"),
4701 0, option_parse_whitespace },
4702 { OPTION_CALLBACK, 0, "ignore-space-change", &state, NULL,
4703 N_("ignore changes in whitespace when finding context"),
4704 PARSE_OPT_NOARG, option_parse_space_change },
4705 { OPTION_CALLBACK, 0, "ignore-whitespace", &state, NULL,
4706 N_("ignore changes in whitespace when finding context"),
4707 PARSE_OPT_NOARG, option_parse_space_change },
4708 OPT_BOOL('R', "reverse", &state.apply_in_reverse,
4709 N_("apply the patch in reverse")),
4710 OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
4711 N_("don't expect at least one line of context")),
4712 OPT_BOOL(0, "reject", &state.apply_with_reject,
4713 N_("leave the rejected hunks in corresponding *.rej files")),
4714 OPT_BOOL(0, "allow-overlap", &state.allow_overlap,
4715 N_("allow overlapping hunks")),
4716 OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
4717 OPT_BIT(0, "inaccurate-eof", &options,
4718 N_("tolerate incorrectly detected missing new-line at the end of file"),
4719 INACCURATE_EOF),
4720 OPT_BIT(0, "recount", &options,
4721 N_("do not trust the line counts in the hunk headers"),
4722 RECOUNT),
4723 { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
4724 N_("prepend <root> to all filenames"),
4725 0, option_parse_directory },
4726 OPT_END()
4729 if (init_apply_state(&state, prefix, &lock_file))
4730 exit(128);
4732 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4733 apply_usage, 0);
4735 if (check_apply_state(&state, force_apply))
4736 exit(128);
4738 ret = apply_all_patches(&state, argc, argv, options);
4740 clear_apply_state(&state);
4742 return ret;