Merge branch 'master' into next
[git.git] / apply.c
1 /*
2  * apply.c
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  *
6  * This applies patches on top of some (arbitrary) version of the SCM.
7  *
8  */
9 #include <fnmatch.h>
10 #include "cache.h"
11 #include "cache-tree.h"
12 #include "quote.h"
13 #include "blob.h"
14 #include "delta.h"
15
16 //  --check turns on checking that the working tree matches the
17 //    files that are being modified, but doesn't apply the patch
18 //  --stat does just a diffstat, and doesn't actually apply
19 //  --numstat does numeric diffstat, and doesn't actually apply
20 //  --index-info shows the old and new index info for paths if available.
21 //
22 static const char *prefix;
23 static int prefix_length = -1;
24 static int newfd = -1;
25
26 static int p_value = 1;
27 static int allow_binary_replacement = 0;
28 static int check_index = 0;
29 static int write_index = 0;
30 static int diffstat = 0;
31 static int numstat = 0;
32 static int summary = 0;
33 static int check = 0;
34 static int apply = 1;
35 static int no_add = 0;
36 static int show_index_info = 0;
37 static int line_termination = '\n';
38 static unsigned long p_context = -1;
39 static const char apply_usage[] =
40 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
41
42 static enum whitespace_eol {
43         nowarn_whitespace,
44         warn_on_whitespace,
45         error_on_whitespace,
46         strip_whitespace,
47 } new_whitespace = warn_on_whitespace;
48 static int whitespace_error = 0;
49 static int squelch_whitespace_errors = 5;
50 static int applied_after_stripping = 0;
51 static const char *patch_input_file = NULL;
52
53 static void parse_whitespace_option(const char *option)
54 {
55         if (!option) {
56                 new_whitespace = warn_on_whitespace;
57                 return;
58         }
59         if (!strcmp(option, "warn")) {
60                 new_whitespace = warn_on_whitespace;
61                 return;
62         }
63         if (!strcmp(option, "nowarn")) {
64                 new_whitespace = nowarn_whitespace;
65                 return;
66         }
67         if (!strcmp(option, "error")) {
68                 new_whitespace = error_on_whitespace;
69                 return;
70         }
71         if (!strcmp(option, "error-all")) {
72                 new_whitespace = error_on_whitespace;
73                 squelch_whitespace_errors = 0;
74                 return;
75         }
76         if (!strcmp(option, "strip")) {
77                 new_whitespace = strip_whitespace;
78                 return;
79         }
80         die("unrecognized whitespace option '%s'", option);
81 }
82
83 static void set_default_whitespace_mode(const char *whitespace_option)
84 {
85         if (!whitespace_option && !apply_default_whitespace) {
86                 new_whitespace = (apply
87                                   ? warn_on_whitespace
88                                   : nowarn_whitespace);
89         }
90 }
91
92 /*
93  * For "diff-stat" like behaviour, we keep track of the biggest change
94  * we've seen, and the longest filename. That allows us to do simple
95  * scaling.
96  */
97 static int max_change, max_len;
98
99 /*
100  * Various "current state", notably line numbers and what
101  * file (and how) we're patching right now.. The "is_xxxx"
102  * things are flags, where -1 means "don't know yet".
103  */
104 static int linenr = 1;
105
106 struct fragment {
107         unsigned long leading, trailing;
108         unsigned long oldpos, oldlines;
109         unsigned long newpos, newlines;
110         const char *patch;
111         int size;
112         struct fragment *next;
113 };
114
115 struct patch {
116         char *new_name, *old_name, *def_name;
117         unsigned int old_mode, new_mode;
118         int is_rename, is_copy, is_new, is_delete, is_binary;
119 #define BINARY_DELTA_DEFLATED 1
120 #define BINARY_LITERAL_DEFLATED 2
121         unsigned long deflate_origlen;
122         int lines_added, lines_deleted;
123         int score;
124         struct fragment *fragments;
125         char *result;
126         unsigned long resultsize;
127         char old_sha1_prefix[41];
128         char new_sha1_prefix[41];
129         struct patch *next;
130 };
131
132 #define CHUNKSIZE (8192)
133 #define SLOP (16)
134
135 static void *read_patch_file(int fd, unsigned long *sizep)
136 {
137         unsigned long size = 0, alloc = CHUNKSIZE;
138         void *buffer = xmalloc(alloc);
139
140         for (;;) {
141                 int nr = alloc - size;
142                 if (nr < 1024) {
143                         alloc += CHUNKSIZE;
144                         buffer = xrealloc(buffer, alloc);
145                         nr = alloc - size;
146                 }
147                 nr = xread(fd, buffer + size, nr);
148                 if (!nr)
149                         break;
150                 if (nr < 0)
151                         die("git-apply: read returned %s", strerror(errno));
152                 size += nr;
153         }
154         *sizep = size;
155
156         /*
157          * Make sure that we have some slop in the buffer
158          * so that we can do speculative "memcmp" etc, and
159          * see to it that it is NUL-filled.
160          */
161         if (alloc < size + SLOP)
162                 buffer = xrealloc(buffer, size + SLOP);
163         memset(buffer + size, 0, SLOP);
164         return buffer;
165 }
166
167 static unsigned long linelen(const char *buffer, unsigned long size)
168 {
169         unsigned long len = 0;
170         while (size--) {
171                 len++;
172                 if (*buffer++ == '\n')
173                         break;
174         }
175         return len;
176 }
177
178 static int is_dev_null(const char *str)
179 {
180         return !memcmp("/dev/null", str, 9) && isspace(str[9]);
181 }
182
183 #define TERM_SPACE      1
184 #define TERM_TAB        2
185
186 static int name_terminate(const char *name, int namelen, int c, int terminate)
187 {
188         if (c == ' ' && !(terminate & TERM_SPACE))
189                 return 0;
190         if (c == '\t' && !(terminate & TERM_TAB))
191                 return 0;
192
193         return 1;
194 }
195
196 static char * find_name(const char *line, char *def, int p_value, int terminate)
197 {
198         int len;
199         const char *start = line;
200         char *name;
201
202         if (*line == '"') {
203                 /* Proposed "new-style" GNU patch/diff format; see
204                  * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
205                  */
206                 name = unquote_c_style(line, NULL);
207                 if (name) {
208                         char *cp = name;
209                         while (p_value) {
210                                 cp = strchr(name, '/');
211                                 if (!cp)
212                                         break;
213                                 cp++;
214                                 p_value--;
215                         }
216                         if (cp) {
217                                 /* name can later be freed, so we need
218                                  * to memmove, not just return cp
219                                  */
220                                 memmove(name, cp, strlen(cp) + 1);
221                                 free(def);
222                                 return name;
223                         }
224                         else {
225                                 free(name);
226                                 name = NULL;
227                         }
228                 }
229         }
230
231         for (;;) {
232                 char c = *line;
233
234                 if (isspace(c)) {
235                         if (c == '\n')
236                                 break;
237                         if (name_terminate(start, line-start, c, terminate))
238                                 break;
239                 }
240                 line++;
241                 if (c == '/' && !--p_value)
242                         start = line;
243         }
244         if (!start)
245                 return def;
246         len = line - start;
247         if (!len)
248                 return def;
249
250         /*
251          * Generally we prefer the shorter name, especially
252          * if the other one is just a variation of that with
253          * something else tacked on to the end (ie "file.orig"
254          * or "file~").
255          */
256         if (def) {
257                 int deflen = strlen(def);
258                 if (deflen < len && !strncmp(start, def, deflen))
259                         return def;
260         }
261
262         name = xmalloc(len + 1);
263         memcpy(name, start, len);
264         name[len] = 0;
265         free(def);
266         return name;
267 }
268
269 /*
270  * Get the name etc info from the --/+++ lines of a traditional patch header
271  *
272  * NOTE! This hardcodes "-p1" behaviour in filename detection.
273  *
274  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
275  * files, we can happily check the index for a match, but for creating a
276  * new file we should try to match whatever "patch" does. I have no idea.
277  */
278 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
279 {
280         char *name;
281
282         first += 4;     // skip "--- "
283         second += 4;    // skip "+++ "
284         if (is_dev_null(first)) {
285                 patch->is_new = 1;
286                 patch->is_delete = 0;
287                 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
288                 patch->new_name = name;
289         } else if (is_dev_null(second)) {
290                 patch->is_new = 0;
291                 patch->is_delete = 1;
292                 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
293                 patch->old_name = name;
294         } else {
295                 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
296                 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
297                 patch->old_name = patch->new_name = name;
298         }
299         if (!name)
300                 die("unable to find filename in patch at line %d", linenr);
301 }
302
303 static int gitdiff_hdrend(const char *line, struct patch *patch)
304 {
305         return -1;
306 }
307
308 /*
309  * We're anal about diff header consistency, to make
310  * sure that we don't end up having strange ambiguous
311  * patches floating around.
312  *
313  * As a result, gitdiff_{old|new}name() will check
314  * their names against any previous information, just
315  * to make sure..
316  */
317 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
318 {
319         if (!orig_name && !isnull)
320                 return find_name(line, NULL, 1, 0);
321
322         if (orig_name) {
323                 int len;
324                 const char *name;
325                 char *another;
326                 name = orig_name;
327                 len = strlen(name);
328                 if (isnull)
329                         die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
330                 another = find_name(line, NULL, 1, 0);
331                 if (!another || memcmp(another, name, len))
332                         die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
333                 free(another);
334                 return orig_name;
335         }
336         else {
337                 /* expect "/dev/null" */
338                 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
339                         die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
340                 return NULL;
341         }
342 }
343
344 static int gitdiff_oldname(const char *line, struct patch *patch)
345 {
346         patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
347         return 0;
348 }
349
350 static int gitdiff_newname(const char *line, struct patch *patch)
351 {
352         patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
353         return 0;
354 }
355
356 static int gitdiff_oldmode(const char *line, struct patch *patch)
357 {
358         patch->old_mode = strtoul(line, NULL, 8);
359         return 0;
360 }
361
362 static int gitdiff_newmode(const char *line, struct patch *patch)
363 {
364         patch->new_mode = strtoul(line, NULL, 8);
365         return 0;
366 }
367
368 static int gitdiff_delete(const char *line, struct patch *patch)
369 {
370         patch->is_delete = 1;
371         patch->old_name = patch->def_name;
372         return gitdiff_oldmode(line, patch);
373 }
374
375 static int gitdiff_newfile(const char *line, struct patch *patch)
376 {
377         patch->is_new = 1;
378         patch->new_name = patch->def_name;
379         return gitdiff_newmode(line, patch);
380 }
381
382 static int gitdiff_copysrc(const char *line, struct patch *patch)
383 {
384         patch->is_copy = 1;
385         patch->old_name = find_name(line, NULL, 0, 0);
386         return 0;
387 }
388
389 static int gitdiff_copydst(const char *line, struct patch *patch)
390 {
391         patch->is_copy = 1;
392         patch->new_name = find_name(line, NULL, 0, 0);
393         return 0;
394 }
395
396 static int gitdiff_renamesrc(const char *line, struct patch *patch)
397 {
398         patch->is_rename = 1;
399         patch->old_name = find_name(line, NULL, 0, 0);
400         return 0;
401 }
402
403 static int gitdiff_renamedst(const char *line, struct patch *patch)
404 {
405         patch->is_rename = 1;
406         patch->new_name = find_name(line, NULL, 0, 0);
407         return 0;
408 }
409
410 static int gitdiff_similarity(const char *line, struct patch *patch)
411 {
412         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
413                 patch->score = 0;
414         return 0;
415 }
416
417 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
418 {
419         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
420                 patch->score = 0;
421         return 0;
422 }
423
424 static int gitdiff_index(const char *line, struct patch *patch)
425 {
426         /* index line is N hexadecimal, "..", N hexadecimal,
427          * and optional space with octal mode.
428          */
429         const char *ptr, *eol;
430         int len;
431
432         ptr = strchr(line, '.');
433         if (!ptr || ptr[1] != '.' || 40 < ptr - line)
434                 return 0;
435         len = ptr - line;
436         memcpy(patch->old_sha1_prefix, line, len);
437         patch->old_sha1_prefix[len] = 0;
438
439         line = ptr + 2;
440         ptr = strchr(line, ' ');
441         eol = strchr(line, '\n');
442
443         if (!ptr || eol < ptr)
444                 ptr = eol;
445         len = ptr - line;
446
447         if (40 < len)
448                 return 0;
449         memcpy(patch->new_sha1_prefix, line, len);
450         patch->new_sha1_prefix[len] = 0;
451         if (*ptr == ' ')
452                 patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
453         return 0;
454 }
455
456 /*
457  * This is normal for a diff that doesn't change anything: we'll fall through
458  * into the next diff. Tell the parser to break out.
459  */
460 static int gitdiff_unrecognized(const char *line, struct patch *patch)
461 {
462         return -1;
463 }
464
465 static const char *stop_at_slash(const char *line, int llen)
466 {
467         int i;
468
469         for (i = 0; i < llen; i++) {
470                 int ch = line[i];
471                 if (ch == '/')
472                         return line + i;
473         }
474         return NULL;
475 }
476
477 /* This is to extract the same name that appears on "diff --git"
478  * line.  We do not find and return anything if it is a rename
479  * patch, and it is OK because we will find the name elsewhere.
480  * We need to reliably find name only when it is mode-change only,
481  * creation or deletion of an empty file.  In any of these cases,
482  * both sides are the same name under a/ and b/ respectively.
483  */
484 static char *git_header_name(char *line, int llen)
485 {
486         int len;
487         const char *name;
488         const char *second = NULL;
489
490         line += strlen("diff --git ");
491         llen -= strlen("diff --git ");
492
493         if (*line == '"') {
494                 const char *cp;
495                 char *first = unquote_c_style(line, &second);
496                 if (!first)
497                         return NULL;
498
499                 /* advance to the first slash */
500                 cp = stop_at_slash(first, strlen(first));
501                 if (!cp || cp == first) {
502                         /* we do not accept absolute paths */
503                 free_first_and_fail:
504                         free(first);
505                         return NULL;
506                 }
507                 len = strlen(cp+1);
508                 memmove(first, cp+1, len+1); /* including NUL */
509
510                 /* second points at one past closing dq of name.
511                  * find the second name.
512                  */
513                 while ((second < line + llen) && isspace(*second))
514                         second++;
515
516                 if (line + llen <= second)
517                         goto free_first_and_fail;
518                 if (*second == '"') {
519                         char *sp = unquote_c_style(second, NULL);
520                         if (!sp)
521                                 goto free_first_and_fail;
522                         cp = stop_at_slash(sp, strlen(sp));
523                         if (!cp || cp == sp) {
524                         free_both_and_fail:
525                                 free(sp);
526                                 goto free_first_and_fail;
527                         }
528                         /* They must match, otherwise ignore */
529                         if (strcmp(cp+1, first))
530                                 goto free_both_and_fail;
531                         free(sp);
532                         return first;
533                 }
534
535                 /* unquoted second */
536                 cp = stop_at_slash(second, line + llen - second);
537                 if (!cp || cp == second)
538                         goto free_first_and_fail;
539                 cp++;
540                 if (line + llen - cp != len + 1 ||
541                     memcmp(first, cp, len))
542                         goto free_first_and_fail;
543                 return first;
544         }
545
546         /* unquoted first name */
547         name = stop_at_slash(line, llen);
548         if (!name || name == line)
549                 return NULL;
550
551         name++;
552
553         /* since the first name is unquoted, a dq if exists must be
554          * the beginning of the second name.
555          */
556         for (second = name; second < line + llen; second++) {
557                 if (*second == '"') {
558                         const char *cp = second;
559                         const char *np;
560                         char *sp = unquote_c_style(second, NULL);
561
562                         if (!sp)
563                                 return NULL;
564                         np = stop_at_slash(sp, strlen(sp));
565                         if (!np || np == sp) {
566                         free_second_and_fail:
567                                 free(sp);
568                                 return NULL;
569                         }
570                         np++;
571                         len = strlen(np);
572                         if (len < cp - name &&
573                             !strncmp(np, name, len) &&
574                             isspace(name[len])) {
575                                 /* Good */
576                                 memmove(sp, np, len + 1);
577                                 return sp;
578                         }
579                         goto free_second_and_fail;
580                 }
581         }
582
583         /*
584          * Accept a name only if it shows up twice, exactly the same
585          * form.
586          */
587         for (len = 0 ; ; len++) {
588                 char c = name[len];
589
590                 switch (c) {
591                 default:
592                         continue;
593                 case '\n':
594                         return NULL;
595                 case '\t': case ' ':
596                         second = name+len;
597                         for (;;) {
598                                 char c = *second++;
599                                 if (c == '\n')
600                                         return NULL;
601                                 if (c == '/')
602                                         break;
603                         }
604                         if (second[len] == '\n' && !memcmp(name, second, len)) {
605                                 char *ret = xmalloc(len + 1);
606                                 memcpy(ret, name, len);
607                                 ret[len] = 0;
608                                 return ret;
609                         }
610                 }
611         }
612         return NULL;
613 }
614
615 /* Verify that we recognize the lines following a git header */
616 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
617 {
618         unsigned long offset;
619
620         /* A git diff has explicit new/delete information, so we don't guess */
621         patch->is_new = 0;
622         patch->is_delete = 0;
623
624         /*
625          * Some things may not have the old name in the
626          * rest of the headers anywhere (pure mode changes,
627          * or removing or adding empty files), so we get
628          * the default name from the header.
629          */
630         patch->def_name = git_header_name(line, len);
631
632         line += len;
633         size -= len;
634         linenr++;
635         for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
636                 static const struct opentry {
637                         const char *str;
638                         int (*fn)(const char *, struct patch *);
639                 } optable[] = {
640                         { "@@ -", gitdiff_hdrend },
641                         { "--- ", gitdiff_oldname },
642                         { "+++ ", gitdiff_newname },
643                         { "old mode ", gitdiff_oldmode },
644                         { "new mode ", gitdiff_newmode },
645                         { "deleted file mode ", gitdiff_delete },
646                         { "new file mode ", gitdiff_newfile },
647                         { "copy from ", gitdiff_copysrc },
648                         { "copy to ", gitdiff_copydst },
649                         { "rename old ", gitdiff_renamesrc },
650                         { "rename new ", gitdiff_renamedst },
651                         { "rename from ", gitdiff_renamesrc },
652                         { "rename to ", gitdiff_renamedst },
653                         { "similarity index ", gitdiff_similarity },
654                         { "dissimilarity index ", gitdiff_dissimilarity },
655                         { "index ", gitdiff_index },
656                         { "", gitdiff_unrecognized },
657                 };
658                 int i;
659
660                 len = linelen(line, size);
661                 if (!len || line[len-1] != '\n')
662                         break;
663                 for (i = 0; i < ARRAY_SIZE(optable); i++) {
664                         const struct opentry *p = optable + i;
665                         int oplen = strlen(p->str);
666                         if (len < oplen || memcmp(p->str, line, oplen))
667                                 continue;
668                         if (p->fn(line + oplen, patch) < 0)
669                                 return offset;
670                         break;
671                 }
672         }
673
674         return offset;
675 }
676
677 static int parse_num(const char *line, unsigned long *p)
678 {
679         char *ptr;
680
681         if (!isdigit(*line))
682                 return 0;
683         *p = strtoul(line, &ptr, 10);
684         return ptr - line;
685 }
686
687 static int parse_range(const char *line, int len, int offset, const char *expect,
688                         unsigned long *p1, unsigned long *p2)
689 {
690         int digits, ex;
691
692         if (offset < 0 || offset >= len)
693                 return -1;
694         line += offset;
695         len -= offset;
696
697         digits = parse_num(line, p1);
698         if (!digits)
699                 return -1;
700
701         offset += digits;
702         line += digits;
703         len -= digits;
704
705         *p2 = 1;
706         if (*line == ',') {
707                 digits = parse_num(line+1, p2);
708                 if (!digits)
709                         return -1;
710
711                 offset += digits+1;
712                 line += digits+1;
713                 len -= digits+1;
714         }
715
716         ex = strlen(expect);
717         if (ex > len)
718                 return -1;
719         if (memcmp(line, expect, ex))
720                 return -1;
721
722         return offset + ex;
723 }
724
725 /*
726  * Parse a unified diff fragment header of the
727  * form "@@ -a,b +c,d @@"
728  */
729 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
730 {
731         int offset;
732
733         if (!len || line[len-1] != '\n')
734                 return -1;
735
736         /* Figure out the number of lines in a fragment */
737         offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
738         offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
739
740         return offset;
741 }
742
743 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
744 {
745         unsigned long offset, len;
746
747         patch->is_rename = patch->is_copy = 0;
748         patch->is_new = patch->is_delete = -1;
749         patch->old_mode = patch->new_mode = 0;
750         patch->old_name = patch->new_name = NULL;
751         for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
752                 unsigned long nextlen;
753
754                 len = linelen(line, size);
755                 if (!len)
756                         break;
757
758                 /* Testing this early allows us to take a few shortcuts.. */
759                 if (len < 6)
760                         continue;
761
762                 /*
763                  * Make sure we don't find any unconnected patch fragmants.
764                  * That's a sign that we didn't find a header, and that a
765                  * patch has become corrupted/broken up.
766                  */
767                 if (!memcmp("@@ -", line, 4)) {
768                         struct fragment dummy;
769                         if (parse_fragment_header(line, len, &dummy) < 0)
770                                 continue;
771                         error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
772                 }
773
774                 if (size < len + 6)
775                         break;
776
777                 /*
778                  * Git patch? It might not have a real patch, just a rename
779                  * or mode change, so we handle that specially
780                  */
781                 if (!memcmp("diff --git ", line, 11)) {
782                         int git_hdr_len = parse_git_header(line, len, size, patch);
783                         if (git_hdr_len <= len)
784                                 continue;
785                         if (!patch->old_name && !patch->new_name) {
786                                 if (!patch->def_name)
787                                         die("git diff header lacks filename information (line %d)", linenr);
788                                 patch->old_name = patch->new_name = patch->def_name;
789                         }
790                         *hdrsize = git_hdr_len;
791                         return offset;
792                 }
793
794                 /** --- followed by +++ ? */
795                 if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
796                         continue;
797
798                 /*
799                  * We only accept unified patches, so we want it to
800                  * at least have "@@ -a,b +c,d @@\n", which is 14 chars
801                  * minimum
802                  */
803                 nextlen = linelen(line + len, size - len);
804                 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
805                         continue;
806
807                 /* Ok, we'll consider it a patch */
808                 parse_traditional_patch(line, line+len, patch);
809                 *hdrsize = len + nextlen;
810                 linenr += 2;
811                 return offset;
812         }
813         return -1;
814 }
815
816 /*
817  * Parse a unified diff. Note that this really needs
818  * to parse each fragment separately, since the only
819  * way to know the difference between a "---" that is
820  * part of a patch, and a "---" that starts the next
821  * patch is to look at the line counts..
822  */
823 static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
824 {
825         int added, deleted;
826         int len = linelen(line, size), offset;
827         unsigned long oldlines, newlines;
828         unsigned long leading, trailing;
829
830         offset = parse_fragment_header(line, len, fragment);
831         if (offset < 0)
832                 return -1;
833         oldlines = fragment->oldlines;
834         newlines = fragment->newlines;
835         leading = 0;
836         trailing = 0;
837
838         if (patch->is_new < 0) {
839                 patch->is_new =  !oldlines;
840                 if (!oldlines)
841                         patch->old_name = NULL;
842         }
843         if (patch->is_delete < 0) {
844                 patch->is_delete = !newlines;
845                 if (!newlines)
846                         patch->new_name = NULL;
847         }
848
849         if (patch->is_new && oldlines)
850                 return error("new file depends on old contents");
851         if (patch->is_delete != !newlines) {
852                 if (newlines)
853                         return error("deleted file still has contents");
854                 fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
855         }
856
857         /* Parse the thing.. */
858         line += len;
859         size -= len;
860         linenr++;
861         added = deleted = 0;
862         for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
863                 if (!oldlines && !newlines)
864                         break;
865                 len = linelen(line, size);
866                 if (!len || line[len-1] != '\n')
867                         return -1;
868                 switch (*line) {
869                 default:
870                         return -1;
871                 case ' ':
872                         oldlines--;
873                         newlines--;
874                         if (!deleted && !added)
875                                 leading++;
876                         trailing++;
877                         break;
878                 case '-':
879                         deleted++;
880                         oldlines--;
881                         trailing = 0;
882                         break;
883                 case '+':
884                         /*
885                          * We know len is at least two, since we have a '+' and
886                          * we checked that the last character was a '\n' above.
887                          * That is, an addition of an empty line would check
888                          * the '+' here.  Sneaky...
889                          */
890                         if ((new_whitespace != nowarn_whitespace) &&
891                             isspace(line[len-2])) {
892                                 whitespace_error++;
893                                 if (squelch_whitespace_errors &&
894                                     squelch_whitespace_errors <
895                                     whitespace_error)
896                                         ;
897                                 else {
898                                         fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n",
899                                                 patch_input_file,
900                                                 linenr, len-2, line+1);
901                                 }
902                         }
903                         added++;
904                         newlines--;
905                         trailing = 0;
906                         break;
907
908                 /* We allow "\ No newline at end of file". Depending
909                  * on locale settings when the patch was produced we
910                  * don't know what this line looks like. The only
911                  * thing we do know is that it begins with "\ ".
912                  * Checking for 12 is just for sanity check -- any
913                  * l10n of "\ No newline..." is at least that long.
914                  */
915                 case '\\':
916                         if (len < 12 || memcmp(line, "\\ ", 2))
917                                 return -1;
918                         break;
919                 }
920         }
921         if (oldlines || newlines)
922                 return -1;
923         fragment->leading = leading;
924         fragment->trailing = trailing;
925
926         /* If a fragment ends with an incomplete line, we failed to include
927          * it in the above loop because we hit oldlines == newlines == 0
928          * before seeing it.
929          */
930         if (12 < size && !memcmp(line, "\\ ", 2))
931                 offset += linelen(line, size);
932
933         patch->lines_added += added;
934         patch->lines_deleted += deleted;
935         return offset;
936 }
937
938 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
939 {
940         unsigned long offset = 0;
941         struct fragment **fragp = &patch->fragments;
942
943         while (size > 4 && !memcmp(line, "@@ -", 4)) {
944                 struct fragment *fragment;
945                 int len;
946
947                 fragment = xcalloc(1, sizeof(*fragment));
948                 len = parse_fragment(line, size, patch, fragment);
949                 if (len <= 0)
950                         die("corrupt patch at line %d", linenr);
951
952                 fragment->patch = line;
953                 fragment->size = len;
954
955                 *fragp = fragment;
956                 fragp = &fragment->next;
957
958                 offset += len;
959                 line += len;
960                 size -= len;
961         }
962         return offset;
963 }
964
965 static inline int metadata_changes(struct patch *patch)
966 {
967         return  patch->is_rename > 0 ||
968                 patch->is_copy > 0 ||
969                 patch->is_new > 0 ||
970                 patch->is_delete ||
971                 (patch->old_mode && patch->new_mode &&
972                  patch->old_mode != patch->new_mode);
973 }
974
975 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
976 {
977         /* We have read "GIT binary patch\n"; what follows is a line
978          * that says the patch method (currently, either "deflated
979          * literal" or "deflated delta") and the length of data before
980          * deflating; a sequence of 'length-byte' followed by base-85
981          * encoded data follows.
982          *
983          * Each 5-byte sequence of base-85 encodes up to 4 bytes,
984          * and we would limit the patch line to 66 characters,
985          * so one line can fit up to 13 groups that would decode
986          * to 52 bytes max.  The length byte 'A'-'Z' corresponds
987          * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
988          * The end of binary is signalled with an empty line.
989          */
990         int llen, used;
991         struct fragment *fragment;
992         char *data = NULL;
993
994         patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
995
996         /* Grab the type of patch */
997         llen = linelen(buffer, size);
998         used = llen;
999         linenr++;
1000
1001         if (!strncmp(buffer, "delta ", 6)) {
1002                 patch->is_binary = BINARY_DELTA_DEFLATED;
1003                 patch->deflate_origlen = strtoul(buffer + 6, NULL, 10);
1004         }
1005         else if (!strncmp(buffer, "literal ", 8)) {
1006                 patch->is_binary = BINARY_LITERAL_DEFLATED;
1007                 patch->deflate_origlen = strtoul(buffer + 8, NULL, 10);
1008         }
1009         else
1010                 return error("unrecognized binary patch at line %d: %.*s",
1011                              linenr-1, llen-1, buffer);
1012         buffer += llen;
1013         while (1) {
1014                 int byte_length, max_byte_length, newsize;
1015                 llen = linelen(buffer, size);
1016                 used += llen;
1017                 linenr++;
1018                 if (llen == 1)
1019                         break;
1020                 /* Minimum line is "A00000\n" which is 7-byte long,
1021                  * and the line length must be multiple of 5 plus 2.
1022                  */
1023                 if ((llen < 7) || (llen-2) % 5)
1024                         goto corrupt;
1025                 max_byte_length = (llen - 2) / 5 * 4;
1026                 byte_length = *buffer;
1027                 if ('A' <= byte_length && byte_length <= 'Z')
1028                         byte_length = byte_length - 'A' + 1;
1029                 else if ('a' <= byte_length && byte_length <= 'z')
1030                         byte_length = byte_length - 'a' + 27;
1031                 else
1032                         goto corrupt;
1033                 /* if the input length was not multiple of 4, we would
1034                  * have filler at the end but the filler should never
1035                  * exceed 3 bytes
1036                  */
1037                 if (max_byte_length < byte_length ||
1038                     byte_length <= max_byte_length - 4)
1039                         goto corrupt;
1040                 newsize = fragment->size + byte_length;
1041                 data = xrealloc(data, newsize);
1042                 if (decode_85(data + fragment->size,
1043                               buffer + 1,
1044                               byte_length))
1045                         goto corrupt;
1046                 fragment->size = newsize;
1047                 buffer += llen;
1048                 size -= llen;
1049         }
1050         fragment->patch = data;
1051         return used;
1052  corrupt:
1053         return error("corrupt binary patch at line %d: %.*s",
1054                      linenr-1, llen-1, buffer);
1055 }
1056
1057 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1058 {
1059         int hdrsize, patchsize;
1060         int offset = find_header(buffer, size, &hdrsize, patch);
1061
1062         if (offset < 0)
1063                 return offset;
1064
1065         patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
1066
1067         if (!patchsize) {
1068                 static const char *binhdr[] = {
1069                         "Binary files ",
1070                         "Files ",
1071                         NULL,
1072                 };
1073                 static const char git_binary[] = "GIT binary patch\n";
1074                 int i;
1075                 int hd = hdrsize + offset;
1076                 unsigned long llen = linelen(buffer + hd, size - hd);
1077
1078                 if (llen == sizeof(git_binary) - 1 &&
1079                     !memcmp(git_binary, buffer + hd, llen)) {
1080                         int used;
1081                         linenr++;
1082                         used = parse_binary(buffer + hd + llen,
1083                                             size - hd - llen, patch);
1084                         if (used)
1085                                 patchsize = used + llen;
1086                         else
1087                                 patchsize = 0;
1088                 }
1089                 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1090                         for (i = 0; binhdr[i]; i++) {
1091                                 int len = strlen(binhdr[i]);
1092                                 if (len < size - hd &&
1093                                     !memcmp(binhdr[i], buffer + hd, len)) {
1094                                         linenr++;
1095                                         patch->is_binary = 1;
1096                                         patchsize = llen;
1097                                         break;
1098                                 }
1099                         }
1100                 }
1101
1102                 /* Empty patch cannot be applied if:
1103                  * - it is a binary patch and we do not do binary_replace, or
1104                  * - text patch without metadata change
1105                  */
1106                 if ((apply || check) &&
1107                     (patch->is_binary
1108                      ? !allow_binary_replacement
1109                      : !metadata_changes(patch)))
1110                         die("patch with only garbage at line %d", linenr);
1111         }
1112
1113         return offset + hdrsize + patchsize;
1114 }
1115
1116 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1117 static const char minuses[]= "----------------------------------------------------------------------";
1118
1119 static void show_stats(struct patch *patch)
1120 {
1121         const char *prefix = "";
1122         char *name = patch->new_name;
1123         char *qname = NULL;
1124         int len, max, add, del, total;
1125
1126         if (!name)
1127                 name = patch->old_name;
1128
1129         if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
1130                 qname = xmalloc(len + 1);
1131                 quote_c_style(name, qname, NULL, 0);
1132                 name = qname;
1133         }
1134
1135         /*
1136          * "scale" the filename
1137          */
1138         len = strlen(name);
1139         max = max_len;
1140         if (max > 50)
1141                 max = 50;
1142         if (len > max) {
1143                 char *slash;
1144                 prefix = "...";
1145                 max -= 3;
1146                 name += len - max;
1147                 slash = strchr(name, '/');
1148                 if (slash)
1149                         name = slash;
1150         }
1151         len = max;
1152
1153         /*
1154          * scale the add/delete
1155          */
1156         max = max_change;
1157         if (max + len > 70)
1158                 max = 70 - len;
1159
1160         add = patch->lines_added;
1161         del = patch->lines_deleted;
1162         total = add + del;
1163
1164         if (max_change > 0) {
1165                 total = (total * max + max_change / 2) / max_change;
1166                 add = (add * max + max_change / 2) / max_change;
1167                 del = total - add;
1168         }
1169         if (patch->is_binary)
1170                 printf(" %s%-*s |  Bin\n", prefix, len, name);
1171         else
1172                 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
1173                        len, name, patch->lines_added + patch->lines_deleted,
1174                        add, pluses, del, minuses);
1175         if (qname)
1176                 free(qname);
1177 }
1178
1179 static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
1180 {
1181         int fd;
1182         unsigned long got;
1183
1184         switch (st->st_mode & S_IFMT) {
1185         case S_IFLNK:
1186                 return readlink(path, buf, size);
1187         case S_IFREG:
1188                 fd = open(path, O_RDONLY);
1189                 if (fd < 0)
1190                         return error("unable to open %s", path);
1191                 got = 0;
1192                 for (;;) {
1193                         int ret = xread(fd, buf + got, size - got);
1194                         if (ret <= 0)
1195                                 break;
1196                         got += ret;
1197                 }
1198                 close(fd);
1199                 return got;
1200
1201         default:
1202                 return -1;
1203         }
1204 }
1205
1206 static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
1207 {
1208         int i;
1209         unsigned long start, backwards, forwards;
1210
1211         if (fragsize > size)
1212                 return -1;
1213
1214         start = 0;
1215         if (line > 1) {
1216                 unsigned long offset = 0;
1217                 i = line-1;
1218                 while (offset + fragsize <= size) {
1219                         if (buf[offset++] == '\n') {
1220                                 start = offset;
1221                                 if (!--i)
1222                                         break;
1223                         }
1224                 }
1225         }
1226
1227         /* Exact line number? */
1228         if (!memcmp(buf + start, fragment, fragsize))
1229                 return start;
1230
1231         /*
1232          * There's probably some smart way to do this, but I'll leave
1233          * that to the smart and beautiful people. I'm simple and stupid.
1234          */
1235         backwards = start;
1236         forwards = start;
1237         for (i = 0; ; i++) {
1238                 unsigned long try;
1239                 int n;
1240
1241                 /* "backward" */
1242                 if (i & 1) {
1243                         if (!backwards) {
1244                                 if (forwards + fragsize > size)
1245                                         break;
1246                                 continue;
1247                         }
1248                         do {
1249                                 --backwards;
1250                         } while (backwards && buf[backwards-1] != '\n');
1251                         try = backwards;
1252                 } else {
1253                         while (forwards + fragsize <= size) {
1254                                 if (buf[forwards++] == '\n')
1255                                         break;
1256                         }
1257                         try = forwards;
1258                 }
1259
1260                 if (try + fragsize > size)
1261                         continue;
1262                 if (memcmp(buf + try, fragment, fragsize))
1263                         continue;
1264                 n = (i >> 1)+1;
1265                 if (i & 1)
1266                         n = -n;
1267                 *lines = n;
1268                 return try;
1269         }
1270
1271         /*
1272          * We should start searching forward and backward.
1273          */
1274         return -1;
1275 }
1276
1277 static void remove_first_line(const char **rbuf, int *rsize)
1278 {
1279         const char *buf = *rbuf;
1280         int size = *rsize;
1281         unsigned long offset;
1282         offset = 0;
1283         while (offset <= size) {
1284                 if (buf[offset++] == '\n')
1285                         break;
1286         }
1287         *rsize = size - offset;
1288         *rbuf = buf + offset;
1289 }
1290
1291 static void remove_last_line(const char **rbuf, int *rsize)
1292 {
1293         const char *buf = *rbuf;
1294         int size = *rsize;
1295         unsigned long offset;
1296         offset = size - 1;
1297         while (offset > 0) {
1298                 if (buf[--offset] == '\n')
1299                         break;
1300         }
1301         *rsize = offset + 1;
1302 }
1303
1304 struct buffer_desc {
1305         char *buffer;
1306         unsigned long size;
1307         unsigned long alloc;
1308 };
1309
1310 static int apply_line(char *output, const char *patch, int plen)
1311 {
1312         /* plen is number of bytes to be copied from patch,
1313          * starting at patch+1 (patch[0] is '+').  Typically
1314          * patch[plen] is '\n'.
1315          */
1316         int add_nl_to_tail = 0;
1317         if ((new_whitespace == strip_whitespace) &&
1318             1 < plen && isspace(patch[plen-1])) {
1319                 if (patch[plen] == '\n')
1320                         add_nl_to_tail = 1;
1321                 plen--;
1322                 while (0 < plen && isspace(patch[plen]))
1323                         plen--;
1324                 applied_after_stripping++;
1325         }
1326         memcpy(output, patch + 1, plen);
1327         if (add_nl_to_tail)
1328                 output[plen++] = '\n';
1329         return plen;
1330 }
1331
1332 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1333 {
1334         char *buf = desc->buffer;
1335         const char *patch = frag->patch;
1336         int offset, size = frag->size;
1337         char *old = xmalloc(size);
1338         char *new = xmalloc(size);
1339         const char *oldlines, *newlines;
1340         int oldsize = 0, newsize = 0;
1341         unsigned long leading, trailing;
1342         int pos, lines;
1343
1344         while (size > 0) {
1345                 int len = linelen(patch, size);
1346                 int plen;
1347
1348                 if (!len)
1349                         break;
1350
1351                 /*
1352                  * "plen" is how much of the line we should use for
1353                  * the actual patch data. Normally we just remove the
1354                  * first character on the line, but if the line is
1355                  * followed by "\ No newline", then we also remove the
1356                  * last one (which is the newline, of course).
1357                  */
1358                 plen = len-1;
1359                 if (len < size && patch[len] == '\\')
1360                         plen--;
1361                 switch (*patch) {
1362                 case ' ':
1363                 case '-':
1364                         memcpy(old + oldsize, patch + 1, plen);
1365                         oldsize += plen;
1366                         if (*patch == '-')
1367                                 break;
1368                 /* Fall-through for ' ' */
1369                 case '+':
1370                         if (*patch != '+' || !no_add)
1371                                 newsize += apply_line(new + newsize, patch,
1372                                                       plen);
1373                         break;
1374                 case '@': case '\\':
1375                         /* Ignore it, we already handled it */
1376                         break;
1377                 default:
1378                         return -1;
1379                 }
1380                 patch += len;
1381                 size -= len;
1382         }
1383
1384 #ifdef NO_ACCURATE_DIFF
1385         if (oldsize > 0 && old[oldsize - 1] == '\n' &&
1386                         newsize > 0 && new[newsize - 1] == '\n') {
1387                 oldsize--;
1388                 newsize--;
1389         }
1390 #endif
1391
1392         oldlines = old;
1393         newlines = new;
1394         leading = frag->leading;
1395         trailing = frag->trailing;
1396         lines = 0;
1397         pos = frag->newpos;
1398         for (;;) {
1399                 offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
1400                 if (offset >= 0) {
1401                         int diff = newsize - oldsize;
1402                         unsigned long size = desc->size + diff;
1403                         unsigned long alloc = desc->alloc;
1404
1405                         /* Warn if it was necessary to reduce the number
1406                          * of context lines.
1407                          */
1408                         if ((leading != frag->leading) || (trailing != frag->trailing))
1409                                 fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
1410                                         leading, trailing, pos + lines);
1411
1412                         if (size > alloc) {
1413                                 alloc = size + 8192;
1414                                 desc->alloc = alloc;
1415                                 buf = xrealloc(buf, alloc);
1416                                 desc->buffer = buf;
1417                         }
1418                         desc->size = size;
1419                         memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1420                         memcpy(buf + offset, newlines, newsize);
1421                         offset = 0;
1422
1423                         break;
1424                 }
1425
1426                 /* Am I at my context limits? */
1427                 if ((leading <= p_context) && (trailing <= p_context))
1428                         break;
1429                 /* Reduce the number of context lines
1430                  * Reduce both leading and trailing if they are equal
1431                  * otherwise just reduce the larger context.
1432                  */
1433                 if (leading >= trailing) {
1434                         remove_first_line(&oldlines, &oldsize);
1435                         remove_first_line(&newlines, &newsize);
1436                         pos--;
1437                         leading--;
1438                 }
1439                 if (trailing > leading) {
1440                         remove_last_line(&oldlines, &oldsize);
1441                         remove_last_line(&newlines, &newsize);
1442                         trailing--;
1443                 }
1444         }
1445
1446         free(old);
1447         free(new);
1448         return offset;
1449 }
1450
1451 static char *inflate_it(const void *data, unsigned long size,
1452                         unsigned long inflated_size)
1453 {
1454         z_stream stream;
1455         void *out;
1456         int st;
1457
1458         memset(&stream, 0, sizeof(stream));
1459
1460         stream.next_in = (unsigned char *)data;
1461         stream.avail_in = size;
1462         stream.next_out = out = xmalloc(inflated_size);
1463         stream.avail_out = inflated_size;
1464         inflateInit(&stream);
1465         st = inflate(&stream, Z_FINISH);
1466         if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1467                 free(out);
1468                 return NULL;
1469         }
1470         return out;
1471 }
1472
1473 static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
1474 {
1475         unsigned long dst_size;
1476         struct fragment *fragment = patch->fragments;
1477         void *data;
1478         void *result;
1479
1480         data = inflate_it(fragment->patch, fragment->size,
1481                           patch->deflate_origlen);
1482         if (!data)
1483                 return error("corrupt patch data");
1484         switch (patch->is_binary) {
1485         case BINARY_DELTA_DEFLATED:
1486                 result = patch_delta(desc->buffer, desc->size,
1487                                      data,
1488                                      patch->deflate_origlen,
1489                                      &dst_size);
1490                 free(desc->buffer);
1491                 desc->buffer = result;
1492                 free(data);
1493                 break;
1494         case BINARY_LITERAL_DEFLATED:
1495                 free(desc->buffer);
1496                 desc->buffer = data;
1497                 dst_size = patch->deflate_origlen;
1498                 break;
1499         }
1500         if (!desc->buffer)
1501                 return -1;
1502         desc->size = desc->alloc = dst_size;
1503         return 0;
1504 }
1505
1506 static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1507 {
1508         const char *name = patch->old_name ? patch->old_name : patch->new_name;
1509         unsigned char sha1[20];
1510         unsigned char hdr[50];
1511         int hdrlen;
1512
1513         if (!allow_binary_replacement)
1514                 return error("cannot apply binary patch to '%s' "
1515                              "without --allow-binary-replacement",
1516                              name);
1517
1518         /* For safety, we require patch index line to contain
1519          * full 40-byte textual SHA1 for old and new, at least for now.
1520          */
1521         if (strlen(patch->old_sha1_prefix) != 40 ||
1522             strlen(patch->new_sha1_prefix) != 40 ||
1523             get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1524             get_sha1_hex(patch->new_sha1_prefix, sha1))
1525                 return error("cannot apply binary patch to '%s' "
1526                              "without full index line", name);
1527
1528         if (patch->old_name) {
1529                 /* See if the old one matches what the patch
1530                  * applies to.
1531                  */
1532                 write_sha1_file_prepare(desc->buffer, desc->size,
1533                                         blob_type, sha1, hdr, &hdrlen);
1534                 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1535                         return error("the patch applies to '%s' (%s), "
1536                                      "which does not match the "
1537                                      "current contents.",
1538                                      name, sha1_to_hex(sha1));
1539         }
1540         else {
1541                 /* Otherwise, the old one must be empty. */
1542                 if (desc->size)
1543                         return error("the patch applies to an empty "
1544                                      "'%s' but it is not empty", name);
1545         }
1546
1547         get_sha1_hex(patch->new_sha1_prefix, sha1);
1548         if (!memcmp(sha1, null_sha1, 20)) {
1549                 free(desc->buffer);
1550                 desc->alloc = desc->size = 0;
1551                 desc->buffer = NULL;
1552                 return 0; /* deletion patch */
1553         }
1554
1555         if (has_sha1_file(sha1)) {
1556                 /* We already have the postimage */
1557                 char type[10];
1558                 unsigned long size;
1559
1560                 free(desc->buffer);
1561                 desc->buffer = read_sha1_file(sha1, type, &size);
1562                 if (!desc->buffer)
1563                         return error("the necessary postimage %s for "
1564                                      "'%s' cannot be read",
1565                                      patch->new_sha1_prefix, name);
1566                 desc->alloc = desc->size = size;
1567         }
1568         else {
1569                 /* We have verified desc matches the preimage;
1570                  * apply the patch data to it, which is stored
1571                  * in the patch->fragments->{patch,size}.
1572                  */
1573                 if (apply_binary_fragment(desc, patch))
1574                         return error("binary patch does not apply to '%s'",
1575                                      name);
1576
1577                 /* verify that the result matches */
1578                 write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
1579                                         sha1, hdr, &hdrlen);
1580                 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1581                         return error("binary patch to '%s' creates incorrect result", name);
1582         }
1583
1584         return 0;
1585 }
1586
1587 static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1588 {
1589         struct fragment *frag = patch->fragments;
1590         const char *name = patch->old_name ? patch->old_name : patch->new_name;
1591
1592         if (patch->is_binary)
1593                 return apply_binary(desc, patch);
1594
1595         while (frag) {
1596                 if (apply_one_fragment(desc, frag) < 0)
1597                         return error("patch failed: %s:%ld",
1598                                      name, frag->oldpos);
1599                 frag = frag->next;
1600         }
1601         return 0;
1602 }
1603
1604 static int apply_data(struct patch *patch, struct stat *st)
1605 {
1606         char *buf;
1607         unsigned long size, alloc;
1608         struct buffer_desc desc;
1609
1610         size = 0;
1611         alloc = 0;
1612         buf = NULL;
1613         if (patch->old_name) {
1614                 size = st->st_size;
1615                 alloc = size + 8192;
1616                 buf = xmalloc(alloc);
1617                 if (read_old_data(st, patch->old_name, buf, alloc) != size)
1618                         return error("read of %s failed", patch->old_name);
1619         }
1620
1621         desc.size = size;
1622         desc.alloc = alloc;
1623         desc.buffer = buf;
1624         if (apply_fragments(&desc, patch) < 0)
1625                 return -1;
1626         patch->result = desc.buffer;
1627         patch->resultsize = desc.size;
1628
1629         if (patch->is_delete && patch->resultsize)
1630                 return error("removal patch leaves file contents");
1631
1632         return 0;
1633 }
1634
1635 static int check_patch(struct patch *patch)
1636 {
1637         struct stat st;
1638         const char *old_name = patch->old_name;
1639         const char *new_name = patch->new_name;
1640         const char *name = old_name ? old_name : new_name;
1641
1642         if (old_name) {
1643                 int changed;
1644                 int stat_ret = lstat(old_name, &st);
1645
1646                 if (check_index) {
1647                         int pos = cache_name_pos(old_name, strlen(old_name));
1648                         if (pos < 0)
1649                                 return error("%s: does not exist in index",
1650                                              old_name);
1651                         if (stat_ret < 0) {
1652                                 struct checkout costate;
1653                                 if (errno != ENOENT)
1654                                         return error("%s: %s", old_name,
1655                                                      strerror(errno));
1656                                 /* checkout */
1657                                 costate.base_dir = "";
1658                                 costate.base_dir_len = 0;
1659                                 costate.force = 0;
1660                                 costate.quiet = 0;
1661                                 costate.not_new = 0;
1662                                 costate.refresh_cache = 1;
1663                                 if (checkout_entry(active_cache[pos],
1664                                                    &costate,
1665                                                    NULL) ||
1666                                     lstat(old_name, &st))
1667                                         return -1;
1668                         }
1669
1670                         changed = ce_match_stat(active_cache[pos], &st, 1);
1671                         if (changed)
1672                                 return error("%s: does not match index",
1673                                              old_name);
1674                 }
1675                 else if (stat_ret < 0)
1676                         return error("%s: %s", old_name, strerror(errno));
1677
1678                 if (patch->is_new < 0)
1679                         patch->is_new = 0;
1680                 st.st_mode = ntohl(create_ce_mode(st.st_mode));
1681                 if (!patch->old_mode)
1682                         patch->old_mode = st.st_mode;
1683                 if ((st.st_mode ^ patch->old_mode) & S_IFMT)
1684                         return error("%s: wrong type", old_name);
1685                 if (st.st_mode != patch->old_mode)
1686                         fprintf(stderr, "warning: %s has type %o, expected %o\n",
1687                                 old_name, st.st_mode, patch->old_mode);
1688         }
1689
1690         if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1691                 if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1692                         return error("%s: already exists in index", new_name);
1693                 if (!lstat(new_name, &st))
1694                         return error("%s: already exists in working directory", new_name);
1695                 if (errno != ENOENT)
1696                         return error("%s: %s", new_name, strerror(errno));
1697                 if (!patch->new_mode) {
1698                         if (patch->is_new)
1699                                 patch->new_mode = S_IFREG | 0644;
1700                         else
1701                                 patch->new_mode = patch->old_mode;
1702                 }
1703         }
1704
1705         if (new_name && old_name) {
1706                 int same = !strcmp(old_name, new_name);
1707                 if (!patch->new_mode)
1708                         patch->new_mode = patch->old_mode;
1709                 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
1710                         return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1711                                 patch->new_mode, new_name, patch->old_mode,
1712                                 same ? "" : " of ", same ? "" : old_name);
1713         }       
1714
1715         if (apply_data(patch, &st) < 0)
1716                 return error("%s: patch does not apply", name);
1717         return 0;
1718 }
1719
1720 static int check_patch_list(struct patch *patch)
1721 {
1722         int error = 0;
1723
1724         for (;patch ; patch = patch->next)
1725                 error |= check_patch(patch);
1726         return error;
1727 }
1728
1729 static inline int is_null_sha1(const unsigned char *sha1)
1730 {
1731         return !memcmp(sha1, null_sha1, 20);
1732 }
1733
1734 static void show_index_list(struct patch *list)
1735 {
1736         struct patch *patch;
1737
1738         /* Once we start supporting the reverse patch, it may be
1739          * worth showing the new sha1 prefix, but until then...
1740          */
1741         for (patch = list; patch; patch = patch->next) {
1742                 const unsigned char *sha1_ptr;
1743                 unsigned char sha1[20];
1744                 const char *name;
1745
1746                 name = patch->old_name ? patch->old_name : patch->new_name;
1747                 if (patch->is_new)
1748                         sha1_ptr = null_sha1;
1749                 else if (get_sha1(patch->old_sha1_prefix, sha1))
1750                         die("sha1 information is lacking or useless (%s).",
1751                             name);
1752                 else
1753                         sha1_ptr = sha1;
1754
1755                 printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
1756                 if (line_termination && quote_c_style(name, NULL, NULL, 0))
1757                         quote_c_style(name, NULL, stdout, 0);
1758                 else
1759                         fputs(name, stdout);
1760                 putchar(line_termination);
1761         }
1762 }
1763
1764 static void stat_patch_list(struct patch *patch)
1765 {
1766         int files, adds, dels;
1767
1768         for (files = adds = dels = 0 ; patch ; patch = patch->next) {
1769                 files++;
1770                 adds += patch->lines_added;
1771                 dels += patch->lines_deleted;
1772                 show_stats(patch);
1773         }
1774
1775         printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
1776 }
1777
1778 static void numstat_patch_list(struct patch *patch)
1779 {
1780         for ( ; patch; patch = patch->next) {
1781                 const char *name;
1782                 name = patch->old_name ? patch->old_name : patch->new_name;
1783                 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
1784                 if (line_termination && quote_c_style(name, NULL, NULL, 0))
1785                         quote_c_style(name, NULL, stdout, 0);
1786                 else
1787                         fputs(name, stdout);
1788                 putchar('\n');
1789         }
1790 }
1791
1792 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
1793 {
1794         if (mode)
1795                 printf(" %s mode %06o %s\n", newdelete, mode, name);
1796         else
1797                 printf(" %s %s\n", newdelete, name);
1798 }
1799
1800 static void show_mode_change(struct patch *p, int show_name)
1801 {
1802         if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
1803                 if (show_name)
1804                         printf(" mode change %06o => %06o %s\n",
1805                                p->old_mode, p->new_mode, p->new_name);
1806                 else
1807                         printf(" mode change %06o => %06o\n",
1808                                p->old_mode, p->new_mode);
1809         }
1810 }
1811
1812 static void show_rename_copy(struct patch *p)
1813 {
1814         const char *renamecopy = p->is_rename ? "rename" : "copy";
1815         const char *old, *new;
1816
1817         /* Find common prefix */
1818         old = p->old_name;
1819         new = p->new_name;
1820         while (1) {
1821                 const char *slash_old, *slash_new;
1822                 slash_old = strchr(old, '/');
1823                 slash_new = strchr(new, '/');
1824                 if (!slash_old ||
1825                     !slash_new ||
1826                     slash_old - old != slash_new - new ||
1827                     memcmp(old, new, slash_new - new))
1828                         break;
1829                 old = slash_old + 1;
1830                 new = slash_new + 1;
1831         }
1832         /* p->old_name thru old is the common prefix, and old and new
1833          * through the end of names are renames
1834          */
1835         if (old != p->old_name)
1836                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1837                        (int)(old - p->old_name), p->old_name,
1838                        old, new, p->score);
1839         else
1840                 printf(" %s %s => %s (%d%%)\n", renamecopy,
1841                        p->old_name, p->new_name, p->score);
1842         show_mode_change(p, 0);
1843 }
1844
1845 static void summary_patch_list(struct patch *patch)
1846 {
1847         struct patch *p;
1848
1849         for (p = patch; p; p = p->next) {
1850                 if (p->is_new)
1851                         show_file_mode_name("create", p->new_mode, p->new_name);
1852                 else if (p->is_delete)
1853                         show_file_mode_name("delete", p->old_mode, p->old_name);
1854                 else {
1855                         if (p->is_rename || p->is_copy)
1856                                 show_rename_copy(p);
1857                         else {
1858                                 if (p->score) {
1859                                         printf(" rewrite %s (%d%%)\n",
1860                                                p->new_name, p->score);
1861                                         show_mode_change(p, 0);
1862                                 }
1863                                 else
1864                                         show_mode_change(p, 1);
1865                         }
1866                 }
1867         }
1868 }
1869
1870 static void patch_stats(struct patch *patch)
1871 {
1872         int lines = patch->lines_added + patch->lines_deleted;
1873
1874         if (lines > max_change)
1875                 max_change = lines;
1876         if (patch->old_name) {
1877                 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
1878                 if (!len)
1879                         len = strlen(patch->old_name);
1880                 if (len > max_len)
1881                         max_len = len;
1882         }
1883         if (patch->new_name) {
1884                 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
1885                 if (!len)
1886                         len = strlen(patch->new_name);
1887                 if (len > max_len)
1888                         max_len = len;
1889         }
1890 }
1891
1892 static void remove_file(struct patch *patch)
1893 {
1894         if (write_index) {
1895                 if (remove_file_from_cache(patch->old_name) < 0)
1896                         die("unable to remove %s from index", patch->old_name);
1897                 cache_tree_invalidate_path(active_cache_tree, patch->old_name);
1898         }
1899         unlink(patch->old_name);
1900 }
1901
1902 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
1903 {
1904         struct stat st;
1905         struct cache_entry *ce;
1906         int namelen = strlen(path);
1907         unsigned ce_size = cache_entry_size(namelen);
1908
1909         if (!write_index)
1910                 return;
1911
1912         ce = xcalloc(1, ce_size);
1913         memcpy(ce->name, path, namelen);
1914         ce->ce_mode = create_ce_mode(mode);
1915         ce->ce_flags = htons(namelen);
1916         if (lstat(path, &st) < 0)
1917                 die("unable to stat newly created file %s", path);
1918         fill_stat_cache_info(ce, &st);
1919         if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
1920                 die("unable to create backing store for newly created file %s", path);
1921         if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
1922                 die("unable to add cache entry for %s", path);
1923 }
1924
1925 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
1926 {
1927         int fd;
1928
1929         if (S_ISLNK(mode))
1930                 return symlink(buf, path);
1931         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
1932         if (fd < 0)
1933                 return -1;
1934         while (size) {
1935                 int written = xwrite(fd, buf, size);
1936                 if (written < 0)
1937                         die("writing file %s: %s", path, strerror(errno));
1938                 if (!written)
1939                         die("out of space writing file %s", path);
1940                 buf += written;
1941                 size -= written;
1942         }
1943         if (close(fd) < 0)
1944                 die("closing file %s: %s", path, strerror(errno));
1945         return 0;
1946 }
1947
1948 /*
1949  * We optimistically assume that the directories exist,
1950  * which is true 99% of the time anyway. If they don't,
1951  * we create them and try again.
1952  */
1953 static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
1954 {
1955         if (!try_create_file(path, mode, buf, size))
1956                 return;
1957
1958         if (errno == ENOENT) {
1959                 if (safe_create_leading_directories(path))
1960                         return;
1961                 if (!try_create_file(path, mode, buf, size))
1962                         return;
1963         }
1964
1965         if (errno == EEXIST) {
1966                 unsigned int nr = getpid();
1967
1968                 for (;;) {
1969                         const char *newpath;
1970                         newpath = mkpath("%s~%u", path, nr);
1971                         if (!try_create_file(newpath, mode, buf, size)) {
1972                                 if (!rename(newpath, path))
1973                                         return;
1974                                 unlink(newpath);
1975                                 break;
1976                         }
1977                         if (errno != EEXIST)
1978                                 break;
1979                         ++nr;
1980                 }
1981         }
1982         die("unable to write file %s mode %o", path, mode);
1983 }
1984
1985 static void create_file(struct patch *patch)
1986 {
1987         char *path = patch->new_name;
1988         unsigned mode = patch->new_mode;
1989         unsigned long size = patch->resultsize;
1990         char *buf = patch->result;
1991
1992         if (!mode)
1993                 mode = S_IFREG | 0644;
1994         create_one_file(path, mode, buf, size);
1995         add_index_file(path, mode, buf, size);
1996         cache_tree_invalidate_path(active_cache_tree, path);
1997 }
1998
1999 static void write_out_one_result(struct patch *patch)
2000 {
2001         if (patch->is_delete > 0) {
2002                 remove_file(patch);
2003                 return;
2004         }
2005         if (patch->is_new > 0 || patch->is_copy) {
2006                 create_file(patch);
2007                 return;
2008         }
2009         /*
2010          * Rename or modification boils down to the same
2011          * thing: remove the old, write the new
2012          */
2013         remove_file(patch);
2014         create_file(patch);
2015 }
2016
2017 static void write_out_results(struct patch *list, int skipped_patch)
2018 {
2019         if (!list && !skipped_patch)
2020                 die("No changes");
2021
2022         while (list) {
2023                 write_out_one_result(list);
2024                 list = list->next;
2025         }
2026 }
2027
2028 static struct cache_file cache_file;
2029
2030 static struct excludes {
2031         struct excludes *next;
2032         const char *path;
2033 } *excludes;
2034
2035 static int use_patch(struct patch *p)
2036 {
2037         const char *pathname = p->new_name ? p->new_name : p->old_name;
2038         struct excludes *x = excludes;
2039         while (x) {
2040                 if (fnmatch(x->path, pathname, 0) == 0)
2041                         return 0;
2042                 x = x->next;
2043         }
2044         if (0 < prefix_length) {
2045                 int pathlen = strlen(pathname);
2046                 if (pathlen <= prefix_length ||
2047                     memcmp(prefix, pathname, prefix_length))
2048                         return 0;
2049         }
2050         return 1;
2051 }
2052
2053 static int apply_patch(int fd, const char *filename)
2054 {
2055         unsigned long offset, size;
2056         char *buffer = read_patch_file(fd, &size);
2057         struct patch *list = NULL, **listp = &list;
2058         int skipped_patch = 0;
2059
2060         patch_input_file = filename;
2061         if (!buffer)
2062                 return -1;
2063         offset = 0;
2064         while (size > 0) {
2065                 struct patch *patch;
2066                 int nr;
2067
2068                 patch = xcalloc(1, sizeof(*patch));
2069                 nr = parse_chunk(buffer + offset, size, patch);
2070                 if (nr < 0)
2071                         break;
2072                 if (use_patch(patch)) {
2073                         patch_stats(patch);
2074                         *listp = patch;
2075                         listp = &patch->next;
2076                 } else {
2077                         /* perhaps free it a bit better? */
2078                         free(patch);
2079                         skipped_patch++;
2080                 }
2081                 offset += nr;
2082                 size -= nr;
2083         }
2084
2085         if (whitespace_error && (new_whitespace == error_on_whitespace))
2086                 apply = 0;
2087
2088         write_index = check_index && apply;
2089         if (write_index && newfd < 0)
2090                 newfd = hold_index_file_for_update(&cache_file, get_index_file());
2091         if (check_index) {
2092                 if (read_cache() < 0)
2093                         die("unable to read index file");
2094         }
2095
2096         if ((check || apply) && check_patch_list(list) < 0)
2097                 exit(1);
2098
2099         if (apply)
2100                 write_out_results(list, skipped_patch);
2101
2102         if (show_index_info)
2103                 show_index_list(list);
2104
2105         if (diffstat)
2106                 stat_patch_list(list);
2107
2108         if (numstat)
2109                 numstat_patch_list(list);
2110
2111         if (summary)
2112                 summary_patch_list(list);
2113
2114         free(buffer);
2115         return 0;
2116 }
2117
2118 static int git_apply_config(const char *var, const char *value)
2119 {
2120         if (!strcmp(var, "apply.whitespace")) {
2121                 apply_default_whitespace = strdup(value);
2122                 return 0;
2123         }
2124         return git_default_config(var, value);
2125 }
2126
2127
2128 int main(int argc, char **argv)
2129 {
2130         int i;
2131         int read_stdin = 1;
2132         const char *whitespace_option = NULL;
2133
2134         for (i = 1; i < argc; i++) {
2135                 const char *arg = argv[i];
2136                 char *end;
2137                 int fd;
2138
2139                 if (!strcmp(arg, "-")) {
2140                         apply_patch(0, "<stdin>");
2141                         read_stdin = 0;
2142                         continue;
2143                 }
2144                 if (!strncmp(arg, "--exclude=", 10)) {
2145                         struct excludes *x = xmalloc(sizeof(*x));
2146                         x->path = arg + 10;
2147                         x->next = excludes;
2148                         excludes = x;
2149                         continue;
2150                 }
2151                 if (!strncmp(arg, "-p", 2)) {
2152                         p_value = atoi(arg + 2);
2153                         continue;
2154                 }
2155                 if (!strcmp(arg, "--no-add")) {
2156                         no_add = 1;
2157                         continue;
2158                 }
2159                 if (!strcmp(arg, "--stat")) {
2160                         apply = 0;
2161                         diffstat = 1;
2162                         continue;
2163                 }
2164                 if (!strcmp(arg, "--allow-binary-replacement") ||
2165                     !strcmp(arg, "--binary")) {
2166                         allow_binary_replacement = 1;
2167                         continue;
2168                 }
2169                 if (!strcmp(arg, "--numstat")) {
2170                         apply = 0;
2171                         numstat = 1;
2172                         continue;
2173                 }
2174                 if (!strcmp(arg, "--summary")) {
2175                         apply = 0;
2176                         summary = 1;
2177                         continue;
2178                 }
2179                 if (!strcmp(arg, "--check")) {
2180                         apply = 0;
2181                         check = 1;
2182                         continue;
2183                 }
2184                 if (!strcmp(arg, "--index")) {
2185                         check_index = 1;
2186                         continue;
2187                 }
2188                 if (!strcmp(arg, "--apply")) {
2189                         apply = 1;
2190                         continue;
2191                 }
2192                 if (!strcmp(arg, "--index-info")) {
2193                         apply = 0;
2194                         show_index_info = 1;
2195                         continue;
2196                 }
2197                 if (!strcmp(arg, "-z")) {
2198                         line_termination = 0;
2199                         continue;
2200                 }
2201                 if (!strncmp(arg, "-C", 2)) {
2202                         p_context = strtoul(arg + 2, &end, 0);
2203                         if (*end != '\0')
2204                                 die("unrecognized context count '%s'", arg + 2);
2205                         continue;
2206                 }
2207                 if (!strncmp(arg, "--whitespace=", 13)) {
2208                         whitespace_option = arg + 13;
2209                         parse_whitespace_option(arg + 13);
2210                         continue;
2211                 }
2212
2213                 if (check_index && prefix_length < 0) {
2214                         prefix = setup_git_directory();
2215                         prefix_length = prefix ? strlen(prefix) : 0;
2216                         git_config(git_apply_config);
2217                         if (!whitespace_option && apply_default_whitespace)
2218                                 parse_whitespace_option(apply_default_whitespace);
2219                 }
2220                 if (0 < prefix_length)
2221                         arg = prefix_filename(prefix, prefix_length, arg);
2222
2223                 fd = open(arg, O_RDONLY);
2224                 if (fd < 0)
2225                         usage(apply_usage);
2226                 read_stdin = 0;
2227                 set_default_whitespace_mode(whitespace_option);
2228                 apply_patch(fd, arg);
2229                 close(fd);
2230         }
2231         set_default_whitespace_mode(whitespace_option);
2232         if (read_stdin)
2233                 apply_patch(0, "<stdin>");
2234         if (whitespace_error) {
2235                 if (squelch_whitespace_errors &&
2236                     squelch_whitespace_errors < whitespace_error) {
2237                         int squelched =
2238                                 whitespace_error - squelch_whitespace_errors;
2239                         fprintf(stderr, "warning: squelched %d whitespace error%s\n",
2240                                 squelched,
2241                                 squelched == 1 ? "" : "s");
2242                 }
2243                 if (new_whitespace == error_on_whitespace)
2244                         die("%d line%s add%s trailing whitespaces.",
2245                             whitespace_error,
2246                             whitespace_error == 1 ? "" : "s",
2247                             whitespace_error == 1 ? "s" : "");
2248                 if (applied_after_stripping)
2249                         fprintf(stderr, "warning: %d line%s applied after"
2250                                 " stripping trailing whitespaces.\n",
2251                                 applied_after_stripping,
2252                                 applied_after_stripping == 1 ? "" : "s");
2253                 else if (whitespace_error)
2254                         fprintf(stderr, "warning: %d line%s add%s trailing"
2255                                 " whitespaces.\n",
2256                                 whitespace_error,
2257                                 whitespace_error == 1 ? "" : "s",
2258                                 whitespace_error == 1 ? "s" : "");
2259         }
2260
2261         if (write_index) {
2262                 if (write_cache(newfd, active_cache, active_nr) ||
2263                     commit_index_file(&cache_file))
2264                         die("Unable to write new cachefile");
2265         }
2266
2267         return 0;
2268 }