parse-options: simplify usage argh handling
[git/git.git] / builtin-apply.c
1 /*
2 * apply.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This applies patches on top of some (arbitrary) version of the SCM.
7 *
8 */
9 #include "cache.h"
10 #include "cache-tree.h"
11 #include "quote.h"
12 #include "blob.h"
13 #include "delta.h"
14 #include "builtin.h"
15 #include "string-list.h"
16 #include "dir.h"
17 #include "parse-options.h"
18
19 /*
20 * --check turns on checking that the working tree matches the
21 * files that are being modified, but doesn't apply the patch
22 * --stat does just a diffstat, and doesn't actually apply
23 * --numstat does numeric diffstat, and doesn't actually apply
24 * --index-info shows the old and new index info for paths if available.
25 * --index updates the cache as well.
26 * --cached updates only the cache without ever touching the working tree.
27 */
28 static const char *prefix;
29 static int prefix_length = -1;
30 static int newfd = -1;
31
32 static int unidiff_zero;
33 static int p_value = 1;
34 static int p_value_known;
35 static int check_index;
36 static int update_index;
37 static int cached;
38 static int diffstat;
39 static int numstat;
40 static int summary;
41 static int check;
42 static int apply = 1;
43 static int apply_in_reverse;
44 static int apply_with_reject;
45 static int apply_verbosely;
46 static int no_add;
47 static const char *fake_ancestor;
48 static int line_termination = '\n';
49 static unsigned int p_context = UINT_MAX;
50 static const char * const apply_usage[] = {
51 "git apply [options] [<patch>...]",
52 NULL
53 };
54
55 static enum ws_error_action {
56 nowarn_ws_error,
57 warn_on_ws_error,
58 die_on_ws_error,
59 correct_ws_error,
60 } ws_error_action = warn_on_ws_error;
61 static int whitespace_error;
62 static int squelch_whitespace_errors = 5;
63 static int applied_after_fixing_ws;
64 static const char *patch_input_file;
65 static const char *root;
66 static int root_len;
67 static int read_stdin = 1;
68 static int options;
69
70 static void parse_whitespace_option(const char *option)
71 {
72 if (!option) {
73 ws_error_action = warn_on_ws_error;
74 return;
75 }
76 if (!strcmp(option, "warn")) {
77 ws_error_action = warn_on_ws_error;
78 return;
79 }
80 if (!strcmp(option, "nowarn")) {
81 ws_error_action = nowarn_ws_error;
82 return;
83 }
84 if (!strcmp(option, "error")) {
85 ws_error_action = die_on_ws_error;
86 return;
87 }
88 if (!strcmp(option, "error-all")) {
89 ws_error_action = die_on_ws_error;
90 squelch_whitespace_errors = 0;
91 return;
92 }
93 if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
94 ws_error_action = correct_ws_error;
95 return;
96 }
97 die("unrecognized whitespace option '%s'", option);
98 }
99
100 static void set_default_whitespace_mode(const char *whitespace_option)
101 {
102 if (!whitespace_option && !apply_default_whitespace)
103 ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
104 }
105
106 /*
107 * For "diff-stat" like behaviour, we keep track of the biggest change
108 * we've seen, and the longest filename. That allows us to do simple
109 * scaling.
110 */
111 static int max_change, max_len;
112
113 /*
114 * Various "current state", notably line numbers and what
115 * file (and how) we're patching right now.. The "is_xxxx"
116 * things are flags, where -1 means "don't know yet".
117 */
118 static int linenr = 1;
119
120 /*
121 * This represents one "hunk" from a patch, starting with
122 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
123 * patch text is pointed at by patch, and its byte length
124 * is stored in size. leading and trailing are the number
125 * of context lines.
126 */
127 struct fragment {
128 unsigned long leading, trailing;
129 unsigned long oldpos, oldlines;
130 unsigned long newpos, newlines;
131 const char *patch;
132 int size;
133 int rejected;
134 struct fragment *next;
135 };
136
137 /*
138 * When dealing with a binary patch, we reuse "leading" field
139 * to store the type of the binary hunk, either deflated "delta"
140 * or deflated "literal".
141 */
142 #define binary_patch_method leading
143 #define BINARY_DELTA_DEFLATED 1
144 #define BINARY_LITERAL_DEFLATED 2
145
146 /*
147 * This represents a "patch" to a file, both metainfo changes
148 * such as creation/deletion, filemode and content changes represented
149 * as a series of fragments.
150 */
151 struct patch {
152 char *new_name, *old_name, *def_name;
153 unsigned int old_mode, new_mode;
154 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
155 int rejected;
156 unsigned ws_rule;
157 unsigned long deflate_origlen;
158 int lines_added, lines_deleted;
159 int score;
160 unsigned int is_toplevel_relative:1;
161 unsigned int inaccurate_eof:1;
162 unsigned int is_binary:1;
163 unsigned int is_copy:1;
164 unsigned int is_rename:1;
165 unsigned int recount:1;
166 struct fragment *fragments;
167 char *result;
168 size_t resultsize;
169 char old_sha1_prefix[41];
170 char new_sha1_prefix[41];
171 struct patch *next;
172 };
173
174 /*
175 * A line in a file, len-bytes long (includes the terminating LF,
176 * except for an incomplete line at the end if the file ends with
177 * one), and its contents hashes to 'hash'.
178 */
179 struct line {
180 size_t len;
181 unsigned hash : 24;
182 unsigned flag : 8;
183 #define LINE_COMMON 1
184 };
185
186 /*
187 * This represents a "file", which is an array of "lines".
188 */
189 struct image {
190 char *buf;
191 size_t len;
192 size_t nr;
193 size_t alloc;
194 struct line *line_allocated;
195 struct line *line;
196 };
197
198 /*
199 * Records filenames that have been touched, in order to handle
200 * the case where more than one patches touch the same file.
201 */
202
203 static struct string_list fn_table;
204
205 static uint32_t hash_line(const char *cp, size_t len)
206 {
207 size_t i;
208 uint32_t h;
209 for (i = 0, h = 0; i < len; i++) {
210 if (!isspace(cp[i])) {
211 h = h * 3 + (cp[i] & 0xff);
212 }
213 }
214 return h;
215 }
216
217 static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
218 {
219 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
220 img->line_allocated[img->nr].len = len;
221 img->line_allocated[img->nr].hash = hash_line(bol, len);
222 img->line_allocated[img->nr].flag = flag;
223 img->nr++;
224 }
225
226 static void prepare_image(struct image *image, char *buf, size_t len,
227 int prepare_linetable)
228 {
229 const char *cp, *ep;
230
231 memset(image, 0, sizeof(*image));
232 image->buf = buf;
233 image->len = len;
234
235 if (!prepare_linetable)
236 return;
237
238 ep = image->buf + image->len;
239 cp = image->buf;
240 while (cp < ep) {
241 const char *next;
242 for (next = cp; next < ep && *next != '\n'; next++)
243 ;
244 if (next < ep)
245 next++;
246 add_line_info(image, cp, next - cp, 0);
247 cp = next;
248 }
249 image->line = image->line_allocated;
250 }
251
252 static void clear_image(struct image *image)
253 {
254 free(image->buf);
255 image->buf = NULL;
256 image->len = 0;
257 }
258
259 static void say_patch_name(FILE *output, const char *pre,
260 struct patch *patch, const char *post)
261 {
262 fputs(pre, output);
263 if (patch->old_name && patch->new_name &&
264 strcmp(patch->old_name, patch->new_name)) {
265 quote_c_style(patch->old_name, NULL, output, 0);
266 fputs(" => ", output);
267 quote_c_style(patch->new_name, NULL, output, 0);
268 } else {
269 const char *n = patch->new_name;
270 if (!n)
271 n = patch->old_name;
272 quote_c_style(n, NULL, output, 0);
273 }
274 fputs(post, output);
275 }
276
277 #define CHUNKSIZE (8192)
278 #define SLOP (16)
279
280 static void read_patch_file(struct strbuf *sb, int fd)
281 {
282 if (strbuf_read(sb, fd, 0) < 0)
283 die("git apply: read returned %s", strerror(errno));
284
285 /*
286 * Make sure that we have some slop in the buffer
287 * so that we can do speculative "memcmp" etc, and
288 * see to it that it is NUL-filled.
289 */
290 strbuf_grow(sb, SLOP);
291 memset(sb->buf + sb->len, 0, SLOP);
292 }
293
294 static unsigned long linelen(const char *buffer, unsigned long size)
295 {
296 unsigned long len = 0;
297 while (size--) {
298 len++;
299 if (*buffer++ == '\n')
300 break;
301 }
302 return len;
303 }
304
305 static int is_dev_null(const char *str)
306 {
307 return !memcmp("/dev/null", str, 9) && isspace(str[9]);
308 }
309
310 #define TERM_SPACE 1
311 #define TERM_TAB 2
312
313 static int name_terminate(const char *name, int namelen, int c, int terminate)
314 {
315 if (c == ' ' && !(terminate & TERM_SPACE))
316 return 0;
317 if (c == '\t' && !(terminate & TERM_TAB))
318 return 0;
319
320 return 1;
321 }
322
323 /* remove double slashes to make --index work with such filenames */
324 static char *squash_slash(char *name)
325 {
326 int i = 0, j = 0;
327
328 while (name[i]) {
329 if ((name[j++] = name[i++]) == '/')
330 while (name[i] == '/')
331 i++;
332 }
333 name[j] = '\0';
334 return name;
335 }
336
337 static char *find_name(const char *line, char *def, int p_value, int terminate)
338 {
339 int len;
340 const char *start = line;
341
342 if (*line == '"') {
343 struct strbuf name = STRBUF_INIT;
344
345 /*
346 * Proposed "new-style" GNU patch/diff format; see
347 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
348 */
349 if (!unquote_c_style(&name, line, NULL)) {
350 char *cp;
351
352 for (cp = name.buf; p_value; p_value--) {
353 cp = strchr(cp, '/');
354 if (!cp)
355 break;
356 cp++;
357 }
358 if (cp) {
359 /* name can later be freed, so we need
360 * to memmove, not just return cp
361 */
362 strbuf_remove(&name, 0, cp - name.buf);
363 free(def);
364 if (root)
365 strbuf_insert(&name, 0, root, root_len);
366 return squash_slash(strbuf_detach(&name, NULL));
367 }
368 }
369 strbuf_release(&name);
370 }
371
372 for (;;) {
373 char c = *line;
374
375 if (isspace(c)) {
376 if (c == '\n')
377 break;
378 if (name_terminate(start, line-start, c, terminate))
379 break;
380 }
381 line++;
382 if (c == '/' && !--p_value)
383 start = line;
384 }
385 if (!start)
386 return squash_slash(def);
387 len = line - start;
388 if (!len)
389 return squash_slash(def);
390
391 /*
392 * Generally we prefer the shorter name, especially
393 * if the other one is just a variation of that with
394 * something else tacked on to the end (ie "file.orig"
395 * or "file~").
396 */
397 if (def) {
398 int deflen = strlen(def);
399 if (deflen < len && !strncmp(start, def, deflen))
400 return squash_slash(def);
401 free(def);
402 }
403
404 if (root) {
405 char *ret = xmalloc(root_len + len + 1);
406 strcpy(ret, root);
407 memcpy(ret + root_len, start, len);
408 ret[root_len + len] = '\0';
409 return squash_slash(ret);
410 }
411
412 return squash_slash(xmemdupz(start, len));
413 }
414
415 static int count_slashes(const char *cp)
416 {
417 int cnt = 0;
418 char ch;
419
420 while ((ch = *cp++))
421 if (ch == '/')
422 cnt++;
423 return cnt;
424 }
425
426 /*
427 * Given the string after "--- " or "+++ ", guess the appropriate
428 * p_value for the given patch.
429 */
430 static int guess_p_value(const char *nameline)
431 {
432 char *name, *cp;
433 int val = -1;
434
435 if (is_dev_null(nameline))
436 return -1;
437 name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB);
438 if (!name)
439 return -1;
440 cp = strchr(name, '/');
441 if (!cp)
442 val = 0;
443 else if (prefix) {
444 /*
445 * Does it begin with "a/$our-prefix" and such? Then this is
446 * very likely to apply to our directory.
447 */
448 if (!strncmp(name, prefix, prefix_length))
449 val = count_slashes(prefix);
450 else {
451 cp++;
452 if (!strncmp(cp, prefix, prefix_length))
453 val = count_slashes(prefix) + 1;
454 }
455 }
456 free(name);
457 return val;
458 }
459
460 /*
461 * Get the name etc info from the ---/+++ lines of a traditional patch header
462 *
463 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
464 * files, we can happily check the index for a match, but for creating a
465 * new file we should try to match whatever "patch" does. I have no idea.
466 */
467 static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
468 {
469 char *name;
470
471 first += 4; /* skip "--- " */
472 second += 4; /* skip "+++ " */
473 if (!p_value_known) {
474 int p, q;
475 p = guess_p_value(first);
476 q = guess_p_value(second);
477 if (p < 0) p = q;
478 if (0 <= p && p == q) {
479 p_value = p;
480 p_value_known = 1;
481 }
482 }
483 if (is_dev_null(first)) {
484 patch->is_new = 1;
485 patch->is_delete = 0;
486 name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB);
487 patch->new_name = name;
488 } else if (is_dev_null(second)) {
489 patch->is_new = 0;
490 patch->is_delete = 1;
491 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
492 patch->old_name = name;
493 } else {
494 name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB);
495 name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB);
496 patch->old_name = patch->new_name = name;
497 }
498 if (!name)
499 die("unable to find filename in patch at line %d", linenr);
500 }
501
502 static int gitdiff_hdrend(const char *line, struct patch *patch)
503 {
504 return -1;
505 }
506
507 /*
508 * We're anal about diff header consistency, to make
509 * sure that we don't end up having strange ambiguous
510 * patches floating around.
511 *
512 * As a result, gitdiff_{old|new}name() will check
513 * their names against any previous information, just
514 * to make sure..
515 */
516 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
517 {
518 if (!orig_name && !isnull)
519 return find_name(line, NULL, p_value, TERM_TAB);
520
521 if (orig_name) {
522 int len;
523 const char *name;
524 char *another;
525 name = orig_name;
526 len = strlen(name);
527 if (isnull)
528 die("git apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
529 another = find_name(line, NULL, p_value, TERM_TAB);
530 if (!another || memcmp(another, name, len))
531 die("git apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
532 free(another);
533 return orig_name;
534 }
535 else {
536 /* expect "/dev/null" */
537 if (memcmp("/dev/null", line, 9) || line[9] != '\n')
538 die("git apply: bad git-diff - expected /dev/null on line %d", linenr);
539 return NULL;
540 }
541 }
542
543 static int gitdiff_oldname(const char *line, struct patch *patch)
544 {
545 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old");
546 return 0;
547 }
548
549 static int gitdiff_newname(const char *line, struct patch *patch)
550 {
551 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new");
552 return 0;
553 }
554
555 static int gitdiff_oldmode(const char *line, struct patch *patch)
556 {
557 patch->old_mode = strtoul(line, NULL, 8);
558 return 0;
559 }
560
561 static int gitdiff_newmode(const char *line, struct patch *patch)
562 {
563 patch->new_mode = strtoul(line, NULL, 8);
564 return 0;
565 }
566
567 static int gitdiff_delete(const char *line, struct patch *patch)
568 {
569 patch->is_delete = 1;
570 patch->old_name = patch->def_name;
571 return gitdiff_oldmode(line, patch);
572 }
573
574 static int gitdiff_newfile(const char *line, struct patch *patch)
575 {
576 patch->is_new = 1;
577 patch->new_name = patch->def_name;
578 return gitdiff_newmode(line, patch);
579 }
580
581 static int gitdiff_copysrc(const char *line, struct patch *patch)
582 {
583 patch->is_copy = 1;
584 patch->old_name = find_name(line, NULL, 0, 0);
585 return 0;
586 }
587
588 static int gitdiff_copydst(const char *line, struct patch *patch)
589 {
590 patch->is_copy = 1;
591 patch->new_name = find_name(line, NULL, 0, 0);
592 return 0;
593 }
594
595 static int gitdiff_renamesrc(const char *line, struct patch *patch)
596 {
597 patch->is_rename = 1;
598 patch->old_name = find_name(line, NULL, 0, 0);
599 return 0;
600 }
601
602 static int gitdiff_renamedst(const char *line, struct patch *patch)
603 {
604 patch->is_rename = 1;
605 patch->new_name = find_name(line, NULL, 0, 0);
606 return 0;
607 }
608
609 static int gitdiff_similarity(const char *line, struct patch *patch)
610 {
611 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
612 patch->score = 0;
613 return 0;
614 }
615
616 static int gitdiff_dissimilarity(const char *line, struct patch *patch)
617 {
618 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX)
619 patch->score = 0;
620 return 0;
621 }
622
623 static int gitdiff_index(const char *line, struct patch *patch)
624 {
625 /*
626 * index line is N hexadecimal, "..", N hexadecimal,
627 * and optional space with octal mode.
628 */
629 const char *ptr, *eol;
630 int len;
631
632 ptr = strchr(line, '.');
633 if (!ptr || ptr[1] != '.' || 40 < ptr - line)
634 return 0;
635 len = ptr - line;
636 memcpy(patch->old_sha1_prefix, line, len);
637 patch->old_sha1_prefix[len] = 0;
638
639 line = ptr + 2;
640 ptr = strchr(line, ' ');
641 eol = strchr(line, '\n');
642
643 if (!ptr || eol < ptr)
644 ptr = eol;
645 len = ptr - line;
646
647 if (40 < len)
648 return 0;
649 memcpy(patch->new_sha1_prefix, line, len);
650 patch->new_sha1_prefix[len] = 0;
651 if (*ptr == ' ')
652 patch->old_mode = strtoul(ptr+1, NULL, 8);
653 return 0;
654 }
655
656 /*
657 * This is normal for a diff that doesn't change anything: we'll fall through
658 * into the next diff. Tell the parser to break out.
659 */
660 static int gitdiff_unrecognized(const char *line, struct patch *patch)
661 {
662 return -1;
663 }
664
665 static const char *stop_at_slash(const char *line, int llen)
666 {
667 int i;
668
669 for (i = 0; i < llen; i++) {
670 int ch = line[i];
671 if (ch == '/')
672 return line + i;
673 }
674 return NULL;
675 }
676
677 /*
678 * This is to extract the same name that appears on "diff --git"
679 * line. We do not find and return anything if it is a rename
680 * patch, and it is OK because we will find the name elsewhere.
681 * We need to reliably find name only when it is mode-change only,
682 * creation or deletion of an empty file. In any of these cases,
683 * both sides are the same name under a/ and b/ respectively.
684 */
685 static char *git_header_name(char *line, int llen)
686 {
687 const char *name;
688 const char *second = NULL;
689 size_t len;
690
691 line += strlen("diff --git ");
692 llen -= strlen("diff --git ");
693
694 if (*line == '"') {
695 const char *cp;
696 struct strbuf first = STRBUF_INIT;
697 struct strbuf sp = STRBUF_INIT;
698
699 if (unquote_c_style(&first, line, &second))
700 goto free_and_fail1;
701
702 /* advance to the first slash */
703 cp = stop_at_slash(first.buf, first.len);
704 /* we do not accept absolute paths */
705 if (!cp || cp == first.buf)
706 goto free_and_fail1;
707 strbuf_remove(&first, 0, cp + 1 - first.buf);
708
709 /*
710 * second points at one past closing dq of name.
711 * find the second name.
712 */
713 while ((second < line + llen) && isspace(*second))
714 second++;
715
716 if (line + llen <= second)
717 goto free_and_fail1;
718 if (*second == '"') {
719 if (unquote_c_style(&sp, second, NULL))
720 goto free_and_fail1;
721 cp = stop_at_slash(sp.buf, sp.len);
722 if (!cp || cp == sp.buf)
723 goto free_and_fail1;
724 /* They must match, otherwise ignore */
725 if (strcmp(cp + 1, first.buf))
726 goto free_and_fail1;
727 strbuf_release(&sp);
728 return strbuf_detach(&first, NULL);
729 }
730
731 /* unquoted second */
732 cp = stop_at_slash(second, line + llen - second);
733 if (!cp || cp == second)
734 goto free_and_fail1;
735 cp++;
736 if (line + llen - cp != first.len + 1 ||
737 memcmp(first.buf, cp, first.len))
738 goto free_and_fail1;
739 return strbuf_detach(&first, NULL);
740
741 free_and_fail1:
742 strbuf_release(&first);
743 strbuf_release(&sp);
744 return NULL;
745 }
746
747 /* unquoted first name */
748 name = stop_at_slash(line, llen);
749 if (!name || name == line)
750 return NULL;
751 name++;
752
753 /*
754 * since the first name is unquoted, a dq if exists must be
755 * the beginning of the second name.
756 */
757 for (second = name; second < line + llen; second++) {
758 if (*second == '"') {
759 struct strbuf sp = STRBUF_INIT;
760 const char *np;
761
762 if (unquote_c_style(&sp, second, NULL))
763 goto free_and_fail2;
764
765 np = stop_at_slash(sp.buf, sp.len);
766 if (!np || np == sp.buf)
767 goto free_and_fail2;
768 np++;
769
770 len = sp.buf + sp.len - np;
771 if (len < second - name &&
772 !strncmp(np, name, len) &&
773 isspace(name[len])) {
774 /* Good */
775 strbuf_remove(&sp, 0, np - sp.buf);
776 return strbuf_detach(&sp, NULL);
777 }
778
779 free_and_fail2:
780 strbuf_release(&sp);
781 return NULL;
782 }
783 }
784
785 /*
786 * Accept a name only if it shows up twice, exactly the same
787 * form.
788 */
789 for (len = 0 ; ; len++) {
790 switch (name[len]) {
791 default:
792 continue;
793 case '\n':
794 return NULL;
795 case '\t': case ' ':
796 second = name+len;
797 for (;;) {
798 char c = *second++;
799 if (c == '\n')
800 return NULL;
801 if (c == '/')
802 break;
803 }
804 if (second[len] == '\n' && !memcmp(name, second, len)) {
805 return xmemdupz(name, len);
806 }
807 }
808 }
809 }
810
811 /* Verify that we recognize the lines following a git header */
812 static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)
813 {
814 unsigned long offset;
815
816 /* A git diff has explicit new/delete information, so we don't guess */
817 patch->is_new = 0;
818 patch->is_delete = 0;
819
820 /*
821 * Some things may not have the old name in the
822 * rest of the headers anywhere (pure mode changes,
823 * or removing or adding empty files), so we get
824 * the default name from the header.
825 */
826 patch->def_name = git_header_name(line, len);
827 if (patch->def_name && root) {
828 char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
829 strcpy(s, root);
830 strcpy(s + root_len, patch->def_name);
831 free(patch->def_name);
832 patch->def_name = s;
833 }
834
835 line += len;
836 size -= len;
837 linenr++;
838 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
839 static const struct opentry {
840 const char *str;
841 int (*fn)(const char *, struct patch *);
842 } optable[] = {
843 { "@@ -", gitdiff_hdrend },
844 { "--- ", gitdiff_oldname },
845 { "+++ ", gitdiff_newname },
846 { "old mode ", gitdiff_oldmode },
847 { "new mode ", gitdiff_newmode },
848 { "deleted file mode ", gitdiff_delete },
849 { "new file mode ", gitdiff_newfile },
850 { "copy from ", gitdiff_copysrc },
851 { "copy to ", gitdiff_copydst },
852 { "rename old ", gitdiff_renamesrc },
853 { "rename new ", gitdiff_renamedst },
854 { "rename from ", gitdiff_renamesrc },
855 { "rename to ", gitdiff_renamedst },
856 { "similarity index ", gitdiff_similarity },
857 { "dissimilarity index ", gitdiff_dissimilarity },
858 { "index ", gitdiff_index },
859 { "", gitdiff_unrecognized },
860 };
861 int i;
862
863 len = linelen(line, size);
864 if (!len || line[len-1] != '\n')
865 break;
866 for (i = 0; i < ARRAY_SIZE(optable); i++) {
867 const struct opentry *p = optable + i;
868 int oplen = strlen(p->str);
869 if (len < oplen || memcmp(p->str, line, oplen))
870 continue;
871 if (p->fn(line + oplen, patch) < 0)
872 return offset;
873 break;
874 }
875 }
876
877 return offset;
878 }
879
880 static int parse_num(const char *line, unsigned long *p)
881 {
882 char *ptr;
883
884 if (!isdigit(*line))
885 return 0;
886 *p = strtoul(line, &ptr, 10);
887 return ptr - line;
888 }
889
890 static int parse_range(const char *line, int len, int offset, const char *expect,
891 unsigned long *p1, unsigned long *p2)
892 {
893 int digits, ex;
894
895 if (offset < 0 || offset >= len)
896 return -1;
897 line += offset;
898 len -= offset;
899
900 digits = parse_num(line, p1);
901 if (!digits)
902 return -1;
903
904 offset += digits;
905 line += digits;
906 len -= digits;
907
908 *p2 = 1;
909 if (*line == ',') {
910 digits = parse_num(line+1, p2);
911 if (!digits)
912 return -1;
913
914 offset += digits+1;
915 line += digits+1;
916 len -= digits+1;
917 }
918
919 ex = strlen(expect);
920 if (ex > len)
921 return -1;
922 if (memcmp(line, expect, ex))
923 return -1;
924
925 return offset + ex;
926 }
927
928 static void recount_diff(char *line, int size, struct fragment *fragment)
929 {
930 int oldlines = 0, newlines = 0, ret = 0;
931
932 if (size < 1) {
933 warning("recount: ignore empty hunk");
934 return;
935 }
936
937 for (;;) {
938 int len = linelen(line, size);
939 size -= len;
940 line += len;
941
942 if (size < 1)
943 break;
944
945 switch (*line) {
946 case ' ': case '\n':
947 newlines++;
948 /* fall through */
949 case '-':
950 oldlines++;
951 continue;
952 case '+':
953 newlines++;
954 continue;
955 case '\\':
956 continue;
957 case '@':
958 ret = size < 3 || prefixcmp(line, "@@ ");
959 break;
960 case 'd':
961 ret = size < 5 || prefixcmp(line, "diff ");
962 break;
963 default:
964 ret = -1;
965 break;
966 }
967 if (ret) {
968 warning("recount: unexpected line: %.*s",
969 (int)linelen(line, size), line);
970 return;
971 }
972 break;
973 }
974 fragment->oldlines = oldlines;
975 fragment->newlines = newlines;
976 }
977
978 /*
979 * Parse a unified diff fragment header of the
980 * form "@@ -a,b +c,d @@"
981 */
982 static int parse_fragment_header(char *line, int len, struct fragment *fragment)
983 {
984 int offset;
985
986 if (!len || line[len-1] != '\n')
987 return -1;
988
989 /* Figure out the number of lines in a fragment */
990 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
991 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
992
993 return offset;
994 }
995
996 static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)
997 {
998 unsigned long offset, len;
999
1000 patch->is_toplevel_relative = 0;
1001 patch->is_rename = patch->is_copy = 0;
1002 patch->is_new = patch->is_delete = -1;
1003 patch->old_mode = patch->new_mode = 0;
1004 patch->old_name = patch->new_name = NULL;
1005 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
1006 unsigned long nextlen;
1007
1008 len = linelen(line, size);
1009 if (!len)
1010 break;
1011
1012 /* Testing this early allows us to take a few shortcuts.. */
1013 if (len < 6)
1014 continue;
1015
1016 /*
1017 * Make sure we don't find any unconnected patch fragments.
1018 * That's a sign that we didn't find a header, and that a
1019 * patch has become corrupted/broken up.
1020 */
1021 if (!memcmp("@@ -", line, 4)) {
1022 struct fragment dummy;
1023 if (parse_fragment_header(line, len, &dummy) < 0)
1024 continue;
1025 die("patch fragment without header at line %d: %.*s",
1026 linenr, (int)len-1, line);
1027 }
1028
1029 if (size < len + 6)
1030 break;
1031
1032 /*
1033 * Git patch? It might not have a real patch, just a rename
1034 * or mode change, so we handle that specially
1035 */
1036 if (!memcmp("diff --git ", line, 11)) {
1037 int git_hdr_len = parse_git_header(line, len, size, patch);
1038 if (git_hdr_len <= len)
1039 continue;
1040 if (!patch->old_name && !patch->new_name) {
1041 if (!patch->def_name)
1042 die("git diff header lacks filename information (line %d)", linenr);
1043 patch->old_name = patch->new_name = patch->def_name;
1044 }
1045 patch->is_toplevel_relative = 1;
1046 *hdrsize = git_hdr_len;
1047 return offset;
1048 }
1049
1050 /* --- followed by +++ ? */
1051 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1052 continue;
1053
1054 /*
1055 * We only accept unified patches, so we want it to
1056 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1057 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1058 */
1059 nextlen = linelen(line + len, size - len);
1060 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1061 continue;
1062
1063 /* Ok, we'll consider it a patch */
1064 parse_traditional_patch(line, line+len, patch);
1065 *hdrsize = len + nextlen;
1066 linenr += 2;
1067 return offset;
1068 }
1069 return -1;
1070 }
1071
1072 static void check_whitespace(const char *line, int len, unsigned ws_rule)
1073 {
1074 char *err;
1075 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1076 if (!result)
1077 return;
1078
1079 whitespace_error++;
1080 if (squelch_whitespace_errors &&
1081 squelch_whitespace_errors < whitespace_error)
1082 ;
1083 else {
1084 err = whitespace_error_string(result);
1085 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1086 patch_input_file, linenr, err, len - 2, line + 1);
1087 free(err);
1088 }
1089 }
1090
1091 /*
1092 * Parse a unified diff. Note that this really needs to parse each
1093 * fragment separately, since the only way to know the difference
1094 * between a "---" that is part of a patch, and a "---" that starts
1095 * the next patch is to look at the line counts..
1096 */
1097 static int parse_fragment(char *line, unsigned long size,
1098 struct patch *patch, struct fragment *fragment)
1099 {
1100 int added, deleted;
1101 int len = linelen(line, size), offset;
1102 unsigned long oldlines, newlines;
1103 unsigned long leading, trailing;
1104
1105 offset = parse_fragment_header(line, len, fragment);
1106 if (offset < 0)
1107 return -1;
1108 if (offset > 0 && patch->recount)
1109 recount_diff(line + offset, size - offset, fragment);
1110 oldlines = fragment->oldlines;
1111 newlines = fragment->newlines;
1112 leading = 0;
1113 trailing = 0;
1114
1115 /* Parse the thing.. */
1116 line += len;
1117 size -= len;
1118 linenr++;
1119 added = deleted = 0;
1120 for (offset = len;
1121 0 < size;
1122 offset += len, size -= len, line += len, linenr++) {
1123 if (!oldlines && !newlines)
1124 break;
1125 len = linelen(line, size);
1126 if (!len || line[len-1] != '\n')
1127 return -1;
1128 switch (*line) {
1129 default:
1130 return -1;
1131 case '\n': /* newer GNU diff, an empty context line */
1132 case ' ':
1133 oldlines--;
1134 newlines--;
1135 if (!deleted && !added)
1136 leading++;
1137 trailing++;
1138 break;
1139 case '-':
1140 if (apply_in_reverse &&
1141 ws_error_action != nowarn_ws_error)
1142 check_whitespace(line, len, patch->ws_rule);
1143 deleted++;
1144 oldlines--;
1145 trailing = 0;
1146 break;
1147 case '+':
1148 if (!apply_in_reverse &&
1149 ws_error_action != nowarn_ws_error)
1150 check_whitespace(line, len, patch->ws_rule);
1151 added++;
1152 newlines--;
1153 trailing = 0;
1154 break;
1155
1156 /*
1157 * We allow "\ No newline at end of file". Depending
1158 * on locale settings when the patch was produced we
1159 * don't know what this line looks like. The only
1160 * thing we do know is that it begins with "\ ".
1161 * Checking for 12 is just for sanity check -- any
1162 * l10n of "\ No newline..." is at least that long.
1163 */
1164 case '\\':
1165 if (len < 12 || memcmp(line, "\\ ", 2))
1166 return -1;
1167 break;
1168 }
1169 }
1170 if (oldlines || newlines)
1171 return -1;
1172 fragment->leading = leading;
1173 fragment->trailing = trailing;
1174
1175 /*
1176 * If a fragment ends with an incomplete line, we failed to include
1177 * it in the above loop because we hit oldlines == newlines == 0
1178 * before seeing it.
1179 */
1180 if (12 < size && !memcmp(line, "\\ ", 2))
1181 offset += linelen(line, size);
1182
1183 patch->lines_added += added;
1184 patch->lines_deleted += deleted;
1185
1186 if (0 < patch->is_new && oldlines)
1187 return error("new file depends on old contents");
1188 if (0 < patch->is_delete && newlines)
1189 return error("deleted file still has contents");
1190 return offset;
1191 }
1192
1193 static int parse_single_patch(char *line, unsigned long size, struct patch *patch)
1194 {
1195 unsigned long offset = 0;
1196 unsigned long oldlines = 0, newlines = 0, context = 0;
1197 struct fragment **fragp = &patch->fragments;
1198
1199 while (size > 4 && !memcmp(line, "@@ -", 4)) {
1200 struct fragment *fragment;
1201 int len;
1202
1203 fragment = xcalloc(1, sizeof(*fragment));
1204 len = parse_fragment(line, size, patch, fragment);
1205 if (len <= 0)
1206 die("corrupt patch at line %d", linenr);
1207 fragment->patch = line;
1208 fragment->size = len;
1209 oldlines += fragment->oldlines;
1210 newlines += fragment->newlines;
1211 context += fragment->leading + fragment->trailing;
1212
1213 *fragp = fragment;
1214 fragp = &fragment->next;
1215
1216 offset += len;
1217 line += len;
1218 size -= len;
1219 }
1220
1221 /*
1222 * If something was removed (i.e. we have old-lines) it cannot
1223 * be creation, and if something was added it cannot be
1224 * deletion. However, the reverse is not true; --unified=0
1225 * patches that only add are not necessarily creation even
1226 * though they do not have any old lines, and ones that only
1227 * delete are not necessarily deletion.
1228 *
1229 * Unfortunately, a real creation/deletion patch do _not_ have
1230 * any context line by definition, so we cannot safely tell it
1231 * apart with --unified=0 insanity. At least if the patch has
1232 * more than one hunk it is not creation or deletion.
1233 */
1234 if (patch->is_new < 0 &&
1235 (oldlines || (patch->fragments && patch->fragments->next)))
1236 patch->is_new = 0;
1237 if (patch->is_delete < 0 &&
1238 (newlines || (patch->fragments && patch->fragments->next)))
1239 patch->is_delete = 0;
1240
1241 if (0 < patch->is_new && oldlines)
1242 die("new file %s depends on old contents", patch->new_name);
1243 if (0 < patch->is_delete && newlines)
1244 die("deleted file %s still has contents", patch->old_name);
1245 if (!patch->is_delete && !newlines && context)
1246 fprintf(stderr, "** warning: file %s becomes empty but "
1247 "is not deleted\n", patch->new_name);
1248
1249 return offset;
1250 }
1251
1252 static inline int metadata_changes(struct patch *patch)
1253 {
1254 return patch->is_rename > 0 ||
1255 patch->is_copy > 0 ||
1256 patch->is_new > 0 ||
1257 patch->is_delete ||
1258 (patch->old_mode && patch->new_mode &&
1259 patch->old_mode != patch->new_mode);
1260 }
1261
1262 static char *inflate_it(const void *data, unsigned long size,
1263 unsigned long inflated_size)
1264 {
1265 z_stream stream;
1266 void *out;
1267 int st;
1268
1269 memset(&stream, 0, sizeof(stream));
1270
1271 stream.next_in = (unsigned char *)data;
1272 stream.avail_in = size;
1273 stream.next_out = out = xmalloc(inflated_size);
1274 stream.avail_out = inflated_size;
1275 git_inflate_init(&stream);
1276 st = git_inflate(&stream, Z_FINISH);
1277 git_inflate_end(&stream);
1278 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1279 free(out);
1280 return NULL;
1281 }
1282 return out;
1283 }
1284
1285 static struct fragment *parse_binary_hunk(char **buf_p,
1286 unsigned long *sz_p,
1287 int *status_p,
1288 int *used_p)
1289 {
1290 /*
1291 * Expect a line that begins with binary patch method ("literal"
1292 * or "delta"), followed by the length of data before deflating.
1293 * a sequence of 'length-byte' followed by base-85 encoded data
1294 * should follow, terminated by a newline.
1295 *
1296 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1297 * and we would limit the patch line to 66 characters,
1298 * so one line can fit up to 13 groups that would decode
1299 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1300 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1301 */
1302 int llen, used;
1303 unsigned long size = *sz_p;
1304 char *buffer = *buf_p;
1305 int patch_method;
1306 unsigned long origlen;
1307 char *data = NULL;
1308 int hunk_size = 0;
1309 struct fragment *frag;
1310
1311 llen = linelen(buffer, size);
1312 used = llen;
1313
1314 *status_p = 0;
1315
1316 if (!prefixcmp(buffer, "delta ")) {
1317 patch_method = BINARY_DELTA_DEFLATED;
1318 origlen = strtoul(buffer + 6, NULL, 10);
1319 }
1320 else if (!prefixcmp(buffer, "literal ")) {
1321 patch_method = BINARY_LITERAL_DEFLATED;
1322 origlen = strtoul(buffer + 8, NULL, 10);
1323 }
1324 else
1325 return NULL;
1326
1327 linenr++;
1328 buffer += llen;
1329 while (1) {
1330 int byte_length, max_byte_length, newsize;
1331 llen = linelen(buffer, size);
1332 used += llen;
1333 linenr++;
1334 if (llen == 1) {
1335 /* consume the blank line */
1336 buffer++;
1337 size--;
1338 break;
1339 }
1340 /*
1341 * Minimum line is "A00000\n" which is 7-byte long,
1342 * and the line length must be multiple of 5 plus 2.
1343 */
1344 if ((llen < 7) || (llen-2) % 5)
1345 goto corrupt;
1346 max_byte_length = (llen - 2) / 5 * 4;
1347 byte_length = *buffer;
1348 if ('A' <= byte_length && byte_length <= 'Z')
1349 byte_length = byte_length - 'A' + 1;
1350 else if ('a' <= byte_length && byte_length <= 'z')
1351 byte_length = byte_length - 'a' + 27;
1352 else
1353 goto corrupt;
1354 /* if the input length was not multiple of 4, we would
1355 * have filler at the end but the filler should never
1356 * exceed 3 bytes
1357 */
1358 if (max_byte_length < byte_length ||
1359 byte_length <= max_byte_length - 4)
1360 goto corrupt;
1361 newsize = hunk_size + byte_length;
1362 data = xrealloc(data, newsize);
1363 if (decode_85(data + hunk_size, buffer + 1, byte_length))
1364 goto corrupt;
1365 hunk_size = newsize;
1366 buffer += llen;
1367 size -= llen;
1368 }
1369
1370 frag = xcalloc(1, sizeof(*frag));
1371 frag->patch = inflate_it(data, hunk_size, origlen);
1372 if (!frag->patch)
1373 goto corrupt;
1374 free(data);
1375 frag->size = origlen;
1376 *buf_p = buffer;
1377 *sz_p = size;
1378 *used_p = used;
1379 frag->binary_patch_method = patch_method;
1380 return frag;
1381
1382 corrupt:
1383 free(data);
1384 *status_p = -1;
1385 error("corrupt binary patch at line %d: %.*s",
1386 linenr-1, llen-1, buffer);
1387 return NULL;
1388 }
1389
1390 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1391 {
1392 /*
1393 * We have read "GIT binary patch\n"; what follows is a line
1394 * that says the patch method (currently, either "literal" or
1395 * "delta") and the length of data before deflating; a
1396 * sequence of 'length-byte' followed by base-85 encoded data
1397 * follows.
1398 *
1399 * When a binary patch is reversible, there is another binary
1400 * hunk in the same format, starting with patch method (either
1401 * "literal" or "delta") with the length of data, and a sequence
1402 * of length-byte + base-85 encoded data, terminated with another
1403 * empty line. This data, when applied to the postimage, produces
1404 * the preimage.
1405 */
1406 struct fragment *forward;
1407 struct fragment *reverse;
1408 int status;
1409 int used, used_1;
1410
1411 forward = parse_binary_hunk(&buffer, &size, &status, &used);
1412 if (!forward && !status)
1413 /* there has to be one hunk (forward hunk) */
1414 return error("unrecognized binary patch at line %d", linenr-1);
1415 if (status)
1416 /* otherwise we already gave an error message */
1417 return status;
1418
1419 reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1420 if (reverse)
1421 used += used_1;
1422 else if (status) {
1423 /*
1424 * Not having reverse hunk is not an error, but having
1425 * a corrupt reverse hunk is.
1426 */
1427 free((void*) forward->patch);
1428 free(forward);
1429 return status;
1430 }
1431 forward->next = reverse;
1432 patch->fragments = forward;
1433 patch->is_binary = 1;
1434 return used;
1435 }
1436
1437 static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1438 {
1439 int hdrsize, patchsize;
1440 int offset = find_header(buffer, size, &hdrsize, patch);
1441
1442 if (offset < 0)
1443 return offset;
1444
1445 patch->ws_rule = whitespace_rule(patch->new_name
1446 ? patch->new_name
1447 : patch->old_name);
1448
1449 patchsize = parse_single_patch(buffer + offset + hdrsize,
1450 size - offset - hdrsize, patch);
1451
1452 if (!patchsize) {
1453 static const char *binhdr[] = {
1454 "Binary files ",
1455 "Files ",
1456 NULL,
1457 };
1458 static const char git_binary[] = "GIT binary patch\n";
1459 int i;
1460 int hd = hdrsize + offset;
1461 unsigned long llen = linelen(buffer + hd, size - hd);
1462
1463 if (llen == sizeof(git_binary) - 1 &&
1464 !memcmp(git_binary, buffer + hd, llen)) {
1465 int used;
1466 linenr++;
1467 used = parse_binary(buffer + hd + llen,
1468 size - hd - llen, patch);
1469 if (used)
1470 patchsize = used + llen;
1471 else
1472 patchsize = 0;
1473 }
1474 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1475 for (i = 0; binhdr[i]; i++) {
1476 int len = strlen(binhdr[i]);
1477 if (len < size - hd &&
1478 !memcmp(binhdr[i], buffer + hd, len)) {
1479 linenr++;
1480 patch->is_binary = 1;
1481 patchsize = llen;
1482 break;
1483 }
1484 }
1485 }
1486
1487 /* Empty patch cannot be applied if it is a text patch
1488 * without metadata change. A binary patch appears
1489 * empty to us here.
1490 */
1491 if ((apply || check) &&
1492 (!patch->is_binary && !metadata_changes(patch)))
1493 die("patch with only garbage at line %d", linenr);
1494 }
1495
1496 return offset + hdrsize + patchsize;
1497 }
1498
1499 #define swap(a,b) myswap((a),(b),sizeof(a))
1500
1501 #define myswap(a, b, size) do { \
1502 unsigned char mytmp[size]; \
1503 memcpy(mytmp, &a, size); \
1504 memcpy(&a, &b, size); \
1505 memcpy(&b, mytmp, size); \
1506 } while (0)
1507
1508 static void reverse_patches(struct patch *p)
1509 {
1510 for (; p; p = p->next) {
1511 struct fragment *frag = p->fragments;
1512
1513 swap(p->new_name, p->old_name);
1514 swap(p->new_mode, p->old_mode);
1515 swap(p->is_new, p->is_delete);
1516 swap(p->lines_added, p->lines_deleted);
1517 swap(p->old_sha1_prefix, p->new_sha1_prefix);
1518
1519 for (; frag; frag = frag->next) {
1520 swap(frag->newpos, frag->oldpos);
1521 swap(frag->newlines, frag->oldlines);
1522 }
1523 }
1524 }
1525
1526 static const char pluses[] =
1527 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1528 static const char minuses[]=
1529 "----------------------------------------------------------------------";
1530
1531 static void show_stats(struct patch *patch)
1532 {
1533 struct strbuf qname = STRBUF_INIT;
1534 char *cp = patch->new_name ? patch->new_name : patch->old_name;
1535 int max, add, del;
1536
1537 quote_c_style(cp, &qname, NULL, 0);
1538
1539 /*
1540 * "scale" the filename
1541 */
1542 max = max_len;
1543 if (max > 50)
1544 max = 50;
1545
1546 if (qname.len > max) {
1547 cp = strchr(qname.buf + qname.len + 3 - max, '/');
1548 if (!cp)
1549 cp = qname.buf + qname.len + 3 - max;
1550 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
1551 }
1552
1553 if (patch->is_binary) {
1554 printf(" %-*s | Bin\n", max, qname.buf);
1555 strbuf_release(&qname);
1556 return;
1557 }
1558
1559 printf(" %-*s |", max, qname.buf);
1560 strbuf_release(&qname);
1561
1562 /*
1563 * scale the add/delete
1564 */
1565 max = max + max_change > 70 ? 70 - max : max_change;
1566 add = patch->lines_added;
1567 del = patch->lines_deleted;
1568
1569 if (max_change > 0) {
1570 int total = ((add + del) * max + max_change / 2) / max_change;
1571 add = (add * max + max_change / 2) / max_change;
1572 del = total - add;
1573 }
1574 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
1575 add, pluses, del, minuses);
1576 }
1577
1578 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1579 {
1580 switch (st->st_mode & S_IFMT) {
1581 case S_IFLNK:
1582 if (strbuf_readlink(buf, path, st->st_size) < 0)
1583 return error("unable to read symlink %s", path);
1584 return 0;
1585 case S_IFREG:
1586 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
1587 return error("unable to open or read %s", path);
1588 convert_to_git(path, buf->buf, buf->len, buf, 0);
1589 return 0;
1590 default:
1591 return -1;
1592 }
1593 }
1594
1595 static void update_pre_post_images(struct image *preimage,
1596 struct image *postimage,
1597 char *buf,
1598 size_t len)
1599 {
1600 int i, ctx;
1601 char *new, *old, *fixed;
1602 struct image fixed_preimage;
1603
1604 /*
1605 * Update the preimage with whitespace fixes. Note that we
1606 * are not losing preimage->buf -- apply_one_fragment() will
1607 * free "oldlines".
1608 */
1609 prepare_image(&fixed_preimage, buf, len, 1);
1610 assert(fixed_preimage.nr == preimage->nr);
1611 for (i = 0; i < preimage->nr; i++)
1612 fixed_preimage.line[i].flag = preimage->line[i].flag;
1613 free(preimage->line_allocated);
1614 *preimage = fixed_preimage;
1615
1616 /*
1617 * Adjust the common context lines in postimage, in place.
1618 * This is possible because whitespace fixing does not make
1619 * the string grow.
1620 */
1621 new = old = postimage->buf;
1622 fixed = preimage->buf;
1623 for (i = ctx = 0; i < postimage->nr; i++) {
1624 size_t len = postimage->line[i].len;
1625 if (!(postimage->line[i].flag & LINE_COMMON)) {
1626 /* an added line -- no counterparts in preimage */
1627 memmove(new, old, len);
1628 old += len;
1629 new += len;
1630 continue;
1631 }
1632
1633 /* a common context -- skip it in the original postimage */
1634 old += len;
1635
1636 /* and find the corresponding one in the fixed preimage */
1637 while (ctx < preimage->nr &&
1638 !(preimage->line[ctx].flag & LINE_COMMON)) {
1639 fixed += preimage->line[ctx].len;
1640 ctx++;
1641 }
1642 if (preimage->nr <= ctx)
1643 die("oops");
1644
1645 /* and copy it in, while fixing the line length */
1646 len = preimage->line[ctx].len;
1647 memcpy(new, fixed, len);
1648 new += len;
1649 fixed += len;
1650 postimage->line[i].len = len;
1651 ctx++;
1652 }
1653
1654 /* Fix the length of the whole thing */
1655 postimage->len = new - postimage->buf;
1656 }
1657
1658 static int match_fragment(struct image *img,
1659 struct image *preimage,
1660 struct image *postimage,
1661 unsigned long try,
1662 int try_lno,
1663 unsigned ws_rule,
1664 int match_beginning, int match_end)
1665 {
1666 int i;
1667 char *fixed_buf, *buf, *orig, *target;
1668
1669 if (preimage->nr + try_lno > img->nr)
1670 return 0;
1671
1672 if (match_beginning && try_lno)
1673 return 0;
1674
1675 if (match_end && preimage->nr + try_lno != img->nr)
1676 return 0;
1677
1678 /* Quick hash check */
1679 for (i = 0; i < preimage->nr; i++)
1680 if (preimage->line[i].hash != img->line[try_lno + i].hash)
1681 return 0;
1682
1683 /*
1684 * Do we have an exact match? If we were told to match
1685 * at the end, size must be exactly at try+fragsize,
1686 * otherwise try+fragsize must be still within the preimage,
1687 * and either case, the old piece should match the preimage
1688 * exactly.
1689 */
1690 if ((match_end
1691 ? (try + preimage->len == img->len)
1692 : (try + preimage->len <= img->len)) &&
1693 !memcmp(img->buf + try, preimage->buf, preimage->len))
1694 return 1;
1695
1696 if (ws_error_action != correct_ws_error)
1697 return 0;
1698
1699 /*
1700 * The hunk does not apply byte-by-byte, but the hash says
1701 * it might with whitespace fuzz.
1702 */
1703 fixed_buf = xmalloc(preimage->len + 1);
1704 buf = fixed_buf;
1705 orig = preimage->buf;
1706 target = img->buf + try;
1707 for (i = 0; i < preimage->nr; i++) {
1708 size_t fixlen; /* length after fixing the preimage */
1709 size_t oldlen = preimage->line[i].len;
1710 size_t tgtlen = img->line[try_lno + i].len;
1711 size_t tgtfixlen; /* length after fixing the target line */
1712 char tgtfixbuf[1024], *tgtfix;
1713 int match;
1714
1715 /* Try fixing the line in the preimage */
1716 fixlen = ws_fix_copy(buf, orig, oldlen, ws_rule, NULL);
1717
1718 /* Try fixing the line in the target */
1719 if (sizeof(tgtfixbuf) > tgtlen)
1720 tgtfix = tgtfixbuf;
1721 else
1722 tgtfix = xmalloc(tgtlen);
1723 tgtfixlen = ws_fix_copy(tgtfix, target, tgtlen, ws_rule, NULL);
1724
1725 /*
1726 * If they match, either the preimage was based on
1727 * a version before our tree fixed whitespace breakage,
1728 * or we are lacking a whitespace-fix patch the tree
1729 * the preimage was based on already had (i.e. target
1730 * has whitespace breakage, the preimage doesn't).
1731 * In either case, we are fixing the whitespace breakages
1732 * so we might as well take the fix together with their
1733 * real change.
1734 */
1735 match = (tgtfixlen == fixlen && !memcmp(tgtfix, buf, fixlen));
1736
1737 if (tgtfix != tgtfixbuf)
1738 free(tgtfix);
1739 if (!match)
1740 goto unmatch_exit;
1741
1742 orig += oldlen;
1743 buf += fixlen;
1744 target += tgtlen;
1745 }
1746
1747 /*
1748 * Yes, the preimage is based on an older version that still
1749 * has whitespace breakages unfixed, and fixing them makes the
1750 * hunk match. Update the context lines in the postimage.
1751 */
1752 update_pre_post_images(preimage, postimage,
1753 fixed_buf, buf - fixed_buf);
1754 return 1;
1755
1756 unmatch_exit:
1757 free(fixed_buf);
1758 return 0;
1759 }
1760
1761 static int find_pos(struct image *img,
1762 struct image *preimage,
1763 struct image *postimage,
1764 int line,
1765 unsigned ws_rule,
1766 int match_beginning, int match_end)
1767 {
1768 int i;
1769 unsigned long backwards, forwards, try;
1770 int backwards_lno, forwards_lno, try_lno;
1771
1772 if (preimage->nr > img->nr)
1773 return -1;
1774
1775 /*
1776 * If match_begining or match_end is specified, there is no
1777 * point starting from a wrong line that will never match and
1778 * wander around and wait for a match at the specified end.
1779 */
1780 if (match_beginning)
1781 line = 0;
1782 else if (match_end)
1783 line = img->nr - preimage->nr;
1784
1785 if (line > img->nr)
1786 line = img->nr;
1787
1788 try = 0;
1789 for (i = 0; i < line; i++)
1790 try += img->line[i].len;
1791
1792 /*
1793 * There's probably some smart way to do this, but I'll leave
1794 * that to the smart and beautiful people. I'm simple and stupid.
1795 */
1796 backwards = try;
1797 backwards_lno = line;
1798 forwards = try;
1799 forwards_lno = line;
1800 try_lno = line;
1801
1802 for (i = 0; ; i++) {
1803 if (match_fragment(img, preimage, postimage,
1804 try, try_lno, ws_rule,
1805 match_beginning, match_end))
1806 return try_lno;
1807
1808 again:
1809 if (backwards_lno == 0 && forwards_lno == img->nr)
1810 break;
1811
1812 if (i & 1) {
1813 if (backwards_lno == 0) {
1814 i++;
1815 goto again;
1816 }
1817 backwards_lno--;
1818 backwards -= img->line[backwards_lno].len;
1819 try = backwards;
1820 try_lno = backwards_lno;
1821 } else {
1822 if (forwards_lno == img->nr) {
1823 i++;
1824 goto again;
1825 }
1826 forwards += img->line[forwards_lno].len;
1827 forwards_lno++;
1828 try = forwards;
1829 try_lno = forwards_lno;
1830 }
1831
1832 }
1833 return -1;
1834 }
1835
1836 static void remove_first_line(struct image *img)
1837 {
1838 img->buf += img->line[0].len;
1839 img->len -= img->line[0].len;
1840 img->line++;
1841 img->nr--;
1842 }
1843
1844 static void remove_last_line(struct image *img)
1845 {
1846 img->len -= img->line[--img->nr].len;
1847 }
1848
1849 static void update_image(struct image *img,
1850 int applied_pos,
1851 struct image *preimage,
1852 struct image *postimage)
1853 {
1854 /*
1855 * remove the copy of preimage at offset in img
1856 * and replace it with postimage
1857 */
1858 int i, nr;
1859 size_t remove_count, insert_count, applied_at = 0;
1860 char *result;
1861
1862 for (i = 0; i < applied_pos; i++)
1863 applied_at += img->line[i].len;
1864
1865 remove_count = 0;
1866 for (i = 0; i < preimage->nr; i++)
1867 remove_count += img->line[applied_pos + i].len;
1868 insert_count = postimage->len;
1869
1870 /* Adjust the contents */
1871 result = xmalloc(img->len + insert_count - remove_count + 1);
1872 memcpy(result, img->buf, applied_at);
1873 memcpy(result + applied_at, postimage->buf, postimage->len);
1874 memcpy(result + applied_at + postimage->len,
1875 img->buf + (applied_at + remove_count),
1876 img->len - (applied_at + remove_count));
1877 free(img->buf);
1878 img->buf = result;
1879 img->len += insert_count - remove_count;
1880 result[img->len] = '\0';
1881
1882 /* Adjust the line table */
1883 nr = img->nr + postimage->nr - preimage->nr;
1884 if (preimage->nr < postimage->nr) {
1885 /*
1886 * NOTE: this knows that we never call remove_first_line()
1887 * on anything other than pre/post image.
1888 */
1889 img->line = xrealloc(img->line, nr * sizeof(*img->line));
1890 img->line_allocated = img->line;
1891 }
1892 if (preimage->nr != postimage->nr)
1893 memmove(img->line + applied_pos + postimage->nr,
1894 img->line + applied_pos + preimage->nr,
1895 (img->nr - (applied_pos + preimage->nr)) *
1896 sizeof(*img->line));
1897 memcpy(img->line + applied_pos,
1898 postimage->line,
1899 postimage->nr * sizeof(*img->line));
1900 img->nr = nr;
1901 }
1902
1903 static int apply_one_fragment(struct image *img, struct fragment *frag,
1904 int inaccurate_eof, unsigned ws_rule)
1905 {
1906 int match_beginning, match_end;
1907 const char *patch = frag->patch;
1908 int size = frag->size;
1909 char *old, *new, *oldlines, *newlines;
1910 int new_blank_lines_at_end = 0;
1911 unsigned long leading, trailing;
1912 int pos, applied_pos;
1913 struct image preimage;
1914 struct image postimage;
1915
1916 memset(&preimage, 0, sizeof(preimage));
1917 memset(&postimage, 0, sizeof(postimage));
1918 oldlines = xmalloc(size);
1919 newlines = xmalloc(size);
1920
1921 old = oldlines;
1922 new = newlines;
1923 while (size > 0) {
1924 char first;
1925 int len = linelen(patch, size);
1926 int plen, added;
1927 int added_blank_line = 0;
1928
1929 if (!len)
1930 break;
1931
1932 /*
1933 * "plen" is how much of the line we should use for
1934 * the actual patch data. Normally we just remove the
1935 * first character on the line, but if the line is
1936 * followed by "\ No newline", then we also remove the
1937 * last one (which is the newline, of course).
1938 */
1939 plen = len - 1;
1940 if (len < size && patch[len] == '\\')
1941 plen--;
1942 first = *patch;
1943 if (apply_in_reverse) {
1944 if (first == '-')
1945 first = '+';
1946 else if (first == '+')
1947 first = '-';
1948 }
1949
1950 switch (first) {
1951 case '\n':
1952 /* Newer GNU diff, empty context line */
1953 if (plen < 0)
1954 /* ... followed by '\No newline'; nothing */
1955 break;
1956 *old++ = '\n';
1957 *new++ = '\n';
1958 add_line_info(&preimage, "\n", 1, LINE_COMMON);
1959 add_line_info(&postimage, "\n", 1, LINE_COMMON);
1960 break;
1961 case ' ':
1962 case '-':
1963 memcpy(old, patch + 1, plen);
1964 add_line_info(&preimage, old, plen,
1965 (first == ' ' ? LINE_COMMON : 0));
1966 old += plen;
1967 if (first == '-')
1968 break;
1969 /* Fall-through for ' ' */
1970 case '+':
1971 /* --no-add does not add new lines */
1972 if (first == '+' && no_add)
1973 break;
1974
1975 if (first != '+' ||
1976 !whitespace_error ||
1977 ws_error_action != correct_ws_error) {
1978 memcpy(new, patch + 1, plen);
1979 added = plen;
1980 }
1981 else {
1982 added = ws_fix_copy(new, patch + 1, plen, ws_rule, &applied_after_fixing_ws);
1983 }
1984 add_line_info(&postimage, new, added,
1985 (first == '+' ? 0 : LINE_COMMON));
1986 new += added;
1987 if (first == '+' &&
1988 added == 1 && new[-1] == '\n')
1989 added_blank_line = 1;
1990 break;
1991 case '@': case '\\':
1992 /* Ignore it, we already handled it */
1993 break;
1994 default:
1995 if (apply_verbosely)
1996 error("invalid start of line: '%c'", first);
1997 return -1;
1998 }
1999 if (added_blank_line)
2000 new_blank_lines_at_end++;
2001 else
2002 new_blank_lines_at_end = 0;
2003 patch += len;
2004 size -= len;
2005 }
2006 if (inaccurate_eof &&
2007 old > oldlines && old[-1] == '\n' &&
2008 new > newlines && new[-1] == '\n') {
2009 old--;
2010 new--;
2011 }
2012
2013 leading = frag->leading;
2014 trailing = frag->trailing;
2015
2016 /*
2017 * A hunk to change lines at the beginning would begin with
2018 * @@ -1,L +N,M @@
2019 * but we need to be careful. -U0 that inserts before the second
2020 * line also has this pattern.
2021 *
2022 * And a hunk to add to an empty file would begin with
2023 * @@ -0,0 +N,M @@
2024 *
2025 * In other words, a hunk that is (frag->oldpos <= 1) with or
2026 * without leading context must match at the beginning.
2027 */
2028 match_beginning = (!frag->oldpos ||
2029 (frag->oldpos == 1 && !unidiff_zero));
2030
2031 /*
2032 * A hunk without trailing lines must match at the end.
2033 * However, we simply cannot tell if a hunk must match end
2034 * from the lack of trailing lines if the patch was generated
2035 * with unidiff without any context.
2036 */
2037 match_end = !unidiff_zero && !trailing;
2038
2039 pos = frag->newpos ? (frag->newpos - 1) : 0;
2040 preimage.buf = oldlines;
2041 preimage.len = old - oldlines;
2042 postimage.buf = newlines;
2043 postimage.len = new - newlines;
2044 preimage.line = preimage.line_allocated;
2045 postimage.line = postimage.line_allocated;
2046
2047 for (;;) {
2048
2049 applied_pos = find_pos(img, &preimage, &postimage, pos,
2050 ws_rule, match_beginning, match_end);
2051
2052 if (applied_pos >= 0)
2053 break;
2054
2055 /* Am I at my context limits? */
2056 if ((leading <= p_context) && (trailing <= p_context))
2057 break;
2058 if (match_beginning || match_end) {
2059 match_beginning = match_end = 0;
2060 continue;
2061 }
2062
2063 /*
2064 * Reduce the number of context lines; reduce both
2065 * leading and trailing if they are equal otherwise
2066 * just reduce the larger context.
2067 */
2068 if (leading >= trailing) {
2069 remove_first_line(&preimage);
2070 remove_first_line(&postimage);
2071 pos--;
2072 leading--;
2073 }
2074 if (trailing > leading) {
2075 remove_last_line(&preimage);
2076 remove_last_line(&postimage);
2077 trailing--;
2078 }
2079 }
2080
2081 if (applied_pos >= 0) {
2082 if (ws_error_action == correct_ws_error &&
2083 new_blank_lines_at_end &&
2084 postimage.nr + applied_pos == img->nr) {
2085 /*
2086 * If the patch application adds blank lines
2087 * at the end, and if the patch applies at the
2088 * end of the image, remove those added blank
2089 * lines.
2090 */
2091 while (new_blank_lines_at_end--)
2092 remove_last_line(&postimage);
2093 }
2094
2095 /*
2096 * Warn if it was necessary to reduce the number
2097 * of context lines.
2098 */
2099 if ((leading != frag->leading) ||
2100 (trailing != frag->trailing))
2101 fprintf(stderr, "Context reduced to (%ld/%ld)"
2102 " to apply fragment at %d\n",
2103 leading, trailing, applied_pos+1);
2104 update_image(img, applied_pos, &preimage, &postimage);
2105 } else {
2106 if (apply_verbosely)
2107 error("while searching for:\n%.*s",
2108 (int)(old - oldlines), oldlines);
2109 }
2110
2111 free(oldlines);
2112 free(newlines);
2113 free(preimage.line_allocated);
2114 free(postimage.line_allocated);
2115
2116 return (applied_pos < 0);
2117 }
2118
2119 static int apply_binary_fragment(struct image *img, struct patch *patch)
2120 {
2121 struct fragment *fragment = patch->fragments;
2122 unsigned long len;
2123 void *dst;
2124
2125 /* Binary patch is irreversible without the optional second hunk */
2126 if (apply_in_reverse) {
2127 if (!fragment->next)
2128 return error("cannot reverse-apply a binary patch "
2129 "without the reverse hunk to '%s'",
2130 patch->new_name
2131 ? patch->new_name : patch->old_name);
2132 fragment = fragment->next;
2133 }
2134 switch (fragment->binary_patch_method) {
2135 case BINARY_DELTA_DEFLATED:
2136 dst = patch_delta(img->buf, img->len, fragment->patch,
2137 fragment->size, &len);
2138 if (!dst)
2139 return -1;
2140 clear_image(img);
2141 img->buf = dst;
2142 img->len = len;
2143 return 0;
2144 case BINARY_LITERAL_DEFLATED:
2145 clear_image(img);
2146 img->len = fragment->size;
2147 img->buf = xmalloc(img->len+1);
2148 memcpy(img->buf, fragment->patch, img->len);
2149 img->buf[img->len] = '\0';
2150 return 0;
2151 }
2152 return -1;
2153 }
2154
2155 static int apply_binary(struct image *img, struct patch *patch)
2156 {
2157 const char *name = patch->old_name ? patch->old_name : patch->new_name;
2158 unsigned char sha1[20];
2159
2160 /*
2161 * For safety, we require patch index line to contain
2162 * full 40-byte textual SHA1 for old and new, at least for now.
2163 */
2164 if (strlen(patch->old_sha1_prefix) != 40 ||
2165 strlen(patch->new_sha1_prefix) != 40 ||
2166 get_sha1_hex(patch->old_sha1_prefix, sha1) ||
2167 get_sha1_hex(patch->new_sha1_prefix, sha1))
2168 return error("cannot apply binary patch to '%s' "
2169 "without full index line", name);
2170
2171 if (patch->old_name) {
2172 /*
2173 * See if the old one matches what the patch
2174 * applies to.
2175 */
2176 hash_sha1_file(img->buf, img->len, blob_type, sha1);
2177 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
2178 return error("the patch applies to '%s' (%s), "
2179 "which does not match the "
2180 "current contents.",
2181 name, sha1_to_hex(sha1));
2182 }
2183 else {
2184 /* Otherwise, the old one must be empty. */
2185 if (img->len)
2186 return error("the patch applies to an empty "
2187 "'%s' but it is not empty", name);
2188 }
2189
2190 get_sha1_hex(patch->new_sha1_prefix, sha1);
2191 if (is_null_sha1(sha1)) {
2192 clear_image(img);
2193 return 0; /* deletion patch */
2194 }
2195
2196 if (has_sha1_file(sha1)) {
2197 /* We already have the postimage */
2198 enum object_type type;
2199 unsigned long size;
2200 char *result;
2201
2202 result = read_sha1_file(sha1, &type, &size);
2203 if (!result)
2204 return error("the necessary postimage %s for "
2205 "'%s' cannot be read",
2206 patch->new_sha1_prefix, name);
2207 clear_image(img);
2208 img->buf = result;
2209 img->len = size;
2210 } else {
2211 /*
2212 * We have verified buf matches the preimage;
2213 * apply the patch data to it, which is stored
2214 * in the patch->fragments->{patch,size}.
2215 */
2216 if (apply_binary_fragment(img, patch))
2217 return error("binary patch does not apply to '%s'",
2218 name);
2219
2220 /* verify that the result matches */
2221 hash_sha1_file(img->buf, img->len, blob_type, sha1);
2222 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
2223 return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)",
2224 name, patch->new_sha1_prefix, sha1_to_hex(sha1));
2225 }
2226
2227 return 0;
2228 }
2229
2230 static int apply_fragments(struct image *img, struct patch *patch)
2231 {
2232 struct fragment *frag = patch->fragments;
2233 const char *name = patch->old_name ? patch->old_name : patch->new_name;
2234 unsigned ws_rule = patch->ws_rule;
2235 unsigned inaccurate_eof = patch->inaccurate_eof;
2236
2237 if (patch->is_binary)
2238 return apply_binary(img, patch);
2239
2240 while (frag) {
2241 if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule)) {
2242 error("patch failed: %s:%ld", name, frag->oldpos);
2243 if (!apply_with_reject)
2244 return -1;
2245 frag->rejected = 1;
2246 }
2247 frag = frag->next;
2248 }
2249 return 0;
2250 }
2251
2252 static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
2253 {
2254 if (!ce)
2255 return 0;
2256
2257 if (S_ISGITLINK(ce->ce_mode)) {
2258 strbuf_grow(buf, 100);
2259 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));
2260 } else {
2261 enum object_type type;
2262 unsigned long sz;
2263 char *result;
2264
2265 result = read_sha1_file(ce->sha1, &type, &sz);
2266 if (!result)
2267 return -1;
2268 /* XXX read_sha1_file NUL-terminates */
2269 strbuf_attach(buf, result, sz, sz + 1);
2270 }
2271 return 0;
2272 }
2273
2274 static struct patch *in_fn_table(const char *name)
2275 {
2276 struct string_list_item *item;
2277
2278 if (name == NULL)
2279 return NULL;
2280
2281 item = string_list_lookup(name, &fn_table);
2282 if (item != NULL)
2283 return (struct patch *)item->util;
2284
2285 return NULL;
2286 }
2287
2288 /*
2289 * item->util in the filename table records the status of the path.
2290 * Usually it points at a patch (whose result records the contents
2291 * of it after applying it), but it could be PATH_WAS_DELETED for a
2292 * path that a previously applied patch has already removed.
2293 */
2294 #define PATH_TO_BE_DELETED ((struct patch *) -2)
2295 #define PATH_WAS_DELETED ((struct patch *) -1)
2296
2297 static int to_be_deleted(struct patch *patch)
2298 {
2299 return patch == PATH_TO_BE_DELETED;
2300 }
2301
2302 static int was_deleted(struct patch *patch)
2303 {
2304 return patch == PATH_WAS_DELETED;
2305 }
2306
2307 static void add_to_fn_table(struct patch *patch)
2308 {
2309 struct string_list_item *item;
2310
2311 /*
2312 * Always add new_name unless patch is a deletion
2313 * This should cover the cases for normal diffs,
2314 * file creations and copies
2315 */
2316 if (patch->new_name != NULL) {
2317 item = string_list_insert(patch->new_name, &fn_table);
2318 item->util = patch;
2319 }
2320
2321 /*
2322 * store a failure on rename/deletion cases because
2323 * later chunks shouldn't patch old names
2324 */
2325 if ((patch->new_name == NULL) || (patch->is_rename)) {
2326 item = string_list_insert(patch->old_name, &fn_table);
2327 item->util = PATH_WAS_DELETED;
2328 }
2329 }
2330
2331 static void prepare_fn_table(struct patch *patch)
2332 {
2333 /*
2334 * store information about incoming file deletion
2335 */
2336 while (patch) {
2337 if ((patch->new_name == NULL) || (patch->is_rename)) {
2338 struct string_list_item *item;
2339 item = string_list_insert(patch->old_name, &fn_table);
2340 item->util = PATH_TO_BE_DELETED;
2341 }
2342 patch = patch->next;
2343 }
2344 }
2345
2346 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
2347 {
2348 struct strbuf buf = STRBUF_INIT;
2349 struct image image;
2350 size_t len;
2351 char *img;
2352 struct patch *tpatch;
2353
2354 if (!(patch->is_copy || patch->is_rename) &&
2355 (tpatch = in_fn_table(patch->old_name)) != NULL && !to_be_deleted(tpatch)) {
2356 if (was_deleted(tpatch)) {
2357 return error("patch %s has been renamed/deleted",
2358 patch->old_name);
2359 }
2360 /* We have a patched copy in memory use that */
2361 strbuf_add(&buf, tpatch->result, tpatch->resultsize);
2362 } else if (cached) {
2363 if (read_file_or_gitlink(ce, &buf))
2364 return error("read of %s failed", patch->old_name);
2365 } else if (patch->old_name) {
2366 if (S_ISGITLINK(patch->old_mode)) {
2367 if (ce) {
2368 read_file_or_gitlink(ce, &buf);
2369 } else {
2370 /*
2371 * There is no way to apply subproject
2372 * patch without looking at the index.
2373 */
2374 patch->fragments = NULL;
2375 }
2376 } else {
2377 if (read_old_data(st, patch->old_name, &buf))
2378 return error("read of %s failed", patch->old_name);
2379 }
2380 }
2381
2382 img = strbuf_detach(&buf, &len);
2383 prepare_image(&image, img, len, !patch->is_binary);
2384
2385 if (apply_fragments(&image, patch) < 0)
2386 return -1; /* note with --reject this succeeds. */
2387 patch->result = image.buf;
2388 patch->resultsize = image.len;
2389 add_to_fn_table(patch);
2390 free(image.line_allocated);
2391
2392 if (0 < patch->is_delete && patch->resultsize)
2393 return error("removal patch leaves file contents");
2394
2395 return 0;
2396 }
2397
2398 static int check_to_create_blob(const char *new_name, int ok_if_exists)
2399 {
2400 struct stat nst;
2401 if (!lstat(new_name, &nst)) {
2402 if (S_ISDIR(nst.st_mode) || ok_if_exists)
2403 return 0;
2404 /*
2405 * A leading component of new_name might be a symlink
2406 * that is going to be removed with this patch, but
2407 * still pointing at somewhere that has the path.
2408 * In such a case, path "new_name" does not exist as
2409 * far as git is concerned.
2410 */
2411 if (has_symlink_leading_path(new_name, strlen(new_name)))
2412 return 0;
2413
2414 return error("%s: already exists in working directory", new_name);
2415 }
2416 else if ((errno != ENOENT) && (errno != ENOTDIR))
2417 return error("%s: %s", new_name, strerror(errno));
2418 return 0;
2419 }
2420
2421 static int verify_index_match(struct cache_entry *ce, struct stat *st)
2422 {
2423 if (S_ISGITLINK(ce->ce_mode)) {
2424 if (!S_ISDIR(st->st_mode))
2425 return -1;
2426 return 0;
2427 }
2428 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID);
2429 }
2430
2431 static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
2432 {
2433 const char *old_name = patch->old_name;
2434 struct patch *tpatch = NULL;
2435 int stat_ret = 0;
2436 unsigned st_mode = 0;
2437
2438 /*
2439 * Make sure that we do not have local modifications from the
2440 * index when we are looking at the index. Also make sure
2441 * we have the preimage file to be patched in the work tree,
2442 * unless --cached, which tells git to apply only in the index.
2443 */
2444 if (!old_name)
2445 return 0;
2446
2447 assert(patch->is_new <= 0);
2448
2449 if (!(patch->is_copy || patch->is_rename) &&
2450 (tpatch = in_fn_table(old_name)) != NULL && !to_be_deleted(tpatch)) {
2451 if (was_deleted(tpatch))
2452 return error("%s: has been deleted/renamed", old_name);
2453 st_mode = tpatch->new_mode;
2454 } else if (!cached) {
2455 stat_ret = lstat(old_name, st);
2456 if (stat_ret && errno != ENOENT)
2457 return error("%s: %s", old_name, strerror(errno));
2458 }
2459
2460 if (to_be_deleted(tpatch))
2461 tpatch = NULL;
2462
2463 if (check_index && !tpatch) {
2464 int pos = cache_name_pos(old_name, strlen(old_name));
2465 if (pos < 0) {
2466 if (patch->is_new < 0)
2467 goto is_new;
2468 return error("%s: does not exist in index", old_name);
2469 }
2470 *ce = active_cache[pos];
2471 if (stat_ret < 0) {
2472 struct checkout costate;
2473 /* checkout */
2474 costate.base_dir = "";
2475 costate.base_dir_len = 0;
2476 costate.force = 0;
2477 costate.quiet = 0;
2478 costate.not_new = 0;
2479 costate.refresh_cache = 1;
2480 if (checkout_entry(*ce, &costate, NULL) ||
2481 lstat(old_name, st))
2482 return -1;
2483 }
2484 if (!cached && verify_index_match(*ce, st))
2485 return error("%s: does not match index", old_name);
2486 if (cached)
2487 st_mode = (*ce)->ce_mode;
2488 } else if (stat_ret < 0) {
2489 if (patch->is_new < 0)
2490 goto is_new;
2491 return error("%s: %s", old_name, strerror(errno));
2492 }
2493
2494 if (!cached && !tpatch)
2495 st_mode = ce_mode_from_stat(*ce, st->st_mode);
2496
2497 if (patch->is_new < 0)
2498 patch->is_new = 0;
2499 if (!patch->old_mode)
2500 patch->old_mode = st_mode;
2501 if ((st_mode ^ patch->old_mode) & S_IFMT)
2502 return error("%s: wrong type", old_name);
2503 if (st_mode != patch->old_mode)
2504 warning("%s has type %o, expected %o",
2505 old_name, st_mode, patch->old_mode);
2506 if (!patch->new_mode && !patch->is_delete)
2507 patch->new_mode = st_mode;
2508 return 0;
2509
2510 is_new:
2511 patch->is_new = 1;
2512 patch->is_delete = 0;
2513 patch->old_name = NULL;
2514 return 0;
2515 }
2516
2517 static int check_patch(struct patch *patch)
2518 {
2519 struct stat st;
2520 const char *old_name = patch->old_name;
2521 const char *new_name = patch->new_name;
2522 const char *name = old_name ? old_name : new_name;
2523 struct cache_entry *ce = NULL;
2524 struct patch *tpatch;
2525 int ok_if_exists;
2526 int status;
2527
2528 patch->rejected = 1; /* we will drop this after we succeed */
2529
2530 status = check_preimage(patch, &ce, &st);
2531 if (status)
2532 return status;
2533 old_name = patch->old_name;
2534
2535 if ((tpatch = in_fn_table(new_name)) &&
2536 (was_deleted(tpatch) || to_be_deleted(tpatch)))
2537 /*
2538 * A type-change diff is always split into a patch to
2539 * delete old, immediately followed by a patch to
2540 * create new (see diff.c::run_diff()); in such a case
2541 * it is Ok that the entry to be deleted by the
2542 * previous patch is still in the working tree and in
2543 * the index.
2544 */
2545 ok_if_exists = 1;
2546 else
2547 ok_if_exists = 0;
2548
2549 if (new_name &&
2550 ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
2551 if (check_index &&
2552 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
2553 !ok_if_exists)
2554 return error("%s: already exists in index", new_name);
2555 if (!cached) {
2556 int err = check_to_create_blob(new_name, ok_if_exists);
2557 if (err)
2558 return err;
2559 }
2560 if (!patch->new_mode) {
2561 if (0 < patch->is_new)
2562 patch->new_mode = S_IFREG | 0644;
2563 else
2564 patch->new_mode = patch->old_mode;
2565 }
2566 }
2567
2568 if (new_name && old_name) {
2569 int same = !strcmp(old_name, new_name);
2570 if (!patch->new_mode)
2571 patch->new_mode = patch->old_mode;
2572 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)
2573 return error("new mode (%o) of %s does not match old mode (%o)%s%s",
2574 patch->new_mode, new_name, patch->old_mode,
2575 same ? "" : " of ", same ? "" : old_name);
2576 }
2577
2578 if (apply_data(patch, &st, ce) < 0)
2579 return error("%s: patch does not apply", name);
2580 patch->rejected = 0;
2581 return 0;
2582 }
2583
2584 static int check_patch_list(struct patch *patch)
2585 {
2586 int err = 0;
2587
2588 prepare_fn_table(patch);
2589 while (patch) {
2590 if (apply_verbosely)
2591 say_patch_name(stderr,
2592 "Checking patch ", patch, "...\n");
2593 err |= check_patch(patch);
2594 patch = patch->next;
2595 }
2596 return err;
2597 }
2598
2599 /* This function tries to read the sha1 from the current index */
2600 static int get_current_sha1(const char *path, unsigned char *sha1)
2601 {
2602 int pos;
2603
2604 if (read_cache() < 0)
2605 return -1;
2606 pos = cache_name_pos(path, strlen(path));
2607 if (pos < 0)
2608 return -1;
2609 hashcpy(sha1, active_cache[pos]->sha1);
2610 return 0;
2611 }
2612
2613 /* Build an index that contains the just the files needed for a 3way merge */
2614 static void build_fake_ancestor(struct patch *list, const char *filename)
2615 {
2616 struct patch *patch;
2617 struct index_state result = { 0 };
2618 int fd;
2619
2620 /* Once we start supporting the reverse patch, it may be
2621 * worth showing the new sha1 prefix, but until then...
2622 */
2623 for (patch = list; patch; patch = patch->next) {
2624 const unsigned char *sha1_ptr;
2625 unsigned char sha1[20];
2626 struct cache_entry *ce;
2627 const char *name;
2628
2629 name = patch->old_name ? patch->old_name : patch->new_name;
2630 if (0 < patch->is_new)
2631 continue;
2632 else if (get_sha1(patch->old_sha1_prefix, sha1))
2633 /* git diff has no index line for mode/type changes */
2634 if (!patch->lines_added && !patch->lines_deleted) {
2635 if (get_current_sha1(patch->new_name, sha1) ||
2636 get_current_sha1(patch->old_name, sha1))
2637 die("mode change for %s, which is not "
2638 "in current HEAD", name);
2639 sha1_ptr = sha1;
2640 } else
2641 die("sha1 information is lacking or useless "
2642 "(%s).", name);
2643 else
2644 sha1_ptr = sha1;
2645
2646 ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);
2647 if (!ce)
2648 die("make_cache_entry failed for path '%s'", name);
2649 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
2650 die ("Could not add %s to temporary index", name);
2651 }
2652
2653 fd = open(filename, O_WRONLY | O_CREAT, 0666);
2654 if (fd < 0 || write_index(&result, fd) || close(fd))
2655 die ("Could not write temporary index to %s", filename);
2656
2657 discard_index(&result);
2658 }
2659
2660 static void stat_patch_list(struct patch *patch)
2661 {
2662 int files, adds, dels;
2663
2664 for (files = adds = dels = 0 ; patch ; patch = patch->next) {
2665 files++;
2666 adds += patch->lines_added;
2667 dels += patch->lines_deleted;
2668 show_stats(patch);
2669 }
2670
2671 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
2672 }
2673
2674 static void numstat_patch_list(struct patch *patch)
2675 {
2676 for ( ; patch; patch = patch->next) {
2677 const char *name;
2678 name = patch->new_name ? patch->new_name : patch->old_name;
2679 if (patch->is_binary)
2680 printf("-\t-\t");
2681 else
2682 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
2683 write_name_quoted(name, stdout, line_termination);
2684 }
2685 }
2686
2687 static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
2688 {
2689 if (mode)
2690 printf(" %s mode %06o %s\n", newdelete, mode, name);
2691 else
2692 printf(" %s %s\n", newdelete, name);
2693 }
2694
2695 static void show_mode_change(struct patch *p, int show_name)
2696 {
2697 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
2698 if (show_name)
2699 printf(" mode change %06o => %06o %s\n",
2700 p->old_mode, p->new_mode, p->new_name);
2701 else
2702 printf(" mode change %06o => %06o\n",
2703 p->old_mode, p->new_mode);
2704 }
2705 }
2706
2707 static void show_rename_copy(struct patch *p)
2708 {
2709 const char *renamecopy = p->is_rename ? "rename" : "copy";
2710 const char *old, *new;
2711
2712 /* Find common prefix */
2713 old = p->old_name;
2714 new = p->new_name;
2715 while (1) {
2716 const char *slash_old, *slash_new;
2717 slash_old = strchr(old, '/');
2718 slash_new = strchr(new, '/');
2719 if (!slash_old ||
2720 !slash_new ||
2721 slash_old - old != slash_new - new ||
2722 memcmp(old, new, slash_new - new))
2723 break;
2724 old = slash_old + 1;
2725 new = slash_new + 1;
2726 }
2727 /* p->old_name thru old is the common prefix, and old and new
2728 * through the end of names are renames
2729 */
2730 if (old != p->old_name)
2731 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2732 (int)(old - p->old_name), p->old_name,
2733 old, new, p->score);
2734 else
2735 printf(" %s %s => %s (%d%%)\n", renamecopy,
2736 p->old_name, p->new_name, p->score);
2737 show_mode_change(p, 0);
2738 }
2739
2740 static void summary_patch_list(struct patch *patch)
2741 {
2742 struct patch *p;
2743
2744 for (p = patch; p; p = p->next) {
2745 if (p->is_new)
2746 show_file_mode_name("create", p->new_mode, p->new_name);
2747 else if (p->is_delete)
2748 show_file_mode_name("delete", p->old_mode, p->old_name);
2749 else {
2750 if (p->is_rename || p->is_copy)
2751 show_rename_copy(p);
2752 else {
2753 if (p->score) {
2754 printf(" rewrite %s (%d%%)\n",
2755 p->new_name, p->score);
2756 show_mode_change(p, 0);
2757 }
2758 else
2759 show_mode_change(p, 1);
2760 }
2761 }
2762 }
2763 }
2764
2765 static void patch_stats(struct patch *patch)
2766 {
2767 int lines = patch->lines_added + patch->lines_deleted;
2768
2769 if (lines > max_change)
2770 max_change = lines;
2771 if (patch->old_name) {
2772 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
2773 if (!len)
2774 len = strlen(patch->old_name);
2775 if (len > max_len)
2776 max_len = len;
2777 }
2778 if (patch->new_name) {
2779 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
2780 if (!len)
2781 len = strlen(patch->new_name);
2782 if (len > max_len)
2783 max_len = len;
2784 }
2785 }
2786
2787 static void remove_file(struct patch *patch, int rmdir_empty)
2788 {
2789 if (update_index) {
2790 if (remove_file_from_cache(patch->old_name) < 0)
2791 die("unable to remove %s from index", patch->old_name);
2792 }
2793 if (!cached) {
2794 if (S_ISGITLINK(patch->old_mode)) {
2795 if (rmdir(patch->old_name))
2796 warning("unable to remove submodule %s",
2797 patch->old_name);
2798 } else if (!unlink_or_warn(patch->old_name) && rmdir_empty) {
2799 remove_path(patch->old_name);
2800 }
2801 }
2802 }
2803
2804 static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
2805 {
2806 struct stat st;
2807 struct cache_entry *ce;
2808 int namelen = strlen(path);
2809 unsigned ce_size = cache_entry_size(namelen);
2810
2811 if (!update_index)
2812 return;
2813
2814 ce = xcalloc(1, ce_size);
2815 memcpy(ce->name, path, namelen);
2816 ce->ce_mode = create_ce_mode(mode);
2817 ce->ce_flags = namelen;
2818 if (S_ISGITLINK(mode)) {
2819 const char *s = buf;
2820
2821 if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
2822 die("corrupt patch for subproject %s", path);
2823 } else {
2824 if (!cached) {
2825 if (lstat(path, &st) < 0)
2826 die("unable to stat newly created file %s",
2827 path);
2828 fill_stat_cache_info(ce, &st);
2829 }
2830 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
2831 die("unable to create backing store for newly created file %s", path);
2832 }
2833 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
2834 die("unable to add cache entry for %s", path);
2835 }
2836
2837 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2838 {
2839 int fd;
2840 struct strbuf nbuf = STRBUF_INIT;
2841
2842 if (S_ISGITLINK(mode)) {
2843 struct stat st;
2844 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
2845 return 0;
2846 return mkdir(path, 0777);
2847 }
2848
2849 if (has_symlinks && S_ISLNK(mode))
2850 /* Although buf:size is counted string, it also is NUL
2851 * terminated.
2852 */
2853 return symlink(buf, path);
2854
2855 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
2856 if (fd < 0)
2857 return -1;
2858
2859 if (convert_to_working_tree(path, buf, size, &nbuf)) {
2860 size = nbuf.len;
2861 buf = nbuf.buf;
2862 }
2863 write_or_die(fd, buf, size);
2864 strbuf_release(&nbuf);
2865
2866 if (close(fd) < 0)
2867 die("closing file %s: %s", path, strerror(errno));
2868 return 0;
2869 }
2870
2871 /*
2872 * We optimistically assume that the directories exist,
2873 * which is true 99% of the time anyway. If they don't,
2874 * we create them and try again.
2875 */
2876 static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
2877 {
2878 if (cached)
2879 return;
2880 if (!try_create_file(path, mode, buf, size))
2881 return;
2882
2883 if (errno == ENOENT) {
2884 if (safe_create_leading_directories(path))
2885 return;
2886 if (!try_create_file(path, mode, buf, size))
2887 return;
2888 }
2889
2890 if (errno == EEXIST || errno == EACCES) {
2891 /* We may be trying to create a file where a directory
2892 * used to be.
2893 */
2894 struct stat st;
2895 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
2896 errno = EEXIST;
2897 }
2898
2899 if (errno == EEXIST) {
2900 unsigned int nr = getpid();
2901
2902 for (;;) {
2903 char newpath[PATH_MAX];
2904 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
2905 if (!try_create_file(newpath, mode, buf, size)) {
2906 if (!rename(newpath, path))
2907 return;
2908 unlink_or_warn(newpath);
2909 break;
2910 }
2911 if (errno != EEXIST)
2912 break;
2913 ++nr;
2914 }
2915 }
2916 die("unable to write file %s mode %o", path, mode);
2917 }
2918
2919 static void create_file(struct patch *patch)
2920 {
2921 char *path = patch->new_name;
2922 unsigned mode = patch->new_mode;
2923 unsigned long size = patch->resultsize;
2924 char *buf = patch->result;
2925
2926 if (!mode)
2927 mode = S_IFREG | 0644;
2928 create_one_file(path, mode, buf, size);
2929 add_index_file(path, mode, buf, size);
2930 }
2931
2932 /* phase zero is to remove, phase one is to create */
2933 static void write_out_one_result(struct patch *patch, int phase)
2934 {
2935 if (patch->is_delete > 0) {
2936 if (phase == 0)
2937 remove_file(patch, 1);
2938 return;
2939 }
2940 if (patch->is_new > 0 || patch->is_copy) {
2941 if (phase == 1)
2942 create_file(patch);
2943 return;
2944 }
2945 /*
2946 * Rename or modification boils down to the same
2947 * thing: remove the old, write the new
2948 */
2949 if (phase == 0)
2950 remove_file(patch, patch->is_rename);
2951 if (phase == 1)
2952 create_file(patch);
2953 }
2954
2955 static int write_out_one_reject(struct patch *patch)
2956 {
2957 FILE *rej;
2958 char namebuf[PATH_MAX];
2959 struct fragment *frag;
2960 int cnt = 0;
2961
2962 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
2963 if (!frag->rejected)
2964 continue;
2965 cnt++;
2966 }
2967
2968 if (!cnt) {
2969 if (apply_verbosely)
2970 say_patch_name(stderr,
2971 "Applied patch ", patch, " cleanly.\n");
2972 return 0;
2973 }
2974
2975 /* This should not happen, because a removal patch that leaves
2976 * contents are marked "rejected" at the patch level.
2977 */
2978 if (!patch->new_name)
2979 die("internal error");
2980
2981 /* Say this even without --verbose */
2982 say_patch_name(stderr, "Applying patch ", patch, " with");
2983 fprintf(stderr, " %d rejects...\n", cnt);
2984
2985 cnt = strlen(patch->new_name);
2986 if (ARRAY_SIZE(namebuf) <= cnt + 5) {
2987 cnt = ARRAY_SIZE(namebuf) - 5;
2988 warning("truncating .rej filename to %.*s.rej",
2989 cnt - 1, patch->new_name);
2990 }
2991 memcpy(namebuf, patch->new_name, cnt);
2992 memcpy(namebuf + cnt, ".rej", 5);
2993
2994 rej = fopen(namebuf, "w");
2995 if (!rej)
2996 return error("cannot open %s: %s", namebuf, strerror(errno));
2997
2998 /* Normal git tools never deal with .rej, so do not pretend
2999 * this is a git patch by saying --git nor give extended
3000 * headers. While at it, maybe please "kompare" that wants
3001 * the trailing TAB and some garbage at the end of line ;-).
3002 */
3003 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
3004 patch->new_name, patch->new_name);
3005 for (cnt = 1, frag = patch->fragments;
3006 frag;
3007 cnt++, frag = frag->next) {
3008 if (!frag->rejected) {
3009 fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);
3010 continue;
3011 }
3012 fprintf(stderr, "Rejected hunk #%d.\n", cnt);
3013 fprintf(rej, "%.*s", frag->size, frag->patch);
3014 if (frag->patch[frag->size-1] != '\n')
3015 fputc('\n', rej);
3016 }
3017 fclose(rej);
3018 return -1;
3019 }
3020
3021 static int write_out_results(struct patch *list, int skipped_patch)
3022 {
3023 int phase;
3024 int errs = 0;
3025 struct patch *l;
3026
3027 if (!list && !skipped_patch)
3028 return error("No changes");
3029
3030 for (phase = 0; phase < 2; phase++) {
3031 l = list;
3032 while (l) {
3033 if (l->rejected)
3034 errs = 1;
3035 else {
3036 write_out_one_result(l, phase);
3037 if (phase == 1 && write_out_one_reject(l))
3038 errs = 1;
3039 }
3040 l = l->next;
3041 }
3042 }
3043 return errs;
3044 }
3045
3046 static struct lock_file lock_file;
3047
3048 static struct string_list limit_by_name;
3049 static int has_include;
3050 static void add_name_limit(const char *name, int exclude)
3051 {
3052 struct string_list_item *it;
3053
3054 it = string_list_append(name, &limit_by_name);
3055 it->util = exclude ? NULL : (void *) 1;
3056 }
3057
3058 static int use_patch(struct patch *p)
3059 {
3060 const char *pathname = p->new_name ? p->new_name : p->old_name;
3061 int i;
3062
3063 /* Paths outside are not touched regardless of "--include" */
3064 if (0 < prefix_length) {
3065 int pathlen = strlen(pathname);
3066 if (pathlen <= prefix_length ||
3067 memcmp(prefix, pathname, prefix_length))
3068 return 0;
3069 }
3070
3071 /* See if it matches any of exclude/include rule */
3072 for (i = 0; i < limit_by_name.nr; i++) {
3073 struct string_list_item *it = &limit_by_name.items[i];
3074 if (!fnmatch(it->string, pathname, 0))
3075 return (it->util != NULL);
3076 }
3077
3078 /*
3079 * If we had any include, a path that does not match any rule is
3080 * not used. Otherwise, we saw bunch of exclude rules (or none)
3081 * and such a path is used.
3082 */
3083 return !has_include;
3084 }
3085
3086
3087 static void prefix_one(char **name)
3088 {
3089 char *old_name = *name;
3090 if (!old_name)
3091 return;
3092 *name = xstrdup(prefix_filename(prefix, prefix_length, *name));
3093 free(old_name);
3094 }
3095
3096 static void prefix_patches(struct patch *p)
3097 {
3098 if (!prefix || p->is_toplevel_relative)
3099 return;
3100 for ( ; p; p = p->next) {
3101 if (p->new_name == p->old_name) {
3102 char *prefixed = p->new_name;
3103 prefix_one(&prefixed);
3104 p->new_name = p->old_name = prefixed;
3105 }
3106 else {
3107 prefix_one(&p->new_name);
3108 prefix_one(&p->old_name);
3109 }
3110 }
3111 }
3112
3113 #define INACCURATE_EOF (1<<0)
3114 #define RECOUNT (1<<1)
3115
3116 static int apply_patch(int fd, const char *filename, int options)
3117 {
3118 size_t offset;
3119 struct strbuf buf = STRBUF_INIT;
3120 struct patch *list = NULL, **listp = &list;
3121 int skipped_patch = 0;
3122
3123 /* FIXME - memory leak when using multiple patch files as inputs */
3124 memset(&fn_table, 0, sizeof(struct string_list));
3125 patch_input_file = filename;
3126 read_patch_file(&buf, fd);
3127 offset = 0;
3128 while (offset < buf.len) {
3129 struct patch *patch;
3130 int nr;
3131
3132 patch = xcalloc(1, sizeof(*patch));
3133 patch->inaccurate_eof = !!(options & INACCURATE_EOF);
3134 patch->recount = !!(options & RECOUNT);
3135 nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
3136 if (nr < 0)
3137 break;
3138 if (apply_in_reverse)
3139 reverse_patches(patch);
3140 if (prefix)
3141 prefix_patches(patch);
3142 if (use_patch(patch)) {
3143 patch_stats(patch);
3144 *listp = patch;
3145 listp = &patch->next;
3146 }
3147 else {
3148 /* perhaps free it a bit better? */
3149 free(patch);
3150 skipped_patch++;
3151 }
3152 offset += nr;
3153 }
3154
3155 if (whitespace_error && (ws_error_action == die_on_ws_error))
3156 apply = 0;
3157
3158 update_index = check_index && apply;
3159 if (update_index && newfd < 0)
3160 newfd = hold_locked_index(&lock_file, 1);
3161
3162 if (check_index) {
3163 if (read_cache() < 0)
3164 die("unable to read index file");
3165 }
3166
3167 if ((check || apply) &&
3168 check_patch_list(list) < 0 &&
3169 !apply_with_reject)
3170 exit(1);
3171
3172 if (apply && write_out_results(list, skipped_patch))
3173 exit(1);
3174
3175 if (fake_ancestor)
3176 build_fake_ancestor(list, fake_ancestor);
3177
3178 if (diffstat)
3179 stat_patch_list(list);
3180
3181 if (numstat)
3182 numstat_patch_list(list);
3183
3184 if (summary)
3185 summary_patch_list(list);
3186
3187 strbuf_release(&buf);
3188 return 0;
3189 }
3190
3191 static int git_apply_config(const char *var, const char *value, void *cb)
3192 {
3193 if (!strcmp(var, "apply.whitespace"))
3194 return git_config_string(&apply_default_whitespace, var, value);
3195 return git_default_config(var, value, cb);
3196 }
3197
3198 static int option_parse_exclude(const struct option *opt,
3199 const char *arg, int unset)
3200 {
3201 add_name_limit(arg, 1);
3202 return 0;
3203 }
3204
3205 static int option_parse_include(const struct option *opt,
3206 const char *arg, int unset)
3207 {
3208 add_name_limit(arg, 0);
3209 has_include = 1;
3210 return 0;
3211 }
3212
3213 static int option_parse_p(const struct option *opt,
3214 const char *arg, int unset)
3215 {
3216 p_value = atoi(arg);
3217 p_value_known = 1;
3218 return 0;
3219 }
3220
3221 static int option_parse_z(const struct option *opt,
3222 const char *arg, int unset)
3223 {
3224 if (unset)
3225 line_termination = '\n';
3226 else
3227 line_termination = 0;
3228 return 0;
3229 }
3230
3231 static int option_parse_whitespace(const struct option *opt,
3232 const char *arg, int unset)
3233 {
3234 const char **whitespace_option = opt->value;
3235
3236 *whitespace_option = arg;
3237 parse_whitespace_option(arg);
3238 return 0;
3239 }
3240
3241 static int option_parse_directory(const struct option *opt,
3242 const char *arg, int unset)
3243 {
3244 root_len = strlen(arg);
3245 if (root_len && arg[root_len - 1] != '/') {
3246 char *new_root;
3247 root = new_root = xmalloc(root_len + 2);
3248 strcpy(new_root, arg);
3249 strcpy(new_root + root_len++, "/");
3250 } else
3251 root = arg;
3252 return 0;
3253 }
3254
3255 int cmd_apply(int argc, const char **argv, const char *unused_prefix)
3256 {
3257 int i;
3258 int errs = 0;
3259 int is_not_gitdir;
3260 int binary;
3261 int force_apply = 0;
3262
3263 const char *whitespace_option = NULL;
3264
3265 struct option builtin_apply_options[] = {
3266 { OPTION_CALLBACK, 0, "exclude", NULL, "path",
3267 "don't apply changes matching the given path",
3268 0, option_parse_exclude },
3269 { OPTION_CALLBACK, 0, "include", NULL, "path",
3270 "apply changes matching the given path",
3271 0, option_parse_include },
3272 { OPTION_CALLBACK, 'p', NULL, NULL, "num",
3273 "remove <num> leading slashes from traditional diff paths",
3274 0, option_parse_p },
3275 OPT_BOOLEAN(0, "no-add", &no_add,
3276 "ignore additions made by the patch"),
3277 OPT_BOOLEAN(0, "stat", &diffstat,
3278 "instead of applying the patch, output diffstat for the input"),
3279 { OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,
3280 NULL, "old option, now no-op",
3281 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3282 { OPTION_BOOLEAN, 0, "binary", &binary,
3283 NULL, "old option, now no-op",
3284 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3285 OPT_BOOLEAN(0, "numstat", &numstat,
3286 "shows number of added and deleted lines in decimal notation"),
3287 OPT_BOOLEAN(0, "summary", &summary,
3288 "instead of applying the patch, output a summary for the input"),
3289 OPT_BOOLEAN(0, "check", &check,
3290 "instead of applying the patch, see if the patch is applicable"),
3291 OPT_BOOLEAN(0, "index", &check_index,
3292 "make sure the patch is applicable to the current index"),
3293 OPT_BOOLEAN(0, "cached", &cached,
3294 "apply a patch without touching the working tree"),
3295 OPT_BOOLEAN(0, "apply", &force_apply,
3296 "also apply the patch (use with --stat/--summary/--check)"),
3297 OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
3298 "build a temporary index based on embedded index information"),
3299 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
3300 "paths are separated with NUL character",
3301 PARSE_OPT_NOARG, option_parse_z },
3302 OPT_INTEGER('C', NULL, &p_context,
3303 "ensure at least <n> lines of context match"),
3304 { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, "action",
3305 "detect new or modified lines that have whitespace errors",
3306 0, option_parse_whitespace },
3307 OPT_BOOLEAN('R', "reverse", &apply_in_reverse,
3308 "apply the patch in reverse"),
3309 OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero,
3310 "don't expect at least one line of context"),
3311 OPT_BOOLEAN(0, "reject", &apply_with_reject,
3312 "leave the rejected hunks in corresponding *.rej files"),
3313 OPT__VERBOSE(&apply_verbosely),
3314 OPT_BIT(0, "inaccurate-eof", &options,
3315 "tolerate incorrectly detected missing new-line at the end of file",
3316 INACCURATE_EOF),
3317 OPT_BIT(0, "recount", &options,
3318 "do not trust the line counts in the hunk headers",
3319 RECOUNT),
3320 { OPTION_CALLBACK, 0, "directory", NULL, "root",
3321 "prepend <root> to all filenames",
3322 0, option_parse_directory },
3323 OPT_END()
3324 };
3325
3326 prefix = setup_git_directory_gently(&is_not_gitdir);
3327 prefix_length = prefix ? strlen(prefix) : 0;
3328 git_config(git_apply_config, NULL);
3329 if (apply_default_whitespace)
3330 parse_whitespace_option(apply_default_whitespace);
3331
3332 argc = parse_options(argc, argv, prefix, builtin_apply_options,
3333 apply_usage, 0);
3334
3335 if (apply_with_reject)
3336 apply = apply_verbosely = 1;
3337 if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))
3338 apply = 0;
3339 if (check_index && is_not_gitdir)
3340 die("--index outside a repository");
3341 if (cached) {
3342 if (is_not_gitdir)
3343 die("--cached outside a repository");
3344 check_index = 1;
3345 }
3346 for (i = 0; i < argc; i++) {
3347 const char *arg = argv[i];
3348 int fd;
3349
3350 if (!strcmp(arg, "-")) {
3351 errs |= apply_patch(0, "<stdin>", options);
3352 read_stdin = 0;
3353 continue;
3354 } else if (0 < prefix_length)
3355 arg = prefix_filename(prefix, prefix_length, arg);
3356
3357 fd = open(arg, O_RDONLY);
3358 if (fd < 0)
3359 die("can't open patch '%s': %s", arg, strerror(errno));
3360 read_stdin = 0;
3361 set_default_whitespace_mode(whitespace_option);
3362 errs |= apply_patch(fd, arg, options);
3363 close(fd);
3364 }
3365 set_default_whitespace_mode(whitespace_option);
3366 if (read_stdin)
3367 errs |= apply_patch(0, "<stdin>", options);
3368 if (whitespace_error) {
3369 if (squelch_whitespace_errors &&
3370 squelch_whitespace_errors < whitespace_error) {
3371 int squelched =
3372 whitespace_error - squelch_whitespace_errors;
3373 warning("squelched %d "
3374 "whitespace error%s",
3375 squelched,
3376 squelched == 1 ? "" : "s");
3377 }
3378 if (ws_error_action == die_on_ws_error)
3379 die("%d line%s add%s whitespace errors.",
3380 whitespace_error,
3381 whitespace_error == 1 ? "" : "s",
3382 whitespace_error == 1 ? "s" : "");
3383 if (applied_after_fixing_ws && apply)
3384 warning("%d line%s applied after"
3385 " fixing whitespace errors.",
3386 applied_after_fixing_ws,
3387 applied_after_fixing_ws == 1 ? "" : "s");
3388 else if (whitespace_error)
3389 warning("%d line%s add%s whitespace errors.",
3390 whitespace_error,
3391 whitespace_error == 1 ? "" : "s",
3392 whitespace_error == 1 ? "s" : "");
3393 }
3394
3395 if (update_index) {
3396 if (write_cache(newfd, active_cache, active_nr) ||
3397 commit_locked_index(&lock_file))
3398 die("Unable to write new index file");
3399 }
3400
3401 return !!errs;
3402 }