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