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