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