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 *o) 119{ 120if(o->buffer_output <2&& o->obuf.len) { 121fputs(o->obuf.buf, stdout); 122strbuf_reset(&o->obuf); 123} 124} 125 126static interr(struct merge_options *o,const char*err, ...) 127{ 128va_list params; 129 130if(o->buffer_output <2) 131flush_output(o); 132else{ 133strbuf_complete(&o->obuf,'\n'); 134strbuf_addstr(&o->obuf,"error: "); 135} 136va_start(params, err); 137strbuf_vaddf(&o->obuf, err, params); 138va_end(params); 139if(o->buffer_output >1) 140strbuf_addch(&o->obuf,'\n'); 141else{ 142error("%s", o->obuf.buf); 143strbuf_reset(&o->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 199struct rename_conflict_info { 200enum rename_type rename_type; 201struct diff_filepair *pair1; 202struct diff_filepair *pair2; 203const char*branch1; 204const char*branch2; 205struct stage_data *dst_entry1; 206struct stage_data *dst_entry2; 207struct diff_filespec ren1_other; 208struct diff_filespec ren2_other; 209}; 210 211/* 212 * Since we want to write the index eventually, we cannot reuse the index 213 * for these (temporary) data. 214 */ 215struct stage_data { 216struct{ 217unsigned mode; 218struct object_id oid; 219} stages[4]; 220struct rename_conflict_info *rename_conflict_info; 221unsigned processed:1; 222}; 223 224staticinlinevoidsetup_rename_conflict_info(enum rename_type rename_type, 225struct diff_filepair *pair1, 226struct diff_filepair *pair2, 227const char*branch1, 228const char*branch2, 229struct stage_data *dst_entry1, 230struct stage_data *dst_entry2, 231struct merge_options *o, 232struct stage_data *src_entry1, 233struct stage_data *src_entry2) 234{ 235int ostage1 =0, ostage2; 236struct rename_conflict_info *ci; 237 238/* 239 * When we have two renames involved, it's easiest to get the 240 * correct things into stage 2 and 3, and to make sure that the 241 * content merge puts HEAD before the other branch if we just 242 * ensure that branch1 == o->branch1. So, simply flip arguments 243 * around if we don't have that. 244 */ 245if(dst_entry2 && branch1 != o->branch1) { 246setup_rename_conflict_info(rename_type, 247 pair2, pair1, 248 branch2, branch1, 249 dst_entry2, dst_entry1, 250 o, 251 src_entry2, src_entry1); 252return; 253} 254 255 ci =xcalloc(1,sizeof(struct rename_conflict_info)); 256 ci->rename_type = rename_type; 257 ci->pair1 = pair1; 258 ci->branch1 = branch1; 259 ci->branch2 = branch2; 260 261 ci->dst_entry1 = dst_entry1; 262 dst_entry1->rename_conflict_info = ci; 263 dst_entry1->processed =0; 264 265assert(!pair2 == !dst_entry2); 266if(dst_entry2) { 267 ci->dst_entry2 = dst_entry2; 268 ci->pair2 = pair2; 269 dst_entry2->rename_conflict_info = ci; 270} 271 272/* 273 * For each rename, there could have been 274 * modifications on the side of history where that 275 * file was not renamed. 276 */ 277if(rename_type == RENAME_ADD || 278 rename_type == RENAME_TWO_FILES_TO_ONE) { 279 ostage1 = o->branch1 == branch1 ?3:2; 280 281 ci->ren1_other.path = pair1->one->path; 282oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid); 283 ci->ren1_other.mode = src_entry1->stages[ostage1].mode; 284} 285 286if(rename_type == RENAME_TWO_FILES_TO_ONE) { 287 ostage2 = ostage1 ^1; 288 289 ci->ren2_other.path = pair2->one->path; 290oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid); 291 ci->ren2_other.mode = src_entry2->stages[ostage2].mode; 292} 293} 294 295static intshow(struct merge_options *o,int v) 296{ 297return(!o->call_depth && o->verbosity >= v) || o->verbosity >=5; 298} 299 300__attribute__((format(printf,3,4))) 301static voidoutput(struct merge_options *o,int v,const char*fmt, ...) 302{ 303va_list ap; 304 305if(!show(o, v)) 306return; 307 308strbuf_addchars(&o->obuf,' ', o->call_depth *2); 309 310va_start(ap, fmt); 311strbuf_vaddf(&o->obuf, fmt, ap); 312va_end(ap); 313 314strbuf_addch(&o->obuf,'\n'); 315if(!o->buffer_output) 316flush_output(o); 317} 318 319static voidoutput_commit_title(struct merge_options *o,struct commit *commit) 320{ 321struct merge_remote_desc *desc; 322 323strbuf_addchars(&o->obuf,' ', o->call_depth *2); 324 desc =merge_remote_util(commit); 325if(desc) 326strbuf_addf(&o->obuf,"virtual%s\n", desc->name); 327else{ 328strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid, 329 DEFAULT_ABBREV); 330strbuf_addch(&o->obuf,' '); 331if(parse_commit(commit) !=0) 332strbuf_addstr(&o->obuf,_("(bad commit)\n")); 333else{ 334const char*title; 335const char*msg =get_commit_buffer(commit, NULL); 336int len =find_commit_subject(msg, &title); 337if(len) 338strbuf_addf(&o->obuf,"%.*s\n", len, title); 339unuse_commit_buffer(commit, msg); 340} 341} 342flush_output(o); 343} 344 345static intadd_cacheinfo(struct merge_options *o, 346unsigned int mode,const struct object_id *oid, 347const char*path,int stage,int refresh,int options) 348{ 349struct index_state *istate = o->repo->index; 350struct cache_entry *ce; 351int ret; 352 353 ce =make_cache_entry(istate, mode, oid ? oid : &null_oid, path, stage,0); 354if(!ce) 355returnerr(o,_("add_cacheinfo failed for path '%s'; merge aborting."), path); 356 357 ret =add_index_entry(istate, ce, options); 358if(refresh) { 359struct cache_entry *nce; 360 361 nce =refresh_cache_entry(istate, ce, 362 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); 363if(!nce) 364returnerr(o,_("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); 365if(nce != ce) 366 ret =add_index_entry(istate, nce, options); 367} 368return ret; 369} 370 371static voidinit_tree_desc_from_tree(struct tree_desc *desc,struct tree *tree) 372{ 373parse_tree(tree); 374init_tree_desc(desc, tree->buffer, tree->size); 375} 376 377static intunpack_trees_start(struct merge_options *o, 378struct tree *common, 379struct tree *head, 380struct tree *merge) 381{ 382int rc; 383struct tree_desc t[3]; 384struct index_state tmp_index = { NULL }; 385 386memset(&o->unpack_opts,0,sizeof(o->unpack_opts)); 387if(o->call_depth) 388 o->unpack_opts.index_only =1; 389else 390 o->unpack_opts.update =1; 391 o->unpack_opts.merge =1; 392 o->unpack_opts.head_idx =2; 393 o->unpack_opts.fn = threeway_merge; 394 o->unpack_opts.src_index = o->repo->index; 395 o->unpack_opts.dst_index = &tmp_index; 396 o->unpack_opts.aggressive = !merge_detect_rename(o); 397setup_unpack_trees_porcelain(&o->unpack_opts,"merge"); 398 399init_tree_desc_from_tree(t+0, common); 400init_tree_desc_from_tree(t+1, head); 401init_tree_desc_from_tree(t+2, merge); 402 403 rc =unpack_trees(3, t, &o->unpack_opts); 404cache_tree_free(&o->repo->index->cache_tree); 405 406/* 407 * Update o->repo->index to match the new results, AFTER saving a copy 408 * in o->orig_index. Update src_index to point to the saved copy. 409 * (verify_uptodate() checks src_index, and the original index is 410 * the one that had the necessary modification timestamps.) 411 */ 412 o->orig_index = *o->repo->index; 413*o->repo->index = tmp_index; 414 o->unpack_opts.src_index = &o->orig_index; 415 416return rc; 417} 418 419static voidunpack_trees_finish(struct merge_options *o) 420{ 421discard_index(&o->orig_index); 422clear_unpack_trees_porcelain(&o->unpack_opts); 423} 424 425struct tree *write_tree_from_memory(struct merge_options *o) 426{ 427struct tree *result = NULL; 428struct index_state *istate = o->repo->index; 429 430if(unmerged_index(istate)) { 431int i; 432fprintf(stderr,"BUG: There are unmerged index entries:\n"); 433for(i =0; i < istate->cache_nr; i++) { 434const struct cache_entry *ce = istate->cache[i]; 435if(ce_stage(ce)) 436fprintf(stderr,"BUG:%d%.*s\n",ce_stage(ce), 437(int)ce_namelen(ce), ce->name); 438} 439BUG("unmerged index entries in merge-recursive.c"); 440} 441 442if(!istate->cache_tree) 443 istate->cache_tree =cache_tree(); 444 445if(!cache_tree_fully_valid(istate->cache_tree) && 446cache_tree_update(istate,0) <0) { 447err(o,_("error building trees")); 448return NULL; 449} 450 451 result =lookup_tree(o->repo, &istate->cache_tree->oid); 452 453return result; 454} 455 456static intsave_files_dirs(const struct object_id *oid, 457struct strbuf *base,const char*path, 458unsigned int mode,int stage,void*context) 459{ 460struct path_hashmap_entry *entry; 461int baselen = base->len; 462struct merge_options *o = context; 463 464strbuf_addstr(base, path); 465 466FLEX_ALLOC_MEM(entry, path, base->buf, base->len); 467hashmap_entry_init(entry,path_hash(entry->path)); 468hashmap_add(&o->current_file_dir_set, entry); 469 470strbuf_setlen(base, baselen); 471return(S_ISDIR(mode) ? READ_TREE_RECURSIVE :0); 472} 473 474static voidget_files_dirs(struct merge_options *o,struct tree *tree) 475{ 476struct pathspec match_all; 477memset(&match_all,0,sizeof(match_all)); 478read_tree_recursive(tree,"",0,0, &match_all, save_files_dirs, o); 479} 480 481static intget_tree_entry_if_blob(const struct object_id *tree, 482const char*path, 483struct object_id *hashy, 484unsigned int*mode_o) 485{ 486int ret; 487 488 ret =get_tree_entry(tree, path, hashy, mode_o); 489if(S_ISDIR(*mode_o)) { 490oidcpy(hashy, &null_oid); 491*mode_o =0; 492} 493return ret; 494} 495 496/* 497 * Returns an index_entry instance which doesn't have to correspond to 498 * a real cache entry in Git's index. 499 */ 500static struct stage_data *insert_stage_data(const char*path, 501struct tree *o,struct tree *a,struct tree *b, 502struct string_list *entries) 503{ 504struct string_list_item *item; 505struct stage_data *e =xcalloc(1,sizeof(struct stage_data)); 506get_tree_entry_if_blob(&o->object.oid, path, 507&e->stages[1].oid, &e->stages[1].mode); 508get_tree_entry_if_blob(&a->object.oid, path, 509&e->stages[2].oid, &e->stages[2].mode); 510get_tree_entry_if_blob(&b->object.oid, path, 511&e->stages[3].oid, &e->stages[3].mode); 512 item =string_list_insert(entries, path); 513 item->util = e; 514return e; 515} 516 517/* 518 * Create a dictionary mapping file names to stage_data objects. The 519 * dictionary contains one entry for every path with a non-zero stage entry. 520 */ 521static struct string_list *get_unmerged(struct index_state *istate) 522{ 523struct string_list *unmerged =xcalloc(1,sizeof(struct string_list)); 524int i; 525 526 unmerged->strdup_strings =1; 527 528for(i =0; i < istate->cache_nr; i++) { 529struct string_list_item *item; 530struct stage_data *e; 531const struct cache_entry *ce = istate->cache[i]; 532if(!ce_stage(ce)) 533continue; 534 535 item =string_list_lookup(unmerged, ce->name); 536if(!item) { 537 item =string_list_insert(unmerged, ce->name); 538 item->util =xcalloc(1,sizeof(struct stage_data)); 539} 540 e = item->util; 541 e->stages[ce_stage(ce)].mode = ce->ce_mode; 542oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); 543} 544 545return unmerged; 546} 547 548static intstring_list_df_name_compare(const char*one,const char*two) 549{ 550int onelen =strlen(one); 551int twolen =strlen(two); 552/* 553 * Here we only care that entries for D/F conflicts are 554 * adjacent, in particular with the file of the D/F conflict 555 * appearing before files below the corresponding directory. 556 * The order of the rest of the list is irrelevant for us. 557 * 558 * To achieve this, we sort with df_name_compare and provide 559 * the mode S_IFDIR so that D/F conflicts will sort correctly. 560 * We use the mode S_IFDIR for everything else for simplicity, 561 * since in other cases any changes in their order due to 562 * sorting cause no problems for us. 563 */ 564int cmp =df_name_compare(one, onelen, S_IFDIR, 565 two, twolen, S_IFDIR); 566/* 567 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure 568 * that 'foo' comes before 'foo/bar'. 569 */ 570if(cmp) 571return cmp; 572return onelen - twolen; 573} 574 575static voidrecord_df_conflict_files(struct merge_options *o, 576struct string_list *entries) 577{ 578/* If there is a D/F conflict and the file for such a conflict 579 * currently exists in the working tree, we want to allow it to be 580 * removed to make room for the corresponding directory if needed. 581 * The files underneath the directories of such D/F conflicts will 582 * be processed before the corresponding file involved in the D/F 583 * conflict. If the D/F directory ends up being removed by the 584 * merge, then we won't have to touch the D/F file. If the D/F 585 * directory needs to be written to the working copy, then the D/F 586 * file will simply be removed (in make_room_for_path()) to make 587 * room for the necessary paths. Note that if both the directory 588 * and the file need to be present, then the D/F file will be 589 * reinstated with a new unique name at the time it is processed. 590 */ 591struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; 592const char*last_file = NULL; 593int last_len =0; 594int i; 595 596/* 597 * If we're merging merge-bases, we don't want to bother with 598 * any working directory changes. 599 */ 600if(o->call_depth) 601return; 602 603/* Ensure D/F conflicts are adjacent in the entries list. */ 604for(i =0; i < entries->nr; i++) { 605struct string_list_item *next = &entries->items[i]; 606string_list_append(&df_sorted_entries, next->string)->util = 607 next->util; 608} 609 df_sorted_entries.cmp = string_list_df_name_compare; 610string_list_sort(&df_sorted_entries); 611 612string_list_clear(&o->df_conflict_file_set,1); 613for(i =0; i < df_sorted_entries.nr; i++) { 614const char*path = df_sorted_entries.items[i].string; 615int len =strlen(path); 616struct stage_data *e = df_sorted_entries.items[i].util; 617 618/* 619 * Check if last_file & path correspond to a D/F conflict; 620 * i.e. whether path is last_file+'/'+<something>. 621 * If so, record that it's okay to remove last_file to make 622 * room for path and friends if needed. 623 */ 624if(last_file && 625 len > last_len && 626memcmp(path, last_file, last_len) ==0&& 627 path[last_len] =='/') { 628string_list_insert(&o->df_conflict_file_set, last_file); 629} 630 631/* 632 * Determine whether path could exist as a file in the 633 * working directory as a possible D/F conflict. This 634 * will only occur when it exists in stage 2 as a 635 * file. 636 */ 637if(S_ISREG(e->stages[2].mode) ||S_ISLNK(e->stages[2].mode)) { 638 last_file = path; 639 last_len = len; 640}else{ 641 last_file = NULL; 642} 643} 644string_list_clear(&df_sorted_entries,0); 645} 646 647struct rename { 648struct diff_filepair *pair; 649/* 650 * Purpose of src_entry and dst_entry: 651 * 652 * If 'before' is renamed to 'after' then src_entry will contain 653 * the versions of 'before' from the merge_base, HEAD, and MERGE in 654 * stages 1, 2, and 3; dst_entry will contain the respective 655 * versions of 'after' in corresponding locations. Thus, we have a 656 * total of six modes and oids, though some will be null. (Stage 0 657 * is ignored; we're interested in handling conflicts.) 658 * 659 * Since we don't turn on break-rewrites by default, neither 660 * src_entry nor dst_entry can have all three of their stages have 661 * non-null oids, meaning at most four of the six will be non-null. 662 * Also, since this is a rename, both src_entry and dst_entry will 663 * have at least one non-null oid, meaning at least two will be 664 * non-null. Of the six oids, a typical rename will have three be 665 * non-null. Only two implies a rename/delete, and four implies a 666 * rename/add. 667 */ 668struct stage_data *src_entry; 669struct stage_data *dst_entry; 670unsigned add_turned_into_rename:1; 671unsigned processed:1; 672}; 673 674static intupdate_stages(struct merge_options *opt,const char*path, 675const struct diff_filespec *o, 676const struct diff_filespec *a, 677const struct diff_filespec *b) 678{ 679 680/* 681 * NOTE: It is usually a bad idea to call update_stages on a path 682 * before calling update_file on that same path, since it can 683 * sometimes lead to spurious "refusing to lose untracked file..." 684 * messages from update_file (via make_room_for path via 685 * would_lose_untracked). Instead, reverse the order of the calls 686 * (executing update_file first and then update_stages). 687 */ 688int clear =1; 689int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; 690if(clear) 691if(remove_file_from_index(opt->repo->index, path)) 692return-1; 693if(o) 694if(add_cacheinfo(opt, o->mode, &o->oid, path,1,0, options)) 695return-1; 696if(a) 697if(add_cacheinfo(opt, a->mode, &a->oid, path,2,0, options)) 698return-1; 699if(b) 700if(add_cacheinfo(opt, b->mode, &b->oid, path,3,0, options)) 701return-1; 702return0; 703} 704 705static voidupdate_entry(struct stage_data *entry, 706struct diff_filespec *o, 707struct diff_filespec *a, 708struct diff_filespec *b) 709{ 710 entry->processed =0; 711 entry->stages[1].mode = o->mode; 712 entry->stages[2].mode = a->mode; 713 entry->stages[3].mode = b->mode; 714oidcpy(&entry->stages[1].oid, &o->oid); 715oidcpy(&entry->stages[2].oid, &a->oid); 716oidcpy(&entry->stages[3].oid, &b->oid); 717} 718 719static intremove_file(struct merge_options *o,int clean, 720const char*path,int no_wd) 721{ 722int update_cache = o->call_depth || clean; 723int update_working_directory = !o->call_depth && !no_wd; 724 725if(update_cache) { 726if(remove_file_from_index(o->repo->index, path)) 727return-1; 728} 729if(update_working_directory) { 730if(ignore_case) { 731struct cache_entry *ce; 732 ce =index_file_exists(o->repo->index, path,strlen(path), 733 ignore_case); 734if(ce &&ce_stage(ce) ==0&&strcmp(path, ce->name)) 735return0; 736} 737if(remove_path(path)) 738return-1; 739} 740return0; 741} 742 743/* add a string to a strbuf, but converting "/" to "_" */ 744static voidadd_flattened_path(struct strbuf *out,const char*s) 745{ 746size_t i = out->len; 747strbuf_addstr(out, s); 748for(; i < out->len; i++) 749if(out->buf[i] =='/') 750 out->buf[i] ='_'; 751} 752 753static char*unique_path(struct merge_options *o,const char*path,const char*branch) 754{ 755struct path_hashmap_entry *entry; 756struct strbuf newpath = STRBUF_INIT; 757int suffix =0; 758size_t base_len; 759 760strbuf_addf(&newpath,"%s~", path); 761add_flattened_path(&newpath, branch); 762 763 base_len = newpath.len; 764while(hashmap_get_from_hash(&o->current_file_dir_set, 765path_hash(newpath.buf), newpath.buf) || 766(!o->call_depth &&file_exists(newpath.buf))) { 767strbuf_setlen(&newpath, base_len); 768strbuf_addf(&newpath,"_%d", suffix++); 769} 770 771FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); 772hashmap_entry_init(entry,path_hash(entry->path)); 773hashmap_add(&o->current_file_dir_set, entry); 774returnstrbuf_detach(&newpath, NULL); 775} 776 777/** 778 * Check whether a directory in the index is in the way of an incoming 779 * file. Return 1 if so. If check_working_copy is non-zero, also 780 * check the working directory. If empty_ok is non-zero, also return 781 * 0 in the case where the working-tree dir exists but is empty. 782 */ 783static intdir_in_way(struct index_state *istate,const char*path, 784int check_working_copy,int empty_ok) 785{ 786int pos; 787struct strbuf dirpath = STRBUF_INIT; 788struct stat st; 789 790strbuf_addstr(&dirpath, path); 791strbuf_addch(&dirpath,'/'); 792 793 pos =index_name_pos(istate, dirpath.buf, dirpath.len); 794 795if(pos <0) 796 pos = -1- pos; 797if(pos < istate->cache_nr && 798!strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) { 799strbuf_release(&dirpath); 800return1; 801} 802 803strbuf_release(&dirpath); 804return check_working_copy && !lstat(path, &st) &&S_ISDIR(st.st_mode) && 805!(empty_ok &&is_empty_dir(path)); 806} 807 808/* 809 * Returns whether path was tracked in the index before the merge started, 810 * and its oid and mode match the specified values 811 */ 812static intwas_tracked_and_matches(struct merge_options *o,const char*path, 813const struct object_id *oid,unsigned mode) 814{ 815int pos =index_name_pos(&o->orig_index, path,strlen(path)); 816struct cache_entry *ce; 817 818if(0> pos) 819/* we were not tracking this path before the merge */ 820return0; 821 822/* See if the file we were tracking before matches */ 823 ce = o->orig_index.cache[pos]; 824return(oid_eq(&ce->oid, oid) && ce->ce_mode == mode); 825} 826 827/* 828 * Returns whether path was tracked in the index before the merge started 829 */ 830static intwas_tracked(struct merge_options *o,const char*path) 831{ 832int pos =index_name_pos(&o->orig_index, path,strlen(path)); 833 834if(0<= pos) 835/* we were tracking this path before the merge */ 836return1; 837 838return0; 839} 840 841static intwould_lose_untracked(struct merge_options *o,const char*path) 842{ 843struct index_state *istate = o->repo->index; 844 845/* 846 * This may look like it can be simplified to: 847 * return !was_tracked(o, path) && file_exists(path) 848 * but it can't. This function needs to know whether path was in 849 * the working tree due to EITHER having been tracked in the index 850 * before the merge OR having been put into the working copy and 851 * index by unpack_trees(). Due to that either-or requirement, we 852 * check the current index instead of the original one. 853 * 854 * Note that we do not need to worry about merge-recursive itself 855 * updating the index after unpack_trees() and before calling this 856 * function, because we strictly require all code paths in 857 * merge-recursive to update the working tree first and the index 858 * second. Doing otherwise would break 859 * update_file()/would_lose_untracked(); see every comment in this 860 * file which mentions "update_stages". 861 */ 862int pos =index_name_pos(istate, path,strlen(path)); 863 864if(pos <0) 865 pos = -1- pos; 866while(pos < istate->cache_nr && 867!strcmp(path, istate->cache[pos]->name)) { 868/* 869 * If stage #0, it is definitely tracked. 870 * If it has stage #2 then it was tracked 871 * before this merge started. All other 872 * cases the path was not tracked. 873 */ 874switch(ce_stage(istate->cache[pos])) { 875case0: 876case2: 877return0; 878} 879 pos++; 880} 881returnfile_exists(path); 882} 883 884static intwas_dirty(struct merge_options *o,const char*path) 885{ 886struct cache_entry *ce; 887int dirty =1; 888 889if(o->call_depth || !was_tracked(o, path)) 890return!dirty; 891 892 ce =index_file_exists(o->unpack_opts.src_index, 893 path,strlen(path), ignore_case); 894 dirty =verify_uptodate(ce, &o->unpack_opts) !=0; 895return dirty; 896} 897 898static intmake_room_for_path(struct merge_options *o,const char*path) 899{ 900int status, i; 901const char*msg =_("failed to create path '%s'%s"); 902 903/* Unlink any D/F conflict files that are in the way */ 904for(i =0; i < o->df_conflict_file_set.nr; i++) { 905const char*df_path = o->df_conflict_file_set.items[i].string; 906size_t pathlen =strlen(path); 907size_t df_pathlen =strlen(df_path); 908if(df_pathlen < pathlen && 909 path[df_pathlen] =='/'&& 910strncmp(path, df_path, df_pathlen) ==0) { 911output(o,3, 912_("Removing%sto make room for subdirectory\n"), 913 df_path); 914unlink(df_path); 915unsorted_string_list_delete_item(&o->df_conflict_file_set, 916 i,0); 917break; 918} 919} 920 921/* Make sure leading directories are created */ 922 status =safe_create_leading_directories_const(path); 923if(status) { 924if(status == SCLD_EXISTS) 925/* something else exists */ 926returnerr(o, msg, path,_(": perhaps a D/F conflict?")); 927returnerr(o, msg, path,""); 928} 929 930/* 931 * Do not unlink a file in the work tree if we are not 932 * tracking it. 933 */ 934if(would_lose_untracked(o, path)) 935returnerr(o,_("refusing to lose untracked file at '%s'"), 936 path); 937 938/* Successful unlink is good.. */ 939if(!unlink(path)) 940return0; 941/* .. and so is no existing file */ 942if(errno == ENOENT) 943return0; 944/* .. but not some other error (who really cares what?) */ 945returnerr(o, msg, path,_(": perhaps a D/F conflict?")); 946} 947 948static intupdate_file_flags(struct merge_options *o, 949const struct object_id *oid, 950unsigned mode, 951const char*path, 952int update_cache, 953int update_wd) 954{ 955int ret =0; 956 957if(o->call_depth) 958 update_wd =0; 959 960if(update_wd) { 961enum object_type type; 962void*buf; 963unsigned long size; 964 965if(S_ISGITLINK(mode)) { 966/* 967 * We may later decide to recursively descend into 968 * the submodule directory and update its index 969 * and/or work tree, but we do not do that now. 970 */ 971 update_wd =0; 972goto update_index; 973} 974 975 buf =read_object_file(oid, &type, &size); 976if(!buf) 977returnerr(o,_("cannot read object%s'%s'"),oid_to_hex(oid), path); 978if(type != OBJ_BLOB) { 979 ret =err(o,_("blob expected for%s'%s'"),oid_to_hex(oid), path); 980goto free_buf; 981} 982if(S_ISREG(mode)) { 983struct strbuf strbuf = STRBUF_INIT; 984if(convert_to_working_tree(o->repo->index, path, buf, size, &strbuf)) { 985free(buf); 986 size = strbuf.len; 987 buf =strbuf_detach(&strbuf, NULL); 988} 989} 990 991if(make_room_for_path(o, path) <0) { 992 update_wd =0; 993goto free_buf; 994} 995if(S_ISREG(mode) || (!has_symlinks &&S_ISLNK(mode))) { 996int fd; 997if(mode &0100) 998 mode =0777; 999else1000 mode =0666;1001 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);1002if(fd <0) {1003 ret =err(o,_("failed to open '%s':%s"),1004 path,strerror(errno));1005goto free_buf;1006}1007write_in_full(fd, buf, size);1008close(fd);1009}else if(S_ISLNK(mode)) {1010char*lnk =xmemdupz(buf, size);1011safe_create_leading_directories_const(path);1012unlink(path);1013if(symlink(lnk, path))1014 ret =err(o,_("failed to symlink '%s':%s"),1015 path,strerror(errno));1016free(lnk);1017}else1018 ret =err(o,1019_("do not know what to do with%06o%s'%s'"),1020 mode,oid_to_hex(oid), path);1021 free_buf:1022free(buf);1023}1024update_index:1025if(!ret && update_cache)1026if(add_cacheinfo(o, mode, oid, path,0, update_wd,1027 ADD_CACHE_OK_TO_ADD))1028return-1;1029return ret;1030}10311032static intupdate_file(struct merge_options *o,1033int clean,1034const struct object_id *oid,1035unsigned mode,1036const char*path)1037{1038returnupdate_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);1039}10401041/* Low level file merging, update and removal */10421043struct merge_file_info {1044struct object_id oid;1045unsigned mode;1046unsigned clean:1,1047 merge:1;1048};10491050static intmerge_3way(struct merge_options *o,1051 mmbuffer_t *result_buf,1052const struct diff_filespec *one,1053const struct diff_filespec *a,1054const struct diff_filespec *b,1055const char*branch1,1056const char*branch2,1057const int extra_marker_size)1058{1059 mmfile_t orig, src1, src2;1060struct ll_merge_options ll_opts = {0};1061char*base_name, *name1, *name2;1062int merge_status;10631064 ll_opts.renormalize = o->renormalize;1065 ll_opts.extra_marker_size = extra_marker_size;1066 ll_opts.xdl_opts = o->xdl_opts;10671068if(o->call_depth) {1069 ll_opts.virtual_ancestor =1;1070 ll_opts.variant =0;1071}else{1072switch(o->recursive_variant) {1073case MERGE_RECURSIVE_OURS:1074 ll_opts.variant = XDL_MERGE_FAVOR_OURS;1075break;1076case MERGE_RECURSIVE_THEIRS:1077 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;1078break;1079default:1080 ll_opts.variant =0;1081break;1082}1083}10841085if(strcmp(a->path, b->path) ||1086(o->ancestor != NULL &&strcmp(a->path, one->path) !=0)) {1087 base_name = o->ancestor == NULL ? NULL :1088mkpathdup("%s:%s", o->ancestor, one->path);1089 name1 =mkpathdup("%s:%s", branch1, a->path);1090 name2 =mkpathdup("%s:%s", branch2, b->path);1091}else{1092 base_name = o->ancestor == NULL ? NULL :1093mkpathdup("%s", o->ancestor);1094 name1 =mkpathdup("%s", branch1);1095 name2 =mkpathdup("%s", branch2);1096}10971098read_mmblob(&orig, &one->oid);1099read_mmblob(&src1, &a->oid);1100read_mmblob(&src2, &b->oid);11011102 merge_status =ll_merge(result_buf, a->path, &orig, base_name,1103&src1, name1, &src2, name2,1104 o->repo->index, &ll_opts);11051106free(base_name);1107free(name1);1108free(name2);1109free(orig.ptr);1110free(src1.ptr);1111free(src2.ptr);1112return merge_status;1113}11141115static intfind_first_merges(struct repository *repo,1116struct object_array *result,const char*path,1117struct commit *a,struct commit *b)1118{1119int i, j;1120struct object_array merges = OBJECT_ARRAY_INIT;1121struct commit *commit;1122int contains_another;11231124char merged_revision[42];1125const char*rev_args[] = {"rev-list","--merges","--ancestry-path",1126"--all", merged_revision, NULL };1127struct rev_info revs;1128struct setup_revision_opt rev_opts;11291130memset(result,0,sizeof(struct object_array));1131memset(&rev_opts,0,sizeof(rev_opts));11321133/* get all revisions that merge commit a */1134xsnprintf(merged_revision,sizeof(merged_revision),"^%s",1135oid_to_hex(&a->object.oid));1136repo_init_revisions(repo, &revs, NULL);1137 rev_opts.submodule = path;1138/* FIXME: can't handle linked worktrees in submodules yet */1139 revs.single_worktree = path != NULL;1140setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);11411142/* save all revisions from the above list that contain b */1143if(prepare_revision_walk(&revs))1144die("revision walk setup failed");1145while((commit =get_revision(&revs)) != NULL) {1146struct object *o = &(commit->object);1147if(in_merge_bases(b, commit))1148add_object_array(o, NULL, &merges);1149}1150reset_revision_walk();11511152/* Now we've got all merges that contain a and b. Prune all1153 * merges that contain another found merge and save them in1154 * result.1155 */1156for(i =0; i < merges.nr; i++) {1157struct commit *m1 = (struct commit *) merges.objects[i].item;11581159 contains_another =0;1160for(j =0; j < merges.nr; j++) {1161struct commit *m2 = (struct commit *) merges.objects[j].item;1162if(i != j &&in_merge_bases(m2, m1)) {1163 contains_another =1;1164break;1165}1166}11671168if(!contains_another)1169add_object_array(merges.objects[i].item, NULL, result);1170}11711172object_array_clear(&merges);1173return result->nr;1174}11751176static voidprint_commit(struct commit *commit)1177{1178struct strbuf sb = STRBUF_INIT;1179struct pretty_print_context ctx = {0};1180 ctx.date_mode.type = DATE_NORMAL;1181format_commit_message(commit,"%h:%m %s", &sb, &ctx);1182fprintf(stderr,"%s\n", sb.buf);1183strbuf_release(&sb);1184}11851186static intmerge_submodule(struct merge_options *o,1187struct object_id *result,const char*path,1188const struct object_id *base,const struct object_id *a,1189const struct object_id *b)1190{1191struct commit *commit_base, *commit_a, *commit_b;1192int parent_count;1193struct object_array merges;11941195int i;1196int search = !o->call_depth;11971198/* store a in result in case we fail */1199oidcpy(result, a);12001201/* we can not handle deletion conflicts */1202if(is_null_oid(base))1203return0;1204if(is_null_oid(a))1205return0;1206if(is_null_oid(b))1207return0;12081209if(add_submodule_odb(path)) {1210output(o,1,_("Failed to merge submodule%s(not checked out)"), path);1211return0;1212}12131214if(!(commit_base =lookup_commit_reference(o->repo, base)) ||1215!(commit_a =lookup_commit_reference(o->repo, a)) ||1216!(commit_b =lookup_commit_reference(o->repo, b))) {1217output(o,1,_("Failed to merge submodule%s(commits not present)"), path);1218return0;1219}12201221/* check whether both changes are forward */1222if(!in_merge_bases(commit_base, commit_a) ||1223!in_merge_bases(commit_base, commit_b)) {1224output(o,1,_("Failed to merge submodule%s(commits don't follow merge-base)"), path);1225return0;1226}12271228/* Case #1: a is contained in b or vice versa */1229if(in_merge_bases(commit_a, commit_b)) {1230oidcpy(result, b);1231if(show(o,3)) {1232output(o,3,_("Fast-forwarding submodule%sto the following commit:"), path);1233output_commit_title(o, commit_b);1234}else if(show(o,2))1235output(o,2,_("Fast-forwarding submodule%s"), path);1236else1237;/* no output */12381239return1;1240}1241if(in_merge_bases(commit_b, commit_a)) {1242oidcpy(result, a);1243if(show(o,3)) {1244output(o,3,_("Fast-forwarding submodule%sto the following commit:"), path);1245output_commit_title(o, commit_a);1246}else if(show(o,2))1247output(o,2,_("Fast-forwarding submodule%s"), path);1248else1249;/* no output */12501251return1;1252}12531254/*1255 * Case #2: There are one or more merges that contain a and b in1256 * the submodule. If there is only one, then present it as a1257 * suggestion to the user, but leave it marked unmerged so the1258 * user needs to confirm the resolution.1259 */12601261/* Skip the search if makes no sense to the calling context. */1262if(!search)1263return0;12641265/* find commit which merges them */1266 parent_count =find_first_merges(o->repo, &merges, path,1267 commit_a, commit_b);1268switch(parent_count) {1269case0:1270output(o,1,_("Failed to merge submodule%s(merge following commits not found)"), path);1271break;12721273case1:1274output(o,1,_("Failed to merge submodule%s(not fast-forward)"), path);1275output(o,2,_("Found a possible merge resolution for the submodule:\n"));1276print_commit((struct commit *) merges.objects[0].item);1277output(o,2,_(1278"If this is correct simply add it to the index "1279"for example\n"1280"by using:\n\n"1281" git update-index --cacheinfo 160000%s\"%s\"\n\n"1282"which will accept this suggestion.\n"),1283oid_to_hex(&merges.objects[0].item->oid), path);1284break;12851286default:1287output(o,1,_("Failed to merge submodule%s(multiple merges found)"), path);1288for(i =0; i < merges.nr; i++)1289print_commit((struct commit *) merges.objects[i].item);1290}12911292object_array_clear(&merges);1293return0;1294}12951296static intmerge_mode_and_contents(struct merge_options *o,1297const struct diff_filespec *one,1298const struct diff_filespec *a,1299const struct diff_filespec *b,1300const char*filename,1301const char*branch1,1302const char*branch2,1303const int extra_marker_size,1304struct merge_file_info *result)1305{1306if(o->branch1 != branch1) {1307/*1308 * It's weird getting a reverse merge with HEAD on the bottom1309 * side of the conflict markers and the other branch on the1310 * top. Fix that.1311 */1312returnmerge_mode_and_contents(o, one, b, a,1313 filename,1314 branch2, branch1,1315 extra_marker_size, result);1316}13171318 result->merge =0;1319 result->clean =1;13201321if((S_IFMT & a->mode) != (S_IFMT & b->mode)) {1322 result->clean =0;1323if(S_ISREG(a->mode)) {1324 result->mode = a->mode;1325oidcpy(&result->oid, &a->oid);1326}else{1327 result->mode = b->mode;1328oidcpy(&result->oid, &b->oid);1329}1330}else{1331if(!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))1332 result->merge =1;13331334/*1335 * Merge modes1336 */1337if(a->mode == b->mode || a->mode == one->mode)1338 result->mode = b->mode;1339else{1340 result->mode = a->mode;1341if(b->mode != one->mode) {1342 result->clean =0;1343 result->merge =1;1344}1345}13461347if(oid_eq(&a->oid, &b->oid) ||oid_eq(&a->oid, &one->oid))1348oidcpy(&result->oid, &b->oid);1349else if(oid_eq(&b->oid, &one->oid))1350oidcpy(&result->oid, &a->oid);1351else if(S_ISREG(a->mode)) {1352 mmbuffer_t result_buf;1353int ret =0, merge_status;13541355 merge_status =merge_3way(o, &result_buf, one, a, b,1356 branch1, branch2,1357 extra_marker_size);13581359if((merge_status <0) || !result_buf.ptr)1360 ret =err(o,_("Failed to execute internal merge"));13611362if(!ret &&1363write_object_file(result_buf.ptr, result_buf.size,1364 blob_type, &result->oid))1365 ret =err(o,_("Unable to add%sto database"),1366 a->path);13671368free(result_buf.ptr);1369if(ret)1370return ret;1371 result->clean = (merge_status ==0);1372}else if(S_ISGITLINK(a->mode)) {1373 result->clean =merge_submodule(o, &result->oid,1374 one->path,1375&one->oid,1376&a->oid,1377&b->oid);1378}else if(S_ISLNK(a->mode)) {1379switch(o->recursive_variant) {1380case MERGE_RECURSIVE_NORMAL:1381oidcpy(&result->oid, &a->oid);1382if(!oid_eq(&a->oid, &b->oid))1383 result->clean =0;1384break;1385case MERGE_RECURSIVE_OURS:1386oidcpy(&result->oid, &a->oid);1387break;1388case MERGE_RECURSIVE_THEIRS:1389oidcpy(&result->oid, &b->oid);1390break;1391}1392}else1393BUG("unsupported object type in the tree");1394}13951396if(result->merge)1397output(o,2,_("Auto-merging%s"), filename);13981399return0;1400}14011402static inthandle_rename_via_dir(struct merge_options *o,1403struct diff_filepair *pair,1404const char*rename_branch,1405const char*other_branch)1406{1407/*1408 * Handle file adds that need to be renamed due to directory rename1409 * detection. This differs from handle_rename_normal, because1410 * there is no content merge to do; just move the file into the1411 * desired final location.1412 */1413const struct diff_filespec *dest = pair->two;14141415if(!o->call_depth &&would_lose_untracked(o, dest->path)) {1416char*alt_path =unique_path(o, dest->path, rename_branch);14171418output(o,1,_("Error: Refusing to lose untracked file at%s; "1419"writing to%sinstead."),1420 dest->path, alt_path);1421/*1422 * Write the file in worktree at alt_path, but not in the1423 * index. Instead, write to dest->path for the index but1424 * only at the higher appropriate stage.1425 */1426if(update_file(o,0, &dest->oid, dest->mode, alt_path))1427return-1;1428free(alt_path);1429returnupdate_stages(o, dest->path, NULL,1430 rename_branch == o->branch1 ? dest : NULL,1431 rename_branch == o->branch1 ? NULL : dest);1432}14331434/* Update dest->path both in index and in worktree */1435if(update_file(o,1, &dest->oid, dest->mode, dest->path))1436return-1;1437return0;1438}14391440static inthandle_change_delete(struct merge_options *o,1441const char*path,const char*old_path,1442const struct object_id *o_oid,int o_mode,1443const struct object_id *changed_oid,1444int changed_mode,1445const char*change_branch,1446const char*delete_branch,1447const char*change,const char*change_past)1448{1449char*alt_path = NULL;1450const char*update_path = path;1451int ret =0;14521453if(dir_in_way(o->repo->index, path, !o->call_depth,0) ||1454(!o->call_depth &&would_lose_untracked(o, path))) {1455 update_path = alt_path =unique_path(o, path, change_branch);1456}14571458if(o->call_depth) {1459/*1460 * We cannot arbitrarily accept either a_sha or b_sha as1461 * correct; since there is no true "middle point" between1462 * them, simply reuse the base version for virtual merge base.1463 */1464 ret =remove_file_from_index(o->repo->index, path);1465if(!ret)1466 ret =update_file(o,0, o_oid, o_mode, update_path);1467}else{1468/*1469 * Despite the four nearly duplicate messages and argument1470 * lists below and the ugliness of the nested if-statements,1471 * having complete messages makes the job easier for1472 * translators.1473 *1474 * The slight variance among the cases is due to the fact1475 * that:1476 * 1) directory/file conflicts (in effect if1477 * !alt_path) could cause us to need to write the1478 * file to a different path.1479 * 2) renames (in effect if !old_path) could mean that1480 * there are two names for the path that the user1481 * may know the file by.1482 */1483if(!alt_path) {1484if(!old_path) {1485output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1486"and%sin%s. Version%sof%sleft in tree."),1487 change, path, delete_branch, change_past,1488 change_branch, change_branch, path);1489}else{1490output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1491"and%sto%sin%s. Version%sof%sleft in tree."),1492 change, old_path, delete_branch, change_past, path,1493 change_branch, change_branch, path);1494}1495}else{1496if(!old_path) {1497output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1498"and%sin%s. Version%sof%sleft in tree at%s."),1499 change, path, delete_branch, change_past,1500 change_branch, change_branch, path, alt_path);1501}else{1502output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1503"and%sto%sin%s. Version%sof%sleft in tree at%s."),1504 change, old_path, delete_branch, change_past, path,1505 change_branch, change_branch, path, alt_path);1506}1507}1508/*1509 * No need to call update_file() on path when change_branch ==1510 * o->branch1 && !alt_path, since that would needlessly touch1511 * path. We could call update_file_flags() with update_cache=01512 * and update_wd=0, but that's a no-op.1513 */1514if(change_branch != o->branch1 || alt_path)1515 ret =update_file(o,0, changed_oid, changed_mode, update_path);1516}1517free(alt_path);15181519return ret;1520}15211522static inthandle_rename_delete(struct merge_options *o,1523struct diff_filepair *pair,1524const char*rename_branch,1525const char*delete_branch)1526{1527const struct diff_filespec *orig = pair->one;1528const struct diff_filespec *dest = pair->two;15291530if(handle_change_delete(o,1531 o->call_depth ? orig->path : dest->path,1532 o->call_depth ? NULL : orig->path,1533&orig->oid, orig->mode,1534&dest->oid, dest->mode,1535 rename_branch, delete_branch,1536_("rename"),_("renamed")))1537return-1;15381539if(o->call_depth)1540returnremove_file_from_index(o->repo->index, dest->path);1541else1542returnupdate_stages(o, dest->path, NULL,1543 rename_branch == o->branch1 ? dest : NULL,1544 rename_branch == o->branch1 ? NULL : dest);1545}15461547static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1548struct stage_data *entry,1549int stage)1550{1551struct object_id *oid = &entry->stages[stage].oid;1552unsigned mode = entry->stages[stage].mode;1553if(mode ==0||is_null_oid(oid))1554return NULL;1555oidcpy(&target->oid, oid);1556 target->mode = mode;1557return target;1558}15591560static inthandle_file_collision(struct merge_options *o,1561const char*collide_path,1562const char*prev_path1,1563const char*prev_path2,1564const char*branch1,const char*branch2,1565const struct object_id *a_oid,1566unsigned int a_mode,1567const struct object_id *b_oid,1568unsigned int b_mode)1569{1570struct merge_file_info mfi;1571struct diff_filespec null, a, b;1572char*alt_path = NULL;1573const char*update_path = collide_path;15741575/*1576 * It's easiest to get the correct things into stage 2 and 3, and1577 * to make sure that the content merge puts HEAD before the other1578 * branch if we just ensure that branch1 == o->branch1. So, simply1579 * flip arguments around if we don't have that.1580 */1581if(branch1 != o->branch1) {1582returnhandle_file_collision(o, collide_path,1583 prev_path2, prev_path1,1584 branch2, branch1,1585 b_oid, b_mode,1586 a_oid, a_mode);1587}15881589/*1590 * In the recursive case, we just opt to undo renames1591 */1592if(o->call_depth && (prev_path1 || prev_path2)) {1593/* Put first file (a_oid, a_mode) in its original spot */1594if(prev_path1) {1595if(update_file(o,1, a_oid, a_mode, prev_path1))1596return-1;1597}else{1598if(update_file(o,1, a_oid, a_mode, collide_path))1599return-1;1600}16011602/* Put second file (b_oid, b_mode) in its original spot */1603if(prev_path2) {1604if(update_file(o,1, b_oid, b_mode, prev_path2))1605return-1;1606}else{1607if(update_file(o,1, b_oid, b_mode, collide_path))1608return-1;1609}16101611/* Don't leave something at collision path if unrenaming both */1612if(prev_path1 && prev_path2)1613remove_file(o,1, collide_path,0);16141615return0;1616}16171618/* Remove rename sources if rename/add or rename/rename(2to1) */1619if(prev_path1)1620remove_file(o,1, prev_path1,1621 o->call_depth ||would_lose_untracked(o, prev_path1));1622if(prev_path2)1623remove_file(o,1, prev_path2,1624 o->call_depth ||would_lose_untracked(o, prev_path2));16251626/*1627 * Remove the collision path, if it wouldn't cause dirty contents1628 * or an untracked file to get lost. We'll either overwrite with1629 * merged contents, or just write out to differently named files.1630 */1631if(was_dirty(o, collide_path)) {1632output(o,1,_("Refusing to lose dirty file at%s"),1633 collide_path);1634 update_path = alt_path =unique_path(o, collide_path,"merged");1635}else if(would_lose_untracked(o, collide_path)) {1636/*1637 * Only way we get here is if both renames were from1638 * a directory rename AND user had an untracked file1639 * at the location where both files end up after the1640 * two directory renames. See testcase 10d of t6043.1641 */1642output(o,1,_("Refusing to lose untracked file at "1643"%s, even though it's in the way."),1644 collide_path);1645 update_path = alt_path =unique_path(o, collide_path,"merged");1646}else{1647/*1648 * FIXME: It's possible that the two files are identical1649 * and that the current working copy happens to match, in1650 * which case we are unnecessarily touching the working1651 * tree file. It's not a likely enough scenario that I1652 * want to code up the checks for it and a better fix is1653 * available if we restructure how unpack_trees() and1654 * merge-recursive interoperate anyway, so punting for1655 * now...1656 */1657remove_file(o,0, collide_path,0);1658}16591660/* Store things in diff_filespecs for functions that need it */1661memset(&a,0,sizeof(struct diff_filespec));1662memset(&b,0,sizeof(struct diff_filespec));1663 null.path = a.path = b.path = (char*)collide_path;1664oidcpy(&null.oid, &null_oid);1665 null.mode =0;1666oidcpy(&a.oid, a_oid);1667 a.mode = a_mode;1668 a.oid_valid =1;1669oidcpy(&b.oid, b_oid);1670 b.mode = b_mode;1671 b.oid_valid =1;16721673if(merge_mode_and_contents(o, &null, &a, &b, collide_path,1674 branch1, branch2, o->call_depth *2, &mfi))1675return-1;1676 mfi.clean &= !alt_path;1677if(update_file(o, mfi.clean, &mfi.oid, mfi.mode, update_path))1678return-1;1679if(!mfi.clean && !o->call_depth &&1680update_stages(o, collide_path, NULL, &a, &b))1681return-1;1682free(alt_path);1683/*1684 * FIXME: If both a & b both started with conflicts (only possible1685 * if they came from a rename/rename(2to1)), but had IDENTICAL1686 * contents including those conflicts, then in the next line we claim1687 * it was clean. If someone cares about this case, we should have the1688 * caller notify us if we started with conflicts.1689 */1690return mfi.clean;1691}16921693static inthandle_rename_add(struct merge_options *o,1694struct rename_conflict_info *ci)1695{1696/* a was renamed to c, and a separate c was added. */1697struct diff_filespec *a = ci->pair1->one;1698struct diff_filespec *c = ci->pair1->two;1699char*path = c->path;1700char*prev_path_desc;1701struct merge_file_info mfi;17021703int other_stage = (ci->branch1 == o->branch1 ?3:2);17041705output(o,1,_("CONFLICT (rename/add): "1706"Rename%s->%sin%s. Added%sin%s"),1707 a->path, c->path, ci->branch1,1708 c->path, ci->branch2);17091710 prev_path_desc =xstrfmt("version of%sfrom%s", path, a->path);1711if(merge_mode_and_contents(o, a, c, &ci->ren1_other, prev_path_desc,1712 o->branch1, o->branch2,17131+ o->call_depth *2, &mfi))1714return-1;1715free(prev_path_desc);17161717returnhandle_file_collision(o,1718 c->path, a->path, NULL,1719 ci->branch1, ci->branch2,1720&mfi.oid, mfi.mode,1721&ci->dst_entry1->stages[other_stage].oid,1722 ci->dst_entry1->stages[other_stage].mode);1723}17241725static char*find_path_for_conflict(struct merge_options *o,1726const char*path,1727const char*branch1,1728const char*branch2)1729{1730char*new_path = NULL;1731if(dir_in_way(o->repo->index, path, !o->call_depth,0)) {1732 new_path =unique_path(o, path, branch1);1733output(o,1,_("%sis a directory in%sadding "1734"as%sinstead"),1735 path, branch2, new_path);1736}else if(would_lose_untracked(o, path)) {1737 new_path =unique_path(o, path, branch1);1738output(o,1,_("Refusing to lose untracked file"1739" at%s; adding as%sinstead"),1740 path, new_path);1741}17421743return new_path;1744}17451746static inthandle_rename_rename_1to2(struct merge_options *o,1747struct rename_conflict_info *ci)1748{1749/* One file was renamed in both branches, but to different names. */1750struct merge_file_info mfi;1751struct diff_filespec other;1752struct diff_filespec *add;1753struct diff_filespec *one = ci->pair1->one;1754struct diff_filespec *a = ci->pair1->two;1755struct diff_filespec *b = ci->pair2->two;1756char*path_desc;17571758output(o,1,_("CONFLICT (rename/rename): "1759"Rename\"%s\"->\"%s\"in branch\"%s\""1760"rename\"%s\"->\"%s\"in\"%s\"%s"),1761 one->path, a->path, ci->branch1,1762 one->path, b->path, ci->branch2,1763 o->call_depth ?_(" (left unresolved)") :"");17641765 path_desc =xstrfmt("%sand%s, both renamed from%s",1766 a->path, b->path, one->path);1767if(merge_mode_and_contents(o, one, a, b, path_desc,1768 ci->branch1, ci->branch2,1769 o->call_depth *2, &mfi))1770return-1;1771free(path_desc);17721773if(o->call_depth) {1774/*1775 * FIXME: For rename/add-source conflicts (if we could detect1776 * such), this is wrong. We should instead find a unique1777 * pathname and then either rename the add-source file to that1778 * unique path, or use that unique path instead of src here.1779 */1780if(update_file(o,0, &mfi.oid, mfi.mode, one->path))1781return-1;17821783/*1784 * Above, we put the merged content at the merge-base's1785 * path. Now we usually need to delete both a->path and1786 * b->path. However, the rename on each side of the merge1787 * could also be involved in a rename/add conflict. In1788 * such cases, we should keep the added file around,1789 * resolving the conflict at that path in its favor.1790 */1791 add =filespec_from_entry(&other, ci->dst_entry1,2^1);1792if(add) {1793if(update_file(o,0, &add->oid, add->mode, a->path))1794return-1;1795}1796else1797remove_file_from_index(o->repo->index, a->path);1798 add =filespec_from_entry(&other, ci->dst_entry2,3^1);1799if(add) {1800if(update_file(o,0, &add->oid, add->mode, b->path))1801return-1;1802}1803else1804remove_file_from_index(o->repo->index, b->path);1805}else{1806/*1807 * For each destination path, we need to see if there is a1808 * rename/add collision. If not, we can write the file out1809 * to the specified location.1810 */1811 add =filespec_from_entry(&other, ci->dst_entry1,2^1);1812if(add) {1813if(handle_file_collision(o, a->path,1814 NULL, NULL,1815 ci->branch1, ci->branch2,1816&mfi.oid, mfi.mode,1817&add->oid, add->mode) <0)1818return-1;1819}else{1820char*new_path =find_path_for_conflict(o, a->path,1821 ci->branch1,1822 ci->branch2);1823if(update_file(o,0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))1824return-1;1825free(new_path);1826if(update_stages(o, a->path, NULL, a, NULL))1827return-1;1828}18291830 add =filespec_from_entry(&other, ci->dst_entry2,3^1);1831if(add) {1832if(handle_file_collision(o, b->path,1833 NULL, NULL,1834 ci->branch1, ci->branch2,1835&add->oid, add->mode,1836&mfi.oid, mfi.mode) <0)1837return-1;1838}else{1839char*new_path =find_path_for_conflict(o, b->path,1840 ci->branch2,1841 ci->branch1);1842if(update_file(o,0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))1843return-1;1844free(new_path);1845if(update_stages(o, b->path, NULL, NULL, b))1846return-1;1847}1848}18491850return0;1851}18521853static inthandle_rename_rename_2to1(struct merge_options *o,1854struct rename_conflict_info *ci)1855{1856/* Two files, a & b, were renamed to the same thing, c. */1857struct diff_filespec *a = ci->pair1->one;1858struct diff_filespec *b = ci->pair2->one;1859struct diff_filespec *c1 = ci->pair1->two;1860struct diff_filespec *c2 = ci->pair2->two;1861char*path = c1->path;/* == c2->path */1862char*path_side_1_desc;1863char*path_side_2_desc;1864struct merge_file_info mfi_c1;1865struct merge_file_info mfi_c2;18661867output(o,1,_("CONFLICT (rename/rename): "1868"Rename%s->%sin%s. "1869"Rename%s->%sin%s"),1870 a->path, c1->path, ci->branch1,1871 b->path, c2->path, ci->branch2);18721873 path_side_1_desc =xstrfmt("version of%sfrom%s", path, a->path);1874 path_side_2_desc =xstrfmt("version of%sfrom%s", path, b->path);1875if(merge_mode_and_contents(o, a, c1, &ci->ren1_other, path_side_1_desc,1876 o->branch1, o->branch2,18771+ o->call_depth *2, &mfi_c1) ||1878merge_mode_and_contents(o, b, &ci->ren2_other, c2, path_side_2_desc,1879 o->branch1, o->branch2,18801+ o->call_depth *2, &mfi_c2))1881return-1;1882free(path_side_1_desc);1883free(path_side_2_desc);18841885returnhandle_file_collision(o, path, a->path, b->path,1886 ci->branch1, ci->branch2,1887&mfi_c1.oid, mfi_c1.mode,1888&mfi_c2.oid, mfi_c2.mode);1889}18901891/*1892 * Get the diff_filepairs changed between o_tree and tree.1893 */1894static struct diff_queue_struct *get_diffpairs(struct merge_options *o,1895struct tree *o_tree,1896struct tree *tree)1897{1898struct diff_queue_struct *ret;1899struct diff_options opts;19001901repo_diff_setup(o->repo, &opts);1902 opts.flags.recursive =1;1903 opts.flags.rename_empty =0;1904 opts.detect_rename =merge_detect_rename(o);1905/*1906 * We do not have logic to handle the detection of copies. In1907 * fact, it may not even make sense to add such logic: would we1908 * really want a change to a base file to be propagated through1909 * multiple other files by a merge?1910 */1911if(opts.detect_rename > DIFF_DETECT_RENAME)1912 opts.detect_rename = DIFF_DETECT_RENAME;1913 opts.rename_limit = o->merge_rename_limit >=0? o->merge_rename_limit :1914 o->diff_rename_limit >=0? o->diff_rename_limit :19151000;1916 opts.rename_score = o->rename_score;1917 opts.show_rename_progress = o->show_rename_progress;1918 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1919diff_setup_done(&opts);1920diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1921diffcore_std(&opts);1922if(opts.needed_rename_limit > o->needed_rename_limit)1923 o->needed_rename_limit = opts.needed_rename_limit;19241925 ret =xmalloc(sizeof(*ret));1926*ret = diff_queued_diff;19271928 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1929 diff_queued_diff.nr =0;1930 diff_queued_diff.queue = NULL;1931diff_flush(&opts);1932return ret;1933}19341935static inttree_has_path(struct tree *tree,const char*path)1936{1937struct object_id hashy;1938unsigned int mode_o;19391940return!get_tree_entry(&tree->object.oid, path,1941&hashy, &mode_o);1942}19431944/*1945 * Return a new string that replaces the beginning portion (which matches1946 * entry->dir), with entry->new_dir. In perl-speak:1947 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1948 * NOTE:1949 * Caller must ensure that old_path starts with entry->dir + '/'.1950 */1951static char*apply_dir_rename(struct dir_rename_entry *entry,1952const char*old_path)1953{1954struct strbuf new_path = STRBUF_INIT;1955int oldlen, newlen;19561957if(entry->non_unique_new_dir)1958return NULL;19591960 oldlen =strlen(entry->dir);1961 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1962strbuf_grow(&new_path, newlen);1963strbuf_addbuf(&new_path, &entry->new_dir);1964strbuf_addstr(&new_path, &old_path[oldlen]);19651966returnstrbuf_detach(&new_path, NULL);1967}19681969static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1970char**old_dir,char**new_dir)1971{1972char*end_of_old, *end_of_new;1973int old_len, new_len;19741975*old_dir = NULL;1976*new_dir = NULL;19771978/*1979 * For1980 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1981 * the "e/foo.c" part is the same, we just want to know that1982 * "a/b/c/d" was renamed to "a/b/some/thing/else"1983 * so, for this example, this function returns "a/b/c/d" in1984 * *old_dir and "a/b/some/thing/else" in *new_dir.1985 *1986 * Also, if the basename of the file changed, we don't care. We1987 * want to know which portion of the directory, if any, changed.1988 */1989 end_of_old =strrchr(old_path,'/');1990 end_of_new =strrchr(new_path,'/');19911992if(end_of_old == NULL || end_of_new == NULL)1993return;1994while(*--end_of_new == *--end_of_old &&1995 end_of_old != old_path &&1996 end_of_new != new_path)1997;/* Do nothing; all in the while loop */1998/*1999 * We've found the first non-matching character in the directory2000 * paths. That means the current directory we were comparing2001 * represents the rename. Move end_of_old and end_of_new back2002 * to the full directory name.2003 */2004if(*end_of_old =='/')2005 end_of_old++;2006if(*end_of_old !='/')2007 end_of_new++;2008 end_of_old =strchr(end_of_old,'/');2009 end_of_new =strchr(end_of_new,'/');20102011/*2012 * It may have been the case that old_path and new_path were the same2013 * directory all along. Don't claim a rename if they're the same.2014 */2015 old_len = end_of_old - old_path;2016 new_len = end_of_new - new_path;20172018if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {2019*old_dir =xstrndup(old_path, old_len);2020*new_dir =xstrndup(new_path, new_len);2021}2022}20232024static voidremove_hashmap_entries(struct hashmap *dir_renames,2025struct string_list *items_to_remove)2026{2027int i;2028struct dir_rename_entry *entry;20292030for(i =0; i < items_to_remove->nr; i++) {2031 entry = items_to_remove->items[i].util;2032hashmap_remove(dir_renames, entry, NULL);2033}2034string_list_clear(items_to_remove,0);2035}20362037/*2038 * See if there is a directory rename for path, and if there are any file2039 * level conflicts for the renamed location. If there is a rename and2040 * there are no conflicts, return the new name. Otherwise, return NULL.2041 */2042static char*handle_path_level_conflicts(struct merge_options *o,2043const char*path,2044struct dir_rename_entry *entry,2045struct hashmap *collisions,2046struct tree *tree)2047{2048char*new_path = NULL;2049struct collision_entry *collision_ent;2050int clean =1;2051struct strbuf collision_paths = STRBUF_INIT;20522053/*2054 * entry has the mapping of old directory name to new directory name2055 * that we want to apply to path.2056 */2057 new_path =apply_dir_rename(entry, path);20582059if(!new_path) {2060/* This should only happen when entry->non_unique_new_dir set */2061if(!entry->non_unique_new_dir)2062BUG("entry->non_unqiue_dir not set and !new_path");2063output(o,1,_("CONFLICT (directory rename split): "2064"Unclear where to place%sbecause directory "2065"%swas renamed to multiple other directories, "2066"with no destination getting a majority of the "2067"files."),2068 path, entry->dir);2069 clean =0;2070return NULL;2071}20722073/*2074 * The caller needs to have ensured that it has pre-populated2075 * collisions with all paths that map to new_path. Do a quick check2076 * to ensure that's the case.2077 */2078 collision_ent =collision_find_entry(collisions, new_path);2079if(collision_ent == NULL)2080BUG("collision_ent is NULL");20812082/*2083 * Check for one-sided add/add/.../add conflicts, i.e.2084 * where implicit renames from the other side doing2085 * directory rename(s) can affect this side of history2086 * to put multiple paths into the same location. Warn2087 * and bail on directory renames for such paths.2088 */2089if(collision_ent->reported_already) {2090 clean =0;2091}else if(tree_has_path(tree, new_path)) {2092 collision_ent->reported_already =1;2093strbuf_add_separated_string_list(&collision_paths,", ",2094&collision_ent->source_files);2095output(o,1,_("CONFLICT (implicit dir rename): Existing "2096"file/dir at%sin the way of implicit "2097"directory rename(s) putting the following "2098"path(s) there:%s."),2099 new_path, collision_paths.buf);2100 clean =0;2101}else if(collision_ent->source_files.nr >1) {2102 collision_ent->reported_already =1;2103strbuf_add_separated_string_list(&collision_paths,", ",2104&collision_ent->source_files);2105output(o,1,_("CONFLICT (implicit dir rename): Cannot map "2106"more than one path to%s; implicit directory "2107"renames tried to put these paths there:%s"),2108 new_path, collision_paths.buf);2109 clean =0;2110}21112112/* Free memory we no longer need */2113strbuf_release(&collision_paths);2114if(!clean && new_path) {2115free(new_path);2116return NULL;2117}21182119return new_path;2120}21212122/*2123 * There are a couple things we want to do at the directory level:2124 * 1. Check for both sides renaming to the same thing, in order to avoid2125 * implicit renaming of files that should be left in place. (See2126 * testcase 6b in t6043 for details.)2127 * 2. Prune directory renames if there are still files left in the2128 * the original directory. These represent a partial directory rename,2129 * i.e. a rename where only some of the files within the directory2130 * were renamed elsewhere. (Technically, this could be done earlier2131 * in get_directory_renames(), except that would prevent us from2132 * doing the previous check and thus failing testcase 6b.)2133 * 3. Check for rename/rename(1to2) conflicts (at the directory level).2134 * In the future, we could potentially record this info as well and2135 * omit reporting rename/rename(1to2) conflicts for each path within2136 * the affected directories, thus cleaning up the merge output.2137 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the2138 * directory level, because merging directories is fine. If it2139 * causes conflicts for files within those merged directories, then2140 * that should be detected at the individual path level.2141 */2142static voidhandle_directory_level_conflicts(struct merge_options *o,2143struct hashmap *dir_re_head,2144struct tree *head,2145struct hashmap *dir_re_merge,2146struct tree *merge)2147{2148struct hashmap_iter iter;2149struct dir_rename_entry *head_ent;2150struct dir_rename_entry *merge_ent;21512152struct string_list remove_from_head = STRING_LIST_INIT_NODUP;2153struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;21542155hashmap_iter_init(dir_re_head, &iter);2156while((head_ent =hashmap_iter_next(&iter))) {2157 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);2158if(merge_ent &&2159!head_ent->non_unique_new_dir &&2160!merge_ent->non_unique_new_dir &&2161!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {2162/* 1. Renamed identically; remove it from both sides */2163string_list_append(&remove_from_head,2164 head_ent->dir)->util = head_ent;2165strbuf_release(&head_ent->new_dir);2166string_list_append(&remove_from_merge,2167 merge_ent->dir)->util = merge_ent;2168strbuf_release(&merge_ent->new_dir);2169}else if(tree_has_path(head, head_ent->dir)) {2170/* 2. This wasn't a directory rename after all */2171string_list_append(&remove_from_head,2172 head_ent->dir)->util = head_ent;2173strbuf_release(&head_ent->new_dir);2174}2175}21762177remove_hashmap_entries(dir_re_head, &remove_from_head);2178remove_hashmap_entries(dir_re_merge, &remove_from_merge);21792180hashmap_iter_init(dir_re_merge, &iter);2181while((merge_ent =hashmap_iter_next(&iter))) {2182 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);2183if(tree_has_path(merge, merge_ent->dir)) {2184/* 2. This wasn't a directory rename after all */2185string_list_append(&remove_from_merge,2186 merge_ent->dir)->util = merge_ent;2187}else if(head_ent &&2188!head_ent->non_unique_new_dir &&2189!merge_ent->non_unique_new_dir) {2190/* 3. rename/rename(1to2) */2191/*2192 * We can assume it's not rename/rename(1to1) because2193 * that was case (1), already checked above. So we2194 * know that head_ent->new_dir and merge_ent->new_dir2195 * are different strings.2196 */2197output(o,1,_("CONFLICT (rename/rename): "2198"Rename directory%s->%sin%s. "2199"Rename directory%s->%sin%s"),2200 head_ent->dir, head_ent->new_dir.buf, o->branch1,2201 head_ent->dir, merge_ent->new_dir.buf, o->branch2);2202string_list_append(&remove_from_head,2203 head_ent->dir)->util = head_ent;2204strbuf_release(&head_ent->new_dir);2205string_list_append(&remove_from_merge,2206 merge_ent->dir)->util = merge_ent;2207strbuf_release(&merge_ent->new_dir);2208}2209}22102211remove_hashmap_entries(dir_re_head, &remove_from_head);2212remove_hashmap_entries(dir_re_merge, &remove_from_merge);2213}22142215static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs,2216struct tree *tree)2217{2218struct hashmap *dir_renames;2219struct hashmap_iter iter;2220struct dir_rename_entry *entry;2221int i;22222223/*2224 * Typically, we think of a directory rename as all files from a2225 * certain directory being moved to a target directory. However,2226 * what if someone first moved two files from the original2227 * directory in one commit, and then renamed the directory2228 * somewhere else in a later commit? At merge time, we just know2229 * that files from the original directory went to two different2230 * places, and that the bulk of them ended up in the same place.2231 * We want each directory rename to represent where the bulk of the2232 * files from that directory end up; this function exists to find2233 * where the bulk of the files went.2234 *2235 * The first loop below simply iterates through the list of file2236 * renames, finding out how often each directory rename pair2237 * possibility occurs.2238 */2239 dir_renames =xmalloc(sizeof(*dir_renames));2240dir_rename_init(dir_renames);2241for(i =0; i < pairs->nr; ++i) {2242struct string_list_item *item;2243int*count;2244struct diff_filepair *pair = pairs->queue[i];2245char*old_dir, *new_dir;22462247/* File not part of directory rename if it wasn't renamed */2248if(pair->status !='R')2249continue;22502251get_renamed_dir_portion(pair->one->path, pair->two->path,2252&old_dir, &new_dir);2253if(!old_dir)2254/* Directory didn't change at all; ignore this one. */2255continue;22562257 entry =dir_rename_find_entry(dir_renames, old_dir);2258if(!entry) {2259 entry =xmalloc(sizeof(*entry));2260dir_rename_entry_init(entry, old_dir);2261hashmap_put(dir_renames, entry);2262}else{2263free(old_dir);2264}2265 item =string_list_lookup(&entry->possible_new_dirs, new_dir);2266if(!item) {2267 item =string_list_insert(&entry->possible_new_dirs,2268 new_dir);2269 item->util =xcalloc(1,sizeof(int));2270}else{2271free(new_dir);2272}2273 count = item->util;2274*count +=1;2275}22762277/*2278 * For each directory with files moved out of it, we find out which2279 * target directory received the most files so we can declare it to2280 * be the "winning" target location for the directory rename. This2281 * winner gets recorded in new_dir. If there is no winner2282 * (multiple target directories received the same number of files),2283 * we set non_unique_new_dir. Once we've determined the winner (or2284 * that there is no winner), we no longer need possible_new_dirs.2285 */2286hashmap_iter_init(dir_renames, &iter);2287while((entry =hashmap_iter_next(&iter))) {2288int max =0;2289int bad_max =0;2290char*best = NULL;22912292for(i =0; i < entry->possible_new_dirs.nr; i++) {2293int*count = entry->possible_new_dirs.items[i].util;22942295if(*count == max)2296 bad_max = max;2297else if(*count > max) {2298 max = *count;2299 best = entry->possible_new_dirs.items[i].string;2300}2301}2302if(bad_max == max)2303 entry->non_unique_new_dir =1;2304else{2305assert(entry->new_dir.len ==0);2306strbuf_addstr(&entry->new_dir, best);2307}2308/*2309 * The relevant directory sub-portion of the original full2310 * filepaths were xstrndup'ed before inserting into2311 * possible_new_dirs, and instead of manually iterating the2312 * list and free'ing each, just lie and tell2313 * possible_new_dirs that it did the strdup'ing so that it2314 * will free them for us.2315 */2316 entry->possible_new_dirs.strdup_strings =1;2317string_list_clear(&entry->possible_new_dirs,1);2318}23192320return dir_renames;2321}23222323static struct dir_rename_entry *check_dir_renamed(const char*path,2324struct hashmap *dir_renames)2325{2326char*temp =xstrdup(path);2327char*end;2328struct dir_rename_entry *entry = NULL;23292330while((end =strrchr(temp,'/'))) {2331*end ='\0';2332 entry =dir_rename_find_entry(dir_renames, temp);2333if(entry)2334break;2335}2336free(temp);2337return entry;2338}23392340static voidcompute_collisions(struct hashmap *collisions,2341struct hashmap *dir_renames,2342struct diff_queue_struct *pairs)2343{2344int i;23452346/*2347 * Multiple files can be mapped to the same path due to directory2348 * renames done by the other side of history. Since that other2349 * side of history could have merged multiple directories into one,2350 * if our side of history added the same file basename to each of2351 * those directories, then all N of them would get implicitly2352 * renamed by the directory rename detection into the same path,2353 * and we'd get an add/add/.../add conflict, and all those adds2354 * from *this* side of history. This is not representable in the2355 * index, and users aren't going to easily be able to make sense of2356 * it. So we need to provide a good warning about what's2357 * happening, and fall back to no-directory-rename detection2358 * behavior for those paths.2359 *2360 * See testcases 9e and all of section 5 from t6043 for examples.2361 */2362collision_init(collisions);23632364for(i =0; i < pairs->nr; ++i) {2365struct dir_rename_entry *dir_rename_ent;2366struct collision_entry *collision_ent;2367char*new_path;2368struct diff_filepair *pair = pairs->queue[i];23692370if(pair->status !='A'&& pair->status !='R')2371continue;2372 dir_rename_ent =check_dir_renamed(pair->two->path,2373 dir_renames);2374if(!dir_rename_ent)2375continue;23762377 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);2378if(!new_path)2379/*2380 * dir_rename_ent->non_unique_new_path is true, which2381 * means there is no directory rename for us to use,2382 * which means it won't cause us any additional2383 * collisions.2384 */2385continue;2386 collision_ent =collision_find_entry(collisions, new_path);2387if(!collision_ent) {2388 collision_ent =xcalloc(1,2389sizeof(struct collision_entry));2390hashmap_entry_init(collision_ent,strhash(new_path));2391hashmap_put(collisions, collision_ent);2392 collision_ent->target_file = new_path;2393}else{2394free(new_path);2395}2396string_list_insert(&collision_ent->source_files,2397 pair->two->path);2398}2399}24002401static char*check_for_directory_rename(struct merge_options *o,2402const char*path,2403struct tree *tree,2404struct hashmap *dir_renames,2405struct hashmap *dir_rename_exclusions,2406struct hashmap *collisions,2407int*clean_merge)2408{2409char*new_path = NULL;2410struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2411struct dir_rename_entry *oentry = NULL;24122413if(!entry)2414return new_path;24152416/*2417 * This next part is a little weird. We do not want to do an2418 * implicit rename into a directory we renamed on our side, because2419 * that will result in a spurious rename/rename(1to2) conflict. An2420 * example:2421 * Base commit: dumbdir/afile, otherdir/bfile2422 * Side 1: smrtdir/afile, otherdir/bfile2423 * Side 2: dumbdir/afile, dumbdir/bfile2424 * Here, while working on Side 1, we could notice that otherdir was2425 * renamed/merged to dumbdir, and change the diff_filepair for2426 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2427 * 2 will notice the rename from dumbdir to smrtdir, and do the2428 * transitive rename to move it from dumbdir/bfile to2429 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2430 * smrtdir, a rename/rename(1to2) conflict. We really just want2431 * the file to end up in smrtdir. And the way to achieve that is2432 * to not let Side1 do the rename to dumbdir, since we know that is2433 * the source of one of our directory renames.2434 *2435 * That's why oentry and dir_rename_exclusions is here.2436 *2437 * As it turns out, this also prevents N-way transient rename2438 * confusion; See testcases 9c and 9d of t6043.2439 */2440 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2441if(oentry) {2442output(o,1,_("WARNING: Avoiding applying%s->%srename "2443"to%s, because%sitself was renamed."),2444 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2445}else{2446 new_path =handle_path_level_conflicts(o, path, entry,2447 collisions, tree);2448*clean_merge &= (new_path != NULL);2449}24502451return new_path;2452}24532454static voidapply_directory_rename_modifications(struct merge_options *o,2455struct diff_filepair *pair,2456char*new_path,2457struct rename *re,2458struct tree *tree,2459struct tree *o_tree,2460struct tree *a_tree,2461struct tree *b_tree,2462struct string_list *entries,2463int*clean)2464{2465struct string_list_item *item;2466int stage = (tree == a_tree ?2:3);2467int update_wd;24682469/*2470 * In all cases where we can do directory rename detection,2471 * unpack_trees() will have read pair->two->path into the2472 * index and the working copy. We need to remove it so that2473 * we can instead place it at new_path. It is guaranteed to2474 * not be untracked (unpack_trees() would have errored out2475 * saying the file would have been overwritten), but it might2476 * be dirty, though.2477 */2478 update_wd = !was_dirty(o, pair->two->path);2479if(!update_wd)2480output(o,1,_("Refusing to lose dirty file at%s"),2481 pair->two->path);2482remove_file(o,1, pair->two->path, !update_wd);24832484/* Find or create a new re->dst_entry */2485 item =string_list_lookup(entries, new_path);2486if(item) {2487/*2488 * Since we're renaming on this side of history, and it's2489 * due to a directory rename on the other side of history2490 * (which we only allow when the directory in question no2491 * longer exists on the other side of history), the2492 * original entry for re->dst_entry is no longer2493 * necessary...2494 */2495 re->dst_entry->processed =1;24962497/*2498 * ...because we'll be using this new one.2499 */2500 re->dst_entry = item->util;2501}else{2502/*2503 * re->dst_entry is for the before-dir-rename path, and we2504 * need it to hold information for the after-dir-rename2505 * path. Before creating a new entry, we need to mark the2506 * old one as unnecessary (...unless it is shared by2507 * src_entry, i.e. this didn't use to be a rename, in which2508 * case we can just allow the normal processing to happen2509 * for it).2510 */2511if(pair->status =='R')2512 re->dst_entry->processed =1;25132514 re->dst_entry =insert_stage_data(new_path,2515 o_tree, a_tree, b_tree,2516 entries);2517 item =string_list_insert(entries, new_path);2518 item->util = re->dst_entry;2519}25202521/*2522 * Update the stage_data with the information about the path we are2523 * moving into place. That slot will be empty and available for us2524 * to write to because of the collision checks in2525 * handle_path_level_conflicts(). In other words,2526 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2527 * open for us to write to.2528 *2529 * It may be tempting to actually update the index at this point as2530 * well, using update_stages_for_stage_data(), but as per the big2531 * "NOTE" in update_stages(), doing so will modify the current2532 * in-memory index which will break calls to would_lose_untracked()2533 * that we need to make. Instead, we need to just make sure that2534 * the various handle_rename_*() functions update the index2535 * explicitly rather than relying on unpack_trees() to have done it.2536 */2537get_tree_entry(&tree->object.oid,2538 pair->two->path,2539&re->dst_entry->stages[stage].oid,2540&re->dst_entry->stages[stage].mode);25412542/* Update pair status */2543if(pair->status =='A') {2544/*2545 * Recording rename information for this add makes it look2546 * like a rename/delete conflict. Make sure we can2547 * correctly handle this as an add that was moved to a new2548 * directory instead of reporting a rename/delete conflict.2549 */2550 re->add_turned_into_rename =1;2551}2552/*2553 * We don't actually look at pair->status again, but it seems2554 * pedagogically correct to adjust it.2555 */2556 pair->status ='R';25572558/*2559 * Finally, record the new location.2560 */2561 pair->two->path = new_path;2562}25632564/*2565 * Get information of all renames which occurred in 'pairs', making use of2566 * any implicit directory renames inferred from the other side of history.2567 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2568 * to be able to associate the correct cache entries with the rename2569 * information; tree is always equal to either a_tree or b_tree.2570 */2571static struct string_list *get_renames(struct merge_options *o,2572struct diff_queue_struct *pairs,2573struct hashmap *dir_renames,2574struct hashmap *dir_rename_exclusions,2575struct tree *tree,2576struct tree *o_tree,2577struct tree *a_tree,2578struct tree *b_tree,2579struct string_list *entries,2580int*clean_merge)2581{2582int i;2583struct hashmap collisions;2584struct hashmap_iter iter;2585struct collision_entry *e;2586struct string_list *renames;25872588compute_collisions(&collisions, dir_renames, pairs);2589 renames =xcalloc(1,sizeof(struct string_list));25902591for(i =0; i < pairs->nr; ++i) {2592struct string_list_item *item;2593struct rename *re;2594struct diff_filepair *pair = pairs->queue[i];2595char*new_path;/* non-NULL only with directory renames */25962597if(pair->status !='A'&& pair->status !='R') {2598diff_free_filepair(pair);2599continue;2600}2601 new_path =check_for_directory_rename(o, pair->two->path, tree,2602 dir_renames,2603 dir_rename_exclusions,2604&collisions,2605 clean_merge);2606if(pair->status !='R'&& !new_path) {2607diff_free_filepair(pair);2608continue;2609}26102611 re =xmalloc(sizeof(*re));2612 re->processed =0;2613 re->add_turned_into_rename =0;2614 re->pair = pair;2615 item =string_list_lookup(entries, re->pair->one->path);2616if(!item)2617 re->src_entry =insert_stage_data(re->pair->one->path,2618 o_tree, a_tree, b_tree, entries);2619else2620 re->src_entry = item->util;26212622 item =string_list_lookup(entries, re->pair->two->path);2623if(!item)2624 re->dst_entry =insert_stage_data(re->pair->two->path,2625 o_tree, a_tree, b_tree, entries);2626else2627 re->dst_entry = item->util;2628 item =string_list_insert(renames, pair->one->path);2629 item->util = re;2630if(new_path)2631apply_directory_rename_modifications(o, pair, new_path,2632 re, tree, o_tree,2633 a_tree, b_tree,2634 entries,2635 clean_merge);2636}26372638hashmap_iter_init(&collisions, &iter);2639while((e =hashmap_iter_next(&iter))) {2640free(e->target_file);2641string_list_clear(&e->source_files,0);2642}2643hashmap_free(&collisions,1);2644return renames;2645}26462647static intprocess_renames(struct merge_options *o,2648struct string_list *a_renames,2649struct string_list *b_renames)2650{2651int clean_merge =1, i, j;2652struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2653struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2654const struct rename *sre;26552656for(i =0; i < a_renames->nr; i++) {2657 sre = a_renames->items[i].util;2658string_list_insert(&a_by_dst, sre->pair->two->path)->util2659= (void*)sre;2660}2661for(i =0; i < b_renames->nr; i++) {2662 sre = b_renames->items[i].util;2663string_list_insert(&b_by_dst, sre->pair->two->path)->util2664= (void*)sre;2665}26662667for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2668struct string_list *renames1, *renames2Dst;2669struct rename *ren1 = NULL, *ren2 = NULL;2670const char*branch1, *branch2;2671const char*ren1_src, *ren1_dst;2672struct string_list_item *lookup;26732674if(i >= a_renames->nr) {2675 ren2 = b_renames->items[j++].util;2676}else if(j >= b_renames->nr) {2677 ren1 = a_renames->items[i++].util;2678}else{2679int compare =strcmp(a_renames->items[i].string,2680 b_renames->items[j].string);2681if(compare <=0)2682 ren1 = a_renames->items[i++].util;2683if(compare >=0)2684 ren2 = b_renames->items[j++].util;2685}26862687/* TODO: refactor, so that 1/2 are not needed */2688if(ren1) {2689 renames1 = a_renames;2690 renames2Dst = &b_by_dst;2691 branch1 = o->branch1;2692 branch2 = o->branch2;2693}else{2694 renames1 = b_renames;2695 renames2Dst = &a_by_dst;2696 branch1 = o->branch2;2697 branch2 = o->branch1;2698SWAP(ren2, ren1);2699}27002701if(ren1->processed)2702continue;2703 ren1->processed =1;2704 ren1->dst_entry->processed =1;2705/* BUG: We should only mark src_entry as processed if we2706 * are not dealing with a rename + add-source case.2707 */2708 ren1->src_entry->processed =1;27092710 ren1_src = ren1->pair->one->path;2711 ren1_dst = ren1->pair->two->path;27122713if(ren2) {2714/* One file renamed on both sides */2715const char*ren2_src = ren2->pair->one->path;2716const char*ren2_dst = ren2->pair->two->path;2717enum rename_type rename_type;2718if(strcmp(ren1_src, ren2_src) !=0)2719BUG("ren1_src != ren2_src");2720 ren2->dst_entry->processed =1;2721 ren2->processed =1;2722if(strcmp(ren1_dst, ren2_dst) !=0) {2723 rename_type = RENAME_ONE_FILE_TO_TWO;2724 clean_merge =0;2725}else{2726 rename_type = RENAME_ONE_FILE_TO_ONE;2727/* BUG: We should only remove ren1_src in2728 * the base stage (think of rename +2729 * add-source cases).2730 */2731remove_file(o,1, ren1_src,1);2732update_entry(ren1->dst_entry,2733 ren1->pair->one,2734 ren1->pair->two,2735 ren2->pair->two);2736}2737setup_rename_conflict_info(rename_type,2738 ren1->pair,2739 ren2->pair,2740 branch1,2741 branch2,2742 ren1->dst_entry,2743 ren2->dst_entry,2744 o,2745 NULL,2746 NULL);2747}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2748/* Two different files renamed to the same thing */2749char*ren2_dst;2750 ren2 = lookup->util;2751 ren2_dst = ren2->pair->two->path;2752if(strcmp(ren1_dst, ren2_dst) !=0)2753BUG("ren1_dst != ren2_dst");27542755 clean_merge =0;2756 ren2->processed =1;2757/*2758 * BUG: We should only mark src_entry as processed2759 * if we are not dealing with a rename + add-source2760 * case.2761 */2762 ren2->src_entry->processed =1;27632764setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2765 ren1->pair,2766 ren2->pair,2767 branch1,2768 branch2,2769 ren1->dst_entry,2770 ren2->dst_entry,2771 o,2772 ren1->src_entry,2773 ren2->src_entry);27742775}else{2776/* Renamed in 1, maybe changed in 2 */2777/* we only use sha1 and mode of these */2778struct diff_filespec src_other, dst_other;2779int try_merge;27802781/*2782 * unpack_trees loads entries from common-commit2783 * into stage 1, from head-commit into stage 2, and2784 * from merge-commit into stage 3. We keep track2785 * of which side corresponds to the rename.2786 */2787int renamed_stage = a_renames == renames1 ?2:3;2788int other_stage = a_renames == renames1 ?3:2;27892790/* BUG: We should only remove ren1_src in the base2791 * stage and in other_stage (think of rename +2792 * add-source case).2793 */2794remove_file(o,1, ren1_src,2795 renamed_stage ==2|| !was_tracked(o, ren1_src));27962797oidcpy(&src_other.oid,2798&ren1->src_entry->stages[other_stage].oid);2799 src_other.mode = ren1->src_entry->stages[other_stage].mode;2800oidcpy(&dst_other.oid,2801&ren1->dst_entry->stages[other_stage].oid);2802 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2803 try_merge =0;28042805if(oid_eq(&src_other.oid, &null_oid) &&2806 ren1->add_turned_into_rename) {2807setup_rename_conflict_info(RENAME_VIA_DIR,2808 ren1->pair,2809 NULL,2810 branch1,2811 branch2,2812 ren1->dst_entry,2813 NULL,2814 o,2815 NULL,2816 NULL);2817}else if(oid_eq(&src_other.oid, &null_oid)) {2818setup_rename_conflict_info(RENAME_DELETE,2819 ren1->pair,2820 NULL,2821 branch1,2822 branch2,2823 ren1->dst_entry,2824 NULL,2825 o,2826 NULL,2827 NULL);2828}else if((dst_other.mode == ren1->pair->two->mode) &&2829oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2830/*2831 * Added file on the other side identical to2832 * the file being renamed: clean merge.2833 * Also, there is no need to overwrite the2834 * file already in the working copy, so call2835 * update_file_flags() instead of2836 * update_file().2837 */2838if(update_file_flags(o,2839&ren1->pair->two->oid,2840 ren1->pair->two->mode,2841 ren1_dst,28421,/* update_cache */28430/* update_wd */))2844 clean_merge = -1;2845}else if(!oid_eq(&dst_other.oid, &null_oid)) {2846/*2847 * Probably not a clean merge, but it's2848 * premature to set clean_merge to 0 here,2849 * because if the rename merges cleanly and2850 * the merge exactly matches the newly added2851 * file, then the merge will be clean.2852 */2853setup_rename_conflict_info(RENAME_ADD,2854 ren1->pair,2855 NULL,2856 branch1,2857 branch2,2858 ren1->dst_entry,2859 NULL,2860 o,2861 ren1->src_entry,2862 NULL);2863}else2864 try_merge =1;28652866if(clean_merge <0)2867goto cleanup_and_return;2868if(try_merge) {2869struct diff_filespec *one, *a, *b;2870 src_other.path = (char*)ren1_src;28712872 one = ren1->pair->one;2873if(a_renames == renames1) {2874 a = ren1->pair->two;2875 b = &src_other;2876}else{2877 b = ren1->pair->two;2878 a = &src_other;2879}2880update_entry(ren1->dst_entry, one, a, b);2881setup_rename_conflict_info(RENAME_NORMAL,2882 ren1->pair,2883 NULL,2884 branch1,2885 NULL,2886 ren1->dst_entry,2887 NULL,2888 o,2889 NULL,2890 NULL);2891}2892}2893}2894cleanup_and_return:2895string_list_clear(&a_by_dst,0);2896string_list_clear(&b_by_dst,0);28972898return clean_merge;2899}29002901struct rename_info {2902struct string_list *head_renames;2903struct string_list *merge_renames;2904};29052906static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2907struct hashmap *dir_renames)2908{2909struct hashmap_iter iter;2910struct dir_rename_entry *e;29112912hashmap_iter_init(dir_renames, &iter);2913while((e =hashmap_iter_next(&iter))) {2914free(e->dir);2915strbuf_release(&e->new_dir);2916/* possible_new_dirs already cleared in get_directory_renames */2917}2918hashmap_free(dir_renames,1);2919free(dir_renames);29202921free(pairs->queue);2922free(pairs);2923}29242925static intdetect_and_process_renames(struct merge_options *o,2926struct tree *common,2927struct tree *head,2928struct tree *merge,2929struct string_list *entries,2930struct rename_info *ri)2931{2932struct diff_queue_struct *head_pairs, *merge_pairs;2933struct hashmap *dir_re_head, *dir_re_merge;2934int clean =1;29352936 ri->head_renames = NULL;2937 ri->merge_renames = NULL;29382939if(!merge_detect_rename(o))2940return1;29412942 head_pairs =get_diffpairs(o, common, head);2943 merge_pairs =get_diffpairs(o, common, merge);29442945if(o->detect_directory_renames) {2946 dir_re_head =get_directory_renames(head_pairs, head);2947 dir_re_merge =get_directory_renames(merge_pairs, merge);29482949handle_directory_level_conflicts(o,2950 dir_re_head, head,2951 dir_re_merge, merge);2952}else{2953 dir_re_head =xmalloc(sizeof(*dir_re_head));2954 dir_re_merge =xmalloc(sizeof(*dir_re_merge));2955dir_rename_init(dir_re_head);2956dir_rename_init(dir_re_merge);2957}29582959 ri->head_renames =get_renames(o, head_pairs,2960 dir_re_merge, dir_re_head, head,2961 common, head, merge, entries,2962&clean);2963if(clean <0)2964goto cleanup;2965 ri->merge_renames =get_renames(o, merge_pairs,2966 dir_re_head, dir_re_merge, merge,2967 common, head, merge, entries,2968&clean);2969if(clean <0)2970goto cleanup;2971 clean &=process_renames(o, ri->head_renames, ri->merge_renames);29722973cleanup:2974/*2975 * Some cleanup is deferred until cleanup_renames() because the2976 * data structures are still needed and referenced in2977 * process_entry(). But there are a few things we can free now.2978 */2979initial_cleanup_rename(head_pairs, dir_re_head);2980initial_cleanup_rename(merge_pairs, dir_re_merge);29812982return clean;2983}29842985static voidfinal_cleanup_rename(struct string_list *rename)2986{2987const struct rename *re;2988int i;29892990if(rename == NULL)2991return;29922993for(i =0; i < rename->nr; i++) {2994 re = rename->items[i].util;2995diff_free_filepair(re->pair);2996}2997string_list_clear(rename,1);2998free(rename);2999}30003001static voidfinal_cleanup_renames(struct rename_info *re_info)3002{3003final_cleanup_rename(re_info->head_renames);3004final_cleanup_rename(re_info->merge_renames);3005}30063007static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)3008{3009return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;3010}30113012static intread_oid_strbuf(struct merge_options *o,3013const struct object_id *oid,3014struct strbuf *dst)3015{3016void*buf;3017enum object_type type;3018unsigned long size;3019 buf =read_object_file(oid, &type, &size);3020if(!buf)3021returnerr(o,_("cannot read object%s"),oid_to_hex(oid));3022if(type != OBJ_BLOB) {3023free(buf);3024returnerr(o,_("object%sis not a blob"),oid_to_hex(oid));3025}3026strbuf_attach(dst, buf, size, size +1);3027return0;3028}30293030static intblob_unchanged(struct merge_options *opt,3031const struct object_id *o_oid,3032unsigned o_mode,3033const struct object_id *a_oid,3034unsigned a_mode,3035int renormalize,const char*path)3036{3037struct strbuf o = STRBUF_INIT;3038struct strbuf a = STRBUF_INIT;3039int ret =0;/* assume changed for safety */30403041if(a_mode != o_mode)3042return0;3043if(oid_eq(o_oid, a_oid))3044return1;3045if(!renormalize)3046return0;30473048assert(o_oid && a_oid);3049if(read_oid_strbuf(opt, o_oid, &o) ||read_oid_strbuf(opt, a_oid, &a))3050goto error_return;3051/*3052 * Note: binary | is used so that both renormalizations are3053 * performed. Comparison can be skipped if both files are3054 * unchanged since their sha1s have already been compared.3055 */3056if(renormalize_buffer(opt->repo->index, path, o.buf, o.len, &o) |3057renormalize_buffer(opt->repo->index, path, a.buf, a.len, &a))3058 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));30593060error_return:3061strbuf_release(&o);3062strbuf_release(&a);3063return ret;3064}30653066static inthandle_modify_delete(struct merge_options *o,3067const char*path,3068struct object_id *o_oid,int o_mode,3069struct object_id *a_oid,int a_mode,3070struct object_id *b_oid,int b_mode)3071{3072const char*modify_branch, *delete_branch;3073struct object_id *changed_oid;3074int changed_mode;30753076if(a_oid) {3077 modify_branch = o->branch1;3078 delete_branch = o->branch2;3079 changed_oid = a_oid;3080 changed_mode = a_mode;3081}else{3082 modify_branch = o->branch2;3083 delete_branch = o->branch1;3084 changed_oid = b_oid;3085 changed_mode = b_mode;3086}30873088returnhandle_change_delete(o,3089 path, NULL,3090 o_oid, o_mode,3091 changed_oid, changed_mode,3092 modify_branch, delete_branch,3093_("modify"),_("modified"));3094}30953096static inthandle_content_merge(struct merge_options *o,3097const char*path,3098int is_dirty,3099struct object_id *o_oid,int o_mode,3100struct object_id *a_oid,int a_mode,3101struct object_id *b_oid,int b_mode,3102struct rename_conflict_info *rename_conflict_info)3103{3104const char*reason =_("content");3105const char*path1 = NULL, *path2 = NULL;3106struct merge_file_info mfi;3107struct diff_filespec one, a, b;3108unsigned df_conflict_remains =0;31093110if(!o_oid) {3111 reason =_("add/add");3112 o_oid = (struct object_id *)&null_oid;3113}3114 one.path = a.path = b.path = (char*)path;3115oidcpy(&one.oid, o_oid);3116 one.mode = o_mode;3117oidcpy(&a.oid, a_oid);3118 a.mode = a_mode;3119oidcpy(&b.oid, b_oid);3120 b.mode = b_mode;31213122if(rename_conflict_info) {3123struct diff_filepair *pair1 = rename_conflict_info->pair1;31243125 path1 = (o->branch1 == rename_conflict_info->branch1) ?3126 pair1->two->path : pair1->one->path;3127/* If rename_conflict_info->pair2 != NULL, we are in3128 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a3129 * normal rename.3130 */3131 path2 = (rename_conflict_info->pair2 ||3132 o->branch2 == rename_conflict_info->branch1) ?3133 pair1->two->path : pair1->one->path;3134 one.path = pair1->one->path;3135 a.path = (char*)path1;3136 b.path = (char*)path2;31373138if(dir_in_way(o->repo->index, path, !o->call_depth,3139S_ISGITLINK(pair1->two->mode)))3140 df_conflict_remains =1;3141}3142if(merge_mode_and_contents(o, &one, &a, &b, path,3143 o->branch1, o->branch2,3144 o->call_depth *2, &mfi))3145return-1;31463147/*3148 * We can skip updating the working tree file iff:3149 * a) The merge is clean3150 * b) The merge matches what was in HEAD (content, mode, pathname)3151 * c) The target path is usable (i.e. not involved in D/F conflict)3152 */3153if(mfi.clean &&3154was_tracked_and_matches(o, path, &mfi.oid, mfi.mode) &&3155!df_conflict_remains) {3156int pos;3157struct cache_entry *ce;31583159output(o,3,_("Skipped%s(merged same as existing)"), path);3160if(add_cacheinfo(o, mfi.mode, &mfi.oid, path,31610, (!o->call_depth && !is_dirty),0))3162return-1;3163/*3164 * However, add_cacheinfo() will delete the old cache entry3165 * and add a new one. We need to copy over any skip_worktree3166 * flag to avoid making the file appear as if it were3167 * deleted by the user.3168 */3169 pos =index_name_pos(&o->orig_index, path,strlen(path));3170 ce = o->orig_index.cache[pos];3171if(ce_skip_worktree(ce)) {3172 pos =index_name_pos(o->repo->index, path,strlen(path));3173 ce = o->repo->index->cache[pos];3174 ce->ce_flags |= CE_SKIP_WORKTREE;3175}3176return mfi.clean;3177}31783179if(!mfi.clean) {3180if(S_ISGITLINK(mfi.mode))3181 reason =_("submodule");3182output(o,1,_("CONFLICT (%s): Merge conflict in%s"),3183 reason, path);3184if(rename_conflict_info && !df_conflict_remains)3185if(update_stages(o, path, &one, &a, &b))3186return-1;3187}31883189if(df_conflict_remains || is_dirty) {3190char*new_path;3191if(o->call_depth) {3192remove_file_from_index(o->repo->index, path);3193}else{3194if(!mfi.clean) {3195if(update_stages(o, path, &one, &a, &b))3196return-1;3197}else{3198int file_from_stage2 =was_tracked(o, path);3199struct diff_filespec merged;3200oidcpy(&merged.oid, &mfi.oid);3201 merged.mode = mfi.mode;32023203if(update_stages(o, path, NULL,3204 file_from_stage2 ? &merged : NULL,3205 file_from_stage2 ? NULL : &merged))3206return-1;3207}32083209}3210 new_path =unique_path(o, path, rename_conflict_info->branch1);3211if(is_dirty) {3212output(o,1,_("Refusing to lose dirty file at%s"),3213 path);3214}3215output(o,1,_("Adding as%sinstead"), new_path);3216if(update_file(o,0, &mfi.oid, mfi.mode, new_path)) {3217free(new_path);3218return-1;3219}3220free(new_path);3221 mfi.clean =0;3222}else if(update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))3223return-1;3224return!is_dirty && mfi.clean;3225}32263227static inthandle_rename_normal(struct merge_options *o,3228const char*path,3229struct object_id *o_oid,unsigned int o_mode,3230struct object_id *a_oid,unsigned int a_mode,3231struct object_id *b_oid,unsigned int b_mode,3232struct rename_conflict_info *ci)3233{3234/* Merge the content and write it out */3235returnhandle_content_merge(o, path,was_dirty(o, path),3236 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,3237 ci);3238}32393240/* Per entry merge function */3241static intprocess_entry(struct merge_options *o,3242const char*path,struct stage_data *entry)3243{3244int clean_merge =1;3245int normalize = o->renormalize;3246unsigned o_mode = entry->stages[1].mode;3247unsigned a_mode = entry->stages[2].mode;3248unsigned b_mode = entry->stages[3].mode;3249struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);3250struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);3251struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);32523253 entry->processed =1;3254if(entry->rename_conflict_info) {3255struct rename_conflict_info *conflict_info = entry->rename_conflict_info;3256switch(conflict_info->rename_type) {3257case RENAME_NORMAL:3258case RENAME_ONE_FILE_TO_ONE:3259 clean_merge =handle_rename_normal(o,3260 path,3261 o_oid, o_mode,3262 a_oid, a_mode,3263 b_oid, b_mode,3264 conflict_info);3265break;3266case RENAME_VIA_DIR:3267 clean_merge =1;3268if(handle_rename_via_dir(o,3269 conflict_info->pair1,3270 conflict_info->branch1,3271 conflict_info->branch2))3272 clean_merge = -1;3273break;3274case RENAME_ADD:3275/*3276 * Probably unclean merge, but if the renamed file3277 * merges cleanly and the result can then be3278 * two-way merged cleanly with the added file, I3279 * guess it's a clean merge?3280 */3281 clean_merge =handle_rename_add(o, conflict_info);3282break;3283case RENAME_DELETE:3284 clean_merge =0;3285if(handle_rename_delete(o,3286 conflict_info->pair1,3287 conflict_info->branch1,3288 conflict_info->branch2))3289 clean_merge = -1;3290break;3291case RENAME_ONE_FILE_TO_TWO:3292 clean_merge =0;3293if(handle_rename_rename_1to2(o, conflict_info))3294 clean_merge = -1;3295break;3296case RENAME_TWO_FILES_TO_ONE:3297/*3298 * Probably unclean merge, but if the two renamed3299 * files merge cleanly and the two resulting files3300 * can then be two-way merged cleanly, I guess it's3301 * a clean merge?3302 */3303 clean_merge =handle_rename_rename_2to1(o,3304 conflict_info);3305break;3306default:3307 entry->processed =0;3308break;3309}3310}else if(o_oid && (!a_oid || !b_oid)) {3311/* Case A: Deleted in one */3312if((!a_oid && !b_oid) ||3313(!b_oid &&blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||3314(!a_oid &&blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {3315/* Deleted in both or deleted in one and3316 * unchanged in the other */3317if(a_oid)3318output(o,2,_("Removing%s"), path);3319/* do not touch working file if it did not exist */3320remove_file(o,1, path, !a_oid);3321}else{3322/* Modify/delete; deleted side may have put a directory in the way */3323 clean_merge =0;3324if(handle_modify_delete(o, path, o_oid, o_mode,3325 a_oid, a_mode, b_oid, b_mode))3326 clean_merge = -1;3327}3328}else if((!o_oid && a_oid && !b_oid) ||3329(!o_oid && !a_oid && b_oid)) {3330/* Case B: Added in one. */3331/* [nothing|directory] -> ([nothing|directory], file) */33323333const char*add_branch;3334const char*other_branch;3335unsigned mode;3336const struct object_id *oid;3337const char*conf;33383339if(a_oid) {3340 add_branch = o->branch1;3341 other_branch = o->branch2;3342 mode = a_mode;3343 oid = a_oid;3344 conf =_("file/directory");3345}else{3346 add_branch = o->branch2;3347 other_branch = o->branch1;3348 mode = b_mode;3349 oid = b_oid;3350 conf =_("directory/file");3351}3352if(dir_in_way(o->repo->index, path,3353!o->call_depth && !S_ISGITLINK(a_mode),33540)) {3355char*new_path =unique_path(o, path, add_branch);3356 clean_merge =0;3357output(o,1,_("CONFLICT (%s): There is a directory with name%sin%s. "3358"Adding%sas%s"),3359 conf, path, other_branch, path, new_path);3360if(update_file(o,0, oid, mode, new_path))3361 clean_merge = -1;3362else if(o->call_depth)3363remove_file_from_index(o->repo->index, path);3364free(new_path);3365}else{3366output(o,2,_("Adding%s"), path);3367/* do not overwrite file if already present */3368if(update_file_flags(o, oid, mode, path,1, !a_oid))3369 clean_merge = -1;3370}3371}else if(a_oid && b_oid) {3372if(!o_oid) {3373/* Case C: Added in both (check for same permissions) */3374output(o,1,3375_("CONFLICT (add/add): Merge conflict in%s"),3376 path);3377 clean_merge =handle_file_collision(o,3378 path, NULL, NULL,3379 o->branch1,3380 o->branch2,3381 a_oid, a_mode,3382 b_oid, b_mode);3383}else{3384/* case D: Modified in both, but differently. */3385int is_dirty =0;/* unpack_trees would have bailed if dirty */3386 clean_merge =handle_content_merge(o, path,3387 is_dirty,3388 o_oid, o_mode,3389 a_oid, a_mode,3390 b_oid, b_mode,3391 NULL);3392}3393}else if(!o_oid && !a_oid && !b_oid) {3394/*3395 * this entry was deleted altogether. a_mode == 0 means3396 * we had that path and want to actively remove it.3397 */3398remove_file(o,1, path, !a_mode);3399}else3400BUG("fatal merge failure, shouldn't happen.");34013402return clean_merge;3403}34043405intmerge_trees(struct merge_options *o,3406struct tree *head,3407struct tree *merge,3408struct tree *common,3409struct tree **result)3410{3411struct index_state *istate = o->repo->index;3412int code, clean;3413struct strbuf sb = STRBUF_INIT;34143415if(!o->call_depth &&index_has_changes(istate, head, &sb)) {3416err(o,_("Your local changes to the following files would be overwritten by merge:\n%s"),3417 sb.buf);3418return-1;3419}34203421if(o->subtree_shift) {3422 merge =shift_tree_object(o->repo, head, merge, o->subtree_shift);3423 common =shift_tree_object(o->repo, head, common, o->subtree_shift);3424}34253426if(oid_eq(&common->object.oid, &merge->object.oid)) {3427output(o,0,_("Already up to date!"));3428*result = head;3429return1;3430}34313432 code =unpack_trees_start(o, common, head, merge);34333434if(code !=0) {3435if(show(o,4) || o->call_depth)3436err(o,_("merging of trees%sand%sfailed"),3437oid_to_hex(&head->object.oid),3438oid_to_hex(&merge->object.oid));3439unpack_trees_finish(o);3440return-1;3441}34423443if(unmerged_index(istate)) {3444struct string_list *entries;3445struct rename_info re_info;3446int i;3447/*3448 * Only need the hashmap while processing entries, so3449 * initialize it here and free it when we are done running3450 * through the entries. Keeping it in the merge_options as3451 * opposed to decaring a local hashmap is for convenience3452 * so that we don't have to pass it to around.3453 */3454hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL,512);3455get_files_dirs(o, head);3456get_files_dirs(o, merge);34573458 entries =get_unmerged(o->repo->index);3459 clean =detect_and_process_renames(o, common, head, merge,3460 entries, &re_info);3461record_df_conflict_files(o, entries);3462if(clean <0)3463goto cleanup;3464for(i = entries->nr-1;0<= i; i--) {3465const char*path = entries->items[i].string;3466struct stage_data *e = entries->items[i].util;3467if(!e->processed) {3468int ret =process_entry(o, path, e);3469if(!ret)3470 clean =0;3471else if(ret <0) {3472 clean = ret;3473goto cleanup;3474}3475}3476}3477for(i =0; i < entries->nr; i++) {3478struct stage_data *e = entries->items[i].util;3479if(!e->processed)3480BUG("unprocessed path???%s",3481 entries->items[i].string);3482}34833484 cleanup:3485final_cleanup_renames(&re_info);34863487string_list_clear(entries,1);3488free(entries);34893490hashmap_free(&o->current_file_dir_set,1);34913492if(clean <0) {3493unpack_trees_finish(o);3494return clean;3495}3496}3497else3498 clean =1;34993500unpack_trees_finish(o);35013502if(o->call_depth && !(*result =write_tree_from_memory(o)))3503return-1;35043505return clean;3506}35073508static struct commit_list *reverse_commit_list(struct commit_list *list)3509{3510struct commit_list *next = NULL, *current, *backup;3511for(current = list; current; current = backup) {3512 backup = current->next;3513 current->next = next;3514 next = current;3515}3516return next;3517}35183519/*3520 * Merge the commits h1 and h2, return the resulting virtual3521 * commit object and a flag indicating the cleanness of the merge.3522 */3523intmerge_recursive(struct merge_options *o,3524struct commit *h1,3525struct commit *h2,3526struct commit_list *ca,3527struct commit **result)3528{3529struct commit_list *iter;3530struct commit *merged_common_ancestors;3531struct tree *mrtree;3532int clean;35333534if(show(o,4)) {3535output(o,4,_("Merging:"));3536output_commit_title(o, h1);3537output_commit_title(o, h2);3538}35393540if(!ca) {3541 ca =get_merge_bases(h1, h2);3542 ca =reverse_commit_list(ca);3543}35443545if(show(o,5)) {3546unsigned cnt =commit_list_count(ca);35473548output(o,5,Q_("found%ucommon ancestor:",3549"found%ucommon ancestors:", cnt), cnt);3550for(iter = ca; iter; iter = iter->next)3551output_commit_title(o, iter->item);3552}35533554 merged_common_ancestors =pop_commit(&ca);3555if(merged_common_ancestors == NULL) {3556/* if there is no common ancestor, use an empty tree */3557struct tree *tree;35583559 tree =lookup_tree(o->repo, o->repo->hash_algo->empty_tree);3560 merged_common_ancestors =make_virtual_commit(o->repo, tree,"ancestor");3561}35623563for(iter = ca; iter; iter = iter->next) {3564const char*saved_b1, *saved_b2;3565 o->call_depth++;3566/*3567 * When the merge fails, the result contains files3568 * with conflict markers. The cleanness flag is3569 * ignored (unless indicating an error), it was never3570 * actually used, as result of merge_trees has always3571 * overwritten it: the committed "conflicts" were3572 * already resolved.3573 */3574discard_index(o->repo->index);3575 saved_b1 = o->branch1;3576 saved_b2 = o->branch2;3577 o->branch1 ="Temporary merge branch 1";3578 o->branch2 ="Temporary merge branch 2";3579if(merge_recursive(o, merged_common_ancestors, iter->item,3580 NULL, &merged_common_ancestors) <0)3581return-1;3582 o->branch1 = saved_b1;3583 o->branch2 = saved_b2;3584 o->call_depth--;35853586if(!merged_common_ancestors)3587returnerr(o,_("merge returned no commit"));3588}35893590discard_index(o->repo->index);3591if(!o->call_depth)3592repo_read_index(o->repo);35933594 o->ancestor ="merged common ancestors";3595 clean =merge_trees(o,get_commit_tree(h1),get_commit_tree(h2),3596get_commit_tree(merged_common_ancestors),3597&mrtree);3598if(clean <0) {3599flush_output(o);3600return clean;3601}36023603if(o->call_depth) {3604*result =make_virtual_commit(o->repo, mrtree,"merged tree");3605commit_list_insert(h1, &(*result)->parents);3606commit_list_insert(h2, &(*result)->parents->next);3607}3608flush_output(o);3609if(!o->call_depth && o->buffer_output <2)3610strbuf_release(&o->obuf);3611if(show(o,2))3612diff_warn_rename_limit("merge.renamelimit",3613 o->needed_rename_limit,0);3614return clean;3615}36163617static struct commit *get_ref(struct repository *repo,const struct object_id *oid,3618const char*name)3619{3620struct object *object;36213622 object =deref_tag(repo,parse_object(repo, oid),3623 name,strlen(name));3624if(!object)3625return NULL;3626if(object->type == OBJ_TREE)3627returnmake_virtual_commit(repo, (struct tree*)object, name);3628if(object->type != OBJ_COMMIT)3629return NULL;3630if(parse_commit((struct commit *)object))3631return NULL;3632return(struct commit *)object;3633}36343635intmerge_recursive_generic(struct merge_options *o,3636const struct object_id *head,3637const struct object_id *merge,3638int num_base_list,3639const struct object_id **base_list,3640struct commit **result)3641{3642int clean;3643struct lock_file lock = LOCK_INIT;3644struct commit *head_commit =get_ref(o->repo, head, o->branch1);3645struct commit *next_commit =get_ref(o->repo, merge, o->branch2);3646struct commit_list *ca = NULL;36473648if(base_list) {3649int i;3650for(i =0; i < num_base_list; ++i) {3651struct commit *base;3652if(!(base =get_ref(o->repo, base_list[i],oid_to_hex(base_list[i]))))3653returnerr(o,_("Could not parse object '%s'"),3654oid_to_hex(base_list[i]));3655commit_list_insert(base, &ca);3656}3657}36583659repo_hold_locked_index(o->repo, &lock, LOCK_DIE_ON_ERROR);3660 clean =merge_recursive(o, head_commit, next_commit, ca,3661 result);3662if(clean <0) {3663rollback_lock_file(&lock);3664return clean;3665}36663667if(write_locked_index(o->repo->index, &lock,3668 COMMIT_LOCK | SKIP_IF_UNCHANGED))3669returnerr(o,_("Unable to write index."));36703671return clean ?0:1;3672}36733674static voidmerge_recursive_config(struct merge_options *o)3675{3676char*value = NULL;3677git_config_get_int("merge.verbosity", &o->verbosity);3678git_config_get_int("diff.renamelimit", &o->diff_rename_limit);3679git_config_get_int("merge.renamelimit", &o->merge_rename_limit);3680if(!git_config_get_string("diff.renames", &value)) {3681 o->diff_detect_rename =git_config_rename("diff.renames", value);3682free(value);3683}3684if(!git_config_get_string("merge.renames", &value)) {3685 o->merge_detect_rename =git_config_rename("merge.renames", value);3686free(value);3687}3688git_config(git_xmerge_config, NULL);3689}36903691voidinit_merge_options(struct merge_options *o,3692struct repository *repo)3693{3694const char*merge_verbosity;3695memset(o,0,sizeof(struct merge_options));3696 o->repo = repo;3697 o->verbosity =2;3698 o->buffer_output =1;3699 o->diff_rename_limit = -1;3700 o->merge_rename_limit = -1;3701 o->renormalize =0;3702 o->diff_detect_rename = -1;3703 o->merge_detect_rename = -1;3704 o->detect_directory_renames =1;3705merge_recursive_config(o);3706 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3707if(merge_verbosity)3708 o->verbosity =strtol(merge_verbosity, NULL,10);3709if(o->verbosity >=5)3710 o->buffer_output =0;3711strbuf_init(&o->obuf,0);3712string_list_init(&o->df_conflict_file_set,1);3713}37143715intparse_merge_opt(struct merge_options *o,const char*s)3716{3717const char*arg;37183719if(!s || !*s)3720return-1;3721if(!strcmp(s,"ours"))3722 o->recursive_variant = MERGE_RECURSIVE_OURS;3723else if(!strcmp(s,"theirs"))3724 o->recursive_variant = MERGE_RECURSIVE_THEIRS;3725else if(!strcmp(s,"subtree"))3726 o->subtree_shift ="";3727else if(skip_prefix(s,"subtree=", &arg))3728 o->subtree_shift = arg;3729else if(!strcmp(s,"patience"))3730 o->xdl_opts =DIFF_WITH_ALG(o, PATIENCE_DIFF);3731else if(!strcmp(s,"histogram"))3732 o->xdl_opts =DIFF_WITH_ALG(o, HISTOGRAM_DIFF);3733else if(skip_prefix(s,"diff-algorithm=", &arg)) {3734long value =parse_algorithm_value(arg);3735if(value <0)3736return-1;3737/* clear out previous settings */3738DIFF_XDL_CLR(o, NEED_MINIMAL);3739 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3740 o->xdl_opts |= value;3741}3742else if(!strcmp(s,"ignore-space-change"))3743DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);3744else if(!strcmp(s,"ignore-all-space"))3745DIFF_XDL_SET(o, IGNORE_WHITESPACE);3746else if(!strcmp(s,"ignore-space-at-eol"))3747DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);3748else if(!strcmp(s,"ignore-cr-at-eol"))3749DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);3750else if(!strcmp(s,"renormalize"))3751 o->renormalize =1;3752else if(!strcmp(s,"no-renormalize"))3753 o->renormalize =0;3754else if(!strcmp(s,"no-renames"))3755 o->merge_detect_rename =0;3756else if(!strcmp(s,"find-renames")) {3757 o->merge_detect_rename =1;3758 o->rename_score =0;3759}3760else if(skip_prefix(s,"find-renames=", &arg) ||3761skip_prefix(s,"rename-threshold=", &arg)) {3762if((o->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3763return-1;3764 o->merge_detect_rename =1;3765}3766else3767return-1;3768return0;3769}