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