t/test-lib: introduce --chain-lint option
[git/debian.git] / builtin / apply.c
blob65b97eee69d4814bf80f87b1fd6857b8ed80cb90
1 /*
2 * apply.c
4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
8 */
9 #include "cache.h"
10 #include "lockfile.h"
11 #include "cache-tree.h"
12 #include "quote.h"
13 #include "blob.h"
14 #include "delta.h"
15 #include "builtin.h"
16 #include "string-list.h"
17 #include "dir.h"
18 #include "diff.h"
19 #include "parse-options.h"
20 #include "xdiff-interface.h"
21 #include "ll-merge.h"
22 #include "rerere.h"
25 * --check turns on checking that the working tree matches the
26 * files that are being modified, but doesn't apply the patch
27 * --stat does just a diffstat, and doesn't actually apply
28 * --numstat does numeric diffstat, and doesn't actually apply
29 * --index-info shows the old and new index info for paths if available.
30 * --index updates the cache as well.
31 * --cached updates only the cache without ever touching the working tree.
33 static const char *prefix;
34 static int prefix_length = -1;
35 static int newfd = -1;
37 static int unidiff_zero;
38 static int p_value = 1;
39 static int p_value_known;
40 static int check_index;
41 static int update_index;
42 static int cached;
43 static int diffstat;
44 static int numstat;
45 static int summary;
46 static int check;
47 static int apply = 1;
48 static int apply_in_reverse;
49 static int apply_with_reject;
50 static int apply_verbosely;
51 static int allow_overlap;
52 static int no_add;
53 static int threeway;
54 static int unsafe_paths;
55 static const char *fake_ancestor;
56 static int line_termination = '\n';
57 static unsigned int p_context = UINT_MAX;
58 static const char * const apply_usage[] = {
59 N_("git apply [<options>] [<patch>...]"),
60 NULL
63 static enum ws_error_action {
64 nowarn_ws_error,
65 warn_on_ws_error,
66 die_on_ws_error,
67 correct_ws_error
68 } ws_error_action = warn_on_ws_error;
69 static int whitespace_error;
70 static int squelch_whitespace_errors = 5;
71 static int applied_after_fixing_ws;
73 static enum ws_ignore {
74 ignore_ws_none,
75 ignore_ws_change
76 } ws_ignore_action = ignore_ws_none;
79 static const char *patch_input_file;
80 static const char *root;
81 static int root_len;
82 static int read_stdin = 1;
83 static int options;
85 static void parse_whitespace_option(const char *option)
87 if (!option) {
88 ws_error_action = warn_on_ws_error;
89 return;
91 if (!strcmp(option, "warn")) {
92 ws_error_action = warn_on_ws_error;
93 return;
95 if (!strcmp(option, "nowarn")) {
96 ws_error_action = nowarn_ws_error;
97 return;
99 if (!strcmp(option, "error")) {
100 ws_error_action = die_on_ws_error;
101 return;
103 if (!strcmp(option, "error-all")) {
104 ws_error_action = die_on_ws_error;
105 squelch_whitespace_errors = 0;
106 return;
108 if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
109 ws_error_action = correct_ws_error;
110 return;
112 die(_("unrecognized whitespace option '%s'"), option);
115 static void parse_ignorewhitespace_option(const char *option)
117 if (!option || !strcmp(option, "no") ||
118 !strcmp(option, "false") || !strcmp(option, "never") ||
119 !strcmp(option, "none")) {
120 ws_ignore_action = ignore_ws_none;
121 return;
123 if (!strcmp(option, "change")) {
124 ws_ignore_action = ignore_ws_change;
125 return;
127 die(_("unrecognized whitespace ignore option '%s'"), option);
130 static void set_default_whitespace_mode(const char *whitespace_option)
132 if (!whitespace_option && !apply_default_whitespace)
133 ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
137 * For "diff-stat" like behaviour, we keep track of the biggest change
138 * we've seen, and the longest filename. That allows us to do simple
139 * scaling.
141 static int max_change, max_len;
144 * Various "current state", notably line numbers and what
145 * file (and how) we're patching right now.. The "is_xxxx"
146 * things are flags, where -1 means "don't know yet".
148 static int linenr = 1;
151 * This represents one "hunk" from a patch, starting with
152 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
153 * patch text is pointed at by patch, and its byte length
154 * is stored in size. leading and trailing are the number
155 * of context lines.
157 struct fragment {
158 unsigned long leading, trailing;
159 unsigned long oldpos, oldlines;
160 unsigned long newpos, newlines;
162 * 'patch' is usually borrowed from buf in apply_patch(),
163 * but some codepaths store an allocated buffer.
165 const char *patch;
166 unsigned free_patch:1,
167 rejected:1;
168 int size;
169 int linenr;
170 struct fragment *next;
174 * When dealing with a binary patch, we reuse "leading" field
175 * to store the type of the binary hunk, either deflated "delta"
176 * or deflated "literal".
178 #define binary_patch_method leading
179 #define BINARY_DELTA_DEFLATED 1
180 #define BINARY_LITERAL_DEFLATED 2
183 * This represents a "patch" to a file, both metainfo changes
184 * such as creation/deletion, filemode and content changes represented
185 * as a series of fragments.
187 struct patch {
188 char *new_name, *old_name, *def_name;
189 unsigned int old_mode, new_mode;
190 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
191 int rejected;
192 unsigned ws_rule;
193 int lines_added, lines_deleted;
194 int score;
195 unsigned int is_toplevel_relative:1;
196 unsigned int inaccurate_eof:1;
197 unsigned int is_binary:1;
198 unsigned int is_copy:1;
199 unsigned int is_rename:1;
200 unsigned int recount:1;
201 unsigned int conflicted_threeway:1;
202 unsigned int direct_to_threeway:1;
203 struct fragment *fragments;
204 char *result;
205 size_t resultsize;
206 char old_sha1_prefix[41];
207 char new_sha1_prefix[41];
208 struct patch *next;
210 /* three-way fallback result */
211 unsigned char threeway_stage[3][20];
214 static void free_fragment_list(struct fragment *list)
216 while (list) {
217 struct fragment *next = list->next;
218 if (list->free_patch)
219 free((char *)list->patch);
220 free(list);
221 list = next;
225 static void free_patch(struct patch *patch)
227 free_fragment_list(patch->fragments);
228 free(patch->def_name);
229 free(patch->old_name);
230 free(patch->new_name);
231 free(patch->result);
232 free(patch);
235 static void free_patch_list(struct patch *list)
237 while (list) {
238 struct patch *next = list->next;
239 free_patch(list);
240 list = next;
245 * A line in a file, len-bytes long (includes the terminating LF,
246 * except for an incomplete line at the end if the file ends with
247 * one), and its contents hashes to 'hash'.
249 struct line {
250 size_t len;
251 unsigned hash : 24;
252 unsigned flag : 8;
253 #define LINE_COMMON 1
254 #define LINE_PATCHED 2
258 * This represents a "file", which is an array of "lines".
260 struct image {
261 char *buf;
262 size_t len;
263 size_t nr;
264 size_t alloc;
265 struct line *line_allocated;
266 struct line *line;
270 * Records filenames that have been touched, in order to handle
271 * the case where more than one patches touch the same file.
274 static struct string_list fn_table;
276 static uint32_t hash_line(const char *cp, size_t len)
278 size_t i;
279 uint32_t h;
280 for (i = 0, h = 0; i < len; i++) {
281 if (!isspace(cp[i])) {
282 h = h * 3 + (cp[i] & 0xff);
285 return h;
289 * Compare lines s1 of length n1 and s2 of length n2, ignoring
290 * whitespace difference. Returns 1 if they match, 0 otherwise
292 static int fuzzy_matchlines(const char *s1, size_t n1,
293 const char *s2, size_t n2)
295 const char *last1 = s1 + n1 - 1;
296 const char *last2 = s2 + n2 - 1;
297 int result = 0;
299 /* ignore line endings */
300 while ((*last1 == '\r') || (*last1 == '\n'))
301 last1--;
302 while ((*last2 == '\r') || (*last2 == '\n'))
303 last2--;
305 /* skip leading whitespaces, if both begin with whitespace */
306 if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
307 while (isspace(*s1) && (s1 <= last1))
308 s1++;
309 while (isspace(*s2) && (s2 <= last2))
310 s2++;
312 /* early return if both lines are empty */
313 if ((s1 > last1) && (s2 > last2))
314 return 1;
315 while (!result) {
316 result = *s1++ - *s2++;
318 * Skip whitespace inside. We check for whitespace on
319 * both buffers because we don't want "a b" to match
320 * "ab"
322 if (isspace(*s1) && isspace(*s2)) {
323 while (isspace(*s1) && s1 <= last1)
324 s1++;
325 while (isspace(*s2) && s2 <= last2)
326 s2++;
329 * If we reached the end on one side only,
330 * lines don't match
332 if (
333 ((s2 > last2) && (s1 <= last1)) ||
334 ((s1 > last1) && (s2 <= last2)))
335 return 0;
336 if ((s1 > last1) && (s2 > last2))
337 break;
340 return !result;
343 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
345 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
346 img->line_allocated[img->nr].len = len;
347 img->line_allocated[img->nr].hash = hash_line(bol, len);
348 img->line_allocated[img->nr].flag = flag;
349 img->nr++;
353 * "buf" has the file contents to be patched (read from various sources).
354 * attach it to "image" and add line-based index to it.
355 * "image" now owns the "buf".
357 static void prepare_image(struct image *image, char *buf, size_t len,
358 int prepare_linetable)
360 const char *cp, *ep;
362 memset(image, 0, sizeof(*image));
363 image->buf = buf;
364 image->len = len;
366 if (!prepare_linetable)
367 return;
369 ep = image->buf + image->len;
370 cp = image->buf;
371 while (cp < ep) {
372 const char *next;
373 for (next = cp; next < ep && *next != '\n'; next++)
375 if (next < ep)
376 next++;
377 add_line_info(image, cp, next - cp, 0);
378 cp = next;
380 image->line = image->line_allocated;
383 static void clear_image(struct image *image)
385 free(image->buf);
386 free(image->line_allocated);
387 memset(image, 0, sizeof(*image));
390 /* fmt must contain _one_ %s and no other substitution */
391 static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
393 struct strbuf sb = STRBUF_INIT;
395 if (patch->old_name && patch->new_name &&
396 strcmp(patch->old_name, patch->new_name)) {
397 quote_c_style(patch->old_name, &sb, NULL, 0);
398 strbuf_addstr(&sb, " => ");
399 quote_c_style(patch->new_name, &sb, NULL, 0);
400 } else {
401 const char *n = patch->new_name;
402 if (!n)
403 n = patch->old_name;
404 quote_c_style(n, &sb, NULL, 0);
406 fprintf(output, fmt, sb.buf);
407 fputc('\n', output);
408 strbuf_release(&sb);
411 #define SLOP (16)
413 static void read_patch_file(struct strbuf *sb, int fd)
415 if (strbuf_read(sb, fd, 0) < 0)
416 die_errno("git apply: failed to read");
419 * Make sure that we have some slop in the buffer
420 * so that we can do speculative "memcmp" etc, and
421 * see to it that it is NUL-filled.
423 strbuf_grow(sb, SLOP);
424 memset(sb->buf + sb->len, 0, SLOP);
427 static unsigned long linelen(const char *buffer, unsigned long size)
429 unsigned long len = 0;
430 while (size--) {
431 len++;
432 if (*buffer++ == '\n')
433 break;
435 return len;
438 static int is_dev_null(const char *str)
440 return skip_prefix(str, "/dev/null", &str) && isspace(*str);
443 #define TERM_SPACE 1
444 #define TERM_TAB 2
446 static int name_terminate(const char *name, int namelen, int c, int terminate)
448 if (c == ' ' && !(terminate & TERM_SPACE))
449 return 0;
450 if (c == '\t' && !(terminate & TERM_TAB))
451 return 0;
453 return 1;
456 /* remove double slashes to make --index work with such filenames */
457 static char *squash_slash(char *name)
459 int i = 0, j = 0;
461 if (!name)
462 return NULL;
464 while (name[i]) {
465 if ((name[j++] = name[i++]) == '/')
466 while (name[i] == '/')
467 i++;
469 name[j] = '\0';
470 return name;
473 static char *find_name_gnu(const char *line, const char *def, int p_value)
475 struct strbuf name = STRBUF_INIT;
476 char *cp;
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);
484 return NULL;
487 for (cp = name.buf; p_value; p_value--) {
488 cp = strchr(cp, '/');
489 if (!cp) {
490 strbuf_release(&name);
491 return NULL;
493 cp++;
496 strbuf_remove(&name, 0, cp - name.buf);
497 if (root)
498 strbuf_insert(&name, 0, root, root_len);
499 return squash_slash(strbuf_detach(&name, NULL));
502 static size_t sane_tz_len(const char *line, size_t len)
504 const char *tz, *p;
506 if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
507 return 0;
508 tz = line + len - strlen(" +0500");
510 if (tz[1] != '+' && tz[1] != '-')
511 return 0;
513 for (p = tz + 2; p != line + len; p++)
514 if (!isdigit(*p))
515 return 0;
517 return line + len - tz;
520 static size_t tz_with_colon_len(const char *line, size_t len)
522 const char *tz, *p;
524 if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
525 return 0;
526 tz = line + len - strlen(" +08:00");
528 if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
529 return 0;
530 p = tz + 2;
531 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
532 !isdigit(*p++) || !isdigit(*p++))
533 return 0;
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")] != '-')
543 return 0;
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. */
549 return 0;
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")] != ':')
563 return 0;
564 p = time = line + len - strlen(" 07:01:32");
566 /* Permit 1-digit hours? */
567 if (*p++ != ' ' ||
568 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
569 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
570 !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
571 return 0;
573 return line + len - time;
576 static size_t fractional_time_len(const char *line, size_t len)
578 const char *p;
579 size_t n;
581 /* Expected format: 19:41:17.620000023 */
582 if (!len || !isdigit(line[len - 1]))
583 return 0;
584 p = line + len - 1;
586 /* Fractional seconds. */
587 while (p > line && isdigit(*p))
588 p--;
589 if (*p != '.')
590 return 0;
592 /* Hours, minutes, and whole seconds. */
593 n = short_time_len(line, p - line);
594 if (!n)
595 return 0;
597 return line + len - p + n;
600 static size_t trailing_spaces_len(const char *line, size_t len)
602 const char *p;
604 /* Expected format: ' ' x (1 or more) */
605 if (!len || line[len - 1] != ' ')
606 return 0;
608 p = line + len;
609 while (p != line) {
610 p--;
611 if (*p != ' ')
612 return line + len - (p + 1);
615 /* All spaces! */
616 return len;
619 static size_t diff_timestamp_len(const char *line, size_t len)
621 const char *end = line + len;
622 size_t n;
625 * Posix: 2010-07-05 19:41:17
626 * GNU: 2010-07-05 19:41:17.620000023 -0500
629 if (!isdigit(end[-1]))
630 return 0;
632 n = sane_tz_len(line, end - line);
633 if (!n)
634 n = tz_with_colon_len(line, end - line);
635 end -= n;
637 n = short_time_len(line, end - line);
638 if (!n)
639 n = fractional_time_len(line, end - line);
640 end -= n;
642 n = date_len(line, end - line);
643 if (!n) /* No date. Too bad. */
644 return 0;
645 end -= n;
647 if (end == line) /* No space before date. */
648 return 0;
649 if (end[-1] == '\t') { /* Success! */
650 end--;
651 return line + len - end;
653 if (end[-1] != ' ') /* No space before date. */
654 return 0;
656 /* Whitespace damage. */
657 end -= trailing_spaces_len(line, end - line);
658 return line + len - end;
661 static char *find_name_common(const char *line, const char *def,
662 int p_value, const char *end, int terminate)
664 int len;
665 const char *start = NULL;
667 if (p_value == 0)
668 start = line;
669 while (line != end) {
670 char c = *line;
672 if (!end && isspace(c)) {
673 if (c == '\n')
674 break;
675 if (name_terminate(start, line-start, c, terminate))
676 break;
678 line++;
679 if (c == '/' && !--p_value)
680 start = line;
682 if (!start)
683 return squash_slash(xstrdup_or_null(def));
684 len = line - start;
685 if (!len)
686 return squash_slash(xstrdup_or_null(def));
689 * Generally we prefer the shorter name, especially
690 * if the other one is just a variation of that with
691 * something else tacked on to the end (ie "file.orig"
692 * or "file~").
694 if (def) {
695 int deflen = strlen(def);
696 if (deflen < len && !strncmp(start, def, deflen))
697 return squash_slash(xstrdup(def));
700 if (root) {
701 char *ret = xmalloc(root_len + len + 1);
702 strcpy(ret, root);
703 memcpy(ret + root_len, start, len);
704 ret[root_len + len] = '\0';
705 return squash_slash(ret);
708 return squash_slash(xmemdupz(start, len));
711 static char *find_name(const char *line, char *def, int p_value, int terminate)
713 if (*line == '"') {
714 char *name = find_name_gnu(line, def, p_value);
715 if (name)
716 return name;
719 return find_name_common(line, def, p_value, NULL, terminate);
722 static char *find_name_traditional(const char *line, char *def, int p_value)
724 size_t len;
725 size_t date_len;
727 if (*line == '"') {
728 char *name = find_name_gnu(line, def, p_value);
729 if (name)
730 return name;
733 len = strchrnul(line, '\n') - line;
734 date_len = diff_timestamp_len(line, len);
735 if (!date_len)
736 return find_name_common(line, def, p_value, NULL, TERM_TAB);
737 len -= date_len;
739 return find_name_common(line, def, p_value, line + len, 0);
742 static int count_slashes(const char *cp)
744 int cnt = 0;
745 char ch;
747 while ((ch = *cp++))
748 if (ch == '/')
749 cnt++;
750 return cnt;
754 * Given the string after "--- " or "+++ ", guess the appropriate
755 * p_value for the given patch.
757 static int guess_p_value(const char *nameline)
759 char *name, *cp;
760 int val = -1;
762 if (is_dev_null(nameline))
763 return -1;
764 name = find_name_traditional(nameline, NULL, 0);
765 if (!name)
766 return -1;
767 cp = strchr(name, '/');
768 if (!cp)
769 val = 0;
770 else if (prefix) {
772 * Does it begin with "a/$our-prefix" and such? Then this is
773 * very likely to apply to our directory.
775 if (!strncmp(name, prefix, prefix_length))
776 val = count_slashes(prefix);
777 else {
778 cp++;
779 if (!strncmp(cp, prefix, prefix_length))
780 val = count_slashes(prefix) + 1;
783 free(name);
784 return val;
788 * Does the ---/+++ line has the POSIX timestamp after the last HT?
789 * GNU diff puts epoch there to signal a creation/deletion event. Is
790 * this such a timestamp?
792 static int has_epoch_timestamp(const char *nameline)
795 * We are only interested in epoch timestamp; any non-zero
796 * fraction cannot be one, hence "(\.0+)?" in the regexp below.
797 * For the same reason, the date must be either 1969-12-31 or
798 * 1970-01-01, and the seconds part must be "00".
800 const char stamp_regexp[] =
801 "^(1969-12-31|1970-01-01)"
803 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
805 "([-+][0-2][0-9]:?[0-5][0-9])\n";
806 const char *timestamp = NULL, *cp, *colon;
807 static regex_t *stamp;
808 regmatch_t m[10];
809 int zoneoffset;
810 int hourminute;
811 int status;
813 for (cp = nameline; *cp != '\n'; cp++) {
814 if (*cp == '\t')
815 timestamp = cp + 1;
817 if (!timestamp)
818 return 0;
819 if (!stamp) {
820 stamp = xmalloc(sizeof(*stamp));
821 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
822 warning(_("Cannot prepare timestamp regexp %s"),
823 stamp_regexp);
824 return 0;
828 status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
829 if (status) {
830 if (status != REG_NOMATCH)
831 warning(_("regexec returned %d for input: %s"),
832 status, timestamp);
833 return 0;
836 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
837 if (*colon == ':')
838 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
839 else
840 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
841 if (timestamp[m[3].rm_so] == '-')
842 zoneoffset = -zoneoffset;
845 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
846 * (west of GMT) or 1970-01-01 (east of GMT)
848 if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
849 (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
850 return 0;
852 hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
853 strtol(timestamp + 14, NULL, 10) -
854 zoneoffset);
856 return ((zoneoffset < 0 && hourminute == 1440) ||
857 (0 <= zoneoffset && !hourminute));
861 * Get the name etc info from the ---/+++ lines of a traditional patch header
863 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
864 * files, we can happily check the index for a match, but for creating a
865 * new file we should try to match whatever "patch" does. I have no idea.
867 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
869 char *name;
871 first += 4; /* skip "--- " */
872 second += 4; /* skip "+++ " */
873 if (!p_value_known) {
874 int p, q;
875 p = guess_p_value(first);
876 q = guess_p_value(second);
877 if (p < 0) p = q;
878 if (0 <= p && p == q) {
879 p_value = p;
880 p_value_known = 1;
883 if (is_dev_null(first)) {
884 patch->is_new = 1;
885 patch->is_delete = 0;
886 name = find_name_traditional(second, NULL, p_value);
887 patch->new_name = name;
888 } else if (is_dev_null(second)) {
889 patch->is_new = 0;
890 patch->is_delete = 1;
891 name = find_name_traditional(first, NULL, p_value);
892 patch->old_name = name;
893 } else {
894 char *first_name;
895 first_name = find_name_traditional(first, NULL, p_value);
896 name = find_name_traditional(second, first_name, p_value);
897 free(first_name);
898 if (has_epoch_timestamp(first)) {
899 patch->is_new = 1;
900 patch->is_delete = 0;
901 patch->new_name = name;
902 } else if (has_epoch_timestamp(second)) {
903 patch->is_new = 0;
904 patch->is_delete = 1;
905 patch->old_name = name;
906 } else {
907 patch->old_name = name;
908 patch->new_name = xstrdup_or_null(name);
911 if (!name)
912 die(_("unable to find filename in patch at line %d"), linenr);
915 static int gitdiff_hdrend(const char *line, struct patch *patch)
917 return -1;
921 * We're anal about diff header consistency, to make
922 * sure that we don't end up having strange ambiguous
923 * patches floating around.
925 * As a result, gitdiff_{old|new}name() will check
926 * their names against any previous information, just
927 * to make sure..
929 #define DIFF_OLD_NAME 0
930 #define DIFF_NEW_NAME 1
932 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, int side)
934 if (!orig_name && !isnull)
935 return find_name(line, NULL, p_value, TERM_TAB);
937 if (orig_name) {
938 int len;
939 const char *name;
940 char *another;
941 name = orig_name;
942 len = strlen(name);
943 if (isnull)
944 die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr);
945 another = find_name(line, NULL, p_value, TERM_TAB);
946 if (!another || memcmp(another, name, len + 1))
947 die((side == DIFF_NEW_NAME) ?
948 _("git apply: bad git-diff - inconsistent new filename on line %d") :
949 _("git apply: bad git-diff - inconsistent old filename on line %d"), linenr);
950 free(another);
951 return orig_name;
953 else {
954 /* expect "/dev/null" */
955 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
956 die(_("git apply: bad git-diff - expected /dev/null on line %d"), linenr);
957 return NULL;
961 static int gitdiff_oldname(const char *line, struct patch *patch)
963 char *orig = patch->old_name;
964 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name,
965 DIFF_OLD_NAME);
966 if (orig != patch->old_name)
967 free(orig);
968 return 0;
971 static int gitdiff_newname(const char *line, struct patch *patch)
973 char *orig = patch->new_name;
974 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name,
975 DIFF_NEW_NAME);
976 if (orig != patch->new_name)
977 free(orig);
978 return 0;
981 static int gitdiff_oldmode(const char *line, struct patch *patch)
983 patch->old_mode = strtoul(line, NULL, 8);
984 return 0;
987 static int gitdiff_newmode(const char *line, struct patch *patch)
989 patch->new_mode = strtoul(line, NULL, 8);
990 return 0;
993 static int gitdiff_delete(const char *line, struct patch *patch)
995 patch->is_delete = 1;
996 free(patch->old_name);
997 patch->old_name = xstrdup_or_null(patch->def_name);
998 return gitdiff_oldmode(line, patch);
1001 static int gitdiff_newfile(const char *line, struct patch *patch)
1003 patch->is_new = 1;
1004 free(patch->new_name);
1005 patch->new_name = xstrdup_or_null(patch->def_name);
1006 return gitdiff_newmode(line, patch);
1009 static int gitdiff_copysrc(const char *line, struct patch *patch)
1011 patch->is_copy = 1;
1012 free(patch->old_name);
1013 patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
1014 return 0;
1017 static int gitdiff_copydst(const char *line, struct patch *patch)
1019 patch->is_copy = 1;
1020 free(patch->new_name);
1021 patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
1022 return 0;
1025 static int gitdiff_renamesrc(const char *line, struct patch *patch)
1027 patch->is_rename = 1;
1028 free(patch->old_name);
1029 patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
1030 return 0;
1033 static int gitdiff_renamedst(const char *line, struct patch *patch)
1035 patch->is_rename = 1;
1036 free(patch->new_name);
1037 patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
1038 return 0;
1041 static int gitdiff_similarity(const char *line, struct patch *patch)
1043 unsigned long val = strtoul(line, NULL, 10);
1044 if (val <= 100)
1045 patch->score = val;
1046 return 0;
1049 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
1051 unsigned long val = strtoul(line, NULL, 10);
1052 if (val <= 100)
1053 patch->score = val;
1054 return 0;
1057 static int gitdiff_index(const char *line, struct patch *patch)
1060 * index line is N hexadecimal, "..", N hexadecimal,
1061 * and optional space with octal mode.
1063 const char *ptr, *eol;
1064 int len;
1066 ptr = strchr(line, '.');
1067 if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1068 return 0;
1069 len = ptr - line;
1070 memcpy(patch->old_sha1_prefix, line, len);
1071 patch->old_sha1_prefix[len] = 0;
1073 line = ptr + 2;
1074 ptr = strchr(line, ' ');
1075 eol = strchrnul(line, '\n');
1077 if (!ptr || eol < ptr)
1078 ptr = eol;
1079 len = ptr - line;
1081 if (40 < len)
1082 return 0;
1083 memcpy(patch->new_sha1_prefix, line, len);
1084 patch->new_sha1_prefix[len] = 0;
1085 if (*ptr == ' ')
1086 patch->old_mode = strtoul(ptr+1, NULL, 8);
1087 return 0;
1091 * This is normal for a diff that doesn't change anything: we'll fall through
1092 * into the next diff. Tell the parser to break out.
1094 static int gitdiff_unrecognized(const char *line, struct patch *patch)
1096 return -1;
1100 * Skip p_value leading components from "line"; as we do not accept
1101 * absolute paths, return NULL in that case.
1103 static const char *skip_tree_prefix(const char *line, int llen)
1105 int nslash;
1106 int i;
1108 if (!p_value)
1109 return (llen && line[0] == '/') ? NULL : line;
1111 nslash = p_value;
1112 for (i = 0; i < llen; i++) {
1113 int ch = line[i];
1114 if (ch == '/' && --nslash <= 0)
1115 return (i == 0) ? NULL : &line[i + 1];
1117 return NULL;
1121 * This is to extract the same name that appears on "diff --git"
1122 * line. We do not find and return anything if it is a rename
1123 * patch, and it is OK because we will find the name elsewhere.
1124 * We need to reliably find name only when it is mode-change only,
1125 * creation or deletion of an empty file. In any of these cases,
1126 * both sides are the same name under a/ and b/ respectively.
1128 static char *git_header_name(const char *line, int llen)
1130 const char *name;
1131 const char *second = NULL;
1132 size_t len, line_len;
1134 line += strlen("diff --git ");
1135 llen -= strlen("diff --git ");
1137 if (*line == '"') {
1138 const char *cp;
1139 struct strbuf first = STRBUF_INIT;
1140 struct strbuf sp = STRBUF_INIT;
1142 if (unquote_c_style(&first, line, &second))
1143 goto free_and_fail1;
1145 /* strip the a/b prefix including trailing slash */
1146 cp = skip_tree_prefix(first.buf, first.len);
1147 if (!cp)
1148 goto free_and_fail1;
1149 strbuf_remove(&first, 0, cp - first.buf);
1152 * second points at one past closing dq of name.
1153 * find the second name.
1155 while ((second < line + llen) && isspace(*second))
1156 second++;
1158 if (line + llen <= second)
1159 goto free_and_fail1;
1160 if (*second == '"') {
1161 if (unquote_c_style(&sp, second, NULL))
1162 goto free_and_fail1;
1163 cp = skip_tree_prefix(sp.buf, sp.len);
1164 if (!cp)
1165 goto free_and_fail1;
1166 /* They must match, otherwise ignore */
1167 if (strcmp(cp, first.buf))
1168 goto free_and_fail1;
1169 strbuf_release(&sp);
1170 return strbuf_detach(&first, NULL);
1173 /* unquoted second */
1174 cp = skip_tree_prefix(second, line + llen - second);
1175 if (!cp)
1176 goto free_and_fail1;
1177 if (line + llen - cp != first.len ||
1178 memcmp(first.buf, cp, first.len))
1179 goto free_and_fail1;
1180 return strbuf_detach(&first, NULL);
1182 free_and_fail1:
1183 strbuf_release(&first);
1184 strbuf_release(&sp);
1185 return NULL;
1188 /* unquoted first name */
1189 name = skip_tree_prefix(line, llen);
1190 if (!name)
1191 return NULL;
1194 * since the first name is unquoted, a dq if exists must be
1195 * the beginning of the second name.
1197 for (second = name; second < line + llen; second++) {
1198 if (*second == '"') {
1199 struct strbuf sp = STRBUF_INIT;
1200 const char *np;
1202 if (unquote_c_style(&sp, second, NULL))
1203 goto free_and_fail2;
1205 np = skip_tree_prefix(sp.buf, sp.len);
1206 if (!np)
1207 goto free_and_fail2;
1209 len = sp.buf + sp.len - np;
1210 if (len < second - name &&
1211 !strncmp(np, name, len) &&
1212 isspace(name[len])) {
1213 /* Good */
1214 strbuf_remove(&sp, 0, np - sp.buf);
1215 return strbuf_detach(&sp, NULL);
1218 free_and_fail2:
1219 strbuf_release(&sp);
1220 return NULL;
1225 * Accept a name only if it shows up twice, exactly the same
1226 * form.
1228 second = strchr(name, '\n');
1229 if (!second)
1230 return NULL;
1231 line_len = second - name;
1232 for (len = 0 ; ; len++) {
1233 switch (name[len]) {
1234 default:
1235 continue;
1236 case '\n':
1237 return NULL;
1238 case '\t': case ' ':
1240 * Is this the separator between the preimage
1241 * and the postimage pathname? Again, we are
1242 * only interested in the case where there is
1243 * no rename, as this is only to set def_name
1244 * and a rename patch has the names elsewhere
1245 * in an unambiguous form.
1247 if (!name[len + 1])
1248 return NULL; /* no postimage name */
1249 second = skip_tree_prefix(name + len + 1,
1250 line_len - (len + 1));
1251 if (!second)
1252 return NULL;
1254 * Does len bytes starting at "name" and "second"
1255 * (that are separated by one HT or SP we just
1256 * found) exactly match?
1258 if (second[len] == '\n' && !strncmp(name, second, len))
1259 return xmemdupz(name, len);
1264 /* Verify that we recognize the lines following a git header */
1265 static int parse_git_header(const char *line, int len, unsigned int size, struct patch *patch)
1267 unsigned long offset;
1269 /* A git diff has explicit new/delete information, so we don't guess */
1270 patch->is_new = 0;
1271 patch->is_delete = 0;
1274 * Some things may not have the old name in the
1275 * rest of the headers anywhere (pure mode changes,
1276 * or removing or adding empty files), so we get
1277 * the default name from the header.
1279 patch->def_name = git_header_name(line, len);
1280 if (patch->def_name && root) {
1281 char *s = xstrfmt("%s%s", root, patch->def_name);
1282 free(patch->def_name);
1283 patch->def_name = s;
1286 line += len;
1287 size -= len;
1288 linenr++;
1289 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
1290 static const struct opentry {
1291 const char *str;
1292 int (*fn)(const char *, struct patch *);
1293 } optable[] = {
1294 { "@@ -", gitdiff_hdrend },
1295 { "--- ", gitdiff_oldname },
1296 { "+++ ", gitdiff_newname },
1297 { "old mode ", gitdiff_oldmode },
1298 { "new mode ", gitdiff_newmode },
1299 { "deleted file mode ", gitdiff_delete },
1300 { "new file mode ", gitdiff_newfile },
1301 { "copy from ", gitdiff_copysrc },
1302 { "copy to ", gitdiff_copydst },
1303 { "rename old ", gitdiff_renamesrc },
1304 { "rename new ", gitdiff_renamedst },
1305 { "rename from ", gitdiff_renamesrc },
1306 { "rename to ", gitdiff_renamedst },
1307 { "similarity index ", gitdiff_similarity },
1308 { "dissimilarity index ", gitdiff_dissimilarity },
1309 { "index ", gitdiff_index },
1310 { "", gitdiff_unrecognized },
1312 int i;
1314 len = linelen(line, size);
1315 if (!len || line[len-1] != '\n')
1316 break;
1317 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1318 const struct opentry *p = optable + i;
1319 int oplen = strlen(p->str);
1320 if (len < oplen || memcmp(p->str, line, oplen))
1321 continue;
1322 if (p->fn(line + oplen, patch) < 0)
1323 return offset;
1324 break;
1328 return offset;
1331 static int parse_num(const char *line, unsigned long *p)
1333 char *ptr;
1335 if (!isdigit(*line))
1336 return 0;
1337 *p = strtoul(line, &ptr, 10);
1338 return ptr - line;
1341 static int parse_range(const char *line, int len, int offset, const char *expect,
1342 unsigned long *p1, unsigned long *p2)
1344 int digits, ex;
1346 if (offset < 0 || offset >= len)
1347 return -1;
1348 line += offset;
1349 len -= offset;
1351 digits = parse_num(line, p1);
1352 if (!digits)
1353 return -1;
1355 offset += digits;
1356 line += digits;
1357 len -= digits;
1359 *p2 = 1;
1360 if (*line == ',') {
1361 digits = parse_num(line+1, p2);
1362 if (!digits)
1363 return -1;
1365 offset += digits+1;
1366 line += digits+1;
1367 len -= digits+1;
1370 ex = strlen(expect);
1371 if (ex > len)
1372 return -1;
1373 if (memcmp(line, expect, ex))
1374 return -1;
1376 return offset + ex;
1379 static void recount_diff(const char *line, int size, struct fragment *fragment)
1381 int oldlines = 0, newlines = 0, ret = 0;
1383 if (size < 1) {
1384 warning("recount: ignore empty hunk");
1385 return;
1388 for (;;) {
1389 int len = linelen(line, size);
1390 size -= len;
1391 line += len;
1393 if (size < 1)
1394 break;
1396 switch (*line) {
1397 case ' ': case '\n':
1398 newlines++;
1399 /* fall through */
1400 case '-':
1401 oldlines++;
1402 continue;
1403 case '+':
1404 newlines++;
1405 continue;
1406 case '\\':
1407 continue;
1408 case '@':
1409 ret = size < 3 || !starts_with(line, "@@ ");
1410 break;
1411 case 'd':
1412 ret = size < 5 || !starts_with(line, "diff ");
1413 break;
1414 default:
1415 ret = -1;
1416 break;
1418 if (ret) {
1419 warning(_("recount: unexpected line: %.*s"),
1420 (int)linelen(line, size), line);
1421 return;
1423 break;
1425 fragment->oldlines = oldlines;
1426 fragment->newlines = newlines;
1430 * Parse a unified diff fragment header of the
1431 * form "@@ -a,b +c,d @@"
1433 static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1435 int offset;
1437 if (!len || line[len-1] != '\n')
1438 return -1;
1440 /* Figure out the number of lines in a fragment */
1441 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1442 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1444 return offset;
1447 static int find_header(const char *line, unsigned long size, int *hdrsize, struct patch *patch)
1449 unsigned long offset, len;
1451 patch->is_toplevel_relative = 0;
1452 patch->is_rename = patch->is_copy = 0;
1453 patch->is_new = patch->is_delete = -1;
1454 patch->old_mode = patch->new_mode = 0;
1455 patch->old_name = patch->new_name = NULL;
1456 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
1457 unsigned long nextlen;
1459 len = linelen(line, size);
1460 if (!len)
1461 break;
1463 /* Testing this early allows us to take a few shortcuts.. */
1464 if (len < 6)
1465 continue;
1468 * Make sure we don't find any unconnected patch fragments.
1469 * That's a sign that we didn't find a header, and that a
1470 * patch has become corrupted/broken up.
1472 if (!memcmp("@@ -", line, 4)) {
1473 struct fragment dummy;
1474 if (parse_fragment_header(line, len, &dummy) < 0)
1475 continue;
1476 die(_("patch fragment without header at line %d: %.*s"),
1477 linenr, (int)len-1, line);
1480 if (size < len + 6)
1481 break;
1484 * Git patch? It might not have a real patch, just a rename
1485 * or mode change, so we handle that specially
1487 if (!memcmp("diff --git ", line, 11)) {
1488 int git_hdr_len = parse_git_header(line, len, size, patch);
1489 if (git_hdr_len <= len)
1490 continue;
1491 if (!patch->old_name && !patch->new_name) {
1492 if (!patch->def_name)
1493 die(Q_("git diff header lacks filename information when removing "
1494 "%d leading pathname component (line %d)",
1495 "git diff header lacks filename information when removing "
1496 "%d leading pathname components (line %d)",
1497 p_value),
1498 p_value, linenr);
1499 patch->old_name = xstrdup(patch->def_name);
1500 patch->new_name = xstrdup(patch->def_name);
1502 if (!patch->is_delete && !patch->new_name)
1503 die("git diff header lacks filename information "
1504 "(line %d)", linenr);
1505 patch->is_toplevel_relative = 1;
1506 *hdrsize = git_hdr_len;
1507 return offset;
1510 /* --- followed by +++ ? */
1511 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1512 continue;
1515 * We only accept unified patches, so we want it to
1516 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1517 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1519 nextlen = linelen(line + len, size - len);
1520 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1521 continue;
1523 /* Ok, we'll consider it a patch */
1524 parse_traditional_patch(line, line+len, patch);
1525 *hdrsize = len + nextlen;
1526 linenr += 2;
1527 return offset;
1529 return -1;
1532 static void record_ws_error(unsigned result, const char *line, int len, int linenr)
1534 char *err;
1536 if (!result)
1537 return;
1539 whitespace_error++;
1540 if (squelch_whitespace_errors &&
1541 squelch_whitespace_errors < whitespace_error)
1542 return;
1544 err = whitespace_error_string(result);
1545 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1546 patch_input_file, linenr, err, len, line);
1547 free(err);
1550 static void check_whitespace(const char *line, int len, unsigned ws_rule)
1552 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1554 record_ws_error(result, line + 1, len - 2, linenr);
1558 * Parse a unified diff. Note that this really needs to parse each
1559 * fragment separately, since the only way to know the difference
1560 * between a "---" that is part of a patch, and a "---" that starts
1561 * the next patch is to look at the line counts..
1563 static int parse_fragment(const char *line, unsigned long size,
1564 struct patch *patch, struct fragment *fragment)
1566 int added, deleted;
1567 int len = linelen(line, size), offset;
1568 unsigned long oldlines, newlines;
1569 unsigned long leading, trailing;
1571 offset = parse_fragment_header(line, len, fragment);
1572 if (offset < 0)
1573 return -1;
1574 if (offset > 0 && patch->recount)
1575 recount_diff(line + offset, size - offset, fragment);
1576 oldlines = fragment->oldlines;
1577 newlines = fragment->newlines;
1578 leading = 0;
1579 trailing = 0;
1581 /* Parse the thing.. */
1582 line += len;
1583 size -= len;
1584 linenr++;
1585 added = deleted = 0;
1586 for (offset = len;
1587 0 < size;
1588 offset += len, size -= len, line += len, linenr++) {
1589 if (!oldlines && !newlines)
1590 break;
1591 len = linelen(line, size);
1592 if (!len || line[len-1] != '\n')
1593 return -1;
1594 switch (*line) {
1595 default:
1596 return -1;
1597 case '\n': /* newer GNU diff, an empty context line */
1598 case ' ':
1599 oldlines--;
1600 newlines--;
1601 if (!deleted && !added)
1602 leading++;
1603 trailing++;
1604 if (!apply_in_reverse &&
1605 ws_error_action == correct_ws_error)
1606 check_whitespace(line, len, patch->ws_rule);
1607 break;
1608 case '-':
1609 if (apply_in_reverse &&
1610 ws_error_action != nowarn_ws_error)
1611 check_whitespace(line, len, patch->ws_rule);
1612 deleted++;
1613 oldlines--;
1614 trailing = 0;
1615 break;
1616 case '+':
1617 if (!apply_in_reverse &&
1618 ws_error_action != nowarn_ws_error)
1619 check_whitespace(line, len, patch->ws_rule);
1620 added++;
1621 newlines--;
1622 trailing = 0;
1623 break;
1626 * We allow "\ No newline at end of file". Depending
1627 * on locale settings when the patch was produced we
1628 * don't know what this line looks like. The only
1629 * thing we do know is that it begins with "\ ".
1630 * Checking for 12 is just for sanity check -- any
1631 * l10n of "\ No newline..." is at least that long.
1633 case '\\':
1634 if (len < 12 || memcmp(line, "\\ ", 2))
1635 return -1;
1636 break;
1639 if (oldlines || newlines)
1640 return -1;
1641 fragment->leading = leading;
1642 fragment->trailing = trailing;
1645 * If a fragment ends with an incomplete line, we failed to include
1646 * it in the above loop because we hit oldlines == newlines == 0
1647 * before seeing it.
1649 if (12 < size && !memcmp(line, "\\ ", 2))
1650 offset += linelen(line, size);
1652 patch->lines_added += added;
1653 patch->lines_deleted += deleted;
1655 if (0 < patch->is_new && oldlines)
1656 return error(_("new file depends on old contents"));
1657 if (0 < patch->is_delete && newlines)
1658 return error(_("deleted file still has contents"));
1659 return offset;
1663 * We have seen "diff --git a/... b/..." header (or a traditional patch
1664 * header). Read hunks that belong to this patch into fragments and hang
1665 * them to the given patch structure.
1667 * The (fragment->patch, fragment->size) pair points into the memory given
1668 * by the caller, not a copy, when we return.
1670 static int parse_single_patch(const char *line, unsigned long size, struct patch *patch)
1672 unsigned long offset = 0;
1673 unsigned long oldlines = 0, newlines = 0, context = 0;
1674 struct fragment **fragp = &patch->fragments;
1676 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1677 struct fragment *fragment;
1678 int len;
1680 fragment = xcalloc(1, sizeof(*fragment));
1681 fragment->linenr = linenr;
1682 len = parse_fragment(line, size, patch, fragment);
1683 if (len <= 0)
1684 die(_("corrupt patch at line %d"), linenr);
1685 fragment->patch = line;
1686 fragment->size = len;
1687 oldlines += fragment->oldlines;
1688 newlines += fragment->newlines;
1689 context += fragment->leading + fragment->trailing;
1691 *fragp = fragment;
1692 fragp = &fragment->next;
1694 offset += len;
1695 line += len;
1696 size -= len;
1700 * If something was removed (i.e. we have old-lines) it cannot
1701 * be creation, and if something was added it cannot be
1702 * deletion. However, the reverse is not true; --unified=0
1703 * patches that only add are not necessarily creation even
1704 * though they do not have any old lines, and ones that only
1705 * delete are not necessarily deletion.
1707 * Unfortunately, a real creation/deletion patch do _not_ have
1708 * any context line by definition, so we cannot safely tell it
1709 * apart with --unified=0 insanity. At least if the patch has
1710 * more than one hunk it is not creation or deletion.
1712 if (patch->is_new < 0 &&
1713 (oldlines || (patch->fragments && patch->fragments->next)))
1714 patch->is_new = 0;
1715 if (patch->is_delete < 0 &&
1716 (newlines || (patch->fragments && patch->fragments->next)))
1717 patch->is_delete = 0;
1719 if (0 < patch->is_new && oldlines)
1720 die(_("new file %s depends on old contents"), patch->new_name);
1721 if (0 < patch->is_delete && newlines)
1722 die(_("deleted file %s still has contents"), patch->old_name);
1723 if (!patch->is_delete && !newlines && context)
1724 fprintf_ln(stderr,
1725 _("** warning: "
1726 "file %s becomes empty but is not deleted"),
1727 patch->new_name);
1729 return offset;
1732 static inline int metadata_changes(struct patch *patch)
1734 return patch->is_rename > 0 ||
1735 patch->is_copy > 0 ||
1736 patch->is_new > 0 ||
1737 patch->is_delete ||
1738 (patch->old_mode && patch->new_mode &&
1739 patch->old_mode != patch->new_mode);
1742 static char *inflate_it(const void *data, unsigned long size,
1743 unsigned long inflated_size)
1745 git_zstream stream;
1746 void *out;
1747 int st;
1749 memset(&stream, 0, sizeof(stream));
1751 stream.next_in = (unsigned char *)data;
1752 stream.avail_in = size;
1753 stream.next_out = out = xmalloc(inflated_size);
1754 stream.avail_out = inflated_size;
1755 git_inflate_init(&stream);
1756 st = git_inflate(&stream, Z_FINISH);
1757 git_inflate_end(&stream);
1758 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1759 free(out);
1760 return NULL;
1762 return out;
1766 * Read a binary hunk and return a new fragment; fragment->patch
1767 * points at an allocated memory that the caller must free, so
1768 * it is marked as "->free_patch = 1".
1770 static struct fragment *parse_binary_hunk(char **buf_p,
1771 unsigned long *sz_p,
1772 int *status_p,
1773 int *used_p)
1776 * Expect a line that begins with binary patch method ("literal"
1777 * or "delta"), followed by the length of data before deflating.
1778 * a sequence of 'length-byte' followed by base-85 encoded data
1779 * should follow, terminated by a newline.
1781 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1782 * and we would limit the patch line to 66 characters,
1783 * so one line can fit up to 13 groups that would decode
1784 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1785 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1787 int llen, used;
1788 unsigned long size = *sz_p;
1789 char *buffer = *buf_p;
1790 int patch_method;
1791 unsigned long origlen;
1792 char *data = NULL;
1793 int hunk_size = 0;
1794 struct fragment *frag;
1796 llen = linelen(buffer, size);
1797 used = llen;
1799 *status_p = 0;
1801 if (starts_with(buffer, "delta ")) {
1802 patch_method = BINARY_DELTA_DEFLATED;
1803 origlen = strtoul(buffer + 6, NULL, 10);
1805 else if (starts_with(buffer, "literal ")) {
1806 patch_method = BINARY_LITERAL_DEFLATED;
1807 origlen = strtoul(buffer + 8, NULL, 10);
1809 else
1810 return NULL;
1812 linenr++;
1813 buffer += llen;
1814 while (1) {
1815 int byte_length, max_byte_length, newsize;
1816 llen = linelen(buffer, size);
1817 used += llen;
1818 linenr++;
1819 if (llen == 1) {
1820 /* consume the blank line */
1821 buffer++;
1822 size--;
1823 break;
1826 * Minimum line is "A00000\n" which is 7-byte long,
1827 * and the line length must be multiple of 5 plus 2.
1829 if ((llen < 7) || (llen-2) % 5)
1830 goto corrupt;
1831 max_byte_length = (llen - 2) / 5 * 4;
1832 byte_length = *buffer;
1833 if ('A' <= byte_length && byte_length <= 'Z')
1834 byte_length = byte_length - 'A' + 1;
1835 else if ('a' <= byte_length && byte_length <= 'z')
1836 byte_length = byte_length - 'a' + 27;
1837 else
1838 goto corrupt;
1839 /* if the input length was not multiple of 4, we would
1840 * have filler at the end but the filler should never
1841 * exceed 3 bytes
1843 if (max_byte_length < byte_length ||
1844 byte_length <= max_byte_length - 4)
1845 goto corrupt;
1846 newsize = hunk_size + byte_length;
1847 data = xrealloc(data, newsize);
1848 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1849 goto corrupt;
1850 hunk_size = newsize;
1851 buffer += llen;
1852 size -= llen;
1855 frag = xcalloc(1, sizeof(*frag));
1856 frag->patch = inflate_it(data, hunk_size, origlen);
1857 frag->free_patch = 1;
1858 if (!frag->patch)
1859 goto corrupt;
1860 free(data);
1861 frag->size = origlen;
1862 *buf_p = buffer;
1863 *sz_p = size;
1864 *used_p = used;
1865 frag->binary_patch_method = patch_method;
1866 return frag;
1868 corrupt:
1869 free(data);
1870 *status_p = -1;
1871 error(_("corrupt binary patch at line %d: %.*s"),
1872 linenr-1, llen-1, buffer);
1873 return NULL;
1876 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1879 * We have read "GIT binary patch\n"; what follows is a line
1880 * that says the patch method (currently, either "literal" or
1881 * "delta") and the length of data before deflating; a
1882 * sequence of 'length-byte' followed by base-85 encoded data
1883 * follows.
1885 * When a binary patch is reversible, there is another binary
1886 * hunk in the same format, starting with patch method (either
1887 * "literal" or "delta") with the length of data, and a sequence
1888 * of length-byte + base-85 encoded data, terminated with another
1889 * empty line. This data, when applied to the postimage, produces
1890 * the preimage.
1892 struct fragment *forward;
1893 struct fragment *reverse;
1894 int status;
1895 int used, used_1;
1897 forward = parse_binary_hunk(&buffer, &size, &status, &used);
1898 if (!forward && !status)
1899 /* there has to be one hunk (forward hunk) */
1900 return error(_("unrecognized binary patch at line %d"), linenr-1);
1901 if (status)
1902 /* otherwise we already gave an error message */
1903 return status;
1905 reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1906 if (reverse)
1907 used += used_1;
1908 else if (status) {
1910 * Not having reverse hunk is not an error, but having
1911 * a corrupt reverse hunk is.
1913 free((void*) forward->patch);
1914 free(forward);
1915 return status;
1917 forward->next = reverse;
1918 patch->fragments = forward;
1919 patch->is_binary = 1;
1920 return used;
1923 static void prefix_one(char **name)
1925 char *old_name = *name;
1926 if (!old_name)
1927 return;
1928 *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
1929 free(old_name);
1932 static void prefix_patch(struct patch *p)
1934 if (!prefix || p->is_toplevel_relative)
1935 return;
1936 prefix_one(&p->new_name);
1937 prefix_one(&p->old_name);
1941 * include/exclude
1944 static struct string_list limit_by_name;
1945 static int has_include;
1946 static void add_name_limit(const char *name, int exclude)
1948 struct string_list_item *it;
1950 it = string_list_append(&limit_by_name, name);
1951 it->util = exclude ? NULL : (void *) 1;
1954 static int use_patch(struct patch *p)
1956 const char *pathname = p->new_name ? p->new_name : p->old_name;
1957 int i;
1959 /* Paths outside are not touched regardless of "--include" */
1960 if (0 < prefix_length) {
1961 int pathlen = strlen(pathname);
1962 if (pathlen <= prefix_length ||
1963 memcmp(prefix, pathname, prefix_length))
1964 return 0;
1967 /* See if it matches any of exclude/include rule */
1968 for (i = 0; i < limit_by_name.nr; i++) {
1969 struct string_list_item *it = &limit_by_name.items[i];
1970 if (!wildmatch(it->string, pathname, 0, NULL))
1971 return (it->util != NULL);
1975 * If we had any include, a path that does not match any rule is
1976 * not used. Otherwise, we saw bunch of exclude rules (or none)
1977 * and such a path is used.
1979 return !has_include;
1984 * Read the patch text in "buffer" that extends for "size" bytes; stop
1985 * reading after seeing a single patch (i.e. changes to a single file).
1986 * Create fragments (i.e. patch hunks) and hang them to the given patch.
1987 * Return the number of bytes consumed, so that the caller can call us
1988 * again for the next patch.
1990 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1992 int hdrsize, patchsize;
1993 int offset = find_header(buffer, size, &hdrsize, patch);
1995 if (offset < 0)
1996 return offset;
1998 prefix_patch(patch);
2000 if (!use_patch(patch))
2001 patch->ws_rule = 0;
2002 else
2003 patch->ws_rule = whitespace_rule(patch->new_name
2004 ? patch->new_name
2005 : patch->old_name);
2007 patchsize = parse_single_patch(buffer + offset + hdrsize,
2008 size - offset - hdrsize, patch);
2010 if (!patchsize) {
2011 static const char git_binary[] = "GIT binary patch\n";
2012 int hd = hdrsize + offset;
2013 unsigned long llen = linelen(buffer + hd, size - hd);
2015 if (llen == sizeof(git_binary) - 1 &&
2016 !memcmp(git_binary, buffer + hd, llen)) {
2017 int used;
2018 linenr++;
2019 used = parse_binary(buffer + hd + llen,
2020 size - hd - llen, patch);
2021 if (used)
2022 patchsize = used + llen;
2023 else
2024 patchsize = 0;
2026 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2027 static const char *binhdr[] = {
2028 "Binary files ",
2029 "Files ",
2030 NULL,
2032 int i;
2033 for (i = 0; binhdr[i]; i++) {
2034 int len = strlen(binhdr[i]);
2035 if (len < size - hd &&
2036 !memcmp(binhdr[i], buffer + hd, len)) {
2037 linenr++;
2038 patch->is_binary = 1;
2039 patchsize = llen;
2040 break;
2045 /* Empty patch cannot be applied if it is a text patch
2046 * without metadata change. A binary patch appears
2047 * empty to us here.
2049 if ((apply || check) &&
2050 (!patch->is_binary && !metadata_changes(patch)))
2051 die(_("patch with only garbage at line %d"), linenr);
2054 return offset + hdrsize + patchsize;
2057 #define swap(a,b) myswap((a),(b),sizeof(a))
2059 #define myswap(a, b, size) do { \
2060 unsigned char mytmp[size]; \
2061 memcpy(mytmp, &a, size); \
2062 memcpy(&a, &b, size); \
2063 memcpy(&b, mytmp, size); \
2064 } while (0)
2066 static void reverse_patches(struct patch *p)
2068 for (; p; p = p->next) {
2069 struct fragment *frag = p->fragments;
2071 swap(p->new_name, p->old_name);
2072 swap(p->new_mode, p->old_mode);
2073 swap(p->is_new, p->is_delete);
2074 swap(p->lines_added, p->lines_deleted);
2075 swap(p->old_sha1_prefix, p->new_sha1_prefix);
2077 for (; frag; frag = frag->next) {
2078 swap(frag->newpos, frag->oldpos);
2079 swap(frag->newlines, frag->oldlines);
2084 static const char pluses[] =
2085 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2086 static const char minuses[]=
2087 "----------------------------------------------------------------------";
2089 static void show_stats(struct patch *patch)
2091 struct strbuf qname = STRBUF_INIT;
2092 char *cp = patch->new_name ? patch->new_name : patch->old_name;
2093 int max, add, del;
2095 quote_c_style(cp, &qname, NULL, 0);
2098 * "scale" the filename
2100 max = max_len;
2101 if (max > 50)
2102 max = 50;
2104 if (qname.len > max) {
2105 cp = strchr(qname.buf + qname.len + 3 - max, '/');
2106 if (!cp)
2107 cp = qname.buf + qname.len + 3 - max;
2108 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2111 if (patch->is_binary) {
2112 printf(" %-*s | Bin\n", max, qname.buf);
2113 strbuf_release(&qname);
2114 return;
2117 printf(" %-*s |", max, qname.buf);
2118 strbuf_release(&qname);
2121 * scale the add/delete
2123 max = max + max_change > 70 ? 70 - max : max_change;
2124 add = patch->lines_added;
2125 del = patch->lines_deleted;
2127 if (max_change > 0) {
2128 int total = ((add + del) * max + max_change / 2) / max_change;
2129 add = (add * max + max_change / 2) / max_change;
2130 del = total - add;
2132 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2133 add, pluses, del, minuses);
2136 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2138 switch (st->st_mode & S_IFMT) {
2139 case S_IFLNK:
2140 if (strbuf_readlink(buf, path, st->st_size) < 0)
2141 return error(_("unable to read symlink %s"), path);
2142 return 0;
2143 case S_IFREG:
2144 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2145 return error(_("unable to open or read %s"), path);
2146 convert_to_git(path, buf->buf, buf->len, buf, 0);
2147 return 0;
2148 default:
2149 return -1;
2154 * Update the preimage, and the common lines in postimage,
2155 * from buffer buf of length len. If postlen is 0 the postimage
2156 * is updated in place, otherwise it's updated on a new buffer
2157 * of length postlen
2160 static void update_pre_post_images(struct image *preimage,
2161 struct image *postimage,
2162 char *buf,
2163 size_t len, size_t postlen)
2165 int i, ctx, reduced;
2166 char *new, *old, *fixed;
2167 struct image fixed_preimage;
2170 * Update the preimage with whitespace fixes. Note that we
2171 * are not losing preimage->buf -- apply_one_fragment() will
2172 * free "oldlines".
2174 prepare_image(&fixed_preimage, buf, len, 1);
2175 assert(postlen
2176 ? fixed_preimage.nr == preimage->nr
2177 : fixed_preimage.nr <= preimage->nr);
2178 for (i = 0; i < fixed_preimage.nr; i++)
2179 fixed_preimage.line[i].flag = preimage->line[i].flag;
2180 free(preimage->line_allocated);
2181 *preimage = fixed_preimage;
2184 * Adjust the common context lines in postimage. This can be
2185 * done in-place when we are shrinking it with whitespace
2186 * fixing, but needs a new buffer when ignoring whitespace or
2187 * expanding leading tabs to spaces.
2189 * We trust the caller to tell us if the update can be done
2190 * in place (postlen==0) or not.
2192 old = postimage->buf;
2193 if (postlen)
2194 new = postimage->buf = xmalloc(postlen);
2195 else
2196 new = old;
2197 fixed = preimage->buf;
2199 for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2200 size_t len = postimage->line[i].len;
2201 if (!(postimage->line[i].flag & LINE_COMMON)) {
2202 /* an added line -- no counterparts in preimage */
2203 memmove(new, old, len);
2204 old += len;
2205 new += len;
2206 continue;
2209 /* a common context -- skip it in the original postimage */
2210 old += len;
2212 /* and find the corresponding one in the fixed preimage */
2213 while (ctx < preimage->nr &&
2214 !(preimage->line[ctx].flag & LINE_COMMON)) {
2215 fixed += preimage->line[ctx].len;
2216 ctx++;
2220 * preimage is expected to run out, if the caller
2221 * fixed addition of trailing blank lines.
2223 if (preimage->nr <= ctx) {
2224 reduced++;
2225 continue;
2228 /* and copy it in, while fixing the line length */
2229 len = preimage->line[ctx].len;
2230 memcpy(new, fixed, len);
2231 new += len;
2232 fixed += len;
2233 postimage->line[i].len = len;
2234 ctx++;
2237 if (postlen
2238 ? postlen < new - postimage->buf
2239 : postimage->len < new - postimage->buf)
2240 die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2241 (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2243 /* Fix the length of the whole thing */
2244 postimage->len = new - postimage->buf;
2245 postimage->nr -= reduced;
2248 static int match_fragment(struct image *img,
2249 struct image *preimage,
2250 struct image *postimage,
2251 unsigned long try,
2252 int try_lno,
2253 unsigned ws_rule,
2254 int match_beginning, int match_end)
2256 int i;
2257 char *fixed_buf, *buf, *orig, *target;
2258 struct strbuf fixed;
2259 size_t fixed_len, postlen;
2260 int preimage_limit;
2262 if (preimage->nr + try_lno <= img->nr) {
2264 * The hunk falls within the boundaries of img.
2266 preimage_limit = preimage->nr;
2267 if (match_end && (preimage->nr + try_lno != img->nr))
2268 return 0;
2269 } else if (ws_error_action == correct_ws_error &&
2270 (ws_rule & WS_BLANK_AT_EOF)) {
2272 * This hunk extends beyond the end of img, and we are
2273 * removing blank lines at the end of the file. This
2274 * many lines from the beginning of the preimage must
2275 * match with img, and the remainder of the preimage
2276 * must be blank.
2278 preimage_limit = img->nr - try_lno;
2279 } else {
2281 * The hunk extends beyond the end of the img and
2282 * we are not removing blanks at the end, so we
2283 * should reject the hunk at this position.
2285 return 0;
2288 if (match_beginning && try_lno)
2289 return 0;
2291 /* Quick hash check */
2292 for (i = 0; i < preimage_limit; i++)
2293 if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2294 (preimage->line[i].hash != img->line[try_lno + i].hash))
2295 return 0;
2297 if (preimage_limit == preimage->nr) {
2299 * Do we have an exact match? If we were told to match
2300 * at the end, size must be exactly at try+fragsize,
2301 * otherwise try+fragsize must be still within the preimage,
2302 * and either case, the old piece should match the preimage
2303 * exactly.
2305 if ((match_end
2306 ? (try + preimage->len == img->len)
2307 : (try + preimage->len <= img->len)) &&
2308 !memcmp(img->buf + try, preimage->buf, preimage->len))
2309 return 1;
2310 } else {
2312 * The preimage extends beyond the end of img, so
2313 * there cannot be an exact match.
2315 * There must be one non-blank context line that match
2316 * a line before the end of img.
2318 char *buf_end;
2320 buf = preimage->buf;
2321 buf_end = buf;
2322 for (i = 0; i < preimage_limit; i++)
2323 buf_end += preimage->line[i].len;
2325 for ( ; buf < buf_end; buf++)
2326 if (!isspace(*buf))
2327 break;
2328 if (buf == buf_end)
2329 return 0;
2333 * No exact match. If we are ignoring whitespace, run a line-by-line
2334 * fuzzy matching. We collect all the line length information because
2335 * we need it to adjust whitespace if we match.
2337 if (ws_ignore_action == ignore_ws_change) {
2338 size_t imgoff = 0;
2339 size_t preoff = 0;
2340 size_t postlen = postimage->len;
2341 size_t extra_chars;
2342 char *preimage_eof;
2343 char *preimage_end;
2344 for (i = 0; i < preimage_limit; i++) {
2345 size_t prelen = preimage->line[i].len;
2346 size_t imglen = img->line[try_lno+i].len;
2348 if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2349 preimage->buf + preoff, prelen))
2350 return 0;
2351 if (preimage->line[i].flag & LINE_COMMON)
2352 postlen += imglen - prelen;
2353 imgoff += imglen;
2354 preoff += prelen;
2358 * Ok, the preimage matches with whitespace fuzz.
2360 * imgoff now holds the true length of the target that
2361 * matches the preimage before the end of the file.
2363 * Count the number of characters in the preimage that fall
2364 * beyond the end of the file and make sure that all of them
2365 * are whitespace characters. (This can only happen if
2366 * we are removing blank lines at the end of the file.)
2368 buf = preimage_eof = preimage->buf + preoff;
2369 for ( ; i < preimage->nr; i++)
2370 preoff += preimage->line[i].len;
2371 preimage_end = preimage->buf + preoff;
2372 for ( ; buf < preimage_end; buf++)
2373 if (!isspace(*buf))
2374 return 0;
2377 * Update the preimage and the common postimage context
2378 * lines to use the same whitespace as the target.
2379 * If whitespace is missing in the target (i.e.
2380 * if the preimage extends beyond the end of the file),
2381 * use the whitespace from the preimage.
2383 extra_chars = preimage_end - preimage_eof;
2384 strbuf_init(&fixed, imgoff + extra_chars);
2385 strbuf_add(&fixed, img->buf + try, imgoff);
2386 strbuf_add(&fixed, preimage_eof, extra_chars);
2387 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2388 update_pre_post_images(preimage, postimage,
2389 fixed_buf, fixed_len, postlen);
2390 return 1;
2393 if (ws_error_action != correct_ws_error)
2394 return 0;
2397 * The hunk does not apply byte-by-byte, but the hash says
2398 * it might with whitespace fuzz. We weren't asked to
2399 * ignore whitespace, we were asked to correct whitespace
2400 * errors, so let's try matching after whitespace correction.
2402 * While checking the preimage against the target, whitespace
2403 * errors in both fixed, we count how large the corresponding
2404 * postimage needs to be. The postimage prepared by
2405 * apply_one_fragment() has whitespace errors fixed on added
2406 * lines already, but the common lines were propagated as-is,
2407 * which may become longer when their whitespace errors are
2408 * fixed.
2411 /* First count added lines in postimage */
2412 postlen = 0;
2413 for (i = 0; i < postimage->nr; i++) {
2414 if (!(postimage->line[i].flag & LINE_COMMON))
2415 postlen += postimage->line[i].len;
2419 * The preimage may extend beyond the end of the file,
2420 * but in this loop we will only handle the part of the
2421 * preimage that falls within the file.
2423 strbuf_init(&fixed, preimage->len + 1);
2424 orig = preimage->buf;
2425 target = img->buf + try;
2426 for (i = 0; i < preimage_limit; i++) {
2427 size_t oldlen = preimage->line[i].len;
2428 size_t tgtlen = img->line[try_lno + i].len;
2429 size_t fixstart = fixed.len;
2430 struct strbuf tgtfix;
2431 int match;
2433 /* Try fixing the line in the preimage */
2434 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2436 /* Try fixing the line in the target */
2437 strbuf_init(&tgtfix, tgtlen);
2438 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2441 * If they match, either the preimage was based on
2442 * a version before our tree fixed whitespace breakage,
2443 * or we are lacking a whitespace-fix patch the tree
2444 * the preimage was based on already had (i.e. target
2445 * has whitespace breakage, the preimage doesn't).
2446 * In either case, we are fixing the whitespace breakages
2447 * so we might as well take the fix together with their
2448 * real change.
2450 match = (tgtfix.len == fixed.len - fixstart &&
2451 !memcmp(tgtfix.buf, fixed.buf + fixstart,
2452 fixed.len - fixstart));
2454 /* Add the length if this is common with the postimage */
2455 if (preimage->line[i].flag & LINE_COMMON)
2456 postlen += tgtfix.len;
2458 strbuf_release(&tgtfix);
2459 if (!match)
2460 goto unmatch_exit;
2462 orig += oldlen;
2463 target += tgtlen;
2468 * Now handle the lines in the preimage that falls beyond the
2469 * end of the file (if any). They will only match if they are
2470 * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2471 * false).
2473 for ( ; i < preimage->nr; i++) {
2474 size_t fixstart = fixed.len; /* start of the fixed preimage */
2475 size_t oldlen = preimage->line[i].len;
2476 int j;
2478 /* Try fixing the line in the preimage */
2479 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2481 for (j = fixstart; j < fixed.len; j++)
2482 if (!isspace(fixed.buf[j]))
2483 goto unmatch_exit;
2485 orig += oldlen;
2489 * Yes, the preimage is based on an older version that still
2490 * has whitespace breakages unfixed, and fixing them makes the
2491 * hunk match. Update the context lines in the postimage.
2493 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2494 if (postlen < postimage->len)
2495 postlen = 0;
2496 update_pre_post_images(preimage, postimage,
2497 fixed_buf, fixed_len, postlen);
2498 return 1;
2500 unmatch_exit:
2501 strbuf_release(&fixed);
2502 return 0;
2505 static int find_pos(struct image *img,
2506 struct image *preimage,
2507 struct image *postimage,
2508 int line,
2509 unsigned ws_rule,
2510 int match_beginning, int match_end)
2512 int i;
2513 unsigned long backwards, forwards, try;
2514 int backwards_lno, forwards_lno, try_lno;
2517 * If match_beginning or match_end is specified, there is no
2518 * point starting from a wrong line that will never match and
2519 * wander around and wait for a match at the specified end.
2521 if (match_beginning)
2522 line = 0;
2523 else if (match_end)
2524 line = img->nr - preimage->nr;
2527 * Because the comparison is unsigned, the following test
2528 * will also take care of a negative line number that can
2529 * result when match_end and preimage is larger than the target.
2531 if ((size_t) line > img->nr)
2532 line = img->nr;
2534 try = 0;
2535 for (i = 0; i < line; i++)
2536 try += img->line[i].len;
2539 * There's probably some smart way to do this, but I'll leave
2540 * that to the smart and beautiful people. I'm simple and stupid.
2542 backwards = try;
2543 backwards_lno = line;
2544 forwards = try;
2545 forwards_lno = line;
2546 try_lno = line;
2548 for (i = 0; ; i++) {
2549 if (match_fragment(img, preimage, postimage,
2550 try, try_lno, ws_rule,
2551 match_beginning, match_end))
2552 return try_lno;
2554 again:
2555 if (backwards_lno == 0 && forwards_lno == img->nr)
2556 break;
2558 if (i & 1) {
2559 if (backwards_lno == 0) {
2560 i++;
2561 goto again;
2563 backwards_lno--;
2564 backwards -= img->line[backwards_lno].len;
2565 try = backwards;
2566 try_lno = backwards_lno;
2567 } else {
2568 if (forwards_lno == img->nr) {
2569 i++;
2570 goto again;
2572 forwards += img->line[forwards_lno].len;
2573 forwards_lno++;
2574 try = forwards;
2575 try_lno = forwards_lno;
2579 return -1;
2582 static void remove_first_line(struct image *img)
2584 img->buf += img->line[0].len;
2585 img->len -= img->line[0].len;
2586 img->line++;
2587 img->nr--;
2590 static void remove_last_line(struct image *img)
2592 img->len -= img->line[--img->nr].len;
2596 * The change from "preimage" and "postimage" has been found to
2597 * apply at applied_pos (counts in line numbers) in "img".
2598 * Update "img" to remove "preimage" and replace it with "postimage".
2600 static void update_image(struct image *img,
2601 int applied_pos,
2602 struct image *preimage,
2603 struct image *postimage)
2606 * remove the copy of preimage at offset in img
2607 * and replace it with postimage
2609 int i, nr;
2610 size_t remove_count, insert_count, applied_at = 0;
2611 char *result;
2612 int preimage_limit;
2615 * If we are removing blank lines at the end of img,
2616 * the preimage may extend beyond the end.
2617 * If that is the case, we must be careful only to
2618 * remove the part of the preimage that falls within
2619 * the boundaries of img. Initialize preimage_limit
2620 * to the number of lines in the preimage that falls
2621 * within the boundaries.
2623 preimage_limit = preimage->nr;
2624 if (preimage_limit > img->nr - applied_pos)
2625 preimage_limit = img->nr - applied_pos;
2627 for (i = 0; i < applied_pos; i++)
2628 applied_at += img->line[i].len;
2630 remove_count = 0;
2631 for (i = 0; i < preimage_limit; i++)
2632 remove_count += img->line[applied_pos + i].len;
2633 insert_count = postimage->len;
2635 /* Adjust the contents */
2636 result = xmalloc(img->len + insert_count - remove_count + 1);
2637 memcpy(result, img->buf, applied_at);
2638 memcpy(result + applied_at, postimage->buf, postimage->len);
2639 memcpy(result + applied_at + postimage->len,
2640 img->buf + (applied_at + remove_count),
2641 img->len - (applied_at + remove_count));
2642 free(img->buf);
2643 img->buf = result;
2644 img->len += insert_count - remove_count;
2645 result[img->len] = '\0';
2647 /* Adjust the line table */
2648 nr = img->nr + postimage->nr - preimage_limit;
2649 if (preimage_limit < postimage->nr) {
2651 * NOTE: this knows that we never call remove_first_line()
2652 * on anything other than pre/post image.
2654 REALLOC_ARRAY(img->line, nr);
2655 img->line_allocated = img->line;
2657 if (preimage_limit != postimage->nr)
2658 memmove(img->line + applied_pos + postimage->nr,
2659 img->line + applied_pos + preimage_limit,
2660 (img->nr - (applied_pos + preimage_limit)) *
2661 sizeof(*img->line));
2662 memcpy(img->line + applied_pos,
2663 postimage->line,
2664 postimage->nr * sizeof(*img->line));
2665 if (!allow_overlap)
2666 for (i = 0; i < postimage->nr; i++)
2667 img->line[applied_pos + i].flag |= LINE_PATCHED;
2668 img->nr = nr;
2672 * Use the patch-hunk text in "frag" to prepare two images (preimage and
2673 * postimage) for the hunk. Find lines that match "preimage" in "img" and
2674 * replace the part of "img" with "postimage" text.
2676 static int apply_one_fragment(struct image *img, struct fragment *frag,
2677 int inaccurate_eof, unsigned ws_rule,
2678 int nth_fragment)
2680 int match_beginning, match_end;
2681 const char *patch = frag->patch;
2682 int size = frag->size;
2683 char *old, *oldlines;
2684 struct strbuf newlines;
2685 int new_blank_lines_at_end = 0;
2686 int found_new_blank_lines_at_end = 0;
2687 int hunk_linenr = frag->linenr;
2688 unsigned long leading, trailing;
2689 int pos, applied_pos;
2690 struct image preimage;
2691 struct image postimage;
2693 memset(&preimage, 0, sizeof(preimage));
2694 memset(&postimage, 0, sizeof(postimage));
2695 oldlines = xmalloc(size);
2696 strbuf_init(&newlines, size);
2698 old = oldlines;
2699 while (size > 0) {
2700 char first;
2701 int len = linelen(patch, size);
2702 int plen;
2703 int added_blank_line = 0;
2704 int is_blank_context = 0;
2705 size_t start;
2707 if (!len)
2708 break;
2711 * "plen" is how much of the line we should use for
2712 * the actual patch data. Normally we just remove the
2713 * first character on the line, but if the line is
2714 * followed by "\ No newline", then we also remove the
2715 * last one (which is the newline, of course).
2717 plen = len - 1;
2718 if (len < size && patch[len] == '\\')
2719 plen--;
2720 first = *patch;
2721 if (apply_in_reverse) {
2722 if (first == '-')
2723 first = '+';
2724 else if (first == '+')
2725 first = '-';
2728 switch (first) {
2729 case '\n':
2730 /* Newer GNU diff, empty context line */
2731 if (plen < 0)
2732 /* ... followed by '\No newline'; nothing */
2733 break;
2734 *old++ = '\n';
2735 strbuf_addch(&newlines, '\n');
2736 add_line_info(&preimage, "\n", 1, LINE_COMMON);
2737 add_line_info(&postimage, "\n", 1, LINE_COMMON);
2738 is_blank_context = 1;
2739 break;
2740 case ' ':
2741 if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2742 ws_blank_line(patch + 1, plen, ws_rule))
2743 is_blank_context = 1;
2744 case '-':
2745 memcpy(old, patch + 1, plen);
2746 add_line_info(&preimage, old, plen,
2747 (first == ' ' ? LINE_COMMON : 0));
2748 old += plen;
2749 if (first == '-')
2750 break;
2751 /* Fall-through for ' ' */
2752 case '+':
2753 /* --no-add does not add new lines */
2754 if (first == '+' && no_add)
2755 break;
2757 start = newlines.len;
2758 if (first != '+' ||
2759 !whitespace_error ||
2760 ws_error_action != correct_ws_error) {
2761 strbuf_add(&newlines, patch + 1, plen);
2763 else {
2764 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &applied_after_fixing_ws);
2766 add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2767 (first == '+' ? 0 : LINE_COMMON));
2768 if (first == '+' &&
2769 (ws_rule & WS_BLANK_AT_EOF) &&
2770 ws_blank_line(patch + 1, plen, ws_rule))
2771 added_blank_line = 1;
2772 break;
2773 case '@': case '\\':
2774 /* Ignore it, we already handled it */
2775 break;
2776 default:
2777 if (apply_verbosely)
2778 error(_("invalid start of line: '%c'"), first);
2779 return -1;
2781 if (added_blank_line) {
2782 if (!new_blank_lines_at_end)
2783 found_new_blank_lines_at_end = hunk_linenr;
2784 new_blank_lines_at_end++;
2786 else if (is_blank_context)
2788 else
2789 new_blank_lines_at_end = 0;
2790 patch += len;
2791 size -= len;
2792 hunk_linenr++;
2794 if (inaccurate_eof &&
2795 old > oldlines && old[-1] == '\n' &&
2796 newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2797 old--;
2798 strbuf_setlen(&newlines, newlines.len - 1);
2801 leading = frag->leading;
2802 trailing = frag->trailing;
2805 * A hunk to change lines at the beginning would begin with
2806 * @@ -1,L +N,M @@
2807 * but we need to be careful. -U0 that inserts before the second
2808 * line also has this pattern.
2810 * And a hunk to add to an empty file would begin with
2811 * @@ -0,0 +N,M @@
2813 * In other words, a hunk that is (frag->oldpos <= 1) with or
2814 * without leading context must match at the beginning.
2816 match_beginning = (!frag->oldpos ||
2817 (frag->oldpos == 1 && !unidiff_zero));
2820 * A hunk without trailing lines must match at the end.
2821 * However, we simply cannot tell if a hunk must match end
2822 * from the lack of trailing lines if the patch was generated
2823 * with unidiff without any context.
2825 match_end = !unidiff_zero && !trailing;
2827 pos = frag->newpos ? (frag->newpos - 1) : 0;
2828 preimage.buf = oldlines;
2829 preimage.len = old - oldlines;
2830 postimage.buf = newlines.buf;
2831 postimage.len = newlines.len;
2832 preimage.line = preimage.line_allocated;
2833 postimage.line = postimage.line_allocated;
2835 for (;;) {
2837 applied_pos = find_pos(img, &preimage, &postimage, pos,
2838 ws_rule, match_beginning, match_end);
2840 if (applied_pos >= 0)
2841 break;
2843 /* Am I at my context limits? */
2844 if ((leading <= p_context) && (trailing <= p_context))
2845 break;
2846 if (match_beginning || match_end) {
2847 match_beginning = match_end = 0;
2848 continue;
2852 * Reduce the number of context lines; reduce both
2853 * leading and trailing if they are equal otherwise
2854 * just reduce the larger context.
2856 if (leading >= trailing) {
2857 remove_first_line(&preimage);
2858 remove_first_line(&postimage);
2859 pos--;
2860 leading--;
2862 if (trailing > leading) {
2863 remove_last_line(&preimage);
2864 remove_last_line(&postimage);
2865 trailing--;
2869 if (applied_pos >= 0) {
2870 if (new_blank_lines_at_end &&
2871 preimage.nr + applied_pos >= img->nr &&
2872 (ws_rule & WS_BLANK_AT_EOF) &&
2873 ws_error_action != nowarn_ws_error) {
2874 record_ws_error(WS_BLANK_AT_EOF, "+", 1,
2875 found_new_blank_lines_at_end);
2876 if (ws_error_action == correct_ws_error) {
2877 while (new_blank_lines_at_end--)
2878 remove_last_line(&postimage);
2881 * We would want to prevent write_out_results()
2882 * from taking place in apply_patch() that follows
2883 * the callchain led us here, which is:
2884 * apply_patch->check_patch_list->check_patch->
2885 * apply_data->apply_fragments->apply_one_fragment
2887 if (ws_error_action == die_on_ws_error)
2888 apply = 0;
2891 if (apply_verbosely && applied_pos != pos) {
2892 int offset = applied_pos - pos;
2893 if (apply_in_reverse)
2894 offset = 0 - offset;
2895 fprintf_ln(stderr,
2896 Q_("Hunk #%d succeeded at %d (offset %d line).",
2897 "Hunk #%d succeeded at %d (offset %d lines).",
2898 offset),
2899 nth_fragment, applied_pos + 1, offset);
2903 * Warn if it was necessary to reduce the number
2904 * of context lines.
2906 if ((leading != frag->leading) ||
2907 (trailing != frag->trailing))
2908 fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
2909 " to apply fragment at %d"),
2910 leading, trailing, applied_pos+1);
2911 update_image(img, applied_pos, &preimage, &postimage);
2912 } else {
2913 if (apply_verbosely)
2914 error(_("while searching for:\n%.*s"),
2915 (int)(old - oldlines), oldlines);
2918 free(oldlines);
2919 strbuf_release(&newlines);
2920 free(preimage.line_allocated);
2921 free(postimage.line_allocated);
2923 return (applied_pos < 0);
2926 static int apply_binary_fragment(struct image *img, struct patch *patch)
2928 struct fragment *fragment = patch->fragments;
2929 unsigned long len;
2930 void *dst;
2932 if (!fragment)
2933 return error(_("missing binary patch data for '%s'"),
2934 patch->new_name ?
2935 patch->new_name :
2936 patch->old_name);
2938 /* Binary patch is irreversible without the optional second hunk */
2939 if (apply_in_reverse) {
2940 if (!fragment->next)
2941 return error("cannot reverse-apply a binary patch "
2942 "without the reverse hunk to '%s'",
2943 patch->new_name
2944 ? patch->new_name : patch->old_name);
2945 fragment = fragment->next;
2947 switch (fragment->binary_patch_method) {
2948 case BINARY_DELTA_DEFLATED:
2949 dst = patch_delta(img->buf, img->len, fragment->patch,
2950 fragment->size, &len);
2951 if (!dst)
2952 return -1;
2953 clear_image(img);
2954 img->buf = dst;
2955 img->len = len;
2956 return 0;
2957 case BINARY_LITERAL_DEFLATED:
2958 clear_image(img);
2959 img->len = fragment->size;
2960 img->buf = xmemdupz(fragment->patch, img->len);
2961 return 0;
2963 return -1;
2967 * Replace "img" with the result of applying the binary patch.
2968 * The binary patch data itself in patch->fragment is still kept
2969 * but the preimage prepared by the caller in "img" is freed here
2970 * or in the helper function apply_binary_fragment() this calls.
2972 static int apply_binary(struct image *img, struct patch *patch)
2974 const char *name = patch->old_name ? patch->old_name : patch->new_name;
2975 unsigned char sha1[20];
2978 * For safety, we require patch index line to contain
2979 * full 40-byte textual SHA1 for old and new, at least for now.
2981 if (strlen(patch->old_sha1_prefix) != 40 ||
2982 strlen(patch->new_sha1_prefix) != 40 ||
2983 get_sha1_hex(patch->old_sha1_prefix, sha1) ||
2984 get_sha1_hex(patch->new_sha1_prefix, sha1))
2985 return error("cannot apply binary patch to '%s' "
2986 "without full index line", name);
2988 if (patch->old_name) {
2990 * See if the old one matches what the patch
2991 * applies to.
2993 hash_sha1_file(img->buf, img->len, blob_type, sha1);
2994 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
2995 return error("the patch applies to '%s' (%s), "
2996 "which does not match the "
2997 "current contents.",
2998 name, sha1_to_hex(sha1));
3000 else {
3001 /* Otherwise, the old one must be empty. */
3002 if (img->len)
3003 return error("the patch applies to an empty "
3004 "'%s' but it is not empty", name);
3007 get_sha1_hex(patch->new_sha1_prefix, sha1);
3008 if (is_null_sha1(sha1)) {
3009 clear_image(img);
3010 return 0; /* deletion patch */
3013 if (has_sha1_file(sha1)) {
3014 /* We already have the postimage */
3015 enum object_type type;
3016 unsigned long size;
3017 char *result;
3019 result = read_sha1_file(sha1, &type, &size);
3020 if (!result)
3021 return error("the necessary postimage %s for "
3022 "'%s' cannot be read",
3023 patch->new_sha1_prefix, name);
3024 clear_image(img);
3025 img->buf = result;
3026 img->len = size;
3027 } else {
3029 * We have verified buf matches the preimage;
3030 * apply the patch data to it, which is stored
3031 * in the patch->fragments->{patch,size}.
3033 if (apply_binary_fragment(img, patch))
3034 return error(_("binary patch does not apply to '%s'"),
3035 name);
3037 /* verify that the result matches */
3038 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3039 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3040 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3041 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3044 return 0;
3047 static int apply_fragments(struct image *img, struct patch *patch)
3049 struct fragment *frag = patch->fragments;
3050 const char *name = patch->old_name ? patch->old_name : patch->new_name;
3051 unsigned ws_rule = patch->ws_rule;
3052 unsigned inaccurate_eof = patch->inaccurate_eof;
3053 int nth = 0;
3055 if (patch->is_binary)
3056 return apply_binary(img, patch);
3058 while (frag) {
3059 nth++;
3060 if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule, nth)) {
3061 error(_("patch failed: %s:%ld"), name, frag->oldpos);
3062 if (!apply_with_reject)
3063 return -1;
3064 frag->rejected = 1;
3066 frag = frag->next;
3068 return 0;
3071 static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3073 if (S_ISGITLINK(mode)) {
3074 strbuf_grow(buf, 100);
3075 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3076 } else {
3077 enum object_type type;
3078 unsigned long sz;
3079 char *result;
3081 result = read_sha1_file(sha1, &type, &sz);
3082 if (!result)
3083 return -1;
3084 /* XXX read_sha1_file NUL-terminates */
3085 strbuf_attach(buf, result, sz, sz + 1);
3087 return 0;
3090 static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3092 if (!ce)
3093 return 0;
3094 return read_blob_object(buf, ce->sha1, ce->ce_mode);
3097 static struct patch *in_fn_table(const char *name)
3099 struct string_list_item *item;
3101 if (name == NULL)
3102 return NULL;
3104 item = string_list_lookup(&fn_table, name);
3105 if (item != NULL)
3106 return (struct patch *)item->util;
3108 return NULL;
3112 * item->util in the filename table records the status of the path.
3113 * Usually it points at a patch (whose result records the contents
3114 * of it after applying it), but it could be PATH_WAS_DELETED for a
3115 * path that a previously applied patch has already removed, or
3116 * PATH_TO_BE_DELETED for a path that a later patch would remove.
3118 * The latter is needed to deal with a case where two paths A and B
3119 * are swapped by first renaming A to B and then renaming B to A;
3120 * moving A to B should not be prevented due to presence of B as we
3121 * will remove it in a later patch.
3123 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3124 #define PATH_WAS_DELETED ((struct patch *) -1)
3126 static int to_be_deleted(struct patch *patch)
3128 return patch == PATH_TO_BE_DELETED;
3131 static int was_deleted(struct patch *patch)
3133 return patch == PATH_WAS_DELETED;
3136 static void add_to_fn_table(struct patch *patch)
3138 struct string_list_item *item;
3141 * Always add new_name unless patch is a deletion
3142 * This should cover the cases for normal diffs,
3143 * file creations and copies
3145 if (patch->new_name != NULL) {
3146 item = string_list_insert(&fn_table, patch->new_name);
3147 item->util = patch;
3151 * store a failure on rename/deletion cases because
3152 * later chunks shouldn't patch old names
3154 if ((patch->new_name == NULL) || (patch->is_rename)) {
3155 item = string_list_insert(&fn_table, patch->old_name);
3156 item->util = PATH_WAS_DELETED;
3160 static void prepare_fn_table(struct patch *patch)
3163 * store information about incoming file deletion
3165 while (patch) {
3166 if ((patch->new_name == NULL) || (patch->is_rename)) {
3167 struct string_list_item *item;
3168 item = string_list_insert(&fn_table, patch->old_name);
3169 item->util = PATH_TO_BE_DELETED;
3171 patch = patch->next;
3175 static int checkout_target(struct index_state *istate,
3176 struct cache_entry *ce, struct stat *st)
3178 struct checkout costate;
3180 memset(&costate, 0, sizeof(costate));
3181 costate.base_dir = "";
3182 costate.refresh_cache = 1;
3183 costate.istate = istate;
3184 if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3185 return error(_("cannot checkout %s"), ce->name);
3186 return 0;
3189 static struct patch *previous_patch(struct patch *patch, int *gone)
3191 struct patch *previous;
3193 *gone = 0;
3194 if (patch->is_copy || patch->is_rename)
3195 return NULL; /* "git" patches do not depend on the order */
3197 previous = in_fn_table(patch->old_name);
3198 if (!previous)
3199 return NULL;
3201 if (to_be_deleted(previous))
3202 return NULL; /* the deletion hasn't happened yet */
3204 if (was_deleted(previous))
3205 *gone = 1;
3207 return previous;
3210 static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3212 if (S_ISGITLINK(ce->ce_mode)) {
3213 if (!S_ISDIR(st->st_mode))
3214 return -1;
3215 return 0;
3217 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3220 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3222 static int load_patch_target(struct strbuf *buf,
3223 const struct cache_entry *ce,
3224 struct stat *st,
3225 const char *name,
3226 unsigned expected_mode)
3228 if (cached || check_index) {
3229 if (read_file_or_gitlink(ce, buf))
3230 return error(_("read of %s failed"), name);
3231 } else if (name) {
3232 if (S_ISGITLINK(expected_mode)) {
3233 if (ce)
3234 return read_file_or_gitlink(ce, buf);
3235 else
3236 return SUBMODULE_PATCH_WITHOUT_INDEX;
3237 } else if (has_symlink_leading_path(name, strlen(name))) {
3238 return error(_("reading from '%s' beyond a symbolic link"), name);
3239 } else {
3240 if (read_old_data(st, name, buf))
3241 return error(_("read of %s failed"), name);
3244 return 0;
3248 * We are about to apply "patch"; populate the "image" with the
3249 * current version we have, from the working tree or from the index,
3250 * depending on the situation e.g. --cached/--index. If we are
3251 * applying a non-git patch that incrementally updates the tree,
3252 * we read from the result of a previous diff.
3254 static int load_preimage(struct image *image,
3255 struct patch *patch, struct stat *st,
3256 const struct cache_entry *ce)
3258 struct strbuf buf = STRBUF_INIT;
3259 size_t len;
3260 char *img;
3261 struct patch *previous;
3262 int status;
3264 previous = previous_patch(patch, &status);
3265 if (status)
3266 return error(_("path %s has been renamed/deleted"),
3267 patch->old_name);
3268 if (previous) {
3269 /* We have a patched copy in memory; use that. */
3270 strbuf_add(&buf, previous->result, previous->resultsize);
3271 } else {
3272 status = load_patch_target(&buf, ce, st,
3273 patch->old_name, patch->old_mode);
3274 if (status < 0)
3275 return status;
3276 else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3278 * There is no way to apply subproject
3279 * patch without looking at the index.
3280 * NEEDSWORK: shouldn't this be flagged
3281 * as an error???
3283 free_fragment_list(patch->fragments);
3284 patch->fragments = NULL;
3285 } else if (status) {
3286 return error(_("read of %s failed"), patch->old_name);
3290 img = strbuf_detach(&buf, &len);
3291 prepare_image(image, img, len, !patch->is_binary);
3292 return 0;
3295 static int three_way_merge(struct image *image,
3296 char *path,
3297 const unsigned char *base,
3298 const unsigned char *ours,
3299 const unsigned char *theirs)
3301 mmfile_t base_file, our_file, their_file;
3302 mmbuffer_t result = { NULL };
3303 int status;
3305 read_mmblob(&base_file, base);
3306 read_mmblob(&our_file, ours);
3307 read_mmblob(&their_file, theirs);
3308 status = ll_merge(&result, path,
3309 &base_file, "base",
3310 &our_file, "ours",
3311 &their_file, "theirs", NULL);
3312 free(base_file.ptr);
3313 free(our_file.ptr);
3314 free(their_file.ptr);
3315 if (status < 0 || !result.ptr) {
3316 free(result.ptr);
3317 return -1;
3319 clear_image(image);
3320 image->buf = result.ptr;
3321 image->len = result.size;
3323 return status;
3327 * When directly falling back to add/add three-way merge, we read from
3328 * the current contents of the new_name. In no cases other than that
3329 * this function will be called.
3331 static int load_current(struct image *image, struct patch *patch)
3333 struct strbuf buf = STRBUF_INIT;
3334 int status, pos;
3335 size_t len;
3336 char *img;
3337 struct stat st;
3338 struct cache_entry *ce;
3339 char *name = patch->new_name;
3340 unsigned mode = patch->new_mode;
3342 if (!patch->is_new)
3343 die("BUG: patch to %s is not a creation", patch->old_name);
3345 pos = cache_name_pos(name, strlen(name));
3346 if (pos < 0)
3347 return error(_("%s: does not exist in index"), name);
3348 ce = active_cache[pos];
3349 if (lstat(name, &st)) {
3350 if (errno != ENOENT)
3351 return error(_("%s: %s"), name, strerror(errno));
3352 if (checkout_target(&the_index, ce, &st))
3353 return -1;
3355 if (verify_index_match(ce, &st))
3356 return error(_("%s: does not match index"), name);
3358 status = load_patch_target(&buf, ce, &st, name, mode);
3359 if (status < 0)
3360 return status;
3361 else if (status)
3362 return -1;
3363 img = strbuf_detach(&buf, &len);
3364 prepare_image(image, img, len, !patch->is_binary);
3365 return 0;
3368 static int try_threeway(struct image *image, struct patch *patch,
3369 struct stat *st, const struct cache_entry *ce)
3371 unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3372 struct strbuf buf = STRBUF_INIT;
3373 size_t len;
3374 int status;
3375 char *img;
3376 struct image tmp_image;
3378 /* No point falling back to 3-way merge in these cases */
3379 if (patch->is_delete ||
3380 S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3381 return -1;
3383 /* Preimage the patch was prepared for */
3384 if (patch->is_new)
3385 write_sha1_file("", 0, blob_type, pre_sha1);
3386 else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3387 read_blob_object(&buf, pre_sha1, patch->old_mode))
3388 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3390 fprintf(stderr, "Falling back to three-way merge...\n");
3392 img = strbuf_detach(&buf, &len);
3393 prepare_image(&tmp_image, img, len, 1);
3394 /* Apply the patch to get the post image */
3395 if (apply_fragments(&tmp_image, patch) < 0) {
3396 clear_image(&tmp_image);
3397 return -1;
3399 /* post_sha1[] is theirs */
3400 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3401 clear_image(&tmp_image);
3403 /* our_sha1[] is ours */
3404 if (patch->is_new) {
3405 if (load_current(&tmp_image, patch))
3406 return error("cannot read the current contents of '%s'",
3407 patch->new_name);
3408 } else {
3409 if (load_preimage(&tmp_image, patch, st, ce))
3410 return error("cannot read the current contents of '%s'",
3411 patch->old_name);
3413 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3414 clear_image(&tmp_image);
3416 /* in-core three-way merge between post and our using pre as base */
3417 status = three_way_merge(image, patch->new_name,
3418 pre_sha1, our_sha1, post_sha1);
3419 if (status < 0) {
3420 fprintf(stderr, "Failed to fall back on three-way merge...\n");
3421 return status;
3424 if (status) {
3425 patch->conflicted_threeway = 1;
3426 if (patch->is_new)
3427 hashclr(patch->threeway_stage[0]);
3428 else
3429 hashcpy(patch->threeway_stage[0], pre_sha1);
3430 hashcpy(patch->threeway_stage[1], our_sha1);
3431 hashcpy(patch->threeway_stage[2], post_sha1);
3432 fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3433 } else {
3434 fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3436 return 0;
3439 static int apply_data(struct patch *patch, struct stat *st, const struct cache_entry *ce)
3441 struct image image;
3443 if (load_preimage(&image, patch, st, ce) < 0)
3444 return -1;
3446 if (patch->direct_to_threeway ||
3447 apply_fragments(&image, patch) < 0) {
3448 /* Note: with --reject, apply_fragments() returns 0 */
3449 if (!threeway || try_threeway(&image, patch, st, ce) < 0)
3450 return -1;
3452 patch->result = image.buf;
3453 patch->resultsize = image.len;
3454 add_to_fn_table(patch);
3455 free(image.line_allocated);
3457 if (0 < patch->is_delete && patch->resultsize)
3458 return error(_("removal patch leaves file contents"));
3460 return 0;
3464 * If "patch" that we are looking at modifies or deletes what we have,
3465 * we would want it not to lose any local modification we have, either
3466 * in the working tree or in the index.
3468 * This also decides if a non-git patch is a creation patch or a
3469 * modification to an existing empty file. We do not check the state
3470 * of the current tree for a creation patch in this function; the caller
3471 * check_patch() separately makes sure (and errors out otherwise) that
3472 * the path the patch creates does not exist in the current tree.
3474 static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
3476 const char *old_name = patch->old_name;
3477 struct patch *previous = NULL;
3478 int stat_ret = 0, status;
3479 unsigned st_mode = 0;
3481 if (!old_name)
3482 return 0;
3484 assert(patch->is_new <= 0);
3485 previous = previous_patch(patch, &status);
3487 if (status)
3488 return error(_("path %s has been renamed/deleted"), old_name);
3489 if (previous) {
3490 st_mode = previous->new_mode;
3491 } else if (!cached) {
3492 stat_ret = lstat(old_name, st);
3493 if (stat_ret && errno != ENOENT)
3494 return error(_("%s: %s"), old_name, strerror(errno));
3497 if (check_index && !previous) {
3498 int pos = cache_name_pos(old_name, strlen(old_name));
3499 if (pos < 0) {
3500 if (patch->is_new < 0)
3501 goto is_new;
3502 return error(_("%s: does not exist in index"), old_name);
3504 *ce = active_cache[pos];
3505 if (stat_ret < 0) {
3506 if (checkout_target(&the_index, *ce, st))
3507 return -1;
3509 if (!cached && verify_index_match(*ce, st))
3510 return error(_("%s: does not match index"), old_name);
3511 if (cached)
3512 st_mode = (*ce)->ce_mode;
3513 } else if (stat_ret < 0) {
3514 if (patch->is_new < 0)
3515 goto is_new;
3516 return error(_("%s: %s"), old_name, strerror(errno));
3519 if (!cached && !previous)
3520 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3522 if (patch->is_new < 0)
3523 patch->is_new = 0;
3524 if (!patch->old_mode)
3525 patch->old_mode = st_mode;
3526 if ((st_mode ^ patch->old_mode) & S_IFMT)
3527 return error(_("%s: wrong type"), old_name);
3528 if (st_mode != patch->old_mode)
3529 warning(_("%s has type %o, expected %o"),
3530 old_name, st_mode, patch->old_mode);
3531 if (!patch->new_mode && !patch->is_delete)
3532 patch->new_mode = st_mode;
3533 return 0;
3535 is_new:
3536 patch->is_new = 1;
3537 patch->is_delete = 0;
3538 free(patch->old_name);
3539 patch->old_name = NULL;
3540 return 0;
3544 #define EXISTS_IN_INDEX 1
3545 #define EXISTS_IN_WORKTREE 2
3547 static int check_to_create(const char *new_name, int ok_if_exists)
3549 struct stat nst;
3551 if (check_index &&
3552 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3553 !ok_if_exists)
3554 return EXISTS_IN_INDEX;
3555 if (cached)
3556 return 0;
3558 if (!lstat(new_name, &nst)) {
3559 if (S_ISDIR(nst.st_mode) || ok_if_exists)
3560 return 0;
3562 * A leading component of new_name might be a symlink
3563 * that is going to be removed with this patch, but
3564 * still pointing at somewhere that has the path.
3565 * In such a case, path "new_name" does not exist as
3566 * far as git is concerned.
3568 if (has_symlink_leading_path(new_name, strlen(new_name)))
3569 return 0;
3571 return EXISTS_IN_WORKTREE;
3572 } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3573 return error("%s: %s", new_name, strerror(errno));
3575 return 0;
3579 * We need to keep track of how symlinks in the preimage are
3580 * manipulated by the patches. A patch to add a/b/c where a/b
3581 * is a symlink should not be allowed to affect the directory
3582 * the symlink points at, but if the same patch removes a/b,
3583 * it is perfectly fine, as the patch removes a/b to make room
3584 * to create a directory a/b so that a/b/c can be created.
3586 static struct string_list symlink_changes;
3587 #define SYMLINK_GOES_AWAY 01
3588 #define SYMLINK_IN_RESULT 02
3590 static uintptr_t register_symlink_changes(const char *path, uintptr_t what)
3592 struct string_list_item *ent;
3594 ent = string_list_lookup(&symlink_changes, path);
3595 if (!ent) {
3596 ent = string_list_insert(&symlink_changes, path);
3597 ent->util = (void *)0;
3599 ent->util = (void *)(what | ((uintptr_t)ent->util));
3600 return (uintptr_t)ent->util;
3603 static uintptr_t check_symlink_changes(const char *path)
3605 struct string_list_item *ent;
3607 ent = string_list_lookup(&symlink_changes, path);
3608 if (!ent)
3609 return 0;
3610 return (uintptr_t)ent->util;
3613 static void prepare_symlink_changes(struct patch *patch)
3615 for ( ; patch; patch = patch->next) {
3616 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3617 (patch->is_rename || patch->is_delete))
3618 /* the symlink at patch->old_name is removed */
3619 register_symlink_changes(patch->old_name, SYMLINK_GOES_AWAY);
3621 if (patch->new_name && S_ISLNK(patch->new_mode))
3622 /* the symlink at patch->new_name is created or remains */
3623 register_symlink_changes(patch->new_name, SYMLINK_IN_RESULT);
3627 static int path_is_beyond_symlink_1(struct strbuf *name)
3629 do {
3630 unsigned int change;
3632 while (--name->len && name->buf[name->len] != '/')
3633 ; /* scan backwards */
3634 if (!name->len)
3635 break;
3636 name->buf[name->len] = '\0';
3637 change = check_symlink_changes(name->buf);
3638 if (change & SYMLINK_IN_RESULT)
3639 return 1;
3640 if (change & SYMLINK_GOES_AWAY)
3642 * This cannot be "return 0", because we may
3643 * see a new one created at a higher level.
3645 continue;
3647 /* otherwise, check the preimage */
3648 if (check_index) {
3649 struct cache_entry *ce;
3651 ce = cache_file_exists(name->buf, name->len, ignore_case);
3652 if (ce && S_ISLNK(ce->ce_mode))
3653 return 1;
3654 } else {
3655 struct stat st;
3656 if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3657 return 1;
3659 } while (1);
3660 return 0;
3663 static int path_is_beyond_symlink(const char *name_)
3665 int ret;
3666 struct strbuf name = STRBUF_INIT;
3668 assert(*name_ != '\0');
3669 strbuf_addstr(&name, name_);
3670 ret = path_is_beyond_symlink_1(&name);
3671 strbuf_release(&name);
3673 return ret;
3676 static void die_on_unsafe_path(struct patch *patch)
3678 const char *old_name = NULL;
3679 const char *new_name = NULL;
3680 if (patch->is_delete)
3681 old_name = patch->old_name;
3682 else if (!patch->is_new && !patch->is_copy)
3683 old_name = patch->old_name;
3684 if (!patch->is_delete)
3685 new_name = patch->new_name;
3687 if (old_name && !verify_path(old_name))
3688 die(_("invalid path '%s'"), old_name);
3689 if (new_name && !verify_path(new_name))
3690 die(_("invalid path '%s'"), new_name);
3694 * Check and apply the patch in-core; leave the result in patch->result
3695 * for the caller to write it out to the final destination.
3697 static int check_patch(struct patch *patch)
3699 struct stat st;
3700 const char *old_name = patch->old_name;
3701 const char *new_name = patch->new_name;
3702 const char *name = old_name ? old_name : new_name;
3703 struct cache_entry *ce = NULL;
3704 struct patch *tpatch;
3705 int ok_if_exists;
3706 int status;
3708 patch->rejected = 1; /* we will drop this after we succeed */
3710 status = check_preimage(patch, &ce, &st);
3711 if (status)
3712 return status;
3713 old_name = patch->old_name;
3716 * A type-change diff is always split into a patch to delete
3717 * old, immediately followed by a patch to create new (see
3718 * diff.c::run_diff()); in such a case it is Ok that the entry
3719 * to be deleted by the previous patch is still in the working
3720 * tree and in the index.
3722 * A patch to swap-rename between A and B would first rename A
3723 * to B and then rename B to A. While applying the first one,
3724 * the presence of B should not stop A from getting renamed to
3725 * B; ask to_be_deleted() about the later rename. Removal of
3726 * B and rename from A to B is handled the same way by asking
3727 * was_deleted().
3729 if ((tpatch = in_fn_table(new_name)) &&
3730 (was_deleted(tpatch) || to_be_deleted(tpatch)))
3731 ok_if_exists = 1;
3732 else
3733 ok_if_exists = 0;
3735 if (new_name &&
3736 ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3737 int err = check_to_create(new_name, ok_if_exists);
3739 if (err && threeway) {
3740 patch->direct_to_threeway = 1;
3741 } else switch (err) {
3742 case 0:
3743 break; /* happy */
3744 case EXISTS_IN_INDEX:
3745 return error(_("%s: already exists in index"), new_name);
3746 break;
3747 case EXISTS_IN_WORKTREE:
3748 return error(_("%s: already exists in working directory"),
3749 new_name);
3750 default:
3751 return err;
3754 if (!patch->new_mode) {
3755 if (0 < patch->is_new)
3756 patch->new_mode = S_IFREG | 0644;
3757 else
3758 patch->new_mode = patch->old_mode;
3762 if (new_name && old_name) {
3763 int same = !strcmp(old_name, new_name);
3764 if (!patch->new_mode)
3765 patch->new_mode = patch->old_mode;
3766 if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3767 if (same)
3768 return error(_("new mode (%o) of %s does not "
3769 "match old mode (%o)"),
3770 patch->new_mode, new_name,
3771 patch->old_mode);
3772 else
3773 return error(_("new mode (%o) of %s does not "
3774 "match old mode (%o) of %s"),
3775 patch->new_mode, new_name,
3776 patch->old_mode, old_name);
3780 if (!unsafe_paths)
3781 die_on_unsafe_path(patch);
3784 * An attempt to read from or delete a path that is beyond a
3785 * symbolic link will be prevented by load_patch_target() that
3786 * is called at the beginning of apply_data() so we do not
3787 * have to worry about a patch marked with "is_delete" bit
3788 * here. We however need to make sure that the patch result
3789 * is not deposited to a path that is beyond a symbolic link
3790 * here.
3792 if (!patch->is_delete && path_is_beyond_symlink(patch->new_name))
3793 return error(_("affected file '%s' is beyond a symbolic link"),
3794 patch->new_name);
3796 if (apply_data(patch, &st, ce) < 0)
3797 return error(_("%s: patch does not apply"), name);
3798 patch->rejected = 0;
3799 return 0;
3802 static int check_patch_list(struct patch *patch)
3804 int err = 0;
3806 prepare_symlink_changes(patch);
3807 prepare_fn_table(patch);
3808 while (patch) {
3809 if (apply_verbosely)
3810 say_patch_name(stderr,
3811 _("Checking patch %s..."), patch);
3812 err |= check_patch(patch);
3813 patch = patch->next;
3815 return err;
3818 /* This function tries to read the sha1 from the current index */
3819 static int get_current_sha1(const char *path, unsigned char *sha1)
3821 int pos;
3823 if (read_cache() < 0)
3824 return -1;
3825 pos = cache_name_pos(path, strlen(path));
3826 if (pos < 0)
3827 return -1;
3828 hashcpy(sha1, active_cache[pos]->sha1);
3829 return 0;
3832 static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3835 * A usable gitlink patch has only one fragment (hunk) that looks like:
3836 * @@ -1 +1 @@
3837 * -Subproject commit <old sha1>
3838 * +Subproject commit <new sha1>
3839 * or
3840 * @@ -1 +0,0 @@
3841 * -Subproject commit <old sha1>
3842 * for a removal patch.
3844 struct fragment *hunk = p->fragments;
3845 static const char heading[] = "-Subproject commit ";
3846 char *preimage;
3848 if (/* does the patch have only one hunk? */
3849 hunk && !hunk->next &&
3850 /* is its preimage one line? */
3851 hunk->oldpos == 1 && hunk->oldlines == 1 &&
3852 /* does preimage begin with the heading? */
3853 (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3854 starts_with(++preimage, heading) &&
3855 /* does it record full SHA-1? */
3856 !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
3857 preimage[sizeof(heading) + 40 - 1] == '\n' &&
3858 /* does the abbreviated name on the index line agree with it? */
3859 starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3860 return 0; /* it all looks fine */
3862 /* we may have full object name on the index line */
3863 return get_sha1_hex(p->old_sha1_prefix, sha1);
3866 /* Build an index that contains the just the files needed for a 3way merge */
3867 static void build_fake_ancestor(struct patch *list, const char *filename)
3869 struct patch *patch;
3870 struct index_state result = { NULL };
3871 static struct lock_file lock;
3873 /* Once we start supporting the reverse patch, it may be
3874 * worth showing the new sha1 prefix, but until then...
3876 for (patch = list; patch; patch = patch->next) {
3877 unsigned char sha1[20];
3878 struct cache_entry *ce;
3879 const char *name;
3881 name = patch->old_name ? patch->old_name : patch->new_name;
3882 if (0 < patch->is_new)
3883 continue;
3885 if (S_ISGITLINK(patch->old_mode)) {
3886 if (!preimage_sha1_in_gitlink_patch(patch, sha1))
3887 ; /* ok, the textual part looks sane */
3888 else
3889 die("sha1 information is lacking or useless for submodule %s",
3890 name);
3891 } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
3892 ; /* ok */
3893 } else if (!patch->lines_added && !patch->lines_deleted) {
3894 /* mode-only change: update the current */
3895 if (get_current_sha1(patch->old_name, sha1))
3896 die("mode change for %s, which is not "
3897 "in current HEAD", name);
3898 } else
3899 die("sha1 information is lacking or useless "
3900 "(%s).", name);
3902 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
3903 if (!ce)
3904 die(_("make_cache_entry failed for path '%s'"), name);
3905 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
3906 die ("Could not add %s to temporary index", name);
3909 hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
3910 if (write_locked_index(&result, &lock, COMMIT_LOCK))
3911 die ("Could not write temporary index to %s", filename);
3913 discard_index(&result);
3916 static void stat_patch_list(struct patch *patch)
3918 int files, adds, dels;
3920 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
3921 files++;
3922 adds += patch->lines_added;
3923 dels += patch->lines_deleted;
3924 show_stats(patch);
3927 print_stat_summary(stdout, files, adds, dels);
3930 static void numstat_patch_list(struct patch *patch)
3932 for ( ; patch; patch = patch->next) {
3933 const char *name;
3934 name = patch->new_name ? patch->new_name : patch->old_name;
3935 if (patch->is_binary)
3936 printf("-\t-\t");
3937 else
3938 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
3939 write_name_quoted(name, stdout, line_termination);
3943 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
3945 if (mode)
3946 printf(" %s mode %06o %s\n", newdelete, mode, name);
3947 else
3948 printf(" %s %s\n", newdelete, name);
3951 static void show_mode_change(struct patch *p, int show_name)
3953 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
3954 if (show_name)
3955 printf(" mode change %06o => %06o %s\n",
3956 p->old_mode, p->new_mode, p->new_name);
3957 else
3958 printf(" mode change %06o => %06o\n",
3959 p->old_mode, p->new_mode);
3963 static void show_rename_copy(struct patch *p)
3965 const char *renamecopy = p->is_rename ? "rename" : "copy";
3966 const char *old, *new;
3968 /* Find common prefix */
3969 old = p->old_name;
3970 new = p->new_name;
3971 while (1) {
3972 const char *slash_old, *slash_new;
3973 slash_old = strchr(old, '/');
3974 slash_new = strchr(new, '/');
3975 if (!slash_old ||
3976 !slash_new ||
3977 slash_old - old != slash_new - new ||
3978 memcmp(old, new, slash_new - new))
3979 break;
3980 old = slash_old + 1;
3981 new = slash_new + 1;
3983 /* p->old_name thru old is the common prefix, and old and new
3984 * through the end of names are renames
3986 if (old != p->old_name)
3987 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
3988 (int)(old - p->old_name), p->old_name,
3989 old, new, p->score);
3990 else
3991 printf(" %s %s => %s (%d%%)\n", renamecopy,
3992 p->old_name, p->new_name, p->score);
3993 show_mode_change(p, 0);
3996 static void summary_patch_list(struct patch *patch)
3998 struct patch *p;
4000 for (p = patch; p; p = p->next) {
4001 if (p->is_new)
4002 show_file_mode_name("create", p->new_mode, p->new_name);
4003 else if (p->is_delete)
4004 show_file_mode_name("delete", p->old_mode, p->old_name);
4005 else {
4006 if (p->is_rename || p->is_copy)
4007 show_rename_copy(p);
4008 else {
4009 if (p->score) {
4010 printf(" rewrite %s (%d%%)\n",
4011 p->new_name, p->score);
4012 show_mode_change(p, 0);
4014 else
4015 show_mode_change(p, 1);
4021 static void patch_stats(struct patch *patch)
4023 int lines = patch->lines_added + patch->lines_deleted;
4025 if (lines > max_change)
4026 max_change = lines;
4027 if (patch->old_name) {
4028 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4029 if (!len)
4030 len = strlen(patch->old_name);
4031 if (len > max_len)
4032 max_len = len;
4034 if (patch->new_name) {
4035 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4036 if (!len)
4037 len = strlen(patch->new_name);
4038 if (len > max_len)
4039 max_len = len;
4043 static void remove_file(struct patch *patch, int rmdir_empty)
4045 if (update_index) {
4046 if (remove_file_from_cache(patch->old_name) < 0)
4047 die(_("unable to remove %s from index"), patch->old_name);
4049 if (!cached) {
4050 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4051 remove_path(patch->old_name);
4056 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
4058 struct stat st;
4059 struct cache_entry *ce;
4060 int namelen = strlen(path);
4061 unsigned ce_size = cache_entry_size(namelen);
4063 if (!update_index)
4064 return;
4066 ce = xcalloc(1, ce_size);
4067 memcpy(ce->name, path, namelen);
4068 ce->ce_mode = create_ce_mode(mode);
4069 ce->ce_flags = create_ce_flags(0);
4070 ce->ce_namelen = namelen;
4071 if (S_ISGITLINK(mode)) {
4072 const char *s;
4074 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4075 get_sha1_hex(s, ce->sha1))
4076 die(_("corrupt patch for submodule %s"), path);
4077 } else {
4078 if (!cached) {
4079 if (lstat(path, &st) < 0)
4080 die_errno(_("unable to stat newly created file '%s'"),
4081 path);
4082 fill_stat_cache_info(ce, &st);
4084 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
4085 die(_("unable to create backing store for newly created file %s"), path);
4087 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4088 die(_("unable to add cache entry for %s"), path);
4091 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4093 int fd;
4094 struct strbuf nbuf = STRBUF_INIT;
4096 if (S_ISGITLINK(mode)) {
4097 struct stat st;
4098 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4099 return 0;
4100 return mkdir(path, 0777);
4103 if (has_symlinks && S_ISLNK(mode))
4104 /* Although buf:size is counted string, it also is NUL
4105 * terminated.
4107 return symlink(buf, path);
4109 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4110 if (fd < 0)
4111 return -1;
4113 if (convert_to_working_tree(path, buf, size, &nbuf)) {
4114 size = nbuf.len;
4115 buf = nbuf.buf;
4117 write_or_die(fd, buf, size);
4118 strbuf_release(&nbuf);
4120 if (close(fd) < 0)
4121 die_errno(_("closing file '%s'"), path);
4122 return 0;
4126 * We optimistically assume that the directories exist,
4127 * which is true 99% of the time anyway. If they don't,
4128 * we create them and try again.
4130 static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
4132 if (cached)
4133 return;
4134 if (!try_create_file(path, mode, buf, size))
4135 return;
4137 if (errno == ENOENT) {
4138 if (safe_create_leading_directories(path))
4139 return;
4140 if (!try_create_file(path, mode, buf, size))
4141 return;
4144 if (errno == EEXIST || errno == EACCES) {
4145 /* We may be trying to create a file where a directory
4146 * used to be.
4148 struct stat st;
4149 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4150 errno = EEXIST;
4153 if (errno == EEXIST) {
4154 unsigned int nr = getpid();
4156 for (;;) {
4157 char newpath[PATH_MAX];
4158 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4159 if (!try_create_file(newpath, mode, buf, size)) {
4160 if (!rename(newpath, path))
4161 return;
4162 unlink_or_warn(newpath);
4163 break;
4165 if (errno != EEXIST)
4166 break;
4167 ++nr;
4170 die_errno(_("unable to write file '%s' mode %o"), path, mode);
4173 static void add_conflicted_stages_file(struct patch *patch)
4175 int stage, namelen;
4176 unsigned ce_size, mode;
4177 struct cache_entry *ce;
4179 if (!update_index)
4180 return;
4181 namelen = strlen(patch->new_name);
4182 ce_size = cache_entry_size(namelen);
4183 mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4185 remove_file_from_cache(patch->new_name);
4186 for (stage = 1; stage < 4; stage++) {
4187 if (is_null_sha1(patch->threeway_stage[stage - 1]))
4188 continue;
4189 ce = xcalloc(1, ce_size);
4190 memcpy(ce->name, patch->new_name, namelen);
4191 ce->ce_mode = create_ce_mode(mode);
4192 ce->ce_flags = create_ce_flags(stage);
4193 ce->ce_namelen = namelen;
4194 hashcpy(ce->sha1, patch->threeway_stage[stage - 1]);
4195 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4196 die(_("unable to add cache entry for %s"), patch->new_name);
4200 static void create_file(struct patch *patch)
4202 char *path = patch->new_name;
4203 unsigned mode = patch->new_mode;
4204 unsigned long size = patch->resultsize;
4205 char *buf = patch->result;
4207 if (!mode)
4208 mode = S_IFREG | 0644;
4209 create_one_file(path, mode, buf, size);
4211 if (patch->conflicted_threeway)
4212 add_conflicted_stages_file(patch);
4213 else
4214 add_index_file(path, mode, buf, size);
4217 /* phase zero is to remove, phase one is to create */
4218 static void write_out_one_result(struct patch *patch, int phase)
4220 if (patch->is_delete > 0) {
4221 if (phase == 0)
4222 remove_file(patch, 1);
4223 return;
4225 if (patch->is_new > 0 || patch->is_copy) {
4226 if (phase == 1)
4227 create_file(patch);
4228 return;
4231 * Rename or modification boils down to the same
4232 * thing: remove the old, write the new
4234 if (phase == 0)
4235 remove_file(patch, patch->is_rename);
4236 if (phase == 1)
4237 create_file(patch);
4240 static int write_out_one_reject(struct patch *patch)
4242 FILE *rej;
4243 char namebuf[PATH_MAX];
4244 struct fragment *frag;
4245 int cnt = 0;
4246 struct strbuf sb = STRBUF_INIT;
4248 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4249 if (!frag->rejected)
4250 continue;
4251 cnt++;
4254 if (!cnt) {
4255 if (apply_verbosely)
4256 say_patch_name(stderr,
4257 _("Applied patch %s cleanly."), patch);
4258 return 0;
4261 /* This should not happen, because a removal patch that leaves
4262 * contents are marked "rejected" at the patch level.
4264 if (!patch->new_name)
4265 die(_("internal error"));
4267 /* Say this even without --verbose */
4268 strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4269 "Applying patch %%s with %d rejects...",
4270 cnt),
4271 cnt);
4272 say_patch_name(stderr, sb.buf, patch);
4273 strbuf_release(&sb);
4275 cnt = strlen(patch->new_name);
4276 if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4277 cnt = ARRAY_SIZE(namebuf) - 5;
4278 warning(_("truncating .rej filename to %.*s.rej"),
4279 cnt - 1, patch->new_name);
4281 memcpy(namebuf, patch->new_name, cnt);
4282 memcpy(namebuf + cnt, ".rej", 5);
4284 rej = fopen(namebuf, "w");
4285 if (!rej)
4286 return error(_("cannot open %s: %s"), namebuf, strerror(errno));
4288 /* Normal git tools never deal with .rej, so do not pretend
4289 * this is a git patch by saying --git or giving extended
4290 * headers. While at it, maybe please "kompare" that wants
4291 * the trailing TAB and some garbage at the end of line ;-).
4293 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4294 patch->new_name, patch->new_name);
4295 for (cnt = 1, frag = patch->fragments;
4296 frag;
4297 cnt++, frag = frag->next) {
4298 if (!frag->rejected) {
4299 fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4300 continue;
4302 fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4303 fprintf(rej, "%.*s", frag->size, frag->patch);
4304 if (frag->patch[frag->size-1] != '\n')
4305 fputc('\n', rej);
4307 fclose(rej);
4308 return -1;
4311 static int write_out_results(struct patch *list)
4313 int phase;
4314 int errs = 0;
4315 struct patch *l;
4316 struct string_list cpath = STRING_LIST_INIT_DUP;
4318 for (phase = 0; phase < 2; phase++) {
4319 l = list;
4320 while (l) {
4321 if (l->rejected)
4322 errs = 1;
4323 else {
4324 write_out_one_result(l, phase);
4325 if (phase == 1) {
4326 if (write_out_one_reject(l))
4327 errs = 1;
4328 if (l->conflicted_threeway) {
4329 string_list_append(&cpath, l->new_name);
4330 errs = 1;
4334 l = l->next;
4338 if (cpath.nr) {
4339 struct string_list_item *item;
4341 string_list_sort(&cpath);
4342 for_each_string_list_item(item, &cpath)
4343 fprintf(stderr, "U %s\n", item->string);
4344 string_list_clear(&cpath, 0);
4346 rerere(0);
4349 return errs;
4352 static struct lock_file lock_file;
4354 #define INACCURATE_EOF (1<<0)
4355 #define RECOUNT (1<<1)
4357 static int apply_patch(int fd, const char *filename, int options)
4359 size_t offset;
4360 struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4361 struct patch *list = NULL, **listp = &list;
4362 int skipped_patch = 0;
4364 patch_input_file = filename;
4365 read_patch_file(&buf, fd);
4366 offset = 0;
4367 while (offset < buf.len) {
4368 struct patch *patch;
4369 int nr;
4371 patch = xcalloc(1, sizeof(*patch));
4372 patch->inaccurate_eof = !!(options & INACCURATE_EOF);
4373 patch->recount = !!(options & RECOUNT);
4374 nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
4375 if (nr < 0)
4376 break;
4377 if (apply_in_reverse)
4378 reverse_patches(patch);
4379 if (use_patch(patch)) {
4380 patch_stats(patch);
4381 *listp = patch;
4382 listp = &patch->next;
4384 else {
4385 free_patch(patch);
4386 skipped_patch++;
4388 offset += nr;
4391 if (!list && !skipped_patch)
4392 die(_("unrecognized input"));
4394 if (whitespace_error && (ws_error_action == die_on_ws_error))
4395 apply = 0;
4397 update_index = check_index && apply;
4398 if (update_index && newfd < 0)
4399 newfd = hold_locked_index(&lock_file, 1);
4401 if (check_index) {
4402 if (read_cache() < 0)
4403 die(_("unable to read index file"));
4406 if ((check || apply) &&
4407 check_patch_list(list) < 0 &&
4408 !apply_with_reject)
4409 exit(1);
4411 if (apply && write_out_results(list)) {
4412 if (apply_with_reject)
4413 exit(1);
4414 /* with --3way, we still need to write the index out */
4415 return 1;
4418 if (fake_ancestor)
4419 build_fake_ancestor(list, fake_ancestor);
4421 if (diffstat)
4422 stat_patch_list(list);
4424 if (numstat)
4425 numstat_patch_list(list);
4427 if (summary)
4428 summary_patch_list(list);
4430 free_patch_list(list);
4431 strbuf_release(&buf);
4432 string_list_clear(&fn_table, 0);
4433 return 0;
4436 static void git_apply_config(void)
4438 git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
4439 git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
4440 git_config(git_default_config, NULL);
4443 static int option_parse_exclude(const struct option *opt,
4444 const char *arg, int unset)
4446 add_name_limit(arg, 1);
4447 return 0;
4450 static int option_parse_include(const struct option *opt,
4451 const char *arg, int unset)
4453 add_name_limit(arg, 0);
4454 has_include = 1;
4455 return 0;
4458 static int option_parse_p(const struct option *opt,
4459 const char *arg, int unset)
4461 p_value = atoi(arg);
4462 p_value_known = 1;
4463 return 0;
4466 static int option_parse_z(const struct option *opt,
4467 const char *arg, int unset)
4469 if (unset)
4470 line_termination = '\n';
4471 else
4472 line_termination = 0;
4473 return 0;
4476 static int option_parse_space_change(const struct option *opt,
4477 const char *arg, int unset)
4479 if (unset)
4480 ws_ignore_action = ignore_ws_none;
4481 else
4482 ws_ignore_action = ignore_ws_change;
4483 return 0;
4486 static int option_parse_whitespace(const struct option *opt,
4487 const char *arg, int unset)
4489 const char **whitespace_option = opt->value;
4491 *whitespace_option = arg;
4492 parse_whitespace_option(arg);
4493 return 0;
4496 static int option_parse_directory(const struct option *opt,
4497 const char *arg, int unset)
4499 root_len = strlen(arg);
4500 if (root_len && arg[root_len - 1] != '/') {
4501 char *new_root;
4502 root = new_root = xmalloc(root_len + 2);
4503 strcpy(new_root, arg);
4504 strcpy(new_root + root_len++, "/");
4505 } else
4506 root = arg;
4507 return 0;
4510 int cmd_apply(int argc, const char **argv, const char *prefix_)
4512 int i;
4513 int errs = 0;
4514 int is_not_gitdir = !startup_info->have_repository;
4515 int force_apply = 0;
4517 const char *whitespace_option = NULL;
4519 struct option builtin_apply_options[] = {
4520 { OPTION_CALLBACK, 0, "exclude", NULL, N_("path"),
4521 N_("don't apply changes matching the given path"),
4522 0, option_parse_exclude },
4523 { OPTION_CALLBACK, 0, "include", NULL, N_("path"),
4524 N_("apply changes matching the given path"),
4525 0, option_parse_include },
4526 { OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
4527 N_("remove <num> leading slashes from traditional diff paths"),
4528 0, option_parse_p },
4529 OPT_BOOL(0, "no-add", &no_add,
4530 N_("ignore additions made by the patch")),
4531 OPT_BOOL(0, "stat", &diffstat,
4532 N_("instead of applying the patch, output diffstat for the input")),
4533 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
4534 OPT_NOOP_NOARG(0, "binary"),
4535 OPT_BOOL(0, "numstat", &numstat,
4536 N_("show number of added and deleted lines in decimal notation")),
4537 OPT_BOOL(0, "summary", &summary,
4538 N_("instead of applying the patch, output a summary for the input")),
4539 OPT_BOOL(0, "check", &check,
4540 N_("instead of applying the patch, see if the patch is applicable")),
4541 OPT_BOOL(0, "index", &check_index,
4542 N_("make sure the patch is applicable to the current index")),
4543 OPT_BOOL(0, "cached", &cached,
4544 N_("apply a patch without touching the working tree")),
4545 OPT_BOOL(0, "unsafe-paths", &unsafe_paths,
4546 N_("accept a patch that touches outside the working area")),
4547 OPT_BOOL(0, "apply", &force_apply,
4548 N_("also apply the patch (use with --stat/--summary/--check)")),
4549 OPT_BOOL('3', "3way", &threeway,
4550 N_( "attempt three-way merge if a patch does not apply")),
4551 OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
4552 N_("build a temporary index based on embedded index information")),
4553 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
4554 N_("paths are separated with NUL character"),
4555 PARSE_OPT_NOARG, option_parse_z },
4556 OPT_INTEGER('C', NULL, &p_context,
4557 N_("ensure at least <n> lines of context match")),
4558 { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),
4559 N_("detect new or modified lines that have whitespace errors"),
4560 0, option_parse_whitespace },
4561 { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
4562 N_("ignore changes in whitespace when finding context"),
4563 PARSE_OPT_NOARG, option_parse_space_change },
4564 { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
4565 N_("ignore changes in whitespace when finding context"),
4566 PARSE_OPT_NOARG, option_parse_space_change },
4567 OPT_BOOL('R', "reverse", &apply_in_reverse,
4568 N_("apply the patch in reverse")),
4569 OPT_BOOL(0, "unidiff-zero", &unidiff_zero,
4570 N_("don't expect at least one line of context")),
4571 OPT_BOOL(0, "reject", &apply_with_reject,
4572 N_("leave the rejected hunks in corresponding *.rej files")),
4573 OPT_BOOL(0, "allow-overlap", &allow_overlap,
4574 N_("allow overlapping hunks")),
4575 OPT__VERBOSE(&apply_verbosely, N_("be verbose")),
4576 OPT_BIT(0, "inaccurate-eof", &options,
4577 N_("tolerate incorrectly detected missing new-line at the end of file"),
4578 INACCURATE_EOF),
4579 OPT_BIT(0, "recount", &options,
4580 N_("do not trust the line counts in the hunk headers"),
4581 RECOUNT),
4582 { OPTION_CALLBACK, 0, "directory", NULL, N_("root"),
4583 N_("prepend <root> to all filenames"),
4584 0, option_parse_directory },
4585 OPT_END()
4588 prefix = prefix_;
4589 prefix_length = prefix ? strlen(prefix) : 0;
4590 git_apply_config();
4591 if (apply_default_whitespace)
4592 parse_whitespace_option(apply_default_whitespace);
4593 if (apply_default_ignorewhitespace)
4594 parse_ignorewhitespace_option(apply_default_ignorewhitespace);
4596 argc = parse_options(argc, argv, prefix, builtin_apply_options,
4597 apply_usage, 0);
4599 if (apply_with_reject && threeway)
4600 die("--reject and --3way cannot be used together.");
4601 if (cached && threeway)
4602 die("--cached and --3way cannot be used together.");
4603 if (threeway) {
4604 if (is_not_gitdir)
4605 die(_("--3way outside a repository"));
4606 check_index = 1;
4608 if (apply_with_reject)
4609 apply = apply_verbosely = 1;
4610 if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))
4611 apply = 0;
4612 if (check_index && is_not_gitdir)
4613 die(_("--index outside a repository"));
4614 if (cached) {
4615 if (is_not_gitdir)
4616 die(_("--cached outside a repository"));
4617 check_index = 1;
4619 if (check_index)
4620 unsafe_paths = 0;
4622 for (i = 0; i < argc; i++) {
4623 const char *arg = argv[i];
4624 int fd;
4626 if (!strcmp(arg, "-")) {
4627 errs |= apply_patch(0, "<stdin>", options);
4628 read_stdin = 0;
4629 continue;
4630 } else if (0 < prefix_length)
4631 arg = prefix_filename(prefix, prefix_length, arg);
4633 fd = open(arg, O_RDONLY);
4634 if (fd < 0)
4635 die_errno(_("can't open patch '%s'"), arg);
4636 read_stdin = 0;
4637 set_default_whitespace_mode(whitespace_option);
4638 errs |= apply_patch(fd, arg, options);
4639 close(fd);
4641 set_default_whitespace_mode(whitespace_option);
4642 if (read_stdin)
4643 errs |= apply_patch(0, "<stdin>", options);
4644 if (whitespace_error) {
4645 if (squelch_whitespace_errors &&
4646 squelch_whitespace_errors < whitespace_error) {
4647 int squelched =
4648 whitespace_error - squelch_whitespace_errors;
4649 warning(Q_("squelched %d whitespace error",
4650 "squelched %d whitespace errors",
4651 squelched),
4652 squelched);
4654 if (ws_error_action == die_on_ws_error)
4655 die(Q_("%d line adds whitespace errors.",
4656 "%d lines add whitespace errors.",
4657 whitespace_error),
4658 whitespace_error);
4659 if (applied_after_fixing_ws && apply)
4660 warning("%d line%s applied after"
4661 " fixing whitespace errors.",
4662 applied_after_fixing_ws,
4663 applied_after_fixing_ws == 1 ? "" : "s");
4664 else if (whitespace_error)
4665 warning(Q_("%d line adds whitespace errors.",
4666 "%d lines add whitespace errors.",
4667 whitespace_error),
4668 whitespace_error);
4671 if (update_index) {
4672 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
4673 die(_("Unable to write new index file"));
4676 return !!errs;