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