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