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"lockfile.h" 11#include"cache-tree.h" 12#include"quote.h" 13#include"blob.h" 14#include"delta.h" 15#include"builtin.h" 16#include"string-list.h" 17#include"dir.h" 18#include"diff.h" 19#include"parse-options.h" 20#include"xdiff-interface.h" 21#include"ll-merge.h" 22#include"rerere.h" 23#include"apply.h" 24 25static const char*const apply_usage[] = { 26N_("git apply [<options>] [<patch>...]"), 27 NULL 28}; 29 30static voidset_default_whitespace_mode(struct apply_state *state) 31{ 32if(!state->whitespace_option && !apply_default_whitespace) 33 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error); 34} 35 36/* 37 * This represents one "hunk" from a patch, starting with 38 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The 39 * patch text is pointed at by patch, and its byte length 40 * is stored in size. leading and trailing are the number 41 * of context lines. 42 */ 43struct fragment { 44unsigned long leading, trailing; 45unsigned long oldpos, oldlines; 46unsigned long newpos, newlines; 47/* 48 * 'patch' is usually borrowed from buf in apply_patch(), 49 * but some codepaths store an allocated buffer. 50 */ 51const char*patch; 52unsigned free_patch:1, 53 rejected:1; 54int size; 55int linenr; 56struct fragment *next; 57}; 58 59/* 60 * When dealing with a binary patch, we reuse "leading" field 61 * to store the type of the binary hunk, either deflated "delta" 62 * or deflated "literal". 63 */ 64#define binary_patch_method leading 65#define BINARY_DELTA_DEFLATED 1 66#define BINARY_LITERAL_DEFLATED 2 67 68/* 69 * This represents a "patch" to a file, both metainfo changes 70 * such as creation/deletion, filemode and content changes represented 71 * as a series of fragments. 72 */ 73struct patch { 74char*new_name, *old_name, *def_name; 75unsigned int old_mode, new_mode; 76int is_new, is_delete;/* -1 = unknown, 0 = false, 1 = true */ 77int rejected; 78unsigned ws_rule; 79int lines_added, lines_deleted; 80int score; 81unsigned int is_toplevel_relative:1; 82unsigned int inaccurate_eof:1; 83unsigned int is_binary:1; 84unsigned int is_copy:1; 85unsigned int is_rename:1; 86unsigned int recount:1; 87unsigned int conflicted_threeway:1; 88unsigned int direct_to_threeway:1; 89struct fragment *fragments; 90char*result; 91size_t resultsize; 92char old_sha1_prefix[41]; 93char new_sha1_prefix[41]; 94struct patch *next; 95 96/* three-way fallback result */ 97struct object_id threeway_stage[3]; 98}; 99 100static voidfree_fragment_list(struct fragment *list) 101{ 102while(list) { 103struct fragment *next = list->next; 104if(list->free_patch) 105free((char*)list->patch); 106free(list); 107 list = next; 108} 109} 110 111static voidfree_patch(struct patch *patch) 112{ 113free_fragment_list(patch->fragments); 114free(patch->def_name); 115free(patch->old_name); 116free(patch->new_name); 117free(patch->result); 118free(patch); 119} 120 121static voidfree_patch_list(struct patch *list) 122{ 123while(list) { 124struct patch *next = list->next; 125free_patch(list); 126 list = next; 127} 128} 129 130/* 131 * A line in a file, len-bytes long (includes the terminating LF, 132 * except for an incomplete line at the end if the file ends with 133 * one), and its contents hashes to 'hash'. 134 */ 135struct line { 136size_t len; 137unsigned hash :24; 138unsigned flag :8; 139#define LINE_COMMON 1 140#define LINE_PATCHED 2 141}; 142 143/* 144 * This represents a "file", which is an array of "lines". 145 */ 146struct image { 147char*buf; 148size_t len; 149size_t nr; 150size_t alloc; 151struct line *line_allocated; 152struct line *line; 153}; 154 155static uint32_thash_line(const char*cp,size_t len) 156{ 157size_t i; 158uint32_t h; 159for(i =0, h =0; i < len; i++) { 160if(!isspace(cp[i])) { 161 h = h *3+ (cp[i] &0xff); 162} 163} 164return h; 165} 166 167/* 168 * Compare lines s1 of length n1 and s2 of length n2, ignoring 169 * whitespace difference. Returns 1 if they match, 0 otherwise 170 */ 171static intfuzzy_matchlines(const char*s1,size_t n1, 172const char*s2,size_t n2) 173{ 174const char*last1 = s1 + n1 -1; 175const char*last2 = s2 + n2 -1; 176int result =0; 177 178/* ignore line endings */ 179while((*last1 =='\r') || (*last1 =='\n')) 180 last1--; 181while((*last2 =='\r') || (*last2 =='\n')) 182 last2--; 183 184/* skip leading whitespaces, if both begin with whitespace */ 185if(s1 <= last1 && s2 <= last2 &&isspace(*s1) &&isspace(*s2)) { 186while(isspace(*s1) && (s1 <= last1)) 187 s1++; 188while(isspace(*s2) && (s2 <= last2)) 189 s2++; 190} 191/* early return if both lines are empty */ 192if((s1 > last1) && (s2 > last2)) 193return1; 194while(!result) { 195 result = *s1++ - *s2++; 196/* 197 * Skip whitespace inside. We check for whitespace on 198 * both buffers because we don't want "a b" to match 199 * "ab" 200 */ 201if(isspace(*s1) &&isspace(*s2)) { 202while(isspace(*s1) && s1 <= last1) 203 s1++; 204while(isspace(*s2) && s2 <= last2) 205 s2++; 206} 207/* 208 * If we reached the end on one side only, 209 * lines don't match 210 */ 211if( 212((s2 > last2) && (s1 <= last1)) || 213((s1 > last1) && (s2 <= last2))) 214return0; 215if((s1 > last1) && (s2 > last2)) 216break; 217} 218 219return!result; 220} 221 222static voidadd_line_info(struct image *img,const char*bol,size_t len,unsigned flag) 223{ 224ALLOC_GROW(img->line_allocated, img->nr +1, img->alloc); 225 img->line_allocated[img->nr].len = len; 226 img->line_allocated[img->nr].hash =hash_line(bol, len); 227 img->line_allocated[img->nr].flag = flag; 228 img->nr++; 229} 230 231/* 232 * "buf" has the file contents to be patched (read from various sources). 233 * attach it to "image" and add line-based index to it. 234 * "image" now owns the "buf". 235 */ 236static voidprepare_image(struct image *image,char*buf,size_t len, 237int prepare_linetable) 238{ 239const char*cp, *ep; 240 241memset(image,0,sizeof(*image)); 242 image->buf = buf; 243 image->len = len; 244 245if(!prepare_linetable) 246return; 247 248 ep = image->buf + image->len; 249 cp = image->buf; 250while(cp < ep) { 251const char*next; 252for(next = cp; next < ep && *next !='\n'; next++) 253; 254if(next < ep) 255 next++; 256add_line_info(image, cp, next - cp,0); 257 cp = next; 258} 259 image->line = image->line_allocated; 260} 261 262static voidclear_image(struct image *image) 263{ 264free(image->buf); 265free(image->line_allocated); 266memset(image,0,sizeof(*image)); 267} 268 269/* fmt must contain _one_ %s and no other substitution */ 270static voidsay_patch_name(FILE*output,const char*fmt,struct patch *patch) 271{ 272struct strbuf sb = STRBUF_INIT; 273 274if(patch->old_name && patch->new_name && 275strcmp(patch->old_name, patch->new_name)) { 276quote_c_style(patch->old_name, &sb, NULL,0); 277strbuf_addstr(&sb," => "); 278quote_c_style(patch->new_name, &sb, NULL,0); 279}else{ 280const char*n = patch->new_name; 281if(!n) 282 n = patch->old_name; 283quote_c_style(n, &sb, NULL,0); 284} 285fprintf(output, fmt, sb.buf); 286fputc('\n', output); 287strbuf_release(&sb); 288} 289 290#define SLOP (16) 291 292static intread_patch_file(struct strbuf *sb,int fd) 293{ 294if(strbuf_read(sb, fd,0) <0) 295returnerror_errno("git apply: failed to read"); 296 297/* 298 * Make sure that we have some slop in the buffer 299 * so that we can do speculative "memcmp" etc, and 300 * see to it that it is NUL-filled. 301 */ 302strbuf_grow(sb, SLOP); 303memset(sb->buf + sb->len,0, SLOP); 304return0; 305} 306 307static unsigned longlinelen(const char*buffer,unsigned long size) 308{ 309unsigned long len =0; 310while(size--) { 311 len++; 312if(*buffer++ =='\n') 313break; 314} 315return len; 316} 317 318static intis_dev_null(const char*str) 319{ 320returnskip_prefix(str,"/dev/null", &str) &&isspace(*str); 321} 322 323#define TERM_SPACE 1 324#define TERM_TAB 2 325 326static intname_terminate(int c,int terminate) 327{ 328if(c ==' '&& !(terminate & TERM_SPACE)) 329return0; 330if(c =='\t'&& !(terminate & TERM_TAB)) 331return0; 332 333return1; 334} 335 336/* remove double slashes to make --index work with such filenames */ 337static char*squash_slash(char*name) 338{ 339int i =0, j =0; 340 341if(!name) 342return NULL; 343 344while(name[i]) { 345if((name[j++] = name[i++]) =='/') 346while(name[i] =='/') 347 i++; 348} 349 name[j] ='\0'; 350return name; 351} 352 353static char*find_name_gnu(struct apply_state *state, 354const char*line, 355const char*def, 356int p_value) 357{ 358struct strbuf name = STRBUF_INIT; 359char*cp; 360 361/* 362 * Proposed "new-style" GNU patch/diff format; see 363 * http://marc.info/?l=git&m=112927316408690&w=2 364 */ 365if(unquote_c_style(&name, line, NULL)) { 366strbuf_release(&name); 367return NULL; 368} 369 370for(cp = name.buf; p_value; p_value--) { 371 cp =strchr(cp,'/'); 372if(!cp) { 373strbuf_release(&name); 374return NULL; 375} 376 cp++; 377} 378 379strbuf_remove(&name,0, cp - name.buf); 380if(state->root.len) 381strbuf_insert(&name,0, state->root.buf, state->root.len); 382returnsquash_slash(strbuf_detach(&name, NULL)); 383} 384 385static size_tsane_tz_len(const char*line,size_t len) 386{ 387const char*tz, *p; 388 389if(len <strlen(" +0500") || line[len-strlen(" +0500")] !=' ') 390return0; 391 tz = line + len -strlen(" +0500"); 392 393if(tz[1] !='+'&& tz[1] !='-') 394return0; 395 396for(p = tz +2; p != line + len; p++) 397if(!isdigit(*p)) 398return0; 399 400return line + len - tz; 401} 402 403static size_ttz_with_colon_len(const char*line,size_t len) 404{ 405const char*tz, *p; 406 407if(len <strlen(" +08:00") || line[len -strlen(":00")] !=':') 408return0; 409 tz = line + len -strlen(" +08:00"); 410 411if(tz[0] !=' '|| (tz[1] !='+'&& tz[1] !='-')) 412return0; 413 p = tz +2; 414if(!isdigit(*p++) || !isdigit(*p++) || *p++ !=':'|| 415!isdigit(*p++) || !isdigit(*p++)) 416return0; 417 418return line + len - tz; 419} 420 421static size_tdate_len(const char*line,size_t len) 422{ 423const char*date, *p; 424 425if(len <strlen("72-02-05") || line[len-strlen("-05")] !='-') 426return0; 427 p = date = line + len -strlen("72-02-05"); 428 429if(!isdigit(*p++) || !isdigit(*p++) || *p++ !='-'|| 430!isdigit(*p++) || !isdigit(*p++) || *p++ !='-'|| 431!isdigit(*p++) || !isdigit(*p++))/* Not a date. */ 432return0; 433 434if(date - line >=strlen("19") && 435isdigit(date[-1]) &&isdigit(date[-2]))/* 4-digit year */ 436 date -=strlen("19"); 437 438return line + len - date; 439} 440 441static size_tshort_time_len(const char*line,size_t len) 442{ 443const char*time, *p; 444 445if(len <strlen(" 07:01:32") || line[len-strlen(":32")] !=':') 446return0; 447 p = time = line + len -strlen(" 07:01:32"); 448 449/* Permit 1-digit hours? */ 450if(*p++ !=' '|| 451!isdigit(*p++) || !isdigit(*p++) || *p++ !=':'|| 452!isdigit(*p++) || !isdigit(*p++) || *p++ !=':'|| 453!isdigit(*p++) || !isdigit(*p++))/* Not a time. */ 454return0; 455 456return line + len - time; 457} 458 459static size_tfractional_time_len(const char*line,size_t len) 460{ 461const char*p; 462size_t n; 463 464/* Expected format: 19:41:17.620000023 */ 465if(!len || !isdigit(line[len -1])) 466return0; 467 p = line + len -1; 468 469/* Fractional seconds. */ 470while(p > line &&isdigit(*p)) 471 p--; 472if(*p !='.') 473return0; 474 475/* Hours, minutes, and whole seconds. */ 476 n =short_time_len(line, p - line); 477if(!n) 478return0; 479 480return line + len - p + n; 481} 482 483static size_ttrailing_spaces_len(const char*line,size_t len) 484{ 485const char*p; 486 487/* Expected format: ' ' x (1 or more) */ 488if(!len || line[len -1] !=' ') 489return0; 490 491 p = line + len; 492while(p != line) { 493 p--; 494if(*p !=' ') 495return line + len - (p +1); 496} 497 498/* All spaces! */ 499return len; 500} 501 502static size_tdiff_timestamp_len(const char*line,size_t len) 503{ 504const char*end = line + len; 505size_t n; 506 507/* 508 * Posix: 2010-07-05 19:41:17 509 * GNU: 2010-07-05 19:41:17.620000023 -0500 510 */ 511 512if(!isdigit(end[-1])) 513return0; 514 515 n =sane_tz_len(line, end - line); 516if(!n) 517 n =tz_with_colon_len(line, end - line); 518 end -= n; 519 520 n =short_time_len(line, end - line); 521if(!n) 522 n =fractional_time_len(line, end - line); 523 end -= n; 524 525 n =date_len(line, end - line); 526if(!n)/* No date. Too bad. */ 527return0; 528 end -= n; 529 530if(end == line)/* No space before date. */ 531return0; 532if(end[-1] =='\t') {/* Success! */ 533 end--; 534return line + len - end; 535} 536if(end[-1] !=' ')/* No space before date. */ 537return0; 538 539/* Whitespace damage. */ 540 end -=trailing_spaces_len(line, end - line); 541return line + len - end; 542} 543 544static char*find_name_common(struct apply_state *state, 545const char*line, 546const char*def, 547int p_value, 548const char*end, 549int terminate) 550{ 551int len; 552const char*start = NULL; 553 554if(p_value ==0) 555 start = line; 556while(line != end) { 557char c = *line; 558 559if(!end &&isspace(c)) { 560if(c =='\n') 561break; 562if(name_terminate(c, terminate)) 563break; 564} 565 line++; 566if(c =='/'&& !--p_value) 567 start = line; 568} 569if(!start) 570returnsquash_slash(xstrdup_or_null(def)); 571 len = line - start; 572if(!len) 573returnsquash_slash(xstrdup_or_null(def)); 574 575/* 576 * Generally we prefer the shorter name, especially 577 * if the other one is just a variation of that with 578 * something else tacked on to the end (ie "file.orig" 579 * or "file~"). 580 */ 581if(def) { 582int deflen =strlen(def); 583if(deflen < len && !strncmp(start, def, deflen)) 584returnsquash_slash(xstrdup(def)); 585} 586 587if(state->root.len) { 588char*ret =xstrfmt("%s%.*s", state->root.buf, len, start); 589returnsquash_slash(ret); 590} 591 592returnsquash_slash(xmemdupz(start, len)); 593} 594 595static char*find_name(struct apply_state *state, 596const char*line, 597char*def, 598int p_value, 599int terminate) 600{ 601if(*line =='"') { 602char*name =find_name_gnu(state, line, def, p_value); 603if(name) 604return name; 605} 606 607returnfind_name_common(state, line, def, p_value, NULL, terminate); 608} 609 610static char*find_name_traditional(struct apply_state *state, 611const char*line, 612char*def, 613int p_value) 614{ 615size_t len; 616size_t date_len; 617 618if(*line =='"') { 619char*name =find_name_gnu(state, line, def, p_value); 620if(name) 621return name; 622} 623 624 len =strchrnul(line,'\n') - line; 625 date_len =diff_timestamp_len(line, len); 626if(!date_len) 627returnfind_name_common(state, line, def, p_value, NULL, TERM_TAB); 628 len -= date_len; 629 630returnfind_name_common(state, line, def, p_value, line + len,0); 631} 632 633static intcount_slashes(const char*cp) 634{ 635int cnt =0; 636char ch; 637 638while((ch = *cp++)) 639if(ch =='/') 640 cnt++; 641return cnt; 642} 643 644/* 645 * Given the string after "--- " or "+++ ", guess the appropriate 646 * p_value for the given patch. 647 */ 648static intguess_p_value(struct apply_state *state,const char*nameline) 649{ 650char*name, *cp; 651int val = -1; 652 653if(is_dev_null(nameline)) 654return-1; 655 name =find_name_traditional(state, nameline, NULL,0); 656if(!name) 657return-1; 658 cp =strchr(name,'/'); 659if(!cp) 660 val =0; 661else if(state->prefix) { 662/* 663 * Does it begin with "a/$our-prefix" and such? Then this is 664 * very likely to apply to our directory. 665 */ 666if(!strncmp(name, state->prefix, state->prefix_length)) 667 val =count_slashes(state->prefix); 668else{ 669 cp++; 670if(!strncmp(cp, state->prefix, state->prefix_length)) 671 val =count_slashes(state->prefix) +1; 672} 673} 674free(name); 675return val; 676} 677 678/* 679 * Does the ---/+++ line have the POSIX timestamp after the last HT? 680 * GNU diff puts epoch there to signal a creation/deletion event. Is 681 * this such a timestamp? 682 */ 683static inthas_epoch_timestamp(const char*nameline) 684{ 685/* 686 * We are only interested in epoch timestamp; any non-zero 687 * fraction cannot be one, hence "(\.0+)?" in the regexp below. 688 * For the same reason, the date must be either 1969-12-31 or 689 * 1970-01-01, and the seconds part must be "00". 690 */ 691const char stamp_regexp[] = 692"^(1969-12-31|1970-01-01)" 693" " 694"[0-2][0-9]:[0-5][0-9]:00(\\.0+)?" 695" " 696"([-+][0-2][0-9]:?[0-5][0-9])\n"; 697const char*timestamp = NULL, *cp, *colon; 698static regex_t *stamp; 699 regmatch_t m[10]; 700int zoneoffset; 701int hourminute; 702int status; 703 704for(cp = nameline; *cp !='\n'; cp++) { 705if(*cp =='\t') 706 timestamp = cp +1; 707} 708if(!timestamp) 709return0; 710if(!stamp) { 711 stamp =xmalloc(sizeof(*stamp)); 712if(regcomp(stamp, stamp_regexp, REG_EXTENDED)) { 713warning(_("Cannot prepare timestamp regexp%s"), 714 stamp_regexp); 715return0; 716} 717} 718 719 status =regexec(stamp, timestamp,ARRAY_SIZE(m), m,0); 720if(status) { 721if(status != REG_NOMATCH) 722warning(_("regexec returned%dfor input:%s"), 723 status, timestamp); 724return0; 725} 726 727 zoneoffset =strtol(timestamp + m[3].rm_so +1, (char**) &colon,10); 728if(*colon ==':') 729 zoneoffset = zoneoffset *60+strtol(colon +1, NULL,10); 730else 731 zoneoffset = (zoneoffset /100) *60+ (zoneoffset %100); 732if(timestamp[m[3].rm_so] =='-') 733 zoneoffset = -zoneoffset; 734 735/* 736 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31 737 * (west of GMT) or 1970-01-01 (east of GMT) 738 */ 739if((zoneoffset <0&&memcmp(timestamp,"1969-12-31",10)) || 740(0<= zoneoffset &&memcmp(timestamp,"1970-01-01",10))) 741return0; 742 743 hourminute = (strtol(timestamp +11, NULL,10) *60+ 744strtol(timestamp +14, NULL,10) - 745 zoneoffset); 746 747return((zoneoffset <0&& hourminute ==1440) || 748(0<= zoneoffset && !hourminute)); 749} 750 751/* 752 * Get the name etc info from the ---/+++ lines of a traditional patch header 753 * 754 * FIXME! The end-of-filename heuristics are kind of screwy. For existing 755 * files, we can happily check the index for a match, but for creating a 756 * new file we should try to match whatever "patch" does. I have no idea. 757 */ 758static intparse_traditional_patch(struct apply_state *state, 759const char*first, 760const char*second, 761struct patch *patch) 762{ 763char*name; 764 765 first +=4;/* skip "--- " */ 766 second +=4;/* skip "+++ " */ 767if(!state->p_value_known) { 768int p, q; 769 p =guess_p_value(state, first); 770 q =guess_p_value(state, second); 771if(p <0) p = q; 772if(0<= p && p == q) { 773 state->p_value = p; 774 state->p_value_known =1; 775} 776} 777if(is_dev_null(first)) { 778 patch->is_new =1; 779 patch->is_delete =0; 780 name =find_name_traditional(state, second, NULL, state->p_value); 781 patch->new_name = name; 782}else if(is_dev_null(second)) { 783 patch->is_new =0; 784 patch->is_delete =1; 785 name =find_name_traditional(state, first, NULL, state->p_value); 786 patch->old_name = name; 787}else{ 788char*first_name; 789 first_name =find_name_traditional(state, first, NULL, state->p_value); 790 name =find_name_traditional(state, second, first_name, state->p_value); 791free(first_name); 792if(has_epoch_timestamp(first)) { 793 patch->is_new =1; 794 patch->is_delete =0; 795 patch->new_name = name; 796}else if(has_epoch_timestamp(second)) { 797 patch->is_new =0; 798 patch->is_delete =1; 799 patch->old_name = name; 800}else{ 801 patch->old_name = name; 802 patch->new_name =xstrdup_or_null(name); 803} 804} 805if(!name) 806returnerror(_("unable to find filename in patch at line%d"), state->linenr); 807 808return0; 809} 810 811static intgitdiff_hdrend(struct apply_state *state, 812const char*line, 813struct patch *patch) 814{ 815return1; 816} 817 818/* 819 * We're anal about diff header consistency, to make 820 * sure that we don't end up having strange ambiguous 821 * patches floating around. 822 * 823 * As a result, gitdiff_{old|new}name() will check 824 * their names against any previous information, just 825 * to make sure.. 826 */ 827#define DIFF_OLD_NAME 0 828#define DIFF_NEW_NAME 1 829 830static voidgitdiff_verify_name(struct apply_state *state, 831const char*line, 832int isnull, 833char**name, 834int side) 835{ 836if(!*name && !isnull) { 837*name =find_name(state, line, NULL, state->p_value, TERM_TAB); 838return; 839} 840 841if(*name) { 842int len =strlen(*name); 843char*another; 844if(isnull) 845die(_("git apply: bad git-diff - expected /dev/null, got%son line%d"), 846*name, state->linenr); 847 another =find_name(state, line, NULL, state->p_value, TERM_TAB); 848if(!another ||memcmp(another, *name, len +1)) 849die((side == DIFF_NEW_NAME) ? 850_("git apply: bad git-diff - inconsistent new filename on line%d") : 851_("git apply: bad git-diff - inconsistent old filename on line%d"), state->linenr); 852free(another); 853}else{ 854/* expect "/dev/null" */ 855if(memcmp("/dev/null", line,9) || line[9] !='\n') 856die(_("git apply: bad git-diff - expected /dev/null on line%d"), state->linenr); 857} 858} 859 860static intgitdiff_oldname(struct apply_state *state, 861const char*line, 862struct patch *patch) 863{ 864gitdiff_verify_name(state, line, 865 patch->is_new, &patch->old_name, 866 DIFF_OLD_NAME); 867return0; 868} 869 870static intgitdiff_newname(struct apply_state *state, 871const char*line, 872struct patch *patch) 873{ 874gitdiff_verify_name(state, line, 875 patch->is_delete, &patch->new_name, 876 DIFF_NEW_NAME); 877return0; 878} 879 880static intgitdiff_oldmode(struct apply_state *state, 881const char*line, 882struct patch *patch) 883{ 884 patch->old_mode =strtoul(line, NULL,8); 885return0; 886} 887 888static intgitdiff_newmode(struct apply_state *state, 889const char*line, 890struct patch *patch) 891{ 892 patch->new_mode =strtoul(line, NULL,8); 893return0; 894} 895 896static intgitdiff_delete(struct apply_state *state, 897const char*line, 898struct patch *patch) 899{ 900 patch->is_delete =1; 901free(patch->old_name); 902 patch->old_name =xstrdup_or_null(patch->def_name); 903returngitdiff_oldmode(state, line, patch); 904} 905 906static intgitdiff_newfile(struct apply_state *state, 907const char*line, 908struct patch *patch) 909{ 910 patch->is_new =1; 911free(patch->new_name); 912 patch->new_name =xstrdup_or_null(patch->def_name); 913returngitdiff_newmode(state, line, patch); 914} 915 916static intgitdiff_copysrc(struct apply_state *state, 917const char*line, 918struct patch *patch) 919{ 920 patch->is_copy =1; 921free(patch->old_name); 922 patch->old_name =find_name(state, line, NULL, state->p_value ? state->p_value -1:0,0); 923return0; 924} 925 926static intgitdiff_copydst(struct apply_state *state, 927const char*line, 928struct patch *patch) 929{ 930 patch->is_copy =1; 931free(patch->new_name); 932 patch->new_name =find_name(state, line, NULL, state->p_value ? state->p_value -1:0,0); 933return0; 934} 935 936static intgitdiff_renamesrc(struct apply_state *state, 937const char*line, 938struct patch *patch) 939{ 940 patch->is_rename =1; 941free(patch->old_name); 942 patch->old_name =find_name(state, line, NULL, state->p_value ? state->p_value -1:0,0); 943return0; 944} 945 946static intgitdiff_renamedst(struct apply_state *state, 947const char*line, 948struct patch *patch) 949{ 950 patch->is_rename =1; 951free(patch->new_name); 952 patch->new_name =find_name(state, line, NULL, state->p_value ? state->p_value -1:0,0); 953return0; 954} 955 956static intgitdiff_similarity(struct apply_state *state, 957const char*line, 958struct patch *patch) 959{ 960unsigned long val =strtoul(line, NULL,10); 961if(val <=100) 962 patch->score = val; 963return0; 964} 965 966static intgitdiff_dissimilarity(struct apply_state *state, 967const char*line, 968struct patch *patch) 969{ 970unsigned long val =strtoul(line, NULL,10); 971if(val <=100) 972 patch->score = val; 973return0; 974} 975 976static intgitdiff_index(struct apply_state *state, 977const char*line, 978struct patch *patch) 979{ 980/* 981 * index line is N hexadecimal, "..", N hexadecimal, 982 * and optional space with octal mode. 983 */ 984const char*ptr, *eol; 985int len; 986 987 ptr =strchr(line,'.'); 988if(!ptr || ptr[1] !='.'||40< ptr - line) 989return0; 990 len = ptr - line; 991memcpy(patch->old_sha1_prefix, line, len); 992 patch->old_sha1_prefix[len] =0; 993 994 line = ptr +2; 995 ptr =strchr(line,' '); 996 eol =strchrnul(line,'\n'); 997 998if(!ptr || eol < ptr) 999 ptr = eol;1000 len = ptr - line;10011002if(40< len)1003return0;1004memcpy(patch->new_sha1_prefix, line, len);1005 patch->new_sha1_prefix[len] =0;1006if(*ptr ==' ')1007 patch->old_mode =strtoul(ptr+1, NULL,8);1008return0;1009}10101011/*1012 * This is normal for a diff that doesn't change anything: we'll fall through1013 * into the next diff. Tell the parser to break out.1014 */1015static intgitdiff_unrecognized(struct apply_state *state,1016const char*line,1017struct patch *patch)1018{1019return1;1020}10211022/*1023 * Skip p_value leading components from "line"; as we do not accept1024 * absolute paths, return NULL in that case.1025 */1026static const char*skip_tree_prefix(struct apply_state *state,1027const char*line,1028int llen)1029{1030int nslash;1031int i;10321033if(!state->p_value)1034return(llen && line[0] =='/') ? NULL : line;10351036 nslash = state->p_value;1037for(i =0; i < llen; i++) {1038int ch = line[i];1039if(ch =='/'&& --nslash <=0)1040return(i ==0) ? NULL : &line[i +1];1041}1042return NULL;1043}10441045/*1046 * This is to extract the same name that appears on "diff --git"1047 * line. We do not find and return anything if it is a rename1048 * patch, and it is OK because we will find the name elsewhere.1049 * We need to reliably find name only when it is mode-change only,1050 * creation or deletion of an empty file. In any of these cases,1051 * both sides are the same name under a/ and b/ respectively.1052 */1053static char*git_header_name(struct apply_state *state,1054const char*line,1055int llen)1056{1057const char*name;1058const char*second = NULL;1059size_t len, line_len;10601061 line +=strlen("diff --git ");1062 llen -=strlen("diff --git ");10631064if(*line =='"') {1065const char*cp;1066struct strbuf first = STRBUF_INIT;1067struct strbuf sp = STRBUF_INIT;10681069if(unquote_c_style(&first, line, &second))1070goto free_and_fail1;10711072/* strip the a/b prefix including trailing slash */1073 cp =skip_tree_prefix(state, first.buf, first.len);1074if(!cp)1075goto free_and_fail1;1076strbuf_remove(&first,0, cp - first.buf);10771078/*1079 * second points at one past closing dq of name.1080 * find the second name.1081 */1082while((second < line + llen) &&isspace(*second))1083 second++;10841085if(line + llen <= second)1086goto free_and_fail1;1087if(*second =='"') {1088if(unquote_c_style(&sp, second, NULL))1089goto free_and_fail1;1090 cp =skip_tree_prefix(state, sp.buf, sp.len);1091if(!cp)1092goto free_and_fail1;1093/* They must match, otherwise ignore */1094if(strcmp(cp, first.buf))1095goto free_and_fail1;1096strbuf_release(&sp);1097returnstrbuf_detach(&first, NULL);1098}10991100/* unquoted second */1101 cp =skip_tree_prefix(state, second, line + llen - second);1102if(!cp)1103goto free_and_fail1;1104if(line + llen - cp != first.len ||1105memcmp(first.buf, cp, first.len))1106goto free_and_fail1;1107returnstrbuf_detach(&first, NULL);11081109 free_and_fail1:1110strbuf_release(&first);1111strbuf_release(&sp);1112return NULL;1113}11141115/* unquoted first name */1116 name =skip_tree_prefix(state, line, llen);1117if(!name)1118return NULL;11191120/*1121 * since the first name is unquoted, a dq if exists must be1122 * the beginning of the second name.1123 */1124for(second = name; second < line + llen; second++) {1125if(*second =='"') {1126struct strbuf sp = STRBUF_INIT;1127const char*np;11281129if(unquote_c_style(&sp, second, NULL))1130goto free_and_fail2;11311132 np =skip_tree_prefix(state, sp.buf, sp.len);1133if(!np)1134goto free_and_fail2;11351136 len = sp.buf + sp.len - np;1137if(len < second - name &&1138!strncmp(np, name, len) &&1139isspace(name[len])) {1140/* Good */1141strbuf_remove(&sp,0, np - sp.buf);1142returnstrbuf_detach(&sp, NULL);1143}11441145 free_and_fail2:1146strbuf_release(&sp);1147return NULL;1148}1149}11501151/*1152 * Accept a name only if it shows up twice, exactly the same1153 * form.1154 */1155 second =strchr(name,'\n');1156if(!second)1157return NULL;1158 line_len = second - name;1159for(len =0; ; len++) {1160switch(name[len]) {1161default:1162continue;1163case'\n':1164return NULL;1165case'\t':case' ':1166/*1167 * Is this the separator between the preimage1168 * and the postimage pathname? Again, we are1169 * only interested in the case where there is1170 * no rename, as this is only to set def_name1171 * and a rename patch has the names elsewhere1172 * in an unambiguous form.1173 */1174if(!name[len +1])1175return NULL;/* no postimage name */1176 second =skip_tree_prefix(state, name + len +1,1177 line_len - (len +1));1178if(!second)1179return NULL;1180/*1181 * Does len bytes starting at "name" and "second"1182 * (that are separated by one HT or SP we just1183 * found) exactly match?1184 */1185if(second[len] =='\n'&& !strncmp(name, second, len))1186returnxmemdupz(name, len);1187}1188}1189}11901191/* Verify that we recognize the lines following a git header */1192static intparse_git_header(struct apply_state *state,1193const char*line,1194int len,1195unsigned int size,1196struct patch *patch)1197{1198unsigned long offset;11991200/* A git diff has explicit new/delete information, so we don't guess */1201 patch->is_new =0;1202 patch->is_delete =0;12031204/*1205 * Some things may not have the old name in the1206 * rest of the headers anywhere (pure mode changes,1207 * or removing or adding empty files), so we get1208 * the default name from the header.1209 */1210 patch->def_name =git_header_name(state, line, len);1211if(patch->def_name && state->root.len) {1212char*s =xstrfmt("%s%s", state->root.buf, patch->def_name);1213free(patch->def_name);1214 patch->def_name = s;1215}12161217 line += len;1218 size -= len;1219 state->linenr++;1220for(offset = len ; size >0; offset += len, size -= len, line += len, state->linenr++) {1221static const struct opentry {1222const char*str;1223int(*fn)(struct apply_state *,const char*,struct patch *);1224} optable[] = {1225{"@@ -", gitdiff_hdrend },1226{"--- ", gitdiff_oldname },1227{"+++ ", gitdiff_newname },1228{"old mode ", gitdiff_oldmode },1229{"new mode ", gitdiff_newmode },1230{"deleted file mode ", gitdiff_delete },1231{"new file mode ", gitdiff_newfile },1232{"copy from ", gitdiff_copysrc },1233{"copy to ", gitdiff_copydst },1234{"rename old ", gitdiff_renamesrc },1235{"rename new ", gitdiff_renamedst },1236{"rename from ", gitdiff_renamesrc },1237{"rename to ", gitdiff_renamedst },1238{"similarity index ", gitdiff_similarity },1239{"dissimilarity index ", gitdiff_dissimilarity },1240{"index ", gitdiff_index },1241{"", gitdiff_unrecognized },1242};1243int i;12441245 len =linelen(line, size);1246if(!len || line[len-1] !='\n')1247break;1248for(i =0; i <ARRAY_SIZE(optable); i++) {1249const struct opentry *p = optable + i;1250int oplen =strlen(p->str);1251int res;1252if(len < oplen ||memcmp(p->str, line, oplen))1253continue;1254 res = p->fn(state, line + oplen, patch);1255if(res <0)1256return-1;1257if(res >0)1258return offset;1259break;1260}1261}12621263return offset;1264}12651266static intparse_num(const char*line,unsigned long*p)1267{1268char*ptr;12691270if(!isdigit(*line))1271return0;1272*p =strtoul(line, &ptr,10);1273return ptr - line;1274}12751276static intparse_range(const char*line,int len,int offset,const char*expect,1277unsigned long*p1,unsigned long*p2)1278{1279int digits, ex;12801281if(offset <0|| offset >= len)1282return-1;1283 line += offset;1284 len -= offset;12851286 digits =parse_num(line, p1);1287if(!digits)1288return-1;12891290 offset += digits;1291 line += digits;1292 len -= digits;12931294*p2 =1;1295if(*line ==',') {1296 digits =parse_num(line+1, p2);1297if(!digits)1298return-1;12991300 offset += digits+1;1301 line += digits+1;1302 len -= digits+1;1303}13041305 ex =strlen(expect);1306if(ex > len)1307return-1;1308if(memcmp(line, expect, ex))1309return-1;13101311return offset + ex;1312}13131314static voidrecount_diff(const char*line,int size,struct fragment *fragment)1315{1316int oldlines =0, newlines =0, ret =0;13171318if(size <1) {1319warning("recount: ignore empty hunk");1320return;1321}13221323for(;;) {1324int len =linelen(line, size);1325 size -= len;1326 line += len;13271328if(size <1)1329break;13301331switch(*line) {1332case' ':case'\n':1333 newlines++;1334/* fall through */1335case'-':1336 oldlines++;1337continue;1338case'+':1339 newlines++;1340continue;1341case'\\':1342continue;1343case'@':1344 ret = size <3|| !starts_with(line,"@@ ");1345break;1346case'd':1347 ret = size <5|| !starts_with(line,"diff ");1348break;1349default:1350 ret = -1;1351break;1352}1353if(ret) {1354warning(_("recount: unexpected line: %.*s"),1355(int)linelen(line, size), line);1356return;1357}1358break;1359}1360 fragment->oldlines = oldlines;1361 fragment->newlines = newlines;1362}13631364/*1365 * Parse a unified diff fragment header of the1366 * form "@@ -a,b +c,d @@"1367 */1368static intparse_fragment_header(const char*line,int len,struct fragment *fragment)1369{1370int offset;13711372if(!len || line[len-1] !='\n')1373return-1;13741375/* Figure out the number of lines in a fragment */1376 offset =parse_range(line, len,4," +", &fragment->oldpos, &fragment->oldlines);1377 offset =parse_range(line, len, offset," @@", &fragment->newpos, &fragment->newlines);13781379return offset;1380}13811382/*1383 * Find file diff header1384 *1385 * Returns:1386 * -1 if no header was found1387 * -128 in case of error1388 * the size of the header in bytes (called "offset") otherwise1389 */1390static intfind_header(struct apply_state *state,1391const char*line,1392unsigned long size,1393int*hdrsize,1394struct patch *patch)1395{1396unsigned long offset, len;13971398 patch->is_toplevel_relative =0;1399 patch->is_rename = patch->is_copy =0;1400 patch->is_new = patch->is_delete = -1;1401 patch->old_mode = patch->new_mode =0;1402 patch->old_name = patch->new_name = NULL;1403for(offset =0; size >0; offset += len, size -= len, line += len, state->linenr++) {1404unsigned long nextlen;14051406 len =linelen(line, size);1407if(!len)1408break;14091410/* Testing this early allows us to take a few shortcuts.. */1411if(len <6)1412continue;14131414/*1415 * Make sure we don't find any unconnected patch fragments.1416 * That's a sign that we didn't find a header, and that a1417 * patch has become corrupted/broken up.1418 */1419if(!memcmp("@@ -", line,4)) {1420struct fragment dummy;1421if(parse_fragment_header(line, len, &dummy) <0)1422continue;1423error(_("patch fragment without header at line%d: %.*s"),1424 state->linenr, (int)len-1, line);1425return-128;1426}14271428if(size < len +6)1429break;14301431/*1432 * Git patch? It might not have a real patch, just a rename1433 * or mode change, so we handle that specially1434 */1435if(!memcmp("diff --git ", line,11)) {1436int git_hdr_len =parse_git_header(state, line, len, size, patch);1437if(git_hdr_len <0)1438return-128;1439if(git_hdr_len <= len)1440continue;1441if(!patch->old_name && !patch->new_name) {1442if(!patch->def_name) {1443error(Q_("git diff header lacks filename information when removing "1444"%dleading pathname component (line%d)",1445"git diff header lacks filename information when removing "1446"%dleading pathname components (line%d)",1447 state->p_value),1448 state->p_value, state->linenr);1449return-128;1450}1451 patch->old_name =xstrdup(patch->def_name);1452 patch->new_name =xstrdup(patch->def_name);1453}1454if(!patch->is_delete && !patch->new_name) {1455error("git diff header lacks filename information "1456"(line%d)", state->linenr);1457return-128;1458}1459 patch->is_toplevel_relative =1;1460*hdrsize = git_hdr_len;1461return offset;1462}14631464/* --- followed by +++ ? */1465if(memcmp("--- ", line,4) ||memcmp("+++ ", line + len,4))1466continue;14671468/*1469 * We only accept unified patches, so we want it to1470 * at least have "@@ -a,b +c,d @@\n", which is 14 chars1471 * minimum ("@@ -0,0 +1 @@\n" is the shortest).1472 */1473 nextlen =linelen(line + len, size - len);1474if(size < nextlen +14||memcmp("@@ -", line + len + nextlen,4))1475continue;14761477/* Ok, we'll consider it a patch */1478if(parse_traditional_patch(state, line, line+len, patch))1479return-128;1480*hdrsize = len + nextlen;1481 state->linenr +=2;1482return offset;1483}1484return-1;1485}14861487static voidrecord_ws_error(struct apply_state *state,1488unsigned result,1489const char*line,1490int len,1491int linenr)1492{1493char*err;14941495if(!result)1496return;14971498 state->whitespace_error++;1499if(state->squelch_whitespace_errors &&1500 state->squelch_whitespace_errors < state->whitespace_error)1501return;15021503 err =whitespace_error_string(result);1504fprintf(stderr,"%s:%d:%s.\n%.*s\n",1505 state->patch_input_file, linenr, err, len, line);1506free(err);1507}15081509static voidcheck_whitespace(struct apply_state *state,1510const char*line,1511int len,1512unsigned ws_rule)1513{1514unsigned result =ws_check(line +1, len -1, ws_rule);15151516record_ws_error(state, result, line +1, len -2, state->linenr);1517}15181519/*1520 * Parse a unified diff. Note that this really needs to parse each1521 * fragment separately, since the only way to know the difference1522 * between a "---" that is part of a patch, and a "---" that starts1523 * the next patch is to look at the line counts..1524 */1525static intparse_fragment(struct apply_state *state,1526const char*line,1527unsigned long size,1528struct patch *patch,1529struct fragment *fragment)1530{1531int added, deleted;1532int len =linelen(line, size), offset;1533unsigned long oldlines, newlines;1534unsigned long leading, trailing;15351536 offset =parse_fragment_header(line, len, fragment);1537if(offset <0)1538return-1;1539if(offset >0&& patch->recount)1540recount_diff(line + offset, size - offset, fragment);1541 oldlines = fragment->oldlines;1542 newlines = fragment->newlines;1543 leading =0;1544 trailing =0;15451546/* Parse the thing.. */1547 line += len;1548 size -= len;1549 state->linenr++;1550 added = deleted =0;1551for(offset = len;15520< size;1553 offset += len, size -= len, line += len, state->linenr++) {1554if(!oldlines && !newlines)1555break;1556 len =linelen(line, size);1557if(!len || line[len-1] !='\n')1558return-1;1559switch(*line) {1560default:1561return-1;1562case'\n':/* newer GNU diff, an empty context line */1563case' ':1564 oldlines--;1565 newlines--;1566if(!deleted && !added)1567 leading++;1568 trailing++;1569if(!state->apply_in_reverse &&1570 state->ws_error_action == correct_ws_error)1571check_whitespace(state, line, len, patch->ws_rule);1572break;1573case'-':1574if(state->apply_in_reverse &&1575 state->ws_error_action != nowarn_ws_error)1576check_whitespace(state, line, len, patch->ws_rule);1577 deleted++;1578 oldlines--;1579 trailing =0;1580break;1581case'+':1582if(!state->apply_in_reverse &&1583 state->ws_error_action != nowarn_ws_error)1584check_whitespace(state, line, len, patch->ws_rule);1585 added++;1586 newlines--;1587 trailing =0;1588break;15891590/*1591 * We allow "\ No newline at end of file". Depending1592 * on locale settings when the patch was produced we1593 * don't know what this line looks like. The only1594 * thing we do know is that it begins with "\ ".1595 * Checking for 12 is just for sanity check -- any1596 * l10n of "\ No newline..." is at least that long.1597 */1598case'\\':1599if(len <12||memcmp(line,"\\",2))1600return-1;1601break;1602}1603}1604if(oldlines || newlines)1605return-1;1606if(!deleted && !added)1607return-1;16081609 fragment->leading = leading;1610 fragment->trailing = trailing;16111612/*1613 * If a fragment ends with an incomplete line, we failed to include1614 * it in the above loop because we hit oldlines == newlines == 01615 * before seeing it.1616 */1617if(12< size && !memcmp(line,"\\",2))1618 offset +=linelen(line, size);16191620 patch->lines_added += added;1621 patch->lines_deleted += deleted;16221623if(0< patch->is_new && oldlines)1624returnerror(_("new file depends on old contents"));1625if(0< patch->is_delete && newlines)1626returnerror(_("deleted file still has contents"));1627return offset;1628}16291630/*1631 * We have seen "diff --git a/... b/..." header (or a traditional patch1632 * header). Read hunks that belong to this patch into fragments and hang1633 * them to the given patch structure.1634 *1635 * The (fragment->patch, fragment->size) pair points into the memory given1636 * by the caller, not a copy, when we return.1637 *1638 * Returns:1639 * -1 in case of error,1640 * the number of bytes in the patch otherwise.1641 */1642static intparse_single_patch(struct apply_state *state,1643const char*line,1644unsigned long size,1645struct patch *patch)1646{1647unsigned long offset =0;1648unsigned long oldlines =0, newlines =0, context =0;1649struct fragment **fragp = &patch->fragments;16501651while(size >4&& !memcmp(line,"@@ -",4)) {1652struct fragment *fragment;1653int len;16541655 fragment =xcalloc(1,sizeof(*fragment));1656 fragment->linenr = state->linenr;1657 len =parse_fragment(state, line, size, patch, fragment);1658if(len <=0) {1659free(fragment);1660returnerror(_("corrupt patch at line%d"), state->linenr);1661}1662 fragment->patch = line;1663 fragment->size = len;1664 oldlines += fragment->oldlines;1665 newlines += fragment->newlines;1666 context += fragment->leading + fragment->trailing;16671668*fragp = fragment;1669 fragp = &fragment->next;16701671 offset += len;1672 line += len;1673 size -= len;1674}16751676/*1677 * If something was removed (i.e. we have old-lines) it cannot1678 * be creation, and if something was added it cannot be1679 * deletion. However, the reverse is not true; --unified=01680 * patches that only add are not necessarily creation even1681 * though they do not have any old lines, and ones that only1682 * delete are not necessarily deletion.1683 *1684 * Unfortunately, a real creation/deletion patch do _not_ have1685 * any context line by definition, so we cannot safely tell it1686 * apart with --unified=0 insanity. At least if the patch has1687 * more than one hunk it is not creation or deletion.1688 */1689if(patch->is_new <0&&1690(oldlines || (patch->fragments && patch->fragments->next)))1691 patch->is_new =0;1692if(patch->is_delete <0&&1693(newlines || (patch->fragments && patch->fragments->next)))1694 patch->is_delete =0;16951696if(0< patch->is_new && oldlines)1697returnerror(_("new file%sdepends on old contents"), patch->new_name);1698if(0< patch->is_delete && newlines)1699returnerror(_("deleted file%sstill has contents"), patch->old_name);1700if(!patch->is_delete && !newlines && context)1701fprintf_ln(stderr,1702_("** warning: "1703"file%sbecomes empty but is not deleted"),1704 patch->new_name);17051706return offset;1707}17081709staticinlineintmetadata_changes(struct patch *patch)1710{1711return patch->is_rename >0||1712 patch->is_copy >0||1713 patch->is_new >0||1714 patch->is_delete ||1715(patch->old_mode && patch->new_mode &&1716 patch->old_mode != patch->new_mode);1717}17181719static char*inflate_it(const void*data,unsigned long size,1720unsigned long inflated_size)1721{1722 git_zstream stream;1723void*out;1724int st;17251726memset(&stream,0,sizeof(stream));17271728 stream.next_in = (unsigned char*)data;1729 stream.avail_in = size;1730 stream.next_out = out =xmalloc(inflated_size);1731 stream.avail_out = inflated_size;1732git_inflate_init(&stream);1733 st =git_inflate(&stream, Z_FINISH);1734git_inflate_end(&stream);1735if((st != Z_STREAM_END) || stream.total_out != inflated_size) {1736free(out);1737return NULL;1738}1739return out;1740}17411742/*1743 * Read a binary hunk and return a new fragment; fragment->patch1744 * points at an allocated memory that the caller must free, so1745 * it is marked as "->free_patch = 1".1746 */1747static struct fragment *parse_binary_hunk(struct apply_state *state,1748char**buf_p,1749unsigned long*sz_p,1750int*status_p,1751int*used_p)1752{1753/*1754 * Expect a line that begins with binary patch method ("literal"1755 * or "delta"), followed by the length of data before deflating.1756 * a sequence of 'length-byte' followed by base-85 encoded data1757 * should follow, terminated by a newline.1758 *1759 * Each 5-byte sequence of base-85 encodes up to 4 bytes,1760 * and we would limit the patch line to 66 characters,1761 * so one line can fit up to 13 groups that would decode1762 * to 52 bytes max. The length byte 'A'-'Z' corresponds1763 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.1764 */1765int llen, used;1766unsigned long size = *sz_p;1767char*buffer = *buf_p;1768int patch_method;1769unsigned long origlen;1770char*data = NULL;1771int hunk_size =0;1772struct fragment *frag;17731774 llen =linelen(buffer, size);1775 used = llen;17761777*status_p =0;17781779if(starts_with(buffer,"delta ")) {1780 patch_method = BINARY_DELTA_DEFLATED;1781 origlen =strtoul(buffer +6, NULL,10);1782}1783else if(starts_with(buffer,"literal ")) {1784 patch_method = BINARY_LITERAL_DEFLATED;1785 origlen =strtoul(buffer +8, NULL,10);1786}1787else1788return NULL;17891790 state->linenr++;1791 buffer += llen;1792while(1) {1793int byte_length, max_byte_length, newsize;1794 llen =linelen(buffer, size);1795 used += llen;1796 state->linenr++;1797if(llen ==1) {1798/* consume the blank line */1799 buffer++;1800 size--;1801break;1802}1803/*1804 * Minimum line is "A00000\n" which is 7-byte long,1805 * and the line length must be multiple of 5 plus 2.1806 */1807if((llen <7) || (llen-2) %5)1808goto corrupt;1809 max_byte_length = (llen -2) /5*4;1810 byte_length = *buffer;1811if('A'<= byte_length && byte_length <='Z')1812 byte_length = byte_length -'A'+1;1813else if('a'<= byte_length && byte_length <='z')1814 byte_length = byte_length -'a'+27;1815else1816goto corrupt;1817/* if the input length was not multiple of 4, we would1818 * have filler at the end but the filler should never1819 * exceed 3 bytes1820 */1821if(max_byte_length < byte_length ||1822 byte_length <= max_byte_length -4)1823goto corrupt;1824 newsize = hunk_size + byte_length;1825 data =xrealloc(data, newsize);1826if(decode_85(data + hunk_size, buffer +1, byte_length))1827goto corrupt;1828 hunk_size = newsize;1829 buffer += llen;1830 size -= llen;1831}18321833 frag =xcalloc(1,sizeof(*frag));1834 frag->patch =inflate_it(data, hunk_size, origlen);1835 frag->free_patch =1;1836if(!frag->patch)1837goto corrupt;1838free(data);1839 frag->size = origlen;1840*buf_p = buffer;1841*sz_p = size;1842*used_p = used;1843 frag->binary_patch_method = patch_method;1844return frag;18451846 corrupt:1847free(data);1848*status_p = -1;1849error(_("corrupt binary patch at line%d: %.*s"),1850 state->linenr-1, llen-1, buffer);1851return NULL;1852}18531854/*1855 * Returns:1856 * -1 in case of error,1857 * the length of the parsed binary patch otherwise1858 */1859static intparse_binary(struct apply_state *state,1860char*buffer,1861unsigned long size,1862struct patch *patch)1863{1864/*1865 * We have read "GIT binary patch\n"; what follows is a line1866 * that says the patch method (currently, either "literal" or1867 * "delta") and the length of data before deflating; a1868 * sequence of 'length-byte' followed by base-85 encoded data1869 * follows.1870 *1871 * When a binary patch is reversible, there is another binary1872 * hunk in the same format, starting with patch method (either1873 * "literal" or "delta") with the length of data, and a sequence1874 * of length-byte + base-85 encoded data, terminated with another1875 * empty line. This data, when applied to the postimage, produces1876 * the preimage.1877 */1878struct fragment *forward;1879struct fragment *reverse;1880int status;1881int used, used_1;18821883 forward =parse_binary_hunk(state, &buffer, &size, &status, &used);1884if(!forward && !status)1885/* there has to be one hunk (forward hunk) */1886returnerror(_("unrecognized binary patch at line%d"), state->linenr-1);1887if(status)1888/* otherwise we already gave an error message */1889return status;18901891 reverse =parse_binary_hunk(state, &buffer, &size, &status, &used_1);1892if(reverse)1893 used += used_1;1894else if(status) {1895/*1896 * Not having reverse hunk is not an error, but having1897 * a corrupt reverse hunk is.1898 */1899free((void*) forward->patch);1900free(forward);1901return status;1902}1903 forward->next = reverse;1904 patch->fragments = forward;1905 patch->is_binary =1;1906return used;1907}19081909static voidprefix_one(struct apply_state *state,char**name)1910{1911char*old_name = *name;1912if(!old_name)1913return;1914*name =xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));1915free(old_name);1916}19171918static voidprefix_patch(struct apply_state *state,struct patch *p)1919{1920if(!state->prefix || p->is_toplevel_relative)1921return;1922prefix_one(state, &p->new_name);1923prefix_one(state, &p->old_name);1924}19251926/*1927 * include/exclude1928 */19291930static voidadd_name_limit(struct apply_state *state,1931const char*name,1932int exclude)1933{1934struct string_list_item *it;19351936 it =string_list_append(&state->limit_by_name, name);1937 it->util = exclude ? NULL : (void*)1;1938}19391940static intuse_patch(struct apply_state *state,struct patch *p)1941{1942const char*pathname = p->new_name ? p->new_name : p->old_name;1943int i;19441945/* Paths outside are not touched regardless of "--include" */1946if(0< state->prefix_length) {1947int pathlen =strlen(pathname);1948if(pathlen <= state->prefix_length ||1949memcmp(state->prefix, pathname, state->prefix_length))1950return0;1951}19521953/* See if it matches any of exclude/include rule */1954for(i =0; i < state->limit_by_name.nr; i++) {1955struct string_list_item *it = &state->limit_by_name.items[i];1956if(!wildmatch(it->string, pathname,0, NULL))1957return(it->util != NULL);1958}19591960/*1961 * If we had any include, a path that does not match any rule is1962 * not used. Otherwise, we saw bunch of exclude rules (or none)1963 * and such a path is used.1964 */1965return!state->has_include;1966}19671968/*1969 * Read the patch text in "buffer" that extends for "size" bytes; stop1970 * reading after seeing a single patch (i.e. changes to a single file).1971 * Create fragments (i.e. patch hunks) and hang them to the given patch.1972 *1973 * Returns:1974 * -1 if no header was found or parse_binary() failed,1975 * -128 on another error,1976 * the number of bytes consumed otherwise,1977 * so that the caller can call us again for the next patch.1978 */1979static intparse_chunk(struct apply_state *state,char*buffer,unsigned long size,struct patch *patch)1980{1981int hdrsize, patchsize;1982int offset =find_header(state, buffer, size, &hdrsize, patch);19831984if(offset <0)1985return offset;19861987prefix_patch(state, patch);19881989if(!use_patch(state, patch))1990 patch->ws_rule =0;1991else1992 patch->ws_rule =whitespace_rule(patch->new_name1993? patch->new_name1994: patch->old_name);19951996 patchsize =parse_single_patch(state,1997 buffer + offset + hdrsize,1998 size - offset - hdrsize,1999 patch);20002001if(patchsize <0)2002return-128;20032004if(!patchsize) {2005static const char git_binary[] ="GIT binary patch\n";2006int hd = hdrsize + offset;2007unsigned long llen =linelen(buffer + hd, size - hd);20082009if(llen ==sizeof(git_binary) -1&&2010!memcmp(git_binary, buffer + hd, llen)) {2011int used;2012 state->linenr++;2013 used =parse_binary(state, buffer + hd + llen,2014 size - hd - llen, patch);2015if(used <0)2016return-1;2017if(used)2018 patchsize = used + llen;2019else2020 patchsize =0;2021}2022else if(!memcmp(" differ\n", buffer + hd + llen -8,8)) {2023static const char*binhdr[] = {2024"Binary files ",2025"Files ",2026 NULL,2027};2028int i;2029for(i =0; binhdr[i]; i++) {2030int len =strlen(binhdr[i]);2031if(len < size - hd &&2032!memcmp(binhdr[i], buffer + hd, len)) {2033 state->linenr++;2034 patch->is_binary =1;2035 patchsize = llen;2036break;2037}2038}2039}20402041/* Empty patch cannot be applied if it is a text patch2042 * without metadata change. A binary patch appears2043 * empty to us here.2044 */2045if((state->apply || state->check) &&2046(!patch->is_binary && !metadata_changes(patch))) {2047error(_("patch with only garbage at line%d"), state->linenr);2048return-128;2049}2050}20512052return offset + hdrsize + patchsize;2053}20542055#define swap(a,b) myswap((a),(b),sizeof(a))20562057#define myswap(a, b, size) do { \2058 unsigned char mytmp[size]; \2059 memcpy(mytmp, &a, size); \2060 memcpy(&a, &b, size); \2061 memcpy(&b, mytmp, size); \2062} while (0)20632064static voidreverse_patches(struct patch *p)2065{2066for(; p; p = p->next) {2067struct fragment *frag = p->fragments;20682069swap(p->new_name, p->old_name);2070swap(p->new_mode, p->old_mode);2071swap(p->is_new, p->is_delete);2072swap(p->lines_added, p->lines_deleted);2073swap(p->old_sha1_prefix, p->new_sha1_prefix);20742075for(; frag; frag = frag->next) {2076swap(frag->newpos, frag->oldpos);2077swap(frag->newlines, frag->oldlines);2078}2079}2080}20812082static const char pluses[] =2083"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";2084static const char minuses[]=2085"----------------------------------------------------------------------";20862087static voidshow_stats(struct apply_state *state,struct patch *patch)2088{2089struct strbuf qname = STRBUF_INIT;2090char*cp = patch->new_name ? patch->new_name : patch->old_name;2091int max, add, del;20922093quote_c_style(cp, &qname, NULL,0);20942095/*2096 * "scale" the filename2097 */2098 max = state->max_len;2099if(max >50)2100 max =50;21012102if(qname.len > max) {2103 cp =strchr(qname.buf + qname.len +3- max,'/');2104if(!cp)2105 cp = qname.buf + qname.len +3- max;2106strbuf_splice(&qname,0, cp - qname.buf,"...",3);2107}21082109if(patch->is_binary) {2110printf(" %-*s | Bin\n", max, qname.buf);2111strbuf_release(&qname);2112return;2113}21142115printf(" %-*s |", max, qname.buf);2116strbuf_release(&qname);21172118/*2119 * scale the add/delete2120 */2121 max = max + state->max_change >70?70- max : state->max_change;2122 add = patch->lines_added;2123 del = patch->lines_deleted;21242125if(state->max_change >0) {2126int total = ((add + del) * max + state->max_change /2) / state->max_change;2127 add = (add * max + state->max_change /2) / state->max_change;2128 del = total - add;2129}2130printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,2131 add, pluses, del, minuses);2132}21332134static intread_old_data(struct stat *st,const char*path,struct strbuf *buf)2135{2136switch(st->st_mode & S_IFMT) {2137case S_IFLNK:2138if(strbuf_readlink(buf, path, st->st_size) <0)2139returnerror(_("unable to read symlink%s"), path);2140return0;2141case S_IFREG:2142if(strbuf_read_file(buf, path, st->st_size) != st->st_size)2143returnerror(_("unable to open or read%s"), path);2144convert_to_git(path, buf->buf, buf->len, buf,0);2145return0;2146default:2147return-1;2148}2149}21502151/*2152 * Update the preimage, and the common lines in postimage,2153 * from buffer buf of length len. If postlen is 0 the postimage2154 * is updated in place, otherwise it's updated on a new buffer2155 * of length postlen2156 */21572158static voidupdate_pre_post_images(struct image *preimage,2159struct image *postimage,2160char*buf,2161size_t len,size_t postlen)2162{2163int i, ctx, reduced;2164char*new, *old, *fixed;2165struct image fixed_preimage;21662167/*2168 * Update the preimage with whitespace fixes. Note that we2169 * are not losing preimage->buf -- apply_one_fragment() will2170 * free "oldlines".2171 */2172prepare_image(&fixed_preimage, buf, len,1);2173assert(postlen2174? fixed_preimage.nr == preimage->nr2175: fixed_preimage.nr <= preimage->nr);2176for(i =0; i < fixed_preimage.nr; i++)2177 fixed_preimage.line[i].flag = preimage->line[i].flag;2178free(preimage->line_allocated);2179*preimage = fixed_preimage;21802181/*2182 * Adjust the common context lines in postimage. This can be2183 * done in-place when we are shrinking it with whitespace2184 * fixing, but needs a new buffer when ignoring whitespace or2185 * expanding leading tabs to spaces.2186 *2187 * We trust the caller to tell us if the update can be done2188 * in place (postlen==0) or not.2189 */2190 old = postimage->buf;2191if(postlen)2192new= postimage->buf =xmalloc(postlen);2193else2194new= old;2195 fixed = preimage->buf;21962197for(i = reduced = ctx =0; i < postimage->nr; i++) {2198size_t l_len = postimage->line[i].len;2199if(!(postimage->line[i].flag & LINE_COMMON)) {2200/* an added line -- no counterparts in preimage */2201memmove(new, old, l_len);2202 old += l_len;2203new+= l_len;2204continue;2205}22062207/* a common context -- skip it in the original postimage */2208 old += l_len;22092210/* and find the corresponding one in the fixed preimage */2211while(ctx < preimage->nr &&2212!(preimage->line[ctx].flag & LINE_COMMON)) {2213 fixed += preimage->line[ctx].len;2214 ctx++;2215}22162217/*2218 * preimage is expected to run out, if the caller2219 * fixed addition of trailing blank lines.2220 */2221if(preimage->nr <= ctx) {2222 reduced++;2223continue;2224}22252226/* and copy it in, while fixing the line length */2227 l_len = preimage->line[ctx].len;2228memcpy(new, fixed, l_len);2229new+= l_len;2230 fixed += l_len;2231 postimage->line[i].len = l_len;2232 ctx++;2233}22342235if(postlen2236? postlen <new- postimage->buf2237: postimage->len <new- postimage->buf)2238die("BUG: caller miscounted postlen: asked%d, orig =%d, used =%d",2239(int)postlen, (int) postimage->len, (int)(new- postimage->buf));22402241/* Fix the length of the whole thing */2242 postimage->len =new- postimage->buf;2243 postimage->nr -= reduced;2244}22452246static intline_by_line_fuzzy_match(struct image *img,2247struct image *preimage,2248struct image *postimage,2249unsigned longtry,2250int try_lno,2251int preimage_limit)2252{2253int i;2254size_t imgoff =0;2255size_t preoff =0;2256size_t postlen = postimage->len;2257size_t extra_chars;2258char*buf;2259char*preimage_eof;2260char*preimage_end;2261struct strbuf fixed;2262char*fixed_buf;2263size_t fixed_len;22642265for(i =0; i < preimage_limit; i++) {2266size_t prelen = preimage->line[i].len;2267size_t imglen = img->line[try_lno+i].len;22682269if(!fuzzy_matchlines(img->buf +try+ imgoff, imglen,2270 preimage->buf + preoff, prelen))2271return0;2272if(preimage->line[i].flag & LINE_COMMON)2273 postlen += imglen - prelen;2274 imgoff += imglen;2275 preoff += prelen;2276}22772278/*2279 * Ok, the preimage matches with whitespace fuzz.2280 *2281 * imgoff now holds the true length of the target that2282 * matches the preimage before the end of the file.2283 *2284 * Count the number of characters in the preimage that fall2285 * beyond the end of the file and make sure that all of them2286 * are whitespace characters. (This can only happen if2287 * we are removing blank lines at the end of the file.)2288 */2289 buf = preimage_eof = preimage->buf + preoff;2290for( ; i < preimage->nr; i++)2291 preoff += preimage->line[i].len;2292 preimage_end = preimage->buf + preoff;2293for( ; buf < preimage_end; buf++)2294if(!isspace(*buf))2295return0;22962297/*2298 * Update the preimage and the common postimage context2299 * lines to use the same whitespace as the target.2300 * If whitespace is missing in the target (i.e.2301 * if the preimage extends beyond the end of the file),2302 * use the whitespace from the preimage.2303 */2304 extra_chars = preimage_end - preimage_eof;2305strbuf_init(&fixed, imgoff + extra_chars);2306strbuf_add(&fixed, img->buf +try, imgoff);2307strbuf_add(&fixed, preimage_eof, extra_chars);2308 fixed_buf =strbuf_detach(&fixed, &fixed_len);2309update_pre_post_images(preimage, postimage,2310 fixed_buf, fixed_len, postlen);2311return1;2312}23132314static intmatch_fragment(struct apply_state *state,2315struct image *img,2316struct image *preimage,2317struct image *postimage,2318unsigned longtry,2319int try_lno,2320unsigned ws_rule,2321int match_beginning,int match_end)2322{2323int i;2324char*fixed_buf, *buf, *orig, *target;2325struct strbuf fixed;2326size_t fixed_len, postlen;2327int preimage_limit;23282329if(preimage->nr + try_lno <= img->nr) {2330/*2331 * The hunk falls within the boundaries of img.2332 */2333 preimage_limit = preimage->nr;2334if(match_end && (preimage->nr + try_lno != img->nr))2335return0;2336}else if(state->ws_error_action == correct_ws_error &&2337(ws_rule & WS_BLANK_AT_EOF)) {2338/*2339 * This hunk extends beyond the end of img, and we are2340 * removing blank lines at the end of the file. This2341 * many lines from the beginning of the preimage must2342 * match with img, and the remainder of the preimage2343 * must be blank.2344 */2345 preimage_limit = img->nr - try_lno;2346}else{2347/*2348 * The hunk extends beyond the end of the img and2349 * we are not removing blanks at the end, so we2350 * should reject the hunk at this position.2351 */2352return0;2353}23542355if(match_beginning && try_lno)2356return0;23572358/* Quick hash check */2359for(i =0; i < preimage_limit; i++)2360if((img->line[try_lno + i].flag & LINE_PATCHED) ||2361(preimage->line[i].hash != img->line[try_lno + i].hash))2362return0;23632364if(preimage_limit == preimage->nr) {2365/*2366 * Do we have an exact match? If we were told to match2367 * at the end, size must be exactly at try+fragsize,2368 * otherwise try+fragsize must be still within the preimage,2369 * and either case, the old piece should match the preimage2370 * exactly.2371 */2372if((match_end2373? (try+ preimage->len == img->len)2374: (try+ preimage->len <= img->len)) &&2375!memcmp(img->buf +try, preimage->buf, preimage->len))2376return1;2377}else{2378/*2379 * The preimage extends beyond the end of img, so2380 * there cannot be an exact match.2381 *2382 * There must be one non-blank context line that match2383 * a line before the end of img.2384 */2385char*buf_end;23862387 buf = preimage->buf;2388 buf_end = buf;2389for(i =0; i < preimage_limit; i++)2390 buf_end += preimage->line[i].len;23912392for( ; buf < buf_end; buf++)2393if(!isspace(*buf))2394break;2395if(buf == buf_end)2396return0;2397}23982399/*2400 * No exact match. If we are ignoring whitespace, run a line-by-line2401 * fuzzy matching. We collect all the line length information because2402 * we need it to adjust whitespace if we match.2403 */2404if(state->ws_ignore_action == ignore_ws_change)2405returnline_by_line_fuzzy_match(img, preimage, postimage,2406try, try_lno, preimage_limit);24072408if(state->ws_error_action != correct_ws_error)2409return0;24102411/*2412 * The hunk does not apply byte-by-byte, but the hash says2413 * it might with whitespace fuzz. We weren't asked to2414 * ignore whitespace, we were asked to correct whitespace2415 * errors, so let's try matching after whitespace correction.2416 *2417 * While checking the preimage against the target, whitespace2418 * errors in both fixed, we count how large the corresponding2419 * postimage needs to be. The postimage prepared by2420 * apply_one_fragment() has whitespace errors fixed on added2421 * lines already, but the common lines were propagated as-is,2422 * which may become longer when their whitespace errors are2423 * fixed.2424 */24252426/* First count added lines in postimage */2427 postlen =0;2428for(i =0; i < postimage->nr; i++) {2429if(!(postimage->line[i].flag & LINE_COMMON))2430 postlen += postimage->line[i].len;2431}24322433/*2434 * The preimage may extend beyond the end of the file,2435 * but in this loop we will only handle the part of the2436 * preimage that falls within the file.2437 */2438strbuf_init(&fixed, preimage->len +1);2439 orig = preimage->buf;2440 target = img->buf +try;2441for(i =0; i < preimage_limit; i++) {2442size_t oldlen = preimage->line[i].len;2443size_t tgtlen = img->line[try_lno + i].len;2444size_t fixstart = fixed.len;2445struct strbuf tgtfix;2446int match;24472448/* Try fixing the line in the preimage */2449ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);24502451/* Try fixing the line in the target */2452strbuf_init(&tgtfix, tgtlen);2453ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);24542455/*2456 * If they match, either the preimage was based on2457 * a version before our tree fixed whitespace breakage,2458 * or we are lacking a whitespace-fix patch the tree2459 * the preimage was based on already had (i.e. target2460 * has whitespace breakage, the preimage doesn't).2461 * In either case, we are fixing the whitespace breakages2462 * so we might as well take the fix together with their2463 * real change.2464 */2465 match = (tgtfix.len == fixed.len - fixstart &&2466!memcmp(tgtfix.buf, fixed.buf + fixstart,2467 fixed.len - fixstart));24682469/* Add the length if this is common with the postimage */2470if(preimage->line[i].flag & LINE_COMMON)2471 postlen += tgtfix.len;24722473strbuf_release(&tgtfix);2474if(!match)2475goto unmatch_exit;24762477 orig += oldlen;2478 target += tgtlen;2479}248024812482/*2483 * Now handle the lines in the preimage that falls beyond the2484 * end of the file (if any). They will only match if they are2485 * empty or only contain whitespace (if WS_BLANK_AT_EOL is2486 * false).2487 */2488for( ; i < preimage->nr; i++) {2489size_t fixstart = fixed.len;/* start of the fixed preimage */2490size_t oldlen = preimage->line[i].len;2491int j;24922493/* Try fixing the line in the preimage */2494ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);24952496for(j = fixstart; j < fixed.len; j++)2497if(!isspace(fixed.buf[j]))2498goto unmatch_exit;24992500 orig += oldlen;2501}25022503/*2504 * Yes, the preimage is based on an older version that still2505 * has whitespace breakages unfixed, and fixing them makes the2506 * hunk match. Update the context lines in the postimage.2507 */2508 fixed_buf =strbuf_detach(&fixed, &fixed_len);2509if(postlen < postimage->len)2510 postlen =0;2511update_pre_post_images(preimage, postimage,2512 fixed_buf, fixed_len, postlen);2513return1;25142515 unmatch_exit:2516strbuf_release(&fixed);2517return0;2518}25192520static intfind_pos(struct apply_state *state,2521struct image *img,2522struct image *preimage,2523struct image *postimage,2524int line,2525unsigned ws_rule,2526int match_beginning,int match_end)2527{2528int i;2529unsigned long backwards, forwards,try;2530int backwards_lno, forwards_lno, try_lno;25312532/*2533 * If match_beginning or match_end is specified, there is no2534 * point starting from a wrong line that will never match and2535 * wander around and wait for a match at the specified end.2536 */2537if(match_beginning)2538 line =0;2539else if(match_end)2540 line = img->nr - preimage->nr;25412542/*2543 * Because the comparison is unsigned, the following test2544 * will also take care of a negative line number that can2545 * result when match_end and preimage is larger than the target.2546 */2547if((size_t) line > img->nr)2548 line = img->nr;25492550try=0;2551for(i =0; i < line; i++)2552try+= img->line[i].len;25532554/*2555 * There's probably some smart way to do this, but I'll leave2556 * that to the smart and beautiful people. I'm simple and stupid.2557 */2558 backwards =try;2559 backwards_lno = line;2560 forwards =try;2561 forwards_lno = line;2562 try_lno = line;25632564for(i =0; ; i++) {2565if(match_fragment(state, img, preimage, postimage,2566try, try_lno, ws_rule,2567 match_beginning, match_end))2568return try_lno;25692570 again:2571if(backwards_lno ==0&& forwards_lno == img->nr)2572break;25732574if(i &1) {2575if(backwards_lno ==0) {2576 i++;2577goto again;2578}2579 backwards_lno--;2580 backwards -= img->line[backwards_lno].len;2581try= backwards;2582 try_lno = backwards_lno;2583}else{2584if(forwards_lno == img->nr) {2585 i++;2586goto again;2587}2588 forwards += img->line[forwards_lno].len;2589 forwards_lno++;2590try= forwards;2591 try_lno = forwards_lno;2592}25932594}2595return-1;2596}25972598static voidremove_first_line(struct image *img)2599{2600 img->buf += img->line[0].len;2601 img->len -= img->line[0].len;2602 img->line++;2603 img->nr--;2604}26052606static voidremove_last_line(struct image *img)2607{2608 img->len -= img->line[--img->nr].len;2609}26102611/*2612 * The change from "preimage" and "postimage" has been found to2613 * apply at applied_pos (counts in line numbers) in "img".2614 * Update "img" to remove "preimage" and replace it with "postimage".2615 */2616static voidupdate_image(struct apply_state *state,2617struct image *img,2618int applied_pos,2619struct image *preimage,2620struct image *postimage)2621{2622/*2623 * remove the copy of preimage at offset in img2624 * and replace it with postimage2625 */2626int i, nr;2627size_t remove_count, insert_count, applied_at =0;2628char*result;2629int preimage_limit;26302631/*2632 * If we are removing blank lines at the end of img,2633 * the preimage may extend beyond the end.2634 * If that is the case, we must be careful only to2635 * remove the part of the preimage that falls within2636 * the boundaries of img. Initialize preimage_limit2637 * to the number of lines in the preimage that falls2638 * within the boundaries.2639 */2640 preimage_limit = preimage->nr;2641if(preimage_limit > img->nr - applied_pos)2642 preimage_limit = img->nr - applied_pos;26432644for(i =0; i < applied_pos; i++)2645 applied_at += img->line[i].len;26462647 remove_count =0;2648for(i =0; i < preimage_limit; i++)2649 remove_count += img->line[applied_pos + i].len;2650 insert_count = postimage->len;26512652/* Adjust the contents */2653 result =xmalloc(st_add3(st_sub(img->len, remove_count), insert_count,1));2654memcpy(result, img->buf, applied_at);2655memcpy(result + applied_at, postimage->buf, postimage->len);2656memcpy(result + applied_at + postimage->len,2657 img->buf + (applied_at + remove_count),2658 img->len - (applied_at + remove_count));2659free(img->buf);2660 img->buf = result;2661 img->len += insert_count - remove_count;2662 result[img->len] ='\0';26632664/* Adjust the line table */2665 nr = img->nr + postimage->nr - preimage_limit;2666if(preimage_limit < postimage->nr) {2667/*2668 * NOTE: this knows that we never call remove_first_line()2669 * on anything other than pre/post image.2670 */2671REALLOC_ARRAY(img->line, nr);2672 img->line_allocated = img->line;2673}2674if(preimage_limit != postimage->nr)2675memmove(img->line + applied_pos + postimage->nr,2676 img->line + applied_pos + preimage_limit,2677(img->nr - (applied_pos + preimage_limit)) *2678sizeof(*img->line));2679memcpy(img->line + applied_pos,2680 postimage->line,2681 postimage->nr *sizeof(*img->line));2682if(!state->allow_overlap)2683for(i =0; i < postimage->nr; i++)2684 img->line[applied_pos + i].flag |= LINE_PATCHED;2685 img->nr = nr;2686}26872688/*2689 * Use the patch-hunk text in "frag" to prepare two images (preimage and2690 * postimage) for the hunk. Find lines that match "preimage" in "img" and2691 * replace the part of "img" with "postimage" text.2692 */2693static intapply_one_fragment(struct apply_state *state,2694struct image *img,struct fragment *frag,2695int inaccurate_eof,unsigned ws_rule,2696int nth_fragment)2697{2698int match_beginning, match_end;2699const char*patch = frag->patch;2700int size = frag->size;2701char*old, *oldlines;2702struct strbuf newlines;2703int new_blank_lines_at_end =0;2704int found_new_blank_lines_at_end =0;2705int hunk_linenr = frag->linenr;2706unsigned long leading, trailing;2707int pos, applied_pos;2708struct image preimage;2709struct image postimage;27102711memset(&preimage,0,sizeof(preimage));2712memset(&postimage,0,sizeof(postimage));2713 oldlines =xmalloc(size);2714strbuf_init(&newlines, size);27152716 old = oldlines;2717while(size >0) {2718char first;2719int len =linelen(patch, size);2720int plen;2721int added_blank_line =0;2722int is_blank_context =0;2723size_t start;27242725if(!len)2726break;27272728/*2729 * "plen" is how much of the line we should use for2730 * the actual patch data. Normally we just remove the2731 * first character on the line, but if the line is2732 * followed by "\ No newline", then we also remove the2733 * last one (which is the newline, of course).2734 */2735 plen = len -1;2736if(len < size && patch[len] =='\\')2737 plen--;2738 first = *patch;2739if(state->apply_in_reverse) {2740if(first =='-')2741 first ='+';2742else if(first =='+')2743 first ='-';2744}27452746switch(first) {2747case'\n':2748/* Newer GNU diff, empty context line */2749if(plen <0)2750/* ... followed by '\No newline'; nothing */2751break;2752*old++ ='\n';2753strbuf_addch(&newlines,'\n');2754add_line_info(&preimage,"\n",1, LINE_COMMON);2755add_line_info(&postimage,"\n",1, LINE_COMMON);2756 is_blank_context =1;2757break;2758case' ':2759if(plen && (ws_rule & WS_BLANK_AT_EOF) &&2760ws_blank_line(patch +1, plen, ws_rule))2761 is_blank_context =1;2762case'-':2763memcpy(old, patch +1, plen);2764add_line_info(&preimage, old, plen,2765(first ==' '? LINE_COMMON :0));2766 old += plen;2767if(first =='-')2768break;2769/* Fall-through for ' ' */2770case'+':2771/* --no-add does not add new lines */2772if(first =='+'&& state->no_add)2773break;27742775 start = newlines.len;2776if(first !='+'||2777!state->whitespace_error ||2778 state->ws_error_action != correct_ws_error) {2779strbuf_add(&newlines, patch +1, plen);2780}2781else{2782ws_fix_copy(&newlines, patch +1, plen, ws_rule, &state->applied_after_fixing_ws);2783}2784add_line_info(&postimage, newlines.buf + start, newlines.len - start,2785(first =='+'?0: LINE_COMMON));2786if(first =='+'&&2787(ws_rule & WS_BLANK_AT_EOF) &&2788ws_blank_line(patch +1, plen, ws_rule))2789 added_blank_line =1;2790break;2791case'@':case'\\':2792/* Ignore it, we already handled it */2793break;2794default:2795if(state->apply_verbosely)2796error(_("invalid start of line: '%c'"), first);2797 applied_pos = -1;2798goto out;2799}2800if(added_blank_line) {2801if(!new_blank_lines_at_end)2802 found_new_blank_lines_at_end = hunk_linenr;2803 new_blank_lines_at_end++;2804}2805else if(is_blank_context)2806;2807else2808 new_blank_lines_at_end =0;2809 patch += len;2810 size -= len;2811 hunk_linenr++;2812}2813if(inaccurate_eof &&2814 old > oldlines && old[-1] =='\n'&&2815 newlines.len >0&& newlines.buf[newlines.len -1] =='\n') {2816 old--;2817strbuf_setlen(&newlines, newlines.len -1);2818}28192820 leading = frag->leading;2821 trailing = frag->trailing;28222823/*2824 * A hunk to change lines at the beginning would begin with2825 * @@ -1,L +N,M @@2826 * but we need to be careful. -U0 that inserts before the second2827 * line also has this pattern.2828 *2829 * And a hunk to add to an empty file would begin with2830 * @@ -0,0 +N,M @@2831 *2832 * In other words, a hunk that is (frag->oldpos <= 1) with or2833 * without leading context must match at the beginning.2834 */2835 match_beginning = (!frag->oldpos ||2836(frag->oldpos ==1&& !state->unidiff_zero));28372838/*2839 * A hunk without trailing lines must match at the end.2840 * However, we simply cannot tell if a hunk must match end2841 * from the lack of trailing lines if the patch was generated2842 * with unidiff without any context.2843 */2844 match_end = !state->unidiff_zero && !trailing;28452846 pos = frag->newpos ? (frag->newpos -1) :0;2847 preimage.buf = oldlines;2848 preimage.len = old - oldlines;2849 postimage.buf = newlines.buf;2850 postimage.len = newlines.len;2851 preimage.line = preimage.line_allocated;2852 postimage.line = postimage.line_allocated;28532854for(;;) {28552856 applied_pos =find_pos(state, img, &preimage, &postimage, pos,2857 ws_rule, match_beginning, match_end);28582859if(applied_pos >=0)2860break;28612862/* Am I at my context limits? */2863if((leading <= state->p_context) && (trailing <= state->p_context))2864break;2865if(match_beginning || match_end) {2866 match_beginning = match_end =0;2867continue;2868}28692870/*2871 * Reduce the number of context lines; reduce both2872 * leading and trailing if they are equal otherwise2873 * just reduce the larger context.2874 */2875if(leading >= trailing) {2876remove_first_line(&preimage);2877remove_first_line(&postimage);2878 pos--;2879 leading--;2880}2881if(trailing > leading) {2882remove_last_line(&preimage);2883remove_last_line(&postimage);2884 trailing--;2885}2886}28872888if(applied_pos >=0) {2889if(new_blank_lines_at_end &&2890 preimage.nr + applied_pos >= img->nr &&2891(ws_rule & WS_BLANK_AT_EOF) &&2892 state->ws_error_action != nowarn_ws_error) {2893record_ws_error(state, WS_BLANK_AT_EOF,"+",1,2894 found_new_blank_lines_at_end);2895if(state->ws_error_action == correct_ws_error) {2896while(new_blank_lines_at_end--)2897remove_last_line(&postimage);2898}2899/*2900 * We would want to prevent write_out_results()2901 * from taking place in apply_patch() that follows2902 * the callchain led us here, which is:2903 * apply_patch->check_patch_list->check_patch->2904 * apply_data->apply_fragments->apply_one_fragment2905 */2906if(state->ws_error_action == die_on_ws_error)2907 state->apply =0;2908}29092910if(state->apply_verbosely && applied_pos != pos) {2911int offset = applied_pos - pos;2912if(state->apply_in_reverse)2913 offset =0- offset;2914fprintf_ln(stderr,2915Q_("Hunk #%dsucceeded at%d(offset%dline).",2916"Hunk #%dsucceeded at%d(offset%dlines).",2917 offset),2918 nth_fragment, applied_pos +1, offset);2919}29202921/*2922 * Warn if it was necessary to reduce the number2923 * of context lines.2924 */2925if((leading != frag->leading) ||2926(trailing != frag->trailing))2927fprintf_ln(stderr,_("Context reduced to (%ld/%ld)"2928" to apply fragment at%d"),2929 leading, trailing, applied_pos+1);2930update_image(state, img, applied_pos, &preimage, &postimage);2931}else{2932if(state->apply_verbosely)2933error(_("while searching for:\n%.*s"),2934(int)(old - oldlines), oldlines);2935}29362937out:2938free(oldlines);2939strbuf_release(&newlines);2940free(preimage.line_allocated);2941free(postimage.line_allocated);29422943return(applied_pos <0);2944}29452946static intapply_binary_fragment(struct apply_state *state,2947struct image *img,2948struct patch *patch)2949{2950struct fragment *fragment = patch->fragments;2951unsigned long len;2952void*dst;29532954if(!fragment)2955returnerror(_("missing binary patch data for '%s'"),2956 patch->new_name ?2957 patch->new_name :2958 patch->old_name);29592960/* Binary patch is irreversible without the optional second hunk */2961if(state->apply_in_reverse) {2962if(!fragment->next)2963returnerror("cannot reverse-apply a binary patch "2964"without the reverse hunk to '%s'",2965 patch->new_name2966? patch->new_name : patch->old_name);2967 fragment = fragment->next;2968}2969switch(fragment->binary_patch_method) {2970case BINARY_DELTA_DEFLATED:2971 dst =patch_delta(img->buf, img->len, fragment->patch,2972 fragment->size, &len);2973if(!dst)2974return-1;2975clear_image(img);2976 img->buf = dst;2977 img->len = len;2978return0;2979case BINARY_LITERAL_DEFLATED:2980clear_image(img);2981 img->len = fragment->size;2982 img->buf =xmemdupz(fragment->patch, img->len);2983return0;2984}2985return-1;2986}29872988/*2989 * Replace "img" with the result of applying the binary patch.2990 * The binary patch data itself in patch->fragment is still kept2991 * but the preimage prepared by the caller in "img" is freed here2992 * or in the helper function apply_binary_fragment() this calls.2993 */2994static intapply_binary(struct apply_state *state,2995struct image *img,2996struct patch *patch)2997{2998const char*name = patch->old_name ? patch->old_name : patch->new_name;2999unsigned char sha1[20];30003001/*3002 * For safety, we require patch index line to contain3003 * full 40-byte textual SHA1 for old and new, at least for now.3004 */3005if(strlen(patch->old_sha1_prefix) !=40||3006strlen(patch->new_sha1_prefix) !=40||3007get_sha1_hex(patch->old_sha1_prefix, sha1) ||3008get_sha1_hex(patch->new_sha1_prefix, sha1))3009returnerror("cannot apply binary patch to '%s' "3010"without full index line", name);30113012if(patch->old_name) {3013/*3014 * See if the old one matches what the patch3015 * applies to.3016 */3017hash_sha1_file(img->buf, img->len, blob_type, sha1);3018if(strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))3019returnerror("the patch applies to '%s' (%s), "3020"which does not match the "3021"current contents.",3022 name,sha1_to_hex(sha1));3023}3024else{3025/* Otherwise, the old one must be empty. */3026if(img->len)3027returnerror("the patch applies to an empty "3028"'%s' but it is not empty", name);3029}30303031get_sha1_hex(patch->new_sha1_prefix, sha1);3032if(is_null_sha1(sha1)) {3033clear_image(img);3034return0;/* deletion patch */3035}30363037if(has_sha1_file(sha1)) {3038/* We already have the postimage */3039enum object_type type;3040unsigned long size;3041char*result;30423043 result =read_sha1_file(sha1, &type, &size);3044if(!result)3045returnerror("the necessary postimage%sfor "3046"'%s' cannot be read",3047 patch->new_sha1_prefix, name);3048clear_image(img);3049 img->buf = result;3050 img->len = size;3051}else{3052/*3053 * We have verified buf matches the preimage;3054 * apply the patch data to it, which is stored3055 * in the patch->fragments->{patch,size}.3056 */3057if(apply_binary_fragment(state, img, patch))3058returnerror(_("binary patch does not apply to '%s'"),3059 name);30603061/* verify that the result matches */3062hash_sha1_file(img->buf, img->len, blob_type, sha1);3063if(strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))3064returnerror(_("binary patch to '%s' creates incorrect result (expecting%s, got%s)"),3065 name, patch->new_sha1_prefix,sha1_to_hex(sha1));3066}30673068return0;3069}30703071static intapply_fragments(struct apply_state *state,struct image *img,struct patch *patch)3072{3073struct fragment *frag = patch->fragments;3074const char*name = patch->old_name ? patch->old_name : patch->new_name;3075unsigned ws_rule = patch->ws_rule;3076unsigned inaccurate_eof = patch->inaccurate_eof;3077int nth =0;30783079if(patch->is_binary)3080returnapply_binary(state, img, patch);30813082while(frag) {3083 nth++;3084if(apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {3085error(_("patch failed:%s:%ld"), name, frag->oldpos);3086if(!state->apply_with_reject)3087return-1;3088 frag->rejected =1;3089}3090 frag = frag->next;3091}3092return0;3093}30943095static intread_blob_object(struct strbuf *buf,const unsigned char*sha1,unsigned mode)3096{3097if(S_ISGITLINK(mode)) {3098strbuf_grow(buf,100);3099strbuf_addf(buf,"Subproject commit%s\n",sha1_to_hex(sha1));3100}else{3101enum object_type type;3102unsigned long sz;3103char*result;31043105 result =read_sha1_file(sha1, &type, &sz);3106if(!result)3107return-1;3108/* XXX read_sha1_file NUL-terminates */3109strbuf_attach(buf, result, sz, sz +1);3110}3111return0;3112}31133114static intread_file_or_gitlink(const struct cache_entry *ce,struct strbuf *buf)3115{3116if(!ce)3117return0;3118returnread_blob_object(buf, ce->sha1, ce->ce_mode);3119}31203121static struct patch *in_fn_table(struct apply_state *state,const char*name)3122{3123struct string_list_item *item;31243125if(name == NULL)3126return NULL;31273128 item =string_list_lookup(&state->fn_table, name);3129if(item != NULL)3130return(struct patch *)item->util;31313132return NULL;3133}31343135/*3136 * item->util in the filename table records the status of the path.3137 * Usually it points at a patch (whose result records the contents3138 * of it after applying it), but it could be PATH_WAS_DELETED for a3139 * path that a previously applied patch has already removed, or3140 * PATH_TO_BE_DELETED for a path that a later patch would remove.3141 *3142 * The latter is needed to deal with a case where two paths A and B3143 * are swapped by first renaming A to B and then renaming B to A;3144 * moving A to B should not be prevented due to presence of B as we3145 * will remove it in a later patch.3146 */3147#define PATH_TO_BE_DELETED ((struct patch *) -2)3148#define PATH_WAS_DELETED ((struct patch *) -1)31493150static intto_be_deleted(struct patch *patch)3151{3152return patch == PATH_TO_BE_DELETED;3153}31543155static intwas_deleted(struct patch *patch)3156{3157return patch == PATH_WAS_DELETED;3158}31593160static voidadd_to_fn_table(struct apply_state *state,struct patch *patch)3161{3162struct string_list_item *item;31633164/*3165 * Always add new_name unless patch is a deletion3166 * This should cover the cases for normal diffs,3167 * file creations and copies3168 */3169if(patch->new_name != NULL) {3170 item =string_list_insert(&state->fn_table, patch->new_name);3171 item->util = patch;3172}31733174/*3175 * store a failure on rename/deletion cases because3176 * later chunks shouldn't patch old names3177 */3178if((patch->new_name == NULL) || (patch->is_rename)) {3179 item =string_list_insert(&state->fn_table, patch->old_name);3180 item->util = PATH_WAS_DELETED;3181}3182}31833184static voidprepare_fn_table(struct apply_state *state,struct patch *patch)3185{3186/*3187 * store information about incoming file deletion3188 */3189while(patch) {3190if((patch->new_name == NULL) || (patch->is_rename)) {3191struct string_list_item *item;3192 item =string_list_insert(&state->fn_table, patch->old_name);3193 item->util = PATH_TO_BE_DELETED;3194}3195 patch = patch->next;3196}3197}31983199static intcheckout_target(struct index_state *istate,3200struct cache_entry *ce,struct stat *st)3201{3202struct checkout costate;32033204memset(&costate,0,sizeof(costate));3205 costate.base_dir ="";3206 costate.refresh_cache =1;3207 costate.istate = istate;3208if(checkout_entry(ce, &costate, NULL) ||lstat(ce->name, st))3209returnerror(_("cannot checkout%s"), ce->name);3210return0;3211}32123213static struct patch *previous_patch(struct apply_state *state,3214struct patch *patch,3215int*gone)3216{3217struct patch *previous;32183219*gone =0;3220if(patch->is_copy || patch->is_rename)3221return NULL;/* "git" patches do not depend on the order */32223223 previous =in_fn_table(state, patch->old_name);3224if(!previous)3225return NULL;32263227if(to_be_deleted(previous))3228return NULL;/* the deletion hasn't happened yet */32293230if(was_deleted(previous))3231*gone =1;32323233return previous;3234}32353236static intverify_index_match(const struct cache_entry *ce,struct stat *st)3237{3238if(S_ISGITLINK(ce->ce_mode)) {3239if(!S_ISDIR(st->st_mode))3240return-1;3241return0;3242}3243returnce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);3244}32453246#define SUBMODULE_PATCH_WITHOUT_INDEX 132473248static intload_patch_target(struct apply_state *state,3249struct strbuf *buf,3250const struct cache_entry *ce,3251struct stat *st,3252const char*name,3253unsigned expected_mode)3254{3255if(state->cached || state->check_index) {3256if(read_file_or_gitlink(ce, buf))3257returnerror(_("failed to read%s"), name);3258}else if(name) {3259if(S_ISGITLINK(expected_mode)) {3260if(ce)3261returnread_file_or_gitlink(ce, buf);3262else3263return SUBMODULE_PATCH_WITHOUT_INDEX;3264}else if(has_symlink_leading_path(name,strlen(name))) {3265returnerror(_("reading from '%s' beyond a symbolic link"), name);3266}else{3267if(read_old_data(st, name, buf))3268returnerror(_("failed to read%s"), name);3269}3270}3271return0;3272}32733274/*3275 * We are about to apply "patch"; populate the "image" with the3276 * current version we have, from the working tree or from the index,3277 * depending on the situation e.g. --cached/--index. If we are3278 * applying a non-git patch that incrementally updates the tree,3279 * we read from the result of a previous diff.3280 */3281static intload_preimage(struct apply_state *state,3282struct image *image,3283struct patch *patch,struct stat *st,3284const struct cache_entry *ce)3285{3286struct strbuf buf = STRBUF_INIT;3287size_t len;3288char*img;3289struct patch *previous;3290int status;32913292 previous =previous_patch(state, patch, &status);3293if(status)3294returnerror(_("path%shas been renamed/deleted"),3295 patch->old_name);3296if(previous) {3297/* We have a patched copy in memory; use that. */3298strbuf_add(&buf, previous->result, previous->resultsize);3299}else{3300 status =load_patch_target(state, &buf, ce, st,3301 patch->old_name, patch->old_mode);3302if(status <0)3303return status;3304else if(status == SUBMODULE_PATCH_WITHOUT_INDEX) {3305/*3306 * There is no way to apply subproject3307 * patch without looking at the index.3308 * NEEDSWORK: shouldn't this be flagged3309 * as an error???3310 */3311free_fragment_list(patch->fragments);3312 patch->fragments = NULL;3313}else if(status) {3314returnerror(_("failed to read%s"), patch->old_name);3315}3316}33173318 img =strbuf_detach(&buf, &len);3319prepare_image(image, img, len, !patch->is_binary);3320return0;3321}33223323static intthree_way_merge(struct image *image,3324char*path,3325const unsigned char*base,3326const unsigned char*ours,3327const unsigned char*theirs)3328{3329 mmfile_t base_file, our_file, their_file;3330 mmbuffer_t result = { NULL };3331int status;33323333read_mmblob(&base_file, base);3334read_mmblob(&our_file, ours);3335read_mmblob(&their_file, theirs);3336 status =ll_merge(&result, path,3337&base_file,"base",3338&our_file,"ours",3339&their_file,"theirs", NULL);3340free(base_file.ptr);3341free(our_file.ptr);3342free(their_file.ptr);3343if(status <0|| !result.ptr) {3344free(result.ptr);3345return-1;3346}3347clear_image(image);3348 image->buf = result.ptr;3349 image->len = result.size;33503351return status;3352}33533354/*3355 * When directly falling back to add/add three-way merge, we read from3356 * the current contents of the new_name. In no cases other than that3357 * this function will be called.3358 */3359static intload_current(struct apply_state *state,3360struct image *image,3361struct patch *patch)3362{3363struct strbuf buf = STRBUF_INIT;3364int status, pos;3365size_t len;3366char*img;3367struct stat st;3368struct cache_entry *ce;3369char*name = patch->new_name;3370unsigned mode = patch->new_mode;33713372if(!patch->is_new)3373die("BUG: patch to%sis not a creation", patch->old_name);33743375 pos =cache_name_pos(name,strlen(name));3376if(pos <0)3377returnerror(_("%s: does not exist in index"), name);3378 ce = active_cache[pos];3379if(lstat(name, &st)) {3380if(errno != ENOENT)3381returnerror(_("%s:%s"), name,strerror(errno));3382if(checkout_target(&the_index, ce, &st))3383return-1;3384}3385if(verify_index_match(ce, &st))3386returnerror(_("%s: does not match index"), name);33873388 status =load_patch_target(state, &buf, ce, &st, name, mode);3389if(status <0)3390return status;3391else if(status)3392return-1;3393 img =strbuf_detach(&buf, &len);3394prepare_image(image, img, len, !patch->is_binary);3395return0;3396}33973398static inttry_threeway(struct apply_state *state,3399struct image *image,3400struct patch *patch,3401struct stat *st,3402const struct cache_entry *ce)3403{3404unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];3405struct strbuf buf = STRBUF_INIT;3406size_t len;3407int status;3408char*img;3409struct image tmp_image;34103411/* No point falling back to 3-way merge in these cases */3412if(patch->is_delete ||3413S_ISGITLINK(patch->old_mode) ||S_ISGITLINK(patch->new_mode))3414return-1;34153416/* Preimage the patch was prepared for */3417if(patch->is_new)3418write_sha1_file("",0, blob_type, pre_sha1);3419else if(get_sha1(patch->old_sha1_prefix, pre_sha1) ||3420read_blob_object(&buf, pre_sha1, patch->old_mode))3421returnerror("repository lacks the necessary blob to fall back on 3-way merge.");34223423fprintf(stderr,"Falling back to three-way merge...\n");34243425 img =strbuf_detach(&buf, &len);3426prepare_image(&tmp_image, img, len,1);3427/* Apply the patch to get the post image */3428if(apply_fragments(state, &tmp_image, patch) <0) {3429clear_image(&tmp_image);3430return-1;3431}3432/* post_sha1[] is theirs */3433write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);3434clear_image(&tmp_image);34353436/* our_sha1[] is ours */3437if(patch->is_new) {3438if(load_current(state, &tmp_image, patch))3439returnerror("cannot read the current contents of '%s'",3440 patch->new_name);3441}else{3442if(load_preimage(state, &tmp_image, patch, st, ce))3443returnerror("cannot read the current contents of '%s'",3444 patch->old_name);3445}3446write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);3447clear_image(&tmp_image);34483449/* in-core three-way merge between post and our using pre as base */3450 status =three_way_merge(image, patch->new_name,3451 pre_sha1, our_sha1, post_sha1);3452if(status <0) {3453fprintf(stderr,"Failed to fall back on three-way merge...\n");3454return status;3455}34563457if(status) {3458 patch->conflicted_threeway =1;3459if(patch->is_new)3460oidclr(&patch->threeway_stage[0]);3461else3462hashcpy(patch->threeway_stage[0].hash, pre_sha1);3463hashcpy(patch->threeway_stage[1].hash, our_sha1);3464hashcpy(patch->threeway_stage[2].hash, post_sha1);3465fprintf(stderr,"Applied patch to '%s' with conflicts.\n", patch->new_name);3466}else{3467fprintf(stderr,"Applied patch to '%s' cleanly.\n", patch->new_name);3468}3469return0;3470}34713472static intapply_data(struct apply_state *state,struct patch *patch,3473struct stat *st,const struct cache_entry *ce)3474{3475struct image image;34763477if(load_preimage(state, &image, patch, st, ce) <0)3478return-1;34793480if(patch->direct_to_threeway ||3481apply_fragments(state, &image, patch) <0) {3482/* Note: with --reject, apply_fragments() returns 0 */3483if(!state->threeway ||try_threeway(state, &image, patch, st, ce) <0)3484return-1;3485}3486 patch->result = image.buf;3487 patch->resultsize = image.len;3488add_to_fn_table(state, patch);3489free(image.line_allocated);34903491if(0< patch->is_delete && patch->resultsize)3492returnerror(_("removal patch leaves file contents"));34933494return0;3495}34963497/*3498 * If "patch" that we are looking at modifies or deletes what we have,3499 * we would want it not to lose any local modification we have, either3500 * in the working tree or in the index.3501 *3502 * This also decides if a non-git patch is a creation patch or a3503 * modification to an existing empty file. We do not check the state3504 * of the current tree for a creation patch in this function; the caller3505 * check_patch() separately makes sure (and errors out otherwise) that3506 * the path the patch creates does not exist in the current tree.3507 */3508static intcheck_preimage(struct apply_state *state,3509struct patch *patch,3510struct cache_entry **ce,3511struct stat *st)3512{3513const char*old_name = patch->old_name;3514struct patch *previous = NULL;3515int stat_ret =0, status;3516unsigned st_mode =0;35173518if(!old_name)3519return0;35203521assert(patch->is_new <=0);3522 previous =previous_patch(state, patch, &status);35233524if(status)3525returnerror(_("path%shas been renamed/deleted"), old_name);3526if(previous) {3527 st_mode = previous->new_mode;3528}else if(!state->cached) {3529 stat_ret =lstat(old_name, st);3530if(stat_ret && errno != ENOENT)3531returnerror(_("%s:%s"), old_name,strerror(errno));3532}35333534if(state->check_index && !previous) {3535int pos =cache_name_pos(old_name,strlen(old_name));3536if(pos <0) {3537if(patch->is_new <0)3538goto is_new;3539returnerror(_("%s: does not exist in index"), old_name);3540}3541*ce = active_cache[pos];3542if(stat_ret <0) {3543if(checkout_target(&the_index, *ce, st))3544return-1;3545}3546if(!state->cached &&verify_index_match(*ce, st))3547returnerror(_("%s: does not match index"), old_name);3548if(state->cached)3549 st_mode = (*ce)->ce_mode;3550}else if(stat_ret <0) {3551if(patch->is_new <0)3552goto is_new;3553returnerror(_("%s:%s"), old_name,strerror(errno));3554}35553556if(!state->cached && !previous)3557 st_mode =ce_mode_from_stat(*ce, st->st_mode);35583559if(patch->is_new <0)3560 patch->is_new =0;3561if(!patch->old_mode)3562 patch->old_mode = st_mode;3563if((st_mode ^ patch->old_mode) & S_IFMT)3564returnerror(_("%s: wrong type"), old_name);3565if(st_mode != patch->old_mode)3566warning(_("%shas type%o, expected%o"),3567 old_name, st_mode, patch->old_mode);3568if(!patch->new_mode && !patch->is_delete)3569 patch->new_mode = st_mode;3570return0;35713572 is_new:3573 patch->is_new =1;3574 patch->is_delete =0;3575free(patch->old_name);3576 patch->old_name = NULL;3577return0;3578}357935803581#define EXISTS_IN_INDEX 13582#define EXISTS_IN_WORKTREE 235833584static intcheck_to_create(struct apply_state *state,3585const char*new_name,3586int ok_if_exists)3587{3588struct stat nst;35893590if(state->check_index &&3591cache_name_pos(new_name,strlen(new_name)) >=0&&3592!ok_if_exists)3593return EXISTS_IN_INDEX;3594if(state->cached)3595return0;35963597if(!lstat(new_name, &nst)) {3598if(S_ISDIR(nst.st_mode) || ok_if_exists)3599return0;3600/*3601 * A leading component of new_name might be a symlink3602 * that is going to be removed with this patch, but3603 * still pointing at somewhere that has the path.3604 * In such a case, path "new_name" does not exist as3605 * far as git is concerned.3606 */3607if(has_symlink_leading_path(new_name,strlen(new_name)))3608return0;36093610return EXISTS_IN_WORKTREE;3611}else if((errno != ENOENT) && (errno != ENOTDIR)) {3612returnerror("%s:%s", new_name,strerror(errno));3613}3614return0;3615}36163617static uintptr_tregister_symlink_changes(struct apply_state *state,3618const char*path,3619uintptr_t what)3620{3621struct string_list_item *ent;36223623 ent =string_list_lookup(&state->symlink_changes, path);3624if(!ent) {3625 ent =string_list_insert(&state->symlink_changes, path);3626 ent->util = (void*)0;3627}3628 ent->util = (void*)(what | ((uintptr_t)ent->util));3629return(uintptr_t)ent->util;3630}36313632static uintptr_tcheck_symlink_changes(struct apply_state *state,const char*path)3633{3634struct string_list_item *ent;36353636 ent =string_list_lookup(&state->symlink_changes, path);3637if(!ent)3638return0;3639return(uintptr_t)ent->util;3640}36413642static voidprepare_symlink_changes(struct apply_state *state,struct patch *patch)3643{3644for( ; patch; patch = patch->next) {3645if((patch->old_name &&S_ISLNK(patch->old_mode)) &&3646(patch->is_rename || patch->is_delete))3647/* the symlink at patch->old_name is removed */3648register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);36493650if(patch->new_name &&S_ISLNK(patch->new_mode))3651/* the symlink at patch->new_name is created or remains */3652register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);3653}3654}36553656static intpath_is_beyond_symlink_1(struct apply_state *state,struct strbuf *name)3657{3658do{3659unsigned int change;36603661while(--name->len && name->buf[name->len] !='/')3662;/* scan backwards */3663if(!name->len)3664break;3665 name->buf[name->len] ='\0';3666 change =check_symlink_changes(state, name->buf);3667if(change & APPLY_SYMLINK_IN_RESULT)3668return1;3669if(change & APPLY_SYMLINK_GOES_AWAY)3670/*3671 * This cannot be "return 0", because we may3672 * see a new one created at a higher level.3673 */3674continue;36753676/* otherwise, check the preimage */3677if(state->check_index) {3678struct cache_entry *ce;36793680 ce =cache_file_exists(name->buf, name->len, ignore_case);3681if(ce &&S_ISLNK(ce->ce_mode))3682return1;3683}else{3684struct stat st;3685if(!lstat(name->buf, &st) &&S_ISLNK(st.st_mode))3686return1;3687}3688}while(1);3689return0;3690}36913692static intpath_is_beyond_symlink(struct apply_state *state,const char*name_)3693{3694int ret;3695struct strbuf name = STRBUF_INIT;36963697assert(*name_ !='\0');3698strbuf_addstr(&name, name_);3699 ret =path_is_beyond_symlink_1(state, &name);3700strbuf_release(&name);37013702return ret;3703}37043705static voiddie_on_unsafe_path(struct patch *patch)3706{3707const char*old_name = NULL;3708const char*new_name = NULL;3709if(patch->is_delete)3710 old_name = patch->old_name;3711else if(!patch->is_new && !patch->is_copy)3712 old_name = patch->old_name;3713if(!patch->is_delete)3714 new_name = patch->new_name;37153716if(old_name && !verify_path(old_name))3717die(_("invalid path '%s'"), old_name);3718if(new_name && !verify_path(new_name))3719die(_("invalid path '%s'"), new_name);3720}37213722/*3723 * Check and apply the patch in-core; leave the result in patch->result3724 * for the caller to write it out to the final destination.3725 */3726static intcheck_patch(struct apply_state *state,struct patch *patch)3727{3728struct stat st;3729const char*old_name = patch->old_name;3730const char*new_name = patch->new_name;3731const char*name = old_name ? old_name : new_name;3732struct cache_entry *ce = NULL;3733struct patch *tpatch;3734int ok_if_exists;3735int status;37363737 patch->rejected =1;/* we will drop this after we succeed */37383739 status =check_preimage(state, patch, &ce, &st);3740if(status)3741return status;3742 old_name = patch->old_name;37433744/*3745 * A type-change diff is always split into a patch to delete3746 * old, immediately followed by a patch to create new (see3747 * diff.c::run_diff()); in such a case it is Ok that the entry3748 * to be deleted by the previous patch is still in the working3749 * tree and in the index.3750 *3751 * A patch to swap-rename between A and B would first rename A3752 * to B and then rename B to A. While applying the first one,3753 * the presence of B should not stop A from getting renamed to3754 * B; ask to_be_deleted() about the later rename. Removal of3755 * B and rename from A to B is handled the same way by asking3756 * was_deleted().3757 */3758if((tpatch =in_fn_table(state, new_name)) &&3759(was_deleted(tpatch) ||to_be_deleted(tpatch)))3760 ok_if_exists =1;3761else3762 ok_if_exists =0;37633764if(new_name &&3765((0< patch->is_new) || patch->is_rename || patch->is_copy)) {3766int err =check_to_create(state, new_name, ok_if_exists);37673768if(err && state->threeway) {3769 patch->direct_to_threeway =1;3770}else switch(err) {3771case0:3772break;/* happy */3773case EXISTS_IN_INDEX:3774returnerror(_("%s: already exists in index"), new_name);3775break;3776case EXISTS_IN_WORKTREE:3777returnerror(_("%s: already exists in working directory"),3778 new_name);3779default:3780return err;3781}37823783if(!patch->new_mode) {3784if(0< patch->is_new)3785 patch->new_mode = S_IFREG |0644;3786else3787 patch->new_mode = patch->old_mode;3788}3789}37903791if(new_name && old_name) {3792int same = !strcmp(old_name, new_name);3793if(!patch->new_mode)3794 patch->new_mode = patch->old_mode;3795if((patch->old_mode ^ patch->new_mode) & S_IFMT) {3796if(same)3797returnerror(_("new mode (%o) of%sdoes not "3798"match old mode (%o)"),3799 patch->new_mode, new_name,3800 patch->old_mode);3801else3802returnerror(_("new mode (%o) of%sdoes not "3803"match old mode (%o) of%s"),3804 patch->new_mode, new_name,3805 patch->old_mode, old_name);3806}3807}38083809if(!state->unsafe_paths)3810die_on_unsafe_path(patch);38113812/*3813 * An attempt to read from or delete a path that is beyond a3814 * symbolic link will be prevented by load_patch_target() that3815 * is called at the beginning of apply_data() so we do not3816 * have to worry about a patch marked with "is_delete" bit3817 * here. We however need to make sure that the patch result3818 * is not deposited to a path that is beyond a symbolic link3819 * here.3820 */3821if(!patch->is_delete &&path_is_beyond_symlink(state, patch->new_name))3822returnerror(_("affected file '%s' is beyond a symbolic link"),3823 patch->new_name);38243825if(apply_data(state, patch, &st, ce) <0)3826returnerror(_("%s: patch does not apply"), name);3827 patch->rejected =0;3828return0;3829}38303831static intcheck_patch_list(struct apply_state *state,struct patch *patch)3832{3833int err =0;38343835prepare_symlink_changes(state, patch);3836prepare_fn_table(state, patch);3837while(patch) {3838if(state->apply_verbosely)3839say_patch_name(stderr,3840_("Checking patch%s..."), patch);3841 err |=check_patch(state, patch);3842 patch = patch->next;3843}3844return err;3845}38463847/* This function tries to read the sha1 from the current index */3848static intget_current_sha1(const char*path,unsigned char*sha1)3849{3850int pos;38513852if(read_cache() <0)3853return-1;3854 pos =cache_name_pos(path,strlen(path));3855if(pos <0)3856return-1;3857hashcpy(sha1, active_cache[pos]->sha1);3858return0;3859}38603861static intpreimage_sha1_in_gitlink_patch(struct patch *p,unsigned char sha1[20])3862{3863/*3864 * A usable gitlink patch has only one fragment (hunk) that looks like:3865 * @@ -1 +1 @@3866 * -Subproject commit <old sha1>3867 * +Subproject commit <new sha1>3868 * or3869 * @@ -1 +0,0 @@3870 * -Subproject commit <old sha1>3871 * for a removal patch.3872 */3873struct fragment *hunk = p->fragments;3874static const char heading[] ="-Subproject commit ";3875char*preimage;38763877if(/* does the patch have only one hunk? */3878 hunk && !hunk->next &&3879/* is its preimage one line? */3880 hunk->oldpos ==1&& hunk->oldlines ==1&&3881/* does preimage begin with the heading? */3882(preimage =memchr(hunk->patch,'\n', hunk->size)) != NULL &&3883starts_with(++preimage, heading) &&3884/* does it record full SHA-1? */3885!get_sha1_hex(preimage +sizeof(heading) -1, sha1) &&3886 preimage[sizeof(heading) +40-1] =='\n'&&3887/* does the abbreviated name on the index line agree with it? */3888starts_with(preimage +sizeof(heading) -1, p->old_sha1_prefix))3889return0;/* it all looks fine */38903891/* we may have full object name on the index line */3892returnget_sha1_hex(p->old_sha1_prefix, sha1);3893}38943895/* Build an index that contains the just the files needed for a 3way merge */3896static voidbuild_fake_ancestor(struct patch *list,const char*filename)3897{3898struct patch *patch;3899struct index_state result = { NULL };3900static struct lock_file lock;39013902/* Once we start supporting the reverse patch, it may be3903 * worth showing the new sha1 prefix, but until then...3904 */3905for(patch = list; patch; patch = patch->next) {3906unsigned char sha1[20];3907struct cache_entry *ce;3908const char*name;39093910 name = patch->old_name ? patch->old_name : patch->new_name;3911if(0< patch->is_new)3912continue;39133914if(S_ISGITLINK(patch->old_mode)) {3915if(!preimage_sha1_in_gitlink_patch(patch, sha1))3916;/* ok, the textual part looks sane */3917else3918die("sha1 information is lacking or useless for submodule%s",3919 name);3920}else if(!get_sha1_blob(patch->old_sha1_prefix, sha1)) {3921;/* ok */3922}else if(!patch->lines_added && !patch->lines_deleted) {3923/* mode-only change: update the current */3924if(get_current_sha1(patch->old_name, sha1))3925die("mode change for%s, which is not "3926"in current HEAD", name);3927}else3928die("sha1 information is lacking or useless "3929"(%s).", name);39303931 ce =make_cache_entry(patch->old_mode, sha1, name,0,0);3932if(!ce)3933die(_("make_cache_entry failed for path '%s'"), name);3934if(add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))3935die("Could not add%sto temporary index", name);3936}39373938hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);3939if(write_locked_index(&result, &lock, COMMIT_LOCK))3940die("Could not write temporary index to%s", filename);39413942discard_index(&result);3943}39443945static voidstat_patch_list(struct apply_state *state,struct patch *patch)3946{3947int files, adds, dels;39483949for(files = adds = dels =0; patch ; patch = patch->next) {3950 files++;3951 adds += patch->lines_added;3952 dels += patch->lines_deleted;3953show_stats(state, patch);3954}39553956print_stat_summary(stdout, files, adds, dels);3957}39583959static voidnumstat_patch_list(struct apply_state *state,3960struct patch *patch)3961{3962for( ; patch; patch = patch->next) {3963const char*name;3964 name = patch->new_name ? patch->new_name : patch->old_name;3965if(patch->is_binary)3966printf("-\t-\t");3967else3968printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);3969write_name_quoted(name, stdout, state->line_termination);3970}3971}39723973static voidshow_file_mode_name(const char*newdelete,unsigned int mode,const char*name)3974{3975if(mode)3976printf("%smode%06o%s\n", newdelete, mode, name);3977else3978printf("%s %s\n", newdelete, name);3979}39803981static voidshow_mode_change(struct patch *p,int show_name)3982{3983if(p->old_mode && p->new_mode && p->old_mode != p->new_mode) {3984if(show_name)3985printf(" mode change%06o =>%06o%s\n",3986 p->old_mode, p->new_mode, p->new_name);3987else3988printf(" mode change%06o =>%06o\n",3989 p->old_mode, p->new_mode);3990}3991}39923993static voidshow_rename_copy(struct patch *p)3994{3995const char*renamecopy = p->is_rename ?"rename":"copy";3996const char*old, *new;39973998/* Find common prefix */3999 old = p->old_name;4000new= p->new_name;4001while(1) {4002const char*slash_old, *slash_new;4003 slash_old =strchr(old,'/');4004 slash_new =strchr(new,'/');4005if(!slash_old ||4006!slash_new ||4007 slash_old - old != slash_new -new||4008memcmp(old,new, slash_new -new))4009break;4010 old = slash_old +1;4011new= slash_new +1;4012}4013/* p->old_name thru old is the common prefix, and old and new4014 * through the end of names are renames4015 */4016if(old != p->old_name)4017printf("%s%.*s{%s=>%s} (%d%%)\n", renamecopy,4018(int)(old - p->old_name), p->old_name,4019 old,new, p->score);4020else4021printf("%s %s=>%s(%d%%)\n", renamecopy,4022 p->old_name, p->new_name, p->score);4023show_mode_change(p,0);4024}40254026static voidsummary_patch_list(struct patch *patch)4027{4028struct patch *p;40294030for(p = patch; p; p = p->next) {4031if(p->is_new)4032show_file_mode_name("create", p->new_mode, p->new_name);4033else if(p->is_delete)4034show_file_mode_name("delete", p->old_mode, p->old_name);4035else{4036if(p->is_rename || p->is_copy)4037show_rename_copy(p);4038else{4039if(p->score) {4040printf(" rewrite%s(%d%%)\n",4041 p->new_name, p->score);4042show_mode_change(p,0);4043}4044else4045show_mode_change(p,1);4046}4047}4048}4049}40504051static voidpatch_stats(struct apply_state *state,struct patch *patch)4052{4053int lines = patch->lines_added + patch->lines_deleted;40544055if(lines > state->max_change)4056 state->max_change = lines;4057if(patch->old_name) {4058int len =quote_c_style(patch->old_name, NULL, NULL,0);4059if(!len)4060 len =strlen(patch->old_name);4061if(len > state->max_len)4062 state->max_len = len;4063}4064if(patch->new_name) {4065int len =quote_c_style(patch->new_name, NULL, NULL,0);4066if(!len)4067 len =strlen(patch->new_name);4068if(len > state->max_len)4069 state->max_len = len;4070}4071}40724073static voidremove_file(struct apply_state *state,struct patch *patch,int rmdir_empty)4074{4075if(state->update_index) {4076if(remove_file_from_cache(patch->old_name) <0)4077die(_("unable to remove%sfrom index"), patch->old_name);4078}4079if(!state->cached) {4080if(!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {4081remove_path(patch->old_name);4082}4083}4084}40854086static voidadd_index_file(struct apply_state *state,4087const char*path,4088unsigned mode,4089void*buf,4090unsigned long size)4091{4092struct stat st;4093struct cache_entry *ce;4094int namelen =strlen(path);4095unsigned ce_size =cache_entry_size(namelen);40964097if(!state->update_index)4098return;40994100 ce =xcalloc(1, ce_size);4101memcpy(ce->name, path, namelen);4102 ce->ce_mode =create_ce_mode(mode);4103 ce->ce_flags =create_ce_flags(0);4104 ce->ce_namelen = namelen;4105if(S_ISGITLINK(mode)) {4106const char*s;41074108if(!skip_prefix(buf,"Subproject commit ", &s) ||4109get_sha1_hex(s, ce->sha1))4110die(_("corrupt patch for submodule%s"), path);4111}else{4112if(!state->cached) {4113if(lstat(path, &st) <0)4114die_errno(_("unable to stat newly created file '%s'"),4115 path);4116fill_stat_cache_info(ce, &st);4117}4118if(write_sha1_file(buf, size, blob_type, ce->sha1) <0)4119die(_("unable to create backing store for newly created file%s"), path);4120}4121if(add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) <0)4122die(_("unable to add cache entry for%s"), path);4123}41244125static inttry_create_file(const char*path,unsigned int mode,const char*buf,unsigned long size)4126{4127int fd;4128struct strbuf nbuf = STRBUF_INIT;41294130if(S_ISGITLINK(mode)) {4131struct stat st;4132if(!lstat(path, &st) &&S_ISDIR(st.st_mode))4133return0;4134returnmkdir(path,0777);4135}41364137if(has_symlinks &&S_ISLNK(mode))4138/* Although buf:size is counted string, it also is NUL4139 * terminated.4140 */4141returnsymlink(buf, path);41424143 fd =open(path, O_CREAT | O_EXCL | O_WRONLY, (mode &0100) ?0777:0666);4144if(fd <0)4145return-1;41464147if(convert_to_working_tree(path, buf, size, &nbuf)) {4148 size = nbuf.len;4149 buf = nbuf.buf;4150}4151write_or_die(fd, buf, size);4152strbuf_release(&nbuf);41534154if(close(fd) <0)4155die_errno(_("closing file '%s'"), path);4156return0;4157}41584159/*4160 * We optimistically assume that the directories exist,4161 * which is true 99% of the time anyway. If they don't,4162 * we create them and try again.4163 */4164static voidcreate_one_file(struct apply_state *state,4165char*path,4166unsigned mode,4167const char*buf,4168unsigned long size)4169{4170if(state->cached)4171return;4172if(!try_create_file(path, mode, buf, size))4173return;41744175if(errno == ENOENT) {4176if(safe_create_leading_directories(path))4177return;4178if(!try_create_file(path, mode, buf, size))4179return;4180}41814182if(errno == EEXIST || errno == EACCES) {4183/* We may be trying to create a file where a directory4184 * used to be.4185 */4186struct stat st;4187if(!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))4188 errno = EEXIST;4189}41904191if(errno == EEXIST) {4192unsigned int nr =getpid();41934194for(;;) {4195char newpath[PATH_MAX];4196mksnpath(newpath,sizeof(newpath),"%s~%u", path, nr);4197if(!try_create_file(newpath, mode, buf, size)) {4198if(!rename(newpath, path))4199return;4200unlink_or_warn(newpath);4201break;4202}4203if(errno != EEXIST)4204break;4205++nr;4206}4207}4208die_errno(_("unable to write file '%s' mode%o"), path, mode);4209}42104211static voidadd_conflicted_stages_file(struct apply_state *state,4212struct patch *patch)4213{4214int stage, namelen;4215unsigned ce_size, mode;4216struct cache_entry *ce;42174218if(!state->update_index)4219return;4220 namelen =strlen(patch->new_name);4221 ce_size =cache_entry_size(namelen);4222 mode = patch->new_mode ? patch->new_mode : (S_IFREG |0644);42234224remove_file_from_cache(patch->new_name);4225for(stage =1; stage <4; stage++) {4226if(is_null_oid(&patch->threeway_stage[stage -1]))4227continue;4228 ce =xcalloc(1, ce_size);4229memcpy(ce->name, patch->new_name, namelen);4230 ce->ce_mode =create_ce_mode(mode);4231 ce->ce_flags =create_ce_flags(stage);4232 ce->ce_namelen = namelen;4233hashcpy(ce->sha1, patch->threeway_stage[stage -1].hash);4234if(add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) <0)4235die(_("unable to add cache entry for%s"), patch->new_name);4236}4237}42384239static voidcreate_file(struct apply_state *state,struct patch *patch)4240{4241char*path = patch->new_name;4242unsigned mode = patch->new_mode;4243unsigned long size = patch->resultsize;4244char*buf = patch->result;42454246if(!mode)4247 mode = S_IFREG |0644;4248create_one_file(state, path, mode, buf, size);42494250if(patch->conflicted_threeway)4251add_conflicted_stages_file(state, patch);4252else4253add_index_file(state, path, mode, buf, size);4254}42554256/* phase zero is to remove, phase one is to create */4257static voidwrite_out_one_result(struct apply_state *state,4258struct patch *patch,4259int phase)4260{4261if(patch->is_delete >0) {4262if(phase ==0)4263remove_file(state, patch,1);4264return;4265}4266if(patch->is_new >0|| patch->is_copy) {4267if(phase ==1)4268create_file(state, patch);4269return;4270}4271/*4272 * Rename or modification boils down to the same4273 * thing: remove the old, write the new4274 */4275if(phase ==0)4276remove_file(state, patch, patch->is_rename);4277if(phase ==1)4278create_file(state, patch);4279}42804281static intwrite_out_one_reject(struct apply_state *state,struct patch *patch)4282{4283FILE*rej;4284char namebuf[PATH_MAX];4285struct fragment *frag;4286int cnt =0;4287struct strbuf sb = STRBUF_INIT;42884289for(cnt =0, frag = patch->fragments; frag; frag = frag->next) {4290if(!frag->rejected)4291continue;4292 cnt++;4293}42944295if(!cnt) {4296if(state->apply_verbosely)4297say_patch_name(stderr,4298_("Applied patch%scleanly."), patch);4299return0;4300}43014302/* This should not happen, because a removal patch that leaves4303 * contents are marked "rejected" at the patch level.4304 */4305if(!patch->new_name)4306die(_("internal error"));43074308/* Say this even without --verbose */4309strbuf_addf(&sb,Q_("Applying patch %%swith%dreject...",4310"Applying patch %%swith%drejects...",4311 cnt),4312 cnt);4313say_patch_name(stderr, sb.buf, patch);4314strbuf_release(&sb);43154316 cnt =strlen(patch->new_name);4317if(ARRAY_SIZE(namebuf) <= cnt +5) {4318 cnt =ARRAY_SIZE(namebuf) -5;4319warning(_("truncating .rej filename to %.*s.rej"),4320 cnt -1, patch->new_name);4321}4322memcpy(namebuf, patch->new_name, cnt);4323memcpy(namebuf + cnt,".rej",5);43244325 rej =fopen(namebuf,"w");4326if(!rej)4327returnerror(_("cannot open%s:%s"), namebuf,strerror(errno));43284329/* Normal git tools never deal with .rej, so do not pretend4330 * this is a git patch by saying --git or giving extended4331 * headers. While at it, maybe please "kompare" that wants4332 * the trailing TAB and some garbage at the end of line ;-).4333 */4334fprintf(rej,"diff a/%sb/%s\t(rejected hunks)\n",4335 patch->new_name, patch->new_name);4336for(cnt =1, frag = patch->fragments;4337 frag;4338 cnt++, frag = frag->next) {4339if(!frag->rejected) {4340fprintf_ln(stderr,_("Hunk #%dapplied cleanly."), cnt);4341continue;4342}4343fprintf_ln(stderr,_("Rejected hunk #%d."), cnt);4344fprintf(rej,"%.*s", frag->size, frag->patch);4345if(frag->patch[frag->size-1] !='\n')4346fputc('\n', rej);4347}4348fclose(rej);4349return-1;4350}43514352static intwrite_out_results(struct apply_state *state,struct patch *list)4353{4354int phase;4355int errs =0;4356struct patch *l;4357struct string_list cpath = STRING_LIST_INIT_DUP;43584359for(phase =0; phase <2; phase++) {4360 l = list;4361while(l) {4362if(l->rejected)4363 errs =1;4364else{4365write_out_one_result(state, l, phase);4366if(phase ==1) {4367if(write_out_one_reject(state, l))4368 errs =1;4369if(l->conflicted_threeway) {4370string_list_append(&cpath, l->new_name);4371 errs =1;4372}4373}4374}4375 l = l->next;4376}4377}43784379if(cpath.nr) {4380struct string_list_item *item;43814382string_list_sort(&cpath);4383for_each_string_list_item(item, &cpath)4384fprintf(stderr,"U%s\n", item->string);4385string_list_clear(&cpath,0);43864387rerere(0);4388}43894390return errs;4391}43924393static struct lock_file lock_file;43944395#define INACCURATE_EOF (1<<0)4396#define RECOUNT (1<<1)43974398/*4399 * Try to apply a patch.4400 *4401 * Returns:4402 * -128 if a bad error happened (like patch unreadable)4403 * -1 if patch did not apply and user cannot deal with it4404 * 0 if the patch applied4405 * 1 if the patch did not apply but user might fix it4406 */4407static intapply_patch(struct apply_state *state,4408int fd,4409const char*filename,4410int options)4411{4412size_t offset;4413struct strbuf buf = STRBUF_INIT;/* owns the patch text */4414struct patch *list = NULL, **listp = &list;4415int skipped_patch =0;4416int res =0;44174418 state->patch_input_file = filename;4419if(read_patch_file(&buf, fd) <0)4420return-128;4421 offset =0;4422while(offset < buf.len) {4423struct patch *patch;4424int nr;44254426 patch =xcalloc(1,sizeof(*patch));4427 patch->inaccurate_eof = !!(options & INACCURATE_EOF);4428 patch->recount = !!(options & RECOUNT);4429 nr =parse_chunk(state, buf.buf + offset, buf.len - offset, patch);4430if(nr <0) {4431free_patch(patch);4432if(nr == -128) {4433 res = -128;4434goto end;4435}4436break;4437}4438if(state->apply_in_reverse)4439reverse_patches(patch);4440if(use_patch(state, patch)) {4441patch_stats(state, patch);4442*listp = patch;4443 listp = &patch->next;4444}4445else{4446if(state->apply_verbosely)4447say_patch_name(stderr,_("Skipped patch '%s'."), patch);4448free_patch(patch);4449 skipped_patch++;4450}4451 offset += nr;4452}44534454if(!list && !skipped_patch) {4455error(_("unrecognized input"));4456 res = -128;4457goto end;4458}44594460if(state->whitespace_error && (state->ws_error_action == die_on_ws_error))4461 state->apply =0;44624463 state->update_index = state->check_index && state->apply;4464if(state->update_index && state->newfd <0)4465 state->newfd =hold_locked_index(state->lock_file,1);44664467if(state->check_index &&read_cache() <0) {4468error(_("unable to read index file"));4469 res = -128;4470goto end;4471}44724473if((state->check || state->apply) &&4474check_patch_list(state, list) <0&&4475!state->apply_with_reject) {4476 res = -1;4477goto end;4478}44794480if(state->apply &&write_out_results(state, list)) {4481/* with --3way, we still need to write the index out */4482 res = state->apply_with_reject ? -1:1;4483goto end;4484}44854486if(state->fake_ancestor)4487build_fake_ancestor(list, state->fake_ancestor);44884489if(state->diffstat)4490stat_patch_list(state, list);44914492if(state->numstat)4493numstat_patch_list(state, list);44944495if(state->summary)4496summary_patch_list(list);44974498end:4499free_patch_list(list);4500strbuf_release(&buf);4501string_list_clear(&state->fn_table,0);4502return res;4503}45044505static intoption_parse_exclude(const struct option *opt,4506const char*arg,int unset)4507{4508struct apply_state *state = opt->value;4509add_name_limit(state, arg,1);4510return0;4511}45124513static intoption_parse_include(const struct option *opt,4514const char*arg,int unset)4515{4516struct apply_state *state = opt->value;4517add_name_limit(state, arg,0);4518 state->has_include =1;4519return0;4520}45214522static intoption_parse_p(const struct option *opt,4523const char*arg,4524int unset)4525{4526struct apply_state *state = opt->value;4527 state->p_value =atoi(arg);4528 state->p_value_known =1;4529return0;4530}45314532static intoption_parse_space_change(const struct option *opt,4533const char*arg,int unset)4534{4535struct apply_state *state = opt->value;4536if(unset)4537 state->ws_ignore_action = ignore_ws_none;4538else4539 state->ws_ignore_action = ignore_ws_change;4540return0;4541}45424543static intoption_parse_whitespace(const struct option *opt,4544const char*arg,int unset)4545{4546struct apply_state *state = opt->value;4547 state->whitespace_option = arg;4548if(parse_whitespace_option(state, arg))4549exit(1);4550return0;4551}45524553static intoption_parse_directory(const struct option *opt,4554const char*arg,int unset)4555{4556struct apply_state *state = opt->value;4557strbuf_reset(&state->root);4558strbuf_addstr(&state->root, arg);4559strbuf_complete(&state->root,'/');4560return0;4561}45624563static intapply_all_patches(struct apply_state *state,4564int argc,4565const char**argv,4566int options)4567{4568int i;4569int res;4570int errs =0;4571int read_stdin =1;45724573for(i =0; i < argc; i++) {4574const char*arg = argv[i];4575int fd;45764577if(!strcmp(arg,"-")) {4578 res =apply_patch(state,0,"<stdin>", options);4579if(res <0)4580goto end;4581 errs |= res;4582 read_stdin =0;4583continue;4584}else if(0< state->prefix_length)4585 arg =prefix_filename(state->prefix,4586 state->prefix_length,4587 arg);45884589 fd =open(arg, O_RDONLY);4590if(fd <0) {4591error(_("can't open patch '%s':%s"), arg,strerror(errno));4592 res = -128;4593goto end;4594}4595 read_stdin =0;4596set_default_whitespace_mode(state);4597 res =apply_patch(state, fd, arg, options);4598close(fd);4599if(res <0)4600goto end;4601 errs |= res;4602}4603set_default_whitespace_mode(state);4604if(read_stdin) {4605 res =apply_patch(state,0,"<stdin>", options);4606if(res <0)4607goto end;4608 errs |= res;4609}46104611if(state->whitespace_error) {4612if(state->squelch_whitespace_errors &&4613 state->squelch_whitespace_errors < state->whitespace_error) {4614int squelched =4615 state->whitespace_error - state->squelch_whitespace_errors;4616warning(Q_("squelched%dwhitespace error",4617"squelched%dwhitespace errors",4618 squelched),4619 squelched);4620}4621if(state->ws_error_action == die_on_ws_error) {4622error(Q_("%dline adds whitespace errors.",4623"%dlines add whitespace errors.",4624 state->whitespace_error),4625 state->whitespace_error);4626 res = -128;4627goto end;4628}4629if(state->applied_after_fixing_ws && state->apply)4630warning("%dline%sapplied after"4631" fixing whitespace errors.",4632 state->applied_after_fixing_ws,4633 state->applied_after_fixing_ws ==1?"":"s");4634else if(state->whitespace_error)4635warning(Q_("%dline adds whitespace errors.",4636"%dlines add whitespace errors.",4637 state->whitespace_error),4638 state->whitespace_error);4639}46404641if(state->update_index) {4642 res =write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);4643if(res) {4644error(_("Unable to write new index file"));4645 res = -128;4646goto end;4647}4648 state->newfd = -1;4649}46504651return!!errs;46524653end:4654if(state->newfd >=0) {4655rollback_lock_file(state->lock_file);4656 state->newfd = -1;4657}46584659return(res == -1?1:128);4660}46614662intcmd_apply(int argc,const char**argv,const char*prefix)4663{4664int force_apply =0;4665int options =0;4666int ret;4667struct apply_state state;46684669struct option builtin_apply_options[] = {4670{ OPTION_CALLBACK,0,"exclude", &state,N_("path"),4671N_("don't apply changes matching the given path"),46720, option_parse_exclude },4673{ OPTION_CALLBACK,0,"include", &state,N_("path"),4674N_("apply changes matching the given path"),46750, option_parse_include },4676{ OPTION_CALLBACK,'p', NULL, &state,N_("num"),4677N_("remove <num> leading slashes from traditional diff paths"),46780, option_parse_p },4679OPT_BOOL(0,"no-add", &state.no_add,4680N_("ignore additions made by the patch")),4681OPT_BOOL(0,"stat", &state.diffstat,4682N_("instead of applying the patch, output diffstat for the input")),4683OPT_NOOP_NOARG(0,"allow-binary-replacement"),4684OPT_NOOP_NOARG(0,"binary"),4685OPT_BOOL(0,"numstat", &state.numstat,4686N_("show number of added and deleted lines in decimal notation")),4687OPT_BOOL(0,"summary", &state.summary,4688N_("instead of applying the patch, output a summary for the input")),4689OPT_BOOL(0,"check", &state.check,4690N_("instead of applying the patch, see if the patch is applicable")),4691OPT_BOOL(0,"index", &state.check_index,4692N_("make sure the patch is applicable to the current index")),4693OPT_BOOL(0,"cached", &state.cached,4694N_("apply a patch without touching the working tree")),4695OPT_BOOL(0,"unsafe-paths", &state.unsafe_paths,4696N_("accept a patch that touches outside the working area")),4697OPT_BOOL(0,"apply", &force_apply,4698N_("also apply the patch (use with --stat/--summary/--check)")),4699OPT_BOOL('3',"3way", &state.threeway,4700N_("attempt three-way merge if a patch does not apply")),4701OPT_FILENAME(0,"build-fake-ancestor", &state.fake_ancestor,4702N_("build a temporary index based on embedded index information")),4703/* Think twice before adding "--nul" synonym to this */4704OPT_SET_INT('z', NULL, &state.line_termination,4705N_("paths are separated with NUL character"),'\0'),4706OPT_INTEGER('C', NULL, &state.p_context,4707N_("ensure at least <n> lines of context match")),4708{ OPTION_CALLBACK,0,"whitespace", &state,N_("action"),4709N_("detect new or modified lines that have whitespace errors"),47100, option_parse_whitespace },4711{ OPTION_CALLBACK,0,"ignore-space-change", &state, NULL,4712N_("ignore changes in whitespace when finding context"),4713 PARSE_OPT_NOARG, option_parse_space_change },4714{ OPTION_CALLBACK,0,"ignore-whitespace", &state, NULL,4715N_("ignore changes in whitespace when finding context"),4716 PARSE_OPT_NOARG, option_parse_space_change },4717OPT_BOOL('R',"reverse", &state.apply_in_reverse,4718N_("apply the patch in reverse")),4719OPT_BOOL(0,"unidiff-zero", &state.unidiff_zero,4720N_("don't expect at least one line of context")),4721OPT_BOOL(0,"reject", &state.apply_with_reject,4722N_("leave the rejected hunks in corresponding *.rej files")),4723OPT_BOOL(0,"allow-overlap", &state.allow_overlap,4724N_("allow overlapping hunks")),4725OPT__VERBOSE(&state.apply_verbosely,N_("be verbose")),4726OPT_BIT(0,"inaccurate-eof", &options,4727N_("tolerate incorrectly detected missing new-line at the end of file"),4728 INACCURATE_EOF),4729OPT_BIT(0,"recount", &options,4730N_("do not trust the line counts in the hunk headers"),4731 RECOUNT),4732{ OPTION_CALLBACK,0,"directory", &state,N_("root"),4733N_("prepend <root> to all filenames"),47340, option_parse_directory },4735OPT_END()4736};47374738if(init_apply_state(&state, prefix, &lock_file))4739exit(128);47404741 argc =parse_options(argc, argv, state.prefix, builtin_apply_options,4742 apply_usage,0);47434744if(check_apply_state(&state, force_apply))4745exit(128);47464747 ret =apply_all_patches(&state, argc, argv, options);47484749clear_apply_state(&state);47504751return ret;4752}