1/* 2 * Recursive Merge algorithm stolen from git-merge-recursive.py by 3 * Fredrik Kuivinen. 4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 5 */ 6#include"cache.h" 7#include"config.h" 8#include"advice.h" 9#include"lockfile.h" 10#include"cache-tree.h" 11#include"object-store.h" 12#include"repository.h" 13#include"commit.h" 14#include"blob.h" 15#include"builtin.h" 16#include"tree-walk.h" 17#include"diff.h" 18#include"diffcore.h" 19#include"tag.h" 20#include"alloc.h" 21#include"unpack-trees.h" 22#include"string-list.h" 23#include"xdiff-interface.h" 24#include"ll-merge.h" 25#include"attr.h" 26#include"merge-recursive.h" 27#include"dir.h" 28#include"submodule.h" 29#include"revision.h" 30#include"commit-reach.h" 31 32struct path_hashmap_entry { 33struct hashmap_entry e; 34char path[FLEX_ARRAY]; 35}; 36 37static intpath_hashmap_cmp(const void*cmp_data, 38const void*entry, 39const void*entry_or_key, 40const void*keydata) 41{ 42const struct path_hashmap_entry *a = entry; 43const struct path_hashmap_entry *b = entry_or_key; 44const char*key = keydata; 45 46if(ignore_case) 47returnstrcasecmp(a->path, key ? key : b->path); 48else 49returnstrcmp(a->path, key ? key : b->path); 50} 51 52static unsigned intpath_hash(const char*path) 53{ 54return ignore_case ?strihash(path) :strhash(path); 55} 56 57static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap, 58char*dir) 59{ 60struct dir_rename_entry key; 61 62if(dir == NULL) 63return NULL; 64hashmap_entry_init(&key,strhash(dir)); 65 key.dir = dir; 66returnhashmap_get(hashmap, &key, NULL); 67} 68 69static intdir_rename_cmp(const void*unused_cmp_data, 70const void*entry, 71const void*entry_or_key, 72const void*unused_keydata) 73{ 74const struct dir_rename_entry *e1 = entry; 75const struct dir_rename_entry *e2 = entry_or_key; 76 77returnstrcmp(e1->dir, e2->dir); 78} 79 80static voiddir_rename_init(struct hashmap *map) 81{ 82hashmap_init(map, dir_rename_cmp, NULL,0); 83} 84 85static voiddir_rename_entry_init(struct dir_rename_entry *entry, 86char*directory) 87{ 88hashmap_entry_init(entry,strhash(directory)); 89 entry->dir = directory; 90 entry->non_unique_new_dir =0; 91strbuf_init(&entry->new_dir,0); 92string_list_init(&entry->possible_new_dirs,0); 93} 94 95static struct collision_entry *collision_find_entry(struct hashmap *hashmap, 96char*target_file) 97{ 98struct collision_entry key; 99 100hashmap_entry_init(&key,strhash(target_file)); 101 key.target_file = target_file; 102returnhashmap_get(hashmap, &key, NULL); 103} 104 105static intcollision_cmp(void*unused_cmp_data, 106const struct collision_entry *e1, 107const struct collision_entry *e2, 108const void*unused_keydata) 109{ 110returnstrcmp(e1->target_file, e2->target_file); 111} 112 113static voidcollision_init(struct hashmap *map) 114{ 115hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL,0); 116} 117 118static voidflush_output(struct merge_options *opt) 119{ 120if(opt->buffer_output <2&& opt->obuf.len) { 121fputs(opt->obuf.buf, stdout); 122strbuf_reset(&opt->obuf); 123} 124} 125 126static interr(struct merge_options *opt,const char*err, ...) 127{ 128va_list params; 129 130if(opt->buffer_output <2) 131flush_output(opt); 132else{ 133strbuf_complete(&opt->obuf,'\n'); 134strbuf_addstr(&opt->obuf,"error: "); 135} 136va_start(params, err); 137strbuf_vaddf(&opt->obuf, err, params); 138va_end(params); 139if(opt->buffer_output >1) 140strbuf_addch(&opt->obuf,'\n'); 141else{ 142error("%s", opt->obuf.buf); 143strbuf_reset(&opt->obuf); 144} 145 146return-1; 147} 148 149static struct tree *shift_tree_object(struct repository *repo, 150struct tree *one,struct tree *two, 151const char*subtree_shift) 152{ 153struct object_id shifted; 154 155if(!*subtree_shift) { 156shift_tree(&one->object.oid, &two->object.oid, &shifted,0); 157}else{ 158shift_tree_by(&one->object.oid, &two->object.oid, &shifted, 159 subtree_shift); 160} 161if(oideq(&two->object.oid, &shifted)) 162return two; 163returnlookup_tree(repo, &shifted); 164} 165 166static struct commit *make_virtual_commit(struct repository *repo, 167struct tree *tree, 168const char*comment) 169{ 170struct commit *commit =alloc_commit_node(repo); 171 172set_merge_remote_desc(commit, comment, (struct object *)commit); 173 commit->maybe_tree = tree; 174 commit->object.parsed =1; 175return commit; 176} 177 178/* 179 * Since we use get_tree_entry(), which does not put the read object into 180 * the object pool, we cannot rely on a == b. 181 */ 182static intoid_eq(const struct object_id *a,const struct object_id *b) 183{ 184if(!a && !b) 185return2; 186return a && b &&oideq(a, b); 187} 188 189enum rename_type { 190 RENAME_NORMAL =0, 191 RENAME_VIA_DIR, 192 RENAME_ADD, 193 RENAME_DELETE, 194 RENAME_ONE_FILE_TO_ONE, 195 RENAME_ONE_FILE_TO_TWO, 196 RENAME_TWO_FILES_TO_ONE 197}; 198 199/* 200 * Since we want to write the index eventually, we cannot reuse the index 201 * for these (temporary) data. 202 */ 203struct stage_data { 204struct{ 205unsigned short mode; 206struct object_id oid; 207} stages[4]; 208struct rename_conflict_info *rename_conflict_info; 209unsigned processed:1; 210}; 211 212struct rename { 213struct diff_filepair *pair; 214/* 215 * Purpose of src_entry and dst_entry: 216 * 217 * If 'before' is renamed to 'after' then src_entry will contain 218 * the versions of 'before' from the merge_base, HEAD, and MERGE in 219 * stages 1, 2, and 3; dst_entry will contain the respective 220 * versions of 'after' in corresponding locations. Thus, we have a 221 * total of six modes and oids, though some will be null. (Stage 0 222 * is ignored; we're interested in handling conflicts.) 223 * 224 * Since we don't turn on break-rewrites by default, neither 225 * src_entry nor dst_entry can have all three of their stages have 226 * non-null oids, meaning at most four of the six will be non-null. 227 * Also, since this is a rename, both src_entry and dst_entry will 228 * have at least one non-null oid, meaning at least two will be 229 * non-null. Of the six oids, a typical rename will have three be 230 * non-null. Only two implies a rename/delete, and four implies a 231 * rename/add. 232 */ 233struct stage_data *src_entry; 234struct stage_data *dst_entry; 235unsigned add_turned_into_rename:1; 236unsigned processed:1; 237}; 238 239struct rename_conflict_info { 240enum rename_type rename_type; 241struct rename *ren1; 242struct rename *ren2; 243const char*branch1; 244const char*branch2; 245struct diff_filespec ren1_other; 246struct diff_filespec ren2_other; 247}; 248 249staticinlinevoidsetup_rename_conflict_info(enum rename_type rename_type, 250struct merge_options *opt, 251struct rename *ren1, 252struct rename *ren2, 253const char*branch1, 254const char*branch2, 255struct stage_data *src_entry1, 256struct stage_data *src_entry2) 257{ 258int ostage1 =0, ostage2; 259struct rename_conflict_info *ci; 260 261/* 262 * When we have two renames involved, it's easiest to get the 263 * correct things into stage 2 and 3, and to make sure that the 264 * content merge puts HEAD before the other branch if we just 265 * ensure that branch1 == opt->branch1. So, simply flip arguments 266 * around if we don't have that. 267 */ 268if(ren2 && branch1 != opt->branch1) { 269setup_rename_conflict_info(rename_type, 270 opt, 271 ren2, ren1, 272 branch2, branch1, 273 src_entry2, src_entry1); 274return; 275} 276 277 ci =xcalloc(1,sizeof(struct rename_conflict_info)); 278 ci->rename_type = rename_type; 279 ci->ren1 = ren1; 280 ci->ren2 = ren2; 281 ci->branch1 = branch1; 282 ci->branch2 = branch2; 283 284 ci->ren1->dst_entry->processed =0; 285 ci->ren1->dst_entry->rename_conflict_info = ci; 286 287if(ren2) { 288 ci->ren2->dst_entry->rename_conflict_info = ci; 289} 290 291/* 292 * For each rename, there could have been 293 * modifications on the side of history where that 294 * file was not renamed. 295 */ 296if(rename_type == RENAME_ADD || 297 rename_type == RENAME_TWO_FILES_TO_ONE) { 298 ostage1 = opt->branch1 == branch1 ?3:2; 299 300 ci->ren1_other.path = ren1->pair->one->path; 301oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid); 302 ci->ren1_other.mode = src_entry1->stages[ostage1].mode; 303} 304 305if(rename_type == RENAME_TWO_FILES_TO_ONE) { 306 ostage2 = ostage1 ^1; 307 308 ci->ren2_other.path = ren2->pair->one->path; 309oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid); 310 ci->ren2_other.mode = src_entry2->stages[ostage2].mode; 311} 312} 313 314static intshow(struct merge_options *opt,int v) 315{ 316return(!opt->call_depth && opt->verbosity >= v) || opt->verbosity >=5; 317} 318 319__attribute__((format(printf,3,4))) 320static voidoutput(struct merge_options *opt,int v,const char*fmt, ...) 321{ 322va_list ap; 323 324if(!show(opt, v)) 325return; 326 327strbuf_addchars(&opt->obuf,' ', opt->call_depth *2); 328 329va_start(ap, fmt); 330strbuf_vaddf(&opt->obuf, fmt, ap); 331va_end(ap); 332 333strbuf_addch(&opt->obuf,'\n'); 334if(!opt->buffer_output) 335flush_output(opt); 336} 337 338static voidoutput_commit_title(struct merge_options *opt,struct commit *commit) 339{ 340struct merge_remote_desc *desc; 341 342strbuf_addchars(&opt->obuf,' ', opt->call_depth *2); 343 desc =merge_remote_util(commit); 344if(desc) 345strbuf_addf(&opt->obuf,"virtual%s\n", desc->name); 346else{ 347strbuf_add_unique_abbrev(&opt->obuf, &commit->object.oid, 348 DEFAULT_ABBREV); 349strbuf_addch(&opt->obuf,' '); 350if(parse_commit(commit) !=0) 351strbuf_addstr(&opt->obuf,_("(bad commit)\n")); 352else{ 353const char*title; 354const char*msg =get_commit_buffer(commit, NULL); 355int len =find_commit_subject(msg, &title); 356if(len) 357strbuf_addf(&opt->obuf,"%.*s\n", len, title); 358unuse_commit_buffer(commit, msg); 359} 360} 361flush_output(opt); 362} 363 364static intadd_cacheinfo(struct merge_options *opt, 365unsigned int mode,const struct object_id *oid, 366const char*path,int stage,int refresh,int options) 367{ 368struct index_state *istate = opt->repo->index; 369struct cache_entry *ce; 370int ret; 371 372 ce =make_cache_entry(istate, mode, oid ? oid : &null_oid, path, stage,0); 373if(!ce) 374returnerr(opt,_("add_cacheinfo failed for path '%s'; merge aborting."), path); 375 376 ret =add_index_entry(istate, ce, options); 377if(refresh) { 378struct cache_entry *nce; 379 380 nce =refresh_cache_entry(istate, ce, 381 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); 382if(!nce) 383returnerr(opt,_("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); 384if(nce != ce) 385 ret =add_index_entry(istate, nce, options); 386} 387return ret; 388} 389 390static voidinit_tree_desc_from_tree(struct tree_desc *desc,struct tree *tree) 391{ 392parse_tree(tree); 393init_tree_desc(desc, tree->buffer, tree->size); 394} 395 396static intunpack_trees_start(struct merge_options *opt, 397struct tree *common, 398struct tree *head, 399struct tree *merge) 400{ 401int rc; 402struct tree_desc t[3]; 403struct index_state tmp_index = { NULL }; 404 405memset(&opt->unpack_opts,0,sizeof(opt->unpack_opts)); 406if(opt->call_depth) 407 opt->unpack_opts.index_only =1; 408else 409 opt->unpack_opts.update =1; 410 opt->unpack_opts.merge =1; 411 opt->unpack_opts.head_idx =2; 412 opt->unpack_opts.fn = threeway_merge; 413 opt->unpack_opts.src_index = opt->repo->index; 414 opt->unpack_opts.dst_index = &tmp_index; 415 opt->unpack_opts.aggressive = !merge_detect_rename(opt); 416setup_unpack_trees_porcelain(&opt->unpack_opts,"merge"); 417 418init_tree_desc_from_tree(t+0, common); 419init_tree_desc_from_tree(t+1, head); 420init_tree_desc_from_tree(t+2, merge); 421 422 rc =unpack_trees(3, t, &opt->unpack_opts); 423cache_tree_free(&opt->repo->index->cache_tree); 424 425/* 426 * Update opt->repo->index to match the new results, AFTER saving a copy 427 * in opt->orig_index. Update src_index to point to the saved copy. 428 * (verify_uptodate() checks src_index, and the original index is 429 * the one that had the necessary modification timestamps.) 430 */ 431 opt->orig_index = *opt->repo->index; 432*opt->repo->index = tmp_index; 433 opt->unpack_opts.src_index = &opt->orig_index; 434 435return rc; 436} 437 438static voidunpack_trees_finish(struct merge_options *opt) 439{ 440discard_index(&opt->orig_index); 441clear_unpack_trees_porcelain(&opt->unpack_opts); 442} 443 444struct tree *write_tree_from_memory(struct merge_options *opt) 445{ 446struct tree *result = NULL; 447struct index_state *istate = opt->repo->index; 448 449if(unmerged_index(istate)) { 450int i; 451fprintf(stderr,"BUG: There are unmerged index entries:\n"); 452for(i =0; i < istate->cache_nr; i++) { 453const struct cache_entry *ce = istate->cache[i]; 454if(ce_stage(ce)) 455fprintf(stderr,"BUG:%d%.*s\n",ce_stage(ce), 456(int)ce_namelen(ce), ce->name); 457} 458BUG("unmerged index entries in merge-recursive.c"); 459} 460 461if(!istate->cache_tree) 462 istate->cache_tree =cache_tree(); 463 464if(!cache_tree_fully_valid(istate->cache_tree) && 465cache_tree_update(istate,0) <0) { 466err(opt,_("error building trees")); 467return NULL; 468} 469 470 result =lookup_tree(opt->repo, &istate->cache_tree->oid); 471 472return result; 473} 474 475static intsave_files_dirs(const struct object_id *oid, 476struct strbuf *base,const char*path, 477unsigned int mode,int stage,void*context) 478{ 479struct path_hashmap_entry *entry; 480int baselen = base->len; 481struct merge_options *opt = context; 482 483strbuf_addstr(base, path); 484 485FLEX_ALLOC_MEM(entry, path, base->buf, base->len); 486hashmap_entry_init(entry,path_hash(entry->path)); 487hashmap_add(&opt->current_file_dir_set, entry); 488 489strbuf_setlen(base, baselen); 490return(S_ISDIR(mode) ? READ_TREE_RECURSIVE :0); 491} 492 493static voidget_files_dirs(struct merge_options *opt,struct tree *tree) 494{ 495struct pathspec match_all; 496memset(&match_all,0,sizeof(match_all)); 497read_tree_recursive(the_repository, tree,"",0,0, 498&match_all, save_files_dirs, opt); 499} 500 501static intget_tree_entry_if_blob(const struct object_id *tree, 502const char*path, 503struct object_id *hashy, 504unsigned short*mode_o) 505{ 506int ret; 507 508 ret =get_tree_entry(tree, path, hashy, mode_o); 509if(S_ISDIR(*mode_o)) { 510oidcpy(hashy, &null_oid); 511*mode_o =0; 512} 513return ret; 514} 515 516/* 517 * Returns an index_entry instance which doesn't have to correspond to 518 * a real cache entry in Git's index. 519 */ 520static struct stage_data *insert_stage_data(const char*path, 521struct tree *o,struct tree *a,struct tree *b, 522struct string_list *entries) 523{ 524struct string_list_item *item; 525struct stage_data *e =xcalloc(1,sizeof(struct stage_data)); 526get_tree_entry_if_blob(&o->object.oid, path, 527&e->stages[1].oid, &e->stages[1].mode); 528get_tree_entry_if_blob(&a->object.oid, path, 529&e->stages[2].oid, &e->stages[2].mode); 530get_tree_entry_if_blob(&b->object.oid, path, 531&e->stages[3].oid, &e->stages[3].mode); 532 item =string_list_insert(entries, path); 533 item->util = e; 534return e; 535} 536 537/* 538 * Create a dictionary mapping file names to stage_data objects. The 539 * dictionary contains one entry for every path with a non-zero stage entry. 540 */ 541static struct string_list *get_unmerged(struct index_state *istate) 542{ 543struct string_list *unmerged =xcalloc(1,sizeof(struct string_list)); 544int i; 545 546 unmerged->strdup_strings =1; 547 548for(i =0; i < istate->cache_nr; i++) { 549struct string_list_item *item; 550struct stage_data *e; 551const struct cache_entry *ce = istate->cache[i]; 552if(!ce_stage(ce)) 553continue; 554 555 item =string_list_lookup(unmerged, ce->name); 556if(!item) { 557 item =string_list_insert(unmerged, ce->name); 558 item->util =xcalloc(1,sizeof(struct stage_data)); 559} 560 e = item->util; 561 e->stages[ce_stage(ce)].mode = ce->ce_mode; 562oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); 563} 564 565return unmerged; 566} 567 568static intstring_list_df_name_compare(const char*one,const char*two) 569{ 570int onelen =strlen(one); 571int twolen =strlen(two); 572/* 573 * Here we only care that entries for D/F conflicts are 574 * adjacent, in particular with the file of the D/F conflict 575 * appearing before files below the corresponding directory. 576 * The order of the rest of the list is irrelevant for us. 577 * 578 * To achieve this, we sort with df_name_compare and provide 579 * the mode S_IFDIR so that D/F conflicts will sort correctly. 580 * We use the mode S_IFDIR for everything else for simplicity, 581 * since in other cases any changes in their order due to 582 * sorting cause no problems for us. 583 */ 584int cmp =df_name_compare(one, onelen, S_IFDIR, 585 two, twolen, S_IFDIR); 586/* 587 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure 588 * that 'foo' comes before 'foo/bar'. 589 */ 590if(cmp) 591return cmp; 592return onelen - twolen; 593} 594 595static voidrecord_df_conflict_files(struct merge_options *opt, 596struct string_list *entries) 597{ 598/* If there is a D/F conflict and the file for such a conflict 599 * currently exists in the working tree, we want to allow it to be 600 * removed to make room for the corresponding directory if needed. 601 * The files underneath the directories of such D/F conflicts will 602 * be processed before the corresponding file involved in the D/F 603 * conflict. If the D/F directory ends up being removed by the 604 * merge, then we won't have to touch the D/F file. If the D/F 605 * directory needs to be written to the working copy, then the D/F 606 * file will simply be removed (in make_room_for_path()) to make 607 * room for the necessary paths. Note that if both the directory 608 * and the file need to be present, then the D/F file will be 609 * reinstated with a new unique name at the time it is processed. 610 */ 611struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; 612const char*last_file = NULL; 613int last_len =0; 614int i; 615 616/* 617 * If we're merging merge-bases, we don't want to bother with 618 * any working directory changes. 619 */ 620if(opt->call_depth) 621return; 622 623/* Ensure D/F conflicts are adjacent in the entries list. */ 624for(i =0; i < entries->nr; i++) { 625struct string_list_item *next = &entries->items[i]; 626string_list_append(&df_sorted_entries, next->string)->util = 627 next->util; 628} 629 df_sorted_entries.cmp = string_list_df_name_compare; 630string_list_sort(&df_sorted_entries); 631 632string_list_clear(&opt->df_conflict_file_set,1); 633for(i =0; i < df_sorted_entries.nr; i++) { 634const char*path = df_sorted_entries.items[i].string; 635int len =strlen(path); 636struct stage_data *e = df_sorted_entries.items[i].util; 637 638/* 639 * Check if last_file & path correspond to a D/F conflict; 640 * i.e. whether path is last_file+'/'+<something>. 641 * If so, record that it's okay to remove last_file to make 642 * room for path and friends if needed. 643 */ 644if(last_file && 645 len > last_len && 646memcmp(path, last_file, last_len) ==0&& 647 path[last_len] =='/') { 648string_list_insert(&opt->df_conflict_file_set, last_file); 649} 650 651/* 652 * Determine whether path could exist as a file in the 653 * working directory as a possible D/F conflict. This 654 * will only occur when it exists in stage 2 as a 655 * file. 656 */ 657if(S_ISREG(e->stages[2].mode) ||S_ISLNK(e->stages[2].mode)) { 658 last_file = path; 659 last_len = len; 660}else{ 661 last_file = NULL; 662} 663} 664string_list_clear(&df_sorted_entries,0); 665} 666 667static intupdate_stages(struct merge_options *opt,const char*path, 668const struct diff_filespec *o, 669const struct diff_filespec *a, 670const struct diff_filespec *b) 671{ 672 673/* 674 * NOTE: It is usually a bad idea to call update_stages on a path 675 * before calling update_file on that same path, since it can 676 * sometimes lead to spurious "refusing to lose untracked file..." 677 * messages from update_file (via make_room_for path via 678 * would_lose_untracked). Instead, reverse the order of the calls 679 * (executing update_file first and then update_stages). 680 */ 681int clear =1; 682int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; 683if(clear) 684if(remove_file_from_index(opt->repo->index, path)) 685return-1; 686if(o) 687if(add_cacheinfo(opt, o->mode, &o->oid, path,1,0, options)) 688return-1; 689if(a) 690if(add_cacheinfo(opt, a->mode, &a->oid, path,2,0, options)) 691return-1; 692if(b) 693if(add_cacheinfo(opt, b->mode, &b->oid, path,3,0, options)) 694return-1; 695return0; 696} 697 698static voidupdate_entry(struct stage_data *entry, 699struct diff_filespec *o, 700struct diff_filespec *a, 701struct diff_filespec *b) 702{ 703 entry->processed =0; 704 entry->stages[1].mode = o->mode; 705 entry->stages[2].mode = a->mode; 706 entry->stages[3].mode = b->mode; 707oidcpy(&entry->stages[1].oid, &o->oid); 708oidcpy(&entry->stages[2].oid, &a->oid); 709oidcpy(&entry->stages[3].oid, &b->oid); 710} 711 712static intremove_file(struct merge_options *opt,int clean, 713const char*path,int no_wd) 714{ 715int update_cache = opt->call_depth || clean; 716int update_working_directory = !opt->call_depth && !no_wd; 717 718if(update_cache) { 719if(remove_file_from_index(opt->repo->index, path)) 720return-1; 721} 722if(update_working_directory) { 723if(ignore_case) { 724struct cache_entry *ce; 725 ce =index_file_exists(opt->repo->index, path,strlen(path), 726 ignore_case); 727if(ce &&ce_stage(ce) ==0&&strcmp(path, ce->name)) 728return0; 729} 730if(remove_path(path)) 731return-1; 732} 733return0; 734} 735 736/* add a string to a strbuf, but converting "/" to "_" */ 737static voidadd_flattened_path(struct strbuf *out,const char*s) 738{ 739size_t i = out->len; 740strbuf_addstr(out, s); 741for(; i < out->len; i++) 742if(out->buf[i] =='/') 743 out->buf[i] ='_'; 744} 745 746static char*unique_path(struct merge_options *opt,const char*path,const char*branch) 747{ 748struct path_hashmap_entry *entry; 749struct strbuf newpath = STRBUF_INIT; 750int suffix =0; 751size_t base_len; 752 753strbuf_addf(&newpath,"%s~", path); 754add_flattened_path(&newpath, branch); 755 756 base_len = newpath.len; 757while(hashmap_get_from_hash(&opt->current_file_dir_set, 758path_hash(newpath.buf), newpath.buf) || 759(!opt->call_depth &&file_exists(newpath.buf))) { 760strbuf_setlen(&newpath, base_len); 761strbuf_addf(&newpath,"_%d", suffix++); 762} 763 764FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); 765hashmap_entry_init(entry,path_hash(entry->path)); 766hashmap_add(&opt->current_file_dir_set, entry); 767returnstrbuf_detach(&newpath, NULL); 768} 769 770/** 771 * Check whether a directory in the index is in the way of an incoming 772 * file. Return 1 if so. If check_working_copy is non-zero, also 773 * check the working directory. If empty_ok is non-zero, also return 774 * 0 in the case where the working-tree dir exists but is empty. 775 */ 776static intdir_in_way(struct index_state *istate,const char*path, 777int check_working_copy,int empty_ok) 778{ 779int pos; 780struct strbuf dirpath = STRBUF_INIT; 781struct stat st; 782 783strbuf_addstr(&dirpath, path); 784strbuf_addch(&dirpath,'/'); 785 786 pos =index_name_pos(istate, dirpath.buf, dirpath.len); 787 788if(pos <0) 789 pos = -1- pos; 790if(pos < istate->cache_nr && 791!strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) { 792strbuf_release(&dirpath); 793return1; 794} 795 796strbuf_release(&dirpath); 797return check_working_copy && !lstat(path, &st) &&S_ISDIR(st.st_mode) && 798!(empty_ok &&is_empty_dir(path)); 799} 800 801/* 802 * Returns whether path was tracked in the index before the merge started, 803 * and its oid and mode match the specified values 804 */ 805static intwas_tracked_and_matches(struct merge_options *opt,const char*path, 806const struct object_id *oid,unsigned mode) 807{ 808int pos =index_name_pos(&opt->orig_index, path,strlen(path)); 809struct cache_entry *ce; 810 811if(0> pos) 812/* we were not tracking this path before the merge */ 813return0; 814 815/* See if the file we were tracking before matches */ 816 ce = opt->orig_index.cache[pos]; 817return(oid_eq(&ce->oid, oid) && ce->ce_mode == mode); 818} 819 820/* 821 * Returns whether path was tracked in the index before the merge started 822 */ 823static intwas_tracked(struct merge_options *opt,const char*path) 824{ 825int pos =index_name_pos(&opt->orig_index, path,strlen(path)); 826 827if(0<= pos) 828/* we were tracking this path before the merge */ 829return1; 830 831return0; 832} 833 834static intwould_lose_untracked(struct merge_options *opt,const char*path) 835{ 836struct index_state *istate = opt->repo->index; 837 838/* 839 * This may look like it can be simplified to: 840 * return !was_tracked(opt, path) && file_exists(path) 841 * but it can't. This function needs to know whether path was in 842 * the working tree due to EITHER having been tracked in the index 843 * before the merge OR having been put into the working copy and 844 * index by unpack_trees(). Due to that either-or requirement, we 845 * check the current index instead of the original one. 846 * 847 * Note that we do not need to worry about merge-recursive itself 848 * updating the index after unpack_trees() and before calling this 849 * function, because we strictly require all code paths in 850 * merge-recursive to update the working tree first and the index 851 * second. Doing otherwise would break 852 * update_file()/would_lose_untracked(); see every comment in this 853 * file which mentions "update_stages". 854 */ 855int pos =index_name_pos(istate, path,strlen(path)); 856 857if(pos <0) 858 pos = -1- pos; 859while(pos < istate->cache_nr && 860!strcmp(path, istate->cache[pos]->name)) { 861/* 862 * If stage #0, it is definitely tracked. 863 * If it has stage #2 then it was tracked 864 * before this merge started. All other 865 * cases the path was not tracked. 866 */ 867switch(ce_stage(istate->cache[pos])) { 868case0: 869case2: 870return0; 871} 872 pos++; 873} 874returnfile_exists(path); 875} 876 877static intwas_dirty(struct merge_options *opt,const char*path) 878{ 879struct cache_entry *ce; 880int dirty =1; 881 882if(opt->call_depth || !was_tracked(opt, path)) 883return!dirty; 884 885 ce =index_file_exists(opt->unpack_opts.src_index, 886 path,strlen(path), ignore_case); 887 dirty =verify_uptodate(ce, &opt->unpack_opts) !=0; 888return dirty; 889} 890 891static intmake_room_for_path(struct merge_options *opt,const char*path) 892{ 893int status, i; 894const char*msg =_("failed to create path '%s'%s"); 895 896/* Unlink any D/F conflict files that are in the way */ 897for(i =0; i < opt->df_conflict_file_set.nr; i++) { 898const char*df_path = opt->df_conflict_file_set.items[i].string; 899size_t pathlen =strlen(path); 900size_t df_pathlen =strlen(df_path); 901if(df_pathlen < pathlen && 902 path[df_pathlen] =='/'&& 903strncmp(path, df_path, df_pathlen) ==0) { 904output(opt,3, 905_("Removing%sto make room for subdirectory\n"), 906 df_path); 907unlink(df_path); 908unsorted_string_list_delete_item(&opt->df_conflict_file_set, 909 i,0); 910break; 911} 912} 913 914/* Make sure leading directories are created */ 915 status =safe_create_leading_directories_const(path); 916if(status) { 917if(status == SCLD_EXISTS) 918/* something else exists */ 919returnerr(opt, msg, path,_(": perhaps a D/F conflict?")); 920returnerr(opt, msg, path,""); 921} 922 923/* 924 * Do not unlink a file in the work tree if we are not 925 * tracking it. 926 */ 927if(would_lose_untracked(opt, path)) 928returnerr(opt,_("refusing to lose untracked file at '%s'"), 929 path); 930 931/* Successful unlink is good.. */ 932if(!unlink(path)) 933return0; 934/* .. and so is no existing file */ 935if(errno == ENOENT) 936return0; 937/* .. but not some other error (who really cares what?) */ 938returnerr(opt, msg, path,_(": perhaps a D/F conflict?")); 939} 940 941static intupdate_file_flags(struct merge_options *opt, 942const struct object_id *oid, 943unsigned mode, 944const char*path, 945int update_cache, 946int update_wd) 947{ 948int ret =0; 949 950if(opt->call_depth) 951 update_wd =0; 952 953if(update_wd) { 954enum object_type type; 955void*buf; 956unsigned long size; 957 958if(S_ISGITLINK(mode)) { 959/* 960 * We may later decide to recursively descend into 961 * the submodule directory and update its index 962 * and/or work tree, but we do not do that now. 963 */ 964 update_wd =0; 965goto update_index; 966} 967 968 buf =read_object_file(oid, &type, &size); 969if(!buf) 970returnerr(opt,_("cannot read object%s'%s'"),oid_to_hex(oid), path); 971if(type != OBJ_BLOB) { 972 ret =err(opt,_("blob expected for%s'%s'"),oid_to_hex(oid), path); 973goto free_buf; 974} 975if(S_ISREG(mode)) { 976struct strbuf strbuf = STRBUF_INIT; 977if(convert_to_working_tree(opt->repo->index, path, buf, size, &strbuf)) { 978free(buf); 979 size = strbuf.len; 980 buf =strbuf_detach(&strbuf, NULL); 981} 982} 983 984if(make_room_for_path(opt, path) <0) { 985 update_wd =0; 986goto free_buf; 987} 988if(S_ISREG(mode) || (!has_symlinks &&S_ISLNK(mode))) { 989int fd; 990if(mode &0100) 991 mode =0777; 992else 993 mode =0666; 994 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); 995if(fd <0) { 996 ret =err(opt,_("failed to open '%s':%s"), 997 path,strerror(errno)); 998goto free_buf; 999}1000write_in_full(fd, buf, size);1001close(fd);1002}else if(S_ISLNK(mode)) {1003char*lnk =xmemdupz(buf, size);1004safe_create_leading_directories_const(path);1005unlink(path);1006if(symlink(lnk, path))1007 ret =err(opt,_("failed to symlink '%s':%s"),1008 path,strerror(errno));1009free(lnk);1010}else1011 ret =err(opt,1012_("do not know what to do with%06o%s'%s'"),1013 mode,oid_to_hex(oid), path);1014 free_buf:1015free(buf);1016}1017update_index:1018if(!ret && update_cache)1019if(add_cacheinfo(opt, mode, oid, path,0, update_wd,1020 ADD_CACHE_OK_TO_ADD))1021return-1;1022return ret;1023}10241025static intupdate_file(struct merge_options *opt,1026int clean,1027const struct object_id *oid,1028unsigned mode,1029const char*path)1030{1031returnupdate_file_flags(opt, oid, mode, path, opt->call_depth || clean, !opt->call_depth);1032}10331034/* Low level file merging, update and removal */10351036struct merge_file_info {1037struct object_id oid;1038unsigned mode;1039unsigned clean:1,1040 merge:1;1041};10421043static intmerge_3way(struct merge_options *opt,1044 mmbuffer_t *result_buf,1045const struct diff_filespec *o,1046const struct diff_filespec *a,1047const struct diff_filespec *b,1048const char*branch1,1049const char*branch2,1050const int extra_marker_size)1051{1052 mmfile_t orig, src1, src2;1053struct ll_merge_options ll_opts = {0};1054char*base_name, *name1, *name2;1055int merge_status;10561057 ll_opts.renormalize = opt->renormalize;1058 ll_opts.extra_marker_size = extra_marker_size;1059 ll_opts.xdl_opts = opt->xdl_opts;10601061if(opt->call_depth) {1062 ll_opts.virtual_ancestor =1;1063 ll_opts.variant =0;1064}else{1065switch(opt->recursive_variant) {1066case MERGE_RECURSIVE_OURS:1067 ll_opts.variant = XDL_MERGE_FAVOR_OURS;1068break;1069case MERGE_RECURSIVE_THEIRS:1070 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;1071break;1072default:1073 ll_opts.variant =0;1074break;1075}1076}10771078if(strcmp(a->path, b->path) ||1079(opt->ancestor != NULL &&strcmp(a->path, o->path) !=0)) {1080 base_name = opt->ancestor == NULL ? NULL :1081mkpathdup("%s:%s", opt->ancestor, o->path);1082 name1 =mkpathdup("%s:%s", branch1, a->path);1083 name2 =mkpathdup("%s:%s", branch2, b->path);1084}else{1085 base_name = opt->ancestor == NULL ? NULL :1086mkpathdup("%s", opt->ancestor);1087 name1 =mkpathdup("%s", branch1);1088 name2 =mkpathdup("%s", branch2);1089}10901091read_mmblob(&orig, &o->oid);1092read_mmblob(&src1, &a->oid);1093read_mmblob(&src2, &b->oid);10941095 merge_status =ll_merge(result_buf, a->path, &orig, base_name,1096&src1, name1, &src2, name2,1097 opt->repo->index, &ll_opts);10981099free(base_name);1100free(name1);1101free(name2);1102free(orig.ptr);1103free(src1.ptr);1104free(src2.ptr);1105return merge_status;1106}11071108static intfind_first_merges(struct repository *repo,1109struct object_array *result,const char*path,1110struct commit *a,struct commit *b)1111{1112int i, j;1113struct object_array merges = OBJECT_ARRAY_INIT;1114struct commit *commit;1115int contains_another;11161117char merged_revision[42];1118const char*rev_args[] = {"rev-list","--merges","--ancestry-path",1119"--all", merged_revision, NULL };1120struct rev_info revs;1121struct setup_revision_opt rev_opts;11221123memset(result,0,sizeof(struct object_array));1124memset(&rev_opts,0,sizeof(rev_opts));11251126/* get all revisions that merge commit a */1127xsnprintf(merged_revision,sizeof(merged_revision),"^%s",1128oid_to_hex(&a->object.oid));1129repo_init_revisions(repo, &revs, NULL);1130 rev_opts.submodule = path;1131/* FIXME: can't handle linked worktrees in submodules yet */1132 revs.single_worktree = path != NULL;1133setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);11341135/* save all revisions from the above list that contain b */1136if(prepare_revision_walk(&revs))1137die("revision walk setup failed");1138while((commit =get_revision(&revs)) != NULL) {1139struct object *o = &(commit->object);1140if(in_merge_bases(b, commit))1141add_object_array(o, NULL, &merges);1142}1143reset_revision_walk();11441145/* Now we've got all merges that contain a and b. Prune all1146 * merges that contain another found merge and save them in1147 * result.1148 */1149for(i =0; i < merges.nr; i++) {1150struct commit *m1 = (struct commit *) merges.objects[i].item;11511152 contains_another =0;1153for(j =0; j < merges.nr; j++) {1154struct commit *m2 = (struct commit *) merges.objects[j].item;1155if(i != j &&in_merge_bases(m2, m1)) {1156 contains_another =1;1157break;1158}1159}11601161if(!contains_another)1162add_object_array(merges.objects[i].item, NULL, result);1163}11641165object_array_clear(&merges);1166return result->nr;1167}11681169static voidprint_commit(struct commit *commit)1170{1171struct strbuf sb = STRBUF_INIT;1172struct pretty_print_context ctx = {0};1173 ctx.date_mode.type = DATE_NORMAL;1174format_commit_message(commit,"%h:%m %s", &sb, &ctx);1175fprintf(stderr,"%s\n", sb.buf);1176strbuf_release(&sb);1177}11781179static intmerge_submodule(struct merge_options *opt,1180struct object_id *result,const char*path,1181const struct object_id *base,const struct object_id *a,1182const struct object_id *b)1183{1184struct commit *commit_base, *commit_a, *commit_b;1185int parent_count;1186struct object_array merges;11871188int i;1189int search = !opt->call_depth;11901191/* store a in result in case we fail */1192oidcpy(result, a);11931194/* we can not handle deletion conflicts */1195if(is_null_oid(base))1196return0;1197if(is_null_oid(a))1198return0;1199if(is_null_oid(b))1200return0;12011202if(add_submodule_odb(path)) {1203output(opt,1,_("Failed to merge submodule%s(not checked out)"), path);1204return0;1205}12061207if(!(commit_base =lookup_commit_reference(opt->repo, base)) ||1208!(commit_a =lookup_commit_reference(opt->repo, a)) ||1209!(commit_b =lookup_commit_reference(opt->repo, b))) {1210output(opt,1,_("Failed to merge submodule%s(commits not present)"), path);1211return0;1212}12131214/* check whether both changes are forward */1215if(!in_merge_bases(commit_base, commit_a) ||1216!in_merge_bases(commit_base, commit_b)) {1217output(opt,1,_("Failed to merge submodule%s(commits don't follow merge-base)"), path);1218return0;1219}12201221/* Case #1: a is contained in b or vice versa */1222if(in_merge_bases(commit_a, commit_b)) {1223oidcpy(result, b);1224if(show(opt,3)) {1225output(opt,3,_("Fast-forwarding submodule%sto the following commit:"), path);1226output_commit_title(opt, commit_b);1227}else if(show(opt,2))1228output(opt,2,_("Fast-forwarding submodule%s"), path);1229else1230;/* no output */12311232return1;1233}1234if(in_merge_bases(commit_b, commit_a)) {1235oidcpy(result, a);1236if(show(opt,3)) {1237output(opt,3,_("Fast-forwarding submodule%sto the following commit:"), path);1238output_commit_title(opt, commit_a);1239}else if(show(opt,2))1240output(opt,2,_("Fast-forwarding submodule%s"), path);1241else1242;/* no output */12431244return1;1245}12461247/*1248 * Case #2: There are one or more merges that contain a and b in1249 * the submodule. If there is only one, then present it as a1250 * suggestion to the user, but leave it marked unmerged so the1251 * user needs to confirm the resolution.1252 */12531254/* Skip the search if makes no sense to the calling context. */1255if(!search)1256return0;12571258/* find commit which merges them */1259 parent_count =find_first_merges(opt->repo, &merges, path,1260 commit_a, commit_b);1261switch(parent_count) {1262case0:1263output(opt,1,_("Failed to merge submodule%s(merge following commits not found)"), path);1264break;12651266case1:1267output(opt,1,_("Failed to merge submodule%s(not fast-forward)"), path);1268output(opt,2,_("Found a possible merge resolution for the submodule:\n"));1269print_commit((struct commit *) merges.objects[0].item);1270output(opt,2,_(1271"If this is correct simply add it to the index "1272"for example\n"1273"by using:\n\n"1274" git update-index --cacheinfo 160000%s\"%s\"\n\n"1275"which will accept this suggestion.\n"),1276oid_to_hex(&merges.objects[0].item->oid), path);1277break;12781279default:1280output(opt,1,_("Failed to merge submodule%s(multiple merges found)"), path);1281for(i =0; i < merges.nr; i++)1282print_commit((struct commit *) merges.objects[i].item);1283}12841285object_array_clear(&merges);1286return0;1287}12881289static intmerge_mode_and_contents(struct merge_options *opt,1290const struct diff_filespec *o,1291const struct diff_filespec *a,1292const struct diff_filespec *b,1293const char*filename,1294const char*branch1,1295const char*branch2,1296const int extra_marker_size,1297struct merge_file_info *result)1298{1299if(opt->branch1 != branch1) {1300/*1301 * It's weird getting a reverse merge with HEAD on the bottom1302 * side of the conflict markers and the other branch on the1303 * top. Fix that.1304 */1305returnmerge_mode_and_contents(opt, o, b, a,1306 filename,1307 branch2, branch1,1308 extra_marker_size, result);1309}13101311 result->merge =0;1312 result->clean =1;13131314if((S_IFMT & a->mode) != (S_IFMT & b->mode)) {1315 result->clean =0;1316if(S_ISREG(a->mode)) {1317 result->mode = a->mode;1318oidcpy(&result->oid, &a->oid);1319}else{1320 result->mode = b->mode;1321oidcpy(&result->oid, &b->oid);1322}1323}else{1324if(!oid_eq(&a->oid, &o->oid) && !oid_eq(&b->oid, &o->oid))1325 result->merge =1;13261327/*1328 * Merge modes1329 */1330if(a->mode == b->mode || a->mode == o->mode)1331 result->mode = b->mode;1332else{1333 result->mode = a->mode;1334if(b->mode != o->mode) {1335 result->clean =0;1336 result->merge =1;1337}1338}13391340if(oid_eq(&a->oid, &b->oid) ||oid_eq(&a->oid, &o->oid))1341oidcpy(&result->oid, &b->oid);1342else if(oid_eq(&b->oid, &o->oid))1343oidcpy(&result->oid, &a->oid);1344else if(S_ISREG(a->mode)) {1345 mmbuffer_t result_buf;1346int ret =0, merge_status;13471348 merge_status =merge_3way(opt, &result_buf, o, a, b,1349 branch1, branch2,1350 extra_marker_size);13511352if((merge_status <0) || !result_buf.ptr)1353 ret =err(opt,_("Failed to execute internal merge"));13541355if(!ret &&1356write_object_file(result_buf.ptr, result_buf.size,1357 blob_type, &result->oid))1358 ret =err(opt,_("Unable to add%sto database"),1359 a->path);13601361free(result_buf.ptr);1362if(ret)1363return ret;1364 result->clean = (merge_status ==0);1365}else if(S_ISGITLINK(a->mode)) {1366 result->clean =merge_submodule(opt, &result->oid,1367 o->path,1368&o->oid,1369&a->oid,1370&b->oid);1371}else if(S_ISLNK(a->mode)) {1372switch(opt->recursive_variant) {1373case MERGE_RECURSIVE_NORMAL:1374oidcpy(&result->oid, &a->oid);1375if(!oid_eq(&a->oid, &b->oid))1376 result->clean =0;1377break;1378case MERGE_RECURSIVE_OURS:1379oidcpy(&result->oid, &a->oid);1380break;1381case MERGE_RECURSIVE_THEIRS:1382oidcpy(&result->oid, &b->oid);1383break;1384}1385}else1386BUG("unsupported object type in the tree");1387}13881389if(result->merge)1390output(opt,2,_("Auto-merging%s"), filename);13911392return0;1393}13941395static inthandle_rename_via_dir(struct merge_options *opt,1396struct diff_filepair *pair,1397const char*rename_branch)1398{1399/*1400 * Handle file adds that need to be renamed due to directory rename1401 * detection. This differs from handle_rename_normal, because1402 * there is no content merge to do; just move the file into the1403 * desired final location.1404 */1405const struct diff_filespec *dest = pair->two;14061407if(!opt->call_depth &&would_lose_untracked(opt, dest->path)) {1408char*alt_path =unique_path(opt, dest->path, rename_branch);14091410output(opt,1,_("Error: Refusing to lose untracked file at%s; "1411"writing to%sinstead."),1412 dest->path, alt_path);1413/*1414 * Write the file in worktree at alt_path, but not in the1415 * index. Instead, write to dest->path for the index but1416 * only at the higher appropriate stage.1417 */1418if(update_file(opt,0, &dest->oid, dest->mode, alt_path))1419return-1;1420free(alt_path);1421returnupdate_stages(opt, dest->path, NULL,1422 rename_branch == opt->branch1 ? dest : NULL,1423 rename_branch == opt->branch1 ? NULL : dest);1424}14251426/* Update dest->path both in index and in worktree */1427if(update_file(opt,1, &dest->oid, dest->mode, dest->path))1428return-1;1429return0;1430}14311432static inthandle_change_delete(struct merge_options *opt,1433const char*path,const char*old_path,1434const struct object_id *o_oid,int o_mode,1435const struct object_id *changed_oid,1436int changed_mode,1437const char*change_branch,1438const char*delete_branch,1439const char*change,const char*change_past)1440{1441char*alt_path = NULL;1442const char*update_path = path;1443int ret =0;14441445if(dir_in_way(opt->repo->index, path, !opt->call_depth,0) ||1446(!opt->call_depth &&would_lose_untracked(opt, path))) {1447 update_path = alt_path =unique_path(opt, path, change_branch);1448}14491450if(opt->call_depth) {1451/*1452 * We cannot arbitrarily accept either a_sha or b_sha as1453 * correct; since there is no true "middle point" between1454 * them, simply reuse the base version for virtual merge base.1455 */1456 ret =remove_file_from_index(opt->repo->index, path);1457if(!ret)1458 ret =update_file(opt,0, o_oid, o_mode, update_path);1459}else{1460/*1461 * Despite the four nearly duplicate messages and argument1462 * lists below and the ugliness of the nested if-statements,1463 * having complete messages makes the job easier for1464 * translators.1465 *1466 * The slight variance among the cases is due to the fact1467 * that:1468 * 1) directory/file conflicts (in effect if1469 * !alt_path) could cause us to need to write the1470 * file to a different path.1471 * 2) renames (in effect if !old_path) could mean that1472 * there are two names for the path that the user1473 * may know the file by.1474 */1475if(!alt_path) {1476if(!old_path) {1477output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1478"and%sin%s. Version%sof%sleft in tree."),1479 change, path, delete_branch, change_past,1480 change_branch, change_branch, path);1481}else{1482output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1483"and%sto%sin%s. Version%sof%sleft in tree."),1484 change, old_path, delete_branch, change_past, path,1485 change_branch, change_branch, path);1486}1487}else{1488if(!old_path) {1489output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1490"and%sin%s. Version%sof%sleft in tree at%s."),1491 change, path, delete_branch, change_past,1492 change_branch, change_branch, path, alt_path);1493}else{1494output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1495"and%sto%sin%s. Version%sof%sleft in tree at%s."),1496 change, old_path, delete_branch, change_past, path,1497 change_branch, change_branch, path, alt_path);1498}1499}1500/*1501 * No need to call update_file() on path when change_branch ==1502 * opt->branch1 && !alt_path, since that would needlessly touch1503 * path. We could call update_file_flags() with update_cache=01504 * and update_wd=0, but that's a no-op.1505 */1506if(change_branch != opt->branch1 || alt_path)1507 ret =update_file(opt,0, changed_oid, changed_mode, update_path);1508}1509free(alt_path);15101511return ret;1512}15131514static inthandle_rename_delete(struct merge_options *opt,1515struct diff_filepair *pair,1516const char*rename_branch,1517const char*delete_branch)1518{1519const struct diff_filespec *orig = pair->one;1520const struct diff_filespec *dest = pair->two;15211522if(handle_change_delete(opt,1523 opt->call_depth ? orig->path : dest->path,1524 opt->call_depth ? NULL : orig->path,1525&orig->oid, orig->mode,1526&dest->oid, dest->mode,1527 rename_branch, delete_branch,1528_("rename"),_("renamed")))1529return-1;15301531if(opt->call_depth)1532returnremove_file_from_index(opt->repo->index, dest->path);1533else1534returnupdate_stages(opt, dest->path, NULL,1535 rename_branch == opt->branch1 ? dest : NULL,1536 rename_branch == opt->branch1 ? NULL : dest);1537}15381539static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1540struct stage_data *entry,1541int stage)1542{1543struct object_id *oid = &entry->stages[stage].oid;1544unsigned mode = entry->stages[stage].mode;1545if(mode ==0||is_null_oid(oid))1546return NULL;1547oidcpy(&target->oid, oid);1548 target->mode = mode;1549return target;1550}15511552static inthandle_file_collision(struct merge_options *opt,1553const char*collide_path,1554const char*prev_path1,1555const char*prev_path2,1556const char*branch1,const char*branch2,1557const struct object_id *a_oid,1558unsigned int a_mode,1559const struct object_id *b_oid,1560unsigned int b_mode)1561{1562struct merge_file_info mfi;1563struct diff_filespec null, a, b;1564char*alt_path = NULL;1565const char*update_path = collide_path;15661567/*1568 * It's easiest to get the correct things into stage 2 and 3, and1569 * to make sure that the content merge puts HEAD before the other1570 * branch if we just ensure that branch1 == opt->branch1. So, simply1571 * flip arguments around if we don't have that.1572 */1573if(branch1 != opt->branch1) {1574returnhandle_file_collision(opt, collide_path,1575 prev_path2, prev_path1,1576 branch2, branch1,1577 b_oid, b_mode,1578 a_oid, a_mode);1579}15801581/*1582 * In the recursive case, we just opt to undo renames1583 */1584if(opt->call_depth && (prev_path1 || prev_path2)) {1585/* Put first file (a_oid, a_mode) in its original spot */1586if(prev_path1) {1587if(update_file(opt,1, a_oid, a_mode, prev_path1))1588return-1;1589}else{1590if(update_file(opt,1, a_oid, a_mode, collide_path))1591return-1;1592}15931594/* Put second file (b_oid, b_mode) in its original spot */1595if(prev_path2) {1596if(update_file(opt,1, b_oid, b_mode, prev_path2))1597return-1;1598}else{1599if(update_file(opt,1, b_oid, b_mode, collide_path))1600return-1;1601}16021603/* Don't leave something at collision path if unrenaming both */1604if(prev_path1 && prev_path2)1605remove_file(opt,1, collide_path,0);16061607return0;1608}16091610/* Remove rename sources if rename/add or rename/rename(2to1) */1611if(prev_path1)1612remove_file(opt,1, prev_path1,1613 opt->call_depth ||would_lose_untracked(opt, prev_path1));1614if(prev_path2)1615remove_file(opt,1, prev_path2,1616 opt->call_depth ||would_lose_untracked(opt, prev_path2));16171618/*1619 * Remove the collision path, if it wouldn't cause dirty contents1620 * or an untracked file to get lost. We'll either overwrite with1621 * merged contents, or just write out to differently named files.1622 */1623if(was_dirty(opt, collide_path)) {1624output(opt,1,_("Refusing to lose dirty file at%s"),1625 collide_path);1626 update_path = alt_path =unique_path(opt, collide_path,"merged");1627}else if(would_lose_untracked(opt, collide_path)) {1628/*1629 * Only way we get here is if both renames were from1630 * a directory rename AND user had an untracked file1631 * at the location where both files end up after the1632 * two directory renames. See testcase 10d of t6043.1633 */1634output(opt,1,_("Refusing to lose untracked file at "1635"%s, even though it's in the way."),1636 collide_path);1637 update_path = alt_path =unique_path(opt, collide_path,"merged");1638}else{1639/*1640 * FIXME: It's possible that the two files are identical1641 * and that the current working copy happens to match, in1642 * which case we are unnecessarily touching the working1643 * tree file. It's not a likely enough scenario that I1644 * want to code up the checks for it and a better fix is1645 * available if we restructure how unpack_trees() and1646 * merge-recursive interoperate anyway, so punting for1647 * now...1648 */1649remove_file(opt,0, collide_path,0);1650}16511652/* Store things in diff_filespecs for functions that need it */1653memset(&a,0,sizeof(struct diff_filespec));1654memset(&b,0,sizeof(struct diff_filespec));1655 null.path = a.path = b.path = (char*)collide_path;1656oidcpy(&null.oid, &null_oid);1657 null.mode =0;1658oidcpy(&a.oid, a_oid);1659 a.mode = a_mode;1660 a.oid_valid =1;1661oidcpy(&b.oid, b_oid);1662 b.mode = b_mode;1663 b.oid_valid =1;16641665if(merge_mode_and_contents(opt, &null, &a, &b, collide_path,1666 branch1, branch2, opt->call_depth *2, &mfi))1667return-1;1668 mfi.clean &= !alt_path;1669if(update_file(opt, mfi.clean, &mfi.oid, mfi.mode, update_path))1670return-1;1671if(!mfi.clean && !opt->call_depth &&1672update_stages(opt, collide_path, NULL, &a, &b))1673return-1;1674free(alt_path);1675/*1676 * FIXME: If both a & b both started with conflicts (only possible1677 * if they came from a rename/rename(2to1)), but had IDENTICAL1678 * contents including those conflicts, then in the next line we claim1679 * it was clean. If someone cares about this case, we should have the1680 * caller notify us if we started with conflicts.1681 */1682return mfi.clean;1683}16841685static inthandle_rename_add(struct merge_options *opt,1686struct rename_conflict_info *ci)1687{1688/* a was renamed to c, and a separate c was added. */1689struct diff_filespec *a = ci->ren1->pair->one;1690struct diff_filespec *c = ci->ren1->pair->two;1691char*path = c->path;1692char*prev_path_desc;1693struct merge_file_info mfi;16941695int other_stage = (ci->branch1 == opt->branch1 ?3:2);16961697output(opt,1,_("CONFLICT (rename/add): "1698"Rename%s->%sin%s. Added%sin%s"),1699 a->path, c->path, ci->branch1,1700 c->path, ci->branch2);17011702 prev_path_desc =xstrfmt("version of%sfrom%s", path, a->path);1703if(merge_mode_and_contents(opt, a, c, &ci->ren1_other, prev_path_desc,1704 opt->branch1, opt->branch2,17051+ opt->call_depth *2, &mfi))1706return-1;1707free(prev_path_desc);17081709returnhandle_file_collision(opt,1710 c->path, a->path, NULL,1711 ci->branch1, ci->branch2,1712&mfi.oid, mfi.mode,1713&ci->ren1->dst_entry->stages[other_stage].oid,1714 ci->ren1->dst_entry->stages[other_stage].mode);1715}17161717static char*find_path_for_conflict(struct merge_options *opt,1718const char*path,1719const char*branch1,1720const char*branch2)1721{1722char*new_path = NULL;1723if(dir_in_way(opt->repo->index, path, !opt->call_depth,0)) {1724 new_path =unique_path(opt, path, branch1);1725output(opt,1,_("%sis a directory in%sadding "1726"as%sinstead"),1727 path, branch2, new_path);1728}else if(would_lose_untracked(opt, path)) {1729 new_path =unique_path(opt, path, branch1);1730output(opt,1,_("Refusing to lose untracked file"1731" at%s; adding as%sinstead"),1732 path, new_path);1733}17341735return new_path;1736}17371738static inthandle_rename_rename_1to2(struct merge_options *opt,1739struct rename_conflict_info *ci)1740{1741/* One file was renamed in both branches, but to different names. */1742struct merge_file_info mfi;1743struct diff_filespec other;1744struct diff_filespec *add;1745struct diff_filespec *o = ci->ren1->pair->one;1746struct diff_filespec *a = ci->ren1->pair->two;1747struct diff_filespec *b = ci->ren2->pair->two;1748char*path_desc;17491750output(opt,1,_("CONFLICT (rename/rename): "1751"Rename\"%s\"->\"%s\"in branch\"%s\""1752"rename\"%s\"->\"%s\"in\"%s\"%s"),1753 o->path, a->path, ci->branch1,1754 o->path, b->path, ci->branch2,1755 opt->call_depth ?_(" (left unresolved)") :"");17561757 path_desc =xstrfmt("%sand%s, both renamed from%s",1758 a->path, b->path, o->path);1759if(merge_mode_and_contents(opt, o, a, b, path_desc,1760 ci->branch1, ci->branch2,1761 opt->call_depth *2, &mfi))1762return-1;1763free(path_desc);17641765if(opt->call_depth) {1766/*1767 * FIXME: For rename/add-source conflicts (if we could detect1768 * such), this is wrong. We should instead find a unique1769 * pathname and then either rename the add-source file to that1770 * unique path, or use that unique path instead of src here.1771 */1772if(update_file(opt,0, &mfi.oid, mfi.mode, o->path))1773return-1;17741775/*1776 * Above, we put the merged content at the merge-base's1777 * path. Now we usually need to delete both a->path and1778 * b->path. However, the rename on each side of the merge1779 * could also be involved in a rename/add conflict. In1780 * such cases, we should keep the added file around,1781 * resolving the conflict at that path in its favor.1782 */1783 add =filespec_from_entry(&other, ci->ren1->dst_entry,2^1);1784if(add) {1785if(update_file(opt,0, &add->oid, add->mode, a->path))1786return-1;1787}1788else1789remove_file_from_index(opt->repo->index, a->path);1790 add =filespec_from_entry(&other, ci->ren2->dst_entry,3^1);1791if(add) {1792if(update_file(opt,0, &add->oid, add->mode, b->path))1793return-1;1794}1795else1796remove_file_from_index(opt->repo->index, b->path);1797}else{1798/*1799 * For each destination path, we need to see if there is a1800 * rename/add collision. If not, we can write the file out1801 * to the specified location.1802 */1803 add =filespec_from_entry(&other, ci->ren1->dst_entry,2^1);1804if(add) {1805if(handle_file_collision(opt, a->path,1806 NULL, NULL,1807 ci->branch1, ci->branch2,1808&mfi.oid, mfi.mode,1809&add->oid, add->mode) <0)1810return-1;1811}else{1812char*new_path =find_path_for_conflict(opt, a->path,1813 ci->branch1,1814 ci->branch2);1815if(update_file(opt,0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))1816return-1;1817free(new_path);1818if(update_stages(opt, a->path, NULL, a, NULL))1819return-1;1820}18211822 add =filespec_from_entry(&other, ci->ren2->dst_entry,3^1);1823if(add) {1824if(handle_file_collision(opt, b->path,1825 NULL, NULL,1826 ci->branch1, ci->branch2,1827&add->oid, add->mode,1828&mfi.oid, mfi.mode) <0)1829return-1;1830}else{1831char*new_path =find_path_for_conflict(opt, b->path,1832 ci->branch2,1833 ci->branch1);1834if(update_file(opt,0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))1835return-1;1836free(new_path);1837if(update_stages(opt, b->path, NULL, NULL, b))1838return-1;1839}1840}18411842return0;1843}18441845static inthandle_rename_rename_2to1(struct merge_options *opt,1846struct rename_conflict_info *ci)1847{1848/* Two files, a & b, were renamed to the same thing, c. */1849struct diff_filespec *a = ci->ren1->pair->one;1850struct diff_filespec *b = ci->ren2->pair->one;1851struct diff_filespec *c1 = ci->ren1->pair->two;1852struct diff_filespec *c2 = ci->ren2->pair->two;1853char*path = c1->path;/* == c2->path */1854char*path_side_1_desc;1855char*path_side_2_desc;1856struct merge_file_info mfi_c1;1857struct merge_file_info mfi_c2;18581859output(opt,1,_("CONFLICT (rename/rename): "1860"Rename%s->%sin%s. "1861"Rename%s->%sin%s"),1862 a->path, c1->path, ci->branch1,1863 b->path, c2->path, ci->branch2);18641865 path_side_1_desc =xstrfmt("version of%sfrom%s", path, a->path);1866 path_side_2_desc =xstrfmt("version of%sfrom%s", path, b->path);1867if(merge_mode_and_contents(opt, a, c1, &ci->ren1_other, path_side_1_desc,1868 opt->branch1, opt->branch2,18691+ opt->call_depth *2, &mfi_c1) ||1870merge_mode_and_contents(opt, b, &ci->ren2_other, c2, path_side_2_desc,1871 opt->branch1, opt->branch2,18721+ opt->call_depth *2, &mfi_c2))1873return-1;1874free(path_side_1_desc);1875free(path_side_2_desc);18761877returnhandle_file_collision(opt, path, a->path, b->path,1878 ci->branch1, ci->branch2,1879&mfi_c1.oid, mfi_c1.mode,1880&mfi_c2.oid, mfi_c2.mode);1881}18821883/*1884 * Get the diff_filepairs changed between o_tree and tree.1885 */1886static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,1887struct tree *o_tree,1888struct tree *tree)1889{1890struct diff_queue_struct *ret;1891struct diff_options opts;18921893repo_diff_setup(opt->repo, &opts);1894 opts.flags.recursive =1;1895 opts.flags.rename_empty =0;1896 opts.detect_rename =merge_detect_rename(opt);1897/*1898 * We do not have logic to handle the detection of copies. In1899 * fact, it may not even make sense to add such logic: would we1900 * really want a change to a base file to be propagated through1901 * multiple other files by a merge?1902 */1903if(opts.detect_rename > DIFF_DETECT_RENAME)1904 opts.detect_rename = DIFF_DETECT_RENAME;1905 opts.rename_limit = opt->merge_rename_limit >=0? opt->merge_rename_limit :1906 opt->diff_rename_limit >=0? opt->diff_rename_limit :19071000;1908 opts.rename_score = opt->rename_score;1909 opts.show_rename_progress = opt->show_rename_progress;1910 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1911diff_setup_done(&opts);1912diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1913diffcore_std(&opts);1914if(opts.needed_rename_limit > opt->needed_rename_limit)1915 opt->needed_rename_limit = opts.needed_rename_limit;19161917 ret =xmalloc(sizeof(*ret));1918*ret = diff_queued_diff;19191920 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1921 diff_queued_diff.nr =0;1922 diff_queued_diff.queue = NULL;1923diff_flush(&opts);1924return ret;1925}19261927static inttree_has_path(struct tree *tree,const char*path)1928{1929struct object_id hashy;1930unsigned short mode_o;19311932return!get_tree_entry(&tree->object.oid, path,1933&hashy, &mode_o);1934}19351936/*1937 * Return a new string that replaces the beginning portion (which matches1938 * entry->dir), with entry->new_dir. In perl-speak:1939 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1940 * NOTE:1941 * Caller must ensure that old_path starts with entry->dir + '/'.1942 */1943static char*apply_dir_rename(struct dir_rename_entry *entry,1944const char*old_path)1945{1946struct strbuf new_path = STRBUF_INIT;1947int oldlen, newlen;19481949if(entry->non_unique_new_dir)1950return NULL;19511952 oldlen =strlen(entry->dir);1953 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1954strbuf_grow(&new_path, newlen);1955strbuf_addbuf(&new_path, &entry->new_dir);1956strbuf_addstr(&new_path, &old_path[oldlen]);19571958returnstrbuf_detach(&new_path, NULL);1959}19601961static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1962char**old_dir,char**new_dir)1963{1964char*end_of_old, *end_of_new;1965int old_len, new_len;19661967*old_dir = NULL;1968*new_dir = NULL;19691970/*1971 * For1972 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1973 * the "e/foo.c" part is the same, we just want to know that1974 * "a/b/c/d" was renamed to "a/b/some/thing/else"1975 * so, for this example, this function returns "a/b/c/d" in1976 * *old_dir and "a/b/some/thing/else" in *new_dir.1977 *1978 * Also, if the basename of the file changed, we don't care. We1979 * want to know which portion of the directory, if any, changed.1980 */1981 end_of_old =strrchr(old_path,'/');1982 end_of_new =strrchr(new_path,'/');19831984if(end_of_old == NULL || end_of_new == NULL)1985return;1986while(*--end_of_new == *--end_of_old &&1987 end_of_old != old_path &&1988 end_of_new != new_path)1989;/* Do nothing; all in the while loop */1990/*1991 * We've found the first non-matching character in the directory1992 * paths. That means the current directory we were comparing1993 * represents the rename. Move end_of_old and end_of_new back1994 * to the full directory name.1995 */1996if(*end_of_old =='/')1997 end_of_old++;1998if(*end_of_old !='/')1999 end_of_new++;2000 end_of_old =strchr(end_of_old,'/');2001 end_of_new =strchr(end_of_new,'/');20022003/*2004 * It may have been the case that old_path and new_path were the same2005 * directory all along. Don't claim a rename if they're the same.2006 */2007 old_len = end_of_old - old_path;2008 new_len = end_of_new - new_path;20092010if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {2011*old_dir =xstrndup(old_path, old_len);2012*new_dir =xstrndup(new_path, new_len);2013}2014}20152016static voidremove_hashmap_entries(struct hashmap *dir_renames,2017struct string_list *items_to_remove)2018{2019int i;2020struct dir_rename_entry *entry;20212022for(i =0; i < items_to_remove->nr; i++) {2023 entry = items_to_remove->items[i].util;2024hashmap_remove(dir_renames, entry, NULL);2025}2026string_list_clear(items_to_remove,0);2027}20282029/*2030 * See if there is a directory rename for path, and if there are any file2031 * level conflicts for the renamed location. If there is a rename and2032 * there are no conflicts, return the new name. Otherwise, return NULL.2033 */2034static char*handle_path_level_conflicts(struct merge_options *opt,2035const char*path,2036struct dir_rename_entry *entry,2037struct hashmap *collisions,2038struct tree *tree)2039{2040char*new_path = NULL;2041struct collision_entry *collision_ent;2042int clean =1;2043struct strbuf collision_paths = STRBUF_INIT;20442045/*2046 * entry has the mapping of old directory name to new directory name2047 * that we want to apply to path.2048 */2049 new_path =apply_dir_rename(entry, path);20502051if(!new_path) {2052/* This should only happen when entry->non_unique_new_dir set */2053if(!entry->non_unique_new_dir)2054BUG("entry->non_unqiue_dir not set and !new_path");2055output(opt,1,_("CONFLICT (directory rename split): "2056"Unclear where to place%sbecause directory "2057"%swas renamed to multiple other directories, "2058"with no destination getting a majority of the "2059"files."),2060 path, entry->dir);2061 clean =0;2062return NULL;2063}20642065/*2066 * The caller needs to have ensured that it has pre-populated2067 * collisions with all paths that map to new_path. Do a quick check2068 * to ensure that's the case.2069 */2070 collision_ent =collision_find_entry(collisions, new_path);2071if(collision_ent == NULL)2072BUG("collision_ent is NULL");20732074/*2075 * Check for one-sided add/add/.../add conflicts, i.e.2076 * where implicit renames from the other side doing2077 * directory rename(s) can affect this side of history2078 * to put multiple paths into the same location. Warn2079 * and bail on directory renames for such paths.2080 */2081if(collision_ent->reported_already) {2082 clean =0;2083}else if(tree_has_path(tree, new_path)) {2084 collision_ent->reported_already =1;2085strbuf_add_separated_string_list(&collision_paths,", ",2086&collision_ent->source_files);2087output(opt,1,_("CONFLICT (implicit dir rename): Existing "2088"file/dir at%sin the way of implicit "2089"directory rename(s) putting the following "2090"path(s) there:%s."),2091 new_path, collision_paths.buf);2092 clean =0;2093}else if(collision_ent->source_files.nr >1) {2094 collision_ent->reported_already =1;2095strbuf_add_separated_string_list(&collision_paths,", ",2096&collision_ent->source_files);2097output(opt,1,_("CONFLICT (implicit dir rename): Cannot map "2098"more than one path to%s; implicit directory "2099"renames tried to put these paths there:%s"),2100 new_path, collision_paths.buf);2101 clean =0;2102}21032104/* Free memory we no longer need */2105strbuf_release(&collision_paths);2106if(!clean && new_path) {2107free(new_path);2108return NULL;2109}21102111return new_path;2112}21132114/*2115 * There are a couple things we want to do at the directory level:2116 * 1. Check for both sides renaming to the same thing, in order to avoid2117 * implicit renaming of files that should be left in place. (See2118 * testcase 6b in t6043 for details.)2119 * 2. Prune directory renames if there are still files left in the2120 * the original directory. These represent a partial directory rename,2121 * i.e. a rename where only some of the files within the directory2122 * were renamed elsewhere. (Technically, this could be done earlier2123 * in get_directory_renames(), except that would prevent us from2124 * doing the previous check and thus failing testcase 6b.)2125 * 3. Check for rename/rename(1to2) conflicts (at the directory level).2126 * In the future, we could potentially record this info as well and2127 * omit reporting rename/rename(1to2) conflicts for each path within2128 * the affected directories, thus cleaning up the merge output.2129 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the2130 * directory level, because merging directories is fine. If it2131 * causes conflicts for files within those merged directories, then2132 * that should be detected at the individual path level.2133 */2134static voidhandle_directory_level_conflicts(struct merge_options *opt,2135struct hashmap *dir_re_head,2136struct tree *head,2137struct hashmap *dir_re_merge,2138struct tree *merge)2139{2140struct hashmap_iter iter;2141struct dir_rename_entry *head_ent;2142struct dir_rename_entry *merge_ent;21432144struct string_list remove_from_head = STRING_LIST_INIT_NODUP;2145struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;21462147hashmap_iter_init(dir_re_head, &iter);2148while((head_ent =hashmap_iter_next(&iter))) {2149 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);2150if(merge_ent &&2151!head_ent->non_unique_new_dir &&2152!merge_ent->non_unique_new_dir &&2153!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {2154/* 1. Renamed identically; remove it from both sides */2155string_list_append(&remove_from_head,2156 head_ent->dir)->util = head_ent;2157strbuf_release(&head_ent->new_dir);2158string_list_append(&remove_from_merge,2159 merge_ent->dir)->util = merge_ent;2160strbuf_release(&merge_ent->new_dir);2161}else if(tree_has_path(head, head_ent->dir)) {2162/* 2. This wasn't a directory rename after all */2163string_list_append(&remove_from_head,2164 head_ent->dir)->util = head_ent;2165strbuf_release(&head_ent->new_dir);2166}2167}21682169remove_hashmap_entries(dir_re_head, &remove_from_head);2170remove_hashmap_entries(dir_re_merge, &remove_from_merge);21712172hashmap_iter_init(dir_re_merge, &iter);2173while((merge_ent =hashmap_iter_next(&iter))) {2174 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);2175if(tree_has_path(merge, merge_ent->dir)) {2176/* 2. This wasn't a directory rename after all */2177string_list_append(&remove_from_merge,2178 merge_ent->dir)->util = merge_ent;2179}else if(head_ent &&2180!head_ent->non_unique_new_dir &&2181!merge_ent->non_unique_new_dir) {2182/* 3. rename/rename(1to2) */2183/*2184 * We can assume it's not rename/rename(1to1) because2185 * that was case (1), already checked above. So we2186 * know that head_ent->new_dir and merge_ent->new_dir2187 * are different strings.2188 */2189output(opt,1,_("CONFLICT (rename/rename): "2190"Rename directory%s->%sin%s. "2191"Rename directory%s->%sin%s"),2192 head_ent->dir, head_ent->new_dir.buf, opt->branch1,2193 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);2194string_list_append(&remove_from_head,2195 head_ent->dir)->util = head_ent;2196strbuf_release(&head_ent->new_dir);2197string_list_append(&remove_from_merge,2198 merge_ent->dir)->util = merge_ent;2199strbuf_release(&merge_ent->new_dir);2200}2201}22022203remove_hashmap_entries(dir_re_head, &remove_from_head);2204remove_hashmap_entries(dir_re_merge, &remove_from_merge);2205}22062207static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)2208{2209struct hashmap *dir_renames;2210struct hashmap_iter iter;2211struct dir_rename_entry *entry;2212int i;22132214/*2215 * Typically, we think of a directory rename as all files from a2216 * certain directory being moved to a target directory. However,2217 * what if someone first moved two files from the original2218 * directory in one commit, and then renamed the directory2219 * somewhere else in a later commit? At merge time, we just know2220 * that files from the original directory went to two different2221 * places, and that the bulk of them ended up in the same place.2222 * We want each directory rename to represent where the bulk of the2223 * files from that directory end up; this function exists to find2224 * where the bulk of the files went.2225 *2226 * The first loop below simply iterates through the list of file2227 * renames, finding out how often each directory rename pair2228 * possibility occurs.2229 */2230 dir_renames =xmalloc(sizeof(*dir_renames));2231dir_rename_init(dir_renames);2232for(i =0; i < pairs->nr; ++i) {2233struct string_list_item *item;2234int*count;2235struct diff_filepair *pair = pairs->queue[i];2236char*old_dir, *new_dir;22372238/* File not part of directory rename if it wasn't renamed */2239if(pair->status !='R')2240continue;22412242get_renamed_dir_portion(pair->one->path, pair->two->path,2243&old_dir, &new_dir);2244if(!old_dir)2245/* Directory didn't change at all; ignore this one. */2246continue;22472248 entry =dir_rename_find_entry(dir_renames, old_dir);2249if(!entry) {2250 entry =xmalloc(sizeof(*entry));2251dir_rename_entry_init(entry, old_dir);2252hashmap_put(dir_renames, entry);2253}else{2254free(old_dir);2255}2256 item =string_list_lookup(&entry->possible_new_dirs, new_dir);2257if(!item) {2258 item =string_list_insert(&entry->possible_new_dirs,2259 new_dir);2260 item->util =xcalloc(1,sizeof(int));2261}else{2262free(new_dir);2263}2264 count = item->util;2265*count +=1;2266}22672268/*2269 * For each directory with files moved out of it, we find out which2270 * target directory received the most files so we can declare it to2271 * be the "winning" target location for the directory rename. This2272 * winner gets recorded in new_dir. If there is no winner2273 * (multiple target directories received the same number of files),2274 * we set non_unique_new_dir. Once we've determined the winner (or2275 * that there is no winner), we no longer need possible_new_dirs.2276 */2277hashmap_iter_init(dir_renames, &iter);2278while((entry =hashmap_iter_next(&iter))) {2279int max =0;2280int bad_max =0;2281char*best = NULL;22822283for(i =0; i < entry->possible_new_dirs.nr; i++) {2284int*count = entry->possible_new_dirs.items[i].util;22852286if(*count == max)2287 bad_max = max;2288else if(*count > max) {2289 max = *count;2290 best = entry->possible_new_dirs.items[i].string;2291}2292}2293if(bad_max == max)2294 entry->non_unique_new_dir =1;2295else{2296assert(entry->new_dir.len ==0);2297strbuf_addstr(&entry->new_dir, best);2298}2299/*2300 * The relevant directory sub-portion of the original full2301 * filepaths were xstrndup'ed before inserting into2302 * possible_new_dirs, and instead of manually iterating the2303 * list and free'ing each, just lie and tell2304 * possible_new_dirs that it did the strdup'ing so that it2305 * will free them for us.2306 */2307 entry->possible_new_dirs.strdup_strings =1;2308string_list_clear(&entry->possible_new_dirs,1);2309}23102311return dir_renames;2312}23132314static struct dir_rename_entry *check_dir_renamed(const char*path,2315struct hashmap *dir_renames)2316{2317char*temp =xstrdup(path);2318char*end;2319struct dir_rename_entry *entry = NULL;23202321while((end =strrchr(temp,'/'))) {2322*end ='\0';2323 entry =dir_rename_find_entry(dir_renames, temp);2324if(entry)2325break;2326}2327free(temp);2328return entry;2329}23302331static voidcompute_collisions(struct hashmap *collisions,2332struct hashmap *dir_renames,2333struct diff_queue_struct *pairs)2334{2335int i;23362337/*2338 * Multiple files can be mapped to the same path due to directory2339 * renames done by the other side of history. Since that other2340 * side of history could have merged multiple directories into one,2341 * if our side of history added the same file basename to each of2342 * those directories, then all N of them would get implicitly2343 * renamed by the directory rename detection into the same path,2344 * and we'd get an add/add/.../add conflict, and all those adds2345 * from *this* side of history. This is not representable in the2346 * index, and users aren't going to easily be able to make sense of2347 * it. So we need to provide a good warning about what's2348 * happening, and fall back to no-directory-rename detection2349 * behavior for those paths.2350 *2351 * See testcases 9e and all of section 5 from t6043 for examples.2352 */2353collision_init(collisions);23542355for(i =0; i < pairs->nr; ++i) {2356struct dir_rename_entry *dir_rename_ent;2357struct collision_entry *collision_ent;2358char*new_path;2359struct diff_filepair *pair = pairs->queue[i];23602361if(pair->status !='A'&& pair->status !='R')2362continue;2363 dir_rename_ent =check_dir_renamed(pair->two->path,2364 dir_renames);2365if(!dir_rename_ent)2366continue;23672368 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);2369if(!new_path)2370/*2371 * dir_rename_ent->non_unique_new_path is true, which2372 * means there is no directory rename for us to use,2373 * which means it won't cause us any additional2374 * collisions.2375 */2376continue;2377 collision_ent =collision_find_entry(collisions, new_path);2378if(!collision_ent) {2379 collision_ent =xcalloc(1,2380sizeof(struct collision_entry));2381hashmap_entry_init(collision_ent,strhash(new_path));2382hashmap_put(collisions, collision_ent);2383 collision_ent->target_file = new_path;2384}else{2385free(new_path);2386}2387string_list_insert(&collision_ent->source_files,2388 pair->two->path);2389}2390}23912392static char*check_for_directory_rename(struct merge_options *opt,2393const char*path,2394struct tree *tree,2395struct hashmap *dir_renames,2396struct hashmap *dir_rename_exclusions,2397struct hashmap *collisions,2398int*clean_merge)2399{2400char*new_path = NULL;2401struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2402struct dir_rename_entry *oentry = NULL;24032404if(!entry)2405return new_path;24062407/*2408 * This next part is a little weird. We do not want to do an2409 * implicit rename into a directory we renamed on our side, because2410 * that will result in a spurious rename/rename(1to2) conflict. An2411 * example:2412 * Base commit: dumbdir/afile, otherdir/bfile2413 * Side 1: smrtdir/afile, otherdir/bfile2414 * Side 2: dumbdir/afile, dumbdir/bfile2415 * Here, while working on Side 1, we could notice that otherdir was2416 * renamed/merged to dumbdir, and change the diff_filepair for2417 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2418 * 2 will notice the rename from dumbdir to smrtdir, and do the2419 * transitive rename to move it from dumbdir/bfile to2420 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2421 * smrtdir, a rename/rename(1to2) conflict. We really just want2422 * the file to end up in smrtdir. And the way to achieve that is2423 * to not let Side1 do the rename to dumbdir, since we know that is2424 * the source of one of our directory renames.2425 *2426 * That's why oentry and dir_rename_exclusions is here.2427 *2428 * As it turns out, this also prevents N-way transient rename2429 * confusion; See testcases 9c and 9d of t6043.2430 */2431 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2432if(oentry) {2433output(opt,1,_("WARNING: Avoiding applying%s->%srename "2434"to%s, because%sitself was renamed."),2435 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2436}else{2437 new_path =handle_path_level_conflicts(opt, path, entry,2438 collisions, tree);2439*clean_merge &= (new_path != NULL);2440}24412442return new_path;2443}24442445static voidapply_directory_rename_modifications(struct merge_options *opt,2446struct diff_filepair *pair,2447char*new_path,2448struct rename *re,2449struct tree *tree,2450struct tree *o_tree,2451struct tree *a_tree,2452struct tree *b_tree,2453struct string_list *entries)2454{2455struct string_list_item *item;2456int stage = (tree == a_tree ?2:3);2457int update_wd;24582459/*2460 * In all cases where we can do directory rename detection,2461 * unpack_trees() will have read pair->two->path into the2462 * index and the working copy. We need to remove it so that2463 * we can instead place it at new_path. It is guaranteed to2464 * not be untracked (unpack_trees() would have errored out2465 * saying the file would have been overwritten), but it might2466 * be dirty, though.2467 */2468 update_wd = !was_dirty(opt, pair->two->path);2469if(!update_wd)2470output(opt,1,_("Refusing to lose dirty file at%s"),2471 pair->two->path);2472remove_file(opt,1, pair->two->path, !update_wd);24732474/* Find or create a new re->dst_entry */2475 item =string_list_lookup(entries, new_path);2476if(item) {2477/*2478 * Since we're renaming on this side of history, and it's2479 * due to a directory rename on the other side of history2480 * (which we only allow when the directory in question no2481 * longer exists on the other side of history), the2482 * original entry for re->dst_entry is no longer2483 * necessary...2484 */2485 re->dst_entry->processed =1;24862487/*2488 * ...because we'll be using this new one.2489 */2490 re->dst_entry = item->util;2491}else{2492/*2493 * re->dst_entry is for the before-dir-rename path, and we2494 * need it to hold information for the after-dir-rename2495 * path. Before creating a new entry, we need to mark the2496 * old one as unnecessary (...unless it is shared by2497 * src_entry, i.e. this didn't use to be a rename, in which2498 * case we can just allow the normal processing to happen2499 * for it).2500 */2501if(pair->status =='R')2502 re->dst_entry->processed =1;25032504 re->dst_entry =insert_stage_data(new_path,2505 o_tree, a_tree, b_tree,2506 entries);2507 item =string_list_insert(entries, new_path);2508 item->util = re->dst_entry;2509}25102511/*2512 * Update the stage_data with the information about the path we are2513 * moving into place. That slot will be empty and available for us2514 * to write to because of the collision checks in2515 * handle_path_level_conflicts(). In other words,2516 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2517 * open for us to write to.2518 *2519 * It may be tempting to actually update the index at this point as2520 * well, using update_stages_for_stage_data(), but as per the big2521 * "NOTE" in update_stages(), doing so will modify the current2522 * in-memory index which will break calls to would_lose_untracked()2523 * that we need to make. Instead, we need to just make sure that2524 * the various handle_rename_*() functions update the index2525 * explicitly rather than relying on unpack_trees() to have done it.2526 */2527get_tree_entry(&tree->object.oid,2528 pair->two->path,2529&re->dst_entry->stages[stage].oid,2530&re->dst_entry->stages[stage].mode);25312532/* Update pair status */2533if(pair->status =='A') {2534/*2535 * Recording rename information for this add makes it look2536 * like a rename/delete conflict. Make sure we can2537 * correctly handle this as an add that was moved to a new2538 * directory instead of reporting a rename/delete conflict.2539 */2540 re->add_turned_into_rename =1;2541}2542/*2543 * We don't actually look at pair->status again, but it seems2544 * pedagogically correct to adjust it.2545 */2546 pair->status ='R';25472548/*2549 * Finally, record the new location.2550 */2551 pair->two->path = new_path;2552}25532554/*2555 * Get information of all renames which occurred in 'pairs', making use of2556 * any implicit directory renames inferred from the other side of history.2557 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2558 * to be able to associate the correct cache entries with the rename2559 * information; tree is always equal to either a_tree or b_tree.2560 */2561static struct string_list *get_renames(struct merge_options *opt,2562struct diff_queue_struct *pairs,2563struct hashmap *dir_renames,2564struct hashmap *dir_rename_exclusions,2565struct tree *tree,2566struct tree *o_tree,2567struct tree *a_tree,2568struct tree *b_tree,2569struct string_list *entries,2570int*clean_merge)2571{2572int i;2573struct hashmap collisions;2574struct hashmap_iter iter;2575struct collision_entry *e;2576struct string_list *renames;25772578compute_collisions(&collisions, dir_renames, pairs);2579 renames =xcalloc(1,sizeof(struct string_list));25802581for(i =0; i < pairs->nr; ++i) {2582struct string_list_item *item;2583struct rename *re;2584struct diff_filepair *pair = pairs->queue[i];2585char*new_path;/* non-NULL only with directory renames */25862587if(pair->status !='A'&& pair->status !='R') {2588diff_free_filepair(pair);2589continue;2590}2591 new_path =check_for_directory_rename(opt, pair->two->path, tree,2592 dir_renames,2593 dir_rename_exclusions,2594&collisions,2595 clean_merge);2596if(pair->status !='R'&& !new_path) {2597diff_free_filepair(pair);2598continue;2599}26002601 re =xmalloc(sizeof(*re));2602 re->processed =0;2603 re->add_turned_into_rename =0;2604 re->pair = pair;2605 item =string_list_lookup(entries, re->pair->one->path);2606if(!item)2607 re->src_entry =insert_stage_data(re->pair->one->path,2608 o_tree, a_tree, b_tree, entries);2609else2610 re->src_entry = item->util;26112612 item =string_list_lookup(entries, re->pair->two->path);2613if(!item)2614 re->dst_entry =insert_stage_data(re->pair->two->path,2615 o_tree, a_tree, b_tree, entries);2616else2617 re->dst_entry = item->util;2618 item =string_list_insert(renames, pair->one->path);2619 item->util = re;2620if(new_path)2621apply_directory_rename_modifications(opt, pair, new_path,2622 re, tree, o_tree,2623 a_tree, b_tree,2624 entries);2625}26262627hashmap_iter_init(&collisions, &iter);2628while((e =hashmap_iter_next(&iter))) {2629free(e->target_file);2630string_list_clear(&e->source_files,0);2631}2632hashmap_free(&collisions,1);2633return renames;2634}26352636static intprocess_renames(struct merge_options *opt,2637struct string_list *a_renames,2638struct string_list *b_renames)2639{2640int clean_merge =1, i, j;2641struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2642struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2643const struct rename *sre;26442645for(i =0; i < a_renames->nr; i++) {2646 sre = a_renames->items[i].util;2647string_list_insert(&a_by_dst, sre->pair->two->path)->util2648= (void*)sre;2649}2650for(i =0; i < b_renames->nr; i++) {2651 sre = b_renames->items[i].util;2652string_list_insert(&b_by_dst, sre->pair->two->path)->util2653= (void*)sre;2654}26552656for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2657struct string_list *renames1, *renames2Dst;2658struct rename *ren1 = NULL, *ren2 = NULL;2659const char*branch1, *branch2;2660const char*ren1_src, *ren1_dst;2661struct string_list_item *lookup;26622663if(i >= a_renames->nr) {2664 ren2 = b_renames->items[j++].util;2665}else if(j >= b_renames->nr) {2666 ren1 = a_renames->items[i++].util;2667}else{2668int compare =strcmp(a_renames->items[i].string,2669 b_renames->items[j].string);2670if(compare <=0)2671 ren1 = a_renames->items[i++].util;2672if(compare >=0)2673 ren2 = b_renames->items[j++].util;2674}26752676/* TODO: refactor, so that 1/2 are not needed */2677if(ren1) {2678 renames1 = a_renames;2679 renames2Dst = &b_by_dst;2680 branch1 = opt->branch1;2681 branch2 = opt->branch2;2682}else{2683 renames1 = b_renames;2684 renames2Dst = &a_by_dst;2685 branch1 = opt->branch2;2686 branch2 = opt->branch1;2687SWAP(ren2, ren1);2688}26892690if(ren1->processed)2691continue;2692 ren1->processed =1;2693 ren1->dst_entry->processed =1;2694/* BUG: We should only mark src_entry as processed if we2695 * are not dealing with a rename + add-source case.2696 */2697 ren1->src_entry->processed =1;26982699 ren1_src = ren1->pair->one->path;2700 ren1_dst = ren1->pair->two->path;27012702if(ren2) {2703/* One file renamed on both sides */2704const char*ren2_src = ren2->pair->one->path;2705const char*ren2_dst = ren2->pair->two->path;2706enum rename_type rename_type;2707if(strcmp(ren1_src, ren2_src) !=0)2708BUG("ren1_src != ren2_src");2709 ren2->dst_entry->processed =1;2710 ren2->processed =1;2711if(strcmp(ren1_dst, ren2_dst) !=0) {2712 rename_type = RENAME_ONE_FILE_TO_TWO;2713 clean_merge =0;2714}else{2715 rename_type = RENAME_ONE_FILE_TO_ONE;2716/* BUG: We should only remove ren1_src in2717 * the base stage (think of rename +2718 * add-source cases).2719 */2720remove_file(opt,1, ren1_src,1);2721update_entry(ren1->dst_entry,2722 ren1->pair->one,2723 ren1->pair->two,2724 ren2->pair->two);2725}2726setup_rename_conflict_info(rename_type,2727 opt,2728 ren1,2729 ren2,2730 branch1,2731 branch2,2732 NULL,2733 NULL);2734}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2735/* Two different files renamed to the same thing */2736char*ren2_dst;2737 ren2 = lookup->util;2738 ren2_dst = ren2->pair->two->path;2739if(strcmp(ren1_dst, ren2_dst) !=0)2740BUG("ren1_dst != ren2_dst");27412742 clean_merge =0;2743 ren2->processed =1;2744/*2745 * BUG: We should only mark src_entry as processed2746 * if we are not dealing with a rename + add-source2747 * case.2748 */2749 ren2->src_entry->processed =1;27502751setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2752 opt,2753 ren1,2754 ren2,2755 branch1,2756 branch2,2757 ren1->src_entry,2758 ren2->src_entry);27592760}else{2761/* Renamed in 1, maybe changed in 2 */2762/* we only use sha1 and mode of these */2763struct diff_filespec src_other, dst_other;2764int try_merge;27652766/*2767 * unpack_trees loads entries from common-commit2768 * into stage 1, from head-commit into stage 2, and2769 * from merge-commit into stage 3. We keep track2770 * of which side corresponds to the rename.2771 */2772int renamed_stage = a_renames == renames1 ?2:3;2773int other_stage = a_renames == renames1 ?3:2;27742775/* BUG: We should only remove ren1_src in the base2776 * stage and in other_stage (think of rename +2777 * add-source case).2778 */2779remove_file(opt,1, ren1_src,2780 renamed_stage ==2|| !was_tracked(opt, ren1_src));27812782oidcpy(&src_other.oid,2783&ren1->src_entry->stages[other_stage].oid);2784 src_other.mode = ren1->src_entry->stages[other_stage].mode;2785oidcpy(&dst_other.oid,2786&ren1->dst_entry->stages[other_stage].oid);2787 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2788 try_merge =0;27892790if(oid_eq(&src_other.oid, &null_oid) &&2791 ren1->add_turned_into_rename) {2792setup_rename_conflict_info(RENAME_VIA_DIR,2793 opt,2794 ren1,2795 NULL,2796 branch1,2797 branch2,2798 NULL,2799 NULL);2800}else if(oid_eq(&src_other.oid, &null_oid)) {2801setup_rename_conflict_info(RENAME_DELETE,2802 opt,2803 ren1,2804 NULL,2805 branch1,2806 branch2,2807 NULL,2808 NULL);2809}else if((dst_other.mode == ren1->pair->two->mode) &&2810oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2811/*2812 * Added file on the other side identical to2813 * the file being renamed: clean merge.2814 * Also, there is no need to overwrite the2815 * file already in the working copy, so call2816 * update_file_flags() instead of2817 * update_file().2818 */2819if(update_file_flags(opt,2820&ren1->pair->two->oid,2821 ren1->pair->two->mode,2822 ren1_dst,28231,/* update_cache */28240/* update_wd */))2825 clean_merge = -1;2826}else if(!oid_eq(&dst_other.oid, &null_oid)) {2827/*2828 * Probably not a clean merge, but it's2829 * premature to set clean_merge to 0 here,2830 * because if the rename merges cleanly and2831 * the merge exactly matches the newly added2832 * file, then the merge will be clean.2833 */2834setup_rename_conflict_info(RENAME_ADD,2835 opt,2836 ren1,2837 NULL,2838 branch1,2839 branch2,2840 ren1->src_entry,2841 NULL);2842}else2843 try_merge =1;28442845if(clean_merge <0)2846goto cleanup_and_return;2847if(try_merge) {2848struct diff_filespec *o, *a, *b;2849 src_other.path = (char*)ren1_src;28502851 o = ren1->pair->one;2852if(a_renames == renames1) {2853 a = ren1->pair->two;2854 b = &src_other;2855}else{2856 b = ren1->pair->two;2857 a = &src_other;2858}2859update_entry(ren1->dst_entry, o, a, b);2860setup_rename_conflict_info(RENAME_NORMAL,2861 opt,2862 ren1,2863 NULL,2864 branch1,2865 NULL,2866 NULL,2867 NULL);2868}2869}2870}2871cleanup_and_return:2872string_list_clear(&a_by_dst,0);2873string_list_clear(&b_by_dst,0);28742875return clean_merge;2876}28772878struct rename_info {2879struct string_list *head_renames;2880struct string_list *merge_renames;2881};28822883static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2884struct hashmap *dir_renames)2885{2886struct hashmap_iter iter;2887struct dir_rename_entry *e;28882889hashmap_iter_init(dir_renames, &iter);2890while((e =hashmap_iter_next(&iter))) {2891free(e->dir);2892strbuf_release(&e->new_dir);2893/* possible_new_dirs already cleared in get_directory_renames */2894}2895hashmap_free(dir_renames,1);2896free(dir_renames);28972898free(pairs->queue);2899free(pairs);2900}29012902static intdetect_and_process_renames(struct merge_options *opt,2903struct tree *common,2904struct tree *head,2905struct tree *merge,2906struct string_list *entries,2907struct rename_info *ri)2908{2909struct diff_queue_struct *head_pairs, *merge_pairs;2910struct hashmap *dir_re_head, *dir_re_merge;2911int clean =1;29122913 ri->head_renames = NULL;2914 ri->merge_renames = NULL;29152916if(!merge_detect_rename(opt))2917return1;29182919 head_pairs =get_diffpairs(opt, common, head);2920 merge_pairs =get_diffpairs(opt, common, merge);29212922if(opt->detect_directory_renames) {2923 dir_re_head =get_directory_renames(head_pairs);2924 dir_re_merge =get_directory_renames(merge_pairs);29252926handle_directory_level_conflicts(opt,2927 dir_re_head, head,2928 dir_re_merge, merge);2929}else{2930 dir_re_head =xmalloc(sizeof(*dir_re_head));2931 dir_re_merge =xmalloc(sizeof(*dir_re_merge));2932dir_rename_init(dir_re_head);2933dir_rename_init(dir_re_merge);2934}29352936 ri->head_renames =get_renames(opt, head_pairs,2937 dir_re_merge, dir_re_head, head,2938 common, head, merge, entries,2939&clean);2940if(clean <0)2941goto cleanup;2942 ri->merge_renames =get_renames(opt, merge_pairs,2943 dir_re_head, dir_re_merge, merge,2944 common, head, merge, entries,2945&clean);2946if(clean <0)2947goto cleanup;2948 clean &=process_renames(opt, ri->head_renames, ri->merge_renames);29492950cleanup:2951/*2952 * Some cleanup is deferred until cleanup_renames() because the2953 * data structures are still needed and referenced in2954 * process_entry(). But there are a few things we can free now.2955 */2956initial_cleanup_rename(head_pairs, dir_re_head);2957initial_cleanup_rename(merge_pairs, dir_re_merge);29582959return clean;2960}29612962static voidfinal_cleanup_rename(struct string_list *rename)2963{2964const struct rename *re;2965int i;29662967if(rename == NULL)2968return;29692970for(i =0; i < rename->nr; i++) {2971 re = rename->items[i].util;2972diff_free_filepair(re->pair);2973}2974string_list_clear(rename,1);2975free(rename);2976}29772978static voidfinal_cleanup_renames(struct rename_info *re_info)2979{2980final_cleanup_rename(re_info->head_renames);2981final_cleanup_rename(re_info->merge_renames);2982}29832984static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)2985{2986return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;2987}29882989static intread_oid_strbuf(struct merge_options *opt,2990const struct object_id *oid,2991struct strbuf *dst)2992{2993void*buf;2994enum object_type type;2995unsigned long size;2996 buf =read_object_file(oid, &type, &size);2997if(!buf)2998returnerr(opt,_("cannot read object%s"),oid_to_hex(oid));2999if(type != OBJ_BLOB) {3000free(buf);3001returnerr(opt,_("object%sis not a blob"),oid_to_hex(oid));3002}3003strbuf_attach(dst, buf, size, size +1);3004return0;3005}30063007static intblob_unchanged(struct merge_options *opt,3008const struct object_id *o_oid,3009unsigned o_mode,3010const struct object_id *a_oid,3011unsigned a_mode,3012int renormalize,const char*path)3013{3014struct strbuf obuf = STRBUF_INIT;3015struct strbuf abuf = STRBUF_INIT;3016int ret =0;/* assume changed for safety */3017const struct index_state *idx = opt->repo->index;30183019if(a_mode != o_mode)3020return0;3021if(oid_eq(o_oid, a_oid))3022return1;3023if(!renormalize)3024return0;30253026assert(o_oid && a_oid);3027if(read_oid_strbuf(opt, o_oid, &obuf) ||3028read_oid_strbuf(opt, a_oid, &abuf))3029goto error_return;3030/*3031 * Note: binary | is used so that both renormalizations are3032 * performed. Comparison can be skipped if both files are3033 * unchanged since their sha1s have already been compared.3034 */3035if(renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |3036renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))3037 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));30383039error_return:3040strbuf_release(&obuf);3041strbuf_release(&abuf);3042return ret;3043}30443045static inthandle_modify_delete(struct merge_options *opt,3046const char*path,3047struct object_id *o_oid,int o_mode,3048struct object_id *a_oid,int a_mode,3049struct object_id *b_oid,int b_mode)3050{3051const char*modify_branch, *delete_branch;3052struct object_id *changed_oid;3053int changed_mode;30543055if(a_oid) {3056 modify_branch = opt->branch1;3057 delete_branch = opt->branch2;3058 changed_oid = a_oid;3059 changed_mode = a_mode;3060}else{3061 modify_branch = opt->branch2;3062 delete_branch = opt->branch1;3063 changed_oid = b_oid;3064 changed_mode = b_mode;3065}30663067returnhandle_change_delete(opt,3068 path, NULL,3069 o_oid, o_mode,3070 changed_oid, changed_mode,3071 modify_branch, delete_branch,3072_("modify"),_("modified"));3073}30743075static inthandle_content_merge(struct merge_options *opt,3076const char*path,3077int is_dirty,3078struct object_id *o_oid,int o_mode,3079struct object_id *a_oid,int a_mode,3080struct object_id *b_oid,int b_mode,3081struct rename_conflict_info *ci)3082{3083const char*reason =_("content");3084const char*path1 = NULL, *path2 = NULL;3085struct merge_file_info mfi;3086struct diff_filespec one, a, b;3087unsigned df_conflict_remains =0;30883089if(!o_oid) {3090 reason =_("add/add");3091 o_oid = (struct object_id *)&null_oid;3092}3093 one.path = a.path = b.path = (char*)path;3094oidcpy(&one.oid, o_oid);3095 one.mode = o_mode;3096oidcpy(&a.oid, a_oid);3097 a.mode = a_mode;3098oidcpy(&b.oid, b_oid);3099 b.mode = b_mode;31003101if(ci) {3102struct diff_filepair *pair1 = ci->ren1->pair;31033104 path1 = (opt->branch1 == ci->branch1) ?3105 pair1->two->path : pair1->one->path;3106/* If ci->ren2->pair != NULL, we are in3107 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a3108 * normal rename.3109 */3110 path2 = ((ci->ren2 && ci->ren2->pair) ||3111 opt->branch2 == ci->branch1) ?3112 pair1->two->path : pair1->one->path;3113 one.path = pair1->one->path;3114 a.path = (char*)path1;3115 b.path = (char*)path2;31163117if(dir_in_way(opt->repo->index, path, !opt->call_depth,3118S_ISGITLINK(pair1->two->mode)))3119 df_conflict_remains =1;3120}3121if(merge_mode_and_contents(opt, &one, &a, &b, path,3122 opt->branch1, opt->branch2,3123 opt->call_depth *2, &mfi))3124return-1;31253126/*3127 * We can skip updating the working tree file iff:3128 * a) The merge is clean3129 * b) The merge matches what was in HEAD (content, mode, pathname)3130 * c) The target path is usable (i.e. not involved in D/F conflict)3131 */3132if(mfi.clean &&3133was_tracked_and_matches(opt, path, &mfi.oid, mfi.mode) &&3134!df_conflict_remains) {3135int pos;3136struct cache_entry *ce;31373138output(opt,3,_("Skipped%s(merged same as existing)"), path);3139if(add_cacheinfo(opt, mfi.mode, &mfi.oid, path,31400, (!opt->call_depth && !is_dirty),0))3141return-1;3142/*3143 * However, add_cacheinfo() will delete the old cache entry3144 * and add a new one. We need to copy over any skip_worktree3145 * flag to avoid making the file appear as if it were3146 * deleted by the user.3147 */3148 pos =index_name_pos(&opt->orig_index, path,strlen(path));3149 ce = opt->orig_index.cache[pos];3150if(ce_skip_worktree(ce)) {3151 pos =index_name_pos(opt->repo->index, path,strlen(path));3152 ce = opt->repo->index->cache[pos];3153 ce->ce_flags |= CE_SKIP_WORKTREE;3154}3155return mfi.clean;3156}31573158if(!mfi.clean) {3159if(S_ISGITLINK(mfi.mode))3160 reason =_("submodule");3161output(opt,1,_("CONFLICT (%s): Merge conflict in%s"),3162 reason, path);3163if(ci && !df_conflict_remains)3164if(update_stages(opt, path, &one, &a, &b))3165return-1;3166}31673168if(df_conflict_remains || is_dirty) {3169char*new_path;3170if(opt->call_depth) {3171remove_file_from_index(opt->repo->index, path);3172}else{3173if(!mfi.clean) {3174if(update_stages(opt, path, &one, &a, &b))3175return-1;3176}else{3177int file_from_stage2 =was_tracked(opt, path);3178struct diff_filespec merged;3179oidcpy(&merged.oid, &mfi.oid);3180 merged.mode = mfi.mode;31813182if(update_stages(opt, path, NULL,3183 file_from_stage2 ? &merged : NULL,3184 file_from_stage2 ? NULL : &merged))3185return-1;3186}31873188}3189 new_path =unique_path(opt, path, ci->branch1);3190if(is_dirty) {3191output(opt,1,_("Refusing to lose dirty file at%s"),3192 path);3193}3194output(opt,1,_("Adding as%sinstead"), new_path);3195if(update_file(opt,0, &mfi.oid, mfi.mode, new_path)) {3196free(new_path);3197return-1;3198}3199free(new_path);3200 mfi.clean =0;3201}else if(update_file(opt, mfi.clean, &mfi.oid, mfi.mode, path))3202return-1;3203return!is_dirty && mfi.clean;3204}32053206static inthandle_rename_normal(struct merge_options *opt,3207const char*path,3208struct object_id *o_oid,unsigned int o_mode,3209struct object_id *a_oid,unsigned int a_mode,3210struct object_id *b_oid,unsigned int b_mode,3211struct rename_conflict_info *ci)3212{3213/* Merge the content and write it out */3214returnhandle_content_merge(opt, path,was_dirty(opt, path),3215 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,3216 ci);3217}32183219/* Per entry merge function */3220static intprocess_entry(struct merge_options *opt,3221const char*path,struct stage_data *entry)3222{3223int clean_merge =1;3224int normalize = opt->renormalize;3225unsigned o_mode = entry->stages[1].mode;3226unsigned a_mode = entry->stages[2].mode;3227unsigned b_mode = entry->stages[3].mode;3228struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);3229struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);3230struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);32313232 entry->processed =1;3233if(entry->rename_conflict_info) {3234struct rename_conflict_info *ci = entry->rename_conflict_info;3235switch(ci->rename_type) {3236case RENAME_NORMAL:3237case RENAME_ONE_FILE_TO_ONE:3238 clean_merge =handle_rename_normal(opt,3239 path,3240 o_oid, o_mode,3241 a_oid, a_mode,3242 b_oid, b_mode,3243 ci);3244break;3245case RENAME_VIA_DIR:3246 clean_merge =1;3247if(handle_rename_via_dir(opt, ci->ren1->pair, ci->branch1))3248 clean_merge = -1;3249break;3250case RENAME_ADD:3251/*3252 * Probably unclean merge, but if the renamed file3253 * merges cleanly and the result can then be3254 * two-way merged cleanly with the added file, I3255 * guess it's a clean merge?3256 */3257 clean_merge =handle_rename_add(opt, ci);3258break;3259case RENAME_DELETE:3260 clean_merge =0;3261if(handle_rename_delete(opt, ci->ren1->pair,3262 ci->branch1, ci->branch2))3263 clean_merge = -1;3264break;3265case RENAME_ONE_FILE_TO_TWO:3266 clean_merge =0;3267if(handle_rename_rename_1to2(opt, ci))3268 clean_merge = -1;3269break;3270case RENAME_TWO_FILES_TO_ONE:3271/*3272 * Probably unclean merge, but if the two renamed3273 * files merge cleanly and the two resulting files3274 * can then be two-way merged cleanly, I guess it's3275 * a clean merge?3276 */3277 clean_merge =handle_rename_rename_2to1(opt, ci);3278break;3279default:3280 entry->processed =0;3281break;3282}3283}else if(o_oid && (!a_oid || !b_oid)) {3284/* Case A: Deleted in one */3285if((!a_oid && !b_oid) ||3286(!b_oid &&blob_unchanged(opt, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||3287(!a_oid &&blob_unchanged(opt, o_oid, o_mode, b_oid, b_mode, normalize, path))) {3288/* Deleted in both or deleted in one and3289 * unchanged in the other */3290if(a_oid)3291output(opt,2,_("Removing%s"), path);3292/* do not touch working file if it did not exist */3293remove_file(opt,1, path, !a_oid);3294}else{3295/* Modify/delete; deleted side may have put a directory in the way */3296 clean_merge =0;3297if(handle_modify_delete(opt, path, o_oid, o_mode,3298 a_oid, a_mode, b_oid, b_mode))3299 clean_merge = -1;3300}3301}else if((!o_oid && a_oid && !b_oid) ||3302(!o_oid && !a_oid && b_oid)) {3303/* Case B: Added in one. */3304/* [nothing|directory] -> ([nothing|directory], file) */33053306const char*add_branch;3307const char*other_branch;3308unsigned mode;3309const struct object_id *oid;3310const char*conf;33113312if(a_oid) {3313 add_branch = opt->branch1;3314 other_branch = opt->branch2;3315 mode = a_mode;3316 oid = a_oid;3317 conf =_("file/directory");3318}else{3319 add_branch = opt->branch2;3320 other_branch = opt->branch1;3321 mode = b_mode;3322 oid = b_oid;3323 conf =_("directory/file");3324}3325if(dir_in_way(opt->repo->index, path,3326!opt->call_depth && !S_ISGITLINK(a_mode),33270)) {3328char*new_path =unique_path(opt, path, add_branch);3329 clean_merge =0;3330output(opt,1,_("CONFLICT (%s): There is a directory with name%sin%s. "3331"Adding%sas%s"),3332 conf, path, other_branch, path, new_path);3333if(update_file(opt,0, oid, mode, new_path))3334 clean_merge = -1;3335else if(opt->call_depth)3336remove_file_from_index(opt->repo->index, path);3337free(new_path);3338}else{3339output(opt,2,_("Adding%s"), path);3340/* do not overwrite file if already present */3341if(update_file_flags(opt, oid, mode, path,1, !a_oid))3342 clean_merge = -1;3343}3344}else if(a_oid && b_oid) {3345if(!o_oid) {3346/* Case C: Added in both (check for same permissions) */3347output(opt,1,3348_("CONFLICT (add/add): Merge conflict in%s"),3349 path);3350 clean_merge =handle_file_collision(opt,3351 path, NULL, NULL,3352 opt->branch1,3353 opt->branch2,3354 a_oid, a_mode,3355 b_oid, b_mode);3356}else{3357/* case D: Modified in both, but differently. */3358int is_dirty =0;/* unpack_trees would have bailed if dirty */3359 clean_merge =handle_content_merge(opt, path,3360 is_dirty,3361 o_oid, o_mode,3362 a_oid, a_mode,3363 b_oid, b_mode,3364 NULL);3365}3366}else if(!o_oid && !a_oid && !b_oid) {3367/*3368 * this entry was deleted altogether. a_mode == 0 means3369 * we had that path and want to actively remove it.3370 */3371remove_file(opt,1, path, !a_mode);3372}else3373BUG("fatal merge failure, shouldn't happen.");33743375return clean_merge;3376}33773378intmerge_trees(struct merge_options *opt,3379struct tree *head,3380struct tree *merge,3381struct tree *common,3382struct tree **result)3383{3384struct index_state *istate = opt->repo->index;3385int code, clean;3386struct strbuf sb = STRBUF_INIT;33873388if(!opt->call_depth &&repo_index_has_changes(opt->repo, head, &sb)) {3389err(opt,_("Your local changes to the following files would be overwritten by merge:\n%s"),3390 sb.buf);3391return-1;3392}33933394if(opt->subtree_shift) {3395 merge =shift_tree_object(opt->repo, head, merge, opt->subtree_shift);3396 common =shift_tree_object(opt->repo, head, common, opt->subtree_shift);3397}33983399if(oid_eq(&common->object.oid, &merge->object.oid)) {3400output(opt,0,_("Already up to date!"));3401*result = head;3402return1;3403}34043405 code =unpack_trees_start(opt, common, head, merge);34063407if(code !=0) {3408if(show(opt,4) || opt->call_depth)3409err(opt,_("merging of trees%sand%sfailed"),3410oid_to_hex(&head->object.oid),3411oid_to_hex(&merge->object.oid));3412unpack_trees_finish(opt);3413return-1;3414}34153416if(unmerged_index(istate)) {3417struct string_list *entries;3418struct rename_info re_info;3419int i;3420/*3421 * Only need the hashmap while processing entries, so3422 * initialize it here and free it when we are done running3423 * through the entries. Keeping it in the merge_options as3424 * opposed to decaring a local hashmap is for convenience3425 * so that we don't have to pass it to around.3426 */3427hashmap_init(&opt->current_file_dir_set, path_hashmap_cmp, NULL,512);3428get_files_dirs(opt, head);3429get_files_dirs(opt, merge);34303431 entries =get_unmerged(opt->repo->index);3432 clean =detect_and_process_renames(opt, common, head, merge,3433 entries, &re_info);3434record_df_conflict_files(opt, entries);3435if(clean <0)3436goto cleanup;3437for(i = entries->nr-1;0<= i; i--) {3438const char*path = entries->items[i].string;3439struct stage_data *e = entries->items[i].util;3440if(!e->processed) {3441int ret =process_entry(opt, path, e);3442if(!ret)3443 clean =0;3444else if(ret <0) {3445 clean = ret;3446goto cleanup;3447}3448}3449}3450for(i =0; i < entries->nr; i++) {3451struct stage_data *e = entries->items[i].util;3452if(!e->processed)3453BUG("unprocessed path???%s",3454 entries->items[i].string);3455}34563457 cleanup:3458final_cleanup_renames(&re_info);34593460string_list_clear(entries,1);3461free(entries);34623463hashmap_free(&opt->current_file_dir_set,1);34643465if(clean <0) {3466unpack_trees_finish(opt);3467return clean;3468}3469}3470else3471 clean =1;34723473unpack_trees_finish(opt);34743475if(opt->call_depth && !(*result =write_tree_from_memory(opt)))3476return-1;34773478return clean;3479}34803481static struct commit_list *reverse_commit_list(struct commit_list *list)3482{3483struct commit_list *next = NULL, *current, *backup;3484for(current = list; current; current = backup) {3485 backup = current->next;3486 current->next = next;3487 next = current;3488}3489return next;3490}34913492/*3493 * Merge the commits h1 and h2, return the resulting virtual3494 * commit object and a flag indicating the cleanness of the merge.3495 */3496intmerge_recursive(struct merge_options *opt,3497struct commit *h1,3498struct commit *h2,3499struct commit_list *ca,3500struct commit **result)3501{3502struct commit_list *iter;3503struct commit *merged_common_ancestors;3504struct tree *mrtree;3505int clean;35063507if(show(opt,4)) {3508output(opt,4,_("Merging:"));3509output_commit_title(opt, h1);3510output_commit_title(opt, h2);3511}35123513if(!ca) {3514 ca =get_merge_bases(h1, h2);3515 ca =reverse_commit_list(ca);3516}35173518if(show(opt,5)) {3519unsigned cnt =commit_list_count(ca);35203521output(opt,5,Q_("found%ucommon ancestor:",3522"found%ucommon ancestors:", cnt), cnt);3523for(iter = ca; iter; iter = iter->next)3524output_commit_title(opt, iter->item);3525}35263527 merged_common_ancestors =pop_commit(&ca);3528if(merged_common_ancestors == NULL) {3529/* if there is no common ancestor, use an empty tree */3530struct tree *tree;35313532 tree =lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);3533 merged_common_ancestors =make_virtual_commit(opt->repo, tree,"ancestor");3534}35353536for(iter = ca; iter; iter = iter->next) {3537const char*saved_b1, *saved_b2;3538 opt->call_depth++;3539/*3540 * When the merge fails, the result contains files3541 * with conflict markers. The cleanness flag is3542 * ignored (unless indicating an error), it was never3543 * actually used, as result of merge_trees has always3544 * overwritten it: the committed "conflicts" were3545 * already resolved.3546 */3547discard_index(opt->repo->index);3548 saved_b1 = opt->branch1;3549 saved_b2 = opt->branch2;3550 opt->branch1 ="Temporary merge branch 1";3551 opt->branch2 ="Temporary merge branch 2";3552if(merge_recursive(opt, merged_common_ancestors, iter->item,3553 NULL, &merged_common_ancestors) <0)3554return-1;3555 opt->branch1 = saved_b1;3556 opt->branch2 = saved_b2;3557 opt->call_depth--;35583559if(!merged_common_ancestors)3560returnerr(opt,_("merge returned no commit"));3561}35623563discard_index(opt->repo->index);3564if(!opt->call_depth)3565repo_read_index(opt->repo);35663567 opt->ancestor ="merged common ancestors";3568 clean =merge_trees(opt,get_commit_tree(h1),get_commit_tree(h2),3569get_commit_tree(merged_common_ancestors),3570&mrtree);3571if(clean <0) {3572flush_output(opt);3573return clean;3574}35753576if(opt->call_depth) {3577*result =make_virtual_commit(opt->repo, mrtree,"merged tree");3578commit_list_insert(h1, &(*result)->parents);3579commit_list_insert(h2, &(*result)->parents->next);3580}3581flush_output(opt);3582if(!opt->call_depth && opt->buffer_output <2)3583strbuf_release(&opt->obuf);3584if(show(opt,2))3585diff_warn_rename_limit("merge.renamelimit",3586 opt->needed_rename_limit,0);3587return clean;3588}35893590static struct commit *get_ref(struct repository *repo,const struct object_id *oid,3591const char*name)3592{3593struct object *object;35943595 object =deref_tag(repo,parse_object(repo, oid),3596 name,strlen(name));3597if(!object)3598return NULL;3599if(object->type == OBJ_TREE)3600returnmake_virtual_commit(repo, (struct tree*)object, name);3601if(object->type != OBJ_COMMIT)3602return NULL;3603if(parse_commit((struct commit *)object))3604return NULL;3605return(struct commit *)object;3606}36073608intmerge_recursive_generic(struct merge_options *opt,3609const struct object_id *head,3610const struct object_id *merge,3611int num_base_list,3612const struct object_id **base_list,3613struct commit **result)3614{3615int clean;3616struct lock_file lock = LOCK_INIT;3617struct commit *head_commit =get_ref(opt->repo, head, opt->branch1);3618struct commit *next_commit =get_ref(opt->repo, merge, opt->branch2);3619struct commit_list *ca = NULL;36203621if(base_list) {3622int i;3623for(i =0; i < num_base_list; ++i) {3624struct commit *base;3625if(!(base =get_ref(opt->repo, base_list[i],oid_to_hex(base_list[i]))))3626returnerr(opt,_("Could not parse object '%s'"),3627oid_to_hex(base_list[i]));3628commit_list_insert(base, &ca);3629}3630}36313632repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);3633 clean =merge_recursive(opt, head_commit, next_commit, ca,3634 result);3635if(clean <0) {3636rollback_lock_file(&lock);3637return clean;3638}36393640if(write_locked_index(opt->repo->index, &lock,3641 COMMIT_LOCK | SKIP_IF_UNCHANGED))3642returnerr(opt,_("Unable to write index."));36433644return clean ?0:1;3645}36463647static voidmerge_recursive_config(struct merge_options *opt)3648{3649char*value = NULL;3650git_config_get_int("merge.verbosity", &opt->verbosity);3651git_config_get_int("diff.renamelimit", &opt->diff_rename_limit);3652git_config_get_int("merge.renamelimit", &opt->merge_rename_limit);3653if(!git_config_get_string("diff.renames", &value)) {3654 opt->diff_detect_rename =git_config_rename("diff.renames", value);3655free(value);3656}3657if(!git_config_get_string("merge.renames", &value)) {3658 opt->merge_detect_rename =git_config_rename("merge.renames", value);3659free(value);3660}3661git_config(git_xmerge_config, NULL);3662}36633664voidinit_merge_options(struct merge_options *opt,3665struct repository *repo)3666{3667const char*merge_verbosity;3668memset(opt,0,sizeof(struct merge_options));3669 opt->repo = repo;3670 opt->verbosity =2;3671 opt->buffer_output =1;3672 opt->diff_rename_limit = -1;3673 opt->merge_rename_limit = -1;3674 opt->renormalize =0;3675 opt->diff_detect_rename = -1;3676 opt->merge_detect_rename = -1;3677 opt->detect_directory_renames =1;3678merge_recursive_config(opt);3679 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3680if(merge_verbosity)3681 opt->verbosity =strtol(merge_verbosity, NULL,10);3682if(opt->verbosity >=5)3683 opt->buffer_output =0;3684strbuf_init(&opt->obuf,0);3685string_list_init(&opt->df_conflict_file_set,1);3686}36873688intparse_merge_opt(struct merge_options *opt,const char*s)3689{3690const char*arg;36913692if(!s || !*s)3693return-1;3694if(!strcmp(s,"ours"))3695 opt->recursive_variant = MERGE_RECURSIVE_OURS;3696else if(!strcmp(s,"theirs"))3697 opt->recursive_variant = MERGE_RECURSIVE_THEIRS;3698else if(!strcmp(s,"subtree"))3699 opt->subtree_shift ="";3700else if(skip_prefix(s,"subtree=", &arg))3701 opt->subtree_shift = arg;3702else if(!strcmp(s,"patience"))3703 opt->xdl_opts =DIFF_WITH_ALG(opt, PATIENCE_DIFF);3704else if(!strcmp(s,"histogram"))3705 opt->xdl_opts =DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);3706else if(skip_prefix(s,"diff-algorithm=", &arg)) {3707long value =parse_algorithm_value(arg);3708if(value <0)3709return-1;3710/* clear out previous settings */3711DIFF_XDL_CLR(opt, NEED_MINIMAL);3712 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3713 opt->xdl_opts |= value;3714}3715else if(!strcmp(s,"ignore-space-change"))3716DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);3717else if(!strcmp(s,"ignore-all-space"))3718DIFF_XDL_SET(opt, IGNORE_WHITESPACE);3719else if(!strcmp(s,"ignore-space-at-eol"))3720DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);3721else if(!strcmp(s,"ignore-cr-at-eol"))3722DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);3723else if(!strcmp(s,"renormalize"))3724 opt->renormalize =1;3725else if(!strcmp(s,"no-renormalize"))3726 opt->renormalize =0;3727else if(!strcmp(s,"no-renames"))3728 opt->merge_detect_rename =0;3729else if(!strcmp(s,"find-renames")) {3730 opt->merge_detect_rename =1;3731 opt->rename_score =0;3732}3733else if(skip_prefix(s,"find-renames=", &arg) ||3734skip_prefix(s,"rename-threshold=", &arg)) {3735if((opt->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3736return-1;3737 opt->merge_detect_rename =1;3738}3739/*3740 * Please update $__git_merge_strategy_options in3741 * git-completion.bash when you add new options3742 */3743else3744return-1;3745return0;3746}