]>
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 | * | |
8 | * NOTE! It does all its work in the index file, and only cares about | |
9 | * the files in the working directory if you tell it to "merge" the | |
10 | * patch apply. | |
11 | * | |
12 | * Even when merging it always takes the source from the index, and | |
13 | * uses the working tree as a "branch" for a 3-way merge. | |
14 | */ | |
15 | #include <ctype.h> | |
d854f783 | 16 | #include <fnmatch.h> |
c1bb9350 LT |
17 | #include "cache.h" |
18 | ||
19 | // We default to the merge behaviour, since that's what most people would | |
a577284a LT |
20 | // expect. |
21 | // | |
22 | // --check turns on checking that the working tree matches the | |
23 | // files that are being modified, but doesn't apply the patch | |
24 | // --stat does just a diffstat, and doesn't actually apply | |
25 | // --show-files shows the directory changes | |
26 | // | |
c1bb9350 | 27 | static int merge_patch = 1; |
3cca928d | 28 | static int check_index = 0; |
5aa7d94c | 29 | static int write_index = 0; |
fab2c257 | 30 | static int diffstat = 0; |
96c912a4 | 31 | static int summary = 0; |
a577284a LT |
32 | static int check = 0; |
33 | static int apply = 1; | |
34 | static int show_files = 0; | |
12dd6e8c JH |
35 | static const char apply_usage[] = |
36 | "git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] <patch>..."; | |
c1bb9350 | 37 | |
3f40315a LT |
38 | /* |
39 | * For "diff-stat" like behaviour, we keep track of the biggest change | |
40 | * we've seen, and the longest filename. That allows us to do simple | |
41 | * scaling. | |
42 | */ | |
43 | static int max_change, max_len; | |
44 | ||
a4acb0eb LT |
45 | /* |
46 | * Various "current state", notably line numbers and what | |
47 | * file (and how) we're patching right now.. The "is_xxxx" | |
48 | * things are flags, where -1 means "don't know yet". | |
49 | */ | |
46979f56 | 50 | static int linenr = 1; |
19c58fb8 LT |
51 | |
52 | struct fragment { | |
53 | unsigned long oldpos, oldlines; | |
54 | unsigned long newpos, newlines; | |
55 | const char *patch; | |
56 | int size; | |
57 | struct fragment *next; | |
58 | }; | |
59 | ||
60 | struct patch { | |
5041aa70 | 61 | char *new_name, *old_name, *def_name; |
19c58fb8 LT |
62 | unsigned int old_mode, new_mode; |
63 | int is_rename, is_copy, is_new, is_delete; | |
3f40315a | 64 | int lines_added, lines_deleted; |
96c912a4 | 65 | int score; |
19c58fb8 | 66 | struct fragment *fragments; |
5aa7d94c | 67 | char *result; |
3cca928d | 68 | unsigned long resultsize; |
19c58fb8 LT |
69 | struct patch *next; |
70 | }; | |
46979f56 | 71 | |
c1bb9350 | 72 | #define CHUNKSIZE (8192) |
a4acb0eb | 73 | #define SLOP (16) |
c1bb9350 LT |
74 | |
75 | static void *read_patch_file(int fd, unsigned long *sizep) | |
76 | { | |
77 | unsigned long size = 0, alloc = CHUNKSIZE; | |
78 | void *buffer = xmalloc(alloc); | |
79 | ||
80 | for (;;) { | |
81 | int nr = alloc - size; | |
82 | if (nr < 1024) { | |
83 | alloc += CHUNKSIZE; | |
84 | buffer = xrealloc(buffer, alloc); | |
85 | nr = alloc - size; | |
86 | } | |
87 | nr = read(fd, buffer + size, nr); | |
88 | if (!nr) | |
89 | break; | |
90 | if (nr < 0) { | |
91 | if (errno == EAGAIN) | |
92 | continue; | |
93 | die("git-apply: read returned %s", strerror(errno)); | |
94 | } | |
95 | size += nr; | |
96 | } | |
97 | *sizep = size; | |
a4acb0eb LT |
98 | |
99 | /* | |
100 | * Make sure that we have some slop in the buffer | |
101 | * so that we can do speculative "memcmp" etc, and | |
102 | * see to it that it is NUL-filled. | |
103 | */ | |
104 | if (alloc < size + SLOP) | |
105 | buffer = xrealloc(buffer, size + SLOP); | |
106 | memset(buffer + size, 0, SLOP); | |
c1bb9350 LT |
107 | return buffer; |
108 | } | |
109 | ||
3cca928d | 110 | static unsigned long linelen(const char *buffer, unsigned long size) |
c1bb9350 LT |
111 | { |
112 | unsigned long len = 0; | |
113 | while (size--) { | |
114 | len++; | |
115 | if (*buffer++ == '\n') | |
116 | break; | |
117 | } | |
118 | return len; | |
119 | } | |
120 | ||
a4acb0eb LT |
121 | static int is_dev_null(const char *str) |
122 | { | |
123 | return !memcmp("/dev/null", str, 9) && isspace(str[9]); | |
124 | } | |
125 | ||
381ca9a3 LT |
126 | #define TERM_SPACE 1 |
127 | #define TERM_TAB 2 | |
9a4a100e LT |
128 | |
129 | static int name_terminate(const char *name, int namelen, int c, int terminate) | |
130 | { | |
131 | if (c == ' ' && !(terminate & TERM_SPACE)) | |
132 | return 0; | |
133 | if (c == '\t' && !(terminate & TERM_TAB)) | |
134 | return 0; | |
135 | ||
9a4a100e LT |
136 | return 1; |
137 | } | |
138 | ||
139 | static char * find_name(const char *line, char *def, int p_value, int terminate) | |
c1bb9350 | 140 | { |
a4acb0eb LT |
141 | int len; |
142 | const char *start = line; | |
143 | char *name; | |
144 | ||
c1bb9350 | 145 | for (;;) { |
a4acb0eb | 146 | char c = *line; |
9a4a100e LT |
147 | |
148 | if (isspace(c)) { | |
149 | if (c == '\n') | |
150 | break; | |
151 | if (name_terminate(start, line-start, c, terminate)) | |
152 | break; | |
153 | } | |
a4acb0eb LT |
154 | line++; |
155 | if (c == '/' && !--p_value) | |
156 | start = line; | |
157 | } | |
158 | if (!start) | |
159 | return def; | |
160 | len = line - start; | |
161 | if (!len) | |
162 | return def; | |
163 | ||
164 | /* | |
165 | * Generally we prefer the shorter name, especially | |
166 | * if the other one is just a variation of that with | |
167 | * something else tacked on to the end (ie "file.orig" | |
168 | * or "file~"). | |
169 | */ | |
170 | if (def) { | |
171 | int deflen = strlen(def); | |
172 | if (deflen < len && !strncmp(start, def, deflen)) | |
173 | return def; | |
c1bb9350 | 174 | } |
a4acb0eb LT |
175 | |
176 | name = xmalloc(len + 1); | |
177 | memcpy(name, start, len); | |
178 | name[len] = 0; | |
179 | free(def); | |
180 | return name; | |
181 | } | |
182 | ||
183 | /* | |
184 | * Get the name etc info from the --/+++ lines of a traditional patch header | |
185 | * | |
186 | * NOTE! This hardcodes "-p1" behaviour in filename detection. | |
9a4a100e LT |
187 | * |
188 | * FIXME! The end-of-filename heuristics are kind of screwy. For existing | |
189 | * files, we can happily check the index for a match, but for creating a | |
190 | * new file we should try to match whatever "patch" does. I have no idea. | |
a4acb0eb | 191 | */ |
19c58fb8 | 192 | static void parse_traditional_patch(const char *first, const char *second, struct patch *patch) |
a4acb0eb LT |
193 | { |
194 | int p_value = 1; | |
195 | char *name; | |
196 | ||
197 | first += 4; // skip "--- " | |
198 | second += 4; // skip "+++ " | |
199 | if (is_dev_null(first)) { | |
19c58fb8 LT |
200 | patch->is_new = 1; |
201 | patch->is_delete = 0; | |
5041aa70 | 202 | name = find_name(second, NULL, p_value, TERM_SPACE | TERM_TAB); |
19c58fb8 | 203 | patch->new_name = name; |
a4acb0eb | 204 | } else if (is_dev_null(second)) { |
19c58fb8 LT |
205 | patch->is_new = 0; |
206 | patch->is_delete = 1; | |
381ca9a3 | 207 | name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); |
19c58fb8 | 208 | patch->old_name = name; |
a4acb0eb | 209 | } else { |
381ca9a3 LT |
210 | name = find_name(first, NULL, p_value, TERM_SPACE | TERM_TAB); |
211 | name = find_name(second, name, p_value, TERM_SPACE | TERM_TAB); | |
19c58fb8 | 212 | patch->old_name = patch->new_name = name; |
a4acb0eb LT |
213 | } |
214 | if (!name) | |
215 | die("unable to find filename in patch at line %d", linenr); | |
a4acb0eb LT |
216 | } |
217 | ||
19c58fb8 | 218 | static int gitdiff_hdrend(const char *line, struct patch *patch) |
a4acb0eb LT |
219 | { |
220 | return -1; | |
221 | } | |
222 | ||
1e3f6b6e LT |
223 | /* |
224 | * We're anal about diff header consistency, to make | |
225 | * sure that we don't end up having strange ambiguous | |
226 | * patches floating around. | |
227 | * | |
228 | * As a result, gitdiff_{old|new}name() will check | |
229 | * their names against any previous information, just | |
230 | * to make sure.. | |
231 | */ | |
232 | static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew) | |
233 | { | |
234 | int len; | |
235 | const char *name; | |
236 | ||
237 | if (!orig_name && !isnull) | |
238 | return find_name(line, NULL, 1, 0); | |
239 | ||
240 | name = "/dev/null"; | |
241 | len = 9; | |
242 | if (orig_name) { | |
243 | name = orig_name; | |
244 | len = strlen(name); | |
245 | if (isnull) | |
246 | die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); | |
247 | } | |
248 | ||
249 | if (*name == '/') | |
250 | goto absolute_path; | |
251 | ||
252 | for (;;) { | |
253 | char c = *line++; | |
254 | if (c == '\n') | |
255 | break; | |
256 | if (c != '/') | |
257 | continue; | |
258 | absolute_path: | |
259 | if (memcmp(line, name, len) || line[len] != '\n') | |
260 | break; | |
261 | return orig_name; | |
262 | } | |
263 | die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); | |
264 | return NULL; | |
265 | } | |
266 | ||
19c58fb8 | 267 | static int gitdiff_oldname(const char *line, struct patch *patch) |
a4acb0eb | 268 | { |
19c58fb8 | 269 | patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old"); |
a4acb0eb LT |
270 | return 0; |
271 | } | |
272 | ||
19c58fb8 | 273 | static int gitdiff_newname(const char *line, struct patch *patch) |
a4acb0eb | 274 | { |
19c58fb8 | 275 | patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new"); |
a4acb0eb LT |
276 | return 0; |
277 | } | |
278 | ||
19c58fb8 | 279 | static int gitdiff_oldmode(const char *line, struct patch *patch) |
a4acb0eb | 280 | { |
19c58fb8 | 281 | patch->old_mode = strtoul(line, NULL, 8); |
a4acb0eb LT |
282 | return 0; |
283 | } | |
284 | ||
19c58fb8 | 285 | static int gitdiff_newmode(const char *line, struct patch *patch) |
a4acb0eb | 286 | { |
19c58fb8 | 287 | patch->new_mode = strtoul(line, NULL, 8); |
a4acb0eb LT |
288 | return 0; |
289 | } | |
290 | ||
19c58fb8 | 291 | static int gitdiff_delete(const char *line, struct patch *patch) |
a4acb0eb | 292 | { |
19c58fb8 | 293 | patch->is_delete = 1; |
5041aa70 | 294 | patch->old_name = patch->def_name; |
19c58fb8 | 295 | return gitdiff_oldmode(line, patch); |
a4acb0eb LT |
296 | } |
297 | ||
19c58fb8 | 298 | static int gitdiff_newfile(const char *line, struct patch *patch) |
a4acb0eb | 299 | { |
19c58fb8 | 300 | patch->is_new = 1; |
5041aa70 | 301 | patch->new_name = patch->def_name; |
19c58fb8 | 302 | return gitdiff_newmode(line, patch); |
a4acb0eb LT |
303 | } |
304 | ||
19c58fb8 | 305 | static int gitdiff_copysrc(const char *line, struct patch *patch) |
a4acb0eb | 306 | { |
19c58fb8 LT |
307 | patch->is_copy = 1; |
308 | patch->old_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
309 | return 0; |
310 | } | |
311 | ||
19c58fb8 | 312 | static int gitdiff_copydst(const char *line, struct patch *patch) |
a4acb0eb | 313 | { |
19c58fb8 LT |
314 | patch->is_copy = 1; |
315 | patch->new_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
316 | return 0; |
317 | } | |
318 | ||
19c58fb8 | 319 | static int gitdiff_renamesrc(const char *line, struct patch *patch) |
a4acb0eb | 320 | { |
19c58fb8 LT |
321 | patch->is_rename = 1; |
322 | patch->old_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
323 | return 0; |
324 | } | |
325 | ||
19c58fb8 | 326 | static int gitdiff_renamedst(const char *line, struct patch *patch) |
a4acb0eb | 327 | { |
19c58fb8 LT |
328 | patch->is_rename = 1; |
329 | patch->new_name = find_name(line, NULL, 0, 0); | |
a4acb0eb LT |
330 | return 0; |
331 | } | |
332 | ||
19c58fb8 | 333 | static int gitdiff_similarity(const char *line, struct patch *patch) |
a4acb0eb | 334 | { |
96c912a4 JH |
335 | if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) |
336 | patch->score = 0; | |
a4acb0eb | 337 | return 0; |
c1bb9350 LT |
338 | } |
339 | ||
70aadac0 JH |
340 | static int gitdiff_dissimilarity(const char *line, struct patch *patch) |
341 | { | |
96c912a4 JH |
342 | if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) |
343 | patch->score = 0; | |
70aadac0 JH |
344 | return 0; |
345 | } | |
346 | ||
9a4a100e LT |
347 | /* |
348 | * This is normal for a diff that doesn't change anything: we'll fall through | |
349 | * into the next diff. Tell the parser to break out. | |
350 | */ | |
19c58fb8 | 351 | static int gitdiff_unrecognized(const char *line, struct patch *patch) |
9a4a100e LT |
352 | { |
353 | return -1; | |
354 | } | |
355 | ||
5041aa70 LT |
356 | static char *git_header_name(char *line) |
357 | { | |
358 | int len; | |
359 | char *name, *second; | |
360 | ||
361 | /* | |
362 | * Find the first '/' | |
363 | */ | |
364 | name = line; | |
365 | for (;;) { | |
366 | char c = *name++; | |
367 | if (c == '\n') | |
368 | return NULL; | |
369 | if (c == '/') | |
370 | break; | |
371 | } | |
372 | ||
373 | /* | |
374 | * We don't accept absolute paths (/dev/null) as possibly valid | |
375 | */ | |
376 | if (name == line+1) | |
377 | return NULL; | |
378 | ||
379 | /* | |
380 | * Accept a name only if it shows up twice, exactly the same | |
381 | * form. | |
382 | */ | |
383 | for (len = 0 ; ; len++) { | |
384 | char c = name[len]; | |
385 | ||
386 | switch (c) { | |
387 | default: | |
388 | continue; | |
389 | case '\n': | |
390 | break; | |
391 | case '\t': case ' ': | |
392 | second = name+len; | |
393 | for (;;) { | |
394 | char c = *second++; | |
395 | if (c == '\n') | |
396 | return NULL; | |
397 | if (c == '/') | |
398 | break; | |
399 | } | |
0e87e048 | 400 | if (second[len] == '\n' && !memcmp(name, second, len)) { |
5041aa70 LT |
401 | char *ret = xmalloc(len + 1); |
402 | memcpy(ret, name, len); | |
403 | ret[len] = 0; | |
404 | return ret; | |
405 | } | |
406 | } | |
407 | } | |
408 | return NULL; | |
409 | } | |
410 | ||
c1bb9350 | 411 | /* Verify that we recognize the lines following a git header */ |
19c58fb8 | 412 | static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch) |
c1bb9350 | 413 | { |
a4acb0eb LT |
414 | unsigned long offset; |
415 | ||
416 | /* A git diff has explicit new/delete information, so we don't guess */ | |
19c58fb8 LT |
417 | patch->is_new = 0; |
418 | patch->is_delete = 0; | |
a4acb0eb | 419 | |
5041aa70 LT |
420 | /* |
421 | * Some things may not have the old name in the | |
422 | * rest of the headers anywhere (pure mode changes, | |
423 | * or removing or adding empty files), so we get | |
424 | * the default name from the header. | |
425 | */ | |
426 | patch->def_name = git_header_name(line + strlen("diff --git ")); | |
427 | ||
a4acb0eb LT |
428 | line += len; |
429 | size -= len; | |
430 | linenr++; | |
431 | for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) { | |
432 | static const struct opentry { | |
433 | const char *str; | |
19c58fb8 | 434 | int (*fn)(const char *, struct patch *); |
a4acb0eb LT |
435 | } optable[] = { |
436 | { "@@ -", gitdiff_hdrend }, | |
437 | { "--- ", gitdiff_oldname }, | |
438 | { "+++ ", gitdiff_newname }, | |
439 | { "old mode ", gitdiff_oldmode }, | |
440 | { "new mode ", gitdiff_newmode }, | |
441 | { "deleted file mode ", gitdiff_delete }, | |
442 | { "new file mode ", gitdiff_newfile }, | |
443 | { "copy from ", gitdiff_copysrc }, | |
444 | { "copy to ", gitdiff_copydst }, | |
33f4d087 LT |
445 | { "rename old ", gitdiff_renamesrc }, |
446 | { "rename new ", gitdiff_renamedst }, | |
dc938417 LT |
447 | { "rename from ", gitdiff_renamesrc }, |
448 | { "rename to ", gitdiff_renamedst }, | |
a4acb0eb | 449 | { "similarity index ", gitdiff_similarity }, |
70aadac0 | 450 | { "dissimilarity index ", gitdiff_dissimilarity }, |
9a4a100e | 451 | { "", gitdiff_unrecognized }, |
a4acb0eb LT |
452 | }; |
453 | int i; | |
c1bb9350 | 454 | |
c1bb9350 | 455 | len = linelen(line, size); |
a4acb0eb | 456 | if (!len || line[len-1] != '\n') |
c1bb9350 | 457 | break; |
a4acb0eb LT |
458 | for (i = 0; i < sizeof(optable) / sizeof(optable[0]); i++) { |
459 | const struct opentry *p = optable + i; | |
460 | int oplen = strlen(p->str); | |
461 | if (len < oplen || memcmp(p->str, line, oplen)) | |
462 | continue; | |
19c58fb8 | 463 | if (p->fn(line + oplen, patch) < 0) |
a4acb0eb | 464 | return offset; |
9a4a100e | 465 | break; |
a4acb0eb | 466 | } |
c1bb9350 LT |
467 | } |
468 | ||
a4acb0eb | 469 | return offset; |
c1bb9350 LT |
470 | } |
471 | ||
fab2c257 | 472 | static int parse_num(const char *line, unsigned long *p) |
46979f56 LT |
473 | { |
474 | char *ptr; | |
fab2c257 LT |
475 | |
476 | if (!isdigit(*line)) | |
477 | return 0; | |
478 | *p = strtoul(line, &ptr, 10); | |
479 | return ptr - line; | |
480 | } | |
481 | ||
482 | static int parse_range(const char *line, int len, int offset, const char *expect, | |
483 | unsigned long *p1, unsigned long *p2) | |
484 | { | |
46979f56 LT |
485 | int digits, ex; |
486 | ||
487 | if (offset < 0 || offset >= len) | |
488 | return -1; | |
489 | line += offset; | |
490 | len -= offset; | |
491 | ||
fab2c257 LT |
492 | digits = parse_num(line, p1); |
493 | if (!digits) | |
46979f56 | 494 | return -1; |
46979f56 LT |
495 | |
496 | offset += digits; | |
497 | line += digits; | |
498 | len -= digits; | |
499 | ||
fab2c257 LT |
500 | *p2 = *p1; |
501 | if (*line == ',') { | |
502 | digits = parse_num(line+1, p2); | |
503 | if (!digits) | |
504 | return -1; | |
505 | ||
506 | offset += digits+1; | |
507 | line += digits+1; | |
508 | len -= digits+1; | |
509 | } | |
510 | ||
46979f56 LT |
511 | ex = strlen(expect); |
512 | if (ex > len) | |
513 | return -1; | |
514 | if (memcmp(line, expect, ex)) | |
515 | return -1; | |
516 | ||
517 | return offset + ex; | |
518 | } | |
519 | ||
520 | /* | |
521 | * Parse a unified diff fragment header of the | |
522 | * form "@@ -a,b +c,d @@" | |
523 | */ | |
19c58fb8 | 524 | static int parse_fragment_header(char *line, int len, struct fragment *fragment) |
46979f56 LT |
525 | { |
526 | int offset; | |
527 | ||
528 | if (!len || line[len-1] != '\n') | |
529 | return -1; | |
530 | ||
531 | /* Figure out the number of lines in a fragment */ | |
fab2c257 LT |
532 | offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); |
533 | offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); | |
46979f56 LT |
534 | |
535 | return offset; | |
536 | } | |
537 | ||
19c58fb8 | 538 | static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch) |
c1bb9350 LT |
539 | { |
540 | unsigned long offset, len; | |
541 | ||
19c58fb8 LT |
542 | patch->is_rename = patch->is_copy = 0; |
543 | patch->is_new = patch->is_delete = -1; | |
544 | patch->old_mode = patch->new_mode = 0; | |
545 | patch->old_name = patch->new_name = NULL; | |
46979f56 | 546 | for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) { |
c1bb9350 LT |
547 | unsigned long nextlen; |
548 | ||
549 | len = linelen(line, size); | |
550 | if (!len) | |
551 | break; | |
552 | ||
553 | /* Testing this early allows us to take a few shortcuts.. */ | |
554 | if (len < 6) | |
555 | continue; | |
46979f56 LT |
556 | |
557 | /* | |
558 | * Make sure we don't find any unconnected patch fragmants. | |
559 | * That's a sign that we didn't find a header, and that a | |
560 | * patch has become corrupted/broken up. | |
561 | */ | |
562 | if (!memcmp("@@ -", line, 4)) { | |
19c58fb8 LT |
563 | struct fragment dummy; |
564 | if (parse_fragment_header(line, len, &dummy) < 0) | |
46979f56 LT |
565 | continue; |
566 | error("patch fragment without header at line %d: %.*s", linenr, len-1, line); | |
567 | } | |
568 | ||
c1bb9350 LT |
569 | if (size < len + 6) |
570 | break; | |
571 | ||
572 | /* | |
573 | * Git patch? It might not have a real patch, just a rename | |
574 | * or mode change, so we handle that specially | |
575 | */ | |
576 | if (!memcmp("diff --git ", line, 11)) { | |
19c58fb8 | 577 | int git_hdr_len = parse_git_header(line, len, size, patch); |
206de27e | 578 | if (git_hdr_len <= len) |
c1bb9350 | 579 | continue; |
b7e8039a LT |
580 | if (!patch->old_name && !patch->new_name) { |
581 | if (!patch->def_name) | |
582 | die("git diff header lacks filename information (line %d)", linenr); | |
583 | patch->old_name = patch->new_name = patch->def_name; | |
584 | } | |
a4acb0eb | 585 | *hdrsize = git_hdr_len; |
c1bb9350 LT |
586 | return offset; |
587 | } | |
588 | ||
589 | /** --- followed by +++ ? */ | |
590 | if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4)) | |
591 | continue; | |
592 | ||
593 | /* | |
594 | * We only accept unified patches, so we want it to | |
595 | * at least have "@@ -a,b +c,d @@\n", which is 14 chars | |
596 | * minimum | |
597 | */ | |
598 | nextlen = linelen(line + len, size - len); | |
599 | if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4)) | |
600 | continue; | |
601 | ||
602 | /* Ok, we'll consider it a patch */ | |
19c58fb8 | 603 | parse_traditional_patch(line, line+len, patch); |
c1bb9350 | 604 | *hdrsize = len + nextlen; |
46979f56 | 605 | linenr += 2; |
c1bb9350 LT |
606 | return offset; |
607 | } | |
608 | return -1; | |
609 | } | |
610 | ||
c1bb9350 LT |
611 | /* |
612 | * Parse a unified diff. Note that this really needs | |
613 | * to parse each fragment separately, since the only | |
614 | * way to know the difference between a "---" that is | |
615 | * part of a patch, and a "---" that starts the next | |
616 | * patch is to look at the line counts.. | |
617 | */ | |
19c58fb8 | 618 | static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment) |
c1bb9350 | 619 | { |
3f40315a | 620 | int added, deleted; |
c1bb9350 | 621 | int len = linelen(line, size), offset; |
30996652 | 622 | unsigned long oldlines, newlines; |
c1bb9350 | 623 | |
19c58fb8 | 624 | offset = parse_fragment_header(line, len, fragment); |
c1bb9350 LT |
625 | if (offset < 0) |
626 | return -1; | |
19c58fb8 LT |
627 | oldlines = fragment->oldlines; |
628 | newlines = fragment->newlines; | |
c1bb9350 | 629 | |
30996652 LT |
630 | if (patch->is_new < 0) { |
631 | patch->is_new = !oldlines; | |
632 | if (!oldlines) | |
633 | patch->old_name = NULL; | |
634 | } | |
635 | if (patch->is_delete < 0) { | |
636 | patch->is_delete = !newlines; | |
637 | if (!newlines) | |
638 | patch->new_name = NULL; | |
639 | } | |
640 | ||
641 | if (patch->is_new != !oldlines) | |
642 | return error("new file depends on old contents"); | |
af3f929f LT |
643 | if (patch->is_delete != !newlines) { |
644 | if (newlines) | |
645 | return error("deleted file still has contents"); | |
646 | fprintf(stderr, "** warning: file %s becomes empty but is not deleted\n", patch->new_name); | |
647 | } | |
a4acb0eb | 648 | |
c1bb9350 LT |
649 | /* Parse the thing.. */ |
650 | line += len; | |
651 | size -= len; | |
46979f56 | 652 | linenr++; |
3f40315a | 653 | added = deleted = 0; |
46979f56 | 654 | for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) { |
c1bb9350 LT |
655 | if (!oldlines && !newlines) |
656 | break; | |
657 | len = linelen(line, size); | |
658 | if (!len || line[len-1] != '\n') | |
659 | return -1; | |
660 | switch (*line) { | |
661 | default: | |
662 | return -1; | |
663 | case ' ': | |
664 | oldlines--; | |
665 | newlines--; | |
666 | break; | |
667 | case '-': | |
3f40315a | 668 | deleted++; |
c1bb9350 LT |
669 | oldlines--; |
670 | break; | |
671 | case '+': | |
3f40315a | 672 | added++; |
c1bb9350 LT |
673 | newlines--; |
674 | break; | |
fab2c257 LT |
675 | /* We allow "\ No newline at end of file" */ |
676 | case '\\': | |
3cca928d LT |
677 | if (len < 12 || memcmp(line, "\\ No newline", 12)) |
678 | return -1; | |
fab2c257 | 679 | break; |
c1bb9350 LT |
680 | } |
681 | } | |
8b64647d JH |
682 | /* If a fragment ends with an incomplete line, we failed to include |
683 | * it in the above loop because we hit oldlines == newlines == 0 | |
684 | * before seeing it. | |
685 | */ | |
686 | if (12 < size && !memcmp(line, "\\ No newline", 12)) | |
687 | offset += linelen(line, size); | |
688 | ||
3f40315a LT |
689 | patch->lines_added += added; |
690 | patch->lines_deleted += deleted; | |
c1bb9350 LT |
691 | return offset; |
692 | } | |
693 | ||
19c58fb8 | 694 | static int parse_single_patch(char *line, unsigned long size, struct patch *patch) |
c1bb9350 LT |
695 | { |
696 | unsigned long offset = 0; | |
19c58fb8 | 697 | struct fragment **fragp = &patch->fragments; |
c1bb9350 LT |
698 | |
699 | while (size > 4 && !memcmp(line, "@@ -", 4)) { | |
19c58fb8 LT |
700 | struct fragment *fragment; |
701 | int len; | |
702 | ||
703 | fragment = xmalloc(sizeof(*fragment)); | |
704 | memset(fragment, 0, sizeof(*fragment)); | |
705 | len = parse_fragment(line, size, patch, fragment); | |
c1bb9350 | 706 | if (len <= 0) |
46979f56 | 707 | die("corrupt patch at line %d", linenr); |
c1bb9350 | 708 | |
19c58fb8 LT |
709 | fragment->patch = line; |
710 | fragment->size = len; | |
711 | ||
712 | *fragp = fragment; | |
713 | fragp = &fragment->next; | |
c1bb9350 LT |
714 | |
715 | offset += len; | |
716 | line += len; | |
717 | size -= len; | |
718 | } | |
719 | return offset; | |
720 | } | |
721 | ||
19c58fb8 | 722 | static int parse_chunk(char *buffer, unsigned long size, struct patch *patch) |
c1bb9350 LT |
723 | { |
724 | int hdrsize, patchsize; | |
19c58fb8 | 725 | int offset = find_header(buffer, size, &hdrsize, patch); |
c1bb9350 LT |
726 | |
727 | if (offset < 0) | |
728 | return offset; | |
c1bb9350 | 729 | |
19c58fb8 | 730 | patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch); |
c1bb9350 LT |
731 | |
732 | return offset + hdrsize + patchsize; | |
733 | } | |
734 | ||
6da4016a LT |
735 | static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; |
736 | static const char minuses[]= "----------------------------------------------------------------------"; | |
3f40315a LT |
737 | |
738 | static void show_stats(struct patch *patch) | |
739 | { | |
62917097 | 740 | const char *prefix = ""; |
03b4538b | 741 | char *name = patch->new_name; |
95bedc9e | 742 | int len, max, add, del, total; |
3f40315a | 743 | |
5041aa70 | 744 | if (!name) |
03b4538b | 745 | name = patch->old_name; |
3f40315a LT |
746 | |
747 | /* | |
748 | * "scale" the filename | |
749 | */ | |
750 | len = strlen(name); | |
751 | max = max_len; | |
752 | if (max > 50) | |
753 | max = 50; | |
62917097 LT |
754 | if (len > max) { |
755 | char *slash; | |
756 | prefix = "..."; | |
757 | max -= 3; | |
3f40315a | 758 | name += len - max; |
62917097 LT |
759 | slash = strchr(name, '/'); |
760 | if (slash) | |
761 | name = slash; | |
762 | } | |
3f40315a LT |
763 | len = max; |
764 | ||
765 | /* | |
766 | * scale the add/delete | |
767 | */ | |
768 | max = max_change; | |
769 | if (max + len > 70) | |
770 | max = 70 - len; | |
95bedc9e LT |
771 | |
772 | add = patch->lines_added; | |
773 | del = patch->lines_deleted; | |
774 | total = add + del; | |
775 | ||
69f956e1 SV |
776 | if (max_change > 0) { |
777 | total = (total * max + max_change / 2) / max_change; | |
778 | add = (add * max + max_change / 2) / max_change; | |
779 | del = total - add; | |
780 | } | |
62917097 | 781 | printf(" %s%-*s |%5d %.*s%.*s\n", prefix, |
3f40315a LT |
782 | len, name, patch->lines_added + patch->lines_deleted, |
783 | add, pluses, del, minuses); | |
784 | } | |
785 | ||
3cca928d LT |
786 | static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size) |
787 | { | |
788 | int fd; | |
789 | unsigned long got; | |
790 | ||
791 | switch (st->st_mode & S_IFMT) { | |
792 | case S_IFLNK: | |
793 | return readlink(path, buf, size); | |
794 | case S_IFREG: | |
795 | fd = open(path, O_RDONLY); | |
796 | if (fd < 0) | |
797 | return error("unable to open %s", path); | |
798 | got = 0; | |
799 | for (;;) { | |
800 | int ret = read(fd, buf + got, size - got); | |
801 | if (ret < 0) { | |
802 | if (errno == EAGAIN) | |
803 | continue; | |
804 | break; | |
805 | } | |
806 | if (!ret) | |
807 | break; | |
808 | got += ret; | |
809 | } | |
810 | close(fd); | |
811 | return got; | |
812 | ||
813 | default: | |
814 | return -1; | |
815 | } | |
816 | } | |
817 | ||
818 | static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line) | |
819 | { | |
6e7c92a9 LT |
820 | int i; |
821 | unsigned long start, backwards, forwards; | |
3cca928d LT |
822 | |
823 | if (fragsize > size) | |
824 | return -1; | |
825 | ||
826 | start = 0; | |
827 | if (line > 1) { | |
3cca928d | 828 | unsigned long offset = 0; |
6e7c92a9 LT |
829 | i = line-1; |
830 | while (offset + fragsize <= size) { | |
3cca928d LT |
831 | if (buf[offset++] == '\n') { |
832 | start = offset; | |
6e7c92a9 | 833 | if (!--i) |
3cca928d LT |
834 | break; |
835 | } | |
836 | } | |
837 | } | |
838 | ||
839 | /* Exact line number? */ | |
840 | if (!memcmp(buf + start, fragment, fragsize)) | |
841 | return start; | |
842 | ||
6e7c92a9 LT |
843 | /* |
844 | * There's probably some smart way to do this, but I'll leave | |
845 | * that to the smart and beautiful people. I'm simple and stupid. | |
846 | */ | |
847 | backwards = start; | |
848 | forwards = start; | |
849 | for (i = 0; ; i++) { | |
850 | unsigned long try; | |
851 | int n; | |
852 | ||
853 | /* "backward" */ | |
854 | if (i & 1) { | |
855 | if (!backwards) { | |
856 | if (forwards + fragsize > size) | |
857 | break; | |
858 | continue; | |
859 | } | |
860 | do { | |
861 | --backwards; | |
862 | } while (backwards && buf[backwards-1] != '\n'); | |
863 | try = backwards; | |
864 | } else { | |
865 | while (forwards + fragsize <= size) { | |
866 | if (buf[forwards++] == '\n') | |
867 | break; | |
868 | } | |
869 | try = forwards; | |
870 | } | |
871 | ||
872 | if (try + fragsize > size) | |
873 | continue; | |
874 | if (memcmp(buf + try, fragment, fragsize)) | |
875 | continue; | |
876 | n = (i >> 1)+1; | |
877 | if (i & 1) | |
878 | n = -n; | |
6e7c92a9 LT |
879 | return try; |
880 | } | |
881 | ||
3cca928d LT |
882 | /* |
883 | * We should start searching forward and backward. | |
884 | */ | |
885 | return -1; | |
886 | } | |
887 | ||
6e7c92a9 LT |
888 | struct buffer_desc { |
889 | char *buffer; | |
890 | unsigned long size; | |
891 | unsigned long alloc; | |
892 | }; | |
893 | ||
894 | static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag) | |
3cca928d | 895 | { |
6e7c92a9 | 896 | char *buf = desc->buffer; |
3cca928d LT |
897 | const char *patch = frag->patch; |
898 | int offset, size = frag->size; | |
899 | char *old = xmalloc(size); | |
900 | char *new = xmalloc(size); | |
901 | int oldsize = 0, newsize = 0; | |
902 | ||
903 | while (size > 0) { | |
904 | int len = linelen(patch, size); | |
905 | int plen; | |
906 | ||
907 | if (!len) | |
908 | break; | |
909 | ||
910 | /* | |
911 | * "plen" is how much of the line we should use for | |
912 | * the actual patch data. Normally we just remove the | |
913 | * first character on the line, but if the line is | |
914 | * followed by "\ No newline", then we also remove the | |
915 | * last one (which is the newline, of course). | |
916 | */ | |
917 | plen = len-1; | |
8b64647d | 918 | if (len < size && patch[len] == '\\') |
3cca928d LT |
919 | plen--; |
920 | switch (*patch) { | |
921 | case ' ': | |
922 | case '-': | |
923 | memcpy(old + oldsize, patch + 1, plen); | |
924 | oldsize += plen; | |
925 | if (*patch == '-') | |
926 | break; | |
927 | /* Fall-through for ' ' */ | |
928 | case '+': | |
929 | memcpy(new + newsize, patch + 1, plen); | |
930 | newsize += plen; | |
931 | break; | |
932 | case '@': case '\\': | |
933 | /* Ignore it, we already handled it */ | |
934 | break; | |
935 | default: | |
936 | return -1; | |
937 | } | |
938 | patch += len; | |
939 | size -= len; | |
940 | } | |
941 | ||
6e7c92a9 | 942 | offset = find_offset(buf, desc->size, old, oldsize, frag->newpos); |
3cca928d | 943 | if (offset >= 0) { |
6e7c92a9 LT |
944 | int diff = newsize - oldsize; |
945 | unsigned long size = desc->size + diff; | |
946 | unsigned long alloc = desc->alloc; | |
947 | ||
948 | if (size > alloc) { | |
949 | alloc = size + 8192; | |
950 | desc->alloc = alloc; | |
951 | buf = xrealloc(buf, alloc); | |
952 | desc->buffer = buf; | |
953 | } | |
954 | desc->size = size; | |
955 | memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize); | |
956 | memcpy(buf + offset, new, newsize); | |
3cca928d LT |
957 | offset = 0; |
958 | } | |
959 | ||
960 | free(old); | |
961 | free(new); | |
962 | return offset; | |
963 | } | |
964 | ||
6e7c92a9 | 965 | static int apply_fragments(struct buffer_desc *desc, struct patch *patch) |
3cca928d LT |
966 | { |
967 | struct fragment *frag = patch->fragments; | |
968 | ||
969 | while (frag) { | |
6e7c92a9 | 970 | if (apply_one_fragment(desc, frag) < 0) |
3cca928d LT |
971 | return error("patch failed: %s:%d", patch->old_name, frag->oldpos); |
972 | frag = frag->next; | |
973 | } | |
30996652 | 974 | return 0; |
3cca928d LT |
975 | } |
976 | ||
977 | static int apply_data(struct patch *patch, struct stat *st) | |
978 | { | |
6e7c92a9 LT |
979 | char *buf; |
980 | unsigned long size, alloc; | |
981 | struct buffer_desc desc; | |
3cca928d | 982 | |
30996652 LT |
983 | size = 0; |
984 | alloc = 0; | |
985 | buf = NULL; | |
986 | if (patch->old_name) { | |
987 | size = st->st_size; | |
988 | alloc = size + 8192; | |
989 | buf = xmalloc(alloc); | |
990 | if (read_old_data(st, patch->old_name, buf, alloc) != size) | |
991 | return error("read of %s failed", patch->old_name); | |
992 | } | |
6e7c92a9 LT |
993 | |
994 | desc.size = size; | |
995 | desc.alloc = alloc; | |
996 | desc.buffer = buf; | |
997 | if (apply_fragments(&desc, patch) < 0) | |
3cca928d | 998 | return -1; |
6e7c92a9 LT |
999 | patch->result = desc.buffer; |
1000 | patch->resultsize = desc.size; | |
5aa7d94c LT |
1001 | |
1002 | if (patch->is_delete && patch->resultsize) | |
1003 | return error("removal patch leaves file contents"); | |
1004 | ||
3cca928d LT |
1005 | return 0; |
1006 | } | |
1007 | ||
a577284a | 1008 | static int check_patch(struct patch *patch) |
fab2c257 | 1009 | { |
a577284a | 1010 | struct stat st; |
fab2c257 LT |
1011 | const char *old_name = patch->old_name; |
1012 | const char *new_name = patch->new_name; | |
1013 | ||
1014 | if (old_name) { | |
a577284a LT |
1015 | int changed; |
1016 | ||
a577284a | 1017 | if (lstat(old_name, &st) < 0) |
84fb9a4d | 1018 | return error("%s: %s", old_name, strerror(errno)); |
3cca928d LT |
1019 | if (check_index) { |
1020 | int pos = cache_name_pos(old_name, strlen(old_name)); | |
1021 | if (pos < 0) | |
1022 | return error("%s: does not exist in index", old_name); | |
1023 | changed = ce_match_stat(active_cache[pos], &st); | |
1024 | if (changed) | |
1025 | return error("%s: does not match index", old_name); | |
1026 | } | |
1027 | if (patch->is_new < 0) | |
1028 | patch->is_new = 0; | |
de4971b5 | 1029 | st.st_mode = ntohl(create_ce_mode(st.st_mode)); |
a577284a LT |
1030 | if (!patch->old_mode) |
1031 | patch->old_mode = st.st_mode; | |
3cca928d LT |
1032 | if ((st.st_mode ^ patch->old_mode) & S_IFMT) |
1033 | return error("%s: wrong type", old_name); | |
1034 | if (st.st_mode != patch->old_mode) | |
1035 | fprintf(stderr, "warning: %s has type %o, expected %o\n", | |
1036 | old_name, st.st_mode, patch->old_mode); | |
fab2c257 | 1037 | } |
a577284a | 1038 | |
fab2c257 | 1039 | if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) { |
3cca928d | 1040 | if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0) |
a577284a LT |
1041 | return error("%s: already exists in index", new_name); |
1042 | if (!lstat(new_name, &st)) | |
1043 | return error("%s: already exists in working directory", new_name); | |
1044 | if (errno != ENOENT) | |
1045 | return error("%s: %s", new_name, strerror(errno)); | |
5aa7d94c LT |
1046 | if (!patch->new_mode) |
1047 | patch->new_mode = S_IFREG | 0644; | |
fab2c257 | 1048 | } |
3cca928d LT |
1049 | |
1050 | if (new_name && old_name) { | |
1051 | int same = !strcmp(old_name, new_name); | |
1052 | if (!patch->new_mode) | |
1053 | patch->new_mode = patch->old_mode; | |
1054 | if ((patch->old_mode ^ patch->new_mode) & S_IFMT) | |
1055 | return error("new mode (%o) of %s does not match old mode (%o)%s%s", | |
1056 | patch->new_mode, new_name, patch->old_mode, | |
1057 | same ? "" : " of ", same ? "" : old_name); | |
1058 | } | |
1059 | ||
1060 | if (apply_data(patch, &st) < 0) | |
1061 | return error("%s: patch does not apply", old_name); | |
a577284a | 1062 | return 0; |
fab2c257 LT |
1063 | } |
1064 | ||
a577284a | 1065 | static int check_patch_list(struct patch *patch) |
19c58fb8 | 1066 | { |
a577284a LT |
1067 | int error = 0; |
1068 | ||
1069 | for (;patch ; patch = patch->next) | |
1070 | error |= check_patch(patch); | |
1071 | return error; | |
1072 | } | |
1073 | ||
1074 | static void show_file(int c, unsigned int mode, const char *name) | |
1075 | { | |
1076 | printf("%c %o %s\n", c, mode, name); | |
1077 | } | |
3f40315a | 1078 | |
a577284a LT |
1079 | static void show_file_list(struct patch *patch) |
1080 | { | |
1081 | for (;patch ; patch = patch->next) { | |
1082 | if (patch->is_rename) { | |
1083 | show_file('-', patch->old_mode, patch->old_name); | |
1084 | show_file('+', patch->new_mode, patch->new_name); | |
1085 | continue; | |
1086 | } | |
1087 | if (patch->is_copy || patch->is_new) { | |
1088 | show_file('+', patch->new_mode, patch->new_name); | |
1089 | continue; | |
1090 | } | |
1091 | if (patch->is_delete) { | |
1092 | show_file('-', patch->old_mode, patch->old_name); | |
1093 | continue; | |
1094 | } | |
1095 | if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) { | |
1096 | printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name); | |
1097 | continue; | |
19c58fb8 | 1098 | } |
a577284a LT |
1099 | printf("M %o %s\n", patch->old_mode, patch->old_name); |
1100 | } | |
1101 | } | |
fab2c257 | 1102 | |
a577284a LT |
1103 | static void stat_patch_list(struct patch *patch) |
1104 | { | |
1105 | int files, adds, dels; | |
1106 | ||
1107 | for (files = adds = dels = 0 ; patch ; patch = patch->next) { | |
1108 | files++; | |
1109 | adds += patch->lines_added; | |
1110 | dels += patch->lines_deleted; | |
1111 | show_stats(patch); | |
1112 | } | |
1113 | ||
1114 | printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels); | |
3f40315a LT |
1115 | } |
1116 | ||
96c912a4 JH |
1117 | static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name) |
1118 | { | |
1119 | if (mode) | |
1120 | printf(" %s mode %06o %s\n", newdelete, mode, name); | |
1121 | else | |
1122 | printf(" %s %s\n", newdelete, name); | |
1123 | } | |
1124 | ||
1125 | static void show_mode_change(struct patch *p, int show_name) | |
1126 | { | |
1127 | if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) { | |
1128 | if (show_name) | |
1129 | printf(" mode change %06o => %06o %s\n", | |
1130 | p->old_mode, p->new_mode, p->new_name); | |
1131 | else | |
1132 | printf(" mode change %06o => %06o\n", | |
1133 | p->old_mode, p->new_mode); | |
1134 | } | |
1135 | } | |
1136 | ||
1137 | static void show_rename_copy(struct patch *p) | |
1138 | { | |
1139 | const char *renamecopy = p->is_rename ? "rename" : "copy"; | |
1140 | const char *old, *new; | |
1141 | ||
1142 | /* Find common prefix */ | |
1143 | old = p->old_name; | |
1144 | new = p->new_name; | |
1145 | while (1) { | |
1146 | const char *slash_old, *slash_new; | |
1147 | slash_old = strchr(old, '/'); | |
1148 | slash_new = strchr(new, '/'); | |
1149 | if (!slash_old || | |
1150 | !slash_new || | |
1151 | slash_old - old != slash_new - new || | |
1152 | memcmp(old, new, slash_new - new)) | |
1153 | break; | |
1154 | old = slash_old + 1; | |
1155 | new = slash_new + 1; | |
1156 | } | |
1157 | /* p->old_name thru old is the common prefix, and old and new | |
1158 | * through the end of names are renames | |
1159 | */ | |
1160 | if (old != p->old_name) | |
1161 | printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, | |
e30e814d | 1162 | (int)(old - p->old_name), p->old_name, |
96c912a4 JH |
1163 | old, new, p->score); |
1164 | else | |
1165 | printf(" %s %s => %s (%d%%)\n", renamecopy, | |
1166 | p->old_name, p->new_name, p->score); | |
1167 | show_mode_change(p, 0); | |
1168 | } | |
1169 | ||
1170 | static void summary_patch_list(struct patch *patch) | |
1171 | { | |
1172 | struct patch *p; | |
1173 | ||
1174 | for (p = patch; p; p = p->next) { | |
1175 | if (p->is_new) | |
1176 | show_file_mode_name("create", p->new_mode, p->new_name); | |
1177 | else if (p->is_delete) | |
1178 | show_file_mode_name("delete", p->old_mode, p->old_name); | |
1179 | else { | |
1180 | if (p->is_rename || p->is_copy) | |
1181 | show_rename_copy(p); | |
1182 | else { | |
1183 | if (p->score) { | |
1184 | printf(" rewrite %s (%d%%)\n", | |
1185 | p->new_name, p->score); | |
1186 | show_mode_change(p, 0); | |
1187 | } | |
1188 | else | |
1189 | show_mode_change(p, 1); | |
1190 | } | |
1191 | } | |
1192 | } | |
1193 | } | |
1194 | ||
3f40315a LT |
1195 | static void patch_stats(struct patch *patch) |
1196 | { | |
1197 | int lines = patch->lines_added + patch->lines_deleted; | |
1198 | ||
1199 | if (lines > max_change) | |
1200 | max_change = lines; | |
1201 | if (patch->old_name) { | |
1202 | int len = strlen(patch->old_name); | |
1203 | if (len > max_len) | |
1204 | max_len = len; | |
1205 | } | |
1206 | if (patch->new_name) { | |
1207 | int len = strlen(patch->new_name); | |
1208 | if (len > max_len) | |
1209 | max_len = len; | |
1210 | } | |
19c58fb8 LT |
1211 | } |
1212 | ||
5aa7d94c LT |
1213 | static void remove_file(struct patch *patch) |
1214 | { | |
1215 | if (write_index) { | |
1216 | if (remove_file_from_cache(patch->old_name) < 0) | |
1217 | die("unable to remove %s from index", patch->old_name); | |
1218 | } | |
1219 | unlink(patch->old_name); | |
1220 | } | |
1221 | ||
1222 | static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size) | |
1223 | { | |
1224 | struct stat st; | |
1225 | struct cache_entry *ce; | |
1226 | int namelen = strlen(path); | |
1227 | unsigned ce_size = cache_entry_size(namelen); | |
1228 | ||
1229 | if (!write_index) | |
1230 | return; | |
1231 | ||
1232 | ce = xmalloc(ce_size); | |
1233 | memset(ce, 0, ce_size); | |
1234 | memcpy(ce->name, path, namelen); | |
1235 | ce->ce_mode = create_ce_mode(mode); | |
1236 | ce->ce_flags = htons(namelen); | |
1237 | if (lstat(path, &st) < 0) | |
1238 | die("unable to stat newly created file %s", path); | |
1239 | fill_stat_cache_info(ce, &st); | |
1240 | if (write_sha1_file(buf, size, "blob", ce->sha1) < 0) | |
1241 | die("unable to create backing store for newly created file %s", path); | |
1242 | if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) | |
1243 | die("unable to add cache entry for %s", path); | |
1244 | } | |
1245 | ||
5c8af185 LT |
1246 | static void create_subdirectories(const char *path) |
1247 | { | |
1248 | int len = strlen(path); | |
1249 | char *buf = xmalloc(len + 1); | |
1250 | const char *slash = path; | |
1251 | ||
1252 | while ((slash = strchr(slash+1, '/')) != NULL) { | |
1253 | len = slash - path; | |
1254 | memcpy(buf, path, len); | |
1255 | buf[len] = 0; | |
f312de01 | 1256 | if (mkdir(buf, 0777) < 0) { |
5c8af185 LT |
1257 | if (errno != EEXIST) |
1258 | break; | |
1259 | } | |
1260 | } | |
1261 | free(buf); | |
1262 | } | |
1263 | ||
1b668341 LT |
1264 | static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) |
1265 | { | |
1266 | int fd; | |
1267 | ||
1268 | if (S_ISLNK(mode)) | |
1269 | return symlink(buf, path); | |
1270 | fd = open(path, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, (mode & 0100) ? 0777 : 0666); | |
1271 | if (fd < 0) | |
1272 | return -1; | |
1273 | while (size) { | |
1274 | int written = write(fd, buf, size); | |
1275 | if (written < 0) { | |
1276 | if (errno == EINTR || errno == EAGAIN) | |
1277 | continue; | |
1278 | die("writing file %s: %s", path, strerror(errno)); | |
1279 | } | |
1280 | if (!written) | |
1281 | die("out of space writing file %s", path); | |
1282 | buf += written; | |
1283 | size -= written; | |
1284 | } | |
1285 | if (close(fd) < 0) | |
1286 | die("closing file %s: %s", path, strerror(errno)); | |
1287 | return 0; | |
1288 | } | |
1289 | ||
5c8af185 LT |
1290 | /* |
1291 | * We optimistically assume that the directories exist, | |
1292 | * which is true 99% of the time anyway. If they don't, | |
1293 | * we create them and try again. | |
1294 | */ | |
1b668341 | 1295 | static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size) |
5c8af185 | 1296 | { |
1b668341 LT |
1297 | if (!try_create_file(path, mode, buf, size)) |
1298 | return; | |
5c8af185 | 1299 | |
1b668341 | 1300 | if (errno == ENOENT) { |
5c8af185 | 1301 | create_subdirectories(path); |
1b668341 LT |
1302 | if (!try_create_file(path, mode, buf, size)) |
1303 | return; | |
5c8af185 | 1304 | } |
5c8af185 | 1305 | |
1b668341 LT |
1306 | if (errno == EEXIST) { |
1307 | unsigned int nr = getpid(); | |
5c8af185 | 1308 | |
1b668341 LT |
1309 | for (;;) { |
1310 | const char *newpath; | |
1311 | newpath = mkpath("%s~%u", path, nr); | |
1312 | if (!try_create_file(newpath, mode, buf, size)) { | |
1313 | if (!rename(newpath, path)) | |
1314 | return; | |
1315 | unlink(newpath); | |
1316 | break; | |
1317 | } | |
1318 | if (errno != EEXIST) | |
1319 | break; | |
1320 | } | |
5c8af185 | 1321 | } |
1b668341 | 1322 | die("unable to write file %s mode %o", path, mode); |
5c8af185 LT |
1323 | } |
1324 | ||
5aa7d94c LT |
1325 | static void create_file(struct patch *patch) |
1326 | { | |
1327 | const char *path = patch->new_name; | |
1328 | unsigned mode = patch->new_mode; | |
1329 | unsigned long size = patch->resultsize; | |
1330 | char *buf = patch->result; | |
1331 | ||
1332 | if (!mode) | |
1333 | mode = S_IFREG | 0644; | |
1b668341 LT |
1334 | create_one_file(path, mode, buf, size); |
1335 | add_index_file(path, mode, buf, size); | |
5aa7d94c LT |
1336 | } |
1337 | ||
1338 | static void write_out_one_result(struct patch *patch) | |
1339 | { | |
1340 | if (patch->is_delete > 0) { | |
1341 | remove_file(patch); | |
1342 | return; | |
1343 | } | |
1344 | if (patch->is_new > 0 || patch->is_copy) { | |
1345 | create_file(patch); | |
1346 | return; | |
1347 | } | |
1348 | /* | |
1349 | * Rename or modification boils down to the same | |
1350 | * thing: remove the old, write the new | |
1351 | */ | |
1352 | remove_file(patch); | |
1353 | create_file(patch); | |
1354 | } | |
1355 | ||
d854f783 | 1356 | static void write_out_results(struct patch *list, int skipped_patch) |
5aa7d94c | 1357 | { |
d854f783 | 1358 | if (!list && !skipped_patch) |
f7b79707 LT |
1359 | die("No changes"); |
1360 | ||
5aa7d94c LT |
1361 | while (list) { |
1362 | write_out_one_result(list); | |
1363 | list = list->next; | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | static struct cache_file cache_file; | |
1368 | ||
d854f783 JH |
1369 | static struct excludes { |
1370 | struct excludes *next; | |
1371 | const char *path; | |
1372 | } *excludes; | |
1373 | ||
1374 | static int use_patch(struct patch *p) | |
1375 | { | |
1376 | const char *pathname = p->new_name ? : p->old_name; | |
1377 | struct excludes *x = excludes; | |
1378 | while (x) { | |
1379 | if (fnmatch(x->path, pathname, 0) == 0) | |
1380 | return 0; | |
1381 | x = x->next; | |
1382 | } | |
1383 | return 1; | |
1384 | } | |
1385 | ||
c1bb9350 LT |
1386 | static int apply_patch(int fd) |
1387 | { | |
5aa7d94c | 1388 | int newfd; |
c1bb9350 LT |
1389 | unsigned long offset, size; |
1390 | char *buffer = read_patch_file(fd, &size); | |
19c58fb8 | 1391 | struct patch *list = NULL, **listp = &list; |
d854f783 | 1392 | int skipped_patch = 0; |
c1bb9350 LT |
1393 | |
1394 | if (!buffer) | |
1395 | return -1; | |
1396 | offset = 0; | |
1397 | while (size > 0) { | |
19c58fb8 LT |
1398 | struct patch *patch; |
1399 | int nr; | |
1400 | ||
1401 | patch = xmalloc(sizeof(*patch)); | |
1402 | memset(patch, 0, sizeof(*patch)); | |
1403 | nr = parse_chunk(buffer + offset, size, patch); | |
c1bb9350 LT |
1404 | if (nr < 0) |
1405 | break; | |
d854f783 JH |
1406 | if (use_patch(patch)) { |
1407 | patch_stats(patch); | |
1408 | *listp = patch; | |
1409 | listp = &patch->next; | |
1410 | } else { | |
1411 | /* perhaps free it a bit better? */ | |
1412 | free(patch); | |
1413 | skipped_patch++; | |
1414 | } | |
c1bb9350 LT |
1415 | offset += nr; |
1416 | size -= nr; | |
1417 | } | |
19c58fb8 | 1418 | |
5aa7d94c LT |
1419 | newfd = -1; |
1420 | write_index = check_index && apply; | |
1421 | if (write_index) | |
1422 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); | |
1423 | if (check_index) { | |
1424 | if (read_cache() < 0) | |
1425 | die("unable to read index file"); | |
1426 | } | |
1427 | ||
a577284a LT |
1428 | if ((check || apply) && check_patch_list(list) < 0) |
1429 | exit(1); | |
1430 | ||
5aa7d94c | 1431 | if (apply) |
d854f783 | 1432 | write_out_results(list, skipped_patch); |
5aa7d94c LT |
1433 | |
1434 | if (write_index) { | |
1435 | if (write_cache(newfd, active_cache, active_nr) || | |
1436 | commit_index_file(&cache_file)) | |
1437 | die("Unable to write new cachefile"); | |
1438 | } | |
1439 | ||
a577284a LT |
1440 | if (show_files) |
1441 | show_file_list(list); | |
1442 | ||
1443 | if (diffstat) | |
1444 | stat_patch_list(list); | |
19c58fb8 | 1445 | |
96c912a4 JH |
1446 | if (summary) |
1447 | summary_patch_list(list); | |
1448 | ||
c1bb9350 LT |
1449 | free(buffer); |
1450 | return 0; | |
1451 | } | |
1452 | ||
1453 | int main(int argc, char **argv) | |
1454 | { | |
1455 | int i; | |
4dfdbe10 | 1456 | int read_stdin = 1; |
c1bb9350 | 1457 | |
c1bb9350 LT |
1458 | for (i = 1; i < argc; i++) { |
1459 | const char *arg = argv[i]; | |
1460 | int fd; | |
1461 | ||
1462 | if (!strcmp(arg, "-")) { | |
1463 | apply_patch(0); | |
4dfdbe10 | 1464 | read_stdin = 0; |
c1bb9350 LT |
1465 | continue; |
1466 | } | |
d854f783 JH |
1467 | if (!strncmp(arg, "--exclude=", 10)) { |
1468 | struct excludes *x = xmalloc(sizeof(*x)); | |
1469 | x->path = arg + 10; | |
1470 | x->next = excludes; | |
1471 | excludes = x; | |
1472 | continue; | |
1473 | } | |
12dd6e8c | 1474 | /* NEEDSWORK: this does not do anything at this moment. */ |
c1bb9350 LT |
1475 | if (!strcmp(arg, "--no-merge")) { |
1476 | merge_patch = 0; | |
1477 | continue; | |
1478 | } | |
fab2c257 | 1479 | if (!strcmp(arg, "--stat")) { |
a577284a | 1480 | apply = 0; |
fab2c257 LT |
1481 | diffstat = 1; |
1482 | continue; | |
1483 | } | |
96c912a4 JH |
1484 | if (!strcmp(arg, "--summary")) { |
1485 | apply = 0; | |
1486 | summary = 1; | |
1487 | continue; | |
1488 | } | |
a577284a LT |
1489 | if (!strcmp(arg, "--check")) { |
1490 | apply = 0; | |
1491 | check = 1; | |
1492 | continue; | |
1493 | } | |
3cca928d LT |
1494 | if (!strcmp(arg, "--index")) { |
1495 | check_index = 1; | |
1496 | continue; | |
1497 | } | |
aefa4a5b LT |
1498 | if (!strcmp(arg, "--apply")) { |
1499 | apply = 1; | |
1500 | continue; | |
1501 | } | |
a577284a LT |
1502 | if (!strcmp(arg, "--show-files")) { |
1503 | show_files = 1; | |
1504 | continue; | |
1505 | } | |
c1bb9350 LT |
1506 | fd = open(arg, O_RDONLY); |
1507 | if (fd < 0) | |
1508 | usage(apply_usage); | |
4dfdbe10 | 1509 | read_stdin = 0; |
c1bb9350 LT |
1510 | apply_patch(fd); |
1511 | close(fd); | |
1512 | } | |
4dfdbe10 LT |
1513 | if (read_stdin) |
1514 | apply_patch(0); | |
c1bb9350 LT |
1515 | return 0; |
1516 | } |