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 166staticinlinevoidset_commit_tree(struct commit *c,struct tree *t) 167{ 168 c->maybe_tree = t; 169} 170 171static struct commit *make_virtual_commit(struct repository *repo, 172struct tree *tree, 173const char*comment) 174{ 175struct commit *commit =alloc_commit_node(repo); 176 177set_merge_remote_desc(commit, comment, (struct object *)commit); 178set_commit_tree(commit, tree); 179 commit->object.parsed =1; 180return commit; 181} 182 183/* 184 * Since we use get_tree_entry(), which does not put the read object into 185 * the object pool, we cannot rely on a == b. 186 */ 187static intoid_eq(const struct object_id *a,const struct object_id *b) 188{ 189if(!a && !b) 190return2; 191return a && b &&oideq(a, b); 192} 193 194enum rename_type { 195 RENAME_NORMAL =0, 196 RENAME_VIA_DIR, 197 RENAME_ADD, 198 RENAME_DELETE, 199 RENAME_ONE_FILE_TO_ONE, 200 RENAME_ONE_FILE_TO_TWO, 201 RENAME_TWO_FILES_TO_ONE 202}; 203 204struct rename_conflict_info { 205enum rename_type rename_type; 206struct diff_filepair *pair1; 207struct diff_filepair *pair2; 208const char*branch1; 209const char*branch2; 210struct stage_data *dst_entry1; 211struct stage_data *dst_entry2; 212struct diff_filespec ren1_other; 213struct diff_filespec ren2_other; 214}; 215 216/* 217 * Since we want to write the index eventually, we cannot reuse the index 218 * for these (temporary) data. 219 */ 220struct stage_data { 221struct{ 222unsigned mode; 223struct object_id oid; 224} stages[4]; 225struct rename_conflict_info *rename_conflict_info; 226unsigned processed:1; 227}; 228 229staticinlinevoidsetup_rename_conflict_info(enum rename_type rename_type, 230struct diff_filepair *pair1, 231struct diff_filepair *pair2, 232const char*branch1, 233const char*branch2, 234struct stage_data *dst_entry1, 235struct stage_data *dst_entry2, 236struct merge_options *o, 237struct stage_data *src_entry1, 238struct stage_data *src_entry2) 239{ 240int ostage1 =0, ostage2; 241struct rename_conflict_info *ci; 242 243/* 244 * When we have two renames involved, it's easiest to get the 245 * correct things into stage 2 and 3, and to make sure that the 246 * content merge puts HEAD before the other branch if we just 247 * ensure that branch1 == o->branch1. So, simply flip arguments 248 * around if we don't have that. 249 */ 250if(dst_entry2 && branch1 != o->branch1) { 251setup_rename_conflict_info(rename_type, 252 pair2, pair1, 253 branch2, branch1, 254 dst_entry2, dst_entry1, 255 o, 256 src_entry2, src_entry1); 257return; 258} 259 260 ci =xcalloc(1,sizeof(struct rename_conflict_info)); 261 ci->rename_type = rename_type; 262 ci->pair1 = pair1; 263 ci->branch1 = branch1; 264 ci->branch2 = branch2; 265 266 ci->dst_entry1 = dst_entry1; 267 dst_entry1->rename_conflict_info = ci; 268 dst_entry1->processed =0; 269 270assert(!pair2 == !dst_entry2); 271if(dst_entry2) { 272 ci->dst_entry2 = dst_entry2; 273 ci->pair2 = pair2; 274 dst_entry2->rename_conflict_info = ci; 275} 276 277/* 278 * For each rename, there could have been 279 * modifications on the side of history where that 280 * file was not renamed. 281 */ 282if(rename_type == RENAME_ADD || 283 rename_type == RENAME_TWO_FILES_TO_ONE) { 284 ostage1 = o->branch1 == branch1 ?3:2; 285 286 ci->ren1_other.path = pair1->one->path; 287oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid); 288 ci->ren1_other.mode = src_entry1->stages[ostage1].mode; 289} 290 291if(rename_type == RENAME_TWO_FILES_TO_ONE) { 292 ostage2 = ostage1 ^1; 293 294 ci->ren2_other.path = pair2->one->path; 295oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid); 296 ci->ren2_other.mode = src_entry2->stages[ostage2].mode; 297} 298} 299 300static intshow(struct merge_options *o,int v) 301{ 302return(!o->call_depth && o->verbosity >= v) || o->verbosity >=5; 303} 304 305__attribute__((format(printf,3,4))) 306static voidoutput(struct merge_options *o,int v,const char*fmt, ...) 307{ 308va_list ap; 309 310if(!show(o, v)) 311return; 312 313strbuf_addchars(&o->obuf,' ', o->call_depth *2); 314 315va_start(ap, fmt); 316strbuf_vaddf(&o->obuf, fmt, ap); 317va_end(ap); 318 319strbuf_addch(&o->obuf,'\n'); 320if(!o->buffer_output) 321flush_output(o); 322} 323 324static voidoutput_commit_title(struct merge_options *o,struct commit *commit) 325{ 326struct merge_remote_desc *desc; 327 328strbuf_addchars(&o->obuf,' ', o->call_depth *2); 329 desc =merge_remote_util(commit); 330if(desc) 331strbuf_addf(&o->obuf,"virtual%s\n", desc->name); 332else{ 333strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid, 334 DEFAULT_ABBREV); 335strbuf_addch(&o->obuf,' '); 336if(parse_commit(commit) !=0) 337strbuf_addstr(&o->obuf,_("(bad commit)\n")); 338else{ 339const char*title; 340const char*msg =get_commit_buffer(commit, NULL); 341int len =find_commit_subject(msg, &title); 342if(len) 343strbuf_addf(&o->obuf,"%.*s\n", len, title); 344unuse_commit_buffer(commit, msg); 345} 346} 347flush_output(o); 348} 349 350static intadd_cacheinfo(struct merge_options *o, 351unsigned int mode,const struct object_id *oid, 352const char*path,int stage,int refresh,int options) 353{ 354struct index_state *istate = o->repo->index; 355struct cache_entry *ce; 356int ret; 357 358 ce =make_cache_entry(istate, mode, oid ? oid : &null_oid, path, stage,0); 359if(!ce) 360returnerr(o,_("add_cacheinfo failed for path '%s'; merge aborting."), path); 361 362 ret =add_index_entry(istate, ce, options); 363if(refresh) { 364struct cache_entry *nce; 365 366 nce =refresh_cache_entry(istate, ce, 367 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); 368if(!nce) 369returnerr(o,_("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); 370if(nce != ce) 371 ret =add_index_entry(istate, nce, options); 372} 373return ret; 374} 375 376static voidinit_tree_desc_from_tree(struct tree_desc *desc,struct tree *tree) 377{ 378parse_tree(tree); 379init_tree_desc(desc, tree->buffer, tree->size); 380} 381 382static intunpack_trees_start(struct merge_options *o, 383struct tree *common, 384struct tree *head, 385struct tree *merge) 386{ 387int rc; 388struct tree_desc t[3]; 389struct index_state tmp_index = { NULL }; 390 391memset(&o->unpack_opts,0,sizeof(o->unpack_opts)); 392if(o->call_depth) 393 o->unpack_opts.index_only =1; 394else 395 o->unpack_opts.update =1; 396 o->unpack_opts.merge =1; 397 o->unpack_opts.head_idx =2; 398 o->unpack_opts.fn = threeway_merge; 399 o->unpack_opts.src_index = o->repo->index; 400 o->unpack_opts.dst_index = &tmp_index; 401 o->unpack_opts.aggressive = !merge_detect_rename(o); 402setup_unpack_trees_porcelain(&o->unpack_opts,"merge"); 403 404init_tree_desc_from_tree(t+0, common); 405init_tree_desc_from_tree(t+1, head); 406init_tree_desc_from_tree(t+2, merge); 407 408 rc =unpack_trees(3, t, &o->unpack_opts); 409cache_tree_free(&o->repo->index->cache_tree); 410 411/* 412 * Update o->repo->index to match the new results, AFTER saving a copy 413 * in o->orig_index. Update src_index to point to the saved copy. 414 * (verify_uptodate() checks src_index, and the original index is 415 * the one that had the necessary modification timestamps.) 416 */ 417 o->orig_index = *o->repo->index; 418*o->repo->index = tmp_index; 419 o->unpack_opts.src_index = &o->orig_index; 420 421return rc; 422} 423 424static voidunpack_trees_finish(struct merge_options *o) 425{ 426discard_index(&o->orig_index); 427clear_unpack_trees_porcelain(&o->unpack_opts); 428} 429 430struct tree *write_tree_from_memory(struct merge_options *o) 431{ 432struct tree *result = NULL; 433struct index_state *istate = o->repo->index; 434 435if(unmerged_index(istate)) { 436int i; 437fprintf(stderr,"BUG: There are unmerged index entries:\n"); 438for(i =0; i < istate->cache_nr; i++) { 439const struct cache_entry *ce = istate->cache[i]; 440if(ce_stage(ce)) 441fprintf(stderr,"BUG:%d%.*s\n",ce_stage(ce), 442(int)ce_namelen(ce), ce->name); 443} 444BUG("unmerged index entries in merge-recursive.c"); 445} 446 447if(!istate->cache_tree) 448 istate->cache_tree =cache_tree(); 449 450if(!cache_tree_fully_valid(istate->cache_tree) && 451cache_tree_update(istate,0) <0) { 452err(o,_("error building trees")); 453return NULL; 454} 455 456 result =lookup_tree(o->repo, &istate->cache_tree->oid); 457 458return result; 459} 460 461static intsave_files_dirs(const struct object_id *oid, 462struct strbuf *base,const char*path, 463unsigned int mode,int stage,void*context) 464{ 465struct path_hashmap_entry *entry; 466int baselen = base->len; 467struct merge_options *o = context; 468 469strbuf_addstr(base, path); 470 471FLEX_ALLOC_MEM(entry, path, base->buf, base->len); 472hashmap_entry_init(entry,path_hash(entry->path)); 473hashmap_add(&o->current_file_dir_set, entry); 474 475strbuf_setlen(base, baselen); 476return(S_ISDIR(mode) ? READ_TREE_RECURSIVE :0); 477} 478 479static voidget_files_dirs(struct merge_options *o,struct tree *tree) 480{ 481struct pathspec match_all; 482memset(&match_all,0,sizeof(match_all)); 483read_tree_recursive(the_repository, tree,"",0,0, 484&match_all, save_files_dirs, o); 485} 486 487static intget_tree_entry_if_blob(const struct object_id *tree, 488const char*path, 489struct object_id *hashy, 490unsigned int*mode_o) 491{ 492int ret; 493 494 ret =get_tree_entry(tree, path, hashy, mode_o); 495if(S_ISDIR(*mode_o)) { 496oidcpy(hashy, &null_oid); 497*mode_o =0; 498} 499return ret; 500} 501 502/* 503 * Returns an index_entry instance which doesn't have to correspond to 504 * a real cache entry in Git's index. 505 */ 506static struct stage_data *insert_stage_data(const char*path, 507struct tree *o,struct tree *a,struct tree *b, 508struct string_list *entries) 509{ 510struct string_list_item *item; 511struct stage_data *e =xcalloc(1,sizeof(struct stage_data)); 512get_tree_entry_if_blob(&o->object.oid, path, 513&e->stages[1].oid, &e->stages[1].mode); 514get_tree_entry_if_blob(&a->object.oid, path, 515&e->stages[2].oid, &e->stages[2].mode); 516get_tree_entry_if_blob(&b->object.oid, path, 517&e->stages[3].oid, &e->stages[3].mode); 518 item =string_list_insert(entries, path); 519 item->util = e; 520return e; 521} 522 523/* 524 * Create a dictionary mapping file names to stage_data objects. The 525 * dictionary contains one entry for every path with a non-zero stage entry. 526 */ 527static struct string_list *get_unmerged(struct index_state *istate) 528{ 529struct string_list *unmerged =xcalloc(1,sizeof(struct string_list)); 530int i; 531 532 unmerged->strdup_strings =1; 533 534for(i =0; i < istate->cache_nr; i++) { 535struct string_list_item *item; 536struct stage_data *e; 537const struct cache_entry *ce = istate->cache[i]; 538if(!ce_stage(ce)) 539continue; 540 541 item =string_list_lookup(unmerged, ce->name); 542if(!item) { 543 item =string_list_insert(unmerged, ce->name); 544 item->util =xcalloc(1,sizeof(struct stage_data)); 545} 546 e = item->util; 547 e->stages[ce_stage(ce)].mode = ce->ce_mode; 548oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); 549} 550 551return unmerged; 552} 553 554static intstring_list_df_name_compare(const char*one,const char*two) 555{ 556int onelen =strlen(one); 557int twolen =strlen(two); 558/* 559 * Here we only care that entries for D/F conflicts are 560 * adjacent, in particular with the file of the D/F conflict 561 * appearing before files below the corresponding directory. 562 * The order of the rest of the list is irrelevant for us. 563 * 564 * To achieve this, we sort with df_name_compare and provide 565 * the mode S_IFDIR so that D/F conflicts will sort correctly. 566 * We use the mode S_IFDIR for everything else for simplicity, 567 * since in other cases any changes in their order due to 568 * sorting cause no problems for us. 569 */ 570int cmp =df_name_compare(one, onelen, S_IFDIR, 571 two, twolen, S_IFDIR); 572/* 573 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure 574 * that 'foo' comes before 'foo/bar'. 575 */ 576if(cmp) 577return cmp; 578return onelen - twolen; 579} 580 581static voidrecord_df_conflict_files(struct merge_options *o, 582struct string_list *entries) 583{ 584/* If there is a D/F conflict and the file for such a conflict 585 * currently exists in the working tree, we want to allow it to be 586 * removed to make room for the corresponding directory if needed. 587 * The files underneath the directories of such D/F conflicts will 588 * be processed before the corresponding file involved in the D/F 589 * conflict. If the D/F directory ends up being removed by the 590 * merge, then we won't have to touch the D/F file. If the D/F 591 * directory needs to be written to the working copy, then the D/F 592 * file will simply be removed (in make_room_for_path()) to make 593 * room for the necessary paths. Note that if both the directory 594 * and the file need to be present, then the D/F file will be 595 * reinstated with a new unique name at the time it is processed. 596 */ 597struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; 598const char*last_file = NULL; 599int last_len =0; 600int i; 601 602/* 603 * If we're merging merge-bases, we don't want to bother with 604 * any working directory changes. 605 */ 606if(o->call_depth) 607return; 608 609/* Ensure D/F conflicts are adjacent in the entries list. */ 610for(i =0; i < entries->nr; i++) { 611struct string_list_item *next = &entries->items[i]; 612string_list_append(&df_sorted_entries, next->string)->util = 613 next->util; 614} 615 df_sorted_entries.cmp = string_list_df_name_compare; 616string_list_sort(&df_sorted_entries); 617 618string_list_clear(&o->df_conflict_file_set,1); 619for(i =0; i < df_sorted_entries.nr; i++) { 620const char*path = df_sorted_entries.items[i].string; 621int len =strlen(path); 622struct stage_data *e = df_sorted_entries.items[i].util; 623 624/* 625 * Check if last_file & path correspond to a D/F conflict; 626 * i.e. whether path is last_file+'/'+<something>. 627 * If so, record that it's okay to remove last_file to make 628 * room for path and friends if needed. 629 */ 630if(last_file && 631 len > last_len && 632memcmp(path, last_file, last_len) ==0&& 633 path[last_len] =='/') { 634string_list_insert(&o->df_conflict_file_set, last_file); 635} 636 637/* 638 * Determine whether path could exist as a file in the 639 * working directory as a possible D/F conflict. This 640 * will only occur when it exists in stage 2 as a 641 * file. 642 */ 643if(S_ISREG(e->stages[2].mode) ||S_ISLNK(e->stages[2].mode)) { 644 last_file = path; 645 last_len = len; 646}else{ 647 last_file = NULL; 648} 649} 650string_list_clear(&df_sorted_entries,0); 651} 652 653struct rename { 654struct diff_filepair *pair; 655/* 656 * Purpose of src_entry and dst_entry: 657 * 658 * If 'before' is renamed to 'after' then src_entry will contain 659 * the versions of 'before' from the merge_base, HEAD, and MERGE in 660 * stages 1, 2, and 3; dst_entry will contain the respective 661 * versions of 'after' in corresponding locations. Thus, we have a 662 * total of six modes and oids, though some will be null. (Stage 0 663 * is ignored; we're interested in handling conflicts.) 664 * 665 * Since we don't turn on break-rewrites by default, neither 666 * src_entry nor dst_entry can have all three of their stages have 667 * non-null oids, meaning at most four of the six will be non-null. 668 * Also, since this is a rename, both src_entry and dst_entry will 669 * have at least one non-null oid, meaning at least two will be 670 * non-null. Of the six oids, a typical rename will have three be 671 * non-null. Only two implies a rename/delete, and four implies a 672 * rename/add. 673 */ 674struct stage_data *src_entry; 675struct stage_data *dst_entry; 676unsigned add_turned_into_rename:1; 677unsigned processed:1; 678}; 679 680static intupdate_stages(struct merge_options *opt,const char*path, 681const struct diff_filespec *o, 682const struct diff_filespec *a, 683const struct diff_filespec *b) 684{ 685 686/* 687 * NOTE: It is usually a bad idea to call update_stages on a path 688 * before calling update_file on that same path, since it can 689 * sometimes lead to spurious "refusing to lose untracked file..." 690 * messages from update_file (via make_room_for path via 691 * would_lose_untracked). Instead, reverse the order of the calls 692 * (executing update_file first and then update_stages). 693 */ 694int clear =1; 695int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; 696if(clear) 697if(remove_file_from_index(opt->repo->index, path)) 698return-1; 699if(o) 700if(add_cacheinfo(opt, o->mode, &o->oid, path,1,0, options)) 701return-1; 702if(a) 703if(add_cacheinfo(opt, a->mode, &a->oid, path,2,0, options)) 704return-1; 705if(b) 706if(add_cacheinfo(opt, b->mode, &b->oid, path,3,0, options)) 707return-1; 708return0; 709} 710 711static voidupdate_entry(struct stage_data *entry, 712struct diff_filespec *o, 713struct diff_filespec *a, 714struct diff_filespec *b) 715{ 716 entry->processed =0; 717 entry->stages[1].mode = o->mode; 718 entry->stages[2].mode = a->mode; 719 entry->stages[3].mode = b->mode; 720oidcpy(&entry->stages[1].oid, &o->oid); 721oidcpy(&entry->stages[2].oid, &a->oid); 722oidcpy(&entry->stages[3].oid, &b->oid); 723} 724 725static intremove_file(struct merge_options *o,int clean, 726const char*path,int no_wd) 727{ 728int update_cache = o->call_depth || clean; 729int update_working_directory = !o->call_depth && !no_wd; 730 731if(update_cache) { 732if(remove_file_from_index(o->repo->index, path)) 733return-1; 734} 735if(update_working_directory) { 736if(ignore_case) { 737struct cache_entry *ce; 738 ce =index_file_exists(o->repo->index, path,strlen(path), 739 ignore_case); 740if(ce &&ce_stage(ce) ==0&&strcmp(path, ce->name)) 741return0; 742} 743if(remove_path(path)) 744return-1; 745} 746return0; 747} 748 749/* add a string to a strbuf, but converting "/" to "_" */ 750static voidadd_flattened_path(struct strbuf *out,const char*s) 751{ 752size_t i = out->len; 753strbuf_addstr(out, s); 754for(; i < out->len; i++) 755if(out->buf[i] =='/') 756 out->buf[i] ='_'; 757} 758 759static char*unique_path(struct merge_options *o,const char*path,const char*branch) 760{ 761struct path_hashmap_entry *entry; 762struct strbuf newpath = STRBUF_INIT; 763int suffix =0; 764size_t base_len; 765 766strbuf_addf(&newpath,"%s~", path); 767add_flattened_path(&newpath, branch); 768 769 base_len = newpath.len; 770while(hashmap_get_from_hash(&o->current_file_dir_set, 771path_hash(newpath.buf), newpath.buf) || 772(!o->call_depth &&file_exists(newpath.buf))) { 773strbuf_setlen(&newpath, base_len); 774strbuf_addf(&newpath,"_%d", suffix++); 775} 776 777FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); 778hashmap_entry_init(entry,path_hash(entry->path)); 779hashmap_add(&o->current_file_dir_set, entry); 780returnstrbuf_detach(&newpath, NULL); 781} 782 783/** 784 * Check whether a directory in the index is in the way of an incoming 785 * file. Return 1 if so. If check_working_copy is non-zero, also 786 * check the working directory. If empty_ok is non-zero, also return 787 * 0 in the case where the working-tree dir exists but is empty. 788 */ 789static intdir_in_way(struct index_state *istate,const char*path, 790int check_working_copy,int empty_ok) 791{ 792int pos; 793struct strbuf dirpath = STRBUF_INIT; 794struct stat st; 795 796strbuf_addstr(&dirpath, path); 797strbuf_addch(&dirpath,'/'); 798 799 pos =index_name_pos(istate, dirpath.buf, dirpath.len); 800 801if(pos <0) 802 pos = -1- pos; 803if(pos < istate->cache_nr && 804!strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) { 805strbuf_release(&dirpath); 806return1; 807} 808 809strbuf_release(&dirpath); 810return check_working_copy && !lstat(path, &st) &&S_ISDIR(st.st_mode) && 811!(empty_ok &&is_empty_dir(path)); 812} 813 814/* 815 * Returns whether path was tracked in the index before the merge started, 816 * and its oid and mode match the specified values 817 */ 818static intwas_tracked_and_matches(struct merge_options *o,const char*path, 819const struct object_id *oid,unsigned mode) 820{ 821int pos =index_name_pos(&o->orig_index, path,strlen(path)); 822struct cache_entry *ce; 823 824if(0> pos) 825/* we were not tracking this path before the merge */ 826return0; 827 828/* See if the file we were tracking before matches */ 829 ce = o->orig_index.cache[pos]; 830return(oid_eq(&ce->oid, oid) && ce->ce_mode == mode); 831} 832 833/* 834 * Returns whether path was tracked in the index before the merge started 835 */ 836static intwas_tracked(struct merge_options *o,const char*path) 837{ 838int pos =index_name_pos(&o->orig_index, path,strlen(path)); 839 840if(0<= pos) 841/* we were tracking this path before the merge */ 842return1; 843 844return0; 845} 846 847static intwould_lose_untracked(struct merge_options *o,const char*path) 848{ 849struct index_state *istate = o->repo->index; 850 851/* 852 * This may look like it can be simplified to: 853 * return !was_tracked(o, path) && file_exists(path) 854 * but it can't. This function needs to know whether path was in 855 * the working tree due to EITHER having been tracked in the index 856 * before the merge OR having been put into the working copy and 857 * index by unpack_trees(). Due to that either-or requirement, we 858 * check the current index instead of the original one. 859 * 860 * Note that we do not need to worry about merge-recursive itself 861 * updating the index after unpack_trees() and before calling this 862 * function, because we strictly require all code paths in 863 * merge-recursive to update the working tree first and the index 864 * second. Doing otherwise would break 865 * update_file()/would_lose_untracked(); see every comment in this 866 * file which mentions "update_stages". 867 */ 868int pos =index_name_pos(istate, path,strlen(path)); 869 870if(pos <0) 871 pos = -1- pos; 872while(pos < istate->cache_nr && 873!strcmp(path, istate->cache[pos]->name)) { 874/* 875 * If stage #0, it is definitely tracked. 876 * If it has stage #2 then it was tracked 877 * before this merge started. All other 878 * cases the path was not tracked. 879 */ 880switch(ce_stage(istate->cache[pos])) { 881case0: 882case2: 883return0; 884} 885 pos++; 886} 887returnfile_exists(path); 888} 889 890static intwas_dirty(struct merge_options *o,const char*path) 891{ 892struct cache_entry *ce; 893int dirty =1; 894 895if(o->call_depth || !was_tracked(o, path)) 896return!dirty; 897 898 ce =index_file_exists(o->unpack_opts.src_index, 899 path,strlen(path), ignore_case); 900 dirty =verify_uptodate(ce, &o->unpack_opts) !=0; 901return dirty; 902} 903 904static intmake_room_for_path(struct merge_options *o,const char*path) 905{ 906int status, i; 907const char*msg =_("failed to create path '%s'%s"); 908 909/* Unlink any D/F conflict files that are in the way */ 910for(i =0; i < o->df_conflict_file_set.nr; i++) { 911const char*df_path = o->df_conflict_file_set.items[i].string; 912size_t pathlen =strlen(path); 913size_t df_pathlen =strlen(df_path); 914if(df_pathlen < pathlen && 915 path[df_pathlen] =='/'&& 916strncmp(path, df_path, df_pathlen) ==0) { 917output(o,3, 918_("Removing%sto make room for subdirectory\n"), 919 df_path); 920unlink(df_path); 921unsorted_string_list_delete_item(&o->df_conflict_file_set, 922 i,0); 923break; 924} 925} 926 927/* Make sure leading directories are created */ 928 status =safe_create_leading_directories_const(path); 929if(status) { 930if(status == SCLD_EXISTS) 931/* something else exists */ 932returnerr(o, msg, path,_(": perhaps a D/F conflict?")); 933returnerr(o, msg, path,""); 934} 935 936/* 937 * Do not unlink a file in the work tree if we are not 938 * tracking it. 939 */ 940if(would_lose_untracked(o, path)) 941returnerr(o,_("refusing to lose untracked file at '%s'"), 942 path); 943 944/* Successful unlink is good.. */ 945if(!unlink(path)) 946return0; 947/* .. and so is no existing file */ 948if(errno == ENOENT) 949return0; 950/* .. but not some other error (who really cares what?) */ 951returnerr(o, msg, path,_(": perhaps a D/F conflict?")); 952} 953 954static intupdate_file_flags(struct merge_options *o, 955const struct object_id *oid, 956unsigned mode, 957const char*path, 958int update_cache, 959int update_wd) 960{ 961int ret =0; 962 963if(o->call_depth) 964 update_wd =0; 965 966if(update_wd) { 967enum object_type type; 968void*buf; 969unsigned long size; 970 971if(S_ISGITLINK(mode)) { 972/* 973 * We may later decide to recursively descend into 974 * the submodule directory and update its index 975 * and/or work tree, but we do not do that now. 976 */ 977 update_wd =0; 978goto update_index; 979} 980 981 buf =read_object_file(oid, &type, &size); 982if(!buf) 983returnerr(o,_("cannot read object%s'%s'"),oid_to_hex(oid), path); 984if(type != OBJ_BLOB) { 985 ret =err(o,_("blob expected for%s'%s'"),oid_to_hex(oid), path); 986goto free_buf; 987} 988if(S_ISREG(mode)) { 989struct strbuf strbuf = STRBUF_INIT; 990if(convert_to_working_tree(o->repo->index, path, buf, size, &strbuf)) { 991free(buf); 992 size = strbuf.len; 993 buf =strbuf_detach(&strbuf, NULL); 994} 995} 996 997if(make_room_for_path(o, path) <0) { 998 update_wd =0; 999goto free_buf;1000}1001if(S_ISREG(mode) || (!has_symlinks &&S_ISLNK(mode))) {1002int fd;1003if(mode &0100)1004 mode =0777;1005else1006 mode =0666;1007 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);1008if(fd <0) {1009 ret =err(o,_("failed to open '%s':%s"),1010 path,strerror(errno));1011goto free_buf;1012}1013write_in_full(fd, buf, size);1014close(fd);1015}else if(S_ISLNK(mode)) {1016char*lnk =xmemdupz(buf, size);1017safe_create_leading_directories_const(path);1018unlink(path);1019if(symlink(lnk, path))1020 ret =err(o,_("failed to symlink '%s':%s"),1021 path,strerror(errno));1022free(lnk);1023}else1024 ret =err(o,1025_("do not know what to do with%06o%s'%s'"),1026 mode,oid_to_hex(oid), path);1027 free_buf:1028free(buf);1029}1030update_index:1031if(!ret && update_cache)1032if(add_cacheinfo(o, mode, oid, path,0, update_wd,1033 ADD_CACHE_OK_TO_ADD))1034return-1;1035return ret;1036}10371038static intupdate_file(struct merge_options *o,1039int clean,1040const struct object_id *oid,1041unsigned mode,1042const char*path)1043{1044returnupdate_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);1045}10461047/* Low level file merging, update and removal */10481049struct merge_file_info {1050struct object_id oid;1051unsigned mode;1052unsigned clean:1,1053 merge:1;1054};10551056static intmerge_3way(struct merge_options *o,1057 mmbuffer_t *result_buf,1058const struct diff_filespec *one,1059const struct diff_filespec *a,1060const struct diff_filespec *b,1061const char*branch1,1062const char*branch2,1063const int extra_marker_size)1064{1065 mmfile_t orig, src1, src2;1066struct ll_merge_options ll_opts = {0};1067char*base_name, *name1, *name2;1068int merge_status;10691070 ll_opts.renormalize = o->renormalize;1071 ll_opts.extra_marker_size = extra_marker_size;1072 ll_opts.xdl_opts = o->xdl_opts;10731074if(o->call_depth) {1075 ll_opts.virtual_ancestor =1;1076 ll_opts.variant =0;1077}else{1078switch(o->recursive_variant) {1079case MERGE_RECURSIVE_OURS:1080 ll_opts.variant = XDL_MERGE_FAVOR_OURS;1081break;1082case MERGE_RECURSIVE_THEIRS:1083 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;1084break;1085default:1086 ll_opts.variant =0;1087break;1088}1089}10901091if(strcmp(a->path, b->path) ||1092(o->ancestor != NULL &&strcmp(a->path, one->path) !=0)) {1093 base_name = o->ancestor == NULL ? NULL :1094mkpathdup("%s:%s", o->ancestor, one->path);1095 name1 =mkpathdup("%s:%s", branch1, a->path);1096 name2 =mkpathdup("%s:%s", branch2, b->path);1097}else{1098 base_name = o->ancestor == NULL ? NULL :1099mkpathdup("%s", o->ancestor);1100 name1 =mkpathdup("%s", branch1);1101 name2 =mkpathdup("%s", branch2);1102}11031104read_mmblob(&orig, &one->oid);1105read_mmblob(&src1, &a->oid);1106read_mmblob(&src2, &b->oid);11071108 merge_status =ll_merge(result_buf, a->path, &orig, base_name,1109&src1, name1, &src2, name2,1110 o->repo->index, &ll_opts);11111112free(base_name);1113free(name1);1114free(name2);1115free(orig.ptr);1116free(src1.ptr);1117free(src2.ptr);1118return merge_status;1119}11201121static intfind_first_merges(struct repository *repo,1122struct object_array *result,const char*path,1123struct commit *a,struct commit *b)1124{1125int i, j;1126struct object_array merges = OBJECT_ARRAY_INIT;1127struct commit *commit;1128int contains_another;11291130char merged_revision[42];1131const char*rev_args[] = {"rev-list","--merges","--ancestry-path",1132"--all", merged_revision, NULL };1133struct rev_info revs;1134struct setup_revision_opt rev_opts;11351136memset(result,0,sizeof(struct object_array));1137memset(&rev_opts,0,sizeof(rev_opts));11381139/* get all revisions that merge commit a */1140xsnprintf(merged_revision,sizeof(merged_revision),"^%s",1141oid_to_hex(&a->object.oid));1142repo_init_revisions(repo, &revs, NULL);1143 rev_opts.submodule = path;1144/* FIXME: can't handle linked worktrees in submodules yet */1145 revs.single_worktree = path != NULL;1146setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);11471148/* save all revisions from the above list that contain b */1149if(prepare_revision_walk(&revs))1150die("revision walk setup failed");1151while((commit =get_revision(&revs)) != NULL) {1152struct object *o = &(commit->object);1153if(in_merge_bases(b, commit))1154add_object_array(o, NULL, &merges);1155}1156reset_revision_walk();11571158/* Now we've got all merges that contain a and b. Prune all1159 * merges that contain another found merge and save them in1160 * result.1161 */1162for(i =0; i < merges.nr; i++) {1163struct commit *m1 = (struct commit *) merges.objects[i].item;11641165 contains_another =0;1166for(j =0; j < merges.nr; j++) {1167struct commit *m2 = (struct commit *) merges.objects[j].item;1168if(i != j &&in_merge_bases(m2, m1)) {1169 contains_another =1;1170break;1171}1172}11731174if(!contains_another)1175add_object_array(merges.objects[i].item, NULL, result);1176}11771178object_array_clear(&merges);1179return result->nr;1180}11811182static voidprint_commit(struct commit *commit)1183{1184struct strbuf sb = STRBUF_INIT;1185struct pretty_print_context ctx = {0};1186 ctx.date_mode.type = DATE_NORMAL;1187format_commit_message(commit,"%h:%m %s", &sb, &ctx);1188fprintf(stderr,"%s\n", sb.buf);1189strbuf_release(&sb);1190}11911192static intmerge_submodule(struct merge_options *o,1193struct object_id *result,const char*path,1194const struct object_id *base,const struct object_id *a,1195const struct object_id *b)1196{1197struct commit *commit_base, *commit_a, *commit_b;1198int parent_count;1199struct object_array merges;12001201int i;1202int search = !o->call_depth;12031204/* store a in result in case we fail */1205oidcpy(result, a);12061207/* we can not handle deletion conflicts */1208if(is_null_oid(base))1209return0;1210if(is_null_oid(a))1211return0;1212if(is_null_oid(b))1213return0;12141215if(add_submodule_odb(path)) {1216output(o,1,_("Failed to merge submodule%s(not checked out)"), path);1217return0;1218}12191220if(!(commit_base =lookup_commit_reference(o->repo, base)) ||1221!(commit_a =lookup_commit_reference(o->repo, a)) ||1222!(commit_b =lookup_commit_reference(o->repo, b))) {1223output(o,1,_("Failed to merge submodule%s(commits not present)"), path);1224return0;1225}12261227/* check whether both changes are forward */1228if(!in_merge_bases(commit_base, commit_a) ||1229!in_merge_bases(commit_base, commit_b)) {1230output(o,1,_("Failed to merge submodule%s(commits don't follow merge-base)"), path);1231return0;1232}12331234/* Case #1: a is contained in b or vice versa */1235if(in_merge_bases(commit_a, commit_b)) {1236oidcpy(result, b);1237if(show(o,3)) {1238output(o,3,_("Fast-forwarding submodule%sto the following commit:"), path);1239output_commit_title(o, commit_b);1240}else if(show(o,2))1241output(o,2,_("Fast-forwarding submodule%s"), path);1242else1243;/* no output */12441245return1;1246}1247if(in_merge_bases(commit_b, commit_a)) {1248oidcpy(result, a);1249if(show(o,3)) {1250output(o,3,_("Fast-forwarding submodule%sto the following commit:"), path);1251output_commit_title(o, commit_a);1252}else if(show(o,2))1253output(o,2,_("Fast-forwarding submodule%s"), path);1254else1255;/* no output */12561257return1;1258}12591260/*1261 * Case #2: There are one or more merges that contain a and b in1262 * the submodule. If there is only one, then present it as a1263 * suggestion to the user, but leave it marked unmerged so the1264 * user needs to confirm the resolution.1265 */12661267/* Skip the search if makes no sense to the calling context. */1268if(!search)1269return0;12701271/* find commit which merges them */1272 parent_count =find_first_merges(o->repo, &merges, path,1273 commit_a, commit_b);1274switch(parent_count) {1275case0:1276output(o,1,_("Failed to merge submodule%s(merge following commits not found)"), path);1277break;12781279case1:1280output(o,1,_("Failed to merge submodule%s(not fast-forward)"), path);1281output(o,2,_("Found a possible merge resolution for the submodule:\n"));1282print_commit((struct commit *) merges.objects[0].item);1283output(o,2,_(1284"If this is correct simply add it to the index "1285"for example\n"1286"by using:\n\n"1287" git update-index --cacheinfo 160000%s\"%s\"\n\n"1288"which will accept this suggestion.\n"),1289oid_to_hex(&merges.objects[0].item->oid), path);1290break;12911292default:1293output(o,1,_("Failed to merge submodule%s(multiple merges found)"), path);1294for(i =0; i < merges.nr; i++)1295print_commit((struct commit *) merges.objects[i].item);1296}12971298object_array_clear(&merges);1299return0;1300}13011302static intmerge_mode_and_contents(struct merge_options *o,1303const struct diff_filespec *one,1304const struct diff_filespec *a,1305const struct diff_filespec *b,1306const char*filename,1307const char*branch1,1308const char*branch2,1309const int extra_marker_size,1310struct merge_file_info *result)1311{1312if(o->branch1 != branch1) {1313/*1314 * It's weird getting a reverse merge with HEAD on the bottom1315 * side of the conflict markers and the other branch on the1316 * top. Fix that.1317 */1318returnmerge_mode_and_contents(o, one, b, a,1319 filename,1320 branch2, branch1,1321 extra_marker_size, result);1322}13231324 result->merge =0;1325 result->clean =1;13261327if((S_IFMT & a->mode) != (S_IFMT & b->mode)) {1328 result->clean =0;1329if(S_ISREG(a->mode)) {1330 result->mode = a->mode;1331oidcpy(&result->oid, &a->oid);1332}else{1333 result->mode = b->mode;1334oidcpy(&result->oid, &b->oid);1335}1336}else{1337if(!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))1338 result->merge =1;13391340/*1341 * Merge modes1342 */1343if(a->mode == b->mode || a->mode == one->mode)1344 result->mode = b->mode;1345else{1346 result->mode = a->mode;1347if(b->mode != one->mode) {1348 result->clean =0;1349 result->merge =1;1350}1351}13521353if(oid_eq(&a->oid, &b->oid) ||oid_eq(&a->oid, &one->oid))1354oidcpy(&result->oid, &b->oid);1355else if(oid_eq(&b->oid, &one->oid))1356oidcpy(&result->oid, &a->oid);1357else if(S_ISREG(a->mode)) {1358 mmbuffer_t result_buf;1359int ret =0, merge_status;13601361 merge_status =merge_3way(o, &result_buf, one, a, b,1362 branch1, branch2,1363 extra_marker_size);13641365if((merge_status <0) || !result_buf.ptr)1366 ret =err(o,_("Failed to execute internal merge"));13671368if(!ret &&1369write_object_file(result_buf.ptr, result_buf.size,1370 blob_type, &result->oid))1371 ret =err(o,_("Unable to add%sto database"),1372 a->path);13731374free(result_buf.ptr);1375if(ret)1376return ret;1377 result->clean = (merge_status ==0);1378}else if(S_ISGITLINK(a->mode)) {1379 result->clean =merge_submodule(o, &result->oid,1380 one->path,1381&one->oid,1382&a->oid,1383&b->oid);1384}else if(S_ISLNK(a->mode)) {1385switch(o->recursive_variant) {1386case MERGE_RECURSIVE_NORMAL:1387oidcpy(&result->oid, &a->oid);1388if(!oid_eq(&a->oid, &b->oid))1389 result->clean =0;1390break;1391case MERGE_RECURSIVE_OURS:1392oidcpy(&result->oid, &a->oid);1393break;1394case MERGE_RECURSIVE_THEIRS:1395oidcpy(&result->oid, &b->oid);1396break;1397}1398}else1399BUG("unsupported object type in the tree");1400}14011402if(result->merge)1403output(o,2,_("Auto-merging%s"), filename);14041405return0;1406}14071408static inthandle_rename_via_dir(struct merge_options *o,1409struct diff_filepair *pair,1410const char*rename_branch)1411{1412/*1413 * Handle file adds that need to be renamed due to directory rename1414 * detection. This differs from handle_rename_normal, because1415 * there is no content merge to do; just move the file into the1416 * desired final location.1417 */1418const struct diff_filespec *dest = pair->two;14191420if(!o->call_depth &&would_lose_untracked(o, dest->path)) {1421char*alt_path =unique_path(o, dest->path, rename_branch);14221423output(o,1,_("Error: Refusing to lose untracked file at%s; "1424"writing to%sinstead."),1425 dest->path, alt_path);1426/*1427 * Write the file in worktree at alt_path, but not in the1428 * index. Instead, write to dest->path for the index but1429 * only at the higher appropriate stage.1430 */1431if(update_file(o,0, &dest->oid, dest->mode, alt_path))1432return-1;1433free(alt_path);1434returnupdate_stages(o, dest->path, NULL,1435 rename_branch == o->branch1 ? dest : NULL,1436 rename_branch == o->branch1 ? NULL : dest);1437}14381439/* Update dest->path both in index and in worktree */1440if(update_file(o,1, &dest->oid, dest->mode, dest->path))1441return-1;1442return0;1443}14441445static inthandle_change_delete(struct merge_options *o,1446const char*path,const char*old_path,1447const struct object_id *o_oid,int o_mode,1448const struct object_id *changed_oid,1449int changed_mode,1450const char*change_branch,1451const char*delete_branch,1452const char*change,const char*change_past)1453{1454char*alt_path = NULL;1455const char*update_path = path;1456int ret =0;14571458if(dir_in_way(o->repo->index, path, !o->call_depth,0) ||1459(!o->call_depth &&would_lose_untracked(o, path))) {1460 update_path = alt_path =unique_path(o, path, change_branch);1461}14621463if(o->call_depth) {1464/*1465 * We cannot arbitrarily accept either a_sha or b_sha as1466 * correct; since there is no true "middle point" between1467 * them, simply reuse the base version for virtual merge base.1468 */1469 ret =remove_file_from_index(o->repo->index, path);1470if(!ret)1471 ret =update_file(o,0, o_oid, o_mode, update_path);1472}else{1473/*1474 * Despite the four nearly duplicate messages and argument1475 * lists below and the ugliness of the nested if-statements,1476 * having complete messages makes the job easier for1477 * translators.1478 *1479 * The slight variance among the cases is due to the fact1480 * that:1481 * 1) directory/file conflicts (in effect if1482 * !alt_path) could cause us to need to write the1483 * file to a different path.1484 * 2) renames (in effect if !old_path) could mean that1485 * there are two names for the path that the user1486 * may know the file by.1487 */1488if(!alt_path) {1489if(!old_path) {1490output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1491"and%sin%s. Version%sof%sleft in tree."),1492 change, path, delete_branch, change_past,1493 change_branch, change_branch, path);1494}else{1495output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1496"and%sto%sin%s. Version%sof%sleft in tree."),1497 change, old_path, delete_branch, change_past, path,1498 change_branch, change_branch, path);1499}1500}else{1501if(!old_path) {1502output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1503"and%sin%s. Version%sof%sleft in tree at%s."),1504 change, path, delete_branch, change_past,1505 change_branch, change_branch, path, alt_path);1506}else{1507output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1508"and%sto%sin%s. Version%sof%sleft in tree at%s."),1509 change, old_path, delete_branch, change_past, path,1510 change_branch, change_branch, path, alt_path);1511}1512}1513/*1514 * No need to call update_file() on path when change_branch ==1515 * o->branch1 && !alt_path, since that would needlessly touch1516 * path. We could call update_file_flags() with update_cache=01517 * and update_wd=0, but that's a no-op.1518 */1519if(change_branch != o->branch1 || alt_path)1520 ret =update_file(o,0, changed_oid, changed_mode, update_path);1521}1522free(alt_path);15231524return ret;1525}15261527static inthandle_rename_delete(struct merge_options *o,1528struct diff_filepair *pair,1529const char*rename_branch,1530const char*delete_branch)1531{1532const struct diff_filespec *orig = pair->one;1533const struct diff_filespec *dest = pair->two;15341535if(handle_change_delete(o,1536 o->call_depth ? orig->path : dest->path,1537 o->call_depth ? NULL : orig->path,1538&orig->oid, orig->mode,1539&dest->oid, dest->mode,1540 rename_branch, delete_branch,1541_("rename"),_("renamed")))1542return-1;15431544if(o->call_depth)1545returnremove_file_from_index(o->repo->index, dest->path);1546else1547returnupdate_stages(o, dest->path, NULL,1548 rename_branch == o->branch1 ? dest : NULL,1549 rename_branch == o->branch1 ? NULL : dest);1550}15511552static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1553struct stage_data *entry,1554int stage)1555{1556struct object_id *oid = &entry->stages[stage].oid;1557unsigned mode = entry->stages[stage].mode;1558if(mode ==0||is_null_oid(oid))1559return NULL;1560oidcpy(&target->oid, oid);1561 target->mode = mode;1562return target;1563}15641565static inthandle_file_collision(struct merge_options *o,1566const char*collide_path,1567const char*prev_path1,1568const char*prev_path2,1569const char*branch1,const char*branch2,1570const struct object_id *a_oid,1571unsigned int a_mode,1572const struct object_id *b_oid,1573unsigned int b_mode)1574{1575struct merge_file_info mfi;1576struct diff_filespec null, a, b;1577char*alt_path = NULL;1578const char*update_path = collide_path;15791580/*1581 * It's easiest to get the correct things into stage 2 and 3, and1582 * to make sure that the content merge puts HEAD before the other1583 * branch if we just ensure that branch1 == o->branch1. So, simply1584 * flip arguments around if we don't have that.1585 */1586if(branch1 != o->branch1) {1587returnhandle_file_collision(o, collide_path,1588 prev_path2, prev_path1,1589 branch2, branch1,1590 b_oid, b_mode,1591 a_oid, a_mode);1592}15931594/*1595 * In the recursive case, we just opt to undo renames1596 */1597if(o->call_depth && (prev_path1 || prev_path2)) {1598/* Put first file (a_oid, a_mode) in its original spot */1599if(prev_path1) {1600if(update_file(o,1, a_oid, a_mode, prev_path1))1601return-1;1602}else{1603if(update_file(o,1, a_oid, a_mode, collide_path))1604return-1;1605}16061607/* Put second file (b_oid, b_mode) in its original spot */1608if(prev_path2) {1609if(update_file(o,1, b_oid, b_mode, prev_path2))1610return-1;1611}else{1612if(update_file(o,1, b_oid, b_mode, collide_path))1613return-1;1614}16151616/* Don't leave something at collision path if unrenaming both */1617if(prev_path1 && prev_path2)1618remove_file(o,1, collide_path,0);16191620return0;1621}16221623/* Remove rename sources if rename/add or rename/rename(2to1) */1624if(prev_path1)1625remove_file(o,1, prev_path1,1626 o->call_depth ||would_lose_untracked(o, prev_path1));1627if(prev_path2)1628remove_file(o,1, prev_path2,1629 o->call_depth ||would_lose_untracked(o, prev_path2));16301631/*1632 * Remove the collision path, if it wouldn't cause dirty contents1633 * or an untracked file to get lost. We'll either overwrite with1634 * merged contents, or just write out to differently named files.1635 */1636if(was_dirty(o, collide_path)) {1637output(o,1,_("Refusing to lose dirty file at%s"),1638 collide_path);1639 update_path = alt_path =unique_path(o, collide_path,"merged");1640}else if(would_lose_untracked(o, collide_path)) {1641/*1642 * Only way we get here is if both renames were from1643 * a directory rename AND user had an untracked file1644 * at the location where both files end up after the1645 * two directory renames. See testcase 10d of t6043.1646 */1647output(o,1,_("Refusing to lose untracked file at "1648"%s, even though it's in the way."),1649 collide_path);1650 update_path = alt_path =unique_path(o, collide_path,"merged");1651}else{1652/*1653 * FIXME: It's possible that the two files are identical1654 * and that the current working copy happens to match, in1655 * which case we are unnecessarily touching the working1656 * tree file. It's not a likely enough scenario that I1657 * want to code up the checks for it and a better fix is1658 * available if we restructure how unpack_trees() and1659 * merge-recursive interoperate anyway, so punting for1660 * now...1661 */1662remove_file(o,0, collide_path,0);1663}16641665/* Store things in diff_filespecs for functions that need it */1666memset(&a,0,sizeof(struct diff_filespec));1667memset(&b,0,sizeof(struct diff_filespec));1668 null.path = a.path = b.path = (char*)collide_path;1669oidcpy(&null.oid, &null_oid);1670 null.mode =0;1671oidcpy(&a.oid, a_oid);1672 a.mode = a_mode;1673 a.oid_valid =1;1674oidcpy(&b.oid, b_oid);1675 b.mode = b_mode;1676 b.oid_valid =1;16771678if(merge_mode_and_contents(o, &null, &a, &b, collide_path,1679 branch1, branch2, o->call_depth *2, &mfi))1680return-1;1681 mfi.clean &= !alt_path;1682if(update_file(o, mfi.clean, &mfi.oid, mfi.mode, update_path))1683return-1;1684if(!mfi.clean && !o->call_depth &&1685update_stages(o, collide_path, NULL, &a, &b))1686return-1;1687free(alt_path);1688/*1689 * FIXME: If both a & b both started with conflicts (only possible1690 * if they came from a rename/rename(2to1)), but had IDENTICAL1691 * contents including those conflicts, then in the next line we claim1692 * it was clean. If someone cares about this case, we should have the1693 * caller notify us if we started with conflicts.1694 */1695return mfi.clean;1696}16971698static inthandle_rename_add(struct merge_options *o,1699struct rename_conflict_info *ci)1700{1701/* a was renamed to c, and a separate c was added. */1702struct diff_filespec *a = ci->pair1->one;1703struct diff_filespec *c = ci->pair1->two;1704char*path = c->path;1705char*prev_path_desc;1706struct merge_file_info mfi;17071708int other_stage = (ci->branch1 == o->branch1 ?3:2);17091710output(o,1,_("CONFLICT (rename/add): "1711"Rename%s->%sin%s. Added%sin%s"),1712 a->path, c->path, ci->branch1,1713 c->path, ci->branch2);17141715 prev_path_desc =xstrfmt("version of%sfrom%s", path, a->path);1716if(merge_mode_and_contents(o, a, c, &ci->ren1_other, prev_path_desc,1717 o->branch1, o->branch2,17181+ o->call_depth *2, &mfi))1719return-1;1720free(prev_path_desc);17211722returnhandle_file_collision(o,1723 c->path, a->path, NULL,1724 ci->branch1, ci->branch2,1725&mfi.oid, mfi.mode,1726&ci->dst_entry1->stages[other_stage].oid,1727 ci->dst_entry1->stages[other_stage].mode);1728}17291730static char*find_path_for_conflict(struct merge_options *o,1731const char*path,1732const char*branch1,1733const char*branch2)1734{1735char*new_path = NULL;1736if(dir_in_way(o->repo->index, path, !o->call_depth,0)) {1737 new_path =unique_path(o, path, branch1);1738output(o,1,_("%sis a directory in%sadding "1739"as%sinstead"),1740 path, branch2, new_path);1741}else if(would_lose_untracked(o, path)) {1742 new_path =unique_path(o, path, branch1);1743output(o,1,_("Refusing to lose untracked file"1744" at%s; adding as%sinstead"),1745 path, new_path);1746}17471748return new_path;1749}17501751static inthandle_rename_rename_1to2(struct merge_options *o,1752struct rename_conflict_info *ci)1753{1754/* One file was renamed in both branches, but to different names. */1755struct merge_file_info mfi;1756struct diff_filespec other;1757struct diff_filespec *add;1758struct diff_filespec *one = ci->pair1->one;1759struct diff_filespec *a = ci->pair1->two;1760struct diff_filespec *b = ci->pair2->two;1761char*path_desc;17621763output(o,1,_("CONFLICT (rename/rename): "1764"Rename\"%s\"->\"%s\"in branch\"%s\""1765"rename\"%s\"->\"%s\"in\"%s\"%s"),1766 one->path, a->path, ci->branch1,1767 one->path, b->path, ci->branch2,1768 o->call_depth ?_(" (left unresolved)") :"");17691770 path_desc =xstrfmt("%sand%s, both renamed from%s",1771 a->path, b->path, one->path);1772if(merge_mode_and_contents(o, one, a, b, path_desc,1773 ci->branch1, ci->branch2,1774 o->call_depth *2, &mfi))1775return-1;1776free(path_desc);17771778if(o->call_depth) {1779/*1780 * FIXME: For rename/add-source conflicts (if we could detect1781 * such), this is wrong. We should instead find a unique1782 * pathname and then either rename the add-source file to that1783 * unique path, or use that unique path instead of src here.1784 */1785if(update_file(o,0, &mfi.oid, mfi.mode, one->path))1786return-1;17871788/*1789 * Above, we put the merged content at the merge-base's1790 * path. Now we usually need to delete both a->path and1791 * b->path. However, the rename on each side of the merge1792 * could also be involved in a rename/add conflict. In1793 * such cases, we should keep the added file around,1794 * resolving the conflict at that path in its favor.1795 */1796 add =filespec_from_entry(&other, ci->dst_entry1,2^1);1797if(add) {1798if(update_file(o,0, &add->oid, add->mode, a->path))1799return-1;1800}1801else1802remove_file_from_index(o->repo->index, a->path);1803 add =filespec_from_entry(&other, ci->dst_entry2,3^1);1804if(add) {1805if(update_file(o,0, &add->oid, add->mode, b->path))1806return-1;1807}1808else1809remove_file_from_index(o->repo->index, b->path);1810}else{1811/*1812 * For each destination path, we need to see if there is a1813 * rename/add collision. If not, we can write the file out1814 * to the specified location.1815 */1816 add =filespec_from_entry(&other, ci->dst_entry1,2^1);1817if(add) {1818if(handle_file_collision(o, a->path,1819 NULL, NULL,1820 ci->branch1, ci->branch2,1821&mfi.oid, mfi.mode,1822&add->oid, add->mode) <0)1823return-1;1824}else{1825char*new_path =find_path_for_conflict(o, a->path,1826 ci->branch1,1827 ci->branch2);1828if(update_file(o,0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))1829return-1;1830free(new_path);1831if(update_stages(o, a->path, NULL, a, NULL))1832return-1;1833}18341835 add =filespec_from_entry(&other, ci->dst_entry2,3^1);1836if(add) {1837if(handle_file_collision(o, b->path,1838 NULL, NULL,1839 ci->branch1, ci->branch2,1840&add->oid, add->mode,1841&mfi.oid, mfi.mode) <0)1842return-1;1843}else{1844char*new_path =find_path_for_conflict(o, b->path,1845 ci->branch2,1846 ci->branch1);1847if(update_file(o,0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))1848return-1;1849free(new_path);1850if(update_stages(o, b->path, NULL, NULL, b))1851return-1;1852}1853}18541855return0;1856}18571858static inthandle_rename_rename_2to1(struct merge_options *o,1859struct rename_conflict_info *ci)1860{1861/* Two files, a & b, were renamed to the same thing, c. */1862struct diff_filespec *a = ci->pair1->one;1863struct diff_filespec *b = ci->pair2->one;1864struct diff_filespec *c1 = ci->pair1->two;1865struct diff_filespec *c2 = ci->pair2->two;1866char*path = c1->path;/* == c2->path */1867char*path_side_1_desc;1868char*path_side_2_desc;1869struct merge_file_info mfi_c1;1870struct merge_file_info mfi_c2;18711872output(o,1,_("CONFLICT (rename/rename): "1873"Rename%s->%sin%s. "1874"Rename%s->%sin%s"),1875 a->path, c1->path, ci->branch1,1876 b->path, c2->path, ci->branch2);18771878 path_side_1_desc =xstrfmt("version of%sfrom%s", path, a->path);1879 path_side_2_desc =xstrfmt("version of%sfrom%s", path, b->path);1880if(merge_mode_and_contents(o, a, c1, &ci->ren1_other, path_side_1_desc,1881 o->branch1, o->branch2,18821+ o->call_depth *2, &mfi_c1) ||1883merge_mode_and_contents(o, b, &ci->ren2_other, c2, path_side_2_desc,1884 o->branch1, o->branch2,18851+ o->call_depth *2, &mfi_c2))1886return-1;1887free(path_side_1_desc);1888free(path_side_2_desc);18891890returnhandle_file_collision(o, path, a->path, b->path,1891 ci->branch1, ci->branch2,1892&mfi_c1.oid, mfi_c1.mode,1893&mfi_c2.oid, mfi_c2.mode);1894}18951896/*1897 * Get the diff_filepairs changed between o_tree and tree.1898 */1899static struct diff_queue_struct *get_diffpairs(struct merge_options *o,1900struct tree *o_tree,1901struct tree *tree)1902{1903struct diff_queue_struct *ret;1904struct diff_options opts;19051906repo_diff_setup(o->repo, &opts);1907 opts.flags.recursive =1;1908 opts.flags.rename_empty =0;1909 opts.detect_rename =merge_detect_rename(o);1910/*1911 * We do not have logic to handle the detection of copies. In1912 * fact, it may not even make sense to add such logic: would we1913 * really want a change to a base file to be propagated through1914 * multiple other files by a merge?1915 */1916if(opts.detect_rename > DIFF_DETECT_RENAME)1917 opts.detect_rename = DIFF_DETECT_RENAME;1918 opts.rename_limit = o->merge_rename_limit >=0? o->merge_rename_limit :1919 o->diff_rename_limit >=0? o->diff_rename_limit :19201000;1921 opts.rename_score = o->rename_score;1922 opts.show_rename_progress = o->show_rename_progress;1923 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1924diff_setup_done(&opts);1925diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1926diffcore_std(&opts);1927if(opts.needed_rename_limit > o->needed_rename_limit)1928 o->needed_rename_limit = opts.needed_rename_limit;19291930 ret =xmalloc(sizeof(*ret));1931*ret = diff_queued_diff;19321933 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1934 diff_queued_diff.nr =0;1935 diff_queued_diff.queue = NULL;1936diff_flush(&opts);1937return ret;1938}19391940static inttree_has_path(struct tree *tree,const char*path)1941{1942struct object_id hashy;1943unsigned int mode_o;19441945return!get_tree_entry(&tree->object.oid, path,1946&hashy, &mode_o);1947}19481949/*1950 * Return a new string that replaces the beginning portion (which matches1951 * entry->dir), with entry->new_dir. In perl-speak:1952 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1953 * NOTE:1954 * Caller must ensure that old_path starts with entry->dir + '/'.1955 */1956static char*apply_dir_rename(struct dir_rename_entry *entry,1957const char*old_path)1958{1959struct strbuf new_path = STRBUF_INIT;1960int oldlen, newlen;19611962if(entry->non_unique_new_dir)1963return NULL;19641965 oldlen =strlen(entry->dir);1966 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1967strbuf_grow(&new_path, newlen);1968strbuf_addbuf(&new_path, &entry->new_dir);1969strbuf_addstr(&new_path, &old_path[oldlen]);19701971returnstrbuf_detach(&new_path, NULL);1972}19731974static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1975char**old_dir,char**new_dir)1976{1977char*end_of_old, *end_of_new;1978int old_len, new_len;19791980*old_dir = NULL;1981*new_dir = NULL;19821983/*1984 * For1985 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1986 * the "e/foo.c" part is the same, we just want to know that1987 * "a/b/c/d" was renamed to "a/b/some/thing/else"1988 * so, for this example, this function returns "a/b/c/d" in1989 * *old_dir and "a/b/some/thing/else" in *new_dir.1990 *1991 * Also, if the basename of the file changed, we don't care. We1992 * want to know which portion of the directory, if any, changed.1993 */1994 end_of_old =strrchr(old_path,'/');1995 end_of_new =strrchr(new_path,'/');19961997if(end_of_old == NULL || end_of_new == NULL)1998return;1999while(*--end_of_new == *--end_of_old &&2000 end_of_old != old_path &&2001 end_of_new != new_path)2002;/* Do nothing; all in the while loop */2003/*2004 * We've found the first non-matching character in the directory2005 * paths. That means the current directory we were comparing2006 * represents the rename. Move end_of_old and end_of_new back2007 * to the full directory name.2008 */2009if(*end_of_old =='/')2010 end_of_old++;2011if(*end_of_old !='/')2012 end_of_new++;2013 end_of_old =strchr(end_of_old,'/');2014 end_of_new =strchr(end_of_new,'/');20152016/*2017 * It may have been the case that old_path and new_path were the same2018 * directory all along. Don't claim a rename if they're the same.2019 */2020 old_len = end_of_old - old_path;2021 new_len = end_of_new - new_path;20222023if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {2024*old_dir =xstrndup(old_path, old_len);2025*new_dir =xstrndup(new_path, new_len);2026}2027}20282029static voidremove_hashmap_entries(struct hashmap *dir_renames,2030struct string_list *items_to_remove)2031{2032int i;2033struct dir_rename_entry *entry;20342035for(i =0; i < items_to_remove->nr; i++) {2036 entry = items_to_remove->items[i].util;2037hashmap_remove(dir_renames, entry, NULL);2038}2039string_list_clear(items_to_remove,0);2040}20412042/*2043 * See if there is a directory rename for path, and if there are any file2044 * level conflicts for the renamed location. If there is a rename and2045 * there are no conflicts, return the new name. Otherwise, return NULL.2046 */2047static char*handle_path_level_conflicts(struct merge_options *o,2048const char*path,2049struct dir_rename_entry *entry,2050struct hashmap *collisions,2051struct tree *tree)2052{2053char*new_path = NULL;2054struct collision_entry *collision_ent;2055int clean =1;2056struct strbuf collision_paths = STRBUF_INIT;20572058/*2059 * entry has the mapping of old directory name to new directory name2060 * that we want to apply to path.2061 */2062 new_path =apply_dir_rename(entry, path);20632064if(!new_path) {2065/* This should only happen when entry->non_unique_new_dir set */2066if(!entry->non_unique_new_dir)2067BUG("entry->non_unqiue_dir not set and !new_path");2068output(o,1,_("CONFLICT (directory rename split): "2069"Unclear where to place%sbecause directory "2070"%swas renamed to multiple other directories, "2071"with no destination getting a majority of the "2072"files."),2073 path, entry->dir);2074 clean =0;2075return NULL;2076}20772078/*2079 * The caller needs to have ensured that it has pre-populated2080 * collisions with all paths that map to new_path. Do a quick check2081 * to ensure that's the case.2082 */2083 collision_ent =collision_find_entry(collisions, new_path);2084if(collision_ent == NULL)2085BUG("collision_ent is NULL");20862087/*2088 * Check for one-sided add/add/.../add conflicts, i.e.2089 * where implicit renames from the other side doing2090 * directory rename(s) can affect this side of history2091 * to put multiple paths into the same location. Warn2092 * and bail on directory renames for such paths.2093 */2094if(collision_ent->reported_already) {2095 clean =0;2096}else if(tree_has_path(tree, new_path)) {2097 collision_ent->reported_already =1;2098strbuf_add_separated_string_list(&collision_paths,", ",2099&collision_ent->source_files);2100output(o,1,_("CONFLICT (implicit dir rename): Existing "2101"file/dir at%sin the way of implicit "2102"directory rename(s) putting the following "2103"path(s) there:%s."),2104 new_path, collision_paths.buf);2105 clean =0;2106}else if(collision_ent->source_files.nr >1) {2107 collision_ent->reported_already =1;2108strbuf_add_separated_string_list(&collision_paths,", ",2109&collision_ent->source_files);2110output(o,1,_("CONFLICT (implicit dir rename): Cannot map "2111"more than one path to%s; implicit directory "2112"renames tried to put these paths there:%s"),2113 new_path, collision_paths.buf);2114 clean =0;2115}21162117/* Free memory we no longer need */2118strbuf_release(&collision_paths);2119if(!clean && new_path) {2120free(new_path);2121return NULL;2122}21232124return new_path;2125}21262127/*2128 * There are a couple things we want to do at the directory level:2129 * 1. Check for both sides renaming to the same thing, in order to avoid2130 * implicit renaming of files that should be left in place. (See2131 * testcase 6b in t6043 for details.)2132 * 2. Prune directory renames if there are still files left in the2133 * the original directory. These represent a partial directory rename,2134 * i.e. a rename where only some of the files within the directory2135 * were renamed elsewhere. (Technically, this could be done earlier2136 * in get_directory_renames(), except that would prevent us from2137 * doing the previous check and thus failing testcase 6b.)2138 * 3. Check for rename/rename(1to2) conflicts (at the directory level).2139 * In the future, we could potentially record this info as well and2140 * omit reporting rename/rename(1to2) conflicts for each path within2141 * the affected directories, thus cleaning up the merge output.2142 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the2143 * directory level, because merging directories is fine. If it2144 * causes conflicts for files within those merged directories, then2145 * that should be detected at the individual path level.2146 */2147static voidhandle_directory_level_conflicts(struct merge_options *o,2148struct hashmap *dir_re_head,2149struct tree *head,2150struct hashmap *dir_re_merge,2151struct tree *merge)2152{2153struct hashmap_iter iter;2154struct dir_rename_entry *head_ent;2155struct dir_rename_entry *merge_ent;21562157struct string_list remove_from_head = STRING_LIST_INIT_NODUP;2158struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;21592160hashmap_iter_init(dir_re_head, &iter);2161while((head_ent =hashmap_iter_next(&iter))) {2162 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);2163if(merge_ent &&2164!head_ent->non_unique_new_dir &&2165!merge_ent->non_unique_new_dir &&2166!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {2167/* 1. Renamed identically; remove it from both sides */2168string_list_append(&remove_from_head,2169 head_ent->dir)->util = head_ent;2170strbuf_release(&head_ent->new_dir);2171string_list_append(&remove_from_merge,2172 merge_ent->dir)->util = merge_ent;2173strbuf_release(&merge_ent->new_dir);2174}else if(tree_has_path(head, head_ent->dir)) {2175/* 2. This wasn't a directory rename after all */2176string_list_append(&remove_from_head,2177 head_ent->dir)->util = head_ent;2178strbuf_release(&head_ent->new_dir);2179}2180}21812182remove_hashmap_entries(dir_re_head, &remove_from_head);2183remove_hashmap_entries(dir_re_merge, &remove_from_merge);21842185hashmap_iter_init(dir_re_merge, &iter);2186while((merge_ent =hashmap_iter_next(&iter))) {2187 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);2188if(tree_has_path(merge, merge_ent->dir)) {2189/* 2. This wasn't a directory rename after all */2190string_list_append(&remove_from_merge,2191 merge_ent->dir)->util = merge_ent;2192}else if(head_ent &&2193!head_ent->non_unique_new_dir &&2194!merge_ent->non_unique_new_dir) {2195/* 3. rename/rename(1to2) */2196/*2197 * We can assume it's not rename/rename(1to1) because2198 * that was case (1), already checked above. So we2199 * know that head_ent->new_dir and merge_ent->new_dir2200 * are different strings.2201 */2202output(o,1,_("CONFLICT (rename/rename): "2203"Rename directory%s->%sin%s. "2204"Rename directory%s->%sin%s"),2205 head_ent->dir, head_ent->new_dir.buf, o->branch1,2206 head_ent->dir, merge_ent->new_dir.buf, o->branch2);2207string_list_append(&remove_from_head,2208 head_ent->dir)->util = head_ent;2209strbuf_release(&head_ent->new_dir);2210string_list_append(&remove_from_merge,2211 merge_ent->dir)->util = merge_ent;2212strbuf_release(&merge_ent->new_dir);2213}2214}22152216remove_hashmap_entries(dir_re_head, &remove_from_head);2217remove_hashmap_entries(dir_re_merge, &remove_from_merge);2218}22192220static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)2221{2222struct hashmap *dir_renames;2223struct hashmap_iter iter;2224struct dir_rename_entry *entry;2225int i;22262227/*2228 * Typically, we think of a directory rename as all files from a2229 * certain directory being moved to a target directory. However,2230 * what if someone first moved two files from the original2231 * directory in one commit, and then renamed the directory2232 * somewhere else in a later commit? At merge time, we just know2233 * that files from the original directory went to two different2234 * places, and that the bulk of them ended up in the same place.2235 * We want each directory rename to represent where the bulk of the2236 * files from that directory end up; this function exists to find2237 * where the bulk of the files went.2238 *2239 * The first loop below simply iterates through the list of file2240 * renames, finding out how often each directory rename pair2241 * possibility occurs.2242 */2243 dir_renames =xmalloc(sizeof(*dir_renames));2244dir_rename_init(dir_renames);2245for(i =0; i < pairs->nr; ++i) {2246struct string_list_item *item;2247int*count;2248struct diff_filepair *pair = pairs->queue[i];2249char*old_dir, *new_dir;22502251/* File not part of directory rename if it wasn't renamed */2252if(pair->status !='R')2253continue;22542255get_renamed_dir_portion(pair->one->path, pair->two->path,2256&old_dir, &new_dir);2257if(!old_dir)2258/* Directory didn't change at all; ignore this one. */2259continue;22602261 entry =dir_rename_find_entry(dir_renames, old_dir);2262if(!entry) {2263 entry =xmalloc(sizeof(*entry));2264dir_rename_entry_init(entry, old_dir);2265hashmap_put(dir_renames, entry);2266}else{2267free(old_dir);2268}2269 item =string_list_lookup(&entry->possible_new_dirs, new_dir);2270if(!item) {2271 item =string_list_insert(&entry->possible_new_dirs,2272 new_dir);2273 item->util =xcalloc(1,sizeof(int));2274}else{2275free(new_dir);2276}2277 count = item->util;2278*count +=1;2279}22802281/*2282 * For each directory with files moved out of it, we find out which2283 * target directory received the most files so we can declare it to2284 * be the "winning" target location for the directory rename. This2285 * winner gets recorded in new_dir. If there is no winner2286 * (multiple target directories received the same number of files),2287 * we set non_unique_new_dir. Once we've determined the winner (or2288 * that there is no winner), we no longer need possible_new_dirs.2289 */2290hashmap_iter_init(dir_renames, &iter);2291while((entry =hashmap_iter_next(&iter))) {2292int max =0;2293int bad_max =0;2294char*best = NULL;22952296for(i =0; i < entry->possible_new_dirs.nr; i++) {2297int*count = entry->possible_new_dirs.items[i].util;22982299if(*count == max)2300 bad_max = max;2301else if(*count > max) {2302 max = *count;2303 best = entry->possible_new_dirs.items[i].string;2304}2305}2306if(bad_max == max)2307 entry->non_unique_new_dir =1;2308else{2309assert(entry->new_dir.len ==0);2310strbuf_addstr(&entry->new_dir, best);2311}2312/*2313 * The relevant directory sub-portion of the original full2314 * filepaths were xstrndup'ed before inserting into2315 * possible_new_dirs, and instead of manually iterating the2316 * list and free'ing each, just lie and tell2317 * possible_new_dirs that it did the strdup'ing so that it2318 * will free them for us.2319 */2320 entry->possible_new_dirs.strdup_strings =1;2321string_list_clear(&entry->possible_new_dirs,1);2322}23232324return dir_renames;2325}23262327static struct dir_rename_entry *check_dir_renamed(const char*path,2328struct hashmap *dir_renames)2329{2330char*temp =xstrdup(path);2331char*end;2332struct dir_rename_entry *entry = NULL;23332334while((end =strrchr(temp,'/'))) {2335*end ='\0';2336 entry =dir_rename_find_entry(dir_renames, temp);2337if(entry)2338break;2339}2340free(temp);2341return entry;2342}23432344static voidcompute_collisions(struct hashmap *collisions,2345struct hashmap *dir_renames,2346struct diff_queue_struct *pairs)2347{2348int i;23492350/*2351 * Multiple files can be mapped to the same path due to directory2352 * renames done by the other side of history. Since that other2353 * side of history could have merged multiple directories into one,2354 * if our side of history added the same file basename to each of2355 * those directories, then all N of them would get implicitly2356 * renamed by the directory rename detection into the same path,2357 * and we'd get an add/add/.../add conflict, and all those adds2358 * from *this* side of history. This is not representable in the2359 * index, and users aren't going to easily be able to make sense of2360 * it. So we need to provide a good warning about what's2361 * happening, and fall back to no-directory-rename detection2362 * behavior for those paths.2363 *2364 * See testcases 9e and all of section 5 from t6043 for examples.2365 */2366collision_init(collisions);23672368for(i =0; i < pairs->nr; ++i) {2369struct dir_rename_entry *dir_rename_ent;2370struct collision_entry *collision_ent;2371char*new_path;2372struct diff_filepair *pair = pairs->queue[i];23732374if(pair->status !='A'&& pair->status !='R')2375continue;2376 dir_rename_ent =check_dir_renamed(pair->two->path,2377 dir_renames);2378if(!dir_rename_ent)2379continue;23802381 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);2382if(!new_path)2383/*2384 * dir_rename_ent->non_unique_new_path is true, which2385 * means there is no directory rename for us to use,2386 * which means it won't cause us any additional2387 * collisions.2388 */2389continue;2390 collision_ent =collision_find_entry(collisions, new_path);2391if(!collision_ent) {2392 collision_ent =xcalloc(1,2393sizeof(struct collision_entry));2394hashmap_entry_init(collision_ent,strhash(new_path));2395hashmap_put(collisions, collision_ent);2396 collision_ent->target_file = new_path;2397}else{2398free(new_path);2399}2400string_list_insert(&collision_ent->source_files,2401 pair->two->path);2402}2403}24042405static char*check_for_directory_rename(struct merge_options *o,2406const char*path,2407struct tree *tree,2408struct hashmap *dir_renames,2409struct hashmap *dir_rename_exclusions,2410struct hashmap *collisions,2411int*clean_merge)2412{2413char*new_path = NULL;2414struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2415struct dir_rename_entry *oentry = NULL;24162417if(!entry)2418return new_path;24192420/*2421 * This next part is a little weird. We do not want to do an2422 * implicit rename into a directory we renamed on our side, because2423 * that will result in a spurious rename/rename(1to2) conflict. An2424 * example:2425 * Base commit: dumbdir/afile, otherdir/bfile2426 * Side 1: smrtdir/afile, otherdir/bfile2427 * Side 2: dumbdir/afile, dumbdir/bfile2428 * Here, while working on Side 1, we could notice that otherdir was2429 * renamed/merged to dumbdir, and change the diff_filepair for2430 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2431 * 2 will notice the rename from dumbdir to smrtdir, and do the2432 * transitive rename to move it from dumbdir/bfile to2433 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2434 * smrtdir, a rename/rename(1to2) conflict. We really just want2435 * the file to end up in smrtdir. And the way to achieve that is2436 * to not let Side1 do the rename to dumbdir, since we know that is2437 * the source of one of our directory renames.2438 *2439 * That's why oentry and dir_rename_exclusions is here.2440 *2441 * As it turns out, this also prevents N-way transient rename2442 * confusion; See testcases 9c and 9d of t6043.2443 */2444 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2445if(oentry) {2446output(o,1,_("WARNING: Avoiding applying%s->%srename "2447"to%s, because%sitself was renamed."),2448 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2449}else{2450 new_path =handle_path_level_conflicts(o, path, entry,2451 collisions, tree);2452*clean_merge &= (new_path != NULL);2453}24542455return new_path;2456}24572458static voidapply_directory_rename_modifications(struct merge_options *o,2459struct diff_filepair *pair,2460char*new_path,2461struct rename *re,2462struct tree *tree,2463struct tree *o_tree,2464struct tree *a_tree,2465struct tree *b_tree,2466struct string_list *entries)2467{2468struct string_list_item *item;2469int stage = (tree == a_tree ?2:3);2470int update_wd;24712472/*2473 * In all cases where we can do directory rename detection,2474 * unpack_trees() will have read pair->two->path into the2475 * index and the working copy. We need to remove it so that2476 * we can instead place it at new_path. It is guaranteed to2477 * not be untracked (unpack_trees() would have errored out2478 * saying the file would have been overwritten), but it might2479 * be dirty, though.2480 */2481 update_wd = !was_dirty(o, pair->two->path);2482if(!update_wd)2483output(o,1,_("Refusing to lose dirty file at%s"),2484 pair->two->path);2485remove_file(o,1, pair->two->path, !update_wd);24862487/* Find or create a new re->dst_entry */2488 item =string_list_lookup(entries, new_path);2489if(item) {2490/*2491 * Since we're renaming on this side of history, and it's2492 * due to a directory rename on the other side of history2493 * (which we only allow when the directory in question no2494 * longer exists on the other side of history), the2495 * original entry for re->dst_entry is no longer2496 * necessary...2497 */2498 re->dst_entry->processed =1;24992500/*2501 * ...because we'll be using this new one.2502 */2503 re->dst_entry = item->util;2504}else{2505/*2506 * re->dst_entry is for the before-dir-rename path, and we2507 * need it to hold information for the after-dir-rename2508 * path. Before creating a new entry, we need to mark the2509 * old one as unnecessary (...unless it is shared by2510 * src_entry, i.e. this didn't use to be a rename, in which2511 * case we can just allow the normal processing to happen2512 * for it).2513 */2514if(pair->status =='R')2515 re->dst_entry->processed =1;25162517 re->dst_entry =insert_stage_data(new_path,2518 o_tree, a_tree, b_tree,2519 entries);2520 item =string_list_insert(entries, new_path);2521 item->util = re->dst_entry;2522}25232524/*2525 * Update the stage_data with the information about the path we are2526 * moving into place. That slot will be empty and available for us2527 * to write to because of the collision checks in2528 * handle_path_level_conflicts(). In other words,2529 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2530 * open for us to write to.2531 *2532 * It may be tempting to actually update the index at this point as2533 * well, using update_stages_for_stage_data(), but as per the big2534 * "NOTE" in update_stages(), doing so will modify the current2535 * in-memory index which will break calls to would_lose_untracked()2536 * that we need to make. Instead, we need to just make sure that2537 * the various handle_rename_*() functions update the index2538 * explicitly rather than relying on unpack_trees() to have done it.2539 */2540get_tree_entry(&tree->object.oid,2541 pair->two->path,2542&re->dst_entry->stages[stage].oid,2543&re->dst_entry->stages[stage].mode);25442545/* Update pair status */2546if(pair->status =='A') {2547/*2548 * Recording rename information for this add makes it look2549 * like a rename/delete conflict. Make sure we can2550 * correctly handle this as an add that was moved to a new2551 * directory instead of reporting a rename/delete conflict.2552 */2553 re->add_turned_into_rename =1;2554}2555/*2556 * We don't actually look at pair->status again, but it seems2557 * pedagogically correct to adjust it.2558 */2559 pair->status ='R';25602561/*2562 * Finally, record the new location.2563 */2564 pair->two->path = new_path;2565}25662567/*2568 * Get information of all renames which occurred in 'pairs', making use of2569 * any implicit directory renames inferred from the other side of history.2570 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2571 * to be able to associate the correct cache entries with the rename2572 * information; tree is always equal to either a_tree or b_tree.2573 */2574static struct string_list *get_renames(struct merge_options *o,2575struct diff_queue_struct *pairs,2576struct hashmap *dir_renames,2577struct hashmap *dir_rename_exclusions,2578struct tree *tree,2579struct tree *o_tree,2580struct tree *a_tree,2581struct tree *b_tree,2582struct string_list *entries,2583int*clean_merge)2584{2585int i;2586struct hashmap collisions;2587struct hashmap_iter iter;2588struct collision_entry *e;2589struct string_list *renames;25902591compute_collisions(&collisions, dir_renames, pairs);2592 renames =xcalloc(1,sizeof(struct string_list));25932594for(i =0; i < pairs->nr; ++i) {2595struct string_list_item *item;2596struct rename *re;2597struct diff_filepair *pair = pairs->queue[i];2598char*new_path;/* non-NULL only with directory renames */25992600if(pair->status !='A'&& pair->status !='R') {2601diff_free_filepair(pair);2602continue;2603}2604 new_path =check_for_directory_rename(o, pair->two->path, tree,2605 dir_renames,2606 dir_rename_exclusions,2607&collisions,2608 clean_merge);2609if(pair->status !='R'&& !new_path) {2610diff_free_filepair(pair);2611continue;2612}26132614 re =xmalloc(sizeof(*re));2615 re->processed =0;2616 re->add_turned_into_rename =0;2617 re->pair = pair;2618 item =string_list_lookup(entries, re->pair->one->path);2619if(!item)2620 re->src_entry =insert_stage_data(re->pair->one->path,2621 o_tree, a_tree, b_tree, entries);2622else2623 re->src_entry = item->util;26242625 item =string_list_lookup(entries, re->pair->two->path);2626if(!item)2627 re->dst_entry =insert_stage_data(re->pair->two->path,2628 o_tree, a_tree, b_tree, entries);2629else2630 re->dst_entry = item->util;2631 item =string_list_insert(renames, pair->one->path);2632 item->util = re;2633if(new_path)2634apply_directory_rename_modifications(o, pair, new_path,2635 re, tree, o_tree,2636 a_tree, b_tree,2637 entries);2638}26392640hashmap_iter_init(&collisions, &iter);2641while((e =hashmap_iter_next(&iter))) {2642free(e->target_file);2643string_list_clear(&e->source_files,0);2644}2645hashmap_free(&collisions,1);2646return renames;2647}26482649static intprocess_renames(struct merge_options *o,2650struct string_list *a_renames,2651struct string_list *b_renames)2652{2653int clean_merge =1, i, j;2654struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2655struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2656const struct rename *sre;26572658for(i =0; i < a_renames->nr; i++) {2659 sre = a_renames->items[i].util;2660string_list_insert(&a_by_dst, sre->pair->two->path)->util2661= (void*)sre;2662}2663for(i =0; i < b_renames->nr; i++) {2664 sre = b_renames->items[i].util;2665string_list_insert(&b_by_dst, sre->pair->two->path)->util2666= (void*)sre;2667}26682669for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2670struct string_list *renames1, *renames2Dst;2671struct rename *ren1 = NULL, *ren2 = NULL;2672const char*branch1, *branch2;2673const char*ren1_src, *ren1_dst;2674struct string_list_item *lookup;26752676if(i >= a_renames->nr) {2677 ren2 = b_renames->items[j++].util;2678}else if(j >= b_renames->nr) {2679 ren1 = a_renames->items[i++].util;2680}else{2681int compare =strcmp(a_renames->items[i].string,2682 b_renames->items[j].string);2683if(compare <=0)2684 ren1 = a_renames->items[i++].util;2685if(compare >=0)2686 ren2 = b_renames->items[j++].util;2687}26882689/* TODO: refactor, so that 1/2 are not needed */2690if(ren1) {2691 renames1 = a_renames;2692 renames2Dst = &b_by_dst;2693 branch1 = o->branch1;2694 branch2 = o->branch2;2695}else{2696 renames1 = b_renames;2697 renames2Dst = &a_by_dst;2698 branch1 = o->branch2;2699 branch2 = o->branch1;2700SWAP(ren2, ren1);2701}27022703if(ren1->processed)2704continue;2705 ren1->processed =1;2706 ren1->dst_entry->processed =1;2707/* BUG: We should only mark src_entry as processed if we2708 * are not dealing with a rename + add-source case.2709 */2710 ren1->src_entry->processed =1;27112712 ren1_src = ren1->pair->one->path;2713 ren1_dst = ren1->pair->two->path;27142715if(ren2) {2716/* One file renamed on both sides */2717const char*ren2_src = ren2->pair->one->path;2718const char*ren2_dst = ren2->pair->two->path;2719enum rename_type rename_type;2720if(strcmp(ren1_src, ren2_src) !=0)2721BUG("ren1_src != ren2_src");2722 ren2->dst_entry->processed =1;2723 ren2->processed =1;2724if(strcmp(ren1_dst, ren2_dst) !=0) {2725 rename_type = RENAME_ONE_FILE_TO_TWO;2726 clean_merge =0;2727}else{2728 rename_type = RENAME_ONE_FILE_TO_ONE;2729/* BUG: We should only remove ren1_src in2730 * the base stage (think of rename +2731 * add-source cases).2732 */2733remove_file(o,1, ren1_src,1);2734update_entry(ren1->dst_entry,2735 ren1->pair->one,2736 ren1->pair->two,2737 ren2->pair->two);2738}2739setup_rename_conflict_info(rename_type,2740 ren1->pair,2741 ren2->pair,2742 branch1,2743 branch2,2744 ren1->dst_entry,2745 ren2->dst_entry,2746 o,2747 NULL,2748 NULL);2749}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2750/* Two different files renamed to the same thing */2751char*ren2_dst;2752 ren2 = lookup->util;2753 ren2_dst = ren2->pair->two->path;2754if(strcmp(ren1_dst, ren2_dst) !=0)2755BUG("ren1_dst != ren2_dst");27562757 clean_merge =0;2758 ren2->processed =1;2759/*2760 * BUG: We should only mark src_entry as processed2761 * if we are not dealing with a rename + add-source2762 * case.2763 */2764 ren2->src_entry->processed =1;27652766setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2767 ren1->pair,2768 ren2->pair,2769 branch1,2770 branch2,2771 ren1->dst_entry,2772 ren2->dst_entry,2773 o,2774 ren1->src_entry,2775 ren2->src_entry);27762777}else{2778/* Renamed in 1, maybe changed in 2 */2779/* we only use sha1 and mode of these */2780struct diff_filespec src_other, dst_other;2781int try_merge;27822783/*2784 * unpack_trees loads entries from common-commit2785 * into stage 1, from head-commit into stage 2, and2786 * from merge-commit into stage 3. We keep track2787 * of which side corresponds to the rename.2788 */2789int renamed_stage = a_renames == renames1 ?2:3;2790int other_stage = a_renames == renames1 ?3:2;27912792/* BUG: We should only remove ren1_src in the base2793 * stage and in other_stage (think of rename +2794 * add-source case).2795 */2796remove_file(o,1, ren1_src,2797 renamed_stage ==2|| !was_tracked(o, ren1_src));27982799oidcpy(&src_other.oid,2800&ren1->src_entry->stages[other_stage].oid);2801 src_other.mode = ren1->src_entry->stages[other_stage].mode;2802oidcpy(&dst_other.oid,2803&ren1->dst_entry->stages[other_stage].oid);2804 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2805 try_merge =0;28062807if(oid_eq(&src_other.oid, &null_oid) &&2808 ren1->add_turned_into_rename) {2809setup_rename_conflict_info(RENAME_VIA_DIR,2810 ren1->pair,2811 NULL,2812 branch1,2813 branch2,2814 ren1->dst_entry,2815 NULL,2816 o,2817 NULL,2818 NULL);2819}else if(oid_eq(&src_other.oid, &null_oid)) {2820setup_rename_conflict_info(RENAME_DELETE,2821 ren1->pair,2822 NULL,2823 branch1,2824 branch2,2825 ren1->dst_entry,2826 NULL,2827 o,2828 NULL,2829 NULL);2830}else if((dst_other.mode == ren1->pair->two->mode) &&2831oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2832/*2833 * Added file on the other side identical to2834 * the file being renamed: clean merge.2835 * Also, there is no need to overwrite the2836 * file already in the working copy, so call2837 * update_file_flags() instead of2838 * update_file().2839 */2840if(update_file_flags(o,2841&ren1->pair->two->oid,2842 ren1->pair->two->mode,2843 ren1_dst,28441,/* update_cache */28450/* update_wd */))2846 clean_merge = -1;2847}else if(!oid_eq(&dst_other.oid, &null_oid)) {2848/*2849 * Probably not a clean merge, but it's2850 * premature to set clean_merge to 0 here,2851 * because if the rename merges cleanly and2852 * the merge exactly matches the newly added2853 * file, then the merge will be clean.2854 */2855setup_rename_conflict_info(RENAME_ADD,2856 ren1->pair,2857 NULL,2858 branch1,2859 branch2,2860 ren1->dst_entry,2861 NULL,2862 o,2863 ren1->src_entry,2864 NULL);2865}else2866 try_merge =1;28672868if(clean_merge <0)2869goto cleanup_and_return;2870if(try_merge) {2871struct diff_filespec *one, *a, *b;2872 src_other.path = (char*)ren1_src;28732874 one = ren1->pair->one;2875if(a_renames == renames1) {2876 a = ren1->pair->two;2877 b = &src_other;2878}else{2879 b = ren1->pair->two;2880 a = &src_other;2881}2882update_entry(ren1->dst_entry, one, a, b);2883setup_rename_conflict_info(RENAME_NORMAL,2884 ren1->pair,2885 NULL,2886 branch1,2887 NULL,2888 ren1->dst_entry,2889 NULL,2890 o,2891 NULL,2892 NULL);2893}2894}2895}2896cleanup_and_return:2897string_list_clear(&a_by_dst,0);2898string_list_clear(&b_by_dst,0);28992900return clean_merge;2901}29022903struct rename_info {2904struct string_list *head_renames;2905struct string_list *merge_renames;2906};29072908static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2909struct hashmap *dir_renames)2910{2911struct hashmap_iter iter;2912struct dir_rename_entry *e;29132914hashmap_iter_init(dir_renames, &iter);2915while((e =hashmap_iter_next(&iter))) {2916free(e->dir);2917strbuf_release(&e->new_dir);2918/* possible_new_dirs already cleared in get_directory_renames */2919}2920hashmap_free(dir_renames,1);2921free(dir_renames);29222923free(pairs->queue);2924free(pairs);2925}29262927static intdetect_and_process_renames(struct merge_options *o,2928struct tree *common,2929struct tree *head,2930struct tree *merge,2931struct string_list *entries,2932struct rename_info *ri)2933{2934struct diff_queue_struct *head_pairs, *merge_pairs;2935struct hashmap *dir_re_head, *dir_re_merge;2936int clean =1;29372938 ri->head_renames = NULL;2939 ri->merge_renames = NULL;29402941if(!merge_detect_rename(o))2942return1;29432944 head_pairs =get_diffpairs(o, common, head);2945 merge_pairs =get_diffpairs(o, common, merge);29462947if(o->detect_directory_renames) {2948 dir_re_head =get_directory_renames(head_pairs);2949 dir_re_merge =get_directory_renames(merge_pairs);29502951handle_directory_level_conflicts(o,2952 dir_re_head, head,2953 dir_re_merge, merge);2954}else{2955 dir_re_head =xmalloc(sizeof(*dir_re_head));2956 dir_re_merge =xmalloc(sizeof(*dir_re_merge));2957dir_rename_init(dir_re_head);2958dir_rename_init(dir_re_merge);2959}29602961 ri->head_renames =get_renames(o, head_pairs,2962 dir_re_merge, dir_re_head, head,2963 common, head, merge, entries,2964&clean);2965if(clean <0)2966goto cleanup;2967 ri->merge_renames =get_renames(o, merge_pairs,2968 dir_re_head, dir_re_merge, merge,2969 common, head, merge, entries,2970&clean);2971if(clean <0)2972goto cleanup;2973 clean &=process_renames(o, ri->head_renames, ri->merge_renames);29742975cleanup:2976/*2977 * Some cleanup is deferred until cleanup_renames() because the2978 * data structures are still needed and referenced in2979 * process_entry(). But there are a few things we can free now.2980 */2981initial_cleanup_rename(head_pairs, dir_re_head);2982initial_cleanup_rename(merge_pairs, dir_re_merge);29832984return clean;2985}29862987static voidfinal_cleanup_rename(struct string_list *rename)2988{2989const struct rename *re;2990int i;29912992if(rename == NULL)2993return;29942995for(i =0; i < rename->nr; i++) {2996 re = rename->items[i].util;2997diff_free_filepair(re->pair);2998}2999string_list_clear(rename,1);3000free(rename);3001}30023003static voidfinal_cleanup_renames(struct rename_info *re_info)3004{3005final_cleanup_rename(re_info->head_renames);3006final_cleanup_rename(re_info->merge_renames);3007}30083009static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)3010{3011return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;3012}30133014static intread_oid_strbuf(struct merge_options *o,3015const struct object_id *oid,3016struct strbuf *dst)3017{3018void*buf;3019enum object_type type;3020unsigned long size;3021 buf =read_object_file(oid, &type, &size);3022if(!buf)3023returnerr(o,_("cannot read object%s"),oid_to_hex(oid));3024if(type != OBJ_BLOB) {3025free(buf);3026returnerr(o,_("object%sis not a blob"),oid_to_hex(oid));3027}3028strbuf_attach(dst, buf, size, size +1);3029return0;3030}30313032static intblob_unchanged(struct merge_options *opt,3033const struct object_id *o_oid,3034unsigned o_mode,3035const struct object_id *a_oid,3036unsigned a_mode,3037int renormalize,const char*path)3038{3039struct strbuf o = STRBUF_INIT;3040struct strbuf a = STRBUF_INIT;3041int ret =0;/* assume changed for safety */30423043if(a_mode != o_mode)3044return0;3045if(oid_eq(o_oid, a_oid))3046return1;3047if(!renormalize)3048return0;30493050assert(o_oid && a_oid);3051if(read_oid_strbuf(opt, o_oid, &o) ||read_oid_strbuf(opt, a_oid, &a))3052goto error_return;3053/*3054 * Note: binary | is used so that both renormalizations are3055 * performed. Comparison can be skipped if both files are3056 * unchanged since their sha1s have already been compared.3057 */3058if(renormalize_buffer(opt->repo->index, path, o.buf, o.len, &o) |3059renormalize_buffer(opt->repo->index, path, a.buf, a.len, &a))3060 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));30613062error_return:3063strbuf_release(&o);3064strbuf_release(&a);3065return ret;3066}30673068static inthandle_modify_delete(struct merge_options *o,3069const char*path,3070struct object_id *o_oid,int o_mode,3071struct object_id *a_oid,int a_mode,3072struct object_id *b_oid,int b_mode)3073{3074const char*modify_branch, *delete_branch;3075struct object_id *changed_oid;3076int changed_mode;30773078if(a_oid) {3079 modify_branch = o->branch1;3080 delete_branch = o->branch2;3081 changed_oid = a_oid;3082 changed_mode = a_mode;3083}else{3084 modify_branch = o->branch2;3085 delete_branch = o->branch1;3086 changed_oid = b_oid;3087 changed_mode = b_mode;3088}30893090returnhandle_change_delete(o,3091 path, NULL,3092 o_oid, o_mode,3093 changed_oid, changed_mode,3094 modify_branch, delete_branch,3095_("modify"),_("modified"));3096}30973098static inthandle_content_merge(struct merge_options *o,3099const char*path,3100int is_dirty,3101struct object_id *o_oid,int o_mode,3102struct object_id *a_oid,int a_mode,3103struct object_id *b_oid,int b_mode,3104struct rename_conflict_info *rename_conflict_info)3105{3106const char*reason =_("content");3107const char*path1 = NULL, *path2 = NULL;3108struct merge_file_info mfi;3109struct diff_filespec one, a, b;3110unsigned df_conflict_remains =0;31113112if(!o_oid) {3113 reason =_("add/add");3114 o_oid = (struct object_id *)&null_oid;3115}3116 one.path = a.path = b.path = (char*)path;3117oidcpy(&one.oid, o_oid);3118 one.mode = o_mode;3119oidcpy(&a.oid, a_oid);3120 a.mode = a_mode;3121oidcpy(&b.oid, b_oid);3122 b.mode = b_mode;31233124if(rename_conflict_info) {3125struct diff_filepair *pair1 = rename_conflict_info->pair1;31263127 path1 = (o->branch1 == rename_conflict_info->branch1) ?3128 pair1->two->path : pair1->one->path;3129/* If rename_conflict_info->pair2 != NULL, we are in3130 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a3131 * normal rename.3132 */3133 path2 = (rename_conflict_info->pair2 ||3134 o->branch2 == rename_conflict_info->branch1) ?3135 pair1->two->path : pair1->one->path;3136 one.path = pair1->one->path;3137 a.path = (char*)path1;3138 b.path = (char*)path2;31393140if(dir_in_way(o->repo->index, path, !o->call_depth,3141S_ISGITLINK(pair1->two->mode)))3142 df_conflict_remains =1;3143}3144if(merge_mode_and_contents(o, &one, &a, &b, path,3145 o->branch1, o->branch2,3146 o->call_depth *2, &mfi))3147return-1;31483149/*3150 * We can skip updating the working tree file iff:3151 * a) The merge is clean3152 * b) The merge matches what was in HEAD (content, mode, pathname)3153 * c) The target path is usable (i.e. not involved in D/F conflict)3154 */3155if(mfi.clean &&3156was_tracked_and_matches(o, path, &mfi.oid, mfi.mode) &&3157!df_conflict_remains) {3158int pos;3159struct cache_entry *ce;31603161output(o,3,_("Skipped%s(merged same as existing)"), path);3162if(add_cacheinfo(o, mfi.mode, &mfi.oid, path,31630, (!o->call_depth && !is_dirty),0))3164return-1;3165/*3166 * However, add_cacheinfo() will delete the old cache entry3167 * and add a new one. We need to copy over any skip_worktree3168 * flag to avoid making the file appear as if it were3169 * deleted by the user.3170 */3171 pos =index_name_pos(&o->orig_index, path,strlen(path));3172 ce = o->orig_index.cache[pos];3173if(ce_skip_worktree(ce)) {3174 pos =index_name_pos(o->repo->index, path,strlen(path));3175 ce = o->repo->index->cache[pos];3176 ce->ce_flags |= CE_SKIP_WORKTREE;3177}3178return mfi.clean;3179}31803181if(!mfi.clean) {3182if(S_ISGITLINK(mfi.mode))3183 reason =_("submodule");3184output(o,1,_("CONFLICT (%s): Merge conflict in%s"),3185 reason, path);3186if(rename_conflict_info && !df_conflict_remains)3187if(update_stages(o, path, &one, &a, &b))3188return-1;3189}31903191if(df_conflict_remains || is_dirty) {3192char*new_path;3193if(o->call_depth) {3194remove_file_from_index(o->repo->index, path);3195}else{3196if(!mfi.clean) {3197if(update_stages(o, path, &one, &a, &b))3198return-1;3199}else{3200int file_from_stage2 =was_tracked(o, path);3201struct diff_filespec merged;3202oidcpy(&merged.oid, &mfi.oid);3203 merged.mode = mfi.mode;32043205if(update_stages(o, path, NULL,3206 file_from_stage2 ? &merged : NULL,3207 file_from_stage2 ? NULL : &merged))3208return-1;3209}32103211}3212 new_path =unique_path(o, path, rename_conflict_info->branch1);3213if(is_dirty) {3214output(o,1,_("Refusing to lose dirty file at%s"),3215 path);3216}3217output(o,1,_("Adding as%sinstead"), new_path);3218if(update_file(o,0, &mfi.oid, mfi.mode, new_path)) {3219free(new_path);3220return-1;3221}3222free(new_path);3223 mfi.clean =0;3224}else if(update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))3225return-1;3226return!is_dirty && mfi.clean;3227}32283229static inthandle_rename_normal(struct merge_options *o,3230const char*path,3231struct object_id *o_oid,unsigned int o_mode,3232struct object_id *a_oid,unsigned int a_mode,3233struct object_id *b_oid,unsigned int b_mode,3234struct rename_conflict_info *ci)3235{3236/* Merge the content and write it out */3237returnhandle_content_merge(o, path,was_dirty(o, path),3238 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,3239 ci);3240}32413242/* Per entry merge function */3243static intprocess_entry(struct merge_options *o,3244const char*path,struct stage_data *entry)3245{3246int clean_merge =1;3247int normalize = o->renormalize;3248unsigned o_mode = entry->stages[1].mode;3249unsigned a_mode = entry->stages[2].mode;3250unsigned b_mode = entry->stages[3].mode;3251struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);3252struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);3253struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);32543255 entry->processed =1;3256if(entry->rename_conflict_info) {3257struct rename_conflict_info *conflict_info = entry->rename_conflict_info;3258switch(conflict_info->rename_type) {3259case RENAME_NORMAL:3260case RENAME_ONE_FILE_TO_ONE:3261 clean_merge =handle_rename_normal(o,3262 path,3263 o_oid, o_mode,3264 a_oid, a_mode,3265 b_oid, b_mode,3266 conflict_info);3267break;3268case RENAME_VIA_DIR:3269 clean_merge =1;3270if(handle_rename_via_dir(o,3271 conflict_info->pair1,3272 conflict_info->branch1))3273 clean_merge = -1;3274break;3275case RENAME_ADD:3276/*3277 * Probably unclean merge, but if the renamed file3278 * merges cleanly and the result can then be3279 * two-way merged cleanly with the added file, I3280 * guess it's a clean merge?3281 */3282 clean_merge =handle_rename_add(o, conflict_info);3283break;3284case RENAME_DELETE:3285 clean_merge =0;3286if(handle_rename_delete(o,3287 conflict_info->pair1,3288 conflict_info->branch1,3289 conflict_info->branch2))3290 clean_merge = -1;3291break;3292case RENAME_ONE_FILE_TO_TWO:3293 clean_merge =0;3294if(handle_rename_rename_1to2(o, conflict_info))3295 clean_merge = -1;3296break;3297case RENAME_TWO_FILES_TO_ONE:3298/*3299 * Probably unclean merge, but if the two renamed3300 * files merge cleanly and the two resulting files3301 * can then be two-way merged cleanly, I guess it's3302 * a clean merge?3303 */3304 clean_merge =handle_rename_rename_2to1(o,3305 conflict_info);3306break;3307default:3308 entry->processed =0;3309break;3310}3311}else if(o_oid && (!a_oid || !b_oid)) {3312/* Case A: Deleted in one */3313if((!a_oid && !b_oid) ||3314(!b_oid &&blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||3315(!a_oid &&blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {3316/* Deleted in both or deleted in one and3317 * unchanged in the other */3318if(a_oid)3319output(o,2,_("Removing%s"), path);3320/* do not touch working file if it did not exist */3321remove_file(o,1, path, !a_oid);3322}else{3323/* Modify/delete; deleted side may have put a directory in the way */3324 clean_merge =0;3325if(handle_modify_delete(o, path, o_oid, o_mode,3326 a_oid, a_mode, b_oid, b_mode))3327 clean_merge = -1;3328}3329}else if((!o_oid && a_oid && !b_oid) ||3330(!o_oid && !a_oid && b_oid)) {3331/* Case B: Added in one. */3332/* [nothing|directory] -> ([nothing|directory], file) */33333334const char*add_branch;3335const char*other_branch;3336unsigned mode;3337const struct object_id *oid;3338const char*conf;33393340if(a_oid) {3341 add_branch = o->branch1;3342 other_branch = o->branch2;3343 mode = a_mode;3344 oid = a_oid;3345 conf =_("file/directory");3346}else{3347 add_branch = o->branch2;3348 other_branch = o->branch1;3349 mode = b_mode;3350 oid = b_oid;3351 conf =_("directory/file");3352}3353if(dir_in_way(o->repo->index, path,3354!o->call_depth && !S_ISGITLINK(a_mode),33550)) {3356char*new_path =unique_path(o, path, add_branch);3357 clean_merge =0;3358output(o,1,_("CONFLICT (%s): There is a directory with name%sin%s. "3359"Adding%sas%s"),3360 conf, path, other_branch, path, new_path);3361if(update_file(o,0, oid, mode, new_path))3362 clean_merge = -1;3363else if(o->call_depth)3364remove_file_from_index(o->repo->index, path);3365free(new_path);3366}else{3367output(o,2,_("Adding%s"), path);3368/* do not overwrite file if already present */3369if(update_file_flags(o, oid, mode, path,1, !a_oid))3370 clean_merge = -1;3371}3372}else if(a_oid && b_oid) {3373if(!o_oid) {3374/* Case C: Added in both (check for same permissions) */3375output(o,1,3376_("CONFLICT (add/add): Merge conflict in%s"),3377 path);3378 clean_merge =handle_file_collision(o,3379 path, NULL, NULL,3380 o->branch1,3381 o->branch2,3382 a_oid, a_mode,3383 b_oid, b_mode);3384}else{3385/* case D: Modified in both, but differently. */3386int is_dirty =0;/* unpack_trees would have bailed if dirty */3387 clean_merge =handle_content_merge(o, path,3388 is_dirty,3389 o_oid, o_mode,3390 a_oid, a_mode,3391 b_oid, b_mode,3392 NULL);3393}3394}else if(!o_oid && !a_oid && !b_oid) {3395/*3396 * this entry was deleted altogether. a_mode == 0 means3397 * we had that path and want to actively remove it.3398 */3399remove_file(o,1, path, !a_mode);3400}else3401BUG("fatal merge failure, shouldn't happen.");34023403return clean_merge;3404}34053406intmerge_trees(struct merge_options *o,3407struct tree *head,3408struct tree *merge,3409struct tree *common,3410struct tree **result)3411{3412struct index_state *istate = o->repo->index;3413int code, clean;3414struct strbuf sb = STRBUF_INIT;34153416if(!o->call_depth &&repo_index_has_changes(o->repo, head, &sb)) {3417err(o,_("Your local changes to the following files would be overwritten by merge:\n%s"),3418 sb.buf);3419return-1;3420}34213422if(o->subtree_shift) {3423 merge =shift_tree_object(o->repo, head, merge, o->subtree_shift);3424 common =shift_tree_object(o->repo, head, common, o->subtree_shift);3425}34263427if(oid_eq(&common->object.oid, &merge->object.oid)) {3428output(o,0,_("Already up to date!"));3429*result = head;3430return1;3431}34323433 code =unpack_trees_start(o, common, head, merge);34343435if(code !=0) {3436if(show(o,4) || o->call_depth)3437err(o,_("merging of trees%sand%sfailed"),3438oid_to_hex(&head->object.oid),3439oid_to_hex(&merge->object.oid));3440unpack_trees_finish(o);3441return-1;3442}34433444if(unmerged_index(istate)) {3445struct string_list *entries;3446struct rename_info re_info;3447int i;3448/*3449 * Only need the hashmap while processing entries, so3450 * initialize it here and free it when we are done running3451 * through the entries. Keeping it in the merge_options as3452 * opposed to decaring a local hashmap is for convenience3453 * so that we don't have to pass it to around.3454 */3455hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL,512);3456get_files_dirs(o, head);3457get_files_dirs(o, merge);34583459 entries =get_unmerged(o->repo->index);3460 clean =detect_and_process_renames(o, common, head, merge,3461 entries, &re_info);3462record_df_conflict_files(o, entries);3463if(clean <0)3464goto cleanup;3465for(i = entries->nr-1;0<= i; i--) {3466const char*path = entries->items[i].string;3467struct stage_data *e = entries->items[i].util;3468if(!e->processed) {3469int ret =process_entry(o, path, e);3470if(!ret)3471 clean =0;3472else if(ret <0) {3473 clean = ret;3474goto cleanup;3475}3476}3477}3478for(i =0; i < entries->nr; i++) {3479struct stage_data *e = entries->items[i].util;3480if(!e->processed)3481BUG("unprocessed path???%s",3482 entries->items[i].string);3483}34843485 cleanup:3486final_cleanup_renames(&re_info);34873488string_list_clear(entries,1);3489free(entries);34903491hashmap_free(&o->current_file_dir_set,1);34923493if(clean <0) {3494unpack_trees_finish(o);3495return clean;3496}3497}3498else3499 clean =1;35003501unpack_trees_finish(o);35023503if(o->call_depth && !(*result =write_tree_from_memory(o)))3504return-1;35053506return clean;3507}35083509static struct commit_list *reverse_commit_list(struct commit_list *list)3510{3511struct commit_list *next = NULL, *current, *backup;3512for(current = list; current; current = backup) {3513 backup = current->next;3514 current->next = next;3515 next = current;3516}3517return next;3518}35193520/*3521 * Merge the commits h1 and h2, return the resulting virtual3522 * commit object and a flag indicating the cleanness of the merge.3523 */3524intmerge_recursive(struct merge_options *o,3525struct commit *h1,3526struct commit *h2,3527struct commit_list *ca,3528struct commit **result)3529{3530struct commit_list *iter;3531struct commit *merged_common_ancestors;3532struct tree *mrtree;3533int clean;35343535if(show(o,4)) {3536output(o,4,_("Merging:"));3537output_commit_title(o, h1);3538output_commit_title(o, h2);3539}35403541if(!ca) {3542 ca =get_merge_bases(h1, h2);3543 ca =reverse_commit_list(ca);3544}35453546if(show(o,5)) {3547unsigned cnt =commit_list_count(ca);35483549output(o,5,Q_("found%ucommon ancestor:",3550"found%ucommon ancestors:", cnt), cnt);3551for(iter = ca; iter; iter = iter->next)3552output_commit_title(o, iter->item);3553}35543555 merged_common_ancestors =pop_commit(&ca);3556if(merged_common_ancestors == NULL) {3557/* if there is no common ancestor, use an empty tree */3558struct tree *tree;35593560 tree =lookup_tree(o->repo, o->repo->hash_algo->empty_tree);3561 merged_common_ancestors =make_virtual_commit(o->repo, tree,"ancestor");3562}35633564for(iter = ca; iter; iter = iter->next) {3565const char*saved_b1, *saved_b2;3566 o->call_depth++;3567/*3568 * When the merge fails, the result contains files3569 * with conflict markers. The cleanness flag is3570 * ignored (unless indicating an error), it was never3571 * actually used, as result of merge_trees has always3572 * overwritten it: the committed "conflicts" were3573 * already resolved.3574 */3575discard_index(o->repo->index);3576 saved_b1 = o->branch1;3577 saved_b2 = o->branch2;3578 o->branch1 ="Temporary merge branch 1";3579 o->branch2 ="Temporary merge branch 2";3580if(merge_recursive(o, merged_common_ancestors, iter->item,3581 NULL, &merged_common_ancestors) <0)3582return-1;3583 o->branch1 = saved_b1;3584 o->branch2 = saved_b2;3585 o->call_depth--;35863587if(!merged_common_ancestors)3588returnerr(o,_("merge returned no commit"));3589}35903591discard_index(o->repo->index);3592if(!o->call_depth)3593repo_read_index(o->repo);35943595 o->ancestor ="merged common ancestors";3596 clean =merge_trees(o,get_commit_tree(h1),get_commit_tree(h2),3597get_commit_tree(merged_common_ancestors),3598&mrtree);3599if(clean <0) {3600flush_output(o);3601return clean;3602}36033604if(o->call_depth) {3605*result =make_virtual_commit(o->repo, mrtree,"merged tree");3606commit_list_insert(h1, &(*result)->parents);3607commit_list_insert(h2, &(*result)->parents->next);3608}3609flush_output(o);3610if(!o->call_depth && o->buffer_output <2)3611strbuf_release(&o->obuf);3612if(show(o,2))3613diff_warn_rename_limit("merge.renamelimit",3614 o->needed_rename_limit,0);3615return clean;3616}36173618static struct commit *get_ref(struct repository *repo,const struct object_id *oid,3619const char*name)3620{3621struct object *object;36223623 object =deref_tag(repo,parse_object(repo, oid),3624 name,strlen(name));3625if(!object)3626return NULL;3627if(object->type == OBJ_TREE)3628returnmake_virtual_commit(repo, (struct tree*)object, name);3629if(object->type != OBJ_COMMIT)3630return NULL;3631if(parse_commit((struct commit *)object))3632return NULL;3633return(struct commit *)object;3634}36353636intmerge_recursive_generic(struct merge_options *o,3637const struct object_id *head,3638const struct object_id *merge,3639int num_base_list,3640const struct object_id **base_list,3641struct commit **result)3642{3643int clean;3644struct lock_file lock = LOCK_INIT;3645struct commit *head_commit =get_ref(o->repo, head, o->branch1);3646struct commit *next_commit =get_ref(o->repo, merge, o->branch2);3647struct commit_list *ca = NULL;36483649if(base_list) {3650int i;3651for(i =0; i < num_base_list; ++i) {3652struct commit *base;3653if(!(base =get_ref(o->repo, base_list[i],oid_to_hex(base_list[i]))))3654returnerr(o,_("Could not parse object '%s'"),3655oid_to_hex(base_list[i]));3656commit_list_insert(base, &ca);3657}3658}36593660repo_hold_locked_index(o->repo, &lock, LOCK_DIE_ON_ERROR);3661 clean =merge_recursive(o, head_commit, next_commit, ca,3662 result);3663if(clean <0) {3664rollback_lock_file(&lock);3665return clean;3666}36673668if(write_locked_index(o->repo->index, &lock,3669 COMMIT_LOCK | SKIP_IF_UNCHANGED))3670returnerr(o,_("Unable to write index."));36713672return clean ?0:1;3673}36743675static voidmerge_recursive_config(struct merge_options *o)3676{3677char*value = NULL;3678git_config_get_int("merge.verbosity", &o->verbosity);3679git_config_get_int("diff.renamelimit", &o->diff_rename_limit);3680git_config_get_int("merge.renamelimit", &o->merge_rename_limit);3681if(!git_config_get_string("diff.renames", &value)) {3682 o->diff_detect_rename =git_config_rename("diff.renames", value);3683free(value);3684}3685if(!git_config_get_string("merge.renames", &value)) {3686 o->merge_detect_rename =git_config_rename("merge.renames", value);3687free(value);3688}3689git_config(git_xmerge_config, NULL);3690}36913692voidinit_merge_options(struct merge_options *o,3693struct repository *repo)3694{3695const char*merge_verbosity;3696memset(o,0,sizeof(struct merge_options));3697 o->repo = repo;3698 o->verbosity =2;3699 o->buffer_output =1;3700 o->diff_rename_limit = -1;3701 o->merge_rename_limit = -1;3702 o->renormalize =0;3703 o->diff_detect_rename = -1;3704 o->merge_detect_rename = -1;3705 o->detect_directory_renames =1;3706merge_recursive_config(o);3707 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3708if(merge_verbosity)3709 o->verbosity =strtol(merge_verbosity, NULL,10);3710if(o->verbosity >=5)3711 o->buffer_output =0;3712strbuf_init(&o->obuf,0);3713string_list_init(&o->df_conflict_file_set,1);3714}37153716intparse_merge_opt(struct merge_options *o,const char*s)3717{3718const char*arg;37193720if(!s || !*s)3721return-1;3722if(!strcmp(s,"ours"))3723 o->recursive_variant = MERGE_RECURSIVE_OURS;3724else if(!strcmp(s,"theirs"))3725 o->recursive_variant = MERGE_RECURSIVE_THEIRS;3726else if(!strcmp(s,"subtree"))3727 o->subtree_shift ="";3728else if(skip_prefix(s,"subtree=", &arg))3729 o->subtree_shift = arg;3730else if(!strcmp(s,"patience"))3731 o->xdl_opts =DIFF_WITH_ALG(o, PATIENCE_DIFF);3732else if(!strcmp(s,"histogram"))3733 o->xdl_opts =DIFF_WITH_ALG(o, HISTOGRAM_DIFF);3734else if(skip_prefix(s,"diff-algorithm=", &arg)) {3735long value =parse_algorithm_value(arg);3736if(value <0)3737return-1;3738/* clear out previous settings */3739DIFF_XDL_CLR(o, NEED_MINIMAL);3740 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3741 o->xdl_opts |= value;3742}3743else if(!strcmp(s,"ignore-space-change"))3744DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);3745else if(!strcmp(s,"ignore-all-space"))3746DIFF_XDL_SET(o, IGNORE_WHITESPACE);3747else if(!strcmp(s,"ignore-space-at-eol"))3748DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);3749else if(!strcmp(s,"ignore-cr-at-eol"))3750DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);3751else if(!strcmp(s,"renormalize"))3752 o->renormalize =1;3753else if(!strcmp(s,"no-renormalize"))3754 o->renormalize =0;3755else if(!strcmp(s,"no-renames"))3756 o->merge_detect_rename =0;3757else if(!strcmp(s,"find-renames")) {3758 o->merge_detect_rename =1;3759 o->rename_score =0;3760}3761else if(skip_prefix(s,"find-renames=", &arg) ||3762skip_prefix(s,"rename-threshold=", &arg)) {3763if((o->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3764return-1;3765 o->merge_detect_rename =1;3766}3767/*3768 * Please update $__git_merge_strategy_options in3769 * git-completion.bash when you add new options3770 */3771else3772return-1;3773return0;3774}