git-upload-pack: Support sending multiple ACK messages
[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 "quote.h"
12
13 //  --check turns on checking that the working tree matches the
14 //    files that are being modified, but doesn't apply the patch
15 //  --stat does just a diffstat, and doesn't actually apply
16 //  --index-info shows the old and new index info for paths if available.
17 //
18 static int check_index = 0;
19 static int write_index = 0;
20 static int diffstat = 0;
21 static int summary = 0;
22 static int check = 0;
23 static int apply = 1;
24 static int show_index_info = 0;
25 static int line_termination = '\n';
26 static const char apply_usage[] =
27 "git-apply [--stat] [--summary] [--check] [--index] [--apply] [--index-info] [-z] <patch>...";
28
29 /*
30  * For "diff-stat" like behaviour, we keep track of the biggest change
31  * we've seen, and the longest filename. That allows us to do simple
32  * scaling.
33  */
34 static int max_change, max_len;
35
36 /*
37  * Various "current state", notably line numbers and what
38  * file (and how) we're patching right now.. The "is_xxxx"
39  * things are flags, where -1 means "don't know yet".
40  */
41 static int linenr = 1;
42
43 struct fragment {
44         unsigned long oldpos, oldlines;
45         unsigned long newpos, newlines;
46         const char *patch;
47         int size;
48         struct fragment *next;
49 };
50
51 struct patch {
52         char *new_name, *old_name, *def_name;
53         unsigned int old_mode, new_mode;
54         int is_rename, is_copy, is_new, is_delete;
55         int lines_added, lines_deleted;
56         int score;
57         struct fragment *fragments;
58         char *result;
59         unsigned long resultsize;
60         char old_sha1_prefix[41];
61         char new_sha1_prefix[41];
62         struct patch *next;
63 };
64
65 #define CHUNKSIZE (8192)
66 #define SLOP (16)
67
68 static void *read_patch_file(int fd, unsigned long *sizep)
69 {
70         unsigned long size = 0, alloc = CHUNKSIZE;
71         void *buffer = xmalloc(alloc);
72
73         for (;;) {
74                 int nr = alloc - size;
75                 if (nr < 1024) {
76                         alloc += CHUNKSIZE;
77                         buffer = xrealloc(buffer, alloc);
78                         nr = alloc - size;
79                 }
80                 nr = read(fd, buffer + size, nr);
81                 if (!nr)
82                         break;
83                 if (nr < 0) {
84                         if (errno == EAGAIN)
85                                 continue;
86                         die("git-apply: read returned %s", strerror(errno));
87                 }
88                 size += nr;
89         }
90         *sizep = size;
91
92         /*
93          * Make sure that we have some slop in the buffer
94          * so that we can do speculative "memcmp" etc, and
95          * see to it that it is NUL-filled.
96          */
97         if (alloc < size + SLOP)
98                 buffer = xrealloc(buffer, size + SLOP);
99         memset(buffer + size, 0, SLOP);
100         return buffer;
101 }
102
103 static unsigned long linelen(const char *buffer, unsigned long size)
104 {
105         unsigned long len = 0;
106         while (size--) {
107                 len++;
108                 if (*buffer++ == '\n')
109                         break;
110         }
111         return len;
112 }
113
114 static int is_dev_null(const char *str)
115 {
116         return !memcmp("/dev/null", str, 9) && isspace(str[9]);
117 }
118
119 #define TERM_SPACE      1
120 #define TERM_TAB        2
121
122 static int name_terminate(const char *name, int namelen, int c, int terminate)
123 {
124         if (c == ' ' && !(terminate & TERM_SPACE))
125                 return 0;
126         if (c == '\t' && !(terminate & TERM_TAB))
127                 return 0;
128
129         return 1;
130 }
131
132 static char * find_name(const char *line, char *def, int p_value, int terminate)
133 {
134         int len;
135         const char *start = line;
136         char *name;
137
138         if (*line == '"') {
139                 /* Proposed "new-style" GNU patch/diff format; see
140                  * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
141                  */
142                 name = unquote_c_style(line, NULL);
143                 if (name) {
144                         char *cp = name;
145                         while (p_value) {
146                                 cp = strchr(name, '/');
147                                 if (!cp)
148                                         break;
149                                 cp++;
150                                 p_value--;
151                         }
152                         if (cp) {
153                                 /* name can later be freed, so we need
154                                  * to memmove, not just return cp
155                                  */
156                                 memmove(name, cp, strlen(cp) + 1);
157                                 free(def);
158                                 return name;
159                         }
160                         else {
161                                 free(name);
162                                 name = NULL;
163                         }
164                 }
165         }
166
167         for (;;) {
168                 char c = *line;
169
170                 if (isspace(c)) {
171                         if (c == '\n')
172                                 break;
173                         if (name_terminate(start, line-start, c, terminate))
174                                 break;
175                 }
176                 line++;
177                 if (c == '/' && !--p_value)
178                         start = line;
179         }
180         if (!start)
181                 return def;
182         len = line - start;
183         if (!len)
184                 return def;
185
186         /*
187          * Generally we prefer the shorter name, especially
188          * if the other one is just a variation of that with
189          * something else tacked on to the end (ie "file.orig"
190          * or "file~").
191          */
192         if (def) {
193                 int deflen = strlen(def);
194                 if (deflen < len && !strncmp(start, def, deflen))
195                         return def;
196         }
197
198         name = xmalloc(len + 1);
199         memcpy(name, start, len);
200         name[len] = 0;
201         free(def);
202         return name;
203 }
204
205 /*
206  * Get the name etc info from the --/+++ lines of a traditional patch header
207  *
208  * NOTE! This hardcodes "-p1" behaviour in filename detection.
209  *
210  * FIXME! The end-of-filename heuristics are kind of screwy. For existing
211  * files, we can happily check the index for a match, but for creating a
212  * new file we should try to match whatever "patch" does. I have no idea.
213  */
214 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
215 {
216         int p_value = 1;
217         char *name;
218
219         first += 4;     // skip "--- "
220         second += 4;    // skip "+++ "
221         if (is_dev_null(first)) {
222                 patch->is_new = 1;
223                 patch->is_delete = 0;
224                 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
225                 patch->new_name = name;
226         } else if (is_dev_null(second)) {
227                 patch->is_new = 0;
228                 patch->is_delete = 1;
229                 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
230                 patch->old_name = name;
231         } else {
232                 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
233                 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
234                 patch->old_name = patch->new_name = name;
235         }
236         if (!name)
237                 die("unable to find filename in patch at line %d", linenr);
238 }
239
240 static int gitdiff_hdrend(const char *line, struct patch *patch)
241 {
242         return -1;
243 }
244
245 /*
246  * We're anal about diff header consistency, to make
247  * sure that we don't end up having strange ambiguous
248  * patches floating around.
249  *
250  * As a result, gitdiff_{old|new}name() will check
251  * their names against any previous information, just
252  * to make sure..
253  */
254 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
255 {
256         if (!orig_name && !isnull)
257                 return find_name(line, NULL, 1, 0);
258
259         if (orig_name) {
260                 int len;
261                 const char *name;
262                 char *another;
263                 name = orig_name;
264                 len = strlen(name);
265                 if (isnull)
266                         die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
267                 another = find_name(line, NULL, 1, 0);
268                 if (!another || memcmp(another, name, len))
269                         die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
270                 free(another);
271                 return orig_name;
272         }
273         else {
274                 /* expect "/dev/null" */
275                 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
276                         die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
277                 return NULL;
278         }
279 }
280
281 static int gitdiff_oldname(const char *line, struct patch *patch)
282 {
283         patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
284         return 0;
285 }
286
287 static int gitdiff_newname(const char *line, struct patch *patch)
288 {
289         patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
290         return 0;
291 }
292
293 static int gitdiff_oldmode(const char *line, struct patch *patch)
294 {
295         patch->old_mode = strtoul(line, NULL, 8);
296         return 0;
297 }
298
299 static int gitdiff_newmode(const char *line, struct patch *patch)
300 {
301         patch->new_mode = strtoul(line, NULL, 8);
302         return 0;
303 }
304
305 static int gitdiff_delete(const char *line, struct patch *patch)
306 {
307         patch->is_delete = 1;
308         patch->old_name = patch->def_name;
309         return gitdiff_oldmode(line, patch);
310 }
311
312 static int gitdiff_newfile(const char *line, struct patch *patch)
313 {
314         patch->is_new = 1;
315         patch->new_name = patch->def_name;
316         return gitdiff_newmode(line, patch);
317 }
318
319 static int gitdiff_copysrc(const char *line, struct patch *patch)
320 {
321         patch->is_copy = 1;
322         patch->old_name = find_name(line, NULL, 0, 0);
323         return 0;
324 }
325
326 static int gitdiff_copydst(const char *line, struct patch *patch)
327 {
328         patch->is_copy = 1;
329         patch->new_name = find_name(line, NULL, 0, 0);
330         return 0;
331 }
332
333 static int gitdiff_renamesrc(const char *line, struct patch *patch)
334 {
335         patch->is_rename = 1;
336         patch->old_name = find_name(line, NULL, 0, 0);
337         return 0;
338 }
339
340 static int gitdiff_renamedst(const char *line, struct patch *patch)
341 {
342         patch->is_rename = 1;
343         patch->new_name = find_name(line, NULL, 0, 0);
344         return 0;
345 }
346
347 static int gitdiff_similarity(const char *line, struct patch *patch)
348 {
349         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
350                 patch->score = 0;
351         return 0;
352 }
353
354 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
355 {
356         if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
357                 patch->score = 0;
358         return 0;
359 }
360
361 static int gitdiff_index(const char *line, struct patch *patch)
362 {
363         /* index line is N hexadecimal, "..", N hexadecimal,
364          * and optional space with octal mode.
365          */
366         const char *ptr, *eol;
367         int len;
368
369         ptr = strchr(line, '.');
370         if (!ptr || ptr[1] != '.' || 40 <= ptr - line)
371                 return 0;
372         len = ptr - line;
373         memcpy(patch->old_sha1_prefix, line, len);
374         patch->old_sha1_prefix[len] = 0;
375
376         line = ptr + 2;
377         ptr = strchr(line, ' ');
378         eol = strchr(line, '\n');
379
380         if (!ptr || eol < ptr)
381                 ptr = eol;
382         len = ptr - line;
383
384         if (40 <= len)
385                 return 0;
386         memcpy(patch->new_sha1_prefix, line, len);
387         patch->new_sha1_prefix[len] = 0;
388         if (*ptr == ' ')
389                 patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
390         return 0;
391 }
392
393 /*
394  * This is normal for a diff that doesn't change anything: we'll fall through
395  * into the next diff. Tell the parser to break out.
396  */
397 static int gitdiff_unrecognized(const char *line, struct patch *patch)
398 {
399         return -1;
400 }
401
402 static const char *stop_at_slash(const char *line, int llen)
403 {
404         int i;
405
406         for (i = 0; i < llen; i++) {
407                 int ch = line[i];
408                 if (ch == '/')
409                         return line + i;
410         }
411         return NULL;
412 }
413
414 /* This is to extract the same name that appears on "diff --git"
415  * line.  We do not find and return anything if it is a rename
416  * patch, and it is OK because we will find the name elsewhere.
417  * We need to reliably find name only when it is mode-change only,
418  * creation or deletion of an empty file.  In any of these cases,
419  * both sides are the same name under a/ and b/ respectively.
420  */
421 static char *git_header_name(char *line, int llen)
422 {
423         int len;
424         const char *name;
425         const char *second = NULL;
426
427         line += strlen("diff --git ");
428         llen -= strlen("diff --git ");
429
430         if (*line == '"') {
431                 const char *cp;
432                 char *first = unquote_c_style(line, &second);
433                 if (!first)
434                         return NULL;
435
436                 /* advance to the first slash */
437                 cp = stop_at_slash(first, strlen(first));
438                 if (!cp || cp == first) {
439                         /* we do not accept absolute paths */
440                 free_first_and_fail:
441                         free(first);
442                         return NULL;
443                 }
444                 len = strlen(cp+1);
445                 memmove(first, cp+1, len+1); /* including NUL */
446
447                 /* second points at one past closing dq of name.
448                  * find the second name.
449                  */
450                 while ((second < line + llen) && isspace(*second))
451                         second++;
452
453                 if (line + llen <= second)
454                         goto free_first_and_fail;
455                 if (*second == '"') {
456                         char *sp = unquote_c_style(second, NULL);
457                         if (!sp)
458                                 goto free_first_and_fail;
459                         cp = stop_at_slash(sp, strlen(sp));
460                         if (!cp || cp == sp) {
461                         free_both_and_fail:
462                                 free(sp);
463                                 goto free_first_and_fail;
464                         }
465                         /* They must match, otherwise ignore */
466                         if (strcmp(cp+1, first))
467                                 goto free_both_and_fail;
468                         free(sp);
469                         return first;
470                 }
471
472                 /* unquoted second */
473                 cp = stop_at_slash(second, line + llen - second);
474                 if (!cp || cp == second)
475                         goto free_first_and_fail;
476                 cp++;
477                 if (line + llen - cp != len + 1 ||
478                     memcmp(first, cp, len))
479                         goto free_first_and_fail;
480                 return first;
481         }
482
483         /* unquoted first name */
484         name = stop_at_slash(line, llen);
485         if (!name || name == line)
486                 return NULL;
487
488         name++;
489
490         /* since the first name is unquoted, a dq if exists must be
491          * the beginning of the second name.
492          */
493         for (second = name; second < line + llen; second++) {
494                 if (*second == '"') {
495                         const char *cp = second;
496                         const char *np;
497                         char *sp = unquote_c_style(second, NULL);
498
499                         if (!sp)
500                                 return NULL;
501                         np = stop_at_slash(sp, strlen(sp));
502                         if (!np || np == sp) {
503                         free_second_and_fail:
504                                 free(sp);
505                                 return NULL;
506                         }
507                         np++;
508                         len = strlen(np);
509                         if (len < cp - name &&
510                             !strncmp(np, name, len) &&
511                             isspace(name[len])) {
512                                 /* Good */
513                                 memmove(sp, np, len + 1);
514                                 return sp;
515                         }
516                         goto free_second_and_fail;
517                 }
518         }
519
520         /*
521          * Accept a name only if it shows up twice, exactly the same
522          * form.
523          */
524         for (len = 0 ; ; len++) {
525                 char c = name[len];
526
527                 switch (c) {
528                 default:
529                         continue;
530                 case '\n':
531                         return NULL;
532                 case '\t': case ' ':
533                         second = name+len;
534                         for (;;) {
535                                 char c = *second++;
536                                 if (c == '\n')
537                                         return NULL;
538                                 if (c == '/')
539                                         break;
540                         }
541                         if (second[len] == '\n' && !memcmp(name, second, len)) {
542                                 char *ret = xmalloc(len + 1);
543                                 memcpy(ret, name, len);
544                                 ret[len] = 0;
545                                 return ret;
546                         }
547                 }
548         }
549         return NULL;
550 }
551
552 /* Verify that we recognize the lines following a git header */
553 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
554 {
555         unsigned long offset;
556
557         /* A git diff has explicit new/delete information, so we don't guess */
558         patch->is_new = 0;
559         patch->is_delete = 0;
560
561         /*
562          * Some things may not have the old name in the
563          * rest of the headers anywhere (pure mode changes,
564          * or removing or adding empty files), so we get
565          * the default name from the header.
566          */
567         patch->def_name = git_header_name(line, len);
568
569         line += len;
570         size -= len;
571         linenr++;
572         for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
573                 static const struct opentry {
574                         const char *str;
575                         int (*fn)(const char *, struct patch *);
576                 } optable[] = {
577                         { "@@ -", gitdiff_hdrend },
578                         { "--- ", gitdiff_oldname },
579                         { "+++ ", gitdiff_newname },
580                         { "old mode ", gitdiff_oldmode },
581                         { "new mode ", gitdiff_newmode },
582                         { "deleted file mode ", gitdiff_delete },
583                         { "new file mode ", gitdiff_newfile },
584                         { "copy from ", gitdiff_copysrc },
585                         { "copy to ", gitdiff_copydst },
586                         { "rename old ", gitdiff_renamesrc },
587                         { "rename new ", gitdiff_renamedst },
588                         { "rename from ", gitdiff_renamesrc },
589                         { "rename to ", gitdiff_renamedst },
590                         { "similarity index ", gitdiff_similarity },
591                         { "dissimilarity index ", gitdiff_dissimilarity },
592                         { "index ", gitdiff_index },
593                         { "", gitdiff_unrecognized },
594                 };
595                 int i;
596
597                 len = linelen(line, size);
598                 if (!len || line[len-1] != '\n')
599                         break;
600                 for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) {
601                         const struct opentry *p = optable + i;
602                         int oplen = strlen(p->str);
603                         if (len < oplen || memcmp(p->str, line, oplen))
604                                 continue;
605                         if (p->fn(line + oplen, patch) < 0)
606                                 return offset;
607                         break;
608                 }
609         }
610
611         return offset;
612 }
613
614 static int parse_num(const char *line, unsigned long *p)
615 {
616         char *ptr;
617
618         if (!isdigit(*line))
619                 return 0;
620         *p = strtoul(line, &ptr, 10);
621         return ptr - line;
622 }
623
624 static int parse_range(const char *line, int len, int offset, const char *expect,
625                         unsigned long *p1, unsigned long *p2)
626 {
627         int digits, ex;
628
629         if (offset < 0 || offset >= len)
630                 return -1;
631         line += offset;
632         len -= offset;
633
634         digits = parse_num(line, p1);
635         if (!digits)
636                 return -1;
637
638         offset += digits;
639         line += digits;
640         len -= digits;
641
642         *p2 = *p1;
643         if (*line == ',') {
644                 digits = parse_num(line+1, p2);
645                 if (!digits)
646                         return -1;
647
648                 offset += digits+1;
649                 line += digits+1;
650                 len -= digits+1;
651         }
652
653         ex = strlen(expect);
654         if (ex > len)
655                 return -1;
656         if (memcmp(line, expect, ex))
657                 return -1;
658
659         return offset + ex;
660 }
661
662 /*
663  * Parse a unified diff fragment header of the
664  * form "@@ -a,b +c,d @@"
665  */
666 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
667 {
668         int offset;
669
670         if (!len || line[len-1] != '\n')
671                 return -1;
672
673         /* Figure out the number of lines in a fragment */
674         offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
675         offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
676
677         return offset;
678 }
679
680 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
681 {
682         unsigned long offset, len;
683
684         patch->is_rename = patch->is_copy = 0;
685         patch->is_new = patch->is_delete = -1;
686         patch->old_mode = patch->new_mode = 0;
687         patch->old_name = patch->new_name = NULL;
688         for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
689                 unsigned long nextlen;
690
691                 len = linelen(line, size);
692                 if (!len)
693                         break;
694
695                 /* Testing this early allows us to take a few shortcuts.. */
696                 if (len < 6)
697                         continue;
698
699                 /*
700                  * Make sure we don't find any unconnected patch fragmants.
701                  * That's a sign that we didn't find a header, and that a
702                  * patch has become corrupted/broken up.
703                  */
704                 if (!memcmp("@@ -", line, 4)) {
705                         struct fragment dummy;
706                         if (parse_fragment_header(line, len, &dummy) < 0)
707                                 continue;
708                         error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
709                 }
710
711                 if (size < len + 6)
712                         break;
713
714                 /*
715                  * Git patch? It might not have a real patch, just a rename
716                  * or mode change, so we handle that specially
717                  */
718                 if (!memcmp("diff --git ", line, 11)) {
719                         int git_hdr_len = parse_git_header(line, len, size, patch);
720                         if (git_hdr_len <= len)
721                                 continue;
722                         if (!patch->old_name && !patch->new_name) {
723                                 if (!patch->def_name)
724                                         die("git diff header lacks filename information (line %d)", linenr);
725                                 patch->old_name = patch->new_name = patch->def_name;
726                         }
727                         *hdrsize = git_hdr_len;
728                         return offset;
729                 }
730
731                 /** --- followed by +++ ? */
732                 if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
733                         continue;
734
735                 /*
736                  * We only accept unified patches, so we want it to
737                  * at least have "@@ -a,b +c,d @@\n", which is 14 chars
738                  * minimum
739                  */
740                 nextlen = linelen(line + len, size - len);
741                 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
742                         continue;
743
744                 /* Ok, we'll consider it a patch */
745                 parse_traditional_patch(line, line+len, patch);
746                 *hdrsize = len + nextlen;
747                 linenr += 2;
748                 return offset;
749         }
750         return -1;
751 }
752
753 /*
754  * Parse a unified diff. Note that this really needs
755  * to parse each fragment separately, since the only
756  * way to know the difference between a "---" that is
757  * part of a patch, and a "---" that starts the next
758  * patch is to look at the line counts..
759  */
760 static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
761 {
762         int added, deleted;
763         int len = linelen(line, size), offset;
764         unsigned long oldlines, newlines;
765
766         offset = parse_fragment_header(line, len, fragment);
767         if (offset < 0)
768                 return -1;
769         oldlines = fragment->oldlines;
770         newlines = fragment->newlines;
771
772         if (patch->is_new < 0) {
773                 patch->is_new =  !oldlines;
774                 if (!oldlines)
775                         patch->old_name = NULL;
776         }
777         if (patch->is_delete < 0) {
778                 patch->is_delete = !newlines;
779                 if (!newlines)
780                         patch->new_name = NULL;
781         }
782
783         if (patch->is_new != !oldlines)
784                 return error("new file depends on old contents");
785         if (patch->is_delete != !newlines) {
786                 if (newlines)
787                         return error("deleted file still has contents");
788                 fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name);
789         }
790
791         /* Parse the thing.. */
792         line += len;
793         size -= len;
794         linenr++;
795         added = deleted = 0;
796         for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
797                 if (!oldlines && !newlines)
798                         break;
799                 len = linelen(line, size);
800                 if (!len || line[len-1] != '\n')
801                         return -1;
802                 switch (*line) {
803                 default:
804                         return -1;
805                 case ' ':
806                         oldlines--;
807                         newlines--;
808                         break;
809                 case '-':
810                         deleted++;
811                         oldlines--;
812                         break;
813                 case '+':
814                         added++;
815                         newlines--;
816                         break;
817
818                 /* We allow "\ No newline at end of file". Depending
819                  * on locale settings when the patch was produced we
820                  * don't know what this line looks like. The only
821                  * thing we do know is that it begins with "\ ".
822                  * Checking for 12 is just for sanity check -- any
823                  * l10n of "\ No newline..." is at least that long.
824                  */
825                 case '\\':
826                         if (len < 12 || memcmp(line, "\\ ", 2))
827                                 return -1;
828                         break;
829                 }
830         }
831         /* If a fragment ends with an incomplete line, we failed to include
832          * it in the above loop because we hit oldlines == newlines == 0
833          * before seeing it.
834          */
835         if (12 < size && !memcmp(line, "\\ ", 2))
836                 offset += linelen(line, size);
837
838         patch->lines_added += added;
839         patch->lines_deleted += deleted;
840         return offset;
841 }
842
843 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
844 {
845         unsigned long offset = 0;
846         struct fragment **fragp = &patch->fragments;
847
848         while (size > 4 && !memcmp(line, "@@ -", 4)) {
849                 struct fragment *fragment;
850                 int len;
851
852                 fragment = xmalloc(sizeof(*fragment));
853                 memset(fragment, 0, sizeof(*fragment));
854                 len = parse_fragment(line, size, patch, fragment);
855                 if (len <= 0)
856                         die("corrupt patch at line %d", linenr);
857
858                 fragment->patch = line;
859                 fragment->size = len;
860
861                 *fragp = fragment;
862                 fragp = &fragment->next;
863
864                 offset += len;
865                 line += len;
866                 size -= len;
867         }
868         return offset;
869 }
870
871 static inline int metadata_changes(struct patch *patch)
872 {
873         return  patch->is_rename > 0 ||
874                 patch->is_copy > 0 ||
875                 patch->is_new > 0 ||
876                 patch->is_delete ||
877                 (patch->old_mode && patch->new_mode &&
878                  patch->old_mode != patch->new_mode);
879 }
880
881 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
882 {
883         int hdrsize, patchsize;
884         int offset = find_header(buffer, size, &hdrsize, patch);
885
886         if (offset < 0)
887                 return offset;
888
889         patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
890
891         if (!patchsize && !metadata_changes(patch))
892                 die("patch with only garbage at line %d", linenr);
893
894         return offset + hdrsize + patchsize;
895 }
896
897 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
898 static const char minuses[]= "----------------------------------------------------------------------";
899
900 static void show_stats(struct patch *patch)
901 {
902         const char *prefix = "";
903         char *name = patch->new_name;
904         char *qname = NULL;
905         int len, max, add, del, total;
906
907         if (!name)
908                 name = patch->old_name;
909
910         if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
911                 qname = xmalloc(len + 1);
912                 quote_c_style(name, qname, NULL, 0);
913                 name = qname;
914         }
915
916         /*
917          * "scale" the filename
918          */
919         len = strlen(name);
920         max = max_len;
921         if (max > 50)
922                 max = 50;
923         if (len > max) {
924                 char *slash;
925                 prefix = "...";
926                 max -= 3;
927                 name += len - max;
928                 slash = strchr(name, '/');
929                 if (slash)
930                         name = slash;
931         }
932         len = max;
933
934         /*
935          * scale the add/delete
936          */
937         max = max_change;
938         if (max + len > 70)
939                 max = 70 - len;
940
941         add = patch->lines_added;
942         del = patch->lines_deleted;
943         total = add + del;
944
945         if (max_change > 0) {
946                 total = (total * max + max_change / 2) / max_change;
947                 add = (add * max + max_change / 2) / max_change;
948                 del = total - add;
949         }
950         printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
951                 len, name, patch->lines_added + patch->lines_deleted,
952                 add, pluses, del, minuses);
953         if (qname)
954                 free(qname);
955 }
956
957 static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
958 {
959         int fd;
960         unsigned long got;
961
962         switch (st->st_mode & S_IFMT) {
963         case S_IFLNK:
964                 return readlink(path, buf, size);
965         case S_IFREG:
966                 fd = open(path, O_RDONLY);
967                 if (fd < 0)
968                         return error("unable to open %s", path);
969                 got = 0;
970                 for (;;) {
971                         int ret = read(fd, buf + got, size - got);
972                         if (ret < 0) {
973                                 if (errno == EAGAIN)
974                                         continue;
975                                 break;
976                         }
977                         if (!ret)
978                                 break;
979                         got += ret;
980                 }
981                 close(fd);
982                 return got;
983
984         default:
985                 return -1;
986         }
987 }
988
989 static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
990 {
991         int i;
992         unsigned long start, backwards, forwards;
993
994         if (fragsize > size)
995                 return -1;
996
997         start = 0;
998         if (line > 1) {
999                 unsigned long offset = 0;
1000                 i = line-1;
1001                 while (offset + fragsize <= size) {
1002                         if (buf[offset++] == '\n') {
1003                                 start = offset;
1004                                 if (!--i)
1005                                         break;
1006                         }
1007                 }
1008         }
1009
1010         /* Exact line number? */
1011         if (!memcmp(buf + start, fragment, fragsize))
1012                 return start;
1013
1014         /*
1015          * There's probably some smart way to do this, but I'll leave
1016          * that to the smart and beautiful people. I'm simple and stupid.
1017          */
1018         backwards = start;
1019         forwards = start;
1020         for (i = 0; ; i++) {
1021                 unsigned long try;
1022                 int n;
1023
1024                 /* "backward" */
1025                 if (i & 1) {
1026                         if (!backwards) {
1027                                 if (forwards + fragsize > size)
1028                                         break;
1029                                 continue;
1030                         }
1031                         do {
1032                                 --backwards;
1033                         } while (backwards && buf[backwards-1] != '\n');
1034                         try = backwards;
1035                 } else {
1036                         while (forwards + fragsize <= size) {
1037                                 if (buf[forwards++] == '\n')
1038                                         break;
1039                         }
1040                         try = forwards;
1041                 }
1042
1043                 if (try + fragsize > size)
1044                         continue;
1045                 if (memcmp(buf + try, fragment, fragsize))
1046                         continue;
1047                 n = (i >> 1)+1;
1048                 if (i & 1)
1049                         n = -n;
1050                 return try;
1051         }
1052
1053         /*
1054          * We should start searching forward and backward.
1055          */
1056         return -1;
1057 }
1058
1059 struct buffer_desc {
1060         char *buffer;
1061         unsigned long size;
1062         unsigned long alloc;
1063 };
1064
1065 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1066 {
1067         char *buf = desc->buffer;
1068         const char *patch = frag->patch;
1069         int offset, size = frag->size;
1070         char *old = xmalloc(size);
1071         char *new = xmalloc(size);
1072         int oldsize = 0, newsize = 0;
1073
1074         while (size > 0) {
1075                 int len = linelen(patch, size);
1076                 int plen;
1077
1078                 if (!len)
1079                         break;
1080
1081                 /*
1082                  * "plen" is how much of the line we should use for
1083                  * the actual patch data. Normally we just remove the
1084                  * first character on the line, but if the line is
1085                  * followed by "\ No newline", then we also remove the
1086                  * last one (which is the newline, of course).
1087                  */
1088                 plen = len-1;
1089                 if (len < size && patch[len] == '\\')
1090                         plen--;
1091                 switch (*patch) {
1092                 case ' ':
1093                 case '-':
1094                         memcpy(old + oldsize, patch + 1, plen);
1095                         oldsize += plen;
1096                         if (*patch == '-')
1097                                 break;
1098                 /* Fall-through for ' ' */
1099                 case '+':
1100                         memcpy(new + newsize, patch + 1, plen);
1101                         newsize += plen;
1102                         break;
1103                 case '@': case '\\':
1104                         /* Ignore it, we already handled it */
1105                         break;
1106                 default:
1107                         return -1;
1108                 }
1109                 patch += len;
1110                 size -= len;
1111         }
1112
1113         offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
1114         if (offset >= 0) {
1115                 int diff = newsize - oldsize;
1116                 unsigned long size = desc->size + diff;
1117                 unsigned long alloc = desc->alloc;
1118
1119                 if (size > alloc) {
1120                         alloc = size + 8192;
1121                         desc->alloc = alloc;
1122                         buf = xrealloc(buf, alloc);
1123                         desc->buffer = buf;
1124                 }
1125                 desc->size = size;
1126                 memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1127                 memcpy(buf + offset, new, newsize);
1128                 offset = 0;
1129         }
1130
1131         free(old);
1132         free(new);
1133         return offset;
1134 }
1135
1136 static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1137 {
1138         struct fragment *frag = patch->fragments;
1139
1140         while (frag) {
1141                 if (apply_one_fragment(desc, frag) < 0)
1142                         return error("patch failed: %s:%ld", patch->old_name, frag->oldpos);
1143                 frag = frag->next;
1144         }
1145         return 0;
1146 }
1147
1148 static int apply_data(struct patch *patch, struct stat *st)
1149 {
1150         char *buf;
1151         unsigned long size, alloc;
1152         struct buffer_desc desc;
1153
1154         size = 0;
1155         alloc = 0;
1156         buf = NULL;
1157         if (patch->old_name) {
1158                 size = st->st_size;
1159                 alloc = size + 8192;
1160                 buf = xmalloc(alloc);
1161                 if (read_old_data(st, patch->old_name, buf, alloc) != size)
1162                         return error("read of %s failed", patch->old_name);
1163         }
1164
1165         desc.size = size;
1166         desc.alloc = alloc;
1167         desc.buffer = buf;
1168         if (apply_fragments(&desc, patch) < 0)
1169                 return -1;
1170         patch->result = desc.buffer;
1171         patch->resultsize = desc.size;
1172
1173         if (patch->is_delete && patch->resultsize)
1174                 return error("removal patch leaves file contents");
1175
1176         return 0;
1177 }
1178
1179 static int check_patch(struct patch *patch)
1180 {
1181         struct stat st;
1182         const char *old_name = patch->old_name;
1183         const char *new_name = patch->new_name;
1184
1185         if (old_name) {
1186                 int changed;
1187                 int stat_ret = lstat(old_name, &st);
1188
1189                 if (check_index) {
1190                         int pos = cache_name_pos(old_name, strlen(old_name));
1191                         if (pos < 0)
1192                                 return error("%s: does not exist in index",
1193                                              old_name);
1194                         if (stat_ret < 0) {
1195                                 struct checkout costate;
1196                                 if (errno != ENOENT)
1197                                         return error("%s: %s", old_name,
1198                                                      strerror(errno));
1199                                 /* checkout */
1200                                 costate.base_dir = "";
1201                                 costate.base_dir_len = 0;
1202                                 costate.force = 0;
1203                                 costate.quiet = 0;
1204                                 costate.not_new = 0;
1205                                 costate.refresh_cache = 1;
1206                                 if (checkout_entry(active_cache[pos],
1207                                                    &costate) ||
1208                                     lstat(old_name, &st))
1209                                         return -1;
1210                         }
1211
1212                         changed = ce_match_stat(active_cache[pos], &st);
1213                         if (changed)
1214                                 return error("%s: does not match index",
1215                                              old_name);
1216                 }
1217                 else if (stat_ret < 0)
1218                         return error("%s: %s", old_name, strerror(errno));
1219
1220                 if (patch->is_new < 0)
1221                         patch->is_new = 0;
1222                 st.st_mode = ntohl(create_ce_mode(st.st_mode));
1223                 if (!patch->old_mode)
1224                         patch->old_mode = st.st_mode;
1225                 if ((st.st_mode ^ patch->old_mode) & S_IFMT)
1226                         return error("%s: wrong type", old_name);
1227                 if (st.st_mode != patch->old_mode)
1228                         fprintf(stderr, "warning: %s has type %o, expected %o\n",
1229                                 old_name, st.st_mode, patch->old_mode);
1230         }
1231
1232         if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1233                 if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1234                         return error("%s: already exists in index", new_name);
1235                 if (!lstat(new_name, &st))
1236                         return error("%s: already exists in working directory", new_name);
1237                 if (errno != ENOENT)
1238                         return error("%s: %s", new_name, strerror(errno));
1239                 if (!patch->new_mode) {
1240                         if (patch->is_new)
1241                                 patch->new_mode = S_IFREG | 0644;
1242                         else
1243                                 patch->new_mode = patch->old_mode;
1244                 }
1245         }
1246
1247         if (new_name && old_name) {
1248                 int same = !strcmp(old_name, new_name);
1249                 if (!patch->new_mode)
1250                         patch->new_mode = patch->old_mode;
1251                 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
1252                         return error("new mode (%o) of %s does not match old mode (%o)%s%s",
1253                                 patch->new_mode, new_name, patch->old_mode,
1254                                 same ? "" : " of ", same ? "" : old_name);
1255         }       
1256
1257         if (apply_data(patch, &st) < 0)
1258                 return error("%s: patch does not apply", old_name);
1259         return 0;
1260 }
1261
1262 static int check_patch_list(struct patch *patch)
1263 {
1264         int error = 0;
1265
1266         for (;patch ; patch = patch->next)
1267                 error |= check_patch(patch);
1268         return error;
1269 }
1270
1271 static inline int is_null_sha1(const unsigned char *sha1)
1272 {
1273         return !memcmp(sha1, null_sha1, 20);
1274 }
1275
1276 static void show_index_list(struct patch *list)
1277 {
1278         struct patch *patch;
1279
1280         /* Once we start supporting the reverse patch, it may be
1281          * worth showing the new sha1 prefix, but until then...
1282          */
1283         for (patch = list; patch; patch = patch->next) {
1284                 const unsigned char *sha1_ptr;
1285                 unsigned char sha1[20];
1286                 const char *name;
1287
1288                 name = patch->old_name ? patch->old_name : patch->new_name;
1289                 if (patch->is_new)
1290                         sha1_ptr = null_sha1;
1291                 else if (get_sha1(patch->old_sha1_prefix, sha1))
1292                         die("sha1 information is lacking or useless (%s).",
1293                             name);
1294                 else
1295                         sha1_ptr = sha1;
1296
1297                 printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
1298                 if (line_termination && quote_c_style(name, NULL, NULL, 0))
1299                         quote_c_style(name, NULL, stdout, 0);
1300                 else
1301                         fputs(name, stdout);
1302                 putchar(line_termination);
1303         }
1304 }
1305
1306 static void stat_patch_list(struct patch *patch)
1307 {
1308         int files, adds, dels;
1309
1310         for (files = adds = dels = 0 ; patch ; patch = patch->next) {
1311                 files++;
1312                 adds += patch->lines_added;
1313                 dels += patch->lines_deleted;
1314                 show_stats(patch);
1315         }
1316
1317         printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
1318 }
1319
1320 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
1321 {
1322         if (mode)
1323                 printf(" %s mode %06o %s\n", newdelete, mode, name);
1324         else
1325                 printf(" %s %s\n", newdelete, name);
1326 }
1327
1328 static void show_mode_change(struct patch *p, int show_name)
1329 {
1330         if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
1331                 if (show_name)
1332                         printf(" mode change %06o => %06o %s\n",
1333                                p->old_mode, p->new_mode, p->new_name);
1334                 else
1335                         printf(" mode change %06o => %06o\n",
1336                                p->old_mode, p->new_mode);
1337         }
1338 }
1339
1340 static void show_rename_copy(struct patch *p)
1341 {
1342         const char *renamecopy = p->is_rename ? "rename" : "copy";
1343         const char *old, *new;
1344
1345         /* Find common prefix */
1346         old = p->old_name;
1347         new = p->new_name;
1348         while (1) {
1349                 const char *slash_old, *slash_new;
1350                 slash_old = strchr(old, '/');
1351                 slash_new = strchr(new, '/');
1352                 if (!slash_old ||
1353                     !slash_new ||
1354                     slash_old - old != slash_new - new ||
1355                     memcmp(old, new, slash_new - new))
1356                         break;
1357                 old = slash_old + 1;
1358                 new = slash_new + 1;
1359         }
1360         /* p->old_name thru old is the common prefix, and old and new
1361          * through the end of names are renames
1362          */
1363         if (old != p->old_name)
1364                 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1365                        (int)(old - p->old_name), p->old_name,
1366                        old, new, p->score);
1367         else
1368                 printf(" %s %s => %s (%d%%)\n", renamecopy,
1369                        p->old_name, p->new_name, p->score);
1370         show_mode_change(p, 0);
1371 }
1372
1373 static void summary_patch_list(struct patch *patch)
1374 {
1375         struct patch *p;
1376
1377         for (p = patch; p; p = p->next) {
1378                 if (p->is_new)
1379                         show_file_mode_name("create", p->new_mode, p->new_name);
1380                 else if (p->is_delete)
1381                         show_file_mode_name("delete", p->old_mode, p->old_name);
1382                 else {
1383                         if (p->is_rename || p->is_copy)
1384                                 show_rename_copy(p);
1385                         else {
1386                                 if (p->score) {
1387                                         printf(" rewrite %s (%d%%)\n",
1388                                                p->new_name, p->score);
1389                                         show_mode_change(p, 0);
1390                                 }
1391                                 else
1392                                         show_mode_change(p, 1);
1393                         }
1394                 }
1395         }
1396 }
1397
1398 static void patch_stats(struct patch *patch)
1399 {
1400         int lines = patch->lines_added + patch->lines_deleted;
1401
1402         if (lines > max_change)
1403                 max_change = lines;
1404         if (patch->old_name) {
1405                 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
1406                 if (!len)
1407                         len = strlen(patch->old_name);
1408                 if (len > max_len)
1409                         max_len = len;
1410         }
1411         if (patch->new_name) {
1412                 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
1413                 if (!len)
1414                         len = strlen(patch->new_name);
1415                 if (len > max_len)
1416                         max_len = len;
1417         }
1418 }
1419
1420 static void remove_file(struct patch *patch)
1421 {
1422         if (write_index) {
1423                 if (remove_file_from_cache(patch->old_name) < 0)
1424                         die("unable to remove %s from index", patch->old_name);
1425         }
1426         unlink(patch->old_name);
1427 }
1428
1429 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
1430 {
1431         struct stat st;
1432         struct cache_entry *ce;
1433         int namelen = strlen(path);
1434         unsigned ce_size = cache_entry_size(namelen);
1435
1436         if (!write_index)
1437                 return;
1438
1439         ce = xmalloc(ce_size);
1440         memset(ce, 0, ce_size);
1441         memcpy(ce->name, path, namelen);
1442         ce->ce_mode = create_ce_mode(mode);
1443         ce->ce_flags = htons(namelen);
1444         if (lstat(path, &st) < 0)
1445                 die("unable to stat newly created file %s", path);
1446         fill_stat_cache_info(ce, &st);
1447         if (write_sha1_file(buf, size, "blob", ce->sha1) < 0)
1448                 die("unable to create backing store for newly created file %s", path);
1449         if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
1450                 die("unable to add cache entry for %s", path);
1451 }
1452
1453 static void create_subdirectories(const char *path)
1454 {
1455         int len = strlen(path);
1456         char *buf = xmalloc(len + 1);
1457         const char *slash = path;
1458
1459         while ((slash = strchr(slash+1, '/')) != NULL) {
1460                 len = slash - path;
1461                 memcpy(buf, path, len);
1462                 buf[len] = 0;
1463                 if (mkdir(buf, 0777) < 0) {
1464                         if (errno != EEXIST)
1465                                 break;
1466                 }
1467         }
1468         free(buf);
1469 }
1470
1471 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
1472 {
1473         int fd;
1474
1475         if (S_ISLNK(mode))
1476                 return symlink(buf, path);
1477         fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, (mode & 0100) ? 0777 : 0666);
1478         if (fd < 0)
1479                 return -1;
1480         while (size) {
1481                 int written = write(fd, buf, size);
1482                 if (written < 0) {
1483                         if (errno == EINTR || errno == EAGAIN)
1484                                 continue;
1485                         die("writing file %s: %s", path, strerror(errno));
1486                 }
1487                 if (!written)
1488                         die("out of space writing file %s", path);
1489                 buf += written;
1490                 size -= written;
1491         }
1492         if (close(fd) < 0)
1493                 die("closing file %s: %s", path, strerror(errno));
1494         return 0;
1495 }
1496
1497 /*
1498  * We optimistically assume that the directories exist,
1499  * which is true 99% of the time anyway. If they don't,
1500  * we create them and try again.
1501  */
1502 static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size)
1503 {
1504         if (!try_create_file(path, mode, buf, size))
1505                 return;
1506
1507         if (errno == ENOENT) {
1508                 create_subdirectories(path);
1509                 if (!try_create_file(path, mode, buf, size))
1510                         return;
1511         }
1512
1513         if (errno == EEXIST) {
1514                 unsigned int nr = getpid();
1515
1516                 for (;;) {
1517                         const char *newpath;
1518                         newpath = mkpath("%s~%u", path, nr);
1519                         if (!try_create_file(newpath, mode, buf, size)) {
1520                                 if (!rename(newpath, path))
1521                                         return;
1522                                 unlink(newpath);
1523                                 break;
1524                         }
1525                         if (errno != EEXIST)
1526                                 break;
1527                 }                       
1528         }
1529         die("unable to write file %s mode %o", path, mode);
1530 }
1531
1532 static void create_file(struct patch *patch)
1533 {
1534         const char *path = patch->new_name;
1535         unsigned mode = patch->new_mode;
1536         unsigned long size = patch->resultsize;
1537         char *buf = patch->result;
1538
1539         if (!mode)
1540                 mode = S_IFREG | 0644;
1541         create_one_file(path, mode, buf, size); 
1542         add_index_file(path, mode, buf, size);
1543 }
1544
1545 static void write_out_one_result(struct patch *patch)
1546 {
1547         if (patch->is_delete > 0) {
1548                 remove_file(patch);
1549                 return;
1550         }
1551         if (patch->is_new > 0 || patch->is_copy) {
1552                 create_file(patch);
1553                 return;
1554         }
1555         /*
1556          * Rename or modification boils down to the same
1557          * thing: remove the old, write the new
1558          */
1559         remove_file(patch);
1560         create_file(patch);
1561 }
1562
1563 static void write_out_results(struct patch *list, int skipped_patch)
1564 {
1565         if (!list && !skipped_patch)
1566                 die("No changes");
1567
1568         while (list) {
1569                 write_out_one_result(list);
1570                 list = list->next;
1571         }
1572 }
1573
1574 static struct cache_file cache_file;
1575
1576 static struct excludes {
1577         struct excludes *next;
1578         const char *path;
1579 } *excludes;
1580
1581 static int use_patch(struct patch *p)
1582 {
1583         const char *pathname = p->new_name ? p->new_name : p->old_name;
1584         struct excludes *x = excludes;
1585         while (x) {
1586                 if (fnmatch(x->path, pathname, 0) == 0)
1587                         return 0;
1588                 x = x->next;
1589         }
1590         return 1;
1591 }
1592
1593 static int apply_patch(int fd)
1594 {
1595         int newfd;
1596         unsigned long offset, size;
1597         char *buffer = read_patch_file(fd, &size);
1598         struct patch *list = NULL, **listp = &list;
1599         int skipped_patch = 0;
1600
1601         if (!buffer)
1602                 return -1;
1603         offset = 0;
1604         while (size > 0) {
1605                 struct patch *patch;
1606                 int nr;
1607
1608                 patch = xmalloc(sizeof(*patch));
1609                 memset(patch, 0, sizeof(*patch));
1610                 nr = parse_chunk(buffer + offset, size, patch);
1611                 if (nr < 0)
1612                         break;
1613                 if (use_patch(patch)) {
1614                         patch_stats(patch);
1615                         *listp = patch;
1616                         listp = &patch->next;
1617                 } else {
1618                         /* perhaps free it a bit better? */
1619                         free(patch);
1620                         skipped_patch++;
1621                 }
1622                 offset += nr;
1623                 size -= nr;
1624         }
1625
1626         newfd = -1;
1627         write_index = check_index && apply;
1628         if (write_index)
1629                 newfd = hold_index_file_for_update(&cache_file, get_index_file());
1630         if (check_index) {
1631                 if (read_cache() < 0)
1632                         die("unable to read index file");
1633         }
1634
1635         if ((check || apply) && check_patch_list(list) < 0)
1636                 exit(1);
1637
1638         if (apply)
1639                 write_out_results(list, skipped_patch);
1640
1641         if (write_index) {
1642                 if (write_cache(newfd, active_cache, active_nr) ||
1643                     commit_index_file(&cache_file))
1644                         die("Unable to write new cachefile");
1645         }
1646
1647         if (show_index_info)
1648                 show_index_list(list);
1649
1650         if (diffstat)
1651                 stat_patch_list(list);
1652
1653         if (summary)
1654                 summary_patch_list(list);
1655
1656         free(buffer);
1657         return 0;
1658 }
1659
1660 int main(int argc, char **argv)
1661 {
1662         int i;
1663         int read_stdin = 1;
1664
1665         for (i = 1; i < argc; i++) {
1666                 const char *arg = argv[i];
1667                 int fd;
1668
1669                 if (!strcmp(arg, "-")) {
1670                         apply_patch(0);
1671                         read_stdin = 0;
1672                         continue;
1673                 }
1674                 if (!strncmp(arg, "--exclude=", 10)) {
1675                         struct excludes *x = xmalloc(sizeof(*x));
1676                         x->path = arg + 10;
1677                         x->next = excludes;
1678                         excludes = x;
1679                         continue;
1680                 }
1681                 if (!strcmp(arg, "--stat")) {
1682                         apply = 0;
1683                         diffstat = 1;
1684                         continue;
1685                 }
1686                 if (!strcmp(arg, "--summary")) {
1687                         apply = 0;
1688                         summary = 1;
1689                         continue;
1690                 }
1691                 if (!strcmp(arg, "--check")) {
1692                         apply = 0;
1693                         check = 1;
1694                         continue;
1695                 }
1696                 if (!strcmp(arg, "--index")) {
1697                         check_index = 1;
1698                         continue;
1699                 }
1700                 if (!strcmp(arg, "--apply")) {
1701                         apply = 1;
1702                         continue;
1703                 }
1704                 if (!strcmp(arg, "--index-info")) {
1705                         apply = 0;
1706                         show_index_info = 1;
1707                         continue;
1708                 }
1709                 if (!strcmp(arg, "-z")) {
1710                         line_termination = 0;
1711                         continue;
1712                 }
1713                 fd = open(arg, O_RDONLY);
1714                 if (fd < 0)
1715                         usage(apply_usage);
1716                 read_stdin = 0;
1717                 apply_patch(fd);
1718                 close(fd);
1719         }
1720         if (read_stdin)
1721                 apply_patch(0);
1722         return 0;
1723 }