4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
15 #include "xdiff-interface.h"
18 #include "parse-options.h"
23 static void git_apply_config(void)
25 git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
26 git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
27 git_config(git_default_config, NULL);
30 static int 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 return error(_("unrecognized whitespace option '%s'"), option);
60 static int 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 return error(_("unrecognized whitespace ignore option '%s'"), option);
76 int init_apply_state(struct apply_state *state,
78 struct lock_file *lock_file)
80 memset(state, 0, sizeof(*state));
81 state->prefix = prefix;
82 state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
83 state->lock_file = lock_file;
86 state->line_termination = '\n';
88 state->p_context = UINT_MAX;
89 state->squelch_whitespace_errors = 5;
90 state->ws_error_action = warn_on_ws_error;
91 state->ws_ignore_action = ignore_ws_none;
93 string_list_init(&state->fn_table, 0);
94 string_list_init(&state->limit_by_name, 0);
95 string_list_init(&state->symlink_changes, 0);
96 strbuf_init(&state->root, 0);
99 if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))
101 if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
106 void clear_apply_state(struct apply_state *state)
108 string_list_clear(&state->limit_by_name, 0);
109 string_list_clear(&state->symlink_changes, 0);
110 strbuf_release(&state->root);
112 /* &state->fn_table is cleared at the end of apply_patch() */
115 int check_apply_state(struct apply_state *state, int force_apply)
117 int is_not_gitdir = !startup_info->have_repository;
119 if (state->apply_with_reject && state->threeway)
120 return error("--reject and --3way cannot be used together.");
121 if (state->cached && state->threeway)
122 return error("--cached and --3way cannot be used together.");
123 if (state->threeway) {
125 return error(_("--3way outside a repository"));
126 state->check_index = 1;
128 if (state->apply_with_reject)
129 state->apply = state->apply_verbosely = 1;
130 if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
132 if (state->check_index && is_not_gitdir)
133 return error(_("--index outside a repository"));
136 return error(_("--cached outside a repository"));
137 state->check_index = 1;
139 if (state->check_index)
140 state->unsafe_paths = 0;
141 if (!state->lock_file)
142 return error("BUG: state->lock_file should not be NULL");
147 static void set_default_whitespace_mode(struct apply_state *state)
149 if (!state->whitespace_option && !apply_default_whitespace)
150 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
154 * This represents one "hunk" from a patch, starting with
155 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
156 * patch text is pointed at by patch, and its byte length
157 * is stored in size. leading and trailing are the number
161 unsigned long leading, trailing;
162 unsigned long oldpos, oldlines;
163 unsigned long newpos, newlines;
165 * 'patch' is usually borrowed from buf in apply_patch(),
166 * but some codepaths store an allocated buffer.
169 unsigned free_patch:1,
173 struct fragment *next;
177 * When dealing with a binary patch, we reuse "leading" field
178 * to store the type of the binary hunk, either deflated "delta"
179 * or deflated "literal".
181 #define binary_patch_method leading
182 #define BINARY_DELTA_DEFLATED 1
183 #define BINARY_LITERAL_DEFLATED 2
186 * This represents a "patch" to a file, both metainfo changes
187 * such as creation/deletion, filemode and content changes represented
188 * as a series of fragments.
191 char *new_name, *old_name, *def_name;
192 unsigned int old_mode, new_mode;
193 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
196 int lines_added, lines_deleted;
198 unsigned int is_toplevel_relative:1;
199 unsigned int inaccurate_eof:1;
200 unsigned int is_binary:1;
201 unsigned int is_copy:1;
202 unsigned int is_rename:1;
203 unsigned int recount:1;
204 unsigned int conflicted_threeway:1;
205 unsigned int direct_to_threeway:1;
206 struct fragment *fragments;
209 char old_sha1_prefix[41];
210 char new_sha1_prefix[41];
213 /* three-way fallback result */
214 struct object_id threeway_stage[3];
217 static void free_fragment_list(struct fragment *list)
220 struct fragment *next = list->next;
221 if (list->free_patch)
222 free((char *)list->patch);
228 static void free_patch(struct patch *patch)
230 free_fragment_list(patch->fragments);
231 free(patch->def_name);
232 free(patch->old_name);
233 free(patch->new_name);
238 static void free_patch_list(struct patch *list)
241 struct patch *next = list->next;
248 * A line in a file, len-bytes long (includes the terminating LF,
249 * except for an incomplete line at the end if the file ends with
250 * one), and its contents hashes to 'hash'.
256 #define LINE_COMMON 1
257 #define LINE_PATCHED 2
261 * This represents a "file", which is an array of "lines".
268 struct line *line_allocated;
272 static uint32_t hash_line(const char *cp, size_t len)
276 for (i = 0, h = 0; i < len; i++) {
277 if (!isspace(cp[i])) {
278 h = h * 3 + (cp[i] & 0xff);
285 * Compare lines s1 of length n1 and s2 of length n2, ignoring
286 * whitespace difference. Returns 1 if they match, 0 otherwise
288 static int fuzzy_matchlines(const char *s1, size_t n1,
289 const char *s2, size_t n2)
291 const char *last1 = s1 + n1 - 1;
292 const char *last2 = s2 + n2 - 1;
295 /* ignore line endings */
296 while ((*last1 == '\r') || (*last1 == '\n'))
298 while ((*last2 == '\r') || (*last2 == '\n'))
301 /* skip leading whitespaces, if both begin with whitespace */
302 if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
303 while (isspace(*s1) && (s1 <= last1))
305 while (isspace(*s2) && (s2 <= last2))
308 /* early return if both lines are empty */
309 if ((s1 > last1) && (s2 > last2))
312 result = *s1++ - *s2++;
314 * Skip whitespace inside. We check for whitespace on
315 * both buffers because we don't want "a b" to match
318 if (isspace(*s1) && isspace(*s2)) {
319 while (isspace(*s1) && s1 <= last1)
321 while (isspace(*s2) && s2 <= last2)
325 * If we reached the end on one side only,
329 ((s2 > last2) && (s1 <= last1)) ||
330 ((s1 > last1) && (s2 <= last2)))
332 if ((s1 > last1) && (s2 > last2))
339 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
341 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
342 img->line_allocated[img->nr].len = len;
343 img->line_allocated[img->nr].hash = hash_line(bol, len);
344 img->line_allocated[img->nr].flag = flag;
349 * "buf" has the file contents to be patched (read from various sources).
350 * attach it to "image" and add line-based index to it.
351 * "image" now owns the "buf".
353 static void prepare_image(struct image *image, char *buf, size_t len,
354 int prepare_linetable)
358 memset(image, 0, sizeof(*image));
362 if (!prepare_linetable)
365 ep = image->buf + image->len;
369 for (next = cp; next < ep && *next != '\n'; next++)
373 add_line_info(image, cp, next - cp, 0);
376 image->line = image->line_allocated;
379 static void clear_image(struct image *image)
382 free(image->line_allocated);
383 memset(image, 0, sizeof(*image));
386 /* fmt must contain _one_ %s and no other substitution */
387 static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
389 struct strbuf sb = STRBUF_INIT;
391 if (patch->old_name && patch->new_name &&
392 strcmp(patch->old_name, patch->new_name)) {
393 quote_c_style(patch->old_name, &sb, NULL, 0);
394 strbuf_addstr(&sb, " => ");
395 quote_c_style(patch->new_name, &sb, NULL, 0);
397 const char *n = patch->new_name;
400 quote_c_style(n, &sb, NULL, 0);
402 fprintf(output, fmt, sb.buf);
409 static int read_patch_file(struct strbuf *sb, int fd)
411 if (strbuf_read(sb, fd, 0) < 0)
412 return error_errno("git apply: failed to read");
415 * Make sure that we have some slop in the buffer
416 * so that we can do speculative "memcmp" etc, and
417 * see to it that it is NUL-filled.
419 strbuf_grow(sb, SLOP);
420 memset(sb->buf + sb->len, 0, SLOP);
424 static unsigned long linelen(const char *buffer, unsigned long size)
426 unsigned long len = 0;
429 if (*buffer++ == '\n')
435 static int is_dev_null(const char *str)
437 return skip_prefix(str, "/dev/null", &str) && isspace(*str);
443 static int name_terminate(int c, int terminate)
445 if (c == ' ' && !(terminate & TERM_SPACE))
447 if (c == '\t' && !(terminate & TERM_TAB))
453 /* remove double slashes to make --index work with such filenames */
454 static char *squash_slash(char *name)
462 if ((name[j++] = name[i++]) == '/')
463 while (name[i] == '/')
470 static char *find_name_gnu(struct apply_state *state,
475 struct strbuf name = STRBUF_INIT;
479 * Proposed "new-style" GNU patch/diff format; see
480 * http://marc.info/?l=git&m=112927316408690&w=2
482 if (unquote_c_style(&name, line, NULL)) {
483 strbuf_release(&name);
487 for (cp = name.buf; p_value; p_value--) {
488 cp = strchr(cp, '/');
490 strbuf_release(&name);
496 strbuf_remove(&name, 0, cp - name.buf);
498 strbuf_insert(&name, 0, state->root.buf, state->root.len);
499 return squash_slash(strbuf_detach(&name, NULL));
502 static size_t sane_tz_len(const char *line, size_t len)
506 if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
508 tz = line + len - strlen(" +0500");
510 if (tz[1] != '+' && tz[1] != '-')
513 for (p = tz + 2; p != line + len; p++)
517 return line + len - tz;
520 static size_t tz_with_colon_len(const char *line, size_t len)
524 if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
526 tz = line + len - strlen(" +08:00");
528 if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
531 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
532 !isdigit(*p++) || !isdigit(*p++))
535 return line + len - tz;
538 static size_t date_len(const char *line, size_t len)
540 const char *date, *p;
542 if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
544 p = date = line + len - strlen("72-02-05");
546 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
547 !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
548 !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */
551 if (date - line >= strlen("19") &&
552 isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */
553 date -= strlen("19");
555 return line + len - date;
558 static size_t short_time_len(const char *line, size_t len)
560 const char *time, *p;
562 if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
564 p = time = line + len - strlen(" 07:01:32");
566 /* Permit 1-digit hours? */
568 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
569 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
570 !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
573 return line + len - time;
576 static size_t fractional_time_len(const char *line, size_t len)
581 /* Expected format: 19:41:17.620000023 */
582 if (!len || !isdigit(line[len - 1]))
586 /* Fractional seconds. */
587 while (p > line && isdigit(*p))
592 /* Hours, minutes, and whole seconds. */
593 n = short_time_len(line, p - line);
597 return line + len - p + n;
600 static size_t trailing_spaces_len(const char *line, size_t len)
604 /* Expected format: ' ' x (1 or more) */
605 if (!len || line[len - 1] != ' ')
612 return line + len - (p + 1);
619 static size_t diff_timestamp_len(const char *line, size_t len)
621 const char *end = line + len;
625 * Posix: 2010-07-05 19:41:17
626 * GNU: 2010-07-05 19:41:17.620000023 -0500
629 if (!isdigit(end[-1]))
632 n = sane_tz_len(line, end - line);
634 n = tz_with_colon_len(line, end - line);
637 n = short_time_len(line, end - line);
639 n = fractional_time_len(line, end - line);
642 n = date_len(line, end - line);
643 if (!n) /* No date. Too bad. */
647 if (end == line) /* No space before date. */
649 if (end[-1] == '\t') { /* Success! */
651 return line + len - end;
653 if (end[-1] != ' ') /* No space before date. */
656 /* Whitespace damage. */
657 end -= trailing_spaces_len(line, end - line);
658 return line + len - end;
661 static char *find_name_common(struct apply_state *state,
669 const char *start = NULL;
673 while (line != end) {
676 if (!end && isspace(c)) {
679 if (name_terminate(c, terminate))
683 if (c == '/' && !--p_value)
687 return squash_slash(xstrdup_or_null(def));
690 return squash_slash(xstrdup_or_null(def));
693 * Generally we prefer the shorter name, especially
694 * if the other one is just a variation of that with
695 * something else tacked on to the end (ie "file.orig"
699 int deflen = strlen(def);
700 if (deflen < len && !strncmp(start, def, deflen))
701 return squash_slash(xstrdup(def));
704 if (state->root.len) {
705 char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
706 return squash_slash(ret);
709 return squash_slash(xmemdupz(start, len));
712 static char *find_name(struct apply_state *state,
719 char *name = find_name_gnu(state, line, def, p_value);
724 return find_name_common(state, line, def, p_value, NULL, terminate);
727 static char *find_name_traditional(struct apply_state *state,
736 char *name = find_name_gnu(state, line, def, p_value);
741 len = strchrnul(line, '\n') - line;
742 date_len = diff_timestamp_len(line, len);
744 return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
747 return find_name_common(state, line, def, p_value, line + len, 0);
750 static int count_slashes(const char *cp)
762 * Given the string after "--- " or "+++ ", guess the appropriate
763 * p_value for the given patch.
765 static int guess_p_value(struct apply_state *state, const char *nameline)
770 if (is_dev_null(nameline))
772 name = find_name_traditional(state, nameline, NULL, 0);
775 cp = strchr(name, '/');
778 else if (state->prefix) {
780 * Does it begin with "a/$our-prefix" and such? Then this is
781 * very likely to apply to our directory.
783 if (!strncmp(name, state->prefix, state->prefix_length))
784 val = count_slashes(state->prefix);
787 if (!strncmp(cp, state->prefix, state->prefix_length))
788 val = count_slashes(state->prefix) + 1;
796 * Does the ---/+++ line have the POSIX timestamp after the last HT?
797 * GNU diff puts epoch there to signal a creation/deletion event. Is
798 * this such a timestamp?
800 static int has_epoch_timestamp(const char *nameline)
803 * We are only interested in epoch timestamp; any non-zero
804 * fraction cannot be one, hence "(\.0+)?" in the regexp below.
805 * For the same reason, the date must be either 1969-12-31 or
806 * 1970-01-01, and the seconds part must be "00".
808 const char stamp_regexp[] =
809 "^(1969-12-31|1970-01-01)"
811 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
813 "([-+][0-2][0-9]:?[0-5][0-9])\n";
814 const char *timestamp = NULL, *cp, *colon;
815 static regex_t *stamp;
821 for (cp = nameline; *cp != '\n'; cp++) {
828 stamp = xmalloc(sizeof(*stamp));
829 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
830 warning(_("Cannot prepare timestamp regexp %s"),
836 status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
838 if (status != REG_NOMATCH)
839 warning(_("regexec returned %d for input: %s"),
844 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
846 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
848 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
849 if (timestamp[m[3].rm_so] == '-')
850 zoneoffset = -zoneoffset;
853 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
854 * (west of GMT) or 1970-01-01 (east of GMT)
856 if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
857 (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
860 hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
861 strtol(timestamp + 14, NULL, 10) -
864 return ((zoneoffset < 0 && hourminute == 1440) ||
865 (0 <= zoneoffset && !hourminute));
869 * Get the name etc info from the ---/+++ lines of a traditional patch header
871 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
872 * files, we can happily check the index for a match, but for creating a
873 * new file we should try to match whatever "patch" does. I have no idea.
875 static int parse_traditional_patch(struct apply_state *state,
882 first += 4; /* skip "--- " */
883 second += 4; /* skip "+++ " */
884 if (!state->p_value_known) {
886 p = guess_p_value(state, first);
887 q = guess_p_value(state, second);
889 if (0 <= p && p == q) {
891 state->p_value_known = 1;
894 if (is_dev_null(first)) {
896 patch->is_delete = 0;
897 name = find_name_traditional(state, second, NULL, state->p_value);
898 patch->new_name = name;
899 } else if (is_dev_null(second)) {
901 patch->is_delete = 1;
902 name = find_name_traditional(state, first, NULL, state->p_value);
903 patch->old_name = name;
906 first_name = find_name_traditional(state, first, NULL, state->p_value);
907 name = find_name_traditional(state, second, first_name, state->p_value);
909 if (has_epoch_timestamp(first)) {
911 patch->is_delete = 0;
912 patch->new_name = name;
913 } else if (has_epoch_timestamp(second)) {
915 patch->is_delete = 1;
916 patch->old_name = name;
918 patch->old_name = name;
919 patch->new_name = xstrdup_or_null(name);
923 return error(_("unable to find filename in patch at line %d"), state->linenr);
928 static int gitdiff_hdrend(struct apply_state *state,
936 * We're anal about diff header consistency, to make
937 * sure that we don't end up having strange ambiguous
938 * patches floating around.
940 * As a result, gitdiff_{old|new}name() will check
941 * their names against any previous information, just
944 #define DIFF_OLD_NAME 0
945 #define DIFF_NEW_NAME 1
947 static int gitdiff_verify_name(struct apply_state *state,
953 if (!*name && !isnull) {
954 *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
959 int len = strlen(*name);
962 return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
963 *name, state->linenr);
964 another = find_name(state, line, NULL, state->p_value, TERM_TAB);
965 if (!another || memcmp(another, *name, len + 1)) {
967 return error((side == DIFF_NEW_NAME) ?
968 _("git apply: bad git-diff - inconsistent new filename on line %d") :
969 _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
973 /* expect "/dev/null" */
974 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
975 return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
981 static int gitdiff_oldname(struct apply_state *state,
985 return gitdiff_verify_name(state, line,
986 patch->is_new, &patch->old_name,
990 static int gitdiff_newname(struct apply_state *state,
994 return gitdiff_verify_name(state, line,
995 patch->is_delete, &patch->new_name,
999 static int gitdiff_oldmode(struct apply_state *state,
1001 struct patch *patch)
1003 patch->old_mode = strtoul(line, NULL, 8);
1007 static int gitdiff_newmode(struct apply_state *state,
1009 struct patch *patch)
1011 patch->new_mode = strtoul(line, NULL, 8);
1015 static int gitdiff_delete(struct apply_state *state,
1017 struct patch *patch)
1019 patch->is_delete = 1;
1020 free(patch->old_name);
1021 patch->old_name = xstrdup_or_null(patch->def_name);
1022 return gitdiff_oldmode(state, line, patch);
1025 static int gitdiff_newfile(struct apply_state *state,
1027 struct patch *patch)
1030 free(patch->new_name);
1031 patch->new_name = xstrdup_or_null(patch->def_name);
1032 return gitdiff_newmode(state, line, patch);
1035 static int gitdiff_copysrc(struct apply_state *state,
1037 struct patch *patch)
1040 free(patch->old_name);
1041 patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1045 static int gitdiff_copydst(struct apply_state *state,
1047 struct patch *patch)
1050 free(patch->new_name);
1051 patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1055 static int gitdiff_renamesrc(struct apply_state *state,
1057 struct patch *patch)
1059 patch->is_rename = 1;
1060 free(patch->old_name);
1061 patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1065 static int gitdiff_renamedst(struct apply_state *state,
1067 struct patch *patch)
1069 patch->is_rename = 1;
1070 free(patch->new_name);
1071 patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1075 static int gitdiff_similarity(struct apply_state *state,
1077 struct patch *patch)
1079 unsigned long val = strtoul(line, NULL, 10);
1085 static int gitdiff_dissimilarity(struct apply_state *state,
1087 struct patch *patch)
1089 unsigned long val = strtoul(line, NULL, 10);
1095 static int gitdiff_index(struct apply_state *state,
1097 struct patch *patch)
1100 * index line is N hexadecimal, "..", N hexadecimal,
1101 * and optional space with octal mode.
1103 const char *ptr, *eol;
1106 ptr = strchr(line, '.');
1107 if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1110 memcpy(patch->old_sha1_prefix, line, len);
1111 patch->old_sha1_prefix[len] = 0;
1114 ptr = strchr(line, ' ');
1115 eol = strchrnul(line, '\n');
1117 if (!ptr || eol < ptr)
1123 memcpy(patch->new_sha1_prefix, line, len);
1124 patch->new_sha1_prefix[len] = 0;
1126 patch->old_mode = strtoul(ptr+1, NULL, 8);
1131 * This is normal for a diff that doesn't change anything: we'll fall through
1132 * into the next diff. Tell the parser to break out.
1134 static int gitdiff_unrecognized(struct apply_state *state,
1136 struct patch *patch)
1142 * Skip p_value leading components from "line"; as we do not accept
1143 * absolute paths, return NULL in that case.
1145 static const char *skip_tree_prefix(struct apply_state *state,
1152 if (!state->p_value)
1153 return (llen && line[0] == '/') ? NULL : line;
1155 nslash = state->p_value;
1156 for (i = 0; i < llen; i++) {
1158 if (ch == '/' && --nslash <= 0)
1159 return (i == 0) ? NULL : &line[i + 1];
1165 * This is to extract the same name that appears on "diff --git"
1166 * line. We do not find and return anything if it is a rename
1167 * patch, and it is OK because we will find the name elsewhere.
1168 * We need to reliably find name only when it is mode-change only,
1169 * creation or deletion of an empty file. In any of these cases,
1170 * both sides are the same name under a/ and b/ respectively.
1172 static char *git_header_name(struct apply_state *state,
1177 const char *second = NULL;
1178 size_t len, line_len;
1180 line += strlen("diff --git ");
1181 llen -= strlen("diff --git ");
1185 struct strbuf first = STRBUF_INIT;
1186 struct strbuf sp = STRBUF_INIT;
1188 if (unquote_c_style(&first, line, &second))
1189 goto free_and_fail1;
1191 /* strip the a/b prefix including trailing slash */
1192 cp = skip_tree_prefix(state, first.buf, first.len);
1194 goto free_and_fail1;
1195 strbuf_remove(&first, 0, cp - first.buf);
1198 * second points at one past closing dq of name.
1199 * find the second name.
1201 while ((second < line + llen) && isspace(*second))
1204 if (line + llen <= second)
1205 goto free_and_fail1;
1206 if (*second == '"') {
1207 if (unquote_c_style(&sp, second, NULL))
1208 goto free_and_fail1;
1209 cp = skip_tree_prefix(state, sp.buf, sp.len);
1211 goto free_and_fail1;
1212 /* They must match, otherwise ignore */
1213 if (strcmp(cp, first.buf))
1214 goto free_and_fail1;
1215 strbuf_release(&sp);
1216 return strbuf_detach(&first, NULL);
1219 /* unquoted second */
1220 cp = skip_tree_prefix(state, second, line + llen - second);
1222 goto free_and_fail1;
1223 if (line + llen - cp != first.len ||
1224 memcmp(first.buf, cp, first.len))
1225 goto free_and_fail1;
1226 return strbuf_detach(&first, NULL);
1229 strbuf_release(&first);
1230 strbuf_release(&sp);
1234 /* unquoted first name */
1235 name = skip_tree_prefix(state, line, llen);
1240 * since the first name is unquoted, a dq if exists must be
1241 * the beginning of the second name.
1243 for (second = name; second < line + llen; second++) {
1244 if (*second == '"') {
1245 struct strbuf sp = STRBUF_INIT;
1248 if (unquote_c_style(&sp, second, NULL))
1249 goto free_and_fail2;
1251 np = skip_tree_prefix(state, sp.buf, sp.len);
1253 goto free_and_fail2;
1255 len = sp.buf + sp.len - np;
1256 if (len < second - name &&
1257 !strncmp(np, name, len) &&
1258 isspace(name[len])) {
1260 strbuf_remove(&sp, 0, np - sp.buf);
1261 return strbuf_detach(&sp, NULL);
1265 strbuf_release(&sp);
1271 * Accept a name only if it shows up twice, exactly the same
1274 second = strchr(name, '\n');
1277 line_len = second - name;
1278 for (len = 0 ; ; len++) {
1279 switch (name[len]) {
1284 case '\t': case ' ':
1286 * Is this the separator between the preimage
1287 * and the postimage pathname? Again, we are
1288 * only interested in the case where there is
1289 * no rename, as this is only to set def_name
1290 * and a rename patch has the names elsewhere
1291 * in an unambiguous form.
1294 return NULL; /* no postimage name */
1295 second = skip_tree_prefix(state, name + len + 1,
1296 line_len - (len + 1));
1300 * Does len bytes starting at "name" and "second"
1301 * (that are separated by one HT or SP we just
1302 * found) exactly match?
1304 if (second[len] == '\n' && !strncmp(name, second, len))
1305 return xmemdupz(name, len);
1310 /* Verify that we recognize the lines following a git header */
1311 static int parse_git_header(struct apply_state *state,
1315 struct patch *patch)
1317 unsigned long offset;
1319 /* A git diff has explicit new/delete information, so we don't guess */
1321 patch->is_delete = 0;
1324 * Some things may not have the old name in the
1325 * rest of the headers anywhere (pure mode changes,
1326 * or removing or adding empty files), so we get
1327 * the default name from the header.
1329 patch->def_name = git_header_name(state, line, len);
1330 if (patch->def_name && state->root.len) {
1331 char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1332 free(patch->def_name);
1333 patch->def_name = s;
1339 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1340 static const struct opentry {
1342 int (*fn)(struct apply_state *, const char *, struct patch *);
1344 { "@@ -", gitdiff_hdrend },
1345 { "--- ", gitdiff_oldname },
1346 { "+++ ", gitdiff_newname },
1347 { "old mode ", gitdiff_oldmode },
1348 { "new mode ", gitdiff_newmode },
1349 { "deleted file mode ", gitdiff_delete },
1350 { "new file mode ", gitdiff_newfile },
1351 { "copy from ", gitdiff_copysrc },
1352 { "copy to ", gitdiff_copydst },
1353 { "rename old ", gitdiff_renamesrc },
1354 { "rename new ", gitdiff_renamedst },
1355 { "rename from ", gitdiff_renamesrc },
1356 { "rename to ", gitdiff_renamedst },
1357 { "similarity index ", gitdiff_similarity },
1358 { "dissimilarity index ", gitdiff_dissimilarity },
1359 { "index ", gitdiff_index },
1360 { "", gitdiff_unrecognized },
1364 len = linelen(line, size);
1365 if (!len || line[len-1] != '\n')
1367 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1368 const struct opentry *p = optable + i;
1369 int oplen = strlen(p->str);
1371 if (len < oplen || memcmp(p->str, line, oplen))
1373 res = p->fn(state, line + oplen, patch);
1385 static int parse_num(const char *line, unsigned long *p)
1389 if (!isdigit(*line))
1391 *p = strtoul(line, &ptr, 10);
1395 static int parse_range(const char *line, int len, int offset, const char *expect,
1396 unsigned long *p1, unsigned long *p2)
1400 if (offset < 0 || offset >= len)
1405 digits = parse_num(line, p1);
1415 digits = parse_num(line+1, p2);
1424 ex = strlen(expect);
1427 if (memcmp(line, expect, ex))
1433 static void recount_diff(const char *line, int size, struct fragment *fragment)
1435 int oldlines = 0, newlines = 0, ret = 0;
1438 warning("recount: ignore empty hunk");
1443 int len = linelen(line, size);
1451 case ' ': case '\n':
1463 ret = size < 3 || !starts_with(line, "@@ ");
1466 ret = size < 5 || !starts_with(line, "diff ");
1473 warning(_("recount: unexpected line: %.*s"),
1474 (int)linelen(line, size), line);
1479 fragment->oldlines = oldlines;
1480 fragment->newlines = newlines;
1484 * Parse a unified diff fragment header of the
1485 * form "@@ -a,b +c,d @@"
1487 static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1491 if (!len || line[len-1] != '\n')
1494 /* Figure out the number of lines in a fragment */
1495 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1496 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1502 * Find file diff header
1505 * -1 if no header was found
1506 * -128 in case of error
1507 * the size of the header in bytes (called "offset") otherwise
1509 static int find_header(struct apply_state *state,
1513 struct patch *patch)
1515 unsigned long offset, len;
1517 patch->is_toplevel_relative = 0;
1518 patch->is_rename = patch->is_copy = 0;
1519 patch->is_new = patch->is_delete = -1;
1520 patch->old_mode = patch->new_mode = 0;
1521 patch->old_name = patch->new_name = NULL;
1522 for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1523 unsigned long nextlen;
1525 len = linelen(line, size);
1529 /* Testing this early allows us to take a few shortcuts.. */
1534 * Make sure we don't find any unconnected patch fragments.
1535 * That's a sign that we didn't find a header, and that a
1536 * patch has become corrupted/broken up.
1538 if (!memcmp("@@ -", line, 4)) {
1539 struct fragment dummy;
1540 if (parse_fragment_header(line, len, &dummy) < 0)
1542 error(_("patch fragment without header at line %d: %.*s"),
1543 state->linenr, (int)len-1, line);
1551 * Git patch? It might not have a real patch, just a rename
1552 * or mode change, so we handle that specially
1554 if (!memcmp("diff --git ", line, 11)) {
1555 int git_hdr_len = parse_git_header(state, line, len, size, patch);
1556 if (git_hdr_len < 0)
1558 if (git_hdr_len <= len)
1560 if (!patch->old_name && !patch->new_name) {
1561 if (!patch->def_name) {
1562 error(Q_("git diff header lacks filename information when removing "
1563 "%d leading pathname component (line %d)",
1564 "git diff header lacks filename information when removing "
1565 "%d leading pathname components (line %d)",
1567 state->p_value, state->linenr);
1570 patch->old_name = xstrdup(patch->def_name);
1571 patch->new_name = xstrdup(patch->def_name);
1573 if (!patch->is_delete && !patch->new_name) {
1574 error("git diff header lacks filename information "
1575 "(line %d)", state->linenr);
1578 patch->is_toplevel_relative = 1;
1579 *hdrsize = git_hdr_len;
1583 /* --- followed by +++ ? */
1584 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1588 * We only accept unified patches, so we want it to
1589 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1590 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1592 nextlen = linelen(line + len, size - len);
1593 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1596 /* Ok, we'll consider it a patch */
1597 if (parse_traditional_patch(state, line, line+len, patch))
1599 *hdrsize = len + nextlen;
1606 static void record_ws_error(struct apply_state *state,
1617 state->whitespace_error++;
1618 if (state->squelch_whitespace_errors &&
1619 state->squelch_whitespace_errors < state->whitespace_error)
1622 err = whitespace_error_string(result);
1623 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1624 state->patch_input_file, linenr, err, len, line);
1628 static void check_whitespace(struct apply_state *state,
1633 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1635 record_ws_error(state, result, line + 1, len - 2, state->linenr);
1639 * Parse a unified diff. Note that this really needs to parse each
1640 * fragment separately, since the only way to know the difference
1641 * between a "---" that is part of a patch, and a "---" that starts
1642 * the next patch is to look at the line counts..
1644 static int parse_fragment(struct apply_state *state,
1647 struct patch *patch,
1648 struct fragment *fragment)
1651 int len = linelen(line, size), offset;
1652 unsigned long oldlines, newlines;
1653 unsigned long leading, trailing;
1655 offset = parse_fragment_header(line, len, fragment);
1658 if (offset > 0 && patch->recount)
1659 recount_diff(line + offset, size - offset, fragment);
1660 oldlines = fragment->oldlines;
1661 newlines = fragment->newlines;
1665 /* Parse the thing.. */
1669 added = deleted = 0;
1672 offset += len, size -= len, line += len, state->linenr++) {
1673 if (!oldlines && !newlines)
1675 len = linelen(line, size);
1676 if (!len || line[len-1] != '\n')
1681 case '\n': /* newer GNU diff, an empty context line */
1685 if (!deleted && !added)
1688 if (!state->apply_in_reverse &&
1689 state->ws_error_action == correct_ws_error)
1690 check_whitespace(state, line, len, patch->ws_rule);
1693 if (state->apply_in_reverse &&
1694 state->ws_error_action != nowarn_ws_error)
1695 check_whitespace(state, line, len, patch->ws_rule);
1701 if (!state->apply_in_reverse &&
1702 state->ws_error_action != nowarn_ws_error)
1703 check_whitespace(state, line, len, patch->ws_rule);
1710 * We allow "\ No newline at end of file". Depending
1711 * on locale settings when the patch was produced we
1712 * don't know what this line looks like. The only
1713 * thing we do know is that it begins with "\ ".
1714 * Checking for 12 is just for sanity check -- any
1715 * l10n of "\ No newline..." is at least that long.
1718 if (len < 12 || memcmp(line, "\\ ", 2))
1723 if (oldlines || newlines)
1725 if (!deleted && !added)
1728 fragment->leading = leading;
1729 fragment->trailing = trailing;
1732 * If a fragment ends with an incomplete line, we failed to include
1733 * it in the above loop because we hit oldlines == newlines == 0
1736 if (12 < size && !memcmp(line, "\\ ", 2))
1737 offset += linelen(line, size);
1739 patch->lines_added += added;
1740 patch->lines_deleted += deleted;
1742 if (0 < patch->is_new && oldlines)
1743 return error(_("new file depends on old contents"));
1744 if (0 < patch->is_delete && newlines)
1745 return error(_("deleted file still has contents"));
1750 * We have seen "diff --git a/... b/..." header (or a traditional patch
1751 * header). Read hunks that belong to this patch into fragments and hang
1752 * them to the given patch structure.
1754 * The (fragment->patch, fragment->size) pair points into the memory given
1755 * by the caller, not a copy, when we return.
1758 * -1 in case of error,
1759 * the number of bytes in the patch otherwise.
1761 static int parse_single_patch(struct apply_state *state,
1764 struct patch *patch)
1766 unsigned long offset = 0;
1767 unsigned long oldlines = 0, newlines = 0, context = 0;
1768 struct fragment **fragp = &patch->fragments;
1770 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1771 struct fragment *fragment;
1774 fragment = xcalloc(1, sizeof(*fragment));
1775 fragment->linenr = state->linenr;
1776 len = parse_fragment(state, line, size, patch, fragment);
1779 return error(_("corrupt patch at line %d"), state->linenr);
1781 fragment->patch = line;
1782 fragment->size = len;
1783 oldlines += fragment->oldlines;
1784 newlines += fragment->newlines;
1785 context += fragment->leading + fragment->trailing;
1788 fragp = &fragment->next;
1796 * If something was removed (i.e. we have old-lines) it cannot
1797 * be creation, and if something was added it cannot be
1798 * deletion. However, the reverse is not true; --unified=0
1799 * patches that only add are not necessarily creation even
1800 * though they do not have any old lines, and ones that only
1801 * delete are not necessarily deletion.
1803 * Unfortunately, a real creation/deletion patch do _not_ have
1804 * any context line by definition, so we cannot safely tell it
1805 * apart with --unified=0 insanity. At least if the patch has
1806 * more than one hunk it is not creation or deletion.
1808 if (patch->is_new < 0 &&
1809 (oldlines || (patch->fragments && patch->fragments->next)))
1811 if (patch->is_delete < 0 &&
1812 (newlines || (patch->fragments && patch->fragments->next)))
1813 patch->is_delete = 0;
1815 if (0 < patch->is_new && oldlines)
1816 return error(_("new file %s depends on old contents"), patch->new_name);
1817 if (0 < patch->is_delete && newlines)
1818 return error(_("deleted file %s still has contents"), patch->old_name);
1819 if (!patch->is_delete && !newlines && context)
1822 "file %s becomes empty but is not deleted"),
1828 static inline int metadata_changes(struct patch *patch)
1830 return patch->is_rename > 0 ||
1831 patch->is_copy > 0 ||
1832 patch->is_new > 0 ||
1834 (patch->old_mode && patch->new_mode &&
1835 patch->old_mode != patch->new_mode);
1838 static char *inflate_it(const void *data, unsigned long size,
1839 unsigned long inflated_size)
1845 memset(&stream, 0, sizeof(stream));
1847 stream.next_in = (unsigned char *)data;
1848 stream.avail_in = size;
1849 stream.next_out = out = xmalloc(inflated_size);
1850 stream.avail_out = inflated_size;
1851 git_inflate_init(&stream);
1852 st = git_inflate(&stream, Z_FINISH);
1853 git_inflate_end(&stream);
1854 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1862 * Read a binary hunk and return a new fragment; fragment->patch
1863 * points at an allocated memory that the caller must free, so
1864 * it is marked as "->free_patch = 1".
1866 static struct fragment *parse_binary_hunk(struct apply_state *state,
1868 unsigned long *sz_p,
1873 * Expect a line that begins with binary patch method ("literal"
1874 * or "delta"), followed by the length of data before deflating.
1875 * a sequence of 'length-byte' followed by base-85 encoded data
1876 * should follow, terminated by a newline.
1878 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1879 * and we would limit the patch line to 66 characters,
1880 * so one line can fit up to 13 groups that would decode
1881 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1882 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1885 unsigned long size = *sz_p;
1886 char *buffer = *buf_p;
1888 unsigned long origlen;
1891 struct fragment *frag;
1893 llen = linelen(buffer, size);
1898 if (starts_with(buffer, "delta ")) {
1899 patch_method = BINARY_DELTA_DEFLATED;
1900 origlen = strtoul(buffer + 6, NULL, 10);
1902 else if (starts_with(buffer, "literal ")) {
1903 patch_method = BINARY_LITERAL_DEFLATED;
1904 origlen = strtoul(buffer + 8, NULL, 10);
1912 int byte_length, max_byte_length, newsize;
1913 llen = linelen(buffer, size);
1917 /* consume the blank line */
1923 * Minimum line is "A00000\n" which is 7-byte long,
1924 * and the line length must be multiple of 5 plus 2.
1926 if ((llen < 7) || (llen-2) % 5)
1928 max_byte_length = (llen - 2) / 5 * 4;
1929 byte_length = *buffer;
1930 if ('A' <= byte_length && byte_length <= 'Z')
1931 byte_length = byte_length - 'A' + 1;
1932 else if ('a' <= byte_length && byte_length <= 'z')
1933 byte_length = byte_length - 'a' + 27;
1936 /* if the input length was not multiple of 4, we would
1937 * have filler at the end but the filler should never
1940 if (max_byte_length < byte_length ||
1941 byte_length <= max_byte_length - 4)
1943 newsize = hunk_size + byte_length;
1944 data = xrealloc(data, newsize);
1945 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1947 hunk_size = newsize;
1952 frag = xcalloc(1, sizeof(*frag));
1953 frag->patch = inflate_it(data, hunk_size, origlen);
1954 frag->free_patch = 1;
1958 frag->size = origlen;
1962 frag->binary_patch_method = patch_method;
1968 error(_("corrupt binary patch at line %d: %.*s"),
1969 state->linenr-1, llen-1, buffer);
1975 * -1 in case of error,
1976 * the length of the parsed binary patch otherwise
1978 static int parse_binary(struct apply_state *state,
1981 struct patch *patch)
1984 * We have read "GIT binary patch\n"; what follows is a line
1985 * that says the patch method (currently, either "literal" or
1986 * "delta") and the length of data before deflating; a
1987 * sequence of 'length-byte' followed by base-85 encoded data
1990 * When a binary patch is reversible, there is another binary
1991 * hunk in the same format, starting with patch method (either
1992 * "literal" or "delta") with the length of data, and a sequence
1993 * of length-byte + base-85 encoded data, terminated with another
1994 * empty line. This data, when applied to the postimage, produces
1997 struct fragment *forward;
1998 struct fragment *reverse;
2002 forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
2003 if (!forward && !status)
2004 /* there has to be one hunk (forward hunk) */
2005 return error(_("unrecognized binary patch at line %d"), state->linenr-1);
2007 /* otherwise we already gave an error message */
2010 reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
2015 * Not having reverse hunk is not an error, but having
2016 * a corrupt reverse hunk is.
2018 free((void*) forward->patch);
2022 forward->next = reverse;
2023 patch->fragments = forward;
2024 patch->is_binary = 1;
2028 static void prefix_one(struct apply_state *state, char **name)
2030 char *old_name = *name;
2033 *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
2037 static void prefix_patch(struct apply_state *state, struct patch *p)
2039 if (!state->prefix || p->is_toplevel_relative)
2041 prefix_one(state, &p->new_name);
2042 prefix_one(state, &p->old_name);
2049 static void add_name_limit(struct apply_state *state,
2053 struct string_list_item *it;
2055 it = string_list_append(&state->limit_by_name, name);
2056 it->util = exclude ? NULL : (void *) 1;
2059 static int use_patch(struct apply_state *state, struct patch *p)
2061 const char *pathname = p->new_name ? p->new_name : p->old_name;
2064 /* Paths outside are not touched regardless of "--include" */
2065 if (0 < state->prefix_length) {
2066 int pathlen = strlen(pathname);
2067 if (pathlen <= state->prefix_length ||
2068 memcmp(state->prefix, pathname, state->prefix_length))
2072 /* See if it matches any of exclude/include rule */
2073 for (i = 0; i < state->limit_by_name.nr; i++) {
2074 struct string_list_item *it = &state->limit_by_name.items[i];
2075 if (!wildmatch(it->string, pathname, 0, NULL))
2076 return (it->util != NULL);
2080 * If we had any include, a path that does not match any rule is
2081 * not used. Otherwise, we saw bunch of exclude rules (or none)
2082 * and such a path is used.
2084 return !state->has_include;
2088 * Read the patch text in "buffer" that extends for "size" bytes; stop
2089 * reading after seeing a single patch (i.e. changes to a single file).
2090 * Create fragments (i.e. patch hunks) and hang them to the given patch.
2093 * -1 if no header was found or parse_binary() failed,
2094 * -128 on another error,
2095 * the number of bytes consumed otherwise,
2096 * so that the caller can call us again for the next patch.
2098 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2100 int hdrsize, patchsize;
2101 int offset = find_header(state, buffer, size, &hdrsize, patch);
2106 prefix_patch(state, patch);
2108 if (!use_patch(state, patch))
2111 patch->ws_rule = whitespace_rule(patch->new_name
2115 patchsize = parse_single_patch(state,
2116 buffer + offset + hdrsize,
2117 size - offset - hdrsize,
2124 static const char git_binary[] = "GIT binary patch\n";
2125 int hd = hdrsize + offset;
2126 unsigned long llen = linelen(buffer + hd, size - hd);
2128 if (llen == sizeof(git_binary) - 1 &&
2129 !memcmp(git_binary, buffer + hd, llen)) {
2132 used = parse_binary(state, buffer + hd + llen,
2133 size - hd - llen, patch);
2137 patchsize = used + llen;
2141 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2142 static const char *binhdr[] = {
2148 for (i = 0; binhdr[i]; i++) {
2149 int len = strlen(binhdr[i]);
2150 if (len < size - hd &&
2151 !memcmp(binhdr[i], buffer + hd, len)) {
2153 patch->is_binary = 1;
2160 /* Empty patch cannot be applied if it is a text patch
2161 * without metadata change. A binary patch appears
2164 if ((state->apply || state->check) &&
2165 (!patch->is_binary && !metadata_changes(patch))) {
2166 error(_("patch with only garbage at line %d"), state->linenr);
2171 return offset + hdrsize + patchsize;
2174 #define swap(a,b) myswap((a),(b),sizeof(a))
2176 #define myswap(a, b, size) do { \
2177 unsigned char mytmp[size]; \
2178 memcpy(mytmp, &a, size); \
2179 memcpy(&a, &b, size); \
2180 memcpy(&b, mytmp, size); \
2183 static void reverse_patches(struct patch *p)
2185 for (; p; p = p->next) {
2186 struct fragment *frag = p->fragments;
2188 swap(p->new_name, p->old_name);
2189 swap(p->new_mode, p->old_mode);
2190 swap(p->is_new, p->is_delete);
2191 swap(p->lines_added, p->lines_deleted);
2192 swap(p->old_sha1_prefix, p->new_sha1_prefix);
2194 for (; frag; frag = frag->next) {
2195 swap(frag->newpos, frag->oldpos);
2196 swap(frag->newlines, frag->oldlines);
2201 static const char pluses[] =
2202 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2203 static const char minuses[]=
2204 "----------------------------------------------------------------------";
2206 static void show_stats(struct apply_state *state, struct patch *patch)
2208 struct strbuf qname = STRBUF_INIT;
2209 char *cp = patch->new_name ? patch->new_name : patch->old_name;
2212 quote_c_style(cp, &qname, NULL, 0);
2215 * "scale" the filename
2217 max = state->max_len;
2221 if (qname.len > max) {
2222 cp = strchr(qname.buf + qname.len + 3 - max, '/');
2224 cp = qname.buf + qname.len + 3 - max;
2225 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2228 if (patch->is_binary) {
2229 printf(" %-*s | Bin\n", max, qname.buf);
2230 strbuf_release(&qname);
2234 printf(" %-*s |", max, qname.buf);
2235 strbuf_release(&qname);
2238 * scale the add/delete
2240 max = max + state->max_change > 70 ? 70 - max : state->max_change;
2241 add = patch->lines_added;
2242 del = patch->lines_deleted;
2244 if (state->max_change > 0) {
2245 int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2246 add = (add * max + state->max_change / 2) / state->max_change;
2249 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2250 add, pluses, del, minuses);
2253 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2255 switch (st->st_mode & S_IFMT) {
2257 if (strbuf_readlink(buf, path, st->st_size) < 0)
2258 return error(_("unable to read symlink %s"), path);
2261 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2262 return error(_("unable to open or read %s"), path);
2263 convert_to_git(path, buf->buf, buf->len, buf, 0);
2271 * Update the preimage, and the common lines in postimage,
2272 * from buffer buf of length len. If postlen is 0 the postimage
2273 * is updated in place, otherwise it's updated on a new buffer
2277 static void update_pre_post_images(struct image *preimage,
2278 struct image *postimage,
2280 size_t len, size_t postlen)
2282 int i, ctx, reduced;
2283 char *new, *old, *fixed;
2284 struct image fixed_preimage;
2287 * Update the preimage with whitespace fixes. Note that we
2288 * are not losing preimage->buf -- apply_one_fragment() will
2291 prepare_image(&fixed_preimage, buf, len, 1);
2293 ? fixed_preimage.nr == preimage->nr
2294 : fixed_preimage.nr <= preimage->nr);
2295 for (i = 0; i < fixed_preimage.nr; i++)
2296 fixed_preimage.line[i].flag = preimage->line[i].flag;
2297 free(preimage->line_allocated);
2298 *preimage = fixed_preimage;
2301 * Adjust the common context lines in postimage. This can be
2302 * done in-place when we are shrinking it with whitespace
2303 * fixing, but needs a new buffer when ignoring whitespace or
2304 * expanding leading tabs to spaces.
2306 * We trust the caller to tell us if the update can be done
2307 * in place (postlen==0) or not.
2309 old = postimage->buf;
2311 new = postimage->buf = xmalloc(postlen);
2314 fixed = preimage->buf;
2316 for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2317 size_t l_len = postimage->line[i].len;
2318 if (!(postimage->line[i].flag & LINE_COMMON)) {
2319 /* an added line -- no counterparts in preimage */
2320 memmove(new, old, l_len);
2326 /* a common context -- skip it in the original postimage */
2329 /* and find the corresponding one in the fixed preimage */
2330 while (ctx < preimage->nr &&
2331 !(preimage->line[ctx].flag & LINE_COMMON)) {
2332 fixed += preimage->line[ctx].len;
2337 * preimage is expected to run out, if the caller
2338 * fixed addition of trailing blank lines.
2340 if (preimage->nr <= ctx) {
2345 /* and copy it in, while fixing the line length */
2346 l_len = preimage->line[ctx].len;
2347 memcpy(new, fixed, l_len);
2350 postimage->line[i].len = l_len;
2355 ? postlen < new - postimage->buf
2356 : postimage->len < new - postimage->buf)
2357 die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2358 (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2360 /* Fix the length of the whole thing */
2361 postimage->len = new - postimage->buf;
2362 postimage->nr -= reduced;
2365 static int line_by_line_fuzzy_match(struct image *img,
2366 struct image *preimage,
2367 struct image *postimage,
2375 size_t postlen = postimage->len;
2380 struct strbuf fixed;
2384 for (i = 0; i < preimage_limit; i++) {
2385 size_t prelen = preimage->line[i].len;
2386 size_t imglen = img->line[try_lno+i].len;
2388 if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2389 preimage->buf + preoff, prelen))
2391 if (preimage->line[i].flag & LINE_COMMON)
2392 postlen += imglen - prelen;
2398 * Ok, the preimage matches with whitespace fuzz.
2400 * imgoff now holds the true length of the target that
2401 * matches the preimage before the end of the file.
2403 * Count the number of characters in the preimage that fall
2404 * beyond the end of the file and make sure that all of them
2405 * are whitespace characters. (This can only happen if
2406 * we are removing blank lines at the end of the file.)
2408 buf = preimage_eof = preimage->buf + preoff;
2409 for ( ; i < preimage->nr; i++)
2410 preoff += preimage->line[i].len;
2411 preimage_end = preimage->buf + preoff;
2412 for ( ; buf < preimage_end; buf++)
2417 * Update the preimage and the common postimage context
2418 * lines to use the same whitespace as the target.
2419 * If whitespace is missing in the target (i.e.
2420 * if the preimage extends beyond the end of the file),
2421 * use the whitespace from the preimage.
2423 extra_chars = preimage_end - preimage_eof;
2424 strbuf_init(&fixed, imgoff + extra_chars);
2425 strbuf_add(&fixed, img->buf + try, imgoff);
2426 strbuf_add(&fixed, preimage_eof, extra_chars);
2427 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2428 update_pre_post_images(preimage, postimage,
2429 fixed_buf, fixed_len, postlen);
2433 static int match_fragment(struct apply_state *state,
2435 struct image *preimage,
2436 struct image *postimage,
2440 int match_beginning, int match_end)
2443 char *fixed_buf, *buf, *orig, *target;
2444 struct strbuf fixed;
2445 size_t fixed_len, postlen;
2448 if (preimage->nr + try_lno <= img->nr) {
2450 * The hunk falls within the boundaries of img.
2452 preimage_limit = preimage->nr;
2453 if (match_end && (preimage->nr + try_lno != img->nr))
2455 } else if (state->ws_error_action == correct_ws_error &&
2456 (ws_rule & WS_BLANK_AT_EOF)) {
2458 * This hunk extends beyond the end of img, and we are
2459 * removing blank lines at the end of the file. This
2460 * many lines from the beginning of the preimage must
2461 * match with img, and the remainder of the preimage
2464 preimage_limit = img->nr - try_lno;
2467 * The hunk extends beyond the end of the img and
2468 * we are not removing blanks at the end, so we
2469 * should reject the hunk at this position.
2474 if (match_beginning && try_lno)
2477 /* Quick hash check */
2478 for (i = 0; i < preimage_limit; i++)
2479 if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2480 (preimage->line[i].hash != img->line[try_lno + i].hash))
2483 if (preimage_limit == preimage->nr) {
2485 * Do we have an exact match? If we were told to match
2486 * at the end, size must be exactly at try+fragsize,
2487 * otherwise try+fragsize must be still within the preimage,
2488 * and either case, the old piece should match the preimage
2492 ? (try + preimage->len == img->len)
2493 : (try + preimage->len <= img->len)) &&
2494 !memcmp(img->buf + try, preimage->buf, preimage->len))
2498 * The preimage extends beyond the end of img, so
2499 * there cannot be an exact match.
2501 * There must be one non-blank context line that match
2502 * a line before the end of img.
2506 buf = preimage->buf;
2508 for (i = 0; i < preimage_limit; i++)
2509 buf_end += preimage->line[i].len;
2511 for ( ; buf < buf_end; buf++)
2519 * No exact match. If we are ignoring whitespace, run a line-by-line
2520 * fuzzy matching. We collect all the line length information because
2521 * we need it to adjust whitespace if we match.
2523 if (state->ws_ignore_action == ignore_ws_change)
2524 return line_by_line_fuzzy_match(img, preimage, postimage,
2525 try, try_lno, preimage_limit);
2527 if (state->ws_error_action != correct_ws_error)
2531 * The hunk does not apply byte-by-byte, but the hash says
2532 * it might with whitespace fuzz. We weren't asked to
2533 * ignore whitespace, we were asked to correct whitespace
2534 * errors, so let's try matching after whitespace correction.
2536 * While checking the preimage against the target, whitespace
2537 * errors in both fixed, we count how large the corresponding
2538 * postimage needs to be. The postimage prepared by
2539 * apply_one_fragment() has whitespace errors fixed on added
2540 * lines already, but the common lines were propagated as-is,
2541 * which may become longer when their whitespace errors are
2545 /* First count added lines in postimage */
2547 for (i = 0; i < postimage->nr; i++) {
2548 if (!(postimage->line[i].flag & LINE_COMMON))
2549 postlen += postimage->line[i].len;
2553 * The preimage may extend beyond the end of the file,
2554 * but in this loop we will only handle the part of the
2555 * preimage that falls within the file.
2557 strbuf_init(&fixed, preimage->len + 1);
2558 orig = preimage->buf;
2559 target = img->buf + try;
2560 for (i = 0; i < preimage_limit; i++) {
2561 size_t oldlen = preimage->line[i].len;
2562 size_t tgtlen = img->line[try_lno + i].len;
2563 size_t fixstart = fixed.len;
2564 struct strbuf tgtfix;
2567 /* Try fixing the line in the preimage */
2568 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2570 /* Try fixing the line in the target */
2571 strbuf_init(&tgtfix, tgtlen);
2572 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2575 * If they match, either the preimage was based on
2576 * a version before our tree fixed whitespace breakage,
2577 * or we are lacking a whitespace-fix patch the tree
2578 * the preimage was based on already had (i.e. target
2579 * has whitespace breakage, the preimage doesn't).
2580 * In either case, we are fixing the whitespace breakages
2581 * so we might as well take the fix together with their
2584 match = (tgtfix.len == fixed.len - fixstart &&
2585 !memcmp(tgtfix.buf, fixed.buf + fixstart,
2586 fixed.len - fixstart));
2588 /* Add the length if this is common with the postimage */
2589 if (preimage->line[i].flag & LINE_COMMON)
2590 postlen += tgtfix.len;
2592 strbuf_release(&tgtfix);
2602 * Now handle the lines in the preimage that falls beyond the
2603 * end of the file (if any). They will only match if they are
2604 * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2607 for ( ; i < preimage->nr; i++) {
2608 size_t fixstart = fixed.len; /* start of the fixed preimage */
2609 size_t oldlen = preimage->line[i].len;
2612 /* Try fixing the line in the preimage */
2613 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2615 for (j = fixstart; j < fixed.len; j++)
2616 if (!isspace(fixed.buf[j]))
2623 * Yes, the preimage is based on an older version that still
2624 * has whitespace breakages unfixed, and fixing them makes the
2625 * hunk match. Update the context lines in the postimage.
2627 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2628 if (postlen < postimage->len)
2630 update_pre_post_images(preimage, postimage,
2631 fixed_buf, fixed_len, postlen);
2635 strbuf_release(&fixed);
2639 static int find_pos(struct apply_state *state,
2641 struct image *preimage,
2642 struct image *postimage,
2645 int match_beginning, int match_end)
2648 unsigned long backwards, forwards, try;
2649 int backwards_lno, forwards_lno, try_lno;
2652 * If match_beginning or match_end is specified, there is no
2653 * point starting from a wrong line that will never match and
2654 * wander around and wait for a match at the specified end.
2656 if (match_beginning)
2659 line = img->nr - preimage->nr;
2662 * Because the comparison is unsigned, the following test
2663 * will also take care of a negative line number that can
2664 * result when match_end and preimage is larger than the target.
2666 if ((size_t) line > img->nr)
2670 for (i = 0; i < line; i++)
2671 try += img->line[i].len;
2674 * There's probably some smart way to do this, but I'll leave
2675 * that to the smart and beautiful people. I'm simple and stupid.
2678 backwards_lno = line;
2680 forwards_lno = line;
2683 for (i = 0; ; i++) {
2684 if (match_fragment(state, img, preimage, postimage,
2685 try, try_lno, ws_rule,
2686 match_beginning, match_end))
2690 if (backwards_lno == 0 && forwards_lno == img->nr)
2694 if (backwards_lno == 0) {
2699 backwards -= img->line[backwards_lno].len;
2701 try_lno = backwards_lno;
2703 if (forwards_lno == img->nr) {
2707 forwards += img->line[forwards_lno].len;
2710 try_lno = forwards_lno;
2717 static void remove_first_line(struct image *img)
2719 img->buf += img->line[0].len;
2720 img->len -= img->line[0].len;
2725 static void remove_last_line(struct image *img)
2727 img->len -= img->line[--img->nr].len;
2731 * The change from "preimage" and "postimage" has been found to
2732 * apply at applied_pos (counts in line numbers) in "img".
2733 * Update "img" to remove "preimage" and replace it with "postimage".
2735 static void update_image(struct apply_state *state,
2738 struct image *preimage,
2739 struct image *postimage)
2742 * remove the copy of preimage at offset in img
2743 * and replace it with postimage
2746 size_t remove_count, insert_count, applied_at = 0;
2751 * If we are removing blank lines at the end of img,
2752 * the preimage may extend beyond the end.
2753 * If that is the case, we must be careful only to
2754 * remove the part of the preimage that falls within
2755 * the boundaries of img. Initialize preimage_limit
2756 * to the number of lines in the preimage that falls
2757 * within the boundaries.
2759 preimage_limit = preimage->nr;
2760 if (preimage_limit > img->nr - applied_pos)
2761 preimage_limit = img->nr - applied_pos;
2763 for (i = 0; i < applied_pos; i++)
2764 applied_at += img->line[i].len;
2767 for (i = 0; i < preimage_limit; i++)
2768 remove_count += img->line[applied_pos + i].len;
2769 insert_count = postimage->len;
2771 /* Adjust the contents */
2772 result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2773 memcpy(result, img->buf, applied_at);
2774 memcpy(result + applied_at, postimage->buf, postimage->len);
2775 memcpy(result + applied_at + postimage->len,
2776 img->buf + (applied_at + remove_count),
2777 img->len - (applied_at + remove_count));
2780 img->len += insert_count - remove_count;
2781 result[img->len] = '\0';
2783 /* Adjust the line table */
2784 nr = img->nr + postimage->nr - preimage_limit;
2785 if (preimage_limit < postimage->nr) {
2787 * NOTE: this knows that we never call remove_first_line()
2788 * on anything other than pre/post image.
2790 REALLOC_ARRAY(img->line, nr);
2791 img->line_allocated = img->line;
2793 if (preimage_limit != postimage->nr)
2794 memmove(img->line + applied_pos + postimage->nr,
2795 img->line + applied_pos + preimage_limit,
2796 (img->nr - (applied_pos + preimage_limit)) *
2797 sizeof(*img->line));
2798 memcpy(img->line + applied_pos,
2800 postimage->nr * sizeof(*img->line));
2801 if (!state->allow_overlap)
2802 for (i = 0; i < postimage->nr; i++)
2803 img->line[applied_pos + i].flag |= LINE_PATCHED;
2808 * Use the patch-hunk text in "frag" to prepare two images (preimage and
2809 * postimage) for the hunk. Find lines that match "preimage" in "img" and
2810 * replace the part of "img" with "postimage" text.
2812 static int apply_one_fragment(struct apply_state *state,
2813 struct image *img, struct fragment *frag,
2814 int inaccurate_eof, unsigned ws_rule,
2817 int match_beginning, match_end;
2818 const char *patch = frag->patch;
2819 int size = frag->size;
2820 char *old, *oldlines;
2821 struct strbuf newlines;
2822 int new_blank_lines_at_end = 0;
2823 int found_new_blank_lines_at_end = 0;
2824 int hunk_linenr = frag->linenr;
2825 unsigned long leading, trailing;
2826 int pos, applied_pos;
2827 struct image preimage;
2828 struct image postimage;
2830 memset(&preimage, 0, sizeof(preimage));
2831 memset(&postimage, 0, sizeof(postimage));
2832 oldlines = xmalloc(size);
2833 strbuf_init(&newlines, size);
2838 int len = linelen(patch, size);
2840 int added_blank_line = 0;
2841 int is_blank_context = 0;
2848 * "plen" is how much of the line we should use for
2849 * the actual patch data. Normally we just remove the
2850 * first character on the line, but if the line is
2851 * followed by "\ No newline", then we also remove the
2852 * last one (which is the newline, of course).
2855 if (len < size && patch[len] == '\\')
2858 if (state->apply_in_reverse) {
2861 else if (first == '+')
2867 /* Newer GNU diff, empty context line */
2869 /* ... followed by '\No newline'; nothing */
2872 strbuf_addch(&newlines, '\n');
2873 add_line_info(&preimage, "\n", 1, LINE_COMMON);
2874 add_line_info(&postimage, "\n", 1, LINE_COMMON);
2875 is_blank_context = 1;
2878 if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2879 ws_blank_line(patch + 1, plen, ws_rule))
2880 is_blank_context = 1;
2882 memcpy(old, patch + 1, plen);
2883 add_line_info(&preimage, old, plen,
2884 (first == ' ' ? LINE_COMMON : 0));
2888 /* Fall-through for ' ' */
2890 /* --no-add does not add new lines */
2891 if (first == '+' && state->no_add)
2894 start = newlines.len;
2896 !state->whitespace_error ||
2897 state->ws_error_action != correct_ws_error) {
2898 strbuf_add(&newlines, patch + 1, plen);
2901 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2903 add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2904 (first == '+' ? 0 : LINE_COMMON));
2906 (ws_rule & WS_BLANK_AT_EOF) &&
2907 ws_blank_line(patch + 1, plen, ws_rule))
2908 added_blank_line = 1;
2910 case '@': case '\\':
2911 /* Ignore it, we already handled it */
2914 if (state->apply_verbosely)
2915 error(_("invalid start of line: '%c'"), first);
2919 if (added_blank_line) {
2920 if (!new_blank_lines_at_end)
2921 found_new_blank_lines_at_end = hunk_linenr;
2922 new_blank_lines_at_end++;
2924 else if (is_blank_context)
2927 new_blank_lines_at_end = 0;
2932 if (inaccurate_eof &&
2933 old > oldlines && old[-1] == '\n' &&
2934 newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2936 strbuf_setlen(&newlines, newlines.len - 1);
2939 leading = frag->leading;
2940 trailing = frag->trailing;
2943 * A hunk to change lines at the beginning would begin with
2945 * but we need to be careful. -U0 that inserts before the second
2946 * line also has this pattern.
2948 * And a hunk to add to an empty file would begin with
2951 * In other words, a hunk that is (frag->oldpos <= 1) with or
2952 * without leading context must match at the beginning.
2954 match_beginning = (!frag->oldpos ||
2955 (frag->oldpos == 1 && !state->unidiff_zero));
2958 * A hunk without trailing lines must match at the end.
2959 * However, we simply cannot tell if a hunk must match end
2960 * from the lack of trailing lines if the patch was generated
2961 * with unidiff without any context.
2963 match_end = !state->unidiff_zero && !trailing;
2965 pos = frag->newpos ? (frag->newpos - 1) : 0;
2966 preimage.buf = oldlines;
2967 preimage.len = old - oldlines;
2968 postimage.buf = newlines.buf;
2969 postimage.len = newlines.len;
2970 preimage.line = preimage.line_allocated;
2971 postimage.line = postimage.line_allocated;
2975 applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2976 ws_rule, match_beginning, match_end);
2978 if (applied_pos >= 0)
2981 /* Am I at my context limits? */
2982 if ((leading <= state->p_context) && (trailing <= state->p_context))
2984 if (match_beginning || match_end) {
2985 match_beginning = match_end = 0;
2990 * Reduce the number of context lines; reduce both
2991 * leading and trailing if they are equal otherwise
2992 * just reduce the larger context.
2994 if (leading >= trailing) {
2995 remove_first_line(&preimage);
2996 remove_first_line(&postimage);
3000 if (trailing > leading) {
3001 remove_last_line(&preimage);
3002 remove_last_line(&postimage);
3007 if (applied_pos >= 0) {
3008 if (new_blank_lines_at_end &&
3009 preimage.nr + applied_pos >= img->nr &&
3010 (ws_rule & WS_BLANK_AT_EOF) &&
3011 state->ws_error_action != nowarn_ws_error) {
3012 record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
3013 found_new_blank_lines_at_end);
3014 if (state->ws_error_action == correct_ws_error) {
3015 while (new_blank_lines_at_end--)
3016 remove_last_line(&postimage);
3019 * We would want to prevent write_out_results()
3020 * from taking place in apply_patch() that follows
3021 * the callchain led us here, which is:
3022 * apply_patch->check_patch_list->check_patch->
3023 * apply_data->apply_fragments->apply_one_fragment
3025 if (state->ws_error_action == die_on_ws_error)
3029 if (state->apply_verbosely && applied_pos != pos) {
3030 int offset = applied_pos - pos;
3031 if (state->apply_in_reverse)
3032 offset = 0 - offset;
3034 Q_("Hunk #%d succeeded at %d (offset %d line).",
3035 "Hunk #%d succeeded at %d (offset %d lines).",
3037 nth_fragment, applied_pos + 1, offset);
3041 * Warn if it was necessary to reduce the number
3044 if ((leading != frag->leading) ||
3045 (trailing != frag->trailing))
3046 fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
3047 " to apply fragment at %d"),
3048 leading, trailing, applied_pos+1);
3049 update_image(state, img, applied_pos, &preimage, &postimage);
3051 if (state->apply_verbosely)
3052 error(_("while searching for:\n%.*s"),
3053 (int)(old - oldlines), oldlines);
3058 strbuf_release(&newlines);
3059 free(preimage.line_allocated);
3060 free(postimage.line_allocated);
3062 return (applied_pos < 0);
3065 static int apply_binary_fragment(struct apply_state *state,
3067 struct patch *patch)
3069 struct fragment *fragment = patch->fragments;
3074 return error(_("missing binary patch data for '%s'"),
3079 /* Binary patch is irreversible without the optional second hunk */
3080 if (state->apply_in_reverse) {
3081 if (!fragment->next)
3082 return error("cannot reverse-apply a binary patch "
3083 "without the reverse hunk to '%s'",
3085 ? patch->new_name : patch->old_name);
3086 fragment = fragment->next;
3088 switch (fragment->binary_patch_method) {
3089 case BINARY_DELTA_DEFLATED:
3090 dst = patch_delta(img->buf, img->len, fragment->patch,
3091 fragment->size, &len);
3098 case BINARY_LITERAL_DEFLATED:
3100 img->len = fragment->size;
3101 img->buf = xmemdupz(fragment->patch, img->len);
3108 * Replace "img" with the result of applying the binary patch.
3109 * The binary patch data itself in patch->fragment is still kept
3110 * but the preimage prepared by the caller in "img" is freed here
3111 * or in the helper function apply_binary_fragment() this calls.
3113 static int apply_binary(struct apply_state *state,
3115 struct patch *patch)
3117 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3118 unsigned char sha1[20];
3121 * For safety, we require patch index line to contain
3122 * full 40-byte textual SHA1 for old and new, at least for now.
3124 if (strlen(patch->old_sha1_prefix) != 40 ||
3125 strlen(patch->new_sha1_prefix) != 40 ||
3126 get_sha1_hex(patch->old_sha1_prefix, sha1) ||
3127 get_sha1_hex(patch->new_sha1_prefix, sha1))
3128 return error("cannot apply binary patch to '%s' "
3129 "without full index line", name);
3131 if (patch->old_name) {
3133 * See if the old one matches what the patch
3136 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3137 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3138 return error("the patch applies to '%s' (%s), "
3139 "which does not match the "
3140 "current contents.",
3141 name, sha1_to_hex(sha1));
3144 /* Otherwise, the old one must be empty. */
3146 return error("the patch applies to an empty "
3147 "'%s' but it is not empty", name);
3150 get_sha1_hex(patch->new_sha1_prefix, sha1);
3151 if (is_null_sha1(sha1)) {
3153 return 0; /* deletion patch */
3156 if (has_sha1_file(sha1)) {
3157 /* We already have the postimage */
3158 enum object_type type;
3162 result = read_sha1_file(sha1, &type, &size);
3164 return error("the necessary postimage %s for "
3165 "'%s' cannot be read",
3166 patch->new_sha1_prefix, name);
3172 * We have verified buf matches the preimage;
3173 * apply the patch data to it, which is stored
3174 * in the patch->fragments->{patch,size}.
3176 if (apply_binary_fragment(state, img, patch))
3177 return error(_("binary patch does not apply to '%s'"),
3180 /* verify that the result matches */
3181 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3182 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3183 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3184 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3190 static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3192 struct fragment *frag = patch->fragments;
3193 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3194 unsigned ws_rule = patch->ws_rule;
3195 unsigned inaccurate_eof = patch->inaccurate_eof;
3198 if (patch->is_binary)
3199 return apply_binary(state, img, patch);
3203 if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3204 error(_("patch failed: %s:%ld"), name, frag->oldpos);
3205 if (!state->apply_with_reject)
3214 static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3216 if (S_ISGITLINK(mode)) {
3217 strbuf_grow(buf, 100);
3218 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3220 enum object_type type;
3224 result = read_sha1_file(sha1, &type, &sz);
3227 /* XXX read_sha1_file NUL-terminates */
3228 strbuf_attach(buf, result, sz, sz + 1);
3233 static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3237 return read_blob_object(buf, ce->sha1, ce->ce_mode);
3240 static struct patch *in_fn_table(struct apply_state *state, const char *name)
3242 struct string_list_item *item;
3247 item = string_list_lookup(&state->fn_table, name);
3249 return (struct patch *)item->util;
3255 * item->util in the filename table records the status of the path.
3256 * Usually it points at a patch (whose result records the contents
3257 * of it after applying it), but it could be PATH_WAS_DELETED for a
3258 * path that a previously applied patch has already removed, or
3259 * PATH_TO_BE_DELETED for a path that a later patch would remove.
3261 * The latter is needed to deal with a case where two paths A and B
3262 * are swapped by first renaming A to B and then renaming B to A;
3263 * moving A to B should not be prevented due to presence of B as we
3264 * will remove it in a later patch.
3266 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3267 #define PATH_WAS_DELETED ((struct patch *) -1)
3269 static int to_be_deleted(struct patch *patch)
3271 return patch == PATH_TO_BE_DELETED;
3274 static int was_deleted(struct patch *patch)
3276 return patch == PATH_WAS_DELETED;
3279 static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3281 struct string_list_item *item;
3284 * Always add new_name unless patch is a deletion
3285 * This should cover the cases for normal diffs,
3286 * file creations and copies
3288 if (patch->new_name != NULL) {
3289 item = string_list_insert(&state->fn_table, patch->new_name);
3294 * store a failure on rename/deletion cases because
3295 * later chunks shouldn't patch old names
3297 if ((patch->new_name == NULL) || (patch->is_rename)) {
3298 item = string_list_insert(&state->fn_table, patch->old_name);
3299 item->util = PATH_WAS_DELETED;
3303 static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3306 * store information about incoming file deletion
3309 if ((patch->new_name == NULL) || (patch->is_rename)) {
3310 struct string_list_item *item;
3311 item = string_list_insert(&state->fn_table, patch->old_name);
3312 item->util = PATH_TO_BE_DELETED;
3314 patch = patch->next;
3318 static int checkout_target(struct index_state *istate,
3319 struct cache_entry *ce, struct stat *st)
3321 struct checkout costate;
3323 memset(&costate, 0, sizeof(costate));
3324 costate.base_dir = "";
3325 costate.refresh_cache = 1;
3326 costate.istate = istate;
3327 if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3328 return error(_("cannot checkout %s"), ce->name);
3332 static struct patch *previous_patch(struct apply_state *state,
3333 struct patch *patch,
3336 struct patch *previous;
3339 if (patch->is_copy || patch->is_rename)
3340 return NULL; /* "git" patches do not depend on the order */
3342 previous = in_fn_table(state, patch->old_name);
3346 if (to_be_deleted(previous))
3347 return NULL; /* the deletion hasn't happened yet */
3349 if (was_deleted(previous))
3355 static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3357 if (S_ISGITLINK(ce->ce_mode)) {
3358 if (!S_ISDIR(st->st_mode))
3362 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3365 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3367 static int load_patch_target(struct apply_state *state,
3369 const struct cache_entry *ce,
3372 unsigned expected_mode)
3374 if (state->cached || state->check_index) {
3375 if (read_file_or_gitlink(ce, buf))
3376 return error(_("failed to read %s"), name);
3378 if (S_ISGITLINK(expected_mode)) {
3380 return read_file_or_gitlink(ce, buf);
3382 return SUBMODULE_PATCH_WITHOUT_INDEX;
3383 } else if (has_symlink_leading_path(name, strlen(name))) {
3384 return error(_("reading from '%s' beyond a symbolic link"), name);
3386 if (read_old_data(st, name, buf))
3387 return error(_("failed to read %s"), name);
3394 * We are about to apply "patch"; populate the "image" with the
3395 * current version we have, from the working tree or from the index,
3396 * depending on the situation e.g. --cached/--index. If we are
3397 * applying a non-git patch that incrementally updates the tree,
3398 * we read from the result of a previous diff.
3400 static int load_preimage(struct apply_state *state,
3401 struct image *image,
3402 struct patch *patch, struct stat *st,
3403 const struct cache_entry *ce)
3405 struct strbuf buf = STRBUF_INIT;
3408 struct patch *previous;
3411 previous = previous_patch(state, patch, &status);
3413 return error(_("path %s has been renamed/deleted"),
3416 /* We have a patched copy in memory; use that. */
3417 strbuf_add(&buf, previous->result, previous->resultsize);
3419 status = load_patch_target(state, &buf, ce, st,
3420 patch->old_name, patch->old_mode);
3423 else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3425 * There is no way to apply subproject
3426 * patch without looking at the index.
3427 * NEEDSWORK: shouldn't this be flagged
3430 free_fragment_list(patch->fragments);
3431 patch->fragments = NULL;
3432 } else if (status) {
3433 return error(_("failed to read %s"), patch->old_name);
3437 img = strbuf_detach(&buf, &len);
3438 prepare_image(image, img, len, !patch->is_binary);
3442 static int three_way_merge(struct image *image,
3444 const unsigned char *base,
3445 const unsigned char *ours,
3446 const unsigned char *theirs)
3448 mmfile_t base_file, our_file, their_file;
3449 mmbuffer_t result = { NULL };
3452 read_mmblob(&base_file, base);
3453 read_mmblob(&our_file, ours);
3454 read_mmblob(&their_file, theirs);
3455 status = ll_merge(&result, path,
3458 &their_file, "theirs", NULL);
3459 free(base_file.ptr);
3461 free(their_file.ptr);
3462 if (status < 0 || !result.ptr) {
3467 image->buf = result.ptr;
3468 image->len = result.size;
3474 * When directly falling back to add/add three-way merge, we read from
3475 * the current contents of the new_name. In no cases other than that
3476 * this function will be called.
3478 static int load_current(struct apply_state *state,
3479 struct image *image,
3480 struct patch *patch)
3482 struct strbuf buf = STRBUF_INIT;
3487 struct cache_entry *ce;
3488 char *name = patch->new_name;
3489 unsigned mode = patch->new_mode;
3492 die("BUG: patch to %s is not a creation", patch->old_name);
3494 pos = cache_name_pos(name, strlen(name));
3496 return error(_("%s: does not exist in index"), name);
3497 ce = active_cache[pos];
3498 if (lstat(name, &st)) {
3499 if (errno != ENOENT)
3500 return error_errno("%s", name);
3501 if (checkout_target(&the_index, ce, &st))
3504 if (verify_index_match(ce, &st))
3505 return error(_("%s: does not match index"), name);
3507 status = load_patch_target(state, &buf, ce, &st, name, mode);
3512 img = strbuf_detach(&buf, &len);
3513 prepare_image(image, img, len, !patch->is_binary);
3517 static int try_threeway(struct apply_state *state,
3518 struct image *image,
3519 struct patch *patch,
3521 const struct cache_entry *ce)
3523 unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3524 struct strbuf buf = STRBUF_INIT;
3528 struct image tmp_image;
3530 /* No point falling back to 3-way merge in these cases */
3531 if (patch->is_delete ||
3532 S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3535 /* Preimage the patch was prepared for */
3537 write_sha1_file("", 0, blob_type, pre_sha1);
3538 else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3539 read_blob_object(&buf, pre_sha1, patch->old_mode))
3540 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3542 fprintf(stderr, "Falling back to three-way merge...\n");
3544 img = strbuf_detach(&buf, &len);
3545 prepare_image(&tmp_image, img, len, 1);
3546 /* Apply the patch to get the post image */
3547 if (apply_fragments(state, &tmp_image, patch) < 0) {
3548 clear_image(&tmp_image);
3551 /* post_sha1[] is theirs */
3552 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3553 clear_image(&tmp_image);
3555 /* our_sha1[] is ours */
3556 if (patch->is_new) {
3557 if (load_current(state, &tmp_image, patch))
3558 return error("cannot read the current contents of '%s'",
3561 if (load_preimage(state, &tmp_image, patch, st, ce))
3562 return error("cannot read the current contents of '%s'",
3565 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3566 clear_image(&tmp_image);
3568 /* in-core three-way merge between post and our using pre as base */
3569 status = three_way_merge(image, patch->new_name,
3570 pre_sha1, our_sha1, post_sha1);
3572 fprintf(stderr, "Failed to fall back on three-way merge...\n");
3577 patch->conflicted_threeway = 1;
3579 oidclr(&patch->threeway_stage[0]);
3581 hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3582 hashcpy(patch->threeway_stage[1].hash, our_sha1);
3583 hashcpy(patch->threeway_stage[2].hash, post_sha1);
3584 fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3586 fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3591 static int apply_data(struct apply_state *state, struct patch *patch,
3592 struct stat *st, const struct cache_entry *ce)
3596 if (load_preimage(state, &image, patch, st, ce) < 0)
3599 if (patch->direct_to_threeway ||
3600 apply_fragments(state, &image, patch) < 0) {
3601 /* Note: with --reject, apply_fragments() returns 0 */
3602 if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
3605 patch->result = image.buf;
3606 patch->resultsize = image.len;
3607 add_to_fn_table(state, patch);
3608 free(image.line_allocated);
3610 if (0 < patch->is_delete && patch->resultsize)
3611 return error(_("removal patch leaves file contents"));
3617 * If "patch" that we are looking at modifies or deletes what we have,
3618 * we would want it not to lose any local modification we have, either
3619 * in the working tree or in the index.
3621 * This also decides if a non-git patch is a creation patch or a
3622 * modification to an existing empty file. We do not check the state
3623 * of the current tree for a creation patch in this function; the caller
3624 * check_patch() separately makes sure (and errors out otherwise) that
3625 * the path the patch creates does not exist in the current tree.
3627 static int check_preimage(struct apply_state *state,
3628 struct patch *patch,
3629 struct cache_entry **ce,
3632 const char *old_name = patch->old_name;
3633 struct patch *previous = NULL;
3634 int stat_ret = 0, status;
3635 unsigned st_mode = 0;
3640 assert(patch->is_new <= 0);
3641 previous = previous_patch(state, patch, &status);
3644 return error(_("path %s has been renamed/deleted"), old_name);
3646 st_mode = previous->new_mode;
3647 } else if (!state->cached) {
3648 stat_ret = lstat(old_name, st);
3649 if (stat_ret && errno != ENOENT)
3650 return error_errno("%s", old_name);
3653 if (state->check_index && !previous) {
3654 int pos = cache_name_pos(old_name, strlen(old_name));
3656 if (patch->is_new < 0)
3658 return error(_("%s: does not exist in index"), old_name);
3660 *ce = active_cache[pos];
3662 if (checkout_target(&the_index, *ce, st))
3665 if (!state->cached && verify_index_match(*ce, st))
3666 return error(_("%s: does not match index"), old_name);
3668 st_mode = (*ce)->ce_mode;
3669 } else if (stat_ret < 0) {
3670 if (patch->is_new < 0)
3672 return error_errno("%s", old_name);
3675 if (!state->cached && !previous)
3676 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3678 if (patch->is_new < 0)
3680 if (!patch->old_mode)
3681 patch->old_mode = st_mode;
3682 if ((st_mode ^ patch->old_mode) & S_IFMT)
3683 return error(_("%s: wrong type"), old_name);
3684 if (st_mode != patch->old_mode)
3685 warning(_("%s has type %o, expected %o"),
3686 old_name, st_mode, patch->old_mode);
3687 if (!patch->new_mode && !patch->is_delete)
3688 patch->new_mode = st_mode;
3693 patch->is_delete = 0;
3694 free(patch->old_name);
3695 patch->old_name = NULL;
3700 #define EXISTS_IN_INDEX 1
3701 #define EXISTS_IN_WORKTREE 2
3703 static int check_to_create(struct apply_state *state,
3704 const char *new_name,
3709 if (state->check_index &&
3710 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3712 return EXISTS_IN_INDEX;
3716 if (!lstat(new_name, &nst)) {
3717 if (S_ISDIR(nst.st_mode) || ok_if_exists)
3720 * A leading component of new_name might be a symlink
3721 * that is going to be removed with this patch, but
3722 * still pointing at somewhere that has the path.
3723 * In such a case, path "new_name" does not exist as
3724 * far as git is concerned.
3726 if (has_symlink_leading_path(new_name, strlen(new_name)))
3729 return EXISTS_IN_WORKTREE;
3730 } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3731 return error_errno("%s", new_name);
3736 static uintptr_t register_symlink_changes(struct apply_state *state,
3740 struct string_list_item *ent;
3742 ent = string_list_lookup(&state->symlink_changes, path);
3744 ent = string_list_insert(&state->symlink_changes, path);
3745 ent->util = (void *)0;
3747 ent->util = (void *)(what | ((uintptr_t)ent->util));
3748 return (uintptr_t)ent->util;
3751 static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3753 struct string_list_item *ent;
3755 ent = string_list_lookup(&state->symlink_changes, path);
3758 return (uintptr_t)ent->util;
3761 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3763 for ( ; patch; patch = patch->next) {
3764 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3765 (patch->is_rename || patch->is_delete))
3766 /* the symlink at patch->old_name is removed */
3767 register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3769 if (patch->new_name && S_ISLNK(patch->new_mode))
3770 /* the symlink at patch->new_name is created or remains */
3771 register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3775 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3778 unsigned int change;
3780 while (--name->len && name->buf[name->len] != '/')
3781 ; /* scan backwards */
3784 name->buf[name->len] = '\0';
3785 change = check_symlink_changes(state, name->buf);
3786 if (change & APPLY_SYMLINK_IN_RESULT)
3788 if (change & APPLY_SYMLINK_GOES_AWAY)
3790 * This cannot be "return 0", because we may
3791 * see a new one created at a higher level.
3795 /* otherwise, check the preimage */
3796 if (state->check_index) {
3797 struct cache_entry *ce;
3799 ce = cache_file_exists(name->buf, name->len, ignore_case);
3800 if (ce && S_ISLNK(ce->ce_mode))
3804 if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3811 static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3814 struct strbuf name = STRBUF_INIT;
3816 assert(*name_ != '\0');
3817 strbuf_addstr(&name, name_);
3818 ret = path_is_beyond_symlink_1(state, &name);
3819 strbuf_release(&name);
3824 static int check_unsafe_path(struct patch *patch)
3826 const char *old_name = NULL;
3827 const char *new_name = NULL;
3828 if (patch->is_delete)
3829 old_name = patch->old_name;
3830 else if (!patch->is_new && !patch->is_copy)
3831 old_name = patch->old_name;
3832 if (!patch->is_delete)
3833 new_name = patch->new_name;
3835 if (old_name && !verify_path(old_name))
3836 return error(_("invalid path '%s'"), old_name);
3837 if (new_name && !verify_path(new_name))
3838 return error(_("invalid path '%s'"), new_name);
3843 * Check and apply the patch in-core; leave the result in patch->result
3844 * for the caller to write it out to the final destination.
3846 static int check_patch(struct apply_state *state, struct patch *patch)
3849 const char *old_name = patch->old_name;
3850 const char *new_name = patch->new_name;
3851 const char *name = old_name ? old_name : new_name;
3852 struct cache_entry *ce = NULL;
3853 struct patch *tpatch;
3857 patch->rejected = 1; /* we will drop this after we succeed */
3859 status = check_preimage(state, patch, &ce, &st);
3862 old_name = patch->old_name;
3865 * A type-change diff is always split into a patch to delete
3866 * old, immediately followed by a patch to create new (see
3867 * diff.c::run_diff()); in such a case it is Ok that the entry
3868 * to be deleted by the previous patch is still in the working
3869 * tree and in the index.
3871 * A patch to swap-rename between A and B would first rename A
3872 * to B and then rename B to A. While applying the first one,
3873 * the presence of B should not stop A from getting renamed to
3874 * B; ask to_be_deleted() about the later rename. Removal of
3875 * B and rename from A to B is handled the same way by asking
3878 if ((tpatch = in_fn_table(state, new_name)) &&
3879 (was_deleted(tpatch) || to_be_deleted(tpatch)))
3885 ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3886 int err = check_to_create(state, new_name, ok_if_exists);
3888 if (err && state->threeway) {
3889 patch->direct_to_threeway = 1;
3890 } else switch (err) {
3893 case EXISTS_IN_INDEX:
3894 return error(_("%s: already exists in index"), new_name);
3896 case EXISTS_IN_WORKTREE:
3897 return error(_("%s: already exists in working directory"),
3903 if (!patch->new_mode) {
3904 if (0 < patch->is_new)
3905 patch->new_mode = S_IFREG | 0644;
3907 patch->new_mode = patch->old_mode;
3911 if (new_name && old_name) {
3912 int same = !strcmp(old_name, new_name);
3913 if (!patch->new_mode)
3914 patch->new_mode = patch->old_mode;
3915 if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3917 return error(_("new mode (%o) of %s does not "
3918 "match old mode (%o)"),
3919 patch->new_mode, new_name,
3922 return error(_("new mode (%o) of %s does not "
3923 "match old mode (%o) of %s"),
3924 patch->new_mode, new_name,
3925 patch->old_mode, old_name);
3929 if (!state->unsafe_paths && check_unsafe_path(patch))
3933 * An attempt to read from or delete a path that is beyond a
3934 * symbolic link will be prevented by load_patch_target() that
3935 * is called at the beginning of apply_data() so we do not
3936 * have to worry about a patch marked with "is_delete" bit
3937 * here. We however need to make sure that the patch result
3938 * is not deposited to a path that is beyond a symbolic link
3941 if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3942 return error(_("affected file '%s' is beyond a symbolic link"),
3945 if (apply_data(state, patch, &st, ce) < 0)
3946 return error(_("%s: patch does not apply"), name);
3947 patch->rejected = 0;
3951 static int check_patch_list(struct apply_state *state, struct patch *patch)
3955 prepare_symlink_changes(state, patch);
3956 prepare_fn_table(state, patch);
3959 if (state->apply_verbosely)
3960 say_patch_name(stderr,
3961 _("Checking patch %s..."), patch);
3962 res = check_patch(state, patch);
3966 patch = patch->next;
3971 /* This function tries to read the sha1 from the current index */
3972 static int get_current_sha1(const char *path, unsigned char *sha1)
3976 if (read_cache() < 0)
3978 pos = cache_name_pos(path, strlen(path));
3981 hashcpy(sha1, active_cache[pos]->sha1);
3985 static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3988 * A usable gitlink patch has only one fragment (hunk) that looks like:
3990 * -Subproject commit <old sha1>
3991 * +Subproject commit <new sha1>
3994 * -Subproject commit <old sha1>
3995 * for a removal patch.
3997 struct fragment *hunk = p->fragments;
3998 static const char heading[] = "-Subproject commit ";
4001 if (/* does the patch have only one hunk? */
4002 hunk && !hunk->next &&
4003 /* is its preimage one line? */
4004 hunk->oldpos == 1 && hunk->oldlines == 1 &&
4005 /* does preimage begin with the heading? */
4006 (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
4007 starts_with(++preimage, heading) &&
4008 /* does it record full SHA-1? */
4009 !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
4010 preimage[sizeof(heading) + 40 - 1] == '\n' &&
4011 /* does the abbreviated name on the index line agree with it? */
4012 starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
4013 return 0; /* it all looks fine */
4015 /* we may have full object name on the index line */
4016 return get_sha1_hex(p->old_sha1_prefix, sha1);
4019 /* Build an index that contains the just the files needed for a 3way merge */
4020 static int build_fake_ancestor(struct patch *list, const char *filename)
4022 struct patch *patch;
4023 struct index_state result = { NULL };
4024 static struct lock_file lock;
4027 /* Once we start supporting the reverse patch, it may be
4028 * worth showing the new sha1 prefix, but until then...
4030 for (patch = list; patch; patch = patch->next) {
4031 unsigned char sha1[20];
4032 struct cache_entry *ce;
4035 name = patch->old_name ? patch->old_name : patch->new_name;
4036 if (0 < patch->is_new)
4039 if (S_ISGITLINK(patch->old_mode)) {
4040 if (!preimage_sha1_in_gitlink_patch(patch, sha1))
4041 ; /* ok, the textual part looks sane */
4043 return error("sha1 information is lacking or "
4044 "useless for submodule %s", name);
4045 } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
4047 } else if (!patch->lines_added && !patch->lines_deleted) {
4048 /* mode-only change: update the current */
4049 if (get_current_sha1(patch->old_name, sha1))
4050 return error("mode change for %s, which is not "
4051 "in current HEAD", name);
4053 return error("sha1 information is lacking or useless "
4056 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
4058 return error(_("make_cache_entry failed for path '%s'"),
4060 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
4062 return error("Could not add %s to temporary index",
4067 hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
4068 res = write_locked_index(&result, &lock, COMMIT_LOCK);
4069 discard_index(&result);
4072 return error("Could not write temporary index to %s", filename);
4077 static void stat_patch_list(struct apply_state *state, struct patch *patch)
4079 int files, adds, dels;
4081 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
4083 adds += patch->lines_added;
4084 dels += patch->lines_deleted;
4085 show_stats(state, patch);
4088 print_stat_summary(stdout, files, adds, dels);
4091 static void numstat_patch_list(struct apply_state *state,
4092 struct patch *patch)
4094 for ( ; patch; patch = patch->next) {
4096 name = patch->new_name ? patch->new_name : patch->old_name;
4097 if (patch->is_binary)
4100 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
4101 write_name_quoted(name, stdout, state->line_termination);
4105 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
4108 printf(" %s mode %06o %s\n", newdelete, mode, name);
4110 printf(" %s %s\n", newdelete, name);
4113 static void show_mode_change(struct patch *p, int show_name)
4115 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
4117 printf(" mode change %06o => %06o %s\n",
4118 p->old_mode, p->new_mode, p->new_name);
4120 printf(" mode change %06o => %06o\n",
4121 p->old_mode, p->new_mode);
4125 static void show_rename_copy(struct patch *p)
4127 const char *renamecopy = p->is_rename ? "rename" : "copy";
4128 const char *old, *new;
4130 /* Find common prefix */
4134 const char *slash_old, *slash_new;
4135 slash_old = strchr(old, '/');
4136 slash_new = strchr(new, '/');
4139 slash_old - old != slash_new - new ||
4140 memcmp(old, new, slash_new - new))
4142 old = slash_old + 1;
4143 new = slash_new + 1;
4145 /* p->old_name thru old is the common prefix, and old and new
4146 * through the end of names are renames
4148 if (old != p->old_name)
4149 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4150 (int)(old - p->old_name), p->old_name,
4151 old, new, p->score);
4153 printf(" %s %s => %s (%d%%)\n", renamecopy,
4154 p->old_name, p->new_name, p->score);
4155 show_mode_change(p, 0);
4158 static void summary_patch_list(struct patch *patch)
4162 for (p = patch; p; p = p->next) {
4164 show_file_mode_name("create", p->new_mode, p->new_name);
4165 else if (p->is_delete)
4166 show_file_mode_name("delete", p->old_mode, p->old_name);
4168 if (p->is_rename || p->is_copy)
4169 show_rename_copy(p);
4172 printf(" rewrite %s (%d%%)\n",
4173 p->new_name, p->score);
4174 show_mode_change(p, 0);
4177 show_mode_change(p, 1);
4183 static void patch_stats(struct apply_state *state, struct patch *patch)
4185 int lines = patch->lines_added + patch->lines_deleted;
4187 if (lines > state->max_change)
4188 state->max_change = lines;
4189 if (patch->old_name) {
4190 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4192 len = strlen(patch->old_name);
4193 if (len > state->max_len)
4194 state->max_len = len;
4196 if (patch->new_name) {
4197 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4199 len = strlen(patch->new_name);
4200 if (len > state->max_len)
4201 state->max_len = len;
4205 static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4207 if (state->update_index) {
4208 if (remove_file_from_cache(patch->old_name) < 0)
4209 return error(_("unable to remove %s from index"), patch->old_name);
4211 if (!state->cached) {
4212 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4213 remove_path(patch->old_name);
4219 static int add_index_file(struct apply_state *state,
4226 struct cache_entry *ce;
4227 int namelen = strlen(path);
4228 unsigned ce_size = cache_entry_size(namelen);
4230 if (!state->update_index)
4233 ce = xcalloc(1, ce_size);
4234 memcpy(ce->name, path, namelen);
4235 ce->ce_mode = create_ce_mode(mode);
4236 ce->ce_flags = create_ce_flags(0);
4237 ce->ce_namelen = namelen;
4238 if (S_ISGITLINK(mode)) {
4241 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4242 get_sha1_hex(s, ce->sha1)) {
4244 return error(_("corrupt patch for submodule %s"), path);
4247 if (!state->cached) {
4248 if (lstat(path, &st) < 0) {
4250 return error_errno(_("unable to stat newly "
4251 "created file '%s'"),
4254 fill_stat_cache_info(ce, &st);
4256 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
4258 return error(_("unable to create backing store "
4259 "for newly created file %s"), path);
4262 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4264 return error(_("unable to add cache entry for %s"), path);
4272 * -1 if an unrecoverable error happened
4273 * 0 if everything went well
4274 * 1 if a recoverable error happened
4276 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4279 struct strbuf nbuf = STRBUF_INIT;
4281 if (S_ISGITLINK(mode)) {
4283 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4285 return !!mkdir(path, 0777);
4288 if (has_symlinks && S_ISLNK(mode))
4289 /* Although buf:size is counted string, it also is NUL
4292 return !!symlink(buf, path);
4294 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4298 if (convert_to_working_tree(path, buf, size, &nbuf)) {
4303 res = write_in_full(fd, buf, size) < 0;
4305 error_errno(_("failed to write to '%s'"), path);
4306 strbuf_release(&nbuf);
4308 if (close(fd) < 0 && !res)
4309 return error_errno(_("closing file '%s'"), path);
4311 return res ? -1 : 0;
4315 * We optimistically assume that the directories exist,
4316 * which is true 99% of the time anyway. If they don't,
4317 * we create them and try again.
4323 static int create_one_file(struct apply_state *state,
4334 res = try_create_file(path, mode, buf, size);
4340 if (errno == ENOENT) {
4341 if (safe_create_leading_directories(path))
4343 res = try_create_file(path, mode, buf, size);
4350 if (errno == EEXIST || errno == EACCES) {
4351 /* We may be trying to create a file where a directory
4355 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4359 if (errno == EEXIST) {
4360 unsigned int nr = getpid();
4363 char newpath[PATH_MAX];
4364 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4365 res = try_create_file(newpath, mode, buf, size);
4369 if (!rename(newpath, path))
4371 unlink_or_warn(newpath);
4374 if (errno != EEXIST)
4379 return error_errno(_("unable to write file '%s' mode %o"),
4383 static int add_conflicted_stages_file(struct apply_state *state,
4384 struct patch *patch)
4387 unsigned ce_size, mode;
4388 struct cache_entry *ce;
4390 if (!state->update_index)
4392 namelen = strlen(patch->new_name);
4393 ce_size = cache_entry_size(namelen);
4394 mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4396 remove_file_from_cache(patch->new_name);
4397 for (stage = 1; stage < 4; stage++) {
4398 if (is_null_oid(&patch->threeway_stage[stage - 1]))
4400 ce = xcalloc(1, ce_size);
4401 memcpy(ce->name, patch->new_name, namelen);
4402 ce->ce_mode = create_ce_mode(mode);
4403 ce->ce_flags = create_ce_flags(stage);
4404 ce->ce_namelen = namelen;
4405 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4406 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4408 return error(_("unable to add cache entry for %s"),
4416 static int create_file(struct apply_state *state, struct patch *patch)
4418 char *path = patch->new_name;
4419 unsigned mode = patch->new_mode;
4420 unsigned long size = patch->resultsize;
4421 char *buf = patch->result;
4424 mode = S_IFREG | 0644;
4425 if (create_one_file(state, path, mode, buf, size))
4428 if (patch->conflicted_threeway)
4429 return add_conflicted_stages_file(state, patch);
4431 return add_index_file(state, path, mode, buf, size);
4434 /* phase zero is to remove, phase one is to create */
4435 static int write_out_one_result(struct apply_state *state,
4436 struct patch *patch,
4439 if (patch->is_delete > 0) {
4441 return remove_file(state, patch, 1);
4444 if (patch->is_new > 0 || patch->is_copy) {
4446 return create_file(state, patch);
4450 * Rename or modification boils down to the same
4451 * thing: remove the old, write the new
4454 return remove_file(state, patch, patch->is_rename);
4456 return create_file(state, patch);
4460 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4463 char namebuf[PATH_MAX];
4464 struct fragment *frag;
4466 struct strbuf sb = STRBUF_INIT;
4468 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4469 if (!frag->rejected)
4475 if (state->apply_verbosely)
4476 say_patch_name(stderr,
4477 _("Applied patch %s cleanly."), patch);
4481 /* This should not happen, because a removal patch that leaves
4482 * contents are marked "rejected" at the patch level.
4484 if (!patch->new_name)
4485 die(_("internal error"));
4487 /* Say this even without --verbose */
4488 strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4489 "Applying patch %%s with %d rejects...",
4492 say_patch_name(stderr, sb.buf, patch);
4493 strbuf_release(&sb);
4495 cnt = strlen(patch->new_name);
4496 if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4497 cnt = ARRAY_SIZE(namebuf) - 5;
4498 warning(_("truncating .rej filename to %.*s.rej"),
4499 cnt - 1, patch->new_name);
4501 memcpy(namebuf, patch->new_name, cnt);
4502 memcpy(namebuf + cnt, ".rej", 5);
4504 rej = fopen(namebuf, "w");
4506 return error_errno(_("cannot open %s"), namebuf);
4508 /* Normal git tools never deal with .rej, so do not pretend
4509 * this is a git patch by saying --git or giving extended
4510 * headers. While at it, maybe please "kompare" that wants
4511 * the trailing TAB and some garbage at the end of line ;-).
4513 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4514 patch->new_name, patch->new_name);
4515 for (cnt = 1, frag = patch->fragments;
4517 cnt++, frag = frag->next) {
4518 if (!frag->rejected) {
4519 fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4522 fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4523 fprintf(rej, "%.*s", frag->size, frag->patch);
4524 if (frag->patch[frag->size-1] != '\n')
4533 * -1 if an error happened
4534 * 0 if the patch applied cleanly
4535 * 1 if the patch did not apply cleanly
4537 static int write_out_results(struct apply_state *state, struct patch *list)
4542 struct string_list cpath = STRING_LIST_INIT_DUP;
4544 for (phase = 0; phase < 2; phase++) {
4550 if (write_out_one_result(state, l, phase)) {
4551 string_list_clear(&cpath, 0);
4555 if (write_out_one_reject(state, l))
4557 if (l->conflicted_threeway) {
4558 string_list_append(&cpath, l->new_name);
4568 struct string_list_item *item;
4570 string_list_sort(&cpath);
4571 for_each_string_list_item(item, &cpath)
4572 fprintf(stderr, "U %s\n", item->string);
4573 string_list_clear(&cpath, 0);
4582 * Try to apply a patch.
4585 * -128 if a bad error happened (like patch unreadable)
4586 * -1 if patch did not apply and user cannot deal with it
4587 * 0 if the patch applied
4588 * 1 if the patch did not apply but user might fix it
4590 static int apply_patch(struct apply_state *state,
4592 const char *filename,
4596 struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4597 struct patch *list = NULL, **listp = &list;
4598 int skipped_patch = 0;
4601 state->patch_input_file = filename;
4602 if (read_patch_file(&buf, fd) < 0)
4605 while (offset < buf.len) {
4606 struct patch *patch;
4609 patch = xcalloc(1, sizeof(*patch));
4610 patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
4611 patch->recount = !!(options & APPLY_OPT_RECOUNT);
4612 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4621 if (state->apply_in_reverse)
4622 reverse_patches(patch);
4623 if (use_patch(state, patch)) {
4624 patch_stats(state, patch);
4626 listp = &patch->next;
4629 if (state->apply_verbosely)
4630 say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4637 if (!list && !skipped_patch) {
4638 error(_("unrecognized input"));
4643 if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4646 state->update_index = state->check_index && state->apply;
4647 if (state->update_index && state->newfd < 0)
4648 state->newfd = hold_locked_index(state->lock_file, 1);
4650 if (state->check_index && read_cache() < 0) {
4651 error(_("unable to read index file"));
4656 if (state->check || state->apply) {
4657 int r = check_patch_list(state, list);
4662 if (r < 0 && !state->apply_with_reject) {
4669 int write_res = write_out_results(state, list);
4670 if (write_res < 0) {
4674 if (write_res > 0) {
4675 /* with --3way, we still need to write the index out */
4676 res = state->apply_with_reject ? -1 : 1;
4681 if (state->fake_ancestor &&
4682 build_fake_ancestor(list, state->fake_ancestor)) {
4687 if (state->diffstat)
4688 stat_patch_list(state, list);
4691 numstat_patch_list(state, list);
4694 summary_patch_list(list);
4697 free_patch_list(list);
4698 strbuf_release(&buf);
4699 string_list_clear(&state->fn_table, 0);
4703 int apply_option_parse_exclude(const struct option *opt,
4704 const char *arg, int unset)
4706 struct apply_state *state = opt->value;
4707 add_name_limit(state, arg, 1);
4711 int apply_option_parse_include(const struct option *opt,
4712 const char *arg, int unset)
4714 struct apply_state *state = opt->value;
4715 add_name_limit(state, arg, 0);
4716 state->has_include = 1;
4720 int apply_option_parse_p(const struct option *opt,
4724 struct apply_state *state = opt->value;
4725 state->p_value = atoi(arg);
4726 state->p_value_known = 1;
4730 int apply_option_parse_space_change(const struct option *opt,
4731 const char *arg, int unset)
4733 struct apply_state *state = opt->value;
4735 state->ws_ignore_action = ignore_ws_none;
4737 state->ws_ignore_action = ignore_ws_change;
4741 int apply_option_parse_whitespace(const struct option *opt,
4742 const char *arg, int unset)
4744 struct apply_state *state = opt->value;
4745 state->whitespace_option = arg;
4746 if (parse_whitespace_option(state, arg))
4751 int apply_option_parse_directory(const struct option *opt,
4752 const char *arg, int unset)
4754 struct apply_state *state = opt->value;
4755 strbuf_reset(&state->root);
4756 strbuf_addstr(&state->root, arg);
4757 strbuf_complete(&state->root, '/');
4761 int apply_all_patches(struct apply_state *state,
4771 for (i = 0; i < argc; i++) {
4772 const char *arg = argv[i];
4775 if (!strcmp(arg, "-")) {
4776 res = apply_patch(state, 0, "<stdin>", options);
4782 } else if (0 < state->prefix_length)
4783 arg = prefix_filename(state->prefix,
4784 state->prefix_length,
4787 fd = open(arg, O_RDONLY);
4789 error(_("can't open patch '%s': %s"), arg, strerror(errno));
4794 set_default_whitespace_mode(state);
4795 res = apply_patch(state, fd, arg, options);
4801 set_default_whitespace_mode(state);
4803 res = apply_patch(state, 0, "<stdin>", options);
4809 if (state->whitespace_error) {
4810 if (state->squelch_whitespace_errors &&
4811 state->squelch_whitespace_errors < state->whitespace_error) {
4813 state->whitespace_error - state->squelch_whitespace_errors;
4814 warning(Q_("squelched %d whitespace error",
4815 "squelched %d whitespace errors",
4819 if (state->ws_error_action == die_on_ws_error) {
4820 error(Q_("%d line adds whitespace errors.",
4821 "%d lines add whitespace errors.",
4822 state->whitespace_error),
4823 state->whitespace_error);
4827 if (state->applied_after_fixing_ws && state->apply)
4828 warning("%d line%s applied after"
4829 " fixing whitespace errors.",
4830 state->applied_after_fixing_ws,
4831 state->applied_after_fixing_ws == 1 ? "" : "s");
4832 else if (state->whitespace_error)
4833 warning(Q_("%d line adds whitespace errors.",
4834 "%d lines add whitespace errors.",
4835 state->whitespace_error),
4836 state->whitespace_error);
4839 if (state->update_index) {
4840 res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
4842 error(_("Unable to write new index file"));
4852 if (state->newfd >= 0) {
4853 rollback_lock_file(state->lock_file);
4857 return (res == -1 ? 1 : 128);