Show original and resulting blob object info in diff output.
[git/git.git] / apply.c
CommitLineData
c1bb9350
LT
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 *
c1bb9350
LT
8 */
9#include <ctype.h>
d854f783 10#include <fnmatch.h>
c1bb9350
LT
11#include "cache.h"
12
a577284a
LT
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//
3cca928d 18static int check_index = 0;
5aa7d94c 19static int write_index = 0;
fab2c257 20static int diffstat = 0;
96c912a4 21static int summary = 0;
a577284a
LT
22static int check = 0;
23static int apply = 1;
24static int show_files = 0;
12dd6e8c 25static const char apply_usage[] =
c2d5036d 26"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] <patch>...";
c1bb9350 27
3f40315a
LT
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 */
33static int max_change, max_len;
34
a4acb0eb
LT
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 */
46979f56 40static int linenr = 1;
19c58fb8
LT
41
42struct fragment {
43 unsigned long oldpos, oldlines;
44 unsigned long newpos, newlines;
45 const char *patch;
46 int size;
47 struct fragment *next;
48};
49
50struct patch {
5041aa70 51 char *new_name, *old_name, *def_name;
19c58fb8
LT
52 unsigned int old_mode, new_mode;
53 int is_rename, is_copy, is_new, is_delete;
3f40315a 54 int lines_added, lines_deleted;
96c912a4 55 int score;
19c58fb8 56 struct fragment *fragments;
5aa7d94c 57 char *result;
3cca928d 58 unsigned long resultsize;
19c58fb8
LT
59 struct patch *next;
60};
46979f56 61
c1bb9350 62#define CHUNKSIZE (8192)
a4acb0eb 63#define SLOP (16)
c1bb9350
LT
64
65static 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;
a4acb0eb
LT
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);
c1bb9350
LT
97 return buffer;
98}
99
3cca928d 100static unsigned long linelen(const char *buffer, unsigned long size)
c1bb9350
LT
101{
102 unsigned long len = 0;
103 while (size--) {
104 len++;
105 if (*buffer++ == '\n')
106 break;
107 }
108 return len;
109}
110
a4acb0eb
LT
111static int is_dev_null(const char *str)
112{
113 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
114}
115
381ca9a3
LT
116#define TERM_SPACE 1
117#define TERM_TAB 2
9a4a100e
LT
118
119static 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
9a4a100e
LT
126 return 1;
127}
128
129static char * find_name(const char *line, char *def, int p_value, int terminate)
c1bb9350 130{
a4acb0eb
LT
131 int len;
132 const char *start = line;
133 char *name;
134
c1bb9350 135 for (;;) {
a4acb0eb 136 char c = *line;
9a4a100e
LT
137
138 if (isspace(c)) {
139 if (c == '\n')
140 break;
141 if (name_terminate(start, line-start, c, terminate))
142 break;
143 }
a4acb0eb
LT
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;
c1bb9350 164 }
a4acb0eb
LT
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.
9a4a100e
LT
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.
a4acb0eb 181 */
19c58fb8 182static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
a4acb0eb
LT
183{
184 int p_value = 1;
185 char *name;
186
187 first += 4; // skip "--- "
188 second += 4; // skip "+++ "
189 if (is_dev_null(first)) {
19c58fb8
LT
190 patch->is_new = 1;
191 patch->is_delete = 0;
5041aa70 192 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
19c58fb8 193 patch->new_name = name;
a4acb0eb 194 } else if (is_dev_null(second)) {
19c58fb8
LT
195 patch->is_new = 0;
196 patch->is_delete = 1;
381ca9a3 197 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
19c58fb8 198 patch->old_name = name;
a4acb0eb 199 } else {
381ca9a3
LT
200 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
201 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
19c58fb8 202 patch->old_name = patch->new_name = name;
a4acb0eb
LT
203 }
204 if (!name)
205 die("unable to find filename in patch at line %d", linenr);
a4acb0eb
LT
206}
207
19c58fb8 208static int gitdiff_hdrend(const char *line, struct patch *patch)
a4acb0eb
LT
209{
210 return -1;
211}
212
1e3f6b6e
LT
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 */
222static 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;
248absolute_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
19c58fb8 257static int gitdiff_oldname(const char *line, struct patch *patch)
a4acb0eb 258{
19c58fb8 259 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
a4acb0eb
LT
260 return 0;
261}
262
19c58fb8 263static int gitdiff_newname(const char *line, struct patch *patch)
a4acb0eb 264{
19c58fb8 265 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
a4acb0eb
LT
266 return 0;
267}
268
19c58fb8 269static int gitdiff_oldmode(const char *line, struct patch *patch)
a4acb0eb 270{
19c58fb8 271 patch->old_mode = strtoul(line, NULL, 8);
a4acb0eb
LT
272 return 0;
273}
274
19c58fb8 275static int gitdiff_newmode(const char *line, struct patch *patch)
a4acb0eb 276{
19c58fb8 277 patch->new_mode = strtoul(line, NULL, 8);
a4acb0eb
LT
278 return 0;
279}
280
19c58fb8 281static int gitdiff_delete(const char *line, struct patch *patch)
a4acb0eb 282{
19c58fb8 283 patch->is_delete = 1;
5041aa70 284 patch->old_name = patch->def_name;
19c58fb8 285 return gitdiff_oldmode(line, patch);
a4acb0eb
LT
286}
287
19c58fb8 288static int gitdiff_newfile(const char *line, struct patch *patch)
a4acb0eb 289{
19c58fb8 290 patch->is_new = 1;
5041aa70 291 patch->new_name = patch->def_name;
19c58fb8 292 return gitdiff_newmode(line, patch);
a4acb0eb
LT
293}
294
19c58fb8 295static int gitdiff_copysrc(const char *line, struct patch *patch)
a4acb0eb 296{
19c58fb8
LT
297 patch->is_copy = 1;
298 patch->old_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
299 return 0;
300}
301
19c58fb8 302static int gitdiff_copydst(const char *line, struct patch *patch)
a4acb0eb 303{
19c58fb8
LT
304 patch->is_copy = 1;
305 patch->new_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
306 return 0;
307}
308
19c58fb8 309static int gitdiff_renamesrc(const char *line, struct patch *patch)
a4acb0eb 310{
19c58fb8
LT
311 patch->is_rename = 1;
312 patch->old_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
313 return 0;
314}
315
19c58fb8 316static int gitdiff_renamedst(const char *line, struct patch *patch)
a4acb0eb 317{
19c58fb8
LT
318 patch->is_rename = 1;
319 patch->new_name = find_name(line, NULL, 0, 0);
a4acb0eb
LT
320 return 0;
321}
322
19c58fb8 323static int gitdiff_similarity(const char *line, struct patch *patch)
a4acb0eb 324{
96c912a4
JH
325 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
326 patch->score = 0;
a4acb0eb 327 return 0;
c1bb9350
LT
328}
329
70aadac0
JH
330static int gitdiff_dissimilarity(const char *line, struct patch *patch)
331{
96c912a4
JH
332 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
333 patch->score = 0;
70aadac0
JH
334 return 0;
335}
336
9a4a100e
LT
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 */
19c58fb8 341static int gitdiff_unrecognized(const char *line, struct patch *patch)
9a4a100e
LT
342{
343 return -1;
344}
345
5041aa70
LT
346static 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':
e70a165d 380 return NULL;
5041aa70
LT
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 }
0e87e048 390 if (second[len] == '\n' && !memcmp(name, second, len)) {
5041aa70
LT
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
c1bb9350 401/* Verify that we recognize the lines following a git header */
19c58fb8 402static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
c1bb9350 403{
a4acb0eb
LT
404 unsigned long offset;
405
406 /* A git diff has explicit new/delete information, so we don't guess */
19c58fb8
LT
407 patch->is_new = 0;
408 patch->is_delete = 0;
a4acb0eb 409
5041aa70
LT
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
a4acb0eb
LT
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;
19c58fb8 424 int (*fn)(const char *, struct patch *);
a4acb0eb
LT
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 },
33f4d087
LT
435 { "rename old ", gitdiff_renamesrc },
436 { "rename new ", gitdiff_renamedst },
dc938417
LT
437 { "rename from ", gitdiff_renamesrc },
438 { "rename to ", gitdiff_renamedst },
a4acb0eb 439 { "similarity index ", gitdiff_similarity },
70aadac0 440 { "dissimilarity index ", gitdiff_dissimilarity },
9a4a100e 441 { "", gitdiff_unrecognized },
a4acb0eb
LT
442 };
443 int i;
c1bb9350 444
c1bb9350 445 len = linelen(line, size);
a4acb0eb 446 if (!len || line[len-1] != '\n')
c1bb9350 447 break;
a4acb0eb
LT
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;
19c58fb8 453 if (p->fn(line + oplen, patch) < 0)
a4acb0eb 454 return offset;
9a4a100e 455 break;
a4acb0eb 456 }
c1bb9350
LT
457 }
458
a4acb0eb 459 return offset;
c1bb9350
LT
460}
461
fab2c257 462static int parse_num(const char *line, unsigned long *p)
46979f56
LT
463{
464 char *ptr;
fab2c257
LT
465
466 if (!isdigit(*line))
467 return 0;
468 *p = strtoul(line, &ptr, 10);
469 return ptr - line;
470}
471
472static int parse_range(const char *line, int len, int offset, const char *expect,
473 unsigned long *p1, unsigned long *p2)
474{
46979f56
LT
475 int digits, ex;
476
477 if (offset < 0 || offset >= len)
478 return -1;
479 line += offset;
480 len -= offset;
481
fab2c257
LT
482 digits = parse_num(line, p1);
483 if (!digits)
46979f56 484 return -1;
46979f56
LT
485
486 offset += digits;
487 line += digits;
488 len -= digits;
489
fab2c257
LT
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
46979f56
LT
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 */
19c58fb8 514static int parse_fragment_header(char *line, int len, struct fragment *fragment)
46979f56
LT
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 */
fab2c257
LT
522 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
523 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
46979f56
LT
524
525 return offset;
526}
527
19c58fb8 528static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
c1bb9350
LT
529{
530 unsigned long offset, len;
531
19c58fb8
LT
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;
46979f56 536 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
c1bb9350
LT
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;
46979f56
LT
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)) {
19c58fb8
LT
553 struct fragment dummy;
554 if (parse_fragment_header(line, len, &dummy) < 0)
46979f56 555 continue;
4ec99bf0 556 error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line);
46979f56
LT
557 }
558
c1bb9350
LT
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)) {
19c58fb8 567 int git_hdr_len = parse_git_header(line, len, size, patch);
206de27e 568 if (git_hdr_len <= len)
c1bb9350 569 continue;
b7e8039a
LT
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 }
a4acb0eb 575 *hdrsize = git_hdr_len;
c1bb9350
LT
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 */
19c58fb8 593 parse_traditional_patch(line, line+len, patch);
c1bb9350 594 *hdrsize = len + nextlen;
46979f56 595 linenr += 2;
c1bb9350
LT
596 return offset;
597 }
598 return -1;
599}
600
c1bb9350
LT
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 */
19c58fb8 608static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
c1bb9350 609{
3f40315a 610 int added, deleted;
c1bb9350 611 int len = linelen(line, size), offset;
30996652 612 unsigned long oldlines, newlines;
c1bb9350 613
19c58fb8 614 offset = parse_fragment_header(line, len, fragment);
c1bb9350
LT
615 if (offset < 0)
616 return -1;
19c58fb8
LT
617 oldlines = fragment->oldlines;
618 newlines = fragment->newlines;
c1bb9350 619
30996652
LT
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");
af3f929f
LT
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 }
a4acb0eb 638
c1bb9350
LT
639 /* Parse the thing.. */
640 line += len;
641 size -= len;
46979f56 642 linenr++;
3f40315a 643 added = deleted = 0;
46979f56 644 for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
c1bb9350
LT
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 '-':
3f40315a 658 deleted++;
c1bb9350
LT
659 oldlines--;
660 break;
661 case '+':
3f40315a 662 added++;
c1bb9350
LT
663 newlines--;
664 break;
433ef8a2
FK
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
56d33b11
JH
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 */
fab2c257 673 case '\\':
433ef8a2 674 if (len < 12 || memcmp(line, "\\ ", 2))
3cca928d 675 return -1;
fab2c257 676 break;
c1bb9350
LT
677 }
678 }
8b64647d
JH
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 */
433ef8a2 683 if (12 < size && !memcmp(line, "\\ ", 2))
8b64647d
JH
684 offset += linelen(line, size);
685
3f40315a
LT
686 patch->lines_added += added;
687 patch->lines_deleted += deleted;
c1bb9350
LT
688 return offset;
689}
690
19c58fb8 691static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
c1bb9350
LT
692{
693 unsigned long offset = 0;
19c58fb8 694 struct fragment **fragp = &patch->fragments;
c1bb9350
LT
695
696 while (size > 4 && !memcmp(line, "@@ -", 4)) {
19c58fb8
LT
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);
c1bb9350 703 if (len <= 0)
46979f56 704 die("corrupt patch at line %d", linenr);
c1bb9350 705
19c58fb8
LT
706 fragment->patch = line;
707 fragment->size = len;
708
709 *fragp = fragment;
710 fragp = &fragment->next;
c1bb9350
LT
711
712 offset += len;
713 line += len;
714 size -= len;
715 }
716 return offset;
717}
718
1fea629f
LT
719static 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
19c58fb8 729static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
c1bb9350
LT
730{
731 int hdrsize, patchsize;
19c58fb8 732 int offset = find_header(buffer, size, &hdrsize, patch);
c1bb9350
LT
733
734 if (offset < 0)
735 return offset;
c1bb9350 736
19c58fb8 737 patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
c1bb9350 738
1fea629f
LT
739 if (!patchsize && !metadata_changes(patch))
740 die("patch with only garbage at line %d", linenr);
741
c1bb9350
LT
742 return offset + hdrsize + patchsize;
743}
744
6da4016a
LT
745static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
746static const char minuses[]= "----------------------------------------------------------------------";
3f40315a
LT
747
748static void show_stats(struct patch *patch)
749{
62917097 750 const char *prefix = "";
03b4538b 751 char *name = patch->new_name;
95bedc9e 752 int len, max, add, del, total;
3f40315a 753
5041aa70 754 if (!name)
03b4538b 755 name = patch->old_name;
3f40315a
LT
756
757 /*
758 * "scale" the filename
759 */
760 len = strlen(name);
761 max = max_len;
762 if (max > 50)
763 max = 50;
62917097
LT
764 if (len > max) {
765 char *slash;
766 prefix = "...";
767 max -= 3;
3f40315a 768 name += len - max;
62917097
LT
769 slash = strchr(name, '/');
770 if (slash)
771 name = slash;
772 }
3f40315a
LT
773 len = max;
774
775 /*
776 * scale the add/delete
777 */
778 max = max_change;
779 if (max + len > 70)
780 max = 70 - len;
95bedc9e
LT
781
782 add = patch->lines_added;
783 del = patch->lines_deleted;
784 total = add + del;
785
69f956e1
SV
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 }
62917097 791 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
3f40315a
LT
792 len, name, patch->lines_added + patch->lines_deleted,
793 add, pluses, del, minuses);
794}
795
3cca928d
LT
796static 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
828static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
829{
6e7c92a9
LT
830 int i;
831 unsigned long start, backwards, forwards;
3cca928d
LT
832
833 if (fragsize > size)
834 return -1;
835
836 start = 0;
837 if (line > 1) {
3cca928d 838 unsigned long offset = 0;
6e7c92a9
LT
839 i = line-1;
840 while (offset + fragsize <= size) {
3cca928d
LT
841 if (buf[offset++] == '\n') {
842 start = offset;
6e7c92a9 843 if (!--i)
3cca928d
LT
844 break;
845 }
846 }
847 }
848
849 /* Exact line number? */
850 if (!memcmp(buf + start, fragment, fragsize))
851 return start;
852
6e7c92a9
LT
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;
6e7c92a9
LT
889 return try;
890 }
891
3cca928d
LT
892 /*
893 * We should start searching forward and backward.
894 */
895 return -1;
896}
897
6e7c92a9
LT
898struct buffer_desc {
899 char *buffer;
900 unsigned long size;
901 unsigned long alloc;
902};
903
904static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
3cca928d 905{
6e7c92a9 906 char *buf = desc->buffer;
3cca928d
LT
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;
8b64647d 928 if (len < size && patch[len] == '\\')
3cca928d
LT
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
6e7c92a9 952 offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
3cca928d 953 if (offset >= 0) {
6e7c92a9
LT
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);
3cca928d
LT
967 offset = 0;
968 }
969
970 free(old);
971 free(new);
972 return offset;
973}
974
6e7c92a9 975static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
3cca928d
LT
976{
977 struct fragment *frag = patch->fragments;
978
979 while (frag) {
6e7c92a9 980 if (apply_one_fragment(desc, frag) < 0)
4ec99bf0 981 return error("patch failed: %s:%ld", patch->old_name, frag->oldpos);
3cca928d
LT
982 frag = frag->next;
983 }
30996652 984 return 0;
3cca928d
LT
985}
986
987static int apply_data(struct patch *patch, struct stat *st)
988{
6e7c92a9
LT
989 char *buf;
990 unsigned long size, alloc;
991 struct buffer_desc desc;
3cca928d 992
30996652
LT
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 }
6e7c92a9
LT
1003
1004 desc.size = size;
1005 desc.alloc = alloc;
1006 desc.buffer = buf;
1007 if (apply_fragments(&desc, patch) < 0)
3cca928d 1008 return -1;
6e7c92a9
LT
1009 patch->result = desc.buffer;
1010 patch->resultsize = desc.size;
5aa7d94c
LT
1011
1012 if (patch->is_delete && patch->resultsize)
1013 return error("removal patch leaves file contents");
1014
3cca928d
LT
1015 return 0;
1016}
1017
a577284a 1018static int check_patch(struct patch *patch)
fab2c257 1019{
a577284a 1020 struct stat st;
fab2c257
LT
1021 const char *old_name = patch->old_name;
1022 const char *new_name = patch->new_name;
1023
1024 if (old_name) {
a577284a 1025 int changed;
56d33b11 1026 int stat_ret = lstat(old_name, &st);
a577284a 1027
3cca928d
LT
1028 if (check_index) {
1029 int pos = cache_name_pos(old_name, strlen(old_name));
1030 if (pos < 0)
56d33b11
JH
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
3cca928d
LT
1051 changed = ce_match_stat(active_cache[pos], &st);
1052 if (changed)
56d33b11
JH
1053 return error("%s: does not match index",
1054 old_name);
3cca928d 1055 }
56d33b11
JH
1056 else if (stat_ret < 0)
1057 return error("%s: %s", old_name, strerror(errno));
1058
3cca928d
LT
1059 if (patch->is_new < 0)
1060 patch->is_new = 0;
de4971b5 1061 st.st_mode = ntohl(create_ce_mode(st.st_mode));
a577284a
LT
1062 if (!patch->old_mode)
1063 patch->old_mode = st.st_mode;
3cca928d
LT
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);
fab2c257 1069 }
a577284a 1070
fab2c257 1071 if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
3cca928d 1072 if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
a577284a
LT
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));
35cc4bcd
JS
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 }
fab2c257 1084 }
3cca928d
LT
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);
a577284a 1098 return 0;
fab2c257
LT
1099}
1100
a577284a 1101static int check_patch_list(struct patch *patch)
19c58fb8 1102{
a577284a
LT
1103 int error = 0;
1104
1105 for (;patch ; patch = patch->next)
1106 error |= check_patch(patch);
1107 return error;
1108}
1109
1110static void show_file(int c, unsigned int mode, const char *name)
1111{
1112 printf("%c %o %s\n", c, mode, name);
1113}
3f40315a 1114
a577284a
LT
1115static 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;
19c58fb8 1134 }
a577284a
LT
1135 printf("M %o %s\n", patch->old_mode, patch->old_name);
1136 }
1137}
fab2c257 1138
a577284a
LT
1139static 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);
3f40315a
LT
1151}
1152
96c912a4
JH
1153static 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
1161static 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
1173static 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,
e30e814d 1198 (int)(old - p->old_name), p->old_name,
96c912a4
JH
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
1206static 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
3f40315a
LT
1231static 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 }
19c58fb8
LT
1247}
1248
5aa7d94c
LT
1249static 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
1258static 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
5c8af185
LT
1282static 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;
f312de01 1292 if (mkdir(buf, 0777) < 0) {
5c8af185
LT
1293 if (errno != EEXIST)
1294 break;
1295 }
1296 }
1297 free(buf);
1298}
1299
1b668341
LT
1300static 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
5c8af185
LT
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 */
1b668341 1331static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size)
5c8af185 1332{
1b668341
LT
1333 if (!try_create_file(path, mode, buf, size))
1334 return;
5c8af185 1335
1b668341 1336 if (errno == ENOENT) {
5c8af185 1337 create_subdirectories(path);
1b668341
LT
1338 if (!try_create_file(path, mode, buf, size))
1339 return;
5c8af185 1340 }
5c8af185 1341
1b668341
LT
1342 if (errno == EEXIST) {
1343 unsigned int nr = getpid();
5c8af185 1344
1b668341
LT
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 }
5c8af185 1357 }
1b668341 1358 die("unable to write file %s mode %o", path, mode);
5c8af185
LT
1359}
1360
5aa7d94c
LT
1361static 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;
1b668341
LT
1370 create_one_file(path, mode, buf, size);
1371 add_index_file(path, mode, buf, size);
5aa7d94c
LT
1372}
1373
1374static 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
d854f783 1392static void write_out_results(struct patch *list, int skipped_patch)
5aa7d94c 1393{
d854f783 1394 if (!list && !skipped_patch)
f7b79707
LT
1395 die("No changes");
1396
5aa7d94c
LT
1397 while (list) {
1398 write_out_one_result(list);
1399 list = list->next;
1400 }
1401}
1402
1403static struct cache_file cache_file;
1404
d854f783
JH
1405static struct excludes {
1406 struct excludes *next;
1407 const char *path;
1408} *excludes;
1409
1410static int use_patch(struct patch *p)
1411{
c7c81b3a 1412 const char *pathname = p->new_name ? p->new_name : p->old_name;
d854f783
JH
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
c1bb9350
LT
1422static int apply_patch(int fd)
1423{
5aa7d94c 1424 int newfd;
c1bb9350
LT
1425 unsigned long offset, size;
1426 char *buffer = read_patch_file(fd, &size);
19c58fb8 1427 struct patch *list = NULL, **listp = &list;
d854f783 1428 int skipped_patch = 0;
c1bb9350
LT
1429
1430 if (!buffer)
1431 return -1;
1432 offset = 0;
1433 while (size > 0) {
19c58fb8
LT
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);
c1bb9350
LT
1440 if (nr < 0)
1441 break;
d854f783
JH
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 }
c1bb9350
LT
1451 offset += nr;
1452 size -= nr;
1453 }
19c58fb8 1454
5aa7d94c
LT
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
a577284a
LT
1464 if ((check || apply) && check_patch_list(list) < 0)
1465 exit(1);
1466
5aa7d94c 1467 if (apply)
d854f783 1468 write_out_results(list, skipped_patch);
5aa7d94c
LT
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
a577284a
LT
1476 if (show_files)
1477 show_file_list(list);
1478
1479 if (diffstat)
1480 stat_patch_list(list);
19c58fb8 1481
96c912a4
JH
1482 if (summary)
1483 summary_patch_list(list);
1484
c1bb9350
LT
1485 free(buffer);
1486 return 0;
1487}
1488
1489int main(int argc, char **argv)
1490{
1491 int i;
4dfdbe10 1492 int read_stdin = 1;
c1bb9350 1493
c1bb9350
LT
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);
4dfdbe10 1500 read_stdin = 0;
c1bb9350
LT
1501 continue;
1502 }
d854f783
JH
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 }
fab2c257 1510 if (!strcmp(arg, "--stat")) {
a577284a 1511 apply = 0;
fab2c257
LT
1512 diffstat = 1;
1513 continue;
1514 }
96c912a4
JH
1515 if (!strcmp(arg, "--summary")) {
1516 apply = 0;
1517 summary = 1;
1518 continue;
1519 }
a577284a
LT
1520 if (!strcmp(arg, "--check")) {
1521 apply = 0;
1522 check = 1;
1523 continue;
1524 }
3cca928d
LT
1525 if (!strcmp(arg, "--index")) {
1526 check_index = 1;
1527 continue;
1528 }
aefa4a5b
LT
1529 if (!strcmp(arg, "--apply")) {
1530 apply = 1;
1531 continue;
1532 }
a577284a
LT
1533 if (!strcmp(arg, "--show-files")) {
1534 show_files = 1;
1535 continue;
1536 }
c1bb9350
LT
1537 fd = open(arg, O_RDONLY);
1538 if (fd < 0)
1539 usage(apply_usage);
4dfdbe10 1540 read_stdin = 0;
c1bb9350
LT
1541 apply_patch(fd);
1542 close(fd);
1543 }
4dfdbe10
LT
1544 if (read_stdin)
1545 apply_patch(0);
c1bb9350
LT
1546 return 0;
1547}