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