Merge branch 'maint-1.5.1' into maint
[git] / builtin-apply.c
1 /*
2  * apply.c
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  *
6  * This applies patches on top of some (arbitrary) version of the SCM.
7  *
8  */
9 #include "cache.h"
10 #include "cache-tree.h"
11 #include "quote.h"
12 #include "blob.h"
13 #include "delta.h"
14 #include "builtin.h"
15
16 /*
17  *  --check turns on checking that the working tree matches the
18  *    files that are being modified, but doesn't apply the patch
19  *  --stat does just a diffstat, and doesn't actually apply
20  *  --numstat does numeric diffstat, and doesn't actually apply
21  *  --index-info shows the old and new index info for paths if available.
22  *  --index updates the cache as well.
23  *  --cached updates only the cache without ever touching the working tree.
24  */
25 static const char *prefix;
26 static int prefix_length = -1;
27 static int newfd = -1;
28
29 static int unidiff_zero;
30 static int p_value = 1;
31 static int p_value_known;
32 static int check_index;
33 static int update_index;
34 static int cached;
35 static int diffstat;
36 static int numstat;
37 static int summary;
38 static int check;
39 static int apply = 1;
40 static int apply_in_reverse;
41 static int apply_with_reject;
42 static int apply_verbosely;
43 static int no_add;
44 static int show_index_info;
45 static int line_termination = '\n';
46 static unsigned long p_context = ULONG_MAX;
47 static const char apply_usage[] =
48 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [--reject] [--verbose] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
49
50 static enum whitespace_eol {
51         nowarn_whitespace,
52         warn_on_whitespace,
53         error_on_whitespace,
54         strip_whitespace,
55 } new_whitespace = warn_on_whitespace;
56 static int whitespace_error;
57 static int squelch_whitespace_errors = 5;
58 static int applied_after_stripping;
59 static const char *patch_input_file;
60
61 static void parse_whitespace_option(const char *option)
62 {
63         if (!option) {
64                 new_whitespace = warn_on_whitespace;
65                 return;
66         }
67         if (!strcmp(option, "warn")) {
68                 new_whitespace = warn_on_whitespace;
69                 return;
70         }
71         if (!strcmp(option, "nowarn")) {
72                 new_whitespace = nowarn_whitespace;
73                 return;
74         }
75         if (!strcmp(option, "error")) {
76                 new_whitespace = error_on_whitespace;
77                 return;
78         }
79         if (!strcmp(option, "error-all")) {
80                 new_whitespace = error_on_whitespace;
81                 squelch_whitespace_errors = 0;
82                 return;
83         }
84         if (!strcmp(option, "strip")) {
85                 new_whitespace = strip_whitespace;
86                 return;
87         }
88         die("unrecognized whitespace option '%s'", option);
89 }
90
91 static void set_default_whitespace_mode(const char *whitespace_option)
92 {
93         if (!whitespace_option && !apply_default_whitespace) {
94                 new_whitespace = (apply
95                                   ? warn_on_whitespace
96                                   : nowarn_whitespace);
97         }
98 }
99
100 /*
101  * For "diff-stat" like behaviour, we keep track of the biggest change
102  * we've seen, and the longest filename. That allows us to do simple
103  * scaling.
104  */
105 static int max_change, max_len;
106
107 /*
108  * Various "current state", notably line numbers and what
109  * file (and how) we're patching right now.. The "is_xxxx"
110  * things are flags, where -1 means "don't know yet".
111  */
112 static int linenr = 1;
113
114 /*
115  * This represents one "hunk" from a patch, starting with
116  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
117  * patch text is pointed at by patch, and its byte length
118  * is stored in size.  leading and trailing are the number
119  * of context lines.
120  */
121 struct fragment {
122         unsigned long leading, trailing;
123         unsigned long oldpos, oldlines;
124         unsigned long newpos, newlines;
125         const char *patch;
126         int size;
127         int rejected;
128         struct fragment *next;
129 };
130
131 /*
132  * When dealing with a binary patch, we reuse "leading" field
133  * to store the type of the binary hunk, either deflated "delta"
134  * or deflated "literal".
135  */
136 #define binary_patch_method leading
137 #define BINARY_DELTA_DEFLATED   1
138 #define BINARY_LITERAL_DEFLATED 2
139
140 struct patch {
141         char *new_name, *old_name, *def_name;
142         unsigned int old_mode, new_mode;
143         int is_new, is_delete;  /* -1 = unknown, 0 = false, 1 = true */
144         int rejected;
145         unsigned long deflate_origlen;
146         int lines_added, lines_deleted;
147         int score;
148         unsigned int is_toplevel_relative:1;
149         unsigned int inaccurate_eof:1;
150         unsigned int is_binary:1;
151         unsigned int is_copy:1;
152         unsigned int is_rename:1;
153         struct fragment *fragments;
154         char *result;
155         unsigned long resultsize;
156         char old_sha1_prefix[41];
157         char new_sha1_prefix[41];
158         struct patch *next;
159 };
160
161 static void say_patch_name(FILE *output, const char *pre, struct patch *patch, const char *post)
162 {
163         fputs(pre, output);
164         if (patch->old_name && patch->new_name &&
165             strcmp(patch->old_name, patch->new_name)) {
166                 write_name_quoted(NULL, 0, patch->old_name, 1, output);
167                 fputs(" => ", output);
168                 write_name_quoted(NULL, 0, patch->new_name, 1, output);
169         }
170         else {
171                 const char *n = patch->new_name;
172                 if (!n)
173                         n = patch->old_name;
174                 write_name_quoted(NULL, 0, n, 1, output);
175         }
176         fputs(post, output);
177 }
178
179 #define CHUNKSIZE (8192)
180 #define SLOP (16)
181
182 static void *read_patch_file(int fd, unsigned long *sizep)
183 {
184         unsigned long size = 0, alloc = CHUNKSIZE;
185         void *buffer = xmalloc(alloc);
186
187         for (;;) {
188                 ssize_t nr = alloc - size;
189                 if (nr < 1024) {
190                         alloc += CHUNKSIZE;
191                         buffer = xrealloc(buffer, alloc);
192                         nr = alloc - size;
193                 }
194                 nr = xread(fd, (char *) buffer + size, nr);
195                 if (!nr)
196                         break;
197                 if (nr < 0)
198                         die("git-apply: read returned %s", strerror(errno));
199                 size += nr;
200         }
201         *sizep = size;
202
203         /*
204          * Make sure that we have some slop in the buffer
205          * so that we can do speculative "memcmp" etc, and
206          * see to it that it is NUL-filled.
207          */
208         if (alloc < size + SLOP)
209                 buffer = xrealloc(buffer, size + SLOP);
210         memset((char *) buffer + size, 0, SLOP);
211         return buffer;
212 }
213
214 static unsigned long linelen(const char *buffer, unsigned long size)
215 {
216         unsigned long len = 0;
217         while (size--) {
218                 len++;
219                 if (*buffer++ == '\n')
220                         break;
221         }
222         return len;
223 }
224
225 static int is_dev_null(const char *str)
226 {
227         return !memcmp("/dev/null", str, 9) && isspace(str[9]);
228 }
229
230 #define TERM_SPACE      1
231 #define TERM_TAB        2
232
233 static int name_terminate(const char *name, int namelen, int c, int terminate)
234 {
235         if (c == ' ' && !(terminate & TERM_SPACE))
236                 return 0;
237         if (c == '\t' && !(terminate & TERM_TAB))
238                 return 0;
239
240         return 1;
241 }
242
243 static char *find_name(const char *line, char *def, int p_value, int terminate)
244 {
245         int len;
246         const char *start = line;
247         char *name;
248
249         if (*line == '"') {
250                 /* Proposed "new-style" GNU patch/diff format; see
251                  * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
252                  */
253                 name = unquote_c_style(line, NULL);
254                 if (name) {
255                         char *cp = name;
256                         while (p_value) {
257                                 cp = strchr(name, '/');
258                                 if (!cp)
259                                         break;
260                                 cp++;
261                                 p_value--;
262                         }
263                         if (cp) {
264                                 /* name can later be freed, so we need
265                                  * to memmove, not just return cp
266                                  */
267                                 memmove(name, cp, strlen(cp) + 1);
268                                 free(def);
269                                 return name;
270                         }
271                         else {
272                                 free(name);
273                                 name = NULL;
274                         }
275                 }
276         }
277
278         for (;;) {
279                 char c = *line;
280
281                 if (isspace(c)) {
282                         if (c == '\n')
283                                 break;
284                         if (name_terminate(start, line-start, c, terminate))
285                                 break;
286                 }
287                 line++;
288                 if (c == '/' && !--p_value)
289                         start = line;
290         }
291         if (!start)
292                 return def;
293         len = line - start;
294         if (!len)
295                 return def;
296
297         /*
298          * Generally we prefer the shorter name, especially
299          * if the other one is just a variation of that with
300          * something else tacked on to the end (ie "file.orig"
301          * or "file~").
302          */
303         if (def) {
304                 int deflen = strlen(def);
305                 if (deflen < len && !strncmp(start, def, deflen))
306                         return def;
307         }
308
309         name = xmalloc(len + 1);
310         memcpy(name, start, len);
311         name[len] = 0;
312         free(def);
313         return name;
314 }
315
316 static int count_slashes(const char *cp)
317 {
318         int cnt = 0;
319         char ch;
320
321         while ((ch = *cp++))
322                 if (ch == '/')
323                         cnt++;
324         return cnt;
325 }
326
327 /*
328  * Given the string after "--- " or "+++ ", guess the appropriate
329  * p_value for the given patch.
330  */
331 static int guess_p_value(const char *nameline)
332 {
333         char *name, *cp;
334         int val = -1;
335
336         if (is_dev_null(nameline))
337                 return -1;
338         name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB);
339         if (!name)
340                 return -1;
341         cp = strchr(name, '/');
342         if (!cp)
343                 val = 0;
344         else if (prefix) {
345                 /*
346                  * Does it begin with "a/$our-prefix" and such?  Then this is
347                  * very likely to apply to our directory.
348                  */
349                 if (!strncmp(name, prefix, prefix_length))
350                         val = count_slashes(prefix);
351                 else {
352                         cp++;
353                         if (!strncmp(cp, prefix, prefix_length))
354                                 val = count_slashes(prefix) + 1;
355                 }
356         }
357         free(name);
358         return val;
359 }
360
361 /*
362  * Get the name etc info from the --/+++ lines of a traditional patch header
363  *
364  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
365  * files, we can happily check the index for a match, but for creating a
366  * new file we should try to match whatever "patch" does. I have no idea.
367  */
368 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
369 {
370         char *name;
371
372         first += 4;     /* skip "--- " */
373         second += 4;    /* skip "+++ " */
374         if (!p_value_known) {
375                 int p, q;
376                 p = guess_p_value(first);
377                 q = guess_p_value(second);
378                 if (p < 0) p = q;
379                 if (0 <= p && p == q) {
380                         p_value = p;
381                         p_value_known = 1;
382                 }
383         }
384         if (is_dev_null(first)) {
385                 patch->is_new = 1;
386                 patch->is_delete = 0;
387                 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
388                 patch->new_name = name;
389         } else if (is_dev_null(second)) {
390                 patch->is_new = 0;
391                 patch->is_delete = 1;
392                 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
393                 patch->old_name = name;
394         } else {
395                 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
396                 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
397                 patch->old_name = patch->new_name = name;
398         }
399         if (!name)
400                 die("unable to find filename in patch at line %d", linenr);
401 }
402
403 static int gitdiff_hdrend(const char *line, struct patch *patch)
404 {
405         return -1;
406 }
407
408 /*
409  * We're anal about diff header consistency, to make
410  * sure that we don't end up having strange ambiguous
411  * patches floating around.
412  *
413  * As a result, gitdiff_{old|new}name() will check
414  * their names against any previous information, just
415  * to make sure..
416  */
417 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
418 {
419         if (!orig_name && !isnull)
420                 return find_name(line, NULL, p_value, TERM_TAB);
421
422         if (orig_name) {
423                 int len;
424                 const char *name;
425                 char *another;
426                 name = orig_name;
427                 len = strlen(name);
428                 if (isnull)
429                         die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
430                 another = find_name(line, NULL, p_value, TERM_TAB);
431                 if (!another || memcmp(another, name, len))
432                         die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
433                 free(another);
434                 return orig_name;
435         }
436         else {
437                 /* expect "/dev/null" */
438                 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
439                         die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
440                 return NULL;
441         }
442 }
443
444 static int gitdiff_oldname(const char *line, struct patch *patch)
445 {
446         patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
447         return 0;
448 }
449
450 static int gitdiff_newname(const char *line, struct patch *patch)
451 {
452         patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
453         return 0;
454 }
455
456 static int gitdiff_oldmode(const char *line, struct patch *patch)
457 {
458         patch->old_mode = strtoul(line, NULL, 8);
459         return 0;
460 }
461
462 static int gitdiff_newmode(const char *line, struct patch *patch)
463 {
464         patch->new_mode = strtoul(line, NULL, 8);
465         return 0;
466 }
467
468 static int gitdiff_delete(const char *line, struct patch *patch)
469 {
470         patch->is_delete = 1;
471         patch->old_name = patch->def_name;
472         return gitdiff_oldmode(line, patch);
473 }
474
475 static int gitdiff_newfile(const char *line, struct patch *patch)
476 {
477         patch->is_new = 1;
478         patch->new_name = patch->def_name;
479         return gitdiff_newmode(line, patch);
480 }
481
482 static int gitdiff_copysrc(const char *line, struct patch *patch)
483 {
484         patch->is_copy = 1;
485         patch->old_name = find_name(line, NULL, 0, 0);
486         return 0;
487 }
488
489 static int gitdiff_copydst(const char *line, struct patch *patch)
490 {
491         patch->is_copy = 1;
492         patch->new_name = find_name(line, NULL, 0, 0);
493         return 0;
494 }
495
496 static int gitdiff_renamesrc(const char *line, struct patch *patch)
497 {
498         patch->is_rename = 1;
499         patch->old_name = find_name(line, NULL, 0, 0);
500         return 0;
501 }
502
503 static int gitdiff_renamedst(const char *line, struct patch *patch)
504 {
505         patch->is_rename = 1;
506         patch->new_name = find_name(line, NULL, 0, 0);
507         return 0;
508 }
509
510 static int gitdiff_similarity(const char *line, struct patch *patch)
511 {
512         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
513                 patch->score = 0;
514         return 0;
515 }
516
517 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
518 {
519         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
520                 patch->score = 0;
521         return 0;
522 }
523
524 static int gitdiff_index(const char *line, struct patch *patch)
525 {
526         /* index line is N hexadecimal, "..", N hexadecimal,
527          * and optional space with octal mode.
528          */
529         const char *ptr, *eol;
530         int len;
531
532         ptr = strchr(line, '.');
533         if (!ptr || ptr[1] != '.' || 40 < ptr - line)
534                 return 0;
535         len = ptr - line;
536         memcpy(patch->old_sha1_prefix, line, len);
537         patch->old_sha1_prefix[len] = 0;
538
539         line = ptr + 2;
540         ptr = strchr(line, ' ');
541         eol = strchr(line, '\n');
542
543         if (!ptr || eol < ptr)
544                 ptr = eol;
545         len = ptr - line;
546
547         if (40 < len)
548                 return 0;
549         memcpy(patch->new_sha1_prefix, line, len);
550         patch->new_sha1_prefix[len] = 0;
551         if (*ptr == ' ')
552                 patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
553         return 0;
554 }
555
556 /*
557  * This is normal for a diff that doesn't change anything: we'll fall through
558  * into the next diff. Tell the parser to break out.
559  */
560 static int gitdiff_unrecognized(const char *line, struct patch *patch)
561 {
562         return -1;
563 }
564
565 static const char *stop_at_slash(const char *line, int llen)
566 {
567         int i;
568
569         for (i = 0; i < llen; i++) {
570                 int ch = line[i];
571                 if (ch == '/')
572                         return line + i;
573         }
574         return NULL;
575 }
576
577 /* This is to extract the same name that appears on "diff --git"
578  * line.  We do not find and return anything if it is a rename
579  * patch, and it is OK because we will find the name elsewhere.
580  * We need to reliably find name only when it is mode-change only,
581  * creation or deletion of an empty file.  In any of these cases,
582  * both sides are the same name under a/ and b/ respectively.
583  */
584 static char *git_header_name(char *line, int llen)
585 {
586         int len;
587         const char *name;
588         const char *second = NULL;
589
590         line += strlen("diff --git ");
591         llen -= strlen("diff --git ");
592
593         if (*line == '"') {
594                 const char *cp;
595                 char *first = unquote_c_style(line, &second);
596                 if (!first)
597                         return NULL;
598
599                 /* advance to the first slash */
600                 cp = stop_at_slash(first, strlen(first));
601                 if (!cp || cp == first) {
602                         /* we do not accept absolute paths */
603                 free_first_and_fail:
604                         free(first);
605                         return NULL;
606                 }
607                 len = strlen(cp+1);
608                 memmove(first, cp+1, len+1); /* including NUL */
609
610                 /* second points at one past closing dq of name.
611                  * find the second name.
612                  */
613                 while ((second < line + llen) && isspace(*second))
614                         second++;
615
616                 if (line + llen <= second)
617                         goto free_first_and_fail;
618                 if (*second == '"') {
619                         char *sp = unquote_c_style(second, NULL);
620                         if (!sp)
621                                 goto free_first_and_fail;
622                         cp = stop_at_slash(sp, strlen(sp));
623                         if (!cp || cp == sp) {
624                         free_both_and_fail:
625                                 free(sp);
626                                 goto free_first_and_fail;
627                         }
628                         /* They must match, otherwise ignore */
629                         if (strcmp(cp+1, first))
630                                 goto free_both_and_fail;
631                         free(sp);
632                         return first;
633                 }
634
635                 /* unquoted second */
636                 cp = stop_at_slash(second, line + llen - second);
637                 if (!cp || cp == second)
638                         goto free_first_and_fail;
639                 cp++;
640                 if (line + llen - cp != len + 1 ||
641                     memcmp(first, cp, len))
642                         goto free_first_and_fail;
643                 return first;
644         }
645
646         /* unquoted first name */
647         name = stop_at_slash(line, llen);
648         if (!name || name == line)
649                 return NULL;
650
651         name++;
652
653         /* since the first name is unquoted, a dq if exists must be
654          * the beginning of the second name.
655          */
656         for (second = name; second < line + llen; second++) {
657                 if (*second == '"') {
658                         const char *cp = second;
659                         const char *np;
660                         char *sp = unquote_c_style(second, NULL);
661
662                         if (!sp)
663                                 return NULL;
664                         np = stop_at_slash(sp, strlen(sp));
665                         if (!np || np == sp) {
666                         free_second_and_fail:
667                                 free(sp);
668                                 return NULL;
669                         }
670                         np++;
671                         len = strlen(np);
672                         if (len < cp - name &&
673                             !strncmp(np, name, len) &&
674                             isspace(name[len])) {
675                                 /* Good */
676                                 memmove(sp, np, len + 1);
677                                 return sp;
678                         }
679                         goto free_second_and_fail;
680                 }
681         }
682
683         /*
684          * Accept a name only if it shows up twice, exactly the same
685          * form.
686          */
687         for (len = 0 ; ; len++) {
688                 switch (name[len]) {
689                 default:
690                         continue;
691                 case '\n':
692                         return NULL;
693                 case '\t': case ' ':
694                         second = name+len;
695                         for (;;) {
696                                 char c = *second++;
697                                 if (c == '\n')
698                                         return NULL;
699                                 if (c == '/')
700                                         break;
701                         }
702                         if (second[len] == '\n' && !memcmp(name, second, len)) {
703                                 char *ret = xmalloc(len + 1);
704                                 memcpy(ret, name, len);
705                                 ret[len] = 0;
706                                 return ret;
707                         }
708                 }
709         }
710         return NULL;
711 }
712
713 /* Verify that we recognize the lines following a git header */
714 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
715 {
716         unsigned long offset;
717
718         /* A git diff has explicit new/delete information, so we don't guess */
719         patch->is_new = 0;
720         patch->is_delete = 0;
721
722         /*
723          * Some things may not have the old name in the
724          * rest of the headers anywhere (pure mode changes,
725          * or removing or adding empty files), so we get
726          * the default name from the header.
727          */
728         patch->def_name = git_header_name(line, len);
729
730         line += len;
731         size -= len;
732         linenr++;
733         for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
734                 static const struct opentry {
735                         const char *str;
736                         int (*fn)(const char *, struct patch *);
737                 } optable[] = {
738                         { "@@ -", gitdiff_hdrend },
739                         { "--- ", gitdiff_oldname },
740                         { "+++ ", gitdiff_newname },
741                         { "old mode ", gitdiff_oldmode },
742                         { "new mode ", gitdiff_newmode },
743                         { "deleted file mode ", gitdiff_delete },
744                         { "new file mode ", gitdiff_newfile },
745                         { "copy from ", gitdiff_copysrc },
746                         { "copy to ", gitdiff_copydst },
747                         { "rename old ", gitdiff_renamesrc },
748                         { "rename new ", gitdiff_renamedst },
749                         { "rename from ", gitdiff_renamesrc },
750                         { "rename to ", gitdiff_renamedst },
751                         { "similarity index ", gitdiff_similarity },
752                         { "dissimilarity index ", gitdiff_dissimilarity },
753                         { "index ", gitdiff_index },
754                         { "", gitdiff_unrecognized },
755                 };
756                 int i;
757
758                 len = linelen(line, size);
759                 if (!len || line[len-1] != '\n')
760                         break;
761                 for (i = 0; i < ARRAY_SIZE(optable); i++) {
762                         const struct opentry *p = optable + i;
763                         int oplen = strlen(p->str);
764                         if (len < oplen || memcmp(p->str, line, oplen))
765                                 continue;
766                         if (p->fn(line + oplen, patch) < 0)
767                                 return offset;
768                         break;
769                 }
770         }
771
772         return offset;
773 }
774
775 static int parse_num(const char *line, unsigned long *p)
776 {
777         char *ptr;
778
779         if (!isdigit(*line))
780                 return 0;
781         *p = strtoul(line, &ptr, 10);
782         return ptr - line;
783 }
784
785 static int parse_range(const char *line, int len, int offset, const char *expect,
786                         unsigned long *p1, unsigned long *p2)
787 {
788         int digits, ex;
789
790         if (offset < 0 || offset >= len)
791                 return -1;
792         line += offset;
793         len -= offset;
794
795         digits = parse_num(line, p1);
796         if (!digits)
797                 return -1;
798
799         offset += digits;
800         line += digits;
801         len -= digits;
802
803         *p2 = 1;
804         if (*line == ',') {
805                 digits = parse_num(line+1, p2);
806                 if (!digits)
807                         return -1;
808
809                 offset += digits+1;
810                 line += digits+1;
811                 len -= digits+1;
812         }
813
814         ex = strlen(expect);
815         if (ex > len)
816                 return -1;
817         if (memcmp(line, expect, ex))
818                 return -1;
819
820         return offset + ex;
821 }
822
823 /*
824  * Parse a unified diff fragment header of the
825  * form "@@ -a,b +c,d @@"
826  */
827 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
828 {
829         int offset;
830
831         if (!len || line[len-1] != '\n')
832                 return -1;
833
834         /* Figure out the number of lines in a fragment */
835         offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
836         offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
837
838         return offset;
839 }
840
841 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
842 {
843         unsigned long offset, len;
844
845         patch->is_toplevel_relative = 0;
846         patch->is_rename = patch->is_copy = 0;
847         patch->is_new = patch->is_delete = -1;
848         patch->old_mode = patch->new_mode = 0;
849         patch->old_name = patch->new_name = NULL;
850         for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
851                 unsigned long nextlen;
852
853                 len = linelen(line, size);
854                 if (!len)
855                         break;
856
857                 /* Testing this early allows us to take a few shortcuts.. */
858                 if (len < 6)
859                         continue;
860
861                 /*
862                  * Make sure we don't find any unconnected patch fragments.
863                  * That's a sign that we didn't find a header, and that a
864                  * patch has become corrupted/broken up.
865                  */
866                 if (!memcmp("@@ -", line, 4)) {
867                         struct fragment dummy;
868                         if (parse_fragment_header(line, len, &dummy) < 0)
869                                 continue;
870                         die("patch fragment without header at line %d: %.*s",
871                             linenr, (int)len-1, line);
872                 }
873
874                 if (size < len + 6)
875                         break;
876
877                 /*
878                  * Git patch? It might not have a real patch, just a rename
879                  * or mode change, so we handle that specially
880                  */
881                 if (!memcmp("diff --git ", line, 11)) {
882                         int git_hdr_len = parse_git_header(line, len, size, patch);
883                         if (git_hdr_len <= len)
884                                 continue;
885                         if (!patch->old_name && !patch->new_name) {
886                                 if (!patch->def_name)
887                                         die("git diff header lacks filename information (line %d)", linenr);
888                                 patch->old_name = patch->new_name = patch->def_name;
889                         }
890                         patch->is_toplevel_relative = 1;
891                         *hdrsize = git_hdr_len;
892                         return offset;
893                 }
894
895                 /** --- followed by +++ ? */
896                 if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
897                         continue;
898
899                 /*
900                  * We only accept unified patches, so we want it to
901                  * at least have "@@ -a,b +c,d @@\n", which is 14 chars
902                  * minimum
903                  */
904                 nextlen = linelen(line + len, size - len);
905                 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
906                         continue;
907
908                 /* Ok, we'll consider it a patch */
909                 parse_traditional_patch(line, line+len, patch);
910                 *hdrsize = len + nextlen;
911                 linenr += 2;
912                 return offset;
913         }
914         return -1;
915 }
916
917 static void check_whitespace(const char *line, int len)
918 {
919         const char *err = "Adds trailing whitespace";
920         int seen_space = 0;
921         int i;
922
923         /*
924          * We know len is at least two, since we have a '+' and we
925          * checked that the last character was a '\n' before calling
926          * this function.  That is, an addition of an empty line would
927          * check the '+' here.  Sneaky...
928          */
929         if (isspace(line[len-2]))
930                 goto error;
931
932         /*
933          * Make sure that there is no space followed by a tab in
934          * indentation.
935          */
936         err = "Space in indent is followed by a tab";
937         for (i = 1; i < len; i++) {
938                 if (line[i] == '\t') {
939                         if (seen_space)
940                                 goto error;
941                 }
942                 else if (line[i] == ' ')
943                         seen_space = 1;
944                 else
945                         break;
946         }
947         return;
948
949  error:
950         whitespace_error++;
951         if (squelch_whitespace_errors &&
952             squelch_whitespace_errors < whitespace_error)
953                 ;
954         else
955                 fprintf(stderr, "%s.\n%s:%d:%.*s\n",
956                         err, patch_input_file, linenr, len-2, line+1);
957 }
958
959
960 /*
961  * Parse a unified diff. Note that this really needs to parse each
962  * fragment separately, since the only way to know the difference
963  * between a "---" that is part of a patch, and a "---" that starts
964  * the next patch is to look at the line counts..
965  */
966 static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
967 {
968         int added, deleted;
969         int len = linelen(line, size), offset;
970         unsigned long oldlines, newlines;
971         unsigned long leading, trailing;
972
973         offset = parse_fragment_header(line, len, fragment);
974         if (offset < 0)
975                 return -1;
976         oldlines = fragment->oldlines;
977         newlines = fragment->newlines;
978         leading = 0;
979         trailing = 0;
980
981         /* Parse the thing.. */
982         line += len;
983         size -= len;
984         linenr++;
985         added = deleted = 0;
986         for (offset = len;
987              0 < size;
988              offset += len, size -= len, line += len, linenr++) {
989                 if (!oldlines && !newlines)
990                         break;
991                 len = linelen(line, size);
992                 if (!len || line[len-1] != '\n')
993                         return -1;
994                 switch (*line) {
995                 default:
996                         return -1;
997                 case '\n': /* newer GNU diff, an empty context line */
998                 case ' ':
999                         oldlines--;
1000                         newlines--;
1001                         if (!deleted && !added)
1002                                 leading++;
1003                         trailing++;
1004                         break;
1005                 case '-':
1006                         deleted++;
1007                         oldlines--;
1008                         trailing = 0;
1009                         break;
1010                 case '+':
1011                         if (new_whitespace != nowarn_whitespace)
1012                                 check_whitespace(line, len);
1013                         added++;
1014                         newlines--;
1015                         trailing = 0;
1016                         break;
1017
1018                 /* We allow "\ No newline at end of file". Depending
1019                  * on locale settings when the patch was produced we
1020                  * don't know what this line looks like. The only
1021                  * thing we do know is that it begins with "\ ".
1022                  * Checking for 12 is just for sanity check -- any
1023                  * l10n of "\ No newline..." is at least that long.
1024                  */
1025                 case '\\':
1026                         if (len < 12 || memcmp(line, "\\ ", 2))
1027                                 return -1;
1028                         break;
1029                 }
1030         }
1031         if (oldlines || newlines)
1032                 return -1;
1033         fragment->leading = leading;
1034         fragment->trailing = trailing;
1035
1036         /* If a fragment ends with an incomplete line, we failed to include
1037          * it in the above loop because we hit oldlines == newlines == 0
1038          * before seeing it.
1039          */
1040         if (12 < size && !memcmp(line, "\\ ", 2))
1041                 offset += linelen(line, size);
1042
1043         patch->lines_added += added;
1044         patch->lines_deleted += deleted;
1045
1046         if (0 < patch->is_new && oldlines)
1047                 return error("new file depends on old contents");
1048         if (0 < patch->is_delete && newlines)
1049                 return error("deleted file still has contents");
1050         return offset;
1051 }
1052
1053 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
1054 {
1055         unsigned long offset = 0;
1056         unsigned long oldlines = 0, newlines = 0, context = 0;
1057         struct fragment **fragp = &patch->fragments;
1058
1059         while (size > 4 && !memcmp(line, "@@ -", 4)) {
1060                 struct fragment *fragment;
1061                 int len;
1062
1063                 fragment = xcalloc(1, sizeof(*fragment));
1064                 len = parse_fragment(line, size, patch, fragment);
1065                 if (len <= 0)
1066                         die("corrupt patch at line %d", linenr);
1067                 fragment->patch = line;
1068                 fragment->size = len;
1069                 oldlines += fragment->oldlines;
1070                 newlines += fragment->newlines;
1071                 context += fragment->leading + fragment->trailing;
1072
1073                 *fragp = fragment;
1074                 fragp = &fragment->next;
1075
1076                 offset += len;
1077                 line += len;
1078                 size -= len;
1079         }
1080
1081         /*
1082          * If something was removed (i.e. we have old-lines) it cannot
1083          * be creation, and if something was added it cannot be
1084          * deletion.  However, the reverse is not true; --unified=0
1085          * patches that only add are not necessarily creation even
1086          * though they do not have any old lines, and ones that only
1087          * delete are not necessarily deletion.
1088          *
1089          * Unfortunately, a real creation/deletion patch do _not_ have
1090          * any context line by definition, so we cannot safely tell it
1091          * apart with --unified=0 insanity.  At least if the patch has
1092          * more than one hunk it is not creation or deletion.
1093          */
1094         if (patch->is_new < 0 &&
1095             (oldlines || (patch->fragments && patch->fragments->next)))
1096                 patch->is_new = 0;
1097         if (patch->is_delete < 0 &&
1098             (newlines || (patch->fragments && patch->fragments->next)))
1099                 patch->is_delete = 0;
1100         if (!unidiff_zero || context) {
1101                 /* If the user says the patch is not generated with
1102                  * --unified=0, or if we have seen context lines,
1103                  * then not having oldlines means the patch is creation,
1104                  * and not having newlines means the patch is deletion.
1105                  */
1106                 if (patch->is_new < 0 && !oldlines) {
1107                         patch->is_new = 1;
1108                         patch->old_name = NULL;
1109                 }
1110                 if (patch->is_delete < 0 && !newlines) {
1111                         patch->is_delete = 1;
1112                         patch->new_name = NULL;
1113                 }
1114         }
1115
1116         if (0 < patch->is_new && oldlines)
1117                 die("new file %s depends on old contents", patch->new_name);
1118         if (0 < patch->is_delete && newlines)
1119                 die("deleted file %s still has contents", patch->old_name);
1120         if (!patch->is_delete && !newlines && context)
1121                 fprintf(stderr, "** warning: file %s becomes empty but "
1122                         "is not deleted\n", patch->new_name);
1123
1124         return offset;
1125 }
1126
1127 static inline int metadata_changes(struct patch *patch)
1128 {
1129         return  patch->is_rename > 0 ||
1130                 patch->is_copy > 0 ||
1131                 patch->is_new > 0 ||
1132                 patch->is_delete ||
1133                 (patch->old_mode && patch->new_mode &&
1134                  patch->old_mode != patch->new_mode);
1135 }
1136
1137 static char *inflate_it(const void *data, unsigned long size,
1138                         unsigned long inflated_size)
1139 {
1140         z_stream stream;
1141         void *out;
1142         int st;
1143
1144         memset(&stream, 0, sizeof(stream));
1145
1146         stream.next_in = (unsigned char *)data;
1147         stream.avail_in = size;
1148         stream.next_out = out = xmalloc(inflated_size);
1149         stream.avail_out = inflated_size;
1150         inflateInit(&stream);
1151         st = inflate(&stream, Z_FINISH);
1152         if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1153                 free(out);
1154                 return NULL;
1155         }
1156         return out;
1157 }
1158
1159 static struct fragment *parse_binary_hunk(char **buf_p,
1160                                           unsigned long *sz_p,
1161                                           int *status_p,
1162                                           int *used_p)
1163 {
1164         /* Expect a line that begins with binary patch method ("literal"
1165          * or "delta"), followed by the length of data before deflating.
1166          * a sequence of 'length-byte' followed by base-85 encoded data
1167          * should follow, terminated by a newline.
1168          *
1169          * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1170          * and we would limit the patch line to 66 characters,
1171          * so one line can fit up to 13 groups that would decode
1172          * to 52 bytes max.  The length byte 'A'-'Z' corresponds
1173          * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1174          */
1175         int llen, used;
1176         unsigned long size = *sz_p;
1177         char *buffer = *buf_p;
1178         int patch_method;
1179         unsigned long origlen;
1180         char *data = NULL;
1181         int hunk_size = 0;
1182         struct fragment *frag;
1183
1184         llen = linelen(buffer, size);
1185         used = llen;
1186
1187         *status_p = 0;
1188
1189         if (!prefixcmp(buffer, "delta ")) {
1190                 patch_method = BINARY_DELTA_DEFLATED;
1191                 origlen = strtoul(buffer + 6, NULL, 10);
1192         }
1193         else if (!prefixcmp(buffer, "literal ")) {
1194                 patch_method = BINARY_LITERAL_DEFLATED;
1195                 origlen = strtoul(buffer + 8, NULL, 10);
1196         }
1197         else
1198                 return NULL;
1199
1200         linenr++;
1201         buffer += llen;
1202         while (1) {
1203                 int byte_length, max_byte_length, newsize;
1204                 llen = linelen(buffer, size);
1205                 used += llen;
1206                 linenr++;
1207                 if (llen == 1) {
1208                         /* consume the blank line */
1209                         buffer++;
1210                         size--;
1211                         break;
1212                 }
1213                 /* Minimum line is "A00000\n" which is 7-byte long,
1214                  * and the line length must be multiple of 5 plus 2.
1215                  */
1216                 if ((llen < 7) || (llen-2) % 5)
1217                         goto corrupt;
1218                 max_byte_length = (llen - 2) / 5 * 4;
1219                 byte_length = *buffer;
1220                 if ('A' <= byte_length && byte_length <= 'Z')
1221                         byte_length = byte_length - 'A' + 1;
1222                 else if ('a' <= byte_length && byte_length <= 'z')
1223                         byte_length = byte_length - 'a' + 27;
1224                 else
1225                         goto corrupt;
1226                 /* if the input length was not multiple of 4, we would
1227                  * have filler at the end but the filler should never
1228                  * exceed 3 bytes
1229                  */
1230                 if (max_byte_length < byte_length ||
1231                     byte_length <= max_byte_length - 4)
1232                         goto corrupt;
1233                 newsize = hunk_size + byte_length;
1234                 data = xrealloc(data, newsize);
1235                 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1236                         goto corrupt;
1237                 hunk_size = newsize;
1238                 buffer += llen;
1239                 size -= llen;
1240         }
1241
1242         frag = xcalloc(1, sizeof(*frag));
1243         frag->patch = inflate_it(data, hunk_size, origlen);
1244         if (!frag->patch)
1245                 goto corrupt;
1246         free(data);
1247         frag->size = origlen;
1248         *buf_p = buffer;
1249         *sz_p = size;
1250         *used_p = used;
1251         frag->binary_patch_method = patch_method;
1252         return frag;
1253
1254  corrupt:
1255         free(data);
1256         *status_p = -1;
1257         error("corrupt binary patch at line %d: %.*s",
1258               linenr-1, llen-1, buffer);
1259         return NULL;
1260 }
1261
1262 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1263 {
1264         /* We have read "GIT binary patch\n"; what follows is a line
1265          * that says the patch method (currently, either "literal" or
1266          * "delta") and the length of data before deflating; a
1267          * sequence of 'length-byte' followed by base-85 encoded data
1268          * follows.
1269          *
1270          * When a binary patch is reversible, there is another binary
1271          * hunk in the same format, starting with patch method (either
1272          * "literal" or "delta") with the length of data, and a sequence
1273          * of length-byte + base-85 encoded data, terminated with another
1274          * empty line.  This data, when applied to the postimage, produces
1275          * the preimage.
1276          */
1277         struct fragment *forward;
1278         struct fragment *reverse;
1279         int status;
1280         int used, used_1;
1281
1282         forward = parse_binary_hunk(&buffer, &size, &status, &used);
1283         if (!forward && !status)
1284                 /* there has to be one hunk (forward hunk) */
1285                 return error("unrecognized binary patch at line %d", linenr-1);
1286         if (status)
1287                 /* otherwise we already gave an error message */
1288                 return status;
1289
1290         reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1291         if (reverse)
1292                 used += used_1;
1293         else if (status) {
1294                 /* not having reverse hunk is not an error, but having
1295                  * a corrupt reverse hunk is.
1296                  */
1297                 free((void*) forward->patch);
1298                 free(forward);
1299                 return status;
1300         }
1301         forward->next = reverse;
1302         patch->fragments = forward;
1303         patch->is_binary = 1;
1304         return used;
1305 }
1306
1307 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1308 {
1309         int hdrsize, patchsize;
1310         int offset = find_header(buffer, size, &hdrsize, patch);
1311
1312         if (offset < 0)
1313                 return offset;
1314
1315         patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
1316
1317         if (!patchsize) {
1318                 static const char *binhdr[] = {
1319                         "Binary files ",
1320                         "Files ",
1321                         NULL,
1322                 };
1323                 static const char git_binary[] = "GIT binary patch\n";
1324                 int i;
1325                 int hd = hdrsize + offset;
1326                 unsigned long llen = linelen(buffer + hd, size - hd);
1327
1328                 if (llen == sizeof(git_binary) - 1 &&
1329                     !memcmp(git_binary, buffer + hd, llen)) {
1330                         int used;
1331                         linenr++;
1332                         used = parse_binary(buffer + hd + llen,
1333                                             size - hd - llen, patch);
1334                         if (used)
1335                                 patchsize = used + llen;
1336                         else
1337                                 patchsize = 0;
1338                 }
1339                 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1340                         for (i = 0; binhdr[i]; i++) {
1341                                 int len = strlen(binhdr[i]);
1342                                 if (len < size - hd &&
1343                                     !memcmp(binhdr[i], buffer + hd, len)) {
1344                                         linenr++;
1345                                         patch->is_binary = 1;
1346                                         patchsize = llen;
1347                                         break;
1348                                 }
1349                         }
1350                 }
1351
1352                 /* Empty patch cannot be applied if it is a text patch
1353                  * without metadata change.  A binary patch appears
1354                  * empty to us here.
1355                  */
1356                 if ((apply || check) &&
1357                     (!patch->is_binary && !metadata_changes(patch)))
1358                         die("patch with only garbage at line %d", linenr);
1359         }
1360
1361         return offset + hdrsize + patchsize;
1362 }
1363
1364 #define swap(a,b) myswap((a),(b),sizeof(a))
1365
1366 #define myswap(a, b, size) do {         \
1367         unsigned char mytmp[size];      \
1368         memcpy(mytmp, &a, size);                \
1369         memcpy(&a, &b, size);           \
1370         memcpy(&b, mytmp, size);                \
1371 } while (0)
1372
1373 static void reverse_patches(struct patch *p)
1374 {
1375         for (; p; p = p->next) {
1376                 struct fragment *frag = p->fragments;
1377
1378                 swap(p->new_name, p->old_name);
1379                 swap(p->new_mode, p->old_mode);
1380                 swap(p->is_new, p->is_delete);
1381                 swap(p->lines_added, p->lines_deleted);
1382                 swap(p->old_sha1_prefix, p->new_sha1_prefix);
1383
1384                 for (; frag; frag = frag->next) {
1385                         swap(frag->newpos, frag->oldpos);
1386                         swap(frag->newlines, frag->oldlines);
1387                 }
1388         }
1389 }
1390
1391 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1392 static const char minuses[]= "----------------------------------------------------------------------";
1393
1394 static void show_stats(struct patch *patch)
1395 {
1396         const char *prefix = "";
1397         char *name = patch->new_name;
1398         char *qname = NULL;
1399         int len, max, add, del, total;
1400
1401         if (!name)
1402                 name = patch->old_name;
1403
1404         if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
1405                 qname = xmalloc(len + 1);
1406                 quote_c_style(name, qname, NULL, 0);
1407                 name = qname;
1408         }
1409
1410         /*
1411          * "scale" the filename
1412          */
1413         len = strlen(name);
1414         max = max_len;
1415         if (max > 50)
1416                 max = 50;
1417         if (len > max) {
1418                 char *slash;
1419                 prefix = "...";
1420                 max -= 3;
1421                 name += len - max;
1422                 slash = strchr(name, '/');
1423                 if (slash)
1424                         name = slash;
1425         }
1426         len = max;
1427
1428         /*
1429          * scale the add/delete
1430          */
1431         max = max_change;
1432         if (max + len > 70)
1433                 max = 70 - len;
1434
1435         add = patch->lines_added;
1436         del = patch->lines_deleted;
1437         total = add + del;
1438
1439         if (max_change > 0) {
1440                 total = (total * max + max_change / 2) / max_change;
1441                 add = (add * max + max_change / 2) / max_change;
1442                 del = total - add;
1443         }
1444         if (patch->is_binary)
1445                 printf(" %s%-*s |  Bin\n", prefix, len, name);
1446         else
1447                 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
1448                        len, name, patch->lines_added + patch->lines_deleted,
1449                        add, pluses, del, minuses);
1450         free(qname);
1451 }
1452
1453 static int read_old_data(struct stat *st, const char *path, char **buf_p, unsigned long *alloc_p, unsigned long *size_p)
1454 {
1455         int fd;
1456         unsigned long got;
1457         unsigned long nsize;
1458         char *nbuf;
1459         unsigned long size = *size_p;
1460         char *buf = *buf_p;
1461
1462         switch (st->st_mode & S_IFMT) {
1463         case S_IFLNK:
1464                 return readlink(path, buf, size) != size;
1465         case S_IFREG:
1466                 fd = open(path, O_RDONLY);
1467                 if (fd < 0)
1468                         return error("unable to open %s", path);
1469                 got = 0;
1470                 for (;;) {
1471                         ssize_t ret = xread(fd, buf + got, size - got);
1472                         if (ret <= 0)
1473                                 break;
1474                         got += ret;
1475                 }
1476                 close(fd);
1477                 nsize = got;
1478                 nbuf = convert_to_git(path, buf, &nsize);
1479                 if (nbuf) {
1480                         free(buf);
1481                         *buf_p = nbuf;
1482                         *alloc_p = nsize;
1483                         *size_p = nsize;
1484                 }
1485                 return got != size;
1486         default:
1487                 return -1;
1488         }
1489 }
1490
1491 static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
1492 {
1493         int i;
1494         unsigned long start, backwards, forwards;
1495
1496         if (fragsize > size)
1497                 return -1;
1498
1499         start = 0;
1500         if (line > 1) {
1501                 unsigned long offset = 0;
1502                 i = line-1;
1503                 while (offset + fragsize <= size) {
1504                         if (buf[offset++] == '\n') {
1505                                 start = offset;
1506                                 if (!--i)
1507                                         break;
1508                         }
1509                 }
1510         }
1511
1512         /* Exact line number? */
1513         if (!memcmp(buf + start, fragment, fragsize))
1514                 return start;
1515
1516         /*
1517          * There's probably some smart way to do this, but I'll leave
1518          * that to the smart and beautiful people. I'm simple and stupid.
1519          */
1520         backwards = start;
1521         forwards = start;
1522         for (i = 0; ; i++) {
1523                 unsigned long try;
1524                 int n;
1525
1526                 /* "backward" */
1527                 if (i & 1) {
1528                         if (!backwards) {
1529                                 if (forwards + fragsize > size)
1530                                         break;
1531                                 continue;
1532                         }
1533                         do {
1534                                 --backwards;
1535                         } while (backwards && buf[backwards-1] != '\n');
1536                         try = backwards;
1537                 } else {
1538                         while (forwards + fragsize <= size) {
1539                                 if (buf[forwards++] == '\n')
1540                                         break;
1541                         }
1542                         try = forwards;
1543                 }
1544
1545                 if (try + fragsize > size)
1546                         continue;
1547                 if (memcmp(buf + try, fragment, fragsize))
1548                         continue;
1549                 n = (i >> 1)+1;
1550                 if (i & 1)
1551                         n = -n;
1552                 *lines = n;
1553                 return try;
1554         }
1555
1556         /*
1557          * We should start searching forward and backward.
1558          */
1559         return -1;
1560 }
1561
1562 static void remove_first_line(const char **rbuf, int *rsize)
1563 {
1564         const char *buf = *rbuf;
1565         int size = *rsize;
1566         unsigned long offset;
1567         offset = 0;
1568         while (offset <= size) {
1569                 if (buf[offset++] == '\n')
1570                         break;
1571         }
1572         *rsize = size - offset;
1573         *rbuf = buf + offset;
1574 }
1575
1576 static void remove_last_line(const char **rbuf, int *rsize)
1577 {
1578         const char *buf = *rbuf;
1579         int size = *rsize;
1580         unsigned long offset;
1581         offset = size - 1;
1582         while (offset > 0) {
1583                 if (buf[--offset] == '\n')
1584                         break;
1585         }
1586         *rsize = offset + 1;
1587 }
1588
1589 struct buffer_desc {
1590         char *buffer;
1591         unsigned long size;
1592         unsigned long alloc;
1593 };
1594
1595 static int apply_line(char *output, const char *patch, int plen)
1596 {
1597         /* plen is number of bytes to be copied from patch,
1598          * starting at patch+1 (patch[0] is '+').  Typically
1599          * patch[plen] is '\n', unless this is the incomplete
1600          * last line.
1601          */
1602         int i;
1603         int add_nl_to_tail = 0;
1604         int fixed = 0;
1605         int last_tab_in_indent = -1;
1606         int last_space_in_indent = -1;
1607         int need_fix_leading_space = 0;
1608         char *buf;
1609
1610         if ((new_whitespace != strip_whitespace) || !whitespace_error ||
1611             *patch != '+') {
1612                 memcpy(output, patch + 1, plen);
1613                 return plen;
1614         }
1615
1616         if (1 < plen && isspace(patch[plen-1])) {
1617                 if (patch[plen] == '\n')
1618                         add_nl_to_tail = 1;
1619                 plen--;
1620                 while (0 < plen && isspace(patch[plen]))
1621                         plen--;
1622                 fixed = 1;
1623         }
1624
1625         for (i = 1; i < plen; i++) {
1626                 char ch = patch[i];
1627                 if (ch == '\t') {
1628                         last_tab_in_indent = i;
1629                         if (0 <= last_space_in_indent)
1630                                 need_fix_leading_space = 1;
1631                 }
1632                 else if (ch == ' ')
1633                         last_space_in_indent = i;
1634                 else
1635                         break;
1636         }
1637
1638         buf = output;
1639         if (need_fix_leading_space) {
1640                 /* between patch[1..last_tab_in_indent] strip the
1641                  * funny spaces, updating them to tab as needed.
1642                  */
1643                 for (i = 1; i < last_tab_in_indent; i++, plen--) {
1644                         char ch = patch[i];
1645                         if (ch != ' ')
1646                                 *output++ = ch;
1647                         else if ((i % 8) == 0)
1648                                 *output++ = '\t';
1649                 }
1650                 fixed = 1;
1651                 i = last_tab_in_indent;
1652         }
1653         else
1654                 i = 1;
1655
1656         memcpy(output, patch + i, plen);
1657         if (add_nl_to_tail)
1658                 output[plen++] = '\n';
1659         if (fixed)
1660                 applied_after_stripping++;
1661         return output + plen - buf;
1662 }
1663
1664 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int inaccurate_eof)
1665 {
1666         int match_beginning, match_end;
1667         char *buf = desc->buffer;
1668         const char *patch = frag->patch;
1669         int offset, size = frag->size;
1670         char *old = xmalloc(size);
1671         char *new = xmalloc(size);
1672         const char *oldlines, *newlines;
1673         int oldsize = 0, newsize = 0;
1674         unsigned long leading, trailing;
1675         int pos, lines;
1676
1677         while (size > 0) {
1678                 char first;
1679                 int len = linelen(patch, size);
1680                 int plen;
1681
1682                 if (!len)
1683                         break;
1684
1685                 /*
1686                  * "plen" is how much of the line we should use for
1687                  * the actual patch data. Normally we just remove the
1688                  * first character on the line, but if the line is
1689                  * followed by "\ No newline", then we also remove the
1690                  * last one (which is the newline, of course).
1691                  */
1692                 plen = len-1;
1693                 if (len < size && patch[len] == '\\')
1694                         plen--;
1695                 first = *patch;
1696                 if (apply_in_reverse) {
1697                         if (first == '-')
1698                                 first = '+';
1699                         else if (first == '+')
1700                                 first = '-';
1701                 }
1702                 switch (first) {
1703                 case '\n':
1704                         /* Newer GNU diff, empty context line */
1705                         if (plen < 0)
1706                                 /* ... followed by '\No newline'; nothing */
1707                                 break;
1708                         old[oldsize++] = '\n';
1709                         new[newsize++] = '\n';
1710                         break;
1711                 case ' ':
1712                 case '-':
1713                         memcpy(old + oldsize, patch + 1, plen);
1714                         oldsize += plen;
1715                         if (first == '-')
1716                                 break;
1717                 /* Fall-through for ' ' */
1718                 case '+':
1719                         if (first != '+' || !no_add)
1720                                 newsize += apply_line(new + newsize, patch,
1721                                                       plen);
1722                         break;
1723                 case '@': case '\\':
1724                         /* Ignore it, we already handled it */
1725                         break;
1726                 default:
1727                         if (apply_verbosely)
1728                                 error("invalid start of line: '%c'", first);
1729                         return -1;
1730                 }
1731                 patch += len;
1732                 size -= len;
1733         }
1734
1735         if (inaccurate_eof && oldsize > 0 && old[oldsize - 1] == '\n' &&
1736                         newsize > 0 && new[newsize - 1] == '\n') {
1737                 oldsize--;
1738                 newsize--;
1739         }
1740
1741         oldlines = old;
1742         newlines = new;
1743         leading = frag->leading;
1744         trailing = frag->trailing;
1745
1746         /*
1747          * If we don't have any leading/trailing data in the patch,
1748          * we want it to match at the beginning/end of the file.
1749          *
1750          * But that would break if the patch is generated with
1751          * --unified=0; sane people wouldn't do that to cause us
1752          * trouble, but we try to please not so sane ones as well.
1753          */
1754         if (unidiff_zero) {
1755                 match_beginning = (!leading && !frag->oldpos);
1756                 match_end = 0;
1757         }
1758         else {
1759                 match_beginning = !leading && (frag->oldpos == 1);
1760                 match_end = !trailing;
1761         }
1762
1763         lines = 0;
1764         pos = frag->newpos;
1765         for (;;) {
1766                 offset = find_offset(buf, desc->size,
1767                                      oldlines, oldsize, pos, &lines);
1768                 if (match_end && offset + oldsize != desc->size)
1769                         offset = -1;
1770                 if (match_beginning && offset)
1771                         offset = -1;
1772                 if (offset >= 0) {
1773                         int diff = newsize - oldsize;
1774                         unsigned long size = desc->size + diff;
1775                         unsigned long alloc = desc->alloc;
1776
1777                         /* Warn if it was necessary to reduce the number
1778                          * of context lines.
1779                          */
1780                         if ((leading != frag->leading) ||
1781                             (trailing != frag->trailing))
1782                                 fprintf(stderr, "Context reduced to (%ld/%ld)"
1783                                         " to apply fragment at %d\n",
1784                                         leading, trailing, pos + lines);
1785
1786                         if (size > alloc) {
1787                                 alloc = size + 8192;
1788                                 desc->alloc = alloc;
1789                                 buf = xrealloc(buf, alloc);
1790                                 desc->buffer = buf;
1791                         }
1792                         desc->size = size;
1793                         memmove(buf + offset + newsize,
1794                                 buf + offset + oldsize,
1795                                 size - offset - newsize);
1796                         memcpy(buf + offset, newlines, newsize);
1797                         offset = 0;
1798
1799                         break;
1800                 }
1801
1802                 /* Am I at my context limits? */
1803                 if ((leading <= p_context) && (trailing <= p_context))
1804                         break;
1805                 if (match_beginning || match_end) {
1806                         match_beginning = match_end = 0;
1807                         continue;
1808                 }
1809                 /* Reduce the number of context lines
1810                  * Reduce both leading and trailing if they are equal
1811                  * otherwise just reduce the larger context.
1812                  */
1813                 if (leading >= trailing) {
1814                         remove_first_line(&oldlines, &oldsize);
1815                         remove_first_line(&newlines, &newsize);
1816                         pos--;
1817                         leading--;
1818                 }
1819                 if (trailing > leading) {
1820                         remove_last_line(&oldlines, &oldsize);
1821                         remove_last_line(&newlines, &newsize);
1822                         trailing--;
1823                 }
1824         }
1825
1826         if (offset && apply_verbosely)
1827                 error("while searching for:\n%.*s", oldsize, oldlines);
1828
1829         free(old);
1830         free(new);
1831         return offset;
1832 }
1833
1834 static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
1835 {
1836         unsigned long dst_size;
1837         struct fragment *fragment = patch->fragments;
1838         void *data;
1839         void *result;
1840
1841         /* Binary patch is irreversible without the optional second hunk */
1842         if (apply_in_reverse) {
1843                 if (!fragment->next)
1844                         return error("cannot reverse-apply a binary patch "
1845                                      "without the reverse hunk to '%s'",
1846                                      patch->new_name
1847                                      ? patch->new_name : patch->old_name);
1848                 fragment = fragment->next;
1849         }
1850         data = (void*) fragment->patch;
1851         switch (fragment->binary_patch_method) {
1852         case BINARY_DELTA_DEFLATED:
1853                 result = patch_delta(desc->buffer, desc->size,
1854                                      data,
1855                                      fragment->size,
1856                                      &dst_size);
1857                 free(desc->buffer);
1858                 desc->buffer = result;
1859                 break;
1860         case BINARY_LITERAL_DEFLATED:
1861                 free(desc->buffer);
1862                 desc->buffer = data;
1863                 dst_size = fragment->size;
1864                 break;
1865         }
1866         if (!desc->buffer)
1867                 return -1;
1868         desc->size = desc->alloc = dst_size;
1869         return 0;
1870 }
1871
1872 static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1873 {
1874         const char *name = patch->old_name ? patch->old_name : patch->new_name;
1875         unsigned char sha1[20];
1876
1877         /* For safety, we require patch index line to contain
1878          * full 40-byte textual SHA1 for old and new, at least for now.
1879          */
1880         if (strlen(patch->old_sha1_prefix) != 40 ||
1881             strlen(patch->new_sha1_prefix) != 40 ||
1882             get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1883             get_sha1_hex(patch->new_sha1_prefix, sha1))
1884                 return error("cannot apply binary patch to '%s' "
1885                              "without full index line", name);
1886
1887         if (patch->old_name) {
1888                 /* See if the old one matches what the patch
1889                  * applies to.
1890                  */
1891                 hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
1892                 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1893                         return error("the patch applies to '%s' (%s), "
1894                                      "which does not match the "
1895                                      "current contents.",
1896                                      name, sha1_to_hex(sha1));
1897         }
1898         else {
1899                 /* Otherwise, the old one must be empty. */
1900                 if (desc->size)
1901                         return error("the patch applies to an empty "
1902                                      "'%s' but it is not empty", name);
1903         }
1904
1905         get_sha1_hex(patch->new_sha1_prefix, sha1);
1906         if (is_null_sha1(sha1)) {
1907                 free(desc->buffer);
1908                 desc->alloc = desc->size = 0;
1909                 desc->buffer = NULL;
1910                 return 0; /* deletion patch */
1911         }
1912
1913         if (has_sha1_file(sha1)) {
1914                 /* We already have the postimage */
1915                 enum object_type type;
1916                 unsigned long size;
1917
1918                 free(desc->buffer);
1919                 desc->buffer = read_sha1_file(sha1, &type, &size);
1920                 if (!desc->buffer)
1921                         return error("the necessary postimage %s for "
1922                                      "'%s' cannot be read",
1923                                      patch->new_sha1_prefix, name);
1924                 desc->alloc = desc->size = size;
1925         }
1926         else {
1927                 /* We have verified desc matches the preimage;
1928                  * apply the patch data to it, which is stored
1929                  * in the patch->fragments->{patch,size}.
1930                  */
1931                 if (apply_binary_fragment(desc, patch))
1932                         return error("binary patch does not apply to '%s'",
1933                                      name);
1934
1935                 /* verify that the result matches */
1936                 hash_sha1_file(desc->buffer, desc->size, blob_type, sha1);
1937                 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1938                         return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)", name, patch->new_sha1_prefix, sha1_to_hex(sha1));
1939         }
1940
1941         return 0;
1942 }
1943
1944 static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1945 {
1946         struct fragment *frag = patch->fragments;
1947         const char *name = patch->old_name ? patch->old_name : patch->new_name;
1948
1949         if (patch->is_binary)
1950                 return apply_binary(desc, patch);
1951
1952         while (frag) {
1953                 if (apply_one_fragment(desc, frag, patch->inaccurate_eof)) {
1954                         error("patch failed: %s:%ld", name, frag->oldpos);
1955                         if (!apply_with_reject)
1956                                 return -1;
1957                         frag->rejected = 1;
1958                 }
1959                 frag = frag->next;
1960         }
1961         return 0;
1962 }
1963
1964 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
1965 {
1966         char *buf;
1967         unsigned long size, alloc;
1968         struct buffer_desc desc;
1969
1970         size = 0;
1971         alloc = 0;
1972         buf = NULL;
1973         if (cached) {
1974                 if (ce) {
1975                         enum object_type type;
1976                         buf = read_sha1_file(ce->sha1, &type, &size);
1977                         if (!buf)
1978                                 return error("read of %s failed",
1979                                              patch->old_name);
1980                         alloc = size;
1981                 }
1982         }
1983         else if (patch->old_name) {
1984                 size = xsize_t(st->st_size);
1985                 alloc = size + 8192;
1986                 buf = xmalloc(alloc);
1987                 if (read_old_data(st, patch->old_name, &buf, &alloc, &size))
1988                         return error("read of %s failed", patch->old_name);
1989         }
1990
1991         desc.size = size;
1992         desc.alloc = alloc;
1993         desc.buffer = buf;
1994
1995         if (apply_fragments(&desc, patch) < 0)
1996                 return -1; /* note with --reject this succeeds. */
1997
1998         /* NUL terminate the result */
1999         if (desc.alloc <= desc.size)
2000                 desc.buffer = xrealloc(desc.buffer, desc.size + 1);
2001         desc.buffer[desc.size] = 0;
2002
2003         patch->result = desc.buffer;
2004         patch->resultsize = desc.size;
2005
2006         if (0 < patch->is_delete && patch->resultsize)
2007                 return error("removal patch leaves file contents");
2008
2009         return 0;
2010 }
2011
2012 static int check_to_create_blob(const char *new_name, int ok_if_exists)
2013 {
2014         struct stat nst;
2015         if (!lstat(new_name, &nst)) {
2016                 if (S_ISDIR(nst.st_mode) || ok_if_exists)
2017                         return 0;
2018                 /*
2019                  * A leading component of new_name might be a symlink
2020                  * that is going to be removed with this patch, but
2021                  * still pointing at somewhere that has the path.
2022                  * In such a case, path "new_name" does not exist as
2023                  * far as git is concerned.
2024                  */
2025                 if (has_symlink_leading_path(new_name, NULL))
2026                         return 0;
2027
2028                 return error("%s: already exists in working directory", new_name);
2029         }
2030         else if ((errno != ENOENT) && (errno != ENOTDIR))
2031                 return error("%s: %s", new_name, strerror(errno));
2032         return 0;
2033 }
2034
2035 static int check_patch(struct patch *patch, struct patch *prev_patch)
2036 {
2037         struct stat st;
2038         const char *old_name = patch->old_name;
2039         const char *new_name = patch->new_name;
2040         const char *name = old_name ? old_name : new_name;
2041         struct cache_entry *ce = NULL;
2042         int ok_if_exists;
2043
2044         patch->rejected = 1; /* we will drop this after we succeed */
2045         if (old_name) {
2046                 int changed = 0;
2047                 int stat_ret = 0;
2048                 unsigned st_mode = 0;
2049
2050                 if (!cached)
2051                         stat_ret = lstat(old_name, &st);
2052                 if (check_index) {
2053                         int pos = cache_name_pos(old_name, strlen(old_name));
2054                         if (pos < 0)
2055                                 return error("%s: does not exist in index",
2056                                              old_name);
2057                         ce = active_cache[pos];
2058                         if (stat_ret < 0) {
2059                                 struct checkout costate;
2060                                 if (errno != ENOENT)
2061                                         return error("%s: %s", old_name,
2062                                                      strerror(errno));
2063                                 /* checkout */
2064                                 costate.base_dir = "";
2065                                 costate.base_dir_len = 0;
2066                                 costate.force = 0;
2067                                 costate.quiet = 0;
2068                                 costate.not_new = 0;
2069                                 costate.refresh_cache = 1;
2070                                 if (checkout_entry(ce,
2071                                                    &costate,
2072                                                    NULL) ||
2073                                     lstat(old_name, &st))
2074                                         return -1;
2075                         }
2076                         if (!cached)
2077                                 changed = ce_match_stat(ce, &st, 1);
2078                         if (changed)
2079                                 return error("%s: does not match index",
2080                                              old_name);
2081                         if (cached)
2082                                 st_mode = ntohl(ce->ce_mode);
2083                 }
2084                 else if (stat_ret < 0)
2085                         return error("%s: %s", old_name, strerror(errno));
2086
2087                 if (!cached)
2088                         st_mode = ntohl(ce_mode_from_stat(ce, st.st_mode));
2089
2090                 if (patch->is_new < 0)
2091                         patch->is_new = 0;
2092                 if (!patch->old_mode)
2093                         patch->old_mode = st_mode;
2094                 if ((st_mode ^ patch->old_mode) & S_IFMT)
2095                         return error("%s: wrong type", old_name);
2096                 if (st_mode != patch->old_mode)
2097                         fprintf(stderr, "warning: %s has type %o, expected %o\n",
2098                                 old_name, st_mode, patch->old_mode);
2099         }
2100
2101         if (new_name && prev_patch && 0 < prev_patch->is_delete &&
2102             !strcmp(prev_patch->old_name, new_name))
2103                 /* A type-change diff is always split into a patch to
2104                  * delete old, immediately followed by a patch to
2105                  * create new (see diff.c::run_diff()); in such a case
2106                  * it is Ok that the entry to be deleted by the
2107                  * previous patch is still in the working tree and in
2108                  * the index.
2109                  */
2110                 ok_if_exists = 1;
2111         else
2112                 ok_if_exists = 0;
2113
2114         if (new_name &&
2115             ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
2116                 if (check_index &&
2117                     cache_name_pos(new_name, strlen(new_name)) >= 0 &&
2118                     !ok_if_exists)
2119                         return error("%s: already exists in index", new_name);
2120                 if (!cached) {
2121                         int err = check_to_create_blob(new_name, ok_if_exists);
2122                         if (err)
2123                                 return err;
2124                 }
2125                 if (!patch->new_mode) {
2126                         if (0 < patch->is_new)
2127                                 patch->new_mode = S_IFREG | 0644;
2128                         else
2129                                 patch->new_mode = patch->old_mode;
2130                 }
2131         }
2132
2133         if (new_name && old_name) {
2134                 int same = !strcmp(old_name, new_name);
2135                 if (!patch->new_mode)
2136                         patch->new_mode = patch->old_mode;
2137                 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
2138                         return error("new mode (%o) of %s does not match old mode (%o)%s%s",
2139                                 patch->new_mode, new_name, patch->old_mode,
2140                                 same ? "" : " of ", same ? "" : old_name);
2141         }
2142
2143         if (apply_data(patch, &st, ce) < 0)
2144                 return error("%s: patch does not apply", name);
2145         patch->rejected = 0;
2146         return 0;
2147 }
2148
2149 static int check_patch_list(struct patch *patch)
2150 {
2151         struct patch *prev_patch = NULL;
2152         int err = 0;
2153
2154         for (prev_patch = NULL; patch ; patch = patch->next) {
2155                 if (apply_verbosely)
2156                         say_patch_name(stderr,
2157                                        "Checking patch ", patch, "...\n");
2158                 err |= check_patch(patch, prev_patch);
2159                 prev_patch = patch;
2160         }
2161         return err;
2162 }
2163
2164 static void show_index_list(struct patch *list)
2165 {
2166         struct patch *patch;
2167
2168         /* Once we start supporting the reverse patch, it may be
2169          * worth showing the new sha1 prefix, but until then...
2170          */
2171         for (patch = list; patch; patch = patch->next) {
2172                 const unsigned char *sha1_ptr;
2173                 unsigned char sha1[20];
2174                 const char *name;
2175
2176                 name = patch->old_name ? patch->old_name : patch->new_name;
2177                 if (0 < patch->is_new)
2178                         sha1_ptr = null_sha1;
2179                 else if (get_sha1(patch->old_sha1_prefix, sha1))
2180                         die("sha1 information is lacking or useless (%s).",
2181                             name);
2182                 else
2183                         sha1_ptr = sha1;
2184
2185                 printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
2186                 if (line_termination && quote_c_style(name, NULL, NULL, 0))
2187                         quote_c_style(name, NULL, stdout, 0);
2188                 else
2189                         fputs(name, stdout);
2190                 putchar(line_termination);
2191         }
2192 }
2193
2194 static void stat_patch_list(struct patch *patch)
2195 {
2196         int files, adds, dels;
2197
2198         for (files = adds = dels = 0 ; patch ; patch = patch->next) {
2199                 files++;
2200                 adds += patch->lines_added;
2201                 dels += patch->lines_deleted;
2202                 show_stats(patch);
2203         }
2204
2205         printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
2206 }
2207
2208 static void numstat_patch_list(struct patch *patch)
2209 {
2210         for ( ; patch; patch = patch->next) {
2211                 const char *name;
2212                 name = patch->new_name ? patch->new_name : patch->old_name;
2213                 if (patch->is_binary)
2214                         printf("-\t-\t");
2215                 else
2216                         printf("%d\t%d\t",
2217                                patch->lines_added, patch->lines_deleted);
2218                 if (line_termination && quote_c_style(name, NULL, NULL, 0))
2219                         quote_c_style(name, NULL, stdout, 0);
2220                 else
2221                         fputs(name, stdout);
2222                 putchar(line_termination);
2223         }
2224 }
2225
2226 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
2227 {
2228         if (mode)
2229                 printf(" %s mode %06o %s\n", newdelete, mode, name);
2230         else
2231                 printf(" %s %s\n", newdelete, name);
2232 }
2233
2234 static void show_mode_change(struct patch *p, int show_name)
2235 {
2236         if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
2237                 if (show_name)
2238                         printf(" mode change %06o => %06o %s\n",
2239                                p->old_mode, p->new_mode, p->new_name);
2240                 else
2241                         printf(" mode change %06o => %06o\n",
2242                                p->old_mode, p->new_mode);
2243         }
2244 }
2245
2246 static void show_rename_copy(struct patch *p)
2247 {
2248         const char *renamecopy = p->is_rename ? "rename" : "copy";
2249         const char *old, *new;
2250
2251         /* Find common prefix */
2252         old = p->old_name;
2253         new = p->new_name;
2254         while (1) {
2255                 const char *slash_old, *slash_new;
2256                 slash_old = strchr(old, '/');
2257                 slash_new = strchr(new, '/');
2258                 if (!slash_old ||
2259                     !slash_new ||
2260                     slash_old - old != slash_new - new ||
2261                     memcmp(old, new, slash_new - new))
2262                         break;
2263                 old = slash_old + 1;
2264                 new = slash_new + 1;
2265         }
2266         /* p->old_name thru old is the common prefix, and old and new
2267          * through the end of names are renames
2268          */
2269         if (old != p->old_name)
2270                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2271                        (int)(old - p->old_name), p->old_name,
2272                        old, new, p->score);
2273         else
2274                 printf(" %s %s => %s (%d%%)\n", renamecopy,
2275                        p->old_name, p->new_name, p->score);
2276         show_mode_change(p, 0);
2277 }
2278
2279 static void summary_patch_list(struct patch *patch)
2280 {
2281         struct patch *p;
2282
2283         for (p = patch; p; p = p->next) {
2284                 if (p->is_new)
2285                         show_file_mode_name("create", p->new_mode, p->new_name);
2286                 else if (p->is_delete)
2287                         show_file_mode_name("delete", p->old_mode, p->old_name);
2288                 else {
2289                         if (p->is_rename || p->is_copy)
2290                                 show_rename_copy(p);
2291                         else {
2292                                 if (p->score) {
2293                                         printf(" rewrite %s (%d%%)\n",
2294                                                p->new_name, p->score);
2295                                         show_mode_change(p, 0);
2296                                 }
2297                                 else
2298                                         show_mode_change(p, 1);
2299                         }
2300                 }
2301         }
2302 }
2303
2304 static void patch_stats(struct patch *patch)
2305 {
2306         int lines = patch->lines_added + patch->lines_deleted;
2307
2308         if (lines > max_change)
2309                 max_change = lines;
2310         if (patch->old_name) {
2311                 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
2312                 if (!len)
2313                         len = strlen(patch->old_name);
2314                 if (len > max_len)
2315                         max_len = len;
2316         }
2317         if (patch->new_name) {
2318                 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
2319                 if (!len)
2320                         len = strlen(patch->new_name);
2321                 if (len > max_len)
2322                         max_len = len;
2323         }
2324 }
2325
2326 static void remove_file(struct patch *patch, int rmdir_empty)
2327 {
2328         if (update_index) {
2329                 if (remove_file_from_cache(patch->old_name) < 0)
2330                         die("unable to remove %s from index", patch->old_name);
2331                 cache_tree_invalidate_path(active_cache_tree, patch->old_name);
2332         }
2333         if (!cached) {
2334                 if (!unlink(patch->old_name) && rmdir_empty) {
2335                         char *name = xstrdup(patch->old_name);
2336                         char *end = strrchr(name, '/');
2337                         while (end) {
2338                                 *end = 0;
2339                                 if (rmdir(name))
2340                                         break;
2341                                 end = strrchr(name, '/');
2342                         }
2343                         free(name);
2344                 }
2345         }
2346 }
2347
2348 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
2349 {
2350         struct stat st;
2351         struct cache_entry *ce;
2352         int namelen = strlen(path);
2353         unsigned ce_size = cache_entry_size(namelen);
2354
2355         if (!update_index)
2356                 return;
2357
2358         ce = xcalloc(1, ce_size);
2359         memcpy(ce->name, path, namelen);
2360         ce->ce_mode = create_ce_mode(mode);
2361         ce->ce_flags = htons(namelen);
2362         if (!cached) {
2363                 if (lstat(path, &st) < 0)
2364                         die("unable to stat newly created file %s", path);
2365                 fill_stat_cache_info(ce, &st);
2366         }
2367         if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
2368                 die("unable to create backing store for newly created file %s", path);
2369         if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
2370                 die("unable to add cache entry for %s", path);
2371 }
2372
2373 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2374 {
2375         int fd;
2376         char *nbuf;
2377
2378         if (has_symlinks && S_ISLNK(mode))
2379                 /* Although buf:size is counted string, it also is NUL
2380                  * terminated.
2381                  */
2382                 return symlink(buf, path);
2383
2384         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
2385         if (fd < 0)
2386                 return -1;
2387
2388         nbuf = convert_to_working_tree(path, buf, &size);
2389         if (nbuf)
2390                 buf = nbuf;
2391
2392         while (size) {
2393                 int written = xwrite(fd, buf, size);
2394                 if (written < 0)
2395                         die("writing file %s: %s", path, strerror(errno));
2396                 if (!written)
2397                         die("out of space writing file %s", path);
2398                 buf += written;
2399                 size -= written;
2400         }
2401         if (close(fd) < 0)
2402                 die("closing file %s: %s", path, strerror(errno));
2403         if (nbuf)
2404                 free(nbuf);
2405         return 0;
2406 }
2407
2408 /*
2409  * We optimistically assume that the directories exist,
2410  * which is true 99% of the time anyway. If they don't,
2411  * we create them and try again.
2412  */
2413 static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
2414 {
2415         if (cached)
2416                 return;
2417         if (!try_create_file(path, mode, buf, size))
2418                 return;
2419
2420         if (errno == ENOENT) {
2421                 if (safe_create_leading_directories(path))
2422                         return;
2423                 if (!try_create_file(path, mode, buf, size))
2424                         return;
2425         }
2426
2427         if (errno == EEXIST || errno == EACCES) {
2428                 /* We may be trying to create a file where a directory
2429                  * used to be.
2430                  */
2431                 struct stat st;
2432                 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
2433                         errno = EEXIST;
2434         }
2435
2436         if (errno == EEXIST) {
2437                 unsigned int nr = getpid();
2438
2439                 for (;;) {
2440                         const char *newpath;
2441                         newpath = mkpath("%s~%u", path, nr);
2442                         if (!try_create_file(newpath, mode, buf, size)) {
2443                                 if (!rename(newpath, path))
2444                                         return;
2445                                 unlink(newpath);
2446                                 break;
2447                         }
2448                         if (errno != EEXIST)
2449                                 break;
2450                         ++nr;
2451                 }
2452         }
2453         die("unable to write file %s mode %o", path, mode);
2454 }
2455
2456 static void create_file(struct patch *patch)
2457 {
2458         char *path = patch->new_name;
2459         unsigned mode = patch->new_mode;
2460         unsigned long size = patch->resultsize;
2461         char *buf = patch->result;
2462
2463         if (!mode)
2464                 mode = S_IFREG | 0644;
2465         create_one_file(path, mode, buf, size);
2466         add_index_file(path, mode, buf, size);
2467         cache_tree_invalidate_path(active_cache_tree, path);
2468 }
2469
2470 /* phase zero is to remove, phase one is to create */
2471 static void write_out_one_result(struct patch *patch, int phase)
2472 {
2473         if (patch->is_delete > 0) {
2474                 if (phase == 0)
2475                         remove_file(patch, 1);
2476                 return;
2477         }
2478         if (patch->is_new > 0 || patch->is_copy) {
2479                 if (phase == 1)
2480                         create_file(patch);
2481                 return;
2482         }
2483         /*
2484          * Rename or modification boils down to the same
2485          * thing: remove the old, write the new
2486          */
2487         if (phase == 0)
2488                 remove_file(patch, 0);
2489         if (phase == 1)
2490                 create_file(patch);
2491 }
2492
2493 static int write_out_one_reject(struct patch *patch)
2494 {
2495         FILE *rej;
2496         char namebuf[PATH_MAX];
2497         struct fragment *frag;
2498         int cnt = 0;
2499
2500         for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
2501                 if (!frag->rejected)
2502                         continue;
2503                 cnt++;
2504         }
2505
2506         if (!cnt) {
2507                 if (apply_verbosely)
2508                         say_patch_name(stderr,
2509                                        "Applied patch ", patch, " cleanly.\n");
2510                 return 0;
2511         }
2512
2513         /* This should not happen, because a removal patch that leaves
2514          * contents are marked "rejected" at the patch level.
2515          */
2516         if (!patch->new_name)
2517                 die("internal error");
2518
2519         /* Say this even without --verbose */
2520         say_patch_name(stderr, "Applying patch ", patch, " with");
2521         fprintf(stderr, " %d rejects...\n", cnt);
2522
2523         cnt = strlen(patch->new_name);
2524         if (ARRAY_SIZE(namebuf) <= cnt + 5) {
2525                 cnt = ARRAY_SIZE(namebuf) - 5;
2526                 fprintf(stderr,
2527                         "warning: truncating .rej filename to %.*s.rej",
2528                         cnt - 1, patch->new_name);
2529         }
2530         memcpy(namebuf, patch->new_name, cnt);
2531         memcpy(namebuf + cnt, ".rej", 5);
2532
2533         rej = fopen(namebuf, "w");
2534         if (!rej)
2535                 return error("cannot open %s: %s", namebuf, strerror(errno));
2536
2537         /* Normal git tools never deal with .rej, so do not pretend
2538          * this is a git patch by saying --git nor give extended
2539          * headers.  While at it, maybe please "kompare" that wants
2540          * the trailing TAB and some garbage at the end of line ;-).
2541          */
2542         fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
2543                 patch->new_name, patch->new_name);
2544         for (cnt = 1, frag = patch->fragments;
2545              frag;
2546              cnt++, frag = frag->next) {
2547                 if (!frag->rejected) {
2548                         fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);
2549                         continue;
2550                 }
2551                 fprintf(stderr, "Rejected hunk #%d.\n", cnt);
2552                 fprintf(rej, "%.*s", frag->size, frag->patch);
2553                 if (frag->patch[frag->size-1] != '\n')
2554                         fputc('\n', rej);
2555         }
2556         fclose(rej);
2557         return -1;
2558 }
2559
2560 static int write_out_results(struct patch *list, int skipped_patch)
2561 {
2562         int phase;
2563         int errs = 0;
2564         struct patch *l;
2565
2566         if (!list && !skipped_patch)
2567                 return error("No changes");
2568
2569         for (phase = 0; phase < 2; phase++) {
2570                 l = list;
2571                 while (l) {
2572                         if (l->rejected)
2573                                 errs = 1;
2574                         else {
2575                                 write_out_one_result(l, phase);
2576                                 if (phase == 1 && write_out_one_reject(l))
2577                                         errs = 1;
2578                         }
2579                         l = l->next;
2580                 }
2581         }
2582         return errs;
2583 }
2584
2585 static struct lock_file lock_file;
2586
2587 static struct excludes {
2588         struct excludes *next;
2589         const char *path;
2590 } *excludes;
2591
2592 static int use_patch(struct patch *p)
2593 {
2594         const char *pathname = p->new_name ? p->new_name : p->old_name;
2595         struct excludes *x = excludes;
2596         while (x) {
2597                 if (fnmatch(x->path, pathname, 0) == 0)
2598                         return 0;
2599                 x = x->next;
2600         }
2601         if (0 < prefix_length) {
2602                 int pathlen = strlen(pathname);
2603                 if (pathlen <= prefix_length ||
2604                     memcmp(prefix, pathname, prefix_length))
2605                         return 0;
2606         }
2607         return 1;
2608 }
2609
2610 static void prefix_one(char **name)
2611 {
2612         char *old_name = *name;
2613         if (!old_name)
2614                 return;
2615         *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
2616         free(old_name);
2617 }
2618
2619 static void prefix_patches(struct patch *p)
2620 {
2621         if (!prefix || p->is_toplevel_relative)
2622                 return;
2623         for ( ; p; p = p->next) {
2624                 if (p->new_name == p->old_name) {
2625                         char *prefixed = p->new_name;
2626                         prefix_one(&prefixed);
2627                         p->new_name = p->old_name = prefixed;
2628                 }
2629                 else {
2630                         prefix_one(&p->new_name);
2631                         prefix_one(&p->old_name);
2632                 }
2633         }
2634 }
2635
2636 static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2637 {
2638         unsigned long offset, size;
2639         char *buffer = read_patch_file(fd, &size);
2640         struct patch *list = NULL, **listp = &list;
2641         int skipped_patch = 0;
2642
2643         patch_input_file = filename;
2644         if (!buffer)
2645                 return -1;
2646         offset = 0;
2647         while (size > 0) {
2648                 struct patch *patch;
2649                 int nr;
2650
2651                 patch = xcalloc(1, sizeof(*patch));
2652                 patch->inaccurate_eof = inaccurate_eof;
2653                 nr = parse_chunk(buffer + offset, size, patch);
2654                 if (nr < 0)
2655                         break;
2656                 if (apply_in_reverse)
2657                         reverse_patches(patch);
2658                 if (prefix)
2659                         prefix_patches(patch);
2660                 if (use_patch(patch)) {
2661                         patch_stats(patch);
2662                         *listp = patch;
2663                         listp = &patch->next;
2664                 }
2665                 else {
2666                         /* perhaps free it a bit better? */
2667                         free(patch);
2668                         skipped_patch++;
2669                 }
2670                 offset += nr;
2671                 size -= nr;
2672         }
2673
2674         if (whitespace_error && (new_whitespace == error_on_whitespace))
2675                 apply = 0;
2676
2677         update_index = check_index && apply;
2678         if (update_index && newfd < 0)
2679                 newfd = hold_locked_index(&lock_file, 1);
2680
2681         if (check_index) {
2682                 if (read_cache() < 0)
2683                         die("unable to read index file");
2684         }
2685
2686         if ((check || apply) &&
2687             check_patch_list(list) < 0 &&
2688             !apply_with_reject)
2689                 exit(1);
2690
2691         if (apply && write_out_results(list, skipped_patch))
2692                 exit(1);
2693
2694         if (show_index_info)
2695                 show_index_list(list);
2696
2697         if (diffstat)
2698                 stat_patch_list(list);
2699
2700         if (numstat)
2701                 numstat_patch_list(list);
2702
2703         if (summary)
2704                 summary_patch_list(list);
2705
2706         free(buffer);
2707         return 0;
2708 }
2709
2710 static int git_apply_config(const char *var, const char *value)
2711 {
2712         if (!strcmp(var, "apply.whitespace")) {
2713                 apply_default_whitespace = xstrdup(value);
2714                 return 0;
2715         }
2716         return git_default_config(var, value);
2717 }
2718
2719
2720 int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2721 {
2722         int i;
2723         int read_stdin = 1;
2724         int inaccurate_eof = 0;
2725         int errs = 0;
2726         int is_not_gitdir = 0;
2727
2728         const char *whitespace_option = NULL;
2729
2730         prefix = setup_git_directory_gently(&is_not_gitdir);
2731         prefix_length = prefix ? strlen(prefix) : 0;
2732         git_config(git_apply_config);
2733         if (apply_default_whitespace)
2734                 parse_whitespace_option(apply_default_whitespace);
2735
2736         for (i = 1; i < argc; i++) {
2737                 const char *arg = argv[i];
2738                 char *end;
2739                 int fd;
2740
2741                 if (!strcmp(arg, "-")) {
2742                         errs |= apply_patch(0, "<stdin>", inaccurate_eof);
2743                         read_stdin = 0;
2744                         continue;
2745                 }
2746                 if (!prefixcmp(arg, "--exclude=")) {
2747                         struct excludes *x = xmalloc(sizeof(*x));
2748                         x->path = arg + 10;
2749                         x->next = excludes;
2750                         excludes = x;
2751                         continue;
2752                 }
2753                 if (!prefixcmp(arg, "-p")) {
2754                         p_value = atoi(arg + 2);
2755                         p_value_known = 1;
2756                         continue;
2757                 }
2758                 if (!strcmp(arg, "--no-add")) {
2759                         no_add = 1;
2760                         continue;
2761                 }
2762                 if (!strcmp(arg, "--stat")) {
2763                         apply = 0;
2764                         diffstat = 1;
2765                         continue;
2766                 }
2767                 if (!strcmp(arg, "--allow-binary-replacement") ||
2768                     !strcmp(arg, "--binary")) {
2769                         continue; /* now no-op */
2770                 }
2771                 if (!strcmp(arg, "--numstat")) {
2772                         apply = 0;
2773                         numstat = 1;
2774                         continue;
2775                 }
2776                 if (!strcmp(arg, "--summary")) {
2777                         apply = 0;
2778                         summary = 1;
2779                         continue;
2780                 }
2781                 if (!strcmp(arg, "--check")) {
2782                         apply = 0;
2783                         check = 1;
2784                         continue;
2785                 }
2786                 if (!strcmp(arg, "--index")) {
2787                         if (is_not_gitdir)
2788                                 die("--index outside a repository");
2789                         check_index = 1;
2790                         continue;
2791                 }
2792                 if (!strcmp(arg, "--cached")) {
2793                         if (is_not_gitdir)
2794                                 die("--cached outside a repository");
2795                         check_index = 1;
2796                         cached = 1;
2797                         continue;
2798                 }
2799                 if (!strcmp(arg, "--apply")) {
2800                         apply = 1;
2801                         continue;
2802                 }
2803                 if (!strcmp(arg, "--index-info")) {
2804                         apply = 0;
2805                         show_index_info = 1;
2806                         continue;
2807                 }
2808                 if (!strcmp(arg, "-z")) {
2809                         line_termination = 0;
2810                         continue;
2811                 }
2812                 if (!prefixcmp(arg, "-C")) {
2813                         p_context = strtoul(arg + 2, &end, 0);
2814                         if (*end != '\0')
2815                                 die("unrecognized context count '%s'", arg + 2);
2816                         continue;
2817                 }
2818                 if (!prefixcmp(arg, "--whitespace=")) {
2819                         whitespace_option = arg + 13;
2820                         parse_whitespace_option(arg + 13);
2821                         continue;
2822                 }
2823                 if (!strcmp(arg, "-R") || !strcmp(arg, "--reverse")) {
2824                         apply_in_reverse = 1;
2825                         continue;
2826                 }
2827                 if (!strcmp(arg, "--unidiff-zero")) {
2828                         unidiff_zero = 1;
2829                         continue;
2830                 }
2831                 if (!strcmp(arg, "--reject")) {
2832                         apply = apply_with_reject = apply_verbosely = 1;
2833                         continue;
2834                 }
2835                 if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose")) {
2836                         apply_verbosely = 1;
2837                         continue;
2838                 }
2839                 if (!strcmp(arg, "--inaccurate-eof")) {
2840                         inaccurate_eof = 1;
2841                         continue;
2842                 }
2843                 if (0 < prefix_length)
2844                         arg = prefix_filename(prefix, prefix_length, arg);
2845
2846                 fd = open(arg, O_RDONLY);
2847                 if (fd < 0)
2848                         usage(apply_usage);
2849                 read_stdin = 0;
2850                 set_default_whitespace_mode(whitespace_option);
2851                 errs |= apply_patch(fd, arg, inaccurate_eof);
2852                 close(fd);
2853         }
2854         set_default_whitespace_mode(whitespace_option);
2855         if (read_stdin)
2856                 errs |= apply_patch(0, "<stdin>", inaccurate_eof);
2857         if (whitespace_error) {
2858                 if (squelch_whitespace_errors &&
2859                     squelch_whitespace_errors < whitespace_error) {
2860                         int squelched =
2861                                 whitespace_error - squelch_whitespace_errors;
2862                         fprintf(stderr, "warning: squelched %d "
2863                                 "whitespace error%s\n",
2864                                 squelched,
2865                                 squelched == 1 ? "" : "s");
2866                 }
2867                 if (new_whitespace == error_on_whitespace)
2868                         die("%d line%s add%s trailing whitespaces.",
2869                             whitespace_error,
2870                             whitespace_error == 1 ? "" : "s",
2871                             whitespace_error == 1 ? "s" : "");
2872                 if (applied_after_stripping)
2873                         fprintf(stderr, "warning: %d line%s applied after"
2874                                 " stripping trailing whitespaces.\n",
2875                                 applied_after_stripping,
2876                                 applied_after_stripping == 1 ? "" : "s");
2877                 else if (whitespace_error)
2878                         fprintf(stderr, "warning: %d line%s add%s trailing"
2879                                 " whitespaces.\n",
2880                                 whitespace_error,
2881                                 whitespace_error == 1 ? "" : "s",
2882                                 whitespace_error == 1 ? "s" : "");
2883         }
2884
2885         if (update_index) {
2886                 if (write_cache(newfd, active_cache, active_nr) ||
2887                     close(newfd) || commit_locked_index(&lock_file))
2888                         die("Unable to write new index file");
2889         }
2890
2891         return !!errs;
2892 }