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