Merge branch 'nb/gnome-keyring-build'
[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 "lockfile.h"
11 #include "cache-tree.h"
12 #include "quote.h"
13 #include "blob.h"
14 #include "delta.h"
15 #include "builtin.h"
16 #include "string-list.h"
17 #include "dir.h"
18 #include "diff.h"
19 #include "parse-options.h"
20 #include "xdiff-interface.h"
21 #include "ll-merge.h"
22 #include "rerere.h"
23
24 enum ws_error_action {
25         nowarn_ws_error,
26         warn_on_ws_error,
27         die_on_ws_error,
28         correct_ws_error
29 };
30
31
32 enum ws_ignore {
33         ignore_ws_none,
34         ignore_ws_change
35 };
36
37 /*
38  * We need to keep track of how symlinks in the preimage are
39  * manipulated by the patches.  A patch to add a/b/c where a/b
40  * is a symlink should not be allowed to affect the directory
41  * the symlink points at, but if the same patch removes a/b,
42  * it is perfectly fine, as the patch removes a/b to make room
43  * to create a directory a/b so that a/b/c can be created.
44  *
45  * See also "struct string_list symlink_changes" in "struct
46  * apply_state".
47  */
48 #define SYMLINK_GOES_AWAY 01
49 #define SYMLINK_IN_RESULT 02
50
51 struct apply_state {
52         const char *prefix;
53         int prefix_length;
54
55         /* These are lock_file related */
56         struct lock_file *lock_file;
57         int newfd;
58
59         /* These control what gets looked at and modified */
60         int apply; /* this is not a dry-run */
61         int cached; /* apply to the index only */
62         int check; /* preimage must match working tree, don't actually apply */
63         int check_index; /* preimage must match the indexed version */
64         int update_index; /* check_index && apply */
65
66         /* These control cosmetic aspect of the output */
67         int diffstat; /* just show a diffstat, and don't actually apply */
68         int numstat; /* just show a numeric diffstat, and don't actually apply */
69         int summary; /* just report creation, deletion, etc, and don't actually apply */
70
71         /* These boolean parameters control how the apply is done */
72         int allow_overlap;
73         int apply_in_reverse;
74         int apply_with_reject;
75         int apply_verbosely;
76         int no_add;
77         int threeway;
78         int unidiff_zero;
79         int unsafe_paths;
80
81         /* Other non boolean parameters */
82         const char *fake_ancestor;
83         const char *patch_input_file;
84         int line_termination;
85         struct strbuf root;
86         int p_value;
87         int p_value_known;
88         unsigned int p_context;
89
90         /* Exclude and include path parameters */
91         struct string_list limit_by_name;
92         int has_include;
93
94         /* Various "current state" */
95         int linenr; /* current line number */
96         struct string_list symlink_changes; /* we have to track symlinks */
97
98         /*
99          * For "diff-stat" like behaviour, we keep track of the biggest change
100          * we've seen, and the longest filename. That allows us to do simple
101          * scaling.
102          */
103         int max_change;
104         int max_len;
105
106         /*
107          * Records filenames that have been touched, in order to handle
108          * the case where more than one patches touch the same file.
109          */
110         struct string_list fn_table;
111
112         /* These control whitespace errors */
113         enum ws_error_action ws_error_action;
114         enum ws_ignore ws_ignore_action;
115         const char *whitespace_option;
116         int whitespace_error;
117         int squelch_whitespace_errors;
118         int applied_after_fixing_ws;
119 };
120
121 static const char * const apply_usage[] = {
122         N_("git apply [<options>] [<patch>...]"),
123         NULL
124 };
125
126 static void parse_whitespace_option(struct apply_state *state, const char *option)
127 {
128         if (!option) {
129                 state->ws_error_action = warn_on_ws_error;
130                 return;
131         }
132         if (!strcmp(option, "warn")) {
133                 state->ws_error_action = warn_on_ws_error;
134                 return;
135         }
136         if (!strcmp(option, "nowarn")) {
137                 state->ws_error_action = nowarn_ws_error;
138                 return;
139         }
140         if (!strcmp(option, "error")) {
141                 state->ws_error_action = die_on_ws_error;
142                 return;
143         }
144         if (!strcmp(option, "error-all")) {
145                 state->ws_error_action = die_on_ws_error;
146                 state->squelch_whitespace_errors = 0;
147                 return;
148         }
149         if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
150                 state->ws_error_action = correct_ws_error;
151                 return;
152         }
153         die(_("unrecognized whitespace option '%s'"), option);
154 }
155
156 static void parse_ignorewhitespace_option(struct apply_state *state,
157                                           const char *option)
158 {
159         if (!option || !strcmp(option, "no") ||
160             !strcmp(option, "false") || !strcmp(option, "never") ||
161             !strcmp(option, "none")) {
162                 state->ws_ignore_action = ignore_ws_none;
163                 return;
164         }
165         if (!strcmp(option, "change")) {
166                 state->ws_ignore_action = ignore_ws_change;
167                 return;
168         }
169         die(_("unrecognized whitespace ignore option '%s'"), option);
170 }
171
172 static void set_default_whitespace_mode(struct apply_state *state)
173 {
174         if (!state->whitespace_option && !apply_default_whitespace)
175                 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
176 }
177
178 /*
179  * This represents one "hunk" from a patch, starting with
180  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
181  * patch text is pointed at by patch, and its byte length
182  * is stored in size.  leading and trailing are the number
183  * of context lines.
184  */
185 struct fragment {
186         unsigned long leading, trailing;
187         unsigned long oldpos, oldlines;
188         unsigned long newpos, newlines;
189         /*
190          * 'patch' is usually borrowed from buf in apply_patch(),
191          * but some codepaths store an allocated buffer.
192          */
193         const char *patch;
194         unsigned free_patch:1,
195                 rejected:1;
196         int size;
197         int linenr;
198         struct fragment *next;
199 };
200
201 /*
202  * When dealing with a binary patch, we reuse "leading" field
203  * to store the type of the binary hunk, either deflated "delta"
204  * or deflated "literal".
205  */
206 #define binary_patch_method leading
207 #define BINARY_DELTA_DEFLATED   1
208 #define BINARY_LITERAL_DEFLATED 2
209
210 /*
211  * This represents a "patch" to a file, both metainfo changes
212  * such as creation/deletion, filemode and content changes represented
213  * as a series of fragments.
214  */
215 struct patch {
216         char *new_name, *old_name, *def_name;
217         unsigned int old_mode, new_mode;
218         int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
219         int rejected;
220         unsigned ws_rule;
221         int lines_added, lines_deleted;
222         int score;
223         unsigned int is_toplevel_relative:1;
224         unsigned int inaccurate_eof:1;
225         unsigned int is_binary:1;
226         unsigned int is_copy:1;
227         unsigned int is_rename:1;
228         unsigned int recount:1;
229         unsigned int conflicted_threeway:1;
230         unsigned int direct_to_threeway:1;
231         struct fragment *fragments;
232         char *result;
233         size_t resultsize;
234         char old_sha1_prefix[41];
235         char new_sha1_prefix[41];
236         struct patch *next;
237
238         /* three-way fallback result */
239         struct object_id threeway_stage[3];
240 };
241
242 static void free_fragment_list(struct fragment *list)
243 {
244         while (list) {
245                 struct fragment *next = list->next;
246                 if (list->free_patch)
247                         free((char *)list->patch);
248                 free(list);
249                 list = next;
250         }
251 }
252
253 static void free_patch(struct patch *patch)
254 {
255         free_fragment_list(patch->fragments);
256         free(patch->def_name);
257         free(patch->old_name);
258         free(patch->new_name);
259         free(patch->result);
260         free(patch);
261 }
262
263 static void free_patch_list(struct patch *list)
264 {
265         while (list) {
266                 struct patch *next = list->next;
267                 free_patch(list);
268                 list = next;
269         }
270 }
271
272 /*
273  * A line in a file, len-bytes long (includes the terminating LF,
274  * except for an incomplete line at the end if the file ends with
275  * one), and its contents hashes to 'hash'.
276  */
277 struct line {
278         size_t len;
279         unsigned hash : 24;
280         unsigned flag : 8;
281 #define LINE_COMMON     1
282 #define LINE_PATCHED    2
283 };
284
285 /*
286  * This represents a "file", which is an array of "lines".
287  */
288 struct image {
289         char *buf;
290         size_t len;
291         size_t nr;
292         size_t alloc;
293         struct line *line_allocated;
294         struct line *line;
295 };
296
297 static uint32_t hash_line(const char *cp, size_t len)
298 {
299         size_t i;
300         uint32_t h;
301         for (i = 0, h = 0; i < len; i++) {
302                 if (!isspace(cp[i])) {
303                         h = h * 3 + (cp[i] & 0xff);
304                 }
305         }
306         return h;
307 }
308
309 /*
310  * Compare lines s1 of length n1 and s2 of length n2, ignoring
311  * whitespace difference. Returns 1 if they match, 0 otherwise
312  */
313 static int fuzzy_matchlines(const char *s1, size_t n1,
314                             const char *s2, size_t n2)
315 {
316         const char *last1 = s1 + n1 - 1;
317         const char *last2 = s2 + n2 - 1;
318         int result = 0;
319
320         /* ignore line endings */
321         while ((*last1 == '\r') || (*last1 == '\n'))
322                 last1--;
323         while ((*last2 == '\r') || (*last2 == '\n'))
324                 last2--;
325
326         /* skip leading whitespaces, if both begin with whitespace */
327         if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
328                 while (isspace(*s1) && (s1 <= last1))
329                         s1++;
330                 while (isspace(*s2) && (s2 <= last2))
331                         s2++;
332         }
333         /* early return if both lines are empty */
334         if ((s1 > last1) && (s2 > last2))
335                 return 1;
336         while (!result) {
337                 result = *s1++ - *s2++;
338                 /*
339                  * Skip whitespace inside. We check for whitespace on
340                  * both buffers because we don't want "a b" to match
341                  * "ab"
342                  */
343                 if (isspace(*s1) && isspace(*s2)) {
344                         while (isspace(*s1) && s1 <= last1)
345                                 s1++;
346                         while (isspace(*s2) && s2 <= last2)
347                                 s2++;
348                 }
349                 /*
350                  * If we reached the end on one side only,
351                  * lines don't match
352                  */
353                 if (
354                     ((s2 > last2) && (s1 <= last1)) ||
355                     ((s1 > last1) && (s2 <= last2)))
356                         return 0;
357                 if ((s1 > last1) && (s2 > last2))
358                         break;
359         }
360
361         return !result;
362 }
363
364 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
365 {
366         ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
367         img->line_allocated[img->nr].len = len;
368         img->line_allocated[img->nr].hash = hash_line(bol, len);
369         img->line_allocated[img->nr].flag = flag;
370         img->nr++;
371 }
372
373 /*
374  * "buf" has the file contents to be patched (read from various sources).
375  * attach it to "image" and add line-based index to it.
376  * "image" now owns the "buf".
377  */
378 static void prepare_image(struct image *image, char *buf, size_t len,
379                           int prepare_linetable)
380 {
381         const char *cp, *ep;
382
383         memset(image, 0, sizeof(*image));
384         image->buf = buf;
385         image->len = len;
386
387         if (!prepare_linetable)
388                 return;
389
390         ep = image->buf + image->len;
391         cp = image->buf;
392         while (cp < ep) {
393                 const char *next;
394                 for (next = cp; next < ep && *next != '\n'; next++)
395                         ;
396                 if (next < ep)
397                         next++;
398                 add_line_info(image, cp, next - cp, 0);
399                 cp = next;
400         }
401         image->line = image->line_allocated;
402 }
403
404 static void clear_image(struct image *image)
405 {
406         free(image->buf);
407         free(image->line_allocated);
408         memset(image, 0, sizeof(*image));
409 }
410
411 /* fmt must contain _one_ %s and no other substitution */
412 static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
413 {
414         struct strbuf sb = STRBUF_INIT;
415
416         if (patch->old_name && patch->new_name &&
417             strcmp(patch->old_name, patch->new_name)) {
418                 quote_c_style(patch->old_name, &sb, NULL, 0);
419                 strbuf_addstr(&sb, " => ");
420                 quote_c_style(patch->new_name, &sb, NULL, 0);
421         } else {
422                 const char *n = patch->new_name;
423                 if (!n)
424                         n = patch->old_name;
425                 quote_c_style(n, &sb, NULL, 0);
426         }
427         fprintf(output, fmt, sb.buf);
428         fputc('\n', output);
429         strbuf_release(&sb);
430 }
431
432 #define SLOP (16)
433
434 static void read_patch_file(struct strbuf *sb, int fd)
435 {
436         if (strbuf_read(sb, fd, 0) < 0)
437                 die_errno("git apply: failed to read");
438
439         /*
440          * Make sure that we have some slop in the buffer
441          * so that we can do speculative "memcmp" etc, and
442          * see to it that it is NUL-filled.
443          */
444         strbuf_grow(sb, SLOP);
445         memset(sb->buf + sb->len, 0, SLOP);
446 }
447
448 static unsigned long linelen(const char *buffer, unsigned long size)
449 {
450         unsigned long len = 0;
451         while (size--) {
452                 len++;
453                 if (*buffer++ == '\n')
454                         break;
455         }
456         return len;
457 }
458
459 static int is_dev_null(const char *str)
460 {
461         return skip_prefix(str, "/dev/null", &str) && isspace(*str);
462 }
463
464 #define TERM_SPACE      1
465 #define TERM_TAB        2
466
467 static int name_terminate(int c, int terminate)
468 {
469         if (c == ' ' && !(terminate & TERM_SPACE))
470                 return 0;
471         if (c == '\t' && !(terminate & TERM_TAB))
472                 return 0;
473
474         return 1;
475 }
476
477 /* remove double slashes to make --index work with such filenames */
478 static char *squash_slash(char *name)
479 {
480         int i = 0, j = 0;
481
482         if (!name)
483                 return NULL;
484
485         while (name[i]) {
486                 if ((name[j++] = name[i++]) == '/')
487                         while (name[i] == '/')
488                                 i++;
489         }
490         name[j] = '\0';
491         return name;
492 }
493
494 static char *find_name_gnu(struct apply_state *state,
495                            const char *line,
496                            const char *def,
497                            int p_value)
498 {
499         struct strbuf name = STRBUF_INIT;
500         char *cp;
501
502         /*
503          * Proposed "new-style" GNU patch/diff format; see
504          * http://marc.info/?l=git&m=112927316408690&w=2
505          */
506         if (unquote_c_style(&name, line, NULL)) {
507                 strbuf_release(&name);
508                 return NULL;
509         }
510
511         for (cp = name.buf; p_value; p_value--) {
512                 cp = strchr(cp, '/');
513                 if (!cp) {
514                         strbuf_release(&name);
515                         return NULL;
516                 }
517                 cp++;
518         }
519
520         strbuf_remove(&name, 0, cp - name.buf);
521         if (state->root.len)
522                 strbuf_insert(&name, 0, state->root.buf, state->root.len);
523         return squash_slash(strbuf_detach(&name, NULL));
524 }
525
526 static size_t sane_tz_len(const char *line, size_t len)
527 {
528         const char *tz, *p;
529
530         if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
531                 return 0;
532         tz = line + len - strlen(" +0500");
533
534         if (tz[1] != '+' && tz[1] != '-')
535                 return 0;
536
537         for (p = tz + 2; p != line + len; p++)
538                 if (!isdigit(*p))
539                         return 0;
540
541         return line + len - tz;
542 }
543
544 static size_t tz_with_colon_len(const char *line, size_t len)
545 {
546         const char *tz, *p;
547
548         if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
549                 return 0;
550         tz = line + len - strlen(" +08:00");
551
552         if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
553                 return 0;
554         p = tz + 2;
555         if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
556             !isdigit(*p++) || !isdigit(*p++))
557                 return 0;
558
559         return line + len - tz;
560 }
561
562 static size_t date_len(const char *line, size_t len)
563 {
564         const char *date, *p;
565
566         if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
567                 return 0;
568         p = date = line + len - strlen("72-02-05");
569
570         if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
571             !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
572             !isdigit(*p++) || !isdigit(*p++))   /* Not a date. */
573                 return 0;
574
575         if (date - line >= strlen("19") &&
576             isdigit(date[-1]) && isdigit(date[-2]))     /* 4-digit year */
577                 date -= strlen("19");
578
579         return line + len - date;
580 }
581
582 static size_t short_time_len(const char *line, size_t len)
583 {
584         const char *time, *p;
585
586         if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
587                 return 0;
588         p = time = line + len - strlen(" 07:01:32");
589
590         /* Permit 1-digit hours? */
591         if (*p++ != ' ' ||
592             !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
593             !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
594             !isdigit(*p++) || !isdigit(*p++))   /* Not a time. */
595                 return 0;
596
597         return line + len - time;
598 }
599
600 static size_t fractional_time_len(const char *line, size_t len)
601 {
602         const char *p;
603         size_t n;
604
605         /* Expected format: 19:41:17.620000023 */
606         if (!len || !isdigit(line[len - 1]))
607                 return 0;
608         p = line + len - 1;
609
610         /* Fractional seconds. */
611         while (p > line && isdigit(*p))
612                 p--;
613         if (*p != '.')
614                 return 0;
615
616         /* Hours, minutes, and whole seconds. */
617         n = short_time_len(line, p - line);
618         if (!n)
619                 return 0;
620
621         return line + len - p + n;
622 }
623
624 static size_t trailing_spaces_len(const char *line, size_t len)
625 {
626         const char *p;
627
628         /* Expected format: ' ' x (1 or more)  */
629         if (!len || line[len - 1] != ' ')
630                 return 0;
631
632         p = line + len;
633         while (p != line) {
634                 p--;
635                 if (*p != ' ')
636                         return line + len - (p + 1);
637         }
638
639         /* All spaces! */
640         return len;
641 }
642
643 static size_t diff_timestamp_len(const char *line, size_t len)
644 {
645         const char *end = line + len;
646         size_t n;
647
648         /*
649          * Posix: 2010-07-05 19:41:17
650          * GNU: 2010-07-05 19:41:17.620000023 -0500
651          */
652
653         if (!isdigit(end[-1]))
654                 return 0;
655
656         n = sane_tz_len(line, end - line);
657         if (!n)
658                 n = tz_with_colon_len(line, end - line);
659         end -= n;
660
661         n = short_time_len(line, end - line);
662         if (!n)
663                 n = fractional_time_len(line, end - line);
664         end -= n;
665
666         n = date_len(line, end - line);
667         if (!n) /* No date.  Too bad. */
668                 return 0;
669         end -= n;
670
671         if (end == line)        /* No space before date. */
672                 return 0;
673         if (end[-1] == '\t') {  /* Success! */
674                 end--;
675                 return line + len - end;
676         }
677         if (end[-1] != ' ')     /* No space before date. */
678                 return 0;
679
680         /* Whitespace damage. */
681         end -= trailing_spaces_len(line, end - line);
682         return line + len - end;
683 }
684
685 static char *find_name_common(struct apply_state *state,
686                               const char *line,
687                               const char *def,
688                               int p_value,
689                               const char *end,
690                               int terminate)
691 {
692         int len;
693         const char *start = NULL;
694
695         if (p_value == 0)
696                 start = line;
697         while (line != end) {
698                 char c = *line;
699
700                 if (!end && isspace(c)) {
701                         if (c == '\n')
702                                 break;
703                         if (name_terminate(c, terminate))
704                                 break;
705                 }
706                 line++;
707                 if (c == '/' && !--p_value)
708                         start = line;
709         }
710         if (!start)
711                 return squash_slash(xstrdup_or_null(def));
712         len = line - start;
713         if (!len)
714                 return squash_slash(xstrdup_or_null(def));
715
716         /*
717          * Generally we prefer the shorter name, especially
718          * if the other one is just a variation of that with
719          * something else tacked on to the end (ie "file.orig"
720          * or "file~").
721          */
722         if (def) {
723                 int deflen = strlen(def);
724                 if (deflen < len && !strncmp(start, def, deflen))
725                         return squash_slash(xstrdup(def));
726         }
727
728         if (state->root.len) {
729                 char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
730                 return squash_slash(ret);
731         }
732
733         return squash_slash(xmemdupz(start, len));
734 }
735
736 static char *find_name(struct apply_state *state,
737                        const char *line,
738                        char *def,
739                        int p_value,
740                        int terminate)
741 {
742         if (*line == '"') {
743                 char *name = find_name_gnu(state, line, def, p_value);
744                 if (name)
745                         return name;
746         }
747
748         return find_name_common(state, line, def, p_value, NULL, terminate);
749 }
750
751 static char *find_name_traditional(struct apply_state *state,
752                                    const char *line,
753                                    char *def,
754                                    int p_value)
755 {
756         size_t len;
757         size_t date_len;
758
759         if (*line == '"') {
760                 char *name = find_name_gnu(state, line, def, p_value);
761                 if (name)
762                         return name;
763         }
764
765         len = strchrnul(line, '\n') - line;
766         date_len = diff_timestamp_len(line, len);
767         if (!date_len)
768                 return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
769         len -= date_len;
770
771         return find_name_common(state, line, def, p_value, line + len, 0);
772 }
773
774 static int count_slashes(const char *cp)
775 {
776         int cnt = 0;
777         char ch;
778
779         while ((ch = *cp++))
780                 if (ch == '/')
781                         cnt++;
782         return cnt;
783 }
784
785 /*
786  * Given the string after "--- " or "+++ ", guess the appropriate
787  * p_value for the given patch.
788  */
789 static int guess_p_value(struct apply_state *state, const char *nameline)
790 {
791         char *name, *cp;
792         int val = -1;
793
794         if (is_dev_null(nameline))
795                 return -1;
796         name = find_name_traditional(state, nameline, NULL, 0);
797         if (!name)
798                 return -1;
799         cp = strchr(name, '/');
800         if (!cp)
801                 val = 0;
802         else if (state->prefix) {
803                 /*
804                  * Does it begin with "a/$our-prefix" and such?  Then this is
805                  * very likely to apply to our directory.
806                  */
807                 if (!strncmp(name, state->prefix, state->prefix_length))
808                         val = count_slashes(state->prefix);
809                 else {
810                         cp++;
811                         if (!strncmp(cp, state->prefix, state->prefix_length))
812                                 val = count_slashes(state->prefix) + 1;
813                 }
814         }
815         free(name);
816         return val;
817 }
818
819 /*
820  * Does the ---/+++ line have the POSIX timestamp after the last HT?
821  * GNU diff puts epoch there to signal a creation/deletion event.  Is
822  * this such a timestamp?
823  */
824 static int has_epoch_timestamp(const char *nameline)
825 {
826         /*
827          * We are only interested in epoch timestamp; any non-zero
828          * fraction cannot be one, hence "(\.0+)?" in the regexp below.
829          * For the same reason, the date must be either 1969-12-31 or
830          * 1970-01-01, and the seconds part must be "00".
831          */
832         const char stamp_regexp[] =
833                 "^(1969-12-31|1970-01-01)"
834                 " "
835                 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
836                 " "
837                 "([-+][0-2][0-9]:?[0-5][0-9])\n";
838         const char *timestamp = NULL, *cp, *colon;
839         static regex_t *stamp;
840         regmatch_t m[10];
841         int zoneoffset;
842         int hourminute;
843         int status;
844
845         for (cp = nameline; *cp != '\n'; cp++) {
846                 if (*cp == '\t')
847                         timestamp = cp + 1;
848         }
849         if (!timestamp)
850                 return 0;
851         if (!stamp) {
852                 stamp = xmalloc(sizeof(*stamp));
853                 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
854                         warning(_("Cannot prepare timestamp regexp %s"),
855                                 stamp_regexp);
856                         return 0;
857                 }
858         }
859
860         status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
861         if (status) {
862                 if (status != REG_NOMATCH)
863                         warning(_("regexec returned %d for input: %s"),
864                                 status, timestamp);
865                 return 0;
866         }
867
868         zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
869         if (*colon == ':')
870                 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
871         else
872                 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
873         if (timestamp[m[3].rm_so] == '-')
874                 zoneoffset = -zoneoffset;
875
876         /*
877          * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
878          * (west of GMT) or 1970-01-01 (east of GMT)
879          */
880         if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
881             (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
882                 return 0;
883
884         hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
885                       strtol(timestamp + 14, NULL, 10) -
886                       zoneoffset);
887
888         return ((zoneoffset < 0 && hourminute == 1440) ||
889                 (0 <= zoneoffset && !hourminute));
890 }
891
892 /*
893  * Get the name etc info from the ---/+++ lines of a traditional patch header
894  *
895  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
896  * files, we can happily check the index for a match, but for creating a
897  * new file we should try to match whatever "patch" does. I have no idea.
898  */
899 static void parse_traditional_patch(struct apply_state *state,
900                                     const char *first,
901                                     const char *second,
902                                     struct patch *patch)
903 {
904         char *name;
905
906         first += 4;     /* skip "--- " */
907         second += 4;    /* skip "+++ " */
908         if (!state->p_value_known) {
909                 int p, q;
910                 p = guess_p_value(state, first);
911                 q = guess_p_value(state, second);
912                 if (p < 0) p = q;
913                 if (0 <= p && p == q) {
914                         state->p_value = p;
915                         state->p_value_known = 1;
916                 }
917         }
918         if (is_dev_null(first)) {
919                 patch->is_new = 1;
920                 patch->is_delete = 0;
921                 name = find_name_traditional(state, second, NULL, state->p_value);
922                 patch->new_name = name;
923         } else if (is_dev_null(second)) {
924                 patch->is_new = 0;
925                 patch->is_delete = 1;
926                 name = find_name_traditional(state, first, NULL, state->p_value);
927                 patch->old_name = name;
928         } else {
929                 char *first_name;
930                 first_name = find_name_traditional(state, first, NULL, state->p_value);
931                 name = find_name_traditional(state, second, first_name, state->p_value);
932                 free(first_name);
933                 if (has_epoch_timestamp(first)) {
934                         patch->is_new = 1;
935                         patch->is_delete = 0;
936                         patch->new_name = name;
937                 } else if (has_epoch_timestamp(second)) {
938                         patch->is_new = 0;
939                         patch->is_delete = 1;
940                         patch->old_name = name;
941                 } else {
942                         patch->old_name = name;
943                         patch->new_name = xstrdup_or_null(name);
944                 }
945         }
946         if (!name)
947                 die(_("unable to find filename in patch at line %d"), state->linenr);
948 }
949
950 static int gitdiff_hdrend(struct apply_state *state,
951                           const char *line,
952                           struct patch *patch)
953 {
954         return -1;
955 }
956
957 /*
958  * We're anal about diff header consistency, to make
959  * sure that we don't end up having strange ambiguous
960  * patches floating around.
961  *
962  * As a result, gitdiff_{old|new}name() will check
963  * their names against any previous information, just
964  * to make sure..
965  */
966 #define DIFF_OLD_NAME 0
967 #define DIFF_NEW_NAME 1
968
969 static void gitdiff_verify_name(struct apply_state *state,
970                                 const char *line,
971                                 int isnull,
972                                 char **name,
973                                 int side)
974 {
975         if (!*name && !isnull) {
976                 *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
977                 return;
978         }
979
980         if (*name) {
981                 int len = strlen(*name);
982                 char *another;
983                 if (isnull)
984                         die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
985                             *name, state->linenr);
986                 another = find_name(state, line, NULL, state->p_value, TERM_TAB);
987                 if (!another || memcmp(another, *name, len + 1))
988                         die((side == DIFF_NEW_NAME) ?
989                             _("git apply: bad git-diff - inconsistent new filename on line %d") :
990                             _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
991                 free(another);
992         } else {
993                 /* expect "/dev/null" */
994                 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
995                         die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
996         }
997 }
998
999 static int gitdiff_oldname(struct apply_state *state,
1000                            const char *line,
1001                            struct patch *patch)
1002 {
1003         gitdiff_verify_name(state, line,
1004                             patch->is_new, &patch->old_name,
1005                             DIFF_OLD_NAME);
1006         return 0;
1007 }
1008
1009 static int gitdiff_newname(struct apply_state *state,
1010                            const char *line,
1011                            struct patch *patch)
1012 {
1013         gitdiff_verify_name(state, line,
1014                             patch->is_delete, &patch->new_name,
1015                             DIFF_NEW_NAME);
1016         return 0;
1017 }
1018
1019 static int gitdiff_oldmode(struct apply_state *state,
1020                            const char *line,
1021                            struct patch *patch)
1022 {
1023         patch->old_mode = strtoul(line, NULL, 8);
1024         return 0;
1025 }
1026
1027 static int gitdiff_newmode(struct apply_state *state,
1028                            const char *line,
1029                            struct patch *patch)
1030 {
1031         patch->new_mode = strtoul(line, NULL, 8);
1032         return 0;
1033 }
1034
1035 static int gitdiff_delete(struct apply_state *state,
1036                           const char *line,
1037                           struct patch *patch)
1038 {
1039         patch->is_delete = 1;
1040         free(patch->old_name);
1041         patch->old_name = xstrdup_or_null(patch->def_name);
1042         return gitdiff_oldmode(state, line, patch);
1043 }
1044
1045 static int gitdiff_newfile(struct apply_state *state,
1046                            const char *line,
1047                            struct patch *patch)
1048 {
1049         patch->is_new = 1;
1050         free(patch->new_name);
1051         patch->new_name = xstrdup_or_null(patch->def_name);
1052         return gitdiff_newmode(state, line, patch);
1053 }
1054
1055 static int gitdiff_copysrc(struct apply_state *state,
1056                            const char *line,
1057                            struct patch *patch)
1058 {
1059         patch->is_copy = 1;
1060         free(patch->old_name);
1061         patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1062         return 0;
1063 }
1064
1065 static int gitdiff_copydst(struct apply_state *state,
1066                            const char *line,
1067                            struct patch *patch)
1068 {
1069         patch->is_copy = 1;
1070         free(patch->new_name);
1071         patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1072         return 0;
1073 }
1074
1075 static int gitdiff_renamesrc(struct apply_state *state,
1076                              const char *line,
1077                              struct patch *patch)
1078 {
1079         patch->is_rename = 1;
1080         free(patch->old_name);
1081         patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1082         return 0;
1083 }
1084
1085 static int gitdiff_renamedst(struct apply_state *state,
1086                              const char *line,
1087                              struct patch *patch)
1088 {
1089         patch->is_rename = 1;
1090         free(patch->new_name);
1091         patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1092         return 0;
1093 }
1094
1095 static int gitdiff_similarity(struct apply_state *state,
1096                               const char *line,
1097                               struct patch *patch)
1098 {
1099         unsigned long val = strtoul(line, NULL, 10);
1100         if (val <= 100)
1101                 patch->score = val;
1102         return 0;
1103 }
1104
1105 static int gitdiff_dissimilarity(struct apply_state *state,
1106                                  const char *line,
1107                                  struct patch *patch)
1108 {
1109         unsigned long val = strtoul(line, NULL, 10);
1110         if (val <= 100)
1111                 patch->score = val;
1112         return 0;
1113 }
1114
1115 static int gitdiff_index(struct apply_state *state,
1116                          const char *line,
1117                          struct patch *patch)
1118 {
1119         /*
1120          * index line is N hexadecimal, "..", N hexadecimal,
1121          * and optional space with octal mode.
1122          */
1123         const char *ptr, *eol;
1124         int len;
1125
1126         ptr = strchr(line, '.');
1127         if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1128                 return 0;
1129         len = ptr - line;
1130         memcpy(patch->old_sha1_prefix, line, len);
1131         patch->old_sha1_prefix[len] = 0;
1132
1133         line = ptr + 2;
1134         ptr = strchr(line, ' ');
1135         eol = strchrnul(line, '\n');
1136
1137         if (!ptr || eol < ptr)
1138                 ptr = eol;
1139         len = ptr - line;
1140
1141         if (40 < len)
1142                 return 0;
1143         memcpy(patch->new_sha1_prefix, line, len);
1144         patch->new_sha1_prefix[len] = 0;
1145         if (*ptr == ' ')
1146                 patch->old_mode = strtoul(ptr+1, NULL, 8);
1147         return 0;
1148 }
1149
1150 /*
1151  * This is normal for a diff that doesn't change anything: we'll fall through
1152  * into the next diff. Tell the parser to break out.
1153  */
1154 static int gitdiff_unrecognized(struct apply_state *state,
1155                                 const char *line,
1156                                 struct patch *patch)
1157 {
1158         return -1;
1159 }
1160
1161 /*
1162  * Skip p_value leading components from "line"; as we do not accept
1163  * absolute paths, return NULL in that case.
1164  */
1165 static const char *skip_tree_prefix(struct apply_state *state,
1166                                     const char *line,
1167                                     int llen)
1168 {
1169         int nslash;
1170         int i;
1171
1172         if (!state->p_value)
1173                 return (llen && line[0] == '/') ? NULL : line;
1174
1175         nslash = state->p_value;
1176         for (i = 0; i < llen; i++) {
1177                 int ch = line[i];
1178                 if (ch == '/' && --nslash <= 0)
1179                         return (i == 0) ? NULL : &line[i + 1];
1180         }
1181         return NULL;
1182 }
1183
1184 /*
1185  * This is to extract the same name that appears on "diff --git"
1186  * line.  We do not find and return anything if it is a rename
1187  * patch, and it is OK because we will find the name elsewhere.
1188  * We need to reliably find name only when it is mode-change only,
1189  * creation or deletion of an empty file.  In any of these cases,
1190  * both sides are the same name under a/ and b/ respectively.
1191  */
1192 static char *git_header_name(struct apply_state *state,
1193                              const char *line,
1194                              int llen)
1195 {
1196         const char *name;
1197         const char *second = NULL;
1198         size_t len, line_len;
1199
1200         line += strlen("diff --git ");
1201         llen -= strlen("diff --git ");
1202
1203         if (*line == '"') {
1204                 const char *cp;
1205                 struct strbuf first = STRBUF_INIT;
1206                 struct strbuf sp = STRBUF_INIT;
1207
1208                 if (unquote_c_style(&first, line, &second))
1209                         goto free_and_fail1;
1210
1211                 /* strip the a/b prefix including trailing slash */
1212                 cp = skip_tree_prefix(state, first.buf, first.len);
1213                 if (!cp)
1214                         goto free_and_fail1;
1215                 strbuf_remove(&first, 0, cp - first.buf);
1216
1217                 /*
1218                  * second points at one past closing dq of name.
1219                  * find the second name.
1220                  */
1221                 while ((second < line + llen) && isspace(*second))
1222                         second++;
1223
1224                 if (line + llen <= second)
1225                         goto free_and_fail1;
1226                 if (*second == '"') {
1227                         if (unquote_c_style(&sp, second, NULL))
1228                                 goto free_and_fail1;
1229                         cp = skip_tree_prefix(state, sp.buf, sp.len);
1230                         if (!cp)
1231                                 goto free_and_fail1;
1232                         /* They must match, otherwise ignore */
1233                         if (strcmp(cp, first.buf))
1234                                 goto free_and_fail1;
1235                         strbuf_release(&sp);
1236                         return strbuf_detach(&first, NULL);
1237                 }
1238
1239                 /* unquoted second */
1240                 cp = skip_tree_prefix(state, second, line + llen - second);
1241                 if (!cp)
1242                         goto free_and_fail1;
1243                 if (line + llen - cp != first.len ||
1244                     memcmp(first.buf, cp, first.len))
1245                         goto free_and_fail1;
1246                 return strbuf_detach(&first, NULL);
1247
1248         free_and_fail1:
1249                 strbuf_release(&first);
1250                 strbuf_release(&sp);
1251                 return NULL;
1252         }
1253
1254         /* unquoted first name */
1255         name = skip_tree_prefix(state, line, llen);
1256         if (!name)
1257                 return NULL;
1258
1259         /*
1260          * since the first name is unquoted, a dq if exists must be
1261          * the beginning of the second name.
1262          */
1263         for (second = name; second < line + llen; second++) {
1264                 if (*second == '"') {
1265                         struct strbuf sp = STRBUF_INIT;
1266                         const char *np;
1267
1268                         if (unquote_c_style(&sp, second, NULL))
1269                                 goto free_and_fail2;
1270
1271                         np = skip_tree_prefix(state, sp.buf, sp.len);
1272                         if (!np)
1273                                 goto free_and_fail2;
1274
1275                         len = sp.buf + sp.len - np;
1276                         if (len < second - name &&
1277                             !strncmp(np, name, len) &&
1278                             isspace(name[len])) {
1279                                 /* Good */
1280                                 strbuf_remove(&sp, 0, np - sp.buf);
1281                                 return strbuf_detach(&sp, NULL);
1282                         }
1283
1284                 free_and_fail2:
1285                         strbuf_release(&sp);
1286                         return NULL;
1287                 }
1288         }
1289
1290         /*
1291          * Accept a name only if it shows up twice, exactly the same
1292          * form.
1293          */
1294         second = strchr(name, '\n');
1295         if (!second)
1296                 return NULL;
1297         line_len = second - name;
1298         for (len = 0 ; ; len++) {
1299                 switch (name[len]) {
1300                 default:
1301                         continue;
1302                 case '\n':
1303                         return NULL;
1304                 case '\t': case ' ':
1305                         /*
1306                          * Is this the separator between the preimage
1307                          * and the postimage pathname?  Again, we are
1308                          * only interested in the case where there is
1309                          * no rename, as this is only to set def_name
1310                          * and a rename patch has the names elsewhere
1311                          * in an unambiguous form.
1312                          */
1313                         if (!name[len + 1])
1314                                 return NULL; /* no postimage name */
1315                         second = skip_tree_prefix(state, name + len + 1,
1316                                                   line_len - (len + 1));
1317                         if (!second)
1318                                 return NULL;
1319                         /*
1320                          * Does len bytes starting at "name" and "second"
1321                          * (that are separated by one HT or SP we just
1322                          * found) exactly match?
1323                          */
1324                         if (second[len] == '\n' && !strncmp(name, second, len))
1325                                 return xmemdupz(name, len);
1326                 }
1327         }
1328 }
1329
1330 /* Verify that we recognize the lines following a git header */
1331 static int parse_git_header(struct apply_state *state,
1332                             const char *line,
1333                             int len,
1334                             unsigned int size,
1335                             struct patch *patch)
1336 {
1337         unsigned long offset;
1338
1339         /* A git diff has explicit new/delete information, so we don't guess */
1340         patch->is_new = 0;
1341         patch->is_delete = 0;
1342
1343         /*
1344          * Some things may not have the old name in the
1345          * rest of the headers anywhere (pure mode changes,
1346          * or removing or adding empty files), so we get
1347          * the default name from the header.
1348          */
1349         patch->def_name = git_header_name(state, line, len);
1350         if (patch->def_name && state->root.len) {
1351                 char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1352                 free(patch->def_name);
1353                 patch->def_name = s;
1354         }
1355
1356         line += len;
1357         size -= len;
1358         state->linenr++;
1359         for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1360                 static const struct opentry {
1361                         const char *str;
1362                         int (*fn)(struct apply_state *, const char *, struct patch *);
1363                 } optable[] = {
1364                         { "@@ -", gitdiff_hdrend },
1365                         { "--- ", gitdiff_oldname },
1366                         { "+++ ", gitdiff_newname },
1367                         { "old mode ", gitdiff_oldmode },
1368                         { "new mode ", gitdiff_newmode },
1369                         { "deleted file mode ", gitdiff_delete },
1370                         { "new file mode ", gitdiff_newfile },
1371                         { "copy from ", gitdiff_copysrc },
1372                         { "copy to ", gitdiff_copydst },
1373                         { "rename old ", gitdiff_renamesrc },
1374                         { "rename new ", gitdiff_renamedst },
1375                         { "rename from ", gitdiff_renamesrc },
1376                         { "rename to ", gitdiff_renamedst },
1377                         { "similarity index ", gitdiff_similarity },
1378                         { "dissimilarity index ", gitdiff_dissimilarity },
1379                         { "index ", gitdiff_index },
1380                         { "", gitdiff_unrecognized },
1381                 };
1382                 int i;
1383
1384                 len = linelen(line, size);
1385                 if (!len || line[len-1] != '\n')
1386                         break;
1387                 for (i = 0; i < ARRAY_SIZE(optable); i++) {
1388                         const struct opentry *p = optable + i;
1389                         int oplen = strlen(p->str);
1390                         if (len < oplen || memcmp(p->str, line, oplen))
1391                                 continue;
1392                         if (p->fn(state, line + oplen, patch) < 0)
1393                                 return offset;
1394                         break;
1395                 }
1396         }
1397
1398         return offset;
1399 }
1400
1401 static int parse_num(const char *line, unsigned long *p)
1402 {
1403         char *ptr;
1404
1405         if (!isdigit(*line))
1406                 return 0;
1407         *p = strtoul(line, &ptr, 10);
1408         return ptr - line;
1409 }
1410
1411 static int parse_range(const char *line, int len, int offset, const char *expect,
1412                        unsigned long *p1, unsigned long *p2)
1413 {
1414         int digits, ex;
1415
1416         if (offset < 0 || offset >= len)
1417                 return -1;
1418         line += offset;
1419         len -= offset;
1420
1421         digits = parse_num(line, p1);
1422         if (!digits)
1423                 return -1;
1424
1425         offset += digits;
1426         line += digits;
1427         len -= digits;
1428
1429         *p2 = 1;
1430         if (*line == ',') {
1431                 digits = parse_num(line+1, p2);
1432                 if (!digits)
1433                         return -1;
1434
1435                 offset += digits+1;
1436                 line += digits+1;
1437                 len -= digits+1;
1438         }
1439
1440         ex = strlen(expect);
1441         if (ex > len)
1442                 return -1;
1443         if (memcmp(line, expect, ex))
1444                 return -1;
1445
1446         return offset + ex;
1447 }
1448
1449 static void recount_diff(const char *line, int size, struct fragment *fragment)
1450 {
1451         int oldlines = 0, newlines = 0, ret = 0;
1452
1453         if (size < 1) {
1454                 warning("recount: ignore empty hunk");
1455                 return;
1456         }
1457
1458         for (;;) {
1459                 int len = linelen(line, size);
1460                 size -= len;
1461                 line += len;
1462
1463                 if (size < 1)
1464                         break;
1465
1466                 switch (*line) {
1467                 case ' ': case '\n':
1468                         newlines++;
1469                         /* fall through */
1470                 case '-':
1471                         oldlines++;
1472                         continue;
1473                 case '+':
1474                         newlines++;
1475                         continue;
1476                 case '\\':
1477                         continue;
1478                 case '@':
1479                         ret = size < 3 || !starts_with(line, "@@ ");
1480                         break;
1481                 case 'd':
1482                         ret = size < 5 || !starts_with(line, "diff ");
1483                         break;
1484                 default:
1485                         ret = -1;
1486                         break;
1487                 }
1488                 if (ret) {
1489                         warning(_("recount: unexpected line: %.*s"),
1490                                 (int)linelen(line, size), line);
1491                         return;
1492                 }
1493                 break;
1494         }
1495         fragment->oldlines = oldlines;
1496         fragment->newlines = newlines;
1497 }
1498
1499 /*
1500  * Parse a unified diff fragment header of the
1501  * form "@@ -a,b +c,d @@"
1502  */
1503 static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1504 {
1505         int offset;
1506
1507         if (!len || line[len-1] != '\n')
1508                 return -1;
1509
1510         /* Figure out the number of lines in a fragment */
1511         offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1512         offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1513
1514         return offset;
1515 }
1516
1517 static int find_header(struct apply_state *state,
1518                        const char *line,
1519                        unsigned long size,
1520                        int *hdrsize,
1521                        struct patch *patch)
1522 {
1523         unsigned long offset, len;
1524
1525         patch->is_toplevel_relative = 0;
1526         patch->is_rename = patch->is_copy = 0;
1527         patch->is_new = patch->is_delete = -1;
1528         patch->old_mode = patch->new_mode = 0;
1529         patch->old_name = patch->new_name = NULL;
1530         for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1531                 unsigned long nextlen;
1532
1533                 len = linelen(line, size);
1534                 if (!len)
1535                         break;
1536
1537                 /* Testing this early allows us to take a few shortcuts.. */
1538                 if (len < 6)
1539                         continue;
1540
1541                 /*
1542                  * Make sure we don't find any unconnected patch fragments.
1543                  * That's a sign that we didn't find a header, and that a
1544                  * patch has become corrupted/broken up.
1545                  */
1546                 if (!memcmp("@@ -", line, 4)) {
1547                         struct fragment dummy;
1548                         if (parse_fragment_header(line, len, &dummy) < 0)
1549                                 continue;
1550                         die(_("patch fragment without header at line %d: %.*s"),
1551                             state->linenr, (int)len-1, line);
1552                 }
1553
1554                 if (size < len + 6)
1555                         break;
1556
1557                 /*
1558                  * Git patch? It might not have a real patch, just a rename
1559                  * or mode change, so we handle that specially
1560                  */
1561                 if (!memcmp("diff --git ", line, 11)) {
1562                         int git_hdr_len = parse_git_header(state, line, len, size, patch);
1563                         if (git_hdr_len <= len)
1564                                 continue;
1565                         if (!patch->old_name && !patch->new_name) {
1566                                 if (!patch->def_name)
1567                                         die(Q_("git diff header lacks filename information when removing "
1568                                                "%d leading pathname component (line %d)",
1569                                                "git diff header lacks filename information when removing "
1570                                                "%d leading pathname components (line %d)",
1571                                                state->p_value),
1572                                             state->p_value, state->linenr);
1573                                 patch->old_name = xstrdup(patch->def_name);
1574                                 patch->new_name = xstrdup(patch->def_name);
1575                         }
1576                         if (!patch->is_delete && !patch->new_name)
1577                                 die("git diff header lacks filename information "
1578                                     "(line %d)", state->linenr);
1579                         patch->is_toplevel_relative = 1;
1580                         *hdrsize = git_hdr_len;
1581                         return offset;
1582                 }
1583
1584                 /* --- followed by +++ ? */
1585                 if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
1586                         continue;
1587
1588                 /*
1589                  * We only accept unified patches, so we want it to
1590                  * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1591                  * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1592                  */
1593                 nextlen = linelen(line + len, size - len);
1594                 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1595                         continue;
1596
1597                 /* Ok, we'll consider it a patch */
1598                 parse_traditional_patch(state, line, line+len, patch);
1599                 *hdrsize = len + nextlen;
1600                 state->linenr += 2;
1601                 return offset;
1602         }
1603         return -1;
1604 }
1605
1606 static void record_ws_error(struct apply_state *state,
1607                             unsigned result,
1608                             const char *line,
1609                             int len,
1610                             int linenr)
1611 {
1612         char *err;
1613
1614         if (!result)
1615                 return;
1616
1617         state->whitespace_error++;
1618         if (state->squelch_whitespace_errors &&
1619             state->squelch_whitespace_errors < state->whitespace_error)
1620                 return;
1621
1622         err = whitespace_error_string(result);
1623         fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1624                 state->patch_input_file, linenr, err, len, line);
1625         free(err);
1626 }
1627
1628 static void check_whitespace(struct apply_state *state,
1629                              const char *line,
1630                              int len,
1631                              unsigned ws_rule)
1632 {
1633         unsigned result = ws_check(line + 1, len - 1, ws_rule);
1634
1635         record_ws_error(state, result, line + 1, len - 2, state->linenr);
1636 }
1637
1638 /*
1639  * Parse a unified diff. Note that this really needs to parse each
1640  * fragment separately, since the only way to know the difference
1641  * between a "---" that is part of a patch, and a "---" that starts
1642  * the next patch is to look at the line counts..
1643  */
1644 static int parse_fragment(struct apply_state *state,
1645                           const char *line,
1646                           unsigned long size,
1647                           struct patch *patch,
1648                           struct fragment *fragment)
1649 {
1650         int added, deleted;
1651         int len = linelen(line, size), offset;
1652         unsigned long oldlines, newlines;
1653         unsigned long leading, trailing;
1654
1655         offset = parse_fragment_header(line, len, fragment);
1656         if (offset < 0)
1657                 return -1;
1658         if (offset > 0 && patch->recount)
1659                 recount_diff(line + offset, size - offset, fragment);
1660         oldlines = fragment->oldlines;
1661         newlines = fragment->newlines;
1662         leading = 0;
1663         trailing = 0;
1664
1665         /* Parse the thing.. */
1666         line += len;
1667         size -= len;
1668         state->linenr++;
1669         added = deleted = 0;
1670         for (offset = len;
1671              0 < size;
1672              offset += len, size -= len, line += len, state->linenr++) {
1673                 if (!oldlines && !newlines)
1674                         break;
1675                 len = linelen(line, size);
1676                 if (!len || line[len-1] != '\n')
1677                         return -1;
1678                 switch (*line) {
1679                 default:
1680                         return -1;
1681                 case '\n': /* newer GNU diff, an empty context line */
1682                 case ' ':
1683                         oldlines--;
1684                         newlines--;
1685                         if (!deleted && !added)
1686                                 leading++;
1687                         trailing++;
1688                         if (!state->apply_in_reverse &&
1689                             state->ws_error_action == correct_ws_error)
1690                                 check_whitespace(state, line, len, patch->ws_rule);
1691                         break;
1692                 case '-':
1693                         if (state->apply_in_reverse &&
1694                             state->ws_error_action != nowarn_ws_error)
1695                                 check_whitespace(state, line, len, patch->ws_rule);
1696                         deleted++;
1697                         oldlines--;
1698                         trailing = 0;
1699                         break;
1700                 case '+':
1701                         if (!state->apply_in_reverse &&
1702                             state->ws_error_action != nowarn_ws_error)
1703                                 check_whitespace(state, line, len, patch->ws_rule);
1704                         added++;
1705                         newlines--;
1706                         trailing = 0;
1707                         break;
1708
1709                 /*
1710                  * We allow "\ No newline at end of file". Depending
1711                  * on locale settings when the patch was produced we
1712                  * don't know what this line looks like. The only
1713                  * thing we do know is that it begins with "\ ".
1714                  * Checking for 12 is just for sanity check -- any
1715                  * l10n of "\ No newline..." is at least that long.
1716                  */
1717                 case '\\':
1718                         if (len < 12 || memcmp(line, "\\ ", 2))
1719                                 return -1;
1720                         break;
1721                 }
1722         }
1723         if (oldlines || newlines)
1724                 return -1;
1725         if (!deleted && !added)
1726                 return -1;
1727
1728         fragment->leading = leading;
1729         fragment->trailing = trailing;
1730
1731         /*
1732          * If a fragment ends with an incomplete line, we failed to include
1733          * it in the above loop because we hit oldlines == newlines == 0
1734          * before seeing it.
1735          */
1736         if (12 < size && !memcmp(line, "\\ ", 2))
1737                 offset += linelen(line, size);
1738
1739         patch->lines_added += added;
1740         patch->lines_deleted += deleted;
1741
1742         if (0 < patch->is_new && oldlines)
1743                 return error(_("new file depends on old contents"));
1744         if (0 < patch->is_delete && newlines)
1745                 return error(_("deleted file still has contents"));
1746         return offset;
1747 }
1748
1749 /*
1750  * We have seen "diff --git a/... b/..." header (or a traditional patch
1751  * header).  Read hunks that belong to this patch into fragments and hang
1752  * them to the given patch structure.
1753  *
1754  * The (fragment->patch, fragment->size) pair points into the memory given
1755  * by the caller, not a copy, when we return.
1756  */
1757 static int parse_single_patch(struct apply_state *state,
1758                               const char *line,
1759                               unsigned long size,
1760                               struct patch *patch)
1761 {
1762         unsigned long offset = 0;
1763         unsigned long oldlines = 0, newlines = 0, context = 0;
1764         struct fragment **fragp = &patch->fragments;
1765
1766         while (size > 4 && !memcmp(line, "@@ -", 4)) {
1767                 struct fragment *fragment;
1768                 int len;
1769
1770                 fragment = xcalloc(1, sizeof(*fragment));
1771                 fragment->linenr = state->linenr;
1772                 len = parse_fragment(state, line, size, patch, fragment);
1773                 if (len <= 0)
1774                         die(_("corrupt patch at line %d"), state->linenr);
1775                 fragment->patch = line;
1776                 fragment->size = len;
1777                 oldlines += fragment->oldlines;
1778                 newlines += fragment->newlines;
1779                 context += fragment->leading + fragment->trailing;
1780
1781                 *fragp = fragment;
1782                 fragp = &fragment->next;
1783
1784                 offset += len;
1785                 line += len;
1786                 size -= len;
1787         }
1788
1789         /*
1790          * If something was removed (i.e. we have old-lines) it cannot
1791          * be creation, and if something was added it cannot be
1792          * deletion.  However, the reverse is not true; --unified=0
1793          * patches that only add are not necessarily creation even
1794          * though they do not have any old lines, and ones that only
1795          * delete are not necessarily deletion.
1796          *
1797          * Unfortunately, a real creation/deletion patch do _not_ have
1798          * any context line by definition, so we cannot safely tell it
1799          * apart with --unified=0 insanity.  At least if the patch has
1800          * more than one hunk it is not creation or deletion.
1801          */
1802         if (patch->is_new < 0 &&
1803             (oldlines || (patch->fragments && patch->fragments->next)))
1804                 patch->is_new = 0;
1805         if (patch->is_delete < 0 &&
1806             (newlines || (patch->fragments && patch->fragments->next)))
1807                 patch->is_delete = 0;
1808
1809         if (0 < patch->is_new && oldlines)
1810                 die(_("new file %s depends on old contents"), patch->new_name);
1811         if (0 < patch->is_delete && newlines)
1812                 die(_("deleted file %s still has contents"), patch->old_name);
1813         if (!patch->is_delete && !newlines && context)
1814                 fprintf_ln(stderr,
1815                            _("** warning: "
1816                              "file %s becomes empty but is not deleted"),
1817                            patch->new_name);
1818
1819         return offset;
1820 }
1821
1822 static inline int metadata_changes(struct patch *patch)
1823 {
1824         return  patch->is_rename > 0 ||
1825                 patch->is_copy > 0 ||
1826                 patch->is_new > 0 ||
1827                 patch->is_delete ||
1828                 (patch->old_mode && patch->new_mode &&
1829                  patch->old_mode != patch->new_mode);
1830 }
1831
1832 static char *inflate_it(const void *data, unsigned long size,
1833                         unsigned long inflated_size)
1834 {
1835         git_zstream stream;
1836         void *out;
1837         int st;
1838
1839         memset(&stream, 0, sizeof(stream));
1840
1841         stream.next_in = (unsigned char *)data;
1842         stream.avail_in = size;
1843         stream.next_out = out = xmalloc(inflated_size);
1844         stream.avail_out = inflated_size;
1845         git_inflate_init(&stream);
1846         st = git_inflate(&stream, Z_FINISH);
1847         git_inflate_end(&stream);
1848         if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1849                 free(out);
1850                 return NULL;
1851         }
1852         return out;
1853 }
1854
1855 /*
1856  * Read a binary hunk and return a new fragment; fragment->patch
1857  * points at an allocated memory that the caller must free, so
1858  * it is marked as "->free_patch = 1".
1859  */
1860 static struct fragment *parse_binary_hunk(struct apply_state *state,
1861                                           char **buf_p,
1862                                           unsigned long *sz_p,
1863                                           int *status_p,
1864                                           int *used_p)
1865 {
1866         /*
1867          * Expect a line that begins with binary patch method ("literal"
1868          * or "delta"), followed by the length of data before deflating.
1869          * a sequence of 'length-byte' followed by base-85 encoded data
1870          * should follow, terminated by a newline.
1871          *
1872          * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1873          * and we would limit the patch line to 66 characters,
1874          * so one line can fit up to 13 groups that would decode
1875          * to 52 bytes max.  The length byte 'A'-'Z' corresponds
1876          * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1877          */
1878         int llen, used;
1879         unsigned long size = *sz_p;
1880         char *buffer = *buf_p;
1881         int patch_method;
1882         unsigned long origlen;
1883         char *data = NULL;
1884         int hunk_size = 0;
1885         struct fragment *frag;
1886
1887         llen = linelen(buffer, size);
1888         used = llen;
1889
1890         *status_p = 0;
1891
1892         if (starts_with(buffer, "delta ")) {
1893                 patch_method = BINARY_DELTA_DEFLATED;
1894                 origlen = strtoul(buffer + 6, NULL, 10);
1895         }
1896         else if (starts_with(buffer, "literal ")) {
1897                 patch_method = BINARY_LITERAL_DEFLATED;
1898                 origlen = strtoul(buffer + 8, NULL, 10);
1899         }
1900         else
1901                 return NULL;
1902
1903         state->linenr++;
1904         buffer += llen;
1905         while (1) {
1906                 int byte_length, max_byte_length, newsize;
1907                 llen = linelen(buffer, size);
1908                 used += llen;
1909                 state->linenr++;
1910                 if (llen == 1) {
1911                         /* consume the blank line */
1912                         buffer++;
1913                         size--;
1914                         break;
1915                 }
1916                 /*
1917                  * Minimum line is "A00000\n" which is 7-byte long,
1918                  * and the line length must be multiple of 5 plus 2.
1919                  */
1920                 if ((llen < 7) || (llen-2) % 5)
1921                         goto corrupt;
1922                 max_byte_length = (llen - 2) / 5 * 4;
1923                 byte_length = *buffer;
1924                 if ('A' <= byte_length && byte_length <= 'Z')
1925                         byte_length = byte_length - 'A' + 1;
1926                 else if ('a' <= byte_length && byte_length <= 'z')
1927                         byte_length = byte_length - 'a' + 27;
1928                 else
1929                         goto corrupt;
1930                 /* if the input length was not multiple of 4, we would
1931                  * have filler at the end but the filler should never
1932                  * exceed 3 bytes
1933                  */
1934                 if (max_byte_length < byte_length ||
1935                     byte_length <= max_byte_length - 4)
1936                         goto corrupt;
1937                 newsize = hunk_size + byte_length;
1938                 data = xrealloc(data, newsize);
1939                 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1940                         goto corrupt;
1941                 hunk_size = newsize;
1942                 buffer += llen;
1943                 size -= llen;
1944         }
1945
1946         frag = xcalloc(1, sizeof(*frag));
1947         frag->patch = inflate_it(data, hunk_size, origlen);
1948         frag->free_patch = 1;
1949         if (!frag->patch)
1950                 goto corrupt;
1951         free(data);
1952         frag->size = origlen;
1953         *buf_p = buffer;
1954         *sz_p = size;
1955         *used_p = used;
1956         frag->binary_patch_method = patch_method;
1957         return frag;
1958
1959  corrupt:
1960         free(data);
1961         *status_p = -1;
1962         error(_("corrupt binary patch at line %d: %.*s"),
1963               state->linenr-1, llen-1, buffer);
1964         return NULL;
1965 }
1966
1967 /*
1968  * Returns:
1969  *   -1 in case of error,
1970  *   the length of the parsed binary patch otherwise
1971  */
1972 static int parse_binary(struct apply_state *state,
1973                         char *buffer,
1974                         unsigned long size,
1975                         struct patch *patch)
1976 {
1977         /*
1978          * We have read "GIT binary patch\n"; what follows is a line
1979          * that says the patch method (currently, either "literal" or
1980          * "delta") and the length of data before deflating; a
1981          * sequence of 'length-byte' followed by base-85 encoded data
1982          * follows.
1983          *
1984          * When a binary patch is reversible, there is another binary
1985          * hunk in the same format, starting with patch method (either
1986          * "literal" or "delta") with the length of data, and a sequence
1987          * of length-byte + base-85 encoded data, terminated with another
1988          * empty line.  This data, when applied to the postimage, produces
1989          * the preimage.
1990          */
1991         struct fragment *forward;
1992         struct fragment *reverse;
1993         int status;
1994         int used, used_1;
1995
1996         forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
1997         if (!forward && !status)
1998                 /* there has to be one hunk (forward hunk) */
1999                 return error(_("unrecognized binary patch at line %d"), state->linenr-1);
2000         if (status)
2001                 /* otherwise we already gave an error message */
2002                 return status;
2003
2004         reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
2005         if (reverse)
2006                 used += used_1;
2007         else if (status) {
2008                 /*
2009                  * Not having reverse hunk is not an error, but having
2010                  * a corrupt reverse hunk is.
2011                  */
2012                 free((void*) forward->patch);
2013                 free(forward);
2014                 return status;
2015         }
2016         forward->next = reverse;
2017         patch->fragments = forward;
2018         patch->is_binary = 1;
2019         return used;
2020 }
2021
2022 static void prefix_one(struct apply_state *state, char **name)
2023 {
2024         char *old_name = *name;
2025         if (!old_name)
2026                 return;
2027         *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
2028         free(old_name);
2029 }
2030
2031 static void prefix_patch(struct apply_state *state, struct patch *p)
2032 {
2033         if (!state->prefix || p->is_toplevel_relative)
2034                 return;
2035         prefix_one(state, &p->new_name);
2036         prefix_one(state, &p->old_name);
2037 }
2038
2039 /*
2040  * include/exclude
2041  */
2042
2043 static void add_name_limit(struct apply_state *state,
2044                            const char *name,
2045                            int exclude)
2046 {
2047         struct string_list_item *it;
2048
2049         it = string_list_append(&state->limit_by_name, name);
2050         it->util = exclude ? NULL : (void *) 1;
2051 }
2052
2053 static int use_patch(struct apply_state *state, struct patch *p)
2054 {
2055         const char *pathname = p->new_name ? p->new_name : p->old_name;
2056         int i;
2057
2058         /* Paths outside are not touched regardless of "--include" */
2059         if (0 < state->prefix_length) {
2060                 int pathlen = strlen(pathname);
2061                 if (pathlen <= state->prefix_length ||
2062                     memcmp(state->prefix, pathname, state->prefix_length))
2063                         return 0;
2064         }
2065
2066         /* See if it matches any of exclude/include rule */
2067         for (i = 0; i < state->limit_by_name.nr; i++) {
2068                 struct string_list_item *it = &state->limit_by_name.items[i];
2069                 if (!wildmatch(it->string, pathname, 0, NULL))
2070                         return (it->util != NULL);
2071         }
2072
2073         /*
2074          * If we had any include, a path that does not match any rule is
2075          * not used.  Otherwise, we saw bunch of exclude rules (or none)
2076          * and such a path is used.
2077          */
2078         return !state->has_include;
2079 }
2080
2081
2082 /*
2083  * Read the patch text in "buffer" that extends for "size" bytes; stop
2084  * reading after seeing a single patch (i.e. changes to a single file).
2085  * Create fragments (i.e. patch hunks) and hang them to the given patch.
2086  * Return the number of bytes consumed, so that the caller can call us
2087  * again for the next patch.
2088  */
2089 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2090 {
2091         int hdrsize, patchsize;
2092         int offset = find_header(state, buffer, size, &hdrsize, patch);
2093
2094         if (offset < 0)
2095                 return offset;
2096
2097         prefix_patch(state, patch);
2098
2099         if (!use_patch(state, patch))
2100                 patch->ws_rule = 0;
2101         else
2102                 patch->ws_rule = whitespace_rule(patch->new_name
2103                                                  ? patch->new_name
2104                                                  : patch->old_name);
2105
2106         patchsize = parse_single_patch(state,
2107                                        buffer + offset + hdrsize,
2108                                        size - offset - hdrsize,
2109                                        patch);
2110
2111         if (!patchsize) {
2112                 static const char git_binary[] = "GIT binary patch\n";
2113                 int hd = hdrsize + offset;
2114                 unsigned long llen = linelen(buffer + hd, size - hd);
2115
2116                 if (llen == sizeof(git_binary) - 1 &&
2117                     !memcmp(git_binary, buffer + hd, llen)) {
2118                         int used;
2119                         state->linenr++;
2120                         used = parse_binary(state, buffer + hd + llen,
2121                                             size - hd - llen, patch);
2122                         if (used < 0)
2123                                 return -1;
2124                         if (used)
2125                                 patchsize = used + llen;
2126                         else
2127                                 patchsize = 0;
2128                 }
2129                 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2130                         static const char *binhdr[] = {
2131                                 "Binary files ",
2132                                 "Files ",
2133                                 NULL,
2134                         };
2135                         int i;
2136                         for (i = 0; binhdr[i]; i++) {
2137                                 int len = strlen(binhdr[i]);
2138                                 if (len < size - hd &&
2139                                     !memcmp(binhdr[i], buffer + hd, len)) {
2140                                         state->linenr++;
2141                                         patch->is_binary = 1;
2142                                         patchsize = llen;
2143                                         break;
2144                                 }
2145                         }
2146                 }
2147
2148                 /* Empty patch cannot be applied if it is a text patch
2149                  * without metadata change.  A binary patch appears
2150                  * empty to us here.
2151                  */
2152                 if ((state->apply || state->check) &&
2153                     (!patch->is_binary && !metadata_changes(patch)))
2154                         die(_("patch with only garbage at line %d"), state->linenr);
2155         }
2156
2157         return offset + hdrsize + patchsize;
2158 }
2159
2160 #define swap(a,b) myswap((a),(b),sizeof(a))
2161
2162 #define myswap(a, b, size) do {         \
2163         unsigned char mytmp[size];      \
2164         memcpy(mytmp, &a, size);                \
2165         memcpy(&a, &b, size);           \
2166         memcpy(&b, mytmp, size);                \
2167 } while (0)
2168
2169 static void reverse_patches(struct patch *p)
2170 {
2171         for (; p; p = p->next) {
2172                 struct fragment *frag = p->fragments;
2173
2174                 swap(p->new_name, p->old_name);
2175                 swap(p->new_mode, p->old_mode);
2176                 swap(p->is_new, p->is_delete);
2177                 swap(p->lines_added, p->lines_deleted);
2178                 swap(p->old_sha1_prefix, p->new_sha1_prefix);
2179
2180                 for (; frag; frag = frag->next) {
2181                         swap(frag->newpos, frag->oldpos);
2182                         swap(frag->newlines, frag->oldlines);
2183                 }
2184         }
2185 }
2186
2187 static const char pluses[] =
2188 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2189 static const char minuses[]=
2190 "----------------------------------------------------------------------";
2191
2192 static void show_stats(struct apply_state *state, struct patch *patch)
2193 {
2194         struct strbuf qname = STRBUF_INIT;
2195         char *cp = patch->new_name ? patch->new_name : patch->old_name;
2196         int max, add, del;
2197
2198         quote_c_style(cp, &qname, NULL, 0);
2199
2200         /*
2201          * "scale" the filename
2202          */
2203         max = state->max_len;
2204         if (max > 50)
2205                 max = 50;
2206
2207         if (qname.len > max) {
2208                 cp = strchr(qname.buf + qname.len + 3 - max, '/');
2209                 if (!cp)
2210                         cp = qname.buf + qname.len + 3 - max;
2211                 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2212         }
2213
2214         if (patch->is_binary) {
2215                 printf(" %-*s |  Bin\n", max, qname.buf);
2216                 strbuf_release(&qname);
2217                 return;
2218         }
2219
2220         printf(" %-*s |", max, qname.buf);
2221         strbuf_release(&qname);
2222
2223         /*
2224          * scale the add/delete
2225          */
2226         max = max + state->max_change > 70 ? 70 - max : state->max_change;
2227         add = patch->lines_added;
2228         del = patch->lines_deleted;
2229
2230         if (state->max_change > 0) {
2231                 int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2232                 add = (add * max + state->max_change / 2) / state->max_change;
2233                 del = total - add;
2234         }
2235         printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2236                 add, pluses, del, minuses);
2237 }
2238
2239 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2240 {
2241         switch (st->st_mode & S_IFMT) {
2242         case S_IFLNK:
2243                 if (strbuf_readlink(buf, path, st->st_size) < 0)
2244                         return error(_("unable to read symlink %s"), path);
2245                 return 0;
2246         case S_IFREG:
2247                 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2248                         return error(_("unable to open or read %s"), path);
2249                 convert_to_git(path, buf->buf, buf->len, buf, 0);
2250                 return 0;
2251         default:
2252                 return -1;
2253         }
2254 }
2255
2256 /*
2257  * Update the preimage, and the common lines in postimage,
2258  * from buffer buf of length len. If postlen is 0 the postimage
2259  * is updated in place, otherwise it's updated on a new buffer
2260  * of length postlen
2261  */
2262
2263 static void update_pre_post_images(struct image *preimage,
2264                                    struct image *postimage,
2265                                    char *buf,
2266                                    size_t len, size_t postlen)
2267 {
2268         int i, ctx, reduced;
2269         char *new, *old, *fixed;
2270         struct image fixed_preimage;
2271
2272         /*
2273          * Update the preimage with whitespace fixes.  Note that we
2274          * are not losing preimage->buf -- apply_one_fragment() will
2275          * free "oldlines".
2276          */
2277         prepare_image(&fixed_preimage, buf, len, 1);
2278         assert(postlen
2279                ? fixed_preimage.nr == preimage->nr
2280                : fixed_preimage.nr <= preimage->nr);
2281         for (i = 0; i < fixed_preimage.nr; i++)
2282                 fixed_preimage.line[i].flag = preimage->line[i].flag;
2283         free(preimage->line_allocated);
2284         *preimage = fixed_preimage;
2285
2286         /*
2287          * Adjust the common context lines in postimage. This can be
2288          * done in-place when we are shrinking it with whitespace
2289          * fixing, but needs a new buffer when ignoring whitespace or
2290          * expanding leading tabs to spaces.
2291          *
2292          * We trust the caller to tell us if the update can be done
2293          * in place (postlen==0) or not.
2294          */
2295         old = postimage->buf;
2296         if (postlen)
2297                 new = postimage->buf = xmalloc(postlen);
2298         else
2299                 new = old;
2300         fixed = preimage->buf;
2301
2302         for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2303                 size_t l_len = postimage->line[i].len;
2304                 if (!(postimage->line[i].flag & LINE_COMMON)) {
2305                         /* an added line -- no counterparts in preimage */
2306                         memmove(new, old, l_len);
2307                         old += l_len;
2308                         new += l_len;
2309                         continue;
2310                 }
2311
2312                 /* a common context -- skip it in the original postimage */
2313                 old += l_len;
2314
2315                 /* and find the corresponding one in the fixed preimage */
2316                 while (ctx < preimage->nr &&
2317                        !(preimage->line[ctx].flag & LINE_COMMON)) {
2318                         fixed += preimage->line[ctx].len;
2319                         ctx++;
2320                 }
2321
2322                 /*
2323                  * preimage is expected to run out, if the caller
2324                  * fixed addition of trailing blank lines.
2325                  */
2326                 if (preimage->nr <= ctx) {
2327                         reduced++;
2328                         continue;
2329                 }
2330
2331                 /* and copy it in, while fixing the line length */
2332                 l_len = preimage->line[ctx].len;
2333                 memcpy(new, fixed, l_len);
2334                 new += l_len;
2335                 fixed += l_len;
2336                 postimage->line[i].len = l_len;
2337                 ctx++;
2338         }
2339
2340         if (postlen
2341             ? postlen < new - postimage->buf
2342             : postimage->len < new - postimage->buf)
2343                 die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2344                     (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2345
2346         /* Fix the length of the whole thing */
2347         postimage->len = new - postimage->buf;
2348         postimage->nr -= reduced;
2349 }
2350
2351 static int line_by_line_fuzzy_match(struct image *img,
2352                                     struct image *preimage,
2353                                     struct image *postimage,
2354                                     unsigned long try,
2355                                     int try_lno,
2356                                     int preimage_limit)
2357 {
2358         int i;
2359         size_t imgoff = 0;
2360         size_t preoff = 0;
2361         size_t postlen = postimage->len;
2362         size_t extra_chars;
2363         char *buf;
2364         char *preimage_eof;
2365         char *preimage_end;
2366         struct strbuf fixed;
2367         char *fixed_buf;
2368         size_t fixed_len;
2369
2370         for (i = 0; i < preimage_limit; i++) {
2371                 size_t prelen = preimage->line[i].len;
2372                 size_t imglen = img->line[try_lno+i].len;
2373
2374                 if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2375                                       preimage->buf + preoff, prelen))
2376                         return 0;
2377                 if (preimage->line[i].flag & LINE_COMMON)
2378                         postlen += imglen - prelen;
2379                 imgoff += imglen;
2380                 preoff += prelen;
2381         }
2382
2383         /*
2384          * Ok, the preimage matches with whitespace fuzz.
2385          *
2386          * imgoff now holds the true length of the target that
2387          * matches the preimage before the end of the file.
2388          *
2389          * Count the number of characters in the preimage that fall
2390          * beyond the end of the file and make sure that all of them
2391          * are whitespace characters. (This can only happen if
2392          * we are removing blank lines at the end of the file.)
2393          */
2394         buf = preimage_eof = preimage->buf + preoff;
2395         for ( ; i < preimage->nr; i++)
2396                 preoff += preimage->line[i].len;
2397         preimage_end = preimage->buf + preoff;
2398         for ( ; buf < preimage_end; buf++)
2399                 if (!isspace(*buf))
2400                         return 0;
2401
2402         /*
2403          * Update the preimage and the common postimage context
2404          * lines to use the same whitespace as the target.
2405          * If whitespace is missing in the target (i.e.
2406          * if the preimage extends beyond the end of the file),
2407          * use the whitespace from the preimage.
2408          */
2409         extra_chars = preimage_end - preimage_eof;
2410         strbuf_init(&fixed, imgoff + extra_chars);
2411         strbuf_add(&fixed, img->buf + try, imgoff);
2412         strbuf_add(&fixed, preimage_eof, extra_chars);
2413         fixed_buf = strbuf_detach(&fixed, &fixed_len);
2414         update_pre_post_images(preimage, postimage,
2415                                fixed_buf, fixed_len, postlen);
2416         return 1;
2417 }
2418
2419 static int match_fragment(struct apply_state *state,
2420                           struct image *img,
2421                           struct image *preimage,
2422                           struct image *postimage,
2423                           unsigned long try,
2424                           int try_lno,
2425                           unsigned ws_rule,
2426                           int match_beginning, int match_end)
2427 {
2428         int i;
2429         char *fixed_buf, *buf, *orig, *target;
2430         struct strbuf fixed;
2431         size_t fixed_len, postlen;
2432         int preimage_limit;
2433
2434         if (preimage->nr + try_lno <= img->nr) {
2435                 /*
2436                  * The hunk falls within the boundaries of img.
2437                  */
2438                 preimage_limit = preimage->nr;
2439                 if (match_end && (preimage->nr + try_lno != img->nr))
2440                         return 0;
2441         } else if (state->ws_error_action == correct_ws_error &&
2442                    (ws_rule & WS_BLANK_AT_EOF)) {
2443                 /*
2444                  * This hunk extends beyond the end of img, and we are
2445                  * removing blank lines at the end of the file.  This
2446                  * many lines from the beginning of the preimage must
2447                  * match with img, and the remainder of the preimage
2448                  * must be blank.
2449                  */
2450                 preimage_limit = img->nr - try_lno;
2451         } else {
2452                 /*
2453                  * The hunk extends beyond the end of the img and
2454                  * we are not removing blanks at the end, so we
2455                  * should reject the hunk at this position.
2456                  */
2457                 return 0;
2458         }
2459
2460         if (match_beginning && try_lno)
2461                 return 0;
2462
2463         /* Quick hash check */
2464         for (i = 0; i < preimage_limit; i++)
2465                 if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2466                     (preimage->line[i].hash != img->line[try_lno + i].hash))
2467                         return 0;
2468
2469         if (preimage_limit == preimage->nr) {
2470                 /*
2471                  * Do we have an exact match?  If we were told to match
2472                  * at the end, size must be exactly at try+fragsize,
2473                  * otherwise try+fragsize must be still within the preimage,
2474                  * and either case, the old piece should match the preimage
2475                  * exactly.
2476                  */
2477                 if ((match_end
2478                      ? (try + preimage->len == img->len)
2479                      : (try + preimage->len <= img->len)) &&
2480                     !memcmp(img->buf + try, preimage->buf, preimage->len))
2481                         return 1;
2482         } else {
2483                 /*
2484                  * The preimage extends beyond the end of img, so
2485                  * there cannot be an exact match.
2486                  *
2487                  * There must be one non-blank context line that match
2488                  * a line before the end of img.
2489                  */
2490                 char *buf_end;
2491
2492                 buf = preimage->buf;
2493                 buf_end = buf;
2494                 for (i = 0; i < preimage_limit; i++)
2495                         buf_end += preimage->line[i].len;
2496
2497                 for ( ; buf < buf_end; buf++)
2498                         if (!isspace(*buf))
2499                                 break;
2500                 if (buf == buf_end)
2501                         return 0;
2502         }
2503
2504         /*
2505          * No exact match. If we are ignoring whitespace, run a line-by-line
2506          * fuzzy matching. We collect all the line length information because
2507          * we need it to adjust whitespace if we match.
2508          */
2509         if (state->ws_ignore_action == ignore_ws_change)
2510                 return line_by_line_fuzzy_match(img, preimage, postimage,
2511                                                 try, try_lno, preimage_limit);
2512
2513         if (state->ws_error_action != correct_ws_error)
2514                 return 0;
2515
2516         /*
2517          * The hunk does not apply byte-by-byte, but the hash says
2518          * it might with whitespace fuzz. We weren't asked to
2519          * ignore whitespace, we were asked to correct whitespace
2520          * errors, so let's try matching after whitespace correction.
2521          *
2522          * While checking the preimage against the target, whitespace
2523          * errors in both fixed, we count how large the corresponding
2524          * postimage needs to be.  The postimage prepared by
2525          * apply_one_fragment() has whitespace errors fixed on added
2526          * lines already, but the common lines were propagated as-is,
2527          * which may become longer when their whitespace errors are
2528          * fixed.
2529          */
2530
2531         /* First count added lines in postimage */
2532         postlen = 0;
2533         for (i = 0; i < postimage->nr; i++) {
2534                 if (!(postimage->line[i].flag & LINE_COMMON))
2535                         postlen += postimage->line[i].len;
2536         }
2537
2538         /*
2539          * The preimage may extend beyond the end of the file,
2540          * but in this loop we will only handle the part of the
2541          * preimage that falls within the file.
2542          */
2543         strbuf_init(&fixed, preimage->len + 1);
2544         orig = preimage->buf;
2545         target = img->buf + try;
2546         for (i = 0; i < preimage_limit; i++) {
2547                 size_t oldlen = preimage->line[i].len;
2548                 size_t tgtlen = img->line[try_lno + i].len;
2549                 size_t fixstart = fixed.len;
2550                 struct strbuf tgtfix;
2551                 int match;
2552
2553                 /* Try fixing the line in the preimage */
2554                 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2555
2556                 /* Try fixing the line in the target */
2557                 strbuf_init(&tgtfix, tgtlen);
2558                 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2559
2560                 /*
2561                  * If they match, either the preimage was based on
2562                  * a version before our tree fixed whitespace breakage,
2563                  * or we are lacking a whitespace-fix patch the tree
2564                  * the preimage was based on already had (i.e. target
2565                  * has whitespace breakage, the preimage doesn't).
2566                  * In either case, we are fixing the whitespace breakages
2567                  * so we might as well take the fix together with their
2568                  * real change.
2569                  */
2570                 match = (tgtfix.len == fixed.len - fixstart &&
2571                          !memcmp(tgtfix.buf, fixed.buf + fixstart,
2572                                              fixed.len - fixstart));
2573
2574                 /* Add the length if this is common with the postimage */
2575                 if (preimage->line[i].flag & LINE_COMMON)
2576                         postlen += tgtfix.len;
2577
2578                 strbuf_release(&tgtfix);
2579                 if (!match)
2580                         goto unmatch_exit;
2581
2582                 orig += oldlen;
2583                 target += tgtlen;
2584         }
2585
2586
2587         /*
2588          * Now handle the lines in the preimage that falls beyond the
2589          * end of the file (if any). They will only match if they are
2590          * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2591          * false).
2592          */
2593         for ( ; i < preimage->nr; i++) {
2594                 size_t fixstart = fixed.len; /* start of the fixed preimage */
2595                 size_t oldlen = preimage->line[i].len;
2596                 int j;
2597
2598                 /* Try fixing the line in the preimage */
2599                 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2600
2601                 for (j = fixstart; j < fixed.len; j++)
2602                         if (!isspace(fixed.buf[j]))
2603                                 goto unmatch_exit;
2604
2605                 orig += oldlen;
2606         }
2607
2608         /*
2609          * Yes, the preimage is based on an older version that still
2610          * has whitespace breakages unfixed, and fixing them makes the
2611          * hunk match.  Update the context lines in the postimage.
2612          */
2613         fixed_buf = strbuf_detach(&fixed, &fixed_len);
2614         if (postlen < postimage->len)
2615                 postlen = 0;
2616         update_pre_post_images(preimage, postimage,
2617                                fixed_buf, fixed_len, postlen);
2618         return 1;
2619
2620  unmatch_exit:
2621         strbuf_release(&fixed);
2622         return 0;
2623 }
2624
2625 static int find_pos(struct apply_state *state,
2626                     struct image *img,
2627                     struct image *preimage,
2628                     struct image *postimage,
2629                     int line,
2630                     unsigned ws_rule,
2631                     int match_beginning, int match_end)
2632 {
2633         int i;
2634         unsigned long backwards, forwards, try;
2635         int backwards_lno, forwards_lno, try_lno;
2636
2637         /*
2638          * If match_beginning or match_end is specified, there is no
2639          * point starting from a wrong line that will never match and
2640          * wander around and wait for a match at the specified end.
2641          */
2642         if (match_beginning)
2643                 line = 0;
2644         else if (match_end)
2645                 line = img->nr - preimage->nr;
2646
2647         /*
2648          * Because the comparison is unsigned, the following test
2649          * will also take care of a negative line number that can
2650          * result when match_end and preimage is larger than the target.
2651          */
2652         if ((size_t) line > img->nr)
2653                 line = img->nr;
2654
2655         try = 0;
2656         for (i = 0; i < line; i++)
2657                 try += img->line[i].len;
2658
2659         /*
2660          * There's probably some smart way to do this, but I'll leave
2661          * that to the smart and beautiful people. I'm simple and stupid.
2662          */
2663         backwards = try;
2664         backwards_lno = line;
2665         forwards = try;
2666         forwards_lno = line;
2667         try_lno = line;
2668
2669         for (i = 0; ; i++) {
2670                 if (match_fragment(state, img, preimage, postimage,
2671                                    try, try_lno, ws_rule,
2672                                    match_beginning, match_end))
2673                         return try_lno;
2674
2675         again:
2676                 if (backwards_lno == 0 && forwards_lno == img->nr)
2677                         break;
2678
2679                 if (i & 1) {
2680                         if (backwards_lno == 0) {
2681                                 i++;
2682                                 goto again;
2683                         }
2684                         backwards_lno--;
2685                         backwards -= img->line[backwards_lno].len;
2686                         try = backwards;
2687                         try_lno = backwards_lno;
2688                 } else {
2689                         if (forwards_lno == img->nr) {
2690                                 i++;
2691                                 goto again;
2692                         }
2693                         forwards += img->line[forwards_lno].len;
2694                         forwards_lno++;
2695                         try = forwards;
2696                         try_lno = forwards_lno;
2697                 }
2698
2699         }
2700         return -1;
2701 }
2702
2703 static void remove_first_line(struct image *img)
2704 {
2705         img->buf += img->line[0].len;
2706         img->len -= img->line[0].len;
2707         img->line++;
2708         img->nr--;
2709 }
2710
2711 static void remove_last_line(struct image *img)
2712 {
2713         img->len -= img->line[--img->nr].len;
2714 }
2715
2716 /*
2717  * The change from "preimage" and "postimage" has been found to
2718  * apply at applied_pos (counts in line numbers) in "img".
2719  * Update "img" to remove "preimage" and replace it with "postimage".
2720  */
2721 static void update_image(struct apply_state *state,
2722                          struct image *img,
2723                          int applied_pos,
2724                          struct image *preimage,
2725                          struct image *postimage)
2726 {
2727         /*
2728          * remove the copy of preimage at offset in img
2729          * and replace it with postimage
2730          */
2731         int i, nr;
2732         size_t remove_count, insert_count, applied_at = 0;
2733         char *result;
2734         int preimage_limit;
2735
2736         /*
2737          * If we are removing blank lines at the end of img,
2738          * the preimage may extend beyond the end.
2739          * If that is the case, we must be careful only to
2740          * remove the part of the preimage that falls within
2741          * the boundaries of img. Initialize preimage_limit
2742          * to the number of lines in the preimage that falls
2743          * within the boundaries.
2744          */
2745         preimage_limit = preimage->nr;
2746         if (preimage_limit > img->nr - applied_pos)
2747                 preimage_limit = img->nr - applied_pos;
2748
2749         for (i = 0; i < applied_pos; i++)
2750                 applied_at += img->line[i].len;
2751
2752         remove_count = 0;
2753         for (i = 0; i < preimage_limit; i++)
2754                 remove_count += img->line[applied_pos + i].len;
2755         insert_count = postimage->len;
2756
2757         /* Adjust the contents */
2758         result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2759         memcpy(result, img->buf, applied_at);
2760         memcpy(result + applied_at, postimage->buf, postimage->len);
2761         memcpy(result + applied_at + postimage->len,
2762                img->buf + (applied_at + remove_count),
2763                img->len - (applied_at + remove_count));
2764         free(img->buf);
2765         img->buf = result;
2766         img->len += insert_count - remove_count;
2767         result[img->len] = '\0';
2768
2769         /* Adjust the line table */
2770         nr = img->nr + postimage->nr - preimage_limit;
2771         if (preimage_limit < postimage->nr) {
2772                 /*
2773                  * NOTE: this knows that we never call remove_first_line()
2774                  * on anything other than pre/post image.
2775                  */
2776                 REALLOC_ARRAY(img->line, nr);
2777                 img->line_allocated = img->line;
2778         }
2779         if (preimage_limit != postimage->nr)
2780                 memmove(img->line + applied_pos + postimage->nr,
2781                         img->line + applied_pos + preimage_limit,
2782                         (img->nr - (applied_pos + preimage_limit)) *
2783                         sizeof(*img->line));
2784         memcpy(img->line + applied_pos,
2785                postimage->line,
2786                postimage->nr * sizeof(*img->line));
2787         if (!state->allow_overlap)
2788                 for (i = 0; i < postimage->nr; i++)
2789                         img->line[applied_pos + i].flag |= LINE_PATCHED;
2790         img->nr = nr;
2791 }
2792
2793 /*
2794  * Use the patch-hunk text in "frag" to prepare two images (preimage and
2795  * postimage) for the hunk.  Find lines that match "preimage" in "img" and
2796  * replace the part of "img" with "postimage" text.
2797  */
2798 static int apply_one_fragment(struct apply_state *state,
2799                               struct image *img, struct fragment *frag,
2800                               int inaccurate_eof, unsigned ws_rule,
2801                               int nth_fragment)
2802 {
2803         int match_beginning, match_end;
2804         const char *patch = frag->patch;
2805         int size = frag->size;
2806         char *old, *oldlines;
2807         struct strbuf newlines;
2808         int new_blank_lines_at_end = 0;
2809         int found_new_blank_lines_at_end = 0;
2810         int hunk_linenr = frag->linenr;
2811         unsigned long leading, trailing;
2812         int pos, applied_pos;
2813         struct image preimage;
2814         struct image postimage;
2815
2816         memset(&preimage, 0, sizeof(preimage));
2817         memset(&postimage, 0, sizeof(postimage));
2818         oldlines = xmalloc(size);
2819         strbuf_init(&newlines, size);
2820
2821         old = oldlines;
2822         while (size > 0) {
2823                 char first;
2824                 int len = linelen(patch, size);
2825                 int plen;
2826                 int added_blank_line = 0;
2827                 int is_blank_context = 0;
2828                 size_t start;
2829
2830                 if (!len)
2831                         break;
2832
2833                 /*
2834                  * "plen" is how much of the line we should use for
2835                  * the actual patch data. Normally we just remove the
2836                  * first character on the line, but if the line is
2837                  * followed by "\ No newline", then we also remove the
2838                  * last one (which is the newline, of course).
2839                  */
2840                 plen = len - 1;
2841                 if (len < size && patch[len] == '\\')
2842                         plen--;
2843                 first = *patch;
2844                 if (state->apply_in_reverse) {
2845                         if (first == '-')
2846                                 first = '+';
2847                         else if (first == '+')
2848                                 first = '-';
2849                 }
2850
2851                 switch (first) {
2852                 case '\n':
2853                         /* Newer GNU diff, empty context line */
2854                         if (plen < 0)
2855                                 /* ... followed by '\No newline'; nothing */
2856                                 break;
2857                         *old++ = '\n';
2858                         strbuf_addch(&newlines, '\n');
2859                         add_line_info(&preimage, "\n", 1, LINE_COMMON);
2860                         add_line_info(&postimage, "\n", 1, LINE_COMMON);
2861                         is_blank_context = 1;
2862                         break;
2863                 case ' ':
2864                         if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2865                             ws_blank_line(patch + 1, plen, ws_rule))
2866                                 is_blank_context = 1;
2867                 case '-':
2868                         memcpy(old, patch + 1, plen);
2869                         add_line_info(&preimage, old, plen,
2870                                       (first == ' ' ? LINE_COMMON : 0));
2871                         old += plen;
2872                         if (first == '-')
2873                                 break;
2874                 /* Fall-through for ' ' */
2875                 case '+':
2876                         /* --no-add does not add new lines */
2877                         if (first == '+' && state->no_add)
2878                                 break;
2879
2880                         start = newlines.len;
2881                         if (first != '+' ||
2882                             !state->whitespace_error ||
2883                             state->ws_error_action != correct_ws_error) {
2884                                 strbuf_add(&newlines, patch + 1, plen);
2885                         }
2886                         else {
2887                                 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2888                         }
2889                         add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2890                                       (first == '+' ? 0 : LINE_COMMON));
2891                         if (first == '+' &&
2892                             (ws_rule & WS_BLANK_AT_EOF) &&
2893                             ws_blank_line(patch + 1, plen, ws_rule))
2894                                 added_blank_line = 1;
2895                         break;
2896                 case '@': case '\\':
2897                         /* Ignore it, we already handled it */
2898                         break;
2899                 default:
2900                         if (state->apply_verbosely)
2901                                 error(_("invalid start of line: '%c'"), first);
2902                         applied_pos = -1;
2903                         goto out;
2904                 }
2905                 if (added_blank_line) {
2906                         if (!new_blank_lines_at_end)
2907                                 found_new_blank_lines_at_end = hunk_linenr;
2908                         new_blank_lines_at_end++;
2909                 }
2910                 else if (is_blank_context)
2911                         ;
2912                 else
2913                         new_blank_lines_at_end = 0;
2914                 patch += len;
2915                 size -= len;
2916                 hunk_linenr++;
2917         }
2918         if (inaccurate_eof &&
2919             old > oldlines && old[-1] == '\n' &&
2920             newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2921                 old--;
2922                 strbuf_setlen(&newlines, newlines.len - 1);
2923         }
2924
2925         leading = frag->leading;
2926         trailing = frag->trailing;
2927
2928         /*
2929          * A hunk to change lines at the beginning would begin with
2930          * @@ -1,L +N,M @@
2931          * but we need to be careful.  -U0 that inserts before the second
2932          * line also has this pattern.
2933          *
2934          * And a hunk to add to an empty file would begin with
2935          * @@ -0,0 +N,M @@
2936          *
2937          * In other words, a hunk that is (frag->oldpos <= 1) with or
2938          * without leading context must match at the beginning.
2939          */
2940         match_beginning = (!frag->oldpos ||
2941                            (frag->oldpos == 1 && !state->unidiff_zero));
2942
2943         /*
2944          * A hunk without trailing lines must match at the end.
2945          * However, we simply cannot tell if a hunk must match end
2946          * from the lack of trailing lines if the patch was generated
2947          * with unidiff without any context.
2948          */
2949         match_end = !state->unidiff_zero && !trailing;
2950
2951         pos = frag->newpos ? (frag->newpos - 1) : 0;
2952         preimage.buf = oldlines;
2953         preimage.len = old - oldlines;
2954         postimage.buf = newlines.buf;
2955         postimage.len = newlines.len;
2956         preimage.line = preimage.line_allocated;
2957         postimage.line = postimage.line_allocated;
2958
2959         for (;;) {
2960
2961                 applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2962                                        ws_rule, match_beginning, match_end);
2963
2964                 if (applied_pos >= 0)
2965                         break;
2966
2967                 /* Am I at my context limits? */
2968                 if ((leading <= state->p_context) && (trailing <= state->p_context))
2969                         break;
2970                 if (match_beginning || match_end) {
2971                         match_beginning = match_end = 0;
2972                         continue;
2973                 }
2974
2975                 /*
2976                  * Reduce the number of context lines; reduce both
2977                  * leading and trailing if they are equal otherwise
2978                  * just reduce the larger context.
2979                  */
2980                 if (leading >= trailing) {
2981                         remove_first_line(&preimage);
2982                         remove_first_line(&postimage);
2983                         pos--;
2984                         leading--;
2985                 }
2986                 if (trailing > leading) {
2987                         remove_last_line(&preimage);
2988                         remove_last_line(&postimage);
2989                         trailing--;
2990                 }
2991         }
2992
2993         if (applied_pos >= 0) {
2994                 if (new_blank_lines_at_end &&
2995                     preimage.nr + applied_pos >= img->nr &&
2996                     (ws_rule & WS_BLANK_AT_EOF) &&
2997                     state->ws_error_action != nowarn_ws_error) {
2998                         record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
2999                                         found_new_blank_lines_at_end);
3000                         if (state->ws_error_action == correct_ws_error) {
3001                                 while (new_blank_lines_at_end--)
3002                                         remove_last_line(&postimage);
3003                         }
3004                         /*
3005                          * We would want to prevent write_out_results()
3006                          * from taking place in apply_patch() that follows
3007                          * the callchain led us here, which is:
3008                          * apply_patch->check_patch_list->check_patch->
3009                          * apply_data->apply_fragments->apply_one_fragment
3010                          */
3011                         if (state->ws_error_action == die_on_ws_error)
3012                                 state->apply = 0;
3013                 }
3014
3015                 if (state->apply_verbosely && applied_pos != pos) {
3016                         int offset = applied_pos - pos;
3017                         if (state->apply_in_reverse)
3018                                 offset = 0 - offset;
3019                         fprintf_ln(stderr,
3020                                    Q_("Hunk #%d succeeded at %d (offset %d line).",
3021                                       "Hunk #%d succeeded at %d (offset %d lines).",
3022                                       offset),
3023                                    nth_fragment, applied_pos + 1, offset);
3024                 }
3025
3026                 /*
3027                  * Warn if it was necessary to reduce the number
3028                  * of context lines.
3029                  */
3030                 if ((leading != frag->leading) ||
3031                     (trailing != frag->trailing))
3032                         fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
3033                                              " to apply fragment at %d"),
3034                                    leading, trailing, applied_pos+1);
3035                 update_image(state, img, applied_pos, &preimage, &postimage);
3036         } else {
3037                 if (state->apply_verbosely)
3038                         error(_("while searching for:\n%.*s"),
3039                               (int)(old - oldlines), oldlines);
3040         }
3041
3042 out:
3043         free(oldlines);
3044         strbuf_release(&newlines);
3045         free(preimage.line_allocated);
3046         free(postimage.line_allocated);
3047
3048         return (applied_pos < 0);
3049 }
3050
3051 static int apply_binary_fragment(struct apply_state *state,
3052                                  struct image *img,
3053                                  struct patch *patch)
3054 {
3055         struct fragment *fragment = patch->fragments;
3056         unsigned long len;
3057         void *dst;
3058
3059         if (!fragment)
3060                 return error(_("missing binary patch data for '%s'"),
3061                              patch->new_name ?
3062                              patch->new_name :
3063                              patch->old_name);
3064
3065         /* Binary patch is irreversible without the optional second hunk */
3066         if (state->apply_in_reverse) {
3067                 if (!fragment->next)
3068                         return error("cannot reverse-apply a binary patch "
3069                                      "without the reverse hunk to '%s'",
3070                                      patch->new_name
3071                                      ? patch->new_name : patch->old_name);
3072                 fragment = fragment->next;
3073         }
3074         switch (fragment->binary_patch_method) {
3075         case BINARY_DELTA_DEFLATED:
3076                 dst = patch_delta(img->buf, img->len, fragment->patch,
3077                                   fragment->size, &len);
3078                 if (!dst)
3079                         return -1;
3080                 clear_image(img);
3081                 img->buf = dst;
3082                 img->len = len;
3083                 return 0;
3084         case BINARY_LITERAL_DEFLATED:
3085                 clear_image(img);
3086                 img->len = fragment->size;
3087                 img->buf = xmemdupz(fragment->patch, img->len);
3088                 return 0;
3089         }
3090         return -1;
3091 }
3092
3093 /*
3094  * Replace "img" with the result of applying the binary patch.
3095  * The binary patch data itself in patch->fragment is still kept
3096  * but the preimage prepared by the caller in "img" is freed here
3097  * or in the helper function apply_binary_fragment() this calls.
3098  */
3099 static int apply_binary(struct apply_state *state,
3100                         struct image *img,
3101                         struct patch *patch)
3102 {
3103         const char *name = patch->old_name ? patch->old_name : patch->new_name;
3104         unsigned char sha1[20];
3105
3106         /*
3107          * For safety, we require patch index line to contain
3108          * full 40-byte textual SHA1 for old and new, at least for now.
3109          */
3110         if (strlen(patch->old_sha1_prefix) != 40 ||
3111             strlen(patch->new_sha1_prefix) != 40 ||
3112             get_sha1_hex(patch->old_sha1_prefix, sha1) ||
3113             get_sha1_hex(patch->new_sha1_prefix, sha1))
3114                 return error("cannot apply binary patch to '%s' "
3115                              "without full index line", name);
3116
3117         if (patch->old_name) {
3118                 /*
3119                  * See if the old one matches what the patch
3120                  * applies to.
3121                  */
3122                 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3123                 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3124                         return error("the patch applies to '%s' (%s), "
3125                                      "which does not match the "
3126                                      "current contents.",
3127                                      name, sha1_to_hex(sha1));
3128         }
3129         else {
3130                 /* Otherwise, the old one must be empty. */
3131                 if (img->len)
3132                         return error("the patch applies to an empty "
3133                                      "'%s' but it is not empty", name);
3134         }
3135
3136         get_sha1_hex(patch->new_sha1_prefix, sha1);
3137         if (is_null_sha1(sha1)) {
3138                 clear_image(img);
3139                 return 0; /* deletion patch */
3140         }
3141
3142         if (has_sha1_file(sha1)) {
3143                 /* We already have the postimage */
3144                 enum object_type type;
3145                 unsigned long size;
3146                 char *result;
3147
3148                 result = read_sha1_file(sha1, &type, &size);
3149                 if (!result)
3150                         return error("the necessary postimage %s for "
3151                                      "'%s' cannot be read",
3152                                      patch->new_sha1_prefix, name);
3153                 clear_image(img);
3154                 img->buf = result;
3155                 img->len = size;
3156         } else {
3157                 /*
3158                  * We have verified buf matches the preimage;
3159                  * apply the patch data to it, which is stored
3160                  * in the patch->fragments->{patch,size}.
3161                  */
3162                 if (apply_binary_fragment(state, img, patch))
3163                         return error(_("binary patch does not apply to '%s'"),
3164                                      name);
3165
3166                 /* verify that the result matches */
3167                 hash_sha1_file(img->buf, img->len, blob_type, sha1);
3168                 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3169                         return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3170                                 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3171         }
3172
3173         return 0;
3174 }
3175
3176 static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3177 {
3178         struct fragment *frag = patch->fragments;
3179         const char *name = patch->old_name ? patch->old_name : patch->new_name;
3180         unsigned ws_rule = patch->ws_rule;
3181         unsigned inaccurate_eof = patch->inaccurate_eof;
3182         int nth = 0;
3183
3184         if (patch->is_binary)
3185                 return apply_binary(state, img, patch);
3186
3187         while (frag) {
3188                 nth++;
3189                 if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3190                         error(_("patch failed: %s:%ld"), name, frag->oldpos);
3191                         if (!state->apply_with_reject)
3192                                 return -1;
3193                         frag->rejected = 1;
3194                 }
3195                 frag = frag->next;
3196         }
3197         return 0;
3198 }
3199
3200 static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3201 {
3202         if (S_ISGITLINK(mode)) {
3203                 strbuf_grow(buf, 100);
3204                 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3205         } else {
3206                 enum object_type type;
3207                 unsigned long sz;
3208                 char *result;
3209
3210                 result = read_sha1_file(sha1, &type, &sz);
3211                 if (!result)
3212                         return -1;
3213                 /* XXX read_sha1_file NUL-terminates */
3214                 strbuf_attach(buf, result, sz, sz + 1);
3215         }
3216         return 0;
3217 }
3218
3219 static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3220 {
3221         if (!ce)
3222                 return 0;
3223         return read_blob_object(buf, ce->sha1, ce->ce_mode);
3224 }
3225
3226 static struct patch *in_fn_table(struct apply_state *state, const char *name)
3227 {
3228         struct string_list_item *item;
3229
3230         if (name == NULL)
3231                 return NULL;
3232
3233         item = string_list_lookup(&state->fn_table, name);
3234         if (item != NULL)
3235                 return (struct patch *)item->util;
3236
3237         return NULL;
3238 }
3239
3240 /*
3241  * item->util in the filename table records the status of the path.
3242  * Usually it points at a patch (whose result records the contents
3243  * of it after applying it), but it could be PATH_WAS_DELETED for a
3244  * path that a previously applied patch has already removed, or
3245  * PATH_TO_BE_DELETED for a path that a later patch would remove.
3246  *
3247  * The latter is needed to deal with a case where two paths A and B
3248  * are swapped by first renaming A to B and then renaming B to A;
3249  * moving A to B should not be prevented due to presence of B as we
3250  * will remove it in a later patch.
3251  */
3252 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3253 #define PATH_WAS_DELETED ((struct patch *) -1)
3254
3255 static int to_be_deleted(struct patch *patch)
3256 {
3257         return patch == PATH_TO_BE_DELETED;
3258 }
3259
3260 static int was_deleted(struct patch *patch)
3261 {
3262         return patch == PATH_WAS_DELETED;
3263 }
3264
3265 static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3266 {
3267         struct string_list_item *item;
3268
3269         /*
3270          * Always add new_name unless patch is a deletion
3271          * This should cover the cases for normal diffs,
3272          * file creations and copies
3273          */
3274         if (patch->new_name != NULL) {
3275                 item = string_list_insert(&state->fn_table, patch->new_name);
3276                 item->util = patch;
3277         }
3278
3279         /*
3280          * store a failure on rename/deletion cases because
3281          * later chunks shouldn't patch old names
3282          */
3283         if ((patch->new_name == NULL) || (patch->is_rename)) {
3284                 item = string_list_insert(&state->fn_table, patch->old_name);
3285                 item->util = PATH_WAS_DELETED;
3286         }
3287 }
3288
3289 static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3290 {
3291         /*
3292          * store information about incoming file deletion
3293          */
3294         while (patch) {
3295                 if ((patch->new_name == NULL) || (patch->is_rename)) {
3296                         struct string_list_item *item;
3297                         item = string_list_insert(&state->fn_table, patch->old_name);
3298                         item->util = PATH_TO_BE_DELETED;
3299                 }
3300                 patch = patch->next;
3301         }
3302 }
3303
3304 static int checkout_target(struct index_state *istate,
3305                            struct cache_entry *ce, struct stat *st)
3306 {
3307         struct checkout costate;
3308
3309         memset(&costate, 0, sizeof(costate));
3310         costate.base_dir = "";
3311         costate.refresh_cache = 1;
3312         costate.istate = istate;
3313         if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3314                 return error(_("cannot checkout %s"), ce->name);
3315         return 0;
3316 }
3317
3318 static struct patch *previous_patch(struct apply_state *state,
3319                                     struct patch *patch,
3320                                     int *gone)
3321 {
3322         struct patch *previous;
3323
3324         *gone = 0;
3325         if (patch->is_copy || patch->is_rename)
3326                 return NULL; /* "git" patches do not depend on the order */
3327
3328         previous = in_fn_table(state, patch->old_name);
3329         if (!previous)
3330                 return NULL;
3331
3332         if (to_be_deleted(previous))
3333                 return NULL; /* the deletion hasn't happened yet */
3334
3335         if (was_deleted(previous))
3336                 *gone = 1;
3337
3338         return previous;
3339 }
3340
3341 static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3342 {
3343         if (S_ISGITLINK(ce->ce_mode)) {
3344                 if (!S_ISDIR(st->st_mode))
3345                         return -1;
3346                 return 0;
3347         }
3348         return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3349 }
3350
3351 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3352
3353 static int load_patch_target(struct apply_state *state,
3354                              struct strbuf *buf,
3355                              const struct cache_entry *ce,
3356                              struct stat *st,
3357                              const char *name,
3358                              unsigned expected_mode)
3359 {
3360         if (state->cached || state->check_index) {
3361                 if (read_file_or_gitlink(ce, buf))
3362                         return error(_("read of %s failed"), name);
3363         } else if (name) {
3364                 if (S_ISGITLINK(expected_mode)) {
3365                         if (ce)
3366                                 return read_file_or_gitlink(ce, buf);
3367                         else
3368                                 return SUBMODULE_PATCH_WITHOUT_INDEX;
3369                 } else if (has_symlink_leading_path(name, strlen(name))) {
3370                         return error(_("reading from '%s' beyond a symbolic link"), name);
3371                 } else {
3372                         if (read_old_data(st, name, buf))
3373                                 return error(_("read of %s failed"), name);
3374                 }
3375         }
3376         return 0;
3377 }
3378
3379 /*
3380  * We are about to apply "patch"; populate the "image" with the
3381  * current version we have, from the working tree or from the index,
3382  * depending on the situation e.g. --cached/--index.  If we are
3383  * applying a non-git patch that incrementally updates the tree,
3384  * we read from the result of a previous diff.
3385  */
3386 static int load_preimage(struct apply_state *state,
3387                          struct image *image,
3388                          struct patch *patch, struct stat *st,
3389                          const struct cache_entry *ce)
3390 {
3391         struct strbuf buf = STRBUF_INIT;
3392         size_t len;
3393         char *img;
3394         struct patch *previous;
3395         int status;
3396
3397         previous = previous_patch(state, patch, &status);
3398         if (status)
3399                 return error(_("path %s has been renamed/deleted"),
3400                              patch->old_name);
3401         if (previous) {
3402                 /* We have a patched copy in memory; use that. */
3403                 strbuf_add(&buf, previous->result, previous->resultsize);
3404         } else {
3405                 status = load_patch_target(state, &buf, ce, st,
3406                                            patch->old_name, patch->old_mode);
3407                 if (status < 0)
3408                         return status;
3409                 else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3410                         /*
3411                          * There is no way to apply subproject
3412                          * patch without looking at the index.
3413                          * NEEDSWORK: shouldn't this be flagged
3414                          * as an error???
3415                          */
3416                         free_fragment_list(patch->fragments);
3417                         patch->fragments = NULL;
3418                 } else if (status) {
3419                         return error(_("read of %s failed"), patch->old_name);
3420                 }
3421         }
3422
3423         img = strbuf_detach(&buf, &len);
3424         prepare_image(image, img, len, !patch->is_binary);
3425         return 0;
3426 }
3427
3428 static int three_way_merge(struct image *image,
3429                            char *path,
3430                            const unsigned char *base,
3431                            const unsigned char *ours,
3432                            const unsigned char *theirs)
3433 {
3434         mmfile_t base_file, our_file, their_file;
3435         mmbuffer_t result = { NULL };
3436         int status;
3437
3438         read_mmblob(&base_file, base);
3439         read_mmblob(&our_file, ours);
3440         read_mmblob(&their_file, theirs);
3441         status = ll_merge(&result, path,
3442                           &base_file, "base",
3443                           &our_file, "ours",
3444                           &their_file, "theirs", NULL);
3445         free(base_file.ptr);
3446         free(our_file.ptr);
3447         free(their_file.ptr);
3448         if (status < 0 || !result.ptr) {
3449                 free(result.ptr);
3450                 return -1;
3451         }
3452         clear_image(image);
3453         image->buf = result.ptr;
3454         image->len = result.size;
3455
3456         return status;
3457 }
3458
3459 /*
3460  * When directly falling back to add/add three-way merge, we read from
3461  * the current contents of the new_name.  In no cases other than that
3462  * this function will be called.
3463  */
3464 static int load_current(struct apply_state *state,
3465                         struct image *image,
3466                         struct patch *patch)
3467 {
3468         struct strbuf buf = STRBUF_INIT;
3469         int status, pos;
3470         size_t len;
3471         char *img;
3472         struct stat st;
3473         struct cache_entry *ce;
3474         char *name = patch->new_name;
3475         unsigned mode = patch->new_mode;
3476
3477         if (!patch->is_new)
3478                 die("BUG: patch to %s is not a creation", patch->old_name);
3479
3480         pos = cache_name_pos(name, strlen(name));
3481         if (pos < 0)
3482                 return error(_("%s: does not exist in index"), name);
3483         ce = active_cache[pos];
3484         if (lstat(name, &st)) {
3485                 if (errno != ENOENT)
3486                         return error(_("%s: %s"), name, strerror(errno));
3487                 if (checkout_target(&the_index, ce, &st))
3488                         return -1;
3489         }
3490         if (verify_index_match(ce, &st))
3491                 return error(_("%s: does not match index"), name);
3492
3493         status = load_patch_target(state, &buf, ce, &st, name, mode);
3494         if (status < 0)
3495                 return status;
3496         else if (status)
3497                 return -1;
3498         img = strbuf_detach(&buf, &len);
3499         prepare_image(image, img, len, !patch->is_binary);
3500         return 0;
3501 }
3502
3503 static int try_threeway(struct apply_state *state,
3504                         struct image *image,
3505                         struct patch *patch,
3506                         struct stat *st,
3507                         const struct cache_entry *ce)
3508 {
3509         unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3510         struct strbuf buf = STRBUF_INIT;
3511         size_t len;
3512         int status;
3513         char *img;
3514         struct image tmp_image;
3515
3516         /* No point falling back to 3-way merge in these cases */
3517         if (patch->is_delete ||
3518             S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3519                 return -1;
3520
3521         /* Preimage the patch was prepared for */
3522         if (patch->is_new)
3523                 write_sha1_file("", 0, blob_type, pre_sha1);
3524         else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3525                  read_blob_object(&buf, pre_sha1, patch->old_mode))
3526                 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3527
3528         fprintf(stderr, "Falling back to three-way merge...\n");
3529
3530         img = strbuf_detach(&buf, &len);
3531         prepare_image(&tmp_image, img, len, 1);
3532         /* Apply the patch to get the post image */
3533         if (apply_fragments(state, &tmp_image, patch) < 0) {
3534                 clear_image(&tmp_image);
3535                 return -1;
3536         }
3537         /* post_sha1[] is theirs */
3538         write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3539         clear_image(&tmp_image);
3540
3541         /* our_sha1[] is ours */
3542         if (patch->is_new) {
3543                 if (load_current(state, &tmp_image, patch))
3544                         return error("cannot read the current contents of '%s'",
3545                                      patch->new_name);
3546         } else {
3547                 if (load_preimage(state, &tmp_image, patch, st, ce))
3548                         return error("cannot read the current contents of '%s'",
3549                                      patch->old_name);
3550         }
3551         write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3552         clear_image(&tmp_image);
3553
3554         /* in-core three-way merge between post and our using pre as base */
3555         status = three_way_merge(image, patch->new_name,
3556                                  pre_sha1, our_sha1, post_sha1);
3557         if (status < 0) {
3558                 fprintf(stderr, "Failed to fall back on three-way merge...\n");
3559                 return status;
3560         }
3561
3562         if (status) {
3563                 patch->conflicted_threeway = 1;
3564                 if (patch->is_new)
3565                         oidclr(&patch->threeway_stage[0]);
3566                 else
3567                         hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3568                 hashcpy(patch->threeway_stage[1].hash, our_sha1);
3569                 hashcpy(patch->threeway_stage[2].hash, post_sha1);
3570                 fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3571         } else {
3572                 fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3573         }
3574         return 0;
3575 }
3576
3577 static int apply_data(struct apply_state *state, struct patch *patch,
3578                       struct stat *st, const struct cache_entry *ce)
3579 {
3580         struct image image;
3581
3582         if (load_preimage(state, &image, patch, st, ce) < 0)
3583                 return -1;
3584
3585         if (patch->direct_to_threeway ||
3586             apply_fragments(state, &image, patch) < 0) {
3587                 /* Note: with --reject, apply_fragments() returns 0 */
3588                 if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
3589                         return -1;
3590         }
3591         patch->result = image.buf;
3592         patch->resultsize = image.len;
3593         add_to_fn_table(state, patch);
3594         free(image.line_allocated);
3595
3596         if (0 < patch->is_delete && patch->resultsize)
3597                 return error(_("removal patch leaves file contents"));
3598
3599         return 0;
3600 }
3601
3602 /*
3603  * If "patch" that we are looking at modifies or deletes what we have,
3604  * we would want it not to lose any local modification we have, either
3605  * in the working tree or in the index.
3606  *
3607  * This also decides if a non-git patch is a creation patch or a
3608  * modification to an existing empty file.  We do not check the state
3609  * of the current tree for a creation patch in this function; the caller
3610  * check_patch() separately makes sure (and errors out otherwise) that
3611  * the path the patch creates does not exist in the current tree.
3612  */
3613 static int check_preimage(struct apply_state *state,
3614                           struct patch *patch,
3615                           struct cache_entry **ce,
3616                           struct stat *st)
3617 {
3618         const char *old_name = patch->old_name;
3619         struct patch *previous = NULL;
3620         int stat_ret = 0, status;
3621         unsigned st_mode = 0;
3622
3623         if (!old_name)
3624                 return 0;
3625
3626         assert(patch->is_new <= 0);
3627         previous = previous_patch(state, patch, &status);
3628
3629         if (status)
3630                 return error(_("path %s has been renamed/deleted"), old_name);
3631         if (previous) {
3632                 st_mode = previous->new_mode;
3633         } else if (!state->cached) {
3634                 stat_ret = lstat(old_name, st);
3635                 if (stat_ret && errno != ENOENT)
3636                         return error(_("%s: %s"), old_name, strerror(errno));
3637         }
3638
3639         if (state->check_index && !previous) {
3640                 int pos = cache_name_pos(old_name, strlen(old_name));
3641                 if (pos < 0) {
3642                         if (patch->is_new < 0)
3643                                 goto is_new;
3644                         return error(_("%s: does not exist in index"), old_name);
3645                 }
3646                 *ce = active_cache[pos];
3647                 if (stat_ret < 0) {
3648                         if (checkout_target(&the_index, *ce, st))
3649                                 return -1;
3650                 }
3651                 if (!state->cached && verify_index_match(*ce, st))
3652                         return error(_("%s: does not match index"), old_name);
3653                 if (state->cached)
3654                         st_mode = (*ce)->ce_mode;
3655         } else if (stat_ret < 0) {
3656                 if (patch->is_new < 0)
3657                         goto is_new;
3658                 return error(_("%s: %s"), old_name, strerror(errno));
3659         }
3660
3661         if (!state->cached && !previous)
3662                 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3663
3664         if (patch->is_new < 0)
3665                 patch->is_new = 0;
3666         if (!patch->old_mode)
3667                 patch->old_mode = st_mode;
3668         if ((st_mode ^ patch->old_mode) & S_IFMT)
3669                 return error(_("%s: wrong type"), old_name);
3670         if (st_mode != patch->old_mode)
3671                 warning(_("%s has type %o, expected %o"),
3672                         old_name, st_mode, patch->old_mode);
3673         if (!patch->new_mode && !patch->is_delete)
3674                 patch->new_mode = st_mode;
3675         return 0;
3676
3677  is_new:
3678         patch->is_new = 1;
3679         patch->is_delete = 0;
3680         free(patch->old_name);
3681         patch->old_name = NULL;
3682         return 0;
3683 }
3684
3685
3686 #define EXISTS_IN_INDEX 1
3687 #define EXISTS_IN_WORKTREE 2
3688
3689 static int check_to_create(struct apply_state *state,
3690                            const char *new_name,
3691                            int ok_if_exists)
3692 {
3693         struct stat nst;
3694
3695         if (state->check_index &&
3696             cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3697             !ok_if_exists)
3698                 return EXISTS_IN_INDEX;
3699         if (state->cached)
3700                 return 0;
3701
3702         if (!lstat(new_name, &nst)) {
3703                 if (S_ISDIR(nst.st_mode) || ok_if_exists)
3704                         return 0;
3705                 /*
3706                  * A leading component of new_name might be a symlink
3707                  * that is going to be removed with this patch, but
3708                  * still pointing at somewhere that has the path.
3709                  * In such a case, path "new_name" does not exist as
3710                  * far as git is concerned.
3711                  */
3712                 if (has_symlink_leading_path(new_name, strlen(new_name)))
3713                         return 0;
3714
3715                 return EXISTS_IN_WORKTREE;
3716         } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3717                 return error("%s: %s", new_name, strerror(errno));
3718         }
3719         return 0;
3720 }
3721
3722 static uintptr_t register_symlink_changes(struct apply_state *state,
3723                                           const char *path,
3724                                           uintptr_t what)
3725 {
3726         struct string_list_item *ent;
3727
3728         ent = string_list_lookup(&state->symlink_changes, path);
3729         if (!ent) {
3730                 ent = string_list_insert(&state->symlink_changes, path);
3731                 ent->util = (void *)0;
3732         }
3733         ent->util = (void *)(what | ((uintptr_t)ent->util));
3734         return (uintptr_t)ent->util;
3735 }
3736
3737 static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3738 {
3739         struct string_list_item *ent;
3740
3741         ent = string_list_lookup(&state->symlink_changes, path);
3742         if (!ent)
3743                 return 0;
3744         return (uintptr_t)ent->util;
3745 }
3746
3747 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3748 {
3749         for ( ; patch; patch = patch->next) {
3750                 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3751                     (patch->is_rename || patch->is_delete))
3752                         /* the symlink at patch->old_name is removed */
3753                         register_symlink_changes(state, patch->old_name, SYMLINK_GOES_AWAY);
3754
3755                 if (patch->new_name && S_ISLNK(patch->new_mode))
3756                         /* the symlink at patch->new_name is created or remains */
3757                         register_symlink_changes(state, patch->new_name, SYMLINK_IN_RESULT);
3758         }
3759 }
3760
3761 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3762 {
3763         do {
3764                 unsigned int change;
3765
3766                 while (--name->len && name->buf[name->len] != '/')
3767                         ; /* scan backwards */
3768                 if (!name->len)
3769                         break;
3770                 name->buf[name->len] = '\0';
3771                 change = check_symlink_changes(state, name->buf);
3772                 if (change & SYMLINK_IN_RESULT)
3773                         return 1;
3774                 if (change & SYMLINK_GOES_AWAY)
3775                         /*
3776                          * This cannot be "return 0", because we may
3777                          * see a new one created at a higher level.
3778                          */
3779                         continue;
3780
3781                 /* otherwise, check the preimage */
3782                 if (state->check_index) {
3783                         struct cache_entry *ce;
3784
3785                         ce = cache_file_exists(name->buf, name->len, ignore_case);
3786                         if (ce && S_ISLNK(ce->ce_mode))
3787                                 return 1;
3788                 } else {
3789                         struct stat st;
3790                         if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3791                                 return 1;
3792                 }
3793         } while (1);
3794         return 0;
3795 }
3796
3797 static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3798 {
3799         int ret;
3800         struct strbuf name = STRBUF_INIT;
3801
3802         assert(*name_ != '\0');
3803         strbuf_addstr(&name, name_);
3804         ret = path_is_beyond_symlink_1(state, &name);
3805         strbuf_release(&name);
3806
3807         return ret;
3808 }
3809
3810 static void die_on_unsafe_path(struct patch *patch)
3811 {
3812         const char *old_name = NULL;
3813         const char *new_name = NULL;
3814         if (patch->is_delete)
3815                 old_name = patch->old_name;
3816         else if (!patch->is_new && !patch->is_copy)
3817                 old_name = patch->old_name;
3818         if (!patch->is_delete)
3819                 new_name = patch->new_name;
3820
3821         if (old_name && !verify_path(old_name))
3822                 die(_("invalid path '%s'"), old_name);
3823         if (new_name && !verify_path(new_name))
3824                 die(_("invalid path '%s'"), new_name);
3825 }
3826
3827 /*
3828  * Check and apply the patch in-core; leave the result in patch->result
3829  * for the caller to write it out to the final destination.
3830  */
3831 static int check_patch(struct apply_state *state, struct patch *patch)
3832 {
3833         struct stat st;
3834         const char *old_name = patch->old_name;
3835         const char *new_name = patch->new_name;
3836         const char *name = old_name ? old_name : new_name;
3837         struct cache_entry *ce = NULL;
3838         struct patch *tpatch;
3839         int ok_if_exists;
3840         int status;
3841
3842         patch->rejected = 1; /* we will drop this after we succeed */
3843
3844         status = check_preimage(state, patch, &ce, &st);
3845         if (status)
3846                 return status;
3847         old_name = patch->old_name;
3848
3849         /*
3850          * A type-change diff is always split into a patch to delete
3851          * old, immediately followed by a patch to create new (see
3852          * diff.c::run_diff()); in such a case it is Ok that the entry
3853          * to be deleted by the previous patch is still in the working
3854          * tree and in the index.
3855          *
3856          * A patch to swap-rename between A and B would first rename A
3857          * to B and then rename B to A.  While applying the first one,
3858          * the presence of B should not stop A from getting renamed to
3859          * B; ask to_be_deleted() about the later rename.  Removal of
3860          * B and rename from A to B is handled the same way by asking
3861          * was_deleted().
3862          */
3863         if ((tpatch = in_fn_table(state, new_name)) &&
3864             (was_deleted(tpatch) || to_be_deleted(tpatch)))
3865                 ok_if_exists = 1;
3866         else
3867                 ok_if_exists = 0;
3868
3869         if (new_name &&
3870             ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3871                 int err = check_to_create(state, new_name, ok_if_exists);
3872
3873                 if (err && state->threeway) {
3874                         patch->direct_to_threeway = 1;
3875                 } else switch (err) {
3876                 case 0:
3877                         break; /* happy */
3878                 case EXISTS_IN_INDEX:
3879                         return error(_("%s: already exists in index"), new_name);
3880                         break;
3881                 case EXISTS_IN_WORKTREE:
3882                         return error(_("%s: already exists in working directory"),
3883                                      new_name);
3884                 default:
3885                         return err;
3886                 }
3887
3888                 if (!patch->new_mode) {
3889                         if (0 < patch->is_new)
3890                                 patch->new_mode = S_IFREG | 0644;
3891                         else
3892                                 patch->new_mode = patch->old_mode;
3893                 }
3894         }
3895
3896         if (new_name && old_name) {
3897                 int same = !strcmp(old_name, new_name);
3898                 if (!patch->new_mode)
3899                         patch->new_mode = patch->old_mode;
3900                 if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3901                         if (same)
3902                                 return error(_("new mode (%o) of %s does not "
3903                                                "match old mode (%o)"),
3904                                         patch->new_mode, new_name,
3905                                         patch->old_mode);
3906                         else
3907                                 return error(_("new mode (%o) of %s does not "
3908                                                "match old mode (%o) of %s"),
3909                                         patch->new_mode, new_name,
3910                                         patch->old_mode, old_name);
3911                 }
3912         }
3913
3914         if (!state->unsafe_paths)
3915                 die_on_unsafe_path(patch);
3916
3917         /*
3918          * An attempt to read from or delete a path that is beyond a
3919          * symbolic link will be prevented by load_patch_target() that
3920          * is called at the beginning of apply_data() so we do not
3921          * have to worry about a patch marked with "is_delete" bit
3922          * here.  We however need to make sure that the patch result
3923          * is not deposited to a path that is beyond a symbolic link
3924          * here.
3925          */
3926         if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3927                 return error(_("affected file '%s' is beyond a symbolic link"),
3928                              patch->new_name);
3929
3930         if (apply_data(state, patch, &st, ce) < 0)
3931                 return error(_("%s: patch does not apply"), name);
3932         patch->rejected = 0;
3933         return 0;
3934 }
3935
3936 static int check_patch_list(struct apply_state *state, struct patch *patch)
3937 {
3938         int err = 0;
3939
3940         prepare_symlink_changes(state, patch);
3941         prepare_fn_table(state, patch);
3942         while (patch) {
3943                 if (state->apply_verbosely)
3944                         say_patch_name(stderr,
3945                                        _("Checking patch %s..."), patch);
3946                 err |= check_patch(state, patch);
3947                 patch = patch->next;
3948         }
3949         return err;
3950 }
3951
3952 /* This function tries to read the sha1 from the current index */
3953 static int get_current_sha1(const char *path, unsigned char *sha1)
3954 {
3955         int pos;
3956
3957         if (read_cache() < 0)
3958                 return -1;
3959         pos = cache_name_pos(path, strlen(path));
3960         if (pos < 0)
3961                 return -1;
3962         hashcpy(sha1, active_cache[pos]->sha1);
3963         return 0;
3964 }
3965
3966 static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3967 {
3968         /*
3969          * A usable gitlink patch has only one fragment (hunk) that looks like:
3970          * @@ -1 +1 @@
3971          * -Subproject commit <old sha1>
3972          * +Subproject commit <new sha1>
3973          * or
3974          * @@ -1 +0,0 @@
3975          * -Subproject commit <old sha1>
3976          * for a removal patch.
3977          */
3978         struct fragment *hunk = p->fragments;
3979         static const char heading[] = "-Subproject commit ";
3980         char *preimage;
3981
3982         if (/* does the patch have only one hunk? */
3983             hunk && !hunk->next &&
3984             /* is its preimage one line? */
3985             hunk->oldpos == 1 && hunk->oldlines == 1 &&
3986             /* does preimage begin with the heading? */
3987             (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3988             starts_with(++preimage, heading) &&
3989             /* does it record full SHA-1? */
3990             !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
3991             preimage[sizeof(heading) + 40 - 1] == '\n' &&
3992             /* does the abbreviated name on the index line agree with it? */
3993             starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3994                 return 0; /* it all looks fine */
3995
3996         /* we may have full object name on the index line */
3997         return get_sha1_hex(p->old_sha1_prefix, sha1);
3998 }
3999
4000 /* Build an index that contains the just the files needed for a 3way merge */
4001 static void build_fake_ancestor(struct patch *list, const char *filename)
4002 {
4003         struct patch *patch;
4004         struct index_state result = { NULL };
4005         static struct lock_file lock;
4006
4007         /* Once we start supporting the reverse patch, it may be
4008          * worth showing the new sha1 prefix, but until then...
4009          */
4010         for (patch = list; patch; patch = patch->next) {
4011                 unsigned char sha1[20];
4012                 struct cache_entry *ce;
4013                 const char *name;
4014
4015                 name = patch->old_name ? patch->old_name : patch->new_name;
4016                 if (0 < patch->is_new)
4017                         continue;
4018
4019                 if (S_ISGITLINK(patch->old_mode)) {
4020                         if (!preimage_sha1_in_gitlink_patch(patch, sha1))
4021                                 ; /* ok, the textual part looks sane */
4022                         else
4023                                 die("sha1 information is lacking or useless for submodule %s",
4024                                     name);
4025                 } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
4026                         ; /* ok */
4027                 } else if (!patch->lines_added && !patch->lines_deleted) {
4028                         /* mode-only change: update the current */
4029                         if (get_current_sha1(patch->old_name, sha1))
4030                                 die("mode change for %s, which is not "
4031                                     "in current HEAD", name);
4032                 } else
4033                         die("sha1 information is lacking or useless "
4034                             "(%s).", name);
4035
4036                 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
4037                 if (!ce)
4038                         die(_("make_cache_entry failed for path '%s'"), name);
4039                 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
4040                         die ("Could not add %s to temporary index", name);
4041         }
4042
4043         hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
4044         if (write_locked_index(&result, &lock, COMMIT_LOCK))
4045                 die ("Could not write temporary index to %s", filename);
4046
4047         discard_index(&result);
4048 }
4049
4050 static void stat_patch_list(struct apply_state *state, struct patch *patch)
4051 {
4052         int files, adds, dels;
4053
4054         for (files = adds = dels = 0 ; patch ; patch = patch->next) {
4055                 files++;
4056                 adds += patch->lines_added;
4057                 dels += patch->lines_deleted;
4058                 show_stats(state, patch);
4059         }
4060
4061         print_stat_summary(stdout, files, adds, dels);
4062 }
4063
4064 static void numstat_patch_list(struct apply_state *state,
4065                                struct patch *patch)
4066 {
4067         for ( ; patch; patch = patch->next) {
4068                 const char *name;
4069                 name = patch->new_name ? patch->new_name : patch->old_name;
4070                 if (patch->is_binary)
4071                         printf("-\t-\t");
4072                 else
4073                         printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
4074                 write_name_quoted(name, stdout, state->line_termination);
4075         }
4076 }
4077
4078 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
4079 {
4080         if (mode)
4081                 printf(" %s mode %06o %s\n", newdelete, mode, name);
4082         else
4083                 printf(" %s %s\n", newdelete, name);
4084 }
4085
4086 static void show_mode_change(struct patch *p, int show_name)
4087 {
4088         if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
4089                 if (show_name)
4090                         printf(" mode change %06o => %06o %s\n",
4091                                p->old_mode, p->new_mode, p->new_name);
4092                 else
4093                         printf(" mode change %06o => %06o\n",
4094                                p->old_mode, p->new_mode);
4095         }
4096 }
4097
4098 static void show_rename_copy(struct patch *p)
4099 {
4100         const char *renamecopy = p->is_rename ? "rename" : "copy";
4101         const char *old, *new;
4102
4103         /* Find common prefix */
4104         old = p->old_name;
4105         new = p->new_name;
4106         while (1) {
4107                 const char *slash_old, *slash_new;
4108                 slash_old = strchr(old, '/');
4109                 slash_new = strchr(new, '/');
4110                 if (!slash_old ||
4111                     !slash_new ||
4112                     slash_old - old != slash_new - new ||
4113                     memcmp(old, new, slash_new - new))
4114                         break;
4115                 old = slash_old + 1;
4116                 new = slash_new + 1;
4117         }
4118         /* p->old_name thru old is the common prefix, and old and new
4119          * through the end of names are renames
4120          */
4121         if (old != p->old_name)
4122                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4123                        (int)(old - p->old_name), p->old_name,
4124                        old, new, p->score);
4125         else
4126                 printf(" %s %s => %s (%d%%)\n", renamecopy,
4127                        p->old_name, p->new_name, p->score);
4128         show_mode_change(p, 0);
4129 }
4130
4131 static void summary_patch_list(struct patch *patch)
4132 {
4133         struct patch *p;
4134
4135         for (p = patch; p; p = p->next) {
4136                 if (p->is_new)
4137                         show_file_mode_name("create", p->new_mode, p->new_name);
4138                 else if (p->is_delete)
4139                         show_file_mode_name("delete", p->old_mode, p->old_name);
4140                 else {
4141                         if (p->is_rename || p->is_copy)
4142                                 show_rename_copy(p);
4143                         else {
4144                                 if (p->score) {
4145                                         printf(" rewrite %s (%d%%)\n",
4146                                                p->new_name, p->score);
4147                                         show_mode_change(p, 0);
4148                                 }
4149                                 else
4150                                         show_mode_change(p, 1);
4151                         }
4152                 }
4153         }
4154 }
4155
4156 static void patch_stats(struct apply_state *state, struct patch *patch)
4157 {
4158         int lines = patch->lines_added + patch->lines_deleted;
4159
4160         if (lines > state->max_change)
4161                 state->max_change = lines;
4162         if (patch->old_name) {
4163                 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4164                 if (!len)
4165                         len = strlen(patch->old_name);
4166                 if (len > state->max_len)
4167                         state->max_len = len;
4168         }
4169         if (patch->new_name) {
4170                 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4171                 if (!len)
4172                         len = strlen(patch->new_name);
4173                 if (len > state->max_len)
4174                         state->max_len = len;
4175         }
4176 }
4177
4178 static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4179 {
4180         if (state->update_index) {
4181                 if (remove_file_from_cache(patch->old_name) < 0)
4182                         die(_("unable to remove %s from index"), patch->old_name);
4183         }
4184         if (!state->cached) {
4185                 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4186                         remove_path(patch->old_name);
4187                 }
4188         }
4189 }
4190
4191 static void add_index_file(struct apply_state *state,
4192                            const char *path,
4193                            unsigned mode,
4194                            void *buf,
4195                            unsigned long size)
4196 {
4197         struct stat st;
4198         struct cache_entry *ce;
4199         int namelen = strlen(path);
4200         unsigned ce_size = cache_entry_size(namelen);
4201
4202         if (!state->update_index)
4203                 return;
4204
4205         ce = xcalloc(1, ce_size);
4206         memcpy(ce->name, path, namelen);
4207         ce->ce_mode = create_ce_mode(mode);
4208         ce->ce_flags = create_ce_flags(0);
4209         ce->ce_namelen = namelen;
4210         if (S_ISGITLINK(mode)) {
4211                 const char *s;
4212
4213                 if (!skip_prefix(buf, "Subproject commit ", &s) ||
4214                     get_sha1_hex(s, ce->sha1))
4215                         die(_("corrupt patch for submodule %s"), path);
4216         } else {
4217                 if (!state->cached) {
4218                         if (lstat(path, &st) < 0)
4219                                 die_errno(_("unable to stat newly created file '%s'"),
4220                                           path);
4221                         fill_stat_cache_info(ce, &st);
4222                 }
4223                 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
4224                         die(_("unable to create backing store for newly created file %s"), path);
4225         }
4226         if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4227                 die(_("unable to add cache entry for %s"), path);
4228 }
4229
4230 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4231 {
4232         int fd;
4233         struct strbuf nbuf = STRBUF_INIT;
4234
4235         if (S_ISGITLINK(mode)) {
4236                 struct stat st;
4237                 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4238                         return 0;
4239                 return mkdir(path, 0777);
4240         }
4241
4242         if (has_symlinks && S_ISLNK(mode))
4243                 /* Although buf:size is counted string, it also is NUL
4244                  * terminated.
4245                  */
4246                 return symlink(buf, path);
4247
4248         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4249         if (fd < 0)
4250                 return -1;
4251
4252         if (convert_to_working_tree(path, buf, size, &nbuf)) {
4253                 size = nbuf.len;
4254                 buf  = nbuf.buf;
4255         }
4256         write_or_die(fd, buf, size);
4257         strbuf_release(&nbuf);
4258
4259         if (close(fd) < 0)
4260                 die_errno(_("closing file '%s'"), path);
4261         return 0;
4262 }
4263
4264 /*
4265  * We optimistically assume that the directories exist,
4266  * which is true 99% of the time anyway. If they don't,
4267  * we create them and try again.
4268  */
4269 static void create_one_file(struct apply_state *state,
4270                             char *path,
4271                             unsigned mode,
4272                             const char *buf,
4273                             unsigned long size)
4274 {
4275         if (state->cached)
4276                 return;
4277         if (!try_create_file(path, mode, buf, size))
4278                 return;
4279
4280         if (errno == ENOENT) {
4281                 if (safe_create_leading_directories(path))
4282                         return;
4283                 if (!try_create_file(path, mode, buf, size))
4284                         return;
4285         }
4286
4287         if (errno == EEXIST || errno == EACCES) {
4288                 /* We may be trying to create a file where a directory
4289                  * used to be.
4290                  */
4291                 struct stat st;
4292                 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4293                         errno = EEXIST;
4294         }
4295
4296         if (errno == EEXIST) {
4297                 unsigned int nr = getpid();
4298
4299                 for (;;) {
4300                         char newpath[PATH_MAX];
4301                         mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4302                         if (!try_create_file(newpath, mode, buf, size)) {
4303                                 if (!rename(newpath, path))
4304                                         return;
4305                                 unlink_or_warn(newpath);
4306                                 break;
4307                         }
4308                         if (errno != EEXIST)
4309                                 break;
4310                         ++nr;
4311                 }
4312         }
4313         die_errno(_("unable to write file '%s' mode %o"), path, mode);
4314 }
4315
4316 static void add_conflicted_stages_file(struct apply_state *state,
4317                                        struct patch *patch)
4318 {
4319         int stage, namelen;
4320         unsigned ce_size, mode;
4321         struct cache_entry *ce;
4322
4323         if (!state->update_index)
4324                 return;
4325         namelen = strlen(patch->new_name);
4326         ce_size = cache_entry_size(namelen);
4327         mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4328
4329         remove_file_from_cache(patch->new_name);
4330         for (stage = 1; stage < 4; stage++) {
4331                 if (is_null_oid(&patch->threeway_stage[stage - 1]))
4332                         continue;
4333                 ce = xcalloc(1, ce_size);
4334                 memcpy(ce->name, patch->new_name, namelen);
4335                 ce->ce_mode = create_ce_mode(mode);
4336                 ce->ce_flags = create_ce_flags(stage);
4337                 ce->ce_namelen = namelen;
4338                 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4339                 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
4340                         die(_("unable to add cache entry for %s"), patch->new_name);
4341         }
4342 }
4343
4344 static void create_file(struct apply_state *state, struct patch *patch)
4345 {
4346         char *path = patch->new_name;
4347         unsigned mode = patch->new_mode;
4348         unsigned long size = patch->resultsize;
4349         char *buf = patch->result;
4350
4351         if (!mode)
4352                 mode = S_IFREG | 0644;
4353         create_one_file(state, path, mode, buf, size);
4354
4355         if (patch->conflicted_threeway)
4356                 add_conflicted_stages_file(state, patch);
4357         else
4358                 add_index_file(state, path, mode, buf, size);
4359 }
4360
4361 /* phase zero is to remove, phase one is to create */
4362 static void write_out_one_result(struct apply_state *state,
4363                                  struct patch *patch,
4364                                  int phase)
4365 {
4366         if (patch->is_delete > 0) {
4367                 if (phase == 0)
4368                         remove_file(state, patch, 1);
4369                 return;
4370         }
4371         if (patch->is_new > 0 || patch->is_copy) {
4372                 if (phase == 1)
4373                         create_file(state, patch);
4374                 return;
4375         }
4376         /*
4377          * Rename or modification boils down to the same
4378          * thing: remove the old, write the new
4379          */
4380         if (phase == 0)
4381                 remove_file(state, patch, patch->is_rename);
4382         if (phase == 1)
4383                 create_file(state, patch);
4384 }
4385
4386 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4387 {
4388         FILE *rej;
4389         char namebuf[PATH_MAX];
4390         struct fragment *frag;
4391         int cnt = 0;
4392         struct strbuf sb = STRBUF_INIT;
4393
4394         for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4395                 if (!frag->rejected)
4396                         continue;
4397                 cnt++;
4398         }
4399
4400         if (!cnt) {
4401                 if (state->apply_verbosely)
4402                         say_patch_name(stderr,
4403                                        _("Applied patch %s cleanly."), patch);
4404                 return 0;
4405         }
4406
4407         /* This should not happen, because a removal patch that leaves
4408          * contents are marked "rejected" at the patch level.
4409          */
4410         if (!patch->new_name)
4411                 die(_("internal error"));
4412
4413         /* Say this even without --verbose */
4414         strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4415                             "Applying patch %%s with %d rejects...",
4416                             cnt),
4417                     cnt);
4418         say_patch_name(stderr, sb.buf, patch);
4419         strbuf_release(&sb);
4420
4421         cnt = strlen(patch->new_name);
4422         if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4423                 cnt = ARRAY_SIZE(namebuf) - 5;
4424                 warning(_("truncating .rej filename to %.*s.rej"),
4425                         cnt - 1, patch->new_name);
4426         }
4427         memcpy(namebuf, patch->new_name, cnt);
4428         memcpy(namebuf + cnt, ".rej", 5);
4429
4430         rej = fopen(namebuf, "w");
4431         if (!rej)
4432                 return error(_("cannot open %s: %s"), namebuf, strerror(errno));
4433
4434         /* Normal git tools never deal with .rej, so do not pretend
4435          * this is a git patch by saying --git or giving extended
4436          * headers.  While at it, maybe please "kompare" that wants
4437          * the trailing TAB and some garbage at the end of line ;-).
4438          */
4439         fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4440                 patch->new_name, patch->new_name);
4441         for (cnt = 1, frag = patch->fragments;
4442              frag;
4443              cnt++, frag = frag->next) {
4444                 if (!frag->rejected) {
4445                         fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4446                         continue;
4447                 }
4448                 fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4449                 fprintf(rej, "%.*s", frag->size, frag->patch);
4450                 if (frag->patch[frag->size-1] != '\n')
4451                         fputc('\n', rej);
4452         }
4453         fclose(rej);
4454         return -1;
4455 }
4456
4457 static int write_out_results(struct apply_state *state, struct patch *list)
4458 {
4459         int phase;
4460         int errs = 0;
4461         struct patch *l;
4462         struct string_list cpath = STRING_LIST_INIT_DUP;
4463
4464         for (phase = 0; phase < 2; phase++) {
4465                 l = list;
4466                 while (l) {
4467                         if (l->rejected)
4468                                 errs = 1;
4469                         else {
4470                                 write_out_one_result(state, l, phase);
4471                                 if (phase == 1) {
4472                                         if (write_out_one_reject(state, l))
4473                                                 errs = 1;
4474                                         if (l->conflicted_threeway) {
4475                                                 string_list_append(&cpath, l->new_name);
4476                                                 errs = 1;
4477                                         }
4478                                 }
4479                         }
4480                         l = l->next;
4481                 }
4482         }
4483
4484         if (cpath.nr) {
4485                 struct string_list_item *item;
4486
4487                 string_list_sort(&cpath);
4488                 for_each_string_list_item(item, &cpath)
4489                         fprintf(stderr, "U %s\n", item->string);
4490                 string_list_clear(&cpath, 0);
4491
4492                 rerere(0);
4493         }
4494
4495         return errs;
4496 }
4497
4498 static struct lock_file lock_file;
4499
4500 #define INACCURATE_EOF  (1<<0)
4501 #define RECOUNT         (1<<1)
4502
4503 static int apply_patch(struct apply_state *state,
4504                        int fd,
4505                        const char *filename,
4506                        int options)
4507 {
4508         size_t offset;
4509         struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4510         struct patch *list = NULL, **listp = &list;
4511         int skipped_patch = 0;
4512
4513         state->patch_input_file = filename;
4514         read_patch_file(&buf, fd);
4515         offset = 0;
4516         while (offset < buf.len) {
4517                 struct patch *patch;
4518                 int nr;
4519
4520                 patch = xcalloc(1, sizeof(*patch));
4521                 patch->inaccurate_eof = !!(options & INACCURATE_EOF);
4522                 patch->recount =  !!(options & RECOUNT);
4523                 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4524                 if (nr < 0) {
4525                         free_patch(patch);
4526                         break;
4527                 }
4528                 if (state->apply_in_reverse)
4529                         reverse_patches(patch);
4530                 if (use_patch(state, patch)) {
4531                         patch_stats(state, patch);
4532                         *listp = patch;
4533                         listp = &patch->next;
4534                 }
4535                 else {
4536                         if (state->apply_verbosely)
4537                                 say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4538                         free_patch(patch);
4539                         skipped_patch++;
4540                 }
4541                 offset += nr;
4542         }
4543
4544         if (!list && !skipped_patch)
4545                 die(_("unrecognized input"));
4546
4547         if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4548                 state->apply = 0;
4549
4550         state->update_index = state->check_index && state->apply;
4551         if (state->update_index && state->newfd < 0)
4552                 state->newfd = hold_locked_index(state->lock_file, 1);
4553
4554         if (state->check_index) {
4555                 if (read_cache() < 0)
4556                         die(_("unable to read index file"));
4557         }
4558
4559         if ((state->check || state->apply) &&
4560             check_patch_list(state, list) < 0 &&
4561             !state->apply_with_reject)
4562                 exit(1);
4563
4564         if (state->apply && write_out_results(state, list)) {
4565                 if (state->apply_with_reject)
4566                         exit(1);
4567                 /* with --3way, we still need to write the index out */
4568                 return 1;
4569         }
4570
4571         if (state->fake_ancestor)
4572                 build_fake_ancestor(list, state->fake_ancestor);
4573
4574         if (state->diffstat)
4575                 stat_patch_list(state, list);
4576
4577         if (state->numstat)
4578                 numstat_patch_list(state, list);
4579
4580         if (state->summary)
4581                 summary_patch_list(list);
4582
4583         free_patch_list(list);
4584         strbuf_release(&buf);
4585         string_list_clear(&state->fn_table, 0);
4586         return 0;
4587 }
4588
4589 static void git_apply_config(void)
4590 {
4591         git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
4592         git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
4593         git_config(git_default_config, NULL);
4594 }
4595
4596 static int option_parse_exclude(const struct option *opt,
4597                                 const char *arg, int unset)
4598 {
4599         struct apply_state *state = opt->value;
4600         add_name_limit(state, arg, 1);
4601         return 0;
4602 }
4603
4604 static int option_parse_include(const struct option *opt,
4605                                 const char *arg, int unset)
4606 {
4607         struct apply_state *state = opt->value;
4608         add_name_limit(state, arg, 0);
4609         state->has_include = 1;
4610         return 0;
4611 }
4612
4613 static int option_parse_p(const struct option *opt,
4614                           const char *arg,
4615                           int unset)
4616 {
4617         struct apply_state *state = opt->value;
4618         state->p_value = atoi(arg);
4619         state->p_value_known = 1;
4620         return 0;
4621 }
4622
4623 static int option_parse_space_change(const struct option *opt,
4624                                      const char *arg, int unset)
4625 {
4626         struct apply_state *state = opt->value;
4627         if (unset)
4628                 state->ws_ignore_action = ignore_ws_none;
4629         else
4630                 state->ws_ignore_action = ignore_ws_change;
4631         return 0;
4632 }
4633
4634 static int option_parse_whitespace(const struct option *opt,
4635                                    const char *arg, int unset)
4636 {
4637         struct apply_state *state = opt->value;
4638         state->whitespace_option = arg;
4639         parse_whitespace_option(state, arg);
4640         return 0;
4641 }
4642
4643 static int option_parse_directory(const struct option *opt,
4644                                   const char *arg, int unset)
4645 {
4646         struct apply_state *state = opt->value;
4647         strbuf_reset(&state->root);
4648         strbuf_addstr(&state->root, arg);
4649         strbuf_complete(&state->root, '/');
4650         return 0;
4651 }
4652
4653 static void init_apply_state(struct apply_state *state,
4654                              const char *prefix,
4655                              struct lock_file *lock_file)
4656 {
4657         memset(state, 0, sizeof(*state));
4658         state->prefix = prefix;
4659         state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4660         state->lock_file = lock_file;
4661         state->newfd = -1;
4662         state->apply = 1;
4663         state->line_termination = '\n';
4664         state->p_value = 1;
4665         state->p_context = UINT_MAX;
4666         state->squelch_whitespace_errors = 5;
4667         state->ws_error_action = warn_on_ws_error;
4668         state->ws_ignore_action = ignore_ws_none;
4669         state->linenr = 1;
4670         strbuf_init(&state->root, 0);
4671
4672         git_apply_config();
4673         if (apply_default_whitespace)
4674                 parse_whitespace_option(state, apply_default_whitespace);
4675         if (apply_default_ignorewhitespace)
4676                 parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
4677 }
4678
4679 static void clear_apply_state(struct apply_state *state)
4680 {
4681         string_list_clear(&state->limit_by_name, 0);
4682         string_list_clear(&state->symlink_changes, 0);
4683         strbuf_release(&state->root);
4684
4685         /* &state->fn_table is cleared at the end of apply_patch() */
4686 }
4687
4688 static void check_apply_state(struct apply_state *state, int force_apply)
4689 {
4690         int is_not_gitdir = !startup_info->have_repository;
4691
4692         if (state->apply_with_reject && state->threeway)
4693                 die("--reject and --3way cannot be used together.");
4694         if (state->cached && state->threeway)
4695                 die("--cached and --3way cannot be used together.");
4696         if (state->threeway) {
4697                 if (is_not_gitdir)
4698                         die(_("--3way outside a repository"));
4699                 state->check_index = 1;
4700         }
4701         if (state->apply_with_reject)
4702                 state->apply = state->apply_verbosely = 1;
4703         if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
4704                 state->apply = 0;
4705         if (state->check_index && is_not_gitdir)
4706                 die(_("--index outside a repository"));
4707         if (state->cached) {
4708                 if (is_not_gitdir)
4709                         die(_("--cached outside a repository"));
4710                 state->check_index = 1;
4711         }
4712         if (state->check_index)
4713                 state->unsafe_paths = 0;
4714         if (!state->lock_file)
4715                 die("BUG: state->lock_file should not be NULL");
4716 }
4717
4718 static int apply_all_patches(struct apply_state *state,
4719                              int argc,
4720                              const char **argv,
4721                              int options)
4722 {
4723         int i;
4724         int errs = 0;
4725         int read_stdin = 1;
4726
4727         for (i = 0; i < argc; i++) {
4728                 const char *arg = argv[i];
4729                 int fd;
4730
4731                 if (!strcmp(arg, "-")) {
4732                         errs |= apply_patch(state, 0, "<stdin>", options);
4733                         read_stdin = 0;
4734                         continue;
4735                 } else if (0 < state->prefix_length)
4736                         arg = prefix_filename(state->prefix,
4737                                               state->prefix_length,
4738                                               arg);
4739
4740                 fd = open(arg, O_RDONLY);
4741                 if (fd < 0)
4742                         die_errno(_("can't open patch '%s'"), arg);
4743                 read_stdin = 0;
4744                 set_default_whitespace_mode(state);
4745                 errs |= apply_patch(state, fd, arg, options);
4746                 close(fd);
4747         }
4748         set_default_whitespace_mode(state);
4749         if (read_stdin)
4750                 errs |= apply_patch(state, 0, "<stdin>", options);
4751
4752         if (state->whitespace_error) {
4753                 if (state->squelch_whitespace_errors &&
4754                     state->squelch_whitespace_errors < state->whitespace_error) {
4755                         int squelched =
4756                                 state->whitespace_error - state->squelch_whitespace_errors;
4757                         warning(Q_("squelched %d whitespace error",
4758                                    "squelched %d whitespace errors",
4759                                    squelched),
4760                                 squelched);
4761                 }
4762                 if (state->ws_error_action == die_on_ws_error)
4763                         die(Q_("%d line adds whitespace errors.",
4764                                "%d lines add whitespace errors.",
4765                                state->whitespace_error),
4766                             state->whitespace_error);
4767                 if (state->applied_after_fixing_ws && state->apply)
4768                         warning("%d line%s applied after"
4769                                 " fixing whitespace errors.",
4770                                 state->applied_after_fixing_ws,
4771                                 state->applied_after_fixing_ws == 1 ? "" : "s");
4772                 else if (state->whitespace_error)
4773                         warning(Q_("%d line adds whitespace errors.",
4774                                    "%d lines add whitespace errors.",
4775                                    state->whitespace_error),
4776                                 state->whitespace_error);
4777         }
4778
4779         if (state->update_index) {
4780                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
4781                         die(_("Unable to write new index file"));
4782                 state->newfd = -1;
4783         }
4784
4785         return !!errs;
4786 }
4787
4788 int cmd_apply(int argc, const char **argv, const char *prefix)
4789 {
4790         int force_apply = 0;
4791         int options = 0;
4792         int ret;
4793         struct apply_state state;
4794
4795         struct option builtin_apply_options[] = {
4796                 { OPTION_CALLBACK, 0, "exclude", &state, N_("path"),
4797                         N_("don't apply changes matching the given path"),
4798                         0, option_parse_exclude },
4799                 { OPTION_CALLBACK, 0, "include", &state, N_("path"),
4800                         N_("apply changes matching the given path"),
4801                         0, option_parse_include },
4802                 { OPTION_CALLBACK, 'p', NULL, &state, N_("num"),
4803                         N_("remove <num> leading slashes from traditional diff paths"),
4804                         0, option_parse_p },
4805                 OPT_BOOL(0, "no-add", &state.no_add,
4806                         N_("ignore additions made by the patch")),
4807                 OPT_BOOL(0, "stat", &state.diffstat,
4808                         N_("instead of applying the patch, output diffstat for the input")),
4809                 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
4810                 OPT_NOOP_NOARG(0, "binary"),
4811                 OPT_BOOL(0, "numstat", &state.numstat,
4812                         N_("show number of added and deleted lines in decimal notation")),
4813                 OPT_BOOL(0, "summary", &state.summary,
4814                         N_("instead of applying the patch, output a summary for the input")),
4815                 OPT_BOOL(0, "check", &state.check,
4816                         N_("instead of applying the patch, see if the patch is applicable")),
4817                 OPT_BOOL(0, "index", &state.check_index,
4818                         N_("make sure the patch is applicable to the current index")),
4819                 OPT_BOOL(0, "cached", &state.cached,
4820                         N_("apply a patch without touching the working tree")),
4821                 OPT_BOOL(0, "unsafe-paths", &state.unsafe_paths,
4822                         N_("accept a patch that touches outside the working area")),
4823                 OPT_BOOL(0, "apply", &force_apply,
4824                         N_("also apply the patch (use with --stat/--summary/--check)")),
4825                 OPT_BOOL('3', "3way", &state.threeway,
4826                          N_( "attempt three-way merge if a patch does not apply")),
4827                 OPT_FILENAME(0, "build-fake-ancestor", &state.fake_ancestor,
4828                         N_("build a temporary index based on embedded index information")),
4829                 /* Think twice before adding "--nul" synonym to this */
4830                 OPT_SET_INT('z', NULL, &state.line_termination,
4831                         N_("paths are separated with NUL character"), '\0'),
4832                 OPT_INTEGER('C', NULL, &state.p_context,
4833                                 N_("ensure at least <n> lines of context match")),
4834                 { OPTION_CALLBACK, 0, "whitespace", &state, N_("action"),
4835                         N_("detect new or modified lines that have whitespace errors"),
4836                         0, option_parse_whitespace },
4837                 { OPTION_CALLBACK, 0, "ignore-space-change", &state, NULL,
4838                         N_("ignore changes in whitespace when finding context"),
4839                         PARSE_OPT_NOARG, option_parse_space_change },
4840                 { OPTION_CALLBACK, 0, "ignore-whitespace", &state, NULL,
4841                         N_("ignore changes in whitespace when finding context"),
4842                         PARSE_OPT_NOARG, option_parse_space_change },
4843                 OPT_BOOL('R', "reverse", &state.apply_in_reverse,
4844                         N_("apply the patch in reverse")),
4845                 OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
4846                         N_("don't expect at least one line of context")),
4847                 OPT_BOOL(0, "reject", &state.apply_with_reject,
4848                         N_("leave the rejected hunks in corresponding *.rej files")),
4849                 OPT_BOOL(0, "allow-overlap", &state.allow_overlap,
4850                         N_("allow overlapping hunks")),
4851                 OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
4852                 OPT_BIT(0, "inaccurate-eof", &options,
4853                         N_("tolerate incorrectly detected missing new-line at the end of file"),
4854                         INACCURATE_EOF),
4855                 OPT_BIT(0, "recount", &options,
4856                         N_("do not trust the line counts in the hunk headers"),
4857                         RECOUNT),
4858                 { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
4859                         N_("prepend <root> to all filenames"),
4860                         0, option_parse_directory },
4861                 OPT_END()
4862         };
4863
4864         init_apply_state(&state, prefix, &lock_file);
4865
4866         argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4867                         apply_usage, 0);
4868
4869         check_apply_state(&state, force_apply);
4870
4871         ret = apply_all_patches(&state, argc, argv, options);
4872
4873         clear_apply_state(&state);
4874
4875         return ret;
4876 }