4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
11 #include "cache-tree.h"
16 #include "string-list.h"
19 #include "parse-options.h"
20 #include "xdiff-interface.h"
25 static const char * const apply_usage[] = {
26 N_("git apply [<options>] [<patch>...]"),
30 static void parse_whitespace_option(struct apply_state *state, const char *option)
33 state->ws_error_action = warn_on_ws_error;
36 if (!strcmp(option, "warn")) {
37 state->ws_error_action = warn_on_ws_error;
40 if (!strcmp(option, "nowarn")) {
41 state->ws_error_action = nowarn_ws_error;
44 if (!strcmp(option, "error")) {
45 state->ws_error_action = die_on_ws_error;
48 if (!strcmp(option, "error-all")) {
49 state->ws_error_action = die_on_ws_error;
50 state->squelch_whitespace_errors = 0;
53 if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
54 state->ws_error_action = correct_ws_error;
57 die(_("unrecognized whitespace option '%s'"), option);
60 static void parse_ignorewhitespace_option(struct apply_state *state,
63 if (!option || !strcmp(option, "no") ||
64 !strcmp(option, "false") || !strcmp(option, "never") ||
65 !strcmp(option, "none")) {
66 state->ws_ignore_action = ignore_ws_none;
69 if (!strcmp(option, "change")) {
70 state->ws_ignore_action = ignore_ws_change;
73 die(_("unrecognized whitespace ignore option '%s'"), option);
76 static void set_default_whitespace_mode(struct apply_state *state)
78 if (!state->whitespace_option && !apply_default_whitespace)
79 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
83 * This represents one "hunk" from a patch, starting with
84 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
85 * patch text is pointed at by patch, and its byte length
86 * is stored in size. leading and trailing are the number
90 unsigned long leading, trailing;
91 unsigned long oldpos, oldlines;
92 unsigned long newpos, newlines;
94 * 'patch' is usually borrowed from buf in apply_patch(),
95 * but some codepaths store an allocated buffer.
98 unsigned free_patch:1,
102 struct fragment *next;
106 * When dealing with a binary patch, we reuse "leading" field
107 * to store the type of the binary hunk, either deflated "delta"
108 * or deflated "literal".
110 #define binary_patch_method leading
111 #define BINARY_DELTA_DEFLATED 1
112 #define BINARY_LITERAL_DEFLATED 2
115 * This represents a "patch" to a file, both metainfo changes
116 * such as creation/deletion, filemode and content changes represented
117 * as a series of fragments.
120 char *new_name, *old_name, *def_name;
121 unsigned int old_mode, new_mode;
122 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
125 int lines_added, lines_deleted;
127 unsigned int is_toplevel_relative:1;
128 unsigned int inaccurate_eof:1;
129 unsigned int is_binary:1;
130 unsigned int is_copy:1;
131 unsigned int is_rename:1;
132 unsigned int recount:1;
133 unsigned int conflicted_threeway:1;
134 unsigned int direct_to_threeway:1;
135 struct fragment *fragments;
138 char old_sha1_prefix[41];
139 char new_sha1_prefix[41];
142 /* three-way fallback result */
143 struct object_id threeway_stage[3];
146 static void free_fragment_list(struct fragment *list)
149 struct fragment *next = list->next;
150 if (list->free_patch)
151 free((char *)list->patch);
157 static void free_patch(struct patch *patch)
159 free_fragment_list(patch->fragments);
160 free(patch->def_name);
161 free(patch->old_name);
162 free(patch->new_name);
167 static void free_patch_list(struct patch *list)
170 struct patch *next = list->next;
177 * A line in a file, len-bytes long (includes the terminating LF,
178 * except for an incomplete line at the end if the file ends with
179 * one), and its contents hashes to 'hash'.
185 #define LINE_COMMON 1
186 #define LINE_PATCHED 2
190 * This represents a "file", which is an array of "lines".
197 struct line *line_allocated;
201 static uint32_t hash_line(const char *cp, size_t len)
205 for (i = 0, h = 0; i < len; i++) {
206 if (!isspace(cp[i])) {
207 h = h * 3 + (cp[i] & 0xff);
214 * Compare lines s1 of length n1 and s2 of length n2, ignoring
215 * whitespace difference. Returns 1 if they match, 0 otherwise
217 static int fuzzy_matchlines(const char *s1, size_t n1,
218 const char *s2, size_t n2)
220 const char *last1 = s1 + n1 - 1;
221 const char *last2 = s2 + n2 - 1;
224 /* ignore line endings */
225 while ((*last1 == '\r') || (*last1 == '\n'))
227 while ((*last2 == '\r') || (*last2 == '\n'))
230 /* skip leading whitespaces, if both begin with whitespace */
231 if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
232 while (isspace(*s1) && (s1 <= last1))
234 while (isspace(*s2) && (s2 <= last2))
237 /* early return if both lines are empty */
238 if ((s1 > last1) && (s2 > last2))
241 result = *s1++ - *s2++;
243 * Skip whitespace inside. We check for whitespace on
244 * both buffers because we don't want "a b" to match
247 if (isspace(*s1) && isspace(*s2)) {
248 while (isspace(*s1) && s1 <= last1)
250 while (isspace(*s2) && s2 <= last2)
254 * If we reached the end on one side only,
258 ((s2 > last2) && (s1 <= last1)) ||
259 ((s1 > last1) && (s2 <= last2)))
261 if ((s1 > last1) && (s2 > last2))
268 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
270 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
271 img->line_allocated[img->nr].len = len;
272 img->line_allocated[img->nr].hash = hash_line(bol, len);
273 img->line_allocated[img->nr].flag = flag;
278 * "buf" has the file contents to be patched (read from various sources).
279 * attach it to "image" and add line-based index to it.
280 * "image" now owns the "buf".
282 static void prepare_image(struct image *image, char *buf, size_t len,
283 int prepare_linetable)
287 memset(image, 0, sizeof(*image));
291 if (!prepare_linetable)
294 ep = image->buf + image->len;
298 for (next = cp; next < ep && *next != '\n'; next++)
302 add_line_info(image, cp, next - cp, 0);
305 image->line = image->line_allocated;
308 static void clear_image(struct image *image)
311 free(image->line_allocated);
312 memset(image, 0, sizeof(*image));
315 /* fmt must contain _one_ %s and no other substitution */
316 static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
318 struct strbuf sb = STRBUF_INIT;
320 if (patch->old_name && patch->new_name &&
321 strcmp(patch->old_name, patch->new_name)) {
322 quote_c_style(patch->old_name, &sb, NULL, 0);
323 strbuf_addstr(&sb, " => ");
324 quote_c_style(patch->new_name, &sb, NULL, 0);
326 const char *n = patch->new_name;
329 quote_c_style(n, &sb, NULL, 0);
331 fprintf(output, fmt, sb.buf);
338 static int read_patch_file(struct strbuf *sb, int fd)
340 if (strbuf_read(sb, fd, 0) < 0)
341 return error_errno("git apply: failed to read");
344 * Make sure that we have some slop in the buffer
345 * so that we can do speculative "memcmp" etc, and
346 * see to it that it is NUL-filled.
348 strbuf_grow(sb, SLOP);
349 memset(sb->buf + sb->len, 0, SLOP);
353 static unsigned long linelen(const char *buffer, unsigned long size)
355 unsigned long len = 0;
358 if (*buffer++ == '\n')
364 static int is_dev_null(const char *str)
366 return skip_prefix(str, "/dev/null", &str) && isspace(*str);
372 static int name_terminate(int c, int terminate)
374 if (c == ' ' && !(terminate & TERM_SPACE))
376 if (c == '\t' && !(terminate & TERM_TAB))
382 /* remove double slashes to make --index work with such filenames */
383 static char *squash_slash(char *name)
391 if ((name[j++] = name[i++]) == '/')
392 while (name[i] == '/')
399 static char *find_name_gnu(struct apply_state *state,
404 struct strbuf name = STRBUF_INIT;
408 * Proposed "new-style" GNU patch/diff format; see
409 * http://marc.info/?l=git&m=112927316408690&w=2
411 if (unquote_c_style(&name, line, NULL)) {
412 strbuf_release(&name);
416 for (cp = name.buf; p_value; p_value--) {
417 cp = strchr(cp, '/');
419 strbuf_release(&name);
425 strbuf_remove(&name, 0, cp - name.buf);
427 strbuf_insert(&name, 0, state->root.buf, state->root.len);
428 return squash_slash(strbuf_detach(&name, NULL));
431 static size_t sane_tz_len(const char *line, size_t len)
435 if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
437 tz = line + len - strlen(" +0500");
439 if (tz[1] != '+' && tz[1] != '-')
442 for (p = tz + 2; p != line + len; p++)
446 return line + len - tz;
449 static size_t tz_with_colon_len(const char *line, size_t len)
453 if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
455 tz = line + len - strlen(" +08:00");
457 if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
460 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
461 !isdigit(*p++) || !isdigit(*p++))
464 return line + len - tz;
467 static size_t date_len(const char *line, size_t len)
469 const char *date, *p;
471 if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
473 p = date = line + len - strlen("72-02-05");
475 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
476 !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
477 !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */
480 if (date - line >= strlen("19") &&
481 isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */
482 date -= strlen("19");
484 return line + len - date;
487 static size_t short_time_len(const char *line, size_t len)
489 const char *time, *p;
491 if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
493 p = time = line + len - strlen(" 07:01:32");
495 /* Permit 1-digit hours? */
497 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
498 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
499 !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
502 return line + len - time;
505 static size_t fractional_time_len(const char *line, size_t len)
510 /* Expected format: 19:41:17.620000023 */
511 if (!len || !isdigit(line[len - 1]))
515 /* Fractional seconds. */
516 while (p > line && isdigit(*p))
521 /* Hours, minutes, and whole seconds. */
522 n = short_time_len(line, p - line);
526 return line + len - p + n;
529 static size_t trailing_spaces_len(const char *line, size_t len)
533 /* Expected format: ' ' x (1 or more) */
534 if (!len || line[len - 1] != ' ')
541 return line + len - (p + 1);
548 static size_t diff_timestamp_len(const char *line, size_t len)
550 const char *end = line + len;
554 * Posix: 2010-07-05 19:41:17
555 * GNU: 2010-07-05 19:41:17.620000023 -0500
558 if (!isdigit(end[-1]))
561 n = sane_tz_len(line, end - line);
563 n = tz_with_colon_len(line, end - line);
566 n = short_time_len(line, end - line);
568 n = fractional_time_len(line, end - line);
571 n = date_len(line, end - line);
572 if (!n) /* No date. Too bad. */
576 if (end == line) /* No space before date. */
578 if (end[-1] == '\t') { /* Success! */
580 return line + len - end;
582 if (end[-1] != ' ') /* No space before date. */
585 /* Whitespace damage. */
586 end -= trailing_spaces_len(line, end - line);
587 return line + len - end;
590 static char *find_name_common(struct apply_state *state,
598 const char *start = NULL;
602 while (line != end) {
605 if (!end && isspace(c)) {
608 if (name_terminate(c, terminate))
612 if (c == '/' && !--p_value)
616 return squash_slash(xstrdup_or_null(def));
619 return squash_slash(xstrdup_or_null(def));
622 * Generally we prefer the shorter name, especially
623 * if the other one is just a variation of that with
624 * something else tacked on to the end (ie "file.orig"
628 int deflen = strlen(def);
629 if (deflen < len && !strncmp(start, def, deflen))
630 return squash_slash(xstrdup(def));
633 if (state->root.len) {
634 char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
635 return squash_slash(ret);
638 return squash_slash(xmemdupz(start, len));
641 static char *find_name(struct apply_state *state,
648 char *name = find_name_gnu(state, line, def, p_value);
653 return find_name_common(state, line, def, p_value, NULL, terminate);
656 static char *find_name_traditional(struct apply_state *state,
665 char *name = find_name_gnu(state, line, def, p_value);
670 len = strchrnul(line, '\n') - line;
671 date_len = diff_timestamp_len(line, len);
673 return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
676 return find_name_common(state, line, def, p_value, line + len, 0);
679 static int count_slashes(const char *cp)
691 * Given the string after "--- " or "+++ ", guess the appropriate
692 * p_value for the given patch.
694 static int guess_p_value(struct apply_state *state, const char *nameline)
699 if (is_dev_null(nameline))
701 name = find_name_traditional(state, nameline, NULL, 0);
704 cp = strchr(name, '/');
707 else if (state->prefix) {
709 * Does it begin with "a/$our-prefix" and such? Then this is
710 * very likely to apply to our directory.
712 if (!strncmp(name, state->prefix, state->prefix_length))
713 val = count_slashes(state->prefix);
716 if (!strncmp(cp, state->prefix, state->prefix_length))
717 val = count_slashes(state->prefix) + 1;
725 * Does the ---/+++ line have the POSIX timestamp after the last HT?
726 * GNU diff puts epoch there to signal a creation/deletion event. Is
727 * this such a timestamp?
729 static int has_epoch_timestamp(const char *nameline)
732 * We are only interested in epoch timestamp; any non-zero
733 * fraction cannot be one, hence "(\.0+)?" in the regexp below.
734 * For the same reason, the date must be either 1969-12-31 or
735 * 1970-01-01, and the seconds part must be "00".
737 const char stamp_regexp[] =
738 "^(1969-12-31|1970-01-01)"
740 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
742 "([-+][0-2][0-9]:?[0-5][0-9])\n";
743 const char *timestamp = NULL, *cp, *colon;
744 static regex_t *stamp;
750 for (cp = nameline; *cp != '\n'; cp++) {
757 stamp = xmalloc(sizeof(*stamp));
758 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
759 warning(_("Cannot prepare timestamp regexp %s"),
765 status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
767 if (status != REG_NOMATCH)
768 warning(_("regexec returned %d for input: %s"),
773 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
775 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
777 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
778 if (timestamp[m[3].rm_so] == '-')
779 zoneoffset = -zoneoffset;
782 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
783 * (west of GMT) or 1970-01-01 (east of GMT)
785 if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
786 (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
789 hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
790 strtol(timestamp + 14, NULL, 10) -
793 return ((zoneoffset < 0 && hourminute == 1440) ||
794 (0 <= zoneoffset && !hourminute));
798 * Get the name etc info from the ---/+++ lines of a traditional patch header
800 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
801 * files, we can happily check the index for a match, but for creating a
802 * new file we should try to match whatever "patch" does. I have no idea.
804 static void parse_traditional_patch(struct apply_state *state,
811 first += 4; /* skip "--- " */
812 second += 4; /* skip "+++ " */
813 if (!state->p_value_known) {
815 p = guess_p_value(state, first);
816 q = guess_p_value(state, second);
818 if (0 <= p && p == q) {
820 state->p_value_known = 1;
823 if (is_dev_null(first)) {
825 patch->is_delete = 0;
826 name = find_name_traditional(state, second, NULL, state->p_value);
827 patch->new_name = name;
828 } else if (is_dev_null(second)) {
830 patch->is_delete = 1;
831 name = find_name_traditional(state, first, NULL, state->p_value);
832 patch->old_name = name;
835 first_name = find_name_traditional(state, first, NULL, state->p_value);
836 name = find_name_traditional(state, second, first_name, state->p_value);
838 if (has_epoch_timestamp(first)) {
840 patch->is_delete = 0;
841 patch->new_name = name;
842 } else if (has_epoch_timestamp(second)) {
844 patch->is_delete = 1;
845 patch->old_name = name;
847 patch->old_name = name;
848 patch->new_name = xstrdup_or_null(name);
852 die(_("unable to find filename in patch at line %d"), state->linenr);
855 static int gitdiff_hdrend(struct apply_state *state,
863 * We're anal about diff header consistency, to make
864 * sure that we don't end up having strange ambiguous
865 * patches floating around.
867 * As a result, gitdiff_{old|new}name() will check
868 * their names against any previous information, just
871 #define DIFF_OLD_NAME 0
872 #define DIFF_NEW_NAME 1
874 static void gitdiff_verify_name(struct apply_state *state,
880 if (!*name && !isnull) {
881 *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
886 int len = strlen(*name);
889 die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
890 *name, state->linenr);
891 another = find_name(state, line, NULL, state->p_value, TERM_TAB);
892 if (!another || memcmp(another, *name, len + 1))
893 die((side == DIFF_NEW_NAME) ?
894 _("git apply: bad git-diff - inconsistent new filename on line %d") :
895 _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
898 /* expect "/dev/null" */
899 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
900 die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
904 static int gitdiff_oldname(struct apply_state *state,
908 gitdiff_verify_name(state, line,
909 patch->is_new, &patch->old_name,
914 static int gitdiff_newname(struct apply_state *state,
918 gitdiff_verify_name(state, line,
919 patch->is_delete, &patch->new_name,
924 static int gitdiff_oldmode(struct apply_state *state,
928 patch->old_mode = strtoul(line, NULL, 8);
932 static int gitdiff_newmode(struct apply_state *state,
936 patch->new_mode = strtoul(line, NULL, 8);
940 static int gitdiff_delete(struct apply_state *state,
944 patch->is_delete = 1;
945 free(patch->old_name);
946 patch->old_name = xstrdup_or_null(patch->def_name);
947 return gitdiff_oldmode(state, line, patch);
950 static int gitdiff_newfile(struct apply_state *state,
955 free(patch->new_name);
956 patch->new_name = xstrdup_or_null(patch->def_name);
957 return gitdiff_newmode(state, line, patch);
960 static int gitdiff_copysrc(struct apply_state *state,
965 free(patch->old_name);
966 patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
970 static int gitdiff_copydst(struct apply_state *state,
975 free(patch->new_name);
976 patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
980 static int gitdiff_renamesrc(struct apply_state *state,
984 patch->is_rename = 1;
985 free(patch->old_name);
986 patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
990 static int gitdiff_renamedst(struct apply_state *state,
994 patch->is_rename = 1;
995 free(patch->new_name);
996 patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1000 static int gitdiff_similarity(struct apply_state *state,
1002 struct patch *patch)
1004 unsigned long val = strtoul(line, NULL, 10);
1010 static int gitdiff_dissimilarity(struct apply_state *state,
1012 struct patch *patch)
1014 unsigned long val = strtoul(line, NULL, 10);
1020 static int gitdiff_index(struct apply_state *state,
1022 struct patch *patch)
1025 * index line is N hexadecimal, "..", N hexadecimal,
1026 * and optional space with octal mode.
1028 const char *ptr, *eol;
1031 ptr = strchr(line, '.');
1032 if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1035 memcpy(patch->old_sha1_prefix, line, len);
1036 patch->old_sha1_prefix[len] = 0;
1039 ptr = strchr(line, ' ');
1040 eol = strchrnul(line, '\n');
1042 if (!ptr || eol < ptr)
1048 memcpy(patch->new_sha1_prefix, line, len);
1049 patch->new_sha1_prefix[len] = 0;
1051 patch->old_mode = strtoul(ptr+1, NULL, 8);
1056 * This is normal for a diff that doesn't change anything: we'll fall through
1057 * into the next diff. Tell the parser to break out.
1059 static int gitdiff_unrecognized(struct apply_state *state,
1061 struct patch *patch)
1067 * Skip p_value leading components from "line"; as we do not accept
1068 * absolute paths, return NULL in that case.
1070 static const char *skip_tree_prefix(struct apply_state *state,
1077 if (!state->p_value)
1078 return (llen && line[0] == '/') ? NULL : line;
1080 nslash = state->p_value;
1081 for (i = 0; i < llen; i++) {
1083 if (ch == '/' && --nslash <= 0)
1084 return (i == 0) ? NULL : &line[i + 1];
1090 * This is to extract the same name that appears on "diff --git"
1091 * line. We do not find and return anything if it is a rename
1092 * patch, and it is OK because we will find the name elsewhere.
1093 * We need to reliably find name only when it is mode-change only,
1094 * creation or deletion of an empty file. In any of these cases,
1095 * both sides are the same name under a/ and b/ respectively.
1097 static char *git_header_name(struct apply_state *state,
1102 const char *second = NULL;
1103 size_t len, line_len;
1105 line += strlen("diff --git ");
1106 llen -= strlen("diff --git ");
1110 struct strbuf first = STRBUF_INIT;
1111 struct strbuf sp = STRBUF_INIT;
1113 if (unquote_c_style(&first, line, &second))
1114 goto free_and_fail1;
1116 /* strip the a/b prefix including trailing slash */
1117 cp = skip_tree_prefix(state, first.buf, first.len);
1119 goto free_and_fail1;
1120 strbuf_remove(&first, 0, cp - first.buf);
1123 * second points at one past closing dq of name.
1124 * find the second name.
1126 while ((second < line + llen) && isspace(*second))
1129 if (line + llen <= second)
1130 goto free_and_fail1;
1131 if (*second == '"') {
1132 if (unquote_c_style(&sp, second, NULL))
1133 goto free_and_fail1;
1134 cp = skip_tree_prefix(state, sp.buf, sp.len);
1136 goto free_and_fail1;
1137 /* They must match, otherwise ignore */
1138 if (strcmp(cp, first.buf))
1139 goto free_and_fail1;
1140 strbuf_release(&sp);
1141 return strbuf_detach(&first, NULL);
1144 /* unquoted second */
1145 cp = skip_tree_prefix(state, second, line + llen - second);
1147 goto free_and_fail1;
1148 if (line + llen - cp != first.len ||
1149 memcmp(first.buf, cp, first.len))
1150 goto free_and_fail1;
1151 return strbuf_detach(&first, NULL);
1154 strbuf_release(&first);
1155 strbuf_release(&sp);
1159 /* unquoted first name */
1160 name = skip_tree_prefix(state, line, llen);
1165 * since the first name is unquoted, a dq if exists must be
1166 * the beginning of the second name.
1168 for (second = name; second < line + llen; second++) {
1169 if (*second == '"') {
1170 struct strbuf sp = STRBUF_INIT;
1173 if (unquote_c_style(&sp, second, NULL))
1174 goto free_and_fail2;
1176 np = skip_tree_prefix(state, sp.buf, sp.len);
1178 goto free_and_fail2;
1180 len = sp.buf + sp.len - np;
1181 if (len < second - name &&
1182 !strncmp(np, name, len) &&
1183 isspace(name[len])) {
1185 strbuf_remove(&sp, 0, np - sp.buf);
1186 return strbuf_detach(&sp, NULL);
1190 strbuf_release(&sp);
1196 * Accept a name only if it shows up twice, exactly the same
1199 second = strchr(name, '\n');
1202 line_len = second - name;
1203 for (len = 0 ; ; len++) {
1204 switch (name[len]) {
1209 case '\t': case ' ':
1211 * Is this the separator between the preimage
1212 * and the postimage pathname? Again, we are
1213 * only interested in the case where there is
1214 * no rename, as this is only to set def_name
1215 * and a rename patch has the names elsewhere
1216 * in an unambiguous form.
1219 return NULL; /* no postimage name */
1220 second = skip_tree_prefix(state, name + len + 1,
1221 line_len - (len + 1));
1225 * Does len bytes starting at "name" and "second"
1226 * (that are separated by one HT or SP we just
1227 * found) exactly match?
1229 if (second[len] == '\n' && !strncmp(name, second, len))
1230 return xmemdupz(name, len);
1235 /* Verify that we recognize the lines following a git header */
1236 static int parse_git_header(struct apply_state *state,
1240 struct patch *patch)
1242 unsigned long offset;
1244 /* A git diff has explicit new/delete information, so we don't guess */
1246 patch->is_delete = 0;
1249 * Some things may not have the old name in the
1250 * rest of the headers anywhere (pure mode changes,
1251 * or removing or adding empty files), so we get
1252 * the default name from the header.
1254 patch->def_name = git_header_name(state, line, len);
1255 if (patch->def_name && state->root.len) {
1256 char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1257 free(patch->def_name);
1258 patch->def_name = s;
1264 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1265 static const struct opentry {
1267 int (*fn)(struct apply_state *, const char *, struct patch *);
1269 { "@@ -", gitdiff_hdrend },
1270 { "--- ", gitdiff_oldname },
1271 { "+++ ", gitdiff_newname },
1272 { "old mode ", gitdiff_oldmode },
1273 { "new mode ", gitdiff_newmode },
1274 { "deleted file mode ", gitdiff_delete },
1275 { "new file mode ", gitdiff_newfile },
1276 { "copy from ", gitdiff_copysrc },
1277 { "copy to ", gitdiff_copydst },
1278 { "rename old ", gitdiff_renamesrc },
1279 { "rename new ", gitdiff_renamedst },
1280 { "rename from ", gitdiff_renamesrc },
1281 { "rename to ", gitdiff_renamedst },
1282 { "similarity index ", gitdiff_similarity },
1283 { "dissimilarity index ", gitdiff_dissimilarity },
1284 { "index ", gitdiff_index },
1285 { "", gitdiff_unrecognized },
1289 len = linelen(line, size);
1290 if (!len || line[len-1] != '\n')
1292 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1293 const struct opentry *p = optable + i;
1294 int oplen = strlen(p->str);
1295 if (len < oplen || memcmp(p->str, line, oplen))
1297 if (p->fn(state, line + oplen, patch) < 0)
1306 static int parse_num(const char *line, unsigned long *p)
1310 if (!isdigit(*line))
1312 *p = strtoul(line, &ptr, 10);
1316 static int parse_range(const char *line, int len, int offset, const char *expect,
1317 unsigned long *p1, unsigned long *p2)
1321 if (offset < 0 || offset >= len)
1326 digits = parse_num(line, p1);
1336 digits = parse_num(line+1, p2);
1345 ex = strlen(expect);
1348 if (memcmp(line, expect, ex))
1354 static void recount_diff(const char *line, int size, struct fragment *fragment)
1356 int oldlines = 0, newlines = 0, ret = 0;
1359 warning("recount: ignore empty hunk");
1364 int len = linelen(line, size);
1372 case ' ': case '\n':
1384 ret = size < 3 || !starts_with(line, "@@ ");
1387 ret = size < 5 || !starts_with(line, "diff ");
1394 warning(_("recount: unexpected line: %.*s"),
1395 (int)linelen(line, size), line);
1400 fragment->oldlines = oldlines;
1401 fragment->newlines = newlines;
1405 * Parse a unified diff fragment header of the
1406 * form "@@ -a,b +c,d @@"
1408 static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1412 if (!len || line[len-1] != '\n')
1415 /* Figure out the number of lines in a fragment */
1416 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1417 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1423 * Find file diff header
1426 * -1 if no header was found
1427 * -128 in case of error
1428 * the size of the header in bytes (called "offset") otherwise
1430 static int find_header(struct apply_state *state,
1434 struct patch *patch)
1436 unsigned long offset, len;
1438 patch->is_toplevel_relative = 0;
1439 patch->is_rename = patch->is_copy = 0;
1440 patch->is_new = patch->is_delete = -1;
1441 patch->old_mode = patch->new_mode = 0;
1442 patch->old_name = patch->new_name = NULL;
1443 for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1444 unsigned long nextlen;
1446 len = linelen(line, size);
1450 /* Testing this early allows us to take a few shortcuts.. */
1455 * Make sure we don't find any unconnected patch fragments.
1456 * That's a sign that we didn't find a header, and that a
1457 * patch has become corrupted/broken up.
1459 if (!memcmp("@@ -", line, 4)) {
1460 struct fragment dummy;
1461 if (parse_fragment_header(line, len, &dummy) < 0)
1463 error(_("patch fragment without header at line %d: %.*s"),
1464 state->linenr, (int)len-1, line);
1472 * Git patch? It might not have a real patch, just a rename
1473 * or mode change, so we handle that specially
1475 if (!memcmp("diff --git ", line, 11)) {
1476 int git_hdr_len = parse_git_header(state, line, len, size, patch);
1477 if (git_hdr_len <= len)
1479 if (!patch->old_name && !patch->new_name) {
1480 if (!patch->def_name) {
1481 error(Q_("git diff header lacks filename information when removing "
1482 "%d leading pathname component (line %d)",
1483 "git diff header lacks filename information when removing "
1484 "%d leading pathname components (line %d)",
1486 state->p_value, state->linenr);
1489 patch->old_name = xstrdup(patch->def_name);
1490 patch->new_name = xstrdup(patch->def_name);
1492 if (!patch->is_delete && !patch->new_name) {
1493 error("git diff header lacks filename information "
1494 "(line %d)", state->linenr);
1497 patch->is_toplevel_relative = 1;
1498 *hdrsize = git_hdr_len;
1502 /* --- followed by +++ ? */
1503 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1507 * We only accept unified patches, so we want it to
1508 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1509 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1511 nextlen = linelen(line + len, size - len);
1512 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1515 /* Ok, we'll consider it a patch */
1516 parse_traditional_patch(state, line, line+len, patch);
1517 *hdrsize = len + nextlen;
1524 static void record_ws_error(struct apply_state *state,
1535 state->whitespace_error++;
1536 if (state->squelch_whitespace_errors &&
1537 state->squelch_whitespace_errors < state->whitespace_error)
1540 err = whitespace_error_string(result);
1541 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1542 state->patch_input_file, linenr, err, len, line);
1546 static void check_whitespace(struct apply_state *state,
1551 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1553 record_ws_error(state, result, line + 1, len - 2, state->linenr);
1557 * Parse a unified diff. Note that this really needs to parse each
1558 * fragment separately, since the only way to know the difference
1559 * between a "---" that is part of a patch, and a "---" that starts
1560 * the next patch is to look at the line counts..
1562 static int parse_fragment(struct apply_state *state,
1565 struct patch *patch,
1566 struct fragment *fragment)
1569 int len = linelen(line, size), offset;
1570 unsigned long oldlines, newlines;
1571 unsigned long leading, trailing;
1573 offset = parse_fragment_header(line, len, fragment);
1576 if (offset > 0 && patch->recount)
1577 recount_diff(line + offset, size - offset, fragment);
1578 oldlines = fragment->oldlines;
1579 newlines = fragment->newlines;
1583 /* Parse the thing.. */
1587 added = deleted = 0;
1590 offset += len, size -= len, line += len, state->linenr++) {
1591 if (!oldlines && !newlines)
1593 len = linelen(line, size);
1594 if (!len || line[len-1] != '\n')
1599 case '\n': /* newer GNU diff, an empty context line */
1603 if (!deleted && !added)
1606 if (!state->apply_in_reverse &&
1607 state->ws_error_action == correct_ws_error)
1608 check_whitespace(state, line, len, patch->ws_rule);
1611 if (state->apply_in_reverse &&
1612 state->ws_error_action != nowarn_ws_error)
1613 check_whitespace(state, line, len, patch->ws_rule);
1619 if (!state->apply_in_reverse &&
1620 state->ws_error_action != nowarn_ws_error)
1621 check_whitespace(state, line, len, patch->ws_rule);
1628 * We allow "\ No newline at end of file". Depending
1629 * on locale settings when the patch was produced we
1630 * don't know what this line looks like. The only
1631 * thing we do know is that it begins with "\ ".
1632 * Checking for 12 is just for sanity check -- any
1633 * l10n of "\ No newline..." is at least that long.
1636 if (len < 12 || memcmp(line, "\\ ", 2))
1641 if (oldlines || newlines)
1643 if (!deleted && !added)
1646 fragment->leading = leading;
1647 fragment->trailing = trailing;
1650 * If a fragment ends with an incomplete line, we failed to include
1651 * it in the above loop because we hit oldlines == newlines == 0
1654 if (12 < size && !memcmp(line, "\\ ", 2))
1655 offset += linelen(line, size);
1657 patch->lines_added += added;
1658 patch->lines_deleted += deleted;
1660 if (0 < patch->is_new && oldlines)
1661 return error(_("new file depends on old contents"));
1662 if (0 < patch->is_delete && newlines)
1663 return error(_("deleted file still has contents"));
1668 * We have seen "diff --git a/... b/..." header (or a traditional patch
1669 * header). Read hunks that belong to this patch into fragments and hang
1670 * them to the given patch structure.
1672 * The (fragment->patch, fragment->size) pair points into the memory given
1673 * by the caller, not a copy, when we return.
1676 * -1 in case of error,
1677 * the number of bytes in the patch otherwise.
1679 static int parse_single_patch(struct apply_state *state,
1682 struct patch *patch)
1684 unsigned long offset = 0;
1685 unsigned long oldlines = 0, newlines = 0, context = 0;
1686 struct fragment **fragp = &patch->fragments;
1688 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1689 struct fragment *fragment;
1692 fragment = xcalloc(1, sizeof(*fragment));
1693 fragment->linenr = state->linenr;
1694 len = parse_fragment(state, line, size, patch, fragment);
1697 return error(_("corrupt patch at line %d"), state->linenr);
1699 fragment->patch = line;
1700 fragment->size = len;
1701 oldlines += fragment->oldlines;
1702 newlines += fragment->newlines;
1703 context += fragment->leading + fragment->trailing;
1706 fragp = &fragment->next;
1714 * If something was removed (i.e. we have old-lines) it cannot
1715 * be creation, and if something was added it cannot be
1716 * deletion. However, the reverse is not true; --unified=0
1717 * patches that only add are not necessarily creation even
1718 * though they do not have any old lines, and ones that only
1719 * delete are not necessarily deletion.
1721 * Unfortunately, a real creation/deletion patch do _not_ have
1722 * any context line by definition, so we cannot safely tell it
1723 * apart with --unified=0 insanity. At least if the patch has
1724 * more than one hunk it is not creation or deletion.
1726 if (patch->is_new < 0 &&
1727 (oldlines || (patch->fragments && patch->fragments->next)))
1729 if (patch->is_delete < 0 &&
1730 (newlines || (patch->fragments && patch->fragments->next)))
1731 patch->is_delete = 0;
1733 if (0 < patch->is_new && oldlines)
1734 return error(_("new file %s depends on old contents"), patch->new_name);
1735 if (0 < patch->is_delete && newlines)
1736 return error(_("deleted file %s still has contents"), patch->old_name);
1737 if (!patch->is_delete && !newlines && context)
1740 "file %s becomes empty but is not deleted"),
1746 static inline int metadata_changes(struct patch *patch)
1748 return patch->is_rename > 0 ||
1749 patch->is_copy > 0 ||
1750 patch->is_new > 0 ||
1752 (patch->old_mode && patch->new_mode &&
1753 patch->old_mode != patch->new_mode);
1756 static char *inflate_it(const void *data, unsigned long size,
1757 unsigned long inflated_size)
1763 memset(&stream, 0, sizeof(stream));
1765 stream.next_in = (unsigned char *)data;
1766 stream.avail_in = size;
1767 stream.next_out = out = xmalloc(inflated_size);
1768 stream.avail_out = inflated_size;
1769 git_inflate_init(&stream);
1770 st = git_inflate(&stream, Z_FINISH);
1771 git_inflate_end(&stream);
1772 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1780 * Read a binary hunk and return a new fragment; fragment->patch
1781 * points at an allocated memory that the caller must free, so
1782 * it is marked as "->free_patch = 1".
1784 static struct fragment *parse_binary_hunk(struct apply_state *state,
1786 unsigned long *sz_p,
1791 * Expect a line that begins with binary patch method ("literal"
1792 * or "delta"), followed by the length of data before deflating.
1793 * a sequence of 'length-byte' followed by base-85 encoded data
1794 * should follow, terminated by a newline.
1796 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1797 * and we would limit the patch line to 66 characters,
1798 * so one line can fit up to 13 groups that would decode
1799 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1800 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1803 unsigned long size = *sz_p;
1804 char *buffer = *buf_p;
1806 unsigned long origlen;
1809 struct fragment *frag;
1811 llen = linelen(buffer, size);
1816 if (starts_with(buffer, "delta ")) {
1817 patch_method = BINARY_DELTA_DEFLATED;
1818 origlen = strtoul(buffer + 6, NULL, 10);
1820 else if (starts_with(buffer, "literal ")) {
1821 patch_method = BINARY_LITERAL_DEFLATED;
1822 origlen = strtoul(buffer + 8, NULL, 10);
1830 int byte_length, max_byte_length, newsize;
1831 llen = linelen(buffer, size);
1835 /* consume the blank line */
1841 * Minimum line is "A00000\n" which is 7-byte long,
1842 * and the line length must be multiple of 5 plus 2.
1844 if ((llen < 7) || (llen-2) % 5)
1846 max_byte_length = (llen - 2) / 5 * 4;
1847 byte_length = *buffer;
1848 if ('A' <= byte_length && byte_length <= 'Z')
1849 byte_length = byte_length - 'A' + 1;
1850 else if ('a' <= byte_length && byte_length <= 'z')
1851 byte_length = byte_length - 'a' + 27;
1854 /* if the input length was not multiple of 4, we would
1855 * have filler at the end but the filler should never
1858 if (max_byte_length < byte_length ||
1859 byte_length <= max_byte_length - 4)
1861 newsize = hunk_size + byte_length;
1862 data = xrealloc(data, newsize);
1863 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1865 hunk_size = newsize;
1870 frag = xcalloc(1, sizeof(*frag));
1871 frag->patch = inflate_it(data, hunk_size, origlen);
1872 frag->free_patch = 1;
1876 frag->size = origlen;
1880 frag->binary_patch_method = patch_method;
1886 error(_("corrupt binary patch at line %d: %.*s"),
1887 state->linenr-1, llen-1, buffer);
1893 * -1 in case of error,
1894 * the length of the parsed binary patch otherwise
1896 static int parse_binary(struct apply_state *state,
1899 struct patch *patch)
1902 * We have read "GIT binary patch\n"; what follows is a line
1903 * that says the patch method (currently, either "literal" or
1904 * "delta") and the length of data before deflating; a
1905 * sequence of 'length-byte' followed by base-85 encoded data
1908 * When a binary patch is reversible, there is another binary
1909 * hunk in the same format, starting with patch method (either
1910 * "literal" or "delta") with the length of data, and a sequence
1911 * of length-byte + base-85 encoded data, terminated with another
1912 * empty line. This data, when applied to the postimage, produces
1915 struct fragment *forward;
1916 struct fragment *reverse;
1920 forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
1921 if (!forward && !status)
1922 /* there has to be one hunk (forward hunk) */
1923 return error(_("unrecognized binary patch at line %d"), state->linenr-1);
1925 /* otherwise we already gave an error message */
1928 reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
1933 * Not having reverse hunk is not an error, but having
1934 * a corrupt reverse hunk is.
1936 free((void*) forward->patch);
1940 forward->next = reverse;
1941 patch->fragments = forward;
1942 patch->is_binary = 1;
1946 static void prefix_one(struct apply_state *state, char **name)
1948 char *old_name = *name;
1951 *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
1955 static void prefix_patch(struct apply_state *state, struct patch *p)
1957 if (!state->prefix || p->is_toplevel_relative)
1959 prefix_one(state, &p->new_name);
1960 prefix_one(state, &p->old_name);
1967 static void add_name_limit(struct apply_state *state,
1971 struct string_list_item *it;
1973 it = string_list_append(&state->limit_by_name, name);
1974 it->util = exclude ? NULL : (void *) 1;
1977 static int use_patch(struct apply_state *state, struct patch *p)
1979 const char *pathname = p->new_name ? p->new_name : p->old_name;
1982 /* Paths outside are not touched regardless of "--include" */
1983 if (0 < state->prefix_length) {
1984 int pathlen = strlen(pathname);
1985 if (pathlen <= state->prefix_length ||
1986 memcmp(state->prefix, pathname, state->prefix_length))
1990 /* See if it matches any of exclude/include rule */
1991 for (i = 0; i < state->limit_by_name.nr; i++) {
1992 struct string_list_item *it = &state->limit_by_name.items[i];
1993 if (!wildmatch(it->string, pathname, 0, NULL))
1994 return (it->util != NULL);
1998 * If we had any include, a path that does not match any rule is
1999 * not used. Otherwise, we saw bunch of exclude rules (or none)
2000 * and such a path is used.
2002 return !state->has_include;
2006 * Read the patch text in "buffer" that extends for "size" bytes; stop
2007 * reading after seeing a single patch (i.e. changes to a single file).
2008 * Create fragments (i.e. patch hunks) and hang them to the given patch.
2011 * -1 if no header was found or parse_binary() failed,
2012 * -128 on another error,
2013 * the number of bytes consumed otherwise,
2014 * so that the caller can call us again for the next patch.
2016 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2018 int hdrsize, patchsize;
2019 int offset = find_header(state, buffer, size, &hdrsize, patch);
2024 prefix_patch(state, patch);
2026 if (!use_patch(state, patch))
2029 patch->ws_rule = whitespace_rule(patch->new_name
2033 patchsize = parse_single_patch(state,
2034 buffer + offset + hdrsize,
2035 size - offset - hdrsize,
2042 static const char git_binary[] = "GIT binary patch\n";
2043 int hd = hdrsize + offset;
2044 unsigned long llen = linelen(buffer + hd, size - hd);
2046 if (llen == sizeof(git_binary) - 1 &&
2047 !memcmp(git_binary, buffer + hd, llen)) {
2050 used = parse_binary(state, buffer + hd + llen,
2051 size - hd - llen, patch);
2055 patchsize = used + llen;
2059 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2060 static const char *binhdr[] = {
2066 for (i = 0; binhdr[i]; i++) {
2067 int len = strlen(binhdr[i]);
2068 if (len < size - hd &&
2069 !memcmp(binhdr[i], buffer + hd, len)) {
2071 patch->is_binary = 1;
2078 /* Empty patch cannot be applied if it is a text patch
2079 * without metadata change. A binary patch appears
2082 if ((state->apply || state->check) &&
2083 (!patch->is_binary && !metadata_changes(patch))) {
2084 error(_("patch with only garbage at line %d"), state->linenr);
2089 return offset + hdrsize + patchsize;
2092 #define swap(a,b) myswap((a),(b),sizeof(a))
2094 #define myswap(a, b, size) do { \
2095 unsigned char mytmp[size]; \
2096 memcpy(mytmp, &a, size); \
2097 memcpy(&a, &b, size); \
2098 memcpy(&b, mytmp, size); \
2101 static void reverse_patches(struct patch *p)
2103 for (; p; p = p->next) {
2104 struct fragment *frag = p->fragments;
2106 swap(p->new_name, p->old_name);
2107 swap(p->new_mode, p->old_mode);
2108 swap(p->is_new, p->is_delete);
2109 swap(p->lines_added, p->lines_deleted);
2110 swap(p->old_sha1_prefix, p->new_sha1_prefix);
2112 for (; frag; frag = frag->next) {
2113 swap(frag->newpos, frag->oldpos);
2114 swap(frag->newlines, frag->oldlines);
2119 static const char pluses[] =
2120 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2121 static const char minuses[]=
2122 "----------------------------------------------------------------------";
2124 static void show_stats(struct apply_state *state, struct patch *patch)
2126 struct strbuf qname = STRBUF_INIT;
2127 char *cp = patch->new_name ? patch->new_name : patch->old_name;
2130 quote_c_style(cp, &qname, NULL, 0);
2133 * "scale" the filename
2135 max = state->max_len;
2139 if (qname.len > max) {
2140 cp = strchr(qname.buf + qname.len + 3 - max, '/');
2142 cp = qname.buf + qname.len + 3 - max;
2143 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2146 if (patch->is_binary) {
2147 printf(" %-*s | Bin\n", max, qname.buf);
2148 strbuf_release(&qname);
2152 printf(" %-*s |", max, qname.buf);
2153 strbuf_release(&qname);
2156 * scale the add/delete
2158 max = max + state->max_change > 70 ? 70 - max : state->max_change;
2159 add = patch->lines_added;
2160 del = patch->lines_deleted;
2162 if (state->max_change > 0) {
2163 int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2164 add = (add * max + state->max_change / 2) / state->max_change;
2167 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2168 add, pluses, del, minuses);
2171 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2173 switch (st->st_mode & S_IFMT) {
2175 if (strbuf_readlink(buf, path, st->st_size) < 0)
2176 return error(_("unable to read symlink %s"), path);
2179 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2180 return error(_("unable to open or read %s"), path);
2181 convert_to_git(path, buf->buf, buf->len, buf, 0);
2189 * Update the preimage, and the common lines in postimage,
2190 * from buffer buf of length len. If postlen is 0 the postimage
2191 * is updated in place, otherwise it's updated on a new buffer
2195 static void update_pre_post_images(struct image *preimage,
2196 struct image *postimage,
2198 size_t len, size_t postlen)
2200 int i, ctx, reduced;
2201 char *new, *old, *fixed;
2202 struct image fixed_preimage;
2205 * Update the preimage with whitespace fixes. Note that we
2206 * are not losing preimage->buf -- apply_one_fragment() will
2209 prepare_image(&fixed_preimage, buf, len, 1);
2211 ? fixed_preimage.nr == preimage->nr
2212 : fixed_preimage.nr <= preimage->nr);
2213 for (i = 0; i < fixed_preimage.nr; i++)
2214 fixed_preimage.line[i].flag = preimage->line[i].flag;
2215 free(preimage->line_allocated);
2216 *preimage = fixed_preimage;
2219 * Adjust the common context lines in postimage. This can be
2220 * done in-place when we are shrinking it with whitespace
2221 * fixing, but needs a new buffer when ignoring whitespace or
2222 * expanding leading tabs to spaces.
2224 * We trust the caller to tell us if the update can be done
2225 * in place (postlen==0) or not.
2227 old = postimage->buf;
2229 new = postimage->buf = xmalloc(postlen);
2232 fixed = preimage->buf;
2234 for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2235 size_t l_len = postimage->line[i].len;
2236 if (!(postimage->line[i].flag & LINE_COMMON)) {
2237 /* an added line -- no counterparts in preimage */
2238 memmove(new, old, l_len);
2244 /* a common context -- skip it in the original postimage */
2247 /* and find the corresponding one in the fixed preimage */
2248 while (ctx < preimage->nr &&
2249 !(preimage->line[ctx].flag & LINE_COMMON)) {
2250 fixed += preimage->line[ctx].len;
2255 * preimage is expected to run out, if the caller
2256 * fixed addition of trailing blank lines.
2258 if (preimage->nr <= ctx) {
2263 /* and copy it in, while fixing the line length */
2264 l_len = preimage->line[ctx].len;
2265 memcpy(new, fixed, l_len);
2268 postimage->line[i].len = l_len;
2273 ? postlen < new - postimage->buf
2274 : postimage->len < new - postimage->buf)
2275 die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2276 (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2278 /* Fix the length of the whole thing */
2279 postimage->len = new - postimage->buf;
2280 postimage->nr -= reduced;
2283 static int line_by_line_fuzzy_match(struct image *img,
2284 struct image *preimage,
2285 struct image *postimage,
2293 size_t postlen = postimage->len;
2298 struct strbuf fixed;
2302 for (i = 0; i < preimage_limit; i++) {
2303 size_t prelen = preimage->line[i].len;
2304 size_t imglen = img->line[try_lno+i].len;
2306 if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2307 preimage->buf + preoff, prelen))
2309 if (preimage->line[i].flag & LINE_COMMON)
2310 postlen += imglen - prelen;
2316 * Ok, the preimage matches with whitespace fuzz.
2318 * imgoff now holds the true length of the target that
2319 * matches the preimage before the end of the file.
2321 * Count the number of characters in the preimage that fall
2322 * beyond the end of the file and make sure that all of them
2323 * are whitespace characters. (This can only happen if
2324 * we are removing blank lines at the end of the file.)
2326 buf = preimage_eof = preimage->buf + preoff;
2327 for ( ; i < preimage->nr; i++)
2328 preoff += preimage->line[i].len;
2329 preimage_end = preimage->buf + preoff;
2330 for ( ; buf < preimage_end; buf++)
2335 * Update the preimage and the common postimage context
2336 * lines to use the same whitespace as the target.
2337 * If whitespace is missing in the target (i.e.
2338 * if the preimage extends beyond the end of the file),
2339 * use the whitespace from the preimage.
2341 extra_chars = preimage_end - preimage_eof;
2342 strbuf_init(&fixed, imgoff + extra_chars);
2343 strbuf_add(&fixed, img->buf + try, imgoff);
2344 strbuf_add(&fixed, preimage_eof, extra_chars);
2345 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2346 update_pre_post_images(preimage, postimage,
2347 fixed_buf, fixed_len, postlen);
2351 static int match_fragment(struct apply_state *state,
2353 struct image *preimage,
2354 struct image *postimage,
2358 int match_beginning, int match_end)
2361 char *fixed_buf, *buf, *orig, *target;
2362 struct strbuf fixed;
2363 size_t fixed_len, postlen;
2366 if (preimage->nr + try_lno <= img->nr) {
2368 * The hunk falls within the boundaries of img.
2370 preimage_limit = preimage->nr;
2371 if (match_end && (preimage->nr + try_lno != img->nr))
2373 } else if (state->ws_error_action == correct_ws_error &&
2374 (ws_rule & WS_BLANK_AT_EOF)) {
2376 * This hunk extends beyond the end of img, and we are
2377 * removing blank lines at the end of the file. This
2378 * many lines from the beginning of the preimage must
2379 * match with img, and the remainder of the preimage
2382 preimage_limit = img->nr - try_lno;
2385 * The hunk extends beyond the end of the img and
2386 * we are not removing blanks at the end, so we
2387 * should reject the hunk at this position.
2392 if (match_beginning && try_lno)
2395 /* Quick hash check */
2396 for (i = 0; i < preimage_limit; i++)
2397 if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2398 (preimage->line[i].hash != img->line[try_lno + i].hash))
2401 if (preimage_limit == preimage->nr) {
2403 * Do we have an exact match? If we were told to match
2404 * at the end, size must be exactly at try+fragsize,
2405 * otherwise try+fragsize must be still within the preimage,
2406 * and either case, the old piece should match the preimage
2410 ? (try + preimage->len == img->len)
2411 : (try + preimage->len <= img->len)) &&
2412 !memcmp(img->buf + try, preimage->buf, preimage->len))
2416 * The preimage extends beyond the end of img, so
2417 * there cannot be an exact match.
2419 * There must be one non-blank context line that match
2420 * a line before the end of img.
2424 buf = preimage->buf;
2426 for (i = 0; i < preimage_limit; i++)
2427 buf_end += preimage->line[i].len;
2429 for ( ; buf < buf_end; buf++)
2437 * No exact match. If we are ignoring whitespace, run a line-by-line
2438 * fuzzy matching. We collect all the line length information because
2439 * we need it to adjust whitespace if we match.
2441 if (state->ws_ignore_action == ignore_ws_change)
2442 return line_by_line_fuzzy_match(img, preimage, postimage,
2443 try, try_lno, preimage_limit);
2445 if (state->ws_error_action != correct_ws_error)
2449 * The hunk does not apply byte-by-byte, but the hash says
2450 * it might with whitespace fuzz. We weren't asked to
2451 * ignore whitespace, we were asked to correct whitespace
2452 * errors, so let's try matching after whitespace correction.
2454 * While checking the preimage against the target, whitespace
2455 * errors in both fixed, we count how large the corresponding
2456 * postimage needs to be. The postimage prepared by
2457 * apply_one_fragment() has whitespace errors fixed on added
2458 * lines already, but the common lines were propagated as-is,
2459 * which may become longer when their whitespace errors are
2463 /* First count added lines in postimage */
2465 for (i = 0; i < postimage->nr; i++) {
2466 if (!(postimage->line[i].flag & LINE_COMMON))
2467 postlen += postimage->line[i].len;
2471 * The preimage may extend beyond the end of the file,
2472 * but in this loop we will only handle the part of the
2473 * preimage that falls within the file.
2475 strbuf_init(&fixed, preimage->len + 1);
2476 orig = preimage->buf;
2477 target = img->buf + try;
2478 for (i = 0; i < preimage_limit; i++) {
2479 size_t oldlen = preimage->line[i].len;
2480 size_t tgtlen = img->line[try_lno + i].len;
2481 size_t fixstart = fixed.len;
2482 struct strbuf tgtfix;
2485 /* Try fixing the line in the preimage */
2486 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2488 /* Try fixing the line in the target */
2489 strbuf_init(&tgtfix, tgtlen);
2490 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2493 * If they match, either the preimage was based on
2494 * a version before our tree fixed whitespace breakage,
2495 * or we are lacking a whitespace-fix patch the tree
2496 * the preimage was based on already had (i.e. target
2497 * has whitespace breakage, the preimage doesn't).
2498 * In either case, we are fixing the whitespace breakages
2499 * so we might as well take the fix together with their
2502 match = (tgtfix.len == fixed.len - fixstart &&
2503 !memcmp(tgtfix.buf, fixed.buf + fixstart,
2504 fixed.len - fixstart));
2506 /* Add the length if this is common with the postimage */
2507 if (preimage->line[i].flag & LINE_COMMON)
2508 postlen += tgtfix.len;
2510 strbuf_release(&tgtfix);
2520 * Now handle the lines in the preimage that falls beyond the
2521 * end of the file (if any). They will only match if they are
2522 * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2525 for ( ; i < preimage->nr; i++) {
2526 size_t fixstart = fixed.len; /* start of the fixed preimage */
2527 size_t oldlen = preimage->line[i].len;
2530 /* Try fixing the line in the preimage */
2531 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2533 for (j = fixstart; j < fixed.len; j++)
2534 if (!isspace(fixed.buf[j]))
2541 * Yes, the preimage is based on an older version that still
2542 * has whitespace breakages unfixed, and fixing them makes the
2543 * hunk match. Update the context lines in the postimage.
2545 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2546 if (postlen < postimage->len)
2548 update_pre_post_images(preimage, postimage,
2549 fixed_buf, fixed_len, postlen);
2553 strbuf_release(&fixed);
2557 static int find_pos(struct apply_state *state,
2559 struct image *preimage,
2560 struct image *postimage,
2563 int match_beginning, int match_end)
2566 unsigned long backwards, forwards, try;
2567 int backwards_lno, forwards_lno, try_lno;
2570 * If match_beginning or match_end is specified, there is no
2571 * point starting from a wrong line that will never match and
2572 * wander around and wait for a match at the specified end.
2574 if (match_beginning)
2577 line = img->nr - preimage->nr;
2580 * Because the comparison is unsigned, the following test
2581 * will also take care of a negative line number that can
2582 * result when match_end and preimage is larger than the target.
2584 if ((size_t) line > img->nr)
2588 for (i = 0; i < line; i++)
2589 try += img->line[i].len;
2592 * There's probably some smart way to do this, but I'll leave
2593 * that to the smart and beautiful people. I'm simple and stupid.
2596 backwards_lno = line;
2598 forwards_lno = line;
2601 for (i = 0; ; i++) {
2602 if (match_fragment(state, img, preimage, postimage,
2603 try, try_lno, ws_rule,
2604 match_beginning, match_end))
2608 if (backwards_lno == 0 && forwards_lno == img->nr)
2612 if (backwards_lno == 0) {
2617 backwards -= img->line[backwards_lno].len;
2619 try_lno = backwards_lno;
2621 if (forwards_lno == img->nr) {
2625 forwards += img->line[forwards_lno].len;
2628 try_lno = forwards_lno;
2635 static void remove_first_line(struct image *img)
2637 img->buf += img->line[0].len;
2638 img->len -= img->line[0].len;
2643 static void remove_last_line(struct image *img)
2645 img->len -= img->line[--img->nr].len;
2649 * The change from "preimage" and "postimage" has been found to
2650 * apply at applied_pos (counts in line numbers) in "img".
2651 * Update "img" to remove "preimage" and replace it with "postimage".
2653 static void update_image(struct apply_state *state,
2656 struct image *preimage,
2657 struct image *postimage)
2660 * remove the copy of preimage at offset in img
2661 * and replace it with postimage
2664 size_t remove_count, insert_count, applied_at = 0;
2669 * If we are removing blank lines at the end of img,
2670 * the preimage may extend beyond the end.
2671 * If that is the case, we must be careful only to
2672 * remove the part of the preimage that falls within
2673 * the boundaries of img. Initialize preimage_limit
2674 * to the number of lines in the preimage that falls
2675 * within the boundaries.
2677 preimage_limit = preimage->nr;
2678 if (preimage_limit > img->nr - applied_pos)
2679 preimage_limit = img->nr - applied_pos;
2681 for (i = 0; i < applied_pos; i++)
2682 applied_at += img->line[i].len;
2685 for (i = 0; i < preimage_limit; i++)
2686 remove_count += img->line[applied_pos + i].len;
2687 insert_count = postimage->len;
2689 /* Adjust the contents */
2690 result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2691 memcpy(result, img->buf, applied_at);
2692 memcpy(result + applied_at, postimage->buf, postimage->len);
2693 memcpy(result + applied_at + postimage->len,
2694 img->buf + (applied_at + remove_count),
2695 img->len - (applied_at + remove_count));
2698 img->len += insert_count - remove_count;
2699 result[img->len] = '\0';
2701 /* Adjust the line table */
2702 nr = img->nr + postimage->nr - preimage_limit;
2703 if (preimage_limit < postimage->nr) {
2705 * NOTE: this knows that we never call remove_first_line()
2706 * on anything other than pre/post image.
2708 REALLOC_ARRAY(img->line, nr);
2709 img->line_allocated = img->line;
2711 if (preimage_limit != postimage->nr)
2712 memmove(img->line + applied_pos + postimage->nr,
2713 img->line + applied_pos + preimage_limit,
2714 (img->nr - (applied_pos + preimage_limit)) *
2715 sizeof(*img->line));
2716 memcpy(img->line + applied_pos,
2718 postimage->nr * sizeof(*img->line));
2719 if (!state->allow_overlap)
2720 for (i = 0; i < postimage->nr; i++)
2721 img->line[applied_pos + i].flag |= LINE_PATCHED;
2726 * Use the patch-hunk text in "frag" to prepare two images (preimage and
2727 * postimage) for the hunk. Find lines that match "preimage" in "img" and
2728 * replace the part of "img" with "postimage" text.
2730 static int apply_one_fragment(struct apply_state *state,
2731 struct image *img, struct fragment *frag,
2732 int inaccurate_eof, unsigned ws_rule,
2735 int match_beginning, match_end;
2736 const char *patch = frag->patch;
2737 int size = frag->size;
2738 char *old, *oldlines;
2739 struct strbuf newlines;
2740 int new_blank_lines_at_end = 0;
2741 int found_new_blank_lines_at_end = 0;
2742 int hunk_linenr = frag->linenr;
2743 unsigned long leading, trailing;
2744 int pos, applied_pos;
2745 struct image preimage;
2746 struct image postimage;
2748 memset(&preimage, 0, sizeof(preimage));
2749 memset(&postimage, 0, sizeof(postimage));
2750 oldlines = xmalloc(size);
2751 strbuf_init(&newlines, size);
2756 int len = linelen(patch, size);
2758 int added_blank_line = 0;
2759 int is_blank_context = 0;
2766 * "plen" is how much of the line we should use for
2767 * the actual patch data. Normally we just remove the
2768 * first character on the line, but if the line is
2769 * followed by "\ No newline", then we also remove the
2770 * last one (which is the newline, of course).
2773 if (len < size && patch[len] == '\\')
2776 if (state->apply_in_reverse) {
2779 else if (first == '+')
2785 /* Newer GNU diff, empty context line */
2787 /* ... followed by '\No newline'; nothing */
2790 strbuf_addch(&newlines, '\n');
2791 add_line_info(&preimage, "\n", 1, LINE_COMMON);
2792 add_line_info(&postimage, "\n", 1, LINE_COMMON);
2793 is_blank_context = 1;
2796 if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2797 ws_blank_line(patch + 1, plen, ws_rule))
2798 is_blank_context = 1;
2800 memcpy(old, patch + 1, plen);
2801 add_line_info(&preimage, old, plen,
2802 (first == ' ' ? LINE_COMMON : 0));
2806 /* Fall-through for ' ' */
2808 /* --no-add does not add new lines */
2809 if (first == '+' && state->no_add)
2812 start = newlines.len;
2814 !state->whitespace_error ||
2815 state->ws_error_action != correct_ws_error) {
2816 strbuf_add(&newlines, patch + 1, plen);
2819 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2821 add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2822 (first == '+' ? 0 : LINE_COMMON));
2824 (ws_rule & WS_BLANK_AT_EOF) &&
2825 ws_blank_line(patch + 1, plen, ws_rule))
2826 added_blank_line = 1;
2828 case '@': case '\\':
2829 /* Ignore it, we already handled it */
2832 if (state->apply_verbosely)
2833 error(_("invalid start of line: '%c'"), first);
2837 if (added_blank_line) {
2838 if (!new_blank_lines_at_end)
2839 found_new_blank_lines_at_end = hunk_linenr;
2840 new_blank_lines_at_end++;
2842 else if (is_blank_context)
2845 new_blank_lines_at_end = 0;
2850 if (inaccurate_eof &&
2851 old > oldlines && old[-1] == '\n' &&
2852 newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2854 strbuf_setlen(&newlines, newlines.len - 1);
2857 leading = frag->leading;
2858 trailing = frag->trailing;
2861 * A hunk to change lines at the beginning would begin with
2863 * but we need to be careful. -U0 that inserts before the second
2864 * line also has this pattern.
2866 * And a hunk to add to an empty file would begin with
2869 * In other words, a hunk that is (frag->oldpos <= 1) with or
2870 * without leading context must match at the beginning.
2872 match_beginning = (!frag->oldpos ||
2873 (frag->oldpos == 1 && !state->unidiff_zero));
2876 * A hunk without trailing lines must match at the end.
2877 * However, we simply cannot tell if a hunk must match end
2878 * from the lack of trailing lines if the patch was generated
2879 * with unidiff without any context.
2881 match_end = !state->unidiff_zero && !trailing;
2883 pos = frag->newpos ? (frag->newpos - 1) : 0;
2884 preimage.buf = oldlines;
2885 preimage.len = old - oldlines;
2886 postimage.buf = newlines.buf;
2887 postimage.len = newlines.len;
2888 preimage.line = preimage.line_allocated;
2889 postimage.line = postimage.line_allocated;
2893 applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2894 ws_rule, match_beginning, match_end);
2896 if (applied_pos >= 0)
2899 /* Am I at my context limits? */
2900 if ((leading <= state->p_context) && (trailing <= state->p_context))
2902 if (match_beginning || match_end) {
2903 match_beginning = match_end = 0;
2908 * Reduce the number of context lines; reduce both
2909 * leading and trailing if they are equal otherwise
2910 * just reduce the larger context.
2912 if (leading >= trailing) {
2913 remove_first_line(&preimage);
2914 remove_first_line(&postimage);
2918 if (trailing > leading) {
2919 remove_last_line(&preimage);
2920 remove_last_line(&postimage);
2925 if (applied_pos >= 0) {
2926 if (new_blank_lines_at_end &&
2927 preimage.nr + applied_pos >= img->nr &&
2928 (ws_rule & WS_BLANK_AT_EOF) &&
2929 state->ws_error_action != nowarn_ws_error) {
2930 record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
2931 found_new_blank_lines_at_end);
2932 if (state->ws_error_action == correct_ws_error) {
2933 while (new_blank_lines_at_end--)
2934 remove_last_line(&postimage);
2937 * We would want to prevent write_out_results()
2938 * from taking place in apply_patch() that follows
2939 * the callchain led us here, which is:
2940 * apply_patch->check_patch_list->check_patch->
2941 * apply_data->apply_fragments->apply_one_fragment
2943 if (state->ws_error_action == die_on_ws_error)
2947 if (state->apply_verbosely && applied_pos != pos) {
2948 int offset = applied_pos - pos;
2949 if (state->apply_in_reverse)
2950 offset = 0 - offset;
2952 Q_("Hunk #%d succeeded at %d (offset %d line).",
2953 "Hunk #%d succeeded at %d (offset %d lines).",
2955 nth_fragment, applied_pos + 1, offset);
2959 * Warn if it was necessary to reduce the number
2962 if ((leading != frag->leading) ||
2963 (trailing != frag->trailing))
2964 fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
2965 " to apply fragment at %d"),
2966 leading, trailing, applied_pos+1);
2967 update_image(state, img, applied_pos, &preimage, &postimage);
2969 if (state->apply_verbosely)
2970 error(_("while searching for:\n%.*s"),
2971 (int)(old - oldlines), oldlines);
2976 strbuf_release(&newlines);
2977 free(preimage.line_allocated);
2978 free(postimage.line_allocated);
2980 return (applied_pos < 0);
2983 static int apply_binary_fragment(struct apply_state *state,
2985 struct patch *patch)
2987 struct fragment *fragment = patch->fragments;
2992 return error(_("missing binary patch data for '%s'"),
2997 /* Binary patch is irreversible without the optional second hunk */
2998 if (state->apply_in_reverse) {
2999 if (!fragment->next)
3000 return error("cannot reverse-apply a binary patch "
3001 "without the reverse hunk to '%s'",
3003 ? patch->new_name : patch->old_name);
3004 fragment = fragment->next;
3006 switch (fragment->binary_patch_method) {
3007 case BINARY_DELTA_DEFLATED:
3008 dst = patch_delta(img->buf, img->len, fragment->patch,
3009 fragment->size, &len);
3016 case BINARY_LITERAL_DEFLATED:
3018 img->len = fragment->size;
3019 img->buf = xmemdupz(fragment->patch, img->len);
3026 * Replace "img" with the result of applying the binary patch.
3027 * The binary patch data itself in patch->fragment is still kept
3028 * but the preimage prepared by the caller in "img" is freed here
3029 * or in the helper function apply_binary_fragment() this calls.
3031 static int apply_binary(struct apply_state *state,
3033 struct patch *patch)
3035 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3036 unsigned char sha1[20];
3039 * For safety, we require patch index line to contain
3040 * full 40-byte textual SHA1 for old and new, at least for now.
3042 if (strlen(patch->old_sha1_prefix) != 40 ||
3043 strlen(patch->new_sha1_prefix) != 40 ||
3044 get_sha1_hex(patch->old_sha1_prefix, sha1) ||
3045 get_sha1_hex(patch->new_sha1_prefix, sha1))
3046 return error("cannot apply binary patch to '%s' "
3047 "without full index line", name);
3049 if (patch->old_name) {
3051 * See if the old one matches what the patch
3054 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3055 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3056 return error("the patch applies to '%s' (%s), "
3057 "which does not match the "
3058 "current contents.",
3059 name, sha1_to_hex(sha1));
3062 /* Otherwise, the old one must be empty. */
3064 return error("the patch applies to an empty "
3065 "'%s' but it is not empty", name);
3068 get_sha1_hex(patch->new_sha1_prefix, sha1);
3069 if (is_null_sha1(sha1)) {
3071 return 0; /* deletion patch */
3074 if (has_sha1_file(sha1)) {
3075 /* We already have the postimage */
3076 enum object_type type;
3080 result = read_sha1_file(sha1, &type, &size);
3082 return error("the necessary postimage %s for "
3083 "'%s' cannot be read",
3084 patch->new_sha1_prefix, name);
3090 * We have verified buf matches the preimage;
3091 * apply the patch data to it, which is stored
3092 * in the patch->fragments->{patch,size}.
3094 if (apply_binary_fragment(state, img, patch))
3095 return error(_("binary patch does not apply to '%s'"),
3098 /* verify that the result matches */
3099 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3100 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3101 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3102 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3108 static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3110 struct fragment *frag = patch->fragments;
3111 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3112 unsigned ws_rule = patch->ws_rule;
3113 unsigned inaccurate_eof = patch->inaccurate_eof;
3116 if (patch->is_binary)
3117 return apply_binary(state, img, patch);
3121 if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3122 error(_("patch failed: %s:%ld"), name, frag->oldpos);
3123 if (!state->apply_with_reject)
3132 static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3134 if (S_ISGITLINK(mode)) {
3135 strbuf_grow(buf, 100);
3136 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3138 enum object_type type;
3142 result = read_sha1_file(sha1, &type, &sz);
3145 /* XXX read_sha1_file NUL-terminates */
3146 strbuf_attach(buf, result, sz, sz + 1);
3151 static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3155 return read_blob_object(buf, ce->sha1, ce->ce_mode);
3158 static struct patch *in_fn_table(struct apply_state *state, const char *name)
3160 struct string_list_item *item;
3165 item = string_list_lookup(&state->fn_table, name);
3167 return (struct patch *)item->util;
3173 * item->util in the filename table records the status of the path.
3174 * Usually it points at a patch (whose result records the contents
3175 * of it after applying it), but it could be PATH_WAS_DELETED for a
3176 * path that a previously applied patch has already removed, or
3177 * PATH_TO_BE_DELETED for a path that a later patch would remove.
3179 * The latter is needed to deal with a case where two paths A and B
3180 * are swapped by first renaming A to B and then renaming B to A;
3181 * moving A to B should not be prevented due to presence of B as we
3182 * will remove it in a later patch.
3184 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3185 #define PATH_WAS_DELETED ((struct patch *) -1)
3187 static int to_be_deleted(struct patch *patch)
3189 return patch == PATH_TO_BE_DELETED;
3192 static int was_deleted(struct patch *patch)
3194 return patch == PATH_WAS_DELETED;
3197 static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3199 struct string_list_item *item;
3202 * Always add new_name unless patch is a deletion
3203 * This should cover the cases for normal diffs,
3204 * file creations and copies
3206 if (patch->new_name != NULL) {
3207 item = string_list_insert(&state->fn_table, patch->new_name);
3212 * store a failure on rename/deletion cases because
3213 * later chunks shouldn't patch old names
3215 if ((patch->new_name == NULL) || (patch->is_rename)) {
3216 item = string_list_insert(&state->fn_table, patch->old_name);
3217 item->util = PATH_WAS_DELETED;
3221 static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3224 * store information about incoming file deletion
3227 if ((patch->new_name == NULL) || (patch->is_rename)) {
3228 struct string_list_item *item;
3229 item = string_list_insert(&state->fn_table, patch->old_name);
3230 item->util = PATH_TO_BE_DELETED;
3232 patch = patch->next;
3236 static int checkout_target(struct index_state *istate,
3237 struct cache_entry *ce, struct stat *st)
3239 struct checkout costate;
3241 memset(&costate, 0, sizeof(costate));
3242 costate.base_dir = "";
3243 costate.refresh_cache = 1;
3244 costate.istate = istate;
3245 if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3246 return error(_("cannot checkout %s"), ce->name);
3250 static struct patch *previous_patch(struct apply_state *state,
3251 struct patch *patch,
3254 struct patch *previous;
3257 if (patch->is_copy || patch->is_rename)
3258 return NULL; /* "git" patches do not depend on the order */
3260 previous = in_fn_table(state, patch->old_name);
3264 if (to_be_deleted(previous))
3265 return NULL; /* the deletion hasn't happened yet */
3267 if (was_deleted(previous))
3273 static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3275 if (S_ISGITLINK(ce->ce_mode)) {
3276 if (!S_ISDIR(st->st_mode))
3280 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3283 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3285 static int load_patch_target(struct apply_state *state,
3287 const struct cache_entry *ce,
3290 unsigned expected_mode)
3292 if (state->cached || state->check_index) {
3293 if (read_file_or_gitlink(ce, buf))
3294 return error(_("failed to read %s"), name);
3296 if (S_ISGITLINK(expected_mode)) {
3298 return read_file_or_gitlink(ce, buf);
3300 return SUBMODULE_PATCH_WITHOUT_INDEX;
3301 } else if (has_symlink_leading_path(name, strlen(name))) {
3302 return error(_("reading from '%s' beyond a symbolic link"), name);
3304 if (read_old_data(st, name, buf))
3305 return error(_("failed to read %s"), name);
3312 * We are about to apply "patch"; populate the "image" with the
3313 * current version we have, from the working tree or from the index,
3314 * depending on the situation e.g. --cached/--index. If we are
3315 * applying a non-git patch that incrementally updates the tree,
3316 * we read from the result of a previous diff.
3318 static int load_preimage(struct apply_state *state,
3319 struct image *image,
3320 struct patch *patch, struct stat *st,
3321 const struct cache_entry *ce)
3323 struct strbuf buf = STRBUF_INIT;
3326 struct patch *previous;
3329 previous = previous_patch(state, patch, &status);
3331 return error(_("path %s has been renamed/deleted"),
3334 /* We have a patched copy in memory; use that. */
3335 strbuf_add(&buf, previous->result, previous->resultsize);
3337 status = load_patch_target(state, &buf, ce, st,
3338 patch->old_name, patch->old_mode);
3341 else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3343 * There is no way to apply subproject
3344 * patch without looking at the index.
3345 * NEEDSWORK: shouldn't this be flagged
3348 free_fragment_list(patch->fragments);
3349 patch->fragments = NULL;
3350 } else if (status) {
3351 return error(_("failed to read %s"), patch->old_name);
3355 img = strbuf_detach(&buf, &len);
3356 prepare_image(image, img, len, !patch->is_binary);
3360 static int three_way_merge(struct image *image,
3362 const unsigned char *base,
3363 const unsigned char *ours,
3364 const unsigned char *theirs)
3366 mmfile_t base_file, our_file, their_file;
3367 mmbuffer_t result = { NULL };
3370 read_mmblob(&base_file, base);
3371 read_mmblob(&our_file, ours);
3372 read_mmblob(&their_file, theirs);
3373 status = ll_merge(&result, path,
3376 &their_file, "theirs", NULL);
3377 free(base_file.ptr);
3379 free(their_file.ptr);
3380 if (status < 0 || !result.ptr) {
3385 image->buf = result.ptr;
3386 image->len = result.size;
3392 * When directly falling back to add/add three-way merge, we read from
3393 * the current contents of the new_name. In no cases other than that
3394 * this function will be called.
3396 static int load_current(struct apply_state *state,
3397 struct image *image,
3398 struct patch *patch)
3400 struct strbuf buf = STRBUF_INIT;
3405 struct cache_entry *ce;
3406 char *name = patch->new_name;
3407 unsigned mode = patch->new_mode;
3410 die("BUG: patch to %s is not a creation", patch->old_name);
3412 pos = cache_name_pos(name, strlen(name));
3414 return error(_("%s: does not exist in index"), name);
3415 ce = active_cache[pos];
3416 if (lstat(name, &st)) {
3417 if (errno != ENOENT)
3418 return error(_("%s: %s"), name, strerror(errno));
3419 if (checkout_target(&the_index, ce, &st))
3422 if (verify_index_match(ce, &st))
3423 return error(_("%s: does not match index"), name);
3425 status = load_patch_target(state, &buf, ce, &st, name, mode);
3430 img = strbuf_detach(&buf, &len);
3431 prepare_image(image, img, len, !patch->is_binary);
3435 static int try_threeway(struct apply_state *state,
3436 struct image *image,
3437 struct patch *patch,
3439 const struct cache_entry *ce)
3441 unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3442 struct strbuf buf = STRBUF_INIT;
3446 struct image tmp_image;
3448 /* No point falling back to 3-way merge in these cases */
3449 if (patch->is_delete ||
3450 S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3453 /* Preimage the patch was prepared for */
3455 write_sha1_file("", 0, blob_type, pre_sha1);
3456 else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3457 read_blob_object(&buf, pre_sha1, patch->old_mode))
3458 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3460 fprintf(stderr, "Falling back to three-way merge...\n");
3462 img = strbuf_detach(&buf, &len);
3463 prepare_image(&tmp_image, img, len, 1);
3464 /* Apply the patch to get the post image */
3465 if (apply_fragments(state, &tmp_image, patch) < 0) {
3466 clear_image(&tmp_image);
3469 /* post_sha1[] is theirs */
3470 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3471 clear_image(&tmp_image);
3473 /* our_sha1[] is ours */
3474 if (patch->is_new) {
3475 if (load_current(state, &tmp_image, patch))
3476 return error("cannot read the current contents of '%s'",
3479 if (load_preimage(state, &tmp_image, patch, st, ce))
3480 return error("cannot read the current contents of '%s'",
3483 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3484 clear_image(&tmp_image);
3486 /* in-core three-way merge between post and our using pre as base */
3487 status = three_way_merge(image, patch->new_name,
3488 pre_sha1, our_sha1, post_sha1);
3490 fprintf(stderr, "Failed to fall back on three-way merge...\n");
3495 patch->conflicted_threeway = 1;
3497 oidclr(&patch->threeway_stage[0]);
3499 hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3500 hashcpy(patch->threeway_stage[1].hash, our_sha1);
3501 hashcpy(patch->threeway_stage[2].hash, post_sha1);
3502 fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3504 fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3509 static int apply_data(struct apply_state *state, struct patch *patch,
3510 struct stat *st, const struct cache_entry *ce)
3514 if (load_preimage(state, &image, patch, st, ce) < 0)
3517 if (patch->direct_to_threeway ||
3518 apply_fragments(state, &image, patch) < 0) {
3519 /* Note: with --reject, apply_fragments() returns 0 */
3520 if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
3523 patch->result = image.buf;
3524 patch->resultsize = image.len;
3525 add_to_fn_table(state, patch);
3526 free(image.line_allocated);
3528 if (0 < patch->is_delete && patch->resultsize)
3529 return error(_("removal patch leaves file contents"));
3535 * If "patch" that we are looking at modifies or deletes what we have,
3536 * we would want it not to lose any local modification we have, either
3537 * in the working tree or in the index.
3539 * This also decides if a non-git patch is a creation patch or a
3540 * modification to an existing empty file. We do not check the state
3541 * of the current tree for a creation patch in this function; the caller
3542 * check_patch() separately makes sure (and errors out otherwise) that
3543 * the path the patch creates does not exist in the current tree.
3545 static int check_preimage(struct apply_state *state,
3546 struct patch *patch,
3547 struct cache_entry **ce,
3550 const char *old_name = patch->old_name;
3551 struct patch *previous = NULL;
3552 int stat_ret = 0, status;
3553 unsigned st_mode = 0;
3558 assert(patch->is_new <= 0);
3559 previous = previous_patch(state, patch, &status);
3562 return error(_("path %s has been renamed/deleted"), old_name);
3564 st_mode = previous->new_mode;
3565 } else if (!state->cached) {
3566 stat_ret = lstat(old_name, st);
3567 if (stat_ret && errno != ENOENT)
3568 return error(_("%s: %s"), old_name, strerror(errno));
3571 if (state->check_index && !previous) {
3572 int pos = cache_name_pos(old_name, strlen(old_name));
3574 if (patch->is_new < 0)
3576 return error(_("%s: does not exist in index"), old_name);
3578 *ce = active_cache[pos];
3580 if (checkout_target(&the_index, *ce, st))
3583 if (!state->cached && verify_index_match(*ce, st))
3584 return error(_("%s: does not match index"), old_name);
3586 st_mode = (*ce)->ce_mode;
3587 } else if (stat_ret < 0) {
3588 if (patch->is_new < 0)
3590 return error(_("%s: %s"), old_name, strerror(errno));
3593 if (!state->cached && !previous)
3594 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3596 if (patch->is_new < 0)
3598 if (!patch->old_mode)
3599 patch->old_mode = st_mode;
3600 if ((st_mode ^ patch->old_mode) & S_IFMT)
3601 return error(_("%s: wrong type"), old_name);
3602 if (st_mode != patch->old_mode)
3603 warning(_("%s has type %o, expected %o"),
3604 old_name, st_mode, patch->old_mode);
3605 if (!patch->new_mode && !patch->is_delete)
3606 patch->new_mode = st_mode;
3611 patch->is_delete = 0;
3612 free(patch->old_name);
3613 patch->old_name = NULL;
3618 #define EXISTS_IN_INDEX 1
3619 #define EXISTS_IN_WORKTREE 2
3621 static int check_to_create(struct apply_state *state,
3622 const char *new_name,
3627 if (state->check_index &&
3628 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3630 return EXISTS_IN_INDEX;
3634 if (!lstat(new_name, &nst)) {
3635 if (S_ISDIR(nst.st_mode) || ok_if_exists)
3638 * A leading component of new_name might be a symlink
3639 * that is going to be removed with this patch, but
3640 * still pointing at somewhere that has the path.
3641 * In such a case, path "new_name" does not exist as
3642 * far as git is concerned.
3644 if (has_symlink_leading_path(new_name, strlen(new_name)))
3647 return EXISTS_IN_WORKTREE;
3648 } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3649 return error("%s: %s", new_name, strerror(errno));
3654 static uintptr_t register_symlink_changes(struct apply_state *state,
3658 struct string_list_item *ent;
3660 ent = string_list_lookup(&state->symlink_changes, path);
3662 ent = string_list_insert(&state->symlink_changes, path);
3663 ent->util = (void *)0;
3665 ent->util = (void *)(what | ((uintptr_t)ent->util));
3666 return (uintptr_t)ent->util;
3669 static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3671 struct string_list_item *ent;
3673 ent = string_list_lookup(&state->symlink_changes, path);
3676 return (uintptr_t)ent->util;
3679 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3681 for ( ; patch; patch = patch->next) {
3682 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3683 (patch->is_rename || patch->is_delete))
3684 /* the symlink at patch->old_name is removed */
3685 register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3687 if (patch->new_name && S_ISLNK(patch->new_mode))
3688 /* the symlink at patch->new_name is created or remains */
3689 register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3693 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3696 unsigned int change;
3698 while (--name->len && name->buf[name->len] != '/')
3699 ; /* scan backwards */
3702 name->buf[name->len] = '\0';
3703 change = check_symlink_changes(state, name->buf);
3704 if (change & APPLY_SYMLINK_IN_RESULT)
3706 if (change & APPLY_SYMLINK_GOES_AWAY)
3708 * This cannot be "return 0", because we may
3709 * see a new one created at a higher level.
3713 /* otherwise, check the preimage */
3714 if (state->check_index) {
3715 struct cache_entry *ce;
3717 ce = cache_file_exists(name->buf, name->len, ignore_case);
3718 if (ce && S_ISLNK(ce->ce_mode))
3722 if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3729 static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3732 struct strbuf name = STRBUF_INIT;
3734 assert(*name_ != '\0');
3735 strbuf_addstr(&name, name_);
3736 ret = path_is_beyond_symlink_1(state, &name);
3737 strbuf_release(&name);
3742 static void die_on_unsafe_path(struct patch *patch)
3744 const char *old_name = NULL;
3745 const char *new_name = NULL;
3746 if (patch->is_delete)
3747 old_name = patch->old_name;
3748 else if (!patch->is_new && !patch->is_copy)
3749 old_name = patch->old_name;
3750 if (!patch->is_delete)
3751 new_name = patch->new_name;
3753 if (old_name && !verify_path(old_name))
3754 die(_("invalid path '%s'"), old_name);
3755 if (new_name && !verify_path(new_name))
3756 die(_("invalid path '%s'"), new_name);
3760 * Check and apply the patch in-core; leave the result in patch->result
3761 * for the caller to write it out to the final destination.
3763 static int check_patch(struct apply_state *state, struct patch *patch)
3766 const char *old_name = patch->old_name;
3767 const char *new_name = patch->new_name;
3768 const char *name = old_name ? old_name : new_name;
3769 struct cache_entry *ce = NULL;
3770 struct patch *tpatch;
3774 patch->rejected = 1; /* we will drop this after we succeed */
3776 status = check_preimage(state, patch, &ce, &st);
3779 old_name = patch->old_name;
3782 * A type-change diff is always split into a patch to delete
3783 * old, immediately followed by a patch to create new (see
3784 * diff.c::run_diff()); in such a case it is Ok that the entry
3785 * to be deleted by the previous patch is still in the working
3786 * tree and in the index.
3788 * A patch to swap-rename between A and B would first rename A
3789 * to B and then rename B to A. While applying the first one,
3790 * the presence of B should not stop A from getting renamed to
3791 * B; ask to_be_deleted() about the later rename. Removal of
3792 * B and rename from A to B is handled the same way by asking
3795 if ((tpatch = in_fn_table(state, new_name)) &&
3796 (was_deleted(tpatch) || to_be_deleted(tpatch)))
3802 ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3803 int err = check_to_create(state, new_name, ok_if_exists);
3805 if (err && state->threeway) {
3806 patch->direct_to_threeway = 1;
3807 } else switch (err) {
3810 case EXISTS_IN_INDEX:
3811 return error(_("%s: already exists in index"), new_name);
3813 case EXISTS_IN_WORKTREE:
3814 return error(_("%s: already exists in working directory"),
3820 if (!patch->new_mode) {
3821 if (0 < patch->is_new)
3822 patch->new_mode = S_IFREG | 0644;
3824 patch->new_mode = patch->old_mode;
3828 if (new_name && old_name) {
3829 int same = !strcmp(old_name, new_name);
3830 if (!patch->new_mode)
3831 patch->new_mode = patch->old_mode;
3832 if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3834 return error(_("new mode (%o) of %s does not "
3835 "match old mode (%o)"),
3836 patch->new_mode, new_name,
3839 return error(_("new mode (%o) of %s does not "
3840 "match old mode (%o) of %s"),
3841 patch->new_mode, new_name,
3842 patch->old_mode, old_name);
3846 if (!state->unsafe_paths)
3847 die_on_unsafe_path(patch);
3850 * An attempt to read from or delete a path that is beyond a
3851 * symbolic link will be prevented by load_patch_target() that
3852 * is called at the beginning of apply_data() so we do not
3853 * have to worry about a patch marked with "is_delete" bit
3854 * here. We however need to make sure that the patch result
3855 * is not deposited to a path that is beyond a symbolic link
3858 if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3859 return error(_("affected file '%s' is beyond a symbolic link"),
3862 if (apply_data(state, patch, &st, ce) < 0)
3863 return error(_("%s: patch does not apply"), name);
3864 patch->rejected = 0;
3868 static int check_patch_list(struct apply_state *state, struct patch *patch)
3872 prepare_symlink_changes(state, patch);
3873 prepare_fn_table(state, patch);
3875 if (state->apply_verbosely)
3876 say_patch_name(stderr,
3877 _("Checking patch %s..."), patch);
3878 err |= check_patch(state, patch);
3879 patch = patch->next;
3884 /* This function tries to read the sha1 from the current index */
3885 static int get_current_sha1(const char *path, unsigned char *sha1)
3889 if (read_cache() < 0)
3891 pos = cache_name_pos(path, strlen(path));
3894 hashcpy(sha1, active_cache[pos]->sha1);
3898 static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3901 * A usable gitlink patch has only one fragment (hunk) that looks like:
3903 * -Subproject commit <old sha1>
3904 * +Subproject commit <new sha1>
3907 * -Subproject commit <old sha1>
3908 * for a removal patch.
3910 struct fragment *hunk = p->fragments;
3911 static const char heading[] = "-Subproject commit ";
3914 if (/* does the patch have only one hunk? */
3915 hunk && !hunk->next &&
3916 /* is its preimage one line? */
3917 hunk->oldpos == 1 && hunk->oldlines == 1 &&
3918 /* does preimage begin with the heading? */
3919 (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3920 starts_with(++preimage, heading) &&
3921 /* does it record full SHA-1? */
3922 !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
3923 preimage[sizeof(heading) + 40 - 1] == '\n' &&
3924 /* does the abbreviated name on the index line agree with it? */
3925 starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3926 return 0; /* it all looks fine */
3928 /* we may have full object name on the index line */
3929 return get_sha1_hex(p->old_sha1_prefix, sha1);
3932 /* Build an index that contains the just the files needed for a 3way merge */
3933 static void build_fake_ancestor(struct patch *list, const char *filename)
3935 struct patch *patch;
3936 struct index_state result = { NULL };
3937 static struct lock_file lock;
3939 /* Once we start supporting the reverse patch, it may be
3940 * worth showing the new sha1 prefix, but until then...
3942 for (patch = list; patch; patch = patch->next) {
3943 unsigned char sha1[20];
3944 struct cache_entry *ce;
3947 name = patch->old_name ? patch->old_name : patch->new_name;
3948 if (0 < patch->is_new)
3951 if (S_ISGITLINK(patch->old_mode)) {
3952 if (!preimage_sha1_in_gitlink_patch(patch, sha1))
3953 ; /* ok, the textual part looks sane */
3955 die("sha1 information is lacking or useless for submodule %s",
3957 } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
3959 } else if (!patch->lines_added && !patch->lines_deleted) {
3960 /* mode-only change: update the current */
3961 if (get_current_sha1(patch->old_name, sha1))
3962 die("mode change for %s, which is not "
3963 "in current HEAD", name);
3965 die("sha1 information is lacking or useless "
3968 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
3970 die(_("make_cache_entry failed for path '%s'"), name);
3971 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
3972 die ("Could not add %s to temporary index", name);
3975 hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
3976 if (write_locked_index(&result, &lock, COMMIT_LOCK))
3977 die ("Could not write temporary index to %s", filename);
3979 discard_index(&result);
3982 static void stat_patch_list(struct apply_state *state, struct patch *patch)
3984 int files, adds, dels;
3986 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
3988 adds += patch->lines_added;
3989 dels += patch->lines_deleted;
3990 show_stats(state, patch);
3993 print_stat_summary(stdout, files, adds, dels);
3996 static void numstat_patch_list(struct apply_state *state,
3997 struct patch *patch)
3999 for ( ; patch; patch = patch->next) {
4001 name = patch->new_name ? patch->new_name : patch->old_name;
4002 if (patch->is_binary)
4005 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
4006 write_name_quoted(name, stdout, state->line_termination);
4010 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
4013 printf(" %s mode %06o %s\n", newdelete, mode, name);
4015 printf(" %s %s\n", newdelete, name);
4018 static void show_mode_change(struct patch *p, int show_name)
4020 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
4022 printf(" mode change %06o => %06o %s\n",
4023 p->old_mode, p->new_mode, p->new_name);
4025 printf(" mode change %06o => %06o\n",
4026 p->old_mode, p->new_mode);
4030 static void show_rename_copy(struct patch *p)
4032 const char *renamecopy = p->is_rename ? "rename" : "copy";
4033 const char *old, *new;
4035 /* Find common prefix */
4039 const char *slash_old, *slash_new;
4040 slash_old = strchr(old, '/');
4041 slash_new = strchr(new, '/');
4044 slash_old - old != slash_new - new ||
4045 memcmp(old, new, slash_new - new))
4047 old = slash_old + 1;
4048 new = slash_new + 1;
4050 /* p->old_name thru old is the common prefix, and old and new
4051 * through the end of names are renames
4053 if (old != p->old_name)
4054 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4055 (int)(old - p->old_name), p->old_name,
4056 old, new, p->score);
4058 printf(" %s %s => %s (%d%%)\n", renamecopy,
4059 p->old_name, p->new_name, p->score);
4060 show_mode_change(p, 0);
4063 static void summary_patch_list(struct patch *patch)
4067 for (p = patch; p; p = p->next) {
4069 show_file_mode_name("create", p->new_mode, p->new_name);
4070 else if (p->is_delete)
4071 show_file_mode_name("delete", p->old_mode, p->old_name);
4073 if (p->is_rename || p->is_copy)
4074 show_rename_copy(p);
4077 printf(" rewrite %s (%d%%)\n",
4078 p->new_name, p->score);
4079 show_mode_change(p, 0);
4082 show_mode_change(p, 1);
4088 static void patch_stats(struct apply_state *state, struct patch *patch)
4090 int lines = patch->lines_added + patch->lines_deleted;
4092 if (lines > state->max_change)
4093 state->max_change = lines;
4094 if (patch->old_name) {
4095 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4097 len = strlen(patch->old_name);
4098 if (len > state->max_len)
4099 state->max_len = len;
4101 if (patch->new_name) {
4102 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4104 len = strlen(patch->new_name);
4105 if (len > state->max_len)
4106 state->max_len = len;
4110 static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4112 if (state->update_index) {
4113 if (remove_file_from_cache(patch->old_name) < 0)
4114 die(_("unable to remove %s from index"), patch->old_name);
4116 if (!state->cached) {
4117 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4118 remove_path(patch->old_name);
4123 static void add_index_file(struct apply_state *state,
4130 struct cache_entry *ce;
4131 int namelen = strlen(path);
4132 unsigned ce_size = cache_entry_size(namelen);
4134 if (!state->update_index)
4137 ce = xcalloc(1, ce_size);
4138 memcpy(ce->name, path, namelen);
4139 ce->ce_mode = create_ce_mode(mode);
4140 ce->ce_flags = create_ce_flags(0);
4141 ce->ce_namelen = namelen;
4142 if (S_ISGITLINK(mode)) {
4145 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4146 get_sha1_hex(s, ce->sha1))
4147 die(_("corrupt patch for submodule %s"), path);
4149 if (!state->cached) {
4150 if (lstat(path, &st) < 0)
4151 die_errno(_("unable to stat newly created file '%s'"),
4153 fill_stat_cache_info(ce, &st);
4155 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
4156 die(_("unable to create backing store for newly created file %s"), path);
4158 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4159 die(_("unable to add cache entry for %s"), path);
4162 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4165 struct strbuf nbuf = STRBUF_INIT;
4167 if (S_ISGITLINK(mode)) {
4169 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4171 return mkdir(path, 0777);
4174 if (has_symlinks && S_ISLNK(mode))
4175 /* Although buf:size is counted string, it also is NUL
4178 return symlink(buf, path);
4180 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4184 if (convert_to_working_tree(path, buf, size, &nbuf)) {
4188 write_or_die(fd, buf, size);
4189 strbuf_release(&nbuf);
4192 die_errno(_("closing file '%s'"), path);
4197 * We optimistically assume that the directories exist,
4198 * which is true 99% of the time anyway. If they don't,
4199 * we create them and try again.
4201 static void create_one_file(struct apply_state *state,
4209 if (!try_create_file(path, mode, buf, size))
4212 if (errno == ENOENT) {
4213 if (safe_create_leading_directories(path))
4215 if (!try_create_file(path, mode, buf, size))
4219 if (errno == EEXIST || errno == EACCES) {
4220 /* We may be trying to create a file where a directory
4224 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4228 if (errno == EEXIST) {
4229 unsigned int nr = getpid();
4232 char newpath[PATH_MAX];
4233 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4234 if (!try_create_file(newpath, mode, buf, size)) {
4235 if (!rename(newpath, path))
4237 unlink_or_warn(newpath);
4240 if (errno != EEXIST)
4245 die_errno(_("unable to write file '%s' mode %o"), path, mode);
4248 static void add_conflicted_stages_file(struct apply_state *state,
4249 struct patch *patch)
4252 unsigned ce_size, mode;
4253 struct cache_entry *ce;
4255 if (!state->update_index)
4257 namelen = strlen(patch->new_name);
4258 ce_size = cache_entry_size(namelen);
4259 mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4261 remove_file_from_cache(patch->new_name);
4262 for (stage = 1; stage < 4; stage++) {
4263 if (is_null_oid(&patch->threeway_stage[stage - 1]))
4265 ce = xcalloc(1, ce_size);
4266 memcpy(ce->name, patch->new_name, namelen);
4267 ce->ce_mode = create_ce_mode(mode);
4268 ce->ce_flags = create_ce_flags(stage);
4269 ce->ce_namelen = namelen;
4270 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4271 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4272 die(_("unable to add cache entry for %s"), patch->new_name);
4276 static void create_file(struct apply_state *state, struct patch *patch)
4278 char *path = patch->new_name;
4279 unsigned mode = patch->new_mode;
4280 unsigned long size = patch->resultsize;
4281 char *buf = patch->result;
4284 mode = S_IFREG | 0644;
4285 create_one_file(state, path, mode, buf, size);
4287 if (patch->conflicted_threeway)
4288 add_conflicted_stages_file(state, patch);
4290 add_index_file(state, path, mode, buf, size);
4293 /* phase zero is to remove, phase one is to create */
4294 static void write_out_one_result(struct apply_state *state,
4295 struct patch *patch,
4298 if (patch->is_delete > 0) {
4300 remove_file(state, patch, 1);
4303 if (patch->is_new > 0 || patch->is_copy) {
4305 create_file(state, patch);
4309 * Rename or modification boils down to the same
4310 * thing: remove the old, write the new
4313 remove_file(state, patch, patch->is_rename);
4315 create_file(state, patch);
4318 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4321 char namebuf[PATH_MAX];
4322 struct fragment *frag;
4324 struct strbuf sb = STRBUF_INIT;
4326 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4327 if (!frag->rejected)
4333 if (state->apply_verbosely)
4334 say_patch_name(stderr,
4335 _("Applied patch %s cleanly."), patch);
4339 /* This should not happen, because a removal patch that leaves
4340 * contents are marked "rejected" at the patch level.
4342 if (!patch->new_name)
4343 die(_("internal error"));
4345 /* Say this even without --verbose */
4346 strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4347 "Applying patch %%s with %d rejects...",
4350 say_patch_name(stderr, sb.buf, patch);
4351 strbuf_release(&sb);
4353 cnt = strlen(patch->new_name);
4354 if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4355 cnt = ARRAY_SIZE(namebuf) - 5;
4356 warning(_("truncating .rej filename to %.*s.rej"),
4357 cnt - 1, patch->new_name);
4359 memcpy(namebuf, patch->new_name, cnt);
4360 memcpy(namebuf + cnt, ".rej", 5);
4362 rej = fopen(namebuf, "w");
4364 return error(_("cannot open %s: %s"), namebuf, strerror(errno));
4366 /* Normal git tools never deal with .rej, so do not pretend
4367 * this is a git patch by saying --git or giving extended
4368 * headers. While at it, maybe please "kompare" that wants
4369 * the trailing TAB and some garbage at the end of line ;-).
4371 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4372 patch->new_name, patch->new_name);
4373 for (cnt = 1, frag = patch->fragments;
4375 cnt++, frag = frag->next) {
4376 if (!frag->rejected) {
4377 fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4380 fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4381 fprintf(rej, "%.*s", frag->size, frag->patch);
4382 if (frag->patch[frag->size-1] != '\n')
4389 static int write_out_results(struct apply_state *state, struct patch *list)
4394 struct string_list cpath = STRING_LIST_INIT_DUP;
4396 for (phase = 0; phase < 2; phase++) {
4402 write_out_one_result(state, l, phase);
4404 if (write_out_one_reject(state, l))
4406 if (l->conflicted_threeway) {
4407 string_list_append(&cpath, l->new_name);
4417 struct string_list_item *item;
4419 string_list_sort(&cpath);
4420 for_each_string_list_item(item, &cpath)
4421 fprintf(stderr, "U %s\n", item->string);
4422 string_list_clear(&cpath, 0);
4430 static struct lock_file lock_file;
4432 #define INACCURATE_EOF (1<<0)
4433 #define RECOUNT (1<<1)
4436 * Try to apply a patch.
4439 * -128 if a bad error happened (like patch unreadable)
4440 * -1 if patch did not apply and user cannot deal with it
4441 * 0 if the patch applied
4442 * 1 if the patch did not apply but user might fix it
4444 static int apply_patch(struct apply_state *state,
4446 const char *filename,
4450 struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4451 struct patch *list = NULL, **listp = &list;
4452 int skipped_patch = 0;
4455 state->patch_input_file = filename;
4456 if (read_patch_file(&buf, fd) < 0)
4459 while (offset < buf.len) {
4460 struct patch *patch;
4463 patch = xcalloc(1, sizeof(*patch));
4464 patch->inaccurate_eof = !!(options & INACCURATE_EOF);
4465 patch->recount = !!(options & RECOUNT);
4466 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4475 if (state->apply_in_reverse)
4476 reverse_patches(patch);
4477 if (use_patch(state, patch)) {
4478 patch_stats(state, patch);
4480 listp = &patch->next;
4483 if (state->apply_verbosely)
4484 say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4491 if (!list && !skipped_patch) {
4492 error(_("unrecognized input"));
4497 if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4500 state->update_index = state->check_index && state->apply;
4501 if (state->update_index && state->newfd < 0)
4502 state->newfd = hold_locked_index(state->lock_file, 1);
4504 if (state->check_index && read_cache() < 0) {
4505 error(_("unable to read index file"));
4510 if ((state->check || state->apply) &&
4511 check_patch_list(state, list) < 0 &&
4512 !state->apply_with_reject) {
4517 if (state->apply && write_out_results(state, list)) {
4518 /* with --3way, we still need to write the index out */
4519 res = state->apply_with_reject ? -1 : 1;
4523 if (state->fake_ancestor)
4524 build_fake_ancestor(list, state->fake_ancestor);
4526 if (state->diffstat)
4527 stat_patch_list(state, list);
4530 numstat_patch_list(state, list);
4533 summary_patch_list(list);
4536 free_patch_list(list);
4537 strbuf_release(&buf);
4538 string_list_clear(&state->fn_table, 0);
4542 static void git_apply_config(void)
4544 git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
4545 git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
4546 git_config(git_default_config, NULL);
4549 static int option_parse_exclude(const struct option *opt,
4550 const char *arg, int unset)
4552 struct apply_state *state = opt->value;
4553 add_name_limit(state, arg, 1);
4557 static int option_parse_include(const struct option *opt,
4558 const char *arg, int unset)
4560 struct apply_state *state = opt->value;
4561 add_name_limit(state, arg, 0);
4562 state->has_include = 1;
4566 static int option_parse_p(const struct option *opt,
4570 struct apply_state *state = opt->value;
4571 state->p_value = atoi(arg);
4572 state->p_value_known = 1;
4576 static int option_parse_space_change(const struct option *opt,
4577 const char *arg, int unset)
4579 struct apply_state *state = opt->value;
4581 state->ws_ignore_action = ignore_ws_none;
4583 state->ws_ignore_action = ignore_ws_change;
4587 static int option_parse_whitespace(const struct option *opt,
4588 const char *arg, int unset)
4590 struct apply_state *state = opt->value;
4591 state->whitespace_option = arg;
4592 parse_whitespace_option(state, arg);
4596 static int option_parse_directory(const struct option *opt,
4597 const char *arg, int unset)
4599 struct apply_state *state = opt->value;
4600 strbuf_reset(&state->root);
4601 strbuf_addstr(&state->root, arg);
4602 strbuf_complete(&state->root, '/');
4606 static void init_apply_state(struct apply_state *state,
4608 struct lock_file *lock_file)
4610 memset(state, 0, sizeof(*state));
4611 state->prefix = prefix;
4612 state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4613 state->lock_file = lock_file;
4616 state->line_termination = '\n';
4618 state->p_context = UINT_MAX;
4619 state->squelch_whitespace_errors = 5;
4620 state->ws_error_action = warn_on_ws_error;
4621 state->ws_ignore_action = ignore_ws_none;
4623 string_list_init(&state->fn_table, 0);
4624 string_list_init(&state->limit_by_name, 0);
4625 string_list_init(&state->symlink_changes, 0);
4626 strbuf_init(&state->root, 0);
4629 if (apply_default_whitespace)
4630 parse_whitespace_option(state, apply_default_whitespace);
4631 if (apply_default_ignorewhitespace)
4632 parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
4635 static void clear_apply_state(struct apply_state *state)
4637 string_list_clear(&state->limit_by_name, 0);
4638 string_list_clear(&state->symlink_changes, 0);
4639 strbuf_release(&state->root);
4641 /* &state->fn_table is cleared at the end of apply_patch() */
4644 static void check_apply_state(struct apply_state *state, int force_apply)
4646 int is_not_gitdir = !startup_info->have_repository;
4648 if (state->apply_with_reject && state->threeway)
4649 die("--reject and --3way cannot be used together.");
4650 if (state->cached && state->threeway)
4651 die("--cached and --3way cannot be used together.");
4652 if (state->threeway) {
4654 die(_("--3way outside a repository"));
4655 state->check_index = 1;
4657 if (state->apply_with_reject)
4658 state->apply = state->apply_verbosely = 1;
4659 if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
4661 if (state->check_index && is_not_gitdir)
4662 die(_("--index outside a repository"));
4663 if (state->cached) {
4665 die(_("--cached outside a repository"));
4666 state->check_index = 1;
4668 if (state->check_index)
4669 state->unsafe_paths = 0;
4670 if (!state->lock_file)
4671 die("BUG: state->lock_file should not be NULL");
4674 static int apply_all_patches(struct apply_state *state,
4684 for (i = 0; i < argc; i++) {
4685 const char *arg = argv[i];
4688 if (!strcmp(arg, "-")) {
4689 res = apply_patch(state, 0, "<stdin>", options);
4695 } else if (0 < state->prefix_length)
4696 arg = prefix_filename(state->prefix,
4697 state->prefix_length,
4700 fd = open(arg, O_RDONLY);
4702 die_errno(_("can't open patch '%s'"), arg);
4704 set_default_whitespace_mode(state);
4705 res = apply_patch(state, fd, arg, options);
4711 set_default_whitespace_mode(state);
4713 res = apply_patch(state, 0, "<stdin>", options);
4719 if (state->whitespace_error) {
4720 if (state->squelch_whitespace_errors &&
4721 state->squelch_whitespace_errors < state->whitespace_error) {
4723 state->whitespace_error - state->squelch_whitespace_errors;
4724 warning(Q_("squelched %d whitespace error",
4725 "squelched %d whitespace errors",
4729 if (state->ws_error_action == die_on_ws_error)
4730 die(Q_("%d line adds whitespace errors.",
4731 "%d lines add whitespace errors.",
4732 state->whitespace_error),
4733 state->whitespace_error);
4734 if (state->applied_after_fixing_ws && state->apply)
4735 warning("%d line%s applied after"
4736 " fixing whitespace errors.",
4737 state->applied_after_fixing_ws,
4738 state->applied_after_fixing_ws == 1 ? "" : "s");
4739 else if (state->whitespace_error)
4740 warning(Q_("%d line adds whitespace errors.",
4741 "%d lines add whitespace errors.",
4742 state->whitespace_error),
4743 state->whitespace_error);
4746 if (state->update_index) {
4747 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
4748 die(_("Unable to write new index file"));
4755 exit(res == -1 ? 1 : 128);
4758 int cmd_apply(int argc, const char **argv, const char *prefix)
4760 int force_apply = 0;
4763 struct apply_state state;
4765 struct option builtin_apply_options[] = {
4766 { OPTION_CALLBACK, 0, "exclude", &state, N_("path"),
4767 N_("don't apply changes matching the given path"),
4768 0, option_parse_exclude },
4769 { OPTION_CALLBACK, 0, "include", &state, N_("path"),
4770 N_("apply changes matching the given path"),
4771 0, option_parse_include },
4772 { OPTION_CALLBACK, 'p', NULL, &state, N_("num"),
4773 N_("remove <num> leading slashes from traditional diff paths"),
4774 0, option_parse_p },
4775 OPT_BOOL(0, "no-add", &state.no_add,
4776 N_("ignore additions made by the patch")),
4777 OPT_BOOL(0, "stat", &state.diffstat,
4778 N_("instead of applying the patch, output diffstat for the input")),
4779 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
4780 OPT_NOOP_NOARG(0, "binary"),
4781 OPT_BOOL(0, "numstat", &state.numstat,
4782 N_("show number of added and deleted lines in decimal notation")),
4783 OPT_BOOL(0, "summary", &state.summary,
4784 N_("instead of applying the patch, output a summary for the input")),
4785 OPT_BOOL(0, "check", &state.check,
4786 N_("instead of applying the patch, see if the patch is applicable")),
4787 OPT_BOOL(0, "index", &state.check_index,
4788 N_("make sure the patch is applicable to the current index")),
4789 OPT_BOOL(0, "cached", &state.cached,
4790 N_("apply a patch without touching the working tree")),
4791 OPT_BOOL(0, "unsafe-paths", &state.unsafe_paths,
4792 N_("accept a patch that touches outside the working area")),
4793 OPT_BOOL(0, "apply", &force_apply,
4794 N_("also apply the patch (use with --stat/--summary/--check)")),
4795 OPT_BOOL('3', "3way", &state.threeway,
4796 N_( "attempt three-way merge if a patch does not apply")),
4797 OPT_FILENAME(0, "build-fake-ancestor", &state.fake_ancestor,
4798 N_("build a temporary index based on embedded index information")),
4799 /* Think twice before adding "--nul" synonym to this */
4800 OPT_SET_INT('z', NULL, &state.line_termination,
4801 N_("paths are separated with NUL character"), '\0'),
4802 OPT_INTEGER('C', NULL, &state.p_context,
4803 N_("ensure at least <n> lines of context match")),
4804 { OPTION_CALLBACK, 0, "whitespace", &state, N_("action"),
4805 N_("detect new or modified lines that have whitespace errors"),
4806 0, option_parse_whitespace },
4807 { OPTION_CALLBACK, 0, "ignore-space-change", &state, NULL,
4808 N_("ignore changes in whitespace when finding context"),
4809 PARSE_OPT_NOARG, option_parse_space_change },
4810 { OPTION_CALLBACK, 0, "ignore-whitespace", &state, NULL,
4811 N_("ignore changes in whitespace when finding context"),
4812 PARSE_OPT_NOARG, option_parse_space_change },
4813 OPT_BOOL('R', "reverse", &state.apply_in_reverse,
4814 N_("apply the patch in reverse")),
4815 OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
4816 N_("don't expect at least one line of context")),
4817 OPT_BOOL(0, "reject", &state.apply_with_reject,
4818 N_("leave the rejected hunks in corresponding *.rej files")),
4819 OPT_BOOL(0, "allow-overlap", &state.allow_overlap,
4820 N_("allow overlapping hunks")),
4821 OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
4822 OPT_BIT(0, "inaccurate-eof", &options,
4823 N_("tolerate incorrectly detected missing new-line at the end of file"),
4825 OPT_BIT(0, "recount", &options,
4826 N_("do not trust the line counts in the hunk headers"),
4828 { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
4829 N_("prepend <root> to all filenames"),
4830 0, option_parse_directory },
4834 init_apply_state(&state, prefix, &lock_file);
4836 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4839 check_apply_state(&state, force_apply);
4841 ret = apply_all_patches(&state, argc, argv, options);
4843 clear_apply_state(&state);