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