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#include "cache.h" 10#include "cache-tree.h" 11#include "quote.h" 12#include "blob.h" 13#include "delta.h" 14#include "builtin.h" 15#include "string-list.h" 16#include "dir.h" 17#include "parse-options.h" 18 19/* 20 * --check turns on checking that the working tree matches the 21 * files that are being modified, but doesn't apply the patch 22 * --stat does just a diffstat, and doesn't actually apply 23 * --numstat does numeric diffstat, and doesn't actually apply 24 * --index-info shows the old and new index info for paths if available. 25 * --index updates the cache as well. 26 * --cached updates only the cache without ever touching the working tree. 27 */ 28static const char *prefix; 29static int prefix_length = -1; 30static int newfd = -1; 31 32static int unidiff_zero; 33static int p_value = 1; 34static int p_value_known; 35static int check_index; 36static int update_index; 37static int cached; 38static int diffstat; 39static int numstat; 40static int summary; 41static int check; 42static int apply = 1; 43static int apply_in_reverse; 44static int apply_with_reject; 45static int apply_verbosely; 46static int no_add; 47static const char *fake_ancestor; 48static int line_termination = '\n'; 49static unsigned int p_context = UINT_MAX; 50static const char * const apply_usage[] = { 51 "git apply [options] [<patch>...]", 52 NULL 53}; 54 55static enum ws_error_action { 56 nowarn_ws_error, 57 warn_on_ws_error, 58 die_on_ws_error, 59 correct_ws_error 60} ws_error_action = warn_on_ws_error; 61static int whitespace_error; 62static int squelch_whitespace_errors = 5; 63static int applied_after_fixing_ws; 64 65static enum ws_ignore { 66 ignore_ws_none, 67 ignore_ws_change 68} ws_ignore_action = ignore_ws_none; 69 70 71static const char *patch_input_file; 72static const char *root; 73static int root_len; 74static int read_stdin = 1; 75static int options; 76 77static void parse_whitespace_option(const char *option) 78{ 79 if (!option) { 80 ws_error_action = warn_on_ws_error; 81 return; 82 } 83 if (!strcmp(option, "warn")) { 84 ws_error_action = warn_on_ws_error; 85 return; 86 } 87 if (!strcmp(option, "nowarn")) { 88 ws_error_action = nowarn_ws_error; 89 return; 90 } 91 if (!strcmp(option, "error")) { 92 ws_error_action = die_on_ws_error; 93 return; 94 } 95 if (!strcmp(option, "error-all")) { 96 ws_error_action = die_on_ws_error; 97 squelch_whitespace_errors = 0; 98 return; 99 } 100 if (!strcmp(option, "strip") || !strcmp(option, "fix")) { 101 ws_error_action = correct_ws_error; 102 return; 103 } 104 die("unrecognized whitespace option '%s'", option); 105} 106 107static void parse_ignorewhitespace_option(const char *option) 108{ 109 if (!option || !strcmp(option, "no") || 110 !strcmp(option, "false") || !strcmp(option, "never") || 111 !strcmp(option, "none")) { 112 ws_ignore_action = ignore_ws_none; 113 return; 114 } 115 if (!strcmp(option, "change")) { 116 ws_ignore_action = ignore_ws_change; 117 return; 118 } 119 die("unrecognized whitespace ignore option '%s'", option); 120} 121 122static void set_default_whitespace_mode(const char *whitespace_option) 123{ 124 if (!whitespace_option && !apply_default_whitespace) 125 ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error); 126} 127 128/* 129 * For "diff-stat" like behaviour, we keep track of the biggest change 130 * we've seen, and the longest filename. That allows us to do simple 131 * scaling. 132 */ 133static int max_change, max_len; 134 135/* 136 * Various "current state", notably line numbers and what 137 * file (and how) we're patching right now.. The "is_xxxx" 138 * things are flags, where -1 means "don't know yet". 139 */ 140static int linenr = 1; 141 142/* 143 * This represents one "hunk" from a patch, starting with 144 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The 145 * patch text is pointed at by patch, and its byte length 146 * is stored in size. leading and trailing are the number 147 * of context lines. 148 */ 149struct fragment { 150 unsigned long leading, trailing; 151 unsigned long oldpos, oldlines; 152 unsigned long newpos, newlines; 153 const char *patch; 154 int size; 155 int rejected; 156 int linenr; 157 struct fragment *next; 158}; 159 160/* 161 * When dealing with a binary patch, we reuse "leading" field 162 * to store the type of the binary hunk, either deflated "delta" 163 * or deflated "literal". 164 */ 165#define binary_patch_method leading 166#define BINARY_DELTA_DEFLATED 1 167#define BINARY_LITERAL_DEFLATED 2 168 169/* 170 * This represents a "patch" to a file, both metainfo changes 171 * such as creation/deletion, filemode and content changes represented 172 * as a series of fragments. 173 */ 174struct patch { 175 char *new_name, *old_name, *def_name; 176 unsigned int old_mode, new_mode; 177 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */ 178 int rejected; 179 unsigned ws_rule; 180 unsigned long deflate_origlen; 181 int lines_added, lines_deleted; 182 int score; 183 unsigned int is_toplevel_relative:1; 184 unsigned int inaccurate_eof:1; 185 unsigned int is_binary:1; 186 unsigned int is_copy:1; 187 unsigned int is_rename:1; 188 unsigned int recount:1; 189 struct fragment *fragments; 190 char *result; 191 size_t resultsize; 192 char old_sha1_prefix[41]; 193 char new_sha1_prefix[41]; 194 struct patch *next; 195}; 196 197/* 198 * A line in a file, len-bytes long (includes the terminating LF, 199 * except for an incomplete line at the end if the file ends with 200 * one), and its contents hashes to 'hash'. 201 */ 202struct line { 203 size_t len; 204 unsigned hash : 24; 205 unsigned flag : 8; 206#define LINE_COMMON 1 207#define LINE_PATCHED 2 208}; 209 210/* 211 * This represents a "file", which is an array of "lines". 212 */ 213struct image { 214 char *buf; 215 size_t len; 216 size_t nr; 217 size_t alloc; 218 struct line *line_allocated; 219 struct line *line; 220}; 221 222/* 223 * Records filenames that have been touched, in order to handle 224 * the case where more than one patches touch the same file. 225 */ 226 227static struct string_list fn_table; 228 229static uint32_t hash_line(const char *cp, size_t len) 230{ 231 size_t i; 232 uint32_t h; 233 for (i = 0, h = 0; i < len; i++) { 234 if (!isspace(cp[i])) { 235 h = h * 3 + (cp[i] & 0xff); 236 } 237 } 238 return h; 239} 240 241/* 242 * Compare lines s1 of length n1 and s2 of length n2, ignoring 243 * whitespace difference. Returns 1 if they match, 0 otherwise 244 */ 245static int fuzzy_matchlines(const char *s1, size_t n1, 246 const char *s2, size_t n2) 247{ 248 const char *last1 = s1 + n1 - 1; 249 const char *last2 = s2 + n2 - 1; 250 int result = 0; 251 252 if (n1 < 0 || n2 < 0) 253 return 0; 254 255 /* ignore line endings */ 256 while ((*last1 == '\r') || (*last1 == '\n')) 257 last1--; 258 while ((*last2 == '\r') || (*last2 == '\n')) 259 last2--; 260 261 /* skip leading whitespace */ 262 while (isspace(*s1) && (s1 <= last1)) 263 s1++; 264 while (isspace(*s2) && (s2 <= last2)) 265 s2++; 266 /* early return if both lines are empty */ 267 if ((s1 > last1) && (s2 > last2)) 268 return 1; 269 while (!result) { 270 result = *s1++ - *s2++; 271 /* 272 * Skip whitespace inside. We check for whitespace on 273 * both buffers because we don't want "a b" to match 274 * "ab" 275 */ 276 if (isspace(*s1) && isspace(*s2)) { 277 while (isspace(*s1) && s1 <= last1) 278 s1++; 279 while (isspace(*s2) && s2 <= last2) 280 s2++; 281 } 282 /* 283 * If we reached the end on one side only, 284 * lines don't match 285 */ 286 if ( 287 ((s2 > last2) && (s1 <= last1)) || 288 ((s1 > last1) && (s2 <= last2))) 289 return 0; 290 if ((s1 > last1) && (s2 > last2)) 291 break; 292 } 293 294 return !result; 295} 296 297static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag) 298{ 299 ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc); 300 img->line_allocated[img->nr].len = len; 301 img->line_allocated[img->nr].hash = hash_line(bol, len); 302 img->line_allocated[img->nr].flag = flag; 303 img->nr++; 304} 305 306static void prepare_image(struct image *image, char *buf, size_t len, 307 int prepare_linetable) 308{ 309 const char *cp, *ep; 310 311 memset(image, 0, sizeof(*image)); 312 image->buf = buf; 313 image->len = len; 314 315 if (!prepare_linetable) 316 return; 317 318 ep = image->buf + image->len; 319 cp = image->buf; 320 while (cp < ep) { 321 const char *next; 322 for (next = cp; next < ep && *next != '\n'; next++) 323 ; 324 if (next < ep) 325 next++; 326 add_line_info(image, cp, next - cp, 0); 327 cp = next; 328 } 329 image->line = image->line_allocated; 330} 331 332static void clear_image(struct image *image) 333{ 334 free(image->buf); 335 image->buf = NULL; 336 image->len = 0; 337} 338 339static void say_patch_name(FILE *output, const char *pre, 340 struct patch *patch, const char *post) 341{ 342 fputs(pre, output); 343 if (patch->old_name && patch->new_name && 344 strcmp(patch->old_name, patch->new_name)) { 345 quote_c_style(patch->old_name, NULL, output, 0); 346 fputs(" => ", output); 347 quote_c_style(patch->new_name, NULL, output, 0); 348 } else { 349 const char *n = patch->new_name; 350 if (!n) 351 n = patch->old_name; 352 quote_c_style(n, NULL, output, 0); 353 } 354 fputs(post, output); 355} 356 357#define CHUNKSIZE (8192) 358#define SLOP (16) 359 360static void read_patch_file(struct strbuf *sb, int fd) 361{ 362 if (strbuf_read(sb, fd, 0) < 0) 363 die_errno("git apply: failed to read"); 364 365 /* 366 * Make sure that we have some slop in the buffer 367 * so that we can do speculative "memcmp" etc, and 368 * see to it that it is NUL-filled. 369 */ 370 strbuf_grow(sb, SLOP); 371 memset(sb->buf + sb->len, 0, SLOP); 372} 373 374static unsigned long linelen(const char *buffer, unsigned long size) 375{ 376 unsigned long len = 0; 377 while (size--) { 378 len++; 379 if (*buffer++ == '\n') 380 break; 381 } 382 return len; 383} 384 385static int is_dev_null(const char *str) 386{ 387 return !memcmp("/dev/null", str, 9) && isspace(str[9]); 388} 389 390#define TERM_SPACE 1 391#define TERM_TAB 2 392 393static int name_terminate(const char *name, int namelen, int c, int terminate) 394{ 395 if (c == ' ' && !(terminate & TERM_SPACE)) 396 return 0; 397 if (c == '\t' && !(terminate & TERM_TAB)) 398 return 0; 399 400 return 1; 401} 402 403/* remove double slashes to make --index work with such filenames */ 404static char *squash_slash(char *name) 405{ 406 int i = 0, j = 0; 407 408 if (!name) 409 return NULL; 410 411 while (name[i]) { 412 if ((name[j++] = name[i++]) == '/') 413 while (name[i] == '/') 414 i++; 415 } 416 name[j] = '\0'; 417 return name; 418} 419 420static char *find_name_gnu(const char *line, char *def, int p_value) 421{ 422 struct strbuf name = STRBUF_INIT; 423 char *cp; 424 425 /* 426 * Proposed "new-style" GNU patch/diff format; see 427 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2 428 */ 429 if (unquote_c_style(&name, line, NULL)) { 430 strbuf_release(&name); 431 return NULL; 432 } 433 434 for (cp = name.buf; p_value; p_value--) { 435 cp = strchr(cp, '/'); 436 if (!cp) { 437 strbuf_release(&name); 438 return NULL; 439 } 440 cp++; 441 } 442 443 /* name can later be freed, so we need 444 * to memmove, not just return cp 445 */ 446 strbuf_remove(&name, 0, cp - name.buf); 447 free(def); 448 if (root) 449 strbuf_insert(&name, 0, root, root_len); 450 return squash_slash(strbuf_detach(&name, NULL)); 451} 452 453static size_t sane_tz_len(const char *line, size_t len) 454{ 455 const char *tz, *p; 456 457 if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ') 458 return 0; 459 tz = line + len - strlen(" +0500"); 460 461 if (tz[1] != '+' && tz[1] != '-') 462 return 0; 463 464 for (p = tz + 2; p != line + len; p++) 465 if (!isdigit(*p)) 466 return 0; 467 468 return line + len - tz; 469} 470 471static size_t tz_with_colon_len(const char *line, size_t len) 472{ 473 const char *tz, *p; 474 475 if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':') 476 return 0; 477 tz = line + len - strlen(" +08:00"); 478 479 if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-')) 480 return 0; 481 p = tz + 2; 482 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || 483 !isdigit(*p++) || !isdigit(*p++)) 484 return 0; 485 486 return line + len - tz; 487} 488 489static size_t date_len(const char *line, size_t len) 490{ 491 const char *date, *p; 492 493 if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-') 494 return 0; 495 p = date = line + len - strlen("72-02-05"); 496 497 if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' || 498 !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' || 499 !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */ 500 return 0; 501 502 if (date - line >= strlen("19") && 503 isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */ 504 date -= strlen("19"); 505 506 return line + len - date; 507} 508 509static size_t short_time_len(const char *line, size_t len) 510{ 511 const char *time, *p; 512 513 if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':') 514 return 0; 515 p = time = line + len - strlen(" 07:01:32"); 516 517 /* Permit 1-digit hours? */ 518 if (*p++ != ' ' || 519 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || 520 !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' || 521 !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */ 522 return 0; 523 524 return line + len - time; 525} 526 527static size_t fractional_time_len(const char *line, size_t len) 528{ 529 const char *p; 530 size_t n; 531 532 /* Expected format: 19:41:17.620000023 */ 533 if (!len || !isdigit(line[len - 1])) 534 return 0; 535 p = line + len - 1; 536 537 /* Fractional seconds. */ 538 while (p > line && isdigit(*p)) 539 p--; 540 if (*p != '.') 541 return 0; 542 543 /* Hours, minutes, and whole seconds. */ 544 n = short_time_len(line, p - line); 545 if (!n) 546 return 0; 547 548 return line + len - p + n; 549} 550 551static size_t trailing_spaces_len(const char *line, size_t len) 552{ 553 const char *p; 554 555 /* Expected format: ' ' x (1 or more) */ 556 if (!len || line[len - 1] != ' ') 557 return 0; 558 559 p = line + len; 560 while (p != line) { 561 p--; 562 if (*p != ' ') 563 return line + len - (p + 1); 564 } 565 566 /* All spaces! */ 567 return len; 568} 569 570static size_t diff_timestamp_len(const char *line, size_t len) 571{ 572 const char *end = line + len; 573 size_t n; 574 575 /* 576 * Posix: 2010-07-05 19:41:17 577 * GNU: 2010-07-05 19:41:17.620000023 -0500 578 */ 579 580 if (!isdigit(end[-1])) 581 return 0; 582 583 n = sane_tz_len(line, end - line); 584 if (!n) 585 n = tz_with_colon_len(line, end - line); 586 end -= n; 587 588 n = short_time_len(line, end - line); 589 if (!n) 590 n = fractional_time_len(line, end - line); 591 end -= n; 592 593 n = date_len(line, end - line); 594 if (!n) /* No date. Too bad. */ 595 return 0; 596 end -= n; 597 598 if (end == line) /* No space before date. */ 599 return 0; 600 if (end[-1] == '\t') { /* Success! */ 601 end--; 602 return line + len - end; 603 } 604 if (end[-1] != ' ') /* No space before date. */ 605 return 0; 606 607 /* Whitespace damage. */ 608 end -= trailing_spaces_len(line, end - line); 609 return line + len - end; 610} 611 612static char *find_name_common(const char *line, char *def, int p_value, 613 const char *end, int terminate) 614{ 615 int len; 616 const char *start = NULL; 617 618 if (p_value == 0) 619 start = line; 620 while (line != end) { 621 char c = *line; 622 623 if (!end && isspace(c)) { 624 if (c == '\n') 625 break; 626 if (name_terminate(start, line-start, c, terminate)) 627 break; 628 } 629 line++; 630 if (c == '/' && !--p_value) 631 start = line; 632 } 633 if (!start) 634 return squash_slash(def); 635 len = line - start; 636 if (!len) 637 return squash_slash(def); 638 639 /* 640 * Generally we prefer the shorter name, especially 641 * if the other one is just a variation of that with 642 * something else tacked on to the end (ie "file.orig" 643 * or "file~"). 644 */ 645 if (def) { 646 int deflen = strlen(def); 647 if (deflen < len && !strncmp(start, def, deflen)) 648 return squash_slash(def); 649 free(def); 650 } 651 652 if (root) { 653 char *ret = xmalloc(root_len + len + 1); 654 strcpy(ret, root); 655 memcpy(ret + root_len, start, len); 656 ret[root_len + len] = '\0'; 657 return squash_slash(ret); 658 } 659 660 return squash_slash(xmemdupz(start, len)); 661} 662 663static char *find_name(const char *line, char *def, int p_value, int terminate) 664{ 665 if (*line == '"') { 666 char *name = find_name_gnu(line, def, p_value); 667 if (name) 668 return name; 669 } 670 671 return find_name_common(line, def, p_value, NULL, terminate); 672} 673 674static char *find_name_traditional(const char *line, char *def, int p_value) 675{ 676 size_t len = strlen(line); 677 size_t date_len; 678 679 if (*line == '"') { 680 char *name = find_name_gnu(line, def, p_value); 681 if (name) 682 return name; 683 } 684 685 len = strchrnul(line, '\n') - line; 686 date_len = diff_timestamp_len(line, len); 687 if (!date_len) 688 return find_name_common(line, def, p_value, NULL, TERM_TAB); 689 len -= date_len; 690 691 return find_name_common(line, def, p_value, line + len, 0); 692} 693 694static int count_slashes(const char *cp) 695{ 696 int cnt = 0; 697 char ch; 698 699 while ((ch = *cp++)) 700 if (ch == '/') 701 cnt++; 702 return cnt; 703} 704 705/* 706 * Given the string after "--- " or "+++ ", guess the appropriate 707 * p_value for the given patch. 708 */ 709static int guess_p_value(const char *nameline) 710{ 711 char *name, *cp; 712 int val = -1; 713 714 if (is_dev_null(nameline)) 715 return -1; 716 name = find_name_traditional(nameline, NULL, 0); 717 if (!name) 718 return -1; 719 cp = strchr(name, '/'); 720 if (!cp) 721 val = 0; 722 else if (prefix) { 723 /* 724 * Does it begin with "a/$our-prefix" and such? Then this is 725 * very likely to apply to our directory. 726 */ 727 if (!strncmp(name, prefix, prefix_length)) 728 val = count_slashes(prefix); 729 else { 730 cp++; 731 if (!strncmp(cp, prefix, prefix_length)) 732 val = count_slashes(prefix) + 1; 733 } 734 } 735 free(name); 736 return val; 737} 738 739/* 740 * Does the ---/+++ line has the POSIX timestamp after the last HT? 741 * GNU diff puts epoch there to signal a creation/deletion event. Is 742 * this such a timestamp? 743 */ 744static int has_epoch_timestamp(const char *nameline) 745{ 746 /* 747 * We are only interested in epoch timestamp; any non-zero 748 * fraction cannot be one, hence "(\.0+)?" in the regexp below. 749 * For the same reason, the date must be either 1969-12-31 or 750 * 1970-01-01, and the seconds part must be "00". 751 */ 752 const char stamp_regexp[] = 753 "^(1969-12-31|1970-01-01)" 754 " " 755 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?" 756 " " 757 "([-+][0-2][0-9]:?[0-5][0-9])\n"; 758 const char *timestamp = NULL, *cp, *colon; 759 static regex_t *stamp; 760 regmatch_t m[10]; 761 int zoneoffset; 762 int hourminute; 763 int status; 764 765 for (cp = nameline; *cp != '\n'; cp++) { 766 if (*cp == '\t') 767 timestamp = cp + 1; 768 } 769 if (!timestamp) 770 return 0; 771 if (!stamp) { 772 stamp = xmalloc(sizeof(*stamp)); 773 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) { 774 warning("Cannot prepare timestamp regexp %s", 775 stamp_regexp); 776 return 0; 777 } 778 } 779 780 status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0); 781 if (status) { 782 if (status != REG_NOMATCH) 783 warning("regexec returned %d for input: %s", 784 status, timestamp); 785 return 0; 786 } 787 788 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10); 789 if (*colon == ':') 790 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10); 791 else 792 zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100); 793 if (timestamp[m[3].rm_so] == '-') 794 zoneoffset = -zoneoffset; 795 796 /* 797 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31 798 * (west of GMT) or 1970-01-01 (east of GMT) 799 */ 800 if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) || 801 (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10))) 802 return 0; 803 804 hourminute = (strtol(timestamp + 11, NULL, 10) * 60 + 805 strtol(timestamp + 14, NULL, 10) - 806 zoneoffset); 807 808 return ((zoneoffset < 0 && hourminute == 1440) || 809 (0 <= zoneoffset && !hourminute)); 810} 811 812/* 813 * Get the name etc info from the ---/+++ lines of a traditional patch header 814 * 815 * FIXME! The end-of-filename heuristics are kind of screwy. For existing 816 * files, we can happily check the index for a match, but for creating a 817 * new file we should try to match whatever "patch" does. I have no idea. 818 */ 819static void parse_traditional_patch(const char *first, const char *second, struct patch *patch) 820{ 821 char *name; 822 823 first += 4; /* skip "--- " */ 824 second += 4; /* skip "+++ " */ 825 if (!p_value_known) { 826 int p, q; 827 p = guess_p_value(first); 828 q = guess_p_value(second); 829 if (p < 0) p = q; 830 if (0 <= p && p == q) { 831 p_value = p; 832 p_value_known = 1; 833 } 834 } 835 if (is_dev_null(first)) { 836 patch->is_new = 1; 837 patch->is_delete = 0; 838 name = find_name_traditional(second, NULL, p_value); 839 patch->new_name = name; 840 } else if (is_dev_null(second)) { 841 patch->is_new = 0; 842 patch->is_delete = 1; 843 name = find_name_traditional(first, NULL, p_value); 844 patch->old_name = name; 845 } else { 846 name = find_name_traditional(first, NULL, p_value); 847 name = find_name_traditional(second, name, p_value); 848 if (has_epoch_timestamp(first)) { 849 patch->is_new = 1; 850 patch->is_delete = 0; 851 patch->new_name = name; 852 } else if (has_epoch_timestamp(second)) { 853 patch->is_new = 0; 854 patch->is_delete = 1; 855 patch->old_name = name; 856 } else { 857 patch->old_name = patch->new_name = name; 858 } 859 } 860 if (!name) 861 die("unable to find filename in patch at line %d", linenr); 862} 863 864static int gitdiff_hdrend(const char *line, struct patch *patch) 865{ 866 return -1; 867} 868 869/* 870 * We're anal about diff header consistency, to make 871 * sure that we don't end up having strange ambiguous 872 * patches floating around. 873 * 874 * As a result, gitdiff_{old|new}name() will check 875 * their names against any previous information, just 876 * to make sure.. 877 */ 878static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew) 879{ 880 if (!orig_name && !isnull) 881 return find_name(line, NULL, p_value, TERM_TAB); 882 883 if (orig_name) { 884 int len; 885 const char *name; 886 char *another; 887 name = orig_name; 888 len = strlen(name); 889 if (isnull) 890 die("git apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); 891 another = find_name(line, NULL, p_value, TERM_TAB); 892 if (!another || memcmp(another, name, len + 1)) 893 die("git apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); 894 free(another); 895 return orig_name; 896 } 897 else { 898 /* expect "/dev/null" */ 899 if (memcmp("/dev/null", line, 9) || line[9] != '\n') 900 die("git apply: bad git-diff - expected /dev/null on line %d", linenr); 901 return NULL; 902 } 903} 904 905static int gitdiff_oldname(const char *line, struct patch *patch) 906{ 907 patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name, "old"); 908 return 0; 909} 910 911static int gitdiff_newname(const char *line, struct patch *patch) 912{ 913 patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name, "new"); 914 return 0; 915} 916 917static int gitdiff_oldmode(const char *line, struct patch *patch) 918{ 919 patch->old_mode = strtoul(line, NULL, 8); 920 return 0; 921} 922 923static int gitdiff_newmode(const char *line, struct patch *patch) 924{ 925 patch->new_mode = strtoul(line, NULL, 8); 926 return 0; 927} 928 929static int gitdiff_delete(const char *line, struct patch *patch) 930{ 931 patch->is_delete = 1; 932 patch->old_name = patch->def_name; 933 return gitdiff_oldmode(line, patch); 934} 935 936static int gitdiff_newfile(const char *line, struct patch *patch) 937{ 938 patch->is_new = 1; 939 patch->new_name = patch->def_name; 940 return gitdiff_newmode(line, patch); 941} 942 943static int gitdiff_copysrc(const char *line, struct patch *patch) 944{ 945 patch->is_copy = 1; 946 patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); 947 return 0; 948} 949 950static int gitdiff_copydst(const char *line, struct patch *patch) 951{ 952 patch->is_copy = 1; 953 patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); 954 return 0; 955} 956 957static int gitdiff_renamesrc(const char *line, struct patch *patch) 958{ 959 patch->is_rename = 1; 960 patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); 961 return 0; 962} 963 964static int gitdiff_renamedst(const char *line, struct patch *patch) 965{ 966 patch->is_rename = 1; 967 patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0); 968 return 0; 969} 970 971static int gitdiff_similarity(const char *line, struct patch *patch) 972{ 973 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) 974 patch->score = 0; 975 return 0; 976} 977 978static int gitdiff_dissimilarity(const char *line, struct patch *patch) 979{ 980 if ((patch->score = strtoul(line, NULL, 10)) == ULONG_MAX) 981 patch->score = 0; 982 return 0; 983} 984 985static int gitdiff_index(const char *line, struct patch *patch) 986{ 987 /* 988 * index line is N hexadecimal, "..", N hexadecimal, 989 * and optional space with octal mode. 990 */ 991 const char *ptr, *eol; 992 int len; 993 994 ptr = strchr(line, '.'); 995 if (!ptr || ptr[1] != '.' || 40 < ptr - line) 996 return 0; 997 len = ptr - line; 998 memcpy(patch->old_sha1_prefix, line, len); 999 patch->old_sha1_prefix[len] = 0;10001001 line = ptr + 2;1002 ptr = strchr(line, ' ');1003 eol = strchr(line, '\n');10041005 if (!ptr || eol < ptr)1006 ptr = eol;1007 len = ptr - line;10081009 if (40 < len)1010 return 0;1011 memcpy(patch->new_sha1_prefix, line, len);1012 patch->new_sha1_prefix[len] = 0;1013 if (*ptr == ' ')1014 patch->old_mode = strtoul(ptr+1, NULL, 8);1015 return 0;1016}10171018/*1019 * This is normal for a diff that doesn't change anything: we'll fall through1020 * into the next diff. Tell the parser to break out.1021 */1022static int gitdiff_unrecognized(const char *line, struct patch *patch)1023{1024 return -1;1025}10261027static const char *stop_at_slash(const char *line, int llen)1028{1029 int nslash = p_value;1030 int i;10311032 for (i = 0; i < llen; i++) {1033 int ch = line[i];1034 if (ch == '/' && --nslash <= 0)1035 return &line[i];1036 }1037 return NULL;1038}10391040/*1041 * This is to extract the same name that appears on "diff --git"1042 * line. We do not find and return anything if it is a rename1043 * patch, and it is OK because we will find the name elsewhere.1044 * We need to reliably find name only when it is mode-change only,1045 * creation or deletion of an empty file. In any of these cases,1046 * both sides are the same name under a/ and b/ respectively.1047 */1048static char *git_header_name(char *line, int llen)1049{1050 const char *name;1051 const char *second = NULL;1052 size_t len, line_len;10531054 line += strlen("diff --git ");1055 llen -= strlen("diff --git ");10561057 if (*line == '"') {1058 const char *cp;1059 struct strbuf first = STRBUF_INIT;1060 struct strbuf sp = STRBUF_INIT;10611062 if (unquote_c_style(&first, line, &second))1063 goto free_and_fail1;10641065 /* advance to the first slash */1066 cp = stop_at_slash(first.buf, first.len);1067 /* we do not accept absolute paths */1068 if (!cp || cp == first.buf)1069 goto free_and_fail1;1070 strbuf_remove(&first, 0, cp + 1 - first.buf);10711072 /*1073 * second points at one past closing dq of name.1074 * find the second name.1075 */1076 while ((second < line + llen) && isspace(*second))1077 second++;10781079 if (line + llen <= second)1080 goto free_and_fail1;1081 if (*second == '"') {1082 if (unquote_c_style(&sp, second, NULL))1083 goto free_and_fail1;1084 cp = stop_at_slash(sp.buf, sp.len);1085 if (!cp || cp == sp.buf)1086 goto free_and_fail1;1087 /* They must match, otherwise ignore */1088 if (strcmp(cp + 1, first.buf))1089 goto free_and_fail1;1090 strbuf_release(&sp);1091 return strbuf_detach(&first, NULL);1092 }10931094 /* unquoted second */1095 cp = stop_at_slash(second, line + llen - second);1096 if (!cp || cp == second)1097 goto free_and_fail1;1098 cp++;1099 if (line + llen - cp != first.len + 1 ||1100 memcmp(first.buf, cp, first.len))1101 goto free_and_fail1;1102 return strbuf_detach(&first, NULL);11031104 free_and_fail1:1105 strbuf_release(&first);1106 strbuf_release(&sp);1107 return NULL;1108 }11091110 /* unquoted first name */1111 name = stop_at_slash(line, llen);1112 if (!name || name == line)1113 return NULL;1114 name++;11151116 /*1117 * since the first name is unquoted, a dq if exists must be1118 * the beginning of the second name.1119 */1120 for (second = name; second < line + llen; second++) {1121 if (*second == '"') {1122 struct strbuf sp = STRBUF_INIT;1123 const char *np;11241125 if (unquote_c_style(&sp, second, NULL))1126 goto free_and_fail2;11271128 np = stop_at_slash(sp.buf, sp.len);1129 if (!np || np == sp.buf)1130 goto free_and_fail2;1131 np++;11321133 len = sp.buf + sp.len - np;1134 if (len < second - name &&1135 !strncmp(np, name, len) &&1136 isspace(name[len])) {1137 /* Good */1138 strbuf_remove(&sp, 0, np - sp.buf);1139 return strbuf_detach(&sp, NULL);1140 }11411142 free_and_fail2:1143 strbuf_release(&sp);1144 return NULL;1145 }1146 }11471148 /*1149 * Accept a name only if it shows up twice, exactly the same1150 * form.1151 */1152 second = strchr(name, '\n');1153 if (!second)1154 return NULL;1155 line_len = second - name;1156 for (len = 0 ; ; len++) {1157 switch (name[len]) {1158 default:1159 continue;1160 case '\n':1161 return NULL;1162 case '\t': case ' ':1163 second = stop_at_slash(name + len, line_len - len);1164 if (!second)1165 return NULL;1166 second++;1167 if (second[len] == '\n' && !strncmp(name, second, len)) {1168 return xmemdupz(name, len);1169 }1170 }1171 }1172}11731174/* Verify that we recognize the lines following a git header */1175static int parse_git_header(char *line, int len, unsigned int size, struct patch *patch)1176{1177 unsigned long offset;11781179 /* A git diff has explicit new/delete information, so we don't guess */1180 patch->is_new = 0;1181 patch->is_delete = 0;11821183 /*1184 * Some things may not have the old name in the1185 * rest of the headers anywhere (pure mode changes,1186 * or removing or adding empty files), so we get1187 * the default name from the header.1188 */1189 patch->def_name = git_header_name(line, len);1190 if (patch->def_name && root) {1191 char *s = xmalloc(root_len + strlen(patch->def_name) + 1);1192 strcpy(s, root);1193 strcpy(s + root_len, patch->def_name);1194 free(patch->def_name);1195 patch->def_name = s;1196 }11971198 line += len;1199 size -= len;1200 linenr++;1201 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {1202 static const struct opentry {1203 const char *str;1204 int (*fn)(const char *, struct patch *);1205 } optable[] = {1206 { "@@ -", gitdiff_hdrend },1207 { "--- ", gitdiff_oldname },1208 { "+++ ", gitdiff_newname },1209 { "old mode ", gitdiff_oldmode },1210 { "new mode ", gitdiff_newmode },1211 { "deleted file mode ", gitdiff_delete },1212 { "new file mode ", gitdiff_newfile },1213 { "copy from ", gitdiff_copysrc },1214 { "copy to ", gitdiff_copydst },1215 { "rename old ", gitdiff_renamesrc },1216 { "rename new ", gitdiff_renamedst },1217 { "rename from ", gitdiff_renamesrc },1218 { "rename to ", gitdiff_renamedst },1219 { "similarity index ", gitdiff_similarity },1220 { "dissimilarity index ", gitdiff_dissimilarity },1221 { "index ", gitdiff_index },1222 { "", gitdiff_unrecognized },1223 };1224 int i;12251226 len = linelen(line, size);1227 if (!len || line[len-1] != '\n')1228 break;1229 for (i = 0; i < ARRAY_SIZE(optable); i++) {1230 const struct opentry *p = optable + i;1231 int oplen = strlen(p->str);1232 if (len < oplen || memcmp(p->str, line, oplen))1233 continue;1234 if (p->fn(line + oplen, patch) < 0)1235 return offset;1236 break;1237 }1238 }12391240 return offset;1241}12421243static int parse_num(const char *line, unsigned long *p)1244{1245 char *ptr;12461247 if (!isdigit(*line))1248 return 0;1249 *p = strtoul(line, &ptr, 10);1250 return ptr - line;1251}12521253static int parse_range(const char *line, int len, int offset, const char *expect,1254 unsigned long *p1, unsigned long *p2)1255{1256 int digits, ex;12571258 if (offset < 0 || offset >= len)1259 return -1;1260 line += offset;1261 len -= offset;12621263 digits = parse_num(line, p1);1264 if (!digits)1265 return -1;12661267 offset += digits;1268 line += digits;1269 len -= digits;12701271 *p2 = 1;1272 if (*line == ',') {1273 digits = parse_num(line+1, p2);1274 if (!digits)1275 return -1;12761277 offset += digits+1;1278 line += digits+1;1279 len -= digits+1;1280 }12811282 ex = strlen(expect);1283 if (ex > len)1284 return -1;1285 if (memcmp(line, expect, ex))1286 return -1;12871288 return offset + ex;1289}12901291static void recount_diff(char *line, int size, struct fragment *fragment)1292{1293 int oldlines = 0, newlines = 0, ret = 0;12941295 if (size < 1) {1296 warning("recount: ignore empty hunk");1297 return;1298 }12991300 for (;;) {1301 int len = linelen(line, size);1302 size -= len;1303 line += len;13041305 if (size < 1)1306 break;13071308 switch (*line) {1309 case ' ': case '\n':1310 newlines++;1311 /* fall through */1312 case '-':1313 oldlines++;1314 continue;1315 case '+':1316 newlines++;1317 continue;1318 case '\\':1319 continue;1320 case '@':1321 ret = size < 3 || prefixcmp(line, "@@ ");1322 break;1323 case 'd':1324 ret = size < 5 || prefixcmp(line, "diff ");1325 break;1326 default:1327 ret = -1;1328 break;1329 }1330 if (ret) {1331 warning("recount: unexpected line: %.*s",1332 (int)linelen(line, size), line);1333 return;1334 }1335 break;1336 }1337 fragment->oldlines = oldlines;1338 fragment->newlines = newlines;1339}13401341/*1342 * Parse a unified diff fragment header of the1343 * form "@@ -a,b +c,d @@"1344 */1345static int parse_fragment_header(char *line, int len, struct fragment *fragment)1346{1347 int offset;13481349 if (!len || line[len-1] != '\n')1350 return -1;13511352 /* Figure out the number of lines in a fragment */1353 offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);1354 offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);13551356 return offset;1357}13581359static int find_header(char *line, unsigned long size, int *hdrsize, struct patch *patch)1360{1361 unsigned long offset, len;13621363 patch->is_toplevel_relative = 0;1364 patch->is_rename = patch->is_copy = 0;1365 patch->is_new = patch->is_delete = -1;1366 patch->old_mode = patch->new_mode = 0;1367 patch->old_name = patch->new_name = NULL;1368 for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {1369 unsigned long nextlen;13701371 len = linelen(line, size);1372 if (!len)1373 break;13741375 /* Testing this early allows us to take a few shortcuts.. */1376 if (len < 6)1377 continue;13781379 /*1380 * Make sure we don't find any unconnected patch fragments.1381 * That's a sign that we didn't find a header, and that a1382 * patch has become corrupted/broken up.1383 */1384 if (!memcmp("@@ -", line, 4)) {1385 struct fragment dummy;1386 if (parse_fragment_header(line, len, &dummy) < 0)1387 continue;1388 die("patch fragment without header at line %d: %.*s",1389 linenr, (int)len-1, line);1390 }13911392 if (size < len + 6)1393 break;13941395 /*1396 * Git patch? It might not have a real patch, just a rename1397 * or mode change, so we handle that specially1398 */1399 if (!memcmp("diff --git ", line, 11)) {1400 int git_hdr_len = parse_git_header(line, len, size, patch);1401 if (git_hdr_len <= len)1402 continue;1403 if (!patch->old_name && !patch->new_name) {1404 if (!patch->def_name)1405 die("git diff header lacks filename information when removing "1406 "%d leading pathname components (line %d)" , p_value, linenr);1407 patch->old_name = patch->new_name = patch->def_name;1408 }1409 patch->is_toplevel_relative = 1;1410 *hdrsize = git_hdr_len;1411 return offset;1412 }14131414 /* --- followed by +++ ? */1415 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))1416 continue;14171418 /*1419 * We only accept unified patches, so we want it to1420 * at least have "@@ -a,b +c,d @@\n", which is 14 chars1421 * minimum ("@@ -0,0 +1 @@\n" is the shortest).1422 */1423 nextlen = linelen(line + len, size - len);1424 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))1425 continue;14261427 /* Ok, we'll consider it a patch */1428 parse_traditional_patch(line, line+len, patch);1429 *hdrsize = len + nextlen;1430 linenr += 2;1431 return offset;1432 }1433 return -1;1434}14351436static void record_ws_error(unsigned result, const char *line, int len, int linenr)1437{1438 char *err;14391440 if (!result)1441 return;14421443 whitespace_error++;1444 if (squelch_whitespace_errors &&1445 squelch_whitespace_errors < whitespace_error)1446 return;14471448 err = whitespace_error_string(result);1449 fprintf(stderr, "%s:%d: %s.\n%.*s\n",1450 patch_input_file, linenr, err, len, line);1451 free(err);1452}14531454static void check_whitespace(const char *line, int len, unsigned ws_rule)1455{1456 unsigned result = ws_check(line + 1, len - 1, ws_rule);14571458 record_ws_error(result, line + 1, len - 2, linenr);1459}14601461/*1462 * Parse a unified diff. Note that this really needs to parse each1463 * fragment separately, since the only way to know the difference1464 * between a "---" that is part of a patch, and a "---" that starts1465 * the next patch is to look at the line counts..1466 */1467static int parse_fragment(char *line, unsigned long size,1468 struct patch *patch, struct fragment *fragment)1469{1470 int added, deleted;1471 int len = linelen(line, size), offset;1472 unsigned long oldlines, newlines;1473 unsigned long leading, trailing;14741475 offset = parse_fragment_header(line, len, fragment);1476 if (offset < 0)1477 return -1;1478 if (offset > 0 && patch->recount)1479 recount_diff(line + offset, size - offset, fragment);1480 oldlines = fragment->oldlines;1481 newlines = fragment->newlines;1482 leading = 0;1483 trailing = 0;14841485 /* Parse the thing.. */1486 line += len;1487 size -= len;1488 linenr++;1489 added = deleted = 0;1490 for (offset = len;1491 0 < size;1492 offset += len, size -= len, line += len, linenr++) {1493 if (!oldlines && !newlines)1494 break;1495 len = linelen(line, size);1496 if (!len || line[len-1] != '\n')1497 return -1;1498 switch (*line) {1499 default:1500 return -1;1501 case '\n': /* newer GNU diff, an empty context line */1502 case ' ':1503 oldlines--;1504 newlines--;1505 if (!deleted && !added)1506 leading++;1507 trailing++;1508 break;1509 case '-':1510 if (apply_in_reverse &&1511 ws_error_action != nowarn_ws_error)1512 check_whitespace(line, len, patch->ws_rule);1513 deleted++;1514 oldlines--;1515 trailing = 0;1516 break;1517 case '+':1518 if (!apply_in_reverse &&1519 ws_error_action != nowarn_ws_error)1520 check_whitespace(line, len, patch->ws_rule);1521 added++;1522 newlines--;1523 trailing = 0;1524 break;15251526 /*1527 * We allow "\ No newline at end of file". Depending1528 * on locale settings when the patch was produced we1529 * don't know what this line looks like. The only1530 * thing we do know is that it begins with "\ ".1531 * Checking for 12 is just for sanity check -- any1532 * l10n of "\ No newline..." is at least that long.1533 */1534 case '\\':1535 if (len < 12 || memcmp(line, "\\ ", 2))1536 return -1;1537 break;1538 }1539 }1540 if (oldlines || newlines)1541 return -1;1542 fragment->leading = leading;1543 fragment->trailing = trailing;15441545 /*1546 * If a fragment ends with an incomplete line, we failed to include1547 * it in the above loop because we hit oldlines == newlines == 01548 * before seeing it.1549 */1550 if (12 < size && !memcmp(line, "\\ ", 2))1551 offset += linelen(line, size);15521553 patch->lines_added += added;1554 patch->lines_deleted += deleted;15551556 if (0 < patch->is_new && oldlines)1557 return error("new file depends on old contents");1558 if (0 < patch->is_delete && newlines)1559 return error("deleted file still has contents");1560 return offset;1561}15621563static int parse_single_patch(char *line, unsigned long size, struct patch *patch)1564{1565 unsigned long offset = 0;1566 unsigned long oldlines = 0, newlines = 0, context = 0;1567 struct fragment **fragp = &patch->fragments;15681569 while (size > 4 && !memcmp(line, "@@ -", 4)) {1570 struct fragment *fragment;1571 int len;15721573 fragment = xcalloc(1, sizeof(*fragment));1574 fragment->linenr = linenr;1575 len = parse_fragment(line, size, patch, fragment);1576 if (len <= 0)1577 die("corrupt patch at line %d", linenr);1578 fragment->patch = line;1579 fragment->size = len;1580 oldlines += fragment->oldlines;1581 newlines += fragment->newlines;1582 context += fragment->leading + fragment->trailing;15831584 *fragp = fragment;1585 fragp = &fragment->next;15861587 offset += len;1588 line += len;1589 size -= len;1590 }15911592 /*1593 * If something was removed (i.e. we have old-lines) it cannot1594 * be creation, and if something was added it cannot be1595 * deletion. However, the reverse is not true; --unified=01596 * patches that only add are not necessarily creation even1597 * though they do not have any old lines, and ones that only1598 * delete are not necessarily deletion.1599 *1600 * Unfortunately, a real creation/deletion patch do _not_ have1601 * any context line by definition, so we cannot safely tell it1602 * apart with --unified=0 insanity. At least if the patch has1603 * more than one hunk it is not creation or deletion.1604 */1605 if (patch->is_new < 0 &&1606 (oldlines || (patch->fragments && patch->fragments->next)))1607 patch->is_new = 0;1608 if (patch->is_delete < 0 &&1609 (newlines || (patch->fragments && patch->fragments->next)))1610 patch->is_delete = 0;16111612 if (0 < patch->is_new && oldlines)1613 die("new file %s depends on old contents", patch->new_name);1614 if (0 < patch->is_delete && newlines)1615 die("deleted file %s still has contents", patch->old_name);1616 if (!patch->is_delete && !newlines && context)1617 fprintf(stderr, "** warning: file %s becomes empty but "1618 "is not deleted\n", patch->new_name);16191620 return offset;1621}16221623static inline int metadata_changes(struct patch *patch)1624{1625 return patch->is_rename > 0 ||1626 patch->is_copy > 0 ||1627 patch->is_new > 0 ||1628 patch->is_delete ||1629 (patch->old_mode && patch->new_mode &&1630 patch->old_mode != patch->new_mode);1631}16321633static char *inflate_it(const void *data, unsigned long size,1634 unsigned long inflated_size)1635{1636 z_stream stream;1637 void *out;1638 int st;16391640 memset(&stream, 0, sizeof(stream));16411642 stream.next_in = (unsigned char *)data;1643 stream.avail_in = size;1644 stream.next_out = out = xmalloc(inflated_size);1645 stream.avail_out = inflated_size;1646 git_inflate_init(&stream);1647 st = git_inflate(&stream, Z_FINISH);1648 git_inflate_end(&stream);1649 if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {1650 free(out);1651 return NULL;1652 }1653 return out;1654}16551656static struct fragment *parse_binary_hunk(char **buf_p,1657 unsigned long *sz_p,1658 int *status_p,1659 int *used_p)1660{1661 /*1662 * Expect a line that begins with binary patch method ("literal"1663 * or "delta"), followed by the length of data before deflating.1664 * a sequence of 'length-byte' followed by base-85 encoded data1665 * should follow, terminated by a newline.1666 *1667 * Each 5-byte sequence of base-85 encodes up to 4 bytes,1668 * and we would limit the patch line to 66 characters,1669 * so one line can fit up to 13 groups that would decode1670 * to 52 bytes max. The length byte 'A'-'Z' corresponds1671 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.1672 */1673 int llen, used;1674 unsigned long size = *sz_p;1675 char *buffer = *buf_p;1676 int patch_method;1677 unsigned long origlen;1678 char *data = NULL;1679 int hunk_size = 0;1680 struct fragment *frag;16811682 llen = linelen(buffer, size);1683 used = llen;16841685 *status_p = 0;16861687 if (!prefixcmp(buffer, "delta ")) {1688 patch_method = BINARY_DELTA_DEFLATED;1689 origlen = strtoul(buffer + 6, NULL, 10);1690 }1691 else if (!prefixcmp(buffer, "literal ")) {1692 patch_method = BINARY_LITERAL_DEFLATED;1693 origlen = strtoul(buffer + 8, NULL, 10);1694 }1695 else1696 return NULL;16971698 linenr++;1699 buffer += llen;1700 while (1) {1701 int byte_length, max_byte_length, newsize;1702 llen = linelen(buffer, size);1703 used += llen;1704 linenr++;1705 if (llen == 1) {1706 /* consume the blank line */1707 buffer++;1708 size--;1709 break;1710 }1711 /*1712 * Minimum line is "A00000\n" which is 7-byte long,1713 * and the line length must be multiple of 5 plus 2.1714 */1715 if ((llen < 7) || (llen-2) % 5)1716 goto corrupt;1717 max_byte_length = (llen - 2) / 5 * 4;1718 byte_length = *buffer;1719 if ('A' <= byte_length && byte_length <= 'Z')1720 byte_length = byte_length - 'A' + 1;1721 else if ('a' <= byte_length && byte_length <= 'z')1722 byte_length = byte_length - 'a' + 27;1723 else1724 goto corrupt;1725 /* if the input length was not multiple of 4, we would1726 * have filler at the end but the filler should never1727 * exceed 3 bytes1728 */1729 if (max_byte_length < byte_length ||1730 byte_length <= max_byte_length - 4)1731 goto corrupt;1732 newsize = hunk_size + byte_length;1733 data = xrealloc(data, newsize);1734 if (decode_85(data + hunk_size, buffer + 1, byte_length))1735 goto corrupt;1736 hunk_size = newsize;1737 buffer += llen;1738 size -= llen;1739 }17401741 frag = xcalloc(1, sizeof(*frag));1742 frag->patch = inflate_it(data, hunk_size, origlen);1743 if (!frag->patch)1744 goto corrupt;1745 free(data);1746 frag->size = origlen;1747 *buf_p = buffer;1748 *sz_p = size;1749 *used_p = used;1750 frag->binary_patch_method = patch_method;1751 return frag;17521753 corrupt:1754 free(data);1755 *status_p = -1;1756 error("corrupt binary patch at line %d: %.*s",1757 linenr-1, llen-1, buffer);1758 return NULL;1759}17601761static int parse_binary(char *buffer, unsigned long size, struct patch *patch)1762{1763 /*1764 * We have read "GIT binary patch\n"; what follows is a line1765 * that says the patch method (currently, either "literal" or1766 * "delta") and the length of data before deflating; a1767 * sequence of 'length-byte' followed by base-85 encoded data1768 * follows.1769 *1770 * When a binary patch is reversible, there is another binary1771 * hunk in the same format, starting with patch method (either1772 * "literal" or "delta") with the length of data, and a sequence1773 * of length-byte + base-85 encoded data, terminated with another1774 * empty line. This data, when applied to the postimage, produces1775 * the preimage.1776 */1777 struct fragment *forward;1778 struct fragment *reverse;1779 int status;1780 int used, used_1;17811782 forward = parse_binary_hunk(&buffer, &size, &status, &used);1783 if (!forward && !status)1784 /* there has to be one hunk (forward hunk) */1785 return error("unrecognized binary patch at line %d", linenr-1);1786 if (status)1787 /* otherwise we already gave an error message */1788 return status;17891790 reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);1791 if (reverse)1792 used += used_1;1793 else if (status) {1794 /*1795 * Not having reverse hunk is not an error, but having1796 * a corrupt reverse hunk is.1797 */1798 free((void*) forward->patch);1799 free(forward);1800 return status;1801 }1802 forward->next = reverse;1803 patch->fragments = forward;1804 patch->is_binary = 1;1805 return used;1806}18071808static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)1809{1810 int hdrsize, patchsize;1811 int offset = find_header(buffer, size, &hdrsize, patch);18121813 if (offset < 0)1814 return offset;18151816 patch->ws_rule = whitespace_rule(patch->new_name1817 ? patch->new_name1818 : patch->old_name);18191820 patchsize = parse_single_patch(buffer + offset + hdrsize,1821 size - offset - hdrsize, patch);18221823 if (!patchsize) {1824 static const char *binhdr[] = {1825 "Binary files ",1826 "Files ",1827 NULL,1828 };1829 static const char git_binary[] = "GIT binary patch\n";1830 int i;1831 int hd = hdrsize + offset;1832 unsigned long llen = linelen(buffer + hd, size - hd);18331834 if (llen == sizeof(git_binary) - 1 &&1835 !memcmp(git_binary, buffer + hd, llen)) {1836 int used;1837 linenr++;1838 used = parse_binary(buffer + hd + llen,1839 size - hd - llen, patch);1840 if (used)1841 patchsize = used + llen;1842 else1843 patchsize = 0;1844 }1845 else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {1846 for (i = 0; binhdr[i]; i++) {1847 int len = strlen(binhdr[i]);1848 if (len < size - hd &&1849 !memcmp(binhdr[i], buffer + hd, len)) {1850 linenr++;1851 patch->is_binary = 1;1852 patchsize = llen;1853 break;1854 }1855 }1856 }18571858 /* Empty patch cannot be applied if it is a text patch1859 * without metadata change. A binary patch appears1860 * empty to us here.1861 */1862 if ((apply || check) &&1863 (!patch->is_binary && !metadata_changes(patch)))1864 die("patch with only garbage at line %d", linenr);1865 }18661867 return offset + hdrsize + patchsize;1868}18691870#define swap(a,b) myswap((a),(b),sizeof(a))18711872#define myswap(a, b, size) do { \1873 unsigned char mytmp[size]; \1874 memcpy(mytmp, &a, size); \1875 memcpy(&a, &b, size); \1876 memcpy(&b, mytmp, size); \1877} while (0)18781879static void reverse_patches(struct patch *p)1880{1881 for (; p; p = p->next) {1882 struct fragment *frag = p->fragments;18831884 swap(p->new_name, p->old_name);1885 swap(p->new_mode, p->old_mode);1886 swap(p->is_new, p->is_delete);1887 swap(p->lines_added, p->lines_deleted);1888 swap(p->old_sha1_prefix, p->new_sha1_prefix);18891890 for (; frag; frag = frag->next) {1891 swap(frag->newpos, frag->oldpos);1892 swap(frag->newlines, frag->oldlines);1893 }1894 }1895}18961897static const char pluses[] =1898"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";1899static const char minuses[]=1900"----------------------------------------------------------------------";19011902static void show_stats(struct patch *patch)1903{1904 struct strbuf qname = STRBUF_INIT;1905 char *cp = patch->new_name ? patch->new_name : patch->old_name;1906 int max, add, del;19071908 quote_c_style(cp, &qname, NULL, 0);19091910 /*1911 * "scale" the filename1912 */1913 max = max_len;1914 if (max > 50)1915 max = 50;19161917 if (qname.len > max) {1918 cp = strchr(qname.buf + qname.len + 3 - max, '/');1919 if (!cp)1920 cp = qname.buf + qname.len + 3 - max;1921 strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);1922 }19231924 if (patch->is_binary) {1925 printf(" %-*s | Bin\n", max, qname.buf);1926 strbuf_release(&qname);1927 return;1928 }19291930 printf(" %-*s |", max, qname.buf);1931 strbuf_release(&qname);19321933 /*1934 * scale the add/delete1935 */1936 max = max + max_change > 70 ? 70 - max : max_change;1937 add = patch->lines_added;1938 del = patch->lines_deleted;19391940 if (max_change > 0) {1941 int total = ((add + del) * max + max_change / 2) / max_change;1942 add = (add * max + max_change / 2) / max_change;1943 del = total - add;1944 }1945 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,1946 add, pluses, del, minuses);1947}19481949static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)1950{1951 switch (st->st_mode & S_IFMT) {1952 case S_IFLNK:1953 if (strbuf_readlink(buf, path, st->st_size) < 0)1954 return error("unable to read symlink %s", path);1955 return 0;1956 case S_IFREG:1957 if (strbuf_read_file(buf, path, st->st_size) != st->st_size)1958 return error("unable to open or read %s", path);1959 convert_to_git(path, buf->buf, buf->len, buf, 0);1960 return 0;1961 default:1962 return -1;1963 }1964}19651966/*1967 * Update the preimage, and the common lines in postimage,1968 * from buffer buf of length len. If postlen is 0 the postimage1969 * is updated in place, otherwise it's updated on a new buffer1970 * of length postlen1971 */19721973static void update_pre_post_images(struct image *preimage,1974 struct image *postimage,1975 char *buf,1976 size_t len, size_t postlen)1977{1978 int i, ctx;1979 char *new, *old, *fixed;1980 struct image fixed_preimage;19811982 /*1983 * Update the preimage with whitespace fixes. Note that we1984 * are not losing preimage->buf -- apply_one_fragment() will1985 * free "oldlines".1986 */1987 prepare_image(&fixed_preimage, buf, len, 1);1988 assert(fixed_preimage.nr == preimage->nr);1989 for (i = 0; i < preimage->nr; i++)1990 fixed_preimage.line[i].flag = preimage->line[i].flag;1991 free(preimage->line_allocated);1992 *preimage = fixed_preimage;19931994 /*1995 * Adjust the common context lines in postimage. This can be1996 * done in-place when we are just doing whitespace fixing,1997 * which does not make the string grow, but needs a new buffer1998 * when ignoring whitespace causes the update, since in this case1999 * we could have e.g. tabs converted to multiple spaces.2000 * We trust the caller to tell us if the update can be done2001 * in place (postlen==0) or not.2002 */2003 old = postimage->buf;2004 if (postlen)2005 new = postimage->buf = xmalloc(postlen);2006 else2007 new = old;2008 fixed = preimage->buf;2009 for (i = ctx = 0; i < postimage->nr; i++) {2010 size_t len = postimage->line[i].len;2011 if (!(postimage->line[i].flag & LINE_COMMON)) {2012 /* an added line -- no counterparts in preimage */2013 memmove(new, old, len);2014 old += len;2015 new += len;2016 continue;2017 }20182019 /* a common context -- skip it in the original postimage */2020 old += len;20212022 /* and find the corresponding one in the fixed preimage */2023 while (ctx < preimage->nr &&2024 !(preimage->line[ctx].flag & LINE_COMMON)) {2025 fixed += preimage->line[ctx].len;2026 ctx++;2027 }2028 if (preimage->nr <= ctx)2029 die("oops");20302031 /* and copy it in, while fixing the line length */2032 len = preimage->line[ctx].len;2033 memcpy(new, fixed, len);2034 new += len;2035 fixed += len;2036 postimage->line[i].len = len;2037 ctx++;2038 }20392040 /* Fix the length of the whole thing */2041 postimage->len = new - postimage->buf;2042}20432044static int match_fragment(struct image *img,2045 struct image *preimage,2046 struct image *postimage,2047 unsigned long try,2048 int try_lno,2049 unsigned ws_rule,2050 int match_beginning, int match_end)2051{2052 int i;2053 char *fixed_buf, *buf, *orig, *target;2054 struct strbuf fixed;2055 size_t fixed_len;2056 int preimage_limit;20572058 if (preimage->nr + try_lno <= img->nr) {2059 /*2060 * The hunk falls within the boundaries of img.2061 */2062 preimage_limit = preimage->nr;2063 if (match_end && (preimage->nr + try_lno != img->nr))2064 return 0;2065 } else if (ws_error_action == correct_ws_error &&2066 (ws_rule & WS_BLANK_AT_EOF)) {2067 /*2068 * This hunk extends beyond the end of img, and we are2069 * removing blank lines at the end of the file. This2070 * many lines from the beginning of the preimage must2071 * match with img, and the remainder of the preimage2072 * must be blank.2073 */2074 preimage_limit = img->nr - try_lno;2075 } else {2076 /*2077 * The hunk extends beyond the end of the img and2078 * we are not removing blanks at the end, so we2079 * should reject the hunk at this position.2080 */2081 return 0;2082 }20832084 if (match_beginning && try_lno)2085 return 0;20862087 /* Quick hash check */2088 for (i = 0; i < preimage_limit; i++)2089 if ((img->line[try_lno + i].flag & LINE_PATCHED) ||2090 (preimage->line[i].hash != img->line[try_lno + i].hash))2091 return 0;20922093 if (preimage_limit == preimage->nr) {2094 /*2095 * Do we have an exact match? If we were told to match2096 * at the end, size must be exactly at try+fragsize,2097 * otherwise try+fragsize must be still within the preimage,2098 * and either case, the old piece should match the preimage2099 * exactly.2100 */2101 if ((match_end2102 ? (try + preimage->len == img->len)2103 : (try + preimage->len <= img->len)) &&2104 !memcmp(img->buf + try, preimage->buf, preimage->len))2105 return 1;2106 } else {2107 /*2108 * The preimage extends beyond the end of img, so2109 * there cannot be an exact match.2110 *2111 * There must be one non-blank context line that match2112 * a line before the end of img.2113 */2114 char *buf_end;21152116 buf = preimage->buf;2117 buf_end = buf;2118 for (i = 0; i < preimage_limit; i++)2119 buf_end += preimage->line[i].len;21202121 for ( ; buf < buf_end; buf++)2122 if (!isspace(*buf))2123 break;2124 if (buf == buf_end)2125 return 0;2126 }21272128 /*2129 * No exact match. If we are ignoring whitespace, run a line-by-line2130 * fuzzy matching. We collect all the line length information because2131 * we need it to adjust whitespace if we match.2132 */2133 if (ws_ignore_action == ignore_ws_change) {2134 size_t imgoff = 0;2135 size_t preoff = 0;2136 size_t postlen = postimage->len;2137 size_t extra_chars;2138 char *preimage_eof;2139 char *preimage_end;2140 for (i = 0; i < preimage_limit; i++) {2141 size_t prelen = preimage->line[i].len;2142 size_t imglen = img->line[try_lno+i].len;21432144 if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,2145 preimage->buf + preoff, prelen))2146 return 0;2147 if (preimage->line[i].flag & LINE_COMMON)2148 postlen += imglen - prelen;2149 imgoff += imglen;2150 preoff += prelen;2151 }21522153 /*2154 * Ok, the preimage matches with whitespace fuzz.2155 *2156 * imgoff now holds the true length of the target that2157 * matches the preimage before the end of the file.2158 *2159 * Count the number of characters in the preimage that fall2160 * beyond the end of the file and make sure that all of them2161 * are whitespace characters. (This can only happen if2162 * we are removing blank lines at the end of the file.)2163 */2164 buf = preimage_eof = preimage->buf + preoff;2165 for ( ; i < preimage->nr; i++)2166 preoff += preimage->line[i].len;2167 preimage_end = preimage->buf + preoff;2168 for ( ; buf < preimage_end; buf++)2169 if (!isspace(*buf))2170 return 0;21712172 /*2173 * Update the preimage and the common postimage context2174 * lines to use the same whitespace as the target.2175 * If whitespace is missing in the target (i.e.2176 * if the preimage extends beyond the end of the file),2177 * use the whitespace from the preimage.2178 */2179 extra_chars = preimage_end - preimage_eof;2180 strbuf_init(&fixed, imgoff + extra_chars);2181 strbuf_add(&fixed, img->buf + try, imgoff);2182 strbuf_add(&fixed, preimage_eof, extra_chars);2183 fixed_buf = strbuf_detach(&fixed, &fixed_len);2184 update_pre_post_images(preimage, postimage,2185 fixed_buf, fixed_len, postlen);2186 return 1;2187 }21882189 if (ws_error_action != correct_ws_error)2190 return 0;21912192 /*2193 * The hunk does not apply byte-by-byte, but the hash says2194 * it might with whitespace fuzz. We haven't been asked to2195 * ignore whitespace, we were asked to correct whitespace2196 * errors, so let's try matching after whitespace correction.2197 *2198 * The preimage may extend beyond the end of the file,2199 * but in this loop we will only handle the part of the2200 * preimage that falls within the file.2201 */2202 strbuf_init(&fixed, preimage->len + 1);2203 orig = preimage->buf;2204 target = img->buf + try;2205 for (i = 0; i < preimage_limit; i++) {2206 size_t oldlen = preimage->line[i].len;2207 size_t tgtlen = img->line[try_lno + i].len;2208 size_t fixstart = fixed.len;2209 struct strbuf tgtfix;2210 int match;22112212 /* Try fixing the line in the preimage */2213 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);22142215 /* Try fixing the line in the target */2216 strbuf_init(&tgtfix, tgtlen);2217 ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);22182219 /*2220 * If they match, either the preimage was based on2221 * a version before our tree fixed whitespace breakage,2222 * or we are lacking a whitespace-fix patch the tree2223 * the preimage was based on already had (i.e. target2224 * has whitespace breakage, the preimage doesn't).2225 * In either case, we are fixing the whitespace breakages2226 * so we might as well take the fix together with their2227 * real change.2228 */2229 match = (tgtfix.len == fixed.len - fixstart &&2230 !memcmp(tgtfix.buf, fixed.buf + fixstart,2231 fixed.len - fixstart));22322233 strbuf_release(&tgtfix);2234 if (!match)2235 goto unmatch_exit;22362237 orig += oldlen;2238 target += tgtlen;2239 }224022412242 /*2243 * Now handle the lines in the preimage that falls beyond the2244 * end of the file (if any). They will only match if they are2245 * empty or only contain whitespace (if WS_BLANK_AT_EOL is2246 * false).2247 */2248 for ( ; i < preimage->nr; i++) {2249 size_t fixstart = fixed.len; /* start of the fixed preimage */2250 size_t oldlen = preimage->line[i].len;2251 int j;22522253 /* Try fixing the line in the preimage */2254 ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);22552256 for (j = fixstart; j < fixed.len; j++)2257 if (!isspace(fixed.buf[j]))2258 goto unmatch_exit;22592260 orig += oldlen;2261 }22622263 /*2264 * Yes, the preimage is based on an older version that still2265 * has whitespace breakages unfixed, and fixing them makes the2266 * hunk match. Update the context lines in the postimage.2267 */2268 fixed_buf = strbuf_detach(&fixed, &fixed_len);2269 update_pre_post_images(preimage, postimage,2270 fixed_buf, fixed_len, 0);2271 return 1;22722273 unmatch_exit:2274 strbuf_release(&fixed);2275 return 0;2276}22772278static int find_pos(struct image *img,2279 struct image *preimage,2280 struct image *postimage,2281 int line,2282 unsigned ws_rule,2283 int match_beginning, int match_end)2284{2285 int i;2286 unsigned long backwards, forwards, try;2287 int backwards_lno, forwards_lno, try_lno;22882289 /*2290 * If match_beginning or match_end is specified, there is no2291 * point starting from a wrong line that will never match and2292 * wander around and wait for a match at the specified end.2293 */2294 if (match_beginning)2295 line = 0;2296 else if (match_end)2297 line = img->nr - preimage->nr;22982299 /*2300 * Because the comparison is unsigned, the following test2301 * will also take care of a negative line number that can2302 * result when match_end and preimage is larger than the target.2303 */2304 if ((size_t) line > img->nr)2305 line = img->nr;23062307 try = 0;2308 for (i = 0; i < line; i++)2309 try += img->line[i].len;23102311 /*2312 * There's probably some smart way to do this, but I'll leave2313 * that to the smart and beautiful people. I'm simple and stupid.2314 */2315 backwards = try;2316 backwards_lno = line;2317 forwards = try;2318 forwards_lno = line;2319 try_lno = line;23202321 for (i = 0; ; i++) {2322 if (match_fragment(img, preimage, postimage,2323 try, try_lno, ws_rule,2324 match_beginning, match_end))2325 return try_lno;23262327 again:2328 if (backwards_lno == 0 && forwards_lno == img->nr)2329 break;23302331 if (i & 1) {2332 if (backwards_lno == 0) {2333 i++;2334 goto again;2335 }2336 backwards_lno--;2337 backwards -= img->line[backwards_lno].len;2338 try = backwards;2339 try_lno = backwards_lno;2340 } else {2341 if (forwards_lno == img->nr) {2342 i++;2343 goto again;2344 }2345 forwards += img->line[forwards_lno].len;2346 forwards_lno++;2347 try = forwards;2348 try_lno = forwards_lno;2349 }23502351 }2352 return -1;2353}23542355static void remove_first_line(struct image *img)2356{2357 img->buf += img->line[0].len;2358 img->len -= img->line[0].len;2359 img->line++;2360 img->nr--;2361}23622363static void remove_last_line(struct image *img)2364{2365 img->len -= img->line[--img->nr].len;2366}23672368static void update_image(struct image *img,2369 int applied_pos,2370 struct image *preimage,2371 struct image *postimage)2372{2373 /*2374 * remove the copy of preimage at offset in img2375 * and replace it with postimage2376 */2377 int i, nr;2378 size_t remove_count, insert_count, applied_at = 0;2379 char *result;2380 int preimage_limit;23812382 /*2383 * If we are removing blank lines at the end of img,2384 * the preimage may extend beyond the end.2385 * If that is the case, we must be careful only to2386 * remove the part of the preimage that falls within2387 * the boundaries of img. Initialize preimage_limit2388 * to the number of lines in the preimage that falls2389 * within the boundaries.2390 */2391 preimage_limit = preimage->nr;2392 if (preimage_limit > img->nr - applied_pos)2393 preimage_limit = img->nr - applied_pos;23942395 for (i = 0; i < applied_pos; i++)2396 applied_at += img->line[i].len;23972398 remove_count = 0;2399 for (i = 0; i < preimage_limit; i++)2400 remove_count += img->line[applied_pos + i].len;2401 insert_count = postimage->len;24022403 /* Adjust the contents */2404 result = xmalloc(img->len + insert_count - remove_count + 1);2405 memcpy(result, img->buf, applied_at);2406 memcpy(result + applied_at, postimage->buf, postimage->len);2407 memcpy(result + applied_at + postimage->len,2408 img->buf + (applied_at + remove_count),2409 img->len - (applied_at + remove_count));2410 free(img->buf);2411 img->buf = result;2412 img->len += insert_count - remove_count;2413 result[img->len] = '\0';24142415 /* Adjust the line table */2416 nr = img->nr + postimage->nr - preimage_limit;2417 if (preimage_limit < postimage->nr) {2418 /*2419 * NOTE: this knows that we never call remove_first_line()2420 * on anything other than pre/post image.2421 */2422 img->line = xrealloc(img->line, nr * sizeof(*img->line));2423 img->line_allocated = img->line;2424 }2425 if (preimage_limit != postimage->nr)2426 memmove(img->line + applied_pos + postimage->nr,2427 img->line + applied_pos + preimage_limit,2428 (img->nr - (applied_pos + preimage_limit)) *2429 sizeof(*img->line));2430 memcpy(img->line + applied_pos,2431 postimage->line,2432 postimage->nr * sizeof(*img->line));2433 for (i = 0; i < postimage->nr; i++)2434 img->line[applied_pos + i].flag |= LINE_PATCHED;24352436 img->nr = nr;2437}24382439static int apply_one_fragment(struct image *img, struct fragment *frag,2440 int inaccurate_eof, unsigned ws_rule)2441{2442 int match_beginning, match_end;2443 const char *patch = frag->patch;2444 int size = frag->size;2445 char *old, *oldlines;2446 struct strbuf newlines;2447 int new_blank_lines_at_end = 0;2448 unsigned long leading, trailing;2449 int pos, applied_pos;2450 struct image preimage;2451 struct image postimage;24522453 memset(&preimage, 0, sizeof(preimage));2454 memset(&postimage, 0, sizeof(postimage));2455 oldlines = xmalloc(size);2456 strbuf_init(&newlines, size);24572458 old = oldlines;2459 while (size > 0) {2460 char first;2461 int len = linelen(patch, size);2462 int plen;2463 int added_blank_line = 0;2464 int is_blank_context = 0;2465 size_t start;24662467 if (!len)2468 break;24692470 /*2471 * "plen" is how much of the line we should use for2472 * the actual patch data. Normally we just remove the2473 * first character on the line, but if the line is2474 * followed by "\ No newline", then we also remove the2475 * last one (which is the newline, of course).2476 */2477 plen = len - 1;2478 if (len < size && patch[len] == '\\')2479 plen--;2480 first = *patch;2481 if (apply_in_reverse) {2482 if (first == '-')2483 first = '+';2484 else if (first == '+')2485 first = '-';2486 }24872488 switch (first) {2489 case '\n':2490 /* Newer GNU diff, empty context line */2491 if (plen < 0)2492 /* ... followed by '\No newline'; nothing */2493 break;2494 *old++ = '\n';2495 strbuf_addch(&newlines, '\n');2496 add_line_info(&preimage, "\n", 1, LINE_COMMON);2497 add_line_info(&postimage, "\n", 1, LINE_COMMON);2498 is_blank_context = 1;2499 break;2500 case ' ':2501 if (plen && (ws_rule & WS_BLANK_AT_EOF) &&2502 ws_blank_line(patch + 1, plen, ws_rule))2503 is_blank_context = 1;2504 case '-':2505 memcpy(old, patch + 1, plen);2506 add_line_info(&preimage, old, plen,2507 (first == ' ' ? LINE_COMMON : 0));2508 old += plen;2509 if (first == '-')2510 break;2511 /* Fall-through for ' ' */2512 case '+':2513 /* --no-add does not add new lines */2514 if (first == '+' && no_add)2515 break;25162517 start = newlines.len;2518 if (first != '+' ||2519 !whitespace_error ||2520 ws_error_action != correct_ws_error) {2521 strbuf_add(&newlines, patch + 1, plen);2522 }2523 else {2524 ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &applied_after_fixing_ws);2525 }2526 add_line_info(&postimage, newlines.buf + start, newlines.len - start,2527 (first == '+' ? 0 : LINE_COMMON));2528 if (first == '+' &&2529 (ws_rule & WS_BLANK_AT_EOF) &&2530 ws_blank_line(patch + 1, plen, ws_rule))2531 added_blank_line = 1;2532 break;2533 case '@': case '\\':2534 /* Ignore it, we already handled it */2535 break;2536 default:2537 if (apply_verbosely)2538 error("invalid start of line: '%c'", first);2539 return -1;2540 }2541 if (added_blank_line)2542 new_blank_lines_at_end++;2543 else if (is_blank_context)2544 ;2545 else2546 new_blank_lines_at_end = 0;2547 patch += len;2548 size -= len;2549 }2550 if (inaccurate_eof &&2551 old > oldlines && old[-1] == '\n' &&2552 newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {2553 old--;2554 strbuf_setlen(&newlines, newlines.len - 1);2555 }25562557 leading = frag->leading;2558 trailing = frag->trailing;25592560 /*2561 * A hunk to change lines at the beginning would begin with2562 * @@ -1,L +N,M @@2563 * but we need to be careful. -U0 that inserts before the second2564 * line also has this pattern.2565 *2566 * And a hunk to add to an empty file would begin with2567 * @@ -0,0 +N,M @@2568 *2569 * In other words, a hunk that is (frag->oldpos <= 1) with or2570 * without leading context must match at the beginning.2571 */2572 match_beginning = (!frag->oldpos ||2573 (frag->oldpos == 1 && !unidiff_zero));25742575 /*2576 * A hunk without trailing lines must match at the end.2577 * However, we simply cannot tell if a hunk must match end2578 * from the lack of trailing lines if the patch was generated2579 * with unidiff without any context.2580 */2581 match_end = !unidiff_zero && !trailing;25822583 pos = frag->newpos ? (frag->newpos - 1) : 0;2584 preimage.buf = oldlines;2585 preimage.len = old - oldlines;2586 postimage.buf = newlines.buf;2587 postimage.len = newlines.len;2588 preimage.line = preimage.line_allocated;2589 postimage.line = postimage.line_allocated;25902591 for (;;) {25922593 applied_pos = find_pos(img, &preimage, &postimage, pos,2594 ws_rule, match_beginning, match_end);25952596 if (applied_pos >= 0)2597 break;25982599 /* Am I at my context limits? */2600 if ((leading <= p_context) && (trailing <= p_context))2601 break;2602 if (match_beginning || match_end) {2603 match_beginning = match_end = 0;2604 continue;2605 }26062607 /*2608 * Reduce the number of context lines; reduce both2609 * leading and trailing if they are equal otherwise2610 * just reduce the larger context.2611 */2612 if (leading >= trailing) {2613 remove_first_line(&preimage);2614 remove_first_line(&postimage);2615 pos--;2616 leading--;2617 }2618 if (trailing > leading) {2619 remove_last_line(&preimage);2620 remove_last_line(&postimage);2621 trailing--;2622 }2623 }26242625 if (applied_pos >= 0) {2626 if (new_blank_lines_at_end &&2627 preimage.nr + applied_pos >= img->nr &&2628 (ws_rule & WS_BLANK_AT_EOF) &&2629 ws_error_action != nowarn_ws_error) {2630 record_ws_error(WS_BLANK_AT_EOF, "+", 1, frag->linenr);2631 if (ws_error_action == correct_ws_error) {2632 while (new_blank_lines_at_end--)2633 remove_last_line(&postimage);2634 }2635 /*2636 * We would want to prevent write_out_results()2637 * from taking place in apply_patch() that follows2638 * the callchain led us here, which is:2639 * apply_patch->check_patch_list->check_patch->2640 * apply_data->apply_fragments->apply_one_fragment2641 */2642 if (ws_error_action == die_on_ws_error)2643 apply = 0;2644 }26452646 /*2647 * Warn if it was necessary to reduce the number2648 * of context lines.2649 */2650 if ((leading != frag->leading) ||2651 (trailing != frag->trailing))2652 fprintf(stderr, "Context reduced to (%ld/%ld)"2653 " to apply fragment at %d\n",2654 leading, trailing, applied_pos+1);2655 update_image(img, applied_pos, &preimage, &postimage);2656 } else {2657 if (apply_verbosely)2658 error("while searching for:\n%.*s",2659 (int)(old - oldlines), oldlines);2660 }26612662 free(oldlines);2663 strbuf_release(&newlines);2664 free(preimage.line_allocated);2665 free(postimage.line_allocated);26662667 return (applied_pos < 0);2668}26692670static int apply_binary_fragment(struct image *img, struct patch *patch)2671{2672 struct fragment *fragment = patch->fragments;2673 unsigned long len;2674 void *dst;26752676 if (!fragment)2677 return error("missing binary patch data for '%s'",2678 patch->new_name ?2679 patch->new_name :2680 patch->old_name);26812682 /* Binary patch is irreversible without the optional second hunk */2683 if (apply_in_reverse) {2684 if (!fragment->next)2685 return error("cannot reverse-apply a binary patch "2686 "without the reverse hunk to '%s'",2687 patch->new_name2688 ? patch->new_name : patch->old_name);2689 fragment = fragment->next;2690 }2691 switch (fragment->binary_patch_method) {2692 case BINARY_DELTA_DEFLATED:2693 dst = patch_delta(img->buf, img->len, fragment->patch,2694 fragment->size, &len);2695 if (!dst)2696 return -1;2697 clear_image(img);2698 img->buf = dst;2699 img->len = len;2700 return 0;2701 case BINARY_LITERAL_DEFLATED:2702 clear_image(img);2703 img->len = fragment->size;2704 img->buf = xmalloc(img->len+1);2705 memcpy(img->buf, fragment->patch, img->len);2706 img->buf[img->len] = '\0';2707 return 0;2708 }2709 return -1;2710}27112712static int apply_binary(struct image *img, struct patch *patch)2713{2714 const char *name = patch->old_name ? patch->old_name : patch->new_name;2715 unsigned char sha1[20];27162717 /*2718 * For safety, we require patch index line to contain2719 * full 40-byte textual SHA1 for old and new, at least for now.2720 */2721 if (strlen(patch->old_sha1_prefix) != 40 ||2722 strlen(patch->new_sha1_prefix) != 40 ||2723 get_sha1_hex(patch->old_sha1_prefix, sha1) ||2724 get_sha1_hex(patch->new_sha1_prefix, sha1))2725 return error("cannot apply binary patch to '%s' "2726 "without full index line", name);27272728 if (patch->old_name) {2729 /*2730 * See if the old one matches what the patch2731 * applies to.2732 */2733 hash_sha1_file(img->buf, img->len, blob_type, sha1);2734 if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))2735 return error("the patch applies to '%s' (%s), "2736 "which does not match the "2737 "current contents.",2738 name, sha1_to_hex(sha1));2739 }2740 else {2741 /* Otherwise, the old one must be empty. */2742 if (img->len)2743 return error("the patch applies to an empty "2744 "'%s' but it is not empty", name);2745 }27462747 get_sha1_hex(patch->new_sha1_prefix, sha1);2748 if (is_null_sha1(sha1)) {2749 clear_image(img);2750 return 0; /* deletion patch */2751 }27522753 if (has_sha1_file(sha1)) {2754 /* We already have the postimage */2755 enum object_type type;2756 unsigned long size;2757 char *result;27582759 result = read_sha1_file(sha1, &type, &size);2760 if (!result)2761 return error("the necessary postimage %s for "2762 "'%s' cannot be read",2763 patch->new_sha1_prefix, name);2764 clear_image(img);2765 img->buf = result;2766 img->len = size;2767 } else {2768 /*2769 * We have verified buf matches the preimage;2770 * apply the patch data to it, which is stored2771 * in the patch->fragments->{patch,size}.2772 */2773 if (apply_binary_fragment(img, patch))2774 return error("binary patch does not apply to '%s'",2775 name);27762777 /* verify that the result matches */2778 hash_sha1_file(img->buf, img->len, blob_type, sha1);2779 if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))2780 return error("binary patch to '%s' creates incorrect result (expecting %s, got %s)",2781 name, patch->new_sha1_prefix, sha1_to_hex(sha1));2782 }27832784 return 0;2785}27862787static int apply_fragments(struct image *img, struct patch *patch)2788{2789 struct fragment *frag = patch->fragments;2790 const char *name = patch->old_name ? patch->old_name : patch->new_name;2791 unsigned ws_rule = patch->ws_rule;2792 unsigned inaccurate_eof = patch->inaccurate_eof;27932794 if (patch->is_binary)2795 return apply_binary(img, patch);27962797 while (frag) {2798 if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule)) {2799 error("patch failed: %s:%ld", name, frag->oldpos);2800 if (!apply_with_reject)2801 return -1;2802 frag->rejected = 1;2803 }2804 frag = frag->next;2805 }2806 return 0;2807}28082809static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)2810{2811 if (!ce)2812 return 0;28132814 if (S_ISGITLINK(ce->ce_mode)) {2815 strbuf_grow(buf, 100);2816 strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(ce->sha1));2817 } else {2818 enum object_type type;2819 unsigned long sz;2820 char *result;28212822 result = read_sha1_file(ce->sha1, &type, &sz);2823 if (!result)2824 return -1;2825 /* XXX read_sha1_file NUL-terminates */2826 strbuf_attach(buf, result, sz, sz + 1);2827 }2828 return 0;2829}28302831static struct patch *in_fn_table(const char *name)2832{2833 struct string_list_item *item;28342835 if (name == NULL)2836 return NULL;28372838 item = string_list_lookup(&fn_table, name);2839 if (item != NULL)2840 return (struct patch *)item->util;28412842 return NULL;2843}28442845/*2846 * item->util in the filename table records the status of the path.2847 * Usually it points at a patch (whose result records the contents2848 * of it after applying it), but it could be PATH_WAS_DELETED for a2849 * path that a previously applied patch has already removed.2850 */2851 #define PATH_TO_BE_DELETED ((struct patch *) -2)2852#define PATH_WAS_DELETED ((struct patch *) -1)28532854static int to_be_deleted(struct patch *patch)2855{2856 return patch == PATH_TO_BE_DELETED;2857}28582859static int was_deleted(struct patch *patch)2860{2861 return patch == PATH_WAS_DELETED;2862}28632864static void add_to_fn_table(struct patch *patch)2865{2866 struct string_list_item *item;28672868 /*2869 * Always add new_name unless patch is a deletion2870 * This should cover the cases for normal diffs,2871 * file creations and copies2872 */2873 if (patch->new_name != NULL) {2874 item = string_list_insert(&fn_table, patch->new_name);2875 item->util = patch;2876 }28772878 /*2879 * store a failure on rename/deletion cases because2880 * later chunks shouldn't patch old names2881 */2882 if ((patch->new_name == NULL) || (patch->is_rename)) {2883 item = string_list_insert(&fn_table, patch->old_name);2884 item->util = PATH_WAS_DELETED;2885 }2886}28872888static void prepare_fn_table(struct patch *patch)2889{2890 /*2891 * store information about incoming file deletion2892 */2893 while (patch) {2894 if ((patch->new_name == NULL) || (patch->is_rename)) {2895 struct string_list_item *item;2896 item = string_list_insert(&fn_table, patch->old_name);2897 item->util = PATH_TO_BE_DELETED;2898 }2899 patch = patch->next;2900 }2901}29022903static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)2904{2905 struct strbuf buf = STRBUF_INIT;2906 struct image image;2907 size_t len;2908 char *img;2909 struct patch *tpatch;29102911 if (!(patch->is_copy || patch->is_rename) &&2912 (tpatch = in_fn_table(patch->old_name)) != NULL && !to_be_deleted(tpatch)) {2913 if (was_deleted(tpatch)) {2914 return error("patch %s has been renamed/deleted",2915 patch->old_name);2916 }2917 /* We have a patched copy in memory use that */2918 strbuf_add(&buf, tpatch->result, tpatch->resultsize);2919 } else if (cached) {2920 if (read_file_or_gitlink(ce, &buf))2921 return error("read of %s failed", patch->old_name);2922 } else if (patch->old_name) {2923 if (S_ISGITLINK(patch->old_mode)) {2924 if (ce) {2925 read_file_or_gitlink(ce, &buf);2926 } else {2927 /*2928 * There is no way to apply subproject2929 * patch without looking at the index.2930 */2931 patch->fragments = NULL;2932 }2933 } else {2934 if (read_old_data(st, patch->old_name, &buf))2935 return error("read of %s failed", patch->old_name);2936 }2937 }29382939 img = strbuf_detach(&buf, &len);2940 prepare_image(&image, img, len, !patch->is_binary);29412942 if (apply_fragments(&image, patch) < 0)2943 return -1; /* note with --reject this succeeds. */2944 patch->result = image.buf;2945 patch->resultsize = image.len;2946 add_to_fn_table(patch);2947 free(image.line_allocated);29482949 if (0 < patch->is_delete && patch->resultsize)2950 return error("removal patch leaves file contents");29512952 return 0;2953}29542955static int check_to_create_blob(const char *new_name, int ok_if_exists)2956{2957 struct stat nst;2958 if (!lstat(new_name, &nst)) {2959 if (S_ISDIR(nst.st_mode) || ok_if_exists)2960 return 0;2961 /*2962 * A leading component of new_name might be a symlink2963 * that is going to be removed with this patch, but2964 * still pointing at somewhere that has the path.2965 * In such a case, path "new_name" does not exist as2966 * far as git is concerned.2967 */2968 if (has_symlink_leading_path(new_name, strlen(new_name)))2969 return 0;29702971 return error("%s: already exists in working directory", new_name);2972 }2973 else if ((errno != ENOENT) && (errno != ENOTDIR))2974 return error("%s: %s", new_name, strerror(errno));2975 return 0;2976}29772978static int verify_index_match(struct cache_entry *ce, struct stat *st)2979{2980 if (S_ISGITLINK(ce->ce_mode)) {2981 if (!S_ISDIR(st->st_mode))2982 return -1;2983 return 0;2984 }2985 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);2986}29872988static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)2989{2990 const char *old_name = patch->old_name;2991 struct patch *tpatch = NULL;2992 int stat_ret = 0;2993 unsigned st_mode = 0;29942995 /*2996 * Make sure that we do not have local modifications from the2997 * index when we are looking at the index. Also make sure2998 * we have the preimage file to be patched in the work tree,2999 * unless --cached, which tells git to apply only in the index.3000 */3001 if (!old_name)3002 return 0;30033004 assert(patch->is_new <= 0);30053006 if (!(patch->is_copy || patch->is_rename) &&3007 (tpatch = in_fn_table(old_name)) != NULL && !to_be_deleted(tpatch)) {3008 if (was_deleted(tpatch))3009 return error("%s: has been deleted/renamed", old_name);3010 st_mode = tpatch->new_mode;3011 } else if (!cached) {3012 stat_ret = lstat(old_name, st);3013 if (stat_ret && errno != ENOENT)3014 return error("%s: %s", old_name, strerror(errno));3015 }30163017 if (to_be_deleted(tpatch))3018 tpatch = NULL;30193020 if (check_index && !tpatch) {3021 int pos = cache_name_pos(old_name, strlen(old_name));3022 if (pos < 0) {3023 if (patch->is_new < 0)3024 goto is_new;3025 return error("%s: does not exist in index", old_name);3026 }3027 *ce = active_cache[pos];3028 if (stat_ret < 0) {3029 struct checkout costate;3030 /* checkout */3031 memset(&costate, 0, sizeof(costate));3032 costate.base_dir = "";3033 costate.refresh_cache = 1;3034 if (checkout_entry(*ce, &costate, NULL) ||3035 lstat(old_name, st))3036 return -1;3037 }3038 if (!cached && verify_index_match(*ce, st))3039 return error("%s: does not match index", old_name);3040 if (cached)3041 st_mode = (*ce)->ce_mode;3042 } else if (stat_ret < 0) {3043 if (patch->is_new < 0)3044 goto is_new;3045 return error("%s: %s", old_name, strerror(errno));3046 }30473048 if (!cached && !tpatch)3049 st_mode = ce_mode_from_stat(*ce, st->st_mode);30503051 if (patch->is_new < 0)3052 patch->is_new = 0;3053 if (!patch->old_mode)3054 patch->old_mode = st_mode;3055 if ((st_mode ^ patch->old_mode) & S_IFMT)3056 return error("%s: wrong type", old_name);3057 if (st_mode != patch->old_mode)3058 warning("%s has type %o, expected %o",3059 old_name, st_mode, patch->old_mode);3060 if (!patch->new_mode && !patch->is_delete)3061 patch->new_mode = st_mode;3062 return 0;30633064 is_new:3065 patch->is_new = 1;3066 patch->is_delete = 0;3067 patch->old_name = NULL;3068 return 0;3069}30703071static int check_patch(struct patch *patch)3072{3073 struct stat st;3074 const char *old_name = patch->old_name;3075 const char *new_name = patch->new_name;3076 const char *name = old_name ? old_name : new_name;3077 struct cache_entry *ce = NULL;3078 struct patch *tpatch;3079 int ok_if_exists;3080 int status;30813082 patch->rejected = 1; /* we will drop this after we succeed */30833084 status = check_preimage(patch, &ce, &st);3085 if (status)3086 return status;3087 old_name = patch->old_name;30883089 if ((tpatch = in_fn_table(new_name)) &&3090 (was_deleted(tpatch) || to_be_deleted(tpatch)))3091 /*3092 * A type-change diff is always split into a patch to3093 * delete old, immediately followed by a patch to3094 * create new (see diff.c::run_diff()); in such a case3095 * it is Ok that the entry to be deleted by the3096 * previous patch is still in the working tree and in3097 * the index.3098 */3099 ok_if_exists = 1;3100 else3101 ok_if_exists = 0;31023103 if (new_name &&3104 ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {3105 if (check_index &&3106 cache_name_pos(new_name, strlen(new_name)) >= 0 &&3107 !ok_if_exists)3108 return error("%s: already exists in index", new_name);3109 if (!cached) {3110 int err = check_to_create_blob(new_name, ok_if_exists);3111 if (err)3112 return err;3113 }3114 if (!patch->new_mode) {3115 if (0 < patch->is_new)3116 patch->new_mode = S_IFREG | 0644;3117 else3118 patch->new_mode = patch->old_mode;3119 }3120 }31213122 if (new_name && old_name) {3123 int same = !strcmp(old_name, new_name);3124 if (!patch->new_mode)3125 patch->new_mode = patch->old_mode;3126 if ((patch->old_mode ^ patch->new_mode) & S_IFMT)3127 return error("new mode (%o) of %s does not match old mode (%o)%s%s",3128 patch->new_mode, new_name, patch->old_mode,3129 same ? "" : " of ", same ? "" : old_name);3130 }31313132 if (apply_data(patch, &st, ce) < 0)3133 return error("%s: patch does not apply", name);3134 patch->rejected = 0;3135 return 0;3136}31373138static int check_patch_list(struct patch *patch)3139{3140 int err = 0;31413142 prepare_fn_table(patch);3143 while (patch) {3144 if (apply_verbosely)3145 say_patch_name(stderr,3146 "Checking patch ", patch, "...\n");3147 err |= check_patch(patch);3148 patch = patch->next;3149 }3150 return err;3151}31523153/* This function tries to read the sha1 from the current index */3154static int get_current_sha1(const char *path, unsigned char *sha1)3155{3156 int pos;31573158 if (read_cache() < 0)3159 return -1;3160 pos = cache_name_pos(path, strlen(path));3161 if (pos < 0)3162 return -1;3163 hashcpy(sha1, active_cache[pos]->sha1);3164 return 0;3165}31663167/* Build an index that contains the just the files needed for a 3way merge */3168static void build_fake_ancestor(struct patch *list, const char *filename)3169{3170 struct patch *patch;3171 struct index_state result = { NULL };3172 int fd;31733174 /* Once we start supporting the reverse patch, it may be3175 * worth showing the new sha1 prefix, but until then...3176 */3177 for (patch = list; patch; patch = patch->next) {3178 const unsigned char *sha1_ptr;3179 unsigned char sha1[20];3180 struct cache_entry *ce;3181 const char *name;31823183 name = patch->old_name ? patch->old_name : patch->new_name;3184 if (0 < patch->is_new)3185 continue;3186 else if (get_sha1(patch->old_sha1_prefix, sha1))3187 /* git diff has no index line for mode/type changes */3188 if (!patch->lines_added && !patch->lines_deleted) {3189 if (get_current_sha1(patch->old_name, sha1))3190 die("mode change for %s, which is not "3191 "in current HEAD", name);3192 sha1_ptr = sha1;3193 } else3194 die("sha1 information is lacking or useless "3195 "(%s).", name);3196 else3197 sha1_ptr = sha1;31983199 ce = make_cache_entry(patch->old_mode, sha1_ptr, name, 0, 0);3200 if (!ce)3201 die("make_cache_entry failed for path '%s'", name);3202 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))3203 die ("Could not add %s to temporary index", name);3204 }32053206 fd = open(filename, O_WRONLY | O_CREAT, 0666);3207 if (fd < 0 || write_index(&result, fd) || close(fd))3208 die ("Could not write temporary index to %s", filename);32093210 discard_index(&result);3211}32123213static void stat_patch_list(struct patch *patch)3214{3215 int files, adds, dels;32163217 for (files = adds = dels = 0 ; patch ; patch = patch->next) {3218 files++;3219 adds += patch->lines_added;3220 dels += patch->lines_deleted;3221 show_stats(patch);3222 }32233224 printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);3225}32263227static void numstat_patch_list(struct patch *patch)3228{3229 for ( ; patch; patch = patch->next) {3230 const char *name;3231 name = patch->new_name ? patch->new_name : patch->old_name;3232 if (patch->is_binary)3233 printf("-\t-\t");3234 else3235 printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);3236 write_name_quoted(name, stdout, line_termination);3237 }3238}32393240static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)3241{3242 if (mode)3243 printf(" %s mode %06o %s\n", newdelete, mode, name);3244 else3245 printf(" %s %s\n", newdelete, name);3246}32473248static void show_mode_change(struct patch *p, int show_name)3249{3250 if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {3251 if (show_name)3252 printf(" mode change %06o => %06o %s\n",3253 p->old_mode, p->new_mode, p->new_name);3254 else3255 printf(" mode change %06o => %06o\n",3256 p->old_mode, p->new_mode);3257 }3258}32593260static void show_rename_copy(struct patch *p)3261{3262 const char *renamecopy = p->is_rename ? "rename" : "copy";3263 const char *old, *new;32643265 /* Find common prefix */3266 old = p->old_name;3267 new = p->new_name;3268 while (1) {3269 const char *slash_old, *slash_new;3270 slash_old = strchr(old, '/');3271 slash_new = strchr(new, '/');3272 if (!slash_old ||3273 !slash_new ||3274 slash_old - old != slash_new - new ||3275 memcmp(old, new, slash_new - new))3276 break;3277 old = slash_old + 1;3278 new = slash_new + 1;3279 }3280 /* p->old_name thru old is the common prefix, and old and new3281 * through the end of names are renames3282 */3283 if (old != p->old_name)3284 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,3285 (int)(old - p->old_name), p->old_name,3286 old, new, p->score);3287 else3288 printf(" %s %s => %s (%d%%)\n", renamecopy,3289 p->old_name, p->new_name, p->score);3290 show_mode_change(p, 0);3291}32923293static void summary_patch_list(struct patch *patch)3294{3295 struct patch *p;32963297 for (p = patch; p; p = p->next) {3298 if (p->is_new)3299 show_file_mode_name("create", p->new_mode, p->new_name);3300 else if (p->is_delete)3301 show_file_mode_name("delete", p->old_mode, p->old_name);3302 else {3303 if (p->is_rename || p->is_copy)3304 show_rename_copy(p);3305 else {3306 if (p->score) {3307 printf(" rewrite %s (%d%%)\n",3308 p->new_name, p->score);3309 show_mode_change(p, 0);3310 }3311 else3312 show_mode_change(p, 1);3313 }3314 }3315 }3316}33173318static void patch_stats(struct patch *patch)3319{3320 int lines = patch->lines_added + patch->lines_deleted;33213322 if (lines > max_change)3323 max_change = lines;3324 if (patch->old_name) {3325 int len = quote_c_style(patch->old_name, NULL, NULL, 0);3326 if (!len)3327 len = strlen(patch->old_name);3328 if (len > max_len)3329 max_len = len;3330 }3331 if (patch->new_name) {3332 int len = quote_c_style(patch->new_name, NULL, NULL, 0);3333 if (!len)3334 len = strlen(patch->new_name);3335 if (len > max_len)3336 max_len = len;3337 }3338}33393340static void remove_file(struct patch *patch, int rmdir_empty)3341{3342 if (update_index) {3343 if (remove_file_from_cache(patch->old_name) < 0)3344 die("unable to remove %s from index", patch->old_name);3345 }3346 if (!cached) {3347 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {3348 remove_path(patch->old_name);3349 }3350 }3351}33523353static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)3354{3355 struct stat st;3356 struct cache_entry *ce;3357 int namelen = strlen(path);3358 unsigned ce_size = cache_entry_size(namelen);33593360 if (!update_index)3361 return;33623363 ce = xcalloc(1, ce_size);3364 memcpy(ce->name, path, namelen);3365 ce->ce_mode = create_ce_mode(mode);3366 ce->ce_flags = namelen;3367 if (S_ISGITLINK(mode)) {3368 const char *s = buf;33693370 if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))3371 die("corrupt patch for subproject %s", path);3372 } else {3373 if (!cached) {3374 if (lstat(path, &st) < 0)3375 die_errno("unable to stat newly created file '%s'",3376 path);3377 fill_stat_cache_info(ce, &st);3378 }3379 if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)3380 die("unable to create backing store for newly created file %s", path);3381 }3382 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)3383 die("unable to add cache entry for %s", path);3384}33853386static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)3387{3388 int fd;3389 struct strbuf nbuf = STRBUF_INIT;33903391 if (S_ISGITLINK(mode)) {3392 struct stat st;3393 if (!lstat(path, &st) && S_ISDIR(st.st_mode))3394 return 0;3395 return mkdir(path, 0777);3396 }33973398 if (has_symlinks && S_ISLNK(mode))3399 /* Although buf:size is counted string, it also is NUL3400 * terminated.3401 */3402 return symlink(buf, path);34033404 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);3405 if (fd < 0)3406 return -1;34073408 if (convert_to_working_tree(path, buf, size, &nbuf)) {3409 size = nbuf.len;3410 buf = nbuf.buf;3411 }3412 write_or_die(fd, buf, size);3413 strbuf_release(&nbuf);34143415 if (close(fd) < 0)3416 die_errno("closing file '%s'", path);3417 return 0;3418}34193420/*3421 * We optimistically assume that the directories exist,3422 * which is true 99% of the time anyway. If they don't,3423 * we create them and try again.3424 */3425static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)3426{3427 if (cached)3428 return;3429 if (!try_create_file(path, mode, buf, size))3430 return;34313432 if (errno == ENOENT) {3433 if (safe_create_leading_directories(path))3434 return;3435 if (!try_create_file(path, mode, buf, size))3436 return;3437 }34383439 if (errno == EEXIST || errno == EACCES) {3440 /* We may be trying to create a file where a directory3441 * used to be.3442 */3443 struct stat st;3444 if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))3445 errno = EEXIST;3446 }34473448 if (errno == EEXIST) {3449 unsigned int nr = getpid();34503451 for (;;) {3452 char newpath[PATH_MAX];3453 mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);3454 if (!try_create_file(newpath, mode, buf, size)) {3455 if (!rename(newpath, path))3456 return;3457 unlink_or_warn(newpath);3458 break;3459 }3460 if (errno != EEXIST)3461 break;3462 ++nr;3463 }3464 }3465 die_errno("unable to write file '%s' mode %o", path, mode);3466}34673468static void create_file(struct patch *patch)3469{3470 char *path = patch->new_name;3471 unsigned mode = patch->new_mode;3472 unsigned long size = patch->resultsize;3473 char *buf = patch->result;34743475 if (!mode)3476 mode = S_IFREG | 0644;3477 create_one_file(path, mode, buf, size);3478 add_index_file(path, mode, buf, size);3479}34803481/* phase zero is to remove, phase one is to create */3482static void write_out_one_result(struct patch *patch, int phase)3483{3484 if (patch->is_delete > 0) {3485 if (phase == 0)3486 remove_file(patch, 1);3487 return;3488 }3489 if (patch->is_new > 0 || patch->is_copy) {3490 if (phase == 1)3491 create_file(patch);3492 return;3493 }3494 /*3495 * Rename or modification boils down to the same3496 * thing: remove the old, write the new3497 */3498 if (phase == 0)3499 remove_file(patch, patch->is_rename);3500 if (phase == 1)3501 create_file(patch);3502}35033504static int write_out_one_reject(struct patch *patch)3505{3506 FILE *rej;3507 char namebuf[PATH_MAX];3508 struct fragment *frag;3509 int cnt = 0;35103511 for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {3512 if (!frag->rejected)3513 continue;3514 cnt++;3515 }35163517 if (!cnt) {3518 if (apply_verbosely)3519 say_patch_name(stderr,3520 "Applied patch ", patch, " cleanly.\n");3521 return 0;3522 }35233524 /* This should not happen, because a removal patch that leaves3525 * contents are marked "rejected" at the patch level.3526 */3527 if (!patch->new_name)3528 die("internal error");35293530 /* Say this even without --verbose */3531 say_patch_name(stderr, "Applying patch ", patch, " with");3532 fprintf(stderr, " %d rejects...\n", cnt);35333534 cnt = strlen(patch->new_name);3535 if (ARRAY_SIZE(namebuf) <= cnt + 5) {3536 cnt = ARRAY_SIZE(namebuf) - 5;3537 warning("truncating .rej filename to %.*s.rej",3538 cnt - 1, patch->new_name);3539 }3540 memcpy(namebuf, patch->new_name, cnt);3541 memcpy(namebuf + cnt, ".rej", 5);35423543 rej = fopen(namebuf, "w");3544 if (!rej)3545 return error("cannot open %s: %s", namebuf, strerror(errno));35463547 /* Normal git tools never deal with .rej, so do not pretend3548 * this is a git patch by saying --git nor give extended3549 * headers. While at it, maybe please "kompare" that wants3550 * the trailing TAB and some garbage at the end of line ;-).3551 */3552 fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",3553 patch->new_name, patch->new_name);3554 for (cnt = 1, frag = patch->fragments;3555 frag;3556 cnt++, frag = frag->next) {3557 if (!frag->rejected) {3558 fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);3559 continue;3560 }3561 fprintf(stderr, "Rejected hunk #%d.\n", cnt);3562 fprintf(rej, "%.*s", frag->size, frag->patch);3563 if (frag->patch[frag->size-1] != '\n')3564 fputc('\n', rej);3565 }3566 fclose(rej);3567 return -1;3568}35693570static int write_out_results(struct patch *list, int skipped_patch)3571{3572 int phase;3573 int errs = 0;3574 struct patch *l;35753576 if (!list && !skipped_patch)3577 return error("No changes");35783579 for (phase = 0; phase < 2; phase++) {3580 l = list;3581 while (l) {3582 if (l->rejected)3583 errs = 1;3584 else {3585 write_out_one_result(l, phase);3586 if (phase == 1 && write_out_one_reject(l))3587 errs = 1;3588 }3589 l = l->next;3590 }3591 }3592 return errs;3593}35943595static struct lock_file lock_file;35963597static struct string_list limit_by_name;3598static int has_include;3599static void add_name_limit(const char *name, int exclude)3600{3601 struct string_list_item *it;36023603 it = string_list_append(&limit_by_name, name);3604 it->util = exclude ? NULL : (void *) 1;3605}36063607static int use_patch(struct patch *p)3608{3609 const char *pathname = p->new_name ? p->new_name : p->old_name;3610 int i;36113612 /* Paths outside are not touched regardless of "--include" */3613 if (0 < prefix_length) {3614 int pathlen = strlen(pathname);3615 if (pathlen <= prefix_length ||3616 memcmp(prefix, pathname, prefix_length))3617 return 0;3618 }36193620 /* See if it matches any of exclude/include rule */3621 for (i = 0; i < limit_by_name.nr; i++) {3622 struct string_list_item *it = &limit_by_name.items[i];3623 if (!fnmatch(it->string, pathname, 0))3624 return (it->util != NULL);3625 }36263627 /*3628 * If we had any include, a path that does not match any rule is3629 * not used. Otherwise, we saw bunch of exclude rules (or none)3630 * and such a path is used.3631 */3632 return !has_include;3633}363436353636static void prefix_one(char **name)3637{3638 char *old_name = *name;3639 if (!old_name)3640 return;3641 *name = xstrdup(prefix_filename(prefix, prefix_length, *name));3642 free(old_name);3643}36443645static void prefix_patches(struct patch *p)3646{3647 if (!prefix || p->is_toplevel_relative)3648 return;3649 for ( ; p; p = p->next) {3650 if (p->new_name == p->old_name) {3651 char *prefixed = p->new_name;3652 prefix_one(&prefixed);3653 p->new_name = p->old_name = prefixed;3654 }3655 else {3656 prefix_one(&p->new_name);3657 prefix_one(&p->old_name);3658 }3659 }3660}36613662#define INACCURATE_EOF (1<<0)3663#define RECOUNT (1<<1)36643665static int apply_patch(int fd, const char *filename, int options)3666{3667 size_t offset;3668 struct strbuf buf = STRBUF_INIT;3669 struct patch *list = NULL, **listp = &list;3670 int skipped_patch = 0;36713672 /* FIXME - memory leak when using multiple patch files as inputs */3673 memset(&fn_table, 0, sizeof(struct string_list));3674 patch_input_file = filename;3675 read_patch_file(&buf, fd);3676 offset = 0;3677 while (offset < buf.len) {3678 struct patch *patch;3679 int nr;36803681 patch = xcalloc(1, sizeof(*patch));3682 patch->inaccurate_eof = !!(options & INACCURATE_EOF);3683 patch->recount = !!(options & RECOUNT);3684 nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);3685 if (nr < 0)3686 break;3687 if (apply_in_reverse)3688 reverse_patches(patch);3689 if (prefix)3690 prefix_patches(patch);3691 if (use_patch(patch)) {3692 patch_stats(patch);3693 *listp = patch;3694 listp = &patch->next;3695 }3696 else {3697 /* perhaps free it a bit better? */3698 free(patch);3699 skipped_patch++;3700 }3701 offset += nr;3702 }37033704 if (whitespace_error && (ws_error_action == die_on_ws_error))3705 apply = 0;37063707 update_index = check_index && apply;3708 if (update_index && newfd < 0)3709 newfd = hold_locked_index(&lock_file, 1);37103711 if (check_index) {3712 if (read_cache() < 0)3713 die("unable to read index file");3714 }37153716 if ((check || apply) &&3717 check_patch_list(list) < 0 &&3718 !apply_with_reject)3719 exit(1);37203721 if (apply && write_out_results(list, skipped_patch))3722 exit(1);37233724 if (fake_ancestor)3725 build_fake_ancestor(list, fake_ancestor);37263727 if (diffstat)3728 stat_patch_list(list);37293730 if (numstat)3731 numstat_patch_list(list);37323733 if (summary)3734 summary_patch_list(list);37353736 strbuf_release(&buf);3737 return 0;3738}37393740static int git_apply_config(const char *var, const char *value, void *cb)3741{3742 if (!strcmp(var, "apply.whitespace"))3743 return git_config_string(&apply_default_whitespace, var, value);3744 else if (!strcmp(var, "apply.ignorewhitespace"))3745 return git_config_string(&apply_default_ignorewhitespace, var, value);3746 return git_default_config(var, value, cb);3747}37483749static int option_parse_exclude(const struct option *opt,3750 const char *arg, int unset)3751{3752 add_name_limit(arg, 1);3753 return 0;3754}37553756static int option_parse_include(const struct option *opt,3757 const char *arg, int unset)3758{3759 add_name_limit(arg, 0);3760 has_include = 1;3761 return 0;3762}37633764static int option_parse_p(const struct option *opt,3765 const char *arg, int unset)3766{3767 p_value = atoi(arg);3768 p_value_known = 1;3769 return 0;3770}37713772static int option_parse_z(const struct option *opt,3773 const char *arg, int unset)3774{3775 if (unset)3776 line_termination = '\n';3777 else3778 line_termination = 0;3779 return 0;3780}37813782static int option_parse_space_change(const struct option *opt,3783 const char *arg, int unset)3784{3785 if (unset)3786 ws_ignore_action = ignore_ws_none;3787 else3788 ws_ignore_action = ignore_ws_change;3789 return 0;3790}37913792static int option_parse_whitespace(const struct option *opt,3793 const char *arg, int unset)3794{3795 const char **whitespace_option = opt->value;37963797 *whitespace_option = arg;3798 parse_whitespace_option(arg);3799 return 0;3800}38013802static int option_parse_directory(const struct option *opt,3803 const char *arg, int unset)3804{3805 root_len = strlen(arg);3806 if (root_len && arg[root_len - 1] != '/') {3807 char *new_root;3808 root = new_root = xmalloc(root_len + 2);3809 strcpy(new_root, arg);3810 strcpy(new_root + root_len++, "/");3811 } else3812 root = arg;3813 return 0;3814}38153816int cmd_apply(int argc, const char **argv, const char *prefix_)3817{3818 int i;3819 int errs = 0;3820 int is_not_gitdir = !startup_info->have_repository;3821 int binary;3822 int force_apply = 0;38233824 const char *whitespace_option = NULL;38253826 struct option builtin_apply_options[] = {3827 { OPTION_CALLBACK, 0, "exclude", NULL, "path",3828 "don't apply changes matching the given path",3829 0, option_parse_exclude },3830 { OPTION_CALLBACK, 0, "include", NULL, "path",3831 "apply changes matching the given path",3832 0, option_parse_include },3833 { OPTION_CALLBACK, 'p', NULL, NULL, "num",3834 "remove <num> leading slashes from traditional diff paths",3835 0, option_parse_p },3836 OPT_BOOLEAN(0, "no-add", &no_add,3837 "ignore additions made by the patch"),3838 OPT_BOOLEAN(0, "stat", &diffstat,3839 "instead of applying the patch, output diffstat for the input"),3840 { OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,3841 NULL, "old option, now no-op",3842 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },3843 { OPTION_BOOLEAN, 0, "binary", &binary,3844 NULL, "old option, now no-op",3845 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },3846 OPT_BOOLEAN(0, "numstat", &numstat,3847 "shows number of added and deleted lines in decimal notation"),3848 OPT_BOOLEAN(0, "summary", &summary,3849 "instead of applying the patch, output a summary for the input"),3850 OPT_BOOLEAN(0, "check", &check,3851 "instead of applying the patch, see if the patch is applicable"),3852 OPT_BOOLEAN(0, "index", &check_index,3853 "make sure the patch is applicable to the current index"),3854 OPT_BOOLEAN(0, "cached", &cached,3855 "apply a patch without touching the working tree"),3856 OPT_BOOLEAN(0, "apply", &force_apply,3857 "also apply the patch (use with --stat/--summary/--check)"),3858 OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,3859 "build a temporary index based on embedded index information"),3860 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,3861 "paths are separated with NUL character",3862 PARSE_OPT_NOARG, option_parse_z },3863 OPT_INTEGER('C', NULL, &p_context,3864 "ensure at least <n> lines of context match"),3865 { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, "action",3866 "detect new or modified lines that have whitespace errors",3867 0, option_parse_whitespace },3868 { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,3869 "ignore changes in whitespace when finding context",3870 PARSE_OPT_NOARG, option_parse_space_change },3871 { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,3872 "ignore changes in whitespace when finding context",3873 PARSE_OPT_NOARG, option_parse_space_change },3874 OPT_BOOLEAN('R', "reverse", &apply_in_reverse,3875 "apply the patch in reverse"),3876 OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero,3877 "don't expect at least one line of context"),3878 OPT_BOOLEAN(0, "reject", &apply_with_reject,3879 "leave the rejected hunks in corresponding *.rej files"),3880 OPT__VERBOSE(&apply_verbosely, "be verbose"),3881 OPT_BIT(0, "inaccurate-eof", &options,3882 "tolerate incorrectly detected missing new-line at the end of file",3883 INACCURATE_EOF),3884 OPT_BIT(0, "recount", &options,3885 "do not trust the line counts in the hunk headers",3886 RECOUNT),3887 { OPTION_CALLBACK, 0, "directory", NULL, "root",3888 "prepend <root> to all filenames",3889 0, option_parse_directory },3890 OPT_END()3891 };38923893 prefix = prefix_;3894 prefix_length = prefix ? strlen(prefix) : 0;3895 git_config(git_apply_config, NULL);3896 if (apply_default_whitespace)3897 parse_whitespace_option(apply_default_whitespace);3898 if (apply_default_ignorewhitespace)3899 parse_ignorewhitespace_option(apply_default_ignorewhitespace);39003901 argc = parse_options(argc, argv, prefix, builtin_apply_options,3902 apply_usage, 0);39033904 if (apply_with_reject)3905 apply = apply_verbosely = 1;3906 if (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))3907 apply = 0;3908 if (check_index && is_not_gitdir)3909 die("--index outside a repository");3910 if (cached) {3911 if (is_not_gitdir)3912 die("--cached outside a repository");3913 check_index = 1;3914 }3915 for (i = 0; i < argc; i++) {3916 const char *arg = argv[i];3917 int fd;39183919 if (!strcmp(arg, "-")) {3920 errs |= apply_patch(0, "<stdin>", options);3921 read_stdin = 0;3922 continue;3923 } else if (0 < prefix_length)3924 arg = prefix_filename(prefix, prefix_length, arg);39253926 fd = open(arg, O_RDONLY);3927 if (fd < 0)3928 die_errno("can't open patch '%s'", arg);3929 read_stdin = 0;3930 set_default_whitespace_mode(whitespace_option);3931 errs |= apply_patch(fd, arg, options);3932 close(fd);3933 }3934 set_default_whitespace_mode(whitespace_option);3935 if (read_stdin)3936 errs |= apply_patch(0, "<stdin>", options);3937 if (whitespace_error) {3938 if (squelch_whitespace_errors &&3939 squelch_whitespace_errors < whitespace_error) {3940 int squelched =3941 whitespace_error - squelch_whitespace_errors;3942 warning("squelched %d "3943 "whitespace error%s",3944 squelched,3945 squelched == 1 ? "" : "s");3946 }3947 if (ws_error_action == die_on_ws_error)3948 die("%d line%s add%s whitespace errors.",3949 whitespace_error,3950 whitespace_error == 1 ? "" : "s",3951 whitespace_error == 1 ? "s" : "");3952 if (applied_after_fixing_ws && apply)3953 warning("%d line%s applied after"3954 " fixing whitespace errors.",3955 applied_after_fixing_ws,3956 applied_after_fixing_ws == 1 ? "" : "s");3957 else if (whitespace_error)3958 warning("%d line%s add%s whitespace errors.",3959 whitespace_error,3960 whitespace_error == 1 ? "" : "s",3961 whitespace_error == 1 ? "s" : "");3962 }39633964 if (update_index) {3965 if (write_cache(newfd, active_cache, active_nr) ||3966 commit_locked_index(&lock_file))3967 die("Unable to write new index file");3968 }39693970 return !!errs;3971}