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