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