Line | Count | Source |
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 | | */ |
9 | | |
10 | | #define USE_THE_REPOSITORY_VARIABLE |
11 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
12 | | |
13 | | #include "git-compat-util.h" |
14 | | #include "abspath.h" |
15 | | #include "base85.h" |
16 | | #include "config.h" |
17 | | #include "odb.h" |
18 | | #include "delta.h" |
19 | | #include "diff.h" |
20 | | #include "dir.h" |
21 | | #include "environment.h" |
22 | | #include "gettext.h" |
23 | | #include "hex.h" |
24 | | #include "xdiff-interface.h" |
25 | | #include "merge-ll.h" |
26 | | #include "lockfile.h" |
27 | | #include "name-hash.h" |
28 | | #include "object-name.h" |
29 | | #include "object-file.h" |
30 | | #include "parse-options.h" |
31 | | #include "path.h" |
32 | | #include "quote.h" |
33 | | #include "read-cache.h" |
34 | | #include "repository.h" |
35 | | #include "rerere.h" |
36 | | #include "apply.h" |
37 | | #include "entry.h" |
38 | | #include "setup.h" |
39 | | #include "symlinks.h" |
40 | | #include "wildmatch.h" |
41 | | #include "ws.h" |
42 | | |
43 | | struct gitdiff_data { |
44 | | struct strbuf *root; |
45 | | int linenr; |
46 | | int p_value; |
47 | | }; |
48 | | |
49 | | static void git_apply_config(void) |
50 | 0 | { |
51 | 0 | repo_config_get_string(the_repository, "apply.whitespace", &apply_default_whitespace); |
52 | 0 | repo_config_get_string(the_repository, "apply.ignorewhitespace", &apply_default_ignorewhitespace); |
53 | 0 | repo_config(the_repository, git_xmerge_config, NULL); |
54 | 0 | } |
55 | | |
56 | | static int parse_whitespace_option(struct apply_state *state, const char *option) |
57 | 0 | { |
58 | 0 | if (!option) { |
59 | 0 | state->ws_error_action = warn_on_ws_error; |
60 | 0 | return 0; |
61 | 0 | } |
62 | 0 | if (!strcmp(option, "warn")) { |
63 | 0 | state->ws_error_action = warn_on_ws_error; |
64 | 0 | return 0; |
65 | 0 | } |
66 | 0 | if (!strcmp(option, "nowarn")) { |
67 | 0 | state->ws_error_action = nowarn_ws_error; |
68 | 0 | return 0; |
69 | 0 | } |
70 | 0 | if (!strcmp(option, "error")) { |
71 | 0 | state->ws_error_action = die_on_ws_error; |
72 | 0 | return 0; |
73 | 0 | } |
74 | 0 | if (!strcmp(option, "error-all")) { |
75 | 0 | state->ws_error_action = die_on_ws_error; |
76 | 0 | state->squelch_whitespace_errors = 0; |
77 | 0 | return 0; |
78 | 0 | } |
79 | 0 | if (!strcmp(option, "strip") || !strcmp(option, "fix")) { |
80 | 0 | state->ws_error_action = correct_ws_error; |
81 | 0 | return 0; |
82 | 0 | } |
83 | | /* |
84 | | * Please update $__git_whitespacelist in git-completion.bash, |
85 | | * Documentation/git-apply.adoc, and Documentation/git-am.adoc |
86 | | * when you add new options. |
87 | | */ |
88 | 0 | return error(_("unrecognized whitespace option '%s'"), option); |
89 | 0 | } |
90 | | |
91 | | static int parse_ignorewhitespace_option(struct apply_state *state, |
92 | | const char *option) |
93 | 0 | { |
94 | 0 | if (!option || !strcmp(option, "no") || |
95 | 0 | !strcmp(option, "false") || !strcmp(option, "never") || |
96 | 0 | !strcmp(option, "none")) { |
97 | 0 | state->ws_ignore_action = ignore_ws_none; |
98 | 0 | return 0; |
99 | 0 | } |
100 | 0 | if (!strcmp(option, "change")) { |
101 | 0 | state->ws_ignore_action = ignore_ws_change; |
102 | 0 | return 0; |
103 | 0 | } |
104 | 0 | return error(_("unrecognized whitespace ignore option '%s'"), option); |
105 | 0 | } |
106 | | |
107 | | int init_apply_state(struct apply_state *state, |
108 | | struct repository *repo, |
109 | | const char *prefix) |
110 | 0 | { |
111 | 0 | memset(state, 0, sizeof(*state)); |
112 | 0 | state->prefix = prefix; |
113 | 0 | state->repo = repo; |
114 | 0 | state->apply = 1; |
115 | 0 | state->line_termination = '\n'; |
116 | 0 | state->p_value = 1; |
117 | 0 | state->p_context = UINT_MAX; |
118 | 0 | state->squelch_whitespace_errors = 5; |
119 | 0 | state->ws_error_action = warn_on_ws_error; |
120 | 0 | state->ws_ignore_action = ignore_ws_none; |
121 | 0 | state->linenr = 1; |
122 | 0 | string_list_init_nodup(&state->fn_table); |
123 | 0 | string_list_init_nodup(&state->limit_by_name); |
124 | 0 | strset_init(&state->removed_symlinks); |
125 | 0 | strset_init(&state->kept_symlinks); |
126 | 0 | strbuf_init(&state->root, 0); |
127 | |
|
128 | 0 | git_apply_config(); |
129 | 0 | if (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace)) |
130 | 0 | return -1; |
131 | 0 | if (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace)) |
132 | 0 | return -1; |
133 | 0 | return 0; |
134 | 0 | } |
135 | | |
136 | | void clear_apply_state(struct apply_state *state) |
137 | 0 | { |
138 | 0 | string_list_clear(&state->limit_by_name, 0); |
139 | 0 | strset_clear(&state->removed_symlinks); |
140 | 0 | strset_clear(&state->kept_symlinks); |
141 | 0 | strbuf_release(&state->root); |
142 | 0 | FREE_AND_NULL(state->fake_ancestor); |
143 | | |
144 | | /* &state->fn_table is cleared at the end of apply_patch() */ |
145 | 0 | } |
146 | | |
147 | | static void mute_routine(const char *msg UNUSED, va_list params UNUSED) |
148 | 0 | { |
149 | | /* do nothing */ |
150 | 0 | } |
151 | | |
152 | | int check_apply_state(struct apply_state *state, int force_apply) |
153 | 0 | { |
154 | 0 | int is_not_gitdir = !startup_info->have_repository; |
155 | |
|
156 | 0 | if (state->apply_with_reject && state->threeway) |
157 | 0 | return error(_("options '%s' and '%s' cannot be used together"), "--reject", "--3way"); |
158 | 0 | if (state->threeway) { |
159 | 0 | if (is_not_gitdir) |
160 | 0 | return error(_("'%s' outside a repository"), "--3way"); |
161 | 0 | state->check_index = 1; |
162 | 0 | } |
163 | 0 | if (state->apply_with_reject) { |
164 | 0 | state->apply = 1; |
165 | 0 | if (state->apply_verbosity == verbosity_normal) |
166 | 0 | state->apply_verbosity = verbosity_verbose; |
167 | 0 | } |
168 | 0 | if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor)) |
169 | 0 | state->apply = 0; |
170 | 0 | if (state->check_index && is_not_gitdir) |
171 | 0 | return error(_("'%s' outside a repository"), "--index"); |
172 | 0 | if (state->cached) { |
173 | 0 | if (is_not_gitdir) |
174 | 0 | return error(_("'%s' outside a repository"), "--cached"); |
175 | 0 | state->check_index = 1; |
176 | 0 | } |
177 | 0 | if (state->ita_only && (state->check_index || is_not_gitdir)) |
178 | 0 | state->ita_only = 0; |
179 | 0 | if (state->check_index) |
180 | 0 | state->unsafe_paths = 0; |
181 | |
|
182 | 0 | if (state->apply_verbosity <= verbosity_silent) { |
183 | 0 | state->saved_error_routine = get_error_routine(); |
184 | 0 | state->saved_warn_routine = get_warn_routine(); |
185 | 0 | set_error_routine(mute_routine); |
186 | 0 | set_warn_routine(mute_routine); |
187 | 0 | } |
188 | |
|
189 | 0 | return 0; |
190 | 0 | } |
191 | | |
192 | | static void set_default_whitespace_mode(struct apply_state *state) |
193 | 0 | { |
194 | 0 | if (!state->whitespace_option && !apply_default_whitespace) |
195 | 0 | state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error); |
196 | 0 | } |
197 | | |
198 | | /* |
199 | | * This represents one "hunk" from a patch, starting with |
200 | | * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The |
201 | | * patch text is pointed at by patch, and its byte length |
202 | | * is stored in size. leading and trailing are the number |
203 | | * of context lines. |
204 | | */ |
205 | | struct fragment { |
206 | | unsigned long leading, trailing; |
207 | | unsigned long oldpos, oldlines; |
208 | | unsigned long newpos, newlines; |
209 | | /* |
210 | | * 'patch' is usually borrowed from buf in apply_patch(), |
211 | | * but some codepaths store an allocated buffer. |
212 | | */ |
213 | | const char *patch; |
214 | | unsigned free_patch:1, |
215 | | rejected:1; |
216 | | int size; |
217 | | int linenr; |
218 | | struct fragment *next; |
219 | | }; |
220 | | |
221 | | /* |
222 | | * When dealing with a binary patch, we reuse "leading" field |
223 | | * to store the type of the binary hunk, either deflated "delta" |
224 | | * or deflated "literal". |
225 | | */ |
226 | 0 | #define binary_patch_method leading |
227 | 0 | #define BINARY_DELTA_DEFLATED 1 |
228 | 0 | #define BINARY_LITERAL_DEFLATED 2 |
229 | | |
230 | | static void free_fragment_list(struct fragment *list) |
231 | 0 | { |
232 | 0 | while (list) { |
233 | 0 | struct fragment *next = list->next; |
234 | 0 | if (list->free_patch) |
235 | 0 | free((char *)list->patch); |
236 | 0 | free(list); |
237 | 0 | list = next; |
238 | 0 | } |
239 | 0 | } |
240 | | |
241 | | void release_patch(struct patch *patch) |
242 | 0 | { |
243 | 0 | free_fragment_list(patch->fragments); |
244 | 0 | free(patch->def_name); |
245 | 0 | free(patch->old_name); |
246 | 0 | free(patch->new_name); |
247 | 0 | free(patch->result); |
248 | 0 | } |
249 | | |
250 | | static void free_patch(struct patch *patch) |
251 | 0 | { |
252 | 0 | release_patch(patch); |
253 | 0 | free(patch); |
254 | 0 | } |
255 | | |
256 | | static void free_patch_list(struct patch *list) |
257 | 0 | { |
258 | 0 | while (list) { |
259 | 0 | struct patch *next = list->next; |
260 | 0 | free_patch(list); |
261 | 0 | list = next; |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | /* |
266 | | * A line in a file, len-bytes long (includes the terminating LF, |
267 | | * except for an incomplete line at the end if the file ends with |
268 | | * one), and its contents hashes to 'hash'. |
269 | | */ |
270 | | struct line { |
271 | | size_t len; |
272 | | unsigned hash : 24; |
273 | | unsigned flag : 8; |
274 | 0 | #define LINE_COMMON 1 |
275 | 0 | #define LINE_PATCHED 2 |
276 | | }; |
277 | | |
278 | | /* |
279 | | * This represents a "file", which is an array of "lines". |
280 | | */ |
281 | | struct image { |
282 | | struct strbuf buf; |
283 | | struct line *line; |
284 | | size_t line_nr, line_alloc; |
285 | | }; |
286 | 0 | #define IMAGE_INIT { \ |
287 | 0 | .buf = STRBUF_INIT, \ |
288 | 0 | } |
289 | | |
290 | | static void image_init(struct image *image) |
291 | 0 | { |
292 | 0 | struct image empty = IMAGE_INIT; |
293 | 0 | memcpy(image, &empty, sizeof(*image)); |
294 | 0 | } |
295 | | |
296 | | static void image_clear(struct image *image) |
297 | 0 | { |
298 | 0 | strbuf_release(&image->buf); |
299 | 0 | free(image->line); |
300 | 0 | image_init(image); |
301 | 0 | } |
302 | | |
303 | | static uint32_t hash_line(const char *cp, size_t len) |
304 | 0 | { |
305 | 0 | size_t i; |
306 | 0 | uint32_t h; |
307 | 0 | for (i = 0, h = 0; i < len; i++) { |
308 | 0 | if (!isspace(cp[i])) { |
309 | 0 | h = h * 3 + (cp[i] & 0xff); |
310 | 0 | } |
311 | 0 | } |
312 | 0 | return h; |
313 | 0 | } |
314 | | |
315 | | static void image_add_line(struct image *img, const char *bol, size_t len, unsigned flag) |
316 | 0 | { |
317 | 0 | ALLOC_GROW(img->line, img->line_nr + 1, img->line_alloc); |
318 | 0 | img->line[img->line_nr].len = len; |
319 | 0 | img->line[img->line_nr].hash = hash_line(bol, len); |
320 | 0 | img->line[img->line_nr].flag = flag; |
321 | 0 | img->line_nr++; |
322 | 0 | } |
323 | | |
324 | | /* |
325 | | * "buf" has the file contents to be patched (read from various sources). |
326 | | * attach it to "image" and add line-based index to it. |
327 | | * "image" now owns the "buf". |
328 | | */ |
329 | | static void image_prepare(struct image *image, char *buf, size_t len, |
330 | | int prepare_linetable) |
331 | 0 | { |
332 | 0 | const char *cp, *ep; |
333 | |
|
334 | 0 | image_clear(image); |
335 | 0 | strbuf_attach(&image->buf, buf, len, len + 1); |
336 | |
|
337 | 0 | if (!prepare_linetable) |
338 | 0 | return; |
339 | | |
340 | 0 | ep = image->buf.buf + image->buf.len; |
341 | 0 | cp = image->buf.buf; |
342 | 0 | while (cp < ep) { |
343 | 0 | const char *next; |
344 | 0 | for (next = cp; next < ep && *next != '\n'; next++) |
345 | 0 | ; |
346 | 0 | if (next < ep) |
347 | 0 | next++; |
348 | 0 | image_add_line(image, cp, next - cp, 0); |
349 | 0 | cp = next; |
350 | 0 | } |
351 | 0 | } |
352 | | |
353 | | static void image_remove_first_line(struct image *img) |
354 | 0 | { |
355 | 0 | strbuf_remove(&img->buf, 0, img->line[0].len); |
356 | 0 | img->line_nr--; |
357 | 0 | if (img->line_nr) |
358 | 0 | MOVE_ARRAY(img->line, img->line + 1, img->line_nr); |
359 | 0 | } |
360 | | |
361 | | static void image_remove_last_line(struct image *img) |
362 | 0 | { |
363 | 0 | size_t last_line_len = img->line[img->line_nr - 1].len; |
364 | 0 | strbuf_setlen(&img->buf, img->buf.len - last_line_len); |
365 | 0 | img->line_nr--; |
366 | 0 | } |
367 | | |
368 | | /* fmt must contain _one_ %s and no other substitution */ |
369 | | static void say_patch_name(FILE *output, const char *fmt, struct patch *patch) |
370 | 0 | { |
371 | 0 | struct strbuf sb = STRBUF_INIT; |
372 | |
|
373 | 0 | if (patch->old_name && patch->new_name && |
374 | 0 | strcmp(patch->old_name, patch->new_name)) { |
375 | 0 | quote_c_style(patch->old_name, &sb, NULL, 0); |
376 | 0 | strbuf_addstr(&sb, " => "); |
377 | 0 | quote_c_style(patch->new_name, &sb, NULL, 0); |
378 | 0 | } else { |
379 | 0 | const char *n = patch->new_name; |
380 | 0 | if (!n) |
381 | 0 | n = patch->old_name; |
382 | 0 | quote_c_style(n, &sb, NULL, 0); |
383 | 0 | } |
384 | 0 | fprintf(output, fmt, sb.buf); |
385 | 0 | fputc('\n', output); |
386 | 0 | strbuf_release(&sb); |
387 | 0 | } |
388 | | |
389 | 0 | #define SLOP (16) |
390 | | |
391 | | /* |
392 | | * apply.c isn't equipped to handle arbitrarily large patches, because |
393 | | * it intermingles `unsigned long` with `int` for the type used to store |
394 | | * buffer lengths. |
395 | | * |
396 | | * Only process patches that are just shy of 1 GiB large in order to |
397 | | * avoid any truncation or overflow issues. |
398 | | */ |
399 | 0 | #define MAX_APPLY_SIZE (1024UL * 1024 * 1023) |
400 | | |
401 | | static int read_patch_file(struct strbuf *sb, int fd) |
402 | 0 | { |
403 | 0 | if (strbuf_read(sb, fd, 0) < 0) |
404 | 0 | return error_errno(_("failed to read patch")); |
405 | 0 | else if (sb->len >= MAX_APPLY_SIZE) |
406 | 0 | return error(_("patch too large")); |
407 | | /* |
408 | | * Make sure that we have some slop in the buffer |
409 | | * so that we can do speculative "memcmp" etc, and |
410 | | * see to it that it is NUL-filled. |
411 | | */ |
412 | 0 | strbuf_grow(sb, SLOP); |
413 | 0 | memset(sb->buf + sb->len, 0, SLOP); |
414 | 0 | return 0; |
415 | 0 | } |
416 | | |
417 | | static unsigned long linelen(const char *buffer, unsigned long size) |
418 | 0 | { |
419 | 0 | unsigned long len = 0; |
420 | 0 | while (size--) { |
421 | 0 | len++; |
422 | 0 | if (*buffer++ == '\n') |
423 | 0 | break; |
424 | 0 | } |
425 | 0 | return len; |
426 | 0 | } |
427 | | |
428 | | static int is_dev_null(const char *str) |
429 | 0 | { |
430 | 0 | return skip_prefix(str, "/dev/null", &str) && isspace(*str); |
431 | 0 | } |
432 | | |
433 | 0 | #define TERM_SPACE 1 |
434 | 0 | #define TERM_TAB 2 |
435 | | |
436 | | static int name_terminate(int c, int terminate) |
437 | 0 | { |
438 | 0 | if (c == ' ' && !(terminate & TERM_SPACE)) |
439 | 0 | return 0; |
440 | 0 | if (c == '\t' && !(terminate & TERM_TAB)) |
441 | 0 | return 0; |
442 | | |
443 | 0 | return 1; |
444 | 0 | } |
445 | | |
446 | | /* remove double slashes to make --index work with such filenames */ |
447 | | static char *squash_slash(char *name) |
448 | 0 | { |
449 | 0 | int i = 0, j = 0; |
450 | |
|
451 | 0 | if (!name) |
452 | 0 | return NULL; |
453 | | |
454 | 0 | while (name[i]) { |
455 | 0 | if ((name[j++] = name[i++]) == '/') |
456 | 0 | while (name[i] == '/') |
457 | 0 | i++; |
458 | 0 | } |
459 | 0 | name[j] = '\0'; |
460 | 0 | return name; |
461 | 0 | } |
462 | | |
463 | | static char *find_name_gnu(struct strbuf *root, |
464 | | const char *line, |
465 | | int p_value) |
466 | 0 | { |
467 | 0 | struct strbuf name = STRBUF_INIT; |
468 | 0 | char *cp; |
469 | | |
470 | | /* |
471 | | * Proposed "new-style" GNU patch/diff format; see |
472 | | * https://lore.kernel.org/git/7vll0wvb2a.fsf@assigned-by-dhcp.cox.net/ |
473 | | */ |
474 | 0 | if (unquote_c_style(&name, line, NULL)) { |
475 | 0 | strbuf_release(&name); |
476 | 0 | return NULL; |
477 | 0 | } |
478 | | |
479 | 0 | for (cp = name.buf; p_value; p_value--) { |
480 | 0 | cp = strchr(cp, '/'); |
481 | 0 | if (!cp) { |
482 | 0 | strbuf_release(&name); |
483 | 0 | return NULL; |
484 | 0 | } |
485 | 0 | cp++; |
486 | 0 | } |
487 | | |
488 | 0 | strbuf_remove(&name, 0, cp - name.buf); |
489 | 0 | if (root->len) |
490 | 0 | strbuf_insert(&name, 0, root->buf, root->len); |
491 | 0 | return squash_slash(strbuf_detach(&name, NULL)); |
492 | 0 | } |
493 | | |
494 | | static size_t sane_tz_len(const char *line, size_t len) |
495 | 0 | { |
496 | 0 | const char *tz, *p; |
497 | |
|
498 | 0 | if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ') |
499 | 0 | return 0; |
500 | 0 | tz = line + len - strlen(" +0500"); |
501 | |
|
502 | 0 | if (tz[1] != '+' && tz[1] != '-') |
503 | 0 | return 0; |
504 | | |
505 | 0 | for (p = tz + 2; p != line + len; p++) |
506 | 0 | if (!isdigit(*p)) |
507 | 0 | return 0; |
508 | | |
509 | 0 | return line + len - tz; |
510 | 0 | } |
511 | | |
512 | | static size_t tz_with_colon_len(const char *line, size_t len) |
513 | 0 | { |
514 | 0 | const char *tz, *p; |
515 | |
|
516 | 0 | if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':') |
517 | 0 | return 0; |
518 | 0 | tz = line + len - strlen(" +08:00"); |
519 | |
|
520 | 0 | if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-')) |
521 | 0 | return 0; |
522 | 0 | p = tz + 2; |
523 | 0 | if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || |
524 | 0 | !isdigit(*p++) || !isdigit(*p++)) |
525 | 0 | return 0; |
526 | | |
527 | 0 | return line + len - tz; |
528 | 0 | } |
529 | | |
530 | | static size_t date_len(const char *line, size_t len) |
531 | 0 | { |
532 | 0 | const char *date, *p; |
533 | |
|
534 | 0 | if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-') |
535 | 0 | return 0; |
536 | 0 | p = date = line + len - strlen("72-02-05"); |
537 | |
|
538 | 0 | if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' || |
539 | 0 | !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' || |
540 | 0 | !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */ |
541 | 0 | return 0; |
542 | | |
543 | 0 | if (date - line >= strlen("19") && |
544 | 0 | isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */ |
545 | 0 | date -= strlen("19"); |
546 | |
|
547 | 0 | return line + len - date; |
548 | 0 | } |
549 | | |
550 | | static size_t short_time_len(const char *line, size_t len) |
551 | 0 | { |
552 | 0 | const char *time, *p; |
553 | |
|
554 | 0 | if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':') |
555 | 0 | return 0; |
556 | 0 | p = time = line + len - strlen(" 07:01:32"); |
557 | | |
558 | | /* Permit 1-digit hours? */ |
559 | 0 | if (*p++ != ' ' || |
560 | 0 | !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || |
561 | 0 | !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || |
562 | 0 | !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */ |
563 | 0 | return 0; |
564 | | |
565 | 0 | return line + len - time; |
566 | 0 | } |
567 | | |
568 | | static size_t fractional_time_len(const char *line, size_t len) |
569 | 0 | { |
570 | 0 | const char *p; |
571 | 0 | size_t n; |
572 | | |
573 | | /* Expected format: 19:41:17.620000023 */ |
574 | 0 | if (!len || !isdigit(line[len - 1])) |
575 | 0 | return 0; |
576 | 0 | p = line + len - 1; |
577 | | |
578 | | /* Fractional seconds. */ |
579 | 0 | while (p > line && isdigit(*p)) |
580 | 0 | p--; |
581 | 0 | if (*p != '.') |
582 | 0 | return 0; |
583 | | |
584 | | /* Hours, minutes, and whole seconds. */ |
585 | 0 | n = short_time_len(line, p - line); |
586 | 0 | if (!n) |
587 | 0 | return 0; |
588 | | |
589 | 0 | return line + len - p + n; |
590 | 0 | } |
591 | | |
592 | | static size_t trailing_spaces_len(const char *line, size_t len) |
593 | 0 | { |
594 | 0 | const char *p; |
595 | | |
596 | | /* Expected format: ' ' x (1 or more) */ |
597 | 0 | if (!len || line[len - 1] != ' ') |
598 | 0 | return 0; |
599 | | |
600 | 0 | p = line + len; |
601 | 0 | while (p != line) { |
602 | 0 | p--; |
603 | 0 | if (*p != ' ') |
604 | 0 | return line + len - (p + 1); |
605 | 0 | } |
606 | | |
607 | | /* All spaces! */ |
608 | 0 | return len; |
609 | 0 | } |
610 | | |
611 | | static size_t diff_timestamp_len(const char *line, size_t len) |
612 | 0 | { |
613 | 0 | const char *end = line + len; |
614 | 0 | size_t n; |
615 | | |
616 | | /* |
617 | | * Posix: 2010-07-05 19:41:17 |
618 | | * GNU: 2010-07-05 19:41:17.620000023 -0500 |
619 | | */ |
620 | |
|
621 | 0 | if (!isdigit(end[-1])) |
622 | 0 | return 0; |
623 | | |
624 | 0 | n = sane_tz_len(line, end - line); |
625 | 0 | if (!n) |
626 | 0 | n = tz_with_colon_len(line, end - line); |
627 | 0 | end -= n; |
628 | |
|
629 | 0 | n = short_time_len(line, end - line); |
630 | 0 | if (!n) |
631 | 0 | n = fractional_time_len(line, end - line); |
632 | 0 | end -= n; |
633 | |
|
634 | 0 | n = date_len(line, end - line); |
635 | 0 | if (!n) /* No date. Too bad. */ |
636 | 0 | return 0; |
637 | 0 | end -= n; |
638 | |
|
639 | 0 | if (end == line) /* No space before date. */ |
640 | 0 | return 0; |
641 | 0 | if (end[-1] == '\t') { /* Success! */ |
642 | 0 | end--; |
643 | 0 | return line + len - end; |
644 | 0 | } |
645 | 0 | if (end[-1] != ' ') /* No space before date. */ |
646 | 0 | return 0; |
647 | | |
648 | | /* Whitespace damage. */ |
649 | 0 | end -= trailing_spaces_len(line, end - line); |
650 | 0 | return line + len - end; |
651 | 0 | } |
652 | | |
653 | | static char *find_name_common(struct strbuf *root, |
654 | | const char *line, |
655 | | const char *def, |
656 | | int p_value, |
657 | | const char *end, |
658 | | int terminate) |
659 | 0 | { |
660 | 0 | int len; |
661 | 0 | const char *start = NULL; |
662 | |
|
663 | 0 | if (p_value == 0) |
664 | 0 | start = line; |
665 | 0 | while (line != end) { |
666 | 0 | char c = *line; |
667 | |
|
668 | 0 | if (!end && isspace(c)) { |
669 | 0 | if (c == '\n') |
670 | 0 | break; |
671 | 0 | if (name_terminate(c, terminate)) |
672 | 0 | break; |
673 | 0 | } |
674 | 0 | line++; |
675 | 0 | if (c == '/' && !--p_value) |
676 | 0 | start = line; |
677 | 0 | } |
678 | 0 | if (!start) |
679 | 0 | return squash_slash(xstrdup_or_null(def)); |
680 | 0 | len = line - start; |
681 | 0 | if (!len) |
682 | 0 | return squash_slash(xstrdup_or_null(def)); |
683 | | |
684 | | /* |
685 | | * Generally we prefer the shorter name, especially |
686 | | * if the other one is just a variation of that with |
687 | | * something else tacked on to the end (ie "file.orig" |
688 | | * or "file~"). |
689 | | */ |
690 | 0 | if (def) { |
691 | 0 | int deflen = strlen(def); |
692 | 0 | if (deflen < len && !strncmp(start, def, deflen)) |
693 | 0 | return squash_slash(xstrdup(def)); |
694 | 0 | } |
695 | | |
696 | 0 | if (root->len) { |
697 | 0 | char *ret = xstrfmt("%s%.*s", root->buf, len, start); |
698 | 0 | return squash_slash(ret); |
699 | 0 | } |
700 | | |
701 | 0 | return squash_slash(xmemdupz(start, len)); |
702 | 0 | } |
703 | | |
704 | | static char *find_name(struct strbuf *root, |
705 | | const char *line, |
706 | | char *def, |
707 | | int p_value, |
708 | | int terminate) |
709 | 0 | { |
710 | 0 | if (*line == '"') { |
711 | 0 | char *name = find_name_gnu(root, line, p_value); |
712 | 0 | if (name) |
713 | 0 | return name; |
714 | 0 | } |
715 | | |
716 | 0 | return find_name_common(root, line, def, p_value, NULL, terminate); |
717 | 0 | } |
718 | | |
719 | | static char *find_name_traditional(struct strbuf *root, |
720 | | const char *line, |
721 | | char *def, |
722 | | int p_value) |
723 | 0 | { |
724 | 0 | size_t len; |
725 | 0 | size_t date_len; |
726 | |
|
727 | 0 | if (*line == '"') { |
728 | 0 | char *name = find_name_gnu(root, line, p_value); |
729 | 0 | if (name) |
730 | 0 | return name; |
731 | 0 | } |
732 | | |
733 | 0 | len = strchrnul(line, '\n') - line; |
734 | 0 | date_len = diff_timestamp_len(line, len); |
735 | 0 | if (!date_len) |
736 | 0 | return find_name_common(root, line, def, p_value, NULL, TERM_TAB); |
737 | 0 | len -= date_len; |
738 | |
|
739 | 0 | return find_name_common(root, line, def, p_value, line + len, 0); |
740 | 0 | } |
741 | | |
742 | | /* |
743 | | * Given the string after "--- " or "+++ ", guess the appropriate |
744 | | * p_value for the given patch. |
745 | | */ |
746 | | static int guess_p_value(struct apply_state *state, const char *nameline) |
747 | 0 | { |
748 | 0 | char *name, *cp; |
749 | 0 | int val = -1; |
750 | |
|
751 | 0 | if (is_dev_null(nameline)) |
752 | 0 | return -1; |
753 | 0 | name = find_name_traditional(&state->root, nameline, NULL, 0); |
754 | 0 | if (!name) |
755 | 0 | return -1; |
756 | 0 | cp = strchr(name, '/'); |
757 | 0 | if (!cp) |
758 | 0 | val = 0; |
759 | 0 | else if (state->prefix) { |
760 | | /* |
761 | | * Does it begin with "a/$our-prefix" and such? Then this is |
762 | | * very likely to apply to our directory. |
763 | | */ |
764 | 0 | if (starts_with(name, state->prefix)) |
765 | 0 | val = count_slashes(state->prefix); |
766 | 0 | else { |
767 | 0 | cp++; |
768 | 0 | if (starts_with(cp, state->prefix)) |
769 | 0 | val = count_slashes(state->prefix) + 1; |
770 | 0 | } |
771 | 0 | } |
772 | 0 | free(name); |
773 | 0 | return val; |
774 | 0 | } |
775 | | |
776 | | /* |
777 | | * Does the ---/+++ line have the POSIX timestamp after the last HT? |
778 | | * GNU diff puts epoch there to signal a creation/deletion event. Is |
779 | | * this such a timestamp? |
780 | | */ |
781 | | static int has_epoch_timestamp(const char *nameline) |
782 | 0 | { |
783 | | /* |
784 | | * We are only interested in epoch timestamp; any non-zero |
785 | | * fraction cannot be one, hence "(\.0+)?" in the regexp below. |
786 | | * For the same reason, the date must be either 1969-12-31 or |
787 | | * 1970-01-01, and the seconds part must be "00". |
788 | | */ |
789 | 0 | const char stamp_regexp[] = |
790 | 0 | "^[0-2][0-9]:([0-5][0-9]):00(\\.0+)?" |
791 | 0 | " " |
792 | 0 | "([-+][0-2][0-9]:?[0-5][0-9])\n"; |
793 | 0 | const char *timestamp = NULL, *cp, *colon; |
794 | 0 | static regex_t *stamp; |
795 | 0 | regmatch_t m[10]; |
796 | 0 | int zoneoffset, epoch_hour, hour, minute; |
797 | 0 | int status; |
798 | |
|
799 | 0 | for (cp = nameline; *cp != '\n'; cp++) { |
800 | 0 | if (*cp == '\t') |
801 | 0 | timestamp = cp + 1; |
802 | 0 | } |
803 | 0 | if (!timestamp) |
804 | 0 | return 0; |
805 | | |
806 | | /* |
807 | | * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31 |
808 | | * (west of GMT) or 1970-01-01 (east of GMT) |
809 | | */ |
810 | 0 | if (skip_prefix(timestamp, "1969-12-31 ", ×tamp)) |
811 | 0 | epoch_hour = 24; |
812 | 0 | else if (skip_prefix(timestamp, "1970-01-01 ", ×tamp)) |
813 | 0 | epoch_hour = 0; |
814 | 0 | else |
815 | 0 | return 0; |
816 | | |
817 | 0 | if (!stamp) { |
818 | 0 | stamp = xmalloc(sizeof(*stamp)); |
819 | 0 | if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) { |
820 | 0 | warning(_("Cannot prepare timestamp regexp %s"), |
821 | 0 | stamp_regexp); |
822 | 0 | return 0; |
823 | 0 | } |
824 | 0 | } |
825 | | |
826 | 0 | status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0); |
827 | 0 | if (status) { |
828 | 0 | if (status != REG_NOMATCH) |
829 | 0 | warning(_("regexec returned %d for input: %s"), |
830 | 0 | status, timestamp); |
831 | 0 | return 0; |
832 | 0 | } |
833 | | |
834 | 0 | hour = strtol(timestamp, NULL, 10); |
835 | 0 | minute = strtol(timestamp + m[1].rm_so, NULL, 10); |
836 | |
|
837 | 0 | zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10); |
838 | 0 | if (*colon == ':') |
839 | 0 | zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10); |
840 | 0 | else |
841 | 0 | zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100); |
842 | 0 | if (timestamp[m[3].rm_so] == '-') |
843 | 0 | zoneoffset = -zoneoffset; |
844 | |
|
845 | 0 | return hour * 60 + minute - zoneoffset == epoch_hour * 60; |
846 | 0 | } |
847 | | |
848 | | /* |
849 | | * Get the name etc info from the ---/+++ lines of a traditional patch header |
850 | | * |
851 | | * FIXME! The end-of-filename heuristics are kind of screwy. For existing |
852 | | * files, we can happily check the index for a match, but for creating a |
853 | | * new file we should try to match whatever "patch" does. I have no idea. |
854 | | */ |
855 | | static int parse_traditional_patch(struct apply_state *state, |
856 | | const char *first, |
857 | | const char *second, |
858 | | struct patch *patch) |
859 | 0 | { |
860 | 0 | char *name; |
861 | |
|
862 | 0 | first += 4; /* skip "--- " */ |
863 | 0 | second += 4; /* skip "+++ " */ |
864 | 0 | if (!state->p_value_known) { |
865 | 0 | int p, q; |
866 | 0 | p = guess_p_value(state, first); |
867 | 0 | q = guess_p_value(state, second); |
868 | 0 | if (p < 0) p = q; |
869 | 0 | if (0 <= p && p == q) { |
870 | 0 | state->p_value = p; |
871 | 0 | state->p_value_known = 1; |
872 | 0 | } |
873 | 0 | } |
874 | 0 | if (is_dev_null(first)) { |
875 | 0 | patch->is_new = 1; |
876 | 0 | patch->is_delete = 0; |
877 | 0 | name = find_name_traditional(&state->root, second, NULL, state->p_value); |
878 | 0 | patch->new_name = name; |
879 | 0 | } else if (is_dev_null(second)) { |
880 | 0 | patch->is_new = 0; |
881 | 0 | patch->is_delete = 1; |
882 | 0 | name = find_name_traditional(&state->root, first, NULL, state->p_value); |
883 | 0 | patch->old_name = name; |
884 | 0 | } else { |
885 | 0 | char *first_name; |
886 | 0 | first_name = find_name_traditional(&state->root, first, NULL, state->p_value); |
887 | 0 | name = find_name_traditional(&state->root, second, first_name, state->p_value); |
888 | 0 | free(first_name); |
889 | 0 | if (has_epoch_timestamp(first)) { |
890 | 0 | patch->is_new = 1; |
891 | 0 | patch->is_delete = 0; |
892 | 0 | patch->new_name = name; |
893 | 0 | } else if (has_epoch_timestamp(second)) { |
894 | 0 | patch->is_new = 0; |
895 | 0 | patch->is_delete = 1; |
896 | 0 | patch->old_name = name; |
897 | 0 | } else { |
898 | 0 | patch->old_name = name; |
899 | 0 | patch->new_name = xstrdup_or_null(name); |
900 | 0 | } |
901 | 0 | } |
902 | 0 | if (!name) |
903 | 0 | return error(_("unable to find filename in patch at line %d"), state->linenr); |
904 | | |
905 | 0 | return 0; |
906 | 0 | } |
907 | | |
908 | | static int gitdiff_hdrend(struct gitdiff_data *state UNUSED, |
909 | | const char *line UNUSED, |
910 | | struct patch *patch UNUSED) |
911 | 0 | { |
912 | 0 | return 1; |
913 | 0 | } |
914 | | |
915 | | /* |
916 | | * We're anal about diff header consistency, to make |
917 | | * sure that we don't end up having strange ambiguous |
918 | | * patches floating around. |
919 | | * |
920 | | * As a result, gitdiff_{old|new}name() will check |
921 | | * their names against any previous information, just |
922 | | * to make sure.. |
923 | | */ |
924 | 0 | #define DIFF_OLD_NAME 0 |
925 | 0 | #define DIFF_NEW_NAME 1 |
926 | | |
927 | | static int gitdiff_verify_name(struct gitdiff_data *state, |
928 | | const char *line, |
929 | | int isnull, |
930 | | char **name, |
931 | | int side) |
932 | 0 | { |
933 | 0 | if (!*name && !isnull) { |
934 | 0 | *name = find_name(state->root, line, NULL, state->p_value, TERM_TAB); |
935 | 0 | return 0; |
936 | 0 | } |
937 | | |
938 | 0 | if (*name) { |
939 | 0 | char *another; |
940 | 0 | if (isnull) |
941 | 0 | return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), |
942 | 0 | *name, state->linenr); |
943 | 0 | another = find_name(state->root, line, NULL, state->p_value, TERM_TAB); |
944 | 0 | if (!another || strcmp(another, *name)) { |
945 | 0 | free(another); |
946 | 0 | return error((side == DIFF_NEW_NAME) ? |
947 | 0 | _("git apply: bad git-diff - inconsistent new filename on line %d") : |
948 | 0 | _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr); |
949 | 0 | } |
950 | 0 | free(another); |
951 | 0 | } else { |
952 | 0 | if (!is_dev_null(line)) |
953 | 0 | return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr); |
954 | 0 | } |
955 | | |
956 | 0 | return 0; |
957 | 0 | } |
958 | | |
959 | | static int gitdiff_oldname(struct gitdiff_data *state, |
960 | | const char *line, |
961 | | struct patch *patch) |
962 | 0 | { |
963 | 0 | return gitdiff_verify_name(state, line, |
964 | 0 | patch->is_new, &patch->old_name, |
965 | 0 | DIFF_OLD_NAME); |
966 | 0 | } |
967 | | |
968 | | static int gitdiff_newname(struct gitdiff_data *state, |
969 | | const char *line, |
970 | | struct patch *patch) |
971 | 0 | { |
972 | 0 | return gitdiff_verify_name(state, line, |
973 | 0 | patch->is_delete, &patch->new_name, |
974 | 0 | DIFF_NEW_NAME); |
975 | 0 | } |
976 | | |
977 | | static int parse_mode_line(const char *line, int linenr, unsigned int *mode) |
978 | 0 | { |
979 | 0 | char *end; |
980 | 0 | *mode = strtoul(line, &end, 8); |
981 | 0 | if (end == line || !isspace(*end)) |
982 | 0 | return error(_("invalid mode on line %d: %s"), linenr, line); |
983 | 0 | *mode = canon_mode(*mode); |
984 | 0 | return 0; |
985 | 0 | } |
986 | | |
987 | | static int gitdiff_oldmode(struct gitdiff_data *state, |
988 | | const char *line, |
989 | | struct patch *patch) |
990 | 0 | { |
991 | 0 | return parse_mode_line(line, state->linenr, &patch->old_mode); |
992 | 0 | } |
993 | | |
994 | | static int gitdiff_newmode(struct gitdiff_data *state, |
995 | | const char *line, |
996 | | struct patch *patch) |
997 | 0 | { |
998 | 0 | return parse_mode_line(line, state->linenr, &patch->new_mode); |
999 | 0 | } |
1000 | | |
1001 | | static int gitdiff_delete(struct gitdiff_data *state, |
1002 | | const char *line, |
1003 | | struct patch *patch) |
1004 | 0 | { |
1005 | 0 | patch->is_delete = 1; |
1006 | 0 | free(patch->old_name); |
1007 | 0 | patch->old_name = xstrdup_or_null(patch->def_name); |
1008 | 0 | return gitdiff_oldmode(state, line, patch); |
1009 | 0 | } |
1010 | | |
1011 | | static int gitdiff_newfile(struct gitdiff_data *state, |
1012 | | const char *line, |
1013 | | struct patch *patch) |
1014 | 0 | { |
1015 | 0 | patch->is_new = 1; |
1016 | 0 | free(patch->new_name); |
1017 | 0 | patch->new_name = xstrdup_or_null(patch->def_name); |
1018 | 0 | return gitdiff_newmode(state, line, patch); |
1019 | 0 | } |
1020 | | |
1021 | | static int gitdiff_copysrc(struct gitdiff_data *state, |
1022 | | const char *line, |
1023 | | struct patch *patch) |
1024 | 0 | { |
1025 | 0 | patch->is_copy = 1; |
1026 | 0 | free(patch->old_name); |
1027 | 0 | patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0); |
1028 | 0 | return 0; |
1029 | 0 | } |
1030 | | |
1031 | | static int gitdiff_copydst(struct gitdiff_data *state, |
1032 | | const char *line, |
1033 | | struct patch *patch) |
1034 | 0 | { |
1035 | 0 | patch->is_copy = 1; |
1036 | 0 | free(patch->new_name); |
1037 | 0 | patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0); |
1038 | 0 | return 0; |
1039 | 0 | } |
1040 | | |
1041 | | static int gitdiff_renamesrc(struct gitdiff_data *state, |
1042 | | const char *line, |
1043 | | struct patch *patch) |
1044 | 0 | { |
1045 | 0 | patch->is_rename = 1; |
1046 | 0 | free(patch->old_name); |
1047 | 0 | patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0); |
1048 | 0 | return 0; |
1049 | 0 | } |
1050 | | |
1051 | | static int gitdiff_renamedst(struct gitdiff_data *state, |
1052 | | const char *line, |
1053 | | struct patch *patch) |
1054 | 0 | { |
1055 | 0 | patch->is_rename = 1; |
1056 | 0 | free(patch->new_name); |
1057 | 0 | patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0); |
1058 | 0 | return 0; |
1059 | 0 | } |
1060 | | |
1061 | | static int gitdiff_similarity(struct gitdiff_data *state UNUSED, |
1062 | | const char *line, |
1063 | | struct patch *patch) |
1064 | 0 | { |
1065 | 0 | unsigned long val = strtoul(line, NULL, 10); |
1066 | 0 | if (val <= 100) |
1067 | 0 | patch->score = val; |
1068 | 0 | return 0; |
1069 | 0 | } |
1070 | | |
1071 | | static int gitdiff_dissimilarity(struct gitdiff_data *state UNUSED, |
1072 | | const char *line, |
1073 | | struct patch *patch) |
1074 | 0 | { |
1075 | 0 | unsigned long val = strtoul(line, NULL, 10); |
1076 | 0 | if (val <= 100) |
1077 | 0 | patch->score = val; |
1078 | 0 | return 0; |
1079 | 0 | } |
1080 | | |
1081 | | static int gitdiff_index(struct gitdiff_data *state, |
1082 | | const char *line, |
1083 | | struct patch *patch) |
1084 | 0 | { |
1085 | | /* |
1086 | | * index line is N hexadecimal, "..", N hexadecimal, |
1087 | | * and optional space with octal mode. |
1088 | | */ |
1089 | 0 | const char *ptr, *eol; |
1090 | 0 | int len; |
1091 | 0 | const unsigned hexsz = the_hash_algo->hexsz; |
1092 | |
|
1093 | 0 | ptr = strchr(line, '.'); |
1094 | 0 | if (!ptr || ptr[1] != '.' || hexsz < ptr - line) |
1095 | 0 | return 0; |
1096 | 0 | len = ptr - line; |
1097 | 0 | memcpy(patch->old_oid_prefix, line, len); |
1098 | 0 | patch->old_oid_prefix[len] = 0; |
1099 | |
|
1100 | 0 | line = ptr + 2; |
1101 | 0 | ptr = strchr(line, ' '); |
1102 | 0 | eol = strchrnul(line, '\n'); |
1103 | |
|
1104 | 0 | if (!ptr || eol < ptr) |
1105 | 0 | ptr = eol; |
1106 | 0 | len = ptr - line; |
1107 | |
|
1108 | 0 | if (hexsz < len) |
1109 | 0 | return 0; |
1110 | 0 | memcpy(patch->new_oid_prefix, line, len); |
1111 | 0 | patch->new_oid_prefix[len] = 0; |
1112 | 0 | if (*ptr == ' ') |
1113 | 0 | return gitdiff_oldmode(state, ptr + 1, patch); |
1114 | 0 | return 0; |
1115 | 0 | } |
1116 | | |
1117 | | /* |
1118 | | * This is normal for a diff that doesn't change anything: we'll fall through |
1119 | | * into the next diff. Tell the parser to break out. |
1120 | | */ |
1121 | | static int gitdiff_unrecognized(struct gitdiff_data *state UNUSED, |
1122 | | const char *line UNUSED, |
1123 | | struct patch *patch UNUSED) |
1124 | 0 | { |
1125 | 0 | return 1; |
1126 | 0 | } |
1127 | | |
1128 | | /* |
1129 | | * Skip p_value leading components from "line"; as we do not accept |
1130 | | * absolute paths, return NULL in that case. |
1131 | | */ |
1132 | | static const char *skip_tree_prefix(int p_value, |
1133 | | const char *line, |
1134 | | int llen) |
1135 | 0 | { |
1136 | 0 | int nslash; |
1137 | 0 | int i; |
1138 | |
|
1139 | 0 | if (!p_value) |
1140 | 0 | return (llen && line[0] == '/') ? NULL : line; |
1141 | | |
1142 | 0 | nslash = p_value; |
1143 | 0 | for (i = 0; i < llen; i++) { |
1144 | 0 | int ch = line[i]; |
1145 | 0 | if (ch == '/' && --nslash <= 0) |
1146 | 0 | return (i == 0) ? NULL : &line[i + 1]; |
1147 | 0 | } |
1148 | 0 | return NULL; |
1149 | 0 | } |
1150 | | |
1151 | | /* |
1152 | | * This is to extract the same name that appears on "diff --git" |
1153 | | * line. We do not find and return anything if it is a rename |
1154 | | * patch, and it is OK because we will find the name elsewhere. |
1155 | | * We need to reliably find name only when it is mode-change only, |
1156 | | * creation or deletion of an empty file. In any of these cases, |
1157 | | * both sides are the same name under a/ and b/ respectively. |
1158 | | */ |
1159 | | static char *git_header_name(int p_value, |
1160 | | const char *line, |
1161 | | int llen) |
1162 | 0 | { |
1163 | 0 | const char *name; |
1164 | 0 | const char *second = NULL; |
1165 | 0 | size_t len, line_len; |
1166 | |
|
1167 | 0 | line += strlen("diff --git "); |
1168 | 0 | llen -= strlen("diff --git "); |
1169 | |
|
1170 | 0 | if (*line == '"') { |
1171 | 0 | const char *cp; |
1172 | 0 | struct strbuf first = STRBUF_INIT; |
1173 | 0 | struct strbuf sp = STRBUF_INIT; |
1174 | |
|
1175 | 0 | if (unquote_c_style(&first, line, &second)) |
1176 | 0 | goto free_and_fail1; |
1177 | | |
1178 | | /* strip the a/b prefix including trailing slash */ |
1179 | 0 | cp = skip_tree_prefix(p_value, first.buf, first.len); |
1180 | 0 | if (!cp) |
1181 | 0 | goto free_and_fail1; |
1182 | 0 | strbuf_remove(&first, 0, cp - first.buf); |
1183 | | |
1184 | | /* |
1185 | | * second points at one past closing dq of name. |
1186 | | * find the second name. |
1187 | | */ |
1188 | 0 | while ((second < line + llen) && isspace(*second)) |
1189 | 0 | second++; |
1190 | |
|
1191 | 0 | if (line + llen <= second) |
1192 | 0 | goto free_and_fail1; |
1193 | 0 | if (*second == '"') { |
1194 | 0 | if (unquote_c_style(&sp, second, NULL)) |
1195 | 0 | goto free_and_fail1; |
1196 | 0 | cp = skip_tree_prefix(p_value, sp.buf, sp.len); |
1197 | 0 | if (!cp) |
1198 | 0 | goto free_and_fail1; |
1199 | | /* They must match, otherwise ignore */ |
1200 | 0 | if (strcmp(cp, first.buf)) |
1201 | 0 | goto free_and_fail1; |
1202 | 0 | strbuf_release(&sp); |
1203 | 0 | return strbuf_detach(&first, NULL); |
1204 | 0 | } |
1205 | | |
1206 | | /* unquoted second */ |
1207 | 0 | cp = skip_tree_prefix(p_value, second, line + llen - second); |
1208 | 0 | if (!cp) |
1209 | 0 | goto free_and_fail1; |
1210 | 0 | if (line + llen - cp != first.len || |
1211 | 0 | memcmp(first.buf, cp, first.len)) |
1212 | 0 | goto free_and_fail1; |
1213 | 0 | return strbuf_detach(&first, NULL); |
1214 | | |
1215 | 0 | free_and_fail1: |
1216 | 0 | strbuf_release(&first); |
1217 | 0 | strbuf_release(&sp); |
1218 | 0 | return NULL; |
1219 | 0 | } |
1220 | | |
1221 | | /* unquoted first name */ |
1222 | 0 | name = skip_tree_prefix(p_value, line, llen); |
1223 | 0 | if (!name) |
1224 | 0 | return NULL; |
1225 | | |
1226 | | /* |
1227 | | * since the first name is unquoted, a dq if exists must be |
1228 | | * the beginning of the second name. |
1229 | | */ |
1230 | 0 | for (second = name; second < line + llen; second++) { |
1231 | 0 | if (*second == '"') { |
1232 | 0 | struct strbuf sp = STRBUF_INIT; |
1233 | 0 | const char *np; |
1234 | |
|
1235 | 0 | if (unquote_c_style(&sp, second, NULL)) |
1236 | 0 | goto free_and_fail2; |
1237 | | |
1238 | 0 | np = skip_tree_prefix(p_value, sp.buf, sp.len); |
1239 | 0 | if (!np) |
1240 | 0 | goto free_and_fail2; |
1241 | | |
1242 | 0 | len = sp.buf + sp.len - np; |
1243 | 0 | if (len < second - name && |
1244 | 0 | !strncmp(np, name, len) && |
1245 | 0 | isspace(name[len])) { |
1246 | | /* Good */ |
1247 | 0 | strbuf_remove(&sp, 0, np - sp.buf); |
1248 | 0 | return strbuf_detach(&sp, NULL); |
1249 | 0 | } |
1250 | | |
1251 | 0 | free_and_fail2: |
1252 | 0 | strbuf_release(&sp); |
1253 | 0 | return NULL; |
1254 | 0 | } |
1255 | 0 | } |
1256 | | |
1257 | | /* |
1258 | | * Accept a name only if it shows up twice, exactly the same |
1259 | | * form. |
1260 | | */ |
1261 | 0 | second = strchr(name, '\n'); |
1262 | 0 | if (!second) |
1263 | 0 | return NULL; |
1264 | 0 | line_len = second - name; |
1265 | 0 | for (len = 0 ; ; len++) { |
1266 | 0 | switch (name[len]) { |
1267 | 0 | default: |
1268 | 0 | continue; |
1269 | 0 | case '\n': |
1270 | 0 | return NULL; |
1271 | 0 | case '\t': case ' ': |
1272 | | /* |
1273 | | * Is this the separator between the preimage |
1274 | | * and the postimage pathname? Again, we are |
1275 | | * only interested in the case where there is |
1276 | | * no rename, as this is only to set def_name |
1277 | | * and a rename patch has the names elsewhere |
1278 | | * in an unambiguous form. |
1279 | | */ |
1280 | 0 | if (!name[len + 1]) |
1281 | 0 | return NULL; /* no postimage name */ |
1282 | 0 | second = skip_tree_prefix(p_value, name + len + 1, |
1283 | 0 | line_len - (len + 1)); |
1284 | | /* |
1285 | | * If we are at the SP at the end of a directory, |
1286 | | * skip_tree_prefix() may return NULL as that makes |
1287 | | * it appears as if we have an absolute path. |
1288 | | * Keep going to find another SP. |
1289 | | */ |
1290 | 0 | if (!second) |
1291 | 0 | continue; |
1292 | | |
1293 | | /* |
1294 | | * Does len bytes starting at "name" and "second" |
1295 | | * (that are separated by one HT or SP we just |
1296 | | * found) exactly match? |
1297 | | */ |
1298 | 0 | if (second[len] == '\n' && !strncmp(name, second, len)) |
1299 | 0 | return xmemdupz(name, len); |
1300 | 0 | } |
1301 | 0 | } |
1302 | 0 | } |
1303 | | |
1304 | | static int check_header_line(int linenr, struct patch *patch) |
1305 | 0 | { |
1306 | 0 | int extensions = (patch->is_delete == 1) + (patch->is_new == 1) + |
1307 | 0 | (patch->is_rename == 1) + (patch->is_copy == 1); |
1308 | 0 | if (extensions > 1) |
1309 | 0 | return error(_("inconsistent header lines %d and %d"), |
1310 | 0 | patch->extension_linenr, linenr); |
1311 | 0 | if (extensions && !patch->extension_linenr) |
1312 | 0 | patch->extension_linenr = linenr; |
1313 | 0 | return 0; |
1314 | 0 | } |
1315 | | |
1316 | | int parse_git_diff_header(struct strbuf *root, |
1317 | | int *linenr, |
1318 | | int p_value, |
1319 | | const char *line, |
1320 | | int len, |
1321 | | unsigned int size, |
1322 | | struct patch *patch) |
1323 | 0 | { |
1324 | 0 | unsigned long offset; |
1325 | 0 | struct gitdiff_data parse_hdr_state; |
1326 | | |
1327 | | /* A git diff has explicit new/delete information, so we don't guess */ |
1328 | 0 | patch->is_new = 0; |
1329 | 0 | patch->is_delete = 0; |
1330 | | |
1331 | | /* |
1332 | | * Some things may not have the old name in the |
1333 | | * rest of the headers anywhere (pure mode changes, |
1334 | | * or removing or adding empty files), so we get |
1335 | | * the default name from the header. |
1336 | | */ |
1337 | 0 | patch->def_name = git_header_name(p_value, line, len); |
1338 | 0 | if (patch->def_name && root->len) { |
1339 | 0 | char *s = xstrfmt("%s%s", root->buf, patch->def_name); |
1340 | 0 | free(patch->def_name); |
1341 | 0 | patch->def_name = s; |
1342 | 0 | } |
1343 | |
|
1344 | 0 | line += len; |
1345 | 0 | size -= len; |
1346 | 0 | (*linenr)++; |
1347 | 0 | parse_hdr_state.root = root; |
1348 | 0 | parse_hdr_state.linenr = *linenr; |
1349 | 0 | parse_hdr_state.p_value = p_value; |
1350 | |
|
1351 | 0 | for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) { |
1352 | 0 | static const struct opentry { |
1353 | 0 | const char *str; |
1354 | 0 | int (*fn)(struct gitdiff_data *, const char *, struct patch *); |
1355 | 0 | } optable[] = { |
1356 | 0 | { "@@ -", gitdiff_hdrend }, |
1357 | 0 | { "--- ", gitdiff_oldname }, |
1358 | 0 | { "+++ ", gitdiff_newname }, |
1359 | 0 | { "old mode ", gitdiff_oldmode }, |
1360 | 0 | { "new mode ", gitdiff_newmode }, |
1361 | 0 | { "deleted file mode ", gitdiff_delete }, |
1362 | 0 | { "new file mode ", gitdiff_newfile }, |
1363 | 0 | { "copy from ", gitdiff_copysrc }, |
1364 | 0 | { "copy to ", gitdiff_copydst }, |
1365 | 0 | { "rename old ", gitdiff_renamesrc }, |
1366 | 0 | { "rename new ", gitdiff_renamedst }, |
1367 | 0 | { "rename from ", gitdiff_renamesrc }, |
1368 | 0 | { "rename to ", gitdiff_renamedst }, |
1369 | 0 | { "similarity index ", gitdiff_similarity }, |
1370 | 0 | { "dissimilarity index ", gitdiff_dissimilarity }, |
1371 | 0 | { "index ", gitdiff_index }, |
1372 | 0 | { "", gitdiff_unrecognized }, |
1373 | 0 | }; |
1374 | 0 | int i; |
1375 | |
|
1376 | 0 | len = linelen(line, size); |
1377 | 0 | if (!len || line[len-1] != '\n') |
1378 | 0 | break; |
1379 | 0 | for (i = 0; i < ARRAY_SIZE(optable); i++) { |
1380 | 0 | const struct opentry *p = optable + i; |
1381 | 0 | int oplen = strlen(p->str); |
1382 | 0 | int res; |
1383 | 0 | if (len < oplen || memcmp(p->str, line, oplen)) |
1384 | 0 | continue; |
1385 | 0 | res = p->fn(&parse_hdr_state, line + oplen, patch); |
1386 | 0 | if (res < 0) |
1387 | 0 | return -1; |
1388 | 0 | if (check_header_line(*linenr, patch)) |
1389 | 0 | return -1; |
1390 | 0 | if (res > 0) |
1391 | 0 | goto done; |
1392 | 0 | break; |
1393 | 0 | } |
1394 | 0 | } |
1395 | | |
1396 | 0 | done: |
1397 | 0 | if (!patch->old_name && !patch->new_name) { |
1398 | 0 | if (!patch->def_name) { |
1399 | 0 | error(Q_("git diff header lacks filename information when removing " |
1400 | 0 | "%d leading pathname component (line %d)", |
1401 | 0 | "git diff header lacks filename information when removing " |
1402 | 0 | "%d leading pathname components (line %d)", |
1403 | 0 | parse_hdr_state.p_value), |
1404 | 0 | parse_hdr_state.p_value, *linenr); |
1405 | 0 | return -128; |
1406 | 0 | } |
1407 | 0 | patch->old_name = xstrdup(patch->def_name); |
1408 | 0 | patch->new_name = xstrdup(patch->def_name); |
1409 | 0 | } |
1410 | 0 | if ((!patch->new_name && !patch->is_delete) || |
1411 | 0 | (!patch->old_name && !patch->is_new)) { |
1412 | 0 | error(_("git diff header lacks filename information " |
1413 | 0 | "(line %d)"), *linenr); |
1414 | 0 | return -128; |
1415 | 0 | } |
1416 | 0 | patch->is_toplevel_relative = 1; |
1417 | 0 | return offset; |
1418 | 0 | } |
1419 | | |
1420 | | static int parse_num(const char *line, unsigned long *p) |
1421 | 0 | { |
1422 | 0 | char *ptr; |
1423 | |
|
1424 | 0 | if (!isdigit(*line)) |
1425 | 0 | return 0; |
1426 | 0 | errno = 0; |
1427 | 0 | *p = strtoul(line, &ptr, 10); |
1428 | 0 | if (errno) |
1429 | 0 | return 0; |
1430 | 0 | return ptr - line; |
1431 | 0 | } |
1432 | | |
1433 | | static int parse_range(const char *line, int len, int offset, const char *expect, |
1434 | | unsigned long *p1, unsigned long *p2) |
1435 | 0 | { |
1436 | 0 | int digits, ex; |
1437 | |
|
1438 | 0 | if (offset < 0 || offset >= len) |
1439 | 0 | return -1; |
1440 | 0 | line += offset; |
1441 | 0 | len -= offset; |
1442 | |
|
1443 | 0 | digits = parse_num(line, p1); |
1444 | 0 | if (!digits) |
1445 | 0 | return -1; |
1446 | | |
1447 | 0 | offset += digits; |
1448 | 0 | line += digits; |
1449 | 0 | len -= digits; |
1450 | |
|
1451 | 0 | *p2 = 1; |
1452 | 0 | if (*line == ',') { |
1453 | 0 | digits = parse_num(line+1, p2); |
1454 | 0 | if (!digits) |
1455 | 0 | return -1; |
1456 | | |
1457 | 0 | offset += digits+1; |
1458 | 0 | line += digits+1; |
1459 | 0 | len -= digits+1; |
1460 | 0 | } |
1461 | | |
1462 | 0 | ex = strlen(expect); |
1463 | 0 | if (ex > len) |
1464 | 0 | return -1; |
1465 | 0 | if (memcmp(line, expect, ex)) |
1466 | 0 | return -1; |
1467 | | |
1468 | 0 | return offset + ex; |
1469 | 0 | } |
1470 | | |
1471 | | static void recount_diff(const char *line, int size, struct fragment *fragment) |
1472 | 0 | { |
1473 | 0 | int oldlines = 0, newlines = 0, ret = 0; |
1474 | |
|
1475 | 0 | if (size < 1) { |
1476 | 0 | warning("recount: ignore empty hunk"); |
1477 | 0 | return; |
1478 | 0 | } |
1479 | | |
1480 | 0 | for (;;) { |
1481 | 0 | int len = linelen(line, size); |
1482 | 0 | size -= len; |
1483 | 0 | line += len; |
1484 | |
|
1485 | 0 | if (size < 1) |
1486 | 0 | break; |
1487 | | |
1488 | 0 | switch (*line) { |
1489 | 0 | case ' ': case '\n': |
1490 | 0 | newlines++; |
1491 | | /* fall through */ |
1492 | 0 | case '-': |
1493 | 0 | oldlines++; |
1494 | 0 | continue; |
1495 | 0 | case '+': |
1496 | 0 | newlines++; |
1497 | 0 | continue; |
1498 | 0 | case '\\': |
1499 | 0 | continue; |
1500 | 0 | case '@': |
1501 | 0 | ret = size < 3 || !starts_with(line, "@@ "); |
1502 | 0 | break; |
1503 | 0 | case 'd': |
1504 | 0 | ret = size < 5 || !starts_with(line, "diff "); |
1505 | 0 | break; |
1506 | 0 | default: |
1507 | 0 | ret = -1; |
1508 | 0 | break; |
1509 | 0 | } |
1510 | 0 | if (ret) { |
1511 | 0 | warning(_("recount: unexpected line: %.*s"), |
1512 | 0 | (int)linelen(line, size), line); |
1513 | 0 | return; |
1514 | 0 | } |
1515 | 0 | break; |
1516 | 0 | } |
1517 | 0 | fragment->oldlines = oldlines; |
1518 | 0 | fragment->newlines = newlines; |
1519 | 0 | } |
1520 | | |
1521 | | /* |
1522 | | * Parse a unified diff fragment header of the |
1523 | | * form "@@ -a,b +c,d @@" |
1524 | | */ |
1525 | | static int parse_fragment_header(const char *line, int len, struct fragment *fragment) |
1526 | 0 | { |
1527 | 0 | int offset; |
1528 | |
|
1529 | 0 | if (!len || line[len-1] != '\n') |
1530 | 0 | return -1; |
1531 | | |
1532 | | /* Figure out the number of lines in a fragment */ |
1533 | 0 | offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); |
1534 | 0 | offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); |
1535 | |
|
1536 | 0 | return offset; |
1537 | 0 | } |
1538 | | |
1539 | | /* |
1540 | | * Find file diff header |
1541 | | * |
1542 | | * Returns: |
1543 | | * -1 if no header was found |
1544 | | * -128 in case of error |
1545 | | * the size of the header in bytes (called "offset") otherwise |
1546 | | */ |
1547 | | static int find_header(struct apply_state *state, |
1548 | | const char *line, |
1549 | | unsigned long size, |
1550 | | int *hdrsize, |
1551 | | struct patch *patch) |
1552 | 0 | { |
1553 | 0 | unsigned long offset, len; |
1554 | |
|
1555 | 0 | patch->is_toplevel_relative = 0; |
1556 | 0 | patch->is_rename = patch->is_copy = 0; |
1557 | 0 | patch->is_new = patch->is_delete = -1; |
1558 | 0 | patch->old_mode = patch->new_mode = 0; |
1559 | 0 | patch->old_name = patch->new_name = NULL; |
1560 | 0 | for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) { |
1561 | 0 | unsigned long nextlen; |
1562 | |
|
1563 | 0 | len = linelen(line, size); |
1564 | 0 | if (!len) |
1565 | 0 | break; |
1566 | | |
1567 | | /* Testing this early allows us to take a few shortcuts.. */ |
1568 | 0 | if (len < 6) |
1569 | 0 | continue; |
1570 | | |
1571 | | /* |
1572 | | * Make sure we don't find any unconnected patch fragments. |
1573 | | * That's a sign that we didn't find a header, and that a |
1574 | | * patch has become corrupted/broken up. |
1575 | | */ |
1576 | 0 | if (!memcmp("@@ -", line, 4)) { |
1577 | 0 | struct fragment dummy; |
1578 | 0 | if (parse_fragment_header(line, len, &dummy) < 0) |
1579 | 0 | continue; |
1580 | 0 | error(_("patch fragment without header at line %d: %.*s"), |
1581 | 0 | state->linenr, (int)len-1, line); |
1582 | 0 | return -128; |
1583 | 0 | } |
1584 | | |
1585 | 0 | if (size < len + 6) |
1586 | 0 | break; |
1587 | | |
1588 | | /* |
1589 | | * Git patch? It might not have a real patch, just a rename |
1590 | | * or mode change, so we handle that specially |
1591 | | */ |
1592 | 0 | if (!memcmp("diff --git ", line, 11)) { |
1593 | 0 | int git_hdr_len = parse_git_diff_header(&state->root, &state->linenr, |
1594 | 0 | state->p_value, line, len, |
1595 | 0 | size, patch); |
1596 | 0 | if (git_hdr_len < 0) |
1597 | 0 | return -128; |
1598 | 0 | if (git_hdr_len <= len) |
1599 | 0 | continue; |
1600 | 0 | *hdrsize = git_hdr_len; |
1601 | 0 | return offset; |
1602 | 0 | } |
1603 | | |
1604 | | /* --- followed by +++ ? */ |
1605 | 0 | if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4)) |
1606 | 0 | continue; |
1607 | | |
1608 | | /* |
1609 | | * We only accept unified patches, so we want it to |
1610 | | * at least have "@@ -a,b +c,d @@\n", which is 14 chars |
1611 | | * minimum ("@@ -0,0 +1 @@\n" is the shortest). |
1612 | | */ |
1613 | 0 | nextlen = linelen(line + len, size - len); |
1614 | 0 | if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4)) |
1615 | 0 | continue; |
1616 | | |
1617 | | /* Ok, we'll consider it a patch */ |
1618 | 0 | if (parse_traditional_patch(state, line, line+len, patch)) |
1619 | 0 | return -128; |
1620 | 0 | *hdrsize = len + nextlen; |
1621 | 0 | state->linenr += 2; |
1622 | 0 | return offset; |
1623 | 0 | } |
1624 | 0 | return -1; |
1625 | 0 | } |
1626 | | |
1627 | | static void record_ws_error(struct apply_state *state, |
1628 | | unsigned result, |
1629 | | const char *line, |
1630 | | int len, |
1631 | | int linenr) |
1632 | 0 | { |
1633 | 0 | char *err; |
1634 | |
|
1635 | 0 | if (!result) |
1636 | 0 | return; |
1637 | | |
1638 | 0 | state->whitespace_error++; |
1639 | 0 | if (state->squelch_whitespace_errors && |
1640 | 0 | state->squelch_whitespace_errors < state->whitespace_error) |
1641 | 0 | return; |
1642 | | |
1643 | | /* |
1644 | | * line[len] for an incomplete line points at the "\n" at the end |
1645 | | * of patch input line, so "%.*s" would drop the last letter on line; |
1646 | | * compensate for it. |
1647 | | */ |
1648 | 0 | if (result & WS_INCOMPLETE_LINE) |
1649 | 0 | len++; |
1650 | |
|
1651 | 0 | err = whitespace_error_string(result); |
1652 | 0 | if (state->apply_verbosity > verbosity_silent) |
1653 | 0 | fprintf(stderr, "%s:%d: %s.\n%.*s\n", |
1654 | 0 | state->patch_input_file, linenr, err, len, line); |
1655 | 0 | free(err); |
1656 | 0 | } |
1657 | | |
1658 | | static void check_whitespace(struct apply_state *state, |
1659 | | const char *line, |
1660 | | int len, |
1661 | | unsigned ws_rule) |
1662 | 0 | { |
1663 | 0 | unsigned result = ws_check(line + 1, len - 1, ws_rule); |
1664 | |
|
1665 | 0 | record_ws_error(state, result, line + 1, len - 2, state->linenr); |
1666 | 0 | } |
1667 | | |
1668 | | /* |
1669 | | * Check if the patch has context lines with CRLF or |
1670 | | * the patch wants to remove lines with CRLF. |
1671 | | */ |
1672 | | static void check_old_for_crlf(struct patch *patch, const char *line, int len) |
1673 | 0 | { |
1674 | 0 | if (len >= 2 && line[len-1] == '\n' && line[len-2] == '\r') { |
1675 | 0 | patch->ws_rule |= WS_CR_AT_EOL; |
1676 | 0 | patch->crlf_in_old = 1; |
1677 | 0 | } |
1678 | 0 | } |
1679 | | |
1680 | | |
1681 | | /* |
1682 | | * Just saw a single line in a fragment. If it is a part of this hunk |
1683 | | * that is a context " ", an added "+", or a removed "-" line, it may |
1684 | | * be followed by "\\ No newline..." to signal that the last "\n" on |
1685 | | * this line needs to be dropped. Depending on locale settings when |
1686 | | * the patch was produced we don't know what this line would exactly |
1687 | | * say. The only thing we do know is that it begins with "\ ". |
1688 | | * Checking for 12 is just for sanity check; "\ No newline..." would |
1689 | | * be at least that long in any l10n. |
1690 | | * |
1691 | | * Return 0 if the line we saw is not followed by "\ No newline...", |
1692 | | * or length of that line. The caller will use it to skip over the |
1693 | | * "\ No newline..." line. |
1694 | | */ |
1695 | | static int adjust_incomplete(const char *line, int len, |
1696 | | unsigned long size) |
1697 | 0 | { |
1698 | 0 | int nextlen; |
1699 | |
|
1700 | 0 | if (*line != '\n' && *line != ' ' && *line != '+' && *line != '-') |
1701 | 0 | return 0; |
1702 | 0 | if (size - len < 12 || memcmp(line + len, "\\ ", 2)) |
1703 | 0 | return 0; |
1704 | 0 | nextlen = linelen(line + len, size - len); |
1705 | 0 | if (nextlen < 12) |
1706 | 0 | return 0; |
1707 | 0 | return nextlen; |
1708 | 0 | } |
1709 | | |
1710 | | /* |
1711 | | * Parse a unified diff. Note that this really needs to parse each |
1712 | | * fragment separately, since the only way to know the difference |
1713 | | * between a "---" that is part of a patch, and a "---" that starts |
1714 | | * the next patch is to look at the line counts.. |
1715 | | */ |
1716 | | static int parse_fragment(struct apply_state *state, |
1717 | | const char *line, |
1718 | | unsigned long size, |
1719 | | struct patch *patch, |
1720 | | struct fragment *fragment) |
1721 | 0 | { |
1722 | 0 | int added, deleted; |
1723 | 0 | int len = linelen(line, size), offset; |
1724 | 0 | int skip_len = 0; |
1725 | 0 | unsigned long oldlines, newlines; |
1726 | 0 | unsigned long leading, trailing; |
1727 | |
|
1728 | 0 | offset = parse_fragment_header(line, len, fragment); |
1729 | 0 | if (offset < 0) |
1730 | 0 | return -1; |
1731 | 0 | if (offset > 0 && patch->recount) |
1732 | 0 | recount_diff(line + offset, size - offset, fragment); |
1733 | 0 | oldlines = fragment->oldlines; |
1734 | 0 | newlines = fragment->newlines; |
1735 | 0 | leading = 0; |
1736 | 0 | trailing = 0; |
1737 | | |
1738 | | /* Parse the thing.. */ |
1739 | 0 | line += len; |
1740 | 0 | size -= len; |
1741 | 0 | state->linenr++; |
1742 | 0 | added = deleted = 0; |
1743 | 0 | for (offset = len; |
1744 | 0 | 0 < size; |
1745 | 0 | offset += len, size -= len, line += len, state->linenr++) { |
1746 | 0 | if (!oldlines && !newlines) |
1747 | 0 | break; |
1748 | 0 | len = linelen(line, size); |
1749 | 0 | if (!len || line[len-1] != '\n') |
1750 | 0 | return -1; |
1751 | | |
1752 | | /* |
1753 | | * For an incomplete line, skip_len counts the bytes |
1754 | | * on "\\ No newline..." marker line that comes next |
1755 | | * to the current line. |
1756 | | * |
1757 | | * Reduce "len" to drop the newline at the end of |
1758 | | * line[], but add one to "skip_len", which will be |
1759 | | * added back to "len" for the next iteration, to |
1760 | | * compensate. |
1761 | | */ |
1762 | 0 | skip_len = adjust_incomplete(line, len, size); |
1763 | 0 | if (skip_len) { |
1764 | 0 | len--; |
1765 | 0 | skip_len++; |
1766 | 0 | } |
1767 | 0 | switch (*line) { |
1768 | 0 | default: |
1769 | 0 | return -1; |
1770 | 0 | case '\n': /* newer GNU diff, an empty context line */ |
1771 | 0 | case ' ': |
1772 | 0 | oldlines--; |
1773 | 0 | newlines--; |
1774 | 0 | if (!deleted && !added) |
1775 | 0 | leading++; |
1776 | 0 | trailing++; |
1777 | 0 | check_old_for_crlf(patch, line, len); |
1778 | 0 | if (!state->apply_in_reverse && |
1779 | 0 | state->ws_error_action == correct_ws_error) |
1780 | 0 | check_whitespace(state, line, len, patch->ws_rule); |
1781 | 0 | break; |
1782 | 0 | case '-': |
1783 | 0 | if (!state->apply_in_reverse) |
1784 | 0 | check_old_for_crlf(patch, line, len); |
1785 | 0 | if (state->apply_in_reverse && |
1786 | 0 | state->ws_error_action != nowarn_ws_error) |
1787 | 0 | check_whitespace(state, line, len, patch->ws_rule); |
1788 | 0 | deleted++; |
1789 | 0 | oldlines--; |
1790 | 0 | trailing = 0; |
1791 | 0 | break; |
1792 | 0 | case '+': |
1793 | 0 | if (state->apply_in_reverse) |
1794 | 0 | check_old_for_crlf(patch, line, len); |
1795 | 0 | if (!state->apply_in_reverse && |
1796 | 0 | state->ws_error_action != nowarn_ws_error) |
1797 | 0 | check_whitespace(state, line, len, patch->ws_rule); |
1798 | 0 | added++; |
1799 | 0 | newlines--; |
1800 | 0 | trailing = 0; |
1801 | 0 | break; |
1802 | 0 | } |
1803 | | |
1804 | | /* eat the "\\ No newline..." as well, if exists */ |
1805 | 0 | if (skip_len) { |
1806 | 0 | len += skip_len; |
1807 | 0 | state->linenr++; |
1808 | 0 | } |
1809 | 0 | } |
1810 | 0 | if (oldlines || newlines) |
1811 | 0 | return -1; |
1812 | 0 | if (!patch->recount && !deleted && !added) |
1813 | 0 | return -1; |
1814 | | |
1815 | 0 | fragment->leading = leading; |
1816 | 0 | fragment->trailing = trailing; |
1817 | |
|
1818 | 0 | patch->lines_added += added; |
1819 | 0 | patch->lines_deleted += deleted; |
1820 | |
|
1821 | 0 | if (0 < patch->is_new && oldlines) |
1822 | 0 | return error(_("new file depends on old contents")); |
1823 | 0 | if (0 < patch->is_delete && newlines) |
1824 | 0 | return error(_("deleted file still has contents")); |
1825 | 0 | return offset; |
1826 | 0 | } |
1827 | | |
1828 | | /* |
1829 | | * We have seen "diff --git a/... b/..." header (or a traditional patch |
1830 | | * header). Read hunks that belong to this patch into fragments and hang |
1831 | | * them to the given patch structure. |
1832 | | * |
1833 | | * The (fragment->patch, fragment->size) pair points into the memory given |
1834 | | * by the caller, not a copy, when we return. |
1835 | | * |
1836 | | * Returns: |
1837 | | * -1 in case of error, |
1838 | | * the number of bytes in the patch otherwise. |
1839 | | */ |
1840 | | static int parse_single_patch(struct apply_state *state, |
1841 | | const char *line, |
1842 | | unsigned long size, |
1843 | | struct patch *patch) |
1844 | 0 | { |
1845 | 0 | unsigned long offset = 0; |
1846 | 0 | unsigned long oldlines = 0, newlines = 0, context = 0; |
1847 | 0 | struct fragment **fragp = &patch->fragments; |
1848 | |
|
1849 | 0 | while (size > 4 && !memcmp(line, "@@ -", 4)) { |
1850 | 0 | struct fragment *fragment; |
1851 | 0 | int len; |
1852 | |
|
1853 | 0 | CALLOC_ARRAY(fragment, 1); |
1854 | 0 | fragment->linenr = state->linenr; |
1855 | 0 | len = parse_fragment(state, line, size, patch, fragment); |
1856 | 0 | if (len <= 0) { |
1857 | 0 | free(fragment); |
1858 | 0 | return error(_("corrupt patch at line %d"), state->linenr); |
1859 | 0 | } |
1860 | 0 | fragment->patch = line; |
1861 | 0 | fragment->size = len; |
1862 | 0 | oldlines += fragment->oldlines; |
1863 | 0 | newlines += fragment->newlines; |
1864 | 0 | context += fragment->leading + fragment->trailing; |
1865 | |
|
1866 | 0 | *fragp = fragment; |
1867 | 0 | fragp = &fragment->next; |
1868 | |
|
1869 | 0 | offset += len; |
1870 | 0 | line += len; |
1871 | 0 | size -= len; |
1872 | 0 | } |
1873 | | |
1874 | | /* |
1875 | | * If something was removed (i.e. we have old-lines) it cannot |
1876 | | * be creation, and if something was added it cannot be |
1877 | | * deletion. However, the reverse is not true; --unified=0 |
1878 | | * patches that only add are not necessarily creation even |
1879 | | * though they do not have any old lines, and ones that only |
1880 | | * delete are not necessarily deletion. |
1881 | | * |
1882 | | * Unfortunately, a real creation/deletion patch do _not_ have |
1883 | | * any context line by definition, so we cannot safely tell it |
1884 | | * apart with --unified=0 insanity. At least if the patch has |
1885 | | * more than one hunk it is not creation or deletion. |
1886 | | */ |
1887 | 0 | if (patch->is_new < 0 && |
1888 | 0 | (oldlines || (patch->fragments && patch->fragments->next))) |
1889 | 0 | patch->is_new = 0; |
1890 | 0 | if (patch->is_delete < 0 && |
1891 | 0 | (newlines || (patch->fragments && patch->fragments->next))) |
1892 | 0 | patch->is_delete = 0; |
1893 | |
|
1894 | 0 | if (0 < patch->is_new && oldlines) |
1895 | 0 | return error(_("new file %s depends on old contents"), patch->new_name); |
1896 | 0 | if (0 < patch->is_delete && newlines) |
1897 | 0 | return error(_("deleted file %s still has contents"), patch->old_name); |
1898 | 0 | if (!patch->is_delete && !newlines && context && state->apply_verbosity > verbosity_silent) |
1899 | 0 | fprintf_ln(stderr, |
1900 | 0 | _("** warning: " |
1901 | 0 | "file %s becomes empty but is not deleted"), |
1902 | 0 | patch->new_name); |
1903 | |
|
1904 | 0 | return offset; |
1905 | 0 | } |
1906 | | |
1907 | | static inline int metadata_changes(struct patch *patch) |
1908 | 0 | { |
1909 | 0 | return patch->is_rename > 0 || |
1910 | 0 | patch->is_copy > 0 || |
1911 | 0 | patch->is_new > 0 || |
1912 | 0 | patch->is_delete || |
1913 | 0 | (patch->old_mode && patch->new_mode && |
1914 | 0 | patch->old_mode != patch->new_mode); |
1915 | 0 | } |
1916 | | |
1917 | | static char *inflate_it(const void *data, unsigned long size, |
1918 | | unsigned long inflated_size) |
1919 | 0 | { |
1920 | 0 | git_zstream stream; |
1921 | 0 | void *out; |
1922 | 0 | int st; |
1923 | |
|
1924 | 0 | memset(&stream, 0, sizeof(stream)); |
1925 | |
|
1926 | 0 | stream.next_in = (unsigned char *)data; |
1927 | 0 | stream.avail_in = size; |
1928 | 0 | stream.next_out = out = xmalloc(inflated_size); |
1929 | 0 | stream.avail_out = inflated_size; |
1930 | 0 | git_inflate_init(&stream); |
1931 | 0 | st = git_inflate(&stream, Z_FINISH); |
1932 | 0 | git_inflate_end(&stream); |
1933 | 0 | if ((st != Z_STREAM_END) || stream.total_out != inflated_size) { |
1934 | 0 | free(out); |
1935 | 0 | return NULL; |
1936 | 0 | } |
1937 | 0 | return out; |
1938 | 0 | } |
1939 | | |
1940 | | /* |
1941 | | * Read a binary hunk and return a new fragment; fragment->patch |
1942 | | * points at an allocated memory that the caller must free, so |
1943 | | * it is marked as "->free_patch = 1". |
1944 | | */ |
1945 | | static struct fragment *parse_binary_hunk(struct apply_state *state, |
1946 | | char **buf_p, |
1947 | | unsigned long *sz_p, |
1948 | | int *status_p, |
1949 | | int *used_p) |
1950 | 0 | { |
1951 | | /* |
1952 | | * Expect a line that begins with binary patch method ("literal" |
1953 | | * or "delta"), followed by the length of data before deflating. |
1954 | | * a sequence of 'length-byte' followed by base-85 encoded data |
1955 | | * should follow, terminated by a newline. |
1956 | | * |
1957 | | * Each 5-byte sequence of base-85 encodes up to 4 bytes, |
1958 | | * and we would limit the patch line to 66 characters, |
1959 | | * so one line can fit up to 13 groups that would decode |
1960 | | * to 52 bytes max. The length byte 'A'-'Z' corresponds |
1961 | | * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes. |
1962 | | */ |
1963 | 0 | int llen, used; |
1964 | 0 | unsigned long size = *sz_p; |
1965 | 0 | char *buffer = *buf_p; |
1966 | 0 | int patch_method; |
1967 | 0 | unsigned long origlen; |
1968 | 0 | char *data = NULL; |
1969 | 0 | int hunk_size = 0; |
1970 | 0 | struct fragment *frag; |
1971 | |
|
1972 | 0 | llen = linelen(buffer, size); |
1973 | 0 | used = llen; |
1974 | |
|
1975 | 0 | *status_p = 0; |
1976 | |
|
1977 | 0 | if (starts_with(buffer, "delta ")) { |
1978 | 0 | patch_method = BINARY_DELTA_DEFLATED; |
1979 | 0 | origlen = strtoul(buffer + 6, NULL, 10); |
1980 | 0 | } |
1981 | 0 | else if (starts_with(buffer, "literal ")) { |
1982 | 0 | patch_method = BINARY_LITERAL_DEFLATED; |
1983 | 0 | origlen = strtoul(buffer + 8, NULL, 10); |
1984 | 0 | } |
1985 | 0 | else |
1986 | 0 | return NULL; |
1987 | | |
1988 | 0 | state->linenr++; |
1989 | 0 | buffer += llen; |
1990 | 0 | size -= llen; |
1991 | 0 | while (1) { |
1992 | 0 | int byte_length, max_byte_length, newsize; |
1993 | 0 | llen = linelen(buffer, size); |
1994 | 0 | used += llen; |
1995 | 0 | state->linenr++; |
1996 | 0 | if (llen == 1) { |
1997 | | /* consume the blank line */ |
1998 | 0 | buffer++; |
1999 | 0 | size--; |
2000 | 0 | break; |
2001 | 0 | } |
2002 | | /* |
2003 | | * Minimum line is "A00000\n" which is 7-byte long, |
2004 | | * and the line length must be multiple of 5 plus 2. |
2005 | | */ |
2006 | 0 | if ((llen < 7) || (llen-2) % 5) |
2007 | 0 | goto corrupt; |
2008 | 0 | max_byte_length = (llen - 2) / 5 * 4; |
2009 | 0 | byte_length = *buffer; |
2010 | 0 | if ('A' <= byte_length && byte_length <= 'Z') |
2011 | 0 | byte_length = byte_length - 'A' + 1; |
2012 | 0 | else if ('a' <= byte_length && byte_length <= 'z') |
2013 | 0 | byte_length = byte_length - 'a' + 27; |
2014 | 0 | else |
2015 | 0 | goto corrupt; |
2016 | | /* if the input length was not multiple of 4, we would |
2017 | | * have filler at the end but the filler should never |
2018 | | * exceed 3 bytes |
2019 | | */ |
2020 | 0 | if (max_byte_length < byte_length || |
2021 | 0 | byte_length <= max_byte_length - 4) |
2022 | 0 | goto corrupt; |
2023 | 0 | newsize = hunk_size + byte_length; |
2024 | 0 | data = xrealloc(data, newsize); |
2025 | 0 | if (decode_85(data + hunk_size, buffer + 1, byte_length)) |
2026 | 0 | goto corrupt; |
2027 | 0 | hunk_size = newsize; |
2028 | 0 | buffer += llen; |
2029 | 0 | size -= llen; |
2030 | 0 | } |
2031 | | |
2032 | 0 | CALLOC_ARRAY(frag, 1); |
2033 | 0 | frag->patch = inflate_it(data, hunk_size, origlen); |
2034 | 0 | frag->free_patch = 1; |
2035 | 0 | if (!frag->patch) |
2036 | 0 | goto corrupt; |
2037 | 0 | free(data); |
2038 | 0 | frag->size = origlen; |
2039 | 0 | *buf_p = buffer; |
2040 | 0 | *sz_p = size; |
2041 | 0 | *used_p = used; |
2042 | 0 | frag->binary_patch_method = patch_method; |
2043 | 0 | return frag; |
2044 | | |
2045 | 0 | corrupt: |
2046 | 0 | free(data); |
2047 | 0 | *status_p = -1; |
2048 | 0 | error(_("corrupt binary patch at line %d: %.*s"), |
2049 | 0 | state->linenr-1, llen-1, buffer); |
2050 | 0 | return NULL; |
2051 | 0 | } |
2052 | | |
2053 | | /* |
2054 | | * Returns: |
2055 | | * -1 in case of error, |
2056 | | * the length of the parsed binary patch otherwise |
2057 | | */ |
2058 | | static int parse_binary(struct apply_state *state, |
2059 | | char *buffer, |
2060 | | unsigned long size, |
2061 | | struct patch *patch) |
2062 | 0 | { |
2063 | | /* |
2064 | | * We have read "GIT binary patch\n"; what follows is a line |
2065 | | * that says the patch method (currently, either "literal" or |
2066 | | * "delta") and the length of data before deflating; a |
2067 | | * sequence of 'length-byte' followed by base-85 encoded data |
2068 | | * follows. |
2069 | | * |
2070 | | * When a binary patch is reversible, there is another binary |
2071 | | * hunk in the same format, starting with patch method (either |
2072 | | * "literal" or "delta") with the length of data, and a sequence |
2073 | | * of length-byte + base-85 encoded data, terminated with another |
2074 | | * empty line. This data, when applied to the postimage, produces |
2075 | | * the preimage. |
2076 | | */ |
2077 | 0 | struct fragment *forward; |
2078 | 0 | struct fragment *reverse; |
2079 | 0 | int status; |
2080 | 0 | int used, used_1; |
2081 | |
|
2082 | 0 | forward = parse_binary_hunk(state, &buffer, &size, &status, &used); |
2083 | 0 | if (!forward && !status) |
2084 | | /* there has to be one hunk (forward hunk) */ |
2085 | 0 | return error(_("unrecognized binary patch at line %d"), state->linenr-1); |
2086 | 0 | if (status) |
2087 | | /* otherwise we already gave an error message */ |
2088 | 0 | return status; |
2089 | | |
2090 | 0 | reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1); |
2091 | 0 | if (reverse) |
2092 | 0 | used += used_1; |
2093 | 0 | else if (status) { |
2094 | | /* |
2095 | | * Not having reverse hunk is not an error, but having |
2096 | | * a corrupt reverse hunk is. |
2097 | | */ |
2098 | 0 | free((void*) forward->patch); |
2099 | 0 | free(forward); |
2100 | 0 | return status; |
2101 | 0 | } |
2102 | 0 | forward->next = reverse; |
2103 | 0 | patch->fragments = forward; |
2104 | 0 | patch->is_binary = 1; |
2105 | 0 | return used; |
2106 | 0 | } |
2107 | | |
2108 | | static void prefix_one(struct apply_state *state, char **name) |
2109 | 0 | { |
2110 | 0 | char *old_name = *name; |
2111 | 0 | if (!old_name) |
2112 | 0 | return; |
2113 | 0 | *name = prefix_filename(state->prefix, *name); |
2114 | 0 | free(old_name); |
2115 | 0 | } |
2116 | | |
2117 | | static void prefix_patch(struct apply_state *state, struct patch *p) |
2118 | 0 | { |
2119 | 0 | if (!state->prefix || p->is_toplevel_relative) |
2120 | 0 | return; |
2121 | 0 | prefix_one(state, &p->new_name); |
2122 | 0 | prefix_one(state, &p->old_name); |
2123 | 0 | } |
2124 | | |
2125 | | /* |
2126 | | * include/exclude |
2127 | | */ |
2128 | | |
2129 | | static void add_name_limit(struct apply_state *state, |
2130 | | const char *name, |
2131 | | int exclude) |
2132 | 0 | { |
2133 | 0 | struct string_list_item *it; |
2134 | |
|
2135 | 0 | it = string_list_append(&state->limit_by_name, name); |
2136 | 0 | it->util = exclude ? NULL : (void *) 1; |
2137 | 0 | } |
2138 | | |
2139 | | static int use_patch(struct apply_state *state, struct patch *p) |
2140 | 0 | { |
2141 | 0 | const char *pathname = p->new_name ? p->new_name : p->old_name; |
2142 | 0 | int i; |
2143 | | |
2144 | | /* Paths outside are not touched regardless of "--include" */ |
2145 | 0 | if (state->prefix && *state->prefix) { |
2146 | 0 | const char *rest; |
2147 | 0 | if (!skip_prefix(pathname, state->prefix, &rest) || !*rest) |
2148 | 0 | return 0; |
2149 | 0 | } |
2150 | | |
2151 | | /* See if it matches any of exclude/include rule */ |
2152 | 0 | for (i = 0; i < state->limit_by_name.nr; i++) { |
2153 | 0 | struct string_list_item *it = &state->limit_by_name.items[i]; |
2154 | 0 | if (!wildmatch(it->string, pathname, 0)) |
2155 | 0 | return (it->util != NULL); |
2156 | 0 | } |
2157 | | |
2158 | | /* |
2159 | | * If we had any include, a path that does not match any rule is |
2160 | | * not used. Otherwise, we saw bunch of exclude rules (or none) |
2161 | | * and such a path is used. |
2162 | | */ |
2163 | 0 | return !state->has_include; |
2164 | 0 | } |
2165 | | |
2166 | | /* |
2167 | | * Read the patch text in "buffer" that extends for "size" bytes; stop |
2168 | | * reading after seeing a single patch (i.e. changes to a single file). |
2169 | | * Create fragments (i.e. patch hunks) and hang them to the given patch. |
2170 | | * |
2171 | | * Returns: |
2172 | | * -1 if no header was found or parse_binary() failed, |
2173 | | * -128 on another error, |
2174 | | * the number of bytes consumed otherwise, |
2175 | | * so that the caller can call us again for the next patch. |
2176 | | */ |
2177 | | static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch) |
2178 | 0 | { |
2179 | 0 | int hdrsize, patchsize; |
2180 | 0 | int offset = find_header(state, buffer, size, &hdrsize, patch); |
2181 | |
|
2182 | 0 | if (offset < 0) |
2183 | 0 | return offset; |
2184 | | |
2185 | 0 | prefix_patch(state, patch); |
2186 | |
|
2187 | 0 | if (!use_patch(state, patch)) |
2188 | 0 | patch->ws_rule = 0; |
2189 | 0 | else if (patch->new_name) |
2190 | 0 | patch->ws_rule = whitespace_rule(state->repo->index, |
2191 | 0 | patch->new_name); |
2192 | 0 | else |
2193 | 0 | patch->ws_rule = whitespace_rule(state->repo->index, |
2194 | 0 | patch->old_name); |
2195 | |
|
2196 | 0 | patchsize = parse_single_patch(state, |
2197 | 0 | buffer + offset + hdrsize, |
2198 | 0 | size - offset - hdrsize, |
2199 | 0 | patch); |
2200 | |
|
2201 | 0 | if (patchsize < 0) |
2202 | 0 | return -128; |
2203 | | |
2204 | 0 | if (!patchsize) { |
2205 | 0 | static const char git_binary[] = "GIT binary patch\n"; |
2206 | 0 | int hd = hdrsize + offset; |
2207 | 0 | unsigned long llen = linelen(buffer + hd, size - hd); |
2208 | |
|
2209 | 0 | if (llen == sizeof(git_binary) - 1 && |
2210 | 0 | !memcmp(git_binary, buffer + hd, llen)) { |
2211 | 0 | int used; |
2212 | 0 | state->linenr++; |
2213 | 0 | used = parse_binary(state, buffer + hd + llen, |
2214 | 0 | size - hd - llen, patch); |
2215 | 0 | if (used < 0) |
2216 | 0 | return -1; |
2217 | 0 | if (used) |
2218 | 0 | patchsize = used + llen; |
2219 | 0 | else |
2220 | 0 | patchsize = 0; |
2221 | 0 | } |
2222 | 0 | else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) { |
2223 | 0 | static const char *binhdr[] = { |
2224 | 0 | "Binary files ", |
2225 | 0 | "Files ", |
2226 | 0 | NULL, |
2227 | 0 | }; |
2228 | 0 | int i; |
2229 | 0 | for (i = 0; binhdr[i]; i++) { |
2230 | 0 | int len = strlen(binhdr[i]); |
2231 | 0 | if (len < size - hd && |
2232 | 0 | !memcmp(binhdr[i], buffer + hd, len)) { |
2233 | 0 | state->linenr++; |
2234 | 0 | patch->is_binary = 1; |
2235 | 0 | patchsize = llen; |
2236 | 0 | break; |
2237 | 0 | } |
2238 | 0 | } |
2239 | 0 | } |
2240 | | |
2241 | | /* Empty patch cannot be applied if it is a text patch |
2242 | | * without metadata change. A binary patch appears |
2243 | | * empty to us here. |
2244 | | */ |
2245 | 0 | if ((state->apply || state->check) && |
2246 | 0 | (!patch->is_binary && !metadata_changes(patch))) { |
2247 | 0 | error(_("patch with only garbage at line %d"), state->linenr); |
2248 | 0 | return -128; |
2249 | 0 | } |
2250 | 0 | } |
2251 | | |
2252 | 0 | return offset + hdrsize + patchsize; |
2253 | 0 | } |
2254 | | |
2255 | | static void reverse_patches(struct patch *p) |
2256 | 0 | { |
2257 | 0 | for (; p; p = p->next) { |
2258 | 0 | struct fragment *frag = p->fragments; |
2259 | |
|
2260 | 0 | SWAP(p->new_name, p->old_name); |
2261 | 0 | if (p->new_mode || p->is_delete) |
2262 | 0 | SWAP(p->new_mode, p->old_mode); |
2263 | 0 | SWAP(p->is_new, p->is_delete); |
2264 | 0 | SWAP(p->lines_added, p->lines_deleted); |
2265 | 0 | SWAP(p->old_oid_prefix, p->new_oid_prefix); |
2266 | |
|
2267 | 0 | for (; frag; frag = frag->next) { |
2268 | 0 | SWAP(frag->newpos, frag->oldpos); |
2269 | 0 | SWAP(frag->newlines, frag->oldlines); |
2270 | 0 | } |
2271 | 0 | } |
2272 | 0 | } |
2273 | | |
2274 | | static const char pluses[] = |
2275 | | "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; |
2276 | | static const char minuses[]= |
2277 | | "----------------------------------------------------------------------"; |
2278 | | |
2279 | | static void show_stats(struct apply_state *state, struct patch *patch) |
2280 | 0 | { |
2281 | 0 | struct strbuf qname = STRBUF_INIT; |
2282 | 0 | char *cp = patch->new_name ? patch->new_name : patch->old_name; |
2283 | 0 | int max, add, del; |
2284 | |
|
2285 | 0 | quote_c_style(cp, &qname, NULL, 0); |
2286 | | |
2287 | | /* |
2288 | | * "scale" the filename |
2289 | | */ |
2290 | 0 | max = state->max_len; |
2291 | 0 | if (max > 50) |
2292 | 0 | max = 50; |
2293 | |
|
2294 | 0 | if (qname.len > max) { |
2295 | 0 | cp = strchr(qname.buf + qname.len + 3 - max, '/'); |
2296 | 0 | if (!cp) |
2297 | 0 | cp = qname.buf + qname.len + 3 - max; |
2298 | 0 | strbuf_splice(&qname, 0, cp - qname.buf, "...", 3); |
2299 | 0 | } |
2300 | |
|
2301 | 0 | if (patch->is_binary) { |
2302 | 0 | printf(" %-*s | Bin\n", max, qname.buf); |
2303 | 0 | strbuf_release(&qname); |
2304 | 0 | return; |
2305 | 0 | } |
2306 | | |
2307 | 0 | printf(" %-*s |", max, qname.buf); |
2308 | 0 | strbuf_release(&qname); |
2309 | | |
2310 | | /* |
2311 | | * scale the add/delete |
2312 | | */ |
2313 | 0 | max = max + state->max_change > 70 ? 70 - max : state->max_change; |
2314 | 0 | add = patch->lines_added; |
2315 | 0 | del = patch->lines_deleted; |
2316 | |
|
2317 | 0 | if (state->max_change > 0) { |
2318 | 0 | int total = ((add + del) * max + state->max_change / 2) / state->max_change; |
2319 | 0 | add = (add * max + state->max_change / 2) / state->max_change; |
2320 | 0 | del = total - add; |
2321 | 0 | } |
2322 | 0 | printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted, |
2323 | 0 | add, pluses, del, minuses); |
2324 | 0 | } |
2325 | | |
2326 | | static int read_old_data(struct stat *st, struct patch *patch, |
2327 | | const char *path, struct strbuf *buf) |
2328 | 0 | { |
2329 | 0 | int conv_flags = patch->crlf_in_old ? |
2330 | 0 | CONV_EOL_KEEP_CRLF : CONV_EOL_RENORMALIZE; |
2331 | 0 | switch (st->st_mode & S_IFMT) { |
2332 | 0 | case S_IFLNK: |
2333 | 0 | if (strbuf_readlink(buf, path, st->st_size) < 0) |
2334 | 0 | return error(_("unable to read symlink %s"), path); |
2335 | 0 | return 0; |
2336 | 0 | case S_IFREG: |
2337 | 0 | if (strbuf_read_file(buf, path, st->st_size) != st->st_size) |
2338 | 0 | return error(_("unable to open or read %s"), path); |
2339 | | /* |
2340 | | * "git apply" without "--index/--cached" should never look |
2341 | | * at the index; the target file may not have been added to |
2342 | | * the index yet, and we may not even be in any Git repository. |
2343 | | * Pass NULL to convert_to_git() to stress this; the function |
2344 | | * should never look at the index when explicit crlf option |
2345 | | * is given. |
2346 | | */ |
2347 | 0 | convert_to_git(NULL, path, buf->buf, buf->len, buf, conv_flags); |
2348 | 0 | return 0; |
2349 | 0 | default: |
2350 | 0 | return -1; |
2351 | 0 | } |
2352 | 0 | } |
2353 | | |
2354 | | /* |
2355 | | * Update the preimage, and the common lines in postimage, |
2356 | | * from buffer buf of length len. |
2357 | | */ |
2358 | | static void update_pre_post_images(struct image *preimage, |
2359 | | struct image *postimage, |
2360 | | char *buf, size_t len) |
2361 | 0 | { |
2362 | 0 | struct image fixed_preimage = IMAGE_INIT; |
2363 | 0 | size_t insert_pos = 0; |
2364 | 0 | int i, ctx, reduced; |
2365 | 0 | const char *fixed; |
2366 | | |
2367 | | /* |
2368 | | * Update the preimage with whitespace fixes. Note that we |
2369 | | * are not losing preimage->buf -- apply_one_fragment() will |
2370 | | * free "oldlines". |
2371 | | */ |
2372 | 0 | image_prepare(&fixed_preimage, buf, len, 1); |
2373 | 0 | for (i = 0; i < fixed_preimage.line_nr; i++) |
2374 | 0 | fixed_preimage.line[i].flag = preimage->line[i].flag; |
2375 | 0 | image_clear(preimage); |
2376 | 0 | *preimage = fixed_preimage; |
2377 | 0 | fixed = preimage->buf.buf; |
2378 | | |
2379 | | /* |
2380 | | * Adjust the common context lines in postimage. |
2381 | | */ |
2382 | 0 | for (i = reduced = ctx = 0; i < postimage->line_nr; i++) { |
2383 | 0 | size_t l_len = postimage->line[i].len; |
2384 | |
|
2385 | 0 | if (!(postimage->line[i].flag & LINE_COMMON)) { |
2386 | | /* an added line -- no counterparts in preimage */ |
2387 | 0 | insert_pos += l_len; |
2388 | 0 | continue; |
2389 | 0 | } |
2390 | | |
2391 | | /* and find the corresponding one in the fixed preimage */ |
2392 | 0 | while (ctx < preimage->line_nr && |
2393 | 0 | !(preimage->line[ctx].flag & LINE_COMMON)) { |
2394 | 0 | fixed += preimage->line[ctx].len; |
2395 | 0 | ctx++; |
2396 | 0 | } |
2397 | | |
2398 | | /* |
2399 | | * preimage is expected to run out, if the caller |
2400 | | * fixed addition of trailing blank lines. |
2401 | | */ |
2402 | 0 | if (preimage->line_nr <= ctx) { |
2403 | 0 | reduced++; |
2404 | 0 | continue; |
2405 | 0 | } |
2406 | | |
2407 | | /* and copy it in, while fixing the line length */ |
2408 | 0 | l_len = preimage->line[ctx].len; |
2409 | 0 | strbuf_splice(&postimage->buf, insert_pos, postimage->line[i].len, |
2410 | 0 | fixed, l_len); |
2411 | 0 | insert_pos += l_len; |
2412 | 0 | fixed += l_len; |
2413 | 0 | postimage->line[i].len = l_len; |
2414 | 0 | ctx++; |
2415 | 0 | } |
2416 | | |
2417 | | /* Fix the length of the whole thing */ |
2418 | 0 | postimage->line_nr -= reduced; |
2419 | 0 | } |
2420 | | |
2421 | | /* |
2422 | | * Compare lines s1 of length n1 and s2 of length n2, ignoring |
2423 | | * whitespace difference. Returns 1 if they match, 0 otherwise |
2424 | | */ |
2425 | | static int fuzzy_matchlines(const char *s1, size_t n1, |
2426 | | const char *s2, size_t n2) |
2427 | 0 | { |
2428 | 0 | const char *end1 = s1 + n1; |
2429 | 0 | const char *end2 = s2 + n2; |
2430 | | |
2431 | | /* ignore line endings */ |
2432 | 0 | while (s1 < end1 && (end1[-1] == '\r' || end1[-1] == '\n')) |
2433 | 0 | end1--; |
2434 | 0 | while (s2 < end2 && (end2[-1] == '\r' || end2[-1] == '\n')) |
2435 | 0 | end2--; |
2436 | |
|
2437 | 0 | while (s1 < end1 && s2 < end2) { |
2438 | 0 | if (isspace(*s1)) { |
2439 | | /* |
2440 | | * Skip whitespace. We check on both buffers |
2441 | | * because we don't want "a b" to match "ab". |
2442 | | */ |
2443 | 0 | if (!isspace(*s2)) |
2444 | 0 | return 0; |
2445 | 0 | while (s1 < end1 && isspace(*s1)) |
2446 | 0 | s1++; |
2447 | 0 | while (s2 < end2 && isspace(*s2)) |
2448 | 0 | s2++; |
2449 | 0 | } else if (*s1++ != *s2++) |
2450 | 0 | return 0; |
2451 | 0 | } |
2452 | | |
2453 | | /* If we reached the end on one side only, lines don't match. */ |
2454 | 0 | return s1 == end1 && s2 == end2; |
2455 | 0 | } |
2456 | | |
2457 | | static int line_by_line_fuzzy_match(struct image *img, |
2458 | | struct image *preimage, |
2459 | | struct image *postimage, |
2460 | | unsigned long current, |
2461 | | int current_lno, |
2462 | | int preimage_limit) |
2463 | 0 | { |
2464 | 0 | int i; |
2465 | 0 | size_t imgoff = 0; |
2466 | 0 | size_t preoff = 0; |
2467 | 0 | size_t extra_chars; |
2468 | 0 | char *buf; |
2469 | 0 | char *preimage_eof; |
2470 | 0 | char *preimage_end; |
2471 | 0 | struct strbuf fixed; |
2472 | 0 | char *fixed_buf; |
2473 | 0 | size_t fixed_len; |
2474 | |
|
2475 | 0 | for (i = 0; i < preimage_limit; i++) { |
2476 | 0 | size_t prelen = preimage->line[i].len; |
2477 | 0 | size_t imglen = img->line[current_lno+i].len; |
2478 | |
|
2479 | 0 | if (!fuzzy_matchlines(img->buf.buf + current + imgoff, imglen, |
2480 | 0 | preimage->buf.buf + preoff, prelen)) |
2481 | 0 | return 0; |
2482 | 0 | imgoff += imglen; |
2483 | 0 | preoff += prelen; |
2484 | 0 | } |
2485 | | |
2486 | | /* |
2487 | | * Ok, the preimage matches with whitespace fuzz. |
2488 | | * |
2489 | | * imgoff now holds the true length of the target that |
2490 | | * matches the preimage before the end of the file. |
2491 | | * |
2492 | | * Count the number of characters in the preimage that fall |
2493 | | * beyond the end of the file and make sure that all of them |
2494 | | * are whitespace characters. (This can only happen if |
2495 | | * we are removing blank lines at the end of the file.) |
2496 | | */ |
2497 | 0 | buf = preimage_eof = preimage->buf.buf + preoff; |
2498 | 0 | for ( ; i < preimage->line_nr; i++) |
2499 | 0 | preoff += preimage->line[i].len; |
2500 | 0 | preimage_end = preimage->buf.buf + preoff; |
2501 | 0 | for ( ; buf < preimage_end; buf++) |
2502 | 0 | if (!isspace(*buf)) |
2503 | 0 | return 0; |
2504 | | |
2505 | | /* |
2506 | | * Update the preimage and the common postimage context |
2507 | | * lines to use the same whitespace as the target. |
2508 | | * If whitespace is missing in the target (i.e. |
2509 | | * if the preimage extends beyond the end of the file), |
2510 | | * use the whitespace from the preimage. |
2511 | | */ |
2512 | 0 | extra_chars = preimage_end - preimage_eof; |
2513 | 0 | strbuf_init(&fixed, imgoff + extra_chars); |
2514 | 0 | strbuf_add(&fixed, img->buf.buf + current, imgoff); |
2515 | 0 | strbuf_add(&fixed, preimage_eof, extra_chars); |
2516 | 0 | fixed_buf = strbuf_detach(&fixed, &fixed_len); |
2517 | 0 | update_pre_post_images(preimage, postimage, |
2518 | 0 | fixed_buf, fixed_len); |
2519 | 0 | return 1; |
2520 | 0 | } |
2521 | | |
2522 | | static int match_fragment(struct apply_state *state, |
2523 | | struct image *img, |
2524 | | struct image *preimage, |
2525 | | struct image *postimage, |
2526 | | unsigned long current, |
2527 | | int current_lno, |
2528 | | unsigned ws_rule, |
2529 | | int match_beginning, int match_end) |
2530 | 0 | { |
2531 | 0 | int i; |
2532 | 0 | const char *orig, *target; |
2533 | 0 | struct strbuf fixed = STRBUF_INIT; |
2534 | 0 | char *fixed_buf; |
2535 | 0 | size_t fixed_len; |
2536 | 0 | int preimage_limit; |
2537 | 0 | int ret; |
2538 | |
|
2539 | 0 | if (preimage->line_nr + current_lno <= img->line_nr) { |
2540 | | /* |
2541 | | * The hunk falls within the boundaries of img. |
2542 | | */ |
2543 | 0 | preimage_limit = preimage->line_nr; |
2544 | 0 | if (match_end && (preimage->line_nr + current_lno != img->line_nr)) { |
2545 | 0 | ret = 0; |
2546 | 0 | goto out; |
2547 | 0 | } |
2548 | 0 | } else if (state->ws_error_action == correct_ws_error && |
2549 | 0 | (ws_rule & WS_BLANK_AT_EOF)) { |
2550 | | /* |
2551 | | * This hunk extends beyond the end of img, and we are |
2552 | | * removing blank lines at the end of the file. This |
2553 | | * many lines from the beginning of the preimage must |
2554 | | * match with img, and the remainder of the preimage |
2555 | | * must be blank. |
2556 | | */ |
2557 | 0 | preimage_limit = img->line_nr - current_lno; |
2558 | 0 | } else { |
2559 | | /* |
2560 | | * The hunk extends beyond the end of the img and |
2561 | | * we are not removing blanks at the end, so we |
2562 | | * should reject the hunk at this position. |
2563 | | */ |
2564 | 0 | ret = 0; |
2565 | 0 | goto out; |
2566 | 0 | } |
2567 | | |
2568 | 0 | if (match_beginning && current_lno) { |
2569 | 0 | ret = 0; |
2570 | 0 | goto out; |
2571 | 0 | } |
2572 | | |
2573 | | /* Quick hash check */ |
2574 | 0 | for (i = 0; i < preimage_limit; i++) { |
2575 | 0 | if ((img->line[current_lno + i].flag & LINE_PATCHED) || |
2576 | 0 | (preimage->line[i].hash != img->line[current_lno + i].hash)) { |
2577 | 0 | ret = 0; |
2578 | 0 | goto out; |
2579 | 0 | } |
2580 | 0 | } |
2581 | | |
2582 | 0 | if (preimage_limit == preimage->line_nr) { |
2583 | | /* |
2584 | | * Do we have an exact match? If we were told to match |
2585 | | * at the end, size must be exactly at current+fragsize, |
2586 | | * otherwise current+fragsize must be still within the preimage, |
2587 | | * and either case, the old piece should match the preimage |
2588 | | * exactly. |
2589 | | */ |
2590 | 0 | if ((match_end |
2591 | 0 | ? (current + preimage->buf.len == img->buf.len) |
2592 | 0 | : (current + preimage->buf.len <= img->buf.len)) && |
2593 | 0 | !memcmp(img->buf.buf + current, preimage->buf.buf, preimage->buf.len)) { |
2594 | 0 | ret = 1; |
2595 | 0 | goto out; |
2596 | 0 | } |
2597 | 0 | } else { |
2598 | | /* |
2599 | | * The preimage extends beyond the end of img, so |
2600 | | * there cannot be an exact match. |
2601 | | * |
2602 | | * There must be one non-blank context line that match |
2603 | | * a line before the end of img. |
2604 | | */ |
2605 | 0 | const char *buf, *buf_end; |
2606 | |
|
2607 | 0 | buf = preimage->buf.buf; |
2608 | 0 | buf_end = buf; |
2609 | 0 | for (i = 0; i < preimage_limit; i++) |
2610 | 0 | buf_end += preimage->line[i].len; |
2611 | |
|
2612 | 0 | for ( ; buf < buf_end; buf++) |
2613 | 0 | if (!isspace(*buf)) |
2614 | 0 | break; |
2615 | 0 | if (buf == buf_end) { |
2616 | 0 | ret = 0; |
2617 | 0 | goto out; |
2618 | 0 | } |
2619 | 0 | } |
2620 | | |
2621 | | /* |
2622 | | * No exact match. If we are ignoring whitespace, run a line-by-line |
2623 | | * fuzzy matching. We collect all the line length information because |
2624 | | * we need it to adjust whitespace if we match. |
2625 | | */ |
2626 | 0 | if (state->ws_ignore_action == ignore_ws_change) { |
2627 | 0 | ret = line_by_line_fuzzy_match(img, preimage, postimage, |
2628 | 0 | current, current_lno, preimage_limit); |
2629 | 0 | goto out; |
2630 | 0 | } |
2631 | | |
2632 | 0 | if (state->ws_error_action != correct_ws_error) { |
2633 | 0 | ret = 0; |
2634 | 0 | goto out; |
2635 | 0 | } |
2636 | | |
2637 | | /* |
2638 | | * The hunk does not apply byte-by-byte, but the hash says |
2639 | | * it might with whitespace fuzz. We weren't asked to |
2640 | | * ignore whitespace, we were asked to correct whitespace |
2641 | | * errors, so let's try matching after whitespace correction. |
2642 | | * |
2643 | | * While checking the preimage against the target, whitespace |
2644 | | * errors in both fixed, we count how large the corresponding |
2645 | | * postimage needs to be. The postimage prepared by |
2646 | | * apply_one_fragment() has whitespace errors fixed on added |
2647 | | * lines already, but the common lines were propagated as-is, |
2648 | | * which may become longer when their whitespace errors are |
2649 | | * fixed. |
2650 | | */ |
2651 | | |
2652 | | /* |
2653 | | * The preimage may extend beyond the end of the file, |
2654 | | * but in this loop we will only handle the part of the |
2655 | | * preimage that falls within the file. |
2656 | | */ |
2657 | 0 | strbuf_grow(&fixed, preimage->buf.len + 1); |
2658 | 0 | orig = preimage->buf.buf; |
2659 | 0 | target = img->buf.buf + current; |
2660 | 0 | for (i = 0; i < preimage_limit; i++) { |
2661 | 0 | size_t oldlen = preimage->line[i].len; |
2662 | 0 | size_t tgtlen = img->line[current_lno + i].len; |
2663 | 0 | size_t fixstart = fixed.len; |
2664 | 0 | struct strbuf tgtfix; |
2665 | 0 | int match; |
2666 | | |
2667 | | /* Try fixing the line in the preimage */ |
2668 | 0 | ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL); |
2669 | | |
2670 | | /* Try fixing the line in the target */ |
2671 | 0 | strbuf_init(&tgtfix, tgtlen); |
2672 | 0 | ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL); |
2673 | | |
2674 | | /* |
2675 | | * If they match, either the preimage was based on |
2676 | | * a version before our tree fixed whitespace breakage, |
2677 | | * or we are lacking a whitespace-fix patch the tree |
2678 | | * the preimage was based on already had (i.e. target |
2679 | | * has whitespace breakage, the preimage doesn't). |
2680 | | * In either case, we are fixing the whitespace breakages |
2681 | | * so we might as well take the fix together with their |
2682 | | * real change. |
2683 | | */ |
2684 | 0 | match = (tgtfix.len == fixed.len - fixstart && |
2685 | 0 | !memcmp(tgtfix.buf, fixed.buf + fixstart, |
2686 | 0 | fixed.len - fixstart)); |
2687 | |
|
2688 | 0 | strbuf_release(&tgtfix); |
2689 | 0 | if (!match) { |
2690 | 0 | ret = 0; |
2691 | 0 | goto out; |
2692 | 0 | } |
2693 | | |
2694 | 0 | orig += oldlen; |
2695 | 0 | target += tgtlen; |
2696 | 0 | } |
2697 | | |
2698 | | |
2699 | | /* |
2700 | | * Now handle the lines in the preimage that falls beyond the |
2701 | | * end of the file (if any). They will only match if they are |
2702 | | * empty or only contain whitespace (if WS_BLANK_AT_EOL is |
2703 | | * false). |
2704 | | */ |
2705 | 0 | for ( ; i < preimage->line_nr; i++) { |
2706 | 0 | size_t fixstart = fixed.len; /* start of the fixed preimage */ |
2707 | 0 | size_t oldlen = preimage->line[i].len; |
2708 | 0 | int j; |
2709 | | |
2710 | | /* Try fixing the line in the preimage */ |
2711 | 0 | ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL); |
2712 | |
|
2713 | 0 | for (j = fixstart; j < fixed.len; j++) { |
2714 | 0 | if (!isspace(fixed.buf[j])) { |
2715 | 0 | ret = 0; |
2716 | 0 | goto out; |
2717 | 0 | } |
2718 | 0 | } |
2719 | | |
2720 | | |
2721 | 0 | orig += oldlen; |
2722 | 0 | } |
2723 | | |
2724 | | /* |
2725 | | * Yes, the preimage is based on an older version that still |
2726 | | * has whitespace breakages unfixed, and fixing them makes the |
2727 | | * hunk match. Update the context lines in the postimage. |
2728 | | */ |
2729 | 0 | fixed_buf = strbuf_detach(&fixed, &fixed_len); |
2730 | 0 | update_pre_post_images(preimage, postimage, |
2731 | 0 | fixed_buf, fixed_len); |
2732 | |
|
2733 | 0 | ret = 1; |
2734 | |
|
2735 | 0 | out: |
2736 | 0 | strbuf_release(&fixed); |
2737 | 0 | return ret; |
2738 | 0 | } |
2739 | | |
2740 | | static int find_pos(struct apply_state *state, |
2741 | | struct image *img, |
2742 | | struct image *preimage, |
2743 | | struct image *postimage, |
2744 | | int line, |
2745 | | unsigned ws_rule, |
2746 | | int match_beginning, int match_end) |
2747 | 0 | { |
2748 | 0 | int i; |
2749 | 0 | unsigned long backwards, forwards, current; |
2750 | 0 | int backwards_lno, forwards_lno, current_lno; |
2751 | | |
2752 | | /* |
2753 | | * When running with --allow-overlap, it is possible that a hunk is |
2754 | | * seen that pretends to start at the beginning (but no longer does), |
2755 | | * and that *still* needs to match the end. So trust `match_end` more |
2756 | | * than `match_beginning`. |
2757 | | */ |
2758 | 0 | if (state->allow_overlap && match_beginning && match_end && |
2759 | 0 | img->line_nr - preimage->line_nr != 0) |
2760 | 0 | match_beginning = 0; |
2761 | | |
2762 | | /* |
2763 | | * If match_beginning or match_end is specified, there is no |
2764 | | * point starting from a wrong line that will never match and |
2765 | | * wander around and wait for a match at the specified end. |
2766 | | */ |
2767 | 0 | if (match_beginning) |
2768 | 0 | line = 0; |
2769 | 0 | else if (match_end) |
2770 | 0 | line = img->line_nr - preimage->line_nr; |
2771 | | |
2772 | | /* |
2773 | | * Because the comparison is unsigned, the following test |
2774 | | * will also take care of a negative line number that can |
2775 | | * result when match_end and preimage is larger than the target. |
2776 | | */ |
2777 | 0 | if ((size_t) line > img->line_nr) |
2778 | 0 | line = img->line_nr; |
2779 | |
|
2780 | 0 | current = 0; |
2781 | 0 | for (i = 0; i < line; i++) |
2782 | 0 | current += img->line[i].len; |
2783 | | |
2784 | | /* |
2785 | | * There's probably some smart way to do this, but I'll leave |
2786 | | * that to the smart and beautiful people. I'm simple and stupid. |
2787 | | */ |
2788 | 0 | backwards = current; |
2789 | 0 | backwards_lno = line; |
2790 | 0 | forwards = current; |
2791 | 0 | forwards_lno = line; |
2792 | 0 | current_lno = line; |
2793 | |
|
2794 | 0 | for (i = 0; ; i++) { |
2795 | 0 | if (match_fragment(state, img, preimage, postimage, |
2796 | 0 | current, current_lno, ws_rule, |
2797 | 0 | match_beginning, match_end)) |
2798 | 0 | return current_lno; |
2799 | | |
2800 | 0 | again: |
2801 | 0 | if (backwards_lno == 0 && forwards_lno == img->line_nr) |
2802 | 0 | break; |
2803 | | |
2804 | 0 | if (i & 1) { |
2805 | 0 | if (backwards_lno == 0) { |
2806 | 0 | i++; |
2807 | 0 | goto again; |
2808 | 0 | } |
2809 | 0 | backwards_lno--; |
2810 | 0 | backwards -= img->line[backwards_lno].len; |
2811 | 0 | current = backwards; |
2812 | 0 | current_lno = backwards_lno; |
2813 | 0 | } else { |
2814 | 0 | if (forwards_lno == img->line_nr) { |
2815 | 0 | i++; |
2816 | 0 | goto again; |
2817 | 0 | } |
2818 | 0 | forwards += img->line[forwards_lno].len; |
2819 | 0 | forwards_lno++; |
2820 | 0 | current = forwards; |
2821 | 0 | current_lno = forwards_lno; |
2822 | 0 | } |
2823 | |
|
2824 | 0 | } |
2825 | 0 | return -1; |
2826 | 0 | } |
2827 | | |
2828 | | /* |
2829 | | * The change from "preimage" and "postimage" has been found to |
2830 | | * apply at applied_pos (counts in line numbers) in "img". |
2831 | | * Update "img" to remove "preimage" and replace it with "postimage". |
2832 | | */ |
2833 | | static void update_image(struct apply_state *state, |
2834 | | struct image *img, |
2835 | | int applied_pos, |
2836 | | struct image *preimage, |
2837 | | struct image *postimage) |
2838 | 0 | { |
2839 | | /* |
2840 | | * remove the copy of preimage at offset in img |
2841 | | * and replace it with postimage |
2842 | | */ |
2843 | 0 | int i, nr; |
2844 | 0 | size_t remove_count, insert_count, applied_at = 0; |
2845 | 0 | size_t result_alloc; |
2846 | 0 | char *result; |
2847 | 0 | int preimage_limit; |
2848 | | |
2849 | | /* |
2850 | | * If we are removing blank lines at the end of img, |
2851 | | * the preimage may extend beyond the end. |
2852 | | * If that is the case, we must be careful only to |
2853 | | * remove the part of the preimage that falls within |
2854 | | * the boundaries of img. Initialize preimage_limit |
2855 | | * to the number of lines in the preimage that falls |
2856 | | * within the boundaries. |
2857 | | */ |
2858 | 0 | preimage_limit = preimage->line_nr; |
2859 | 0 | if (preimage_limit > img->line_nr - applied_pos) |
2860 | 0 | preimage_limit = img->line_nr - applied_pos; |
2861 | |
|
2862 | 0 | for (i = 0; i < applied_pos; i++) |
2863 | 0 | applied_at += img->line[i].len; |
2864 | |
|
2865 | 0 | remove_count = 0; |
2866 | 0 | for (i = 0; i < preimage_limit; i++) |
2867 | 0 | remove_count += img->line[applied_pos + i].len; |
2868 | 0 | insert_count = postimage->buf.len; |
2869 | | |
2870 | | /* Adjust the contents */ |
2871 | 0 | result_alloc = st_add3(st_sub(img->buf.len, remove_count), insert_count, 1); |
2872 | 0 | result = xmalloc(result_alloc); |
2873 | 0 | memcpy(result, img->buf.buf, applied_at); |
2874 | 0 | memcpy(result + applied_at, postimage->buf.buf, postimage->buf.len); |
2875 | 0 | memcpy(result + applied_at + postimage->buf.len, |
2876 | 0 | img->buf.buf + (applied_at + remove_count), |
2877 | 0 | img->buf.len - (applied_at + remove_count)); |
2878 | 0 | strbuf_attach(&img->buf, result, postimage->buf.len + img->buf.len - remove_count, |
2879 | 0 | result_alloc); |
2880 | | |
2881 | | /* Adjust the line table */ |
2882 | 0 | nr = img->line_nr + postimage->line_nr - preimage_limit; |
2883 | 0 | if (preimage_limit < postimage->line_nr) |
2884 | | /* |
2885 | | * NOTE: this knows that we never call image_remove_first_line() |
2886 | | * on anything other than pre/post image. |
2887 | | */ |
2888 | 0 | REALLOC_ARRAY(img->line, nr); |
2889 | 0 | if (preimage_limit != postimage->line_nr) |
2890 | 0 | MOVE_ARRAY(img->line + applied_pos + postimage->line_nr, |
2891 | 0 | img->line + applied_pos + preimage_limit, |
2892 | 0 | img->line_nr - (applied_pos + preimage_limit)); |
2893 | 0 | COPY_ARRAY(img->line + applied_pos, postimage->line, postimage->line_nr); |
2894 | 0 | if (!state->allow_overlap) |
2895 | 0 | for (i = 0; i < postimage->line_nr; i++) |
2896 | 0 | img->line[applied_pos + i].flag |= LINE_PATCHED; |
2897 | 0 | img->line_nr = nr; |
2898 | 0 | } |
2899 | | |
2900 | | /* |
2901 | | * Use the patch-hunk text in "frag" to prepare two images (preimage and |
2902 | | * postimage) for the hunk. Find lines that match "preimage" in "img" and |
2903 | | * replace the part of "img" with "postimage" text. |
2904 | | */ |
2905 | | static int apply_one_fragment(struct apply_state *state, |
2906 | | struct image *img, struct fragment *frag, |
2907 | | int inaccurate_eof, unsigned ws_rule, |
2908 | | int nth_fragment) |
2909 | 0 | { |
2910 | 0 | int match_beginning, match_end; |
2911 | 0 | const char *patch = frag->patch; |
2912 | 0 | int size = frag->size; |
2913 | 0 | char *old, *oldlines; |
2914 | 0 | struct strbuf newlines; |
2915 | 0 | int new_blank_lines_at_end = 0; |
2916 | 0 | int found_new_blank_lines_at_end = 0; |
2917 | 0 | int hunk_linenr = frag->linenr; |
2918 | 0 | unsigned long leading, trailing; |
2919 | 0 | int pos, applied_pos; |
2920 | 0 | struct image preimage = IMAGE_INIT; |
2921 | 0 | struct image postimage = IMAGE_INIT; |
2922 | |
|
2923 | 0 | oldlines = xmalloc(size); |
2924 | 0 | strbuf_init(&newlines, size); |
2925 | |
|
2926 | 0 | old = oldlines; |
2927 | 0 | while (size > 0) { |
2928 | 0 | char first; |
2929 | 0 | int len = linelen(patch, size); |
2930 | 0 | int plen; |
2931 | 0 | int added_blank_line = 0; |
2932 | 0 | int is_blank_context = 0; |
2933 | 0 | size_t start; |
2934 | |
|
2935 | 0 | if (!len) |
2936 | 0 | break; |
2937 | | |
2938 | | /* |
2939 | | * "plen" is how much of the line we should use for |
2940 | | * the actual patch data. Normally we just remove the |
2941 | | * first character on the line, but if the line is |
2942 | | * followed by "\ No newline", then we also remove the |
2943 | | * last one (which is the newline, of course). |
2944 | | */ |
2945 | 0 | plen = len - 1; |
2946 | 0 | if (len < size && patch[len] == '\\') |
2947 | 0 | plen--; |
2948 | 0 | first = *patch; |
2949 | 0 | if (state->apply_in_reverse) { |
2950 | 0 | if (first == '-') |
2951 | 0 | first = '+'; |
2952 | 0 | else if (first == '+') |
2953 | 0 | first = '-'; |
2954 | 0 | } |
2955 | |
|
2956 | 0 | switch (first) { |
2957 | 0 | case '\n': |
2958 | | /* Newer GNU diff, empty context line */ |
2959 | 0 | if (plen < 0) |
2960 | | /* ... followed by '\No newline'; nothing */ |
2961 | 0 | break; |
2962 | 0 | *old++ = '\n'; |
2963 | 0 | strbuf_addch(&newlines, '\n'); |
2964 | 0 | image_add_line(&preimage, "\n", 1, LINE_COMMON); |
2965 | 0 | image_add_line(&postimage, "\n", 1, LINE_COMMON); |
2966 | 0 | is_blank_context = 1; |
2967 | 0 | break; |
2968 | 0 | case ' ': |
2969 | 0 | if (plen && (ws_rule & WS_BLANK_AT_EOF) && |
2970 | 0 | ws_blank_line(patch + 1, plen)) |
2971 | 0 | is_blank_context = 1; |
2972 | | /* fallthrough */ |
2973 | 0 | case '-': |
2974 | 0 | memcpy(old, patch + 1, plen); |
2975 | 0 | image_add_line(&preimage, old, plen, |
2976 | 0 | (first == ' ' ? LINE_COMMON : 0)); |
2977 | 0 | old += plen; |
2978 | 0 | if (first == '-') |
2979 | 0 | break; |
2980 | | /* fallthrough */ |
2981 | 0 | case '+': |
2982 | | /* --no-add does not add new lines */ |
2983 | 0 | if (first == '+' && state->no_add) |
2984 | 0 | break; |
2985 | | |
2986 | 0 | start = newlines.len; |
2987 | 0 | if (first != '+' || |
2988 | 0 | !state->whitespace_error || |
2989 | 0 | state->ws_error_action != correct_ws_error) { |
2990 | 0 | strbuf_add(&newlines, patch + 1, plen); |
2991 | 0 | } |
2992 | 0 | else { |
2993 | 0 | ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws); |
2994 | 0 | } |
2995 | 0 | image_add_line(&postimage, newlines.buf + start, newlines.len - start, |
2996 | 0 | (first == '+' ? 0 : LINE_COMMON)); |
2997 | 0 | if (first == '+' && |
2998 | 0 | (ws_rule & WS_BLANK_AT_EOF) && |
2999 | 0 | ws_blank_line(patch + 1, plen)) |
3000 | 0 | added_blank_line = 1; |
3001 | 0 | break; |
3002 | 0 | case '@': case '\\': |
3003 | | /* Ignore it, we already handled it */ |
3004 | 0 | break; |
3005 | 0 | default: |
3006 | 0 | if (state->apply_verbosity > verbosity_normal) |
3007 | 0 | error(_("invalid start of line: '%c'"), first); |
3008 | 0 | applied_pos = -1; |
3009 | 0 | goto out; |
3010 | 0 | } |
3011 | 0 | if (added_blank_line) { |
3012 | 0 | if (!new_blank_lines_at_end) |
3013 | 0 | found_new_blank_lines_at_end = hunk_linenr; |
3014 | 0 | new_blank_lines_at_end++; |
3015 | 0 | } |
3016 | 0 | else if (is_blank_context) |
3017 | 0 | ; |
3018 | 0 | else |
3019 | 0 | new_blank_lines_at_end = 0; |
3020 | 0 | patch += len; |
3021 | 0 | size -= len; |
3022 | 0 | hunk_linenr++; |
3023 | 0 | } |
3024 | 0 | if (inaccurate_eof && |
3025 | 0 | old > oldlines && old[-1] == '\n' && |
3026 | 0 | newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') { |
3027 | 0 | old--; |
3028 | 0 | strbuf_setlen(&newlines, newlines.len - 1); |
3029 | 0 | preimage.line[preimage.line_nr - 1].len--; |
3030 | 0 | postimage.line[postimage.line_nr - 1].len--; |
3031 | 0 | } |
3032 | |
|
3033 | 0 | leading = frag->leading; |
3034 | 0 | trailing = frag->trailing; |
3035 | | |
3036 | | /* |
3037 | | * A hunk to change lines at the beginning would begin with |
3038 | | * @@ -1,L +N,M @@ |
3039 | | * but we need to be careful. -U0 that inserts before the second |
3040 | | * line also has this pattern. |
3041 | | * |
3042 | | * And a hunk to add to an empty file would begin with |
3043 | | * @@ -0,0 +N,M @@ |
3044 | | * |
3045 | | * In other words, a hunk that is (frag->oldpos <= 1) with or |
3046 | | * without leading context must match at the beginning. |
3047 | | */ |
3048 | 0 | match_beginning = (!frag->oldpos || |
3049 | 0 | (frag->oldpos == 1 && !state->unidiff_zero)); |
3050 | | |
3051 | | /* |
3052 | | * A hunk without trailing lines must match at the end. |
3053 | | * However, we simply cannot tell if a hunk must match end |
3054 | | * from the lack of trailing lines if the patch was generated |
3055 | | * with unidiff without any context. |
3056 | | */ |
3057 | 0 | match_end = !state->unidiff_zero && !trailing; |
3058 | |
|
3059 | 0 | pos = frag->newpos ? (frag->newpos - 1) : 0; |
3060 | 0 | strbuf_add(&preimage.buf, oldlines, old - oldlines); |
3061 | 0 | strbuf_swap(&postimage.buf, &newlines); |
3062 | |
|
3063 | 0 | for (;;) { |
3064 | |
|
3065 | 0 | applied_pos = find_pos(state, img, &preimage, &postimage, pos, |
3066 | 0 | ws_rule, match_beginning, match_end); |
3067 | |
|
3068 | 0 | if (applied_pos >= 0) |
3069 | 0 | break; |
3070 | | |
3071 | | /* Am I at my context limits? */ |
3072 | 0 | if ((leading <= state->p_context) && (trailing <= state->p_context)) |
3073 | 0 | break; |
3074 | 0 | if (match_beginning || match_end) { |
3075 | 0 | match_beginning = match_end = 0; |
3076 | 0 | continue; |
3077 | 0 | } |
3078 | | |
3079 | | /* |
3080 | | * Reduce the number of context lines; reduce both |
3081 | | * leading and trailing if they are equal otherwise |
3082 | | * just reduce the larger context. |
3083 | | */ |
3084 | 0 | if (leading >= trailing) { |
3085 | 0 | image_remove_first_line(&preimage); |
3086 | 0 | image_remove_first_line(&postimage); |
3087 | 0 | pos--; |
3088 | 0 | leading--; |
3089 | 0 | } |
3090 | 0 | if (trailing > leading) { |
3091 | 0 | image_remove_last_line(&preimage); |
3092 | 0 | image_remove_last_line(&postimage); |
3093 | 0 | trailing--; |
3094 | 0 | } |
3095 | 0 | } |
3096 | |
|
3097 | 0 | if (applied_pos >= 0) { |
3098 | 0 | if (new_blank_lines_at_end && |
3099 | 0 | preimage.line_nr + applied_pos >= img->line_nr && |
3100 | 0 | (ws_rule & WS_BLANK_AT_EOF) && |
3101 | 0 | state->ws_error_action != nowarn_ws_error) { |
3102 | 0 | record_ws_error(state, WS_BLANK_AT_EOF, "+", 1, |
3103 | 0 | found_new_blank_lines_at_end); |
3104 | 0 | if (state->ws_error_action == correct_ws_error) { |
3105 | 0 | while (new_blank_lines_at_end--) |
3106 | 0 | image_remove_last_line(&postimage); |
3107 | 0 | } |
3108 | | /* |
3109 | | * We would want to prevent write_out_results() |
3110 | | * from taking place in apply_patch() that follows |
3111 | | * the callchain led us here, which is: |
3112 | | * apply_patch->check_patch_list->check_patch-> |
3113 | | * apply_data->apply_fragments->apply_one_fragment |
3114 | | */ |
3115 | 0 | if (state->ws_error_action == die_on_ws_error) |
3116 | 0 | state->apply = 0; |
3117 | 0 | } |
3118 | |
|
3119 | 0 | if (state->apply_verbosity > verbosity_normal && applied_pos != pos) { |
3120 | 0 | int offset = applied_pos - pos; |
3121 | 0 | if (state->apply_in_reverse) |
3122 | 0 | offset = 0 - offset; |
3123 | 0 | fprintf_ln(stderr, |
3124 | 0 | Q_("Hunk #%d succeeded at %d (offset %d line).", |
3125 | 0 | "Hunk #%d succeeded at %d (offset %d lines).", |
3126 | 0 | offset), |
3127 | 0 | nth_fragment, applied_pos + 1, offset); |
3128 | 0 | } |
3129 | | |
3130 | | /* |
3131 | | * Warn if it was necessary to reduce the number |
3132 | | * of context lines. |
3133 | | */ |
3134 | 0 | if ((leading != frag->leading || |
3135 | 0 | trailing != frag->trailing) && state->apply_verbosity > verbosity_silent) |
3136 | 0 | fprintf_ln(stderr, _("Context reduced to (%ld/%ld)" |
3137 | 0 | " to apply fragment at %d"), |
3138 | 0 | leading, trailing, applied_pos+1); |
3139 | 0 | update_image(state, img, applied_pos, &preimage, &postimage); |
3140 | 0 | } else { |
3141 | 0 | if (state->apply_verbosity > verbosity_normal) |
3142 | 0 | error(_("while searching for:\n%.*s"), |
3143 | 0 | (int)(old - oldlines), oldlines); |
3144 | 0 | } |
3145 | |
|
3146 | 0 | out: |
3147 | 0 | free(oldlines); |
3148 | 0 | strbuf_release(&newlines); |
3149 | 0 | image_clear(&preimage); |
3150 | 0 | image_clear(&postimage); |
3151 | |
|
3152 | 0 | return (applied_pos < 0); |
3153 | 0 | } |
3154 | | |
3155 | | static int apply_binary_fragment(struct apply_state *state, |
3156 | | struct image *img, |
3157 | | struct patch *patch) |
3158 | 0 | { |
3159 | 0 | struct fragment *fragment = patch->fragments; |
3160 | 0 | unsigned long len; |
3161 | 0 | void *dst; |
3162 | |
|
3163 | 0 | if (!fragment) |
3164 | 0 | return error(_("missing binary patch data for '%s'"), |
3165 | 0 | patch->new_name ? |
3166 | 0 | patch->new_name : |
3167 | 0 | patch->old_name); |
3168 | | |
3169 | | /* Binary patch is irreversible without the optional second hunk */ |
3170 | 0 | if (state->apply_in_reverse) { |
3171 | 0 | if (!fragment->next) |
3172 | 0 | return error(_("cannot reverse-apply a binary patch " |
3173 | 0 | "without the reverse hunk to '%s'"), |
3174 | 0 | patch->new_name |
3175 | 0 | ? patch->new_name : patch->old_name); |
3176 | 0 | fragment = fragment->next; |
3177 | 0 | } |
3178 | 0 | switch (fragment->binary_patch_method) { |
3179 | 0 | case BINARY_DELTA_DEFLATED: |
3180 | 0 | dst = patch_delta(img->buf.buf, img->buf.len, fragment->patch, |
3181 | 0 | fragment->size, &len); |
3182 | 0 | if (!dst) |
3183 | 0 | return -1; |
3184 | 0 | image_clear(img); |
3185 | 0 | strbuf_attach(&img->buf, dst, len, len + 1); |
3186 | 0 | return 0; |
3187 | 0 | case BINARY_LITERAL_DEFLATED: |
3188 | 0 | image_clear(img); |
3189 | 0 | strbuf_add(&img->buf, fragment->patch, fragment->size); |
3190 | 0 | return 0; |
3191 | 0 | } |
3192 | 0 | return -1; |
3193 | 0 | } |
3194 | | |
3195 | | /* |
3196 | | * Replace "img" with the result of applying the binary patch. |
3197 | | * The binary patch data itself in patch->fragment is still kept |
3198 | | * but the preimage prepared by the caller in "img" is freed here |
3199 | | * or in the helper function apply_binary_fragment() this calls. |
3200 | | */ |
3201 | | static int apply_binary(struct apply_state *state, |
3202 | | struct image *img, |
3203 | | struct patch *patch) |
3204 | 0 | { |
3205 | 0 | const char *name = patch->old_name ? patch->old_name : patch->new_name; |
3206 | 0 | struct object_id oid; |
3207 | 0 | const unsigned hexsz = the_hash_algo->hexsz; |
3208 | | |
3209 | | /* |
3210 | | * For safety, we require patch index line to contain |
3211 | | * full hex textual object ID for old and new, at least for now. |
3212 | | */ |
3213 | 0 | if (strlen(patch->old_oid_prefix) != hexsz || |
3214 | 0 | strlen(patch->new_oid_prefix) != hexsz || |
3215 | 0 | get_oid_hex(patch->old_oid_prefix, &oid) || |
3216 | 0 | get_oid_hex(patch->new_oid_prefix, &oid)) |
3217 | 0 | return error(_("cannot apply binary patch to '%s' " |
3218 | 0 | "without full index line"), name); |
3219 | | |
3220 | 0 | if (patch->old_name) { |
3221 | | /* |
3222 | | * See if the old one matches what the patch |
3223 | | * applies to. |
3224 | | */ |
3225 | 0 | hash_object_file(the_hash_algo, img->buf.buf, img->buf.len, |
3226 | 0 | OBJ_BLOB, &oid); |
3227 | 0 | if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix)) |
3228 | 0 | return error(_("the patch applies to '%s' (%s), " |
3229 | 0 | "which does not match the " |
3230 | 0 | "current contents."), |
3231 | 0 | name, oid_to_hex(&oid)); |
3232 | 0 | } |
3233 | 0 | else { |
3234 | | /* Otherwise, the old one must be empty. */ |
3235 | 0 | if (img->buf.len) |
3236 | 0 | return error(_("the patch applies to an empty " |
3237 | 0 | "'%s' but it is not empty"), name); |
3238 | 0 | } |
3239 | | |
3240 | 0 | get_oid_hex(patch->new_oid_prefix, &oid); |
3241 | 0 | if (is_null_oid(&oid)) { |
3242 | 0 | image_clear(img); |
3243 | 0 | return 0; /* deletion patch */ |
3244 | 0 | } |
3245 | | |
3246 | 0 | if (odb_has_object(the_repository->objects, &oid, 0)) { |
3247 | | /* We already have the postimage */ |
3248 | 0 | enum object_type type; |
3249 | 0 | unsigned long size; |
3250 | 0 | char *result; |
3251 | |
|
3252 | 0 | result = odb_read_object(the_repository->objects, &oid, |
3253 | 0 | &type, &size); |
3254 | 0 | if (!result) |
3255 | 0 | return error(_("the necessary postimage %s for " |
3256 | 0 | "'%s' cannot be read"), |
3257 | 0 | patch->new_oid_prefix, name); |
3258 | 0 | image_clear(img); |
3259 | 0 | strbuf_attach(&img->buf, result, size, size + 1); |
3260 | 0 | } else { |
3261 | | /* |
3262 | | * We have verified buf matches the preimage; |
3263 | | * apply the patch data to it, which is stored |
3264 | | * in the patch->fragments->{patch,size}. |
3265 | | */ |
3266 | 0 | if (apply_binary_fragment(state, img, patch)) |
3267 | 0 | return error(_("binary patch does not apply to '%s'"), |
3268 | 0 | name); |
3269 | | |
3270 | | /* verify that the result matches */ |
3271 | 0 | hash_object_file(the_hash_algo, img->buf.buf, img->buf.len, OBJ_BLOB, |
3272 | 0 | &oid); |
3273 | 0 | if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix)) |
3274 | 0 | return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"), |
3275 | 0 | name, patch->new_oid_prefix, oid_to_hex(&oid)); |
3276 | 0 | } |
3277 | | |
3278 | 0 | return 0; |
3279 | 0 | } |
3280 | | |
3281 | | static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch) |
3282 | 0 | { |
3283 | 0 | struct fragment *frag = patch->fragments; |
3284 | 0 | const char *name = patch->old_name ? patch->old_name : patch->new_name; |
3285 | 0 | unsigned ws_rule = patch->ws_rule; |
3286 | 0 | unsigned inaccurate_eof = patch->inaccurate_eof; |
3287 | 0 | int nth = 0; |
3288 | |
|
3289 | 0 | if (patch->is_binary) |
3290 | 0 | return apply_binary(state, img, patch); |
3291 | | |
3292 | 0 | while (frag) { |
3293 | 0 | nth++; |
3294 | 0 | if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) { |
3295 | 0 | error(_("patch failed: %s:%ld"), name, frag->oldpos); |
3296 | 0 | if (!state->apply_with_reject) |
3297 | 0 | return -1; |
3298 | 0 | frag->rejected = 1; |
3299 | 0 | } |
3300 | 0 | frag = frag->next; |
3301 | 0 | } |
3302 | 0 | return 0; |
3303 | 0 | } |
3304 | | |
3305 | | static int read_blob_object(struct strbuf *buf, const struct object_id *oid, unsigned mode) |
3306 | 0 | { |
3307 | 0 | if (S_ISGITLINK(mode)) { |
3308 | 0 | strbuf_grow(buf, 100); |
3309 | 0 | strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid)); |
3310 | 0 | } else { |
3311 | 0 | enum object_type type; |
3312 | 0 | unsigned long sz; |
3313 | 0 | char *result; |
3314 | |
|
3315 | 0 | result = odb_read_object(the_repository->objects, oid, |
3316 | 0 | &type, &sz); |
3317 | 0 | if (!result) |
3318 | 0 | return -1; |
3319 | | /* XXX read_sha1_file NUL-terminates */ |
3320 | 0 | strbuf_attach(buf, result, sz, sz + 1); |
3321 | 0 | } |
3322 | 0 | return 0; |
3323 | 0 | } |
3324 | | |
3325 | | static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf) |
3326 | 0 | { |
3327 | 0 | if (!ce) |
3328 | 0 | return 0; |
3329 | 0 | return read_blob_object(buf, &ce->oid, ce->ce_mode); |
3330 | 0 | } |
3331 | | |
3332 | | static struct patch *in_fn_table(struct apply_state *state, const char *name) |
3333 | 0 | { |
3334 | 0 | struct string_list_item *item; |
3335 | |
|
3336 | 0 | if (!name) |
3337 | 0 | return NULL; |
3338 | | |
3339 | 0 | item = string_list_lookup(&state->fn_table, name); |
3340 | 0 | if (item) |
3341 | 0 | return (struct patch *)item->util; |
3342 | | |
3343 | 0 | return NULL; |
3344 | 0 | } |
3345 | | |
3346 | | /* |
3347 | | * item->util in the filename table records the status of the path. |
3348 | | * Usually it points at a patch (whose result records the contents |
3349 | | * of it after applying it), but it could be PATH_WAS_DELETED for a |
3350 | | * path that a previously applied patch has already removed, or |
3351 | | * PATH_TO_BE_DELETED for a path that a later patch would remove. |
3352 | | * |
3353 | | * The latter is needed to deal with a case where two paths A and B |
3354 | | * are swapped by first renaming A to B and then renaming B to A; |
3355 | | * moving A to B should not be prevented due to presence of B as we |
3356 | | * will remove it in a later patch. |
3357 | | */ |
3358 | 0 | #define PATH_TO_BE_DELETED ((struct patch *) -2) |
3359 | 0 | #define PATH_WAS_DELETED ((struct patch *) -1) |
3360 | | |
3361 | | static int to_be_deleted(struct patch *patch) |
3362 | 0 | { |
3363 | 0 | return patch == PATH_TO_BE_DELETED; |
3364 | 0 | } |
3365 | | |
3366 | | static int was_deleted(struct patch *patch) |
3367 | 0 | { |
3368 | 0 | return patch == PATH_WAS_DELETED; |
3369 | 0 | } |
3370 | | |
3371 | | static void add_to_fn_table(struct apply_state *state, struct patch *patch) |
3372 | 0 | { |
3373 | 0 | struct string_list_item *item; |
3374 | | |
3375 | | /* |
3376 | | * Always add new_name unless patch is a deletion |
3377 | | * This should cover the cases for normal diffs, |
3378 | | * file creations and copies |
3379 | | */ |
3380 | 0 | if (patch->new_name) { |
3381 | 0 | item = string_list_insert(&state->fn_table, patch->new_name); |
3382 | 0 | item->util = patch; |
3383 | 0 | } |
3384 | | |
3385 | | /* |
3386 | | * store a failure on rename/deletion cases because |
3387 | | * later chunks shouldn't patch old names |
3388 | | */ |
3389 | 0 | if ((patch->new_name == NULL) || (patch->is_rename)) { |
3390 | 0 | item = string_list_insert(&state->fn_table, patch->old_name); |
3391 | 0 | item->util = PATH_WAS_DELETED; |
3392 | 0 | } |
3393 | 0 | } |
3394 | | |
3395 | | static void prepare_fn_table(struct apply_state *state, struct patch *patch) |
3396 | 0 | { |
3397 | | /* |
3398 | | * store information about incoming file deletion |
3399 | | */ |
3400 | 0 | while (patch) { |
3401 | 0 | if ((patch->new_name == NULL) || (patch->is_rename)) { |
3402 | 0 | struct string_list_item *item; |
3403 | 0 | item = string_list_insert(&state->fn_table, patch->old_name); |
3404 | 0 | item->util = PATH_TO_BE_DELETED; |
3405 | 0 | } |
3406 | 0 | patch = patch->next; |
3407 | 0 | } |
3408 | 0 | } |
3409 | | |
3410 | | static int checkout_target(struct index_state *istate, |
3411 | | struct cache_entry *ce, struct stat *st) |
3412 | 0 | { |
3413 | 0 | struct checkout costate = CHECKOUT_INIT; |
3414 | |
|
3415 | 0 | costate.refresh_cache = 1; |
3416 | 0 | costate.istate = istate; |
3417 | 0 | if (checkout_entry(ce, &costate, NULL, NULL) || |
3418 | 0 | lstat(ce->name, st)) |
3419 | 0 | return error(_("cannot checkout %s"), ce->name); |
3420 | 0 | return 0; |
3421 | 0 | } |
3422 | | |
3423 | | static struct patch *previous_patch(struct apply_state *state, |
3424 | | struct patch *patch, |
3425 | | int *gone) |
3426 | 0 | { |
3427 | 0 | struct patch *previous; |
3428 | |
|
3429 | 0 | *gone = 0; |
3430 | 0 | if (patch->is_copy || patch->is_rename) |
3431 | 0 | return NULL; /* "git" patches do not depend on the order */ |
3432 | | |
3433 | 0 | previous = in_fn_table(state, patch->old_name); |
3434 | 0 | if (!previous) |
3435 | 0 | return NULL; |
3436 | | |
3437 | 0 | if (to_be_deleted(previous)) |
3438 | 0 | return NULL; /* the deletion hasn't happened yet */ |
3439 | | |
3440 | 0 | if (was_deleted(previous)) |
3441 | 0 | *gone = 1; |
3442 | |
|
3443 | 0 | return previous; |
3444 | 0 | } |
3445 | | |
3446 | | static int verify_index_match(struct apply_state *state, |
3447 | | const struct cache_entry *ce, |
3448 | | struct stat *st) |
3449 | 0 | { |
3450 | 0 | if (S_ISGITLINK(ce->ce_mode)) { |
3451 | 0 | if (!S_ISDIR(st->st_mode)) |
3452 | 0 | return -1; |
3453 | 0 | return 0; |
3454 | 0 | } |
3455 | 0 | return ie_match_stat(state->repo->index, ce, st, |
3456 | 0 | CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE); |
3457 | 0 | } |
3458 | | |
3459 | 0 | #define SUBMODULE_PATCH_WITHOUT_INDEX 1 |
3460 | | |
3461 | | static int load_patch_target(struct apply_state *state, |
3462 | | struct strbuf *buf, |
3463 | | const struct cache_entry *ce, |
3464 | | struct stat *st, |
3465 | | struct patch *patch, |
3466 | | const char *name, |
3467 | | unsigned expected_mode) |
3468 | 0 | { |
3469 | 0 | if (state->cached || state->check_index) { |
3470 | 0 | if (read_file_or_gitlink(ce, buf)) |
3471 | 0 | return error(_("failed to read %s"), name); |
3472 | 0 | } else if (name) { |
3473 | 0 | if (S_ISGITLINK(expected_mode)) { |
3474 | 0 | if (ce) |
3475 | 0 | return read_file_or_gitlink(ce, buf); |
3476 | 0 | else |
3477 | 0 | return SUBMODULE_PATCH_WITHOUT_INDEX; |
3478 | 0 | } else if (has_symlink_leading_path(name, strlen(name))) { |
3479 | 0 | return error(_("reading from '%s' beyond a symbolic link"), name); |
3480 | 0 | } else { |
3481 | 0 | if (read_old_data(st, patch, name, buf)) |
3482 | 0 | return error(_("failed to read %s"), name); |
3483 | 0 | } |
3484 | 0 | } |
3485 | 0 | return 0; |
3486 | 0 | } |
3487 | | |
3488 | | /* |
3489 | | * We are about to apply "patch"; populate the "image" with the |
3490 | | * current version we have, from the working tree or from the index, |
3491 | | * depending on the situation e.g. --cached/--index. If we are |
3492 | | * applying a non-git patch that incrementally updates the tree, |
3493 | | * we read from the result of a previous diff. |
3494 | | */ |
3495 | | static int load_preimage(struct apply_state *state, |
3496 | | struct image *image, |
3497 | | struct patch *patch, struct stat *st, |
3498 | | const struct cache_entry *ce) |
3499 | 0 | { |
3500 | 0 | struct strbuf buf = STRBUF_INIT; |
3501 | 0 | size_t len; |
3502 | 0 | char *img; |
3503 | 0 | struct patch *previous; |
3504 | 0 | int status; |
3505 | |
|
3506 | 0 | previous = previous_patch(state, patch, &status); |
3507 | 0 | if (status) |
3508 | 0 | return error(_("path %s has been renamed/deleted"), |
3509 | 0 | patch->old_name); |
3510 | 0 | if (previous) { |
3511 | | /* We have a patched copy in memory; use that. */ |
3512 | 0 | strbuf_add(&buf, previous->result, previous->resultsize); |
3513 | 0 | } else { |
3514 | 0 | status = load_patch_target(state, &buf, ce, st, patch, |
3515 | 0 | patch->old_name, patch->old_mode); |
3516 | 0 | if (status < 0) |
3517 | 0 | return status; |
3518 | 0 | else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) { |
3519 | | /* |
3520 | | * There is no way to apply subproject |
3521 | | * patch without looking at the index. |
3522 | | * NEEDSWORK: shouldn't this be flagged |
3523 | | * as an error??? |
3524 | | */ |
3525 | 0 | free_fragment_list(patch->fragments); |
3526 | 0 | patch->fragments = NULL; |
3527 | 0 | } else if (status) { |
3528 | 0 | return error(_("failed to read %s"), patch->old_name); |
3529 | 0 | } |
3530 | 0 | } |
3531 | | |
3532 | 0 | img = strbuf_detach(&buf, &len); |
3533 | 0 | image_prepare(image, img, len, !patch->is_binary); |
3534 | 0 | return 0; |
3535 | 0 | } |
3536 | | |
3537 | | static int resolve_to(struct image *image, const struct object_id *result_id) |
3538 | 0 | { |
3539 | 0 | unsigned long size; |
3540 | 0 | enum object_type type; |
3541 | 0 | char *data; |
3542 | |
|
3543 | 0 | image_clear(image); |
3544 | |
|
3545 | 0 | data = odb_read_object(the_repository->objects, result_id, &type, &size); |
3546 | 0 | if (!data || type != OBJ_BLOB) |
3547 | 0 | die("unable to read blob object %s", oid_to_hex(result_id)); |
3548 | 0 | strbuf_attach(&image->buf, data, size, size + 1); |
3549 | |
|
3550 | 0 | return 0; |
3551 | 0 | } |
3552 | | |
3553 | | static int three_way_merge(struct apply_state *state, |
3554 | | struct image *image, |
3555 | | char *path, |
3556 | | const struct object_id *base, |
3557 | | const struct object_id *ours, |
3558 | | const struct object_id *theirs) |
3559 | 0 | { |
3560 | 0 | mmfile_t base_file, our_file, their_file; |
3561 | 0 | struct ll_merge_options merge_opts = LL_MERGE_OPTIONS_INIT; |
3562 | 0 | mmbuffer_t result = { NULL }; |
3563 | 0 | enum ll_merge_result status; |
3564 | | |
3565 | | /* resolve trivial cases first */ |
3566 | 0 | if (oideq(base, ours)) |
3567 | 0 | return resolve_to(image, theirs); |
3568 | 0 | else if (oideq(base, theirs) || oideq(ours, theirs)) |
3569 | 0 | return resolve_to(image, ours); |
3570 | | |
3571 | 0 | read_mmblob(&base_file, base); |
3572 | 0 | read_mmblob(&our_file, ours); |
3573 | 0 | read_mmblob(&their_file, theirs); |
3574 | 0 | merge_opts.variant = state->merge_variant; |
3575 | 0 | status = ll_merge(&result, path, |
3576 | 0 | &base_file, "base", |
3577 | 0 | &our_file, "ours", |
3578 | 0 | &their_file, "theirs", |
3579 | 0 | state->repo->index, |
3580 | 0 | &merge_opts); |
3581 | 0 | if (status == LL_MERGE_BINARY_CONFLICT) |
3582 | 0 | warning("Cannot merge binary files: %s (%s vs. %s)", |
3583 | 0 | path, "ours", "theirs"); |
3584 | 0 | free(base_file.ptr); |
3585 | 0 | free(our_file.ptr); |
3586 | 0 | free(their_file.ptr); |
3587 | 0 | if (status < 0 || !result.ptr) { |
3588 | 0 | free(result.ptr); |
3589 | 0 | return -1; |
3590 | 0 | } |
3591 | 0 | image_clear(image); |
3592 | 0 | strbuf_attach(&image->buf, result.ptr, result.size, result.size); |
3593 | |
|
3594 | 0 | return status; |
3595 | 0 | } |
3596 | | |
3597 | | /* |
3598 | | * When directly falling back to add/add three-way merge, we read from |
3599 | | * the current contents of the new_name. In no cases other than that |
3600 | | * this function will be called. |
3601 | | */ |
3602 | | static int load_current(struct apply_state *state, |
3603 | | struct image *image, |
3604 | | struct patch *patch) |
3605 | 0 | { |
3606 | 0 | struct strbuf buf = STRBUF_INIT; |
3607 | 0 | int status, pos; |
3608 | 0 | size_t len; |
3609 | 0 | char *img; |
3610 | 0 | struct stat st; |
3611 | 0 | struct cache_entry *ce; |
3612 | 0 | char *name = patch->new_name; |
3613 | 0 | unsigned mode = patch->new_mode; |
3614 | |
|
3615 | 0 | if (!patch->is_new) |
3616 | 0 | BUG("patch to %s is not a creation", patch->old_name); |
3617 | | |
3618 | 0 | pos = index_name_pos(state->repo->index, name, strlen(name)); |
3619 | 0 | if (pos < 0) |
3620 | 0 | return error(_("%s: does not exist in index"), name); |
3621 | 0 | ce = state->repo->index->cache[pos]; |
3622 | 0 | if (lstat(name, &st)) { |
3623 | 0 | if (errno != ENOENT) |
3624 | 0 | return error_errno("%s", name); |
3625 | 0 | if (checkout_target(state->repo->index, ce, &st)) |
3626 | 0 | return -1; |
3627 | 0 | } |
3628 | 0 | if (verify_index_match(state, ce, &st)) |
3629 | 0 | return error(_("%s: does not match index"), name); |
3630 | | |
3631 | 0 | status = load_patch_target(state, &buf, ce, &st, patch, name, mode); |
3632 | 0 | if (status < 0) |
3633 | 0 | return status; |
3634 | 0 | else if (status) |
3635 | 0 | return -1; |
3636 | 0 | img = strbuf_detach(&buf, &len); |
3637 | 0 | image_prepare(image, img, len, !patch->is_binary); |
3638 | 0 | return 0; |
3639 | 0 | } |
3640 | | |
3641 | | static int try_threeway(struct apply_state *state, |
3642 | | struct image *image, |
3643 | | struct patch *patch, |
3644 | | struct stat *st, |
3645 | | const struct cache_entry *ce) |
3646 | 0 | { |
3647 | 0 | struct object_id pre_oid, post_oid, our_oid; |
3648 | 0 | struct strbuf buf = STRBUF_INIT; |
3649 | 0 | size_t len; |
3650 | 0 | int status; |
3651 | 0 | char *img; |
3652 | 0 | struct image tmp_image = IMAGE_INIT; |
3653 | | |
3654 | | /* No point falling back to 3-way merge in these cases */ |
3655 | 0 | if (patch->is_delete || |
3656 | 0 | S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode) || |
3657 | 0 | (patch->is_new && !patch->direct_to_threeway) || |
3658 | 0 | (patch->is_rename && !patch->lines_added && !patch->lines_deleted)) |
3659 | 0 | return -1; |
3660 | | |
3661 | | /* Preimage the patch was prepared for */ |
3662 | 0 | if (patch->is_new) |
3663 | 0 | odb_write_object(the_repository->objects, "", 0, OBJ_BLOB, &pre_oid); |
3664 | 0 | else if (repo_get_oid(the_repository, patch->old_oid_prefix, &pre_oid) || |
3665 | 0 | read_blob_object(&buf, &pre_oid, patch->old_mode)) |
3666 | 0 | return error(_("repository lacks the necessary blob to perform 3-way merge.")); |
3667 | | |
3668 | 0 | if (state->apply_verbosity > verbosity_silent && patch->direct_to_threeway) |
3669 | 0 | fprintf(stderr, _("Performing three-way merge...\n")); |
3670 | |
|
3671 | 0 | img = strbuf_detach(&buf, &len); |
3672 | 0 | image_prepare(&tmp_image, img, len, 1); |
3673 | | /* Apply the patch to get the post image */ |
3674 | 0 | if (apply_fragments(state, &tmp_image, patch) < 0) { |
3675 | 0 | image_clear(&tmp_image); |
3676 | 0 | return -1; |
3677 | 0 | } |
3678 | | /* post_oid is theirs */ |
3679 | 0 | odb_write_object(the_repository->objects, tmp_image.buf.buf, |
3680 | 0 | tmp_image.buf.len, OBJ_BLOB, &post_oid); |
3681 | 0 | image_clear(&tmp_image); |
3682 | | |
3683 | | /* our_oid is ours */ |
3684 | 0 | if (patch->is_new) { |
3685 | 0 | if (load_current(state, &tmp_image, patch)) |
3686 | 0 | return error(_("cannot read the current contents of '%s'"), |
3687 | 0 | patch->new_name); |
3688 | 0 | } else { |
3689 | 0 | if (load_preimage(state, &tmp_image, patch, st, ce)) |
3690 | 0 | return error(_("cannot read the current contents of '%s'"), |
3691 | 0 | patch->old_name); |
3692 | 0 | } |
3693 | 0 | odb_write_object(the_repository->objects, tmp_image.buf.buf, |
3694 | 0 | tmp_image.buf.len, OBJ_BLOB, &our_oid); |
3695 | 0 | image_clear(&tmp_image); |
3696 | | |
3697 | | /* in-core three-way merge between post and our using pre as base */ |
3698 | 0 | status = three_way_merge(state, image, patch->new_name, |
3699 | 0 | &pre_oid, &our_oid, &post_oid); |
3700 | 0 | if (status < 0) { |
3701 | 0 | if (state->apply_verbosity > verbosity_silent) |
3702 | 0 | fprintf(stderr, |
3703 | 0 | _("Failed to perform three-way merge...\n")); |
3704 | 0 | return status; |
3705 | 0 | } |
3706 | | |
3707 | 0 | if (status) { |
3708 | 0 | patch->conflicted_threeway = 1; |
3709 | 0 | if (patch->is_new) |
3710 | 0 | oidclr(&patch->threeway_stage[0], the_repository->hash_algo); |
3711 | 0 | else |
3712 | 0 | oidcpy(&patch->threeway_stage[0], &pre_oid); |
3713 | 0 | oidcpy(&patch->threeway_stage[1], &our_oid); |
3714 | 0 | oidcpy(&patch->threeway_stage[2], &post_oid); |
3715 | 0 | if (state->apply_verbosity > verbosity_silent) |
3716 | 0 | fprintf(stderr, |
3717 | 0 | _("Applied patch to '%s' with conflicts.\n"), |
3718 | 0 | patch->new_name); |
3719 | 0 | } else { |
3720 | 0 | if (state->apply_verbosity > verbosity_silent) |
3721 | 0 | fprintf(stderr, |
3722 | 0 | _("Applied patch to '%s' cleanly.\n"), |
3723 | 0 | patch->new_name); |
3724 | 0 | } |
3725 | 0 | return 0; |
3726 | 0 | } |
3727 | | |
3728 | | static int apply_data(struct apply_state *state, struct patch *patch, |
3729 | | struct stat *st, const struct cache_entry *ce) |
3730 | 0 | { |
3731 | 0 | struct image image = IMAGE_INIT; |
3732 | |
|
3733 | 0 | if (load_preimage(state, &image, patch, st, ce) < 0) |
3734 | 0 | return -1; |
3735 | | |
3736 | 0 | if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0) { |
3737 | 0 | if (state->apply_verbosity > verbosity_silent && |
3738 | 0 | state->threeway && !patch->direct_to_threeway) |
3739 | 0 | fprintf(stderr, _("Falling back to direct application...\n")); |
3740 | | |
3741 | | /* Note: with --reject, apply_fragments() returns 0 */ |
3742 | 0 | if (patch->direct_to_threeway || apply_fragments(state, &image, patch) < 0) { |
3743 | 0 | image_clear(&image); |
3744 | 0 | return -1; |
3745 | 0 | } |
3746 | 0 | } |
3747 | 0 | patch->result = strbuf_detach(&image.buf, &patch->resultsize); |
3748 | 0 | add_to_fn_table(state, patch); |
3749 | 0 | free(image.line); |
3750 | |
|
3751 | 0 | if (0 < patch->is_delete && patch->resultsize) |
3752 | 0 | return error(_("removal patch leaves file contents")); |
3753 | | |
3754 | 0 | return 0; |
3755 | 0 | } |
3756 | | |
3757 | | /* |
3758 | | * If "patch" that we are looking at modifies or deletes what we have, |
3759 | | * we would want it not to lose any local modification we have, either |
3760 | | * in the working tree or in the index. |
3761 | | * |
3762 | | * This also decides if a non-git patch is a creation patch or a |
3763 | | * modification to an existing empty file. We do not check the state |
3764 | | * of the current tree for a creation patch in this function; the caller |
3765 | | * check_patch() separately makes sure (and errors out otherwise) that |
3766 | | * the path the patch creates does not exist in the current tree. |
3767 | | */ |
3768 | | static int check_preimage(struct apply_state *state, |
3769 | | struct patch *patch, |
3770 | | struct cache_entry **ce, |
3771 | | struct stat *st) |
3772 | 0 | { |
3773 | 0 | const char *old_name = patch->old_name; |
3774 | 0 | struct patch *previous = NULL; |
3775 | 0 | int stat_ret = 0, status; |
3776 | 0 | unsigned st_mode = 0; |
3777 | |
|
3778 | 0 | if (!old_name) |
3779 | 0 | return 0; |
3780 | | |
3781 | 0 | assert(patch->is_new <= 0); |
3782 | 0 | previous = previous_patch(state, patch, &status); |
3783 | |
|
3784 | 0 | if (status) |
3785 | 0 | return error(_("path %s has been renamed/deleted"), old_name); |
3786 | 0 | if (previous) { |
3787 | 0 | st_mode = previous->new_mode; |
3788 | 0 | } else if (!state->cached) { |
3789 | 0 | stat_ret = lstat(old_name, st); |
3790 | 0 | if (stat_ret && errno != ENOENT) |
3791 | 0 | return error_errno("%s", old_name); |
3792 | 0 | } |
3793 | | |
3794 | 0 | if (state->check_index && !previous) { |
3795 | 0 | int pos = index_name_pos(state->repo->index, old_name, |
3796 | 0 | strlen(old_name)); |
3797 | 0 | if (pos < 0) { |
3798 | 0 | if (patch->is_new < 0) |
3799 | 0 | goto is_new; |
3800 | 0 | return error(_("%s: does not exist in index"), old_name); |
3801 | 0 | } |
3802 | 0 | *ce = state->repo->index->cache[pos]; |
3803 | 0 | if (stat_ret < 0) { |
3804 | 0 | if (checkout_target(state->repo->index, *ce, st)) |
3805 | 0 | return -1; |
3806 | 0 | } |
3807 | 0 | if (!state->cached && verify_index_match(state, *ce, st)) |
3808 | 0 | return error(_("%s: does not match index"), old_name); |
3809 | 0 | if (state->cached) |
3810 | 0 | st_mode = (*ce)->ce_mode; |
3811 | 0 | } else if (stat_ret < 0) { |
3812 | 0 | if (patch->is_new < 0) |
3813 | 0 | goto is_new; |
3814 | 0 | return error_errno("%s", old_name); |
3815 | 0 | } |
3816 | | |
3817 | 0 | if (!state->cached && !previous) { |
3818 | 0 | if (*ce && !(*ce)->ce_mode) |
3819 | 0 | BUG("ce_mode == 0 for path '%s'", old_name); |
3820 | | |
3821 | 0 | if (trust_executable_bit || !S_ISREG(st->st_mode)) |
3822 | 0 | st_mode = ce_mode_from_stat(*ce, st->st_mode); |
3823 | 0 | else if (*ce) |
3824 | 0 | st_mode = (*ce)->ce_mode; |
3825 | 0 | else |
3826 | 0 | st_mode = patch->old_mode; |
3827 | 0 | } |
3828 | | |
3829 | 0 | if (patch->is_new < 0) |
3830 | 0 | patch->is_new = 0; |
3831 | 0 | if (!patch->old_mode) |
3832 | 0 | patch->old_mode = st_mode; |
3833 | 0 | if ((st_mode ^ patch->old_mode) & S_IFMT) |
3834 | 0 | return error(_("%s: wrong type"), old_name); |
3835 | 0 | if (st_mode != patch->old_mode) |
3836 | 0 | warning(_("%s has type %o, expected %o"), |
3837 | 0 | old_name, st_mode, patch->old_mode); |
3838 | 0 | if (!patch->new_mode && !patch->is_delete) |
3839 | 0 | patch->new_mode = st_mode; |
3840 | 0 | return 0; |
3841 | | |
3842 | 0 | is_new: |
3843 | 0 | patch->is_new = 1; |
3844 | 0 | patch->is_delete = 0; |
3845 | 0 | FREE_AND_NULL(patch->old_name); |
3846 | 0 | return 0; |
3847 | 0 | } |
3848 | | |
3849 | | |
3850 | 0 | #define EXISTS_IN_INDEX 1 |
3851 | 0 | #define EXISTS_IN_WORKTREE 2 |
3852 | 0 | #define EXISTS_IN_INDEX_AS_ITA 3 |
3853 | | |
3854 | | static int check_to_create(struct apply_state *state, |
3855 | | const char *new_name, |
3856 | | int ok_if_exists) |
3857 | 0 | { |
3858 | 0 | struct stat nst; |
3859 | |
|
3860 | 0 | if (state->check_index && (!ok_if_exists || !state->cached)) { |
3861 | 0 | int pos; |
3862 | |
|
3863 | 0 | pos = index_name_pos(state->repo->index, new_name, strlen(new_name)); |
3864 | 0 | if (pos >= 0) { |
3865 | 0 | struct cache_entry *ce = state->repo->index->cache[pos]; |
3866 | | |
3867 | | /* allow ITA, as they do not yet exist in the index */ |
3868 | 0 | if (!ok_if_exists && !(ce->ce_flags & CE_INTENT_TO_ADD)) |
3869 | 0 | return EXISTS_IN_INDEX; |
3870 | | |
3871 | | /* ITA entries can never match working tree files */ |
3872 | 0 | if (!state->cached && (ce->ce_flags & CE_INTENT_TO_ADD)) |
3873 | 0 | return EXISTS_IN_INDEX_AS_ITA; |
3874 | 0 | } |
3875 | 0 | } |
3876 | | |
3877 | 0 | if (state->cached) |
3878 | 0 | return 0; |
3879 | | |
3880 | 0 | if (!lstat(new_name, &nst)) { |
3881 | 0 | if (S_ISDIR(nst.st_mode) || ok_if_exists) |
3882 | 0 | return 0; |
3883 | | /* |
3884 | | * A leading component of new_name might be a symlink |
3885 | | * that is going to be removed with this patch, but |
3886 | | * still pointing at somewhere that has the path. |
3887 | | * In such a case, path "new_name" does not exist as |
3888 | | * far as git is concerned. |
3889 | | */ |
3890 | 0 | if (has_symlink_leading_path(new_name, strlen(new_name))) |
3891 | 0 | return 0; |
3892 | | |
3893 | 0 | return EXISTS_IN_WORKTREE; |
3894 | 0 | } else if (!is_missing_file_error(errno)) { |
3895 | 0 | return error_errno("%s", new_name); |
3896 | 0 | } |
3897 | 0 | return 0; |
3898 | 0 | } |
3899 | | |
3900 | | static void prepare_symlink_changes(struct apply_state *state, struct patch *patch) |
3901 | 0 | { |
3902 | 0 | for ( ; patch; patch = patch->next) { |
3903 | 0 | if ((patch->old_name && S_ISLNK(patch->old_mode)) && |
3904 | 0 | (patch->is_rename || patch->is_delete)) |
3905 | | /* the symlink at patch->old_name is removed */ |
3906 | 0 | strset_add(&state->removed_symlinks, patch->old_name); |
3907 | |
|
3908 | 0 | if (patch->new_name && S_ISLNK(patch->new_mode)) |
3909 | | /* the symlink at patch->new_name is created or remains */ |
3910 | 0 | strset_add(&state->kept_symlinks, patch->new_name); |
3911 | 0 | } |
3912 | 0 | } |
3913 | | |
3914 | | static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name) |
3915 | 0 | { |
3916 | 0 | do { |
3917 | 0 | while (--name->len && name->buf[name->len] != '/') |
3918 | 0 | ; /* scan backwards */ |
3919 | 0 | if (!name->len) |
3920 | 0 | break; |
3921 | 0 | name->buf[name->len] = '\0'; |
3922 | 0 | if (strset_contains(&state->kept_symlinks, name->buf)) |
3923 | 0 | return 1; |
3924 | 0 | if (strset_contains(&state->removed_symlinks, name->buf)) |
3925 | | /* |
3926 | | * This cannot be "return 0", because we may |
3927 | | * see a new one created at a higher level. |
3928 | | */ |
3929 | 0 | continue; |
3930 | | |
3931 | | /* otherwise, check the preimage */ |
3932 | 0 | if (state->check_index) { |
3933 | 0 | struct cache_entry *ce; |
3934 | |
|
3935 | 0 | ce = index_file_exists(state->repo->index, name->buf, |
3936 | 0 | name->len, ignore_case); |
3937 | 0 | if (ce && S_ISLNK(ce->ce_mode)) |
3938 | 0 | return 1; |
3939 | 0 | } else { |
3940 | 0 | struct stat st; |
3941 | 0 | if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode)) |
3942 | 0 | return 1; |
3943 | 0 | } |
3944 | 0 | } while (1); |
3945 | 0 | return 0; |
3946 | 0 | } |
3947 | | |
3948 | | static int path_is_beyond_symlink(struct apply_state *state, const char *name_) |
3949 | 0 | { |
3950 | 0 | int ret; |
3951 | 0 | struct strbuf name = STRBUF_INIT; |
3952 | |
|
3953 | 0 | assert(*name_ != '\0'); |
3954 | 0 | strbuf_addstr(&name, name_); |
3955 | 0 | ret = path_is_beyond_symlink_1(state, &name); |
3956 | 0 | strbuf_release(&name); |
3957 | |
|
3958 | 0 | return ret; |
3959 | 0 | } |
3960 | | |
3961 | | static int check_unsafe_path(struct patch *patch) |
3962 | 0 | { |
3963 | 0 | const char *old_name = NULL; |
3964 | 0 | const char *new_name = NULL; |
3965 | 0 | if (patch->is_delete) |
3966 | 0 | old_name = patch->old_name; |
3967 | 0 | else if (!patch->is_new && !patch->is_copy) |
3968 | 0 | old_name = patch->old_name; |
3969 | 0 | if (!patch->is_delete) |
3970 | 0 | new_name = patch->new_name; |
3971 | |
|
3972 | 0 | if (old_name && !verify_path(old_name, patch->old_mode)) |
3973 | 0 | return error(_("invalid path '%s'"), old_name); |
3974 | 0 | if (new_name && !verify_path(new_name, patch->new_mode)) |
3975 | 0 | return error(_("invalid path '%s'"), new_name); |
3976 | 0 | return 0; |
3977 | 0 | } |
3978 | | |
3979 | | /* |
3980 | | * Check and apply the patch in-core; leave the result in patch->result |
3981 | | * for the caller to write it out to the final destination. |
3982 | | */ |
3983 | | static int check_patch(struct apply_state *state, struct patch *patch) |
3984 | 0 | { |
3985 | 0 | struct stat st; |
3986 | 0 | const char *old_name = patch->old_name; |
3987 | 0 | const char *new_name = patch->new_name; |
3988 | 0 | const char *name = old_name ? old_name : new_name; |
3989 | 0 | struct cache_entry *ce = NULL; |
3990 | 0 | struct patch *tpatch; |
3991 | 0 | int ok_if_exists; |
3992 | 0 | int status; |
3993 | |
|
3994 | 0 | patch->rejected = 1; /* we will drop this after we succeed */ |
3995 | |
|
3996 | 0 | status = check_preimage(state, patch, &ce, &st); |
3997 | 0 | if (status) |
3998 | 0 | return status; |
3999 | 0 | old_name = patch->old_name; |
4000 | | |
4001 | | /* |
4002 | | * A type-change diff is always split into a patch to delete |
4003 | | * old, immediately followed by a patch to create new (see |
4004 | | * diff.c::run_diff()); in such a case it is Ok that the entry |
4005 | | * to be deleted by the previous patch is still in the working |
4006 | | * tree and in the index. |
4007 | | * |
4008 | | * A patch to swap-rename between A and B would first rename A |
4009 | | * to B and then rename B to A. While applying the first one, |
4010 | | * the presence of B should not stop A from getting renamed to |
4011 | | * B; ask to_be_deleted() about the later rename. Removal of |
4012 | | * B and rename from A to B is handled the same way by asking |
4013 | | * was_deleted(). |
4014 | | */ |
4015 | 0 | if ((tpatch = in_fn_table(state, new_name)) && |
4016 | 0 | (was_deleted(tpatch) || to_be_deleted(tpatch))) |
4017 | 0 | ok_if_exists = 1; |
4018 | 0 | else |
4019 | 0 | ok_if_exists = 0; |
4020 | |
|
4021 | 0 | if (new_name && |
4022 | 0 | ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) { |
4023 | 0 | int err = check_to_create(state, new_name, ok_if_exists); |
4024 | |
|
4025 | 0 | if (err && state->threeway) { |
4026 | 0 | patch->direct_to_threeway = 1; |
4027 | 0 | } else switch (err) { |
4028 | 0 | case 0: |
4029 | 0 | break; /* happy */ |
4030 | 0 | case EXISTS_IN_INDEX: |
4031 | 0 | return error(_("%s: already exists in index"), new_name); |
4032 | 0 | case EXISTS_IN_INDEX_AS_ITA: |
4033 | 0 | return error(_("%s: does not match index"), new_name); |
4034 | 0 | case EXISTS_IN_WORKTREE: |
4035 | 0 | return error(_("%s: already exists in working directory"), |
4036 | 0 | new_name); |
4037 | 0 | default: |
4038 | 0 | return err; |
4039 | 0 | } |
4040 | | |
4041 | 0 | if (!patch->new_mode) { |
4042 | 0 | if (0 < patch->is_new) |
4043 | 0 | patch->new_mode = S_IFREG | 0644; |
4044 | 0 | else |
4045 | 0 | patch->new_mode = patch->old_mode; |
4046 | 0 | } |
4047 | 0 | } |
4048 | | |
4049 | 0 | if (new_name && old_name) { |
4050 | 0 | int same = !strcmp(old_name, new_name); |
4051 | 0 | if (!patch->new_mode) |
4052 | 0 | patch->new_mode = patch->old_mode; |
4053 | 0 | if ((patch->old_mode ^ patch->new_mode) & S_IFMT) { |
4054 | 0 | if (same) |
4055 | 0 | return error(_("new mode (%o) of %s does not " |
4056 | 0 | "match old mode (%o)"), |
4057 | 0 | patch->new_mode, new_name, |
4058 | 0 | patch->old_mode); |
4059 | 0 | else |
4060 | 0 | return error(_("new mode (%o) of %s does not " |
4061 | 0 | "match old mode (%o) of %s"), |
4062 | 0 | patch->new_mode, new_name, |
4063 | 0 | patch->old_mode, old_name); |
4064 | 0 | } |
4065 | 0 | } |
4066 | | |
4067 | 0 | if (!state->unsafe_paths && check_unsafe_path(patch)) |
4068 | 0 | return -128; |
4069 | | |
4070 | | /* |
4071 | | * An attempt to read from or delete a path that is beyond a |
4072 | | * symbolic link will be prevented by load_patch_target() that |
4073 | | * is called at the beginning of apply_data() so we do not |
4074 | | * have to worry about a patch marked with "is_delete" bit |
4075 | | * here. We however need to make sure that the patch result |
4076 | | * is not deposited to a path that is beyond a symbolic link |
4077 | | * here. |
4078 | | */ |
4079 | 0 | if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name)) |
4080 | 0 | return error(_("affected file '%s' is beyond a symbolic link"), |
4081 | 0 | patch->new_name); |
4082 | | |
4083 | 0 | if (apply_data(state, patch, &st, ce) < 0) |
4084 | 0 | return error(_("%s: patch does not apply"), name); |
4085 | 0 | patch->rejected = 0; |
4086 | 0 | return 0; |
4087 | 0 | } |
4088 | | |
4089 | | static int check_patch_list(struct apply_state *state, struct patch *patch) |
4090 | 0 | { |
4091 | 0 | int err = 0; |
4092 | |
|
4093 | 0 | prepare_symlink_changes(state, patch); |
4094 | 0 | prepare_fn_table(state, patch); |
4095 | 0 | while (patch) { |
4096 | 0 | int res; |
4097 | 0 | if (state->apply_verbosity > verbosity_normal) |
4098 | 0 | say_patch_name(stderr, |
4099 | 0 | _("Checking patch %s..."), patch); |
4100 | 0 | res = check_patch(state, patch); |
4101 | 0 | if (res == -128) |
4102 | 0 | return -128; |
4103 | 0 | err |= res; |
4104 | 0 | patch = patch->next; |
4105 | 0 | } |
4106 | 0 | return err; |
4107 | 0 | } |
4108 | | |
4109 | | static int read_apply_cache(struct apply_state *state) |
4110 | 0 | { |
4111 | 0 | if (state->index_file) |
4112 | 0 | return read_index_from(state->repo->index, state->index_file, |
4113 | 0 | repo_get_git_dir(the_repository)); |
4114 | 0 | else |
4115 | 0 | return repo_read_index(state->repo); |
4116 | 0 | } |
4117 | | |
4118 | | /* This function tries to read the object name from the current index */ |
4119 | | static int get_current_oid(struct apply_state *state, const char *path, |
4120 | | struct object_id *oid) |
4121 | 0 | { |
4122 | 0 | int pos; |
4123 | |
|
4124 | 0 | if (read_apply_cache(state) < 0) |
4125 | 0 | return -1; |
4126 | 0 | pos = index_name_pos(state->repo->index, path, strlen(path)); |
4127 | 0 | if (pos < 0) |
4128 | 0 | return -1; |
4129 | 0 | oidcpy(oid, &state->repo->index->cache[pos]->oid); |
4130 | 0 | return 0; |
4131 | 0 | } |
4132 | | |
4133 | | static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid) |
4134 | 0 | { |
4135 | | /* |
4136 | | * A usable gitlink patch has only one fragment (hunk) that looks like: |
4137 | | * @@ -1 +1 @@ |
4138 | | * -Subproject commit <old sha1> |
4139 | | * +Subproject commit <new sha1> |
4140 | | * or |
4141 | | * @@ -1 +0,0 @@ |
4142 | | * -Subproject commit <old sha1> |
4143 | | * for a removal patch. |
4144 | | */ |
4145 | 0 | struct fragment *hunk = p->fragments; |
4146 | 0 | static const char heading[] = "-Subproject commit "; |
4147 | 0 | char *preimage; |
4148 | |
|
4149 | 0 | if (/* does the patch have only one hunk? */ |
4150 | 0 | hunk && !hunk->next && |
4151 | | /* is its preimage one line? */ |
4152 | 0 | hunk->oldpos == 1 && hunk->oldlines == 1 && |
4153 | | /* does preimage begin with the heading? */ |
4154 | 0 | (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL && |
4155 | 0 | starts_with(++preimage, heading) && |
4156 | | /* does it record full SHA-1? */ |
4157 | 0 | !get_oid_hex(preimage + sizeof(heading) - 1, oid) && |
4158 | 0 | preimage[sizeof(heading) + the_hash_algo->hexsz - 1] == '\n' && |
4159 | | /* does the abbreviated name on the index line agree with it? */ |
4160 | 0 | starts_with(preimage + sizeof(heading) - 1, p->old_oid_prefix)) |
4161 | 0 | return 0; /* it all looks fine */ |
4162 | | |
4163 | | /* we may have full object name on the index line */ |
4164 | 0 | return get_oid_hex(p->old_oid_prefix, oid); |
4165 | 0 | } |
4166 | | |
4167 | | /* Build an index that contains just the files needed for a 3way merge */ |
4168 | | static int build_fake_ancestor(struct apply_state *state, struct patch *list) |
4169 | 0 | { |
4170 | 0 | struct patch *patch; |
4171 | 0 | struct index_state result = INDEX_STATE_INIT(state->repo); |
4172 | 0 | struct lock_file lock = LOCK_INIT; |
4173 | 0 | int res; |
4174 | | |
4175 | | /* Once we start supporting the reverse patch, it may be |
4176 | | * worth showing the new sha1 prefix, but until then... |
4177 | | */ |
4178 | 0 | for (patch = list; patch; patch = patch->next) { |
4179 | 0 | struct object_id oid; |
4180 | 0 | struct cache_entry *ce; |
4181 | 0 | const char *name; |
4182 | |
|
4183 | 0 | name = patch->old_name ? patch->old_name : patch->new_name; |
4184 | 0 | if (0 < patch->is_new) |
4185 | 0 | continue; |
4186 | | |
4187 | 0 | if (S_ISGITLINK(patch->old_mode)) { |
4188 | 0 | if (!preimage_oid_in_gitlink_patch(patch, &oid)) |
4189 | 0 | ; /* ok, the textual part looks sane */ |
4190 | 0 | else |
4191 | 0 | return error(_("sha1 information is lacking or " |
4192 | 0 | "useless for submodule %s"), name); |
4193 | 0 | } else if (!repo_get_oid_blob(the_repository, patch->old_oid_prefix, &oid)) { |
4194 | 0 | ; /* ok */ |
4195 | 0 | } else if (!patch->lines_added && !patch->lines_deleted) { |
4196 | | /* mode-only change: update the current */ |
4197 | 0 | if (get_current_oid(state, patch->old_name, &oid)) |
4198 | 0 | return error(_("mode change for %s, which is not " |
4199 | 0 | "in current HEAD"), name); |
4200 | 0 | } else |
4201 | 0 | return error(_("sha1 information is lacking or useless " |
4202 | 0 | "(%s)."), name); |
4203 | | |
4204 | 0 | ce = make_cache_entry(&result, patch->old_mode, &oid, name, 0, 0); |
4205 | 0 | if (!ce) |
4206 | 0 | return error(_("make_cache_entry failed for path '%s'"), |
4207 | 0 | name); |
4208 | 0 | if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) { |
4209 | 0 | discard_cache_entry(ce); |
4210 | 0 | return error(_("could not add %s to temporary index"), |
4211 | 0 | name); |
4212 | 0 | } |
4213 | 0 | } |
4214 | | |
4215 | 0 | hold_lock_file_for_update(&lock, state->fake_ancestor, LOCK_DIE_ON_ERROR); |
4216 | 0 | res = write_locked_index(&result, &lock, COMMIT_LOCK); |
4217 | 0 | discard_index(&result); |
4218 | |
|
4219 | 0 | if (res) |
4220 | 0 | return error(_("could not write temporary index to %s"), |
4221 | 0 | state->fake_ancestor); |
4222 | | |
4223 | 0 | return 0; |
4224 | 0 | } |
4225 | | |
4226 | | static void stat_patch_list(struct apply_state *state, struct patch *patch) |
4227 | 0 | { |
4228 | 0 | int files, adds, dels; |
4229 | |
|
4230 | 0 | for (files = adds = dels = 0 ; patch ; patch = patch->next) { |
4231 | 0 | files++; |
4232 | 0 | adds += patch->lines_added; |
4233 | 0 | dels += patch->lines_deleted; |
4234 | 0 | show_stats(state, patch); |
4235 | 0 | } |
4236 | |
|
4237 | 0 | print_stat_summary(stdout, files, adds, dels); |
4238 | 0 | } |
4239 | | |
4240 | | static void numstat_patch_list(struct apply_state *state, |
4241 | | struct patch *patch) |
4242 | 0 | { |
4243 | 0 | for ( ; patch; patch = patch->next) { |
4244 | 0 | const char *name; |
4245 | 0 | name = patch->new_name ? patch->new_name : patch->old_name; |
4246 | 0 | if (patch->is_binary) |
4247 | 0 | printf("-\t-\t"); |
4248 | 0 | else |
4249 | 0 | printf("%d\t%d\t", patch->lines_added, patch->lines_deleted); |
4250 | 0 | write_name_quoted(name, stdout, state->line_termination); |
4251 | 0 | } |
4252 | 0 | } |
4253 | | |
4254 | | static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name) |
4255 | 0 | { |
4256 | 0 | if (mode) |
4257 | 0 | printf(" %s mode %06o %s\n", newdelete, mode, name); |
4258 | 0 | else |
4259 | 0 | printf(" %s %s\n", newdelete, name); |
4260 | 0 | } |
4261 | | |
4262 | | static void show_mode_change(struct patch *p, int show_name) |
4263 | 0 | { |
4264 | 0 | if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) { |
4265 | 0 | if (show_name) |
4266 | 0 | printf(" mode change %06o => %06o %s\n", |
4267 | 0 | p->old_mode, p->new_mode, p->new_name); |
4268 | 0 | else |
4269 | 0 | printf(" mode change %06o => %06o\n", |
4270 | 0 | p->old_mode, p->new_mode); |
4271 | 0 | } |
4272 | 0 | } |
4273 | | |
4274 | | static void show_rename_copy(struct patch *p) |
4275 | 0 | { |
4276 | 0 | const char *renamecopy = p->is_rename ? "rename" : "copy"; |
4277 | 0 | const char *old_name, *new_name; |
4278 | | |
4279 | | /* Find common prefix */ |
4280 | 0 | old_name = p->old_name; |
4281 | 0 | new_name = p->new_name; |
4282 | 0 | while (1) { |
4283 | 0 | const char *slash_old, *slash_new; |
4284 | 0 | slash_old = strchr(old_name, '/'); |
4285 | 0 | slash_new = strchr(new_name, '/'); |
4286 | 0 | if (!slash_old || |
4287 | 0 | !slash_new || |
4288 | 0 | slash_old - old_name != slash_new - new_name || |
4289 | 0 | memcmp(old_name, new_name, slash_new - new_name)) |
4290 | 0 | break; |
4291 | 0 | old_name = slash_old + 1; |
4292 | 0 | new_name = slash_new + 1; |
4293 | 0 | } |
4294 | | /* p->old_name through old_name is the common prefix, and old_name and |
4295 | | * new_name through the end of names are renames |
4296 | | */ |
4297 | 0 | if (old_name != p->old_name) |
4298 | 0 | printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, |
4299 | 0 | (int)(old_name - p->old_name), p->old_name, |
4300 | 0 | old_name, new_name, p->score); |
4301 | 0 | else |
4302 | 0 | printf(" %s %s => %s (%d%%)\n", renamecopy, |
4303 | 0 | p->old_name, p->new_name, p->score); |
4304 | 0 | show_mode_change(p, 0); |
4305 | 0 | } |
4306 | | |
4307 | | static void summary_patch_list(struct patch *patch) |
4308 | 0 | { |
4309 | 0 | struct patch *p; |
4310 | |
|
4311 | 0 | for (p = patch; p; p = p->next) { |
4312 | 0 | if (p->is_new) |
4313 | 0 | show_file_mode_name("create", p->new_mode, p->new_name); |
4314 | 0 | else if (p->is_delete) |
4315 | 0 | show_file_mode_name("delete", p->old_mode, p->old_name); |
4316 | 0 | else { |
4317 | 0 | if (p->is_rename || p->is_copy) |
4318 | 0 | show_rename_copy(p); |
4319 | 0 | else { |
4320 | 0 | if (p->score) { |
4321 | 0 | printf(" rewrite %s (%d%%)\n", |
4322 | 0 | p->new_name, p->score); |
4323 | 0 | show_mode_change(p, 0); |
4324 | 0 | } |
4325 | 0 | else |
4326 | 0 | show_mode_change(p, 1); |
4327 | 0 | } |
4328 | 0 | } |
4329 | 0 | } |
4330 | 0 | } |
4331 | | |
4332 | | static void patch_stats(struct apply_state *state, struct patch *patch) |
4333 | 0 | { |
4334 | 0 | int lines = patch->lines_added + patch->lines_deleted; |
4335 | |
|
4336 | 0 | if (lines > state->max_change) |
4337 | 0 | state->max_change = lines; |
4338 | 0 | if (patch->old_name) { |
4339 | 0 | int len = quote_c_style(patch->old_name, NULL, NULL, 0); |
4340 | 0 | if (!len) |
4341 | 0 | len = strlen(patch->old_name); |
4342 | 0 | if (len > state->max_len) |
4343 | 0 | state->max_len = len; |
4344 | 0 | } |
4345 | 0 | if (patch->new_name) { |
4346 | 0 | int len = quote_c_style(patch->new_name, NULL, NULL, 0); |
4347 | 0 | if (!len) |
4348 | 0 | len = strlen(patch->new_name); |
4349 | 0 | if (len > state->max_len) |
4350 | 0 | state->max_len = len; |
4351 | 0 | } |
4352 | 0 | } |
4353 | | |
4354 | | static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty) |
4355 | 0 | { |
4356 | 0 | if (state->update_index && !state->ita_only) { |
4357 | 0 | if (remove_file_from_index(state->repo->index, patch->old_name) < 0) |
4358 | 0 | return error(_("unable to remove %s from index"), patch->old_name); |
4359 | 0 | } |
4360 | 0 | if (!state->cached) { |
4361 | 0 | if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) { |
4362 | 0 | remove_path(patch->old_name); |
4363 | 0 | } |
4364 | 0 | } |
4365 | 0 | return 0; |
4366 | 0 | } |
4367 | | |
4368 | | static int add_index_file(struct apply_state *state, |
4369 | | const char *path, |
4370 | | unsigned mode, |
4371 | | void *buf, |
4372 | | unsigned long size) |
4373 | 0 | { |
4374 | 0 | struct stat st; |
4375 | 0 | struct cache_entry *ce; |
4376 | 0 | int namelen = strlen(path); |
4377 | |
|
4378 | 0 | ce = make_empty_cache_entry(state->repo->index, namelen); |
4379 | 0 | memcpy(ce->name, path, namelen); |
4380 | 0 | ce->ce_mode = create_ce_mode(mode); |
4381 | 0 | ce->ce_flags = create_ce_flags(0); |
4382 | 0 | ce->ce_namelen = namelen; |
4383 | 0 | if (state->ita_only) { |
4384 | 0 | ce->ce_flags |= CE_INTENT_TO_ADD; |
4385 | 0 | set_object_name_for_intent_to_add_entry(ce); |
4386 | 0 | } else if (S_ISGITLINK(mode)) { |
4387 | 0 | const char *s; |
4388 | |
|
4389 | 0 | if (!skip_prefix(buf, "Subproject commit ", &s) || |
4390 | 0 | get_oid_hex(s, &ce->oid)) { |
4391 | 0 | discard_cache_entry(ce); |
4392 | 0 | return error(_("corrupt patch for submodule %s"), path); |
4393 | 0 | } |
4394 | 0 | } else { |
4395 | 0 | if (!state->cached) { |
4396 | 0 | if (lstat(path, &st) < 0) { |
4397 | 0 | discard_cache_entry(ce); |
4398 | 0 | return error_errno(_("unable to stat newly " |
4399 | 0 | "created file '%s'"), |
4400 | 0 | path); |
4401 | 0 | } |
4402 | 0 | fill_stat_cache_info(state->repo->index, ce, &st); |
4403 | 0 | } |
4404 | 0 | if (odb_write_object(the_repository->objects, buf, size, |
4405 | 0 | OBJ_BLOB, &ce->oid) < 0) { |
4406 | 0 | discard_cache_entry(ce); |
4407 | 0 | return error(_("unable to create backing store " |
4408 | 0 | "for newly created file %s"), path); |
4409 | 0 | } |
4410 | 0 | } |
4411 | 0 | if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) { |
4412 | 0 | discard_cache_entry(ce); |
4413 | 0 | return error(_("unable to add cache entry for %s"), path); |
4414 | 0 | } |
4415 | | |
4416 | 0 | return 0; |
4417 | 0 | } |
4418 | | |
4419 | | /* |
4420 | | * Returns: |
4421 | | * -1 if an unrecoverable error happened |
4422 | | * 0 if everything went well |
4423 | | * 1 if a recoverable error happened |
4424 | | */ |
4425 | | static int try_create_file(struct apply_state *state, const char *path, |
4426 | | unsigned int mode, const char *buf, |
4427 | | unsigned long size) |
4428 | 0 | { |
4429 | 0 | int fd, res; |
4430 | 0 | struct strbuf nbuf = STRBUF_INIT; |
4431 | |
|
4432 | 0 | if (S_ISGITLINK(mode)) { |
4433 | 0 | struct stat st; |
4434 | 0 | if (!lstat(path, &st) && S_ISDIR(st.st_mode)) |
4435 | 0 | return 0; |
4436 | 0 | return !!mkdir(path, 0777); |
4437 | 0 | } |
4438 | | |
4439 | 0 | if (has_symlinks && S_ISLNK(mode)) |
4440 | | /* Although buf:size is counted string, it also is NUL |
4441 | | * terminated. |
4442 | | */ |
4443 | 0 | return !!symlink(buf, path); |
4444 | | |
4445 | 0 | fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666); |
4446 | 0 | if (fd < 0) |
4447 | 0 | return 1; |
4448 | | |
4449 | 0 | if (convert_to_working_tree(state->repo->index, path, buf, size, &nbuf, NULL)) { |
4450 | 0 | size = nbuf.len; |
4451 | 0 | buf = nbuf.buf; |
4452 | 0 | } |
4453 | |
|
4454 | 0 | res = write_in_full(fd, buf, size) < 0; |
4455 | 0 | if (res) |
4456 | 0 | error_errno(_("failed to write to '%s'"), path); |
4457 | 0 | strbuf_release(&nbuf); |
4458 | |
|
4459 | 0 | if (close(fd) < 0 && !res) |
4460 | 0 | return error_errno(_("closing file '%s'"), path); |
4461 | | |
4462 | 0 | return res ? -1 : 0; |
4463 | 0 | } |
4464 | | |
4465 | | /* |
4466 | | * We optimistically assume that the directories exist, |
4467 | | * which is true 99% of the time anyway. If they don't, |
4468 | | * we create them and try again. |
4469 | | * |
4470 | | * Returns: |
4471 | | * -1 on error |
4472 | | * 0 otherwise |
4473 | | */ |
4474 | | static int create_one_file(struct apply_state *state, |
4475 | | char *path, |
4476 | | unsigned mode, |
4477 | | const char *buf, |
4478 | | unsigned long size) |
4479 | 0 | { |
4480 | 0 | char *newpath = NULL; |
4481 | 0 | int res; |
4482 | |
|
4483 | 0 | if (state->cached) |
4484 | 0 | return 0; |
4485 | | |
4486 | | /* |
4487 | | * We already try to detect whether files are beyond a symlink in our |
4488 | | * up-front checks. But in the case where symlinks are created by any |
4489 | | * of the intermediate hunks it can happen that our up-front checks |
4490 | | * didn't yet see the symlink, but at the point of arriving here there |
4491 | | * in fact is one. We thus repeat the check for symlinks here. |
4492 | | * |
4493 | | * Note that this does not make the up-front check obsolete as the |
4494 | | * failure mode is different: |
4495 | | * |
4496 | | * - The up-front checks cause us to abort before we have written |
4497 | | * anything into the working directory. So when we exit this way the |
4498 | | * working directory remains clean. |
4499 | | * |
4500 | | * - The checks here happen in the middle of the action where we have |
4501 | | * already started to apply the patch. The end result will be a dirty |
4502 | | * working directory. |
4503 | | * |
4504 | | * Ideally, we should update the up-front checks to catch what would |
4505 | | * happen when we apply the patch before we damage the working tree. |
4506 | | * We have all the information necessary to do so. But for now, as a |
4507 | | * part of embargoed security work, having this check would serve as a |
4508 | | * reasonable first step. |
4509 | | */ |
4510 | 0 | if (path_is_beyond_symlink(state, path)) |
4511 | 0 | return error(_("affected file '%s' is beyond a symbolic link"), path); |
4512 | | |
4513 | 0 | res = try_create_file(state, path, mode, buf, size); |
4514 | 0 | if (res < 0) |
4515 | 0 | return -1; |
4516 | 0 | if (!res) |
4517 | 0 | return 0; |
4518 | | |
4519 | 0 | if (errno == ENOENT) { |
4520 | 0 | if (safe_create_leading_directories_no_share(path)) |
4521 | 0 | return 0; |
4522 | 0 | res = try_create_file(state, path, mode, buf, size); |
4523 | 0 | if (res < 0) |
4524 | 0 | return -1; |
4525 | 0 | if (!res) |
4526 | 0 | return 0; |
4527 | 0 | } |
4528 | | |
4529 | 0 | if (errno == EEXIST || errno == EACCES) { |
4530 | | /* We may be trying to create a file where a directory |
4531 | | * used to be. |
4532 | | */ |
4533 | 0 | struct stat st; |
4534 | 0 | if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path))) |
4535 | 0 | errno = EEXIST; |
4536 | 0 | } |
4537 | |
|
4538 | 0 | if (errno == EEXIST) { |
4539 | 0 | unsigned int nr = getpid(); |
4540 | |
|
4541 | 0 | for (;;) { |
4542 | 0 | newpath = mkpathdup("%s~%u", path, nr); |
4543 | 0 | res = try_create_file(state, newpath, mode, buf, size); |
4544 | 0 | if (res < 0) |
4545 | 0 | goto out; |
4546 | 0 | if (!res) { |
4547 | 0 | if (!rename(newpath, path)) |
4548 | 0 | goto out; |
4549 | 0 | unlink_or_warn(newpath); |
4550 | 0 | break; |
4551 | 0 | } |
4552 | 0 | if (errno != EEXIST) |
4553 | 0 | break; |
4554 | 0 | ++nr; |
4555 | 0 | FREE_AND_NULL(newpath); |
4556 | 0 | } |
4557 | 0 | } |
4558 | 0 | res = error_errno(_("unable to write file '%s' mode %o"), path, mode); |
4559 | 0 | out: |
4560 | 0 | free(newpath); |
4561 | 0 | return res; |
4562 | 0 | } |
4563 | | |
4564 | | static int add_conflicted_stages_file(struct apply_state *state, |
4565 | | struct patch *patch) |
4566 | 0 | { |
4567 | 0 | int stage, namelen; |
4568 | 0 | unsigned mode; |
4569 | 0 | struct cache_entry *ce; |
4570 | |
|
4571 | 0 | if (!state->update_index) |
4572 | 0 | return 0; |
4573 | 0 | namelen = strlen(patch->new_name); |
4574 | 0 | mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644); |
4575 | |
|
4576 | 0 | remove_file_from_index(state->repo->index, patch->new_name); |
4577 | 0 | for (stage = 1; stage < 4; stage++) { |
4578 | 0 | if (is_null_oid(&patch->threeway_stage[stage - 1])) |
4579 | 0 | continue; |
4580 | 0 | ce = make_empty_cache_entry(state->repo->index, namelen); |
4581 | 0 | memcpy(ce->name, patch->new_name, namelen); |
4582 | 0 | ce->ce_mode = create_ce_mode(mode); |
4583 | 0 | ce->ce_flags = create_ce_flags(stage); |
4584 | 0 | ce->ce_namelen = namelen; |
4585 | 0 | oidcpy(&ce->oid, &patch->threeway_stage[stage - 1]); |
4586 | 0 | if (add_index_entry(state->repo->index, ce, ADD_CACHE_OK_TO_ADD) < 0) { |
4587 | 0 | discard_cache_entry(ce); |
4588 | 0 | return error(_("unable to add cache entry for %s"), |
4589 | 0 | patch->new_name); |
4590 | 0 | } |
4591 | 0 | } |
4592 | | |
4593 | 0 | return 0; |
4594 | 0 | } |
4595 | | |
4596 | | static int create_file(struct apply_state *state, struct patch *patch) |
4597 | 0 | { |
4598 | 0 | char *path = patch->new_name; |
4599 | 0 | unsigned mode = patch->new_mode; |
4600 | 0 | unsigned long size = patch->resultsize; |
4601 | 0 | char *buf = patch->result; |
4602 | |
|
4603 | 0 | if (!mode) |
4604 | 0 | mode = S_IFREG | 0644; |
4605 | 0 | if (create_one_file(state, path, mode, buf, size)) |
4606 | 0 | return -1; |
4607 | | |
4608 | 0 | if (patch->conflicted_threeway) |
4609 | 0 | return add_conflicted_stages_file(state, patch); |
4610 | 0 | else if (state->check_index || (state->ita_only && patch->is_new > 0)) |
4611 | 0 | return add_index_file(state, path, mode, buf, size); |
4612 | 0 | return 0; |
4613 | 0 | } |
4614 | | |
4615 | | /* phase zero is to remove, phase one is to create */ |
4616 | | static int write_out_one_result(struct apply_state *state, |
4617 | | struct patch *patch, |
4618 | | int phase) |
4619 | 0 | { |
4620 | 0 | if (patch->is_delete > 0) { |
4621 | 0 | if (phase == 0) |
4622 | 0 | return remove_file(state, patch, 1); |
4623 | 0 | return 0; |
4624 | 0 | } |
4625 | 0 | if (patch->is_new > 0 || patch->is_copy) { |
4626 | 0 | if (phase == 1) |
4627 | 0 | return create_file(state, patch); |
4628 | 0 | return 0; |
4629 | 0 | } |
4630 | | /* |
4631 | | * Rename or modification boils down to the same |
4632 | | * thing: remove the old, write the new |
4633 | | */ |
4634 | 0 | if (phase == 0) |
4635 | 0 | return remove_file(state, patch, patch->is_rename); |
4636 | 0 | if (phase == 1) |
4637 | 0 | return create_file(state, patch); |
4638 | 0 | return 0; |
4639 | 0 | } |
4640 | | |
4641 | | static int write_out_one_reject(struct apply_state *state, struct patch *patch) |
4642 | 0 | { |
4643 | 0 | FILE *rej; |
4644 | 0 | char *namebuf; |
4645 | 0 | struct fragment *frag; |
4646 | 0 | int fd, cnt = 0; |
4647 | 0 | struct strbuf sb = STRBUF_INIT; |
4648 | |
|
4649 | 0 | for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) { |
4650 | 0 | if (!frag->rejected) |
4651 | 0 | continue; |
4652 | 0 | cnt++; |
4653 | 0 | } |
4654 | |
|
4655 | 0 | if (!cnt) { |
4656 | 0 | if (state->apply_verbosity > verbosity_normal) |
4657 | 0 | say_patch_name(stderr, |
4658 | 0 | _("Applied patch %s cleanly."), patch); |
4659 | 0 | return 0; |
4660 | 0 | } |
4661 | | |
4662 | | /* This should not happen, because a removal patch that leaves |
4663 | | * contents are marked "rejected" at the patch level. |
4664 | | */ |
4665 | 0 | if (!patch->new_name) |
4666 | 0 | die(_("internal error")); |
4667 | | |
4668 | | /* Say this even without --verbose */ |
4669 | 0 | strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...", |
4670 | 0 | "Applying patch %%s with %d rejects...", |
4671 | 0 | cnt), |
4672 | 0 | cnt); |
4673 | 0 | if (state->apply_verbosity > verbosity_silent) |
4674 | 0 | say_patch_name(stderr, sb.buf, patch); |
4675 | 0 | strbuf_release(&sb); |
4676 | |
|
4677 | 0 | namebuf = xstrfmt("%s.rej", patch->new_name); |
4678 | |
|
4679 | 0 | fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666); |
4680 | 0 | if (fd < 0) { |
4681 | 0 | if (errno != EEXIST) { |
4682 | 0 | error_errno(_("cannot open %s"), namebuf); |
4683 | 0 | goto error; |
4684 | 0 | } |
4685 | 0 | if (unlink(namebuf)) { |
4686 | 0 | error_errno(_("cannot unlink '%s'"), namebuf); |
4687 | 0 | goto error; |
4688 | 0 | } |
4689 | 0 | fd = open(namebuf, O_CREAT | O_EXCL | O_WRONLY, 0666); |
4690 | 0 | if (fd < 0) { |
4691 | 0 | error_errno(_("cannot open %s"), namebuf); |
4692 | 0 | goto error; |
4693 | 0 | } |
4694 | 0 | } |
4695 | 0 | rej = fdopen(fd, "w"); |
4696 | 0 | if (!rej) { |
4697 | 0 | error_errno(_("cannot open %s"), namebuf); |
4698 | 0 | close(fd); |
4699 | 0 | goto error; |
4700 | 0 | } |
4701 | | |
4702 | | /* Normal git tools never deal with .rej, so do not pretend |
4703 | | * this is a git patch by saying --git or giving extended |
4704 | | * headers. While at it, maybe please "kompare" that wants |
4705 | | * the trailing TAB and some garbage at the end of line ;-). |
4706 | | */ |
4707 | 0 | fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n", |
4708 | 0 | patch->new_name, patch->new_name); |
4709 | 0 | for (cnt = 1, frag = patch->fragments; |
4710 | 0 | frag; |
4711 | 0 | cnt++, frag = frag->next) { |
4712 | 0 | if (!frag->rejected) { |
4713 | 0 | if (state->apply_verbosity > verbosity_silent) |
4714 | 0 | fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt); |
4715 | 0 | continue; |
4716 | 0 | } |
4717 | 0 | if (state->apply_verbosity > verbosity_silent) |
4718 | 0 | fprintf_ln(stderr, _("Rejected hunk #%d."), cnt); |
4719 | 0 | fprintf(rej, "%.*s", frag->size, frag->patch); |
4720 | 0 | if (frag->patch[frag->size-1] != '\n') |
4721 | 0 | fputc('\n', rej); |
4722 | 0 | } |
4723 | 0 | fclose(rej); |
4724 | 0 | error: |
4725 | 0 | free(namebuf); |
4726 | 0 | return -1; |
4727 | 0 | } |
4728 | | |
4729 | | /* |
4730 | | * Returns: |
4731 | | * -1 if an error happened |
4732 | | * 0 if the patch applied cleanly |
4733 | | * 1 if the patch did not apply cleanly |
4734 | | */ |
4735 | | static int write_out_results(struct apply_state *state, struct patch *list) |
4736 | 0 | { |
4737 | 0 | int phase; |
4738 | 0 | int errs = 0; |
4739 | 0 | struct patch *l; |
4740 | 0 | struct string_list cpath = STRING_LIST_INIT_DUP; |
4741 | |
|
4742 | 0 | for (phase = 0; phase < 2; phase++) { |
4743 | 0 | l = list; |
4744 | 0 | while (l) { |
4745 | 0 | if (l->rejected) |
4746 | 0 | errs = 1; |
4747 | 0 | else { |
4748 | 0 | if (write_out_one_result(state, l, phase)) { |
4749 | 0 | string_list_clear(&cpath, 0); |
4750 | 0 | return -1; |
4751 | 0 | } |
4752 | 0 | if (phase == 1) { |
4753 | 0 | if (write_out_one_reject(state, l)) |
4754 | 0 | errs = 1; |
4755 | 0 | if (l->conflicted_threeway) { |
4756 | 0 | string_list_append(&cpath, l->new_name); |
4757 | 0 | errs = 1; |
4758 | 0 | } |
4759 | 0 | } |
4760 | 0 | } |
4761 | 0 | l = l->next; |
4762 | 0 | } |
4763 | 0 | } |
4764 | | |
4765 | 0 | if (cpath.nr) { |
4766 | 0 | struct string_list_item *item; |
4767 | |
|
4768 | 0 | string_list_sort(&cpath); |
4769 | 0 | if (state->apply_verbosity > verbosity_silent) { |
4770 | 0 | for_each_string_list_item(item, &cpath) |
4771 | 0 | fprintf(stderr, "U %s\n", item->string); |
4772 | 0 | } |
4773 | 0 | string_list_clear(&cpath, 0); |
4774 | | |
4775 | | /* |
4776 | | * rerere relies on the partially merged result being in the working |
4777 | | * tree with conflict markers, but that isn't written with --cached. |
4778 | | */ |
4779 | 0 | if (!state->cached) |
4780 | 0 | repo_rerere(state->repo, 0); |
4781 | 0 | } |
4782 | |
|
4783 | 0 | return errs; |
4784 | 0 | } |
4785 | | |
4786 | | /* |
4787 | | * Try to apply a patch. |
4788 | | * |
4789 | | * Returns: |
4790 | | * -128 if a bad error happened (like patch unreadable) |
4791 | | * -1 if patch did not apply and user cannot deal with it |
4792 | | * 0 if the patch applied |
4793 | | * 1 if the patch did not apply but user might fix it |
4794 | | */ |
4795 | | static int apply_patch(struct apply_state *state, |
4796 | | int fd, |
4797 | | const char *filename, |
4798 | | int options) |
4799 | 0 | { |
4800 | 0 | size_t offset; |
4801 | 0 | struct strbuf buf = STRBUF_INIT; /* owns the patch text */ |
4802 | 0 | struct patch *list = NULL, **listp = &list; |
4803 | 0 | int skipped_patch = 0; |
4804 | 0 | int res = 0; |
4805 | 0 | int flush_attributes = 0; |
4806 | |
|
4807 | 0 | state->patch_input_file = filename; |
4808 | 0 | if (read_patch_file(&buf, fd) < 0) |
4809 | 0 | return -128; |
4810 | 0 | offset = 0; |
4811 | 0 | while (offset < buf.len) { |
4812 | 0 | struct patch *patch; |
4813 | 0 | int nr; |
4814 | |
|
4815 | 0 | CALLOC_ARRAY(patch, 1); |
4816 | 0 | patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF); |
4817 | 0 | patch->recount = !!(options & APPLY_OPT_RECOUNT); |
4818 | 0 | nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch); |
4819 | 0 | if (nr < 0) { |
4820 | 0 | free_patch(patch); |
4821 | 0 | if (nr == -128) { |
4822 | 0 | res = -128; |
4823 | 0 | goto end; |
4824 | 0 | } |
4825 | 0 | break; |
4826 | 0 | } |
4827 | 0 | if (state->apply_in_reverse) |
4828 | 0 | reverse_patches(patch); |
4829 | 0 | if (use_patch(state, patch)) { |
4830 | 0 | patch_stats(state, patch); |
4831 | 0 | if (!list || !state->apply_in_reverse) { |
4832 | 0 | *listp = patch; |
4833 | 0 | listp = &patch->next; |
4834 | 0 | } else { |
4835 | 0 | patch->next = list; |
4836 | 0 | list = patch; |
4837 | 0 | } |
4838 | |
|
4839 | 0 | if ((patch->new_name && |
4840 | 0 | ends_with_path_components(patch->new_name, |
4841 | 0 | GITATTRIBUTES_FILE)) || |
4842 | 0 | (patch->old_name && |
4843 | 0 | ends_with_path_components(patch->old_name, |
4844 | 0 | GITATTRIBUTES_FILE))) |
4845 | 0 | flush_attributes = 1; |
4846 | 0 | } |
4847 | 0 | else { |
4848 | 0 | if (state->apply_verbosity > verbosity_normal) |
4849 | 0 | say_patch_name(stderr, _("Skipped patch '%s'."), patch); |
4850 | 0 | free_patch(patch); |
4851 | 0 | skipped_patch++; |
4852 | 0 | } |
4853 | 0 | offset += nr; |
4854 | 0 | } |
4855 | | |
4856 | 0 | if (!list && !skipped_patch) { |
4857 | 0 | if (!state->allow_empty) { |
4858 | 0 | error(_("No valid patches in input (allow with \"--allow-empty\")")); |
4859 | 0 | res = -128; |
4860 | 0 | } |
4861 | 0 | goto end; |
4862 | 0 | } |
4863 | | |
4864 | 0 | if (state->whitespace_error && (state->ws_error_action == die_on_ws_error)) |
4865 | 0 | state->apply = 0; |
4866 | |
|
4867 | 0 | state->update_index = (state->check_index || state->ita_only) && state->apply; |
4868 | 0 | if (state->update_index && !is_lock_file_locked(&state->lock_file)) { |
4869 | 0 | if (state->index_file) |
4870 | 0 | hold_lock_file_for_update(&state->lock_file, |
4871 | 0 | state->index_file, |
4872 | 0 | LOCK_DIE_ON_ERROR); |
4873 | 0 | else |
4874 | 0 | repo_hold_locked_index(state->repo, &state->lock_file, |
4875 | 0 | LOCK_DIE_ON_ERROR); |
4876 | 0 | } |
4877 | |
|
4878 | 0 | if ((state->check_index || state->update_index) && read_apply_cache(state) < 0) { |
4879 | 0 | error(_("unable to read index file")); |
4880 | 0 | res = -128; |
4881 | 0 | goto end; |
4882 | 0 | } |
4883 | | |
4884 | 0 | if (state->check || state->apply) { |
4885 | 0 | int r = check_patch_list(state, list); |
4886 | 0 | if (r == -128) { |
4887 | 0 | res = -128; |
4888 | 0 | goto end; |
4889 | 0 | } |
4890 | 0 | if (r < 0 && !state->apply_with_reject) { |
4891 | 0 | res = -1; |
4892 | 0 | goto end; |
4893 | 0 | } |
4894 | 0 | } |
4895 | | |
4896 | 0 | if (state->apply) { |
4897 | 0 | int write_res = write_out_results(state, list); |
4898 | 0 | if (write_res < 0) { |
4899 | 0 | res = -128; |
4900 | 0 | goto end; |
4901 | 0 | } |
4902 | 0 | if (write_res > 0) { |
4903 | | /* with --3way, we still need to write the index out */ |
4904 | 0 | res = state->apply_with_reject ? -1 : 1; |
4905 | 0 | goto end; |
4906 | 0 | } |
4907 | 0 | } |
4908 | | |
4909 | 0 | if (state->fake_ancestor && |
4910 | 0 | build_fake_ancestor(state, list)) { |
4911 | 0 | res = -128; |
4912 | 0 | goto end; |
4913 | 0 | } |
4914 | | |
4915 | 0 | if (state->diffstat && state->apply_verbosity > verbosity_silent) |
4916 | 0 | stat_patch_list(state, list); |
4917 | |
|
4918 | 0 | if (state->numstat && state->apply_verbosity > verbosity_silent) |
4919 | 0 | numstat_patch_list(state, list); |
4920 | |
|
4921 | 0 | if (state->summary && state->apply_verbosity > verbosity_silent) |
4922 | 0 | summary_patch_list(list); |
4923 | |
|
4924 | 0 | if (flush_attributes) |
4925 | 0 | reset_parsed_attributes(); |
4926 | 0 | end: |
4927 | 0 | free_patch_list(list); |
4928 | 0 | strbuf_release(&buf); |
4929 | 0 | string_list_clear(&state->fn_table, 0); |
4930 | 0 | return res; |
4931 | 0 | } |
4932 | | |
4933 | | static int apply_option_parse_exclude(const struct option *opt, |
4934 | | const char *arg, int unset) |
4935 | 0 | { |
4936 | 0 | struct apply_state *state = opt->value; |
4937 | |
|
4938 | 0 | BUG_ON_OPT_NEG(unset); |
4939 | | |
4940 | 0 | add_name_limit(state, arg, 1); |
4941 | 0 | return 0; |
4942 | 0 | } |
4943 | | |
4944 | | static int apply_option_parse_include(const struct option *opt, |
4945 | | const char *arg, int unset) |
4946 | 0 | { |
4947 | 0 | struct apply_state *state = opt->value; |
4948 | |
|
4949 | 0 | BUG_ON_OPT_NEG(unset); |
4950 | | |
4951 | 0 | add_name_limit(state, arg, 0); |
4952 | 0 | state->has_include = 1; |
4953 | 0 | return 0; |
4954 | 0 | } |
4955 | | |
4956 | | static int apply_option_parse_p(const struct option *opt, |
4957 | | const char *arg, |
4958 | | int unset) |
4959 | 0 | { |
4960 | 0 | struct apply_state *state = opt->value; |
4961 | |
|
4962 | 0 | BUG_ON_OPT_NEG(unset); |
4963 | | |
4964 | 0 | state->p_value = atoi(arg); |
4965 | 0 | state->p_value_known = 1; |
4966 | 0 | return 0; |
4967 | 0 | } |
4968 | | |
4969 | | static int apply_option_parse_space_change(const struct option *opt, |
4970 | | const char *arg, int unset) |
4971 | 0 | { |
4972 | 0 | struct apply_state *state = opt->value; |
4973 | |
|
4974 | 0 | BUG_ON_OPT_ARG(arg); |
4975 | | |
4976 | 0 | if (unset) |
4977 | 0 | state->ws_ignore_action = ignore_ws_none; |
4978 | 0 | else |
4979 | 0 | state->ws_ignore_action = ignore_ws_change; |
4980 | 0 | return 0; |
4981 | 0 | } |
4982 | | |
4983 | | static int apply_option_parse_whitespace(const struct option *opt, |
4984 | | const char *arg, int unset) |
4985 | 0 | { |
4986 | 0 | struct apply_state *state = opt->value; |
4987 | |
|
4988 | 0 | BUG_ON_OPT_NEG(unset); |
4989 | | |
4990 | 0 | state->whitespace_option = arg; |
4991 | 0 | if (parse_whitespace_option(state, arg)) |
4992 | 0 | return -1; |
4993 | 0 | return 0; |
4994 | 0 | } |
4995 | | |
4996 | | static int apply_option_parse_directory(const struct option *opt, |
4997 | | const char *arg, int unset) |
4998 | 0 | { |
4999 | 0 | struct apply_state *state = opt->value; |
5000 | |
|
5001 | 0 | BUG_ON_OPT_NEG(unset); |
5002 | | |
5003 | 0 | strbuf_reset(&state->root); |
5004 | 0 | strbuf_addstr(&state->root, arg); |
5005 | 0 | strbuf_complete(&state->root, '/'); |
5006 | 0 | return 0; |
5007 | 0 | } |
5008 | | |
5009 | | int apply_all_patches(struct apply_state *state, |
5010 | | int argc, |
5011 | | const char **argv, |
5012 | | int options) |
5013 | 0 | { |
5014 | 0 | int i; |
5015 | 0 | int res; |
5016 | 0 | int errs = 0; |
5017 | 0 | int read_stdin = 1; |
5018 | |
|
5019 | 0 | for (i = 0; i < argc; i++) { |
5020 | 0 | const char *arg = argv[i]; |
5021 | 0 | char *to_free = NULL; |
5022 | 0 | int fd; |
5023 | |
|
5024 | 0 | if (!strcmp(arg, "-")) { |
5025 | 0 | res = apply_patch(state, 0, "<stdin>", options); |
5026 | 0 | if (res < 0) |
5027 | 0 | goto end; |
5028 | 0 | errs |= res; |
5029 | 0 | read_stdin = 0; |
5030 | 0 | continue; |
5031 | 0 | } else |
5032 | 0 | arg = to_free = prefix_filename(state->prefix, arg); |
5033 | | |
5034 | 0 | fd = open(arg, O_RDONLY); |
5035 | 0 | if (fd < 0) { |
5036 | 0 | error(_("can't open patch '%s': %s"), arg, strerror(errno)); |
5037 | 0 | res = -128; |
5038 | 0 | free(to_free); |
5039 | 0 | goto end; |
5040 | 0 | } |
5041 | 0 | read_stdin = 0; |
5042 | 0 | set_default_whitespace_mode(state); |
5043 | 0 | res = apply_patch(state, fd, arg, options); |
5044 | 0 | close(fd); |
5045 | 0 | free(to_free); |
5046 | 0 | if (res < 0) |
5047 | 0 | goto end; |
5048 | 0 | errs |= res; |
5049 | 0 | } |
5050 | 0 | set_default_whitespace_mode(state); |
5051 | 0 | if (read_stdin) { |
5052 | 0 | res = apply_patch(state, 0, "<stdin>", options); |
5053 | 0 | if (res < 0) |
5054 | 0 | goto end; |
5055 | 0 | errs |= res; |
5056 | 0 | } |
5057 | | |
5058 | 0 | if (state->whitespace_error) { |
5059 | 0 | if (state->squelch_whitespace_errors && |
5060 | 0 | state->squelch_whitespace_errors < state->whitespace_error) { |
5061 | 0 | int squelched = |
5062 | 0 | state->whitespace_error - state->squelch_whitespace_errors; |
5063 | 0 | warning(Q_("squelched %d whitespace error", |
5064 | 0 | "squelched %d whitespace errors", |
5065 | 0 | squelched), |
5066 | 0 | squelched); |
5067 | 0 | } |
5068 | 0 | if (state->ws_error_action == die_on_ws_error) { |
5069 | 0 | error(Q_("%d line adds whitespace errors.", |
5070 | 0 | "%d lines add whitespace errors.", |
5071 | 0 | state->whitespace_error), |
5072 | 0 | state->whitespace_error); |
5073 | 0 | res = -128; |
5074 | 0 | goto end; |
5075 | 0 | } |
5076 | 0 | if (state->applied_after_fixing_ws && state->apply) |
5077 | 0 | warning(Q_("%d line applied after" |
5078 | 0 | " fixing whitespace errors.", |
5079 | 0 | "%d lines applied after" |
5080 | 0 | " fixing whitespace errors.", |
5081 | 0 | state->applied_after_fixing_ws), |
5082 | 0 | state->applied_after_fixing_ws); |
5083 | 0 | else if (state->whitespace_error) |
5084 | 0 | warning(Q_("%d line adds whitespace errors.", |
5085 | 0 | "%d lines add whitespace errors.", |
5086 | 0 | state->whitespace_error), |
5087 | 0 | state->whitespace_error); |
5088 | 0 | } |
5089 | | |
5090 | 0 | if (state->update_index) { |
5091 | 0 | res = write_locked_index(state->repo->index, &state->lock_file, COMMIT_LOCK); |
5092 | 0 | if (res) { |
5093 | 0 | error(_("Unable to write new index file")); |
5094 | 0 | res = -128; |
5095 | 0 | goto end; |
5096 | 0 | } |
5097 | 0 | } |
5098 | | |
5099 | 0 | res = !!errs; |
5100 | |
|
5101 | 0 | end: |
5102 | 0 | rollback_lock_file(&state->lock_file); |
5103 | |
|
5104 | 0 | if (state->apply_verbosity <= verbosity_silent) { |
5105 | 0 | set_error_routine(state->saved_error_routine); |
5106 | 0 | set_warn_routine(state->saved_warn_routine); |
5107 | 0 | } |
5108 | |
|
5109 | 0 | if (res > -1) |
5110 | 0 | return res; |
5111 | 0 | return (res == -1 ? 1 : 128); |
5112 | 0 | } |
5113 | | |
5114 | | int apply_parse_options(int argc, const char **argv, |
5115 | | struct apply_state *state, |
5116 | | int *force_apply, int *options, |
5117 | | const char * const *apply_usage) |
5118 | 0 | { |
5119 | 0 | struct option builtin_apply_options[] = { |
5120 | 0 | OPT_CALLBACK_F(0, "exclude", state, N_("path"), |
5121 | 0 | N_("don't apply changes matching the given path"), |
5122 | 0 | PARSE_OPT_NONEG, apply_option_parse_exclude), |
5123 | 0 | OPT_CALLBACK_F(0, "include", state, N_("path"), |
5124 | 0 | N_("apply changes matching the given path"), |
5125 | 0 | PARSE_OPT_NONEG, apply_option_parse_include), |
5126 | 0 | OPT_CALLBACK('p', NULL, state, N_("num"), |
5127 | 0 | N_("remove <num> leading slashes from traditional diff paths"), |
5128 | 0 | apply_option_parse_p), |
5129 | 0 | OPT_BOOL(0, "no-add", &state->no_add, |
5130 | 0 | N_("ignore additions made by the patch")), |
5131 | 0 | OPT_BOOL(0, "stat", &state->diffstat, |
5132 | 0 | N_("instead of applying the patch, output diffstat for the input")), |
5133 | 0 | OPT_NOOP_NOARG(0, "allow-binary-replacement"), |
5134 | 0 | OPT_NOOP_NOARG(0, "binary"), |
5135 | 0 | OPT_BOOL(0, "numstat", &state->numstat, |
5136 | 0 | N_("show number of added and deleted lines in decimal notation")), |
5137 | 0 | OPT_BOOL(0, "summary", &state->summary, |
5138 | 0 | N_("instead of applying the patch, output a summary for the input")), |
5139 | 0 | OPT_BOOL(0, "check", &state->check, |
5140 | 0 | N_("instead of applying the patch, see if the patch is applicable")), |
5141 | 0 | OPT_BOOL(0, "index", &state->check_index, |
5142 | 0 | N_("make sure the patch is applicable to the current index")), |
5143 | 0 | OPT_BOOL('N', "intent-to-add", &state->ita_only, |
5144 | 0 | N_("mark new files with `git add --intent-to-add`")), |
5145 | 0 | OPT_BOOL(0, "cached", &state->cached, |
5146 | 0 | N_("apply a patch without touching the working tree")), |
5147 | 0 | OPT_BOOL_F(0, "unsafe-paths", &state->unsafe_paths, |
5148 | 0 | N_("accept a patch that touches outside the working area"), |
5149 | 0 | PARSE_OPT_NOCOMPLETE), |
5150 | 0 | OPT_BOOL(0, "apply", force_apply, |
5151 | 0 | N_("also apply the patch (use with --stat/--summary/--check)")), |
5152 | 0 | OPT_BOOL('3', "3way", &state->threeway, |
5153 | 0 | N_( "attempt three-way merge, fall back on normal patch if that fails")), |
5154 | 0 | OPT_SET_INT_F(0, "ours", &state->merge_variant, |
5155 | 0 | N_("for conflicts, use our version"), |
5156 | 0 | XDL_MERGE_FAVOR_OURS, PARSE_OPT_NONEG), |
5157 | 0 | OPT_SET_INT_F(0, "theirs", &state->merge_variant, |
5158 | 0 | N_("for conflicts, use their version"), |
5159 | 0 | XDL_MERGE_FAVOR_THEIRS, PARSE_OPT_NONEG), |
5160 | 0 | OPT_SET_INT_F(0, "union", &state->merge_variant, |
5161 | 0 | N_("for conflicts, use a union version"), |
5162 | 0 | XDL_MERGE_FAVOR_UNION, PARSE_OPT_NONEG), |
5163 | 0 | OPT_FILENAME(0, "build-fake-ancestor", &state->fake_ancestor, |
5164 | 0 | N_("build a temporary index based on embedded index information")), |
5165 | | /* Think twice before adding "--nul" synonym to this */ |
5166 | 0 | OPT_SET_INT('z', NULL, &state->line_termination, |
5167 | 0 | N_("paths are separated with NUL character"), '\0'), |
5168 | 0 | OPT_UNSIGNED('C', NULL, &state->p_context, |
5169 | 0 | N_("ensure at least <n> lines of context match")), |
5170 | 0 | OPT_CALLBACK(0, "whitespace", state, N_("action"), |
5171 | 0 | N_("detect new or modified lines that have whitespace errors"), |
5172 | 0 | apply_option_parse_whitespace), |
5173 | 0 | OPT_CALLBACK_F(0, "ignore-space-change", state, NULL, |
5174 | 0 | N_("ignore changes in whitespace when finding context"), |
5175 | 0 | PARSE_OPT_NOARG, apply_option_parse_space_change), |
5176 | 0 | OPT_CALLBACK_F(0, "ignore-whitespace", state, NULL, |
5177 | 0 | N_("ignore changes in whitespace when finding context"), |
5178 | 0 | PARSE_OPT_NOARG, apply_option_parse_space_change), |
5179 | 0 | OPT_BOOL('R', "reverse", &state->apply_in_reverse, |
5180 | 0 | N_("apply the patch in reverse")), |
5181 | 0 | OPT_BOOL(0, "unidiff-zero", &state->unidiff_zero, |
5182 | 0 | N_("don't expect at least one line of context")), |
5183 | 0 | OPT_BOOL(0, "reject", &state->apply_with_reject, |
5184 | 0 | N_("leave the rejected hunks in corresponding *.rej files")), |
5185 | 0 | OPT_BOOL(0, "allow-overlap", &state->allow_overlap, |
5186 | 0 | N_("allow overlapping hunks")), |
5187 | 0 | OPT__VERBOSITY(&state->apply_verbosity), |
5188 | 0 | OPT_BIT(0, "inaccurate-eof", options, |
5189 | 0 | N_("tolerate incorrectly detected missing new-line at the end of file"), |
5190 | 0 | APPLY_OPT_INACCURATE_EOF), |
5191 | 0 | OPT_BIT(0, "recount", options, |
5192 | 0 | N_("do not trust the line counts in the hunk headers"), |
5193 | 0 | APPLY_OPT_RECOUNT), |
5194 | 0 | OPT_CALLBACK(0, "directory", state, N_("root"), |
5195 | 0 | N_("prepend <root> to all filenames"), |
5196 | 0 | apply_option_parse_directory), |
5197 | 0 | OPT_BOOL(0, "allow-empty", &state->allow_empty, |
5198 | 0 | N_("don't return error for empty patches")), |
5199 | 0 | OPT_END() |
5200 | 0 | }; |
5201 | |
|
5202 | 0 | argc = parse_options(argc, argv, state->prefix, builtin_apply_options, apply_usage, 0); |
5203 | |
|
5204 | 0 | if (state->merge_variant && !state->threeway) |
5205 | 0 | die(_("--ours, --theirs, and --union require --3way")); |
5206 | | |
5207 | 0 | return argc; |
5208 | 0 | } |