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