Code

fix "git apply --index ..." not to deref NULL
[git.git] / builtin / apply.c
1 /*
2  * apply.c
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  *
6  * This applies patches on top of some (arbitrary) version of the SCM.
7  *
8  */
9 #include "cache.h"
10 #include "cache-tree.h"
11 #include "quote.h"
12 #include "blob.h"
13 #include "delta.h"
14 #include "builtin.h"
15 #include "string-list.h"
16 #include "dir.h"
17 #include "parse-options.h"
19 /*
20  *  --check turns on checking that the working tree matches the
21  *    files that are being modified, but doesn't apply the patch
22  *  --stat does just a diffstat, and doesn't actually apply
23  *  --numstat does numeric diffstat, and doesn't actually apply
24  *  --index-info shows the old and new index info for paths if available.
25  *  --index updates the cache as well.
26  *  --cached updates only the cache without ever touching the working tree.
27  */
28 static const char *prefix;
29 static int prefix_length = -1;
30 static int newfd = -1;
32 static int unidiff_zero;
33 static int p_value = 1;
34 static int p_value_known;
35 static int check_index;
36 static int update_index;
37 static int cached;
38 static int diffstat;
39 static int numstat;
40 static int summary;
41 static int check;
42 static int apply = 1;
43 static int apply_in_reverse;
44 static int apply_with_reject;
45 static int apply_verbosely;
46 static int no_add;
47 static const char *fake_ancestor;
48 static int line_termination = '\n';
49 static unsigned int p_context = UINT_MAX;
50 static const char * const apply_usage[] = {
51         "git apply [options] [<patch>...]",
52         NULL
53 };
55 static enum ws_error_action {
56         nowarn_ws_error,
57         warn_on_ws_error,
58         die_on_ws_error,
59         correct_ws_error
60 } ws_error_action = warn_on_ws_error;
61 static int whitespace_error;
62 static int squelch_whitespace_errors = 5;
63 static int applied_after_fixing_ws;
65 static enum ws_ignore {
66         ignore_ws_none,
67         ignore_ws_change
68 } ws_ignore_action = ignore_ws_none;
71 static const char *patch_input_file;
72 static const char *root;
73 static int root_len;
74 static int read_stdin = 1;
75 static int options;
77 static void parse_whitespace_option(const char *option)
78 {
79         if (!option) {
80                 ws_error_action = warn_on_ws_error;
81                 return;
82         }
83         if (!strcmp(option, "warn")) {
84                 ws_error_action = warn_on_ws_error;
85                 return;
86         }
87         if (!strcmp(option, "nowarn")) {
88                 ws_error_action = nowarn_ws_error;
89                 return;
90         }
91         if (!strcmp(option, "error")) {
92                 ws_error_action = die_on_ws_error;
93                 return;
94         }
95         if (!strcmp(option, "error-all")) {
96                 ws_error_action = die_on_ws_error;
97                 squelch_whitespace_errors = 0;
98                 return;
99         }
100         if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
101                 ws_error_action = correct_ws_error;
102                 return;
103         }
104         die("unrecognized whitespace option '%s'", option);
107 static void parse_ignorewhitespace_option(const char *option)
109         if (!option || !strcmp(option, "no") ||
110             !strcmp(option, "false") || !strcmp(option, "never") ||
111             !strcmp(option, "none")) {
112                 ws_ignore_action = ignore_ws_none;
113                 return;
114         }
115         if (!strcmp(option, "change")) {
116                 ws_ignore_action = ignore_ws_change;
117                 return;
118         }
119         die("unrecognized whitespace ignore option '%s'", option);
122 static void set_default_whitespace_mode(const char *whitespace_option)
124         if (!whitespace_option && !apply_default_whitespace)
125                 ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
128 /*
129  * For "diff-stat" like behaviour, we keep track of the biggest change
130  * we've seen, and the longest filename. That allows us to do simple
131  * scaling.
132  */
133 static int max_change, max_len;
135 /*
136  * Various "current state", notably line numbers and what
137  * file (and how) we're patching right now.. The "is_xxxx"
138  * things are flags, where -1 means "don't know yet".
139  */
140 static int linenr = 1;
142 /*
143  * This represents one "hunk" from a patch, starting with
144  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
145  * patch text is pointed at by patch, and its byte length
146  * is stored in size.  leading and trailing are the number
147  * of context lines.
148  */
149 struct fragment {
150         unsigned long leading, trailing;
151         unsigned long oldpos, oldlines;
152         unsigned long newpos, newlines;
153         const char *patch;
154         int size;
155         int rejected;
156         int linenr;
157         struct fragment *next;
158 };
160 /*
161  * When dealing with a binary patch, we reuse "leading" field
162  * to store the type of the binary hunk, either deflated "delta"
163  * or deflated "literal".
164  */
165 #define binary_patch_method leading
166 #define BINARY_DELTA_DEFLATED   1
167 #define BINARY_LITERAL_DEFLATED 2
169 /*
170  * This represents a "patch" to a file, both metainfo changes
171  * such as creation/deletion, filemode and content changes represented
172  * as a series of fragments.
173  */
174 struct patch {
175         char *new_name, *old_name, *def_name;
176         unsigned int old_mode, new_mode;
177         int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
178         int rejected;
179         unsigned ws_rule;
180         unsigned long deflate_origlen;
181         int lines_added, lines_deleted;
182         int score;
183         unsigned int is_toplevel_relative:1;
184         unsigned int inaccurate_eof:1;
185         unsigned int is_binary:1;
186         unsigned int is_copy:1;
187         unsigned int is_rename:1;
188         unsigned int recount:1;
189         struct fragment *fragments;
190         char *result;
191         size_t resultsize;
192         char old_sha1_prefix[41];
193         char new_sha1_prefix[41];
194         struct patch *next;
195 };
197 /*
198  * A line in a file, len-bytes long (includes the terminating LF,
199  * except for an incomplete line at the end if the file ends with
200  * one), and its contents hashes to 'hash'.
201  */
202 struct line {
203         size_t len;
204         unsigned hash : 24;
205         unsigned flag : 8;
206 #define LINE_COMMON     1
207 };
209 /*
210  * This represents a "file", which is an array of "lines".
211  */
212 struct image {
213         char *buf;
214         size_t len;
215         size_t nr;
216         size_t alloc;
217         struct line *line_allocated;
218         struct line *line;
219 };
221 /*
222  * Records filenames that have been touched, in order to handle
223  * the case where more than one patches touch the same file.
224  */
226 static struct string_list fn_table;
228 static uint32_t hash_line(const char *cp, size_t len)
230         size_t i;
231         uint32_t h;
232         for (i = 0, h = 0; i < len; i++) {
233                 if (!isspace(cp[i])) {
234                         h = h * 3 + (cp[i] & 0xff);
235                 }
236         }
237         return h;
240 /*
241  * Compare lines s1 of length n1 and s2 of length n2, ignoring
242  * whitespace difference. Returns 1 if they match, 0 otherwise
243  */
244 static int fuzzy_matchlines(const char *s1, size_t n1,
245                             const char *s2, size_t n2)
247         const char *last1 = s1 + n1 - 1;
248         const char *last2 = s2 + n2 - 1;
249         int result = 0;
251         if (n1 < 0 || n2 < 0)
252                 return 0;
254         /* ignore line endings */
255         while ((*last1 == '\r') || (*last1 == '\n'))
256                 last1--;
257         while ((*last2 == '\r') || (*last2 == '\n'))
258                 last2--;
260         /* skip leading whitespace */
261         while (isspace(*s1) && (s1 <= last1))
262                 s1++;
263         while (isspace(*s2) && (s2 <= last2))
264                 s2++;
265         /* early return if both lines are empty */
266         if ((s1 > last1) && (s2 > last2))
267                 return 1;
268         while (!result) {
269                 result = *s1++ - *s2++;
270                 /*
271                  * Skip whitespace inside. We check for whitespace on
272                  * both buffers because we don't want "a b" to match
273                  * "ab"
274                  */
275                 if (isspace(*s1) && isspace(*s2)) {
276                         while (isspace(*s1) && s1 <= last1)
277                                 s1++;
278                         while (isspace(*s2) && s2 <= last2)
279                                 s2++;
280                 }
281                 /*
282                  * If we reached the end on one side only,
283                  * lines don't match
284                  */
285                 if (
286                     ((s2 > last2) && (s1 <= last1)) ||
287                     ((s1 > last1) && (s2 <= last2)))
288                         return 0;
289                 if ((s1 > last1) && (s2 > last2))
290                         break;
291         }
293         return !result;
296 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
298         ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
299         img->line_allocated[img->nr].len = len;
300         img->line_allocated[img->nr].hash = hash_line(bol, len);
301         img->line_allocated[img->nr].flag = flag;
302         img->nr++;
305 static void prepare_image(struct image *image, char *buf, size_t len,
306                           int prepare_linetable)
308         const char *cp, *ep;
310         memset(image, 0, sizeof(*image));
311         image->buf = buf;
312         image->len = len;
314         if (!prepare_linetable)
315                 return;
317         ep = image->buf + image->len;
318         cp = image->buf;
319         while (cp < ep) {
320                 const char *next;
321                 for (next = cp; next < ep && *next != '\n'; next++)
322                         ;
323                 if (next < ep)
324                         next++;
325                 add_line_info(image, cp, next - cp, 0);
326                 cp = next;
327         }
328         image->line = image->line_allocated;
331 static void clear_image(struct image *image)
333         free(image->buf);
334         image->buf = NULL;
335         image->len = 0;
338 static void say_patch_name(FILE *output, const char *pre,
339                            struct patch *patch, const char *post)
341         fputs(pre, output);
342         if (patch->old_name && patch->new_name &&
343             strcmp(patch->old_name, patch->new_name)) {
344                 quote_c_style(patch->old_name, NULL, output, 0);
345                 fputs(" => ", output);
346                 quote_c_style(patch->new_name, NULL, output, 0);
347         } else {
348                 const char *n = patch->new_name;
349                 if (!n)
350                         n = patch->old_name;
351                 quote_c_style(n, NULL, output, 0);
352         }
353         fputs(post, output);
356 #define CHUNKSIZE (8192)
357 #define SLOP (16)
359 static void read_patch_file(struct strbuf *sb, int fd)
361         if (strbuf_read(sb, fd, 0) < 0)
362                 die_errno("git apply: failed to read");
364         /*
365          * Make sure that we have some slop in the buffer
366          * so that we can do speculative "memcmp" etc, and
367          * see to it that it is NUL-filled.
368          */
369         strbuf_grow(sb, SLOP);
370         memset(sb->buf + sb->len, 0, SLOP);
373 static unsigned long linelen(const char *buffer, unsigned long size)
375         unsigned long len = 0;
376         while (size--) {
377                 len++;
378                 if (*buffer++ == '\n')
379                         break;
380         }
381         return len;
384 static int is_dev_null(const char *str)
386         return !memcmp("/dev/null", str, 9) && isspace(str[9]);
389 #define TERM_SPACE      1
390 #define TERM_TAB        2
392 static int name_terminate(const char *name, int namelen, int c, int terminate)
394         if (c == ' ' && !(terminate & TERM_SPACE))
395                 return 0;
396         if (c == '\t' && !(terminate & TERM_TAB))
397                 return 0;
399         return 1;
402 /* remove double slashes to make --index work with such filenames */
403 static char *squash_slash(char *name)
405         int i = 0, j = 0;
407         if (!name)
408                 return NULL;
410         while (name[i]) {
411                 if ((name[j++] = name[i++]) == '/')
412                         while (name[i] == '/')
413                                 i++;
414         }
415         name[j] = '\0';
416         return name;
419 static char *find_name_gnu(const char *line, char *def, int p_value)
421         struct strbuf name = STRBUF_INIT;
422         char *cp;
424         /*
425          * Proposed "new-style" GNU patch/diff format; see
426          * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
427          */
428         if (unquote_c_style(&name, line, NULL)) {
429                 strbuf_release(&name);
430                 return NULL;
431         }
433         for (cp = name.buf; p_value; p_value--) {
434                 cp = strchr(cp, '/');
435                 if (!cp) {
436                         strbuf_release(&name);
437                         return NULL;
438                 }
439                 cp++;
440         }
442         /* name can later be freed, so we need
443          * to memmove, not just return cp
444          */
445         strbuf_remove(&name, 0, cp - name.buf);
446         free(def);
447         if (root)
448                 strbuf_insert(&name, 0, root, root_len);
449         return squash_slash(strbuf_detach(&name, NULL));
452 static size_t sane_tz_len(const char *line, size_t len)
454         const char *tz, *p;
456         if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
457                 return 0;
458         tz = line + len - strlen(" +0500");
460         if (tz[1] != '+' && tz[1] != '-')
461                 return 0;
463         for (p = tz + 2; p != line + len; p++)
464                 if (!isdigit(*p))
465                         return 0;
467         return line + len - tz;
470 static size_t tz_with_colon_len(const char *line, size_t len)
472         const char *tz, *p;
474         if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
475                 return 0;
476         tz = line + len - strlen(" +08:00");
478         if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
479                 return 0;
480         p = tz + 2;
481         if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
482             !isdigit(*p++) || !isdigit(*p++))
483                 return 0;
485         return line + len - tz;
488 static size_t date_len(const char *line, size_t len)
490         const char *date, *p;
492         if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
493                 return 0;
494         p = date = line + len - strlen("72-02-05");
496         if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
497             !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
498             !isdigit(*p++) || !isdigit(*p++))   /* Not a date. */
499                 return 0;
501         if (date - line >= strlen("19") &&
502             isdigit(date[-1]) && isdigit(date[-2]))     /* 4-digit year */
503                 date -= strlen("19");
505         return line + len - date;
508 static size_t short_time_len(const char *line, size_t len)
510         const char *time, *p;
512         if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
513                 return 0;
514         p = time = line + len - strlen(" 07:01:32");
516         /* Permit 1-digit hours? */
517         if (*p++ != ' ' ||
518             !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
519             !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
520             !isdigit(*p++) || !isdigit(*p++))   /* Not a time. */
521                 return 0;
523         return line + len - time;
526 static size_t fractional_time_len(const char *line, size_t len)
528         const char *p;
529         size_t n;
531         /* Expected format: 19:41:17.620000023 */
532         if (!len || !isdigit(line[len - 1]))
533                 return 0;
534         p = line + len - 1;
536         /* Fractional seconds. */
537         while (p > line && isdigit(*p))
538                 p--;
539         if (*p != '.')
540                 return 0;
542         /* Hours, minutes, and whole seconds. */
543         n = short_time_len(line, p - line);
544         if (!n)
545                 return 0;
547         return line + len - p + n;
550 static size_t trailing_spaces_len(const char *line, size_t len)
552         const char *p;
554         /* Expected format: ' ' x (1 or more)  */
555         if (!len || line[len - 1] != ' ')
556                 return 0;
558         p = line + len;
559         while (p != line) {
560                 p--;
561                 if (*p != ' ')
562                         return line + len - (p + 1);
563         }
565         /* All spaces! */
566         return len;
569 static size_t diff_timestamp_len(const char *line, size_t len)
571         const char *end = line + len;
572         size_t n;
574         /*
575          * Posix: 2010-07-05 19:41:17
576          * GNU: 2010-07-05 19:41:17.620000023 -0500
577          */
579         if (!isdigit(end[-1]))
580                 return 0;
582         n = sane_tz_len(line, end - line);
583         if (!n)
584                 n = tz_with_colon_len(line, end - line);
585         end -= n;
587         n = short_time_len(line, end - line);
588         if (!n)
589                 n = fractional_time_len(line, end - line);
590         end -= n;
592         n = date_len(line, end - line);
593         if (!n) /* No date.  Too bad. */
594                 return 0;
595         end -= n;
597         if (end == line)        /* No space before date. */
598                 return 0;
599         if (end[-1] == '\t') {  /* Success! */
600                 end--;
601                 return line + len - end;
602         }
603         if (end[-1] != ' ')     /* No space before date. */
604                 return 0;
606         /* Whitespace damage. */
607         end -= trailing_spaces_len(line, end - line);
608         return line + len - end;
611 static char *find_name_common(const char *line, char *def, int p_value,
612                                 const char *end, int terminate)
614         int len;
615         const char *start = NULL;
617         if (p_value == 0)
618                 start = line;
619         while (line != end) {
620                 char c = *line;
622                 if (!end && isspace(c)) {
623                         if (c == '\n')
624                                 break;
625                         if (name_terminate(start, line-start, c, terminate))
626                                 break;
627                 }
628                 line++;
629                 if (c == '/' && !--p_value)
630                         start = line;
631         }
632         if (!start)
633                 return squash_slash(def);
634         len = line - start;
635         if (!len)
636                 return squash_slash(def);
638         /*
639          * Generally we prefer the shorter name, especially
640          * if the other one is just a variation of that with
641          * something else tacked on to the end (ie "file.orig"
642          * or "file~").
643          */
644         if (def) {
645                 int deflen = strlen(def);
646                 if (deflen < len && !strncmp(start, def, deflen))
647                         return squash_slash(def);
648                 free(def);
649         }
651         if (root) {
652                 char *ret = xmalloc(root_len + len + 1);
653                 strcpy(ret, root);
654                 memcpy(ret + root_len, start, len);
655                 ret[root_len + len] = '\0';
656                 return squash_slash(ret);
657         }
659         return squash_slash(xmemdupz(start, len));
662 static char *find_name(const char *line, char *def, int p_value, int terminate)
664         if (*line == '"') {
665                 char *name = find_name_gnu(line, def, p_value);
666                 if (name)
667                         return name;
668         }
670         return find_name_common(line, def, p_value, NULL, terminate);
673 static char *find_name_traditional(const char *line, char *def, int p_value)
675         size_t len = strlen(line);
676         size_t date_len;
678         if (*line == '"') {
679                 char *name = find_name_gnu(line, def, p_value);
680                 if (name)
681                         return name;
682         }
684         len = strchrnul(line, '\n') - line;
685         date_len = diff_timestamp_len(line, len);
686         if (!date_len)
687                 return find_name_common(line, def, p_value, NULL, TERM_TAB);
688         len -= date_len;
690         return find_name_common(line, def, p_value, line + len, 0);
693 static int count_slashes(const char *cp)
695         int cnt = 0;
696         char ch;
698         while ((ch = *cp++))
699                 if (ch == '/')
700                         cnt++;
701         return cnt;
704 /*
705  * Given the string after "--- " or "+++ ", guess the appropriate
706  * p_value for the given patch.
707  */
708 static int guess_p_value(const char *nameline)
710         char *name, *cp;
711         int val = -1;
713         if (is_dev_null(nameline))
714                 return -1;
715         name = find_name_traditional(nameline, NULL, 0);
716         if (!name)
717                 return -1;
718         cp = strchr(name, '/');
719         if (!cp)
720                 val = 0;
721         else if (prefix) {
722                 /*
723                  * Does it begin with "a/$our-prefix" and such?  Then this is
724                  * very likely to apply to our directory.
725                  */
726                 if (!strncmp(name, prefix, prefix_length))
727                         val = count_slashes(prefix);
728                 else {
729                         cp++;
730                         if (!strncmp(cp, prefix, prefix_length))
731                                 val = count_slashes(prefix) + 1;
732                 }
733         }
734         free(name);
735         return val;
738 /*
739  * Does the ---/+++ line has the POSIX timestamp after the last HT?
740  * GNU diff puts epoch there to signal a creation/deletion event.  Is
741  * this such a timestamp?
742  */
743 static int has_epoch_timestamp(const char *nameline)
745         /*
746          * We are only interested in epoch timestamp; any non-zero
747          * fraction cannot be one, hence "(\.0+)?" in the regexp below.
748          * For the same reason, the date must be either 1969-12-31 or
749          * 1970-01-01, and the seconds part must be "00".
750          */
751         const char stamp_regexp[] =
752                 "^(1969-12-31|1970-01-01)"
753                 " "
754                 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
755                 " "
756                 "([-+][0-2][0-9]:?[0-5][0-9])\n";
757         const char *timestamp = NULL, *cp, *colon;
758         static regex_t *stamp;
759         regmatch_t m[10];
760         int zoneoffset;
761         int hourminute;
762         int status;
764         for (cp = nameline; *cp != '\n'; cp++) {
765                 if (*cp == '\t')
766                         timestamp = cp + 1;
767         }
768         if (!timestamp)
769                 return 0;
770         if (!stamp) {
771                 stamp = xmalloc(sizeof(*stamp));
772                 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
773                         warning("Cannot prepare timestamp regexp %s",
774                                 stamp_regexp);
775                         return 0;
776                 }
777         }
779         status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
780         if (status) {
781                 if (status != REG_NOMATCH)
782                         warning("regexec returned %d for input: %s",
783                                 status, timestamp);
784                 return 0;
785         }
787         zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
788         if (*colon == ':')
789                 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
790         else
791                 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
792         if (timestamp[m[3].rm_so] == '-')
793                 zoneoffset = -zoneoffset;
795         /*
796          * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
797          * (west of GMT) or 1970-01-01 (east of GMT)
798          */
799         if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
800             (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
801                 return 0;
803         hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
804                       strtol(timestamp + 14, NULL, 10) -
805                       zoneoffset);
807         return ((zoneoffset < 0 && hourminute == 1440) ||
808                 (0 <= zoneoffset && !hourminute));
811 /*
812  * Get the name etc info from the ---/+++ lines of a traditional patch header
813  *
814  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
815  * files, we can happily check the index for a match, but for creating a
816  * new file we should try to match whatever "patch" does. I have no idea.
817  */
818 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
820         char *name;
822         first += 4;     /* skip "--- " */
823         second += 4;    /* skip "+++ " */
824         if (!p_value_known) {
825                 int p, q;
826                 p = guess_p_value(first);
827                 q = guess_p_value(second);
828                 if (p < 0) p = q;
829                 if (0 <= p && p == q) {
830                         p_value = p;
831                         p_value_known = 1;
832                 }
833         }
834         if (is_dev_null(first)) {
835                 patch->is_new = 1;
836                 patch->is_delete = 0;
837                 name = find_name_traditional(second, NULL, p_value);
838                 patch->new_name = name;
839         } else if (is_dev_null(second)) {
840                 patch->is_new = 0;
841                 patch->is_delete = 1;
842                 name = find_name_traditional(first, NULL, p_value);
843                 patch->old_name = name;
844         } else {
845                 name = find_name_traditional(first, NULL, p_value);
846                 name = find_name_traditional(second, name, p_value);
847                 if (has_epoch_timestamp(first)) {
848                         patch->is_new = 1;
849                         patch->is_delete = 0;
850                         patch->new_name = name;
851                 } else if (has_epoch_timestamp(second)) {
852                         patch->is_new = 0;
853                         patch->is_delete = 1;
854                         patch->old_name = name;
855                 } else {
856                         patch->old_name = patch->new_name = name;
857                 }
858         }
859         if (!name)
860                 die("unable to find filename in patch at line %d", linenr);
863 static int gitdiff_hdrend(const char *line, struct patch *patch)
865         return -1;
868 /*
869  * We're anal about diff header consistency, to make
870  * sure that we don't end up having strange ambiguous
871  * patches floating around.
872  *
873  * As a result, gitdiff_{old|new}name() will check
874  * their names against any previous information, just
875  * to make sure..
876  */
877 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
879         if (!orig_name && !isnull)
880                 return find_name(line, NULL, p_value, TERM_TAB);
882         if (orig_name) {
883                 int len;
884                 const char *name;
885                 char *another;
886                 name = orig_name;
887                 len = strlen(name);
888                 if (isnull)
889                         die("git apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
890                 another = find_name(line, NULL, p_value, TERM_TAB);
891                 if (!another || memcmp(another, name, len + 1))
892                         die("git apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
893                 free(another);
894                 return orig_name;
895         }
896         else {
897                 /* expect "/dev/null" */
898                 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
899                         die("git apply: bad git-diff - expected /dev/null on line %d", linenr);
900                 return NULL;
901         }
904 static int gitdiff_oldname(const char *line, struct patch *patch)
906         patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
907         return 0;
910 static int gitdiff_newname(const char *line, struct patch *patch)
912         patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
913         return 0;
916 static int gitdiff_oldmode(const char *line, struct patch *patch)
918         patch->old_mode = strtoul(line, NULL, 8);
919         return 0;
922 static int gitdiff_newmode(const char *line, struct patch *patch)
924         patch->new_mode = strtoul(line, NULL, 8);
925         return 0;
928 static int gitdiff_delete(const char *line, struct patch *patch)
930         patch->is_delete = 1;
931         patch->old_name = patch->def_name;
932         return gitdiff_oldmode(line, patch);
935 static int gitdiff_newfile(const char *line, struct patch *patch)
937         patch->is_new = 1;
938         patch->new_name = patch->def_name;
939         return gitdiff_newmode(line, patch);
942 static int gitdiff_copysrc(const char *line, struct patch *patch)
944         patch->is_copy = 1;
945         patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
946         return 0;
949 static int gitdiff_copydst(const char *line, struct patch *patch)
951         patch->is_copy = 1;
952         patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
953         return 0;
956 static int gitdiff_renamesrc(const char *line, struct patch *patch)
958         patch->is_rename = 1;
959         patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
960         return 0;
963 static int gitdiff_renamedst(const char *line, struct patch *patch)
965         patch->is_rename = 1;
966         patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
967         return 0;
970 static int gitdiff_similarity(const char *line, struct patch *patch)
972         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
973                 patch->score = 0;
974         return 0;
977 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
979         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
980                 patch->score = 0;
981         return 0;
984 static int gitdiff_index(const char *line, struct patch *patch)
986         /*
987          * index line is N hexadecimal, "..", N hexadecimal,
988          * and optional space with octal mode.
989          */
990         const char *ptr, *eol;
991         int len;
993         ptr = strchr(line, '.');
994         if (!ptr || ptr[1] != '.' || 40 < ptr - line)
995                 return 0;
996         len = ptr - line;
997         memcpy(patch->old_sha1_prefix, line, len);
998         patch->old_sha1_prefix[len] = 0;
1000         line = ptr + 2;
1001         ptr = strchr(line, ' ');
1002         eol = strchr(line, '\n');
1004         if (!ptr || eol < ptr)
1005                 ptr = eol;
1006         len = ptr - line;
1008         if (40 < len)
1009                 return 0;
1010         memcpy(patch->new_sha1_prefix, line, len);
1011         patch->new_sha1_prefix[len] = 0;
1012         if (*ptr == ' ')
1013                 patch->old_mode = strtoul(ptr+1, NULL, 8);
1014         return 0;
1017 /*
1018  * This is normal for a diff that doesn't change anything: we'll fall through
1019  * into the next diff. Tell the parser to break out.
1020  */
1021 static int gitdiff_unrecognized(const char *line, struct patch *patch)
1023         return -1;
1026 static const char *stop_at_slash(const char *line, int llen)
1028         int nslash = p_value;
1029         int i;
1031         for (i = 0; i < llen; i++) {
1032                 int ch = line[i];
1033                 if (ch == '/' && --nslash <= 0)
1034                         return &line[i];
1035         }
1036         return NULL;
1039 /*
1040  * This is to extract the same name that appears on "diff --git"
1041  * line.  We do not find and return anything if it is a rename
1042  * patch, and it is OK because we will find the name elsewhere.
1043  * We need to reliably find name only when it is mode-change only,
1044  * creation or deletion of an empty file.  In any of these cases,
1045  * both sides are the same name under a/ and b/ respectively.
1046  */
1047 static char *git_header_name(char *line, int llen)
1049         const char *name;
1050         const char *second = NULL;
1051         size_t len, line_len;
1053         line += strlen("diff --git ");
1054         llen -= strlen("diff --git ");
1056         if (*line == '"') {
1057                 const char *cp;
1058                 struct strbuf first = STRBUF_INIT;
1059                 struct strbuf sp = STRBUF_INIT;
1061                 if (unquote_c_style(&first, line, &second))
1062                         goto free_and_fail1;
1064                 /* advance to the first slash */
1065                 cp = stop_at_slash(first.buf, first.len);
1066                 /* we do not accept absolute paths */
1067                 if (!cp || cp == first.buf)
1068                         goto free_and_fail1;
1069                 strbuf_remove(&first, 0, cp + 1 - first.buf);
1071                 /*
1072                  * second points at one past closing dq of name.
1073                  * find the second name.
1074                  */
1075                 while ((second < line + llen) && isspace(*second))
1076                         second++;
1078                 if (line + llen <= second)
1079                         goto free_and_fail1;
1080                 if (*second == '"') {
1081                         if (unquote_c_style(&sp, second, NULL))
1082                                 goto free_and_fail1;
1083                         cp = stop_at_slash(sp.buf, sp.len);
1084                         if (!cp || cp == sp.buf)
1085                                 goto free_and_fail1;
1086                         /* They must match, otherwise ignore */
1087                         if (strcmp(cp + 1, first.buf))
1088                                 goto free_and_fail1;
1089                         strbuf_release(&sp);
1090                         return strbuf_detach(&first, NULL);
1091                 }
1093                 /* unquoted second */
1094                 cp = stop_at_slash(second, line + llen - second);
1095                 if (!cp || cp == second)
1096                         goto free_and_fail1;
1097                 cp++;
1098                 if (line + llen - cp != first.len + 1 ||
1099                     memcmp(first.buf, cp, first.len))
1100                         goto free_and_fail1;
1101                 return strbuf_detach(&first, NULL);
1103         free_and_fail1:
1104                 strbuf_release(&first);
1105                 strbuf_release(&sp);
1106                 return NULL;
1107         }
1109         /* unquoted first name */
1110         name = stop_at_slash(line, llen);
1111         if (!name || name == line)
1112                 return NULL;
1113         name++;
1115         /*
1116          * since the first name is unquoted, a dq if exists must be
1117          * the beginning of the second name.
1118          */
1119         for (second = name; second < line + llen; second++) {
1120                 if (*second == '"') {
1121                         struct strbuf sp = STRBUF_INIT;
1122                         const char *np;
1124                         if (unquote_c_style(&sp, second, NULL))
1125                                 goto free_and_fail2;
1127                         np = stop_at_slash(sp.buf, sp.len);
1128                         if (!np || np == sp.buf)
1129                                 goto free_and_fail2;
1130                         np++;
1132                         len = sp.buf + sp.len - np;
1133                         if (len < second - name &&
1134                             !strncmp(np, name, len) &&
1135                             isspace(name[len])) {
1136                                 /* Good */
1137                                 strbuf_remove(&sp, 0, np - sp.buf);
1138                                 return strbuf_detach(&sp, NULL);
1139                         }
1141                 free_and_fail2:
1142                         strbuf_release(&sp);
1143                         return NULL;
1144                 }
1145         }
1147         /*
1148          * Accept a name only if it shows up twice, exactly the same
1149          * form.
1150          */
1151         second = strchr(name, '\n');
1152         if (!second)
1153                 return NULL;
1154         line_len = second - name;
1155         for (len = 0 ; ; len++) {
1156                 switch (name[len]) {
1157                 default:
1158                         continue;
1159                 case '\n':
1160                         return NULL;
1161                 case '\t': case ' ':
1162                         second = stop_at_slash(name + len, line_len - len);
1163                         if (!second)
1164                                 return NULL;
1165                         second++;
1166                         if (second[len] == '\n' && !strncmp(name, second, len)) {
1167                                 return xmemdupz(name, len);
1168                         }
1169                 }
1170         }
1173 /* Verify that we recognize the lines following a git header */
1174 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
1176         unsigned long offset;
1178         /* A git diff has explicit new/delete information, so we don't guess */
1179         patch->is_new = 0;
1180         patch->is_delete = 0;
1182         /*
1183          * Some things may not have the old name in the
1184          * rest of the headers anywhere (pure mode changes,
1185          * or removing or adding empty files), so we get
1186          * the default name from the header.
1187          */
1188         patch->def_name = git_header_name(line, len);
1189         if (patch->def_name && root) {
1190                 char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
1191                 strcpy(s, root);
1192                 strcpy(s + root_len, patch->def_name);
1193                 free(patch->def_name);
1194                 patch->def_name = s;
1195         }
1197         line += len;
1198         size -= len;
1199         linenr++;
1200         for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
1201                 static const struct opentry {
1202                         const char *str;
1203                         int (*fn)(const char *, struct patch *);
1204                 } optable[] = {
1205                         { "@@ -", gitdiff_hdrend },
1206                         { "--- ", gitdiff_oldname },
1207                         { "+++ ", gitdiff_newname },
1208                         { "old mode ", gitdiff_oldmode },
1209                         { "new mode ", gitdiff_newmode },
1210                         { "deleted file mode ", gitdiff_delete },
1211                         { "new file mode ", gitdiff_newfile },
1212                         { "copy from ", gitdiff_copysrc },
1213                         { "copy to ", gitdiff_copydst },
1214                         { "rename old ", gitdiff_renamesrc },
1215                         { "rename new ", gitdiff_renamedst },
1216                         { "rename from ", gitdiff_renamesrc },
1217                         { "rename to ", gitdiff_renamedst },
1218                         { "similarity index ", gitdiff_similarity },
1219                         { "dissimilarity index ", gitdiff_dissimilarity },
1220                         { "index ", gitdiff_index },
1221                         { "", gitdiff_unrecognized },
1222                 };
1223                 int i;
1225                 len = linelen(line, size);
1226                 if (!len || line[len-1] != '\n')
1227                         break;
1228                 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1229                         const struct opentry *p = optable + i;
1230                         int oplen = strlen(p->str);
1231                         if (len < oplen || memcmp(p->str, line, oplen))
1232                                 continue;
1233                         if (p->fn(line + oplen, patch) < 0)
1234                                 return offset;
1235                         break;
1236                 }
1237         }
1239         return offset;
1242 static int parse_num(const char *line, unsigned long *p)
1244         char *ptr;
1246         if (!isdigit(*line))
1247                 return 0;
1248         *p = strtoul(line, &ptr, 10);
1249         return ptr - line;
1252 static int parse_range(const char *line, int len, int offset, const char *expect,
1253                        unsigned long *p1, unsigned long *p2)
1255         int digits, ex;
1257         if (offset < 0 || offset >= len)
1258                 return -1;
1259         line += offset;
1260         len -= offset;
1262         digits = parse_num(line, p1);
1263         if (!digits)
1264                 return -1;
1266         offset += digits;
1267         line += digits;
1268         len -= digits;
1270         *p2 = 1;
1271         if (*line == ',') {
1272                 digits = parse_num(line+1, p2);
1273                 if (!digits)
1274                         return -1;
1276                 offset += digits+1;
1277                 line += digits+1;
1278                 len -= digits+1;
1279         }
1281         ex = strlen(expect);
1282         if (ex > len)
1283                 return -1;
1284         if (memcmp(line, expect, ex))
1285                 return -1;
1287         return offset + ex;
1290 static void recount_diff(char *line, int size, struct fragment *fragment)
1292         int oldlines = 0, newlines = 0, ret = 0;
1294         if (size < 1) {
1295                 warning("recount: ignore empty hunk");
1296                 return;
1297         }
1299         for (;;) {
1300                 int len = linelen(line, size);
1301                 size -= len;
1302                 line += len;
1304                 if (size < 1)
1305                         break;
1307                 switch (*line) {
1308                 case ' ': case '\n':
1309                         newlines++;
1310                         /* fall through */
1311                 case '-':
1312                         oldlines++;
1313                         continue;
1314                 case '+':
1315                         newlines++;
1316                         continue;
1317                 case '\\':
1318                         continue;
1319                 case '@':
1320                         ret = size < 3 || prefixcmp(line, "@@ ");
1321                         break;
1322                 case 'd':
1323                         ret = size < 5 || prefixcmp(line, "diff ");
1324                         break;
1325                 default:
1326                         ret = -1;
1327                         break;
1328                 }
1329                 if (ret) {
1330                         warning("recount: unexpected line: %.*s",
1331                                 (int)linelen(line, size), line);
1332                         return;
1333                 }
1334                 break;
1335         }
1336         fragment->oldlines = oldlines;
1337         fragment->newlines = newlines;
1340 /*
1341  * Parse a unified diff fragment header of the
1342  * form "@@ -a,b +c,d @@"
1343  */
1344 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
1346         int offset;
1348         if (!len || line[len-1] != '\n')
1349                 return -1;
1351         /* Figure out the number of lines in a fragment */
1352         offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1353         offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1355         return offset;
1358 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
1360         unsigned long offset, len;
1362         patch->is_toplevel_relative = 0;
1363         patch->is_rename = patch->is_copy = 0;
1364         patch->is_new = patch->is_delete = -1;
1365         patch->old_mode = patch->new_mode = 0;
1366         patch->old_name = patch->new_name = NULL;
1367         for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
1368                 unsigned long nextlen;
1370                 len = linelen(line, size);
1371                 if (!len)
1372                         break;
1374                 /* Testing this early allows us to take a few shortcuts.. */
1375                 if (len < 6)
1376                         continue;
1378                 /*
1379                  * Make sure we don't find any unconnected patch fragments.
1380                  * That's a sign that we didn't find a header, and that a
1381                  * patch has become corrupted/broken up.
1382                  */
1383                 if (!memcmp("@@ -", line, 4)) {
1384                         struct fragment dummy;
1385                         if (parse_fragment_header(line, len, &dummy) < 0)
1386                                 continue;
1387                         die("patch fragment without header at line %d: %.*s",
1388                             linenr, (int)len-1, line);
1389                 }
1391                 if (size < len + 6)
1392                         break;
1394                 /*
1395                  * Git patch? It might not have a real patch, just a rename
1396                  * or mode change, so we handle that specially
1397                  */
1398                 if (!memcmp("diff --git ", line, 11)) {
1399                         int git_hdr_len = parse_git_header(line, len, size, patch);
1400                         if (git_hdr_len <= len)
1401                                 continue;
1402                         if (!patch->old_name && !patch->new_name) {
1403                                 if (!patch->def_name)
1404                                         die("git diff header lacks filename information when removing "
1405                                             "%d leading pathname components (line %d)" , p_value, linenr);
1406                                 patch->old_name = patch->new_name = patch->def_name;
1407                         }
1408                         if (!patch->is_delete && !patch->new_name)
1409                                 die("git diff header lacks filename information "
1410                                     "(line %d)", linenr);
1411                         patch->is_toplevel_relative = 1;
1412                         *hdrsize = git_hdr_len;
1413                         return offset;
1414                 }
1416                 /* --- followed by +++ ? */
1417                 if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
1418                         continue;
1420                 /*
1421                  * We only accept unified patches, so we want it to
1422                  * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1423                  * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1424                  */
1425                 nextlen = linelen(line + len, size - len);
1426                 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1427                         continue;
1429                 /* Ok, we'll consider it a patch */
1430                 parse_traditional_patch(line, line+len, patch);
1431                 *hdrsize = len + nextlen;
1432                 linenr += 2;
1433                 return offset;
1434         }
1435         return -1;
1438 static void record_ws_error(unsigned result, const char *line, int len, int linenr)
1440         char *err;
1442         if (!result)
1443                 return;
1445         whitespace_error++;
1446         if (squelch_whitespace_errors &&
1447             squelch_whitespace_errors < whitespace_error)
1448                 return;
1450         err = whitespace_error_string(result);
1451         fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1452                 patch_input_file, linenr, err, len, line);
1453         free(err);
1456 static void check_whitespace(const char *line, int len, unsigned ws_rule)
1458         unsigned result = ws_check(line + 1, len - 1, ws_rule);
1460         record_ws_error(result, line + 1, len - 2, linenr);
1463 /*
1464  * Parse a unified diff. Note that this really needs to parse each
1465  * fragment separately, since the only way to know the difference
1466  * between a "---" that is part of a patch, and a "---" that starts
1467  * the next patch is to look at the line counts..
1468  */
1469 static int parse_fragment(char *line, unsigned long size,
1470                           struct patch *patch, struct fragment *fragment)
1472         int added, deleted;
1473         int len = linelen(line, size), offset;
1474         unsigned long oldlines, newlines;
1475         unsigned long leading, trailing;
1477         offset = parse_fragment_header(line, len, fragment);
1478         if (offset < 0)
1479                 return -1;
1480         if (offset > 0 && patch->recount)
1481                 recount_diff(line + offset, size - offset, fragment);
1482         oldlines = fragment->oldlines;
1483         newlines = fragment->newlines;
1484         leading = 0;
1485         trailing = 0;
1487         /* Parse the thing.. */
1488         line += len;
1489         size -= len;
1490         linenr++;
1491         added = deleted = 0;
1492         for (offset = len;
1493              0 < size;
1494              offset += len, size -= len, line += len, linenr++) {
1495                 if (!oldlines && !newlines)
1496                         break;
1497                 len = linelen(line, size);
1498                 if (!len || line[len-1] != '\n')
1499                         return -1;
1500                 switch (*line) {
1501                 default:
1502                         return -1;
1503                 case '\n': /* newer GNU diff, an empty context line */
1504                 case ' ':
1505                         oldlines--;
1506                         newlines--;
1507                         if (!deleted && !added)
1508                                 leading++;
1509                         trailing++;
1510                         break;
1511                 case '-':
1512                         if (apply_in_reverse &&
1513                             ws_error_action != nowarn_ws_error)
1514                                 check_whitespace(line, len, patch->ws_rule);
1515                         deleted++;
1516                         oldlines--;
1517                         trailing = 0;
1518                         break;
1519                 case '+':
1520                         if (!apply_in_reverse &&
1521                             ws_error_action != nowarn_ws_error)
1522                                 check_whitespace(line, len, patch->ws_rule);
1523                         added++;
1524                         newlines--;
1525                         trailing = 0;
1526                         break;
1528                 /*
1529                  * We allow "\ No newline at end of file". Depending
1530                  * on locale settings when the patch was produced we
1531                  * don't know what this line looks like. The only
1532                  * thing we do know is that it begins with "\ ".
1533                  * Checking for 12 is just for sanity check -- any
1534                  * l10n of "\ No newline..." is at least that long.
1535                  */
1536                 case '\\':
1537                         if (len < 12 || memcmp(line, "\\ ", 2))
1538                                 return -1;
1539                         break;
1540                 }
1541         }
1542         if (oldlines || newlines)
1543                 return -1;
1544         fragment->leading = leading;
1545         fragment->trailing = trailing;
1547         /*
1548          * If a fragment ends with an incomplete line, we failed to include
1549          * it in the above loop because we hit oldlines == newlines == 0
1550          * before seeing it.
1551          */
1552         if (12 < size && !memcmp(line, "\\ ", 2))
1553                 offset += linelen(line, size);
1555         patch->lines_added += added;
1556         patch->lines_deleted += deleted;
1558         if (0 < patch->is_new && oldlines)
1559                 return error("new file depends on old contents");
1560         if (0 < patch->is_delete && newlines)
1561                 return error("deleted file still has contents");
1562         return offset;
1565 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
1567         unsigned long offset = 0;
1568         unsigned long oldlines = 0, newlines = 0, context = 0;
1569         struct fragment **fragp = &patch->fragments;
1571         while (size > 4 && !memcmp(line, "@@ -", 4)) {
1572                 struct fragment *fragment;
1573                 int len;
1575                 fragment = xcalloc(1, sizeof(*fragment));
1576                 fragment->linenr = linenr;
1577                 len = parse_fragment(line, size, patch, fragment);
1578                 if (len <= 0)
1579                         die("corrupt patch at line %d", linenr);
1580                 fragment->patch = line;
1581                 fragment->size = len;
1582                 oldlines += fragment->oldlines;
1583                 newlines += fragment->newlines;
1584                 context += fragment->leading + fragment->trailing;
1586                 *fragp = fragment;
1587                 fragp = &fragment->next;
1589                 offset += len;
1590                 line += len;
1591                 size -= len;
1592         }
1594         /*
1595          * If something was removed (i.e. we have old-lines) it cannot
1596          * be creation, and if something was added it cannot be
1597          * deletion.  However, the reverse is not true; --unified=0
1598          * patches that only add are not necessarily creation even
1599          * though they do not have any old lines, and ones that only
1600          * delete are not necessarily deletion.
1601          *
1602          * Unfortunately, a real creation/deletion patch do _not_ have
1603          * any context line by definition, so we cannot safely tell it
1604          * apart with --unified=0 insanity.  At least if the patch has
1605          * more than one hunk it is not creation or deletion.
1606          */
1607         if (patch->is_new < 0 &&
1608             (oldlines || (patch->fragments && patch->fragments->next)))
1609                 patch->is_new = 0;
1610         if (patch->is_delete < 0 &&
1611             (newlines || (patch->fragments && patch->fragments->next)))
1612                 patch->is_delete = 0;
1614         if (0 < patch->is_new && oldlines)
1615                 die("new file %s depends on old contents", patch->new_name);
1616         if (0 < patch->is_delete && newlines)
1617                 die("deleted file %s still has contents", patch->old_name);
1618         if (!patch->is_delete && !newlines && context)
1619                 fprintf(stderr, "** warning: file %s becomes empty but "
1620                         "is not deleted\n", patch->new_name);
1622         return offset;
1625 static inline int metadata_changes(struct patch *patch)
1627         return  patch->is_rename > 0 ||
1628                 patch->is_copy > 0 ||
1629                 patch->is_new > 0 ||
1630                 patch->is_delete ||
1631                 (patch->old_mode && patch->new_mode &&
1632                  patch->old_mode != patch->new_mode);
1635 static char *inflate_it(const void *data, unsigned long size,
1636                         unsigned long inflated_size)
1638         z_stream stream;
1639         void *out;
1640         int st;
1642         memset(&stream, 0, sizeof(stream));
1644         stream.next_in = (unsigned char *)data;
1645         stream.avail_in = size;
1646         stream.next_out = out = xmalloc(inflated_size);
1647         stream.avail_out = inflated_size;
1648         git_inflate_init(&stream);
1649         st = git_inflate(&stream, Z_FINISH);
1650         git_inflate_end(&stream);
1651         if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1652                 free(out);
1653                 return NULL;
1654         }
1655         return out;
1658 static struct fragment *parse_binary_hunk(char **buf_p,
1659                                           unsigned long *sz_p,
1660                                           int *status_p,
1661                                           int *used_p)
1663         /*
1664          * Expect a line that begins with binary patch method ("literal"
1665          * or "delta"), followed by the length of data before deflating.
1666          * a sequence of 'length-byte' followed by base-85 encoded data
1667          * should follow, terminated by a newline.
1668          *
1669          * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1670          * and we would limit the patch line to 66 characters,
1671          * so one line can fit up to 13 groups that would decode
1672          * to 52 bytes max.  The length byte 'A'-'Z' corresponds
1673          * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1674          */
1675         int llen, used;
1676         unsigned long size = *sz_p;
1677         char *buffer = *buf_p;
1678         int patch_method;
1679         unsigned long origlen;
1680         char *data = NULL;
1681         int hunk_size = 0;
1682         struct fragment *frag;
1684         llen = linelen(buffer, size);
1685         used = llen;
1687         *status_p = 0;
1689         if (!prefixcmp(buffer, "delta ")) {
1690                 patch_method = BINARY_DELTA_DEFLATED;
1691                 origlen = strtoul(buffer + 6, NULL, 10);
1692         }
1693         else if (!prefixcmp(buffer, "literal ")) {
1694                 patch_method = BINARY_LITERAL_DEFLATED;
1695                 origlen = strtoul(buffer + 8, NULL, 10);
1696         }
1697         else
1698                 return NULL;
1700         linenr++;
1701         buffer += llen;
1702         while (1) {
1703                 int byte_length, max_byte_length, newsize;
1704                 llen = linelen(buffer, size);
1705                 used += llen;
1706                 linenr++;
1707                 if (llen == 1) {
1708                         /* consume the blank line */
1709                         buffer++;
1710                         size--;
1711                         break;
1712                 }
1713                 /*
1714                  * Minimum line is "A00000\n" which is 7-byte long,
1715                  * and the line length must be multiple of 5 plus 2.
1716                  */
1717                 if ((llen < 7) || (llen-2) % 5)
1718                         goto corrupt;
1719                 max_byte_length = (llen - 2) / 5 * 4;
1720                 byte_length = *buffer;
1721                 if ('A' <= byte_length && byte_length <= 'Z')
1722                         byte_length = byte_length - 'A' + 1;
1723                 else if ('a' <= byte_length && byte_length <= 'z')
1724                         byte_length = byte_length - 'a' + 27;
1725                 else
1726                         goto corrupt;
1727                 /* if the input length was not multiple of 4, we would
1728                  * have filler at the end but the filler should never
1729                  * exceed 3 bytes
1730                  */
1731                 if (max_byte_length < byte_length ||
1732                     byte_length <= max_byte_length - 4)
1733                         goto corrupt;
1734                 newsize = hunk_size + byte_length;
1735                 data = xrealloc(data, newsize);
1736                 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1737                         goto corrupt;
1738                 hunk_size = newsize;
1739                 buffer += llen;
1740                 size -= llen;
1741         }
1743         frag = xcalloc(1, sizeof(*frag));
1744         frag->patch = inflate_it(data, hunk_size, origlen);
1745         if (!frag->patch)
1746                 goto corrupt;
1747         free(data);
1748         frag->size = origlen;
1749         *buf_p = buffer;
1750         *sz_p = size;
1751         *used_p = used;
1752         frag->binary_patch_method = patch_method;
1753         return frag;
1755  corrupt:
1756         free(data);
1757         *status_p = -1;
1758         error("corrupt binary patch at line %d: %.*s",
1759               linenr-1, llen-1, buffer);
1760         return NULL;
1763 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1765         /*
1766          * We have read "GIT binary patch\n"; what follows is a line
1767          * that says the patch method (currently, either "literal" or
1768          * "delta") and the length of data before deflating; a
1769          * sequence of 'length-byte' followed by base-85 encoded data
1770          * follows.
1771          *
1772          * When a binary patch is reversible, there is another binary
1773          * hunk in the same format, starting with patch method (either
1774          * "literal" or "delta") with the length of data, and a sequence
1775          * of length-byte + base-85 encoded data, terminated with another
1776          * empty line.  This data, when applied to the postimage, produces
1777          * the preimage.
1778          */
1779         struct fragment *forward;
1780         struct fragment *reverse;
1781         int status;
1782         int used, used_1;
1784         forward = parse_binary_hunk(&buffer, &size, &status, &used);
1785         if (!forward && !status)
1786                 /* there has to be one hunk (forward hunk) */
1787                 return error("unrecognized binary patch at line %d", linenr-1);
1788         if (status)
1789                 /* otherwise we already gave an error message */
1790                 return status;
1792         reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1793         if (reverse)
1794                 used += used_1;
1795         else if (status) {
1796                 /*
1797                  * Not having reverse hunk is not an error, but having
1798                  * a corrupt reverse hunk is.
1799                  */
1800                 free((void*) forward->patch);
1801                 free(forward);
1802                 return status;
1803         }
1804         forward->next = reverse;
1805         patch->fragments = forward;
1806         patch->is_binary = 1;
1807         return used;
1810 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1812         int hdrsize, patchsize;
1813         int offset = find_header(buffer, size, &hdrsize, patch);
1815         if (offset < 0)
1816                 return offset;
1818         patch->ws_rule = whitespace_rule(patch->new_name
1819                                          ? patch->new_name
1820                                          : patch->old_name);
1822         patchsize = parse_single_patch(buffer + offset + hdrsize,
1823                                        size - offset - hdrsize, patch);
1825         if (!patchsize) {
1826                 static const char *binhdr[] = {
1827                         "Binary files ",
1828                         "Files ",
1829                         NULL,
1830                 };
1831                 static const char git_binary[] = "GIT binary patch\n";
1832                 int i;
1833                 int hd = hdrsize + offset;
1834                 unsigned long llen = linelen(buffer + hd, size - hd);
1836                 if (llen == sizeof(git_binary) - 1 &&
1837                     !memcmp(git_binary, buffer + hd, llen)) {
1838                         int used;
1839                         linenr++;
1840                         used = parse_binary(buffer + hd + llen,
1841                                             size - hd - llen, patch);
1842                         if (used)
1843                                 patchsize = used + llen;
1844                         else
1845                                 patchsize = 0;
1846                 }
1847                 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1848                         for (i = 0; binhdr[i]; i++) {
1849                                 int len = strlen(binhdr[i]);
1850                                 if (len < size - hd &&
1851                                     !memcmp(binhdr[i], buffer + hd, len)) {
1852                                         linenr++;
1853                                         patch->is_binary = 1;
1854                                         patchsize = llen;
1855                                         break;
1856                                 }
1857                         }
1858                 }
1860                 /* Empty patch cannot be applied if it is a text patch
1861                  * without metadata change.  A binary patch appears
1862                  * empty to us here.
1863                  */
1864                 if ((apply || check) &&
1865                     (!patch->is_binary && !metadata_changes(patch)))
1866                         die("patch with only garbage at line %d", linenr);
1867         }
1869         return offset + hdrsize + patchsize;
1872 #define swap(a,b) myswap((a),(b),sizeof(a))
1874 #define myswap(a, b, size) do {         \
1875         unsigned char mytmp[size];      \
1876         memcpy(mytmp, &a, size);                \
1877         memcpy(&a, &b, size);           \
1878         memcpy(&b, mytmp, size);                \
1879 } while (0)
1881 static void reverse_patches(struct patch *p)
1883         for (; p; p = p->next) {
1884                 struct fragment *frag = p->fragments;
1886                 swap(p->new_name, p->old_name);
1887                 swap(p->new_mode, p->old_mode);
1888                 swap(p->is_new, p->is_delete);
1889                 swap(p->lines_added, p->lines_deleted);
1890                 swap(p->old_sha1_prefix, p->new_sha1_prefix);
1892                 for (; frag; frag = frag->next) {
1893                         swap(frag->newpos, frag->oldpos);
1894                         swap(frag->newlines, frag->oldlines);
1895                 }
1896         }
1899 static const char pluses[] =
1900 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1901 static const char minuses[]=
1902 "----------------------------------------------------------------------";
1904 static void show_stats(struct patch *patch)
1906         struct strbuf qname = STRBUF_INIT;
1907         char *cp = patch->new_name ? patch->new_name : patch->old_name;
1908         int max, add, del;
1910         quote_c_style(cp, &qname, NULL, 0);
1912         /*
1913          * "scale" the filename
1914          */
1915         max = max_len;
1916         if (max > 50)
1917                 max = 50;
1919         if (qname.len > max) {
1920                 cp = strchr(qname.buf + qname.len + 3 - max, '/');
1921                 if (!cp)
1922                         cp = qname.buf + qname.len + 3 - max;
1923                 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
1924         }
1926         if (patch->is_binary) {
1927                 printf(" %-*s |  Bin\n", max, qname.buf);
1928                 strbuf_release(&qname);
1929                 return;
1930         }
1932         printf(" %-*s |", max, qname.buf);
1933         strbuf_release(&qname);
1935         /*
1936          * scale the add/delete
1937          */
1938         max = max + max_change > 70 ? 70 - max : max_change;
1939         add = patch->lines_added;
1940         del = patch->lines_deleted;
1942         if (max_change > 0) {
1943                 int total = ((add + del) * max + max_change / 2) / max_change;
1944                 add = (add * max + max_change / 2) / max_change;
1945                 del = total - add;
1946         }
1947         printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
1948                 add, pluses, del, minuses);
1951 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1953         switch (st->st_mode & S_IFMT) {
1954         case S_IFLNK:
1955                 if (strbuf_readlink(buf, path, st->st_size) < 0)
1956                         return error("unable to read symlink %s", path);
1957                 return 0;
1958         case S_IFREG:
1959                 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
1960                         return error("unable to open or read %s", path);
1961                 convert_to_git(path, buf->buf, buf->len, buf, 0);
1962                 return 0;
1963         default:
1964                 return -1;
1965         }
1968 /*
1969  * Update the preimage, and the common lines in postimage,
1970  * from buffer buf of length len. If postlen is 0 the postimage
1971  * is updated in place, otherwise it's updated on a new buffer
1972  * of length postlen
1973  */
1975 static void update_pre_post_images(struct image *preimage,
1976                                    struct image *postimage,
1977                                    char *buf,
1978                                    size_t len, size_t postlen)
1980         int i, ctx;
1981         char *new, *old, *fixed;
1982         struct image fixed_preimage;
1984         /*
1985          * Update the preimage with whitespace fixes.  Note that we
1986          * are not losing preimage->buf -- apply_one_fragment() will
1987          * free "oldlines".
1988          */
1989         prepare_image(&fixed_preimage, buf, len, 1);
1990         assert(fixed_preimage.nr == preimage->nr);
1991         for (i = 0; i < preimage->nr; i++)
1992                 fixed_preimage.line[i].flag = preimage->line[i].flag;
1993         free(preimage->line_allocated);
1994         *preimage = fixed_preimage;
1996         /*
1997          * Adjust the common context lines in postimage. This can be
1998          * done in-place when we are just doing whitespace fixing,
1999          * which does not make the string grow, but needs a new buffer
2000          * when ignoring whitespace causes the update, since in this case
2001          * we could have e.g. tabs converted to multiple spaces.
2002          * We trust the caller to tell us if the update can be done
2003          * in place (postlen==0) or not.
2004          */
2005         old = postimage->buf;
2006         if (postlen)
2007                 new = postimage->buf = xmalloc(postlen);
2008         else
2009                 new = old;
2010         fixed = preimage->buf;
2011         for (i = ctx = 0; i < postimage->nr; i++) {
2012                 size_t len = postimage->line[i].len;
2013                 if (!(postimage->line[i].flag & LINE_COMMON)) {
2014                         /* an added line -- no counterparts in preimage */
2015                         memmove(new, old, len);
2016                         old += len;
2017                         new += len;
2018                         continue;
2019                 }
2021                 /* a common context -- skip it in the original postimage */
2022                 old += len;
2024                 /* and find the corresponding one in the fixed preimage */
2025                 while (ctx < preimage->nr &&
2026                        !(preimage->line[ctx].flag & LINE_COMMON)) {
2027                         fixed += preimage->line[ctx].len;
2028                         ctx++;
2029                 }
2030                 if (preimage->nr <= ctx)
2031                         die("oops");
2033                 /* and copy it in, while fixing the line length */
2034                 len = preimage->line[ctx].len;
2035                 memcpy(new, fixed, len);
2036                 new += len;
2037                 fixed += len;
2038                 postimage->line[i].len = len;
2039                 ctx++;
2040         }
2042         /* Fix the length of the whole thing */
2043         postimage->len = new - postimage->buf;
2046 static int match_fragment(struct image *img,
2047                           struct image *preimage,
2048                           struct image *postimage,
2049                           unsigned long try,
2050                           int try_lno,
2051                           unsigned ws_rule,
2052                           int match_beginning, int match_end)
2054         int i;
2055         char *fixed_buf, *buf, *orig, *target;
2056         struct strbuf fixed;
2057         size_t fixed_len;
2058         int preimage_limit;
2060         if (preimage->nr + try_lno <= img->nr) {
2061                 /*
2062                  * The hunk falls within the boundaries of img.
2063                  */
2064                 preimage_limit = preimage->nr;
2065                 if (match_end && (preimage->nr + try_lno != img->nr))
2066                         return 0;
2067         } else if (ws_error_action == correct_ws_error &&
2068                    (ws_rule & WS_BLANK_AT_EOF)) {
2069                 /*
2070                  * This hunk extends beyond the end of img, and we are
2071                  * removing blank lines at the end of the file.  This
2072                  * many lines from the beginning of the preimage must
2073                  * match with img, and the remainder of the preimage
2074                  * must be blank.
2075                  */
2076                 preimage_limit = img->nr - try_lno;
2077         } else {
2078                 /*
2079                  * The hunk extends beyond the end of the img and
2080                  * we are not removing blanks at the end, so we
2081                  * should reject the hunk at this position.
2082                  */
2083                 return 0;
2084         }
2086         if (match_beginning && try_lno)
2087                 return 0;
2089         /* Quick hash check */
2090         for (i = 0; i < preimage_limit; i++)
2091                 if (preimage->line[i].hash != img->line[try_lno + i].hash)
2092                         return 0;
2094         if (preimage_limit == preimage->nr) {
2095                 /*
2096                  * Do we have an exact match?  If we were told to match
2097                  * at the end, size must be exactly at try+fragsize,
2098                  * otherwise try+fragsize must be still within the preimage,
2099                  * and either case, the old piece should match the preimage
2100                  * exactly.
2101                  */
2102                 if ((match_end
2103                      ? (try + preimage->len == img->len)
2104                      : (try + preimage->len <= img->len)) &&
2105                     !memcmp(img->buf + try, preimage->buf, preimage->len))
2106                         return 1;
2107         } else {
2108                 /*
2109                  * The preimage extends beyond the end of img, so
2110                  * there cannot be an exact match.
2111                  *
2112                  * There must be one non-blank context line that match
2113                  * a line before the end of img.
2114                  */
2115                 char *buf_end;
2117                 buf = preimage->buf;
2118                 buf_end = buf;
2119                 for (i = 0; i < preimage_limit; i++)
2120                         buf_end += preimage->line[i].len;
2122                 for ( ; buf < buf_end; buf++)
2123                         if (!isspace(*buf))
2124                                 break;
2125                 if (buf == buf_end)
2126                         return 0;
2127         }
2129         /*
2130          * No exact match. If we are ignoring whitespace, run a line-by-line
2131          * fuzzy matching. We collect all the line length information because
2132          * we need it to adjust whitespace if we match.
2133          */
2134         if (ws_ignore_action == ignore_ws_change) {
2135                 size_t imgoff = 0;
2136                 size_t preoff = 0;
2137                 size_t postlen = postimage->len;
2138                 size_t extra_chars;
2139                 char *preimage_eof;
2140                 char *preimage_end;
2141                 for (i = 0; i < preimage_limit; i++) {
2142                         size_t prelen = preimage->line[i].len;
2143                         size_t imglen = img->line[try_lno+i].len;
2145                         if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2146                                               preimage->buf + preoff, prelen))
2147                                 return 0;
2148                         if (preimage->line[i].flag & LINE_COMMON)
2149                                 postlen += imglen - prelen;
2150                         imgoff += imglen;
2151                         preoff += prelen;
2152                 }
2154                 /*
2155                  * Ok, the preimage matches with whitespace fuzz.
2156                  *
2157                  * imgoff now holds the true length of the target that
2158                  * matches the preimage before the end of the file.
2159                  *
2160                  * Count the number of characters in the preimage that fall
2161                  * beyond the end of the file and make sure that all of them
2162                  * are whitespace characters. (This can only happen if
2163                  * we are removing blank lines at the end of the file.)
2164                  */
2165                 buf = preimage_eof = preimage->buf + preoff;
2166                 for ( ; i < preimage->nr; i++)
2167                         preoff += preimage->line[i].len;
2168                 preimage_end = preimage->buf + preoff;
2169                 for ( ; buf < preimage_end; buf++)
2170                         if (!isspace(*buf))
2171                                 return 0;
2173                 /*
2174                  * Update the preimage and the common postimage context
2175                  * lines to use the same whitespace as the target.
2176                  * If whitespace is missing in the target (i.e.
2177                  * if the preimage extends beyond the end of the file),
2178                  * use the whitespace from the preimage.
2179                  */
2180                 extra_chars = preimage_end - preimage_eof;
2181                 strbuf_init(&fixed, imgoff + extra_chars);
2182                 strbuf_add(&fixed, img->buf + try, imgoff);
2183                 strbuf_add(&fixed, preimage_eof, extra_chars);
2184                 fixed_buf = strbuf_detach(&fixed, &fixed_len);
2185                 update_pre_post_images(preimage, postimage,
2186                                 fixed_buf, fixed_len, postlen);
2187                 return 1;
2188         }
2190         if (ws_error_action != correct_ws_error)
2191                 return 0;
2193         /*
2194          * The hunk does not apply byte-by-byte, but the hash says
2195          * it might with whitespace fuzz. We haven't been asked to
2196          * ignore whitespace, we were asked to correct whitespace
2197          * errors, so let's try matching after whitespace correction.
2198          *
2199          * The preimage may extend beyond the end of the file,
2200          * but in this loop we will only handle the part of the
2201          * preimage that falls within the file.
2202          */
2203         strbuf_init(&fixed, preimage->len + 1);
2204         orig = preimage->buf;
2205         target = img->buf + try;
2206         for (i = 0; i < preimage_limit; i++) {
2207                 size_t oldlen = preimage->line[i].len;
2208                 size_t tgtlen = img->line[try_lno + i].len;
2209                 size_t fixstart = fixed.len;
2210                 struct strbuf tgtfix;
2211                 int match;
2213                 /* Try fixing the line in the preimage */
2214                 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2216                 /* Try fixing the line in the target */
2217                 strbuf_init(&tgtfix, tgtlen);
2218                 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2220                 /*
2221                  * If they match, either the preimage was based on
2222                  * a version before our tree fixed whitespace breakage,
2223                  * or we are lacking a whitespace-fix patch the tree
2224                  * the preimage was based on already had (i.e. target
2225                  * has whitespace breakage, the preimage doesn't).
2226                  * In either case, we are fixing the whitespace breakages
2227                  * so we might as well take the fix together with their
2228                  * real change.
2229                  */
2230                 match = (tgtfix.len == fixed.len - fixstart &&
2231                          !memcmp(tgtfix.buf, fixed.buf + fixstart,
2232                                              fixed.len - fixstart));
2234                 strbuf_release(&tgtfix);
2235                 if (!match)
2236                         goto unmatch_exit;
2238                 orig += oldlen;
2239                 target += tgtlen;
2240         }
2243         /*
2244          * Now handle the lines in the preimage that falls beyond the
2245          * end of the file (if any). They will only match if they are
2246          * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2247          * false).
2248          */
2249         for ( ; i < preimage->nr; i++) {
2250                 size_t fixstart = fixed.len; /* start of the fixed preimage */
2251                 size_t oldlen = preimage->line[i].len;
2252                 int j;
2254                 /* Try fixing the line in the preimage */
2255                 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2257                 for (j = fixstart; j < fixed.len; j++)
2258                         if (!isspace(fixed.buf[j]))
2259                                 goto unmatch_exit;
2261                 orig += oldlen;
2262         }
2264         /*
2265          * Yes, the preimage is based on an older version that still
2266          * has whitespace breakages unfixed, and fixing them makes the
2267          * hunk match.  Update the context lines in the postimage.
2268          */
2269         fixed_buf = strbuf_detach(&fixed, &fixed_len);
2270         update_pre_post_images(preimage, postimage,
2271                                fixed_buf, fixed_len, 0);
2272         return 1;
2274  unmatch_exit:
2275         strbuf_release(&fixed);
2276         return 0;
2279 static int find_pos(struct image *img,
2280                     struct image *preimage,
2281                     struct image *postimage,
2282                     int line,
2283                     unsigned ws_rule,
2284                     int match_beginning, int match_end)
2286         int i;
2287         unsigned long backwards, forwards, try;
2288         int backwards_lno, forwards_lno, try_lno;
2290         /*
2291          * If match_beginning or match_end is specified, there is no
2292          * point starting from a wrong line that will never match and
2293          * wander around and wait for a match at the specified end.
2294          */
2295         if (match_beginning)
2296                 line = 0;
2297         else if (match_end)
2298                 line = img->nr - preimage->nr;
2300         /*
2301          * Because the comparison is unsigned, the following test
2302          * will also take care of a negative line number that can
2303          * result when match_end and preimage is larger than the target.
2304          */
2305         if ((size_t) line > img->nr)
2306                 line = img->nr;
2308         try = 0;
2309         for (i = 0; i < line; i++)
2310                 try += img->line[i].len;
2312         /*
2313          * There's probably some smart way to do this, but I'll leave
2314          * that to the smart and beautiful people. I'm simple and stupid.
2315          */
2316         backwards = try;
2317         backwards_lno = line;
2318         forwards = try;
2319         forwards_lno = line;
2320         try_lno = line;
2322         for (i = 0; ; i++) {
2323                 if (match_fragment(img, preimage, postimage,
2324                                    try, try_lno, ws_rule,
2325                                    match_beginning, match_end))
2326                         return try_lno;
2328         again:
2329                 if (backwards_lno == 0 && forwards_lno == img->nr)
2330                         break;
2332                 if (i & 1) {
2333                         if (backwards_lno == 0) {
2334                                 i++;
2335                                 goto again;
2336                         }
2337                         backwards_lno--;
2338                         backwards -= img->line[backwards_lno].len;
2339                         try = backwards;
2340                         try_lno = backwards_lno;
2341                 } else {
2342                         if (forwards_lno == img->nr) {
2343                                 i++;
2344                                 goto again;
2345                         }
2346                         forwards += img->line[forwards_lno].len;
2347                         forwards_lno++;
2348                         try = forwards;
2349                         try_lno = forwards_lno;
2350                 }
2352         }
2353         return -1;
2356 static void remove_first_line(struct image *img)
2358         img->buf += img->line[0].len;
2359         img->len -= img->line[0].len;
2360         img->line++;
2361         img->nr--;
2364 static void remove_last_line(struct image *img)
2366         img->len -= img->line[--img->nr].len;
2369 static void update_image(struct image *img,
2370                          int applied_pos,
2371                          struct image *preimage,
2372                          struct image *postimage)
2374         /*
2375          * remove the copy of preimage at offset in img
2376          * and replace it with postimage
2377          */
2378         int i, nr;
2379         size_t remove_count, insert_count, applied_at = 0;
2380         char *result;
2381         int preimage_limit;
2383         /*
2384          * If we are removing blank lines at the end of img,
2385          * the preimage may extend beyond the end.
2386          * If that is the case, we must be careful only to
2387          * remove the part of the preimage that falls within
2388          * the boundaries of img. Initialize preimage_limit
2389          * to the number of lines in the preimage that falls
2390          * within the boundaries.
2391          */
2392         preimage_limit = preimage->nr;
2393         if (preimage_limit > img->nr - applied_pos)
2394                 preimage_limit = img->nr - applied_pos;
2396         for (i = 0; i < applied_pos; i++)
2397                 applied_at += img->line[i].len;
2399         remove_count = 0;
2400         for (i = 0; i < preimage_limit; i++)
2401                 remove_count += img->line[applied_pos + i].len;
2402         insert_count = postimage->len;
2404         /* Adjust the contents */
2405         result = xmalloc(img->len + insert_count - remove_count + 1);
2406         memcpy(result, img->buf, applied_at);
2407         memcpy(result + applied_at, postimage->buf, postimage->len);
2408         memcpy(result + applied_at + postimage->len,
2409                img->buf + (applied_at + remove_count),
2410                img->len - (applied_at + remove_count));
2411         free(img->buf);
2412         img->buf = result;
2413         img->len += insert_count - remove_count;
2414         result[img->len] = '\0';
2416         /* Adjust the line table */
2417         nr = img->nr + postimage->nr - preimage_limit;
2418         if (preimage_limit < postimage->nr) {
2419                 /*
2420                  * NOTE: this knows that we never call remove_first_line()
2421                  * on anything other than pre/post image.
2422                  */
2423                 img->line = xrealloc(img->line, nr * sizeof(*img->line));
2424                 img->line_allocated = img->line;
2425         }
2426         if (preimage_limit != postimage->nr)
2427                 memmove(img->line + applied_pos + postimage->nr,
2428                         img->line + applied_pos + preimage_limit,
2429                         (img->nr - (applied_pos + preimage_limit)) *
2430                         sizeof(*img->line));
2431         memcpy(img->line + applied_pos,
2432                postimage->line,
2433                postimage->nr * sizeof(*img->line));
2434         img->nr = nr;
2437 static int apply_one_fragment(struct image *img, struct fragment *frag,
2438                               int inaccurate_eof, unsigned ws_rule)
2440         int match_beginning, match_end;
2441         const char *patch = frag->patch;
2442         int size = frag->size;
2443         char *old, *oldlines;
2444         struct strbuf newlines;
2445         int new_blank_lines_at_end = 0;
2446         unsigned long leading, trailing;
2447         int pos, applied_pos;
2448         struct image preimage;
2449         struct image postimage;
2451         memset(&preimage, 0, sizeof(preimage));
2452         memset(&postimage, 0, sizeof(postimage));
2453         oldlines = xmalloc(size);
2454         strbuf_init(&newlines, size);
2456         old = oldlines;
2457         while (size > 0) {
2458                 char first;
2459                 int len = linelen(patch, size);
2460                 int plen;
2461                 int added_blank_line = 0;
2462                 int is_blank_context = 0;
2463                 size_t start;
2465                 if (!len)
2466                         break;
2468                 /*
2469                  * "plen" is how much of the line we should use for
2470                  * the actual patch data. Normally we just remove the
2471                  * first character on the line, but if the line is
2472                  * followed by "\ No newline", then we also remove the
2473                  * last one (which is the newline, of course).
2474                  */
2475                 plen = len - 1;
2476                 if (len < size && patch[len] == '\\')
2477                         plen--;
2478                 first = *patch;
2479                 if (apply_in_reverse) {
2480                         if (first == '-')
2481                                 first = '+';
2482                         else if (first == '+')
2483                                 first = '-';
2484                 }
2486                 switch (first) {
2487                 case '\n':
2488                         /* Newer GNU diff, empty context line */
2489                         if (plen < 0)
2490                                 /* ... followed by '\No newline'; nothing */
2491                                 break;
2492                         *old++ = '\n';
2493                         strbuf_addch(&newlines, '\n');
2494                         add_line_info(&preimage, "\n", 1, LINE_COMMON);
2495                         add_line_info(&postimage, "\n", 1, LINE_COMMON);
2496                         is_blank_context = 1;
2497                         break;
2498                 case ' ':
2499                         if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2500                             ws_blank_line(patch + 1, plen, ws_rule))
2501                                 is_blank_context = 1;
2502                 case '-':
2503                         memcpy(old, patch + 1, plen);
2504                         add_line_info(&preimage, old, plen,
2505                                       (first == ' ' ? LINE_COMMON : 0));
2506                         old += plen;
2507                         if (first == '-')
2508                                 break;
2509                 /* Fall-through for ' ' */
2510                 case '+':
2511                         /* --no-add does not add new lines */
2512                         if (first == '+' && no_add)
2513                                 break;
2515                         start = newlines.len;
2516                         if (first != '+' ||
2517                             !whitespace_error ||
2518                             ws_error_action != correct_ws_error) {
2519                                 strbuf_add(&newlines, patch + 1, plen);
2520                         }
2521                         else {
2522                                 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &applied_after_fixing_ws);
2523                         }
2524                         add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2525                                       (first == '+' ? 0 : LINE_COMMON));
2526                         if (first == '+' &&
2527                             (ws_rule & WS_BLANK_AT_EOF) &&
2528                             ws_blank_line(patch + 1, plen, ws_rule))
2529                                 added_blank_line = 1;
2530                         break;
2531                 case '@': case '\\':
2532                         /* Ignore it, we already handled it */
2533                         break;
2534                 default:
2535                         if (apply_verbosely)
2536                                 error("invalid start of line: '%c'", first);
2537                         return -1;
2538                 }
2539                 if (added_blank_line)
2540                         new_blank_lines_at_end++;
2541                 else if (is_blank_context)
2542                         ;
2543                 else
2544                         new_blank_lines_at_end = 0;
2545                 patch += len;
2546                 size -= len;
2547         }
2548         if (inaccurate_eof &&
2549             old > oldlines && old[-1] == '\n' &&
2550             newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2551                 old--;
2552                 strbuf_setlen(&newlines, newlines.len - 1);
2553         }
2555         leading = frag->leading;
2556         trailing = frag->trailing;
2558         /*
2559          * A hunk to change lines at the beginning would begin with
2560          * @@ -1,L +N,M @@
2561          * but we need to be careful.  -U0 that inserts before the second
2562          * line also has this pattern.
2563          *
2564          * And a hunk to add to an empty file would begin with
2565          * @@ -0,0 +N,M @@
2566          *
2567          * In other words, a hunk that is (frag->oldpos <= 1) with or
2568          * without leading context must match at the beginning.
2569          */
2570         match_beginning = (!frag->oldpos ||
2571                            (frag->oldpos == 1 && !unidiff_zero));
2573         /*
2574          * A hunk without trailing lines must match at the end.
2575          * However, we simply cannot tell if a hunk must match end
2576          * from the lack of trailing lines if the patch was generated
2577          * with unidiff without any context.
2578          */
2579         match_end = !unidiff_zero && !trailing;
2581         pos = frag->newpos ? (frag->newpos - 1) : 0;
2582         preimage.buf = oldlines;
2583         preimage.len = old - oldlines;
2584         postimage.buf = newlines.buf;
2585         postimage.len = newlines.len;
2586         preimage.line = preimage.line_allocated;
2587         postimage.line = postimage.line_allocated;
2589         for (;;) {
2591                 applied_pos = find_pos(img, &preimage, &postimage, pos,
2592                                        ws_rule, match_beginning, match_end);
2594                 if (applied_pos >= 0)
2595                         break;
2597                 /* Am I at my context limits? */
2598                 if ((leading <= p_context) && (trailing <= p_context))
2599                         break;
2600                 if (match_beginning || match_end) {
2601                         match_beginning = match_end = 0;
2602                         continue;
2603                 }
2605                 /*
2606                  * Reduce the number of context lines; reduce both
2607                  * leading and trailing if they are equal otherwise
2608                  * just reduce the larger context.
2609                  */
2610                 if (leading >= trailing) {
2611                         remove_first_line(&preimage);
2612                         remove_first_line(&postimage);
2613                         pos--;
2614                         leading--;
2615                 }
2616                 if (trailing > leading) {
2617                         remove_last_line(&preimage);
2618                         remove_last_line(&postimage);
2619                         trailing--;
2620                 }
2621         }
2623         if (applied_pos >= 0) {
2624                 if (new_blank_lines_at_end &&
2625                     preimage.nr + applied_pos >= img->nr &&
2626                     (ws_rule & WS_BLANK_AT_EOF) &&
2627                     ws_error_action != nowarn_ws_error) {
2628                         record_ws_error(WS_BLANK_AT_EOF, "+", 1, frag->linenr);
2629                         if (ws_error_action == correct_ws_error) {
2630                                 while (new_blank_lines_at_end--)
2631                                         remove_last_line(&postimage);
2632                         }
2633                         /*
2634                          * We would want to prevent write_out_results()
2635                          * from taking place in apply_patch() that follows
2636                          * the callchain led us here, which is:
2637                          * apply_patch->check_patch_list->check_patch->
2638                          * apply_data->apply_fragments->apply_one_fragment
2639                          */
2640                         if (ws_error_action == die_on_ws_error)
2641                                 apply = 0;
2642                 }
2644                 /*
2645                  * Warn if it was necessary to reduce the number
2646                  * of context lines.
2647                  */
2648                 if ((leading != frag->leading) ||
2649                     (trailing != frag->trailing))
2650                         fprintf(stderr, "Context reduced to (%ld/%ld)"
2651                                 " to apply fragment at %d\n",
2652                                 leading, trailing, applied_pos+1);
2653                 update_image(img, applied_pos, &preimage, &postimage);
2654         } else {
2655                 if (apply_verbosely)
2656                         error("while searching for:\n%.*s",
2657                               (int)(old - oldlines), oldlines);
2658         }
2660         free(oldlines);
2661         strbuf_release(&newlines);
2662         free(preimage.line_allocated);
2663         free(postimage.line_allocated);
2665         return (applied_pos < 0);
2668 static int apply_binary_fragment(struct image *img, struct patch *patch)
2670         struct fragment *fragment = patch->fragments;
2671         unsigned long len;
2672         void *dst;
2674         if (!fragment)
2675                 return error("missing binary patch data for '%s'",
2676                              patch->new_name ?
2677                              patch->new_name :
2678                              patch->old_name);
2680         /* Binary patch is irreversible without the optional second hunk */
2681         if (apply_in_reverse) {
2682                 if (!fragment->next)
2683                         return error("cannot reverse-apply a binary patch "
2684                                      "without the reverse hunk to '%s'",
2685                                      patch->new_name
2686                                      ? patch->new_name : patch->old_name);
2687                 fragment = fragment->next;
2688         }
2689         switch (fragment->binary_patch_method) {
2690         case BINARY_DELTA_DEFLATED:
2691                 dst = patch_delta(img->buf, img->len, fragment->patch,
2692                                   fragment->size, &len);
2693                 if (!dst)
2694                         return -1;
2695                 clear_image(img);
2696                 img->buf = dst;
2697                 img->len = len;
2698                 return 0;
2699         case BINARY_LITERAL_DEFLATED:
2700                 clear_image(img);
2701                 img->len = fragment->size;
2702                 img->buf = xmalloc(img->len+1);
2703                 memcpy(img->buf, fragment->patch, img->len);
2704                 img->buf[img->len] = '\0';
2705                 return 0;
2706         }
2707         return -1;
2710 static int apply_binary(struct image *img, struct patch *patch)
2712         const char *name = patch->old_name ? patch->old_name : patch->new_name;
2713         unsigned char sha1[20];
2715         /*
2716          * For safety, we require patch index line to contain
2717          * full 40-byte textual SHA1 for old and new, at least for now.
2718          */
2719         if (strlen(patch->old_sha1_prefix) != 40 ||
2720             strlen(patch->new_sha1_prefix) != 40 ||
2721             get_sha1_hex(patch->old_sha1_prefix, sha1) ||
2722             get_sha1_hex(patch->new_sha1_prefix, sha1))
2723                 return error("cannot apply binary patch to '%s' "
2724                              "without full index line", name);
2726         if (patch->old_name) {
2727                 /*
2728                  * See if the old one matches what the patch
2729                  * applies to.
2730                  */
2731                 hash_sha1_file(img->buf, img->len, blob_type, sha1);
2732                 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
2733                         return error("the patch applies to '%s' (%s), "
2734                                      "which does not match the "
2735                                      "current contents.",
2736                                      name, sha1_to_hex(sha1));
2737         }
2738         else {
2739                 /* Otherwise, the old one must be empty. */
2740                 if (img->len)
2741                         return error("the patch applies to an empty "
2742                                      "'%s' but it is not empty", name);
2743         }
2745         get_sha1_hex(patch->new_sha1_prefix, sha1);
2746         if (is_null_sha1(sha1)) {
2747                 clear_image(img);
2748                 return 0; /* deletion patch */
2749         }
2751         if (has_sha1_file(sha1)) {
2752                 /* We already have the postimage */
2753                 enum object_type type;
2754                 unsigned long size;
2755                 char *result;
2757                 result = read_sha1_file(sha1, &type, &size);
2758                 if (!result)
2759                         return error("the necessary postimage %s for "
2760                                      "'%s' cannot be read",
2761                                      patch->new_sha1_prefix, name);
2762                 clear_image(img);
2763                 img->buf = result;
2764                 img->len = size;
2765         } else {
2766                 /*
2767                  * We have verified buf matches the preimage;
2768                  * apply the patch data to it, which is stored
2769                  * in the patch->fragments->{patch,size}.
2770                  */
2771                 if (apply_binary_fragment(img, patch))
2772                         return error("binary patch does not apply to '%s'",
2773                                      name);
2775                 /* verify that the result matches */
2776                 hash_sha1_file(img->buf, img->len, blob_type, sha1);
2777                 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
2778                         return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)",
2779                                 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
2780         }
2782         return 0;
2785 static int apply_fragments(struct image *img, struct patch *patch)
2787         struct fragment *frag = patch->fragments;
2788         const char *name = patch->old_name ? patch->old_name : patch->new_name;
2789         unsigned ws_rule = patch->ws_rule;
2790         unsigned inaccurate_eof = patch->inaccurate_eof;
2792         if (patch->is_binary)
2793                 return apply_binary(img, patch);
2795         while (frag) {
2796                 if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule)) {
2797                         error("patch failed: %s:%ld", name, frag->oldpos);
2798                         if (!apply_with_reject)
2799                                 return -1;
2800                         frag->rejected = 1;
2801                 }
2802                 frag = frag->next;
2803         }
2804         return 0;
2807 static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
2809         if (!ce)
2810                 return 0;
2812         if (S_ISGITLINK(ce->ce_mode)) {
2813                 strbuf_grow(buf, 100);
2814                 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
2815         } else {
2816                 enum object_type type;
2817                 unsigned long sz;
2818                 char *result;
2820                 result = read_sha1_file(ce->sha1, &type, &sz);
2821                 if (!result)
2822                         return -1;
2823                 /* XXX read_sha1_file NUL-terminates */
2824                 strbuf_attach(buf, result, sz, sz + 1);
2825         }
2826         return 0;
2829 static struct patch *in_fn_table(const char *name)
2831         struct string_list_item *item;
2833         if (name == NULL)
2834                 return NULL;
2836         item = string_list_lookup(&fn_table, name);
2837         if (item != NULL)
2838                 return (struct patch *)item->util;
2840         return NULL;
2843 /*
2844  * item->util in the filename table records the status of the path.
2845  * Usually it points at a patch (whose result records the contents
2846  * of it after applying it), but it could be PATH_WAS_DELETED for a
2847  * path that a previously applied patch has already removed.
2848  */
2849  #define PATH_TO_BE_DELETED ((struct patch *) -2)
2850 #define PATH_WAS_DELETED ((struct patch *) -1)
2852 static int to_be_deleted(struct patch *patch)
2854         return patch == PATH_TO_BE_DELETED;
2857 static int was_deleted(struct patch *patch)
2859         return patch == PATH_WAS_DELETED;
2862 static void add_to_fn_table(struct patch *patch)
2864         struct string_list_item *item;
2866         /*
2867          * Always add new_name unless patch is a deletion
2868          * This should cover the cases for normal diffs,
2869          * file creations and copies
2870          */
2871         if (patch->new_name != NULL) {
2872                 item = string_list_insert(&fn_table, patch->new_name);
2873                 item->util = patch;
2874         }
2876         /*
2877          * store a failure on rename/deletion cases because
2878          * later chunks shouldn't patch old names
2879          */
2880         if ((patch->new_name == NULL) || (patch->is_rename)) {
2881                 item = string_list_insert(&fn_table, patch->old_name);
2882                 item->util = PATH_WAS_DELETED;
2883         }
2886 static void prepare_fn_table(struct patch *patch)
2888         /*
2889          * store information about incoming file deletion
2890          */
2891         while (patch) {
2892                 if ((patch->new_name == NULL) || (patch->is_rename)) {
2893                         struct string_list_item *item;
2894                         item = string_list_insert(&fn_table, patch->old_name);
2895                         item->util = PATH_TO_BE_DELETED;
2896                 }
2897                 patch = patch->next;
2898         }
2901 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
2903         struct strbuf buf = STRBUF_INIT;
2904         struct image image;
2905         size_t len;
2906         char *img;
2907         struct patch *tpatch;
2909         if (!(patch->is_copy || patch->is_rename) &&
2910             (tpatch = in_fn_table(patch->old_name)) != NULL && !to_be_deleted(tpatch)) {
2911                 if (was_deleted(tpatch)) {
2912                         return error("patch %s has been renamed/deleted",
2913                                 patch->old_name);
2914                 }
2915                 /* We have a patched copy in memory use that */
2916                 strbuf_add(&buf, tpatch->result, tpatch->resultsize);
2917         } else if (cached) {
2918                 if (read_file_or_gitlink(ce, &buf))
2919                         return error("read of %s failed", patch->old_name);
2920         } else if (patch->old_name) {
2921                 if (S_ISGITLINK(patch->old_mode)) {
2922                         if (ce) {
2923                                 read_file_or_gitlink(ce, &buf);
2924                         } else {
2925                                 /*
2926                                  * There is no way to apply subproject
2927                                  * patch without looking at the index.
2928                                  */
2929                                 patch->fragments = NULL;
2930                         }
2931                 } else {
2932                         if (read_old_data(st, patch->old_name, &buf))
2933                                 return error("read of %s failed", patch->old_name);
2934                 }
2935         }
2937         img = strbuf_detach(&buf, &len);
2938         prepare_image(&image, img, len, !patch->is_binary);
2940         if (apply_fragments(&image, patch) < 0)
2941                 return -1; /* note with --reject this succeeds. */
2942         patch->result = image.buf;
2943         patch->resultsize = image.len;
2944         add_to_fn_table(patch);
2945         free(image.line_allocated);
2947         if (0 < patch->is_delete && patch->resultsize)
2948                 return error("removal patch leaves file contents");
2950         return 0;
2953 static int check_to_create_blob(const char *new_name, int ok_if_exists)
2955         struct stat nst;
2956         if (!lstat(new_name, &nst)) {
2957                 if (S_ISDIR(nst.st_mode) || ok_if_exists)
2958                         return 0;
2959                 /*
2960                  * A leading component of new_name might be a symlink
2961                  * that is going to be removed with this patch, but
2962                  * still pointing at somewhere that has the path.
2963                  * In such a case, path "new_name" does not exist as
2964                  * far as git is concerned.
2965                  */
2966                 if (has_symlink_leading_path(new_name, strlen(new_name)))
2967                         return 0;
2969                 return error("%s: already exists in working directory", new_name);
2970         }
2971         else if ((errno != ENOENT) && (errno != ENOTDIR))
2972                 return error("%s: %s", new_name, strerror(errno));
2973         return 0;
2976 static int verify_index_match(struct cache_entry *ce, struct stat *st)
2978         if (S_ISGITLINK(ce->ce_mode)) {
2979                 if (!S_ISDIR(st->st_mode))
2980                         return -1;
2981                 return 0;
2982         }
2983         return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
2986 static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
2988         const char *old_name = patch->old_name;
2989         struct patch *tpatch = NULL;
2990         int stat_ret = 0;
2991         unsigned st_mode = 0;
2993         /*
2994          * Make sure that we do not have local modifications from the
2995          * index when we are looking at the index.  Also make sure
2996          * we have the preimage file to be patched in the work tree,
2997          * unless --cached, which tells git to apply only in the index.
2998          */
2999         if (!old_name)
3000                 return 0;
3002         assert(patch->is_new <= 0);
3004         if (!(patch->is_copy || patch->is_rename) &&
3005             (tpatch = in_fn_table(old_name)) != NULL && !to_be_deleted(tpatch)) {
3006                 if (was_deleted(tpatch))
3007                         return error("%s: has been deleted/renamed", old_name);
3008                 st_mode = tpatch->new_mode;
3009         } else if (!cached) {
3010                 stat_ret = lstat(old_name, st);
3011                 if (stat_ret && errno != ENOENT)
3012                         return error("%s: %s", old_name, strerror(errno));
3013         }
3015         if (to_be_deleted(tpatch))
3016                 tpatch = NULL;
3018         if (check_index && !tpatch) {
3019                 int pos = cache_name_pos(old_name, strlen(old_name));
3020                 if (pos < 0) {
3021                         if (patch->is_new < 0)
3022                                 goto is_new;
3023                         return error("%s: does not exist in index", old_name);
3024                 }
3025                 *ce = active_cache[pos];
3026                 if (stat_ret < 0) {
3027                         struct checkout costate;
3028                         /* checkout */
3029                         memset(&costate, 0, sizeof(costate));
3030                         costate.base_dir = "";
3031                         costate.refresh_cache = 1;
3032                         if (checkout_entry(*ce, &costate, NULL) ||
3033                             lstat(old_name, st))
3034                                 return -1;
3035                 }
3036                 if (!cached && verify_index_match(*ce, st))
3037                         return error("%s: does not match index", old_name);
3038                 if (cached)
3039                         st_mode = (*ce)->ce_mode;
3040         } else if (stat_ret < 0) {
3041                 if (patch->is_new < 0)
3042                         goto is_new;
3043                 return error("%s: %s", old_name, strerror(errno));
3044         }
3046         if (!cached && !tpatch)
3047                 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3049         if (patch->is_new < 0)
3050                 patch->is_new = 0;
3051         if (!patch->old_mode)
3052                 patch->old_mode = st_mode;
3053         if ((st_mode ^ patch->old_mode) & S_IFMT)
3054                 return error("%s: wrong type", old_name);
3055         if (st_mode != patch->old_mode)
3056                 warning("%s has type %o, expected %o",
3057                         old_name, st_mode, patch->old_mode);
3058         if (!patch->new_mode && !patch->is_delete)
3059                 patch->new_mode = st_mode;
3060         return 0;
3062  is_new:
3063         patch->is_new = 1;
3064         patch->is_delete = 0;
3065         patch->old_name = NULL;
3066         return 0;
3069 static int check_patch(struct patch *patch)
3071         struct stat st;
3072         const char *old_name = patch->old_name;
3073         const char *new_name = patch->new_name;
3074         const char *name = old_name ? old_name : new_name;
3075         struct cache_entry *ce = NULL;
3076         struct patch *tpatch;
3077         int ok_if_exists;
3078         int status;
3080         patch->rejected = 1; /* we will drop this after we succeed */
3082         status = check_preimage(patch, &ce, &st);
3083         if (status)
3084                 return status;
3085         old_name = patch->old_name;
3087         if ((tpatch = in_fn_table(new_name)) &&
3088                         (was_deleted(tpatch) || to_be_deleted(tpatch)))
3089                 /*
3090                  * A type-change diff is always split into a patch to
3091                  * delete old, immediately followed by a patch to
3092                  * create new (see diff.c::run_diff()); in such a case
3093                  * it is Ok that the entry to be deleted by the
3094                  * previous patch is still in the working tree and in
3095                  * the index.
3096                  */
3097                 ok_if_exists = 1;
3098         else
3099                 ok_if_exists = 0;
3101         if (new_name &&
3102             ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
3103                 if (check_index &&
3104                     cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3105                     !ok_if_exists)
3106                         return error("%s: already exists in index", new_name);
3107                 if (!cached) {
3108                         int err = check_to_create_blob(new_name, ok_if_exists);
3109                         if (err)
3110                                 return err;
3111                 }
3112                 if (!patch->new_mode) {
3113                         if (0 < patch->is_new)
3114                                 patch->new_mode = S_IFREG | 0644;
3115                         else
3116                                 patch->new_mode = patch->old_mode;
3117                 }
3118         }
3120         if (new_name && old_name) {
3121                 int same = !strcmp(old_name, new_name);
3122                 if (!patch->new_mode)
3123                         patch->new_mode = patch->old_mode;
3124                 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
3125                         return error("new mode (%o) of %s does not match old mode (%o)%s%s",
3126                                 patch->new_mode, new_name, patch->old_mode,
3127                                 same ? "" : " of ", same ? "" : old_name);
3128         }
3130         if (apply_data(patch, &st, ce) < 0)
3131                 return error("%s: patch does not apply", name);
3132         patch->rejected = 0;
3133         return 0;
3136 static int check_patch_list(struct patch *patch)
3138         int err = 0;
3140         prepare_fn_table(patch);
3141         while (patch) {
3142                 if (apply_verbosely)
3143                         say_patch_name(stderr,
3144                                        "Checking patch ", patch, "...\n");
3145                 err |= check_patch(patch);
3146                 patch = patch->next;
3147         }
3148         return err;
3151 /* This function tries to read the sha1 from the current index */
3152 static int get_current_sha1(const char *path, unsigned char *sha1)
3154         int pos;
3156         if (read_cache() < 0)
3157                 return -1;
3158         pos = cache_name_pos(path, strlen(path));
3159         if (pos < 0)
3160                 return -1;
3161         hashcpy(sha1, active_cache[pos]->sha1);
3162         return 0;
3165 /* Build an index that contains the just the files needed for a 3way merge */
3166 static void build_fake_ancestor(struct patch *list, const char *filename)
3168         struct patch *patch;
3169         struct index_state result = { NULL };
3170         int fd;
3172         /* Once we start supporting the reverse patch, it may be
3173          * worth showing the new sha1 prefix, but until then...
3174          */
3175         for (patch = list; patch; patch = patch->next) {
3176                 const unsigned char *sha1_ptr;
3177                 unsigned char sha1[20];
3178                 struct cache_entry *ce;
3179                 const char *name;
3181                 name = patch->old_name ? patch->old_name : patch->new_name;
3182                 if (0 < patch->is_new)
3183                         continue;
3184                 else if (get_sha1(patch->old_sha1_prefix, sha1))
3185                         /* git diff has no index line for mode/type changes */
3186                         if (!patch->lines_added && !patch->lines_deleted) {
3187                                 if (get_current_sha1(patch->old_name, sha1))
3188                                         die("mode change for %s, which is not "
3189                                                 "in current HEAD", name);
3190                                 sha1_ptr = sha1;
3191                         } else
3192                                 die("sha1 information is lacking or useless "
3193                                         "(%s).", name);
3194                 else
3195                         sha1_ptr = sha1;
3197                 ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);
3198                 if (!ce)
3199                         die("make_cache_entry failed for path '%s'", name);
3200                 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
3201                         die ("Could not add %s to temporary index", name);
3202         }
3204         fd = open(filename, O_WRONLY | O_CREAT, 0666);
3205         if (fd < 0 || write_index(&result, fd) || close(fd))
3206                 die ("Could not write temporary index to %s", filename);
3208         discard_index(&result);
3211 static void stat_patch_list(struct patch *patch)
3213         int files, adds, dels;
3215         for (files = adds = dels = 0 ; patch ; patch = patch->next) {
3216                 files++;
3217                 adds += patch->lines_added;
3218                 dels += patch->lines_deleted;
3219                 show_stats(patch);
3220         }
3222         printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
3225 static void numstat_patch_list(struct patch *patch)
3227         for ( ; patch; patch = patch->next) {
3228                 const char *name;
3229                 name = patch->new_name ? patch->new_name : patch->old_name;
3230                 if (patch->is_binary)
3231                         printf("-\t-\t");
3232                 else
3233                         printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
3234                 write_name_quoted(name, stdout, line_termination);
3235         }
3238 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
3240         if (mode)
3241                 printf(" %s mode %06o %s\n", newdelete, mode, name);
3242         else
3243                 printf(" %s %s\n", newdelete, name);
3246 static void show_mode_change(struct patch *p, int show_name)
3248         if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
3249                 if (show_name)
3250                         printf(" mode change %06o => %06o %s\n",
3251                                p->old_mode, p->new_mode, p->new_name);
3252                 else
3253                         printf(" mode change %06o => %06o\n",
3254                                p->old_mode, p->new_mode);
3255         }
3258 static void show_rename_copy(struct patch *p)
3260         const char *renamecopy = p->is_rename ? "rename" : "copy";
3261         const char *old, *new;
3263         /* Find common prefix */
3264         old = p->old_name;
3265         new = p->new_name;
3266         while (1) {
3267                 const char *slash_old, *slash_new;
3268                 slash_old = strchr(old, '/');
3269                 slash_new = strchr(new, '/');
3270                 if (!slash_old ||
3271                     !slash_new ||
3272                     slash_old - old != slash_new - new ||
3273                     memcmp(old, new, slash_new - new))
3274                         break;
3275                 old = slash_old + 1;
3276                 new = slash_new + 1;
3277         }
3278         /* p->old_name thru old is the common prefix, and old and new
3279          * through the end of names are renames
3280          */
3281         if (old != p->old_name)
3282                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
3283                        (int)(old - p->old_name), p->old_name,
3284                        old, new, p->score);
3285         else
3286                 printf(" %s %s => %s (%d%%)\n", renamecopy,
3287                        p->old_name, p->new_name, p->score);
3288         show_mode_change(p, 0);
3291 static void summary_patch_list(struct patch *patch)
3293         struct patch *p;
3295         for (p = patch; p; p = p->next) {
3296                 if (p->is_new)
3297                         show_file_mode_name("create", p->new_mode, p->new_name);
3298                 else if (p->is_delete)
3299                         show_file_mode_name("delete", p->old_mode, p->old_name);
3300                 else {
3301                         if (p->is_rename || p->is_copy)
3302                                 show_rename_copy(p);
3303                         else {
3304                                 if (p->score) {
3305                                         printf(" rewrite %s (%d%%)\n",
3306                                                p->new_name, p->score);
3307                                         show_mode_change(p, 0);
3308                                 }
3309                                 else
3310                                         show_mode_change(p, 1);
3311                         }
3312                 }
3313         }
3316 static void patch_stats(struct patch *patch)
3318         int lines = patch->lines_added + patch->lines_deleted;
3320         if (lines > max_change)
3321                 max_change = lines;
3322         if (patch->old_name) {
3323                 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
3324                 if (!len)
3325                         len = strlen(patch->old_name);
3326                 if (len > max_len)
3327                         max_len = len;
3328         }
3329         if (patch->new_name) {
3330                 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
3331                 if (!len)
3332                         len = strlen(patch->new_name);
3333                 if (len > max_len)
3334                         max_len = len;
3335         }
3338 static void remove_file(struct patch *patch, int rmdir_empty)
3340         if (update_index) {
3341                 if (remove_file_from_cache(patch->old_name) < 0)
3342                         die("unable to remove %s from index", patch->old_name);
3343         }
3344         if (!cached) {
3345                 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
3346                         remove_path(patch->old_name);
3347                 }
3348         }
3351 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
3353         struct stat st;
3354         struct cache_entry *ce;
3355         int namelen = strlen(path);
3356         unsigned ce_size = cache_entry_size(namelen);
3358         if (!update_index)
3359                 return;
3361         ce = xcalloc(1, ce_size);
3362         memcpy(ce->name, path, namelen);
3363         ce->ce_mode = create_ce_mode(mode);
3364         ce->ce_flags = namelen;
3365         if (S_ISGITLINK(mode)) {
3366                 const char *s = buf;
3368                 if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
3369                         die("corrupt patch for subproject %s", path);
3370         } else {
3371                 if (!cached) {
3372                         if (lstat(path, &st) < 0)
3373                                 die_errno("unable to stat newly created file '%s'",
3374                                           path);
3375                         fill_stat_cache_info(ce, &st);
3376                 }
3377                 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
3378                         die("unable to create backing store for newly created file %s", path);
3379         }
3380         if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
3381                 die("unable to add cache entry for %s", path);
3384 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
3386         int fd;
3387         struct strbuf nbuf = STRBUF_INIT;
3389         if (S_ISGITLINK(mode)) {
3390                 struct stat st;
3391                 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
3392                         return 0;
3393                 return mkdir(path, 0777);
3394         }
3396         if (has_symlinks && S_ISLNK(mode))
3397                 /* Although buf:size is counted string, it also is NUL
3398                  * terminated.
3399                  */
3400                 return symlink(buf, path);
3402         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
3403         if (fd < 0)
3404                 return -1;
3406         if (convert_to_working_tree(path, buf, size, &nbuf)) {
3407                 size = nbuf.len;
3408                 buf  = nbuf.buf;
3409         }
3410         write_or_die(fd, buf, size);
3411         strbuf_release(&nbuf);
3413         if (close(fd) < 0)
3414                 die_errno("closing file '%s'", path);
3415         return 0;
3418 /*
3419  * We optimistically assume that the directories exist,
3420  * which is true 99% of the time anyway. If they don't,
3421  * we create them and try again.
3422  */
3423 static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
3425         if (cached)
3426                 return;
3427         if (!try_create_file(path, mode, buf, size))
3428                 return;
3430         if (errno == ENOENT) {
3431                 if (safe_create_leading_directories(path))
3432                         return;
3433                 if (!try_create_file(path, mode, buf, size))
3434                         return;
3435         }
3437         if (errno == EEXIST || errno == EACCES) {
3438                 /* We may be trying to create a file where a directory
3439                  * used to be.
3440                  */
3441                 struct stat st;
3442                 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
3443                         errno = EEXIST;
3444         }
3446         if (errno == EEXIST) {
3447                 unsigned int nr = getpid();
3449                 for (;;) {
3450                         char newpath[PATH_MAX];
3451                         mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
3452                         if (!try_create_file(newpath, mode, buf, size)) {
3453                                 if (!rename(newpath, path))
3454                                         return;
3455                                 unlink_or_warn(newpath);
3456                                 break;
3457                         }
3458                         if (errno != EEXIST)
3459                                 break;
3460                         ++nr;
3461                 }
3462         }
3463         die_errno("unable to write file '%s' mode %o", path, mode);
3466 static void create_file(struct patch *patch)
3468         char *path = patch->new_name;
3469         unsigned mode = patch->new_mode;
3470         unsigned long size = patch->resultsize;
3471         char *buf = patch->result;
3473         if (!mode)
3474                 mode = S_IFREG | 0644;
3475         create_one_file(path, mode, buf, size);
3476         add_index_file(path, mode, buf, size);
3479 /* phase zero is to remove, phase one is to create */
3480 static void write_out_one_result(struct patch *patch, int phase)
3482         if (patch->is_delete > 0) {
3483                 if (phase == 0)
3484                         remove_file(patch, 1);
3485                 return;
3486         }
3487         if (patch->is_new > 0 || patch->is_copy) {
3488                 if (phase == 1)
3489                         create_file(patch);
3490                 return;
3491         }
3492         /*
3493          * Rename or modification boils down to the same
3494          * thing: remove the old, write the new
3495          */
3496         if (phase == 0)
3497                 remove_file(patch, patch->is_rename);
3498         if (phase == 1)
3499                 create_file(patch);
3502 static int write_out_one_reject(struct patch *patch)
3504         FILE *rej;
3505         char namebuf[PATH_MAX];
3506         struct fragment *frag;
3507         int cnt = 0;
3509         for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
3510                 if (!frag->rejected)
3511                         continue;
3512                 cnt++;
3513         }
3515         if (!cnt) {
3516                 if (apply_verbosely)
3517                         say_patch_name(stderr,
3518                                        "Applied patch ", patch, " cleanly.\n");
3519                 return 0;
3520         }
3522         /* This should not happen, because a removal patch that leaves
3523          * contents are marked "rejected" at the patch level.
3524          */
3525         if (!patch->new_name)
3526                 die("internal error");
3528         /* Say this even without --verbose */
3529         say_patch_name(stderr, "Applying patch ", patch, " with");
3530         fprintf(stderr, " %d rejects...\n", cnt);
3532         cnt = strlen(patch->new_name);
3533         if (ARRAY_SIZE(namebuf) <= cnt + 5) {
3534                 cnt = ARRAY_SIZE(namebuf) - 5;
3535                 warning("truncating .rej filename to %.*s.rej",
3536                         cnt - 1, patch->new_name);
3537         }
3538         memcpy(namebuf, patch->new_name, cnt);
3539         memcpy(namebuf + cnt, ".rej", 5);
3541         rej = fopen(namebuf, "w");
3542         if (!rej)
3543                 return error("cannot open %s: %s", namebuf, strerror(errno));
3545         /* Normal git tools never deal with .rej, so do not pretend
3546          * this is a git patch by saying --git nor give extended
3547          * headers.  While at it, maybe please "kompare" that wants
3548          * the trailing TAB and some garbage at the end of line ;-).
3549          */
3550         fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
3551                 patch->new_name, patch->new_name);
3552         for (cnt = 1, frag = patch->fragments;
3553              frag;
3554              cnt++, frag = frag->next) {
3555                 if (!frag->rejected) {
3556                         fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);
3557                         continue;
3558                 }
3559                 fprintf(stderr, "Rejected hunk #%d.\n", cnt);
3560                 fprintf(rej, "%.*s", frag->size, frag->patch);
3561                 if (frag->patch[frag->size-1] != '\n')
3562                         fputc('\n', rej);
3563         }
3564         fclose(rej);
3565         return -1;
3568 static int write_out_results(struct patch *list, int skipped_patch)
3570         int phase;
3571         int errs = 0;
3572         struct patch *l;
3574         if (!list && !skipped_patch)
3575                 return error("No changes");
3577         for (phase = 0; phase < 2; phase++) {
3578                 l = list;
3579                 while (l) {
3580                         if (l->rejected)
3581                                 errs = 1;
3582                         else {
3583                                 write_out_one_result(l, phase);
3584                                 if (phase == 1 && write_out_one_reject(l))
3585                                         errs = 1;
3586                         }
3587                         l = l->next;
3588                 }
3589         }
3590         return errs;
3593 static struct lock_file lock_file;
3595 static struct string_list limit_by_name;
3596 static int has_include;
3597 static void add_name_limit(const char *name, int exclude)
3599         struct string_list_item *it;
3601         it = string_list_append(&limit_by_name, name);
3602         it->util = exclude ? NULL : (void *) 1;
3605 static int use_patch(struct patch *p)
3607         const char *pathname = p->new_name ? p->new_name : p->old_name;
3608         int i;
3610         /* Paths outside are not touched regardless of "--include" */
3611         if (0 < prefix_length) {
3612                 int pathlen = strlen(pathname);
3613                 if (pathlen <= prefix_length ||
3614                     memcmp(prefix, pathname, prefix_length))
3615                         return 0;
3616         }
3618         /* See if it matches any of exclude/include rule */
3619         for (i = 0; i < limit_by_name.nr; i++) {
3620                 struct string_list_item *it = &limit_by_name.items[i];
3621                 if (!fnmatch(it->string, pathname, 0))
3622                         return (it->util != NULL);
3623         }
3625         /*
3626          * If we had any include, a path that does not match any rule is
3627          * not used.  Otherwise, we saw bunch of exclude rules (or none)
3628          * and such a path is used.
3629          */
3630         return !has_include;
3634 static void prefix_one(char **name)
3636         char *old_name = *name;
3637         if (!old_name)
3638                 return;
3639         *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
3640         free(old_name);
3643 static void prefix_patches(struct patch *p)
3645         if (!prefix || p->is_toplevel_relative)
3646                 return;
3647         for ( ; p; p = p->next) {
3648                 if (p->new_name == p->old_name) {
3649                         char *prefixed = p->new_name;
3650                         prefix_one(&prefixed);
3651                         p->new_name = p->old_name = prefixed;
3652                 }
3653                 else {
3654                         prefix_one(&p->new_name);
3655                         prefix_one(&p->old_name);
3656                 }
3657         }
3660 #define INACCURATE_EOF  (1<<0)
3661 #define RECOUNT         (1<<1)
3663 static int apply_patch(int fd, const char *filename, int options)
3665         size_t offset;
3666         struct strbuf buf = STRBUF_INIT;
3667         struct patch *list = NULL, **listp = &list;
3668         int skipped_patch = 0;
3670         /* FIXME - memory leak when using multiple patch files as inputs */
3671         memset(&fn_table, 0, sizeof(struct string_list));
3672         patch_input_file = filename;
3673         read_patch_file(&buf, fd);
3674         offset = 0;
3675         while (offset < buf.len) {
3676                 struct patch *patch;
3677                 int nr;
3679                 patch = xcalloc(1, sizeof(*patch));
3680                 patch->inaccurate_eof = !!(options & INACCURATE_EOF);
3681                 patch->recount =  !!(options & RECOUNT);
3682                 nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
3683                 if (nr < 0)
3684                         break;
3685                 if (apply_in_reverse)
3686                         reverse_patches(patch);
3687                 if (prefix)
3688                         prefix_patches(patch);
3689                 if (use_patch(patch)) {
3690                         patch_stats(patch);
3691                         *listp = patch;
3692                         listp = &patch->next;
3693                 }
3694                 else {
3695                         /* perhaps free it a bit better? */
3696                         free(patch);
3697                         skipped_patch++;
3698                 }
3699                 offset += nr;
3700         }
3702         if (whitespace_error && (ws_error_action == die_on_ws_error))
3703                 apply = 0;
3705         update_index = check_index && apply;
3706         if (update_index && newfd < 0)
3707                 newfd = hold_locked_index(&lock_file, 1);
3709         if (check_index) {
3710                 if (read_cache() < 0)
3711                         die("unable to read index file");
3712         }
3714         if ((check || apply) &&
3715             check_patch_list(list) < 0 &&
3716             !apply_with_reject)
3717                 exit(1);
3719         if (apply && write_out_results(list, skipped_patch))
3720                 exit(1);
3722         if (fake_ancestor)
3723                 build_fake_ancestor(list, fake_ancestor);
3725         if (diffstat)
3726                 stat_patch_list(list);
3728         if (numstat)
3729                 numstat_patch_list(list);
3731         if (summary)
3732                 summary_patch_list(list);
3734         strbuf_release(&buf);
3735         return 0;
3738 static int git_apply_config(const char *var, const char *value, void *cb)
3740         if (!strcmp(var, "apply.whitespace"))
3741                 return git_config_string(&apply_default_whitespace, var, value);
3742         else if (!strcmp(var, "apply.ignorewhitespace"))
3743                 return git_config_string(&apply_default_ignorewhitespace, var, value);
3744         return git_default_config(var, value, cb);
3747 static int option_parse_exclude(const struct option *opt,
3748                                 const char *arg, int unset)
3750         add_name_limit(arg, 1);
3751         return 0;
3754 static int option_parse_include(const struct option *opt,
3755                                 const char *arg, int unset)
3757         add_name_limit(arg, 0);
3758         has_include = 1;
3759         return 0;
3762 static int option_parse_p(const struct option *opt,
3763                           const char *arg, int unset)
3765         p_value = atoi(arg);
3766         p_value_known = 1;
3767         return 0;
3770 static int option_parse_z(const struct option *opt,
3771                           const char *arg, int unset)
3773         if (unset)
3774                 line_termination = '\n';
3775         else
3776                 line_termination = 0;
3777         return 0;
3780 static int option_parse_space_change(const struct option *opt,
3781                           const char *arg, int unset)
3783         if (unset)
3784                 ws_ignore_action = ignore_ws_none;
3785         else
3786                 ws_ignore_action = ignore_ws_change;
3787         return 0;
3790 static int option_parse_whitespace(const struct option *opt,
3791                                    const char *arg, int unset)
3793         const char **whitespace_option = opt->value;
3795         *whitespace_option = arg;
3796         parse_whitespace_option(arg);
3797         return 0;
3800 static int option_parse_directory(const struct option *opt,
3801                                   const char *arg, int unset)
3803         root_len = strlen(arg);
3804         if (root_len && arg[root_len - 1] != '/') {
3805                 char *new_root;
3806                 root = new_root = xmalloc(root_len + 2);
3807                 strcpy(new_root, arg);
3808                 strcpy(new_root + root_len++, "/");
3809         } else
3810                 root = arg;
3811         return 0;
3814 int cmd_apply(int argc, const char **argv, const char *prefix_)
3816         int i;
3817         int errs = 0;
3818         int is_not_gitdir = !startup_info->have_repository;
3819         int binary;
3820         int force_apply = 0;
3822         const char *whitespace_option = NULL;
3824         struct option builtin_apply_options[] = {
3825                 { OPTION_CALLBACK, 0, "exclude", NULL, "path",
3826                         "don't apply changes matching the given path",
3827                         0, option_parse_exclude },
3828                 { OPTION_CALLBACK, 0, "include", NULL, "path",
3829                         "apply changes matching the given path",
3830                         0, option_parse_include },
3831                 { OPTION_CALLBACK, 'p', NULL, NULL, "num",
3832                         "remove <num> leading slashes from traditional diff paths",
3833                         0, option_parse_p },
3834                 OPT_BOOLEAN(0, "no-add", &no_add,
3835                         "ignore additions made by the patch"),
3836                 OPT_BOOLEAN(0, "stat", &diffstat,
3837                         "instead of applying the patch, output diffstat for the input"),
3838                 { OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,
3839                   NULL, "old option, now no-op",
3840                   PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3841                 { OPTION_BOOLEAN, 0, "binary", &binary,
3842                   NULL, "old option, now no-op",
3843                   PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3844                 OPT_BOOLEAN(0, "numstat", &numstat,
3845                         "shows number of added and deleted lines in decimal notation"),
3846                 OPT_BOOLEAN(0, "summary", &summary,
3847                         "instead of applying the patch, output a summary for the input"),
3848                 OPT_BOOLEAN(0, "check", &check,
3849                         "instead of applying the patch, see if the patch is applicable"),
3850                 OPT_BOOLEAN(0, "index", &check_index,
3851                         "make sure the patch is applicable to the current index"),
3852                 OPT_BOOLEAN(0, "cached", &cached,
3853                         "apply a patch without touching the working tree"),
3854                 OPT_BOOLEAN(0, "apply", &force_apply,
3855                         "also apply the patch (use with --stat/--summary/--check)"),
3856                 OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
3857                         "build a temporary index based on embedded index information"),
3858                 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
3859                         "paths are separated with NUL character",
3860                         PARSE_OPT_NOARG, option_parse_z },
3861                 OPT_INTEGER('C', NULL, &p_context,
3862                                 "ensure at least <n> lines of context match"),
3863                 { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, "action",
3864                         "detect new or modified lines that have whitespace errors",
3865                         0, option_parse_whitespace },
3866                 { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
3867                         "ignore changes in whitespace when finding context",
3868                         PARSE_OPT_NOARG, option_parse_space_change },
3869                 { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
3870                         "ignore changes in whitespace when finding context",
3871                         PARSE_OPT_NOARG, option_parse_space_change },
3872                 OPT_BOOLEAN('R', "reverse", &apply_in_reverse,
3873                         "apply the patch in reverse"),
3874                 OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero,
3875                         "don't expect at least one line of context"),
3876                 OPT_BOOLEAN(0, "reject", &apply_with_reject,
3877                         "leave the rejected hunks in corresponding *.rej files"),
3878                 OPT__VERBOSE(&apply_verbosely, "be verbose"),
3879                 OPT_BIT(0, "inaccurate-eof", &options,
3880                         "tolerate incorrectly detected missing new-line at the end of file",
3881                         INACCURATE_EOF),
3882                 OPT_BIT(0, "recount", &options,
3883                         "do not trust the line counts in the hunk headers",
3884                         RECOUNT),
3885                 { OPTION_CALLBACK, 0, "directory", NULL, "root",
3886                         "prepend <root> to all filenames",
3887                         0, option_parse_directory },
3888                 OPT_END()
3889         };
3891         prefix = prefix_;
3892         prefix_length = prefix ? strlen(prefix) : 0;
3893         git_config(git_apply_config, NULL);
3894         if (apply_default_whitespace)
3895                 parse_whitespace_option(apply_default_whitespace);
3896         if (apply_default_ignorewhitespace)
3897                 parse_ignorewhitespace_option(apply_default_ignorewhitespace);
3899         argc = parse_options(argc, argv, prefix, builtin_apply_options,
3900                         apply_usage, 0);
3902         if (apply_with_reject)
3903                 apply = apply_verbosely = 1;
3904         if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))
3905                 apply = 0;
3906         if (check_index && is_not_gitdir)
3907                 die("--index outside a repository");
3908         if (cached) {
3909                 if (is_not_gitdir)
3910                         die("--cached outside a repository");
3911                 check_index = 1;
3912         }
3913         for (i = 0; i < argc; i++) {
3914                 const char *arg = argv[i];
3915                 int fd;
3917                 if (!strcmp(arg, "-")) {
3918                         errs |= apply_patch(0, "<stdin>", options);
3919                         read_stdin = 0;
3920                         continue;
3921                 } else if (0 < prefix_length)
3922                         arg = prefix_filename(prefix, prefix_length, arg);
3924                 fd = open(arg, O_RDONLY);
3925                 if (fd < 0)
3926                         die_errno("can't open patch '%s'", arg);
3927                 read_stdin = 0;
3928                 set_default_whitespace_mode(whitespace_option);
3929                 errs |= apply_patch(fd, arg, options);
3930                 close(fd);
3931         }
3932         set_default_whitespace_mode(whitespace_option);
3933         if (read_stdin)
3934                 errs |= apply_patch(0, "<stdin>", options);
3935         if (whitespace_error) {
3936                 if (squelch_whitespace_errors &&
3937                     squelch_whitespace_errors < whitespace_error) {
3938                         int squelched =
3939                                 whitespace_error - squelch_whitespace_errors;
3940                         warning("squelched %d "
3941                                 "whitespace error%s",
3942                                 squelched,
3943                                 squelched == 1 ? "" : "s");
3944                 }
3945                 if (ws_error_action == die_on_ws_error)
3946                         die("%d line%s add%s whitespace errors.",
3947                             whitespace_error,
3948                             whitespace_error == 1 ? "" : "s",
3949                             whitespace_error == 1 ? "s" : "");
3950                 if (applied_after_fixing_ws && apply)
3951                         warning("%d line%s applied after"
3952                                 " fixing whitespace errors.",
3953                                 applied_after_fixing_ws,
3954                                 applied_after_fixing_ws == 1 ? "" : "s");
3955                 else if (whitespace_error)
3956                         warning("%d line%s add%s whitespace errors.",
3957                                 whitespace_error,
3958                                 whitespace_error == 1 ? "" : "s",
3959                                 whitespace_error == 1 ? "s" : "");
3960         }
3962         if (update_index) {
3963                 if (write_cache(newfd, active_cache, active_nr) ||
3964                     commit_locked_index(&lock_file))
3965                         die("Unable to write new index file");
3966         }
3968         return !!errs;