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