Sync with 2.33.8
[git/debian.git] / apply.c
blob81ac28fd7430dfd9bc511193e7973c8453d86439
1 /*
2 * apply.c
4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
8 */
10 #include "cache.h"
11 #include "config.h"
12 #include "object-store.h"
13 #include "blob.h"
14 #include "delta.h"
15 #include "diff.h"
16 #include "dir.h"
17 #include "xdiff-interface.h"
18 #include "ll-merge.h"
19 #include "lockfile.h"
20 #include "parse-options.h"
21 #include "quote.h"
22 #include "rerere.h"
23 #include "apply.h"
24 #include "entry.h"
26 struct gitdiff_data {
27 struct strbuf *root;
28 int linenr;
29 int p_value;
32 static void git_apply_config(void)
34 git_config_get_string("apply.whitespace", &apply_default_whitespace);
35 git_config_get_string("apply.ignorewhitespace", &apply_default_ignorewhitespace);
36 git_config(git_xmerge_config, NULL);
39 static int parse_whitespace_option(struct apply_state *state, const char *option)
41 if (!option) {
42 state->ws_error_action = warn_on_ws_error;
43 return 0;
45 if (!strcmp(option, "warn")) {
46 state->ws_error_action = warn_on_ws_error;
47 return 0;
49 if (!strcmp(option, "nowarn")) {
50 state->ws_error_action = nowarn_ws_error;
51 return 0;
53 if (!strcmp(option, "error")) {
54 state->ws_error_action = die_on_ws_error;
55 return 0;
57 if (!strcmp(option, "error-all")) {
58 state->ws_error_action = die_on_ws_error;
59 state->squelch_whitespace_errors = 0;
60 return 0;
62 if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
63 state->ws_error_action = correct_ws_error;
64 return 0;
67 * Please update $__git_whitespacelist in git-completion.bash
68 * when you add new options.
70 return error(_("unrecognized whitespace option '%s'"), option);
73 static int parse_ignorewhitespace_option(struct apply_state *state,
74 const char *option)
76 if (!option || !strcmp(option, "no") ||
77 !strcmp(option, "false") || !strcmp(option, "never") ||
78 !strcmp(option, "none")) {
79 state->ws_ignore_action = ignore_ws_none;
80 return 0;
82 if (!strcmp(option, "change")) {
83 state->ws_ignore_action = ignore_ws_change;
84 return 0;
86 return error(_("unrecognized whitespace ignore option '%s'"), option);
89 int init_apply_state(struct apply_state *state,
90 struct repository *repo,
91 const char *prefix)
93 memset(state, 0, sizeof(*state));
94 state->prefix = prefix;
95 state->repo = repo;
96 state->apply = 1;
97 state->line_termination = '\n';
98 state->p_value = 1;
99 state->p_context = UINT_MAX;
100 state->squelch_whitespace_errors = 5;
101 state->ws_error_action = warn_on_ws_error;
102 state->ws_ignore_action = ignore_ws_none;
103 state->linenr = 1;
104 string_list_init_nodup(&state->fn_table);
105 string_list_init_nodup(&state->limit_by_name);
106 string_list_init_nodup(&state->symlink_changes);
107 strbuf_init(&state->root, 0);
109 git_apply_config();
110 if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
111 return -1;
112 if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
113 return -1;
114 return 0;
117 void clear_apply_state(struct apply_state *state)
119 string_list_clear(&state->limit_by_name, 0);
120 string_list_clear(&state->symlink_changes, 0);
121 strbuf_release(&state->root);
123 /* &state->fn_table is cleared at the end of apply_patch() */
126 static void mute_routine(const char *msg, va_list params)
128 /* do nothing */
131 int check_apply_state(struct apply_state *state, int force_apply)
133 int is_not_gitdir = !startup_info->have_repository;
135 if (state->apply_with_reject && state->threeway)
136 return error(_("--reject and --3way cannot be used together."));
137 if (state->threeway) {
138 if (is_not_gitdir)
139 return error(_("--3way outside a repository"));
140 state->check_index = 1;
142 if (state->apply_with_reject) {
143 state->apply = 1;
144 if (state->apply_verbosity == verbosity_normal)
145 state->apply_verbosity = verbosity_verbose;
147 if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
148 state->apply = 0;
149 if (state->check_index && is_not_gitdir)
150 return error(_("--index outside a repository"));
151 if (state->cached) {
152 if (is_not_gitdir)
153 return error(_("--cached outside a repository"));
154 state->check_index = 1;
156 if (state->ita_only && (state->check_index || is_not_gitdir))
157 state->ita_only = 0;
158 if (state->check_index)
159 state->unsafe_paths = 0;
161 if (state->apply_verbosity <= verbosity_silent) {
162 state->saved_error_routine = get_error_routine();
163 state->saved_warn_routine = get_warn_routine();
164 set_error_routine(mute_routine);
165 set_warn_routine(mute_routine);
168 return 0;
171 static void set_default_whitespace_mode(struct apply_state *state)
173 if (!state->whitespace_option && !apply_default_whitespace)
174 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
178 * This represents one "hunk" from a patch, starting with
179 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
180 * patch text is pointed at by patch, and its byte length
181 * is stored in size. leading and trailing are the number
182 * of context lines.
184 struct fragment {
185 unsigned long leading, trailing;
186 unsigned long oldpos, oldlines;
187 unsigned long newpos, newlines;
189 * 'patch' is usually borrowed from buf in apply_patch(),
190 * but some codepaths store an allocated buffer.
192 const char *patch;
193 unsigned free_patch:1,
194 rejected:1;
195 int size;
196 int linenr;
197 struct fragment *next;
201 * When dealing with a binary patch, we reuse "leading" field
202 * to store the type of the binary hunk, either deflated "delta"
203 * or deflated "literal".
205 #define binary_patch_method leading
206 #define BINARY_DELTA_DEFLATED 1
207 #define BINARY_LITERAL_DEFLATED 2
209 static void free_fragment_list(struct fragment *list)
211 while (list) {
212 struct fragment *next = list->next;
213 if (list->free_patch)
214 free((char *)list->patch);
215 free(list);
216 list = next;
220 static void free_patch(struct patch *patch)
222 free_fragment_list(patch->fragments);
223 free(patch->def_name);
224 free(patch->old_name);
225 free(patch->new_name);
226 free(patch->result);
227 free(patch);
230 static void free_patch_list(struct patch *list)
232 while (list) {
233 struct patch *next = list->next;
234 free_patch(list);
235 list = next;
240 * A line in a file, len-bytes long (includes the terminating LF,
241 * except for an incomplete line at the end if the file ends with
242 * one), and its contents hashes to 'hash'.
244 struct line {
245 size_t len;
246 unsigned hash : 24;
247 unsigned flag : 8;
248 #define LINE_COMMON 1
249 #define LINE_PATCHED 2
253 * This represents a "file", which is an array of "lines".
255 struct image {
256 char *buf;
257 size_t len;
258 size_t nr;
259 size_t alloc;
260 struct line *line_allocated;
261 struct line *line;
264 static uint32_t hash_line(const char *cp, size_t len)
266 size_t i;
267 uint32_t h;
268 for (i = 0, h = 0; i < len; i++) {
269 if (!isspace(cp[i])) {
270 h = h * 3 + (cp[i] & 0xff);
273 return h;
277 * Compare lines s1 of length n1 and s2 of length n2, ignoring
278 * whitespace difference. Returns 1 if they match, 0 otherwise
280 static int fuzzy_matchlines(const char *s1, size_t n1,
281 const char *s2, size_t n2)
283 const char *end1 = s1 + n1;
284 const char *end2 = s2 + n2;
286 /* ignore line endings */
287 while (s1 < end1 && (end1[-1] == '\r' || end1[-1] == '\n'))
288 end1--;
289 while (s2 < end2 && (end2[-1] == '\r' || end2[-1] == '\n'))
290 end2--;
292 while (s1 < end1 && s2 < end2) {
293 if (isspace(*s1)) {
295 * Skip whitespace. We check on both buffers
296 * because we don't want "a b" to match "ab".
298 if (!isspace(*s2))
299 return 0;
300 while (s1 < end1 && isspace(*s1))
301 s1++;
302 while (s2 < end2 && isspace(*s2))
303 s2++;
304 } else if (*s1++ != *s2++)
305 return 0;
308 /* If we reached the end on one side only, lines don't match. */
309 return s1 == end1 && s2 == end2;
312 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
314 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
315 img->line_allocated[img->nr].len = len;
316 img->line_allocated[img->nr].hash = hash_line(bol, len);
317 img->line_allocated[img->nr].flag = flag;
318 img->nr++;
322 * "buf" has the file contents to be patched (read from various sources).
323 * attach it to "image" and add line-based index to it.
324 * "image" now owns the "buf".
326 static void prepare_image(struct image *image, char *buf, size_t len,
327 int prepare_linetable)
329 const char *cp, *ep;
331 memset(image, 0, sizeof(*image));
332 image->buf = buf;
333 image->len = len;
335 if (!prepare_linetable)
336 return;
338 ep = image->buf + image->len;
339 cp = image->buf;
340 while (cp < ep) {
341 const char *next;
342 for (next = cp; next < ep && *next != '\n'; next++)
344 if (next < ep)
345 next++;
346 add_line_info(image, cp, next - cp, 0);
347 cp = next;
349 image->line = image->line_allocated;
352 static void clear_image(struct image *image)
354 free(image->buf);
355 free(image->line_allocated);
356 memset(image, 0, sizeof(*image));
359 /* fmt must contain _one_ %s and no other substitution */
360 static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
362 struct strbuf sb = STRBUF_INIT;
364 if (patch->old_name && patch->new_name &&
365 strcmp(patch->old_name, patch->new_name)) {
366 quote_c_style(patch->old_name, &sb, NULL, 0);
367 strbuf_addstr(&sb, " => ");
368 quote_c_style(patch->new_name, &sb, NULL, 0);
369 } else {
370 const char *n = patch->new_name;
371 if (!n)
372 n = patch->old_name;
373 quote_c_style(n, &sb, NULL, 0);
375 fprintf(output, fmt, sb.buf);
376 fputc('\n', output);
377 strbuf_release(&sb);
380 #define SLOP (16)
382 static int read_patch_file(struct strbuf *sb, int fd)
384 if (strbuf_read(sb, fd, 0) < 0)
385 return error_errno("git apply: failed to read");
388 * Make sure that we have some slop in the buffer
389 * so that we can do speculative "memcmp" etc, and
390 * see to it that it is NUL-filled.
392 strbuf_grow(sb, SLOP);
393 memset(sb->buf + sb->len, 0, SLOP);
394 return 0;
397 static unsigned long linelen(const char *buffer, unsigned long size)
399 unsigned long len = 0;
400 while (size--) {
401 len++;
402 if (*buffer++ == '\n')
403 break;
405 return len;
408 static int is_dev_null(const char *str)
410 return skip_prefix(str, "/dev/null", &str) && isspace(*str);
413 #define TERM_SPACE 1
414 #define TERM_TAB 2
416 static int name_terminate(int c, int terminate)
418 if (c == ' ' && !(terminate & TERM_SPACE))
419 return 0;
420 if (c == '\t' && !(terminate & TERM_TAB))
421 return 0;
423 return 1;
426 /* remove double slashes to make --index work with such filenames */
427 static char *squash_slash(char *name)
429 int i = 0, j = 0;
431 if (!name)
432 return NULL;
434 while (name[i]) {
435 if ((name[j++] = name[i++]) == '/')
436 while (name[i] == '/')
437 i++;
439 name[j] = '\0';
440 return name;
443 static char *find_name_gnu(struct strbuf *root,
444 const char *line,
445 int p_value)
447 struct strbuf name = STRBUF_INIT;
448 char *cp;
451 * Proposed "new-style" GNU patch/diff format; see
452 * https://lore.kernel.org/git/7vll0wvb2a.fsf@assigned-by-dhcp.cox.net/
454 if (unquote_c_style(&name, line, NULL)) {
455 strbuf_release(&name);
456 return NULL;
459 for (cp = name.buf; p_value; p_value--) {
460 cp = strchr(cp, '/');
461 if (!cp) {
462 strbuf_release(&name);
463 return NULL;
465 cp++;
468 strbuf_remove(&name, 0, cp - name.buf);
469 if (root->len)
470 strbuf_insert(&name, 0, root->buf, root->len);
471 return squash_slash(strbuf_detach(&name, NULL));
474 static size_t sane_tz_len(const char *line, size_t len)
476 const char *tz, *p;
478 if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
479 return 0;
480 tz = line + len - strlen(" +0500");
482 if (tz[1] != '+' && tz[1] != '-')
483 return 0;
485 for (p = tz + 2; p != line + len; p++)
486 if (!isdigit(*p))
487 return 0;
489 return line + len - tz;
492 static size_t tz_with_colon_len(const char *line, size_t len)
494 const char *tz, *p;
496 if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
497 return 0;
498 tz = line + len - strlen(" +08:00");
500 if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
501 return 0;
502 p = tz + 2;
503 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
504 !isdigit(*p++) || !isdigit(*p++))
505 return 0;
507 return line + len - tz;
510 static size_t date_len(const char *line, size_t len)
512 const char *date, *p;
514 if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
515 return 0;
516 p = date = line + len - strlen("72-02-05");
518 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
519 !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
520 !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */
521 return 0;
523 if (date - line >= strlen("19") &&
524 isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */
525 date -= strlen("19");
527 return line + len - date;
530 static size_t short_time_len(const char *line, size_t len)
532 const char *time, *p;
534 if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
535 return 0;
536 p = time = line + len - strlen(" 07:01:32");
538 /* Permit 1-digit hours? */
539 if (*p++ != ' ' ||
540 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
541 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
542 !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
543 return 0;
545 return line + len - time;
548 static size_t fractional_time_len(const char *line, size_t len)
550 const char *p;
551 size_t n;
553 /* Expected format: 19:41:17.620000023 */
554 if (!len || !isdigit(line[len - 1]))
555 return 0;
556 p = line + len - 1;
558 /* Fractional seconds. */
559 while (p > line && isdigit(*p))
560 p--;
561 if (*p != '.')
562 return 0;
564 /* Hours, minutes, and whole seconds. */
565 n = short_time_len(line, p - line);
566 if (!n)
567 return 0;
569 return line + len - p + n;
572 static size_t trailing_spaces_len(const char *line, size_t len)
574 const char *p;
576 /* Expected format: ' ' x (1 or more) */
577 if (!len || line[len - 1] != ' ')
578 return 0;
580 p = line + len;
581 while (p != line) {
582 p--;
583 if (*p != ' ')
584 return line + len - (p + 1);
587 /* All spaces! */
588 return len;
591 static size_t diff_timestamp_len(const char *line, size_t len)
593 const char *end = line + len;
594 size_t n;
597 * Posix: 2010-07-05 19:41:17
598 * GNU: 2010-07-05 19:41:17.620000023 -0500
601 if (!isdigit(end[-1]))
602 return 0;
604 n = sane_tz_len(line, end - line);
605 if (!n)
606 n = tz_with_colon_len(line, end - line);
607 end -= n;
609 n = short_time_len(line, end - line);
610 if (!n)
611 n = fractional_time_len(line, end - line);
612 end -= n;
614 n = date_len(line, end - line);
615 if (!n) /* No date. Too bad. */
616 return 0;
617 end -= n;
619 if (end == line) /* No space before date. */
620 return 0;
621 if (end[-1] == '\t') { /* Success! */
622 end--;
623 return line + len - end;
625 if (end[-1] != ' ') /* No space before date. */
626 return 0;
628 /* Whitespace damage. */
629 end -= trailing_spaces_len(line, end - line);
630 return line + len - end;
633 static char *find_name_common(struct strbuf *root,
634 const char *line,
635 const char *def,
636 int p_value,
637 const char *end,
638 int terminate)
640 int len;
641 const char *start = NULL;
643 if (p_value == 0)
644 start = line;
645 while (line != end) {
646 char c = *line;
648 if (!end && isspace(c)) {
649 if (c == '\n')
650 break;
651 if (name_terminate(c, terminate))
652 break;
654 line++;
655 if (c == '/' && !--p_value)
656 start = line;
658 if (!start)
659 return squash_slash(xstrdup_or_null(def));
660 len = line - start;
661 if (!len)
662 return squash_slash(xstrdup_or_null(def));
665 * Generally we prefer the shorter name, especially
666 * if the other one is just a variation of that with
667 * something else tacked on to the end (ie "file.orig"
668 * or "file~").
670 if (def) {
671 int deflen = strlen(def);
672 if (deflen < len && !strncmp(start, def, deflen))
673 return squash_slash(xstrdup(def));
676 if (root->len) {
677 char *ret = xstrfmt("%s%.*s", root->buf, len, start);
678 return squash_slash(ret);
681 return squash_slash(xmemdupz(start, len));
684 static char *find_name(struct strbuf *root,
685 const char *line,
686 char *def,
687 int p_value,
688 int terminate)
690 if (*line == '"') {
691 char *name = find_name_gnu(root, line, p_value);
692 if (name)
693 return name;
696 return find_name_common(root, line, def, p_value, NULL, terminate);
699 static char *find_name_traditional(struct strbuf *root,
700 const char *line,
701 char *def,
702 int p_value)
704 size_t len;
705 size_t date_len;
707 if (*line == '"') {
708 char *name = find_name_gnu(root, line, p_value);
709 if (name)
710 return name;
713 len = strchrnul(line, '\n') - line;
714 date_len = diff_timestamp_len(line, len);
715 if (!date_len)
716 return find_name_common(root, line, def, p_value, NULL, TERM_TAB);
717 len -= date_len;
719 return find_name_common(root, line, def, p_value, line + len, 0);
723 * Given the string after "--- " or "+++ ", guess the appropriate
724 * p_value for the given patch.
726 static int guess_p_value(struct apply_state *state, const char *nameline)
728 char *name, *cp;
729 int val = -1;
731 if (is_dev_null(nameline))
732 return -1;
733 name = find_name_traditional(&state->root, nameline, NULL, 0);
734 if (!name)
735 return -1;
736 cp = strchr(name, '/');
737 if (!cp)
738 val = 0;
739 else if (state->prefix) {
741 * Does it begin with "a/$our-prefix" and such? Then this is
742 * very likely to apply to our directory.
744 if (starts_with(name, state->prefix))
745 val = count_slashes(state->prefix);
746 else {
747 cp++;
748 if (starts_with(cp, state->prefix))
749 val = count_slashes(state->prefix) + 1;
752 free(name);
753 return val;
757 * Does the ---/+++ line have the POSIX timestamp after the last HT?
758 * GNU diff puts epoch there to signal a creation/deletion event. Is
759 * this such a timestamp?
761 static int has_epoch_timestamp(const char *nameline)
764 * We are only interested in epoch timestamp; any non-zero
765 * fraction cannot be one, hence "(\.0+)?" in the regexp below.
766 * For the same reason, the date must be either 1969-12-31 or
767 * 1970-01-01, and the seconds part must be "00".
769 const char stamp_regexp[] =
770 "^[0-2][0-9]:([0-5][0-9]):00(\\.0+)?"
772 "([-+][0-2][0-9]:?[0-5][0-9])\n";
773 const char *timestamp = NULL, *cp, *colon;
774 static regex_t *stamp;
775 regmatch_t m[10];
776 int zoneoffset, epoch_hour, hour, minute;
777 int status;
779 for (cp = nameline; *cp != '\n'; cp++) {
780 if (*cp == '\t')
781 timestamp = cp + 1;
783 if (!timestamp)
784 return 0;
787 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
788 * (west of GMT) or 1970-01-01 (east of GMT)
790 if (skip_prefix(timestamp, "1969-12-31 ", &timestamp))
791 epoch_hour = 24;
792 else if (skip_prefix(timestamp, "1970-01-01 ", &timestamp))
793 epoch_hour = 0;
794 else
795 return 0;
797 if (!stamp) {
798 stamp = xmalloc(sizeof(*stamp));
799 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
800 warning(_("Cannot prepare timestamp regexp %s"),
801 stamp_regexp);
802 return 0;
806 status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
807 if (status) {
808 if (status != REG_NOMATCH)
809 warning(_("regexec returned %d for input: %s"),
810 status, timestamp);
811 return 0;
814 hour = strtol(timestamp, NULL, 10);
815 minute = strtol(timestamp + m[1].rm_so, NULL, 10);
817 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
818 if (*colon == ':')
819 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
820 else
821 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
822 if (timestamp[m[3].rm_so] == '-')
823 zoneoffset = -zoneoffset;
825 return hour * 60 + minute - zoneoffset == epoch_hour * 60;
829 * Get the name etc info from the ---/+++ lines of a traditional patch header
831 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
832 * files, we can happily check the index for a match, but for creating a
833 * new file we should try to match whatever "patch" does. I have no idea.
835 static int parse_traditional_patch(struct apply_state *state,
836 const char *first,
837 const char *second,
838 struct patch *patch)
840 char *name;
842 first += 4; /* skip "--- " */
843 second += 4; /* skip "+++ " */
844 if (!state->p_value_known) {
845 int p, q;
846 p = guess_p_value(state, first);
847 q = guess_p_value(state, second);
848 if (p < 0) p = q;
849 if (0 <= p && p == q) {
850 state->p_value = p;
851 state->p_value_known = 1;
854 if (is_dev_null(first)) {
855 patch->is_new = 1;
856 patch->is_delete = 0;
857 name = find_name_traditional(&state->root, second, NULL, state->p_value);
858 patch->new_name = name;
859 } else if (is_dev_null(second)) {
860 patch->is_new = 0;
861 patch->is_delete = 1;
862 name = find_name_traditional(&state->root, first, NULL, state->p_value);
863 patch->old_name = name;
864 } else {
865 char *first_name;
866 first_name = find_name_traditional(&state->root, first, NULL, state->p_value);
867 name = find_name_traditional(&state->root, second, first_name, state->p_value);
868 free(first_name);
869 if (has_epoch_timestamp(first)) {
870 patch->is_new = 1;
871 patch->is_delete = 0;
872 patch->new_name = name;
873 } else if (has_epoch_timestamp(second)) {
874 patch->is_new = 0;
875 patch->is_delete = 1;
876 patch->old_name = name;
877 } else {
878 patch->old_name = name;
879 patch->new_name = xstrdup_or_null(name);
882 if (!name)
883 return error(_("unable to find filename in patch at line %d"), state->linenr);
885 return 0;
888 static int gitdiff_hdrend(struct gitdiff_data *state,
889 const char *line,
890 struct patch *patch)
892 return 1;
896 * We're anal about diff header consistency, to make
897 * sure that we don't end up having strange ambiguous
898 * patches floating around.
900 * As a result, gitdiff_{old|new}name() will check
901 * their names against any previous information, just
902 * to make sure..
904 #define DIFF_OLD_NAME 0
905 #define DIFF_NEW_NAME 1
907 static int gitdiff_verify_name(struct gitdiff_data *state,
908 const char *line,
909 int isnull,
910 char **name,
911 int side)
913 if (!*name && !isnull) {
914 *name = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
915 return 0;
918 if (*name) {
919 char *another;
920 if (isnull)
921 return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
922 *name, state->linenr);
923 another = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
924 if (!another || strcmp(another, *name)) {
925 free(another);
926 return error((side == DIFF_NEW_NAME) ?
927 _("git apply: bad git-diff - inconsistent new filename on line %d") :
928 _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
930 free(another);
931 } else {
932 if (!is_dev_null(line))
933 return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
936 return 0;
939 static int gitdiff_oldname(struct gitdiff_data *state,
940 const char *line,
941 struct patch *patch)
943 return gitdiff_verify_name(state, line,
944 patch->is_new, &patch->old_name,
945 DIFF_OLD_NAME);
948 static int gitdiff_newname(struct gitdiff_data *state,
949 const char *line,
950 struct patch *patch)
952 return gitdiff_verify_name(state, line,
953 patch->is_delete, &patch->new_name,
954 DIFF_NEW_NAME);
957 static int parse_mode_line(const char *line, int linenr, unsigned int *mode)
959 char *end;
960 *mode = strtoul(line, &end, 8);
961 if (end == line || !isspace(*end))
962 return error(_("invalid mode on line %d: %s"), linenr, line);
963 return 0;
966 static int gitdiff_oldmode(struct gitdiff_data *state,
967 const char *line,
968 struct patch *patch)
970 return parse_mode_line(line, state->linenr, &patch->old_mode);
973 static int gitdiff_newmode(struct gitdiff_data *state,
974 const char *line,
975 struct patch *patch)
977 return parse_mode_line(line, state->linenr, &patch->new_mode);
980 static int gitdiff_delete(struct gitdiff_data *state,
981 const char *line,
982 struct patch *patch)
984 patch->is_delete = 1;
985 free(patch->old_name);
986 patch->old_name = xstrdup_or_null(patch->def_name);
987 return gitdiff_oldmode(state, line, patch);
990 static int gitdiff_newfile(struct gitdiff_data *state,
991 const char *line,
992 struct patch *patch)
994 patch->is_new = 1;
995 free(patch->new_name);
996 patch->new_name = xstrdup_or_null(patch->def_name);
997 return gitdiff_newmode(state, line, patch);
1000 static int gitdiff_copysrc(struct gitdiff_data *state,
1001 const char *line,
1002 struct patch *patch)
1004 patch->is_copy = 1;
1005 free(patch->old_name);
1006 patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1007 return 0;
1010 static int gitdiff_copydst(struct gitdiff_data *state,
1011 const char *line,
1012 struct patch *patch)
1014 patch->is_copy = 1;
1015 free(patch->new_name);
1016 patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1017 return 0;
1020 static int gitdiff_renamesrc(struct gitdiff_data *state,
1021 const char *line,
1022 struct patch *patch)
1024 patch->is_rename = 1;
1025 free(patch->old_name);
1026 patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1027 return 0;
1030 static int gitdiff_renamedst(struct gitdiff_data *state,
1031 const char *line,
1032 struct patch *patch)
1034 patch->is_rename = 1;
1035 free(patch->new_name);
1036 patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1037 return 0;
1040 static int gitdiff_similarity(struct gitdiff_data *state,
1041 const char *line,
1042 struct patch *patch)
1044 unsigned long val = strtoul(line, NULL, 10);
1045 if (val <= 100)
1046 patch->score = val;
1047 return 0;
1050 static int gitdiff_dissimilarity(struct gitdiff_data *state,
1051 const char *line,
1052 struct patch *patch)
1054 unsigned long val = strtoul(line, NULL, 10);
1055 if (val <= 100)
1056 patch->score = val;
1057 return 0;
1060 static int gitdiff_index(struct gitdiff_data *state,
1061 const char *line,
1062 struct patch *patch)
1065 * index line is N hexadecimal, "..", N hexadecimal,
1066 * and optional space with octal mode.
1068 const char *ptr, *eol;
1069 int len;
1070 const unsigned hexsz = the_hash_algo->hexsz;
1072 ptr = strchr(line, '.');
1073 if (!ptr || ptr[1] != '.' || hexsz < ptr - line)
1074 return 0;
1075 len = ptr - line;
1076 memcpy(patch->old_oid_prefix, line, len);
1077 patch->old_oid_prefix[len] = 0;
1079 line = ptr + 2;
1080 ptr = strchr(line, ' ');
1081 eol = strchrnul(line, '\n');
1083 if (!ptr || eol < ptr)
1084 ptr = eol;
1085 len = ptr - line;
1087 if (hexsz < len)
1088 return 0;
1089 memcpy(patch->new_oid_prefix, line, len);
1090 patch->new_oid_prefix[len] = 0;
1091 if (*ptr == ' ')
1092 return gitdiff_oldmode(state, ptr + 1, patch);
1093 return 0;
1097 * This is normal for a diff that doesn't change anything: we'll fall through
1098 * into the next diff. Tell the parser to break out.
1100 static int gitdiff_unrecognized(struct gitdiff_data *state,
1101 const char *line,
1102 struct patch *patch)
1104 return 1;
1108 * Skip p_value leading components from "line"; as we do not accept
1109 * absolute paths, return NULL in that case.
1111 static const char *skip_tree_prefix(int p_value,
1112 const char *line,
1113 int llen)
1115 int nslash;
1116 int i;
1118 if (!p_value)
1119 return (llen && line[0] == '/') ? NULL : line;
1121 nslash = p_value;
1122 for (i = 0; i < llen; i++) {
1123 int ch = line[i];
1124 if (ch == '/' && --nslash <= 0)
1125 return (i == 0) ? NULL : &line[i + 1];
1127 return NULL;
1131 * This is to extract the same name that appears on "diff --git"
1132 * line. We do not find and return anything if it is a rename
1133 * patch, and it is OK because we will find the name elsewhere.
1134 * We need to reliably find name only when it is mode-change only,
1135 * creation or deletion of an empty file. In any of these cases,
1136 * both sides are the same name under a/ and b/ respectively.
1138 static char *git_header_name(int p_value,
1139 const char *line,
1140 int llen)
1142 const char *name;
1143 const char *second = NULL;
1144 size_t len, line_len;
1146 line += strlen("diff --git ");
1147 llen -= strlen("diff --git ");
1149 if (*line == '"') {
1150 const char *cp;
1151 struct strbuf first = STRBUF_INIT;
1152 struct strbuf sp = STRBUF_INIT;
1154 if (unquote_c_style(&first, line, &second))
1155 goto free_and_fail1;
1157 /* strip the a/b prefix including trailing slash */
1158 cp = skip_tree_prefix(p_value, first.buf, first.len);
1159 if (!cp)
1160 goto free_and_fail1;
1161 strbuf_remove(&first, 0, cp - first.buf);
1164 * second points at one past closing dq of name.
1165 * find the second name.
1167 while ((second < line + llen) && isspace(*second))
1168 second++;
1170 if (line + llen <= second)
1171 goto free_and_fail1;
1172 if (*second == '"') {
1173 if (unquote_c_style(&sp, second, NULL))
1174 goto free_and_fail1;
1175 cp = skip_tree_prefix(p_value, sp.buf, sp.len);
1176 if (!cp)
1177 goto free_and_fail1;
1178 /* They must match, otherwise ignore */
1179 if (strcmp(cp, first.buf))
1180 goto free_and_fail1;
1181 strbuf_release(&sp);
1182 return strbuf_detach(&first, NULL);
1185 /* unquoted second */
1186 cp = skip_tree_prefix(p_value, second, line + llen - second);
1187 if (!cp)
1188 goto free_and_fail1;
1189 if (line + llen - cp != first.len ||
1190 memcmp(first.buf, cp, first.len))
1191 goto free_and_fail1;
1192 return strbuf_detach(&first, NULL);
1194 free_and_fail1:
1195 strbuf_release(&first);
1196 strbuf_release(&sp);
1197 return NULL;
1200 /* unquoted first name */
1201 name = skip_tree_prefix(p_value, line, llen);
1202 if (!name)
1203 return NULL;
1206 * since the first name is unquoted, a dq if exists must be
1207 * the beginning of the second name.
1209 for (second = name; second < line + llen; second++) {
1210 if (*second == '"') {
1211 struct strbuf sp = STRBUF_INIT;
1212 const char *np;
1214 if (unquote_c_style(&sp, second, NULL))
1215 goto free_and_fail2;
1217 np = skip_tree_prefix(p_value, sp.buf, sp.len);
1218 if (!np)
1219 goto free_and_fail2;
1221 len = sp.buf + sp.len - np;
1222 if (len < second - name &&
1223 !strncmp(np, name, len) &&
1224 isspace(name[len])) {
1225 /* Good */
1226 strbuf_remove(&sp, 0, np - sp.buf);
1227 return strbuf_detach(&sp, NULL);
1230 free_and_fail2:
1231 strbuf_release(&sp);
1232 return NULL;
1237 * Accept a name only if it shows up twice, exactly the same
1238 * form.
1240 second = strchr(name, '\n');
1241 if (!second)
1242 return NULL;
1243 line_len = second - name;
1244 for (len = 0 ; ; len++) {
1245 switch (name[len]) {
1246 default:
1247 continue;
1248 case '\n':
1249 return NULL;
1250 case '\t': case ' ':
1252 * Is this the separator between the preimage
1253 * and the postimage pathname? Again, we are
1254 * only interested in the case where there is
1255 * no rename, as this is only to set def_name
1256 * and a rename patch has the names elsewhere
1257 * in an unambiguous form.
1259 if (!name[len + 1])
1260 return NULL; /* no postimage name */
1261 second = skip_tree_prefix(p_value, name + len + 1,
1262 line_len - (len + 1));
1263 if (!second)
1264 return NULL;
1266 * Does len bytes starting at "name" and "second"
1267 * (that are separated by one HT or SP we just
1268 * found) exactly match?
1270 if (second[len] == '\n' && !strncmp(name, second, len))
1271 return xmemdupz(name, len);
1276 static int check_header_line(int linenr, struct patch *patch)
1278 int extensions = (patch->is_delete == 1) + (patch->is_new == 1) +
1279 (patch->is_rename == 1) + (patch->is_copy == 1);
1280 if (extensions > 1)
1281 return error(_("inconsistent header lines %d and %d"),
1282 patch->extension_linenr, linenr);
1283 if (extensions && !patch->extension_linenr)
1284 patch->extension_linenr = linenr;
1285 return 0;
1288 int parse_git_diff_header(struct strbuf *root,
1289 int *linenr,
1290 int p_value,
1291 const char *line,
1292 int len,
1293 unsigned int size,
1294 struct patch *patch)
1296 unsigned long offset;
1297 struct gitdiff_data parse_hdr_state;
1299 /* A git diff has explicit new/delete information, so we don't guess */
1300 patch->is_new = 0;
1301 patch->is_delete = 0;
1304 * Some things may not have the old name in the
1305 * rest of the headers anywhere (pure mode changes,
1306 * or removing or adding empty files), so we get
1307 * the default name from the header.
1309 patch->def_name = git_header_name(p_value, line, len);
1310 if (patch->def_name && root->len) {
1311 char *s = xstrfmt("%s%s", root->buf, patch->def_name);
1312 free(patch->def_name);
1313 patch->def_name = s;
1316 line += len;
1317 size -= len;
1318 (*linenr)++;
1319 parse_hdr_state.root = root;
1320 parse_hdr_state.linenr = *linenr;
1321 parse_hdr_state.p_value = p_value;
1323 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
1324 static const struct opentry {
1325 const char *str;
1326 int (*fn)(struct gitdiff_data *, const char *, struct patch *);
1327 } optable[] = {
1328 { "@@ -", gitdiff_hdrend },
1329 { "--- ", gitdiff_oldname },
1330 { "+++ ", gitdiff_newname },
1331 { "old mode ", gitdiff_oldmode },
1332 { "new mode ", gitdiff_newmode },
1333 { "deleted file mode ", gitdiff_delete },
1334 { "new file mode ", gitdiff_newfile },
1335 { "copy from ", gitdiff_copysrc },
1336 { "copy to ", gitdiff_copydst },
1337 { "rename old ", gitdiff_renamesrc },
1338 { "rename new ", gitdiff_renamedst },
1339 { "rename from ", gitdiff_renamesrc },
1340 { "rename to ", gitdiff_renamedst },
1341 { "similarity index ", gitdiff_similarity },
1342 { "dissimilarity index ", gitdiff_dissimilarity },
1343 { "index ", gitdiff_index },
1344 { "", gitdiff_unrecognized },
1346 int i;
1348 len = linelen(line, size);
1349 if (!len || line[len-1] != '\n')
1350 break;
1351 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1352 const struct opentry *p = optable + i;
1353 int oplen = strlen(p->str);
1354 int res;
1355 if (len < oplen || memcmp(p->str, line, oplen))
1356 continue;
1357 res = p->fn(&parse_hdr_state, line + oplen, patch);
1358 if (res < 0)
1359 return -1;
1360 if (check_header_line(*linenr, patch))
1361 return -1;
1362 if (res > 0)
1363 goto done;
1364 break;
1368 done:
1369 if (!patch->old_name && !patch->new_name) {
1370 if (!patch->def_name) {
1371 error(Q_("git diff header lacks filename information when removing "
1372 "%d leading pathname component (line %d)",
1373 "git diff header lacks filename information when removing "
1374 "%d leading pathname components (line %d)",
1375 parse_hdr_state.p_value),
1376 parse_hdr_state.p_value, *linenr);
1377 return -128;
1379 patch->old_name = xstrdup(patch->def_name);
1380 patch->new_name = xstrdup(patch->def_name);
1382 if ((!patch->new_name && !patch->is_delete) ||
1383 (!patch->old_name && !patch->is_new)) {
1384 error(_("git diff header lacks filename information "
1385 "(line %d)"), *linenr);
1386 return -128;
1388 patch->is_toplevel_relative = 1;
1389 return offset;
1392 static int parse_num(const char *line, unsigned long *p)
1394 char *ptr;
1396 if (!isdigit(*line))
1397 return 0;
1398 *p = strtoul(line, &ptr, 10);
1399 return ptr - line;
1402 static int parse_range(const char *line, int len, int offset, const char *expect,
1403 unsigned long *p1, unsigned long *p2)
1405 int digits, ex;
1407 if (offset < 0 || offset >= len)
1408 return -1;
1409 line += offset;
1410 len -= offset;
1412 digits = parse_num(line, p1);
1413 if (!digits)
1414 return -1;
1416 offset += digits;
1417 line += digits;
1418 len -= digits;
1420 *p2 = 1;
1421 if (*line == ',') {
1422 digits = parse_num(line+1, p2);
1423 if (!digits)
1424 return -1;
1426 offset += digits+1;
1427 line += digits+1;
1428 len -= digits+1;
1431 ex = strlen(expect);
1432 if (ex > len)
1433 return -1;
1434 if (memcmp(line, expect, ex))
1435 return -1;
1437 return offset + ex;
1440 static void recount_diff(const char *line, int size, struct fragment *fragment)
1442 int oldlines = 0, newlines = 0, ret = 0;
1444 if (size < 1) {
1445 warning("recount: ignore empty hunk");
1446 return;
1449 for (;;) {
1450 int len = linelen(line, size);
1451 size -= len;
1452 line += len;
1454 if (size < 1)
1455 break;
1457 switch (*line) {
1458 case ' ': case '\n':
1459 newlines++;
1460 /* fall through */
1461 case '-':
1462 oldlines++;
1463 continue;
1464 case '+':
1465 newlines++;
1466 continue;
1467 case '\\':
1468 continue;
1469 case '@':
1470 ret = size < 3 || !starts_with(line, "@@ ");
1471 break;
1472 case 'd':
1473 ret = size < 5 || !starts_with(line, "diff ");
1474 break;
1475 default:
1476 ret = -1;
1477 break;
1479 if (ret) {
1480 warning(_("recount: unexpected line: %.*s"),
1481 (int)linelen(line, size), line);
1482 return;
1484 break;
1486 fragment->oldlines = oldlines;
1487 fragment->newlines = newlines;
1491 * Parse a unified diff fragment header of the
1492 * form "@@ -a,b +c,d @@"
1494 static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1496 int offset;
1498 if (!len || line[len-1] != '\n')
1499 return -1;
1501 /* Figure out the number of lines in a fragment */
1502 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1503 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1505 return offset;
1509 * Find file diff header
1511 * Returns:
1512 * -1 if no header was found
1513 * -128 in case of error
1514 * the size of the header in bytes (called "offset") otherwise
1516 static int find_header(struct apply_state *state,
1517 const char *line,
1518 unsigned long size,
1519 int *hdrsize,
1520 struct patch *patch)
1522 unsigned long offset, len;
1524 patch->is_toplevel_relative = 0;
1525 patch->is_rename = patch->is_copy = 0;
1526 patch->is_new = patch->is_delete = -1;
1527 patch->old_mode = patch->new_mode = 0;
1528 patch->old_name = patch->new_name = NULL;
1529 for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1530 unsigned long nextlen;
1532 len = linelen(line, size);
1533 if (!len)
1534 break;
1536 /* Testing this early allows us to take a few shortcuts.. */
1537 if (len < 6)
1538 continue;
1541 * Make sure we don't find any unconnected patch fragments.
1542 * That's a sign that we didn't find a header, and that a
1543 * patch has become corrupted/broken up.
1545 if (!memcmp("@@ -", line, 4)) {
1546 struct fragment dummy;
1547 if (parse_fragment_header(line, len, &dummy) < 0)
1548 continue;
1549 error(_("patch fragment without header at line %d: %.*s"),
1550 state->linenr, (int)len-1, line);
1551 return -128;
1554 if (size < len + 6)
1555 break;
1558 * Git patch? It might not have a real patch, just a rename
1559 * or mode change, so we handle that specially
1561 if (!memcmp("diff --git ", line, 11)) {
1562 int git_hdr_len = parse_git_diff_header(&state->root, &state->linenr,
1563 state->p_value, line, len,
1564 size, patch);
1565 if (git_hdr_len < 0)
1566 return -128;
1567 if (git_hdr_len <= len)
1568 continue;
1569 *hdrsize = git_hdr_len;
1570 return offset;
1573 /* --- followed by +++ ? */
1574 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1575 continue;
1578 * We only accept unified patches, so we want it to
1579 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1580 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1582 nextlen = linelen(line + len, size - len);
1583 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1584 continue;
1586 /* Ok, we'll consider it a patch */
1587 if (parse_traditional_patch(state, line, line+len, patch))
1588 return -128;
1589 *hdrsize = len + nextlen;
1590 state->linenr += 2;
1591 return offset;
1593 return -1;
1596 static void record_ws_error(struct apply_state *state,
1597 unsigned result,
1598 const char *line,
1599 int len,
1600 int linenr)
1602 char *err;
1604 if (!result)
1605 return;
1607 state->whitespace_error++;
1608 if (state->squelch_whitespace_errors &&
1609 state->squelch_whitespace_errors < state->whitespace_error)
1610 return;
1612 err = whitespace_error_string(result);
1613 if (state->apply_verbosity > verbosity_silent)
1614 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1615 state->patch_input_file, linenr, err, len, line);
1616 free(err);
1619 static void check_whitespace(struct apply_state *state,
1620 const char *line,
1621 int len,
1622 unsigned ws_rule)
1624 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1626 record_ws_error(state, result, line + 1, len - 2, state->linenr);
1630 * Check if the patch has context lines with CRLF or
1631 * the patch wants to remove lines with CRLF.
1633 static void check_old_for_crlf(struct patch *patch, const char *line, int len)
1635 if (len >= 2 && line[len-1] == '\n' && line[len-2] == '\r') {
1636 patch->ws_rule |= WS_CR_AT_EOL;
1637 patch->crlf_in_old = 1;
1643 * Parse a unified diff. Note that this really needs to parse each
1644 * fragment separately, since the only way to know the difference
1645 * between a "---" that is part of a patch, and a "---" that starts
1646 * the next patch is to look at the line counts..
1648 static int parse_fragment(struct apply_state *state,
1649 const char *line,
1650 unsigned long size,
1651 struct patch *patch,
1652 struct fragment *fragment)
1654 int added, deleted;
1655 int len = linelen(line, size), offset;
1656 unsigned long oldlines, newlines;
1657 unsigned long leading, trailing;
1659 offset = parse_fragment_header(line, len, fragment);
1660 if (offset < 0)
1661 return -1;
1662 if (offset > 0 && patch->recount)
1663 recount_diff(line + offset, size - offset, fragment);
1664 oldlines = fragment->oldlines;
1665 newlines = fragment->newlines;
1666 leading = 0;
1667 trailing = 0;
1669 /* Parse the thing.. */
1670 line += len;
1671 size -= len;
1672 state->linenr++;
1673 added = deleted = 0;
1674 for (offset = len;
1675 0 < size;
1676 offset += len, size -= len, line += len, state->linenr++) {
1677 if (!oldlines && !newlines)
1678 break;
1679 len = linelen(line, size);
1680 if (!len || line[len-1] != '\n')
1681 return -1;
1682 switch (*line) {
1683 default:
1684 return -1;
1685 case '\n': /* newer GNU diff, an empty context line */
1686 case ' ':
1687 oldlines--;
1688 newlines--;
1689 if (!deleted && !added)
1690 leading++;
1691 trailing++;
1692 check_old_for_crlf(patch, line, len);
1693 if (!state->apply_in_reverse &&
1694 state->ws_error_action == correct_ws_error)
1695 check_whitespace(state, line, len, patch->ws_rule);
1696 break;
1697 case '-':
1698 if (!state->apply_in_reverse)
1699 check_old_for_crlf(patch, line, len);
1700 if (state->apply_in_reverse &&
1701 state->ws_error_action != nowarn_ws_error)
1702 check_whitespace(state, line, len, patch->ws_rule);
1703 deleted++;
1704 oldlines--;
1705 trailing = 0;
1706 break;
1707 case '+':
1708 if (state->apply_in_reverse)
1709 check_old_for_crlf(patch, line, len);
1710 if (!state->apply_in_reverse &&
1711 state->ws_error_action != nowarn_ws_error)
1712 check_whitespace(state, line, len, patch->ws_rule);
1713 added++;
1714 newlines--;
1715 trailing = 0;
1716 break;
1719 * We allow "\ No newline at end of file". Depending
1720 * on locale settings when the patch was produced we
1721 * don't know what this line looks like. The only
1722 * thing we do know is that it begins with "\ ".
1723 * Checking for 12 is just for sanity check -- any
1724 * l10n of "\ No newline..." is at least that long.
1726 case '\\':
1727 if (len < 12 || memcmp(line, "\\ ", 2))
1728 return -1;
1729 break;
1732 if (oldlines || newlines)
1733 return -1;
1734 if (!patch->recount && !deleted && !added)
1735 return -1;
1737 fragment->leading = leading;
1738 fragment->trailing = trailing;
1741 * If a fragment ends with an incomplete line, we failed to include
1742 * it in the above loop because we hit oldlines == newlines == 0
1743 * before seeing it.
1745 if (12 < size && !memcmp(line, "\\ ", 2))
1746 offset += linelen(line, size);
1748 patch->lines_added += added;
1749 patch->lines_deleted += deleted;
1751 if (0 < patch->is_new && oldlines)
1752 return error(_("new file depends on old contents"));
1753 if (0 < patch->is_delete && newlines)
1754 return error(_("deleted file still has contents"));
1755 return offset;
1759 * We have seen "diff --git a/... b/..." header (or a traditional patch
1760 * header). Read hunks that belong to this patch into fragments and hang
1761 * them to the given patch structure.
1763 * The (fragment->patch, fragment->size) pair points into the memory given
1764 * by the caller, not a copy, when we return.
1766 * Returns:
1767 * -1 in case of error,
1768 * the number of bytes in the patch otherwise.
1770 static int parse_single_patch(struct apply_state *state,
1771 const char *line,
1772 unsigned long size,
1773 struct patch *patch)
1775 unsigned long offset = 0;
1776 unsigned long oldlines = 0, newlines = 0, context = 0;
1777 struct fragment **fragp = &patch->fragments;
1779 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1780 struct fragment *fragment;
1781 int len;
1783 CALLOC_ARRAY(fragment, 1);
1784 fragment->linenr = state->linenr;
1785 len = parse_fragment(state, line, size, patch, fragment);
1786 if (len <= 0) {
1787 free(fragment);
1788 return error(_("corrupt patch at line %d"), state->linenr);
1790 fragment->patch = line;
1791 fragment->size = len;
1792 oldlines += fragment->oldlines;
1793 newlines += fragment->newlines;
1794 context += fragment->leading + fragment->trailing;
1796 *fragp = fragment;
1797 fragp = &fragment->next;
1799 offset += len;
1800 line += len;
1801 size -= len;
1805 * If something was removed (i.e. we have old-lines) it cannot
1806 * be creation, and if something was added it cannot be
1807 * deletion. However, the reverse is not true; --unified=0
1808 * patches that only add are not necessarily creation even
1809 * though they do not have any old lines, and ones that only
1810 * delete are not necessarily deletion.
1812 * Unfortunately, a real creation/deletion patch do _not_ have
1813 * any context line by definition, so we cannot safely tell it
1814 * apart with --unified=0 insanity. At least if the patch has
1815 * more than one hunk it is not creation or deletion.
1817 if (patch->is_new < 0 &&
1818 (oldlines || (patch->fragments && patch->fragments->next)))
1819 patch->is_new = 0;
1820 if (patch->is_delete < 0 &&
1821 (newlines || (patch->fragments && patch->fragments->next)))
1822 patch->is_delete = 0;
1824 if (0 < patch->is_new && oldlines)
1825 return error(_("new file %s depends on old contents"), patch->new_name);
1826 if (0 < patch->is_delete && newlines)
1827 return error(_("deleted file %s still has contents"), patch->old_name);
1828 if (!patch->is_delete && !newlines && context && state->apply_verbosity > verbosity_silent)
1829 fprintf_ln(stderr,
1830 _("** warning: "
1831 "file %s becomes empty but is not deleted"),
1832 patch->new_name);
1834 return offset;
1837 static inline int metadata_changes(struct patch *patch)
1839 return patch->is_rename > 0 ||
1840 patch->is_copy > 0 ||
1841 patch->is_new > 0 ||
1842 patch->is_delete ||
1843 (patch->old_mode && patch->new_mode &&
1844 patch->old_mode != patch->new_mode);
1847 static char *inflate_it(const void *data, unsigned long size,
1848 unsigned long inflated_size)
1850 git_zstream stream;
1851 void *out;
1852 int st;
1854 memset(&stream, 0, sizeof(stream));
1856 stream.next_in = (unsigned char *)data;
1857 stream.avail_in = size;
1858 stream.next_out = out = xmalloc(inflated_size);
1859 stream.avail_out = inflated_size;
1860 git_inflate_init(&stream);
1861 st = git_inflate(&stream, Z_FINISH);
1862 git_inflate_end(&stream);
1863 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1864 free(out);
1865 return NULL;
1867 return out;
1871 * Read a binary hunk and return a new fragment; fragment->patch
1872 * points at an allocated memory that the caller must free, so
1873 * it is marked as "->free_patch = 1".
1875 static struct fragment *parse_binary_hunk(struct apply_state *state,
1876 char **buf_p,
1877 unsigned long *sz_p,
1878 int *status_p,
1879 int *used_p)
1882 * Expect a line that begins with binary patch method ("literal"
1883 * or "delta"), followed by the length of data before deflating.
1884 * a sequence of 'length-byte' followed by base-85 encoded data
1885 * should follow, terminated by a newline.
1887 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1888 * and we would limit the patch line to 66 characters,
1889 * so one line can fit up to 13 groups that would decode
1890 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1891 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1893 int llen, used;
1894 unsigned long size = *sz_p;
1895 char *buffer = *buf_p;
1896 int patch_method;
1897 unsigned long origlen;
1898 char *data = NULL;
1899 int hunk_size = 0;
1900 struct fragment *frag;
1902 llen = linelen(buffer, size);
1903 used = llen;
1905 *status_p = 0;
1907 if (starts_with(buffer, "delta ")) {
1908 patch_method = BINARY_DELTA_DEFLATED;
1909 origlen = strtoul(buffer + 6, NULL, 10);
1911 else if (starts_with(buffer, "literal ")) {
1912 patch_method = BINARY_LITERAL_DEFLATED;
1913 origlen = strtoul(buffer + 8, NULL, 10);
1915 else
1916 return NULL;
1918 state->linenr++;
1919 buffer += llen;
1920 size -= llen;
1921 while (1) {
1922 int byte_length, max_byte_length, newsize;
1923 llen = linelen(buffer, size);
1924 used += llen;
1925 state->linenr++;
1926 if (llen == 1) {
1927 /* consume the blank line */
1928 buffer++;
1929 size--;
1930 break;
1933 * Minimum line is "A00000\n" which is 7-byte long,
1934 * and the line length must be multiple of 5 plus 2.
1936 if ((llen < 7) || (llen-2) % 5)
1937 goto corrupt;
1938 max_byte_length = (llen - 2) / 5 * 4;
1939 byte_length = *buffer;
1940 if ('A' <= byte_length && byte_length <= 'Z')
1941 byte_length = byte_length - 'A' + 1;
1942 else if ('a' <= byte_length && byte_length <= 'z')
1943 byte_length = byte_length - 'a' + 27;
1944 else
1945 goto corrupt;
1946 /* if the input length was not multiple of 4, we would
1947 * have filler at the end but the filler should never
1948 * exceed 3 bytes
1950 if (max_byte_length < byte_length ||
1951 byte_length <= max_byte_length - 4)
1952 goto corrupt;
1953 newsize = hunk_size + byte_length;
1954 data = xrealloc(data, newsize);
1955 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1956 goto corrupt;
1957 hunk_size = newsize;
1958 buffer += llen;
1959 size -= llen;
1962 CALLOC_ARRAY(frag, 1);
1963 frag->patch = inflate_it(data, hunk_size, origlen);
1964 frag->free_patch = 1;
1965 if (!frag->patch)
1966 goto corrupt;
1967 free(data);
1968 frag->size = origlen;
1969 *buf_p = buffer;
1970 *sz_p = size;
1971 *used_p = used;
1972 frag->binary_patch_method = patch_method;
1973 return frag;
1975 corrupt:
1976 free(data);
1977 *status_p = -1;
1978 error(_("corrupt binary patch at line %d: %.*s"),
1979 state->linenr-1, llen-1, buffer);
1980 return NULL;
1984 * Returns:
1985 * -1 in case of error,
1986 * the length of the parsed binary patch otherwise
1988 static int parse_binary(struct apply_state *state,
1989 char *buffer,
1990 unsigned long size,
1991 struct patch *patch)
1994 * We have read "GIT binary patch\n"; what follows is a line
1995 * that says the patch method (currently, either "literal" or
1996 * "delta") and the length of data before deflating; a
1997 * sequence of 'length-byte' followed by base-85 encoded data
1998 * follows.
2000 * When a binary patch is reversible, there is another binary
2001 * hunk in the same format, starting with patch method (either
2002 * "literal" or "delta") with the length of data, and a sequence
2003 * of length-byte + base-85 encoded data, terminated with another
2004 * empty line. This data, when applied to the postimage, produces
2005 * the preimage.
2007 struct fragment *forward;
2008 struct fragment *reverse;
2009 int status;
2010 int used, used_1;
2012 forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
2013 if (!forward && !status)
2014 /* there has to be one hunk (forward hunk) */
2015 return error(_("unrecognized binary patch at line %d"), state->linenr-1);
2016 if (status)
2017 /* otherwise we already gave an error message */
2018 return status;
2020 reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
2021 if (reverse)
2022 used += used_1;
2023 else if (status) {
2025 * Not having reverse hunk is not an error, but having
2026 * a corrupt reverse hunk is.
2028 free((void*) forward->patch);
2029 free(forward);
2030 return status;
2032 forward->next = reverse;
2033 patch->fragments = forward;
2034 patch->is_binary = 1;
2035 return used;
2038 static void prefix_one(struct apply_state *state, char **name)
2040 char *old_name = *name;
2041 if (!old_name)
2042 return;
2043 *name = prefix_filename(state->prefix, *name);
2044 free(old_name);
2047 static void prefix_patch(struct apply_state *state, struct patch *p)
2049 if (!state->prefix || p->is_toplevel_relative)
2050 return;
2051 prefix_one(state, &p->new_name);
2052 prefix_one(state, &p->old_name);
2056 * include/exclude
2059 static void add_name_limit(struct apply_state *state,
2060 const char *name,
2061 int exclude)
2063 struct string_list_item *it;
2065 it = string_list_append(&state->limit_by_name, name);
2066 it->util = exclude ? NULL : (void *) 1;
2069 static int use_patch(struct apply_state *state, struct patch *p)
2071 const char *pathname = p->new_name ? p->new_name : p->old_name;
2072 int i;
2074 /* Paths outside are not touched regardless of "--include" */
2075 if (state->prefix && *state->prefix) {
2076 const char *rest;
2077 if (!skip_prefix(pathname, state->prefix, &rest) || !*rest)
2078 return 0;
2081 /* See if it matches any of exclude/include rule */
2082 for (i = 0; i < state->limit_by_name.nr; i++) {
2083 struct string_list_item *it = &state->limit_by_name.items[i];
2084 if (!wildmatch(it->string, pathname, 0))
2085 return (it->util != NULL);
2089 * If we had any include, a path that does not match any rule is
2090 * not used. Otherwise, we saw bunch of exclude rules (or none)
2091 * and such a path is used.
2093 return !state->has_include;
2097 * Read the patch text in "buffer" that extends for "size" bytes; stop
2098 * reading after seeing a single patch (i.e. changes to a single file).
2099 * Create fragments (i.e. patch hunks) and hang them to the given patch.
2101 * Returns:
2102 * -1 if no header was found or parse_binary() failed,
2103 * -128 on another error,
2104 * the number of bytes consumed otherwise,
2105 * so that the caller can call us again for the next patch.
2107 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2109 int hdrsize, patchsize;
2110 int offset = find_header(state, buffer, size, &hdrsize, patch);
2112 if (offset < 0)
2113 return offset;
2115 prefix_patch(state, patch);
2117 if (!use_patch(state, patch))
2118 patch->ws_rule = 0;
2119 else if (patch->new_name)
2120 patch->ws_rule = whitespace_rule(state->repo->index,
2121 patch->new_name);
2122 else
2123 patch->ws_rule = whitespace_rule(state->repo->index,
2124 patch->old_name);
2126 patchsize = parse_single_patch(state,
2127 buffer + offset + hdrsize,
2128 size - offset - hdrsize,
2129 patch);
2131 if (patchsize < 0)
2132 return -128;
2134 if (!patchsize) {
2135 static const char git_binary[] = "GIT binary patch\n";
2136 int hd = hdrsize + offset;
2137 unsigned long llen = linelen(buffer + hd, size - hd);
2139 if (llen == sizeof(git_binary) - 1 &&
2140 !memcmp(git_binary, buffer + hd, llen)) {
2141 int used;
2142 state->linenr++;
2143 used = parse_binary(state, buffer + hd + llen,
2144 size - hd - llen, patch);
2145 if (used < 0)
2146 return -1;
2147 if (used)
2148 patchsize = used + llen;
2149 else
2150 patchsize = 0;
2152 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2153 static const char *binhdr[] = {
2154 "Binary files ",
2155 "Files ",
2156 NULL,
2158 int i;
2159 for (i = 0; binhdr[i]; i++) {
2160 int len = strlen(binhdr[i]);
2161 if (len < size - hd &&
2162 !memcmp(binhdr[i], buffer + hd, len)) {
2163 state->linenr++;
2164 patch->is_binary = 1;
2165 patchsize = llen;
2166 break;
2171 /* Empty patch cannot be applied if it is a text patch
2172 * without metadata change. A binary patch appears
2173 * empty to us here.
2175 if ((state->apply || state->check) &&
2176 (!patch->is_binary && !metadata_changes(patch))) {
2177 error(_("patch with only garbage at line %d"), state->linenr);
2178 return -128;
2182 return offset + hdrsize + patchsize;
2185 static void reverse_patches(struct patch *p)
2187 for (; p; p = p->next) {
2188 struct fragment *frag = p->fragments;
2190 SWAP(p->new_name, p->old_name);
2191 SWAP(p->new_mode, p->old_mode);
2192 SWAP(p->is_new, p->is_delete);
2193 SWAP(p->lines_added, p->lines_deleted);
2194 SWAP(p->old_oid_prefix, p->new_oid_prefix);
2196 for (; frag; frag = frag->next) {
2197 SWAP(frag->newpos, frag->oldpos);
2198 SWAP(frag->newlines, frag->oldlines);
2203 static const char pluses[] =
2204 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2205 static const char minuses[]=
2206 "----------------------------------------------------------------------";
2208 static void show_stats(struct apply_state *state, struct patch *patch)
2210 struct strbuf qname = STRBUF_INIT;
2211 char *cp = patch->new_name ? patch->new_name : patch->old_name;
2212 int max, add, del;
2214 quote_c_style(cp, &qname, NULL, 0);
2217 * "scale" the filename
2219 max = state->max_len;
2220 if (max > 50)
2221 max = 50;
2223 if (qname.len > max) {
2224 cp = strchr(qname.buf + qname.len + 3 - max, '/');
2225 if (!cp)
2226 cp = qname.buf + qname.len + 3 - max;
2227 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2230 if (patch->is_binary) {
2231 printf(" %-*s | Bin\n", max, qname.buf);
2232 strbuf_release(&qname);
2233 return;
2236 printf(" %-*s |", max, qname.buf);
2237 strbuf_release(&qname);
2240 * scale the add/delete
2242 max = max + state->max_change > 70 ? 70 - max : state->max_change;
2243 add = patch->lines_added;
2244 del = patch->lines_deleted;
2246 if (state->max_change > 0) {
2247 int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2248 add = (add * max + state->max_change / 2) / state->max_change;
2249 del = total - add;
2251 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2252 add, pluses, del, minuses);
2255 static int read_old_data(struct stat *st, struct patch *patch,
2256 const char *path, struct strbuf *buf)
2258 int conv_flags = patch->crlf_in_old ?
2259 CONV_EOL_KEEP_CRLF : CONV_EOL_RENORMALIZE;
2260 switch (st->st_mode & S_IFMT) {
2261 case S_IFLNK:
2262 if (strbuf_readlink(buf, path, st->st_size) < 0)
2263 return error(_("unable to read symlink %s"), path);
2264 return 0;
2265 case S_IFREG:
2266 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2267 return error(_("unable to open or read %s"), path);
2269 * "git apply" without "--index/--cached" should never look
2270 * at the index; the target file may not have been added to
2271 * the index yet, and we may not even be in any Git repository.
2272 * Pass NULL to convert_to_git() to stress this; the function
2273 * should never look at the index when explicit crlf option
2274 * is given.
2276 convert_to_git(NULL, path, buf->buf, buf->len, buf, conv_flags);
2277 return 0;
2278 default:
2279 return -1;
2284 * Update the preimage, and the common lines in postimage,
2285 * from buffer buf of length len. If postlen is 0 the postimage
2286 * is updated in place, otherwise it's updated on a new buffer
2287 * of length postlen
2290 static void update_pre_post_images(struct image *preimage,
2291 struct image *postimage,
2292 char *buf,
2293 size_t len, size_t postlen)
2295 int i, ctx, reduced;
2296 char *new_buf, *old_buf, *fixed;
2297 struct image fixed_preimage;
2300 * Update the preimage with whitespace fixes. Note that we
2301 * are not losing preimage->buf -- apply_one_fragment() will
2302 * free "oldlines".
2304 prepare_image(&fixed_preimage, buf, len, 1);
2305 assert(postlen
2306 ? fixed_preimage.nr == preimage->nr
2307 : fixed_preimage.nr <= preimage->nr);
2308 for (i = 0; i < fixed_preimage.nr; i++)
2309 fixed_preimage.line[i].flag = preimage->line[i].flag;
2310 free(preimage->line_allocated);
2311 *preimage = fixed_preimage;
2314 * Adjust the common context lines in postimage. This can be
2315 * done in-place when we are shrinking it with whitespace
2316 * fixing, but needs a new buffer when ignoring whitespace or
2317 * expanding leading tabs to spaces.
2319 * We trust the caller to tell us if the update can be done
2320 * in place (postlen==0) or not.
2322 old_buf = postimage->buf;
2323 if (postlen)
2324 new_buf = postimage->buf = xmalloc(postlen);
2325 else
2326 new_buf = old_buf;
2327 fixed = preimage->buf;
2329 for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2330 size_t l_len = postimage->line[i].len;
2331 if (!(postimage->line[i].flag & LINE_COMMON)) {
2332 /* an added line -- no counterparts in preimage */
2333 memmove(new_buf, old_buf, l_len);
2334 old_buf += l_len;
2335 new_buf += l_len;
2336 continue;
2339 /* a common context -- skip it in the original postimage */
2340 old_buf += l_len;
2342 /* and find the corresponding one in the fixed preimage */
2343 while (ctx < preimage->nr &&
2344 !(preimage->line[ctx].flag & LINE_COMMON)) {
2345 fixed += preimage->line[ctx].len;
2346 ctx++;
2350 * preimage is expected to run out, if the caller
2351 * fixed addition of trailing blank lines.
2353 if (preimage->nr <= ctx) {
2354 reduced++;
2355 continue;
2358 /* and copy it in, while fixing the line length */
2359 l_len = preimage->line[ctx].len;
2360 memcpy(new_buf, fixed, l_len);
2361 new_buf += l_len;
2362 fixed += l_len;
2363 postimage->line[i].len = l_len;
2364 ctx++;
2367 if (postlen
2368 ? postlen < new_buf - postimage->buf
2369 : postimage->len < new_buf - postimage->buf)
2370 BUG("caller miscounted postlen: asked %d, orig = %d, used = %d",
2371 (int)postlen, (int) postimage->len, (int)(new_buf - postimage->buf));
2373 /* Fix the length of the whole thing */
2374 postimage->len = new_buf - postimage->buf;
2375 postimage->nr -= reduced;
2378 static int line_by_line_fuzzy_match(struct image *img,
2379 struct image *preimage,
2380 struct image *postimage,
2381 unsigned long current,
2382 int current_lno,
2383 int preimage_limit)
2385 int i;
2386 size_t imgoff = 0;
2387 size_t preoff = 0;
2388 size_t postlen = postimage->len;
2389 size_t extra_chars;
2390 char *buf;
2391 char *preimage_eof;
2392 char *preimage_end;
2393 struct strbuf fixed;
2394 char *fixed_buf;
2395 size_t fixed_len;
2397 for (i = 0; i < preimage_limit; i++) {
2398 size_t prelen = preimage->line[i].len;
2399 size_t imglen = img->line[current_lno+i].len;
2401 if (!fuzzy_matchlines(img->buf + current + imgoff, imglen,
2402 preimage->buf + preoff, prelen))
2403 return 0;
2404 if (preimage->line[i].flag & LINE_COMMON)
2405 postlen += imglen - prelen;
2406 imgoff += imglen;
2407 preoff += prelen;
2411 * Ok, the preimage matches with whitespace fuzz.
2413 * imgoff now holds the true length of the target that
2414 * matches the preimage before the end of the file.
2416 * Count the number of characters in the preimage that fall
2417 * beyond the end of the file and make sure that all of them
2418 * are whitespace characters. (This can only happen if
2419 * we are removing blank lines at the end of the file.)
2421 buf = preimage_eof = preimage->buf + preoff;
2422 for ( ; i < preimage->nr; i++)
2423 preoff += preimage->line[i].len;
2424 preimage_end = preimage->buf + preoff;
2425 for ( ; buf < preimage_end; buf++)
2426 if (!isspace(*buf))
2427 return 0;
2430 * Update the preimage and the common postimage context
2431 * lines to use the same whitespace as the target.
2432 * If whitespace is missing in the target (i.e.
2433 * if the preimage extends beyond the end of the file),
2434 * use the whitespace from the preimage.
2436 extra_chars = preimage_end - preimage_eof;
2437 strbuf_init(&fixed, imgoff + extra_chars);
2438 strbuf_add(&fixed, img->buf + current, imgoff);
2439 strbuf_add(&fixed, preimage_eof, extra_chars);
2440 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2441 update_pre_post_images(preimage, postimage,
2442 fixed_buf, fixed_len, postlen);
2443 return 1;
2446 static int match_fragment(struct apply_state *state,
2447 struct image *img,
2448 struct image *preimage,
2449 struct image *postimage,
2450 unsigned long current,
2451 int current_lno,
2452 unsigned ws_rule,
2453 int match_beginning, int match_end)
2455 int i;
2456 char *fixed_buf, *buf, *orig, *target;
2457 struct strbuf fixed;
2458 size_t fixed_len, postlen;
2459 int preimage_limit;
2461 if (preimage->nr + current_lno <= img->nr) {
2463 * The hunk falls within the boundaries of img.
2465 preimage_limit = preimage->nr;
2466 if (match_end && (preimage->nr + current_lno != img->nr))
2467 return 0;
2468 } else if (state->ws_error_action == correct_ws_error &&
2469 (ws_rule & WS_BLANK_AT_EOF)) {
2471 * This hunk extends beyond the end of img, and we are
2472 * removing blank lines at the end of the file. This
2473 * many lines from the beginning of the preimage must
2474 * match with img, and the remainder of the preimage
2475 * must be blank.
2477 preimage_limit = img->nr - current_lno;
2478 } else {
2480 * The hunk extends beyond the end of the img and
2481 * we are not removing blanks at the end, so we
2482 * should reject the hunk at this position.
2484 return 0;
2487 if (match_beginning && current_lno)
2488 return 0;
2490 /* Quick hash check */
2491 for (i = 0; i < preimage_limit; i++)
2492 if ((img->line[current_lno + i].flag & LINE_PATCHED) ||
2493 (preimage->line[i].hash != img->line[current_lno + i].hash))
2494 return 0;
2496 if (preimage_limit == preimage->nr) {
2498 * Do we have an exact match? If we were told to match
2499 * at the end, size must be exactly at current+fragsize,
2500 * otherwise current+fragsize must be still within the preimage,
2501 * and either case, the old piece should match the preimage
2502 * exactly.
2504 if ((match_end
2505 ? (current + preimage->len == img->len)
2506 : (current + preimage->len <= img->len)) &&
2507 !memcmp(img->buf + current, preimage->buf, preimage->len))
2508 return 1;
2509 } else {
2511 * The preimage extends beyond the end of img, so
2512 * there cannot be an exact match.
2514 * There must be one non-blank context line that match
2515 * a line before the end of img.
2517 char *buf_end;
2519 buf = preimage->buf;
2520 buf_end = buf;
2521 for (i = 0; i < preimage_limit; i++)
2522 buf_end += preimage->line[i].len;
2524 for ( ; buf < buf_end; buf++)
2525 if (!isspace(*buf))
2526 break;
2527 if (buf == buf_end)
2528 return 0;
2532 * No exact match. If we are ignoring whitespace, run a line-by-line
2533 * fuzzy matching. We collect all the line length information because
2534 * we need it to adjust whitespace if we match.
2536 if (state->ws_ignore_action == ignore_ws_change)
2537 return line_by_line_fuzzy_match(img, preimage, postimage,
2538 current, current_lno, preimage_limit);
2540 if (state->ws_error_action != correct_ws_error)
2541 return 0;
2544 * The hunk does not apply byte-by-byte, but the hash says
2545 * it might with whitespace fuzz. We weren't asked to
2546 * ignore whitespace, we were asked to correct whitespace
2547 * errors, so let's try matching after whitespace correction.
2549 * While checking the preimage against the target, whitespace
2550 * errors in both fixed, we count how large the corresponding
2551 * postimage needs to be. The postimage prepared by
2552 * apply_one_fragment() has whitespace errors fixed on added
2553 * lines already, but the common lines were propagated as-is,
2554 * which may become longer when their whitespace errors are
2555 * fixed.
2558 /* First count added lines in postimage */
2559 postlen = 0;
2560 for (i = 0; i < postimage->nr; i++) {
2561 if (!(postimage->line[i].flag & LINE_COMMON))
2562 postlen += postimage->line[i].len;
2566 * The preimage may extend beyond the end of the file,
2567 * but in this loop we will only handle the part of the
2568 * preimage that falls within the file.
2570 strbuf_init(&fixed, preimage->len + 1);
2571 orig = preimage->buf;
2572 target = img->buf + current;
2573 for (i = 0; i < preimage_limit; i++) {
2574 size_t oldlen = preimage->line[i].len;
2575 size_t tgtlen = img->line[current_lno + i].len;
2576 size_t fixstart = fixed.len;
2577 struct strbuf tgtfix;
2578 int match;
2580 /* Try fixing the line in the preimage */
2581 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2583 /* Try fixing the line in the target */
2584 strbuf_init(&tgtfix, tgtlen);
2585 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2588 * If they match, either the preimage was based on
2589 * a version before our tree fixed whitespace breakage,
2590 * or we are lacking a whitespace-fix patch the tree
2591 * the preimage was based on already had (i.e. target
2592 * has whitespace breakage, the preimage doesn't).
2593 * In either case, we are fixing the whitespace breakages
2594 * so we might as well take the fix together with their
2595 * real change.
2597 match = (tgtfix.len == fixed.len - fixstart &&
2598 !memcmp(tgtfix.buf, fixed.buf + fixstart,
2599 fixed.len - fixstart));
2601 /* Add the length if this is common with the postimage */
2602 if (preimage->line[i].flag & LINE_COMMON)
2603 postlen += tgtfix.len;
2605 strbuf_release(&tgtfix);
2606 if (!match)
2607 goto unmatch_exit;
2609 orig += oldlen;
2610 target += tgtlen;
2615 * Now handle the lines in the preimage that falls beyond the
2616 * end of the file (if any). They will only match if they are
2617 * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2618 * false).
2620 for ( ; i < preimage->nr; i++) {
2621 size_t fixstart = fixed.len; /* start of the fixed preimage */
2622 size_t oldlen = preimage->line[i].len;
2623 int j;
2625 /* Try fixing the line in the preimage */
2626 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2628 for (j = fixstart; j < fixed.len; j++)
2629 if (!isspace(fixed.buf[j]))
2630 goto unmatch_exit;
2632 orig += oldlen;
2636 * Yes, the preimage is based on an older version that still
2637 * has whitespace breakages unfixed, and fixing them makes the
2638 * hunk match. Update the context lines in the postimage.
2640 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2641 if (postlen < postimage->len)
2642 postlen = 0;
2643 update_pre_post_images(preimage, postimage,
2644 fixed_buf, fixed_len, postlen);
2645 return 1;
2647 unmatch_exit:
2648 strbuf_release(&fixed);
2649 return 0;
2652 static int find_pos(struct apply_state *state,
2653 struct image *img,
2654 struct image *preimage,
2655 struct image *postimage,
2656 int line,
2657 unsigned ws_rule,
2658 int match_beginning, int match_end)
2660 int i;
2661 unsigned long backwards, forwards, current;
2662 int backwards_lno, forwards_lno, current_lno;
2665 * When running with --allow-overlap, it is possible that a hunk is
2666 * seen that pretends to start at the beginning (but no longer does),
2667 * and that *still* needs to match the end. So trust `match_end` more
2668 * than `match_beginning`.
2670 if (state->allow_overlap && match_beginning && match_end &&
2671 img->nr - preimage->nr != 0)
2672 match_beginning = 0;
2675 * If match_beginning or match_end is specified, there is no
2676 * point starting from a wrong line that will never match and
2677 * wander around and wait for a match at the specified end.
2679 if (match_beginning)
2680 line = 0;
2681 else if (match_end)
2682 line = img->nr - preimage->nr;
2685 * Because the comparison is unsigned, the following test
2686 * will also take care of a negative line number that can
2687 * result when match_end and preimage is larger than the target.
2689 if ((size_t) line > img->nr)
2690 line = img->nr;
2692 current = 0;
2693 for (i = 0; i < line; i++)
2694 current += img->line[i].len;
2697 * There's probably some smart way to do this, but I'll leave
2698 * that to the smart and beautiful people. I'm simple and stupid.
2700 backwards = current;
2701 backwards_lno = line;
2702 forwards = current;
2703 forwards_lno = line;
2704 current_lno = line;
2706 for (i = 0; ; i++) {
2707 if (match_fragment(state, img, preimage, postimage,
2708 current, current_lno, ws_rule,
2709 match_beginning, match_end))
2710 return current_lno;
2712 again:
2713 if (backwards_lno == 0 && forwards_lno == img->nr)
2714 break;
2716 if (i & 1) {
2717 if (backwards_lno == 0) {
2718 i++;
2719 goto again;
2721 backwards_lno--;
2722 backwards -= img->line[backwards_lno].len;
2723 current = backwards;
2724 current_lno = backwards_lno;
2725 } else {
2726 if (forwards_lno == img->nr) {
2727 i++;
2728 goto again;
2730 forwards += img->line[forwards_lno].len;
2731 forwards_lno++;
2732 current = forwards;
2733 current_lno = forwards_lno;
2737 return -1;
2740 static void remove_first_line(struct image *img)
2742 img->buf += img->line[0].len;
2743 img->len -= img->line[0].len;
2744 img->line++;
2745 img->nr--;
2748 static void remove_last_line(struct image *img)
2750 img->len -= img->line[--img->nr].len;
2754 * The change from "preimage" and "postimage" has been found to
2755 * apply at applied_pos (counts in line numbers) in "img".
2756 * Update "img" to remove "preimage" and replace it with "postimage".
2758 static void update_image(struct apply_state *state,
2759 struct image *img,
2760 int applied_pos,
2761 struct image *preimage,
2762 struct image *postimage)
2765 * remove the copy of preimage at offset in img
2766 * and replace it with postimage
2768 int i, nr;
2769 size_t remove_count, insert_count, applied_at = 0;
2770 char *result;
2771 int preimage_limit;
2774 * If we are removing blank lines at the end of img,
2775 * the preimage may extend beyond the end.
2776 * If that is the case, we must be careful only to
2777 * remove the part of the preimage that falls within
2778 * the boundaries of img. Initialize preimage_limit
2779 * to the number of lines in the preimage that falls
2780 * within the boundaries.
2782 preimage_limit = preimage->nr;
2783 if (preimage_limit > img->nr - applied_pos)
2784 preimage_limit = img->nr - applied_pos;
2786 for (i = 0; i < applied_pos; i++)
2787 applied_at += img->line[i].len;
2789 remove_count = 0;
2790 for (i = 0; i < preimage_limit; i++)
2791 remove_count += img->line[applied_pos + i].len;
2792 insert_count = postimage->len;
2794 /* Adjust the contents */
2795 result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2796 memcpy(result, img->buf, applied_at);
2797 memcpy(result + applied_at, postimage->buf, postimage->len);
2798 memcpy(result + applied_at + postimage->len,
2799 img->buf + (applied_at + remove_count),
2800 img->len - (applied_at + remove_count));
2801 free(img->buf);
2802 img->buf = result;
2803 img->len += insert_count - remove_count;
2804 result[img->len] = '\0';
2806 /* Adjust the line table */
2807 nr = img->nr + postimage->nr - preimage_limit;
2808 if (preimage_limit < postimage->nr) {
2810 * NOTE: this knows that we never call remove_first_line()
2811 * on anything other than pre/post image.
2813 REALLOC_ARRAY(img->line, nr);
2814 img->line_allocated = img->line;
2816 if (preimage_limit != postimage->nr)
2817 MOVE_ARRAY(img->line + applied_pos + postimage->nr,
2818 img->line + applied_pos + preimage_limit,
2819 img->nr - (applied_pos + preimage_limit));
2820 COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->nr);
2821 if (!state->allow_overlap)
2822 for (i = 0; i < postimage->nr; i++)
2823 img->line[applied_pos + i].flag |= LINE_PATCHED;
2824 img->nr = nr;
2828 * Use the patch-hunk text in "frag" to prepare two images (preimage and
2829 * postimage) for the hunk. Find lines that match "preimage" in "img" and
2830 * replace the part of "img" with "postimage" text.
2832 static int apply_one_fragment(struct apply_state *state,
2833 struct image *img, struct fragment *frag,
2834 int inaccurate_eof, unsigned ws_rule,
2835 int nth_fragment)
2837 int match_beginning, match_end;
2838 const char *patch = frag->patch;
2839 int size = frag->size;
2840 char *old, *oldlines;
2841 struct strbuf newlines;
2842 int new_blank_lines_at_end = 0;
2843 int found_new_blank_lines_at_end = 0;
2844 int hunk_linenr = frag->linenr;
2845 unsigned long leading, trailing;
2846 int pos, applied_pos;
2847 struct image preimage;
2848 struct image postimage;
2850 memset(&preimage, 0, sizeof(preimage));
2851 memset(&postimage, 0, sizeof(postimage));
2852 oldlines = xmalloc(size);
2853 strbuf_init(&newlines, size);
2855 old = oldlines;
2856 while (size > 0) {
2857 char first;
2858 int len = linelen(patch, size);
2859 int plen;
2860 int added_blank_line = 0;
2861 int is_blank_context = 0;
2862 size_t start;
2864 if (!len)
2865 break;
2868 * "plen" is how much of the line we should use for
2869 * the actual patch data. Normally we just remove the
2870 * first character on the line, but if the line is
2871 * followed by "\ No newline", then we also remove the
2872 * last one (which is the newline, of course).
2874 plen = len - 1;
2875 if (len < size && patch[len] == '\\')
2876 plen--;
2877 first = *patch;
2878 if (state->apply_in_reverse) {
2879 if (first == '-')
2880 first = '+';
2881 else if (first == '+')
2882 first = '-';
2885 switch (first) {
2886 case '\n':
2887 /* Newer GNU diff, empty context line */
2888 if (plen < 0)
2889 /* ... followed by '\No newline'; nothing */
2890 break;
2891 *old++ = '\n';
2892 strbuf_addch(&newlines, '\n');
2893 add_line_info(&preimage, "\n", 1, LINE_COMMON);
2894 add_line_info(&postimage, "\n", 1, LINE_COMMON);
2895 is_blank_context = 1;
2896 break;
2897 case ' ':
2898 if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2899 ws_blank_line(patch + 1, plen, ws_rule))
2900 is_blank_context = 1;
2901 /* fallthrough */
2902 case '-':
2903 memcpy(old, patch + 1, plen);
2904 add_line_info(&preimage, old, plen,
2905 (first == ' ' ? LINE_COMMON : 0));
2906 old += plen;
2907 if (first == '-')
2908 break;
2909 /* fallthrough */
2910 case '+':
2911 /* --no-add does not add new lines */
2912 if (first == '+' && state->no_add)
2913 break;
2915 start = newlines.len;
2916 if (first != '+' ||
2917 !state->whitespace_error ||
2918 state->ws_error_action != correct_ws_error) {
2919 strbuf_add(&newlines, patch + 1, plen);
2921 else {
2922 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2924 add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2925 (first == '+' ? 0 : LINE_COMMON));
2926 if (first == '+' &&
2927 (ws_rule & WS_BLANK_AT_EOF) &&
2928 ws_blank_line(patch + 1, plen, ws_rule))
2929 added_blank_line = 1;
2930 break;
2931 case '@': case '\\':
2932 /* Ignore it, we already handled it */
2933 break;
2934 default:
2935 if (state->apply_verbosity > verbosity_normal)
2936 error(_("invalid start of line: '%c'"), first);
2937 applied_pos = -1;
2938 goto out;
2940 if (added_blank_line) {
2941 if (!new_blank_lines_at_end)
2942 found_new_blank_lines_at_end = hunk_linenr;
2943 new_blank_lines_at_end++;
2945 else if (is_blank_context)
2947 else
2948 new_blank_lines_at_end = 0;
2949 patch += len;
2950 size -= len;
2951 hunk_linenr++;
2953 if (inaccurate_eof &&
2954 old > oldlines && old[-1] == '\n' &&
2955 newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2956 old--;
2957 strbuf_setlen(&newlines, newlines.len - 1);
2958 preimage.line_allocated[preimage.nr - 1].len--;
2959 postimage.line_allocated[postimage.nr - 1].len--;
2962 leading = frag->leading;
2963 trailing = frag->trailing;
2966 * A hunk to change lines at the beginning would begin with
2967 * @@ -1,L +N,M @@
2968 * but we need to be careful. -U0 that inserts before the second
2969 * line also has this pattern.
2971 * And a hunk to add to an empty file would begin with
2972 * @@ -0,0 +N,M @@
2974 * In other words, a hunk that is (frag->oldpos <= 1) with or
2975 * without leading context must match at the beginning.
2977 match_beginning = (!frag->oldpos ||
2978 (frag->oldpos == 1 && !state->unidiff_zero));
2981 * A hunk without trailing lines must match at the end.
2982 * However, we simply cannot tell if a hunk must match end
2983 * from the lack of trailing lines if the patch was generated
2984 * with unidiff without any context.
2986 match_end = !state->unidiff_zero && !trailing;
2988 pos = frag->newpos ? (frag->newpos - 1) : 0;
2989 preimage.buf = oldlines;
2990 preimage.len = old - oldlines;
2991 postimage.buf = newlines.buf;
2992 postimage.len = newlines.len;
2993 preimage.line = preimage.line_allocated;
2994 postimage.line = postimage.line_allocated;
2996 for (;;) {
2998 applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2999 ws_rule, match_beginning, match_end);
3001 if (applied_pos >= 0)
3002 break;
3004 /* Am I at my context limits? */
3005 if ((leading <= state->p_context) && (trailing <= state->p_context))
3006 break;
3007 if (match_beginning || match_end) {
3008 match_beginning = match_end = 0;
3009 continue;
3013 * Reduce the number of context lines; reduce both
3014 * leading and trailing if they are equal otherwise
3015 * just reduce the larger context.
3017 if (leading >= trailing) {
3018 remove_first_line(&preimage);
3019 remove_first_line(&postimage);
3020 pos--;
3021 leading--;
3023 if (trailing > leading) {
3024 remove_last_line(&preimage);
3025 remove_last_line(&postimage);
3026 trailing--;
3030 if (applied_pos >= 0) {
3031 if (new_blank_lines_at_end &&
3032 preimage.nr + applied_pos >= img->nr &&
3033 (ws_rule & WS_BLANK_AT_EOF) &&
3034 state->ws_error_action != nowarn_ws_error) {
3035 record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
3036 found_new_blank_lines_at_end);
3037 if (state->ws_error_action == correct_ws_error) {
3038 while (new_blank_lines_at_end--)
3039 remove_last_line(&postimage);
3042 * We would want to prevent write_out_results()
3043 * from taking place in apply_patch() that follows
3044 * the callchain led us here, which is:
3045 * apply_patch->check_patch_list->check_patch->
3046 * apply_data->apply_fragments->apply_one_fragment
3048 if (state->ws_error_action == die_on_ws_error)
3049 state->apply = 0;
3052 if (state->apply_verbosity > verbosity_normal && applied_pos != pos) {
3053 int offset = applied_pos - pos;
3054 if (state->apply_in_reverse)
3055 offset = 0 - offset;
3056 fprintf_ln(stderr,
3057 Q_("Hunk #%d succeeded at %d (offset %d line).",
3058 "Hunk #%d succeeded at %d (offset %d lines).",
3059 offset),
3060 nth_fragment, applied_pos + 1, offset);
3064 * Warn if it was necessary to reduce the number
3065 * of context lines.
3067 if ((leading != frag->leading ||
3068 trailing != frag->trailing) && state->apply_verbosity > verbosity_silent)
3069 fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
3070 " to apply fragment at %d"),
3071 leading, trailing, applied_pos+1);
3072 update_image(state, img, applied_pos, &preimage, &postimage);
3073 } else {
3074 if (state->apply_verbosity > verbosity_normal)
3075 error(_("while searching for:\n%.*s"),
3076 (int)(old - oldlines), oldlines);
3079 out:
3080 free(oldlines);
3081 strbuf_release(&newlines);
3082 free(preimage.line_allocated);
3083 free(postimage.line_allocated);
3085 return (applied_pos < 0);
3088 static int apply_binary_fragment(struct apply_state *state,
3089 struct image *img,
3090 struct patch *patch)
3092 struct fragment *fragment = patch->fragments;
3093 unsigned long len;
3094 void *dst;
3096 if (!fragment)
3097 return error(_("missing binary patch data for '%s'"),
3098 patch->new_name ?
3099 patch->new_name :
3100 patch->old_name);
3102 /* Binary patch is irreversible without the optional second hunk */
3103 if (state->apply_in_reverse) {
3104 if (!fragment->next)
3105 return error(_("cannot reverse-apply a binary patch "
3106 "without the reverse hunk to '%s'"),
3107 patch->new_name
3108 ? patch->new_name : patch->old_name);
3109 fragment = fragment->next;
3111 switch (fragment->binary_patch_method) {
3112 case BINARY_DELTA_DEFLATED:
3113 dst = patch_delta(img->buf, img->len, fragment->patch,
3114 fragment->size, &len);
3115 if (!dst)
3116 return -1;
3117 clear_image(img);
3118 img->buf = dst;
3119 img->len = len;
3120 return 0;
3121 case BINARY_LITERAL_DEFLATED:
3122 clear_image(img);
3123 img->len = fragment->size;
3124 img->buf = xmemdupz(fragment->patch, img->len);
3125 return 0;
3127 return -1;
3131 * Replace "img" with the result of applying the binary patch.
3132 * The binary patch data itself in patch->fragment is still kept
3133 * but the preimage prepared by the caller in "img" is freed here
3134 * or in the helper function apply_binary_fragment() this calls.
3136 static int apply_binary(struct apply_state *state,
3137 struct image *img,
3138 struct patch *patch)
3140 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3141 struct object_id oid;
3142 const unsigned hexsz = the_hash_algo->hexsz;
3145 * For safety, we require patch index line to contain
3146 * full hex textual object ID for old and new, at least for now.
3148 if (strlen(patch->old_oid_prefix) != hexsz ||
3149 strlen(patch->new_oid_prefix) != hexsz ||
3150 get_oid_hex(patch->old_oid_prefix, &oid) ||
3151 get_oid_hex(patch->new_oid_prefix, &oid))
3152 return error(_("cannot apply binary patch to '%s' "
3153 "without full index line"), name);
3155 if (patch->old_name) {
3157 * See if the old one matches what the patch
3158 * applies to.
3160 hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3161 &oid);
3162 if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix))
3163 return error(_("the patch applies to '%s' (%s), "
3164 "which does not match the "
3165 "current contents."),
3166 name, oid_to_hex(&oid));
3168 else {
3169 /* Otherwise, the old one must be empty. */
3170 if (img->len)
3171 return error(_("the patch applies to an empty "
3172 "'%s' but it is not empty"), name);
3175 get_oid_hex(patch->new_oid_prefix, &oid);
3176 if (is_null_oid(&oid)) {
3177 clear_image(img);
3178 return 0; /* deletion patch */
3181 if (has_object(the_repository, &oid, 0)) {
3182 /* We already have the postimage */
3183 enum object_type type;
3184 unsigned long size;
3185 char *result;
3187 result = read_object_file(&oid, &type, &size);
3188 if (!result)
3189 return error(_("the necessary postimage %s for "
3190 "'%s' cannot be read"),
3191 patch->new_oid_prefix, name);
3192 clear_image(img);
3193 img->buf = result;
3194 img->len = size;
3195 } else {
3197 * We have verified buf matches the preimage;
3198 * apply the patch data to it, which is stored
3199 * in the patch->fragments->{patch,size}.
3201 if (apply_binary_fragment(state, img, patch))
3202 return error(_("binary patch does not apply to '%s'"),
3203 name);
3205 /* verify that the result matches */
3206 hash_object_file(the_hash_algo, img->buf, img->len, blob_type,
3207 &oid);
3208 if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix))
3209 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3210 name, patch->new_oid_prefix, oid_to_hex(&oid));
3213 return 0;
3216 static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3218 struct fragment *frag = patch->fragments;
3219 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3220 unsigned ws_rule = patch->ws_rule;
3221 unsigned inaccurate_eof = patch->inaccurate_eof;
3222 int nth = 0;
3224 if (patch->is_binary)
3225 return apply_binary(state, img, patch);
3227 while (frag) {
3228 nth++;
3229 if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3230 error(_("patch failed: %s:%ld"), name, frag->oldpos);
3231 if (!state->apply_with_reject)
3232 return -1;
3233 frag->rejected = 1;
3235 frag = frag->next;
3237 return 0;
3240 static int read_blob_object(struct strbuf *buf, const struct object_id *oid, unsigned mode)
3242 if (S_ISGITLINK(mode)) {
3243 strbuf_grow(buf, 100);
3244 strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid));
3245 } else {
3246 enum object_type type;
3247 unsigned long sz;
3248 char *result;
3250 result = read_object_file(oid, &type, &sz);
3251 if (!result)
3252 return -1;
3253 /* XXX read_sha1_file NUL-terminates */
3254 strbuf_attach(buf, result, sz, sz + 1);
3256 return 0;
3259 static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3261 if (!ce)
3262 return 0;
3263 return read_blob_object(buf, &ce->oid, ce->ce_mode);
3266 static struct patch *in_fn_table(struct apply_state *state, const char *name)
3268 struct string_list_item *item;
3270 if (name == NULL)
3271 return NULL;
3273 item = string_list_lookup(&state->fn_table, name);
3274 if (item != NULL)
3275 return (struct patch *)item->util;
3277 return NULL;
3281 * item->util in the filename table records the status of the path.
3282 * Usually it points at a patch (whose result records the contents
3283 * of it after applying it), but it could be PATH_WAS_DELETED for a
3284 * path that a previously applied patch has already removed, or
3285 * PATH_TO_BE_DELETED for a path that a later patch would remove.
3287 * The latter is needed to deal with a case where two paths A and B
3288 * are swapped by first renaming A to B and then renaming B to A;
3289 * moving A to B should not be prevented due to presence of B as we
3290 * will remove it in a later patch.
3292 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3293 #define PATH_WAS_DELETED ((struct patch *) -1)
3295 static int to_be_deleted(struct patch *patch)
3297 return patch == PATH_TO_BE_DELETED;
3300 static int was_deleted(struct patch *patch)
3302 return patch == PATH_WAS_DELETED;
3305 static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3307 struct string_list_item *item;
3310 * Always add new_name unless patch is a deletion
3311 * This should cover the cases for normal diffs,
3312 * file creations and copies
3314 if (patch->new_name != NULL) {
3315 item = string_list_insert(&state->fn_table, patch->new_name);
3316 item->util = patch;
3320 * store a failure on rename/deletion cases because
3321 * later chunks shouldn't patch old names
3323 if ((patch->new_name == NULL) || (patch->is_rename)) {
3324 item = string_list_insert(&state->fn_table, patch->old_name);
3325 item->util = PATH_WAS_DELETED;
3329 static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3332 * store information about incoming file deletion
3334 while (patch) {
3335 if ((patch->new_name == NULL) || (patch->is_rename)) {
3336 struct string_list_item *item;
3337 item = string_list_insert(&state->fn_table, patch->old_name);
3338 item->util = PATH_TO_BE_DELETED;
3340 patch = patch->next;
3344 static int checkout_target(struct index_state *istate,
3345 struct cache_entry *ce, struct stat *st)
3347 struct checkout costate = CHECKOUT_INIT;
3349 costate.refresh_cache = 1;
3350 costate.istate = istate;
3351 if (checkout_entry(ce, &costate, NULL, NULL) ||
3352 lstat(ce->name, st))
3353 return error(_("cannot checkout %s"), ce->name);
3354 return 0;
3357 static struct patch *previous_patch(struct apply_state *state,
3358 struct patch *patch,
3359 int *gone)
3361 struct patch *previous;
3363 *gone = 0;
3364 if (patch->is_copy || patch->is_rename)
3365 return NULL; /* "git" patches do not depend on the order */
3367 previous = in_fn_table(state, patch->old_name);
3368 if (!previous)
3369 return NULL;
3371 if (to_be_deleted(previous))
3372 return NULL; /* the deletion hasn't happened yet */
3374 if (was_deleted(previous))
3375 *gone = 1;
3377 return previous;
3380 static int verify_index_match(struct apply_state *state,
3381 const struct cache_entry *ce,
3382 struct stat *st)
3384 if (S_ISGITLINK(ce->ce_mode)) {
3385 if (!S_ISDIR(st->st_mode))
3386 return -1;
3387 return 0;
3389 return ie_match_stat(state->repo->index, ce, st,
3390 CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE);
3393 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3395 static int load_patch_target(struct apply_state *state,
3396 struct strbuf *buf,
3397 const struct cache_entry *ce,
3398 struct stat *st,
3399 struct patch *patch,
3400 const char *name,
3401 unsigned expected_mode)
3403 if (state->cached || state->check_index) {
3404 if (read_file_or_gitlink(ce, buf))
3405 return error(_("failed to read %s"), name);
3406 } else if (name) {
3407 if (S_ISGITLINK(expected_mode)) {
3408 if (ce)
3409 return read_file_or_gitlink(ce, buf);
3410 else
3411 return SUBMODULE_PATCH_WITHOUT_INDEX;
3412 } else if (has_symlink_leading_path(name, strlen(name))) {
3413 return error(_("reading from '%s' beyond a symbolic link"), name);
3414 } else {
3415 if (read_old_data(st, patch, name, buf))
3416 return error(_("failed to read %s"), name);
3419 return 0;
3423 * We are about to apply "patch"; populate the "image" with the
3424 * current version we have, from the working tree or from the index,
3425 * depending on the situation e.g. --cached/--index. If we are
3426 * applying a non-git patch that incrementally updates the tree,
3427 * we read from the result of a previous diff.
3429 static int load_preimage(struct apply_state *state,
3430 struct image *image,
3431 struct patch *patch, struct stat *st,
3432 const struct cache_entry *ce)
3434 struct strbuf buf = STRBUF_INIT;
3435 size_t len;
3436 char *img;
3437 struct patch *previous;
3438 int status;
3440 previous = previous_patch(state, patch, &status);
3441 if (status)
3442 return error(_("path %s has been renamed/deleted"),
3443 patch->old_name);
3444 if (previous) {
3445 /* We have a patched copy in memory; use that. */
3446 strbuf_add(&buf, previous->result, previous->resultsize);
3447 } else {
3448 status = load_patch_target(state, &buf, ce, st, patch,
3449 patch->old_name, patch->old_mode);
3450 if (status < 0)
3451 return status;
3452 else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3454 * There is no way to apply subproject
3455 * patch without looking at the index.
3456 * NEEDSWORK: shouldn't this be flagged
3457 * as an error???
3459 free_fragment_list(patch->fragments);
3460 patch->fragments = NULL;
3461 } else if (status) {
3462 return error(_("failed to read %s"), patch->old_name);
3466 img = strbuf_detach(&buf, &len);
3467 prepare_image(image, img, len, !patch->is_binary);
3468 return 0;
3471 static int resolve_to(struct image *image, const struct object_id *result_id)
3473 unsigned long size;
3474 enum object_type type;
3476 clear_image(image);
3478 image->buf = read_object_file(result_id, &type, &size);
3479 if (!image->buf || type != OBJ_BLOB)
3480 die("unable to read blob object %s", oid_to_hex(result_id));
3481 image->len = size;
3483 return 0;
3486 static int three_way_merge(struct apply_state *state,
3487 struct image *image,
3488 char *path,
3489 const struct object_id *base,
3490 const struct object_id *ours,
3491 const struct object_id *theirs)
3493 mmfile_t base_file, our_file, their_file;
3494 mmbuffer_t result = { NULL };
3495 int status;
3497 /* resolve trivial cases first */
3498 if (oideq(base, ours))
3499 return resolve_to(image, theirs);
3500 else if (oideq(base, theirs) || oideq(ours, theirs))
3501 return resolve_to(image, ours);
3503 read_mmblob(&base_file, base);
3504 read_mmblob(&our_file, ours);
3505 read_mmblob(&their_file, theirs);
3506 status = ll_merge(&result, path,
3507 &base_file, "base",
3508 &our_file, "ours",
3509 &their_file, "theirs",
3510 state->repo->index,
3511 NULL);
3512 free(base_file.ptr);
3513 free(our_file.ptr);
3514 free(their_file.ptr);
3515 if (status < 0 || !result.ptr) {
3516 free(result.ptr);
3517 return -1;
3519 clear_image(image);
3520 image->buf = result.ptr;
3521 image->len = result.size;
3523 return status;
3527 * When directly falling back to add/add three-way merge, we read from
3528 * the current contents of the new_name. In no cases other than that
3529 * this function will be called.
3531 static int load_current(struct apply_state *state,
3532 struct image *image,
3533 struct patch *patch)
3535 struct strbuf buf = STRBUF_INIT;
3536 int status, pos;
3537 size_t len;
3538 char *img;
3539 struct stat st;
3540 struct cache_entry *ce;
3541 char *name = patch->new_name;
3542 unsigned mode = patch->new_mode;
3544 if (!patch->is_new)
3545 BUG("patch to %s is not a creation", patch->old_name);
3547 pos = index_name_pos(state->repo->index, name, strlen(name));
3548 if (pos < 0)
3549 return error(_("%s: does not exist in index"), name);
3550 ce = state->repo->index->cache[pos];
3551 if (lstat(name, &st)) {
3552 if (errno != ENOENT)
3553 return error_errno("%s", name);
3554 if (checkout_target(state->repo->index, ce, &st))
3555 return -1;
3557 if (verify_index_match(state, ce, &st))
3558 return error(_("%s: does not match index"), name);
3560 status = load_patch_target(state, &buf, ce, &st, patch, name, mode);
3561 if (status < 0)
3562 return status;
3563 else if (status)
3564 return -1;
3565 img = strbuf_detach(&buf, &len);
3566 prepare_image(image, img, len, !patch->is_binary);
3567 return 0;
3570 static int try_threeway(struct apply_state *state,
3571 struct image *image,
3572 struct patch *patch,
3573 struct stat *st,
3574 const struct cache_entry *ce)
3576 struct object_id pre_oid, post_oid, our_oid;
3577 struct strbuf buf = STRBUF_INIT;
3578 size_t len;
3579 int status;
3580 char *img;
3581 struct image tmp_image;
3583 /* No point falling back to 3-way merge in these cases */
3584 if (patch->is_delete ||
3585 S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3586 return -1;
3588 /* Preimage the patch was prepared for */
3589 if (patch->is_new)
3590 write_object_file("", 0, blob_type, &pre_oid);
3591 else if (get_oid(patch->old_oid_prefix, &pre_oid) ||
3592 read_blob_object(&buf, &pre_oid, patch->old_mode))
3593 return error(_("repository lacks the necessary blob to perform 3-way merge."));
3595 if (state->apply_verbosity > verbosity_silent && patch->direct_to_threeway)
3596 fprintf(stderr, _("Performing three-way merge...\n"));
3598 img = strbuf_detach(&buf, &len);
3599 prepare_image(&tmp_image, img, len, 1);
3600 /* Apply the patch to get the post image */
3601 if (apply_fragments(state, &tmp_image, patch) < 0) {
3602 clear_image(&tmp_image);
3603 return -1;
3605 /* post_oid is theirs */
3606 write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid);
3607 clear_image(&tmp_image);
3609 /* our_oid is ours */
3610 if (patch->is_new) {
3611 if (load_current(state, &tmp_image, patch))
3612 return error(_("cannot read the current contents of '%s'"),
3613 patch->new_name);
3614 } else {
3615 if (load_preimage(state, &tmp_image, patch, st, ce))
3616 return error(_("cannot read the current contents of '%s'"),
3617 patch->old_name);
3619 write_object_file(tmp_image.buf, tmp_image.len, blob_type, &our_oid);
3620 clear_image(&tmp_image);
3622 /* in-core three-way merge between post and our using pre as base */
3623 status = three_way_merge(state, image, patch->new_name,
3624 &pre_oid, &our_oid, &post_oid);
3625 if (status < 0) {
3626 if (state->apply_verbosity > verbosity_silent)
3627 fprintf(stderr,
3628 _("Failed to perform three-way merge...\n"));
3629 return status;
3632 if (status) {
3633 patch->conflicted_threeway = 1;
3634 if (patch->is_new)
3635 oidclr(&patch->threeway_stage[0]);
3636 else
3637 oidcpy(&patch->threeway_stage[0], &pre_oid);
3638 oidcpy(&patch->threeway_stage[1], &our_oid);
3639 oidcpy(&patch->threeway_stage[2], &post_oid);
3640 if (state->apply_verbosity > verbosity_silent)
3641 fprintf(stderr,
3642 _("Applied patch to '%s' with conflicts.\n"),
3643 patch->new_name);
3644 } else {
3645 if (state->apply_verbosity > verbosity_silent)
3646 fprintf(stderr,
3647 _("Applied patch to '%s' cleanly.\n"),
3648 patch->new_name);
3650 return 0;
3653 static int apply_data(struct apply_state *state, struct patch *patch,
3654 struct stat *st, const struct cache_entry *ce)
3656 struct image image;
3658 if (load_preimage(state, &image, patch, st, ce) < 0)
3659 return -1;
3661 if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0) {
3662 if (state->apply_verbosity > verbosity_silent &&
3663 state->threeway && !patch->direct_to_threeway)
3664 fprintf(stderr, _("Falling back to direct application...\n"));
3666 /* Note: with --reject, apply_fragments() returns 0 */
3667 if (patch->direct_to_threeway || apply_fragments(state, &image, patch) < 0)
3668 return -1;
3670 patch->result = image.buf;
3671 patch->resultsize = image.len;
3672 add_to_fn_table(state, patch);
3673 free(image.line_allocated);
3675 if (0 < patch->is_delete && patch->resultsize)
3676 return error(_("removal patch leaves file contents"));
3678 return 0;
3682 * If "patch" that we are looking at modifies or deletes what we have,
3683 * we would want it not to lose any local modification we have, either
3684 * in the working tree or in the index.
3686 * This also decides if a non-git patch is a creation patch or a
3687 * modification to an existing empty file. We do not check the state
3688 * of the current tree for a creation patch in this function; the caller
3689 * check_patch() separately makes sure (and errors out otherwise) that
3690 * the path the patch creates does not exist in the current tree.
3692 static int check_preimage(struct apply_state *state,
3693 struct patch *patch,
3694 struct cache_entry **ce,
3695 struct stat *st)
3697 const char *old_name = patch->old_name;
3698 struct patch *previous = NULL;
3699 int stat_ret = 0, status;
3700 unsigned st_mode = 0;
3702 if (!old_name)
3703 return 0;
3705 assert(patch->is_new <= 0);
3706 previous = previous_patch(state, patch, &status);
3708 if (status)
3709 return error(_("path %s has been renamed/deleted"), old_name);
3710 if (previous) {
3711 st_mode = previous->new_mode;
3712 } else if (!state->cached) {
3713 stat_ret = lstat(old_name, st);
3714 if (stat_ret && errno != ENOENT)
3715 return error_errno("%s", old_name);
3718 if (state->check_index && !previous) {
3719 int pos = index_name_pos(state->repo->index, old_name,
3720 strlen(old_name));
3721 if (pos < 0) {
3722 if (patch->is_new < 0)
3723 goto is_new;
3724 return error(_("%s: does not exist in index"), old_name);
3726 *ce = state->repo->index->cache[pos];
3727 if (stat_ret < 0) {
3728 if (checkout_target(state->repo->index, *ce, st))
3729 return -1;
3731 if (!state->cached && verify_index_match(state, *ce, st))
3732 return error(_("%s: does not match index"), old_name);
3733 if (state->cached)
3734 st_mode = (*ce)->ce_mode;
3735 } else if (stat_ret < 0) {
3736 if (patch->is_new < 0)
3737 goto is_new;
3738 return error_errno("%s", old_name);
3741 if (!state->cached && !previous)
3742 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3744 if (patch->is_new < 0)
3745 patch->is_new = 0;
3746 if (!patch->old_mode)
3747 patch->old_mode = st_mode;
3748 if ((st_mode ^ patch->old_mode) & S_IFMT)
3749 return error(_("%s: wrong type"), old_name);
3750 if (st_mode != patch->old_mode)
3751 warning(_("%s has type %o, expected %o"),
3752 old_name, st_mode, patch->old_mode);
3753 if (!patch->new_mode && !patch->is_delete)
3754 patch->new_mode = st_mode;
3755 return 0;
3757 is_new:
3758 patch->is_new = 1;
3759 patch->is_delete = 0;
3760 FREE_AND_NULL(patch->old_name);
3761 return 0;
3765 #define EXISTS_IN_INDEX 1
3766 #define EXISTS_IN_WORKTREE 2
3767 #define EXISTS_IN_INDEX_AS_ITA 3
3769 static int check_to_create(struct apply_state *state,
3770 const char *new_name,
3771 int ok_if_exists)
3773 struct stat nst;
3775 if (state->check_index && (!ok_if_exists || !state->cached)) {
3776 int pos;
3778 pos = index_name_pos(state->repo->index, new_name, strlen(new_name));
3779 if (pos >= 0) {
3780 struct cache_entry *ce = state->repo->index->cache[pos];
3782 /* allow ITA, as they do not yet exist in the index */
3783 if (!ok_if_exists && !(ce->ce_flags & CE_INTENT_TO_ADD))
3784 return EXISTS_IN_INDEX;
3786 /* ITA entries can never match working tree files */
3787 if (!state->cached && (ce->ce_flags & CE_INTENT_TO_ADD))
3788 return EXISTS_IN_INDEX_AS_ITA;
3792 if (state->cached)
3793 return 0;
3795 if (!lstat(new_name, &nst)) {
3796 if (S_ISDIR(nst.st_mode) || ok_if_exists)
3797 return 0;
3799 * A leading component of new_name might be a symlink
3800 * that is going to be removed with this patch, but
3801 * still pointing at somewhere that has the path.
3802 * In such a case, path "new_name" does not exist as
3803 * far as git is concerned.
3805 if (has_symlink_leading_path(new_name, strlen(new_name)))
3806 return 0;
3808 return EXISTS_IN_WORKTREE;
3809 } else if (!is_missing_file_error(errno)) {
3810 return error_errno("%s", new_name);
3812 return 0;
3815 static uintptr_t register_symlink_changes(struct apply_state *state,
3816 const char *path,
3817 uintptr_t what)
3819 struct string_list_item *ent;
3821 ent = string_list_lookup(&state->symlink_changes, path);
3822 if (!ent) {
3823 ent = string_list_insert(&state->symlink_changes, path);
3824 ent->util = (void *)0;
3826 ent->util = (void *)(what | ((uintptr_t)ent->util));
3827 return (uintptr_t)ent->util;
3830 static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3832 struct string_list_item *ent;
3834 ent = string_list_lookup(&state->symlink_changes, path);
3835 if (!ent)
3836 return 0;
3837 return (uintptr_t)ent->util;
3840 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3842 for ( ; patch; patch = patch->next) {
3843 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3844 (patch->is_rename || patch->is_delete))
3845 /* the symlink at patch->old_name is removed */
3846 register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3848 if (patch->new_name && S_ISLNK(patch->new_mode))
3849 /* the symlink at patch->new_name is created or remains */
3850 register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3854 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3856 do {
3857 unsigned int change;
3859 while (--name->len && name->buf[name->len] != '/')
3860 ; /* scan backwards */
3861 if (!name->len)
3862 break;
3863 name->buf[name->len] = '\0';
3864 change = check_symlink_changes(state, name->buf);
3865 if (change & APPLY_SYMLINK_IN_RESULT)
3866 return 1;
3867 if (change & APPLY_SYMLINK_GOES_AWAY)
3869 * This cannot be "return 0", because we may
3870 * see a new one created at a higher level.
3872 continue;
3874 /* otherwise, check the preimage */
3875 if (state->check_index) {
3876 struct cache_entry *ce;
3878 ce = index_file_exists(state->repo->index, name->buf,
3879 name->len, ignore_case);
3880 if (ce && S_ISLNK(ce->ce_mode))
3881 return 1;
3882 } else {
3883 struct stat st;
3884 if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3885 return 1;
3887 } while (1);
3888 return 0;
3891 static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3893 int ret;
3894 struct strbuf name = STRBUF_INIT;
3896 assert(*name_ != '\0');
3897 strbuf_addstr(&name, name_);
3898 ret = path_is_beyond_symlink_1(state, &name);
3899 strbuf_release(&name);
3901 return ret;
3904 static int check_unsafe_path(struct patch *patch)
3906 const char *old_name = NULL;
3907 const char *new_name = NULL;
3908 if (patch->is_delete)
3909 old_name = patch->old_name;
3910 else if (!patch->is_new && !patch->is_copy)
3911 old_name = patch->old_name;
3912 if (!patch->is_delete)
3913 new_name = patch->new_name;
3915 if (old_name && !verify_path(old_name, patch->old_mode))
3916 return error(_("invalid path '%s'"), old_name);
3917 if (new_name && !verify_path(new_name, patch->new_mode))
3918 return error(_("invalid path '%s'"), new_name);
3919 return 0;
3923 * Check and apply the patch in-core; leave the result in patch->result
3924 * for the caller to write it out to the final destination.
3926 static int check_patch(struct apply_state *state, struct patch *patch)
3928 struct stat st;
3929 const char *old_name = patch->old_name;
3930 const char *new_name = patch->new_name;
3931 const char *name = old_name ? old_name : new_name;
3932 struct cache_entry *ce = NULL;
3933 struct patch *tpatch;
3934 int ok_if_exists;
3935 int status;
3937 patch->rejected = 1; /* we will drop this after we succeed */
3939 status = check_preimage(state, patch, &ce, &st);
3940 if (status)
3941 return status;
3942 old_name = patch->old_name;
3945 * A type-change diff is always split into a patch to delete
3946 * old, immediately followed by a patch to create new (see
3947 * diff.c::run_diff()); in such a case it is Ok that the entry
3948 * to be deleted by the previous patch is still in the working
3949 * tree and in the index.
3951 * A patch to swap-rename between A and B would first rename A
3952 * to B and then rename B to A. While applying the first one,
3953 * the presence of B should not stop A from getting renamed to
3954 * B; ask to_be_deleted() about the later rename. Removal of
3955 * B and rename from A to B is handled the same way by asking
3956 * was_deleted().
3958 if ((tpatch = in_fn_table(state, new_name)) &&
3959 (was_deleted(tpatch) || to_be_deleted(tpatch)))
3960 ok_if_exists = 1;
3961 else
3962 ok_if_exists = 0;
3964 if (new_name &&
3965 ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3966 int err = check_to_create(state, new_name, ok_if_exists);
3968 if (err && state->threeway) {
3969 patch->direct_to_threeway = 1;
3970 } else switch (err) {
3971 case 0:
3972 break; /* happy */
3973 case EXISTS_IN_INDEX:
3974 return error(_("%s: already exists in index"), new_name);
3975 case EXISTS_IN_INDEX_AS_ITA:
3976 return error(_("%s: does not match index"), new_name);
3977 case EXISTS_IN_WORKTREE:
3978 return error(_("%s: already exists in working directory"),
3979 new_name);
3980 default:
3981 return err;
3984 if (!patch->new_mode) {
3985 if (0 < patch->is_new)
3986 patch->new_mode = S_IFREG | 0644;
3987 else
3988 patch->new_mode = patch->old_mode;
3992 if (new_name && old_name) {
3993 int same = !strcmp(old_name, new_name);
3994 if (!patch->new_mode)
3995 patch->new_mode = patch->old_mode;
3996 if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3997 if (same)
3998 return error(_("new mode (%o) of %s does not "
3999 "match old mode (%o)"),
4000 patch->new_mode, new_name,
4001 patch->old_mode);
4002 else
4003 return error(_("new mode (%o) of %s does not "
4004 "match old mode (%o) of %s"),
4005 patch->new_mode, new_name,
4006 patch->old_mode, old_name);
4010 if (!state->unsafe_paths && check_unsafe_path(patch))
4011 return -128;
4014 * An attempt to read from or delete a path that is beyond a
4015 * symbolic link will be prevented by load_patch_target() that
4016 * is called at the beginning of apply_data() so we do not
4017 * have to worry about a patch marked with "is_delete" bit
4018 * here. We however need to make sure that the patch result
4019 * is not deposited to a path that is beyond a symbolic link
4020 * here.
4022 if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
4023 return error(_("affected file '%s' is beyond a symbolic link"),
4024 patch->new_name);
4026 if (apply_data(state, patch, &st, ce) < 0)
4027 return error(_("%s: patch does not apply"), name);
4028 patch->rejected = 0;
4029 return 0;
4032 static int check_patch_list(struct apply_state *state, struct patch *patch)
4034 int err = 0;
4036 prepare_symlink_changes(state, patch);
4037 prepare_fn_table(state, patch);
4038 while (patch) {
4039 int res;
4040 if (state->apply_verbosity > verbosity_normal)
4041 say_patch_name(stderr,
4042 _("Checking patch %s..."), patch);
4043 res = check_patch(state, patch);
4044 if (res == -128)
4045 return -128;
4046 err |= res;
4047 patch = patch->next;
4049 return err;
4052 static int read_apply_cache(struct apply_state *state)
4054 if (state->index_file)
4055 return read_index_from(state->repo->index, state->index_file,
4056 get_git_dir());
4057 else
4058 return repo_read_index(state->repo);
4061 /* This function tries to read the object name from the current index */
4062 static int get_current_oid(struct apply_state *state, const char *path,
4063 struct object_id *oid)
4065 int pos;
4067 if (read_apply_cache(state) < 0)
4068 return -1;
4069 pos = index_name_pos(state->repo->index, path, strlen(path));
4070 if (pos < 0)
4071 return -1;
4072 oidcpy(oid, &state->repo->index->cache[pos]->oid);
4073 return 0;
4076 static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
4079 * A usable gitlink patch has only one fragment (hunk) that looks like:
4080 * @@ -1 +1 @@
4081 * -Subproject commit <old sha1>
4082 * +Subproject commit <new sha1>
4083 * or
4084 * @@ -1 +0,0 @@
4085 * -Subproject commit <old sha1>
4086 * for a removal patch.
4088 struct fragment *hunk = p->fragments;
4089 static const char heading[] = "-Subproject commit ";
4090 char *preimage;
4092 if (/* does the patch have only one hunk? */
4093 hunk && !hunk->next &&
4094 /* is its preimage one line? */
4095 hunk->oldpos == 1 && hunk->oldlines == 1 &&
4096 /* does preimage begin with the heading? */
4097 (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
4098 starts_with(++preimage, heading) &&
4099 /* does it record full SHA-1? */
4100 !get_oid_hex(preimage + sizeof(heading) - 1, oid) &&
4101 preimage[sizeof(heading) + the_hash_algo->hexsz - 1] == '\n' &&
4102 /* does the abbreviated name on the index line agree with it? */
4103 starts_with(preimage + sizeof(heading) - 1, p->old_oid_prefix))
4104 return 0; /* it all looks fine */
4106 /* we may have full object name on the index line */
4107 return get_oid_hex(p->old_oid_prefix, oid);
4110 /* Build an index that contains just the files needed for a 3way merge */
4111 static int build_fake_ancestor(struct apply_state *state, struct patch *list)
4113 struct patch *patch;
4114 struct index_state result = { NULL };
4115 struct lock_file lock = LOCK_INIT;
4116 int res;
4118 /* Once we start supporting the reverse patch, it may be
4119 * worth showing the new sha1 prefix, but until then...
4121 for (patch = list; patch; patch = patch->next) {
4122 struct object_id oid;
4123 struct cache_entry *ce;
4124 const char *name;
4126 name = patch->old_name ? patch->old_name : patch->new_name;
4127 if (0 < patch->is_new)
4128 continue;
4130 if (S_ISGITLINK(patch->old_mode)) {
4131 if (!preimage_oid_in_gitlink_patch(patch, &oid))
4132 ; /* ok, the textual part looks sane */
4133 else
4134 return error(_("sha1 information is lacking or "
4135 "useless for submodule %s"), name);
4136 } else if (!get_oid_blob(patch->old_oid_prefix, &oid)) {
4137 ; /* ok */
4138 } else if (!patch->lines_added && !patch->lines_deleted) {
4139 /* mode-only change: update the current */
4140 if (get_current_oid(state, patch->old_name, &oid))
4141 return error(_("mode change for %s, which is not "
4142 "in current HEAD"), name);
4143 } else
4144 return error(_("sha1 information is lacking or useless "
4145 "(%s)."), name);
4147 ce = make_cache_entry(&result, patch->old_mode, &oid, name, 0, 0);
4148 if (!ce)
4149 return error(_("make_cache_entry failed for path '%s'"),
4150 name);
4151 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
4152 discard_cache_entry(ce);
4153 return error(_("could not add %s to temporary index"),
4154 name);
4158 hold_lock_file_for_update(&lock, state->fake_ancestor, LOCK_DIE_ON_ERROR);
4159 res = write_locked_index(&result, &lock, COMMIT_LOCK);
4160 discard_index(&result);
4162 if (res)
4163 return error(_("could not write temporary index to %s"),
4164 state->fake_ancestor);
4166 return 0;
4169 static void stat_patch_list(struct apply_state *state, struct patch *patch)
4171 int files, adds, dels;
4173 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
4174 files++;
4175 adds += patch->lines_added;
4176 dels += patch->lines_deleted;
4177 show_stats(state, patch);
4180 print_stat_summary(stdout, files, adds, dels);
4183 static void numstat_patch_list(struct apply_state *state,
4184 struct patch *patch)
4186 for ( ; patch; patch = patch->next) {
4187 const char *name;
4188 name = patch->new_name ? patch->new_name : patch->old_name;
4189 if (patch->is_binary)
4190 printf("-\t-\t");
4191 else
4192 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
4193 write_name_quoted(name, stdout, state->line_termination);
4197 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
4199 if (mode)
4200 printf(" %s mode %06o %s\n", newdelete, mode, name);
4201 else
4202 printf(" %s %s\n", newdelete, name);
4205 static void show_mode_change(struct patch *p, int show_name)
4207 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
4208 if (show_name)
4209 printf(" mode change %06o => %06o %s\n",
4210 p->old_mode, p->new_mode, p->new_name);
4211 else
4212 printf(" mode change %06o => %06o\n",
4213 p->old_mode, p->new_mode);
4217 static void show_rename_copy(struct patch *p)
4219 const char *renamecopy = p->is_rename ? "rename" : "copy";
4220 const char *old_name, *new_name;
4222 /* Find common prefix */
4223 old_name = p->old_name;
4224 new_name = p->new_name;
4225 while (1) {
4226 const char *slash_old, *slash_new;
4227 slash_old = strchr(old_name, '/');
4228 slash_new = strchr(new_name, '/');
4229 if (!slash_old ||
4230 !slash_new ||
4231 slash_old - old_name != slash_new - new_name ||
4232 memcmp(old_name, new_name, slash_new - new_name))
4233 break;
4234 old_name = slash_old + 1;
4235 new_name = slash_new + 1;
4237 /* p->old_name through old_name is the common prefix, and old_name and
4238 * new_name through the end of names are renames
4240 if (old_name != p->old_name)
4241 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4242 (int)(old_name - p->old_name), p->old_name,
4243 old_name, new_name, p->score);
4244 else
4245 printf(" %s %s => %s (%d%%)\n", renamecopy,
4246 p->old_name, p->new_name, p->score);
4247 show_mode_change(p, 0);
4250 static void summary_patch_list(struct patch *patch)
4252 struct patch *p;
4254 for (p = patch; p; p = p->next) {
4255 if (p->is_new)
4256 show_file_mode_name("create", p->new_mode, p->new_name);
4257 else if (p->is_delete)
4258 show_file_mode_name("delete", p->old_mode, p->old_name);
4259 else {
4260 if (p->is_rename || p->is_copy)
4261 show_rename_copy(p);
4262 else {
4263 if (p->score) {
4264 printf(" rewrite %s (%d%%)\n",
4265 p->new_name, p->score);
4266 show_mode_change(p, 0);
4268 else
4269 show_mode_change(p, 1);
4275 static void patch_stats(struct apply_state *state, struct patch *patch)
4277 int lines = patch->lines_added + patch->lines_deleted;
4279 if (lines > state->max_change)
4280 state->max_change = lines;
4281 if (patch->old_name) {
4282 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4283 if (!len)
4284 len = strlen(patch->old_name);
4285 if (len > state->max_len)
4286 state->max_len = len;
4288 if (patch->new_name) {
4289 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4290 if (!len)
4291 len = strlen(patch->new_name);
4292 if (len > state->max_len)
4293 state->max_len = len;
4297 static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4299 if (state->update_index && !state->ita_only) {
4300 if (remove_file_from_index(state->repo->index, patch->old_name) < 0)
4301 return error(_("unable to remove %s from index"), patch->old_name);
4303 if (!state->cached) {
4304 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4305 remove_path(patch->old_name);
4308 return 0;
4311 static int add_index_file(struct apply_state *state,
4312 const char *path,
4313 unsigned mode,
4314 void *buf,
4315 unsigned long size)
4317 struct stat st;
4318 struct cache_entry *ce;
4319 int namelen = strlen(path);
4321 ce = make_empty_cache_entry(state->repo->index, namelen);
4322 memcpy(ce->name, path, namelen);
4323 ce->ce_mode = create_ce_mode(mode);
4324 ce->ce_flags = create_ce_flags(0);
4325 ce->ce_namelen = namelen;
4326 if (state->ita_only) {
4327 ce->ce_flags |= CE_INTENT_TO_ADD;
4328 set_object_name_for_intent_to_add_entry(ce);
4329 } else if (S_ISGITLINK(mode)) {
4330 const char *s;
4332 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4333 get_oid_hex(s, &ce->oid)) {
4334 discard_cache_entry(ce);
4335 return error(_("corrupt patch for submodule %s"), path);
4337 } else {
4338 if (!state->cached) {
4339 if (lstat(path, &st) < 0) {
4340 discard_cache_entry(ce);
4341 return error_errno(_("unable to stat newly "
4342 "created file '%s'"),
4343 path);
4345 fill_stat_cache_info(state->repo->index, ce, &st);
4347 if (write_object_file(buf, size, blob_type, &ce->oid) < 0) {
4348 discard_cache_entry(ce);
4349 return error(_("unable to create backing store "
4350 "for newly created file %s"), path);
4353 if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) {
4354 discard_cache_entry(ce);
4355 return error(_("unable to add cache entry for %s"), path);
4358 return 0;
4362 * Returns:
4363 * -1 if an unrecoverable error happened
4364 * 0 if everything went well
4365 * 1 if a recoverable error happened
4367 static int try_create_file(struct apply_state *state, const char *path,
4368 unsigned int mode, const char *buf,
4369 unsigned long size)
4371 int fd, res;
4372 struct strbuf nbuf = STRBUF_INIT;
4374 if (S_ISGITLINK(mode)) {
4375 struct stat st;
4376 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4377 return 0;
4378 return !!mkdir(path, 0777);
4381 if (has_symlinks && S_ISLNK(mode))
4382 /* Although buf:size is counted string, it also is NUL
4383 * terminated.
4385 return !!symlink(buf, path);
4387 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4388 if (fd < 0)
4389 return 1;
4391 if (convert_to_working_tree(state->repo->index, path, buf, size, &nbuf, NULL)) {
4392 size = nbuf.len;
4393 buf = nbuf.buf;
4396 res = write_in_full(fd, buf, size) < 0;
4397 if (res)
4398 error_errno(_("failed to write to '%s'"), path);
4399 strbuf_release(&nbuf);
4401 if (close(fd) < 0 && !res)
4402 return error_errno(_("closing file '%s'"), path);
4404 return res ? -1 : 0;
4408 * We optimistically assume that the directories exist,
4409 * which is true 99% of the time anyway. If they don't,
4410 * we create them and try again.
4412 * Returns:
4413 * -1 on error
4414 * 0 otherwise
4416 static int create_one_file(struct apply_state *state,
4417 char *path,
4418 unsigned mode,
4419 const char *buf,
4420 unsigned long size)
4422 int res;
4424 if (state->cached)
4425 return 0;
4428 * We already try to detect whether files are beyond a symlink in our
4429 * up-front checks. But in the case where symlinks are created by any
4430 * of the intermediate hunks it can happen that our up-front checks
4431 * didn't yet see the symlink, but at the point of arriving here there
4432 * in fact is one. We thus repeat the check for symlinks here.
4434 * Note that this does not make the up-front check obsolete as the
4435 * failure mode is different:
4437 * - The up-front checks cause us to abort before we have written
4438 * anything into the working directory. So when we exit this way the
4439 * working directory remains clean.
4441 * - The checks here happen in the middle of the action where we have
4442 * already started to apply the patch. The end result will be a dirty
4443 * working directory.
4445 * Ideally, we should update the up-front checks to catch what would
4446 * happen when we apply the patch before we damage the working tree.
4447 * We have all the information necessary to do so. But for now, as a
4448 * part of embargoed security work, having this check would serve as a
4449 * reasonable first step.
4451 if (path_is_beyond_symlink(state, path))
4452 return error(_("affected file '%s' is beyond a symbolic link"), path);
4454 res = try_create_file(state, path, mode, buf, size);
4455 if (res < 0)
4456 return -1;
4457 if (!res)
4458 return 0;
4460 if (errno == ENOENT) {
4461 if (safe_create_leading_directories_no_share(path))
4462 return 0;
4463 res = try_create_file(state, path, mode, buf, size);
4464 if (res < 0)
4465 return -1;
4466 if (!res)
4467 return 0;
4470 if (errno == EEXIST || errno == EACCES) {
4471 /* We may be trying to create a file where a directory
4472 * used to be.
4474 struct stat st;
4475 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4476 errno = EEXIST;
4479 if (errno == EEXIST) {
4480 unsigned int nr = getpid();
4482 for (;;) {
4483 char newpath[PATH_MAX];
4484 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4485 res = try_create_file(state, newpath, mode, buf, size);
4486 if (res < 0)
4487 return -1;
4488 if (!res) {
4489 if (!rename(newpath, path))
4490 return 0;
4491 unlink_or_warn(newpath);
4492 break;
4494 if (errno != EEXIST)
4495 break;
4496 ++nr;
4499 return error_errno(_("unable to write file '%s' mode %o"),
4500 path, mode);
4503 static int add_conflicted_stages_file(struct apply_state *state,
4504 struct patch *patch)
4506 int stage, namelen;
4507 unsigned mode;
4508 struct cache_entry *ce;
4510 if (!state->update_index)
4511 return 0;
4512 namelen = strlen(patch->new_name);
4513 mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4515 remove_file_from_index(state->repo->index, patch->new_name);
4516 for (stage = 1; stage < 4; stage++) {
4517 if (is_null_oid(&patch->threeway_stage[stage - 1]))
4518 continue;
4519 ce = make_empty_cache_entry(state->repo->index, namelen);
4520 memcpy(ce->name, patch->new_name, namelen);
4521 ce->ce_mode = create_ce_mode(mode);
4522 ce->ce_flags = create_ce_flags(stage);
4523 ce->ce_namelen = namelen;
4524 oidcpy(&ce->oid, &patch->threeway_stage[stage - 1]);
4525 if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) {
4526 discard_cache_entry(ce);
4527 return error(_("unable to add cache entry for %s"),
4528 patch->new_name);
4532 return 0;
4535 static int create_file(struct apply_state *state, struct patch *patch)
4537 char *path = patch->new_name;
4538 unsigned mode = patch->new_mode;
4539 unsigned long size = patch->resultsize;
4540 char *buf = patch->result;
4542 if (!mode)
4543 mode = S_IFREG | 0644;
4544 if (create_one_file(state, path, mode, buf, size))
4545 return -1;
4547 if (patch->conflicted_threeway)
4548 return add_conflicted_stages_file(state, patch);
4549 else if (state->update_index)
4550 return add_index_file(state, path, mode, buf, size);
4551 return 0;
4554 /* phase zero is to remove, phase one is to create */
4555 static int write_out_one_result(struct apply_state *state,
4556 struct patch *patch,
4557 int phase)
4559 if (patch->is_delete > 0) {
4560 if (phase == 0)
4561 return remove_file(state, patch, 1);
4562 return 0;
4564 if (patch->is_new > 0 || patch->is_copy) {
4565 if (phase == 1)
4566 return create_file(state, patch);
4567 return 0;
4570 * Rename or modification boils down to the same
4571 * thing: remove the old, write the new
4573 if (phase == 0)
4574 return remove_file(state, patch, patch->is_rename);
4575 if (phase == 1)
4576 return create_file(state, patch);
4577 return 0;
4580 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4582 FILE *rej;
4583 char namebuf[PATH_MAX];
4584 struct fragment *frag;
4585 int fd, cnt = 0;
4586 struct strbuf sb = STRBUF_INIT;
4588 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4589 if (!frag->rejected)
4590 continue;
4591 cnt++;
4594 if (!cnt) {
4595 if (state->apply_verbosity > verbosity_normal)
4596 say_patch_name(stderr,
4597 _("Applied patch %s cleanly."), patch);
4598 return 0;
4601 /* This should not happen, because a removal patch that leaves
4602 * contents are marked "rejected" at the patch level.
4604 if (!patch->new_name)
4605 die(_("internal error"));
4607 /* Say this even without --verbose */
4608 strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4609 "Applying patch %%s with %d rejects...",
4610 cnt),
4611 cnt);
4612 if (state->apply_verbosity > verbosity_silent)
4613 say_patch_name(stderr, sb.buf, patch);
4614 strbuf_release(&sb);
4616 cnt = strlen(patch->new_name);
4617 if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4618 cnt = ARRAY_SIZE(namebuf) - 5;
4619 warning(_("truncating .rej filename to %.*s.rej"),
4620 cnt - 1, patch->new_name);
4622 memcpy(namebuf, patch->new_name, cnt);
4623 memcpy(namebuf + cnt, ".rej", 5);
4625 fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666);
4626 if (fd < 0) {
4627 if (errno != EEXIST)
4628 return error_errno(_("cannot open %s"), namebuf);
4629 if (unlink(namebuf))
4630 return error_errno(_("cannot unlink '%s'"), namebuf);
4631 fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666);
4632 if (fd < 0)
4633 return error_errno(_("cannot open %s"), namebuf);
4635 rej = fdopen(fd, "w");
4636 if (!rej)
4637 return error_errno(_("cannot open %s"), namebuf);
4639 /* Normal git tools never deal with .rej, so do not pretend
4640 * this is a git patch by saying --git or giving extended
4641 * headers. While at it, maybe please "kompare" that wants
4642 * the trailing TAB and some garbage at the end of line ;-).
4644 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4645 patch->new_name, patch->new_name);
4646 for (cnt = 1, frag = patch->fragments;
4647 frag;
4648 cnt++, frag = frag->next) {
4649 if (!frag->rejected) {
4650 if (state->apply_verbosity > verbosity_silent)
4651 fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4652 continue;
4654 if (state->apply_verbosity > verbosity_silent)
4655 fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4656 fprintf(rej, "%.*s", frag->size, frag->patch);
4657 if (frag->patch[frag->size-1] != '\n')
4658 fputc('\n', rej);
4660 fclose(rej);
4661 return -1;
4665 * Returns:
4666 * -1 if an error happened
4667 * 0 if the patch applied cleanly
4668 * 1 if the patch did not apply cleanly
4670 static int write_out_results(struct apply_state *state, struct patch *list)
4672 int phase;
4673 int errs = 0;
4674 struct patch *l;
4675 struct string_list cpath = STRING_LIST_INIT_DUP;
4677 for (phase = 0; phase < 2; phase++) {
4678 l = list;
4679 while (l) {
4680 if (l->rejected)
4681 errs = 1;
4682 else {
4683 if (write_out_one_result(state, l, phase)) {
4684 string_list_clear(&cpath, 0);
4685 return -1;
4687 if (phase == 1) {
4688 if (write_out_one_reject(state, l))
4689 errs = 1;
4690 if (l->conflicted_threeway) {
4691 string_list_append(&cpath, l->new_name);
4692 errs = 1;
4696 l = l->next;
4700 if (cpath.nr) {
4701 struct string_list_item *item;
4703 string_list_sort(&cpath);
4704 if (state->apply_verbosity > verbosity_silent) {
4705 for_each_string_list_item(item, &cpath)
4706 fprintf(stderr, "U %s\n", item->string);
4708 string_list_clear(&cpath, 0);
4711 * rerere relies on the partially merged result being in the working
4712 * tree with conflict markers, but that isn't written with --cached.
4714 if (!state->cached)
4715 repo_rerere(state->repo, 0);
4718 return errs;
4722 * Try to apply a patch.
4724 * Returns:
4725 * -128 if a bad error happened (like patch unreadable)
4726 * -1 if patch did not apply and user cannot deal with it
4727 * 0 if the patch applied
4728 * 1 if the patch did not apply but user might fix it
4730 static int apply_patch(struct apply_state *state,
4731 int fd,
4732 const char *filename,
4733 int options)
4735 size_t offset;
4736 struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4737 struct patch *list = NULL, **listp = &list;
4738 int skipped_patch = 0;
4739 int res = 0;
4740 int flush_attributes = 0;
4742 state->patch_input_file = filename;
4743 if (read_patch_file(&buf, fd) < 0)
4744 return -128;
4745 offset = 0;
4746 while (offset < buf.len) {
4747 struct patch *patch;
4748 int nr;
4750 CALLOC_ARRAY(patch, 1);
4751 patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
4752 patch->recount = !!(options & APPLY_OPT_RECOUNT);
4753 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4754 if (nr < 0) {
4755 free_patch(patch);
4756 if (nr == -128) {
4757 res = -128;
4758 goto end;
4760 break;
4762 if (state->apply_in_reverse)
4763 reverse_patches(patch);
4764 if (use_patch(state, patch)) {
4765 patch_stats(state, patch);
4766 if (!list || !state->apply_in_reverse) {
4767 *listp = patch;
4768 listp = &patch->next;
4769 } else {
4770 patch->next = list;
4771 list = patch;
4774 if ((patch->new_name &&
4775 ends_with_path_components(patch->new_name,
4776 GITATTRIBUTES_FILE)) ||
4777 (patch->old_name &&
4778 ends_with_path_components(patch->old_name,
4779 GITATTRIBUTES_FILE)))
4780 flush_attributes = 1;
4782 else {
4783 if (state->apply_verbosity > verbosity_normal)
4784 say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4785 free_patch(patch);
4786 skipped_patch++;
4788 offset += nr;
4791 if (!list && !skipped_patch) {
4792 error(_("unrecognized input"));
4793 res = -128;
4794 goto end;
4797 if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4798 state->apply = 0;
4800 state->update_index = (state->check_index || state->ita_only) && state->apply;
4801 if (state->update_index && !is_lock_file_locked(&state->lock_file)) {
4802 if (state->index_file)
4803 hold_lock_file_for_update(&state->lock_file,
4804 state->index_file,
4805 LOCK_DIE_ON_ERROR);
4806 else
4807 repo_hold_locked_index(state->repo, &state->lock_file,
4808 LOCK_DIE_ON_ERROR);
4811 if (state->check_index && read_apply_cache(state) < 0) {
4812 error(_("unable to read index file"));
4813 res = -128;
4814 goto end;
4817 if (state->check || state->apply) {
4818 int r = check_patch_list(state, list);
4819 if (r == -128) {
4820 res = -128;
4821 goto end;
4823 if (r < 0 && !state->apply_with_reject) {
4824 res = -1;
4825 goto end;
4829 if (state->apply) {
4830 int write_res = write_out_results(state, list);
4831 if (write_res < 0) {
4832 res = -128;
4833 goto end;
4835 if (write_res > 0) {
4836 /* with --3way, we still need to write the index out */
4837 res = state->apply_with_reject ? -1 : 1;
4838 goto end;
4842 if (state->fake_ancestor &&
4843 build_fake_ancestor(state, list)) {
4844 res = -128;
4845 goto end;
4848 if (state->diffstat && state->apply_verbosity > verbosity_silent)
4849 stat_patch_list(state, list);
4851 if (state->numstat && state->apply_verbosity > verbosity_silent)
4852 numstat_patch_list(state, list);
4854 if (state->summary && state->apply_verbosity > verbosity_silent)
4855 summary_patch_list(list);
4857 if (flush_attributes)
4858 reset_parsed_attributes();
4859 end:
4860 free_patch_list(list);
4861 strbuf_release(&buf);
4862 string_list_clear(&state->fn_table, 0);
4863 return res;
4866 static int apply_option_parse_exclude(const struct option *opt,
4867 const char *arg, int unset)
4869 struct apply_state *state = opt->value;
4871 BUG_ON_OPT_NEG(unset);
4873 add_name_limit(state, arg, 1);
4874 return 0;
4877 static int apply_option_parse_include(const struct option *opt,
4878 const char *arg, int unset)
4880 struct apply_state *state = opt->value;
4882 BUG_ON_OPT_NEG(unset);
4884 add_name_limit(state, arg, 0);
4885 state->has_include = 1;
4886 return 0;
4889 static int apply_option_parse_p(const struct option *opt,
4890 const char *arg,
4891 int unset)
4893 struct apply_state *state = opt->value;
4895 BUG_ON_OPT_NEG(unset);
4897 state->p_value = atoi(arg);
4898 state->p_value_known = 1;
4899 return 0;
4902 static int apply_option_parse_space_change(const struct option *opt,
4903 const char *arg, int unset)
4905 struct apply_state *state = opt->value;
4907 BUG_ON_OPT_ARG(arg);
4909 if (unset)
4910 state->ws_ignore_action = ignore_ws_none;
4911 else
4912 state->ws_ignore_action = ignore_ws_change;
4913 return 0;
4916 static int apply_option_parse_whitespace(const struct option *opt,
4917 const char *arg, int unset)
4919 struct apply_state *state = opt->value;
4921 BUG_ON_OPT_NEG(unset);
4923 state->whitespace_option = arg;
4924 if (parse_whitespace_option(state, arg))
4925 return -1;
4926 return 0;
4929 static int apply_option_parse_directory(const struct option *opt,
4930 const char *arg, int unset)
4932 struct apply_state *state = opt->value;
4934 BUG_ON_OPT_NEG(unset);
4936 strbuf_reset(&state->root);
4937 strbuf_addstr(&state->root, arg);
4938 strbuf_complete(&state->root, '/');
4939 return 0;
4942 int apply_all_patches(struct apply_state *state,
4943 int argc,
4944 const char **argv,
4945 int options)
4947 int i;
4948 int res;
4949 int errs = 0;
4950 int read_stdin = 1;
4952 for (i = 0; i < argc; i++) {
4953 const char *arg = argv[i];
4954 char *to_free = NULL;
4955 int fd;
4957 if (!strcmp(arg, "-")) {
4958 res = apply_patch(state, 0, "<stdin>", options);
4959 if (res < 0)
4960 goto end;
4961 errs |= res;
4962 read_stdin = 0;
4963 continue;
4964 } else
4965 arg = to_free = prefix_filename(state->prefix, arg);
4967 fd = open(arg, O_RDONLY);
4968 if (fd < 0) {
4969 error(_("can't open patch '%s': %s"), arg, strerror(errno));
4970 res = -128;
4971 free(to_free);
4972 goto end;
4974 read_stdin = 0;
4975 set_default_whitespace_mode(state);
4976 res = apply_patch(state, fd, arg, options);
4977 close(fd);
4978 free(to_free);
4979 if (res < 0)
4980 goto end;
4981 errs |= res;
4983 set_default_whitespace_mode(state);
4984 if (read_stdin) {
4985 res = apply_patch(state, 0, "<stdin>", options);
4986 if (res < 0)
4987 goto end;
4988 errs |= res;
4991 if (state->whitespace_error) {
4992 if (state->squelch_whitespace_errors &&
4993 state->squelch_whitespace_errors < state->whitespace_error) {
4994 int squelched =
4995 state->whitespace_error - state->squelch_whitespace_errors;
4996 warning(Q_("squelched %d whitespace error",
4997 "squelched %d whitespace errors",
4998 squelched),
4999 squelched);
5001 if (state->ws_error_action == die_on_ws_error) {
5002 error(Q_("%d line adds whitespace errors.",
5003 "%d lines add whitespace errors.",
5004 state->whitespace_error),
5005 state->whitespace_error);
5006 res = -128;
5007 goto end;
5009 if (state->applied_after_fixing_ws && state->apply)
5010 warning(Q_("%d line applied after"
5011 " fixing whitespace errors.",
5012 "%d lines applied after"
5013 " fixing whitespace errors.",
5014 state->applied_after_fixing_ws),
5015 state->applied_after_fixing_ws);
5016 else if (state->whitespace_error)
5017 warning(Q_("%d line adds whitespace errors.",
5018 "%d lines add whitespace errors.",
5019 state->whitespace_error),
5020 state->whitespace_error);
5023 if (state->update_index) {
5024 res = write_locked_index(state->repo->index, &state->lock_file, COMMIT_LOCK);
5025 if (res) {
5026 error(_("Unable to write new index file"));
5027 res = -128;
5028 goto end;
5032 res = !!errs;
5034 end:
5035 rollback_lock_file(&state->lock_file);
5037 if (state->apply_verbosity <= verbosity_silent) {
5038 set_error_routine(state->saved_error_routine);
5039 set_warn_routine(state->saved_warn_routine);
5042 if (res > -1)
5043 return res;
5044 return (res == -1 ? 1 : 128);
5047 int apply_parse_options(int argc, const char **argv,
5048 struct apply_state *state,
5049 int *force_apply, int *options,
5050 const char * const *apply_usage)
5052 struct option builtin_apply_options[] = {
5053 OPT_CALLBACK_F(0, "exclude", state, N_("path"),
5054 N_("don't apply changes matching the given path"),
5055 PARSE_OPT_NONEG, apply_option_parse_exclude),
5056 OPT_CALLBACK_F(0, "include", state, N_("path"),
5057 N_("apply changes matching the given path"),
5058 PARSE_OPT_NONEG, apply_option_parse_include),
5059 OPT_CALLBACK('p', NULL, state, N_("num"),
5060 N_("remove <num> leading slashes from traditional diff paths"),
5061 apply_option_parse_p),
5062 OPT_BOOL(0, "no-add", &state->no_add,
5063 N_("ignore additions made by the patch")),
5064 OPT_BOOL(0, "stat", &state->diffstat,
5065 N_("instead of applying the patch, output diffstat for the input")),
5066 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
5067 OPT_NOOP_NOARG(0, "binary"),
5068 OPT_BOOL(0, "numstat", &state->numstat,
5069 N_("show number of added and deleted lines in decimal notation")),
5070 OPT_BOOL(0, "summary", &state->summary,
5071 N_("instead of applying the patch, output a summary for the input")),
5072 OPT_BOOL(0, "check", &state->check,
5073 N_("instead of applying the patch, see if the patch is applicable")),
5074 OPT_BOOL(0, "index", &state->check_index,
5075 N_("make sure the patch is applicable to the current index")),
5076 OPT_BOOL('N', "intent-to-add", &state->ita_only,
5077 N_("mark new files with `git add --intent-to-add`")),
5078 OPT_BOOL(0, "cached", &state->cached,
5079 N_("apply a patch without touching the working tree")),
5080 OPT_BOOL_F(0, "unsafe-paths", &state->unsafe_paths,
5081 N_("accept a patch that touches outside the working area"),
5082 PARSE_OPT_NOCOMPLETE),
5083 OPT_BOOL(0, "apply", force_apply,
5084 N_("also apply the patch (use with --stat/--summary/--check)")),
5085 OPT_BOOL('3', "3way", &state->threeway,
5086 N_( "attempt three-way merge, fall back on normal patch if that fails")),
5087 OPT_FILENAME(0, "build-fake-ancestor", &state->fake_ancestor,
5088 N_("build a temporary index based on embedded index information")),
5089 /* Think twice before adding "--nul" synonym to this */
5090 OPT_SET_INT('z', NULL, &state->line_termination,
5091 N_("paths are separated with NUL character"), '\0'),
5092 OPT_INTEGER('C', NULL, &state->p_context,
5093 N_("ensure at least <n> lines of context match")),
5094 OPT_CALLBACK(0, "whitespace", state, N_("action"),
5095 N_("detect new or modified lines that have whitespace errors"),
5096 apply_option_parse_whitespace),
5097 OPT_CALLBACK_F(0, "ignore-space-change", state, NULL,
5098 N_("ignore changes in whitespace when finding context"),
5099 PARSE_OPT_NOARG, apply_option_parse_space_change),
5100 OPT_CALLBACK_F(0, "ignore-whitespace", state, NULL,
5101 N_("ignore changes in whitespace when finding context"),
5102 PARSE_OPT_NOARG, apply_option_parse_space_change),
5103 OPT_BOOL('R', "reverse", &state->apply_in_reverse,
5104 N_("apply the patch in reverse")),
5105 OPT_BOOL(0, "unidiff-zero", &state->unidiff_zero,
5106 N_("don't expect at least one line of context")),
5107 OPT_BOOL(0, "reject", &state->apply_with_reject,
5108 N_("leave the rejected hunks in corresponding *.rej files")),
5109 OPT_BOOL(0, "allow-overlap", &state->allow_overlap,
5110 N_("allow overlapping hunks")),
5111 OPT__VERBOSE(&state->apply_verbosity, N_("be verbose")),
5112 OPT_BIT(0, "inaccurate-eof", options,
5113 N_("tolerate incorrectly detected missing new-line at the end of file"),
5114 APPLY_OPT_INACCURATE_EOF),
5115 OPT_BIT(0, "recount", options,
5116 N_("do not trust the line counts in the hunk headers"),
5117 APPLY_OPT_RECOUNT),
5118 OPT_CALLBACK(0, "directory", state, N_("root"),
5119 N_("prepend <root> to all filenames"),
5120 apply_option_parse_directory),
5121 OPT_END()
5124 return parse_options(argc, argv, state->prefix, builtin_apply_options, apply_usage, 0);