]>
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 | */ |
d854f783 | 9 | #include <fnmatch.h> |
c1bb9350 | 10 | #include "cache.h" |
03ac6e64 | 11 | #include "cache-tree.h" |
22943f1a | 12 | #include "quote.h" |
8e440259 | 13 | #include "blob.h" |
051308f6 | 14 | #include "delta.h" |
ac6245e3 | 15 | #include "builtin.h" |
c1bb9350 | 16 | |
a9486b02 PR |
17 | /* |
18 | * --check turns on checking that the working tree matches the | |
19 | * files that are being modified, but doesn't apply the patch | |
20 | * --stat does just a diffstat, and doesn't actually apply | |
21 | * --numstat does numeric diffstat, and doesn't actually apply | |
22 | * --index-info shows the old and new index info for paths if available. | |
23 | * --index updates the cache as well. | |
24 | * --cached updates only the cache without ever touching the working tree. | |
25 | */ | |
edf2e370 JH |
26 | static const char *prefix; |
27 | static int prefix_length = -1; | |
dbd0f7d3 | 28 | static int newfd = -1; |
edf2e370 | 29 | |
e36f8b60 | 30 | static int p_value = 1; |
011f4274 | 31 | static int allow_binary_replacement = 0; |
3cca928d | 32 | static int check_index = 0; |
5aa7d94c | 33 | static int write_index = 0; |
04e4888e | 34 | static int cached = 0; |
fab2c257 | 35 | static int diffstat = 0; |
7d8b7c21 | 36 | static int numstat = 0; |
96c912a4 | 37 | static int summary = 0; |
a577284a LT |
38 | static int check = 0; |
39 | static int apply = 1; | |
cb93c193 | 40 | static int no_add = 0; |
2cf67f1e | 41 | static int show_index_info = 0; |
22943f1a | 42 | static int line_termination = '\n'; |
47495887 | 43 | static unsigned long p_context = -1; |
12dd6e8c | 44 | static const char apply_usage[] = |
04e4888e | 45 | "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>..."; |
c1bb9350 | 46 | |
19bfcd5a | 47 | static enum whitespace_eol { |
2ae1c53b | 48 | nowarn_whitespace, |
19bfcd5a | 49 | warn_on_whitespace, |
b5767dd6 | 50 | error_on_whitespace, |
2ae1c53b | 51 | strip_whitespace, |
621603b7 | 52 | } new_whitespace = warn_on_whitespace; |
b5767dd6 | 53 | static int whitespace_error = 0; |
fc96b7c9 JH |
54 | static int squelch_whitespace_errors = 5; |
55 | static int applied_after_stripping = 0; | |
b5767dd6 | 56 | static const char *patch_input_file = NULL; |
19bfcd5a | 57 | |
2ae1c53b JH |
58 | static void parse_whitespace_option(const char *option) |
59 | { | |
60 | if (!option) { | |
621603b7 | 61 | new_whitespace = warn_on_whitespace; |
2ae1c53b JH |
62 | return; |
63 | } | |
64 | if (!strcmp(option, "warn")) { | |
65 | new_whitespace = warn_on_whitespace; | |
66 | return; | |
67 | } | |
621603b7 JH |
68 | if (!strcmp(option, "nowarn")) { |
69 | new_whitespace = nowarn_whitespace; | |
70 | return; | |
71 | } | |
2ae1c53b JH |
72 | if (!strcmp(option, "error")) { |
73 | new_whitespace = error_on_whitespace; | |
74 | return; | |
75 | } | |
76 | if (!strcmp(option, "error-all")) { | |
77 | new_whitespace = error_on_whitespace; | |
78 | squelch_whitespace_errors = 0; | |
79 | return; | |
80 | } | |
81 | if (!strcmp(option, "strip")) { | |
82 | new_whitespace = strip_whitespace; | |
83 | return; | |
84 | } | |
85 | die("unrecognized whitespace option '%s'", option); | |
86 | } | |
87 | ||
f21d6726 JH |
88 | static void set_default_whitespace_mode(const char *whitespace_option) |
89 | { | |
90 | if (!whitespace_option && !apply_default_whitespace) { | |
91 | new_whitespace = (apply | |
92 | ? warn_on_whitespace | |
93 | : nowarn_whitespace); | |
94 | } | |
95 | } | |
96 | ||
3f40315a LT |
97 | /* |
98 | * For "diff-stat" like behaviour, we keep track of the biggest change | |
99 | * we've seen, and the longest filename. That allows us to do simple | |
100 | * scaling. | |
101 | */ | |
102 | static int max_change, max_len; | |
103 | ||
a4acb0eb LT |
104 | /* |
105 | * Various "current state", notably line numbers and what | |
106 | * file (and how) we're patching right now.. The "is_xxxx" | |
107 | * things are flags, where -1 means "don't know yet". | |
108 | */ | |
46979f56 | 109 | static int linenr = 1; |
19c58fb8 LT |
110 | |
111 | struct fragment { | |
47495887 | 112 | unsigned long leading, trailing; |
19c58fb8 LT |
113 | unsigned long oldpos, oldlines; |
114 | unsigned long newpos, newlines; | |
115 | const char *patch; | |
116 | int size; | |
117 | struct fragment *next; | |
118 | }; | |
119 | ||
120 | struct patch { | |
5041aa70 | 121 | char *new_name, *old_name, *def_name; |
19c58fb8 | 122 | unsigned int old_mode, new_mode; |
e5a94313 | 123 | int is_rename, is_copy, is_new, is_delete, is_binary, is_reverse; |
0660626c JH |
124 | #define BINARY_DELTA_DEFLATED 1 |
125 | #define BINARY_LITERAL_DEFLATED 2 | |
126 | unsigned long deflate_origlen; | |
3f40315a | 127 | int lines_added, lines_deleted; |
96c912a4 | 128 | int score; |
3eaa38da | 129 | int inaccurate_eof:1; |
19c58fb8 | 130 | struct fragment *fragments; |
5aa7d94c | 131 | char *result; |
3cca928d | 132 | unsigned long resultsize; |
2cf67f1e JH |
133 | char old_sha1_prefix[41]; |
134 | char new_sha1_prefix[41]; | |
19c58fb8 LT |
135 | struct patch *next; |
136 | }; | |
46979f56 | 137 | |
c1bb9350 | 138 | #define CHUNKSIZE (8192) |
a4acb0eb | 139 | #define SLOP (16) |
c1bb9350 LT |
140 | |
141 | static void *read_patch_file(int fd, unsigned long *sizep) | |
142 | { | |
143 | unsigned long size = 0, alloc = CHUNKSIZE; | |
144 | void *buffer = xmalloc(alloc); | |
145 | ||
146 | for (;;) { | |
147 | int nr = alloc - size; | |
148 | if (nr < 1024) { | |
149 | alloc += CHUNKSIZE; | |
150 | buffer = xrealloc(buffer, alloc); | |
151 | nr = alloc - size; | |
152 | } | |
1d7f171c | 153 | nr = xread(fd, (char *) buffer + size, nr); |
c1bb9350 LT |
154 | if (!nr) |
155 | break; | |
1c15afb9 | 156 | if (nr < 0) |
c1bb9350 | 157 | die("git-apply: read returned %s", strerror(errno)); |
c1bb9350 LT |
158 | size += nr; |
159 | } | |
160 | *sizep = size; | |
a4acb0eb LT |
161 | |
162 | /* | |
163 | * Make sure that we have some slop in the buffer | |
164 | * so that we can do speculative "memcmp" etc, and | |
165 | * see to it that it is NUL-filled. | |
166 | */ | |
167 | if (alloc < size + SLOP) | |
168 | buffer = xrealloc(buffer, size + SLOP); | |
1d7f171c | 169 | memset((char *) buffer + size, 0, SLOP); |
c1bb9350 LT |
170 | return buffer; |
171 | } | |
172 | ||
3cca928d | 173 | static unsigned long linelen(const char *buffer, unsigned long size) |
c1bb9350 LT |
174 | { |
175 | unsigned long len = 0; | |
176 | while (size--) { | |
177 | len++; | |
178 | if (*buffer++ == '\n') | |
179 | break; | |
180 | } | |
181 | return len; | |
182 | } | |
183 | ||
a4acb0eb LT |
184 | static int is_dev_null(const char *str) |
185 | { | |
186 | return !memcmp("/dev/null", str, 9) && isspace(str[9]); | |
187 | } | |
188 | ||
381ca9a3 LT |
189 | #define TERM_SPACE 1 |
190 | #define TERM_TAB 2 | |
9a4a100e LT |
191 | |
192 | static int name_terminate(const char *name, int namelen, int c, int terminate) | |
193 | { | |
194 | if (c == ' ' && !(terminate & TERM_SPACE)) | |
195 | return 0; | |
196 | if (c == '\t' && !(terminate & TERM_TAB)) | |
197 | return 0; | |
198 | ||
9a4a100e LT |
199 | return 1; |
200 | } | |
201 | ||
202 | static char * find_name(const char *line, char *def, int p_value, int terminate) | |
c1bb9350 | 203 | { |
a4acb0eb LT |
204 | int len; |
205 | const char *start = line; | |
206 | char *name; | |
207 | ||
22943f1a JH |
208 | if (*line == '"') { |
209 | /* Proposed "new-style" GNU patch/diff format; see | |
210 | * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2 | |
211 | */ | |
212 | name = unquote_c_style(line, NULL); | |
213 | if (name) { | |
214 | char *cp = name; | |
215 | while (p_value) { | |
216 | cp = strchr(name, '/'); | |
217 | if (!cp) | |
218 | break; | |
219 | cp++; | |
220 | p_value--; | |
221 | } | |
222 | if (cp) { | |
223 | /* name can later be freed, so we need | |
224 | * to memmove, not just return cp | |
225 | */ | |
226 | memmove(name, cp, strlen(cp) + 1); | |
227 | free(def); | |
228 | return name; | |
229 | } | |
230 | else { | |
231 | free(name); | |
232 | name = NULL; | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
c1bb9350 | 237 | for (;;) { |
a4acb0eb | 238 | char c = *line; |
9a4a100e LT |
239 | |
240 | if (isspace(c)) { | |
241 | if (c == '\n') | |
242 | break; | |
243 | if (name_terminate(start, line-start, c, terminate)) | |
244 | break; | |
245 | } | |
a4acb0eb LT |
246 | line++; |
247 | if (c == '/' && !--p_value) | |
248 | start = line; | |
249 | } | |
250 | if (!start) | |
251 | return def; | |
252 | len = line - start; | |
253 | if (!len) | |
254 | return def; | |
255 | ||
256 | /* | |
257 | * Generally we prefer the shorter name, especially | |
258 | * if the other one is just a variation of that with | |
259 | * something else tacked on to the end (ie "file.orig" | |
260 | * or "file~"). | |
261 | */ | |
262 | if (def) { | |
263 | int deflen = strlen(def); | |
264 | if (deflen < len && !strncmp(start, def, deflen)) | |
265 | return def; | |
c1bb9350 | 266 | } |
a4acb0eb LT |
267 | |
268 | name = xmalloc(len + 1); | |
269 | memcpy(name, start, len); | |
270 | name[len] = 0; | |
271 | free(def); | |
272 | return name; | |
273 | } | |
274 | ||
275 | /* | |
276 | * Get the name etc info from the --/+++ lines of a traditional patch header | |
277 | * | |
278 | * NOTE! This hardcodes "-p1" behaviour in filename detection. | |
9a4a100e LT |
279 | * |
280 | * FIXME! The end-of-filename heuristics are kind of screwy. For existing | |
281 | * files, we can happily check the index for a match, but for creating a | |
282 | * new file we should try to match whatever "patch" does. I have no idea. | |
a4acb0eb | 283 | */ |
19c58fb8 | 284 | static void parse_traditional_patch(const char *first, const char *second, struct patch *patch) |
a4acb0eb | 285 | { |
a4acb0eb LT |
286 | char *name; |
287 | ||
a9486b02 PR |
288 | first += 4; /* skip "--- " */ |
289 | second += 4; /* skip "+++ " */ | |
a4acb0eb | 290 | if (is_dev_null(first)) { |
19c58fb8 LT |
291 | patch->is_new = 1; |
292 | patch->is_delete = 0; | |
5041aa70 | 293 | name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB); |
19c58fb8 | 294 | patch->new_name = name; |
a4acb0eb | 295 | } else if (is_dev_null(second)) { |
19c58fb8 LT |
296 | patch->is_new = 0; |
297 | patch->is_delete = 1; | |
381ca9a3 | 298 | name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); |
19c58fb8 | 299 | patch->old_name = name; |
a4acb0eb | 300 | } else { |
381ca9a3 LT |
301 | name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); |
302 | name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB); | |
19c58fb8 | 303 | patch->old_name = patch->new_name = name; |
a4acb0eb LT |
304 | } |
305 | if (!name) | |
306 | die("unable to find filename in patch at line %d", linenr); | |
a4acb0eb LT |
307 | } |
308 | ||
19c58fb8 | 309 | static int gitdiff_hdrend(const char *line, struct patch *patch) |
a4acb0eb LT |
310 | { |
311 | return -1; | |
312 | } | |
313 | ||
1e3f6b6e LT |
314 | /* |
315 | * We're anal about diff header consistency, to make | |
316 | * sure that we don't end up having strange ambiguous | |
317 | * patches floating around. | |
318 | * | |
319 | * As a result, gitdiff_{old|new}name() will check | |
320 | * their names against any previous information, just | |
321 | * to make sure.. | |
322 | */ | |
323 | static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew) | |
324 | { | |
1e3f6b6e LT |
325 | if (!orig_name && !isnull) |
326 | return find_name(line, NULL, 1, 0); | |
327 | ||
1e3f6b6e | 328 | if (orig_name) { |
22943f1a JH |
329 | int len; |
330 | const char *name; | |
331 | char *another; | |
1e3f6b6e LT |
332 | name = orig_name; |
333 | len = strlen(name); | |
334 | if (isnull) | |
335 | die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); | |
22943f1a JH |
336 | another = find_name(line, NULL, 1, 0); |
337 | if (!another || memcmp(another, name, len)) | |
338 | die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); | |
339 | free(another); | |
1e3f6b6e LT |
340 | return orig_name; |
341 | } | |
22943f1a JH |
342 | else { |
343 | /* expect "/dev/null" */ | |
344 | if (memcmp("/dev/null", line, 9) || line[9] != '\n') | |
345 | die("git-apply: bad git-diff - expected /dev/null on line %d", linenr); | |
346 | return NULL; | |
347 | } | |
1e3f6b6e LT |
348 | } |
349 | ||
19c58fb8 | 350 | static int gitdiff_oldname(const char *line, struct patch *patch) |
a4acb0eb | 351 | { |
19c58fb8 | 352 | patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old"); |
a4acb0eb LT |
353 | return 0; |
354 | } | |
355 | ||
19c58fb8 | 356 | static int gitdiff_newname(const char *line, struct patch *patch) |
a4acb0eb | 357 | { |
19c58fb8 | 358 | patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new"); |
a4acb0eb LT |
359 | return 0; |
360 | } | |
361 | ||
19c58fb8 | 362 | static int gitdiff_oldmode(const char *line, struct patch *patch) |
a4acb0eb | 363 | { |
19c58fb8 | 364 | patch->old_mode = strtoul(line, NULL, 8); |
a4acb0eb LT |
365 | return 0; |
366 | } | |
367 | ||
19c58fb8 | 368 | static int gitdiff_newmode(const char *line, struct patch *patch) |
a4acb0eb | 369 | { |
19c58fb8 | 370 | patch->new_mode = strtoul(line, NULL, 8); |
a4acb0eb LT |
371 | return 0; |
372 | } | |
373 | ||
19c58fb8 | 374 | static int gitdiff_delete(const char *line, struct patch *patch) |
a4acb0eb | 375 | { |
19c58fb8 | 376 | patch->is_delete = 1; |
5041aa70 | 377 | patch->old_name = patch->def_name; |
19c58fb8 | 378 | return gitdiff_oldmode(line, patch); |
a4acb0eb LT |
379 | } |
380 | ||
19c58fb8 | 381 | static int gitdiff_newfile(const char *line, struct patch *patch) |
a4acb0eb | 382 | { |
19c58fb8 | 383 | patch->is_new = 1; |
5041aa70 | 384 | patch->new_name = patch->def_name; |
19c58fb8 | 385 | return gitdiff_newmode(line, patch); |
a4acb0eb LT |
386 | } |
387 | ||
19c58fb8 | 388 | static int gitdiff_copysrc(const char *line, struct patch *patch) |
a4acb0eb | 389 | { |
19c58fb8 LT |
390 | patch->is_copy = 1; |
391 | patch->old_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
392 | return 0; |
393 | } | |
394 | ||
19c58fb8 | 395 | static int gitdiff_copydst(const char *line, struct patch *patch) |
a4acb0eb | 396 | { |
19c58fb8 LT |
397 | patch->is_copy = 1; |
398 | patch->new_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
399 | return 0; |
400 | } | |
401 | ||
19c58fb8 | 402 | static int gitdiff_renamesrc(const char *line, struct patch *patch) |
a4acb0eb | 403 | { |
19c58fb8 LT |
404 | patch->is_rename = 1; |
405 | patch->old_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
406 | return 0; |
407 | } | |
408 | ||
19c58fb8 | 409 | static int gitdiff_renamedst(const char *line, struct patch *patch) |
a4acb0eb | 410 | { |
19c58fb8 LT |
411 | patch->is_rename = 1; |
412 | patch->new_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
413 | return 0; |
414 | } | |
415 | ||
19c58fb8 | 416 | static int gitdiff_similarity(const char *line, struct patch *patch) |
a4acb0eb | 417 | { |
96c912a4 JH |
418 | if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) |
419 | patch->score = 0; | |
a4acb0eb | 420 | return 0; |
c1bb9350 LT |
421 | } |
422 | ||
70aadac0 JH |
423 | static int gitdiff_dissimilarity(const char *line, struct patch *patch) |
424 | { | |
96c912a4 JH |
425 | if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) |
426 | patch->score = 0; | |
70aadac0 JH |
427 | return 0; |
428 | } | |
429 | ||
2cf67f1e JH |
430 | static int gitdiff_index(const char *line, struct patch *patch) |
431 | { | |
432 | /* index line is N hexadecimal, "..", N hexadecimal, | |
433 | * and optional space with octal mode. | |
434 | */ | |
435 | const char *ptr, *eol; | |
436 | int len; | |
437 | ||
438 | ptr = strchr(line, '.'); | |
9add69b1 | 439 | if (!ptr || ptr[1] != '.' || 40 < ptr - line) |
2cf67f1e JH |
440 | return 0; |
441 | len = ptr - line; | |
442 | memcpy(patch->old_sha1_prefix, line, len); | |
443 | patch->old_sha1_prefix[len] = 0; | |
444 | ||
445 | line = ptr + 2; | |
446 | ptr = strchr(line, ' '); | |
447 | eol = strchr(line, '\n'); | |
448 | ||
449 | if (!ptr || eol < ptr) | |
450 | ptr = eol; | |
451 | len = ptr - line; | |
452 | ||
9add69b1 | 453 | if (40 < len) |
2cf67f1e JH |
454 | return 0; |
455 | memcpy(patch->new_sha1_prefix, line, len); | |
456 | patch->new_sha1_prefix[len] = 0; | |
457 | if (*ptr == ' ') | |
458 | patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8); | |
459 | return 0; | |
460 | } | |
461 | ||
9a4a100e LT |
462 | /* |
463 | * This is normal for a diff that doesn't change anything: we'll fall through | |
464 | * into the next diff. Tell the parser to break out. | |
465 | */ | |
19c58fb8 | 466 | static int gitdiff_unrecognized(const char *line, struct patch *patch) |
9a4a100e LT |
467 | { |
468 | return -1; | |
469 | } | |
470 | ||
22943f1a JH |
471 | static const char *stop_at_slash(const char *line, int llen) |
472 | { | |
473 | int i; | |
474 | ||
475 | for (i = 0; i < llen; i++) { | |
476 | int ch = line[i]; | |
477 | if (ch == '/') | |
478 | return line + i; | |
479 | } | |
480 | return NULL; | |
481 | } | |
482 | ||
483 | /* This is to extract the same name that appears on "diff --git" | |
484 | * line. We do not find and return anything if it is a rename | |
485 | * patch, and it is OK because we will find the name elsewhere. | |
486 | * We need to reliably find name only when it is mode-change only, | |
487 | * creation or deletion of an empty file. In any of these cases, | |
488 | * both sides are the same name under a/ and b/ respectively. | |
489 | */ | |
490 | static char *git_header_name(char *line, int llen) | |
5041aa70 LT |
491 | { |
492 | int len; | |
22943f1a JH |
493 | const char *name; |
494 | const char *second = NULL; | |
5041aa70 | 495 | |
22943f1a JH |
496 | line += strlen("diff --git "); |
497 | llen -= strlen("diff --git "); | |
498 | ||
499 | if (*line == '"') { | |
500 | const char *cp; | |
501 | char *first = unquote_c_style(line, &second); | |
502 | if (!first) | |
5041aa70 | 503 | return NULL; |
22943f1a JH |
504 | |
505 | /* advance to the first slash */ | |
506 | cp = stop_at_slash(first, strlen(first)); | |
507 | if (!cp || cp == first) { | |
508 | /* we do not accept absolute paths */ | |
509 | free_first_and_fail: | |
510 | free(first); | |
511 | return NULL; | |
512 | } | |
513 | len = strlen(cp+1); | |
514 | memmove(first, cp+1, len+1); /* including NUL */ | |
515 | ||
516 | /* second points at one past closing dq of name. | |
517 | * find the second name. | |
518 | */ | |
519 | while ((second < line + llen) && isspace(*second)) | |
520 | second++; | |
521 | ||
522 | if (line + llen <= second) | |
523 | goto free_first_and_fail; | |
524 | if (*second == '"') { | |
525 | char *sp = unquote_c_style(second, NULL); | |
526 | if (!sp) | |
527 | goto free_first_and_fail; | |
528 | cp = stop_at_slash(sp, strlen(sp)); | |
529 | if (!cp || cp == sp) { | |
530 | free_both_and_fail: | |
531 | free(sp); | |
532 | goto free_first_and_fail; | |
533 | } | |
534 | /* They must match, otherwise ignore */ | |
535 | if (strcmp(cp+1, first)) | |
536 | goto free_both_and_fail; | |
537 | free(sp); | |
538 | return first; | |
539 | } | |
540 | ||
541 | /* unquoted second */ | |
542 | cp = stop_at_slash(second, line + llen - second); | |
543 | if (!cp || cp == second) | |
544 | goto free_first_and_fail; | |
545 | cp++; | |
546 | if (line + llen - cp != len + 1 || | |
547 | memcmp(first, cp, len)) | |
548 | goto free_first_and_fail; | |
549 | return first; | |
5041aa70 LT |
550 | } |
551 | ||
22943f1a JH |
552 | /* unquoted first name */ |
553 | name = stop_at_slash(line, llen); | |
554 | if (!name || name == line) | |
5041aa70 LT |
555 | return NULL; |
556 | ||
22943f1a JH |
557 | name++; |
558 | ||
559 | /* since the first name is unquoted, a dq if exists must be | |
560 | * the beginning of the second name. | |
561 | */ | |
562 | for (second = name; second < line + llen; second++) { | |
563 | if (*second == '"') { | |
564 | const char *cp = second; | |
565 | const char *np; | |
566 | char *sp = unquote_c_style(second, NULL); | |
567 | ||
568 | if (!sp) | |
569 | return NULL; | |
570 | np = stop_at_slash(sp, strlen(sp)); | |
571 | if (!np || np == sp) { | |
572 | free_second_and_fail: | |
573 | free(sp); | |
574 | return NULL; | |
575 | } | |
576 | np++; | |
577 | len = strlen(np); | |
578 | if (len < cp - name && | |
579 | !strncmp(np, name, len) && | |
580 | isspace(name[len])) { | |
581 | /* Good */ | |
582 | memmove(sp, np, len + 1); | |
583 | return sp; | |
584 | } | |
585 | goto free_second_and_fail; | |
586 | } | |
587 | } | |
588 | ||
5041aa70 LT |
589 | /* |
590 | * Accept a name only if it shows up twice, exactly the same | |
591 | * form. | |
592 | */ | |
593 | for (len = 0 ; ; len++) { | |
594 | char c = name[len]; | |
595 | ||
596 | switch (c) { | |
597 | default: | |
598 | continue; | |
599 | case '\n': | |
e70a165d | 600 | return NULL; |
5041aa70 LT |
601 | case '\t': case ' ': |
602 | second = name+len; | |
603 | for (;;) { | |
604 | char c = *second++; | |
605 | if (c == '\n') | |
606 | return NULL; | |
607 | if (c == '/') | |
608 | break; | |
609 | } | |
0e87e048 | 610 | if (second[len] == '\n' && !memcmp(name, second, len)) { |
5041aa70 LT |
611 | char *ret = xmalloc(len + 1); |
612 | memcpy(ret, name, len); | |
613 | ret[len] = 0; | |
614 | return ret; | |
615 | } | |
616 | } | |
617 | } | |
618 | return NULL; | |
619 | } | |
620 | ||
c1bb9350 | 621 | /* Verify that we recognize the lines following a git header */ |
19c58fb8 | 622 | static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch) |
c1bb9350 | 623 | { |
a4acb0eb LT |
624 | unsigned long offset; |
625 | ||
626 | /* A git diff has explicit new/delete information, so we don't guess */ | |
19c58fb8 LT |
627 | patch->is_new = 0; |
628 | patch->is_delete = 0; | |
a4acb0eb | 629 | |
5041aa70 LT |
630 | /* |
631 | * Some things may not have the old name in the | |
632 | * rest of the headers anywhere (pure mode changes, | |
633 | * or removing or adding empty files), so we get | |
634 | * the default name from the header. | |
635 | */ | |
22943f1a | 636 | patch->def_name = git_header_name(line, len); |
5041aa70 | 637 | |
a4acb0eb LT |
638 | line += len; |
639 | size -= len; | |
640 | linenr++; | |
641 | for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) { | |
642 | static const struct opentry { | |
643 | const char *str; | |
19c58fb8 | 644 | int (*fn)(const char *, struct patch *); |
a4acb0eb LT |
645 | } optable[] = { |
646 | { "@@ -", gitdiff_hdrend }, | |
647 | { "--- ", gitdiff_oldname }, | |
648 | { "+++ ", gitdiff_newname }, | |
649 | { "old mode ", gitdiff_oldmode }, | |
650 | { "new mode ", gitdiff_newmode }, | |
651 | { "deleted file mode ", gitdiff_delete }, | |
652 | { "new file mode ", gitdiff_newfile }, | |
653 | { "copy from ", gitdiff_copysrc }, | |
654 | { "copy to ", gitdiff_copydst }, | |
33f4d087 LT |
655 | { "rename old ", gitdiff_renamesrc }, |
656 | { "rename new ", gitdiff_renamedst }, | |
dc938417 LT |
657 | { "rename from ", gitdiff_renamesrc }, |
658 | { "rename to ", gitdiff_renamedst }, | |
a4acb0eb | 659 | { "similarity index ", gitdiff_similarity }, |
70aadac0 | 660 | { "dissimilarity index ", gitdiff_dissimilarity }, |
2cf67f1e | 661 | { "index ", gitdiff_index }, |
9a4a100e | 662 | { "", gitdiff_unrecognized }, |
a4acb0eb LT |
663 | }; |
664 | int i; | |
c1bb9350 | 665 | |
c1bb9350 | 666 | len = linelen(line, size); |
a4acb0eb | 667 | if (!len || line[len-1] != '\n') |
c1bb9350 | 668 | break; |
b4f2a6ac | 669 | for (i = 0; i < ARRAY_SIZE(optable); i++) { |
a4acb0eb LT |
670 | const struct opentry *p = optable + i; |
671 | int oplen = strlen(p->str); | |
672 | if (len < oplen || memcmp(p->str, line, oplen)) | |
673 | continue; | |
19c58fb8 | 674 | if (p->fn(line + oplen, patch) < 0) |
a4acb0eb | 675 | return offset; |
9a4a100e | 676 | break; |
a4acb0eb | 677 | } |
c1bb9350 LT |
678 | } |
679 | ||
a4acb0eb | 680 | return offset; |
c1bb9350 LT |
681 | } |
682 | ||
fab2c257 | 683 | static int parse_num(const char *line, unsigned long *p) |
46979f56 LT |
684 | { |
685 | char *ptr; | |
fab2c257 LT |
686 | |
687 | if (!isdigit(*line)) | |
688 | return 0; | |
689 | *p = strtoul(line, &ptr, 10); | |
690 | return ptr - line; | |
691 | } | |
692 | ||
693 | static int parse_range(const char *line, int len, int offset, const char *expect, | |
694 | unsigned long *p1, unsigned long *p2) | |
695 | { | |
46979f56 LT |
696 | int digits, ex; |
697 | ||
698 | if (offset < 0 || offset >= len) | |
699 | return -1; | |
700 | line += offset; | |
701 | len -= offset; | |
702 | ||
fab2c257 LT |
703 | digits = parse_num(line, p1); |
704 | if (!digits) | |
46979f56 | 705 | return -1; |
46979f56 LT |
706 | |
707 | offset += digits; | |
708 | line += digits; | |
709 | len -= digits; | |
710 | ||
c1504628 | 711 | *p2 = 1; |
fab2c257 LT |
712 | if (*line == ',') { |
713 | digits = parse_num(line+1, p2); | |
714 | if (!digits) | |
715 | return -1; | |
716 | ||
717 | offset += digits+1; | |
718 | line += digits+1; | |
719 | len -= digits+1; | |
720 | } | |
721 | ||
46979f56 LT |
722 | ex = strlen(expect); |
723 | if (ex > len) | |
724 | return -1; | |
725 | if (memcmp(line, expect, ex)) | |
726 | return -1; | |
727 | ||
728 | return offset + ex; | |
729 | } | |
730 | ||
731 | /* | |
732 | * Parse a unified diff fragment header of the | |
733 | * form "@@ -a,b +c,d @@" | |
734 | */ | |
19c58fb8 | 735 | static int parse_fragment_header(char *line, int len, struct fragment *fragment) |
46979f56 LT |
736 | { |
737 | int offset; | |
738 | ||
739 | if (!len || line[len-1] != '\n') | |
740 | return -1; | |
741 | ||
742 | /* Figure out the number of lines in a fragment */ | |
fab2c257 LT |
743 | offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); |
744 | offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); | |
46979f56 LT |
745 | |
746 | return offset; | |
747 | } | |
748 | ||
19c58fb8 | 749 | static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch) |
c1bb9350 LT |
750 | { |
751 | unsigned long offset, len; | |
752 | ||
19c58fb8 LT |
753 | patch->is_rename = patch->is_copy = 0; |
754 | patch->is_new = patch->is_delete = -1; | |
755 | patch->old_mode = patch->new_mode = 0; | |
756 | patch->old_name = patch->new_name = NULL; | |
46979f56 | 757 | for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) { |
c1bb9350 LT |
758 | unsigned long nextlen; |
759 | ||
760 | len = linelen(line, size); | |
761 | if (!len) | |
762 | break; | |
763 | ||
764 | /* Testing this early allows us to take a few shortcuts.. */ | |
765 | if (len < 6) | |
766 | continue; | |
46979f56 LT |
767 | |
768 | /* | |
82e5a82f | 769 | * Make sure we don't find any unconnected patch fragments. |
46979f56 LT |
770 | * That's a sign that we didn't find a header, and that a |
771 | * patch has become corrupted/broken up. | |
772 | */ | |
773 | if (!memcmp("@@ -", line, 4)) { | |
19c58fb8 LT |
774 | struct fragment dummy; |
775 | if (parse_fragment_header(line, len, &dummy) < 0) | |
46979f56 | 776 | continue; |
4ec99bf0 | 777 | error("patch fragment without header at line %d: %.*s", linenr, (int)len-1, line); |
46979f56 LT |
778 | } |
779 | ||
c1bb9350 LT |
780 | if (size < len + 6) |
781 | break; | |
782 | ||
783 | /* | |
784 | * Git patch? It might not have a real patch, just a rename | |
785 | * or mode change, so we handle that specially | |
786 | */ | |
787 | if (!memcmp("diff --git ", line, 11)) { | |
19c58fb8 | 788 | int git_hdr_len = parse_git_header(line, len, size, patch); |
206de27e | 789 | if (git_hdr_len <= len) |
c1bb9350 | 790 | continue; |
b7e8039a LT |
791 | if (!patch->old_name && !patch->new_name) { |
792 | if (!patch->def_name) | |
793 | die("git diff header lacks filename information (line %d)", linenr); | |
794 | patch->old_name = patch->new_name = patch->def_name; | |
795 | } | |
a4acb0eb | 796 | *hdrsize = git_hdr_len; |
c1bb9350 LT |
797 | return offset; |
798 | } | |
799 | ||
800 | /** --- followed by +++ ? */ | |
801 | if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4)) | |
802 | continue; | |
803 | ||
804 | /* | |
805 | * We only accept unified patches, so we want it to | |
806 | * at least have "@@ -a,b +c,d @@\n", which is 14 chars | |
807 | * minimum | |
808 | */ | |
809 | nextlen = linelen(line + len, size - len); | |
810 | if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4)) | |
811 | continue; | |
812 | ||
813 | /* Ok, we'll consider it a patch */ | |
19c58fb8 | 814 | parse_traditional_patch(line, line+len, patch); |
c1bb9350 | 815 | *hdrsize = len + nextlen; |
46979f56 | 816 | linenr += 2; |
c1bb9350 LT |
817 | return offset; |
818 | } | |
819 | return -1; | |
820 | } | |
821 | ||
c1bb9350 LT |
822 | /* |
823 | * Parse a unified diff. Note that this really needs | |
824 | * to parse each fragment separately, since the only | |
825 | * way to know the difference between a "---" that is | |
826 | * part of a patch, and a "---" that starts the next | |
827 | * patch is to look at the line counts.. | |
828 | */ | |
19c58fb8 | 829 | static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment) |
c1bb9350 | 830 | { |
3f40315a | 831 | int added, deleted; |
c1bb9350 | 832 | int len = linelen(line, size), offset; |
30996652 | 833 | unsigned long oldlines, newlines; |
47495887 | 834 | unsigned long leading, trailing; |
c1bb9350 | 835 | |
19c58fb8 | 836 | offset = parse_fragment_header(line, len, fragment); |
c1bb9350 LT |
837 | if (offset < 0) |
838 | return -1; | |
19c58fb8 LT |
839 | oldlines = fragment->oldlines; |
840 | newlines = fragment->newlines; | |
47495887 EB |
841 | leading = 0; |
842 | trailing = 0; | |
c1bb9350 | 843 | |
30996652 LT |
844 | if (patch->is_new < 0) { |
845 | patch->is_new = !oldlines; | |
846 | if (!oldlines) | |
847 | patch->old_name = NULL; | |
848 | } | |
849 | if (patch->is_delete < 0) { | |
850 | patch->is_delete = !newlines; | |
851 | if (!newlines) | |
852 | patch->new_name = NULL; | |
853 | } | |
854 | ||
3103cf9e | 855 | if (patch->is_new && oldlines) |
30996652 | 856 | return error("new file depends on old contents"); |
af3f929f LT |
857 | if (patch->is_delete != !newlines) { |
858 | if (newlines) | |
859 | return error("deleted file still has contents"); | |
860 | fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name); | |
861 | } | |
a4acb0eb | 862 | |
c1bb9350 LT |
863 | /* Parse the thing.. */ |
864 | line += len; | |
865 | size -= len; | |
46979f56 | 866 | linenr++; |
3f40315a | 867 | added = deleted = 0; |
46979f56 | 868 | for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) { |
c1bb9350 LT |
869 | if (!oldlines && !newlines) |
870 | break; | |
871 | len = linelen(line, size); | |
872 | if (!len || line[len-1] != '\n') | |
873 | return -1; | |
874 | switch (*line) { | |
875 | default: | |
876 | return -1; | |
877 | case ' ': | |
878 | oldlines--; | |
879 | newlines--; | |
47495887 EB |
880 | if (!deleted && !added) |
881 | leading++; | |
882 | trailing++; | |
c1bb9350 LT |
883 | break; |
884 | case '-': | |
3f40315a | 885 | deleted++; |
c1bb9350 | 886 | oldlines--; |
47495887 | 887 | trailing = 0; |
c1bb9350 LT |
888 | break; |
889 | case '+': | |
19bfcd5a LT |
890 | /* |
891 | * We know len is at least two, since we have a '+' and | |
b5767dd6 JH |
892 | * we checked that the last character was a '\n' above. |
893 | * That is, an addition of an empty line would check | |
894 | * the '+' here. Sneaky... | |
19bfcd5a | 895 | */ |
2ae1c53b | 896 | if ((new_whitespace != nowarn_whitespace) && |
b5767dd6 | 897 | isspace(line[len-2])) { |
fc96b7c9 JH |
898 | whitespace_error++; |
899 | if (squelch_whitespace_errors && | |
900 | squelch_whitespace_errors < | |
901 | whitespace_error) | |
902 | ; | |
903 | else { | |
904 | fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n", | |
905 | patch_input_file, | |
906 | linenr, len-2, line+1); | |
907 | } | |
19bfcd5a | 908 | } |
3f40315a | 909 | added++; |
c1bb9350 | 910 | newlines--; |
47495887 | 911 | trailing = 0; |
c1bb9350 | 912 | break; |
433ef8a2 FK |
913 | |
914 | /* We allow "\ No newline at end of file". Depending | |
915 | * on locale settings when the patch was produced we | |
916 | * don't know what this line looks like. The only | |
56d33b11 JH |
917 | * thing we do know is that it begins with "\ ". |
918 | * Checking for 12 is just for sanity check -- any | |
919 | * l10n of "\ No newline..." is at least that long. | |
920 | */ | |
fab2c257 | 921 | case '\\': |
433ef8a2 | 922 | if (len < 12 || memcmp(line, "\\ ", 2)) |
3cca928d | 923 | return -1; |
fab2c257 | 924 | break; |
c1bb9350 LT |
925 | } |
926 | } | |
c1504628 LT |
927 | if (oldlines || newlines) |
928 | return -1; | |
47495887 EB |
929 | fragment->leading = leading; |
930 | fragment->trailing = trailing; | |
931 | ||
8b64647d JH |
932 | /* If a fragment ends with an incomplete line, we failed to include |
933 | * it in the above loop because we hit oldlines == newlines == 0 | |
934 | * before seeing it. | |
935 | */ | |
433ef8a2 | 936 | if (12 < size && !memcmp(line, "\\ ", 2)) |
8b64647d JH |
937 | offset += linelen(line, size); |
938 | ||
3f40315a LT |
939 | patch->lines_added += added; |
940 | patch->lines_deleted += deleted; | |
c1bb9350 LT |
941 | return offset; |
942 | } | |
943 | ||
19c58fb8 | 944 | static int parse_single_patch(char *line, unsigned long size, struct patch *patch) |
c1bb9350 LT |
945 | { |
946 | unsigned long offset = 0; | |
19c58fb8 | 947 | struct fragment **fragp = &patch->fragments; |
c1bb9350 LT |
948 | |
949 | while (size > 4 && !memcmp(line, "@@ -", 4)) { | |
19c58fb8 LT |
950 | struct fragment *fragment; |
951 | int len; | |
952 | ||
90321c10 | 953 | fragment = xcalloc(1, sizeof(*fragment)); |
19c58fb8 | 954 | len = parse_fragment(line, size, patch, fragment); |
c1bb9350 | 955 | if (len <= 0) |
46979f56 | 956 | die("corrupt patch at line %d", linenr); |
c1bb9350 | 957 | |
19c58fb8 LT |
958 | fragment->patch = line; |
959 | fragment->size = len; | |
960 | ||
961 | *fragp = fragment; | |
962 | fragp = &fragment->next; | |
c1bb9350 LT |
963 | |
964 | offset += len; | |
965 | line += len; | |
966 | size -= len; | |
967 | } | |
968 | return offset; | |
969 | } | |
970 | ||
1fea629f LT |
971 | static inline int metadata_changes(struct patch *patch) |
972 | { | |
973 | return patch->is_rename > 0 || | |
974 | patch->is_copy > 0 || | |
975 | patch->is_new > 0 || | |
976 | patch->is_delete || | |
977 | (patch->old_mode && patch->new_mode && | |
978 | patch->old_mode != patch->new_mode); | |
979 | } | |
980 | ||
051308f6 JH |
981 | static int parse_binary(char *buffer, unsigned long size, struct patch *patch) |
982 | { | |
0660626c JH |
983 | /* We have read "GIT binary patch\n"; what follows is a line |
984 | * that says the patch method (currently, either "deflated | |
985 | * literal" or "deflated delta") and the length of data before | |
986 | * deflating; a sequence of 'length-byte' followed by base-85 | |
987 | * encoded data follows. | |
051308f6 JH |
988 | * |
989 | * Each 5-byte sequence of base-85 encodes up to 4 bytes, | |
990 | * and we would limit the patch line to 66 characters, | |
991 | * so one line can fit up to 13 groups that would decode | |
992 | * to 52 bytes max. The length byte 'A'-'Z' corresponds | |
993 | * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes. | |
82e5a82f | 994 | * The end of binary is signaled with an empty line. |
051308f6 JH |
995 | */ |
996 | int llen, used; | |
997 | struct fragment *fragment; | |
0660626c | 998 | char *data = NULL; |
051308f6 | 999 | |
051308f6 | 1000 | patch->fragments = fragment = xcalloc(1, sizeof(*fragment)); |
0660626c JH |
1001 | |
1002 | /* Grab the type of patch */ | |
1003 | llen = linelen(buffer, size); | |
1004 | used = llen; | |
1005 | linenr++; | |
1006 | ||
1007 | if (!strncmp(buffer, "delta ", 6)) { | |
1008 | patch->is_binary = BINARY_DELTA_DEFLATED; | |
1009 | patch->deflate_origlen = strtoul(buffer + 6, NULL, 10); | |
1010 | } | |
1011 | else if (!strncmp(buffer, "literal ", 8)) { | |
1012 | patch->is_binary = BINARY_LITERAL_DEFLATED; | |
1013 | patch->deflate_origlen = strtoul(buffer + 8, NULL, 10); | |
1014 | } | |
1015 | else | |
1016 | return error("unrecognized binary patch at line %d: %.*s", | |
1017 | linenr-1, llen-1, buffer); | |
1018 | buffer += llen; | |
051308f6 JH |
1019 | while (1) { |
1020 | int byte_length, max_byte_length, newsize; | |
1021 | llen = linelen(buffer, size); | |
1022 | used += llen; | |
1023 | linenr++; | |
1024 | if (llen == 1) | |
1025 | break; | |
1026 | /* Minimum line is "A00000\n" which is 7-byte long, | |
1027 | * and the line length must be multiple of 5 plus 2. | |
1028 | */ | |
1029 | if ((llen < 7) || (llen-2) % 5) | |
1030 | goto corrupt; | |
1031 | max_byte_length = (llen - 2) / 5 * 4; | |
1032 | byte_length = *buffer; | |
1033 | if ('A' <= byte_length && byte_length <= 'Z') | |
1034 | byte_length = byte_length - 'A' + 1; | |
1035 | else if ('a' <= byte_length && byte_length <= 'z') | |
1036 | byte_length = byte_length - 'a' + 27; | |
1037 | else | |
1038 | goto corrupt; | |
1039 | /* if the input length was not multiple of 4, we would | |
1040 | * have filler at the end but the filler should never | |
1041 | * exceed 3 bytes | |
1042 | */ | |
1043 | if (max_byte_length < byte_length || | |
1044 | byte_length <= max_byte_length - 4) | |
1045 | goto corrupt; | |
1046 | newsize = fragment->size + byte_length; | |
0660626c JH |
1047 | data = xrealloc(data, newsize); |
1048 | if (decode_85(data + fragment->size, | |
051308f6 JH |
1049 | buffer + 1, |
1050 | byte_length)) | |
1051 | goto corrupt; | |
1052 | fragment->size = newsize; | |
1053 | buffer += llen; | |
1054 | size -= llen; | |
1055 | } | |
0660626c | 1056 | fragment->patch = data; |
051308f6 JH |
1057 | return used; |
1058 | corrupt: | |
1059 | return error("corrupt binary patch at line %d: %.*s", | |
1060 | linenr-1, llen-1, buffer); | |
1061 | } | |
1062 | ||
19c58fb8 | 1063 | static int parse_chunk(char *buffer, unsigned long size, struct patch *patch) |
c1bb9350 LT |
1064 | { |
1065 | int hdrsize, patchsize; | |
19c58fb8 | 1066 | int offset = find_header(buffer, size, &hdrsize, patch); |
c1bb9350 LT |
1067 | |
1068 | if (offset < 0) | |
1069 | return offset; | |
c1bb9350 | 1070 | |
19c58fb8 | 1071 | patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch); |
c1bb9350 | 1072 | |
92927ed0 | 1073 | if (!patchsize) { |
3200d1ae JH |
1074 | static const char *binhdr[] = { |
1075 | "Binary files ", | |
1076 | "Files ", | |
1077 | NULL, | |
1078 | }; | |
051308f6 | 1079 | static const char git_binary[] = "GIT binary patch\n"; |
3200d1ae JH |
1080 | int i; |
1081 | int hd = hdrsize + offset; | |
1082 | unsigned long llen = linelen(buffer + hd, size - hd); | |
1083 | ||
051308f6 JH |
1084 | if (llen == sizeof(git_binary) - 1 && |
1085 | !memcmp(git_binary, buffer + hd, llen)) { | |
1086 | int used; | |
1087 | linenr++; | |
1088 | used = parse_binary(buffer + hd + llen, | |
1089 | size - hd - llen, patch); | |
1090 | if (used) | |
1091 | patchsize = used + llen; | |
1092 | else | |
1093 | patchsize = 0; | |
1094 | } | |
1095 | else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) { | |
3200d1ae JH |
1096 | for (i = 0; binhdr[i]; i++) { |
1097 | int len = strlen(binhdr[i]); | |
1098 | if (len < size - hd && | |
1099 | !memcmp(binhdr[i], buffer + hd, len)) { | |
051308f6 | 1100 | linenr++; |
3200d1ae | 1101 | patch->is_binary = 1; |
051308f6 | 1102 | patchsize = llen; |
3200d1ae JH |
1103 | break; |
1104 | } | |
1105 | } | |
051308f6 | 1106 | } |
ff36de08 | 1107 | |
92927ed0 | 1108 | /* Empty patch cannot be applied if: |
011f4274 JH |
1109 | * - it is a binary patch and we do not do binary_replace, or |
1110 | * - text patch without metadata change | |
92927ed0 JH |
1111 | */ |
1112 | if ((apply || check) && | |
011f4274 JH |
1113 | (patch->is_binary |
1114 | ? !allow_binary_replacement | |
1115 | : !metadata_changes(patch))) | |
ff36de08 JH |
1116 | die("patch with only garbage at line %d", linenr); |
1117 | } | |
1fea629f | 1118 | |
c1bb9350 LT |
1119 | return offset + hdrsize + patchsize; |
1120 | } | |
1121 | ||
e5a94313 JS |
1122 | #define swap(a,b) myswap((a),(b),sizeof(a)) |
1123 | ||
1124 | #define myswap(a, b, size) do { \ | |
1125 | unsigned char mytmp[size]; \ | |
1126 | memcpy(mytmp, &a, size); \ | |
1127 | memcpy(&a, &b, size); \ | |
1128 | memcpy(&b, mytmp, size); \ | |
1129 | } while (0) | |
1130 | ||
1131 | static void reverse_patches(struct patch *p) | |
1132 | { | |
1133 | for (; p; p = p->next) { | |
1134 | struct fragment *frag = p->fragments; | |
1135 | ||
1136 | swap(p->new_name, p->old_name); | |
1137 | swap(p->new_mode, p->old_mode); | |
1138 | swap(p->is_new, p->is_delete); | |
1139 | swap(p->lines_added, p->lines_deleted); | |
1140 | swap(p->old_sha1_prefix, p->new_sha1_prefix); | |
1141 | ||
1142 | for (; frag; frag = frag->next) { | |
1143 | swap(frag->newpos, frag->oldpos); | |
1144 | swap(frag->newlines, frag->oldlines); | |
1145 | } | |
1146 | p->is_reverse = !p->is_reverse; | |
1147 | } | |
1148 | } | |
1149 | ||
6da4016a LT |
1150 | static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; |
1151 | static const char minuses[]= "----------------------------------------------------------------------"; | |
3f40315a LT |
1152 | |
1153 | static void show_stats(struct patch *patch) | |
1154 | { | |
62917097 | 1155 | const char *prefix = ""; |
03b4538b | 1156 | char *name = patch->new_name; |
22943f1a | 1157 | char *qname = NULL; |
95bedc9e | 1158 | int len, max, add, del, total; |
3f40315a | 1159 | |
5041aa70 | 1160 | if (!name) |
03b4538b | 1161 | name = patch->old_name; |
3f40315a | 1162 | |
22943f1a JH |
1163 | if (0 < (len = quote_c_style(name, NULL, NULL, 0))) { |
1164 | qname = xmalloc(len + 1); | |
1165 | quote_c_style(name, qname, NULL, 0); | |
1166 | name = qname; | |
1167 | } | |
1168 | ||
3f40315a LT |
1169 | /* |
1170 | * "scale" the filename | |
1171 | */ | |
1172 | len = strlen(name); | |
1173 | max = max_len; | |
1174 | if (max > 50) | |
1175 | max = 50; | |
62917097 LT |
1176 | if (len > max) { |
1177 | char *slash; | |
1178 | prefix = "..."; | |
1179 | max -= 3; | |
3f40315a | 1180 | name += len - max; |
62917097 LT |
1181 | slash = strchr(name, '/'); |
1182 | if (slash) | |
1183 | name = slash; | |
1184 | } | |
3f40315a LT |
1185 | len = max; |
1186 | ||
1187 | /* | |
1188 | * scale the add/delete | |
1189 | */ | |
1190 | max = max_change; | |
1191 | if (max + len > 70) | |
1192 | max = 70 - len; | |
95bedc9e LT |
1193 | |
1194 | add = patch->lines_added; | |
1195 | del = patch->lines_deleted; | |
1196 | total = add + del; | |
1197 | ||
69f956e1 SV |
1198 | if (max_change > 0) { |
1199 | total = (total * max + max_change / 2) / max_change; | |
1200 | add = (add * max + max_change / 2) / max_change; | |
1201 | del = total - add; | |
1202 | } | |
ff36de08 JH |
1203 | if (patch->is_binary) |
1204 | printf(" %s%-*s | Bin\n", prefix, len, name); | |
1205 | else | |
1206 | printf(" %s%-*s |%5d %.*s%.*s\n", prefix, | |
1207 | len, name, patch->lines_added + patch->lines_deleted, | |
1208 | add, pluses, del, minuses); | |
22943f1a JH |
1209 | if (qname) |
1210 | free(qname); | |
3f40315a LT |
1211 | } |
1212 | ||
3cca928d LT |
1213 | static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size) |
1214 | { | |
1215 | int fd; | |
1216 | unsigned long got; | |
1217 | ||
1218 | switch (st->st_mode & S_IFMT) { | |
1219 | case S_IFLNK: | |
1220 | return readlink(path, buf, size); | |
1221 | case S_IFREG: | |
1222 | fd = open(path, O_RDONLY); | |
1223 | if (fd < 0) | |
1224 | return error("unable to open %s", path); | |
1225 | got = 0; | |
1226 | for (;;) { | |
1d7f171c | 1227 | int ret = xread(fd, (char *) buf + got, size - got); |
1c15afb9 | 1228 | if (ret <= 0) |
3cca928d LT |
1229 | break; |
1230 | got += ret; | |
1231 | } | |
1232 | close(fd); | |
1233 | return got; | |
1234 | ||
1235 | default: | |
1236 | return -1; | |
1237 | } | |
1238 | } | |
1239 | ||
47495887 | 1240 | static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines) |
3cca928d | 1241 | { |
6e7c92a9 LT |
1242 | int i; |
1243 | unsigned long start, backwards, forwards; | |
3cca928d LT |
1244 | |
1245 | if (fragsize > size) | |
1246 | return -1; | |
1247 | ||
1248 | start = 0; | |
1249 | if (line > 1) { | |
3cca928d | 1250 | unsigned long offset = 0; |
6e7c92a9 LT |
1251 | i = line-1; |
1252 | while (offset + fragsize <= size) { | |
3cca928d LT |
1253 | if (buf[offset++] == '\n') { |
1254 | start = offset; | |
6e7c92a9 | 1255 | if (!--i) |
3cca928d LT |
1256 | break; |
1257 | } | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | /* Exact line number? */ | |
1262 | if (!memcmp(buf + start, fragment, fragsize)) | |
1263 | return start; | |
1264 | ||
6e7c92a9 LT |
1265 | /* |
1266 | * There's probably some smart way to do this, but I'll leave | |
1267 | * that to the smart and beautiful people. I'm simple and stupid. | |
1268 | */ | |
1269 | backwards = start; | |
1270 | forwards = start; | |
1271 | for (i = 0; ; i++) { | |
1272 | unsigned long try; | |
1273 | int n; | |
1274 | ||
1275 | /* "backward" */ | |
1276 | if (i & 1) { | |
1277 | if (!backwards) { | |
1278 | if (forwards + fragsize > size) | |
1279 | break; | |
1280 | continue; | |
1281 | } | |
1282 | do { | |
1283 | --backwards; | |
1284 | } while (backwards && buf[backwards-1] != '\n'); | |
1285 | try = backwards; | |
1286 | } else { | |
1287 | while (forwards + fragsize <= size) { | |
1288 | if (buf[forwards++] == '\n') | |
1289 | break; | |
1290 | } | |
1291 | try = forwards; | |
1292 | } | |
1293 | ||
1294 | if (try + fragsize > size) | |
1295 | continue; | |
1296 | if (memcmp(buf + try, fragment, fragsize)) | |
1297 | continue; | |
1298 | n = (i >> 1)+1; | |
1299 | if (i & 1) | |
1300 | n = -n; | |
47495887 | 1301 | *lines = n; |
6e7c92a9 LT |
1302 | return try; |
1303 | } | |
1304 | ||
3cca928d LT |
1305 | /* |
1306 | * We should start searching forward and backward. | |
1307 | */ | |
1308 | return -1; | |
1309 | } | |
1310 | ||
47495887 EB |
1311 | static void remove_first_line(const char **rbuf, int *rsize) |
1312 | { | |
1313 | const char *buf = *rbuf; | |
1314 | int size = *rsize; | |
1315 | unsigned long offset; | |
1316 | offset = 0; | |
1317 | while (offset <= size) { | |
1318 | if (buf[offset++] == '\n') | |
1319 | break; | |
1320 | } | |
1321 | *rsize = size - offset; | |
1322 | *rbuf = buf + offset; | |
1323 | } | |
1324 | ||
1325 | static void remove_last_line(const char **rbuf, int *rsize) | |
1326 | { | |
1327 | const char *buf = *rbuf; | |
1328 | int size = *rsize; | |
1329 | unsigned long offset; | |
1330 | offset = size - 1; | |
1331 | while (offset > 0) { | |
1332 | if (buf[--offset] == '\n') | |
1333 | break; | |
1334 | } | |
1335 | *rsize = offset + 1; | |
1336 | } | |
1337 | ||
6e7c92a9 LT |
1338 | struct buffer_desc { |
1339 | char *buffer; | |
1340 | unsigned long size; | |
1341 | unsigned long alloc; | |
1342 | }; | |
1343 | ||
b5767dd6 JH |
1344 | static int apply_line(char *output, const char *patch, int plen) |
1345 | { | |
1346 | /* plen is number of bytes to be copied from patch, | |
1347 | * starting at patch+1 (patch[0] is '+'). Typically | |
1348 | * patch[plen] is '\n'. | |
1349 | */ | |
1350 | int add_nl_to_tail = 0; | |
2ae1c53b | 1351 | if ((new_whitespace == strip_whitespace) && |
b5767dd6 JH |
1352 | 1 < plen && isspace(patch[plen-1])) { |
1353 | if (patch[plen] == '\n') | |
1354 | add_nl_to_tail = 1; | |
1355 | plen--; | |
1356 | while (0 < plen && isspace(patch[plen])) | |
1357 | plen--; | |
fc96b7c9 | 1358 | applied_after_stripping++; |
b5767dd6 JH |
1359 | } |
1360 | memcpy(output, patch + 1, plen); | |
1361 | if (add_nl_to_tail) | |
1362 | output[plen++] = '\n'; | |
1363 | return plen; | |
1364 | } | |
1365 | ||
3eaa38da | 1366 | static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, |
e5a94313 | 1367 | int reverse, int inaccurate_eof) |
3cca928d | 1368 | { |
65aadb92 | 1369 | int match_beginning, match_end; |
6e7c92a9 | 1370 | char *buf = desc->buffer; |
3cca928d LT |
1371 | const char *patch = frag->patch; |
1372 | int offset, size = frag->size; | |
1373 | char *old = xmalloc(size); | |
1374 | char *new = xmalloc(size); | |
47495887 | 1375 | const char *oldlines, *newlines; |
3cca928d | 1376 | int oldsize = 0, newsize = 0; |
47495887 EB |
1377 | unsigned long leading, trailing; |
1378 | int pos, lines; | |
3cca928d LT |
1379 | |
1380 | while (size > 0) { | |
e5a94313 | 1381 | char first; |
3cca928d LT |
1382 | int len = linelen(patch, size); |
1383 | int plen; | |
1384 | ||
1385 | if (!len) | |
1386 | break; | |
1387 | ||
1388 | /* | |
1389 | * "plen" is how much of the line we should use for | |
1390 | * the actual patch data. Normally we just remove the | |
1391 | * first character on the line, but if the line is | |
1392 | * followed by "\ No newline", then we also remove the | |
1393 | * last one (which is the newline, of course). | |
1394 | */ | |
1395 | plen = len-1; | |
8b64647d | 1396 | if (len < size && patch[len] == '\\') |
3cca928d | 1397 | plen--; |
e5a94313 JS |
1398 | first = *patch; |
1399 | if (reverse) { | |
1400 | if (first == '-') | |
1401 | first = '+'; | |
1402 | else if (first == '+') | |
1403 | first = '-'; | |
1404 | } | |
1405 | switch (first) { | |
3cca928d LT |
1406 | case ' ': |
1407 | case '-': | |
1408 | memcpy(old + oldsize, patch + 1, plen); | |
1409 | oldsize += plen; | |
e5a94313 | 1410 | if (first == '-') |
3cca928d LT |
1411 | break; |
1412 | /* Fall-through for ' ' */ | |
1413 | case '+': | |
e5a94313 | 1414 | if (first != '+' || !no_add) |
b5767dd6 JH |
1415 | newsize += apply_line(new + newsize, patch, |
1416 | plen); | |
3cca928d LT |
1417 | break; |
1418 | case '@': case '\\': | |
1419 | /* Ignore it, we already handled it */ | |
1420 | break; | |
1421 | default: | |
1422 | return -1; | |
1423 | } | |
1424 | patch += len; | |
1425 | size -= len; | |
1426 | } | |
1427 | ||
3eaa38da | 1428 | if (inaccurate_eof && oldsize > 0 && old[oldsize - 1] == '\n' && |
5b5d4d9e JS |
1429 | newsize > 0 && new[newsize - 1] == '\n') { |
1430 | oldsize--; | |
1431 | newsize--; | |
1432 | } | |
47495887 EB |
1433 | |
1434 | oldlines = old; | |
1435 | newlines = new; | |
1436 | leading = frag->leading; | |
1437 | trailing = frag->trailing; | |
1bf1a859 LT |
1438 | |
1439 | /* | |
65aadb92 JH |
1440 | * If we don't have any leading/trailing data in the patch, |
1441 | * we want it to match at the beginning/end of the file. | |
1bf1a859 | 1442 | */ |
65aadb92 | 1443 | match_beginning = !leading && (frag->oldpos == 1); |
1bf1a859 LT |
1444 | match_end = !trailing; |
1445 | ||
47495887 EB |
1446 | lines = 0; |
1447 | pos = frag->newpos; | |
1448 | for (;;) { | |
1449 | offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines); | |
1bf1a859 LT |
1450 | if (match_end && offset + oldsize != desc->size) |
1451 | offset = -1; | |
65aadb92 JH |
1452 | if (match_beginning && offset) |
1453 | offset = -1; | |
47495887 EB |
1454 | if (offset >= 0) { |
1455 | int diff = newsize - oldsize; | |
1456 | unsigned long size = desc->size + diff; | |
1457 | unsigned long alloc = desc->alloc; | |
1458 | ||
1459 | /* Warn if it was necessary to reduce the number | |
1460 | * of context lines. | |
1461 | */ | |
1462 | if ((leading != frag->leading) || (trailing != frag->trailing)) | |
1463 | fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n", | |
1464 | leading, trailing, pos + lines); | |
1465 | ||
1466 | if (size > alloc) { | |
1467 | alloc = size + 8192; | |
1468 | desc->alloc = alloc; | |
1469 | buf = xrealloc(buf, alloc); | |
1470 | desc->buffer = buf; | |
1471 | } | |
1472 | desc->size = size; | |
1473 | memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize); | |
1474 | memcpy(buf + offset, newlines, newsize); | |
1475 | offset = 0; | |
1476 | ||
1477 | break; | |
1478 | } | |
1479 | ||
1480 | /* Am I at my context limits? */ | |
1481 | if ((leading <= p_context) && (trailing <= p_context)) | |
1482 | break; | |
65aadb92 JH |
1483 | if (match_beginning || match_end) { |
1484 | match_beginning = match_end = 0; | |
1bf1a859 LT |
1485 | continue; |
1486 | } | |
47495887 EB |
1487 | /* Reduce the number of context lines |
1488 | * Reduce both leading and trailing if they are equal | |
1489 | * otherwise just reduce the larger context. | |
1490 | */ | |
1491 | if (leading >= trailing) { | |
1492 | remove_first_line(&oldlines, &oldsize); | |
1493 | remove_first_line(&newlines, &newsize); | |
1494 | pos--; | |
1495 | leading--; | |
1496 | } | |
1497 | if (trailing > leading) { | |
1498 | remove_last_line(&oldlines, &oldsize); | |
1499 | remove_last_line(&newlines, &newsize); | |
1500 | trailing--; | |
6e7c92a9 | 1501 | } |
3cca928d LT |
1502 | } |
1503 | ||
1504 | free(old); | |
1505 | free(new); | |
1506 | return offset; | |
1507 | } | |
1508 | ||
0660626c JH |
1509 | static char *inflate_it(const void *data, unsigned long size, |
1510 | unsigned long inflated_size) | |
1511 | { | |
1512 | z_stream stream; | |
1513 | void *out; | |
1514 | int st; | |
1515 | ||
1516 | memset(&stream, 0, sizeof(stream)); | |
1517 | ||
1518 | stream.next_in = (unsigned char *)data; | |
1519 | stream.avail_in = size; | |
1520 | stream.next_out = out = xmalloc(inflated_size); | |
1521 | stream.avail_out = inflated_size; | |
1522 | inflateInit(&stream); | |
1523 | st = inflate(&stream, Z_FINISH); | |
1524 | if ((st != Z_STREAM_END) || stream.total_out != inflated_size) { | |
1525 | free(out); | |
1526 | return NULL; | |
1527 | } | |
1528 | return out; | |
1529 | } | |
1530 | ||
1531 | static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch) | |
1532 | { | |
1533 | unsigned long dst_size; | |
1534 | struct fragment *fragment = patch->fragments; | |
1535 | void *data; | |
1536 | void *result; | |
1537 | ||
ab9f30fd JH |
1538 | /* Binary patch is irreversible */ |
1539 | if (patch->is_reverse) | |
1540 | return error("cannot reverse-apply a binary patch to '%s'", | |
1541 | patch->new_name | |
1542 | ? patch->new_name : patch->old_name); | |
1543 | ||
0660626c JH |
1544 | data = inflate_it(fragment->patch, fragment->size, |
1545 | patch->deflate_origlen); | |
1546 | if (!data) | |
1547 | return error("corrupt patch data"); | |
1548 | switch (patch->is_binary) { | |
1549 | case BINARY_DELTA_DEFLATED: | |
1550 | result = patch_delta(desc->buffer, desc->size, | |
1551 | data, | |
1552 | patch->deflate_origlen, | |
1553 | &dst_size); | |
1554 | free(desc->buffer); | |
1555 | desc->buffer = result; | |
1556 | free(data); | |
1557 | break; | |
1558 | case BINARY_LITERAL_DEFLATED: | |
1559 | free(desc->buffer); | |
1560 | desc->buffer = data; | |
1561 | dst_size = patch->deflate_origlen; | |
1562 | break; | |
1563 | } | |
1564 | if (!desc->buffer) | |
1565 | return -1; | |
1566 | desc->size = desc->alloc = dst_size; | |
1567 | return 0; | |
1568 | } | |
1569 | ||
051308f6 | 1570 | static int apply_binary(struct buffer_desc *desc, struct patch *patch) |
3cca928d | 1571 | { |
011f4274 | 1572 | const char *name = patch->old_name ? patch->old_name : patch->new_name; |
051308f6 JH |
1573 | unsigned char sha1[20]; |
1574 | unsigned char hdr[50]; | |
1575 | int hdrlen; | |
011f4274 | 1576 | |
051308f6 JH |
1577 | if (!allow_binary_replacement) |
1578 | return error("cannot apply binary patch to '%s' " | |
1579 | "without --allow-binary-replacement", | |
1580 | name); | |
011f4274 | 1581 | |
051308f6 JH |
1582 | /* For safety, we require patch index line to contain |
1583 | * full 40-byte textual SHA1 for old and new, at least for now. | |
1584 | */ | |
1585 | if (strlen(patch->old_sha1_prefix) != 40 || | |
1586 | strlen(patch->new_sha1_prefix) != 40 || | |
1587 | get_sha1_hex(patch->old_sha1_prefix, sha1) || | |
1588 | get_sha1_hex(patch->new_sha1_prefix, sha1)) | |
1589 | return error("cannot apply binary patch to '%s' " | |
1590 | "without full index line", name); | |
011f4274 | 1591 | |
051308f6 JH |
1592 | if (patch->old_name) { |
1593 | /* See if the old one matches what the patch | |
1594 | * applies to. | |
011f4274 | 1595 | */ |
051308f6 JH |
1596 | write_sha1_file_prepare(desc->buffer, desc->size, |
1597 | blob_type, sha1, hdr, &hdrlen); | |
1598 | if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix)) | |
1599 | return error("the patch applies to '%s' (%s), " | |
1600 | "which does not match the " | |
1601 | "current contents.", | |
1602 | name, sha1_to_hex(sha1)); | |
1603 | } | |
1604 | else { | |
1605 | /* Otherwise, the old one must be empty. */ | |
1606 | if (desc->size) | |
1607 | return error("the patch applies to an empty " | |
1608 | "'%s' but it is not empty", name); | |
1609 | } | |
011f4274 | 1610 | |
0660626c JH |
1611 | get_sha1_hex(patch->new_sha1_prefix, sha1); |
1612 | if (!memcmp(sha1, null_sha1, 20)) { | |
051308f6 JH |
1613 | free(desc->buffer); |
1614 | desc->alloc = desc->size = 0; | |
0660626c | 1615 | desc->buffer = NULL; |
051308f6 | 1616 | return 0; /* deletion patch */ |
0660626c | 1617 | } |
011f4274 | 1618 | |
051308f6 | 1619 | if (has_sha1_file(sha1)) { |
0660626c | 1620 | /* We already have the postimage */ |
051308f6 JH |
1621 | char type[10]; |
1622 | unsigned long size; | |
1623 | ||
0660626c | 1624 | free(desc->buffer); |
051308f6 JH |
1625 | desc->buffer = read_sha1_file(sha1, type, &size); |
1626 | if (!desc->buffer) | |
1627 | return error("the necessary postimage %s for " | |
1628 | "'%s' cannot be read", | |
1629 | patch->new_sha1_prefix, name); | |
1630 | desc->alloc = desc->size = size; | |
1631 | } | |
1632 | else { | |
0660626c JH |
1633 | /* We have verified desc matches the preimage; |
1634 | * apply the patch data to it, which is stored | |
1635 | * in the patch->fragments->{patch,size}. | |
011f4274 | 1636 | */ |
0660626c | 1637 | if (apply_binary_fragment(desc, patch)) |
051308f6 JH |
1638 | return error("binary patch does not apply to '%s'", |
1639 | name); | |
011f4274 | 1640 | |
051308f6 JH |
1641 | /* verify that the result matches */ |
1642 | write_sha1_file_prepare(desc->buffer, desc->size, blob_type, | |
1643 | sha1, hdr, &hdrlen); | |
1644 | if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix)) | |
1645 | return error("binary patch to '%s' creates incorrect result", name); | |
011f4274 | 1646 | } |
3cca928d | 1647 | |
051308f6 JH |
1648 | return 0; |
1649 | } | |
1650 | ||
1651 | static int apply_fragments(struct buffer_desc *desc, struct patch *patch) | |
1652 | { | |
1653 | struct fragment *frag = patch->fragments; | |
1654 | const char *name = patch->old_name ? patch->old_name : patch->new_name; | |
1655 | ||
1656 | if (patch->is_binary) | |
1657 | return apply_binary(desc, patch); | |
1658 | ||
3cca928d | 1659 | while (frag) { |
e5a94313 JS |
1660 | if (apply_one_fragment(desc, frag, patch->is_reverse, |
1661 | patch->inaccurate_eof) < 0) | |
011f4274 JH |
1662 | return error("patch failed: %s:%ld", |
1663 | name, frag->oldpos); | |
3cca928d LT |
1664 | frag = frag->next; |
1665 | } | |
30996652 | 1666 | return 0; |
3cca928d LT |
1667 | } |
1668 | ||
04e4888e | 1669 | static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce) |
3cca928d | 1670 | { |
6e7c92a9 LT |
1671 | char *buf; |
1672 | unsigned long size, alloc; | |
1673 | struct buffer_desc desc; | |
3cca928d | 1674 | |
30996652 LT |
1675 | size = 0; |
1676 | alloc = 0; | |
1677 | buf = NULL; | |
04e4888e JH |
1678 | if (cached) { |
1679 | if (ce) { | |
1680 | char type[20]; | |
1681 | buf = read_sha1_file(ce->sha1, type, &size); | |
1682 | if (!buf) | |
1683 | return error("read of %s failed", | |
1684 | patch->old_name); | |
1685 | alloc = size; | |
1686 | } | |
1687 | } | |
1688 | else if (patch->old_name) { | |
30996652 LT |
1689 | size = st->st_size; |
1690 | alloc = size + 8192; | |
1691 | buf = xmalloc(alloc); | |
1692 | if (read_old_data(st, patch->old_name, buf, alloc) != size) | |
1693 | return error("read of %s failed", patch->old_name); | |
1694 | } | |
6e7c92a9 LT |
1695 | |
1696 | desc.size = size; | |
1697 | desc.alloc = alloc; | |
1698 | desc.buffer = buf; | |
1699 | if (apply_fragments(&desc, patch) < 0) | |
3cca928d | 1700 | return -1; |
6e7c92a9 LT |
1701 | patch->result = desc.buffer; |
1702 | patch->resultsize = desc.size; | |
5aa7d94c LT |
1703 | |
1704 | if (patch->is_delete && patch->resultsize) | |
1705 | return error("removal patch leaves file contents"); | |
1706 | ||
3cca928d LT |
1707 | return 0; |
1708 | } | |
1709 | ||
7f95aef2 | 1710 | static int check_patch(struct patch *patch, struct patch *prev_patch) |
fab2c257 | 1711 | { |
a577284a | 1712 | struct stat st; |
fab2c257 LT |
1713 | const char *old_name = patch->old_name; |
1714 | const char *new_name = patch->new_name; | |
011f4274 | 1715 | const char *name = old_name ? old_name : new_name; |
04e4888e | 1716 | struct cache_entry *ce = NULL; |
7f95aef2 | 1717 | int ok_if_exists; |
fab2c257 LT |
1718 | |
1719 | if (old_name) { | |
04e4888e JH |
1720 | int changed = 0; |
1721 | int stat_ret = 0; | |
1722 | unsigned st_mode = 0; | |
a577284a | 1723 | |
04e4888e JH |
1724 | if (!cached) |
1725 | stat_ret = lstat(old_name, &st); | |
3cca928d LT |
1726 | if (check_index) { |
1727 | int pos = cache_name_pos(old_name, strlen(old_name)); | |
1728 | if (pos < 0) | |
56d33b11 JH |
1729 | return error("%s: does not exist in index", |
1730 | old_name); | |
04e4888e | 1731 | ce = active_cache[pos]; |
56d33b11 JH |
1732 | if (stat_ret < 0) { |
1733 | struct checkout costate; | |
1734 | if (errno != ENOENT) | |
1735 | return error("%s: %s", old_name, | |
1736 | strerror(errno)); | |
1737 | /* checkout */ | |
1738 | costate.base_dir = ""; | |
1739 | costate.base_dir_len = 0; | |
1740 | costate.force = 0; | |
1741 | costate.quiet = 0; | |
1742 | costate.not_new = 0; | |
1743 | costate.refresh_cache = 1; | |
04e4888e | 1744 | if (checkout_entry(ce, |
de84f99c SP |
1745 | &costate, |
1746 | NULL) || | |
56d33b11 JH |
1747 | lstat(old_name, &st)) |
1748 | return -1; | |
1749 | } | |
04e4888e JH |
1750 | if (!cached) |
1751 | changed = ce_match_stat(ce, &st, 1); | |
3cca928d | 1752 | if (changed) |
56d33b11 JH |
1753 | return error("%s: does not match index", |
1754 | old_name); | |
04e4888e JH |
1755 | if (cached) |
1756 | st_mode = ntohl(ce->ce_mode); | |
3cca928d | 1757 | } |
56d33b11 JH |
1758 | else if (stat_ret < 0) |
1759 | return error("%s: %s", old_name, strerror(errno)); | |
1760 | ||
04e4888e JH |
1761 | if (!cached) |
1762 | st_mode = ntohl(create_ce_mode(st.st_mode)); | |
1763 | ||
3cca928d LT |
1764 | if (patch->is_new < 0) |
1765 | patch->is_new = 0; | |
a577284a | 1766 | if (!patch->old_mode) |
04e4888e JH |
1767 | patch->old_mode = st_mode; |
1768 | if ((st_mode ^ patch->old_mode) & S_IFMT) | |
3cca928d | 1769 | return error("%s: wrong type", old_name); |
04e4888e | 1770 | if (st_mode != patch->old_mode) |
3cca928d | 1771 | fprintf(stderr, "warning: %s has type %o, expected %o\n", |
04e4888e | 1772 | old_name, st_mode, patch->old_mode); |
fab2c257 | 1773 | } |
a577284a | 1774 | |
7f95aef2 JH |
1775 | if (new_name && prev_patch && prev_patch->is_delete && |
1776 | !strcmp(prev_patch->old_name, new_name)) | |
1777 | /* A type-change diff is always split into a patch to | |
1778 | * delete old, immediately followed by a patch to | |
1779 | * create new (see diff.c::run_diff()); in such a case | |
1780 | * it is Ok that the entry to be deleted by the | |
1781 | * previous patch is still in the working tree and in | |
1782 | * the index. | |
1783 | */ | |
1784 | ok_if_exists = 1; | |
1785 | else | |
1786 | ok_if_exists = 0; | |
1787 | ||
fab2c257 | 1788 | if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) { |
7f95aef2 JH |
1789 | if (check_index && |
1790 | cache_name_pos(new_name, strlen(new_name)) >= 0 && | |
1791 | !ok_if_exists) | |
a577284a | 1792 | return error("%s: already exists in index", new_name); |
d91d4c2c | 1793 | if (!cached) { |
c28c571c JH |
1794 | struct stat nst; |
1795 | if (!lstat(new_name, &nst)) { | |
7f95aef2 | 1796 | if (S_ISDIR(nst.st_mode) || ok_if_exists) |
c28c571c JH |
1797 | ; /* ok */ |
1798 | else | |
1799 | return error("%s: already exists in working directory", new_name); | |
1800 | } | |
1801 | else if ((errno != ENOENT) && (errno != ENOTDIR)) | |
d91d4c2c JH |
1802 | return error("%s: %s", new_name, strerror(errno)); |
1803 | } | |
35cc4bcd JS |
1804 | if (!patch->new_mode) { |
1805 | if (patch->is_new) | |
1806 | patch->new_mode = S_IFREG | 0644; | |
1807 | else | |
1808 | patch->new_mode = patch->old_mode; | |
1809 | } | |
fab2c257 | 1810 | } |
3cca928d LT |
1811 | |
1812 | if (new_name && old_name) { | |
1813 | int same = !strcmp(old_name, new_name); | |
1814 | if (!patch->new_mode) | |
1815 | patch->new_mode = patch->old_mode; | |
1816 | if ((patch->old_mode ^ patch->new_mode) & S_IFMT) | |
1817 | return error("new mode (%o) of %s does not match old mode (%o)%s%s", | |
1818 | patch->new_mode, new_name, patch->old_mode, | |
1819 | same ? "" : " of ", same ? "" : old_name); | |
04e4888e | 1820 | } |
3cca928d | 1821 | |
04e4888e | 1822 | if (apply_data(patch, &st, ce) < 0) |
011f4274 | 1823 | return error("%s: patch does not apply", name); |
a577284a | 1824 | return 0; |
fab2c257 LT |
1825 | } |
1826 | ||
a577284a | 1827 | static int check_patch_list(struct patch *patch) |
19c58fb8 | 1828 | { |
7f95aef2 | 1829 | struct patch *prev_patch = NULL; |
a577284a LT |
1830 | int error = 0; |
1831 | ||
7f95aef2 JH |
1832 | for (prev_patch = NULL; patch ; patch = patch->next) { |
1833 | error |= check_patch(patch, prev_patch); | |
1834 | prev_patch = patch; | |
1835 | } | |
a577284a LT |
1836 | return error; |
1837 | } | |
1838 | ||
2cf67f1e JH |
1839 | static inline int is_null_sha1(const unsigned char *sha1) |
1840 | { | |
1841 | return !memcmp(sha1, null_sha1, 20); | |
1842 | } | |
1843 | ||
1844 | static void show_index_list(struct patch *list) | |
1845 | { | |
1846 | struct patch *patch; | |
1847 | ||
1848 | /* Once we start supporting the reverse patch, it may be | |
1849 | * worth showing the new sha1 prefix, but until then... | |
1850 | */ | |
1851 | for (patch = list; patch; patch = patch->next) { | |
1852 | const unsigned char *sha1_ptr; | |
1853 | unsigned char sha1[20]; | |
1854 | const char *name; | |
1855 | ||
1856 | name = patch->old_name ? patch->old_name : patch->new_name; | |
1857 | if (patch->is_new) | |
1858 | sha1_ptr = null_sha1; | |
1859 | else if (get_sha1(patch->old_sha1_prefix, sha1)) | |
1860 | die("sha1 information is lacking or useless (%s).", | |
1861 | name); | |
1862 | else | |
1863 | sha1_ptr = sha1; | |
22943f1a JH |
1864 | |
1865 | printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr)); | |
1866 | if (line_termination && quote_c_style(name, NULL, NULL, 0)) | |
1867 | quote_c_style(name, NULL, stdout, 0); | |
1868 | else | |
1869 | fputs(name, stdout); | |
1870 | putchar(line_termination); | |
2cf67f1e JH |
1871 | } |
1872 | } | |
1873 | ||
a577284a LT |
1874 | static void stat_patch_list(struct patch *patch) |
1875 | { | |
1876 | int files, adds, dels; | |
1877 | ||
1878 | for (files = adds = dels = 0 ; patch ; patch = patch->next) { | |
1879 | files++; | |
1880 | adds += patch->lines_added; | |
1881 | dels += patch->lines_deleted; | |
1882 | show_stats(patch); | |
1883 | } | |
1884 | ||
1885 | printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels); | |
3f40315a LT |
1886 | } |
1887 | ||
7d8b7c21 JH |
1888 | static void numstat_patch_list(struct patch *patch) |
1889 | { | |
1890 | for ( ; patch; patch = patch->next) { | |
1891 | const char *name; | |
49e3343c | 1892 | name = patch->new_name ? patch->new_name : patch->old_name; |
7d8b7c21 JH |
1893 | printf("%d\t%d\t", patch->lines_added, patch->lines_deleted); |
1894 | if (line_termination && quote_c_style(name, NULL, NULL, 0)) | |
1895 | quote_c_style(name, NULL, stdout, 0); | |
1896 | else | |
1897 | fputs(name, stdout); | |
1898 | putchar('\n'); | |
1899 | } | |
1900 | } | |
1901 | ||
96c912a4 JH |
1902 | static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name) |
1903 | { | |
1904 | if (mode) | |
1905 | printf(" %s mode %06o %s\n", newdelete, mode, name); | |
1906 | else | |
1907 | printf(" %s %s\n", newdelete, name); | |
1908 | } | |
1909 | ||
1910 | static void show_mode_change(struct patch *p, int show_name) | |
1911 | { | |
1912 | if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) { | |
1913 | if (show_name) | |
1914 | printf(" mode change %06o => %06o %s\n", | |
1915 | p->old_mode, p->new_mode, p->new_name); | |
1916 | else | |
1917 | printf(" mode change %06o => %06o\n", | |
1918 | p->old_mode, p->new_mode); | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | static void show_rename_copy(struct patch *p) | |
1923 | { | |
1924 | const char *renamecopy = p->is_rename ? "rename" : "copy"; | |
1925 | const char *old, *new; | |
1926 | ||
1927 | /* Find common prefix */ | |
1928 | old = p->old_name; | |
1929 | new = p->new_name; | |
1930 | while (1) { | |
1931 | const char *slash_old, *slash_new; | |
1932 | slash_old = strchr(old, '/'); | |
1933 | slash_new = strchr(new, '/'); | |
1934 | if (!slash_old || | |
1935 | !slash_new || | |
1936 | slash_old - old != slash_new - new || | |
1937 | memcmp(old, new, slash_new - new)) | |
1938 | break; | |
1939 | old = slash_old + 1; | |
1940 | new = slash_new + 1; | |
1941 | } | |
1942 | /* p->old_name thru old is the common prefix, and old and new | |
1943 | * through the end of names are renames | |
1944 | */ | |
1945 | if (old != p->old_name) | |
1946 | printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, | |
e30e814d | 1947 | (int)(old - p->old_name), p->old_name, |
96c912a4 JH |
1948 | old, new, p->score); |
1949 | else | |
1950 | printf(" %s %s => %s (%d%%)\n", renamecopy, | |
1951 | p->old_name, p->new_name, p->score); | |
1952 | show_mode_change(p, 0); | |
1953 | } | |
1954 | ||
1955 | static void summary_patch_list(struct patch *patch) | |
1956 | { | |
1957 | struct patch *p; | |
1958 | ||
1959 | for (p = patch; p; p = p->next) { | |
1960 | if (p->is_new) | |
1961 | show_file_mode_name("create", p->new_mode, p->new_name); | |
1962 | else if (p->is_delete) | |
1963 | show_file_mode_name("delete", p->old_mode, p->old_name); | |
1964 | else { | |
1965 | if (p->is_rename || p->is_copy) | |
1966 | show_rename_copy(p); | |
1967 | else { | |
1968 | if (p->score) { | |
1969 | printf(" rewrite %s (%d%%)\n", | |
1970 | p->new_name, p->score); | |
1971 | show_mode_change(p, 0); | |
1972 | } | |
1973 | else | |
1974 | show_mode_change(p, 1); | |
1975 | } | |
1976 | } | |
1977 | } | |
1978 | } | |
1979 | ||
3f40315a LT |
1980 | static void patch_stats(struct patch *patch) |
1981 | { | |
1982 | int lines = patch->lines_added + patch->lines_deleted; | |
1983 | ||
1984 | if (lines > max_change) | |
1985 | max_change = lines; | |
1986 | if (patch->old_name) { | |
22943f1a JH |
1987 | int len = quote_c_style(patch->old_name, NULL, NULL, 0); |
1988 | if (!len) | |
1989 | len = strlen(patch->old_name); | |
3f40315a LT |
1990 | if (len > max_len) |
1991 | max_len = len; | |
1992 | } | |
1993 | if (patch->new_name) { | |
22943f1a JH |
1994 | int len = quote_c_style(patch->new_name, NULL, NULL, 0); |
1995 | if (!len) | |
1996 | len = strlen(patch->new_name); | |
3f40315a LT |
1997 | if (len > max_len) |
1998 | max_len = len; | |
1999 | } | |
19c58fb8 LT |
2000 | } |
2001 | ||
5aa7d94c LT |
2002 | static void remove_file(struct patch *patch) |
2003 | { | |
2004 | if (write_index) { | |
2005 | if (remove_file_from_cache(patch->old_name) < 0) | |
2006 | die("unable to remove %s from index", patch->old_name); | |
03ac6e64 | 2007 | cache_tree_invalidate_path(active_cache_tree, patch->old_name); |
5aa7d94c | 2008 | } |
04e4888e JH |
2009 | if (!cached) |
2010 | unlink(patch->old_name); | |
5aa7d94c LT |
2011 | } |
2012 | ||
2013 | static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size) | |
2014 | { | |
2015 | struct stat st; | |
2016 | struct cache_entry *ce; | |
2017 | int namelen = strlen(path); | |
2018 | unsigned ce_size = cache_entry_size(namelen); | |
2019 | ||
2020 | if (!write_index) | |
2021 | return; | |
2022 | ||
90321c10 | 2023 | ce = xcalloc(1, ce_size); |
5aa7d94c LT |
2024 | memcpy(ce->name, path, namelen); |
2025 | ce->ce_mode = create_ce_mode(mode); | |
2026 | ce->ce_flags = htons(namelen); | |
04e4888e JH |
2027 | if (!cached) { |
2028 | if (lstat(path, &st) < 0) | |
2029 | die("unable to stat newly created file %s", path); | |
2030 | fill_stat_cache_info(ce, &st); | |
2031 | } | |
8e440259 | 2032 | if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) |
5aa7d94c LT |
2033 | die("unable to create backing store for newly created file %s", path); |
2034 | if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) | |
2035 | die("unable to add cache entry for %s", path); | |
2036 | } | |
2037 | ||
1b668341 LT |
2038 | static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) |
2039 | { | |
2040 | int fd; | |
2041 | ||
2042 | if (S_ISLNK(mode)) | |
2043 | return symlink(buf, path); | |
781411ed | 2044 | fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666); |
1b668341 LT |
2045 | if (fd < 0) |
2046 | return -1; | |
2047 | while (size) { | |
1c15afb9 JH |
2048 | int written = xwrite(fd, buf, size); |
2049 | if (written < 0) | |
1b668341 | 2050 | die("writing file %s: %s", path, strerror(errno)); |
1b668341 LT |
2051 | if (!written) |
2052 | die("out of space writing file %s", path); | |
2053 | buf += written; | |
2054 | size -= written; | |
2055 | } | |
2056 | if (close(fd) < 0) | |
2057 | die("closing file %s: %s", path, strerror(errno)); | |
2058 | return 0; | |
2059 | } | |
2060 | ||
5c8af185 LT |
2061 | /* |
2062 | * We optimistically assume that the directories exist, | |
2063 | * which is true 99% of the time anyway. If they don't, | |
2064 | * we create them and try again. | |
2065 | */ | |
8361e1d4 | 2066 | static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size) |
5c8af185 | 2067 | { |
04e4888e JH |
2068 | if (cached) |
2069 | return; | |
1b668341 LT |
2070 | if (!try_create_file(path, mode, buf, size)) |
2071 | return; | |
5c8af185 | 2072 | |
1b668341 | 2073 | if (errno == ENOENT) { |
8361e1d4 JR |
2074 | if (safe_create_leading_directories(path)) |
2075 | return; | |
1b668341 LT |
2076 | if (!try_create_file(path, mode, buf, size)) |
2077 | return; | |
5c8af185 | 2078 | } |
5c8af185 | 2079 | |
56ac168f | 2080 | if (errno == EEXIST || errno == EACCES) { |
c28c571c JH |
2081 | /* We may be trying to create a file where a directory |
2082 | * used to be. | |
2083 | */ | |
2084 | struct stat st; | |
2085 | errno = 0; | |
2086 | if (!lstat(path, &st) && S_ISDIR(st.st_mode) && !rmdir(path)) | |
2087 | errno = EEXIST; | |
2088 | } | |
2089 | ||
1b668341 LT |
2090 | if (errno == EEXIST) { |
2091 | unsigned int nr = getpid(); | |
5c8af185 | 2092 | |
1b668341 LT |
2093 | for (;;) { |
2094 | const char *newpath; | |
2095 | newpath = mkpath("%s~%u", path, nr); | |
2096 | if (!try_create_file(newpath, mode, buf, size)) { | |
2097 | if (!rename(newpath, path)) | |
2098 | return; | |
2099 | unlink(newpath); | |
2100 | break; | |
2101 | } | |
2102 | if (errno != EEXIST) | |
2103 | break; | |
d9e08be9 AR |
2104 | ++nr; |
2105 | } | |
5c8af185 | 2106 | } |
1b668341 | 2107 | die("unable to write file %s mode %o", path, mode); |
5c8af185 LT |
2108 | } |
2109 | ||
5aa7d94c LT |
2110 | static void create_file(struct patch *patch) |
2111 | { | |
8361e1d4 | 2112 | char *path = patch->new_name; |
5aa7d94c LT |
2113 | unsigned mode = patch->new_mode; |
2114 | unsigned long size = patch->resultsize; | |
2115 | char *buf = patch->result; | |
2116 | ||
2117 | if (!mode) | |
2118 | mode = S_IFREG | 0644; | |
03ac6e64 | 2119 | create_one_file(path, mode, buf, size); |
1b668341 | 2120 | add_index_file(path, mode, buf, size); |
03ac6e64 | 2121 | cache_tree_invalidate_path(active_cache_tree, path); |
5aa7d94c LT |
2122 | } |
2123 | ||
eed46644 JH |
2124 | /* phase zero is to remove, phase one is to create */ |
2125 | static void write_out_one_result(struct patch *patch, int phase) | |
5aa7d94c LT |
2126 | { |
2127 | if (patch->is_delete > 0) { | |
eed46644 JH |
2128 | if (phase == 0) |
2129 | remove_file(patch); | |
5aa7d94c LT |
2130 | return; |
2131 | } | |
2132 | if (patch->is_new > 0 || patch->is_copy) { | |
eed46644 JH |
2133 | if (phase == 1) |
2134 | create_file(patch); | |
5aa7d94c LT |
2135 | return; |
2136 | } | |
2137 | /* | |
2138 | * Rename or modification boils down to the same | |
2139 | * thing: remove the old, write the new | |
2140 | */ | |
eed46644 JH |
2141 | if (phase == 0) |
2142 | remove_file(patch); | |
2143 | if (phase == 1) | |
5aa7d94c LT |
2144 | create_file(patch); |
2145 | } | |
2146 | ||
d854f783 | 2147 | static void write_out_results(struct patch *list, int skipped_patch) |
5aa7d94c | 2148 | { |
eed46644 JH |
2149 | int phase; |
2150 | ||
d854f783 | 2151 | if (!list && !skipped_patch) |
f7b79707 LT |
2152 | die("No changes"); |
2153 | ||
eed46644 JH |
2154 | for (phase = 0; phase < 2; phase++) { |
2155 | struct patch *l = list; | |
2156 | while (l) { | |
2157 | write_out_one_result(l, phase); | |
2158 | l = l->next; | |
2159 | } | |
5aa7d94c LT |
2160 | } |
2161 | } | |
2162 | ||
021b6e45 | 2163 | static struct lock_file lock_file; |
5aa7d94c | 2164 | |
d854f783 JH |
2165 | static struct excludes { |
2166 | struct excludes *next; | |
2167 | const char *path; | |
2168 | } *excludes; | |
2169 | ||
2170 | static int use_patch(struct patch *p) | |
2171 | { | |
c7c81b3a | 2172 | const char *pathname = p->new_name ? p->new_name : p->old_name; |
d854f783 JH |
2173 | struct excludes *x = excludes; |
2174 | while (x) { | |
2175 | if (fnmatch(x->path, pathname, 0) == 0) | |
2176 | return 0; | |
2177 | x = x->next; | |
2178 | } | |
edf2e370 JH |
2179 | if (0 < prefix_length) { |
2180 | int pathlen = strlen(pathname); | |
2181 | if (pathlen <= prefix_length || | |
2182 | memcmp(prefix, pathname, prefix_length)) | |
2183 | return 0; | |
2184 | } | |
d854f783 JH |
2185 | return 1; |
2186 | } | |
2187 | ||
e5a94313 JS |
2188 | static int apply_patch(int fd, const char *filename, |
2189 | int reverse, int inaccurate_eof) | |
c1bb9350 LT |
2190 | { |
2191 | unsigned long offset, size; | |
2192 | char *buffer = read_patch_file(fd, &size); | |
19c58fb8 | 2193 | struct patch *list = NULL, **listp = &list; |
d854f783 | 2194 | int skipped_patch = 0; |
c1bb9350 | 2195 | |
b5767dd6 | 2196 | patch_input_file = filename; |
c1bb9350 LT |
2197 | if (!buffer) |
2198 | return -1; | |
2199 | offset = 0; | |
2200 | while (size > 0) { | |
19c58fb8 LT |
2201 | struct patch *patch; |
2202 | int nr; | |
2203 | ||
90321c10 | 2204 | patch = xcalloc(1, sizeof(*patch)); |
3eaa38da | 2205 | patch->inaccurate_eof = inaccurate_eof; |
19c58fb8 | 2206 | nr = parse_chunk(buffer + offset, size, patch); |
c1bb9350 LT |
2207 | if (nr < 0) |
2208 | break; | |
e5a94313 JS |
2209 | if (reverse) |
2210 | reverse_patches(patch); | |
d854f783 JH |
2211 | if (use_patch(patch)) { |
2212 | patch_stats(patch); | |
2213 | *listp = patch; | |
2214 | listp = &patch->next; | |
2215 | } else { | |
2216 | /* perhaps free it a bit better? */ | |
2217 | free(patch); | |
2218 | skipped_patch++; | |
2219 | } | |
c1bb9350 LT |
2220 | offset += nr; |
2221 | size -= nr; | |
2222 | } | |
19c58fb8 | 2223 | |
b5767dd6 JH |
2224 | if (whitespace_error && (new_whitespace == error_on_whitespace)) |
2225 | apply = 0; | |
2226 | ||
5aa7d94c | 2227 | write_index = check_index && apply; |
021b6e45 JH |
2228 | if (write_index && newfd < 0) { |
2229 | newfd = hold_lock_file_for_update(&lock_file, | |
2230 | get_index_file()); | |
2231 | if (newfd < 0) | |
2232 | die("unable to create new index file"); | |
2233 | } | |
5aa7d94c LT |
2234 | if (check_index) { |
2235 | if (read_cache() < 0) | |
2236 | die("unable to read index file"); | |
2237 | } | |
2238 | ||
a577284a LT |
2239 | if ((check || apply) && check_patch_list(list) < 0) |
2240 | exit(1); | |
2241 | ||
5aa7d94c | 2242 | if (apply) |
d854f783 | 2243 | write_out_results(list, skipped_patch); |
5aa7d94c | 2244 | |
2cf67f1e JH |
2245 | if (show_index_info) |
2246 | show_index_list(list); | |
2247 | ||
a577284a LT |
2248 | if (diffstat) |
2249 | stat_patch_list(list); | |
19c58fb8 | 2250 | |
7d8b7c21 JH |
2251 | if (numstat) |
2252 | numstat_patch_list(list); | |
2253 | ||
96c912a4 JH |
2254 | if (summary) |
2255 | summary_patch_list(list); | |
2256 | ||
c1bb9350 LT |
2257 | free(buffer); |
2258 | return 0; | |
2259 | } | |
2260 | ||
2ae1c53b JH |
2261 | static int git_apply_config(const char *var, const char *value) |
2262 | { | |
2263 | if (!strcmp(var, "apply.whitespace")) { | |
2264 | apply_default_whitespace = strdup(value); | |
2265 | return 0; | |
2266 | } | |
2267 | return git_default_config(var, value); | |
2268 | } | |
2269 | ||
2270 | ||
a633fca0 | 2271 | int cmd_apply(int argc, const char **argv, const char *prefix) |
c1bb9350 LT |
2272 | { |
2273 | int i; | |
4dfdbe10 | 2274 | int read_stdin = 1; |
e5a94313 | 2275 | int reverse = 0; |
3eaa38da JS |
2276 | int inaccurate_eof = 0; |
2277 | ||
2ae1c53b | 2278 | const char *whitespace_option = NULL; |
c1bb9350 | 2279 | |
c1bb9350 LT |
2280 | for (i = 1; i < argc; i++) { |
2281 | const char *arg = argv[i]; | |
47495887 | 2282 | char *end; |
c1bb9350 LT |
2283 | int fd; |
2284 | ||
2285 | if (!strcmp(arg, "-")) { | |
e5a94313 | 2286 | apply_patch(0, "<stdin>", reverse, inaccurate_eof); |
4dfdbe10 | 2287 | read_stdin = 0; |
c1bb9350 LT |
2288 | continue; |
2289 | } | |
d854f783 JH |
2290 | if (!strncmp(arg, "--exclude=", 10)) { |
2291 | struct excludes *x = xmalloc(sizeof(*x)); | |
2292 | x->path = arg + 10; | |
2293 | x->next = excludes; | |
2294 | excludes = x; | |
2295 | continue; | |
2296 | } | |
e36f8b60 DB |
2297 | if (!strncmp(arg, "-p", 2)) { |
2298 | p_value = atoi(arg + 2); | |
2299 | continue; | |
2300 | } | |
cb93c193 JH |
2301 | if (!strcmp(arg, "--no-add")) { |
2302 | no_add = 1; | |
2303 | continue; | |
2304 | } | |
fab2c257 | 2305 | if (!strcmp(arg, "--stat")) { |
a577284a | 2306 | apply = 0; |
fab2c257 LT |
2307 | diffstat = 1; |
2308 | continue; | |
2309 | } | |
0660626c JH |
2310 | if (!strcmp(arg, "--allow-binary-replacement") || |
2311 | !strcmp(arg, "--binary")) { | |
011f4274 JH |
2312 | allow_binary_replacement = 1; |
2313 | continue; | |
2314 | } | |
7d8b7c21 JH |
2315 | if (!strcmp(arg, "--numstat")) { |
2316 | apply = 0; | |
2317 | numstat = 1; | |
2318 | continue; | |
2319 | } | |
96c912a4 JH |
2320 | if (!strcmp(arg, "--summary")) { |
2321 | apply = 0; | |
2322 | summary = 1; | |
2323 | continue; | |
2324 | } | |
a577284a LT |
2325 | if (!strcmp(arg, "--check")) { |
2326 | apply = 0; | |
2327 | check = 1; | |
2328 | continue; | |
2329 | } | |
3cca928d LT |
2330 | if (!strcmp(arg, "--index")) { |
2331 | check_index = 1; | |
2332 | continue; | |
2333 | } | |
04e4888e JH |
2334 | if (!strcmp(arg, "--cached")) { |
2335 | check_index = 1; | |
2336 | cached = 1; | |
2337 | continue; | |
2338 | } | |
aefa4a5b LT |
2339 | if (!strcmp(arg, "--apply")) { |
2340 | apply = 1; | |
2341 | continue; | |
2342 | } | |
22943f1a | 2343 | if (!strcmp(arg, "--index-info")) { |
2cf67f1e JH |
2344 | apply = 0; |
2345 | show_index_info = 1; | |
2346 | continue; | |
2347 | } | |
22943f1a JH |
2348 | if (!strcmp(arg, "-z")) { |
2349 | line_termination = 0; | |
2350 | continue; | |
2351 | } | |
47495887 EB |
2352 | if (!strncmp(arg, "-C", 2)) { |
2353 | p_context = strtoul(arg + 2, &end, 0); | |
2354 | if (*end != '\0') | |
2355 | die("unrecognized context count '%s'", arg + 2); | |
2356 | continue; | |
2357 | } | |
19bfcd5a | 2358 | if (!strncmp(arg, "--whitespace=", 13)) { |
2ae1c53b JH |
2359 | whitespace_option = arg + 13; |
2360 | parse_whitespace_option(arg + 13); | |
2361 | continue; | |
19bfcd5a | 2362 | } |
e5a94313 JS |
2363 | if (!strcmp(arg, "-R") || !strcmp(arg, "--reverse")) { |
2364 | reverse = 1; | |
2365 | continue; | |
2366 | } | |
3eaa38da JS |
2367 | if (!strcmp(arg, "--inaccurate-eof")) { |
2368 | inaccurate_eof = 1; | |
2369 | continue; | |
2370 | } | |
edf2e370 JH |
2371 | |
2372 | if (check_index && prefix_length < 0) { | |
2373 | prefix = setup_git_directory(); | |
2374 | prefix_length = prefix ? strlen(prefix) : 0; | |
2ae1c53b JH |
2375 | git_config(git_apply_config); |
2376 | if (!whitespace_option && apply_default_whitespace) | |
2377 | parse_whitespace_option(apply_default_whitespace); | |
edf2e370 JH |
2378 | } |
2379 | if (0 < prefix_length) | |
2380 | arg = prefix_filename(prefix, prefix_length, arg); | |
2381 | ||
c1bb9350 LT |
2382 | fd = open(arg, O_RDONLY); |
2383 | if (fd < 0) | |
2384 | usage(apply_usage); | |
4dfdbe10 | 2385 | read_stdin = 0; |
f21d6726 | 2386 | set_default_whitespace_mode(whitespace_option); |
e5a94313 | 2387 | apply_patch(fd, arg, reverse, inaccurate_eof); |
c1bb9350 LT |
2388 | close(fd); |
2389 | } | |
f21d6726 | 2390 | set_default_whitespace_mode(whitespace_option); |
4dfdbe10 | 2391 | if (read_stdin) |
e5a94313 | 2392 | apply_patch(0, "<stdin>", reverse, inaccurate_eof); |
fc96b7c9 JH |
2393 | if (whitespace_error) { |
2394 | if (squelch_whitespace_errors && | |
2395 | squelch_whitespace_errors < whitespace_error) { | |
2396 | int squelched = | |
2397 | whitespace_error - squelch_whitespace_errors; | |
2398 | fprintf(stderr, "warning: squelched %d whitespace error%s\n", | |
2399 | squelched, | |
2400 | squelched == 1 ? "" : "s"); | |
2401 | } | |
2402 | if (new_whitespace == error_on_whitespace) | |
2403 | die("%d line%s add%s trailing whitespaces.", | |
2404 | whitespace_error, | |
2405 | whitespace_error == 1 ? "" : "s", | |
2406 | whitespace_error == 1 ? "s" : ""); | |
2407 | if (applied_after_stripping) | |
2408 | fprintf(stderr, "warning: %d line%s applied after" | |
2409 | " stripping trailing whitespaces.\n", | |
2410 | applied_after_stripping, | |
2411 | applied_after_stripping == 1 ? "" : "s"); | |
2412 | else if (whitespace_error) | |
2413 | fprintf(stderr, "warning: %d line%s add%s trailing" | |
2414 | " whitespaces.\n", | |
2415 | whitespace_error, | |
2416 | whitespace_error == 1 ? "" : "s", | |
2417 | whitespace_error == 1 ? "s" : ""); | |
2418 | } | |
dbd0f7d3 EW |
2419 | |
2420 | if (write_index) { | |
2421 | if (write_cache(newfd, active_cache, active_nr) || | |
6244b249 | 2422 | close(newfd) || commit_lock_file(&lock_file)) |
021b6e45 | 2423 | die("Unable to write new index file"); |
dbd0f7d3 EW |
2424 | } |
2425 | ||
c1bb9350 LT |
2426 | return 0; |
2427 | } |