1/* 2 * Recursive Merge algorithm stolen from git-merge-recursive.py by 3 * Fredrik Kuivinen. 4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 5 */ 6#include"cache.h" 7#include"config.h" 8#include"advice.h" 9#include"lockfile.h" 10#include"cache-tree.h" 11#include"object-store.h" 12#include"repository.h" 13#include"commit.h" 14#include"blob.h" 15#include"builtin.h" 16#include"tree-walk.h" 17#include"diff.h" 18#include"diffcore.h" 19#include"tag.h" 20#include"alloc.h" 21#include"unpack-trees.h" 22#include"string-list.h" 23#include"xdiff-interface.h" 24#include"ll-merge.h" 25#include"attr.h" 26#include"merge-recursive.h" 27#include"dir.h" 28#include"submodule.h" 29#include"revision.h" 30#include"commit-reach.h" 31 32struct path_hashmap_entry { 33struct hashmap_entry e; 34char path[FLEX_ARRAY]; 35}; 36 37static intpath_hashmap_cmp(const void*cmp_data, 38const void*entry, 39const void*entry_or_key, 40const void*keydata) 41{ 42const struct path_hashmap_entry *a = entry; 43const struct path_hashmap_entry *b = entry_or_key; 44const char*key = keydata; 45 46if(ignore_case) 47returnstrcasecmp(a->path, key ? key : b->path); 48else 49returnstrcmp(a->path, key ? key : b->path); 50} 51 52static unsigned intpath_hash(const char*path) 53{ 54return ignore_case ?strihash(path) :strhash(path); 55} 56 57static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap, 58char*dir) 59{ 60struct dir_rename_entry key; 61 62if(dir == NULL) 63return NULL; 64hashmap_entry_init(&key,strhash(dir)); 65 key.dir = dir; 66returnhashmap_get(hashmap, &key, NULL); 67} 68 69static intdir_rename_cmp(const void*unused_cmp_data, 70const void*entry, 71const void*entry_or_key, 72const void*unused_keydata) 73{ 74const struct dir_rename_entry *e1 = entry; 75const struct dir_rename_entry *e2 = entry_or_key; 76 77returnstrcmp(e1->dir, e2->dir); 78} 79 80static voiddir_rename_init(struct hashmap *map) 81{ 82hashmap_init(map, dir_rename_cmp, NULL,0); 83} 84 85static voiddir_rename_entry_init(struct dir_rename_entry *entry, 86char*directory) 87{ 88hashmap_entry_init(entry,strhash(directory)); 89 entry->dir = directory; 90 entry->non_unique_new_dir =0; 91strbuf_init(&entry->new_dir,0); 92string_list_init(&entry->possible_new_dirs,0); 93} 94 95static struct collision_entry *collision_find_entry(struct hashmap *hashmap, 96char*target_file) 97{ 98struct collision_entry key; 99 100hashmap_entry_init(&key,strhash(target_file)); 101 key.target_file = target_file; 102returnhashmap_get(hashmap, &key, NULL); 103} 104 105static intcollision_cmp(void*unused_cmp_data, 106const struct collision_entry *e1, 107const struct collision_entry *e2, 108const void*unused_keydata) 109{ 110returnstrcmp(e1->target_file, e2->target_file); 111} 112 113static voidcollision_init(struct hashmap *map) 114{ 115hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL,0); 116} 117 118static voidflush_output(struct merge_options *opt) 119{ 120if(opt->buffer_output <2&& opt->obuf.len) { 121fputs(opt->obuf.buf, stdout); 122strbuf_reset(&opt->obuf); 123} 124} 125 126static interr(struct merge_options *opt,const char*err, ...) 127{ 128va_list params; 129 130if(opt->buffer_output <2) 131flush_output(opt); 132else{ 133strbuf_complete(&opt->obuf,'\n'); 134strbuf_addstr(&opt->obuf,"error: "); 135} 136va_start(params, err); 137strbuf_vaddf(&opt->obuf, err, params); 138va_end(params); 139if(opt->buffer_output >1) 140strbuf_addch(&opt->obuf,'\n'); 141else{ 142error("%s", opt->obuf.buf); 143strbuf_reset(&opt->obuf); 144} 145 146return-1; 147} 148 149static struct tree *shift_tree_object(struct repository *repo, 150struct tree *one,struct tree *two, 151const char*subtree_shift) 152{ 153struct object_id shifted; 154 155if(!*subtree_shift) { 156shift_tree(repo, &one->object.oid, &two->object.oid, &shifted,0); 157}else{ 158shift_tree_by(repo, &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 204/* 205 * Since we want to write the index eventually, we cannot reuse the index 206 * for these (temporary) data. 207 */ 208struct stage_data { 209struct diff_filespec stages[4];/* mostly for oid & mode; maybe path */ 210struct rename_conflict_info *rename_conflict_info; 211unsigned processed:1; 212}; 213 214struct rename { 215unsigned processed:1; 216struct diff_filepair *pair; 217const char*branch;/* branch that the rename occurred on */ 218/* 219 * If directory rename detection affected this rename, what was its 220 * original type ('A' or 'R') and it's original destination before 221 * the directory rename (otherwise, '\0' and NULL for these two vars). 222 */ 223char dir_rename_original_type; 224char*dir_rename_original_dest; 225/* 226 * Purpose of src_entry and dst_entry: 227 * 228 * If 'before' is renamed to 'after' then src_entry will contain 229 * the versions of 'before' from the merge_base, HEAD, and MERGE in 230 * stages 1, 2, and 3; dst_entry will contain the respective 231 * versions of 'after' in corresponding locations. Thus, we have a 232 * total of six modes and oids, though some will be null. (Stage 0 233 * is ignored; we're interested in handling conflicts.) 234 * 235 * Since we don't turn on break-rewrites by default, neither 236 * src_entry nor dst_entry can have all three of their stages have 237 * non-null oids, meaning at most four of the six will be non-null. 238 * Also, since this is a rename, both src_entry and dst_entry will 239 * have at least one non-null oid, meaning at least two will be 240 * non-null. Of the six oids, a typical rename will have three be 241 * non-null. Only two implies a rename/delete, and four implies a 242 * rename/add. 243 */ 244struct stage_data *src_entry; 245struct stage_data *dst_entry; 246}; 247 248struct rename_conflict_info { 249enum rename_type rename_type; 250struct rename *ren1; 251struct rename *ren2; 252}; 253 254staticinlinevoidsetup_rename_conflict_info(enum rename_type rename_type, 255struct merge_options *opt, 256struct rename *ren1, 257struct rename *ren2) 258{ 259struct rename_conflict_info *ci; 260 261/* 262 * When we have two renames involved, it's easiest to get the 263 * correct things into stage 2 and 3, and to make sure that the 264 * content merge puts HEAD before the other branch if we just 265 * ensure that branch1 == opt->branch1. So, simply flip arguments 266 * around if we don't have that. 267 */ 268if(ren2 && ren1->branch != opt->branch1) { 269setup_rename_conflict_info(rename_type, opt, ren2, ren1); 270return; 271} 272 273 ci =xcalloc(1,sizeof(struct rename_conflict_info)); 274 ci->rename_type = rename_type; 275 ci->ren1 = ren1; 276 ci->ren2 = ren2; 277 278 ci->ren1->dst_entry->processed =0; 279 ci->ren1->dst_entry->rename_conflict_info = ci; 280if(ren2) { 281 ci->ren2->dst_entry->rename_conflict_info = ci; 282} 283} 284 285static intshow(struct merge_options *opt,int v) 286{ 287return(!opt->call_depth && opt->verbosity >= v) || opt->verbosity >=5; 288} 289 290__attribute__((format(printf,3,4))) 291static voidoutput(struct merge_options *opt,int v,const char*fmt, ...) 292{ 293va_list ap; 294 295if(!show(opt, v)) 296return; 297 298strbuf_addchars(&opt->obuf,' ', opt->call_depth *2); 299 300va_start(ap, fmt); 301strbuf_vaddf(&opt->obuf, fmt, ap); 302va_end(ap); 303 304strbuf_addch(&opt->obuf,'\n'); 305if(!opt->buffer_output) 306flush_output(opt); 307} 308 309static voidoutput_commit_title(struct merge_options *opt,struct commit *commit) 310{ 311struct merge_remote_desc *desc; 312 313strbuf_addchars(&opt->obuf,' ', opt->call_depth *2); 314 desc =merge_remote_util(commit); 315if(desc) 316strbuf_addf(&opt->obuf,"virtual%s\n", desc->name); 317else{ 318strbuf_add_unique_abbrev(&opt->obuf, &commit->object.oid, 319 DEFAULT_ABBREV); 320strbuf_addch(&opt->obuf,' '); 321if(parse_commit(commit) !=0) 322strbuf_addstr(&opt->obuf,_("(bad commit)\n")); 323else{ 324const char*title; 325const char*msg =get_commit_buffer(commit, NULL); 326int len =find_commit_subject(msg, &title); 327if(len) 328strbuf_addf(&opt->obuf,"%.*s\n", len, title); 329unuse_commit_buffer(commit, msg); 330} 331} 332flush_output(opt); 333} 334 335static intadd_cacheinfo(struct merge_options *opt, 336const struct diff_filespec *blob, 337const char*path,int stage,int refresh,int options) 338{ 339struct index_state *istate = opt->repo->index; 340struct cache_entry *ce; 341int ret; 342 343 ce =make_cache_entry(istate, blob->mode, &blob->oid, path, stage,0); 344if(!ce) 345returnerr(opt,_("add_cacheinfo failed for path '%s'; merge aborting."), path); 346 347 ret =add_index_entry(istate, ce, options); 348if(refresh) { 349struct cache_entry *nce; 350 351 nce =refresh_cache_entry(istate, ce, 352 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); 353if(!nce) 354returnerr(opt,_("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); 355if(nce != ce) 356 ret =add_index_entry(istate, nce, options); 357} 358return ret; 359} 360 361static voidinit_tree_desc_from_tree(struct tree_desc *desc,struct tree *tree) 362{ 363parse_tree(tree); 364init_tree_desc(desc, tree->buffer, tree->size); 365} 366 367static intunpack_trees_start(struct merge_options *opt, 368struct tree *common, 369struct tree *head, 370struct tree *merge) 371{ 372int rc; 373struct tree_desc t[3]; 374struct index_state tmp_index = { NULL }; 375 376memset(&opt->unpack_opts,0,sizeof(opt->unpack_opts)); 377if(opt->call_depth) 378 opt->unpack_opts.index_only =1; 379else 380 opt->unpack_opts.update =1; 381 opt->unpack_opts.merge =1; 382 opt->unpack_opts.head_idx =2; 383 opt->unpack_opts.fn = threeway_merge; 384 opt->unpack_opts.src_index = opt->repo->index; 385 opt->unpack_opts.dst_index = &tmp_index; 386 opt->unpack_opts.aggressive = !merge_detect_rename(opt); 387setup_unpack_trees_porcelain(&opt->unpack_opts,"merge"); 388 389init_tree_desc_from_tree(t+0, common); 390init_tree_desc_from_tree(t+1, head); 391init_tree_desc_from_tree(t+2, merge); 392 393 rc =unpack_trees(3, t, &opt->unpack_opts); 394cache_tree_free(&opt->repo->index->cache_tree); 395 396/* 397 * Update opt->repo->index to match the new results, AFTER saving a copy 398 * in opt->orig_index. Update src_index to point to the saved copy. 399 * (verify_uptodate() checks src_index, and the original index is 400 * the one that had the necessary modification timestamps.) 401 */ 402 opt->orig_index = *opt->repo->index; 403*opt->repo->index = tmp_index; 404 opt->unpack_opts.src_index = &opt->orig_index; 405 406return rc; 407} 408 409static voidunpack_trees_finish(struct merge_options *opt) 410{ 411discard_index(&opt->orig_index); 412clear_unpack_trees_porcelain(&opt->unpack_opts); 413} 414 415struct tree *write_tree_from_memory(struct merge_options *opt) 416{ 417struct tree *result = NULL; 418struct index_state *istate = opt->repo->index; 419 420if(unmerged_index(istate)) { 421int i; 422fprintf(stderr,"BUG: There are unmerged index entries:\n"); 423for(i =0; i < istate->cache_nr; i++) { 424const struct cache_entry *ce = istate->cache[i]; 425if(ce_stage(ce)) 426fprintf(stderr,"BUG:%d%.*s\n",ce_stage(ce), 427(int)ce_namelen(ce), ce->name); 428} 429BUG("unmerged index entries in merge-recursive.c"); 430} 431 432if(!istate->cache_tree) 433 istate->cache_tree =cache_tree(); 434 435if(!cache_tree_fully_valid(istate->cache_tree) && 436cache_tree_update(istate,0) <0) { 437err(opt,_("error building trees")); 438return NULL; 439} 440 441 result =lookup_tree(opt->repo, &istate->cache_tree->oid); 442 443return result; 444} 445 446static intsave_files_dirs(const struct object_id *oid, 447struct strbuf *base,const char*path, 448unsigned int mode,int stage,void*context) 449{ 450struct path_hashmap_entry *entry; 451int baselen = base->len; 452struct merge_options *opt = context; 453 454strbuf_addstr(base, path); 455 456FLEX_ALLOC_MEM(entry, path, base->buf, base->len); 457hashmap_entry_init(entry,path_hash(entry->path)); 458hashmap_add(&opt->current_file_dir_set, entry); 459 460strbuf_setlen(base, baselen); 461return(S_ISDIR(mode) ? READ_TREE_RECURSIVE :0); 462} 463 464static voidget_files_dirs(struct merge_options *opt,struct tree *tree) 465{ 466struct pathspec match_all; 467memset(&match_all,0,sizeof(match_all)); 468read_tree_recursive(opt->repo, tree,"",0,0, 469&match_all, save_files_dirs, opt); 470} 471 472static intget_tree_entry_if_blob(struct repository *r, 473const struct object_id *tree, 474const char*path, 475struct diff_filespec *dfs) 476{ 477int ret; 478 479 ret =get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode); 480if(S_ISDIR(dfs->mode)) { 481oidcpy(&dfs->oid, &null_oid); 482 dfs->mode =0; 483} 484return ret; 485} 486 487/* 488 * Returns an index_entry instance which doesn't have to correspond to 489 * a real cache entry in Git's index. 490 */ 491static struct stage_data *insert_stage_data(struct repository *r, 492const char*path, 493struct tree *o,struct tree *a,struct tree *b, 494struct string_list *entries) 495{ 496struct string_list_item *item; 497struct stage_data *e =xcalloc(1,sizeof(struct stage_data)); 498get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]); 499get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]); 500get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]); 501 item =string_list_insert(entries, path); 502 item->util = e; 503return e; 504} 505 506/* 507 * Create a dictionary mapping file names to stage_data objects. The 508 * dictionary contains one entry for every path with a non-zero stage entry. 509 */ 510static struct string_list *get_unmerged(struct index_state *istate) 511{ 512struct string_list *unmerged =xcalloc(1,sizeof(struct string_list)); 513int i; 514 515 unmerged->strdup_strings =1; 516 517for(i =0; i < istate->cache_nr; i++) { 518struct string_list_item *item; 519struct stage_data *e; 520const struct cache_entry *ce = istate->cache[i]; 521if(!ce_stage(ce)) 522continue; 523 524 item =string_list_lookup(unmerged, ce->name); 525if(!item) { 526 item =string_list_insert(unmerged, ce->name); 527 item->util =xcalloc(1,sizeof(struct stage_data)); 528} 529 e = item->util; 530 e->stages[ce_stage(ce)].mode = ce->ce_mode; 531oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); 532} 533 534return unmerged; 535} 536 537static intstring_list_df_name_compare(const char*one,const char*two) 538{ 539int onelen =strlen(one); 540int twolen =strlen(two); 541/* 542 * Here we only care that entries for D/F conflicts are 543 * adjacent, in particular with the file of the D/F conflict 544 * appearing before files below the corresponding directory. 545 * The order of the rest of the list is irrelevant for us. 546 * 547 * To achieve this, we sort with df_name_compare and provide 548 * the mode S_IFDIR so that D/F conflicts will sort correctly. 549 * We use the mode S_IFDIR for everything else for simplicity, 550 * since in other cases any changes in their order due to 551 * sorting cause no problems for us. 552 */ 553int cmp =df_name_compare(one, onelen, S_IFDIR, 554 two, twolen, S_IFDIR); 555/* 556 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure 557 * that 'foo' comes before 'foo/bar'. 558 */ 559if(cmp) 560return cmp; 561return onelen - twolen; 562} 563 564static voidrecord_df_conflict_files(struct merge_options *opt, 565struct string_list *entries) 566{ 567/* If there is a D/F conflict and the file for such a conflict 568 * currently exists in the working tree, we want to allow it to be 569 * removed to make room for the corresponding directory if needed. 570 * The files underneath the directories of such D/F conflicts will 571 * be processed before the corresponding file involved in the D/F 572 * conflict. If the D/F directory ends up being removed by the 573 * merge, then we won't have to touch the D/F file. If the D/F 574 * directory needs to be written to the working copy, then the D/F 575 * file will simply be removed (in make_room_for_path()) to make 576 * room for the necessary paths. Note that if both the directory 577 * and the file need to be present, then the D/F file will be 578 * reinstated with a new unique name at the time it is processed. 579 */ 580struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; 581const char*last_file = NULL; 582int last_len =0; 583int i; 584 585/* 586 * If we're merging merge-bases, we don't want to bother with 587 * any working directory changes. 588 */ 589if(opt->call_depth) 590return; 591 592/* Ensure D/F conflicts are adjacent in the entries list. */ 593for(i =0; i < entries->nr; i++) { 594struct string_list_item *next = &entries->items[i]; 595string_list_append(&df_sorted_entries, next->string)->util = 596 next->util; 597} 598 df_sorted_entries.cmp = string_list_df_name_compare; 599string_list_sort(&df_sorted_entries); 600 601string_list_clear(&opt->df_conflict_file_set,1); 602for(i =0; i < df_sorted_entries.nr; i++) { 603const char*path = df_sorted_entries.items[i].string; 604int len =strlen(path); 605struct stage_data *e = df_sorted_entries.items[i].util; 606 607/* 608 * Check if last_file & path correspond to a D/F conflict; 609 * i.e. whether path is last_file+'/'+<something>. 610 * If so, record that it's okay to remove last_file to make 611 * room for path and friends if needed. 612 */ 613if(last_file && 614 len > last_len && 615memcmp(path, last_file, last_len) ==0&& 616 path[last_len] =='/') { 617string_list_insert(&opt->df_conflict_file_set, last_file); 618} 619 620/* 621 * Determine whether path could exist as a file in the 622 * working directory as a possible D/F conflict. This 623 * will only occur when it exists in stage 2 as a 624 * file. 625 */ 626if(S_ISREG(e->stages[2].mode) ||S_ISLNK(e->stages[2].mode)) { 627 last_file = path; 628 last_len = len; 629}else{ 630 last_file = NULL; 631} 632} 633string_list_clear(&df_sorted_entries,0); 634} 635 636static intupdate_stages(struct merge_options *opt,const char*path, 637const struct diff_filespec *o, 638const struct diff_filespec *a, 639const struct diff_filespec *b) 640{ 641 642/* 643 * NOTE: It is usually a bad idea to call update_stages on a path 644 * before calling update_file on that same path, since it can 645 * sometimes lead to spurious "refusing to lose untracked file..." 646 * messages from update_file (via make_room_for path via 647 * would_lose_untracked). Instead, reverse the order of the calls 648 * (executing update_file first and then update_stages). 649 */ 650int clear =1; 651int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; 652if(clear) 653if(remove_file_from_index(opt->repo->index, path)) 654return-1; 655if(o) 656if(add_cacheinfo(opt, o, path,1,0, options)) 657return-1; 658if(a) 659if(add_cacheinfo(opt, a, path,2,0, options)) 660return-1; 661if(b) 662if(add_cacheinfo(opt, b, path,3,0, options)) 663return-1; 664return0; 665} 666 667static voidupdate_entry(struct stage_data *entry, 668struct diff_filespec *o, 669struct diff_filespec *a, 670struct diff_filespec *b) 671{ 672 entry->processed =0; 673 entry->stages[1].mode = o->mode; 674 entry->stages[2].mode = a->mode; 675 entry->stages[3].mode = b->mode; 676oidcpy(&entry->stages[1].oid, &o->oid); 677oidcpy(&entry->stages[2].oid, &a->oid); 678oidcpy(&entry->stages[3].oid, &b->oid); 679} 680 681static intremove_file(struct merge_options *opt,int clean, 682const char*path,int no_wd) 683{ 684int update_cache = opt->call_depth || clean; 685int update_working_directory = !opt->call_depth && !no_wd; 686 687if(update_cache) { 688if(remove_file_from_index(opt->repo->index, path)) 689return-1; 690} 691if(update_working_directory) { 692if(ignore_case) { 693struct cache_entry *ce; 694 ce =index_file_exists(opt->repo->index, path,strlen(path), 695 ignore_case); 696if(ce &&ce_stage(ce) ==0&&strcmp(path, ce->name)) 697return0; 698} 699if(remove_path(path)) 700return-1; 701} 702return0; 703} 704 705/* add a string to a strbuf, but converting "/" to "_" */ 706static voidadd_flattened_path(struct strbuf *out,const char*s) 707{ 708size_t i = out->len; 709strbuf_addstr(out, s); 710for(; i < out->len; i++) 711if(out->buf[i] =='/') 712 out->buf[i] ='_'; 713} 714 715static char*unique_path(struct merge_options *opt,const char*path,const char*branch) 716{ 717struct path_hashmap_entry *entry; 718struct strbuf newpath = STRBUF_INIT; 719int suffix =0; 720size_t base_len; 721 722strbuf_addf(&newpath,"%s~", path); 723add_flattened_path(&newpath, branch); 724 725 base_len = newpath.len; 726while(hashmap_get_from_hash(&opt->current_file_dir_set, 727path_hash(newpath.buf), newpath.buf) || 728(!opt->call_depth &&file_exists(newpath.buf))) { 729strbuf_setlen(&newpath, base_len); 730strbuf_addf(&newpath,"_%d", suffix++); 731} 732 733FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); 734hashmap_entry_init(entry,path_hash(entry->path)); 735hashmap_add(&opt->current_file_dir_set, entry); 736returnstrbuf_detach(&newpath, NULL); 737} 738 739/** 740 * Check whether a directory in the index is in the way of an incoming 741 * file. Return 1 if so. If check_working_copy is non-zero, also 742 * check the working directory. If empty_ok is non-zero, also return 743 * 0 in the case where the working-tree dir exists but is empty. 744 */ 745static intdir_in_way(struct index_state *istate,const char*path, 746int check_working_copy,int empty_ok) 747{ 748int pos; 749struct strbuf dirpath = STRBUF_INIT; 750struct stat st; 751 752strbuf_addstr(&dirpath, path); 753strbuf_addch(&dirpath,'/'); 754 755 pos =index_name_pos(istate, dirpath.buf, dirpath.len); 756 757if(pos <0) 758 pos = -1- pos; 759if(pos < istate->cache_nr && 760!strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) { 761strbuf_release(&dirpath); 762return1; 763} 764 765strbuf_release(&dirpath); 766return check_working_copy && !lstat(path, &st) &&S_ISDIR(st.st_mode) && 767!(empty_ok &&is_empty_dir(path)); 768} 769 770/* 771 * Returns whether path was tracked in the index before the merge started, 772 * and its oid and mode match the specified values 773 */ 774static intwas_tracked_and_matches(struct merge_options *opt,const char*path, 775const struct diff_filespec *blob) 776{ 777int pos =index_name_pos(&opt->orig_index, path,strlen(path)); 778struct cache_entry *ce; 779 780if(0> pos) 781/* we were not tracking this path before the merge */ 782return0; 783 784/* See if the file we were tracking before matches */ 785 ce = opt->orig_index.cache[pos]; 786return(oid_eq(&ce->oid, &blob->oid) && ce->ce_mode == blob->mode); 787} 788 789/* 790 * Returns whether path was tracked in the index before the merge started 791 */ 792static intwas_tracked(struct merge_options *opt,const char*path) 793{ 794int pos =index_name_pos(&opt->orig_index, path,strlen(path)); 795 796if(0<= pos) 797/* we were tracking this path before the merge */ 798return1; 799 800return0; 801} 802 803static intwould_lose_untracked(struct merge_options *opt,const char*path) 804{ 805struct index_state *istate = opt->repo->index; 806 807/* 808 * This may look like it can be simplified to: 809 * return !was_tracked(opt, path) && file_exists(path) 810 * but it can't. This function needs to know whether path was in 811 * the working tree due to EITHER having been tracked in the index 812 * before the merge OR having been put into the working copy and 813 * index by unpack_trees(). Due to that either-or requirement, we 814 * check the current index instead of the original one. 815 * 816 * Note that we do not need to worry about merge-recursive itself 817 * updating the index after unpack_trees() and before calling this 818 * function, because we strictly require all code paths in 819 * merge-recursive to update the working tree first and the index 820 * second. Doing otherwise would break 821 * update_file()/would_lose_untracked(); see every comment in this 822 * file which mentions "update_stages". 823 */ 824int pos =index_name_pos(istate, path,strlen(path)); 825 826if(pos <0) 827 pos = -1- pos; 828while(pos < istate->cache_nr && 829!strcmp(path, istate->cache[pos]->name)) { 830/* 831 * If stage #0, it is definitely tracked. 832 * If it has stage #2 then it was tracked 833 * before this merge started. All other 834 * cases the path was not tracked. 835 */ 836switch(ce_stage(istate->cache[pos])) { 837case0: 838case2: 839return0; 840} 841 pos++; 842} 843returnfile_exists(path); 844} 845 846static intwas_dirty(struct merge_options *opt,const char*path) 847{ 848struct cache_entry *ce; 849int dirty =1; 850 851if(opt->call_depth || !was_tracked(opt, path)) 852return!dirty; 853 854 ce =index_file_exists(opt->unpack_opts.src_index, 855 path,strlen(path), ignore_case); 856 dirty =verify_uptodate(ce, &opt->unpack_opts) !=0; 857return dirty; 858} 859 860static intmake_room_for_path(struct merge_options *opt,const char*path) 861{ 862int status, i; 863const char*msg =_("failed to create path '%s'%s"); 864 865/* Unlink any D/F conflict files that are in the way */ 866for(i =0; i < opt->df_conflict_file_set.nr; i++) { 867const char*df_path = opt->df_conflict_file_set.items[i].string; 868size_t pathlen =strlen(path); 869size_t df_pathlen =strlen(df_path); 870if(df_pathlen < pathlen && 871 path[df_pathlen] =='/'&& 872strncmp(path, df_path, df_pathlen) ==0) { 873output(opt,3, 874_("Removing%sto make room for subdirectory\n"), 875 df_path); 876unlink(df_path); 877unsorted_string_list_delete_item(&opt->df_conflict_file_set, 878 i,0); 879break; 880} 881} 882 883/* Make sure leading directories are created */ 884 status =safe_create_leading_directories_const(path); 885if(status) { 886if(status == SCLD_EXISTS) 887/* something else exists */ 888returnerr(opt, msg, path,_(": perhaps a D/F conflict?")); 889returnerr(opt, msg, path,""); 890} 891 892/* 893 * Do not unlink a file in the work tree if we are not 894 * tracking it. 895 */ 896if(would_lose_untracked(opt, path)) 897returnerr(opt,_("refusing to lose untracked file at '%s'"), 898 path); 899 900/* Successful unlink is good.. */ 901if(!unlink(path)) 902return0; 903/* .. and so is no existing file */ 904if(errno == ENOENT) 905return0; 906/* .. but not some other error (who really cares what?) */ 907returnerr(opt, msg, path,_(": perhaps a D/F conflict?")); 908} 909 910static intupdate_file_flags(struct merge_options *opt, 911const struct diff_filespec *contents, 912const char*path, 913int update_cache, 914int update_wd) 915{ 916int ret =0; 917 918if(opt->call_depth) 919 update_wd =0; 920 921if(update_wd) { 922enum object_type type; 923void*buf; 924unsigned long size; 925 926if(S_ISGITLINK(contents->mode)) { 927/* 928 * We may later decide to recursively descend into 929 * the submodule directory and update its index 930 * and/or work tree, but we do not do that now. 931 */ 932 update_wd =0; 933goto update_index; 934} 935 936 buf =read_object_file(&contents->oid, &type, &size); 937if(!buf) 938returnerr(opt,_("cannot read object%s'%s'"), 939oid_to_hex(&contents->oid), path); 940if(type != OBJ_BLOB) { 941 ret =err(opt,_("blob expected for%s'%s'"), 942oid_to_hex(&contents->oid), path); 943goto free_buf; 944} 945if(S_ISREG(contents->mode)) { 946struct strbuf strbuf = STRBUF_INIT; 947if(convert_to_working_tree(opt->repo->index, path, buf, size, &strbuf)) { 948free(buf); 949 size = strbuf.len; 950 buf =strbuf_detach(&strbuf, NULL); 951} 952} 953 954if(make_room_for_path(opt, path) <0) { 955 update_wd =0; 956goto free_buf; 957} 958if(S_ISREG(contents->mode) || 959(!has_symlinks &&S_ISLNK(contents->mode))) { 960int fd; 961int mode = (contents->mode &0100?0777:0666); 962 963 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); 964if(fd <0) { 965 ret =err(opt,_("failed to open '%s':%s"), 966 path,strerror(errno)); 967goto free_buf; 968} 969write_in_full(fd, buf, size); 970close(fd); 971}else if(S_ISLNK(contents->mode)) { 972char*lnk =xmemdupz(buf, size); 973safe_create_leading_directories_const(path); 974unlink(path); 975if(symlink(lnk, path)) 976 ret =err(opt,_("failed to symlink '%s':%s"), 977 path,strerror(errno)); 978free(lnk); 979}else 980 ret =err(opt, 981_("do not know what to do with%06o%s'%s'"), 982 contents->mode,oid_to_hex(&contents->oid), path); 983 free_buf: 984free(buf); 985} 986update_index: 987if(!ret && update_cache) 988if(add_cacheinfo(opt, contents, path,0, update_wd, 989 ADD_CACHE_OK_TO_ADD)) 990return-1; 991return ret; 992} 993 994static intupdate_file(struct merge_options *opt, 995int clean, 996const struct diff_filespec *contents, 997const char*path) 998{ 999returnupdate_file_flags(opt, contents, path,1000 opt->call_depth || clean, !opt->call_depth);1001}10021003/* Low level file merging, update and removal */10041005struct merge_file_info {1006struct diff_filespec blob;/* mostly use oid & mode; sometimes path */1007unsigned clean:1,1008 merge:1;1009};10101011static intmerge_3way(struct merge_options *opt,1012 mmbuffer_t *result_buf,1013const struct diff_filespec *o,1014const struct diff_filespec *a,1015const struct diff_filespec *b,1016const char*branch1,1017const char*branch2,1018const int extra_marker_size)1019{1020 mmfile_t orig, src1, src2;1021struct ll_merge_options ll_opts = {0};1022char*base_name, *name1, *name2;1023int merge_status;10241025 ll_opts.renormalize = opt->renormalize;1026 ll_opts.extra_marker_size = extra_marker_size;1027 ll_opts.xdl_opts = opt->xdl_opts;10281029if(opt->call_depth) {1030 ll_opts.virtual_ancestor =1;1031 ll_opts.variant =0;1032}else{1033switch(opt->recursive_variant) {1034case MERGE_RECURSIVE_OURS:1035 ll_opts.variant = XDL_MERGE_FAVOR_OURS;1036break;1037case MERGE_RECURSIVE_THEIRS:1038 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;1039break;1040default:1041 ll_opts.variant =0;1042break;1043}1044}10451046assert(a->path && b->path);1047if(strcmp(a->path, b->path) ||1048(opt->ancestor != NULL &&strcmp(a->path, o->path) !=0)) {1049 base_name = opt->ancestor == NULL ? NULL :1050mkpathdup("%s:%s", opt->ancestor, o->path);1051 name1 =mkpathdup("%s:%s", branch1, a->path);1052 name2 =mkpathdup("%s:%s", branch2, b->path);1053}else{1054 base_name = opt->ancestor == NULL ? NULL :1055mkpathdup("%s", opt->ancestor);1056 name1 =mkpathdup("%s", branch1);1057 name2 =mkpathdup("%s", branch2);1058}10591060read_mmblob(&orig, &o->oid);1061read_mmblob(&src1, &a->oid);1062read_mmblob(&src2, &b->oid);10631064 merge_status =ll_merge(result_buf, a->path, &orig, base_name,1065&src1, name1, &src2, name2,1066 opt->repo->index, &ll_opts);10671068free(base_name);1069free(name1);1070free(name2);1071free(orig.ptr);1072free(src1.ptr);1073free(src2.ptr);1074return merge_status;1075}10761077static intfind_first_merges(struct repository *repo,1078struct object_array *result,const char*path,1079struct commit *a,struct commit *b)1080{1081int i, j;1082struct object_array merges = OBJECT_ARRAY_INIT;1083struct commit *commit;1084int contains_another;10851086char merged_revision[GIT_MAX_HEXSZ +2];1087const char*rev_args[] = {"rev-list","--merges","--ancestry-path",1088"--all", merged_revision, NULL };1089struct rev_info revs;1090struct setup_revision_opt rev_opts;10911092memset(result,0,sizeof(struct object_array));1093memset(&rev_opts,0,sizeof(rev_opts));10941095/* get all revisions that merge commit a */1096xsnprintf(merged_revision,sizeof(merged_revision),"^%s",1097oid_to_hex(&a->object.oid));1098repo_init_revisions(repo, &revs, NULL);1099 rev_opts.submodule = path;1100/* FIXME: can't handle linked worktrees in submodules yet */1101 revs.single_worktree = path != NULL;1102setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);11031104/* save all revisions from the above list that contain b */1105if(prepare_revision_walk(&revs))1106die("revision walk setup failed");1107while((commit =get_revision(&revs)) != NULL) {1108struct object *o = &(commit->object);1109if(in_merge_bases(b, commit))1110add_object_array(o, NULL, &merges);1111}1112reset_revision_walk();11131114/* Now we've got all merges that contain a and b. Prune all1115 * merges that contain another found merge and save them in1116 * result.1117 */1118for(i =0; i < merges.nr; i++) {1119struct commit *m1 = (struct commit *) merges.objects[i].item;11201121 contains_another =0;1122for(j =0; j < merges.nr; j++) {1123struct commit *m2 = (struct commit *) merges.objects[j].item;1124if(i != j &&in_merge_bases(m2, m1)) {1125 contains_another =1;1126break;1127}1128}11291130if(!contains_another)1131add_object_array(merges.objects[i].item, NULL, result);1132}11331134object_array_clear(&merges);1135return result->nr;1136}11371138static voidprint_commit(struct commit *commit)1139{1140struct strbuf sb = STRBUF_INIT;1141struct pretty_print_context ctx = {0};1142 ctx.date_mode.type = DATE_NORMAL;1143format_commit_message(commit,"%h:%m %s", &sb, &ctx);1144fprintf(stderr,"%s\n", sb.buf);1145strbuf_release(&sb);1146}11471148static intis_valid(const struct diff_filespec *dfs)1149{1150return dfs->mode !=0&& !is_null_oid(&dfs->oid);1151}11521153static intmerge_submodule(struct merge_options *opt,1154struct object_id *result,const char*path,1155const struct object_id *base,const struct object_id *a,1156const struct object_id *b)1157{1158struct commit *commit_base, *commit_a, *commit_b;1159int parent_count;1160struct object_array merges;11611162int i;1163int search = !opt->call_depth;11641165/* store a in result in case we fail */1166oidcpy(result, a);11671168/* we can not handle deletion conflicts */1169if(is_null_oid(base))1170return0;1171if(is_null_oid(a))1172return0;1173if(is_null_oid(b))1174return0;11751176if(add_submodule_odb(path)) {1177output(opt,1,_("Failed to merge submodule%s(not checked out)"), path);1178return0;1179}11801181if(!(commit_base =lookup_commit_reference(opt->repo, base)) ||1182!(commit_a =lookup_commit_reference(opt->repo, a)) ||1183!(commit_b =lookup_commit_reference(opt->repo, b))) {1184output(opt,1,_("Failed to merge submodule%s(commits not present)"), path);1185return0;1186}11871188/* check whether both changes are forward */1189if(!in_merge_bases(commit_base, commit_a) ||1190!in_merge_bases(commit_base, commit_b)) {1191output(opt,1,_("Failed to merge submodule%s(commits don't follow merge-base)"), path);1192return0;1193}11941195/* Case #1: a is contained in b or vice versa */1196if(in_merge_bases(commit_a, commit_b)) {1197oidcpy(result, b);1198if(show(opt,3)) {1199output(opt,3,_("Fast-forwarding submodule%sto the following commit:"), path);1200output_commit_title(opt, commit_b);1201}else if(show(opt,2))1202output(opt,2,_("Fast-forwarding submodule%s"), path);1203else1204;/* no output */12051206return1;1207}1208if(in_merge_bases(commit_b, commit_a)) {1209oidcpy(result, a);1210if(show(opt,3)) {1211output(opt,3,_("Fast-forwarding submodule%sto the following commit:"), path);1212output_commit_title(opt, commit_a);1213}else if(show(opt,2))1214output(opt,2,_("Fast-forwarding submodule%s"), path);1215else1216;/* no output */12171218return1;1219}12201221/*1222 * Case #2: There are one or more merges that contain a and b in1223 * the submodule. If there is only one, then present it as a1224 * suggestion to the user, but leave it marked unmerged so the1225 * user needs to confirm the resolution.1226 */12271228/* Skip the search if makes no sense to the calling context. */1229if(!search)1230return0;12311232/* find commit which merges them */1233 parent_count =find_first_merges(opt->repo, &merges, path,1234 commit_a, commit_b);1235switch(parent_count) {1236case0:1237output(opt,1,_("Failed to merge submodule%s(merge following commits not found)"), path);1238break;12391240case1:1241output(opt,1,_("Failed to merge submodule%s(not fast-forward)"), path);1242output(opt,2,_("Found a possible merge resolution for the submodule:\n"));1243print_commit((struct commit *) merges.objects[0].item);1244output(opt,2,_(1245"If this is correct simply add it to the index "1246"for example\n"1247"by using:\n\n"1248" git update-index --cacheinfo 160000%s\"%s\"\n\n"1249"which will accept this suggestion.\n"),1250oid_to_hex(&merges.objects[0].item->oid), path);1251break;12521253default:1254output(opt,1,_("Failed to merge submodule%s(multiple merges found)"), path);1255for(i =0; i < merges.nr; i++)1256print_commit((struct commit *) merges.objects[i].item);1257}12581259object_array_clear(&merges);1260return0;1261}12621263static intmerge_mode_and_contents(struct merge_options *opt,1264const struct diff_filespec *o,1265const struct diff_filespec *a,1266const struct diff_filespec *b,1267const char*filename,1268const char*branch1,1269const char*branch2,1270const int extra_marker_size,1271struct merge_file_info *result)1272{1273if(opt->branch1 != branch1) {1274/*1275 * It's weird getting a reverse merge with HEAD on the bottom1276 * side of the conflict markers and the other branch on the1277 * top. Fix that.1278 */1279returnmerge_mode_and_contents(opt, o, b, a,1280 filename,1281 branch2, branch1,1282 extra_marker_size, result);1283}12841285 result->merge =0;1286 result->clean =1;12871288if((S_IFMT & a->mode) != (S_IFMT & b->mode)) {1289 result->clean =0;1290if(S_ISREG(a->mode)) {1291 result->blob.mode = a->mode;1292oidcpy(&result->blob.oid, &a->oid);1293}else{1294 result->blob.mode = b->mode;1295oidcpy(&result->blob.oid, &b->oid);1296}1297}else{1298if(!oid_eq(&a->oid, &o->oid) && !oid_eq(&b->oid, &o->oid))1299 result->merge =1;13001301/*1302 * Merge modes1303 */1304if(a->mode == b->mode || a->mode == o->mode)1305 result->blob.mode = b->mode;1306else{1307 result->blob.mode = a->mode;1308if(b->mode != o->mode) {1309 result->clean =0;1310 result->merge =1;1311}1312}13131314if(oid_eq(&a->oid, &b->oid) ||oid_eq(&a->oid, &o->oid))1315oidcpy(&result->blob.oid, &b->oid);1316else if(oid_eq(&b->oid, &o->oid))1317oidcpy(&result->blob.oid, &a->oid);1318else if(S_ISREG(a->mode)) {1319 mmbuffer_t result_buf;1320int ret =0, merge_status;13211322 merge_status =merge_3way(opt, &result_buf, o, a, b,1323 branch1, branch2,1324 extra_marker_size);13251326if((merge_status <0) || !result_buf.ptr)1327 ret =err(opt,_("Failed to execute internal merge"));13281329if(!ret &&1330write_object_file(result_buf.ptr, result_buf.size,1331 blob_type, &result->blob.oid))1332 ret =err(opt,_("Unable to add%sto database"),1333 a->path);13341335free(result_buf.ptr);1336if(ret)1337return ret;1338 result->clean = (merge_status ==0);1339}else if(S_ISGITLINK(a->mode)) {1340 result->clean =merge_submodule(opt, &result->blob.oid,1341 o->path,1342&o->oid,1343&a->oid,1344&b->oid);1345}else if(S_ISLNK(a->mode)) {1346switch(opt->recursive_variant) {1347case MERGE_RECURSIVE_NORMAL:1348oidcpy(&result->blob.oid, &a->oid);1349if(!oid_eq(&a->oid, &b->oid))1350 result->clean =0;1351break;1352case MERGE_RECURSIVE_OURS:1353oidcpy(&result->blob.oid, &a->oid);1354break;1355case MERGE_RECURSIVE_THEIRS:1356oidcpy(&result->blob.oid, &b->oid);1357break;1358}1359}else1360BUG("unsupported object type in the tree");1361}13621363if(result->merge)1364output(opt,2,_("Auto-merging%s"), filename);13651366return0;1367}13681369static inthandle_rename_via_dir(struct merge_options *opt,1370struct rename_conflict_info *ci)1371{1372/*1373 * Handle file adds that need to be renamed due to directory rename1374 * detection. This differs from handle_rename_normal, because1375 * there is no content merge to do; just move the file into the1376 * desired final location.1377 */1378const struct rename *ren = ci->ren1;1379const struct diff_filespec *dest = ren->pair->two;1380char*file_path = dest->path;1381int mark_conflicted = (opt->detect_directory_renames ==1);1382assert(ren->dir_rename_original_dest);13831384if(!opt->call_depth &&would_lose_untracked(opt, dest->path)) {1385 mark_conflicted =1;1386 file_path =unique_path(opt, dest->path, ren->branch);1387output(opt,1,_("Error: Refusing to lose untracked file at%s; "1388"writing to%sinstead."),1389 dest->path, file_path);1390}13911392if(mark_conflicted) {1393/*1394 * Write the file in worktree at file_path. In the index,1395 * only record the file at dest->path in the appropriate1396 * higher stage.1397 */1398if(update_file(opt,0, dest, file_path))1399return-1;1400if(file_path != dest->path)1401free(file_path);1402if(update_stages(opt, dest->path, NULL,1403 ren->branch == opt->branch1 ? dest : NULL,1404 ren->branch == opt->branch1 ? NULL : dest))1405return-1;1406return0;/* not clean, but conflicted */1407}else{1408/* Update dest->path both in index and in worktree */1409if(update_file(opt,1, dest, dest->path))1410return-1;1411return1;/* clean */1412}1413}14141415static inthandle_change_delete(struct merge_options *opt,1416const char*path,const char*old_path,1417const struct diff_filespec *o,1418const struct diff_filespec *changed,1419const char*change_branch,1420const char*delete_branch,1421const char*change,const char*change_past)1422{1423char*alt_path = NULL;1424const char*update_path = path;1425int ret =0;14261427if(dir_in_way(opt->repo->index, path, !opt->call_depth,0) ||1428(!opt->call_depth &&would_lose_untracked(opt, path))) {1429 update_path = alt_path =unique_path(opt, path, change_branch);1430}14311432if(opt->call_depth) {1433/*1434 * We cannot arbitrarily accept either a_sha or b_sha as1435 * correct; since there is no true "middle point" between1436 * them, simply reuse the base version for virtual merge base.1437 */1438 ret =remove_file_from_index(opt->repo->index, path);1439if(!ret)1440 ret =update_file(opt,0, o, update_path);1441}else{1442/*1443 * Despite the four nearly duplicate messages and argument1444 * lists below and the ugliness of the nested if-statements,1445 * having complete messages makes the job easier for1446 * translators.1447 *1448 * The slight variance among the cases is due to the fact1449 * that:1450 * 1) directory/file conflicts (in effect if1451 * !alt_path) could cause us to need to write the1452 * file to a different path.1453 * 2) renames (in effect if !old_path) could mean that1454 * there are two names for the path that the user1455 * may know the file by.1456 */1457if(!alt_path) {1458if(!old_path) {1459output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1460"and%sin%s. Version%sof%sleft in tree."),1461 change, path, delete_branch, change_past,1462 change_branch, change_branch, path);1463}else{1464output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1465"and%sto%sin%s. Version%sof%sleft in tree."),1466 change, old_path, delete_branch, change_past, path,1467 change_branch, change_branch, path);1468}1469}else{1470if(!old_path) {1471output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1472"and%sin%s. Version%sof%sleft in tree at%s."),1473 change, path, delete_branch, change_past,1474 change_branch, change_branch, path, alt_path);1475}else{1476output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1477"and%sto%sin%s. Version%sof%sleft in tree at%s."),1478 change, old_path, delete_branch, change_past, path,1479 change_branch, change_branch, path, alt_path);1480}1481}1482/*1483 * No need to call update_file() on path when change_branch ==1484 * opt->branch1 && !alt_path, since that would needlessly touch1485 * path. We could call update_file_flags() with update_cache=01486 * and update_wd=0, but that's a no-op.1487 */1488if(change_branch != opt->branch1 || alt_path)1489 ret =update_file(opt,0, changed, update_path);1490}1491free(alt_path);14921493return ret;1494}14951496static inthandle_rename_delete(struct merge_options *opt,1497struct rename_conflict_info *ci)1498{1499const struct rename *ren = ci->ren1;1500const struct diff_filespec *orig = ren->pair->one;1501const struct diff_filespec *dest = ren->pair->two;1502const char*rename_branch = ren->branch;1503const char*delete_branch = (opt->branch1 == ren->branch ?1504 opt->branch2 : opt->branch1);15051506if(handle_change_delete(opt,1507 opt->call_depth ? orig->path : dest->path,1508 opt->call_depth ? NULL : orig->path,1509 orig, dest,1510 rename_branch, delete_branch,1511_("rename"),_("renamed")))1512return-1;15131514if(opt->call_depth)1515returnremove_file_from_index(opt->repo->index, dest->path);1516else1517returnupdate_stages(opt, dest->path, NULL,1518 rename_branch == opt->branch1 ? dest : NULL,1519 rename_branch == opt->branch1 ? NULL : dest);1520}15211522static inthandle_file_collision(struct merge_options *opt,1523const char*collide_path,1524const char*prev_path1,1525const char*prev_path2,1526const char*branch1,const char*branch2,1527struct diff_filespec *a,1528struct diff_filespec *b)1529{1530struct merge_file_info mfi;1531struct diff_filespec null;1532char*alt_path = NULL;1533const char*update_path = collide_path;15341535/*1536 * It's easiest to get the correct things into stage 2 and 3, and1537 * to make sure that the content merge puts HEAD before the other1538 * branch if we just ensure that branch1 == opt->branch1. So, simply1539 * flip arguments around if we don't have that.1540 */1541if(branch1 != opt->branch1) {1542returnhandle_file_collision(opt, collide_path,1543 prev_path2, prev_path1,1544 branch2, branch1,1545 b, a);1546}15471548/*1549 * In the recursive case, we just opt to undo renames1550 */1551if(opt->call_depth && (prev_path1 || prev_path2)) {1552/* Put first file (a->oid, a->mode) in its original spot */1553if(prev_path1) {1554if(update_file(opt,1, a, prev_path1))1555return-1;1556}else{1557if(update_file(opt,1, a, collide_path))1558return-1;1559}15601561/* Put second file (b->oid, b->mode) in its original spot */1562if(prev_path2) {1563if(update_file(opt,1, b, prev_path2))1564return-1;1565}else{1566if(update_file(opt,1, b, collide_path))1567return-1;1568}15691570/* Don't leave something at collision path if unrenaming both */1571if(prev_path1 && prev_path2)1572remove_file(opt,1, collide_path,0);15731574return0;1575}15761577/* Remove rename sources if rename/add or rename/rename(2to1) */1578if(prev_path1)1579remove_file(opt,1, prev_path1,1580 opt->call_depth ||would_lose_untracked(opt, prev_path1));1581if(prev_path2)1582remove_file(opt,1, prev_path2,1583 opt->call_depth ||would_lose_untracked(opt, prev_path2));15841585/*1586 * Remove the collision path, if it wouldn't cause dirty contents1587 * or an untracked file to get lost. We'll either overwrite with1588 * merged contents, or just write out to differently named files.1589 */1590if(was_dirty(opt, collide_path)) {1591output(opt,1,_("Refusing to lose dirty file at%s"),1592 collide_path);1593 update_path = alt_path =unique_path(opt, collide_path,"merged");1594}else if(would_lose_untracked(opt, collide_path)) {1595/*1596 * Only way we get here is if both renames were from1597 * a directory rename AND user had an untracked file1598 * at the location where both files end up after the1599 * two directory renames. See testcase 10d of t6043.1600 */1601output(opt,1,_("Refusing to lose untracked file at "1602"%s, even though it's in the way."),1603 collide_path);1604 update_path = alt_path =unique_path(opt, collide_path,"merged");1605}else{1606/*1607 * FIXME: It's possible that the two files are identical1608 * and that the current working copy happens to match, in1609 * which case we are unnecessarily touching the working1610 * tree file. It's not a likely enough scenario that I1611 * want to code up the checks for it and a better fix is1612 * available if we restructure how unpack_trees() and1613 * merge-recursive interoperate anyway, so punting for1614 * now...1615 */1616remove_file(opt,0, collide_path,0);1617}16181619/* Store things in diff_filespecs for functions that need it */1620 null.path = (char*)collide_path;1621oidcpy(&null.oid, &null_oid);1622 null.mode =0;16231624if(merge_mode_and_contents(opt, &null, a, b, collide_path,1625 branch1, branch2, opt->call_depth *2, &mfi))1626return-1;1627 mfi.clean &= !alt_path;1628if(update_file(opt, mfi.clean, &mfi.blob, update_path))1629return-1;1630if(!mfi.clean && !opt->call_depth &&1631update_stages(opt, collide_path, NULL, a, b))1632return-1;1633free(alt_path);1634/*1635 * FIXME: If both a & b both started with conflicts (only possible1636 * if they came from a rename/rename(2to1)), but had IDENTICAL1637 * contents including those conflicts, then in the next line we claim1638 * it was clean. If someone cares about this case, we should have the1639 * caller notify us if we started with conflicts.1640 */1641return mfi.clean;1642}16431644static inthandle_rename_add(struct merge_options *opt,1645struct rename_conflict_info *ci)1646{1647/* a was renamed to c, and a separate c was added. */1648struct diff_filespec *a = ci->ren1->pair->one;1649struct diff_filespec *c = ci->ren1->pair->two;1650char*path = c->path;1651char*prev_path_desc;1652struct merge_file_info mfi;16531654const char*rename_branch = ci->ren1->branch;1655const char*add_branch = (opt->branch1 == rename_branch ?1656 opt->branch2 : opt->branch1);1657int other_stage = (ci->ren1->branch == opt->branch1 ?3:2);16581659output(opt,1,_("CONFLICT (rename/add): "1660"Rename%s->%sin%s. Added%sin%s"),1661 a->path, c->path, rename_branch,1662 c->path, add_branch);16631664 prev_path_desc =xstrfmt("version of%sfrom%s", path, a->path);1665 ci->ren1->src_entry->stages[other_stage].path = a->path;1666if(merge_mode_and_contents(opt, a, c,1667&ci->ren1->src_entry->stages[other_stage],1668 prev_path_desc,1669 opt->branch1, opt->branch2,16701+ opt->call_depth *2, &mfi))1671return-1;1672free(prev_path_desc);16731674 ci->ren1->dst_entry->stages[other_stage].path = mfi.blob.path = c->path;1675returnhandle_file_collision(opt,1676 c->path, a->path, NULL,1677 rename_branch, add_branch,1678&mfi.blob,1679&ci->ren1->dst_entry->stages[other_stage]);1680}16811682static char*find_path_for_conflict(struct merge_options *opt,1683const char*path,1684const char*branch1,1685const char*branch2)1686{1687char*new_path = NULL;1688if(dir_in_way(opt->repo->index, path, !opt->call_depth,0)) {1689 new_path =unique_path(opt, path, branch1);1690output(opt,1,_("%sis a directory in%sadding "1691"as%sinstead"),1692 path, branch2, new_path);1693}else if(would_lose_untracked(opt, path)) {1694 new_path =unique_path(opt, path, branch1);1695output(opt,1,_("Refusing to lose untracked file"1696" at%s; adding as%sinstead"),1697 path, new_path);1698}16991700return new_path;1701}17021703static inthandle_rename_rename_1to2(struct merge_options *opt,1704struct rename_conflict_info *ci)1705{1706/* One file was renamed in both branches, but to different names. */1707struct merge_file_info mfi;1708struct diff_filespec *add;1709struct diff_filespec *o = ci->ren1->pair->one;1710struct diff_filespec *a = ci->ren1->pair->two;1711struct diff_filespec *b = ci->ren2->pair->two;1712char*path_desc;17131714output(opt,1,_("CONFLICT (rename/rename): "1715"Rename\"%s\"->\"%s\"in branch\"%s\""1716"rename\"%s\"->\"%s\"in\"%s\"%s"),1717 o->path, a->path, ci->ren1->branch,1718 o->path, b->path, ci->ren2->branch,1719 opt->call_depth ?_(" (left unresolved)") :"");17201721 path_desc =xstrfmt("%sand%s, both renamed from%s",1722 a->path, b->path, o->path);1723if(merge_mode_and_contents(opt, o, a, b, path_desc,1724 ci->ren1->branch, ci->ren2->branch,1725 opt->call_depth *2, &mfi))1726return-1;1727free(path_desc);17281729if(opt->call_depth) {1730/*1731 * FIXME: For rename/add-source conflicts (if we could detect1732 * such), this is wrong. We should instead find a unique1733 * pathname and then either rename the add-source file to that1734 * unique path, or use that unique path instead of src here.1735 */1736if(update_file(opt,0, &mfi.blob, o->path))1737return-1;17381739/*1740 * Above, we put the merged content at the merge-base's1741 * path. Now we usually need to delete both a->path and1742 * b->path. However, the rename on each side of the merge1743 * could also be involved in a rename/add conflict. In1744 * such cases, we should keep the added file around,1745 * resolving the conflict at that path in its favor.1746 */1747 add = &ci->ren1->dst_entry->stages[2^1];1748if(is_valid(add)) {1749if(update_file(opt,0, add, a->path))1750return-1;1751}1752else1753remove_file_from_index(opt->repo->index, a->path);1754 add = &ci->ren2->dst_entry->stages[3^1];1755if(is_valid(add)) {1756if(update_file(opt,0, add, b->path))1757return-1;1758}1759else1760remove_file_from_index(opt->repo->index, b->path);1761}else{1762/*1763 * For each destination path, we need to see if there is a1764 * rename/add collision. If not, we can write the file out1765 * to the specified location.1766 */1767 add = &ci->ren1->dst_entry->stages[2^1];1768if(is_valid(add)) {1769 add->path = mfi.blob.path = a->path;1770if(handle_file_collision(opt, a->path,1771 NULL, NULL,1772 ci->ren1->branch,1773 ci->ren2->branch,1774&mfi.blob, add) <0)1775return-1;1776}else{1777char*new_path =find_path_for_conflict(opt, a->path,1778 ci->ren1->branch,1779 ci->ren2->branch);1780if(update_file(opt,0, &mfi.blob,1781 new_path ? new_path : a->path))1782return-1;1783free(new_path);1784if(update_stages(opt, a->path, NULL, a, NULL))1785return-1;1786}17871788 add = &ci->ren2->dst_entry->stages[3^1];1789if(is_valid(add)) {1790 add->path = mfi.blob.path = b->path;1791if(handle_file_collision(opt, b->path,1792 NULL, NULL,1793 ci->ren1->branch,1794 ci->ren2->branch,1795 add, &mfi.blob) <0)1796return-1;1797}else{1798char*new_path =find_path_for_conflict(opt, b->path,1799 ci->ren2->branch,1800 ci->ren1->branch);1801if(update_file(opt,0, &mfi.blob,1802 new_path ? new_path : b->path))1803return-1;1804free(new_path);1805if(update_stages(opt, b->path, NULL, NULL, b))1806return-1;1807}1808}18091810return0;1811}18121813static inthandle_rename_rename_2to1(struct merge_options *opt,1814struct rename_conflict_info *ci)1815{1816/* Two files, a & b, were renamed to the same thing, c. */1817struct diff_filespec *a = ci->ren1->pair->one;1818struct diff_filespec *b = ci->ren2->pair->one;1819struct diff_filespec *c1 = ci->ren1->pair->two;1820struct diff_filespec *c2 = ci->ren2->pair->two;1821char*path = c1->path;/* == c2->path */1822char*path_side_1_desc;1823char*path_side_2_desc;1824struct merge_file_info mfi_c1;1825struct merge_file_info mfi_c2;1826int ostage1, ostage2;18271828output(opt,1,_("CONFLICT (rename/rename): "1829"Rename%s->%sin%s. "1830"Rename%s->%sin%s"),1831 a->path, c1->path, ci->ren1->branch,1832 b->path, c2->path, ci->ren2->branch);18331834 path_side_1_desc =xstrfmt("version of%sfrom%s", path, a->path);1835 path_side_2_desc =xstrfmt("version of%sfrom%s", path, b->path);1836 ostage1 = ci->ren1->branch == opt->branch1 ?3:2;1837 ostage2 = ostage1 ^1;1838 ci->ren1->src_entry->stages[ostage1].path = a->path;1839 ci->ren2->src_entry->stages[ostage2].path = b->path;1840if(merge_mode_and_contents(opt, a, c1,1841&ci->ren1->src_entry->stages[ostage1],1842 path_side_1_desc,1843 opt->branch1, opt->branch2,18441+ opt->call_depth *2, &mfi_c1) ||1845merge_mode_and_contents(opt, b,1846&ci->ren2->src_entry->stages[ostage2],1847 c2, path_side_2_desc,1848 opt->branch1, opt->branch2,18491+ opt->call_depth *2, &mfi_c2))1850return-1;1851free(path_side_1_desc);1852free(path_side_2_desc);1853 mfi_c1.blob.path = path;1854 mfi_c2.blob.path = path;18551856returnhandle_file_collision(opt, path, a->path, b->path,1857 ci->ren1->branch, ci->ren2->branch,1858&mfi_c1.blob, &mfi_c2.blob);1859}18601861/*1862 * Get the diff_filepairs changed between o_tree and tree.1863 */1864static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,1865struct tree *o_tree,1866struct tree *tree)1867{1868struct diff_queue_struct *ret;1869struct diff_options opts;18701871repo_diff_setup(opt->repo, &opts);1872 opts.flags.recursive =1;1873 opts.flags.rename_empty =0;1874 opts.detect_rename =merge_detect_rename(opt);1875/*1876 * We do not have logic to handle the detection of copies. In1877 * fact, it may not even make sense to add such logic: would we1878 * really want a change to a base file to be propagated through1879 * multiple other files by a merge?1880 */1881if(opts.detect_rename > DIFF_DETECT_RENAME)1882 opts.detect_rename = DIFF_DETECT_RENAME;1883 opts.rename_limit = opt->merge_rename_limit >=0? opt->merge_rename_limit :1884 opt->diff_rename_limit >=0? opt->diff_rename_limit :18851000;1886 opts.rename_score = opt->rename_score;1887 opts.show_rename_progress = opt->show_rename_progress;1888 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1889diff_setup_done(&opts);1890diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1891diffcore_std(&opts);1892if(opts.needed_rename_limit > opt->needed_rename_limit)1893 opt->needed_rename_limit = opts.needed_rename_limit;18941895 ret =xmalloc(sizeof(*ret));1896*ret = diff_queued_diff;18971898 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1899 diff_queued_diff.nr =0;1900 diff_queued_diff.queue = NULL;1901diff_flush(&opts);1902return ret;1903}19041905static inttree_has_path(struct repository *r,struct tree *tree,1906const char*path)1907{1908struct object_id hashy;1909unsigned short mode_o;19101911return!get_tree_entry(r,1912&tree->object.oid, path,1913&hashy, &mode_o);1914}19151916/*1917 * Return a new string that replaces the beginning portion (which matches1918 * entry->dir), with entry->new_dir. In perl-speak:1919 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1920 * NOTE:1921 * Caller must ensure that old_path starts with entry->dir + '/'.1922 */1923static char*apply_dir_rename(struct dir_rename_entry *entry,1924const char*old_path)1925{1926struct strbuf new_path = STRBUF_INIT;1927int oldlen, newlen;19281929if(entry->non_unique_new_dir)1930return NULL;19311932 oldlen =strlen(entry->dir);1933 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1934strbuf_grow(&new_path, newlen);1935strbuf_addbuf(&new_path, &entry->new_dir);1936strbuf_addstr(&new_path, &old_path[oldlen]);19371938returnstrbuf_detach(&new_path, NULL);1939}19401941static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1942char**old_dir,char**new_dir)1943{1944char*end_of_old, *end_of_new;1945int old_len, new_len;19461947*old_dir = NULL;1948*new_dir = NULL;19491950/*1951 * For1952 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1953 * the "e/foo.c" part is the same, we just want to know that1954 * "a/b/c/d" was renamed to "a/b/some/thing/else"1955 * so, for this example, this function returns "a/b/c/d" in1956 * *old_dir and "a/b/some/thing/else" in *new_dir.1957 *1958 * Also, if the basename of the file changed, we don't care. We1959 * want to know which portion of the directory, if any, changed.1960 */1961 end_of_old =strrchr(old_path,'/');1962 end_of_new =strrchr(new_path,'/');19631964if(end_of_old == NULL || end_of_new == NULL)1965return;1966while(*--end_of_new == *--end_of_old &&1967 end_of_old != old_path &&1968 end_of_new != new_path)1969;/* Do nothing; all in the while loop */1970/*1971 * We've found the first non-matching character in the directory1972 * paths. That means the current directory we were comparing1973 * represents the rename. Move end_of_old and end_of_new back1974 * to the full directory name.1975 */1976if(*end_of_old =='/')1977 end_of_old++;1978if(*end_of_old !='/')1979 end_of_new++;1980 end_of_old =strchr(end_of_old,'/');1981 end_of_new =strchr(end_of_new,'/');19821983/*1984 * It may have been the case that old_path and new_path were the same1985 * directory all along. Don't claim a rename if they're the same.1986 */1987 old_len = end_of_old - old_path;1988 new_len = end_of_new - new_path;19891990if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {1991*old_dir =xstrndup(old_path, old_len);1992*new_dir =xstrndup(new_path, new_len);1993}1994}19951996static voidremove_hashmap_entries(struct hashmap *dir_renames,1997struct string_list *items_to_remove)1998{1999int i;2000struct dir_rename_entry *entry;20012002for(i =0; i < items_to_remove->nr; i++) {2003 entry = items_to_remove->items[i].util;2004hashmap_remove(dir_renames, entry, NULL);2005}2006string_list_clear(items_to_remove,0);2007}20082009/*2010 * See if there is a directory rename for path, and if there are any file2011 * level conflicts for the renamed location. If there is a rename and2012 * there are no conflicts, return the new name. Otherwise, return NULL.2013 */2014static char*handle_path_level_conflicts(struct merge_options *opt,2015const char*path,2016struct dir_rename_entry *entry,2017struct hashmap *collisions,2018struct tree *tree)2019{2020char*new_path = NULL;2021struct collision_entry *collision_ent;2022int clean =1;2023struct strbuf collision_paths = STRBUF_INIT;20242025/*2026 * entry has the mapping of old directory name to new directory name2027 * that we want to apply to path.2028 */2029 new_path =apply_dir_rename(entry, path);20302031if(!new_path) {2032/* This should only happen when entry->non_unique_new_dir set */2033if(!entry->non_unique_new_dir)2034BUG("entry->non_unqiue_dir not set and !new_path");2035output(opt,1,_("CONFLICT (directory rename split): "2036"Unclear where to place%sbecause directory "2037"%swas renamed to multiple other directories, "2038"with no destination getting a majority of the "2039"files."),2040 path, entry->dir);2041 clean =0;2042return NULL;2043}20442045/*2046 * The caller needs to have ensured that it has pre-populated2047 * collisions with all paths that map to new_path. Do a quick check2048 * to ensure that's the case.2049 */2050 collision_ent =collision_find_entry(collisions, new_path);2051if(collision_ent == NULL)2052BUG("collision_ent is NULL");20532054/*2055 * Check for one-sided add/add/.../add conflicts, i.e.2056 * where implicit renames from the other side doing2057 * directory rename(s) can affect this side of history2058 * to put multiple paths into the same location. Warn2059 * and bail on directory renames for such paths.2060 */2061if(collision_ent->reported_already) {2062 clean =0;2063}else if(tree_has_path(opt->repo, tree, new_path)) {2064 collision_ent->reported_already =1;2065strbuf_add_separated_string_list(&collision_paths,", ",2066&collision_ent->source_files);2067output(opt,1,_("CONFLICT (implicit dir rename): Existing "2068"file/dir at%sin the way of implicit "2069"directory rename(s) putting the following "2070"path(s) there:%s."),2071 new_path, collision_paths.buf);2072 clean =0;2073}else if(collision_ent->source_files.nr >1) {2074 collision_ent->reported_already =1;2075strbuf_add_separated_string_list(&collision_paths,", ",2076&collision_ent->source_files);2077output(opt,1,_("CONFLICT (implicit dir rename): Cannot map "2078"more than one path to%s; implicit directory "2079"renames tried to put these paths there:%s"),2080 new_path, collision_paths.buf);2081 clean =0;2082}20832084/* Free memory we no longer need */2085strbuf_release(&collision_paths);2086if(!clean && new_path) {2087free(new_path);2088return NULL;2089}20902091return new_path;2092}20932094/*2095 * There are a couple things we want to do at the directory level:2096 * 1. Check for both sides renaming to the same thing, in order to avoid2097 * implicit renaming of files that should be left in place. (See2098 * testcase 6b in t6043 for details.)2099 * 2. Prune directory renames if there are still files left in the2100 * the original directory. These represent a partial directory rename,2101 * i.e. a rename where only some of the files within the directory2102 * were renamed elsewhere. (Technically, this could be done earlier2103 * in get_directory_renames(), except that would prevent us from2104 * doing the previous check and thus failing testcase 6b.)2105 * 3. Check for rename/rename(1to2) conflicts (at the directory level).2106 * In the future, we could potentially record this info as well and2107 * omit reporting rename/rename(1to2) conflicts for each path within2108 * the affected directories, thus cleaning up the merge output.2109 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the2110 * directory level, because merging directories is fine. If it2111 * causes conflicts for files within those merged directories, then2112 * that should be detected at the individual path level.2113 */2114static voidhandle_directory_level_conflicts(struct merge_options *opt,2115struct hashmap *dir_re_head,2116struct tree *head,2117struct hashmap *dir_re_merge,2118struct tree *merge)2119{2120struct hashmap_iter iter;2121struct dir_rename_entry *head_ent;2122struct dir_rename_entry *merge_ent;21232124struct string_list remove_from_head = STRING_LIST_INIT_NODUP;2125struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;21262127hashmap_iter_init(dir_re_head, &iter);2128while((head_ent =hashmap_iter_next(&iter))) {2129 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);2130if(merge_ent &&2131!head_ent->non_unique_new_dir &&2132!merge_ent->non_unique_new_dir &&2133!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {2134/* 1. Renamed identically; remove it from both sides */2135string_list_append(&remove_from_head,2136 head_ent->dir)->util = head_ent;2137strbuf_release(&head_ent->new_dir);2138string_list_append(&remove_from_merge,2139 merge_ent->dir)->util = merge_ent;2140strbuf_release(&merge_ent->new_dir);2141}else if(tree_has_path(opt->repo, head, head_ent->dir)) {2142/* 2. This wasn't a directory rename after all */2143string_list_append(&remove_from_head,2144 head_ent->dir)->util = head_ent;2145strbuf_release(&head_ent->new_dir);2146}2147}21482149remove_hashmap_entries(dir_re_head, &remove_from_head);2150remove_hashmap_entries(dir_re_merge, &remove_from_merge);21512152hashmap_iter_init(dir_re_merge, &iter);2153while((merge_ent =hashmap_iter_next(&iter))) {2154 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);2155if(tree_has_path(opt->repo, merge, merge_ent->dir)) {2156/* 2. This wasn't a directory rename after all */2157string_list_append(&remove_from_merge,2158 merge_ent->dir)->util = merge_ent;2159}else if(head_ent &&2160!head_ent->non_unique_new_dir &&2161!merge_ent->non_unique_new_dir) {2162/* 3. rename/rename(1to2) */2163/*2164 * We can assume it's not rename/rename(1to1) because2165 * that was case (1), already checked above. So we2166 * know that head_ent->new_dir and merge_ent->new_dir2167 * are different strings.2168 */2169output(opt,1,_("CONFLICT (rename/rename): "2170"Rename directory%s->%sin%s. "2171"Rename directory%s->%sin%s"),2172 head_ent->dir, head_ent->new_dir.buf, opt->branch1,2173 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);2174string_list_append(&remove_from_head,2175 head_ent->dir)->util = head_ent;2176strbuf_release(&head_ent->new_dir);2177string_list_append(&remove_from_merge,2178 merge_ent->dir)->util = merge_ent;2179strbuf_release(&merge_ent->new_dir);2180}2181}21822183remove_hashmap_entries(dir_re_head, &remove_from_head);2184remove_hashmap_entries(dir_re_merge, &remove_from_merge);2185}21862187static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)2188{2189struct hashmap *dir_renames;2190struct hashmap_iter iter;2191struct dir_rename_entry *entry;2192int i;21932194/*2195 * Typically, we think of a directory rename as all files from a2196 * certain directory being moved to a target directory. However,2197 * what if someone first moved two files from the original2198 * directory in one commit, and then renamed the directory2199 * somewhere else in a later commit? At merge time, we just know2200 * that files from the original directory went to two different2201 * places, and that the bulk of them ended up in the same place.2202 * We want each directory rename to represent where the bulk of the2203 * files from that directory end up; this function exists to find2204 * where the bulk of the files went.2205 *2206 * The first loop below simply iterates through the list of file2207 * renames, finding out how often each directory rename pair2208 * possibility occurs.2209 */2210 dir_renames =xmalloc(sizeof(*dir_renames));2211dir_rename_init(dir_renames);2212for(i =0; i < pairs->nr; ++i) {2213struct string_list_item *item;2214int*count;2215struct diff_filepair *pair = pairs->queue[i];2216char*old_dir, *new_dir;22172218/* File not part of directory rename if it wasn't renamed */2219if(pair->status !='R')2220continue;22212222get_renamed_dir_portion(pair->one->path, pair->two->path,2223&old_dir, &new_dir);2224if(!old_dir)2225/* Directory didn't change at all; ignore this one. */2226continue;22272228 entry =dir_rename_find_entry(dir_renames, old_dir);2229if(!entry) {2230 entry =xmalloc(sizeof(*entry));2231dir_rename_entry_init(entry, old_dir);2232hashmap_put(dir_renames, entry);2233}else{2234free(old_dir);2235}2236 item =string_list_lookup(&entry->possible_new_dirs, new_dir);2237if(!item) {2238 item =string_list_insert(&entry->possible_new_dirs,2239 new_dir);2240 item->util =xcalloc(1,sizeof(int));2241}else{2242free(new_dir);2243}2244 count = item->util;2245*count +=1;2246}22472248/*2249 * For each directory with files moved out of it, we find out which2250 * target directory received the most files so we can declare it to2251 * be the "winning" target location for the directory rename. This2252 * winner gets recorded in new_dir. If there is no winner2253 * (multiple target directories received the same number of files),2254 * we set non_unique_new_dir. Once we've determined the winner (or2255 * that there is no winner), we no longer need possible_new_dirs.2256 */2257hashmap_iter_init(dir_renames, &iter);2258while((entry =hashmap_iter_next(&iter))) {2259int max =0;2260int bad_max =0;2261char*best = NULL;22622263for(i =0; i < entry->possible_new_dirs.nr; i++) {2264int*count = entry->possible_new_dirs.items[i].util;22652266if(*count == max)2267 bad_max = max;2268else if(*count > max) {2269 max = *count;2270 best = entry->possible_new_dirs.items[i].string;2271}2272}2273if(bad_max == max)2274 entry->non_unique_new_dir =1;2275else{2276assert(entry->new_dir.len ==0);2277strbuf_addstr(&entry->new_dir, best);2278}2279/*2280 * The relevant directory sub-portion of the original full2281 * filepaths were xstrndup'ed before inserting into2282 * possible_new_dirs, and instead of manually iterating the2283 * list and free'ing each, just lie and tell2284 * possible_new_dirs that it did the strdup'ing so that it2285 * will free them for us.2286 */2287 entry->possible_new_dirs.strdup_strings =1;2288string_list_clear(&entry->possible_new_dirs,1);2289}22902291return dir_renames;2292}22932294static struct dir_rename_entry *check_dir_renamed(const char*path,2295struct hashmap *dir_renames)2296{2297char*temp =xstrdup(path);2298char*end;2299struct dir_rename_entry *entry = NULL;23002301while((end =strrchr(temp,'/'))) {2302*end ='\0';2303 entry =dir_rename_find_entry(dir_renames, temp);2304if(entry)2305break;2306}2307free(temp);2308return entry;2309}23102311static voidcompute_collisions(struct hashmap *collisions,2312struct hashmap *dir_renames,2313struct diff_queue_struct *pairs)2314{2315int i;23162317/*2318 * Multiple files can be mapped to the same path due to directory2319 * renames done by the other side of history. Since that other2320 * side of history could have merged multiple directories into one,2321 * if our side of history added the same file basename to each of2322 * those directories, then all N of them would get implicitly2323 * renamed by the directory rename detection into the same path,2324 * and we'd get an add/add/.../add conflict, and all those adds2325 * from *this* side of history. This is not representable in the2326 * index, and users aren't going to easily be able to make sense of2327 * it. So we need to provide a good warning about what's2328 * happening, and fall back to no-directory-rename detection2329 * behavior for those paths.2330 *2331 * See testcases 9e and all of section 5 from t6043 for examples.2332 */2333collision_init(collisions);23342335for(i =0; i < pairs->nr; ++i) {2336struct dir_rename_entry *dir_rename_ent;2337struct collision_entry *collision_ent;2338char*new_path;2339struct diff_filepair *pair = pairs->queue[i];23402341if(pair->status !='A'&& pair->status !='R')2342continue;2343 dir_rename_ent =check_dir_renamed(pair->two->path,2344 dir_renames);2345if(!dir_rename_ent)2346continue;23472348 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);2349if(!new_path)2350/*2351 * dir_rename_ent->non_unique_new_path is true, which2352 * means there is no directory rename for us to use,2353 * which means it won't cause us any additional2354 * collisions.2355 */2356continue;2357 collision_ent =collision_find_entry(collisions, new_path);2358if(!collision_ent) {2359 collision_ent =xcalloc(1,2360sizeof(struct collision_entry));2361hashmap_entry_init(collision_ent,strhash(new_path));2362hashmap_put(collisions, collision_ent);2363 collision_ent->target_file = new_path;2364}else{2365free(new_path);2366}2367string_list_insert(&collision_ent->source_files,2368 pair->two->path);2369}2370}23712372static char*check_for_directory_rename(struct merge_options *opt,2373const char*path,2374struct tree *tree,2375struct hashmap *dir_renames,2376struct hashmap *dir_rename_exclusions,2377struct hashmap *collisions,2378int*clean_merge)2379{2380char*new_path = NULL;2381struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2382struct dir_rename_entry *oentry = NULL;23832384if(!entry)2385return new_path;23862387/*2388 * This next part is a little weird. We do not want to do an2389 * implicit rename into a directory we renamed on our side, because2390 * that will result in a spurious rename/rename(1to2) conflict. An2391 * example:2392 * Base commit: dumbdir/afile, otherdir/bfile2393 * Side 1: smrtdir/afile, otherdir/bfile2394 * Side 2: dumbdir/afile, dumbdir/bfile2395 * Here, while working on Side 1, we could notice that otherdir was2396 * renamed/merged to dumbdir, and change the diff_filepair for2397 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2398 * 2 will notice the rename from dumbdir to smrtdir, and do the2399 * transitive rename to move it from dumbdir/bfile to2400 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2401 * smrtdir, a rename/rename(1to2) conflict. We really just want2402 * the file to end up in smrtdir. And the way to achieve that is2403 * to not let Side1 do the rename to dumbdir, since we know that is2404 * the source of one of our directory renames.2405 *2406 * That's why oentry and dir_rename_exclusions is here.2407 *2408 * As it turns out, this also prevents N-way transient rename2409 * confusion; See testcases 9c and 9d of t6043.2410 */2411 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2412if(oentry) {2413output(opt,1,_("WARNING: Avoiding applying%s->%srename "2414"to%s, because%sitself was renamed."),2415 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2416}else{2417 new_path =handle_path_level_conflicts(opt, path, entry,2418 collisions, tree);2419*clean_merge &= (new_path != NULL);2420}24212422return new_path;2423}24242425static voidapply_directory_rename_modifications(struct merge_options *opt,2426struct diff_filepair *pair,2427char*new_path,2428struct rename *re,2429struct tree *tree,2430struct tree *o_tree,2431struct tree *a_tree,2432struct tree *b_tree,2433struct string_list *entries)2434{2435struct string_list_item *item;2436int stage = (tree == a_tree ?2:3);2437int update_wd;24382439/*2440 * In all cases where we can do directory rename detection,2441 * unpack_trees() will have read pair->two->path into the2442 * index and the working copy. We need to remove it so that2443 * we can instead place it at new_path. It is guaranteed to2444 * not be untracked (unpack_trees() would have errored out2445 * saying the file would have been overwritten), but it might2446 * be dirty, though.2447 */2448 update_wd = !was_dirty(opt, pair->two->path);2449if(!update_wd)2450output(opt,1,_("Refusing to lose dirty file at%s"),2451 pair->two->path);2452remove_file(opt,1, pair->two->path, !update_wd);24532454/* Find or create a new re->dst_entry */2455 item =string_list_lookup(entries, new_path);2456if(item) {2457/*2458 * Since we're renaming on this side of history, and it's2459 * due to a directory rename on the other side of history2460 * (which we only allow when the directory in question no2461 * longer exists on the other side of history), the2462 * original entry for re->dst_entry is no longer2463 * necessary...2464 */2465 re->dst_entry->processed =1;24662467/*2468 * ...because we'll be using this new one.2469 */2470 re->dst_entry = item->util;2471}else{2472/*2473 * re->dst_entry is for the before-dir-rename path, and we2474 * need it to hold information for the after-dir-rename2475 * path. Before creating a new entry, we need to mark the2476 * old one as unnecessary (...unless it is shared by2477 * src_entry, i.e. this didn't use to be a rename, in which2478 * case we can just allow the normal processing to happen2479 * for it).2480 */2481if(pair->status =='R')2482 re->dst_entry->processed =1;24832484 re->dst_entry =insert_stage_data(opt->repo, new_path,2485 o_tree, a_tree, b_tree,2486 entries);2487 item =string_list_insert(entries, new_path);2488 item->util = re->dst_entry;2489}24902491/*2492 * Update the stage_data with the information about the path we are2493 * moving into place. That slot will be empty and available for us2494 * to write to because of the collision checks in2495 * handle_path_level_conflicts(). In other words,2496 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2497 * open for us to write to.2498 *2499 * It may be tempting to actually update the index at this point as2500 * well, using update_stages_for_stage_data(), but as per the big2501 * "NOTE" in update_stages(), doing so will modify the current2502 * in-memory index which will break calls to would_lose_untracked()2503 * that we need to make. Instead, we need to just make sure that2504 * the various handle_rename_*() functions update the index2505 * explicitly rather than relying on unpack_trees() to have done it.2506 */2507get_tree_entry(opt->repo,2508&tree->object.oid,2509 pair->two->path,2510&re->dst_entry->stages[stage].oid,2511&re->dst_entry->stages[stage].mode);25122513/*2514 * Record the original change status (or 'type' of change). If it2515 * was originally an add ('A'), this lets us differentiate later2516 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they2517 * otherwise look the same). If it was originally a rename ('R'),2518 * this lets us remember and report accurately about the transitive2519 * renaming that occurred via the directory rename detection. Also,2520 * record the original destination name.2521 */2522 re->dir_rename_original_type = pair->status;2523 re->dir_rename_original_dest = pair->two->path;25242525/*2526 * We don't actually look at pair->status again, but it seems2527 * pedagogically correct to adjust it.2528 */2529 pair->status ='R';25302531/*2532 * Finally, record the new location.2533 */2534 pair->two->path = new_path;2535}25362537/*2538 * Get information of all renames which occurred in 'pairs', making use of2539 * any implicit directory renames inferred from the other side of history.2540 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2541 * to be able to associate the correct cache entries with the rename2542 * information; tree is always equal to either a_tree or b_tree.2543 */2544static struct string_list *get_renames(struct merge_options *opt,2545const char*branch,2546struct diff_queue_struct *pairs,2547struct hashmap *dir_renames,2548struct hashmap *dir_rename_exclusions,2549struct tree *tree,2550struct tree *o_tree,2551struct tree *a_tree,2552struct tree *b_tree,2553struct string_list *entries,2554int*clean_merge)2555{2556int i;2557struct hashmap collisions;2558struct hashmap_iter iter;2559struct collision_entry *e;2560struct string_list *renames;25612562compute_collisions(&collisions, dir_renames, pairs);2563 renames =xcalloc(1,sizeof(struct string_list));25642565for(i =0; i < pairs->nr; ++i) {2566struct string_list_item *item;2567struct rename *re;2568struct diff_filepair *pair = pairs->queue[i];2569char*new_path;/* non-NULL only with directory renames */25702571if(pair->status !='A'&& pair->status !='R') {2572diff_free_filepair(pair);2573continue;2574}2575 new_path =check_for_directory_rename(opt, pair->two->path, tree,2576 dir_renames,2577 dir_rename_exclusions,2578&collisions,2579 clean_merge);2580if(pair->status !='R'&& !new_path) {2581diff_free_filepair(pair);2582continue;2583}25842585 re =xmalloc(sizeof(*re));2586 re->processed =0;2587 re->pair = pair;2588 re->branch = branch;2589 re->dir_rename_original_type ='\0';2590 re->dir_rename_original_dest = NULL;2591 item =string_list_lookup(entries, re->pair->one->path);2592if(!item)2593 re->src_entry =insert_stage_data(opt->repo,2594 re->pair->one->path,2595 o_tree, a_tree, b_tree, entries);2596else2597 re->src_entry = item->util;25982599 item =string_list_lookup(entries, re->pair->two->path);2600if(!item)2601 re->dst_entry =insert_stage_data(opt->repo,2602 re->pair->two->path,2603 o_tree, a_tree, b_tree, entries);2604else2605 re->dst_entry = item->util;2606 item =string_list_insert(renames, pair->one->path);2607 item->util = re;2608if(new_path)2609apply_directory_rename_modifications(opt, pair, new_path,2610 re, tree, o_tree,2611 a_tree, b_tree,2612 entries);2613}26142615hashmap_iter_init(&collisions, &iter);2616while((e =hashmap_iter_next(&iter))) {2617free(e->target_file);2618string_list_clear(&e->source_files,0);2619}2620hashmap_free(&collisions,1);2621return renames;2622}26232624static intprocess_renames(struct merge_options *opt,2625struct string_list *a_renames,2626struct string_list *b_renames)2627{2628int clean_merge =1, i, j;2629struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2630struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2631const struct rename *sre;26322633for(i =0; i < a_renames->nr; i++) {2634 sre = a_renames->items[i].util;2635string_list_insert(&a_by_dst, sre->pair->two->path)->util2636= (void*)sre;2637}2638for(i =0; i < b_renames->nr; i++) {2639 sre = b_renames->items[i].util;2640string_list_insert(&b_by_dst, sre->pair->two->path)->util2641= (void*)sre;2642}26432644for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2645struct string_list *renames1, *renames2Dst;2646struct rename *ren1 = NULL, *ren2 = NULL;2647const char*ren1_src, *ren1_dst;2648struct string_list_item *lookup;26492650if(i >= a_renames->nr) {2651 ren2 = b_renames->items[j++].util;2652}else if(j >= b_renames->nr) {2653 ren1 = a_renames->items[i++].util;2654}else{2655int compare =strcmp(a_renames->items[i].string,2656 b_renames->items[j].string);2657if(compare <=0)2658 ren1 = a_renames->items[i++].util;2659if(compare >=0)2660 ren2 = b_renames->items[j++].util;2661}26622663/* TODO: refactor, so that 1/2 are not needed */2664if(ren1) {2665 renames1 = a_renames;2666 renames2Dst = &b_by_dst;2667}else{2668 renames1 = b_renames;2669 renames2Dst = &a_by_dst;2670SWAP(ren2, ren1);2671}26722673if(ren1->processed)2674continue;2675 ren1->processed =1;2676 ren1->dst_entry->processed =1;2677/* BUG: We should only mark src_entry as processed if we2678 * are not dealing with a rename + add-source case.2679 */2680 ren1->src_entry->processed =1;26812682 ren1_src = ren1->pair->one->path;2683 ren1_dst = ren1->pair->two->path;26842685if(ren2) {2686/* One file renamed on both sides */2687const char*ren2_src = ren2->pair->one->path;2688const char*ren2_dst = ren2->pair->two->path;2689enum rename_type rename_type;2690if(strcmp(ren1_src, ren2_src) !=0)2691BUG("ren1_src != ren2_src");2692 ren2->dst_entry->processed =1;2693 ren2->processed =1;2694if(strcmp(ren1_dst, ren2_dst) !=0) {2695 rename_type = RENAME_ONE_FILE_TO_TWO;2696 clean_merge =0;2697}else{2698 rename_type = RENAME_ONE_FILE_TO_ONE;2699/* BUG: We should only remove ren1_src in2700 * the base stage (think of rename +2701 * add-source cases).2702 */2703remove_file(opt,1, ren1_src,1);2704update_entry(ren1->dst_entry,2705 ren1->pair->one,2706 ren1->pair->two,2707 ren2->pair->two);2708}2709setup_rename_conflict_info(rename_type, opt, ren1, ren2);2710}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2711/* Two different files renamed to the same thing */2712char*ren2_dst;2713 ren2 = lookup->util;2714 ren2_dst = ren2->pair->two->path;2715if(strcmp(ren1_dst, ren2_dst) !=0)2716BUG("ren1_dst != ren2_dst");27172718 clean_merge =0;2719 ren2->processed =1;2720/*2721 * BUG: We should only mark src_entry as processed2722 * if we are not dealing with a rename + add-source2723 * case.2724 */2725 ren2->src_entry->processed =1;27262727setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2728 opt, ren1, ren2);2729}else{2730/* Renamed in 1, maybe changed in 2 */2731/* we only use sha1 and mode of these */2732struct diff_filespec src_other, dst_other;2733int try_merge;27342735/*2736 * unpack_trees loads entries from common-commit2737 * into stage 1, from head-commit into stage 2, and2738 * from merge-commit into stage 3. We keep track2739 * of which side corresponds to the rename.2740 */2741int renamed_stage = a_renames == renames1 ?2:3;2742int other_stage = a_renames == renames1 ?3:2;27432744/* BUG: We should only remove ren1_src in the base2745 * stage and in other_stage (think of rename +2746 * add-source case).2747 */2748remove_file(opt,1, ren1_src,2749 renamed_stage ==2|| !was_tracked(opt, ren1_src));27502751oidcpy(&src_other.oid,2752&ren1->src_entry->stages[other_stage].oid);2753 src_other.mode = ren1->src_entry->stages[other_stage].mode;2754oidcpy(&dst_other.oid,2755&ren1->dst_entry->stages[other_stage].oid);2756 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2757 try_merge =0;27582759if(oid_eq(&src_other.oid, &null_oid) &&2760 ren1->dir_rename_original_type =='A') {2761setup_rename_conflict_info(RENAME_VIA_DIR,2762 opt, ren1, NULL);2763}else if(oid_eq(&src_other.oid, &null_oid)) {2764setup_rename_conflict_info(RENAME_DELETE,2765 opt, ren1, NULL);2766}else if((dst_other.mode == ren1->pair->two->mode) &&2767oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2768/*2769 * Added file on the other side identical to2770 * the file being renamed: clean merge.2771 * Also, there is no need to overwrite the2772 * file already in the working copy, so call2773 * update_file_flags() instead of2774 * update_file().2775 */2776if(update_file_flags(opt,2777 ren1->pair->two,2778 ren1_dst,27791,/* update_cache */27800/* update_wd */))2781 clean_merge = -1;2782}else if(!oid_eq(&dst_other.oid, &null_oid)) {2783/*2784 * Probably not a clean merge, but it's2785 * premature to set clean_merge to 0 here,2786 * because if the rename merges cleanly and2787 * the merge exactly matches the newly added2788 * file, then the merge will be clean.2789 */2790setup_rename_conflict_info(RENAME_ADD,2791 opt, ren1, NULL);2792}else2793 try_merge =1;27942795if(clean_merge <0)2796goto cleanup_and_return;2797if(try_merge) {2798struct diff_filespec *o, *a, *b;2799 src_other.path = (char*)ren1_src;28002801 o = ren1->pair->one;2802if(a_renames == renames1) {2803 a = ren1->pair->two;2804 b = &src_other;2805}else{2806 b = ren1->pair->two;2807 a = &src_other;2808}2809update_entry(ren1->dst_entry, o, a, b);2810setup_rename_conflict_info(RENAME_NORMAL,2811 opt, ren1, NULL);2812}2813}2814}2815cleanup_and_return:2816string_list_clear(&a_by_dst,0);2817string_list_clear(&b_by_dst,0);28182819return clean_merge;2820}28212822struct rename_info {2823struct string_list *head_renames;2824struct string_list *merge_renames;2825};28262827static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2828struct hashmap *dir_renames)2829{2830struct hashmap_iter iter;2831struct dir_rename_entry *e;28322833hashmap_iter_init(dir_renames, &iter);2834while((e =hashmap_iter_next(&iter))) {2835free(e->dir);2836strbuf_release(&e->new_dir);2837/* possible_new_dirs already cleared in get_directory_renames */2838}2839hashmap_free(dir_renames,1);2840free(dir_renames);28412842free(pairs->queue);2843free(pairs);2844}28452846static intdetect_and_process_renames(struct merge_options *opt,2847struct tree *common,2848struct tree *head,2849struct tree *merge,2850struct string_list *entries,2851struct rename_info *ri)2852{2853struct diff_queue_struct *head_pairs, *merge_pairs;2854struct hashmap *dir_re_head, *dir_re_merge;2855int clean =1;28562857 ri->head_renames = NULL;2858 ri->merge_renames = NULL;28592860if(!merge_detect_rename(opt))2861return1;28622863 head_pairs =get_diffpairs(opt, common, head);2864 merge_pairs =get_diffpairs(opt, common, merge);28652866if(opt->detect_directory_renames) {2867 dir_re_head =get_directory_renames(head_pairs);2868 dir_re_merge =get_directory_renames(merge_pairs);28692870handle_directory_level_conflicts(opt,2871 dir_re_head, head,2872 dir_re_merge, merge);2873}else{2874 dir_re_head =xmalloc(sizeof(*dir_re_head));2875 dir_re_merge =xmalloc(sizeof(*dir_re_merge));2876dir_rename_init(dir_re_head);2877dir_rename_init(dir_re_merge);2878}28792880 ri->head_renames =get_renames(opt, opt->branch1, head_pairs,2881 dir_re_merge, dir_re_head, head,2882 common, head, merge, entries,2883&clean);2884if(clean <0)2885goto cleanup;2886 ri->merge_renames =get_renames(opt, opt->branch2, merge_pairs,2887 dir_re_head, dir_re_merge, merge,2888 common, head, merge, entries,2889&clean);2890if(clean <0)2891goto cleanup;2892 clean &=process_renames(opt, ri->head_renames, ri->merge_renames);28932894cleanup:2895/*2896 * Some cleanup is deferred until cleanup_renames() because the2897 * data structures are still needed and referenced in2898 * process_entry(). But there are a few things we can free now.2899 */2900initial_cleanup_rename(head_pairs, dir_re_head);2901initial_cleanup_rename(merge_pairs, dir_re_merge);29022903return clean;2904}29052906static voidfinal_cleanup_rename(struct string_list *rename)2907{2908const struct rename *re;2909int i;29102911if(rename == NULL)2912return;29132914for(i =0; i < rename->nr; i++) {2915 re = rename->items[i].util;2916diff_free_filepair(re->pair);2917}2918string_list_clear(rename,1);2919free(rename);2920}29212922static voidfinal_cleanup_renames(struct rename_info *re_info)2923{2924final_cleanup_rename(re_info->head_renames);2925final_cleanup_rename(re_info->merge_renames);2926}29272928static intread_oid_strbuf(struct merge_options *opt,2929const struct object_id *oid,2930struct strbuf *dst)2931{2932void*buf;2933enum object_type type;2934unsigned long size;2935 buf =read_object_file(oid, &type, &size);2936if(!buf)2937returnerr(opt,_("cannot read object%s"),oid_to_hex(oid));2938if(type != OBJ_BLOB) {2939free(buf);2940returnerr(opt,_("object%sis not a blob"),oid_to_hex(oid));2941}2942strbuf_attach(dst, buf, size, size +1);2943return0;2944}29452946static intblob_unchanged(struct merge_options *opt,2947const struct diff_filespec *o,2948const struct diff_filespec *a,2949int renormalize,const char*path)2950{2951struct strbuf obuf = STRBUF_INIT;2952struct strbuf abuf = STRBUF_INIT;2953int ret =0;/* assume changed for safety */2954const struct index_state *idx = opt->repo->index;29552956if(a->mode != o->mode)2957return0;2958if(oid_eq(&o->oid, &a->oid))2959return1;2960if(!renormalize)2961return0;29622963if(read_oid_strbuf(opt, &o->oid, &obuf) ||2964read_oid_strbuf(opt, &a->oid, &abuf))2965goto error_return;2966/*2967 * Note: binary | is used so that both renormalizations are2968 * performed. Comparison can be skipped if both files are2969 * unchanged since their sha1s have already been compared.2970 */2971if(renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |2972renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))2973 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));29742975error_return:2976strbuf_release(&obuf);2977strbuf_release(&abuf);2978return ret;2979}29802981static inthandle_modify_delete(struct merge_options *opt,2982const char*path,2983const struct diff_filespec *o,2984const struct diff_filespec *a,2985const struct diff_filespec *b)2986{2987const char*modify_branch, *delete_branch;2988const struct diff_filespec *changed;29892990if(is_valid(a)) {2991 modify_branch = opt->branch1;2992 delete_branch = opt->branch2;2993 changed = a;2994}else{2995 modify_branch = opt->branch2;2996 delete_branch = opt->branch1;2997 changed = b;2998}29993000returnhandle_change_delete(opt,3001 path, NULL,3002 o, changed,3003 modify_branch, delete_branch,3004_("modify"),_("modified"));3005}30063007static inthandle_content_merge(struct merge_file_info *mfi,3008struct merge_options *opt,3009const char*path,3010int is_dirty,3011const struct diff_filespec *o,3012const struct diff_filespec *a,3013const struct diff_filespec *b,3014struct rename_conflict_info *ci)3015{3016const char*reason =_("content");3017unsigned df_conflict_remains =0;30183019if(!is_valid(o))3020 reason =_("add/add");30213022assert(o->path && a->path && b->path);3023if(ci &&dir_in_way(opt->repo->index, path, !opt->call_depth,3024S_ISGITLINK(ci->ren1->pair->two->mode)))3025 df_conflict_remains =1;30263027if(merge_mode_and_contents(opt, o, a, b, path,3028 opt->branch1, opt->branch2,3029 opt->call_depth *2, mfi))3030return-1;30313032/*3033 * We can skip updating the working tree file iff:3034 * a) The merge is clean3035 * b) The merge matches what was in HEAD (content, mode, pathname)3036 * c) The target path is usable (i.e. not involved in D/F conflict)3037 */3038if(mfi->clean &&was_tracked_and_matches(opt, path, &mfi->blob) &&3039!df_conflict_remains) {3040int pos;3041struct cache_entry *ce;30423043output(opt,3,_("Skipped%s(merged same as existing)"), path);3044if(add_cacheinfo(opt, &mfi->blob, path,30450, (!opt->call_depth && !is_dirty),0))3046return-1;3047/*3048 * However, add_cacheinfo() will delete the old cache entry3049 * and add a new one. We need to copy over any skip_worktree3050 * flag to avoid making the file appear as if it were3051 * deleted by the user.3052 */3053 pos =index_name_pos(&opt->orig_index, path,strlen(path));3054 ce = opt->orig_index.cache[pos];3055if(ce_skip_worktree(ce)) {3056 pos =index_name_pos(opt->repo->index, path,strlen(path));3057 ce = opt->repo->index->cache[pos];3058 ce->ce_flags |= CE_SKIP_WORKTREE;3059}3060return mfi->clean;3061}30623063if(!mfi->clean) {3064if(S_ISGITLINK(mfi->blob.mode))3065 reason =_("submodule");3066output(opt,1,_("CONFLICT (%s): Merge conflict in%s"),3067 reason, path);3068if(ci && !df_conflict_remains)3069if(update_stages(opt, path, o, a, b))3070return-1;3071}30723073if(df_conflict_remains || is_dirty) {3074char*new_path;3075if(opt->call_depth) {3076remove_file_from_index(opt->repo->index, path);3077}else{3078if(!mfi->clean) {3079if(update_stages(opt, path, o, a, b))3080return-1;3081}else{3082int file_from_stage2 =was_tracked(opt, path);30833084if(update_stages(opt, path, NULL,3085 file_from_stage2 ? &mfi->blob : NULL,3086 file_from_stage2 ? NULL : &mfi->blob))3087return-1;3088}30893090}3091 new_path =unique_path(opt, path, ci->ren1->branch);3092if(is_dirty) {3093output(opt,1,_("Refusing to lose dirty file at%s"),3094 path);3095}3096output(opt,1,_("Adding as%sinstead"), new_path);3097if(update_file(opt,0, &mfi->blob, new_path)) {3098free(new_path);3099return-1;3100}3101free(new_path);3102 mfi->clean =0;3103}else if(update_file(opt, mfi->clean, &mfi->blob, path))3104return-1;3105return!is_dirty && mfi->clean;3106}31073108static inthandle_rename_normal(struct merge_options *opt,3109const char*path,3110const struct diff_filespec *o,3111const struct diff_filespec *a,3112const struct diff_filespec *b,3113struct rename_conflict_info *ci)3114{3115struct rename *ren = ci->ren1;3116struct merge_file_info mfi;3117int clean;3118int side = (ren->branch == opt->branch1 ?2:3);31193120/* Merge the content and write it out */3121 clean =handle_content_merge(&mfi, opt, path,was_dirty(opt, path),3122 o, a, b, ci);31233124if(clean && opt->detect_directory_renames ==1&&3125 ren->dir_rename_original_dest) {3126if(update_stages(opt, path,3127 NULL,3128 side ==2? &mfi.blob : NULL,3129 side ==2? NULL : &mfi.blob))3130return-1;3131 clean =0;/* not clean, but conflicted */3132}3133return clean;3134}31353136static voiddir_rename_warning(const char*msg,3137int is_add,3138int clean,3139struct merge_options *opt,3140struct rename *ren)3141{3142const char*other_branch;3143 other_branch = (ren->branch == opt->branch1 ?3144 opt->branch2 : opt->branch1);3145if(is_add) {3146output(opt, clean ?2:1, msg,3147 ren->pair->one->path, ren->branch,3148 other_branch, ren->pair->two->path);3149return;3150}3151output(opt, clean ?2:1, msg,3152 ren->pair->one->path, ren->dir_rename_original_dest, ren->branch,3153 other_branch, ren->pair->two->path);3154}3155static intwarn_about_dir_renamed_entries(struct merge_options *opt,3156struct rename *ren)3157{3158const char*msg;3159int clean =1, is_add;31603161if(!ren)3162return clean;31633164/* Return early if ren was not affected/created by a directory rename */3165if(!ren->dir_rename_original_dest)3166return clean;31673168/* Sanity checks */3169assert(opt->detect_directory_renames >0);3170assert(ren->dir_rename_original_type =='A'||3171 ren->dir_rename_original_type =='R');31723173/* Check whether to treat directory renames as a conflict */3174 clean = (opt->detect_directory_renames ==2);31753176 is_add = (ren->dir_rename_original_type =='A');3177if(ren->dir_rename_original_type =='A'&& clean) {3178 msg =_("Path updated:%sadded in%sinside a "3179"directory that was renamed in%s; moving it to%s.");3180}else if(ren->dir_rename_original_type =='A'&& !clean) {3181 msg =_("CONFLICT (file location):%sadded in%s"3182"inside a directory that was renamed in%s, "3183"suggesting it should perhaps be moved to%s.");3184}else if(ren->dir_rename_original_type =='R'&& clean) {3185 msg =_("Path updated:%srenamed to%sin%s, inside a "3186"directory that was renamed in%s; moving it to%s.");3187}else if(ren->dir_rename_original_type =='R'&& !clean) {3188 msg =_("CONFLICT (file location):%srenamed to%sin%s, "3189"inside a directory that was renamed in%s, "3190"suggesting it should perhaps be moved to%s.");3191}else{3192BUG("Impossible dir_rename_original_type/clean combination");3193}3194dir_rename_warning(msg, is_add, clean, opt, ren);31953196return clean;3197}31983199/* Per entry merge function */3200static intprocess_entry(struct merge_options *opt,3201const char*path,struct stage_data *entry)3202{3203int clean_merge =1;3204int normalize = opt->renormalize;32053206struct diff_filespec *o = &entry->stages[1];3207struct diff_filespec *a = &entry->stages[2];3208struct diff_filespec *b = &entry->stages[3];3209int o_valid =is_valid(o);3210int a_valid =is_valid(a);3211int b_valid =is_valid(b);3212 o->path = a->path = b->path = (char*)path;32133214 entry->processed =1;3215if(entry->rename_conflict_info) {3216struct rename_conflict_info *ci = entry->rename_conflict_info;3217struct diff_filespec *temp;3218int path_clean;32193220 path_clean =warn_about_dir_renamed_entries(opt, ci->ren1);3221 path_clean &=warn_about_dir_renamed_entries(opt, ci->ren2);32223223/*3224 * For cases with a single rename, {o,a,b}->path have all been3225 * set to the rename target path; we need to set two of these3226 * back to the rename source.3227 * For rename/rename conflicts, we'll manually fix paths below.3228 */3229 temp = (opt->branch1 == ci->ren1->branch) ? b : a;3230 o->path = temp->path = ci->ren1->pair->one->path;3231if(ci->ren2) {3232assert(opt->branch1 == ci->ren1->branch);3233}32343235switch(ci->rename_type) {3236case RENAME_NORMAL:3237case RENAME_ONE_FILE_TO_ONE:3238 clean_merge =handle_rename_normal(opt, path, o, a, b,3239 ci);3240break;3241case RENAME_VIA_DIR:3242 clean_merge =handle_rename_via_dir(opt, ci);3243break;3244case RENAME_ADD:3245/*3246 * Probably unclean merge, but if the renamed file3247 * merges cleanly and the result can then be3248 * two-way merged cleanly with the added file, I3249 * guess it's a clean merge?3250 */3251 clean_merge =handle_rename_add(opt, ci);3252break;3253case RENAME_DELETE:3254 clean_merge =0;3255if(handle_rename_delete(opt, ci))3256 clean_merge = -1;3257break;3258case RENAME_ONE_FILE_TO_TWO:3259/*3260 * Manually fix up paths; note:3261 * ren[12]->pair->one->path are equal.3262 */3263 o->path = ci->ren1->pair->one->path;3264 a->path = ci->ren1->pair->two->path;3265 b->path = ci->ren2->pair->two->path;32663267 clean_merge =0;3268if(handle_rename_rename_1to2(opt, ci))3269 clean_merge = -1;3270break;3271case RENAME_TWO_FILES_TO_ONE:3272/*3273 * Manually fix up paths; note,3274 * ren[12]->pair->two->path are actually equal.3275 */3276 o->path = NULL;3277 a->path = ci->ren1->pair->two->path;3278 b->path = ci->ren2->pair->two->path;32793280/*3281 * Probably unclean merge, but if the two renamed3282 * files merge cleanly and the two resulting files3283 * can then be two-way merged cleanly, I guess it's3284 * a clean merge?3285 */3286 clean_merge =handle_rename_rename_2to1(opt, ci);3287break;3288default:3289 entry->processed =0;3290break;3291}3292if(path_clean < clean_merge)3293 clean_merge = path_clean;3294}else if(o_valid && (!a_valid || !b_valid)) {3295/* Case A: Deleted in one */3296if((!a_valid && !b_valid) ||3297(!b_valid &&blob_unchanged(opt, o, a, normalize, path)) ||3298(!a_valid &&blob_unchanged(opt, o, b, normalize, path))) {3299/* Deleted in both or deleted in one and3300 * unchanged in the other */3301if(a_valid)3302output(opt,2,_("Removing%s"), path);3303/* do not touch working file if it did not exist */3304remove_file(opt,1, path, !a_valid);3305}else{3306/* Modify/delete; deleted side may have put a directory in the way */3307 clean_merge =0;3308if(handle_modify_delete(opt, path, o, a, b))3309 clean_merge = -1;3310}3311}else if((!o_valid && a_valid && !b_valid) ||3312(!o_valid && !a_valid && b_valid)) {3313/* Case B: Added in one. */3314/* [nothing|directory] -> ([nothing|directory], file) */33153316const char*add_branch;3317const char*other_branch;3318const char*conf;3319const struct diff_filespec *contents;33203321if(a_valid) {3322 add_branch = opt->branch1;3323 other_branch = opt->branch2;3324 contents = a;3325 conf =_("file/directory");3326}else{3327 add_branch = opt->branch2;3328 other_branch = opt->branch1;3329 contents = b;3330 conf =_("directory/file");3331}3332if(dir_in_way(opt->repo->index, path,3333!opt->call_depth && !S_ISGITLINK(a->mode),33340)) {3335char*new_path =unique_path(opt, path, add_branch);3336 clean_merge =0;3337output(opt,1,_("CONFLICT (%s): There is a directory with name%sin%s. "3338"Adding%sas%s"),3339 conf, path, other_branch, path, new_path);3340if(update_file(opt,0, contents, new_path))3341 clean_merge = -1;3342else if(opt->call_depth)3343remove_file_from_index(opt->repo->index, path);3344free(new_path);3345}else{3346output(opt,2,_("Adding%s"), path);3347/* do not overwrite file if already present */3348if(update_file_flags(opt, contents, path,1, !a_valid))3349 clean_merge = -1;3350}3351}else if(a_valid && b_valid) {3352if(!o_valid) {3353/* Case C: Added in both (check for same permissions) */3354output(opt,1,3355_("CONFLICT (add/add): Merge conflict in%s"),3356 path);3357 clean_merge =handle_file_collision(opt,3358 path, NULL, NULL,3359 opt->branch1,3360 opt->branch2,3361 a, b);3362}else{3363/* case D: Modified in both, but differently. */3364struct merge_file_info mfi;3365int is_dirty =0;/* unpack_trees would have bailed if dirty */3366 clean_merge =handle_content_merge(&mfi, opt, path,3367 is_dirty,3368 o, a, b, NULL);3369}3370}else if(!o_valid && !a_valid && !b_valid) {3371/*3372 * this entry was deleted altogether. a_mode == 0 means3373 * we had that path and want to actively remove it.3374 */3375remove_file(opt,1, path, !a->mode);3376}else3377BUG("fatal merge failure, shouldn't happen.");33783379return clean_merge;3380}33813382intmerge_trees(struct merge_options *opt,3383struct tree *head,3384struct tree *merge,3385struct tree *common,3386struct tree **result)3387{3388struct index_state *istate = opt->repo->index;3389int code, clean;3390struct strbuf sb = STRBUF_INIT;33913392if(!opt->call_depth &&repo_index_has_changes(opt->repo, head, &sb)) {3393err(opt,_("Your local changes to the following files would be overwritten by merge:\n%s"),3394 sb.buf);3395return-1;3396}33973398if(opt->subtree_shift) {3399 merge =shift_tree_object(opt->repo, head, merge, opt->subtree_shift);3400 common =shift_tree_object(opt->repo, head, common, opt->subtree_shift);3401}34023403if(oid_eq(&common->object.oid, &merge->object.oid)) {3404output(opt,0,_("Already up to date!"));3405*result = head;3406return1;3407}34083409 code =unpack_trees_start(opt, common, head, merge);34103411if(code !=0) {3412if(show(opt,4) || opt->call_depth)3413err(opt,_("merging of trees%sand%sfailed"),3414oid_to_hex(&head->object.oid),3415oid_to_hex(&merge->object.oid));3416unpack_trees_finish(opt);3417return-1;3418}34193420if(unmerged_index(istate)) {3421struct string_list *entries;3422struct rename_info re_info;3423int i;3424/*3425 * Only need the hashmap while processing entries, so3426 * initialize it here and free it when we are done running3427 * through the entries. Keeping it in the merge_options as3428 * opposed to decaring a local hashmap is for convenience3429 * so that we don't have to pass it to around.3430 */3431hashmap_init(&opt->current_file_dir_set, path_hashmap_cmp, NULL,512);3432get_files_dirs(opt, head);3433get_files_dirs(opt, merge);34343435 entries =get_unmerged(opt->repo->index);3436 clean =detect_and_process_renames(opt, common, head, merge,3437 entries, &re_info);3438record_df_conflict_files(opt, entries);3439if(clean <0)3440goto cleanup;3441for(i = entries->nr-1;0<= i; i--) {3442const char*path = entries->items[i].string;3443struct stage_data *e = entries->items[i].util;3444if(!e->processed) {3445int ret =process_entry(opt, path, e);3446if(!ret)3447 clean =0;3448else if(ret <0) {3449 clean = ret;3450goto cleanup;3451}3452}3453}3454for(i =0; i < entries->nr; i++) {3455struct stage_data *e = entries->items[i].util;3456if(!e->processed)3457BUG("unprocessed path???%s",3458 entries->items[i].string);3459}34603461 cleanup:3462final_cleanup_renames(&re_info);34633464string_list_clear(entries,1);3465free(entries);34663467hashmap_free(&opt->current_file_dir_set,1);34683469if(clean <0) {3470unpack_trees_finish(opt);3471return clean;3472}3473}3474else3475 clean =1;34763477unpack_trees_finish(opt);34783479if(opt->call_depth && !(*result =write_tree_from_memory(opt)))3480return-1;34813482return clean;3483}34843485static struct commit_list *reverse_commit_list(struct commit_list *list)3486{3487struct commit_list *next = NULL, *current, *backup;3488for(current = list; current; current = backup) {3489 backup = current->next;3490 current->next = next;3491 next = current;3492}3493return next;3494}34953496/*3497 * Merge the commits h1 and h2, return the resulting virtual3498 * commit object and a flag indicating the cleanness of the merge.3499 */3500intmerge_recursive(struct merge_options *opt,3501struct commit *h1,3502struct commit *h2,3503struct commit_list *ca,3504struct commit **result)3505{3506struct commit_list *iter;3507struct commit *merged_common_ancestors;3508struct tree *mrtree;3509int clean;35103511if(show(opt,4)) {3512output(opt,4,_("Merging:"));3513output_commit_title(opt, h1);3514output_commit_title(opt, h2);3515}35163517if(!ca) {3518 ca =get_merge_bases(h1, h2);3519 ca =reverse_commit_list(ca);3520}35213522if(show(opt,5)) {3523unsigned cnt =commit_list_count(ca);35243525output(opt,5,Q_("found%ucommon ancestor:",3526"found%ucommon ancestors:", cnt), cnt);3527for(iter = ca; iter; iter = iter->next)3528output_commit_title(opt, iter->item);3529}35303531 merged_common_ancestors =pop_commit(&ca);3532if(merged_common_ancestors == NULL) {3533/* if there is no common ancestor, use an empty tree */3534struct tree *tree;35353536 tree =lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);3537 merged_common_ancestors =make_virtual_commit(opt->repo, tree,"ancestor");3538}35393540for(iter = ca; iter; iter = iter->next) {3541const char*saved_b1, *saved_b2;3542 opt->call_depth++;3543/*3544 * When the merge fails, the result contains files3545 * with conflict markers. The cleanness flag is3546 * ignored (unless indicating an error), it was never3547 * actually used, as result of merge_trees has always3548 * overwritten it: the committed "conflicts" were3549 * already resolved.3550 */3551discard_index(opt->repo->index);3552 saved_b1 = opt->branch1;3553 saved_b2 = opt->branch2;3554 opt->branch1 ="Temporary merge branch 1";3555 opt->branch2 ="Temporary merge branch 2";3556if(merge_recursive(opt, merged_common_ancestors, iter->item,3557 NULL, &merged_common_ancestors) <0)3558return-1;3559 opt->branch1 = saved_b1;3560 opt->branch2 = saved_b2;3561 opt->call_depth--;35623563if(!merged_common_ancestors)3564returnerr(opt,_("merge returned no commit"));3565}35663567discard_index(opt->repo->index);3568if(!opt->call_depth)3569repo_read_index(opt->repo);35703571 opt->ancestor ="merged common ancestors";3572 clean =merge_trees(opt,get_commit_tree(h1),get_commit_tree(h2),3573get_commit_tree(merged_common_ancestors),3574&mrtree);3575if(clean <0) {3576flush_output(opt);3577return clean;3578}35793580if(opt->call_depth) {3581*result =make_virtual_commit(opt->repo, mrtree,"merged tree");3582commit_list_insert(h1, &(*result)->parents);3583commit_list_insert(h2, &(*result)->parents->next);3584}3585flush_output(opt);3586if(!opt->call_depth && opt->buffer_output <2)3587strbuf_release(&opt->obuf);3588if(show(opt,2))3589diff_warn_rename_limit("merge.renamelimit",3590 opt->needed_rename_limit,0);3591return clean;3592}35933594static struct commit *get_ref(struct repository *repo,const struct object_id *oid,3595const char*name)3596{3597struct object *object;35983599 object =deref_tag(repo,parse_object(repo, oid),3600 name,strlen(name));3601if(!object)3602return NULL;3603if(object->type == OBJ_TREE)3604returnmake_virtual_commit(repo, (struct tree*)object, name);3605if(object->type != OBJ_COMMIT)3606return NULL;3607if(parse_commit((struct commit *)object))3608return NULL;3609return(struct commit *)object;3610}36113612intmerge_recursive_generic(struct merge_options *opt,3613const struct object_id *head,3614const struct object_id *merge,3615int num_base_list,3616const struct object_id **base_list,3617struct commit **result)3618{3619int clean;3620struct lock_file lock = LOCK_INIT;3621struct commit *head_commit =get_ref(opt->repo, head, opt->branch1);3622struct commit *next_commit =get_ref(opt->repo, merge, opt->branch2);3623struct commit_list *ca = NULL;36243625if(base_list) {3626int i;3627for(i =0; i < num_base_list; ++i) {3628struct commit *base;3629if(!(base =get_ref(opt->repo, base_list[i],oid_to_hex(base_list[i]))))3630returnerr(opt,_("Could not parse object '%s'"),3631oid_to_hex(base_list[i]));3632commit_list_insert(base, &ca);3633}3634}36353636repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);3637 clean =merge_recursive(opt, head_commit, next_commit, ca,3638 result);3639if(clean <0) {3640rollback_lock_file(&lock);3641return clean;3642}36433644if(write_locked_index(opt->repo->index, &lock,3645 COMMIT_LOCK | SKIP_IF_UNCHANGED))3646returnerr(opt,_("Unable to write index."));36473648return clean ?0:1;3649}36503651static voidmerge_recursive_config(struct merge_options *opt)3652{3653char*value = NULL;3654git_config_get_int("merge.verbosity", &opt->verbosity);3655git_config_get_int("diff.renamelimit", &opt->diff_rename_limit);3656git_config_get_int("merge.renamelimit", &opt->merge_rename_limit);3657if(!git_config_get_string("diff.renames", &value)) {3658 opt->diff_detect_rename =git_config_rename("diff.renames", value);3659free(value);3660}3661if(!git_config_get_string("merge.renames", &value)) {3662 opt->merge_detect_rename =git_config_rename("merge.renames", value);3663free(value);3664}3665if(!git_config_get_string("merge.directoryrenames", &value)) {3666int boolval =git_parse_maybe_bool(value);3667if(0<= boolval) {3668 opt->detect_directory_renames = boolval ?2:0;3669}else if(!strcasecmp(value,"conflict")) {3670 opt->detect_directory_renames =1;3671}/* avoid erroring on values from future versions of git */3672free(value);3673}3674git_config(git_xmerge_config, NULL);3675}36763677voidinit_merge_options(struct merge_options *opt,3678struct repository *repo)3679{3680const char*merge_verbosity;3681memset(opt,0,sizeof(struct merge_options));3682 opt->repo = repo;3683 opt->verbosity =2;3684 opt->buffer_output =1;3685 opt->diff_rename_limit = -1;3686 opt->merge_rename_limit = -1;3687 opt->renormalize =0;3688 opt->diff_detect_rename = -1;3689 opt->merge_detect_rename = -1;3690 opt->detect_directory_renames =1;3691merge_recursive_config(opt);3692 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3693if(merge_verbosity)3694 opt->verbosity =strtol(merge_verbosity, NULL,10);3695if(opt->verbosity >=5)3696 opt->buffer_output =0;3697strbuf_init(&opt->obuf,0);3698string_list_init(&opt->df_conflict_file_set,1);3699}37003701intparse_merge_opt(struct merge_options *opt,const char*s)3702{3703const char*arg;37043705if(!s || !*s)3706return-1;3707if(!strcmp(s,"ours"))3708 opt->recursive_variant = MERGE_RECURSIVE_OURS;3709else if(!strcmp(s,"theirs"))3710 opt->recursive_variant = MERGE_RECURSIVE_THEIRS;3711else if(!strcmp(s,"subtree"))3712 opt->subtree_shift ="";3713else if(skip_prefix(s,"subtree=", &arg))3714 opt->subtree_shift = arg;3715else if(!strcmp(s,"patience"))3716 opt->xdl_opts =DIFF_WITH_ALG(opt, PATIENCE_DIFF);3717else if(!strcmp(s,"histogram"))3718 opt->xdl_opts =DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);3719else if(skip_prefix(s,"diff-algorithm=", &arg)) {3720long value =parse_algorithm_value(arg);3721if(value <0)3722return-1;3723/* clear out previous settings */3724DIFF_XDL_CLR(opt, NEED_MINIMAL);3725 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3726 opt->xdl_opts |= value;3727}3728else if(!strcmp(s,"ignore-space-change"))3729DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);3730else if(!strcmp(s,"ignore-all-space"))3731DIFF_XDL_SET(opt, IGNORE_WHITESPACE);3732else if(!strcmp(s,"ignore-space-at-eol"))3733DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);3734else if(!strcmp(s,"ignore-cr-at-eol"))3735DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);3736else if(!strcmp(s,"renormalize"))3737 opt->renormalize =1;3738else if(!strcmp(s,"no-renormalize"))3739 opt->renormalize =0;3740else if(!strcmp(s,"no-renames"))3741 opt->merge_detect_rename =0;3742else if(!strcmp(s,"find-renames")) {3743 opt->merge_detect_rename =1;3744 opt->rename_score =0;3745}3746else if(skip_prefix(s,"find-renames=", &arg) ||3747skip_prefix(s,"rename-threshold=", &arg)) {3748if((opt->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3749return-1;3750 opt->merge_detect_rename =1;3751}3752/*3753 * Please update $__git_merge_strategy_options in3754 * git-completion.bash when you add new options3755 */3756else3757return-1;3758return0;3759}