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