1/* 2 * Recursive Merge algorithm stolen from git-merge-recursive.py by 3 * Fredrik Kuivinen. 4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 5 */ 6#include"cache.h" 7#include"config.h" 8#include"advice.h" 9#include"lockfile.h" 10#include"cache-tree.h" 11#include"object-store.h" 12#include"repository.h" 13#include"commit.h" 14#include"blob.h" 15#include"builtin.h" 16#include"tree-walk.h" 17#include"diff.h" 18#include"diffcore.h" 19#include"tag.h" 20#include"alloc.h" 21#include"unpack-trees.h" 22#include"string-list.h" 23#include"xdiff-interface.h" 24#include"ll-merge.h" 25#include"attr.h" 26#include"merge-recursive.h" 27#include"dir.h" 28#include"submodule.h" 29#include"revision.h" 30#include"commit-reach.h" 31 32struct path_hashmap_entry { 33struct hashmap_entry e; 34char path[FLEX_ARRAY]; 35}; 36 37static intpath_hashmap_cmp(const void*cmp_data, 38const void*entry, 39const void*entry_or_key, 40const void*keydata) 41{ 42const struct path_hashmap_entry *a = entry; 43const struct path_hashmap_entry *b = entry_or_key; 44const char*key = keydata; 45 46if(ignore_case) 47returnstrcasecmp(a->path, key ? key : b->path); 48else 49returnstrcmp(a->path, key ? key : b->path); 50} 51 52static unsigned intpath_hash(const char*path) 53{ 54return ignore_case ?strihash(path) :strhash(path); 55} 56 57static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap, 58char*dir) 59{ 60struct dir_rename_entry key; 61 62if(dir == NULL) 63return NULL; 64hashmap_entry_init(&key,strhash(dir)); 65 key.dir = dir; 66returnhashmap_get(hashmap, &key, NULL); 67} 68 69static intdir_rename_cmp(const void*unused_cmp_data, 70const void*entry, 71const void*entry_or_key, 72const void*unused_keydata) 73{ 74const struct dir_rename_entry *e1 = entry; 75const struct dir_rename_entry *e2 = entry_or_key; 76 77returnstrcmp(e1->dir, e2->dir); 78} 79 80static voiddir_rename_init(struct hashmap *map) 81{ 82hashmap_init(map, dir_rename_cmp, NULL,0); 83} 84 85static voiddir_rename_entry_init(struct dir_rename_entry *entry, 86char*directory) 87{ 88hashmap_entry_init(entry,strhash(directory)); 89 entry->dir = directory; 90 entry->non_unique_new_dir =0; 91strbuf_init(&entry->new_dir,0); 92string_list_init(&entry->possible_new_dirs,0); 93} 94 95static struct collision_entry *collision_find_entry(struct hashmap *hashmap, 96char*target_file) 97{ 98struct collision_entry key; 99 100hashmap_entry_init(&key,strhash(target_file)); 101 key.target_file = target_file; 102returnhashmap_get(hashmap, &key, NULL); 103} 104 105static intcollision_cmp(void*unused_cmp_data, 106const struct collision_entry *e1, 107const struct collision_entry *e2, 108const void*unused_keydata) 109{ 110returnstrcmp(e1->target_file, e2->target_file); 111} 112 113static voidcollision_init(struct hashmap *map) 114{ 115hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL,0); 116} 117 118static voidflush_output(struct merge_options *opt) 119{ 120if(opt->buffer_output <2&& opt->obuf.len) { 121fputs(opt->obuf.buf, stdout); 122strbuf_reset(&opt->obuf); 123} 124} 125 126static interr(struct merge_options *opt,const char*err, ...) 127{ 128va_list params; 129 130if(opt->buffer_output <2) 131flush_output(opt); 132else{ 133strbuf_complete(&opt->obuf,'\n'); 134strbuf_addstr(&opt->obuf,"error: "); 135} 136va_start(params, err); 137strbuf_vaddf(&opt->obuf, err, params); 138va_end(params); 139if(opt->buffer_output >1) 140strbuf_addch(&opt->obuf,'\n'); 141else{ 142error("%s", opt->obuf.buf); 143strbuf_reset(&opt->obuf); 144} 145 146return-1; 147} 148 149static struct tree *shift_tree_object(struct repository *repo, 150struct tree *one,struct tree *two, 151const char*subtree_shift) 152{ 153struct object_id shifted; 154 155if(!*subtree_shift) { 156shift_tree(&one->object.oid, &two->object.oid, &shifted,0); 157}else{ 158shift_tree_by(&one->object.oid, &two->object.oid, &shifted, 159 subtree_shift); 160} 161if(oideq(&two->object.oid, &shifted)) 162return two; 163returnlookup_tree(repo, &shifted); 164} 165 166static struct commit *make_virtual_commit(struct repository *repo, 167struct tree *tree, 168const char*comment) 169{ 170struct commit *commit =alloc_commit_node(repo); 171 172set_merge_remote_desc(commit, comment, (struct object *)commit); 173 commit->maybe_tree = tree; 174 commit->object.parsed =1; 175return commit; 176} 177 178/* 179 * Since we use get_tree_entry(), which does not put the read object into 180 * the object pool, we cannot rely on a == b. 181 */ 182static intoid_eq(const struct object_id *a,const struct object_id *b) 183{ 184if(!a && !b) 185return2; 186return a && b &&oideq(a, b); 187} 188 189enum rename_type { 190 RENAME_NORMAL =0, 191 RENAME_VIA_DIR, 192 RENAME_ADD, 193 RENAME_DELETE, 194 RENAME_ONE_FILE_TO_ONE, 195 RENAME_ONE_FILE_TO_TWO, 196 RENAME_TWO_FILES_TO_ONE 197}; 198 199/* 200 * Since we want to write the index eventually, we cannot reuse the index 201 * for these (temporary) data. 202 */ 203struct stage_data { 204struct{ 205unsigned short mode; 206struct object_id oid; 207} stages[4]; 208struct rename_conflict_info *rename_conflict_info; 209unsigned processed:1; 210}; 211 212struct rename { 213struct diff_filepair *pair; 214const char*branch;/* branch that the rename occurred on */ 215/* 216 * Purpose of src_entry and dst_entry: 217 * 218 * If 'before' is renamed to 'after' then src_entry will contain 219 * the versions of 'before' from the merge_base, HEAD, and MERGE in 220 * stages 1, 2, and 3; dst_entry will contain the respective 221 * versions of 'after' in corresponding locations. Thus, we have a 222 * total of six modes and oids, though some will be null. (Stage 0 223 * is ignored; we're interested in handling conflicts.) 224 * 225 * Since we don't turn on break-rewrites by default, neither 226 * src_entry nor dst_entry can have all three of their stages have 227 * non-null oids, meaning at most four of the six will be non-null. 228 * Also, since this is a rename, both src_entry and dst_entry will 229 * have at least one non-null oid, meaning at least two will be 230 * non-null. Of the six oids, a typical rename will have three be 231 * non-null. Only two implies a rename/delete, and four implies a 232 * rename/add. 233 */ 234struct stage_data *src_entry; 235struct stage_data *dst_entry; 236unsigned add_turned_into_rename:1; 237unsigned processed:1; 238}; 239 240struct rename_conflict_info { 241enum rename_type rename_type; 242struct rename *ren1; 243struct rename *ren2; 244}; 245 246staticinlinevoidsetup_rename_conflict_info(enum rename_type rename_type, 247struct merge_options *opt, 248struct rename *ren1, 249struct rename *ren2) 250{ 251struct rename_conflict_info *ci; 252 253/* 254 * When we have two renames involved, it's easiest to get the 255 * correct things into stage 2 and 3, and to make sure that the 256 * content merge puts HEAD before the other branch if we just 257 * ensure that branch1 == opt->branch1. So, simply flip arguments 258 * around if we don't have that. 259 */ 260if(ren2 && ren1->branch != opt->branch1) { 261setup_rename_conflict_info(rename_type, opt, ren2, ren1); 262return; 263} 264 265 ci =xcalloc(1,sizeof(struct rename_conflict_info)); 266 ci->rename_type = rename_type; 267 ci->ren1 = ren1; 268 ci->ren2 = ren2; 269 270 ci->ren1->dst_entry->processed =0; 271 ci->ren1->dst_entry->rename_conflict_info = ci; 272 273if(ren2) { 274 ci->ren2->dst_entry->rename_conflict_info = ci; 275} 276} 277 278static intshow(struct merge_options *opt,int v) 279{ 280return(!opt->call_depth && opt->verbosity >= v) || opt->verbosity >=5; 281} 282 283__attribute__((format(printf,3,4))) 284static voidoutput(struct merge_options *opt,int v,const char*fmt, ...) 285{ 286va_list ap; 287 288if(!show(opt, v)) 289return; 290 291strbuf_addchars(&opt->obuf,' ', opt->call_depth *2); 292 293va_start(ap, fmt); 294strbuf_vaddf(&opt->obuf, fmt, ap); 295va_end(ap); 296 297strbuf_addch(&opt->obuf,'\n'); 298if(!opt->buffer_output) 299flush_output(opt); 300} 301 302static voidoutput_commit_title(struct merge_options *opt,struct commit *commit) 303{ 304struct merge_remote_desc *desc; 305 306strbuf_addchars(&opt->obuf,' ', opt->call_depth *2); 307 desc =merge_remote_util(commit); 308if(desc) 309strbuf_addf(&opt->obuf,"virtual%s\n", desc->name); 310else{ 311strbuf_add_unique_abbrev(&opt->obuf, &commit->object.oid, 312 DEFAULT_ABBREV); 313strbuf_addch(&opt->obuf,' '); 314if(parse_commit(commit) !=0) 315strbuf_addstr(&opt->obuf,_("(bad commit)\n")); 316else{ 317const char*title; 318const char*msg =get_commit_buffer(commit, NULL); 319int len =find_commit_subject(msg, &title); 320if(len) 321strbuf_addf(&opt->obuf,"%.*s\n", len, title); 322unuse_commit_buffer(commit, msg); 323} 324} 325flush_output(opt); 326} 327 328static intadd_cacheinfo(struct merge_options *opt, 329unsigned int mode,const struct object_id *oid, 330const char*path,int stage,int refresh,int options) 331{ 332struct index_state *istate = opt->repo->index; 333struct cache_entry *ce; 334int ret; 335 336 ce =make_cache_entry(istate, mode, oid ? oid : &null_oid, path, stage,0); 337if(!ce) 338returnerr(opt,_("add_cacheinfo failed for path '%s'; merge aborting."), path); 339 340 ret =add_index_entry(istate, ce, options); 341if(refresh) { 342struct cache_entry *nce; 343 344 nce =refresh_cache_entry(istate, ce, 345 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); 346if(!nce) 347returnerr(opt,_("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); 348if(nce != ce) 349 ret =add_index_entry(istate, nce, options); 350} 351return ret; 352} 353 354static voidinit_tree_desc_from_tree(struct tree_desc *desc,struct tree *tree) 355{ 356parse_tree(tree); 357init_tree_desc(desc, tree->buffer, tree->size); 358} 359 360static intunpack_trees_start(struct merge_options *opt, 361struct tree *common, 362struct tree *head, 363struct tree *merge) 364{ 365int rc; 366struct tree_desc t[3]; 367struct index_state tmp_index = { NULL }; 368 369memset(&opt->unpack_opts,0,sizeof(opt->unpack_opts)); 370if(opt->call_depth) 371 opt->unpack_opts.index_only =1; 372else 373 opt->unpack_opts.update =1; 374 opt->unpack_opts.merge =1; 375 opt->unpack_opts.head_idx =2; 376 opt->unpack_opts.fn = threeway_merge; 377 opt->unpack_opts.src_index = opt->repo->index; 378 opt->unpack_opts.dst_index = &tmp_index; 379 opt->unpack_opts.aggressive = !merge_detect_rename(opt); 380setup_unpack_trees_porcelain(&opt->unpack_opts,"merge"); 381 382init_tree_desc_from_tree(t+0, common); 383init_tree_desc_from_tree(t+1, head); 384init_tree_desc_from_tree(t+2, merge); 385 386 rc =unpack_trees(3, t, &opt->unpack_opts); 387cache_tree_free(&opt->repo->index->cache_tree); 388 389/* 390 * Update opt->repo->index to match the new results, AFTER saving a copy 391 * in opt->orig_index. Update src_index to point to the saved copy. 392 * (verify_uptodate() checks src_index, and the original index is 393 * the one that had the necessary modification timestamps.) 394 */ 395 opt->orig_index = *opt->repo->index; 396*opt->repo->index = tmp_index; 397 opt->unpack_opts.src_index = &opt->orig_index; 398 399return rc; 400} 401 402static voidunpack_trees_finish(struct merge_options *opt) 403{ 404discard_index(&opt->orig_index); 405clear_unpack_trees_porcelain(&opt->unpack_opts); 406} 407 408struct tree *write_tree_from_memory(struct merge_options *opt) 409{ 410struct tree *result = NULL; 411struct index_state *istate = opt->repo->index; 412 413if(unmerged_index(istate)) { 414int i; 415fprintf(stderr,"BUG: There are unmerged index entries:\n"); 416for(i =0; i < istate->cache_nr; i++) { 417const struct cache_entry *ce = istate->cache[i]; 418if(ce_stage(ce)) 419fprintf(stderr,"BUG:%d%.*s\n",ce_stage(ce), 420(int)ce_namelen(ce), ce->name); 421} 422BUG("unmerged index entries in merge-recursive.c"); 423} 424 425if(!istate->cache_tree) 426 istate->cache_tree =cache_tree(); 427 428if(!cache_tree_fully_valid(istate->cache_tree) && 429cache_tree_update(istate,0) <0) { 430err(opt,_("error building trees")); 431return NULL; 432} 433 434 result =lookup_tree(opt->repo, &istate->cache_tree->oid); 435 436return result; 437} 438 439static intsave_files_dirs(const struct object_id *oid, 440struct strbuf *base,const char*path, 441unsigned int mode,int stage,void*context) 442{ 443struct path_hashmap_entry *entry; 444int baselen = base->len; 445struct merge_options *opt = context; 446 447strbuf_addstr(base, path); 448 449FLEX_ALLOC_MEM(entry, path, base->buf, base->len); 450hashmap_entry_init(entry,path_hash(entry->path)); 451hashmap_add(&opt->current_file_dir_set, entry); 452 453strbuf_setlen(base, baselen); 454return(S_ISDIR(mode) ? READ_TREE_RECURSIVE :0); 455} 456 457static voidget_files_dirs(struct merge_options *opt,struct tree *tree) 458{ 459struct pathspec match_all; 460memset(&match_all,0,sizeof(match_all)); 461read_tree_recursive(the_repository, tree,"",0,0, 462&match_all, save_files_dirs, opt); 463} 464 465static intget_tree_entry_if_blob(const struct object_id *tree, 466const char*path, 467struct object_id *hashy, 468unsigned short*mode_o) 469{ 470int ret; 471 472 ret =get_tree_entry(tree, path, hashy, mode_o); 473if(S_ISDIR(*mode_o)) { 474oidcpy(hashy, &null_oid); 475*mode_o =0; 476} 477return ret; 478} 479 480/* 481 * Returns an index_entry instance which doesn't have to correspond to 482 * a real cache entry in Git's index. 483 */ 484static struct stage_data *insert_stage_data(const char*path, 485struct tree *o,struct tree *a,struct tree *b, 486struct string_list *entries) 487{ 488struct string_list_item *item; 489struct stage_data *e =xcalloc(1,sizeof(struct stage_data)); 490get_tree_entry_if_blob(&o->object.oid, path, 491&e->stages[1].oid, &e->stages[1].mode); 492get_tree_entry_if_blob(&a->object.oid, path, 493&e->stages[2].oid, &e->stages[2].mode); 494get_tree_entry_if_blob(&b->object.oid, path, 495&e->stages[3].oid, &e->stages[3].mode); 496 item =string_list_insert(entries, path); 497 item->util = e; 498return e; 499} 500 501/* 502 * Create a dictionary mapping file names to stage_data objects. The 503 * dictionary contains one entry for every path with a non-zero stage entry. 504 */ 505static struct string_list *get_unmerged(struct index_state *istate) 506{ 507struct string_list *unmerged =xcalloc(1,sizeof(struct string_list)); 508int i; 509 510 unmerged->strdup_strings =1; 511 512for(i =0; i < istate->cache_nr; i++) { 513struct string_list_item *item; 514struct stage_data *e; 515const struct cache_entry *ce = istate->cache[i]; 516if(!ce_stage(ce)) 517continue; 518 519 item =string_list_lookup(unmerged, ce->name); 520if(!item) { 521 item =string_list_insert(unmerged, ce->name); 522 item->util =xcalloc(1,sizeof(struct stage_data)); 523} 524 e = item->util; 525 e->stages[ce_stage(ce)].mode = ce->ce_mode; 526oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); 527} 528 529return unmerged; 530} 531 532static intstring_list_df_name_compare(const char*one,const char*two) 533{ 534int onelen =strlen(one); 535int twolen =strlen(two); 536/* 537 * Here we only care that entries for D/F conflicts are 538 * adjacent, in particular with the file of the D/F conflict 539 * appearing before files below the corresponding directory. 540 * The order of the rest of the list is irrelevant for us. 541 * 542 * To achieve this, we sort with df_name_compare and provide 543 * the mode S_IFDIR so that D/F conflicts will sort correctly. 544 * We use the mode S_IFDIR for everything else for simplicity, 545 * since in other cases any changes in their order due to 546 * sorting cause no problems for us. 547 */ 548int cmp =df_name_compare(one, onelen, S_IFDIR, 549 two, twolen, S_IFDIR); 550/* 551 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure 552 * that 'foo' comes before 'foo/bar'. 553 */ 554if(cmp) 555return cmp; 556return onelen - twolen; 557} 558 559static voidrecord_df_conflict_files(struct merge_options *opt, 560struct string_list *entries) 561{ 562/* If there is a D/F conflict and the file for such a conflict 563 * currently exists in the working tree, we want to allow it to be 564 * removed to make room for the corresponding directory if needed. 565 * The files underneath the directories of such D/F conflicts will 566 * be processed before the corresponding file involved in the D/F 567 * conflict. If the D/F directory ends up being removed by the 568 * merge, then we won't have to touch the D/F file. If the D/F 569 * directory needs to be written to the working copy, then the D/F 570 * file will simply be removed (in make_room_for_path()) to make 571 * room for the necessary paths. Note that if both the directory 572 * and the file need to be present, then the D/F file will be 573 * reinstated with a new unique name at the time it is processed. 574 */ 575struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; 576const char*last_file = NULL; 577int last_len =0; 578int i; 579 580/* 581 * If we're merging merge-bases, we don't want to bother with 582 * any working directory changes. 583 */ 584if(opt->call_depth) 585return; 586 587/* Ensure D/F conflicts are adjacent in the entries list. */ 588for(i =0; i < entries->nr; i++) { 589struct string_list_item *next = &entries->items[i]; 590string_list_append(&df_sorted_entries, next->string)->util = 591 next->util; 592} 593 df_sorted_entries.cmp = string_list_df_name_compare; 594string_list_sort(&df_sorted_entries); 595 596string_list_clear(&opt->df_conflict_file_set,1); 597for(i =0; i < df_sorted_entries.nr; i++) { 598const char*path = df_sorted_entries.items[i].string; 599int len =strlen(path); 600struct stage_data *e = df_sorted_entries.items[i].util; 601 602/* 603 * Check if last_file & path correspond to a D/F conflict; 604 * i.e. whether path is last_file+'/'+<something>. 605 * If so, record that it's okay to remove last_file to make 606 * room for path and friends if needed. 607 */ 608if(last_file && 609 len > last_len && 610memcmp(path, last_file, last_len) ==0&& 611 path[last_len] =='/') { 612string_list_insert(&opt->df_conflict_file_set, last_file); 613} 614 615/* 616 * Determine whether path could exist as a file in the 617 * working directory as a possible D/F conflict. This 618 * will only occur when it exists in stage 2 as a 619 * file. 620 */ 621if(S_ISREG(e->stages[2].mode) ||S_ISLNK(e->stages[2].mode)) { 622 last_file = path; 623 last_len = len; 624}else{ 625 last_file = NULL; 626} 627} 628string_list_clear(&df_sorted_entries,0); 629} 630 631static intupdate_stages(struct merge_options *opt,const char*path, 632const struct diff_filespec *o, 633const struct diff_filespec *a, 634const struct diff_filespec *b) 635{ 636 637/* 638 * NOTE: It is usually a bad idea to call update_stages on a path 639 * before calling update_file on that same path, since it can 640 * sometimes lead to spurious "refusing to lose untracked file..." 641 * messages from update_file (via make_room_for path via 642 * would_lose_untracked). Instead, reverse the order of the calls 643 * (executing update_file first and then update_stages). 644 */ 645int clear =1; 646int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; 647if(clear) 648if(remove_file_from_index(opt->repo->index, path)) 649return-1; 650if(o) 651if(add_cacheinfo(opt, o->mode, &o->oid, path,1,0, options)) 652return-1; 653if(a) 654if(add_cacheinfo(opt, a->mode, &a->oid, path,2,0, options)) 655return-1; 656if(b) 657if(add_cacheinfo(opt, b->mode, &b->oid, path,3,0, options)) 658return-1; 659return0; 660} 661 662static voidupdate_entry(struct stage_data *entry, 663struct diff_filespec *o, 664struct diff_filespec *a, 665struct diff_filespec *b) 666{ 667 entry->processed =0; 668 entry->stages[1].mode = o->mode; 669 entry->stages[2].mode = a->mode; 670 entry->stages[3].mode = b->mode; 671oidcpy(&entry->stages[1].oid, &o->oid); 672oidcpy(&entry->stages[2].oid, &a->oid); 673oidcpy(&entry->stages[3].oid, &b->oid); 674} 675 676static intremove_file(struct merge_options *opt,int clean, 677const char*path,int no_wd) 678{ 679int update_cache = opt->call_depth || clean; 680int update_working_directory = !opt->call_depth && !no_wd; 681 682if(update_cache) { 683if(remove_file_from_index(opt->repo->index, path)) 684return-1; 685} 686if(update_working_directory) { 687if(ignore_case) { 688struct cache_entry *ce; 689 ce =index_file_exists(opt->repo->index, path,strlen(path), 690 ignore_case); 691if(ce &&ce_stage(ce) ==0&&strcmp(path, ce->name)) 692return0; 693} 694if(remove_path(path)) 695return-1; 696} 697return0; 698} 699 700/* add a string to a strbuf, but converting "/" to "_" */ 701static voidadd_flattened_path(struct strbuf *out,const char*s) 702{ 703size_t i = out->len; 704strbuf_addstr(out, s); 705for(; i < out->len; i++) 706if(out->buf[i] =='/') 707 out->buf[i] ='_'; 708} 709 710static char*unique_path(struct merge_options *opt,const char*path,const char*branch) 711{ 712struct path_hashmap_entry *entry; 713struct strbuf newpath = STRBUF_INIT; 714int suffix =0; 715size_t base_len; 716 717strbuf_addf(&newpath,"%s~", path); 718add_flattened_path(&newpath, branch); 719 720 base_len = newpath.len; 721while(hashmap_get_from_hash(&opt->current_file_dir_set, 722path_hash(newpath.buf), newpath.buf) || 723(!opt->call_depth &&file_exists(newpath.buf))) { 724strbuf_setlen(&newpath, base_len); 725strbuf_addf(&newpath,"_%d", suffix++); 726} 727 728FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); 729hashmap_entry_init(entry,path_hash(entry->path)); 730hashmap_add(&opt->current_file_dir_set, entry); 731returnstrbuf_detach(&newpath, NULL); 732} 733 734/** 735 * Check whether a directory in the index is in the way of an incoming 736 * file. Return 1 if so. If check_working_copy is non-zero, also 737 * check the working directory. If empty_ok is non-zero, also return 738 * 0 in the case where the working-tree dir exists but is empty. 739 */ 740static intdir_in_way(struct index_state *istate,const char*path, 741int check_working_copy,int empty_ok) 742{ 743int pos; 744struct strbuf dirpath = STRBUF_INIT; 745struct stat st; 746 747strbuf_addstr(&dirpath, path); 748strbuf_addch(&dirpath,'/'); 749 750 pos =index_name_pos(istate, dirpath.buf, dirpath.len); 751 752if(pos <0) 753 pos = -1- pos; 754if(pos < istate->cache_nr && 755!strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) { 756strbuf_release(&dirpath); 757return1; 758} 759 760strbuf_release(&dirpath); 761return check_working_copy && !lstat(path, &st) &&S_ISDIR(st.st_mode) && 762!(empty_ok &&is_empty_dir(path)); 763} 764 765/* 766 * Returns whether path was tracked in the index before the merge started, 767 * and its oid and mode match the specified values 768 */ 769static intwas_tracked_and_matches(struct merge_options *opt,const char*path, 770const struct object_id *oid,unsigned mode) 771{ 772int pos =index_name_pos(&opt->orig_index, path,strlen(path)); 773struct cache_entry *ce; 774 775if(0> pos) 776/* we were not tracking this path before the merge */ 777return0; 778 779/* See if the file we were tracking before matches */ 780 ce = opt->orig_index.cache[pos]; 781return(oid_eq(&ce->oid, oid) && ce->ce_mode == mode); 782} 783 784/* 785 * Returns whether path was tracked in the index before the merge started 786 */ 787static intwas_tracked(struct merge_options *opt,const char*path) 788{ 789int pos =index_name_pos(&opt->orig_index, path,strlen(path)); 790 791if(0<= pos) 792/* we were tracking this path before the merge */ 793return1; 794 795return0; 796} 797 798static intwould_lose_untracked(struct merge_options *opt,const char*path) 799{ 800struct index_state *istate = opt->repo->index; 801 802/* 803 * This may look like it can be simplified to: 804 * return !was_tracked(opt, path) && file_exists(path) 805 * but it can't. This function needs to know whether path was in 806 * the working tree due to EITHER having been tracked in the index 807 * before the merge OR having been put into the working copy and 808 * index by unpack_trees(). Due to that either-or requirement, we 809 * check the current index instead of the original one. 810 * 811 * Note that we do not need to worry about merge-recursive itself 812 * updating the index after unpack_trees() and before calling this 813 * function, because we strictly require all code paths in 814 * merge-recursive to update the working tree first and the index 815 * second. Doing otherwise would break 816 * update_file()/would_lose_untracked(); see every comment in this 817 * file which mentions "update_stages". 818 */ 819int pos =index_name_pos(istate, path,strlen(path)); 820 821if(pos <0) 822 pos = -1- pos; 823while(pos < istate->cache_nr && 824!strcmp(path, istate->cache[pos]->name)) { 825/* 826 * If stage #0, it is definitely tracked. 827 * If it has stage #2 then it was tracked 828 * before this merge started. All other 829 * cases the path was not tracked. 830 */ 831switch(ce_stage(istate->cache[pos])) { 832case0: 833case2: 834return0; 835} 836 pos++; 837} 838returnfile_exists(path); 839} 840 841static intwas_dirty(struct merge_options *opt,const char*path) 842{ 843struct cache_entry *ce; 844int dirty =1; 845 846if(opt->call_depth || !was_tracked(opt, path)) 847return!dirty; 848 849 ce =index_file_exists(opt->unpack_opts.src_index, 850 path,strlen(path), ignore_case); 851 dirty =verify_uptodate(ce, &opt->unpack_opts) !=0; 852return dirty; 853} 854 855static intmake_room_for_path(struct merge_options *opt,const char*path) 856{ 857int status, i; 858const char*msg =_("failed to create path '%s'%s"); 859 860/* Unlink any D/F conflict files that are in the way */ 861for(i =0; i < opt->df_conflict_file_set.nr; i++) { 862const char*df_path = opt->df_conflict_file_set.items[i].string; 863size_t pathlen =strlen(path); 864size_t df_pathlen =strlen(df_path); 865if(df_pathlen < pathlen && 866 path[df_pathlen] =='/'&& 867strncmp(path, df_path, df_pathlen) ==0) { 868output(opt,3, 869_("Removing%sto make room for subdirectory\n"), 870 df_path); 871unlink(df_path); 872unsorted_string_list_delete_item(&opt->df_conflict_file_set, 873 i,0); 874break; 875} 876} 877 878/* Make sure leading directories are created */ 879 status =safe_create_leading_directories_const(path); 880if(status) { 881if(status == SCLD_EXISTS) 882/* something else exists */ 883returnerr(opt, msg, path,_(": perhaps a D/F conflict?")); 884returnerr(opt, msg, path,""); 885} 886 887/* 888 * Do not unlink a file in the work tree if we are not 889 * tracking it. 890 */ 891if(would_lose_untracked(opt, path)) 892returnerr(opt,_("refusing to lose untracked file at '%s'"), 893 path); 894 895/* Successful unlink is good.. */ 896if(!unlink(path)) 897return0; 898/* .. and so is no existing file */ 899if(errno == ENOENT) 900return0; 901/* .. but not some other error (who really cares what?) */ 902returnerr(opt, msg, path,_(": perhaps a D/F conflict?")); 903} 904 905static intupdate_file_flags(struct merge_options *opt, 906const struct object_id *oid, 907unsigned mode, 908const char*path, 909int update_cache, 910int update_wd) 911{ 912int ret =0; 913 914if(opt->call_depth) 915 update_wd =0; 916 917if(update_wd) { 918enum object_type type; 919void*buf; 920unsigned long size; 921 922if(S_ISGITLINK(mode)) { 923/* 924 * We may later decide to recursively descend into 925 * the submodule directory and update its index 926 * and/or work tree, but we do not do that now. 927 */ 928 update_wd =0; 929goto update_index; 930} 931 932 buf =read_object_file(oid, &type, &size); 933if(!buf) 934returnerr(opt,_("cannot read object%s'%s'"),oid_to_hex(oid), path); 935if(type != OBJ_BLOB) { 936 ret =err(opt,_("blob expected for%s'%s'"),oid_to_hex(oid), path); 937goto free_buf; 938} 939if(S_ISREG(mode)) { 940struct strbuf strbuf = STRBUF_INIT; 941if(convert_to_working_tree(opt->repo->index, path, buf, size, &strbuf)) { 942free(buf); 943 size = strbuf.len; 944 buf =strbuf_detach(&strbuf, NULL); 945} 946} 947 948if(make_room_for_path(opt, path) <0) { 949 update_wd =0; 950goto free_buf; 951} 952if(S_ISREG(mode) || (!has_symlinks &&S_ISLNK(mode))) { 953int fd; 954if(mode &0100) 955 mode =0777; 956else 957 mode =0666; 958 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); 959if(fd <0) { 960 ret =err(opt,_("failed to open '%s':%s"), 961 path,strerror(errno)); 962goto free_buf; 963} 964write_in_full(fd, buf, size); 965close(fd); 966}else if(S_ISLNK(mode)) { 967char*lnk =xmemdupz(buf, size); 968safe_create_leading_directories_const(path); 969unlink(path); 970if(symlink(lnk, path)) 971 ret =err(opt,_("failed to symlink '%s':%s"), 972 path,strerror(errno)); 973free(lnk); 974}else 975 ret =err(opt, 976_("do not know what to do with%06o%s'%s'"), 977 mode,oid_to_hex(oid), path); 978 free_buf: 979free(buf); 980} 981update_index: 982if(!ret && update_cache) 983if(add_cacheinfo(opt, mode, oid, path,0, update_wd, 984 ADD_CACHE_OK_TO_ADD)) 985return-1; 986return ret; 987} 988 989static intupdate_file(struct merge_options *opt, 990int clean, 991const struct object_id *oid, 992unsigned mode, 993const char*path) 994{ 995returnupdate_file_flags(opt, oid, mode, path, opt->call_depth || clean, !opt->call_depth); 996} 997 998/* Low level file merging, update and removal */ 9991000struct merge_file_info {1001struct object_id oid;1002unsigned mode;1003unsigned clean:1,1004 merge:1;1005};10061007static intmerge_3way(struct merge_options *opt,1008 mmbuffer_t *result_buf,1009const struct diff_filespec *o,1010const struct diff_filespec *a,1011const struct diff_filespec *b,1012const char*branch1,1013const char*branch2,1014const int extra_marker_size)1015{1016 mmfile_t orig, src1, src2;1017struct ll_merge_options ll_opts = {0};1018char*base_name, *name1, *name2;1019int merge_status;10201021 ll_opts.renormalize = opt->renormalize;1022 ll_opts.extra_marker_size = extra_marker_size;1023 ll_opts.xdl_opts = opt->xdl_opts;10241025if(opt->call_depth) {1026 ll_opts.virtual_ancestor =1;1027 ll_opts.variant =0;1028}else{1029switch(opt->recursive_variant) {1030case MERGE_RECURSIVE_OURS:1031 ll_opts.variant = XDL_MERGE_FAVOR_OURS;1032break;1033case MERGE_RECURSIVE_THEIRS:1034 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;1035break;1036default:1037 ll_opts.variant =0;1038break;1039}1040}10411042if(strcmp(a->path, b->path) ||1043(opt->ancestor != NULL &&strcmp(a->path, o->path) !=0)) {1044 base_name = opt->ancestor == NULL ? NULL :1045mkpathdup("%s:%s", opt->ancestor, o->path);1046 name1 =mkpathdup("%s:%s", branch1, a->path);1047 name2 =mkpathdup("%s:%s", branch2, b->path);1048}else{1049 base_name = opt->ancestor == NULL ? NULL :1050mkpathdup("%s", opt->ancestor);1051 name1 =mkpathdup("%s", branch1);1052 name2 =mkpathdup("%s", branch2);1053}10541055read_mmblob(&orig, &o->oid);1056read_mmblob(&src1, &a->oid);1057read_mmblob(&src2, &b->oid);10581059 merge_status =ll_merge(result_buf, a->path, &orig, base_name,1060&src1, name1, &src2, name2,1061 opt->repo->index, &ll_opts);10621063free(base_name);1064free(name1);1065free(name2);1066free(orig.ptr);1067free(src1.ptr);1068free(src2.ptr);1069return merge_status;1070}10711072static intfind_first_merges(struct repository *repo,1073struct object_array *result,const char*path,1074struct commit *a,struct commit *b)1075{1076int i, j;1077struct object_array merges = OBJECT_ARRAY_INIT;1078struct commit *commit;1079int contains_another;10801081char merged_revision[42];1082const char*rev_args[] = {"rev-list","--merges","--ancestry-path",1083"--all", merged_revision, NULL };1084struct rev_info revs;1085struct setup_revision_opt rev_opts;10861087memset(result,0,sizeof(struct object_array));1088memset(&rev_opts,0,sizeof(rev_opts));10891090/* get all revisions that merge commit a */1091xsnprintf(merged_revision,sizeof(merged_revision),"^%s",1092oid_to_hex(&a->object.oid));1093repo_init_revisions(repo, &revs, NULL);1094 rev_opts.submodule = path;1095/* FIXME: can't handle linked worktrees in submodules yet */1096 revs.single_worktree = path != NULL;1097setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);10981099/* save all revisions from the above list that contain b */1100if(prepare_revision_walk(&revs))1101die("revision walk setup failed");1102while((commit =get_revision(&revs)) != NULL) {1103struct object *o = &(commit->object);1104if(in_merge_bases(b, commit))1105add_object_array(o, NULL, &merges);1106}1107reset_revision_walk();11081109/* Now we've got all merges that contain a and b. Prune all1110 * merges that contain another found merge and save them in1111 * result.1112 */1113for(i =0; i < merges.nr; i++) {1114struct commit *m1 = (struct commit *) merges.objects[i].item;11151116 contains_another =0;1117for(j =0; j < merges.nr; j++) {1118struct commit *m2 = (struct commit *) merges.objects[j].item;1119if(i != j &&in_merge_bases(m2, m1)) {1120 contains_another =1;1121break;1122}1123}11241125if(!contains_another)1126add_object_array(merges.objects[i].item, NULL, result);1127}11281129object_array_clear(&merges);1130return result->nr;1131}11321133static voidprint_commit(struct commit *commit)1134{1135struct strbuf sb = STRBUF_INIT;1136struct pretty_print_context ctx = {0};1137 ctx.date_mode.type = DATE_NORMAL;1138format_commit_message(commit,"%h:%m %s", &sb, &ctx);1139fprintf(stderr,"%s\n", sb.buf);1140strbuf_release(&sb);1141}11421143static intmerge_submodule(struct merge_options *opt,1144struct object_id *result,const char*path,1145const struct object_id *base,const struct object_id *a,1146const struct object_id *b)1147{1148struct commit *commit_base, *commit_a, *commit_b;1149int parent_count;1150struct object_array merges;11511152int i;1153int search = !opt->call_depth;11541155/* store a in result in case we fail */1156oidcpy(result, a);11571158/* we can not handle deletion conflicts */1159if(is_null_oid(base))1160return0;1161if(is_null_oid(a))1162return0;1163if(is_null_oid(b))1164return0;11651166if(add_submodule_odb(path)) {1167output(opt,1,_("Failed to merge submodule%s(not checked out)"), path);1168return0;1169}11701171if(!(commit_base =lookup_commit_reference(opt->repo, base)) ||1172!(commit_a =lookup_commit_reference(opt->repo, a)) ||1173!(commit_b =lookup_commit_reference(opt->repo, b))) {1174output(opt,1,_("Failed to merge submodule%s(commits not present)"), path);1175return0;1176}11771178/* check whether both changes are forward */1179if(!in_merge_bases(commit_base, commit_a) ||1180!in_merge_bases(commit_base, commit_b)) {1181output(opt,1,_("Failed to merge submodule%s(commits don't follow merge-base)"), path);1182return0;1183}11841185/* Case #1: a is contained in b or vice versa */1186if(in_merge_bases(commit_a, commit_b)) {1187oidcpy(result, b);1188if(show(opt,3)) {1189output(opt,3,_("Fast-forwarding submodule%sto the following commit:"), path);1190output_commit_title(opt, commit_b);1191}else if(show(opt,2))1192output(opt,2,_("Fast-forwarding submodule%s"), path);1193else1194;/* no output */11951196return1;1197}1198if(in_merge_bases(commit_b, commit_a)) {1199oidcpy(result, a);1200if(show(opt,3)) {1201output(opt,3,_("Fast-forwarding submodule%sto the following commit:"), path);1202output_commit_title(opt, commit_a);1203}else if(show(opt,2))1204output(opt,2,_("Fast-forwarding submodule%s"), path);1205else1206;/* no output */12071208return1;1209}12101211/*1212 * Case #2: There are one or more merges that contain a and b in1213 * the submodule. If there is only one, then present it as a1214 * suggestion to the user, but leave it marked unmerged so the1215 * user needs to confirm the resolution.1216 */12171218/* Skip the search if makes no sense to the calling context. */1219if(!search)1220return0;12211222/* find commit which merges them */1223 parent_count =find_first_merges(opt->repo, &merges, path,1224 commit_a, commit_b);1225switch(parent_count) {1226case0:1227output(opt,1,_("Failed to merge submodule%s(merge following commits not found)"), path);1228break;12291230case1:1231output(opt,1,_("Failed to merge submodule%s(not fast-forward)"), path);1232output(opt,2,_("Found a possible merge resolution for the submodule:\n"));1233print_commit((struct commit *) merges.objects[0].item);1234output(opt,2,_(1235"If this is correct simply add it to the index "1236"for example\n"1237"by using:\n\n"1238" git update-index --cacheinfo 160000%s\"%s\"\n\n"1239"which will accept this suggestion.\n"),1240oid_to_hex(&merges.objects[0].item->oid), path);1241break;12421243default:1244output(opt,1,_("Failed to merge submodule%s(multiple merges found)"), path);1245for(i =0; i < merges.nr; i++)1246print_commit((struct commit *) merges.objects[i].item);1247}12481249object_array_clear(&merges);1250return0;1251}12521253static intmerge_mode_and_contents(struct merge_options *opt,1254const struct diff_filespec *o,1255const struct diff_filespec *a,1256const struct diff_filespec *b,1257const char*filename,1258const char*branch1,1259const char*branch2,1260const int extra_marker_size,1261struct merge_file_info *result)1262{1263if(opt->branch1 != branch1) {1264/*1265 * It's weird getting a reverse merge with HEAD on the bottom1266 * side of the conflict markers and the other branch on the1267 * top. Fix that.1268 */1269returnmerge_mode_and_contents(opt, o, b, a,1270 filename,1271 branch2, branch1,1272 extra_marker_size, result);1273}12741275 result->merge =0;1276 result->clean =1;12771278if((S_IFMT & a->mode) != (S_IFMT & b->mode)) {1279 result->clean =0;1280if(S_ISREG(a->mode)) {1281 result->mode = a->mode;1282oidcpy(&result->oid, &a->oid);1283}else{1284 result->mode = b->mode;1285oidcpy(&result->oid, &b->oid);1286}1287}else{1288if(!oid_eq(&a->oid, &o->oid) && !oid_eq(&b->oid, &o->oid))1289 result->merge =1;12901291/*1292 * Merge modes1293 */1294if(a->mode == b->mode || a->mode == o->mode)1295 result->mode = b->mode;1296else{1297 result->mode = a->mode;1298if(b->mode != o->mode) {1299 result->clean =0;1300 result->merge =1;1301}1302}13031304if(oid_eq(&a->oid, &b->oid) ||oid_eq(&a->oid, &o->oid))1305oidcpy(&result->oid, &b->oid);1306else if(oid_eq(&b->oid, &o->oid))1307oidcpy(&result->oid, &a->oid);1308else if(S_ISREG(a->mode)) {1309 mmbuffer_t result_buf;1310int ret =0, merge_status;13111312 merge_status =merge_3way(opt, &result_buf, o, a, b,1313 branch1, branch2,1314 extra_marker_size);13151316if((merge_status <0) || !result_buf.ptr)1317 ret =err(opt,_("Failed to execute internal merge"));13181319if(!ret &&1320write_object_file(result_buf.ptr, result_buf.size,1321 blob_type, &result->oid))1322 ret =err(opt,_("Unable to add%sto database"),1323 a->path);13241325free(result_buf.ptr);1326if(ret)1327return ret;1328 result->clean = (merge_status ==0);1329}else if(S_ISGITLINK(a->mode)) {1330 result->clean =merge_submodule(opt, &result->oid,1331 o->path,1332&o->oid,1333&a->oid,1334&b->oid);1335}else if(S_ISLNK(a->mode)) {1336switch(opt->recursive_variant) {1337case MERGE_RECURSIVE_NORMAL:1338oidcpy(&result->oid, &a->oid);1339if(!oid_eq(&a->oid, &b->oid))1340 result->clean =0;1341break;1342case MERGE_RECURSIVE_OURS:1343oidcpy(&result->oid, &a->oid);1344break;1345case MERGE_RECURSIVE_THEIRS:1346oidcpy(&result->oid, &b->oid);1347break;1348}1349}else1350BUG("unsupported object type in the tree");1351}13521353if(result->merge)1354output(opt,2,_("Auto-merging%s"), filename);13551356return0;1357}13581359static inthandle_rename_via_dir(struct merge_options *opt,1360struct rename_conflict_info *ci)1361{1362/*1363 * Handle file adds that need to be renamed due to directory rename1364 * detection. This differs from handle_rename_normal, because1365 * there is no content merge to do; just move the file into the1366 * desired final location.1367 */1368const struct rename *ren = ci->ren1;1369const struct diff_filespec *dest = ren->pair->two;13701371if(!opt->call_depth &&would_lose_untracked(opt, dest->path)) {1372char*alt_path =unique_path(opt, dest->path, ren->branch);13731374output(opt,1,_("Error: Refusing to lose untracked file at%s; "1375"writing to%sinstead."),1376 dest->path, alt_path);1377/*1378 * Write the file in worktree at alt_path, but not in the1379 * index. Instead, write to dest->path for the index but1380 * only at the higher appropriate stage.1381 */1382if(update_file(opt,0, &dest->oid, dest->mode, alt_path))1383return-1;1384free(alt_path);1385returnupdate_stages(opt, dest->path, NULL,1386 ren->branch == opt->branch1 ? dest : NULL,1387 ren->branch == opt->branch1 ? NULL : dest);1388}13891390/* Update dest->path both in index and in worktree */1391if(update_file(opt,1, &dest->oid, dest->mode, dest->path))1392return-1;1393return0;1394}13951396static inthandle_change_delete(struct merge_options *opt,1397const char*path,const char*old_path,1398const struct object_id *o_oid,int o_mode,1399const struct object_id *changed_oid,1400int changed_mode,1401const char*change_branch,1402const char*delete_branch,1403const char*change,const char*change_past)1404{1405char*alt_path = NULL;1406const char*update_path = path;1407int ret =0;14081409if(dir_in_way(opt->repo->index, path, !opt->call_depth,0) ||1410(!opt->call_depth &&would_lose_untracked(opt, path))) {1411 update_path = alt_path =unique_path(opt, path, change_branch);1412}14131414if(opt->call_depth) {1415/*1416 * We cannot arbitrarily accept either a_sha or b_sha as1417 * correct; since there is no true "middle point" between1418 * them, simply reuse the base version for virtual merge base.1419 */1420 ret =remove_file_from_index(opt->repo->index, path);1421if(!ret)1422 ret =update_file(opt,0, o_oid, o_mode, update_path);1423}else{1424/*1425 * Despite the four nearly duplicate messages and argument1426 * lists below and the ugliness of the nested if-statements,1427 * having complete messages makes the job easier for1428 * translators.1429 *1430 * The slight variance among the cases is due to the fact1431 * that:1432 * 1) directory/file conflicts (in effect if1433 * !alt_path) could cause us to need to write the1434 * file to a different path.1435 * 2) renames (in effect if !old_path) could mean that1436 * there are two names for the path that the user1437 * may know the file by.1438 */1439if(!alt_path) {1440if(!old_path) {1441output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1442"and%sin%s. Version%sof%sleft in tree."),1443 change, path, delete_branch, change_past,1444 change_branch, change_branch, path);1445}else{1446output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1447"and%sto%sin%s. Version%sof%sleft in tree."),1448 change, old_path, delete_branch, change_past, path,1449 change_branch, change_branch, path);1450}1451}else{1452if(!old_path) {1453output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1454"and%sin%s. Version%sof%sleft in tree at%s."),1455 change, path, delete_branch, change_past,1456 change_branch, change_branch, path, alt_path);1457}else{1458output(opt,1,_("CONFLICT (%s/delete):%sdeleted in%s"1459"and%sto%sin%s. Version%sof%sleft in tree at%s."),1460 change, old_path, delete_branch, change_past, path,1461 change_branch, change_branch, path, alt_path);1462}1463}1464/*1465 * No need to call update_file() on path when change_branch ==1466 * opt->branch1 && !alt_path, since that would needlessly touch1467 * path. We could call update_file_flags() with update_cache=01468 * and update_wd=0, but that's a no-op.1469 */1470if(change_branch != opt->branch1 || alt_path)1471 ret =update_file(opt,0, changed_oid, changed_mode, update_path);1472}1473free(alt_path);14741475return ret;1476}14771478static inthandle_rename_delete(struct merge_options *opt,1479struct rename_conflict_info *ci)1480{1481const struct rename *ren = ci->ren1;1482const struct diff_filespec *orig = ren->pair->one;1483const struct diff_filespec *dest = ren->pair->two;1484const char*rename_branch = ren->branch;1485const char*delete_branch = (opt->branch1 == ren->branch ?1486 opt->branch2 : opt->branch1);14871488if(handle_change_delete(opt,1489 opt->call_depth ? orig->path : dest->path,1490 opt->call_depth ? NULL : orig->path,1491&orig->oid, orig->mode,1492&dest->oid, dest->mode,1493 rename_branch, delete_branch,1494_("rename"),_("renamed")))1495return-1;14961497if(opt->call_depth)1498returnremove_file_from_index(opt->repo->index, dest->path);1499else1500returnupdate_stages(opt, dest->path, NULL,1501 rename_branch == opt->branch1 ? dest : NULL,1502 rename_branch == opt->branch1 ? NULL : dest);1503}15041505static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1506struct stage_data *entry,1507int stage)1508{1509struct object_id *oid = &entry->stages[stage].oid;1510unsigned mode = entry->stages[stage].mode;1511if(mode ==0||is_null_oid(oid))1512return NULL;1513oidcpy(&target->oid, oid);1514 target->mode = mode;1515return target;1516}15171518static inthandle_file_collision(struct merge_options *opt,1519const char*collide_path,1520const char*prev_path1,1521const char*prev_path2,1522const char*branch1,const char*branch2,1523const struct object_id *a_oid,1524unsigned int a_mode,1525const struct object_id *b_oid,1526unsigned int b_mode)1527{1528struct merge_file_info mfi;1529struct diff_filespec null, a, b;1530char*alt_path = NULL;1531const char*update_path = collide_path;15321533/*1534 * It's easiest to get the correct things into stage 2 and 3, and1535 * to make sure that the content merge puts HEAD before the other1536 * branch if we just ensure that branch1 == opt->branch1. So, simply1537 * flip arguments around if we don't have that.1538 */1539if(branch1 != opt->branch1) {1540returnhandle_file_collision(opt, collide_path,1541 prev_path2, prev_path1,1542 branch2, branch1,1543 b_oid, b_mode,1544 a_oid, a_mode);1545}15461547/*1548 * In the recursive case, we just opt to undo renames1549 */1550if(opt->call_depth && (prev_path1 || prev_path2)) {1551/* Put first file (a_oid, a_mode) in its original spot */1552if(prev_path1) {1553if(update_file(opt,1, a_oid, a_mode, prev_path1))1554return-1;1555}else{1556if(update_file(opt,1, a_oid, a_mode, collide_path))1557return-1;1558}15591560/* Put second file (b_oid, b_mode) in its original spot */1561if(prev_path2) {1562if(update_file(opt,1, b_oid, b_mode, prev_path2))1563return-1;1564}else{1565if(update_file(opt,1, b_oid, b_mode, collide_path))1566return-1;1567}15681569/* Don't leave something at collision path if unrenaming both */1570if(prev_path1 && prev_path2)1571remove_file(opt,1, collide_path,0);15721573return0;1574}15751576/* Remove rename sources if rename/add or rename/rename(2to1) */1577if(prev_path1)1578remove_file(opt,1, prev_path1,1579 opt->call_depth ||would_lose_untracked(opt, prev_path1));1580if(prev_path2)1581remove_file(opt,1, prev_path2,1582 opt->call_depth ||would_lose_untracked(opt, prev_path2));15831584/*1585 * Remove the collision path, if it wouldn't cause dirty contents1586 * or an untracked file to get lost. We'll either overwrite with1587 * merged contents, or just write out to differently named files.1588 */1589if(was_dirty(opt, collide_path)) {1590output(opt,1,_("Refusing to lose dirty file at%s"),1591 collide_path);1592 update_path = alt_path =unique_path(opt, collide_path,"merged");1593}else if(would_lose_untracked(opt, collide_path)) {1594/*1595 * Only way we get here is if both renames were from1596 * a directory rename AND user had an untracked file1597 * at the location where both files end up after the1598 * two directory renames. See testcase 10d of t6043.1599 */1600output(opt,1,_("Refusing to lose untracked file at "1601"%s, even though it's in the way."),1602 collide_path);1603 update_path = alt_path =unique_path(opt, collide_path,"merged");1604}else{1605/*1606 * FIXME: It's possible that the two files are identical1607 * and that the current working copy happens to match, in1608 * which case we are unnecessarily touching the working1609 * tree file. It's not a likely enough scenario that I1610 * want to code up the checks for it and a better fix is1611 * available if we restructure how unpack_trees() and1612 * merge-recursive interoperate anyway, so punting for1613 * now...1614 */1615remove_file(opt,0, collide_path,0);1616}16171618/* Store things in diff_filespecs for functions that need it */1619memset(&a,0,sizeof(struct diff_filespec));1620memset(&b,0,sizeof(struct diff_filespec));1621 null.path = a.path = b.path = (char*)collide_path;1622oidcpy(&null.oid, &null_oid);1623 null.mode =0;1624oidcpy(&a.oid, a_oid);1625 a.mode = a_mode;1626 a.oid_valid =1;1627oidcpy(&b.oid, b_oid);1628 b.mode = b_mode;1629 b.oid_valid =1;16301631if(merge_mode_and_contents(opt, &null, &a, &b, collide_path,1632 branch1, branch2, opt->call_depth *2, &mfi))1633return-1;1634 mfi.clean &= !alt_path;1635if(update_file(opt, mfi.clean, &mfi.oid, mfi.mode, update_path))1636return-1;1637if(!mfi.clean && !opt->call_depth &&1638update_stages(opt, collide_path, NULL, &a, &b))1639return-1;1640free(alt_path);1641/*1642 * FIXME: If both a & b both started with conflicts (only possible1643 * if they came from a rename/rename(2to1)), but had IDENTICAL1644 * contents including those conflicts, then in the next line we claim1645 * it was clean. If someone cares about this case, we should have the1646 * caller notify us if we started with conflicts.1647 */1648return mfi.clean;1649}16501651static inthandle_rename_add(struct merge_options *opt,1652struct rename_conflict_info *ci)1653{1654/* a was renamed to c, and a separate c was added. */1655struct diff_filespec *a = ci->ren1->pair->one;1656struct diff_filespec *c = ci->ren1->pair->two;1657struct diff_filespec tmp;1658char*path = c->path;1659char*prev_path_desc;1660struct merge_file_info mfi;16611662const char*rename_branch = ci->ren1->branch;1663const char*add_branch = (opt->branch1 == rename_branch ?1664 opt->branch2 : opt->branch1);1665int other_stage = (ci->ren1->branch == opt->branch1 ?3:2);16661667output(opt,1,_("CONFLICT (rename/add): "1668"Rename%s->%sin%s. Added%sin%s"),1669 a->path, c->path, rename_branch,1670 c->path, add_branch);16711672filespec_from_entry(&tmp, ci->ren1->src_entry, other_stage);1673 tmp.path = a->path;16741675 prev_path_desc =xstrfmt("version of%sfrom%s", path, a->path);1676if(merge_mode_and_contents(opt, a, c, &tmp,1677 prev_path_desc,1678 opt->branch1, opt->branch2,16791+ opt->call_depth *2, &mfi))1680return-1;1681free(prev_path_desc);16821683returnhandle_file_collision(opt,1684 c->path, a->path, NULL,1685 rename_branch, add_branch,1686&mfi.oid, mfi.mode,1687&ci->ren1->dst_entry->stages[other_stage].oid,1688 ci->ren1->dst_entry->stages[other_stage].mode);1689}16901691static char*find_path_for_conflict(struct merge_options *opt,1692const char*path,1693const char*branch1,1694const char*branch2)1695{1696char*new_path = NULL;1697if(dir_in_way(opt->repo->index, path, !opt->call_depth,0)) {1698 new_path =unique_path(opt, path, branch1);1699output(opt,1,_("%sis a directory in%sadding "1700"as%sinstead"),1701 path, branch2, new_path);1702}else if(would_lose_untracked(opt, path)) {1703 new_path =unique_path(opt, path, branch1);1704output(opt,1,_("Refusing to lose untracked file"1705" at%s; adding as%sinstead"),1706 path, new_path);1707}17081709return new_path;1710}17111712static inthandle_rename_rename_1to2(struct merge_options *opt,1713struct rename_conflict_info *ci)1714{1715/* One file was renamed in both branches, but to different names. */1716struct merge_file_info mfi;1717struct diff_filespec other;1718struct diff_filespec *add;1719struct diff_filespec *o = ci->ren1->pair->one;1720struct diff_filespec *a = ci->ren1->pair->two;1721struct diff_filespec *b = ci->ren2->pair->two;1722char*path_desc;17231724output(opt,1,_("CONFLICT (rename/rename): "1725"Rename\"%s\"->\"%s\"in branch\"%s\""1726"rename\"%s\"->\"%s\"in\"%s\"%s"),1727 o->path, a->path, ci->ren1->branch,1728 o->path, b->path, ci->ren2->branch,1729 opt->call_depth ?_(" (left unresolved)") :"");17301731 path_desc =xstrfmt("%sand%s, both renamed from%s",1732 a->path, b->path, o->path);1733if(merge_mode_and_contents(opt, o, a, b, path_desc,1734 ci->ren1->branch, ci->ren2->branch,1735 opt->call_depth *2, &mfi))1736return-1;1737free(path_desc);17381739if(opt->call_depth) {1740/*1741 * FIXME: For rename/add-source conflicts (if we could detect1742 * such), this is wrong. We should instead find a unique1743 * pathname and then either rename the add-source file to that1744 * unique path, or use that unique path instead of src here.1745 */1746if(update_file(opt,0, &mfi.oid, mfi.mode, o->path))1747return-1;17481749/*1750 * Above, we put the merged content at the merge-base's1751 * path. Now we usually need to delete both a->path and1752 * b->path. However, the rename on each side of the merge1753 * could also be involved in a rename/add conflict. In1754 * such cases, we should keep the added file around,1755 * resolving the conflict at that path in its favor.1756 */1757 add =filespec_from_entry(&other, ci->ren1->dst_entry,2^1);1758if(add) {1759if(update_file(opt,0, &add->oid, add->mode, a->path))1760return-1;1761}1762else1763remove_file_from_index(opt->repo->index, a->path);1764 add =filespec_from_entry(&other, ci->ren2->dst_entry,3^1);1765if(add) {1766if(update_file(opt,0, &add->oid, add->mode, b->path))1767return-1;1768}1769else1770remove_file_from_index(opt->repo->index, b->path);1771}else{1772/*1773 * For each destination path, we need to see if there is a1774 * rename/add collision. If not, we can write the file out1775 * to the specified location.1776 */1777 add =filespec_from_entry(&other, ci->ren1->dst_entry,2^1);1778if(add) {1779if(handle_file_collision(opt, a->path,1780 NULL, NULL,1781 ci->ren1->branch,1782 ci->ren2->branch,1783&mfi.oid, mfi.mode,1784&add->oid, add->mode) <0)1785return-1;1786}else{1787char*new_path =find_path_for_conflict(opt, a->path,1788 ci->ren1->branch,1789 ci->ren2->branch);1790if(update_file(opt,0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))1791return-1;1792free(new_path);1793if(update_stages(opt, a->path, NULL, a, NULL))1794return-1;1795}17961797 add =filespec_from_entry(&other, ci->ren2->dst_entry,3^1);1798if(add) {1799if(handle_file_collision(opt, b->path,1800 NULL, NULL,1801 ci->ren1->branch,1802 ci->ren2->branch,1803&add->oid, add->mode,1804&mfi.oid, mfi.mode) <0)1805return-1;1806}else{1807char*new_path =find_path_for_conflict(opt, b->path,1808 ci->ren2->branch,1809 ci->ren1->branch);1810if(update_file(opt,0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))1811return-1;1812free(new_path);1813if(update_stages(opt, b->path, NULL, NULL, b))1814return-1;1815}1816}18171818return0;1819}18201821static inthandle_rename_rename_2to1(struct merge_options *opt,1822struct rename_conflict_info *ci)1823{1824/* Two files, a & b, were renamed to the same thing, c. */1825struct diff_filespec *a = ci->ren1->pair->one;1826struct diff_filespec *b = ci->ren2->pair->one;1827struct diff_filespec *c1 = ci->ren1->pair->two;1828struct diff_filespec *c2 = ci->ren2->pair->two;1829struct diff_filespec tmp1, tmp2;1830char*path = c1->path;/* == c2->path */1831char*path_side_1_desc;1832char*path_side_2_desc;1833struct merge_file_info mfi_c1;1834struct merge_file_info mfi_c2;18351836output(opt,1,_("CONFLICT (rename/rename): "1837"Rename%s->%sin%s. "1838"Rename%s->%sin%s"),1839 a->path, c1->path, ci->ren1->branch,1840 b->path, c2->path, ci->ren2->branch);18411842filespec_from_entry(&tmp1, ci->ren1->src_entry,3);1843 tmp1.path = a->path;1844filespec_from_entry(&tmp2, ci->ren2->src_entry,2);1845 tmp2.path = b->path;18461847 path_side_1_desc =xstrfmt("version of%sfrom%s", path, a->path);1848 path_side_2_desc =xstrfmt("version of%sfrom%s", path, b->path);1849if(merge_mode_and_contents(opt, a, c1, &tmp1, path_side_1_desc,1850 opt->branch1, opt->branch2,18511+ opt->call_depth *2, &mfi_c1) ||1852merge_mode_and_contents(opt, b, &tmp2, c2, path_side_2_desc,1853 opt->branch1, opt->branch2,18541+ opt->call_depth *2, &mfi_c2))1855return-1;1856free(path_side_1_desc);1857free(path_side_2_desc);18581859returnhandle_file_collision(opt, path, a->path, b->path,1860 ci->ren1->branch, ci->ren2->branch,1861&mfi_c1.oid, mfi_c1.mode,1862&mfi_c2.oid, mfi_c2.mode);1863}18641865/*1866 * Get the diff_filepairs changed between o_tree and tree.1867 */1868static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,1869struct tree *o_tree,1870struct tree *tree)1871{1872struct diff_queue_struct *ret;1873struct diff_options opts;18741875repo_diff_setup(opt->repo, &opts);1876 opts.flags.recursive =1;1877 opts.flags.rename_empty =0;1878 opts.detect_rename =merge_detect_rename(opt);1879/*1880 * We do not have logic to handle the detection of copies. In1881 * fact, it may not even make sense to add such logic: would we1882 * really want a change to a base file to be propagated through1883 * multiple other files by a merge?1884 */1885if(opts.detect_rename > DIFF_DETECT_RENAME)1886 opts.detect_rename = DIFF_DETECT_RENAME;1887 opts.rename_limit = opt->merge_rename_limit >=0? opt->merge_rename_limit :1888 opt->diff_rename_limit >=0? opt->diff_rename_limit :18891000;1890 opts.rename_score = opt->rename_score;1891 opts.show_rename_progress = opt->show_rename_progress;1892 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1893diff_setup_done(&opts);1894diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1895diffcore_std(&opts);1896if(opts.needed_rename_limit > opt->needed_rename_limit)1897 opt->needed_rename_limit = opts.needed_rename_limit;18981899 ret =xmalloc(sizeof(*ret));1900*ret = diff_queued_diff;19011902 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1903 diff_queued_diff.nr =0;1904 diff_queued_diff.queue = NULL;1905diff_flush(&opts);1906return ret;1907}19081909static inttree_has_path(struct tree *tree,const char*path)1910{1911struct object_id hashy;1912unsigned short mode_o;19131914return!get_tree_entry(&tree->object.oid, path,1915&hashy, &mode_o);1916}19171918/*1919 * Return a new string that replaces the beginning portion (which matches1920 * entry->dir), with entry->new_dir. In perl-speak:1921 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1922 * NOTE:1923 * Caller must ensure that old_path starts with entry->dir + '/'.1924 */1925static char*apply_dir_rename(struct dir_rename_entry *entry,1926const char*old_path)1927{1928struct strbuf new_path = STRBUF_INIT;1929int oldlen, newlen;19301931if(entry->non_unique_new_dir)1932return NULL;19331934 oldlen =strlen(entry->dir);1935 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1936strbuf_grow(&new_path, newlen);1937strbuf_addbuf(&new_path, &entry->new_dir);1938strbuf_addstr(&new_path, &old_path[oldlen]);19391940returnstrbuf_detach(&new_path, NULL);1941}19421943static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1944char**old_dir,char**new_dir)1945{1946char*end_of_old, *end_of_new;1947int old_len, new_len;19481949*old_dir = NULL;1950*new_dir = NULL;19511952/*1953 * For1954 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1955 * the "e/foo.c" part is the same, we just want to know that1956 * "a/b/c/d" was renamed to "a/b/some/thing/else"1957 * so, for this example, this function returns "a/b/c/d" in1958 * *old_dir and "a/b/some/thing/else" in *new_dir.1959 *1960 * Also, if the basename of the file changed, we don't care. We1961 * want to know which portion of the directory, if any, changed.1962 */1963 end_of_old =strrchr(old_path,'/');1964 end_of_new =strrchr(new_path,'/');19651966if(end_of_old == NULL || end_of_new == NULL)1967return;1968while(*--end_of_new == *--end_of_old &&1969 end_of_old != old_path &&1970 end_of_new != new_path)1971;/* Do nothing; all in the while loop */1972/*1973 * We've found the first non-matching character in the directory1974 * paths. That means the current directory we were comparing1975 * represents the rename. Move end_of_old and end_of_new back1976 * to the full directory name.1977 */1978if(*end_of_old =='/')1979 end_of_old++;1980if(*end_of_old !='/')1981 end_of_new++;1982 end_of_old =strchr(end_of_old,'/');1983 end_of_new =strchr(end_of_new,'/');19841985/*1986 * It may have been the case that old_path and new_path were the same1987 * directory all along. Don't claim a rename if they're the same.1988 */1989 old_len = end_of_old - old_path;1990 new_len = end_of_new - new_path;19911992if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {1993*old_dir =xstrndup(old_path, old_len);1994*new_dir =xstrndup(new_path, new_len);1995}1996}19971998static voidremove_hashmap_entries(struct hashmap *dir_renames,1999struct string_list *items_to_remove)2000{2001int i;2002struct dir_rename_entry *entry;20032004for(i =0; i < items_to_remove->nr; i++) {2005 entry = items_to_remove->items[i].util;2006hashmap_remove(dir_renames, entry, NULL);2007}2008string_list_clear(items_to_remove,0);2009}20102011/*2012 * See if there is a directory rename for path, and if there are any file2013 * level conflicts for the renamed location. If there is a rename and2014 * there are no conflicts, return the new name. Otherwise, return NULL.2015 */2016static char*handle_path_level_conflicts(struct merge_options *opt,2017const char*path,2018struct dir_rename_entry *entry,2019struct hashmap *collisions,2020struct tree *tree)2021{2022char*new_path = NULL;2023struct collision_entry *collision_ent;2024int clean =1;2025struct strbuf collision_paths = STRBUF_INIT;20262027/*2028 * entry has the mapping of old directory name to new directory name2029 * that we want to apply to path.2030 */2031 new_path =apply_dir_rename(entry, path);20322033if(!new_path) {2034/* This should only happen when entry->non_unique_new_dir set */2035if(!entry->non_unique_new_dir)2036BUG("entry->non_unqiue_dir not set and !new_path");2037output(opt,1,_("CONFLICT (directory rename split): "2038"Unclear where to place%sbecause directory "2039"%swas renamed to multiple other directories, "2040"with no destination getting a majority of the "2041"files."),2042 path, entry->dir);2043 clean =0;2044return NULL;2045}20462047/*2048 * The caller needs to have ensured that it has pre-populated2049 * collisions with all paths that map to new_path. Do a quick check2050 * to ensure that's the case.2051 */2052 collision_ent =collision_find_entry(collisions, new_path);2053if(collision_ent == NULL)2054BUG("collision_ent is NULL");20552056/*2057 * Check for one-sided add/add/.../add conflicts, i.e.2058 * where implicit renames from the other side doing2059 * directory rename(s) can affect this side of history2060 * to put multiple paths into the same location. Warn2061 * and bail on directory renames for such paths.2062 */2063if(collision_ent->reported_already) {2064 clean =0;2065}else if(tree_has_path(tree, new_path)) {2066 collision_ent->reported_already =1;2067strbuf_add_separated_string_list(&collision_paths,", ",2068&collision_ent->source_files);2069output(opt,1,_("CONFLICT (implicit dir rename): Existing "2070"file/dir at%sin the way of implicit "2071"directory rename(s) putting the following "2072"path(s) there:%s."),2073 new_path, collision_paths.buf);2074 clean =0;2075}else if(collision_ent->source_files.nr >1) {2076 collision_ent->reported_already =1;2077strbuf_add_separated_string_list(&collision_paths,", ",2078&collision_ent->source_files);2079output(opt,1,_("CONFLICT (implicit dir rename): Cannot map "2080"more than one path to%s; implicit directory "2081"renames tried to put these paths there:%s"),2082 new_path, collision_paths.buf);2083 clean =0;2084}20852086/* Free memory we no longer need */2087strbuf_release(&collision_paths);2088if(!clean && new_path) {2089free(new_path);2090return NULL;2091}20922093return new_path;2094}20952096/*2097 * There are a couple things we want to do at the directory level:2098 * 1. Check for both sides renaming to the same thing, in order to avoid2099 * implicit renaming of files that should be left in place. (See2100 * testcase 6b in t6043 for details.)2101 * 2. Prune directory renames if there are still files left in the2102 * the original directory. These represent a partial directory rename,2103 * i.e. a rename where only some of the files within the directory2104 * were renamed elsewhere. (Technically, this could be done earlier2105 * in get_directory_renames(), except that would prevent us from2106 * doing the previous check and thus failing testcase 6b.)2107 * 3. Check for rename/rename(1to2) conflicts (at the directory level).2108 * In the future, we could potentially record this info as well and2109 * omit reporting rename/rename(1to2) conflicts for each path within2110 * the affected directories, thus cleaning up the merge output.2111 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the2112 * directory level, because merging directories is fine. If it2113 * causes conflicts for files within those merged directories, then2114 * that should be detected at the individual path level.2115 */2116static voidhandle_directory_level_conflicts(struct merge_options *opt,2117struct hashmap *dir_re_head,2118struct tree *head,2119struct hashmap *dir_re_merge,2120struct tree *merge)2121{2122struct hashmap_iter iter;2123struct dir_rename_entry *head_ent;2124struct dir_rename_entry *merge_ent;21252126struct string_list remove_from_head = STRING_LIST_INIT_NODUP;2127struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;21282129hashmap_iter_init(dir_re_head, &iter);2130while((head_ent =hashmap_iter_next(&iter))) {2131 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);2132if(merge_ent &&2133!head_ent->non_unique_new_dir &&2134!merge_ent->non_unique_new_dir &&2135!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {2136/* 1. Renamed identically; remove it from both sides */2137string_list_append(&remove_from_head,2138 head_ent->dir)->util = head_ent;2139strbuf_release(&head_ent->new_dir);2140string_list_append(&remove_from_merge,2141 merge_ent->dir)->util = merge_ent;2142strbuf_release(&merge_ent->new_dir);2143}else if(tree_has_path(head, head_ent->dir)) {2144/* 2. This wasn't a directory rename after all */2145string_list_append(&remove_from_head,2146 head_ent->dir)->util = head_ent;2147strbuf_release(&head_ent->new_dir);2148}2149}21502151remove_hashmap_entries(dir_re_head, &remove_from_head);2152remove_hashmap_entries(dir_re_merge, &remove_from_merge);21532154hashmap_iter_init(dir_re_merge, &iter);2155while((merge_ent =hashmap_iter_next(&iter))) {2156 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);2157if(tree_has_path(merge, merge_ent->dir)) {2158/* 2. This wasn't a directory rename after all */2159string_list_append(&remove_from_merge,2160 merge_ent->dir)->util = merge_ent;2161}else if(head_ent &&2162!head_ent->non_unique_new_dir &&2163!merge_ent->non_unique_new_dir) {2164/* 3. rename/rename(1to2) */2165/*2166 * We can assume it's not rename/rename(1to1) because2167 * that was case (1), already checked above. So we2168 * know that head_ent->new_dir and merge_ent->new_dir2169 * are different strings.2170 */2171output(opt,1,_("CONFLICT (rename/rename): "2172"Rename directory%s->%sin%s. "2173"Rename directory%s->%sin%s"),2174 head_ent->dir, head_ent->new_dir.buf, opt->branch1,2175 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);2176string_list_append(&remove_from_head,2177 head_ent->dir)->util = head_ent;2178strbuf_release(&head_ent->new_dir);2179string_list_append(&remove_from_merge,2180 merge_ent->dir)->util = merge_ent;2181strbuf_release(&merge_ent->new_dir);2182}2183}21842185remove_hashmap_entries(dir_re_head, &remove_from_head);2186remove_hashmap_entries(dir_re_merge, &remove_from_merge);2187}21882189static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)2190{2191struct hashmap *dir_renames;2192struct hashmap_iter iter;2193struct dir_rename_entry *entry;2194int i;21952196/*2197 * Typically, we think of a directory rename as all files from a2198 * certain directory being moved to a target directory. However,2199 * what if someone first moved two files from the original2200 * directory in one commit, and then renamed the directory2201 * somewhere else in a later commit? At merge time, we just know2202 * that files from the original directory went to two different2203 * places, and that the bulk of them ended up in the same place.2204 * We want each directory rename to represent where the bulk of the2205 * files from that directory end up; this function exists to find2206 * where the bulk of the files went.2207 *2208 * The first loop below simply iterates through the list of file2209 * renames, finding out how often each directory rename pair2210 * possibility occurs.2211 */2212 dir_renames =xmalloc(sizeof(*dir_renames));2213dir_rename_init(dir_renames);2214for(i =0; i < pairs->nr; ++i) {2215struct string_list_item *item;2216int*count;2217struct diff_filepair *pair = pairs->queue[i];2218char*old_dir, *new_dir;22192220/* File not part of directory rename if it wasn't renamed */2221if(pair->status !='R')2222continue;22232224get_renamed_dir_portion(pair->one->path, pair->two->path,2225&old_dir, &new_dir);2226if(!old_dir)2227/* Directory didn't change at all; ignore this one. */2228continue;22292230 entry =dir_rename_find_entry(dir_renames, old_dir);2231if(!entry) {2232 entry =xmalloc(sizeof(*entry));2233dir_rename_entry_init(entry, old_dir);2234hashmap_put(dir_renames, entry);2235}else{2236free(old_dir);2237}2238 item =string_list_lookup(&entry->possible_new_dirs, new_dir);2239if(!item) {2240 item =string_list_insert(&entry->possible_new_dirs,2241 new_dir);2242 item->util =xcalloc(1,sizeof(int));2243}else{2244free(new_dir);2245}2246 count = item->util;2247*count +=1;2248}22492250/*2251 * For each directory with files moved out of it, we find out which2252 * target directory received the most files so we can declare it to2253 * be the "winning" target location for the directory rename. This2254 * winner gets recorded in new_dir. If there is no winner2255 * (multiple target directories received the same number of files),2256 * we set non_unique_new_dir. Once we've determined the winner (or2257 * that there is no winner), we no longer need possible_new_dirs.2258 */2259hashmap_iter_init(dir_renames, &iter);2260while((entry =hashmap_iter_next(&iter))) {2261int max =0;2262int bad_max =0;2263char*best = NULL;22642265for(i =0; i < entry->possible_new_dirs.nr; i++) {2266int*count = entry->possible_new_dirs.items[i].util;22672268if(*count == max)2269 bad_max = max;2270else if(*count > max) {2271 max = *count;2272 best = entry->possible_new_dirs.items[i].string;2273}2274}2275if(bad_max == max)2276 entry->non_unique_new_dir =1;2277else{2278assert(entry->new_dir.len ==0);2279strbuf_addstr(&entry->new_dir, best);2280}2281/*2282 * The relevant directory sub-portion of the original full2283 * filepaths were xstrndup'ed before inserting into2284 * possible_new_dirs, and instead of manually iterating the2285 * list and free'ing each, just lie and tell2286 * possible_new_dirs that it did the strdup'ing so that it2287 * will free them for us.2288 */2289 entry->possible_new_dirs.strdup_strings =1;2290string_list_clear(&entry->possible_new_dirs,1);2291}22922293return dir_renames;2294}22952296static struct dir_rename_entry *check_dir_renamed(const char*path,2297struct hashmap *dir_renames)2298{2299char*temp =xstrdup(path);2300char*end;2301struct dir_rename_entry *entry = NULL;23022303while((end =strrchr(temp,'/'))) {2304*end ='\0';2305 entry =dir_rename_find_entry(dir_renames, temp);2306if(entry)2307break;2308}2309free(temp);2310return entry;2311}23122313static voidcompute_collisions(struct hashmap *collisions,2314struct hashmap *dir_renames,2315struct diff_queue_struct *pairs)2316{2317int i;23182319/*2320 * Multiple files can be mapped to the same path due to directory2321 * renames done by the other side of history. Since that other2322 * side of history could have merged multiple directories into one,2323 * if our side of history added the same file basename to each of2324 * those directories, then all N of them would get implicitly2325 * renamed by the directory rename detection into the same path,2326 * and we'd get an add/add/.../add conflict, and all those adds2327 * from *this* side of history. This is not representable in the2328 * index, and users aren't going to easily be able to make sense of2329 * it. So we need to provide a good warning about what's2330 * happening, and fall back to no-directory-rename detection2331 * behavior for those paths.2332 *2333 * See testcases 9e and all of section 5 from t6043 for examples.2334 */2335collision_init(collisions);23362337for(i =0; i < pairs->nr; ++i) {2338struct dir_rename_entry *dir_rename_ent;2339struct collision_entry *collision_ent;2340char*new_path;2341struct diff_filepair *pair = pairs->queue[i];23422343if(pair->status !='A'&& pair->status !='R')2344continue;2345 dir_rename_ent =check_dir_renamed(pair->two->path,2346 dir_renames);2347if(!dir_rename_ent)2348continue;23492350 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);2351if(!new_path)2352/*2353 * dir_rename_ent->non_unique_new_path is true, which2354 * means there is no directory rename for us to use,2355 * which means it won't cause us any additional2356 * collisions.2357 */2358continue;2359 collision_ent =collision_find_entry(collisions, new_path);2360if(!collision_ent) {2361 collision_ent =xcalloc(1,2362sizeof(struct collision_entry));2363hashmap_entry_init(collision_ent,strhash(new_path));2364hashmap_put(collisions, collision_ent);2365 collision_ent->target_file = new_path;2366}else{2367free(new_path);2368}2369string_list_insert(&collision_ent->source_files,2370 pair->two->path);2371}2372}23732374static char*check_for_directory_rename(struct merge_options *opt,2375const char*path,2376struct tree *tree,2377struct hashmap *dir_renames,2378struct hashmap *dir_rename_exclusions,2379struct hashmap *collisions,2380int*clean_merge)2381{2382char*new_path = NULL;2383struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2384struct dir_rename_entry *oentry = NULL;23852386if(!entry)2387return new_path;23882389/*2390 * This next part is a little weird. We do not want to do an2391 * implicit rename into a directory we renamed on our side, because2392 * that will result in a spurious rename/rename(1to2) conflict. An2393 * example:2394 * Base commit: dumbdir/afile, otherdir/bfile2395 * Side 1: smrtdir/afile, otherdir/bfile2396 * Side 2: dumbdir/afile, dumbdir/bfile2397 * Here, while working on Side 1, we could notice that otherdir was2398 * renamed/merged to dumbdir, and change the diff_filepair for2399 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2400 * 2 will notice the rename from dumbdir to smrtdir, and do the2401 * transitive rename to move it from dumbdir/bfile to2402 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2403 * smrtdir, a rename/rename(1to2) conflict. We really just want2404 * the file to end up in smrtdir. And the way to achieve that is2405 * to not let Side1 do the rename to dumbdir, since we know that is2406 * the source of one of our directory renames.2407 *2408 * That's why oentry and dir_rename_exclusions is here.2409 *2410 * As it turns out, this also prevents N-way transient rename2411 * confusion; See testcases 9c and 9d of t6043.2412 */2413 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2414if(oentry) {2415output(opt,1,_("WARNING: Avoiding applying%s->%srename "2416"to%s, because%sitself was renamed."),2417 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2418}else{2419 new_path =handle_path_level_conflicts(opt, path, entry,2420 collisions, tree);2421*clean_merge &= (new_path != NULL);2422}24232424return new_path;2425}24262427static voidapply_directory_rename_modifications(struct merge_options *opt,2428struct diff_filepair *pair,2429char*new_path,2430struct rename *re,2431struct tree *tree,2432struct tree *o_tree,2433struct tree *a_tree,2434struct tree *b_tree,2435struct string_list *entries)2436{2437struct string_list_item *item;2438int stage = (tree == a_tree ?2:3);2439int update_wd;24402441/*2442 * In all cases where we can do directory rename detection,2443 * unpack_trees() will have read pair->two->path into the2444 * index and the working copy. We need to remove it so that2445 * we can instead place it at new_path. It is guaranteed to2446 * not be untracked (unpack_trees() would have errored out2447 * saying the file would have been overwritten), but it might2448 * be dirty, though.2449 */2450 update_wd = !was_dirty(opt, pair->two->path);2451if(!update_wd)2452output(opt,1,_("Refusing to lose dirty file at%s"),2453 pair->two->path);2454remove_file(opt,1, pair->two->path, !update_wd);24552456/* Find or create a new re->dst_entry */2457 item =string_list_lookup(entries, new_path);2458if(item) {2459/*2460 * Since we're renaming on this side of history, and it's2461 * due to a directory rename on the other side of history2462 * (which we only allow when the directory in question no2463 * longer exists on the other side of history), the2464 * original entry for re->dst_entry is no longer2465 * necessary...2466 */2467 re->dst_entry->processed =1;24682469/*2470 * ...because we'll be using this new one.2471 */2472 re->dst_entry = item->util;2473}else{2474/*2475 * re->dst_entry is for the before-dir-rename path, and we2476 * need it to hold information for the after-dir-rename2477 * path. Before creating a new entry, we need to mark the2478 * old one as unnecessary (...unless it is shared by2479 * src_entry, i.e. this didn't use to be a rename, in which2480 * case we can just allow the normal processing to happen2481 * for it).2482 */2483if(pair->status =='R')2484 re->dst_entry->processed =1;24852486 re->dst_entry =insert_stage_data(new_path,2487 o_tree, a_tree, b_tree,2488 entries);2489 item =string_list_insert(entries, new_path);2490 item->util = re->dst_entry;2491}24922493/*2494 * Update the stage_data with the information about the path we are2495 * moving into place. That slot will be empty and available for us2496 * to write to because of the collision checks in2497 * handle_path_level_conflicts(). In other words,2498 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2499 * open for us to write to.2500 *2501 * It may be tempting to actually update the index at this point as2502 * well, using update_stages_for_stage_data(), but as per the big2503 * "NOTE" in update_stages(), doing so will modify the current2504 * in-memory index which will break calls to would_lose_untracked()2505 * that we need to make. Instead, we need to just make sure that2506 * the various handle_rename_*() functions update the index2507 * explicitly rather than relying on unpack_trees() to have done it.2508 */2509get_tree_entry(&tree->object.oid,2510 pair->two->path,2511&re->dst_entry->stages[stage].oid,2512&re->dst_entry->stages[stage].mode);25132514/* Update pair status */2515if(pair->status =='A') {2516/*2517 * Recording rename information for this add makes it look2518 * like a rename/delete conflict. Make sure we can2519 * correctly handle this as an add that was moved to a new2520 * directory instead of reporting a rename/delete conflict.2521 */2522 re->add_turned_into_rename =1;2523}2524/*2525 * We don't actually look at pair->status again, but it seems2526 * pedagogically correct to adjust it.2527 */2528 pair->status ='R';25292530/*2531 * Finally, record the new location.2532 */2533 pair->two->path = new_path;2534}25352536/*2537 * Get information of all renames which occurred in 'pairs', making use of2538 * any implicit directory renames inferred from the other side of history.2539 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2540 * to be able to associate the correct cache entries with the rename2541 * information; tree is always equal to either a_tree or b_tree.2542 */2543static struct string_list *get_renames(struct merge_options *opt,2544const char*branch,2545struct diff_queue_struct *pairs,2546struct hashmap *dir_renames,2547struct hashmap *dir_rename_exclusions,2548struct tree *tree,2549struct tree *o_tree,2550struct tree *a_tree,2551struct tree *b_tree,2552struct string_list *entries,2553int*clean_merge)2554{2555int i;2556struct hashmap collisions;2557struct hashmap_iter iter;2558struct collision_entry *e;2559struct string_list *renames;25602561compute_collisions(&collisions, dir_renames, pairs);2562 renames =xcalloc(1,sizeof(struct string_list));25632564for(i =0; i < pairs->nr; ++i) {2565struct string_list_item *item;2566struct rename *re;2567struct diff_filepair *pair = pairs->queue[i];2568char*new_path;/* non-NULL only with directory renames */25692570if(pair->status !='A'&& pair->status !='R') {2571diff_free_filepair(pair);2572continue;2573}2574 new_path =check_for_directory_rename(opt, pair->two->path, tree,2575 dir_renames,2576 dir_rename_exclusions,2577&collisions,2578 clean_merge);2579if(pair->status !='R'&& !new_path) {2580diff_free_filepair(pair);2581continue;2582}25832584 re =xmalloc(sizeof(*re));2585 re->processed =0;2586 re->add_turned_into_rename =0;2587 re->pair = pair;2588 re->branch = branch;2589 item =string_list_lookup(entries, re->pair->one->path);2590if(!item)2591 re->src_entry =insert_stage_data(re->pair->one->path,2592 o_tree, a_tree, b_tree, entries);2593else2594 re->src_entry = item->util;25952596 item =string_list_lookup(entries, re->pair->two->path);2597if(!item)2598 re->dst_entry =insert_stage_data(re->pair->two->path,2599 o_tree, a_tree, b_tree, entries);2600else2601 re->dst_entry = item->util;2602 item =string_list_insert(renames, pair->one->path);2603 item->util = re;2604if(new_path)2605apply_directory_rename_modifications(opt, pair, new_path,2606 re, tree, o_tree,2607 a_tree, b_tree,2608 entries);2609}26102611hashmap_iter_init(&collisions, &iter);2612while((e =hashmap_iter_next(&iter))) {2613free(e->target_file);2614string_list_clear(&e->source_files,0);2615}2616hashmap_free(&collisions,1);2617return renames;2618}26192620static intprocess_renames(struct merge_options *opt,2621struct string_list *a_renames,2622struct string_list *b_renames)2623{2624int clean_merge =1, i, j;2625struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2626struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2627const struct rename *sre;26282629for(i =0; i < a_renames->nr; i++) {2630 sre = a_renames->items[i].util;2631string_list_insert(&a_by_dst, sre->pair->two->path)->util2632= (void*)sre;2633}2634for(i =0; i < b_renames->nr; i++) {2635 sre = b_renames->items[i].util;2636string_list_insert(&b_by_dst, sre->pair->two->path)->util2637= (void*)sre;2638}26392640for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2641struct string_list *renames1, *renames2Dst;2642struct rename *ren1 = NULL, *ren2 = NULL;2643const char*ren1_src, *ren1_dst;2644struct string_list_item *lookup;26452646if(i >= a_renames->nr) {2647 ren2 = b_renames->items[j++].util;2648}else if(j >= b_renames->nr) {2649 ren1 = a_renames->items[i++].util;2650}else{2651int compare =strcmp(a_renames->items[i].string,2652 b_renames->items[j].string);2653if(compare <=0)2654 ren1 = a_renames->items[i++].util;2655if(compare >=0)2656 ren2 = b_renames->items[j++].util;2657}26582659/* TODO: refactor, so that 1/2 are not needed */2660if(ren1) {2661 renames1 = a_renames;2662 renames2Dst = &b_by_dst;2663}else{2664 renames1 = b_renames;2665 renames2Dst = &a_by_dst;2666SWAP(ren2, ren1);2667}26682669if(ren1->processed)2670continue;2671 ren1->processed =1;2672 ren1->dst_entry->processed =1;2673/* BUG: We should only mark src_entry as processed if we2674 * are not dealing with a rename + add-source case.2675 */2676 ren1->src_entry->processed =1;26772678 ren1_src = ren1->pair->one->path;2679 ren1_dst = ren1->pair->two->path;26802681if(ren2) {2682/* One file renamed on both sides */2683const char*ren2_src = ren2->pair->one->path;2684const char*ren2_dst = ren2->pair->two->path;2685enum rename_type rename_type;2686if(strcmp(ren1_src, ren2_src) !=0)2687BUG("ren1_src != ren2_src");2688 ren2->dst_entry->processed =1;2689 ren2->processed =1;2690if(strcmp(ren1_dst, ren2_dst) !=0) {2691 rename_type = RENAME_ONE_FILE_TO_TWO;2692 clean_merge =0;2693}else{2694 rename_type = RENAME_ONE_FILE_TO_ONE;2695/* BUG: We should only remove ren1_src in2696 * the base stage (think of rename +2697 * add-source cases).2698 */2699remove_file(opt,1, ren1_src,1);2700update_entry(ren1->dst_entry,2701 ren1->pair->one,2702 ren1->pair->two,2703 ren2->pair->two);2704}2705setup_rename_conflict_info(rename_type, opt, ren1, ren2);2706}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2707/* Two different files renamed to the same thing */2708char*ren2_dst;2709 ren2 = lookup->util;2710 ren2_dst = ren2->pair->two->path;2711if(strcmp(ren1_dst, ren2_dst) !=0)2712BUG("ren1_dst != ren2_dst");27132714 clean_merge =0;2715 ren2->processed =1;2716/*2717 * BUG: We should only mark src_entry as processed2718 * if we are not dealing with a rename + add-source2719 * case.2720 */2721 ren2->src_entry->processed =1;27222723setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2724 opt, ren1, ren2);27252726}else{2727/* Renamed in 1, maybe changed in 2 */2728/* we only use sha1 and mode of these */2729struct diff_filespec src_other, dst_other;2730int try_merge;27312732/*2733 * unpack_trees loads entries from common-commit2734 * into stage 1, from head-commit into stage 2, and2735 * from merge-commit into stage 3. We keep track2736 * of which side corresponds to the rename.2737 */2738int renamed_stage = a_renames == renames1 ?2:3;2739int other_stage = a_renames == renames1 ?3:2;27402741/* BUG: We should only remove ren1_src in the base2742 * stage and in other_stage (think of rename +2743 * add-source case).2744 */2745remove_file(opt,1, ren1_src,2746 renamed_stage ==2|| !was_tracked(opt, ren1_src));27472748oidcpy(&src_other.oid,2749&ren1->src_entry->stages[other_stage].oid);2750 src_other.mode = ren1->src_entry->stages[other_stage].mode;2751oidcpy(&dst_other.oid,2752&ren1->dst_entry->stages[other_stage].oid);2753 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2754 try_merge =0;27552756if(oid_eq(&src_other.oid, &null_oid) &&2757 ren1->add_turned_into_rename) {2758setup_rename_conflict_info(RENAME_VIA_DIR,2759 opt, ren1, NULL);2760}else if(oid_eq(&src_other.oid, &null_oid)) {2761setup_rename_conflict_info(RENAME_DELETE,2762 opt, ren1, NULL);2763}else if((dst_other.mode == ren1->pair->two->mode) &&2764oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2765/*2766 * Added file on the other side identical to2767 * the file being renamed: clean merge.2768 * Also, there is no need to overwrite the2769 * file already in the working copy, so call2770 * update_file_flags() instead of2771 * update_file().2772 */2773if(update_file_flags(opt,2774&ren1->pair->two->oid,2775 ren1->pair->two->mode,2776 ren1_dst,27771,/* update_cache */27780/* update_wd */))2779 clean_merge = -1;2780}else if(!oid_eq(&dst_other.oid, &null_oid)) {2781/*2782 * Probably not a clean merge, but it's2783 * premature to set clean_merge to 0 here,2784 * because if the rename merges cleanly and2785 * the merge exactly matches the newly added2786 * file, then the merge will be clean.2787 */2788setup_rename_conflict_info(RENAME_ADD,2789 opt, ren1, NULL);2790}else2791 try_merge =1;27922793if(clean_merge <0)2794goto cleanup_and_return;2795if(try_merge) {2796struct diff_filespec *o, *a, *b;2797 src_other.path = (char*)ren1_src;27982799 o = ren1->pair->one;2800if(a_renames == renames1) {2801 a = ren1->pair->two;2802 b = &src_other;2803}else{2804 b = ren1->pair->two;2805 a = &src_other;2806}2807update_entry(ren1->dst_entry, o, a, b);2808setup_rename_conflict_info(RENAME_NORMAL,2809 opt, ren1, NULL);2810}2811}2812}2813cleanup_and_return:2814string_list_clear(&a_by_dst,0);2815string_list_clear(&b_by_dst,0);28162817return clean_merge;2818}28192820struct rename_info {2821struct string_list *head_renames;2822struct string_list *merge_renames;2823};28242825static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2826struct hashmap *dir_renames)2827{2828struct hashmap_iter iter;2829struct dir_rename_entry *e;28302831hashmap_iter_init(dir_renames, &iter);2832while((e =hashmap_iter_next(&iter))) {2833free(e->dir);2834strbuf_release(&e->new_dir);2835/* possible_new_dirs already cleared in get_directory_renames */2836}2837hashmap_free(dir_renames,1);2838free(dir_renames);28392840free(pairs->queue);2841free(pairs);2842}28432844static intdetect_and_process_renames(struct merge_options *opt,2845struct tree *common,2846struct tree *head,2847struct tree *merge,2848struct string_list *entries,2849struct rename_info *ri)2850{2851struct diff_queue_struct *head_pairs, *merge_pairs;2852struct hashmap *dir_re_head, *dir_re_merge;2853int clean =1;28542855 ri->head_renames = NULL;2856 ri->merge_renames = NULL;28572858if(!merge_detect_rename(opt))2859return1;28602861 head_pairs =get_diffpairs(opt, common, head);2862 merge_pairs =get_diffpairs(opt, common, merge);28632864if(opt->detect_directory_renames) {2865 dir_re_head =get_directory_renames(head_pairs);2866 dir_re_merge =get_directory_renames(merge_pairs);28672868handle_directory_level_conflicts(opt,2869 dir_re_head, head,2870 dir_re_merge, merge);2871}else{2872 dir_re_head =xmalloc(sizeof(*dir_re_head));2873 dir_re_merge =xmalloc(sizeof(*dir_re_merge));2874dir_rename_init(dir_re_head);2875dir_rename_init(dir_re_merge);2876}28772878 ri->head_renames =get_renames(opt, opt->branch1, head_pairs,2879 dir_re_merge, dir_re_head, head,2880 common, head, merge, entries,2881&clean);2882if(clean <0)2883goto cleanup;2884 ri->merge_renames =get_renames(opt, opt->branch2, merge_pairs,2885 dir_re_head, dir_re_merge, merge,2886 common, head, merge, entries,2887&clean);2888if(clean <0)2889goto cleanup;2890 clean &=process_renames(opt, ri->head_renames, ri->merge_renames);28912892cleanup:2893/*2894 * Some cleanup is deferred until cleanup_renames() because the2895 * data structures are still needed and referenced in2896 * process_entry(). But there are a few things we can free now.2897 */2898initial_cleanup_rename(head_pairs, dir_re_head);2899initial_cleanup_rename(merge_pairs, dir_re_merge);29002901return clean;2902}29032904static voidfinal_cleanup_rename(struct string_list *rename)2905{2906const struct rename *re;2907int i;29082909if(rename == NULL)2910return;29112912for(i =0; i < rename->nr; i++) {2913 re = rename->items[i].util;2914diff_free_filepair(re->pair);2915}2916string_list_clear(rename,1);2917free(rename);2918}29192920static voidfinal_cleanup_renames(struct rename_info *re_info)2921{2922final_cleanup_rename(re_info->head_renames);2923final_cleanup_rename(re_info->merge_renames);2924}29252926static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)2927{2928return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;2929}29302931static intread_oid_strbuf(struct merge_options *opt,2932const struct object_id *oid,2933struct strbuf *dst)2934{2935void*buf;2936enum object_type type;2937unsigned long size;2938 buf =read_object_file(oid, &type, &size);2939if(!buf)2940returnerr(opt,_("cannot read object%s"),oid_to_hex(oid));2941if(type != OBJ_BLOB) {2942free(buf);2943returnerr(opt,_("object%sis not a blob"),oid_to_hex(oid));2944}2945strbuf_attach(dst, buf, size, size +1);2946return0;2947}29482949static intblob_unchanged(struct merge_options *opt,2950const struct object_id *o_oid,2951unsigned o_mode,2952const struct object_id *a_oid,2953unsigned a_mode,2954int renormalize,const char*path)2955{2956struct strbuf obuf = STRBUF_INIT;2957struct strbuf abuf = STRBUF_INIT;2958int ret =0;/* assume changed for safety */2959const struct index_state *idx = opt->repo->index;29602961if(a_mode != o_mode)2962return0;2963if(oid_eq(o_oid, a_oid))2964return1;2965if(!renormalize)2966return0;29672968assert(o_oid && a_oid);2969if(read_oid_strbuf(opt, o_oid, &obuf) ||2970read_oid_strbuf(opt, a_oid, &abuf))2971goto error_return;2972/*2973 * Note: binary | is used so that both renormalizations are2974 * performed. Comparison can be skipped if both files are2975 * unchanged since their sha1s have already been compared.2976 */2977if(renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |2978renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))2979 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));29802981error_return:2982strbuf_release(&obuf);2983strbuf_release(&abuf);2984return ret;2985}29862987static inthandle_modify_delete(struct merge_options *opt,2988const char*path,2989struct object_id *o_oid,int o_mode,2990struct object_id *a_oid,int a_mode,2991struct object_id *b_oid,int b_mode)2992{2993const char*modify_branch, *delete_branch;2994struct object_id *changed_oid;2995int changed_mode;29962997if(a_oid) {2998 modify_branch = opt->branch1;2999 delete_branch = opt->branch2;3000 changed_oid = a_oid;3001 changed_mode = a_mode;3002}else{3003 modify_branch = opt->branch2;3004 delete_branch = opt->branch1;3005 changed_oid = b_oid;3006 changed_mode = b_mode;3007}30083009returnhandle_change_delete(opt,3010 path, NULL,3011 o_oid, o_mode,3012 changed_oid, changed_mode,3013 modify_branch, delete_branch,3014_("modify"),_("modified"));3015}30163017static inthandle_content_merge(struct merge_options *opt,3018const char*path,3019int is_dirty,3020struct object_id *o_oid,int o_mode,3021struct object_id *a_oid,int a_mode,3022struct object_id *b_oid,int b_mode,3023struct rename_conflict_info *ci)3024{3025const char*reason =_("content");3026const char*path1 = NULL, *path2 = NULL;3027struct merge_file_info mfi;3028struct diff_filespec one, a, b;3029unsigned df_conflict_remains =0;30303031if(!o_oid) {3032 reason =_("add/add");3033 o_oid = (struct object_id *)&null_oid;3034}3035 one.path = a.path = b.path = (char*)path;3036oidcpy(&one.oid, o_oid);3037 one.mode = o_mode;3038oidcpy(&a.oid, a_oid);3039 a.mode = a_mode;3040oidcpy(&b.oid, b_oid);3041 b.mode = b_mode;30423043if(ci) {3044struct diff_filepair *pair1 = ci->ren1->pair;30453046 path1 = (opt->branch1 == ci->ren1->branch) ?3047 pair1->two->path : pair1->one->path;3048/* If ci->ren2->pair != NULL, we are in3049 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a3050 * normal rename.3051 */3052 path2 = ((ci->ren2 && ci->ren2->pair) ||3053 opt->branch2 == ci->ren1->branch) ?3054 pair1->two->path : pair1->one->path;3055 one.path = pair1->one->path;3056 a.path = (char*)path1;3057 b.path = (char*)path2;30583059if(dir_in_way(opt->repo->index, path, !opt->call_depth,3060S_ISGITLINK(pair1->two->mode)))3061 df_conflict_remains =1;3062}3063if(merge_mode_and_contents(opt, &one, &a, &b, path,3064 opt->branch1, opt->branch2,3065 opt->call_depth *2, &mfi))3066return-1;30673068/*3069 * We can skip updating the working tree file iff:3070 * a) The merge is clean3071 * b) The merge matches what was in HEAD (content, mode, pathname)3072 * c) The target path is usable (i.e. not involved in D/F conflict)3073 */3074if(mfi.clean &&3075was_tracked_and_matches(opt, path, &mfi.oid, mfi.mode) &&3076!df_conflict_remains) {3077int pos;3078struct cache_entry *ce;30793080output(opt,3,_("Skipped%s(merged same as existing)"), path);3081if(add_cacheinfo(opt, mfi.mode, &mfi.oid, path,30820, (!opt->call_depth && !is_dirty),0))3083return-1;3084/*3085 * However, add_cacheinfo() will delete the old cache entry3086 * and add a new one. We need to copy over any skip_worktree3087 * flag to avoid making the file appear as if it were3088 * deleted by the user.3089 */3090 pos =index_name_pos(&opt->orig_index, path,strlen(path));3091 ce = opt->orig_index.cache[pos];3092if(ce_skip_worktree(ce)) {3093 pos =index_name_pos(opt->repo->index, path,strlen(path));3094 ce = opt->repo->index->cache[pos];3095 ce->ce_flags |= CE_SKIP_WORKTREE;3096}3097return mfi.clean;3098}30993100if(!mfi.clean) {3101if(S_ISGITLINK(mfi.mode))3102 reason =_("submodule");3103output(opt,1,_("CONFLICT (%s): Merge conflict in%s"),3104 reason, path);3105if(ci && !df_conflict_remains)3106if(update_stages(opt, path, &one, &a, &b))3107return-1;3108}31093110if(df_conflict_remains || is_dirty) {3111char*new_path;3112if(opt->call_depth) {3113remove_file_from_index(opt->repo->index, path);3114}else{3115if(!mfi.clean) {3116if(update_stages(opt, path, &one, &a, &b))3117return-1;3118}else{3119int file_from_stage2 =was_tracked(opt, path);3120struct diff_filespec merged;3121oidcpy(&merged.oid, &mfi.oid);3122 merged.mode = mfi.mode;31233124if(update_stages(opt, path, NULL,3125 file_from_stage2 ? &merged : NULL,3126 file_from_stage2 ? NULL : &merged))3127return-1;3128}31293130}3131 new_path =unique_path(opt, path, ci->ren1->branch);3132if(is_dirty) {3133output(opt,1,_("Refusing to lose dirty file at%s"),3134 path);3135}3136output(opt,1,_("Adding as%sinstead"), new_path);3137if(update_file(opt,0, &mfi.oid, mfi.mode, new_path)) {3138free(new_path);3139return-1;3140}3141free(new_path);3142 mfi.clean =0;3143}else if(update_file(opt, mfi.clean, &mfi.oid, mfi.mode, path))3144return-1;3145return!is_dirty && mfi.clean;3146}31473148static inthandle_rename_normal(struct merge_options *opt,3149const char*path,3150struct object_id *o_oid,unsigned int o_mode,3151struct object_id *a_oid,unsigned int a_mode,3152struct object_id *b_oid,unsigned int b_mode,3153struct rename_conflict_info *ci)3154{3155/* Merge the content and write it out */3156returnhandle_content_merge(opt, path,was_dirty(opt, path),3157 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,3158 ci);3159}31603161/* Per entry merge function */3162static intprocess_entry(struct merge_options *opt,3163const char*path,struct stage_data *entry)3164{3165int clean_merge =1;3166int normalize = opt->renormalize;3167unsigned o_mode = entry->stages[1].mode;3168unsigned a_mode = entry->stages[2].mode;3169unsigned b_mode = entry->stages[3].mode;3170struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);3171struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);3172struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);31733174 entry->processed =1;3175if(entry->rename_conflict_info) {3176struct rename_conflict_info *ci = entry->rename_conflict_info;3177switch(ci->rename_type) {3178case RENAME_NORMAL:3179case RENAME_ONE_FILE_TO_ONE:3180 clean_merge =handle_rename_normal(opt,3181 path,3182 o_oid, o_mode,3183 a_oid, a_mode,3184 b_oid, b_mode,3185 ci);3186break;3187case RENAME_VIA_DIR:3188 clean_merge =1;3189if(handle_rename_via_dir(opt, ci))3190 clean_merge = -1;3191break;3192case RENAME_ADD:3193/*3194 * Probably unclean merge, but if the renamed file3195 * merges cleanly and the result can then be3196 * two-way merged cleanly with the added file, I3197 * guess it's a clean merge?3198 */3199 clean_merge =handle_rename_add(opt, ci);3200break;3201case RENAME_DELETE:3202 clean_merge =0;3203if(handle_rename_delete(opt, ci))3204 clean_merge = -1;3205break;3206case RENAME_ONE_FILE_TO_TWO:3207 clean_merge =0;3208if(handle_rename_rename_1to2(opt, ci))3209 clean_merge = -1;3210break;3211case RENAME_TWO_FILES_TO_ONE:3212/*3213 * Probably unclean merge, but if the two renamed3214 * files merge cleanly and the two resulting files3215 * can then be two-way merged cleanly, I guess it's3216 * a clean merge?3217 */3218 clean_merge =handle_rename_rename_2to1(opt, ci);3219break;3220default:3221 entry->processed =0;3222break;3223}3224}else if(o_oid && (!a_oid || !b_oid)) {3225/* Case A: Deleted in one */3226if((!a_oid && !b_oid) ||3227(!b_oid &&blob_unchanged(opt, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||3228(!a_oid &&blob_unchanged(opt, o_oid, o_mode, b_oid, b_mode, normalize, path))) {3229/* Deleted in both or deleted in one and3230 * unchanged in the other */3231if(a_oid)3232output(opt,2,_("Removing%s"), path);3233/* do not touch working file if it did not exist */3234remove_file(opt,1, path, !a_oid);3235}else{3236/* Modify/delete; deleted side may have put a directory in the way */3237 clean_merge =0;3238if(handle_modify_delete(opt, path, o_oid, o_mode,3239 a_oid, a_mode, b_oid, b_mode))3240 clean_merge = -1;3241}3242}else if((!o_oid && a_oid && !b_oid) ||3243(!o_oid && !a_oid && b_oid)) {3244/* Case B: Added in one. */3245/* [nothing|directory] -> ([nothing|directory], file) */32463247const char*add_branch;3248const char*other_branch;3249unsigned mode;3250const struct object_id *oid;3251const char*conf;32523253if(a_oid) {3254 add_branch = opt->branch1;3255 other_branch = opt->branch2;3256 mode = a_mode;3257 oid = a_oid;3258 conf =_("file/directory");3259}else{3260 add_branch = opt->branch2;3261 other_branch = opt->branch1;3262 mode = b_mode;3263 oid = b_oid;3264 conf =_("directory/file");3265}3266if(dir_in_way(opt->repo->index, path,3267!opt->call_depth && !S_ISGITLINK(a_mode),32680)) {3269char*new_path =unique_path(opt, path, add_branch);3270 clean_merge =0;3271output(opt,1,_("CONFLICT (%s): There is a directory with name%sin%s. "3272"Adding%sas%s"),3273 conf, path, other_branch, path, new_path);3274if(update_file(opt,0, oid, mode, new_path))3275 clean_merge = -1;3276else if(opt->call_depth)3277remove_file_from_index(opt->repo->index, path);3278free(new_path);3279}else{3280output(opt,2,_("Adding%s"), path);3281/* do not overwrite file if already present */3282if(update_file_flags(opt, oid, mode, path,1, !a_oid))3283 clean_merge = -1;3284}3285}else if(a_oid && b_oid) {3286if(!o_oid) {3287/* Case C: Added in both (check for same permissions) */3288output(opt,1,3289_("CONFLICT (add/add): Merge conflict in%s"),3290 path);3291 clean_merge =handle_file_collision(opt,3292 path, NULL, NULL,3293 opt->branch1,3294 opt->branch2,3295 a_oid, a_mode,3296 b_oid, b_mode);3297}else{3298/* case D: Modified in both, but differently. */3299int is_dirty =0;/* unpack_trees would have bailed if dirty */3300 clean_merge =handle_content_merge(opt, path,3301 is_dirty,3302 o_oid, o_mode,3303 a_oid, a_mode,3304 b_oid, b_mode,3305 NULL);3306}3307}else if(!o_oid && !a_oid && !b_oid) {3308/*3309 * this entry was deleted altogether. a_mode == 0 means3310 * we had that path and want to actively remove it.3311 */3312remove_file(opt,1, path, !a_mode);3313}else3314BUG("fatal merge failure, shouldn't happen.");33153316return clean_merge;3317}33183319intmerge_trees(struct merge_options *opt,3320struct tree *head,3321struct tree *merge,3322struct tree *common,3323struct tree **result)3324{3325struct index_state *istate = opt->repo->index;3326int code, clean;3327struct strbuf sb = STRBUF_INIT;33283329if(!opt->call_depth &&repo_index_has_changes(opt->repo, head, &sb)) {3330err(opt,_("Your local changes to the following files would be overwritten by merge:\n%s"),3331 sb.buf);3332return-1;3333}33343335if(opt->subtree_shift) {3336 merge =shift_tree_object(opt->repo, head, merge, opt->subtree_shift);3337 common =shift_tree_object(opt->repo, head, common, opt->subtree_shift);3338}33393340if(oid_eq(&common->object.oid, &merge->object.oid)) {3341output(opt,0,_("Already up to date!"));3342*result = head;3343return1;3344}33453346 code =unpack_trees_start(opt, common, head, merge);33473348if(code !=0) {3349if(show(opt,4) || opt->call_depth)3350err(opt,_("merging of trees%sand%sfailed"),3351oid_to_hex(&head->object.oid),3352oid_to_hex(&merge->object.oid));3353unpack_trees_finish(opt);3354return-1;3355}33563357if(unmerged_index(istate)) {3358struct string_list *entries;3359struct rename_info re_info;3360int i;3361/*3362 * Only need the hashmap while processing entries, so3363 * initialize it here and free it when we are done running3364 * through the entries. Keeping it in the merge_options as3365 * opposed to decaring a local hashmap is for convenience3366 * so that we don't have to pass it to around.3367 */3368hashmap_init(&opt->current_file_dir_set, path_hashmap_cmp, NULL,512);3369get_files_dirs(opt, head);3370get_files_dirs(opt, merge);33713372 entries =get_unmerged(opt->repo->index);3373 clean =detect_and_process_renames(opt, common, head, merge,3374 entries, &re_info);3375record_df_conflict_files(opt, entries);3376if(clean <0)3377goto cleanup;3378for(i = entries->nr-1;0<= i; i--) {3379const char*path = entries->items[i].string;3380struct stage_data *e = entries->items[i].util;3381if(!e->processed) {3382int ret =process_entry(opt, path, e);3383if(!ret)3384 clean =0;3385else if(ret <0) {3386 clean = ret;3387goto cleanup;3388}3389}3390}3391for(i =0; i < entries->nr; i++) {3392struct stage_data *e = entries->items[i].util;3393if(!e->processed)3394BUG("unprocessed path???%s",3395 entries->items[i].string);3396}33973398 cleanup:3399final_cleanup_renames(&re_info);34003401string_list_clear(entries,1);3402free(entries);34033404hashmap_free(&opt->current_file_dir_set,1);34053406if(clean <0) {3407unpack_trees_finish(opt);3408return clean;3409}3410}3411else3412 clean =1;34133414unpack_trees_finish(opt);34153416if(opt->call_depth && !(*result =write_tree_from_memory(opt)))3417return-1;34183419return clean;3420}34213422static struct commit_list *reverse_commit_list(struct commit_list *list)3423{3424struct commit_list *next = NULL, *current, *backup;3425for(current = list; current; current = backup) {3426 backup = current->next;3427 current->next = next;3428 next = current;3429}3430return next;3431}34323433/*3434 * Merge the commits h1 and h2, return the resulting virtual3435 * commit object and a flag indicating the cleanness of the merge.3436 */3437intmerge_recursive(struct merge_options *opt,3438struct commit *h1,3439struct commit *h2,3440struct commit_list *ca,3441struct commit **result)3442{3443struct commit_list *iter;3444struct commit *merged_common_ancestors;3445struct tree *mrtree;3446int clean;34473448if(show(opt,4)) {3449output(opt,4,_("Merging:"));3450output_commit_title(opt, h1);3451output_commit_title(opt, h2);3452}34533454if(!ca) {3455 ca =get_merge_bases(h1, h2);3456 ca =reverse_commit_list(ca);3457}34583459if(show(opt,5)) {3460unsigned cnt =commit_list_count(ca);34613462output(opt,5,Q_("found%ucommon ancestor:",3463"found%ucommon ancestors:", cnt), cnt);3464for(iter = ca; iter; iter = iter->next)3465output_commit_title(opt, iter->item);3466}34673468 merged_common_ancestors =pop_commit(&ca);3469if(merged_common_ancestors == NULL) {3470/* if there is no common ancestor, use an empty tree */3471struct tree *tree;34723473 tree =lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);3474 merged_common_ancestors =make_virtual_commit(opt->repo, tree,"ancestor");3475}34763477for(iter = ca; iter; iter = iter->next) {3478const char*saved_b1, *saved_b2;3479 opt->call_depth++;3480/*3481 * When the merge fails, the result contains files3482 * with conflict markers. The cleanness flag is3483 * ignored (unless indicating an error), it was never3484 * actually used, as result of merge_trees has always3485 * overwritten it: the committed "conflicts" were3486 * already resolved.3487 */3488discard_index(opt->repo->index);3489 saved_b1 = opt->branch1;3490 saved_b2 = opt->branch2;3491 opt->branch1 ="Temporary merge branch 1";3492 opt->branch2 ="Temporary merge branch 2";3493if(merge_recursive(opt, merged_common_ancestors, iter->item,3494 NULL, &merged_common_ancestors) <0)3495return-1;3496 opt->branch1 = saved_b1;3497 opt->branch2 = saved_b2;3498 opt->call_depth--;34993500if(!merged_common_ancestors)3501returnerr(opt,_("merge returned no commit"));3502}35033504discard_index(opt->repo->index);3505if(!opt->call_depth)3506repo_read_index(opt->repo);35073508 opt->ancestor ="merged common ancestors";3509 clean =merge_trees(opt,get_commit_tree(h1),get_commit_tree(h2),3510get_commit_tree(merged_common_ancestors),3511&mrtree);3512if(clean <0) {3513flush_output(opt);3514return clean;3515}35163517if(opt->call_depth) {3518*result =make_virtual_commit(opt->repo, mrtree,"merged tree");3519commit_list_insert(h1, &(*result)->parents);3520commit_list_insert(h2, &(*result)->parents->next);3521}3522flush_output(opt);3523if(!opt->call_depth && opt->buffer_output <2)3524strbuf_release(&opt->obuf);3525if(show(opt,2))3526diff_warn_rename_limit("merge.renamelimit",3527 opt->needed_rename_limit,0);3528return clean;3529}35303531static struct commit *get_ref(struct repository *repo,const struct object_id *oid,3532const char*name)3533{3534struct object *object;35353536 object =deref_tag(repo,parse_object(repo, oid),3537 name,strlen(name));3538if(!object)3539return NULL;3540if(object->type == OBJ_TREE)3541returnmake_virtual_commit(repo, (struct tree*)object, name);3542if(object->type != OBJ_COMMIT)3543return NULL;3544if(parse_commit((struct commit *)object))3545return NULL;3546return(struct commit *)object;3547}35483549intmerge_recursive_generic(struct merge_options *opt,3550const struct object_id *head,3551const struct object_id *merge,3552int num_base_list,3553const struct object_id **base_list,3554struct commit **result)3555{3556int clean;3557struct lock_file lock = LOCK_INIT;3558struct commit *head_commit =get_ref(opt->repo, head, opt->branch1);3559struct commit *next_commit =get_ref(opt->repo, merge, opt->branch2);3560struct commit_list *ca = NULL;35613562if(base_list) {3563int i;3564for(i =0; i < num_base_list; ++i) {3565struct commit *base;3566if(!(base =get_ref(opt->repo, base_list[i],oid_to_hex(base_list[i]))))3567returnerr(opt,_("Could not parse object '%s'"),3568oid_to_hex(base_list[i]));3569commit_list_insert(base, &ca);3570}3571}35723573repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);3574 clean =merge_recursive(opt, head_commit, next_commit, ca,3575 result);3576if(clean <0) {3577rollback_lock_file(&lock);3578return clean;3579}35803581if(write_locked_index(opt->repo->index, &lock,3582 COMMIT_LOCK | SKIP_IF_UNCHANGED))3583returnerr(opt,_("Unable to write index."));35843585return clean ?0:1;3586}35873588static voidmerge_recursive_config(struct merge_options *opt)3589{3590char*value = NULL;3591git_config_get_int("merge.verbosity", &opt->verbosity);3592git_config_get_int("diff.renamelimit", &opt->diff_rename_limit);3593git_config_get_int("merge.renamelimit", &opt->merge_rename_limit);3594if(!git_config_get_string("diff.renames", &value)) {3595 opt->diff_detect_rename =git_config_rename("diff.renames", value);3596free(value);3597}3598if(!git_config_get_string("merge.renames", &value)) {3599 opt->merge_detect_rename =git_config_rename("merge.renames", value);3600free(value);3601}3602git_config(git_xmerge_config, NULL);3603}36043605voidinit_merge_options(struct merge_options *opt,3606struct repository *repo)3607{3608const char*merge_verbosity;3609memset(opt,0,sizeof(struct merge_options));3610 opt->repo = repo;3611 opt->verbosity =2;3612 opt->buffer_output =1;3613 opt->diff_rename_limit = -1;3614 opt->merge_rename_limit = -1;3615 opt->renormalize =0;3616 opt->diff_detect_rename = -1;3617 opt->merge_detect_rename = -1;3618 opt->detect_directory_renames =1;3619merge_recursive_config(opt);3620 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3621if(merge_verbosity)3622 opt->verbosity =strtol(merge_verbosity, NULL,10);3623if(opt->verbosity >=5)3624 opt->buffer_output =0;3625strbuf_init(&opt->obuf,0);3626string_list_init(&opt->df_conflict_file_set,1);3627}36283629intparse_merge_opt(struct merge_options *opt,const char*s)3630{3631const char*arg;36323633if(!s || !*s)3634return-1;3635if(!strcmp(s,"ours"))3636 opt->recursive_variant = MERGE_RECURSIVE_OURS;3637else if(!strcmp(s,"theirs"))3638 opt->recursive_variant = MERGE_RECURSIVE_THEIRS;3639else if(!strcmp(s,"subtree"))3640 opt->subtree_shift ="";3641else if(skip_prefix(s,"subtree=", &arg))3642 opt->subtree_shift = arg;3643else if(!strcmp(s,"patience"))3644 opt->xdl_opts =DIFF_WITH_ALG(opt, PATIENCE_DIFF);3645else if(!strcmp(s,"histogram"))3646 opt->xdl_opts =DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);3647else if(skip_prefix(s,"diff-algorithm=", &arg)) {3648long value =parse_algorithm_value(arg);3649if(value <0)3650return-1;3651/* clear out previous settings */3652DIFF_XDL_CLR(opt, NEED_MINIMAL);3653 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3654 opt->xdl_opts |= value;3655}3656else if(!strcmp(s,"ignore-space-change"))3657DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);3658else if(!strcmp(s,"ignore-all-space"))3659DIFF_XDL_SET(opt, IGNORE_WHITESPACE);3660else if(!strcmp(s,"ignore-space-at-eol"))3661DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);3662else if(!strcmp(s,"ignore-cr-at-eol"))3663DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);3664else if(!strcmp(s,"renormalize"))3665 opt->renormalize =1;3666else if(!strcmp(s,"no-renormalize"))3667 opt->renormalize =0;3668else if(!strcmp(s,"no-renames"))3669 opt->merge_detect_rename =0;3670else if(!strcmp(s,"find-renames")) {3671 opt->merge_detect_rename =1;3672 opt->rename_score =0;3673}3674else if(skip_prefix(s,"find-renames=", &arg) ||3675skip_prefix(s,"rename-threshold=", &arg)) {3676if((opt->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3677return-1;3678 opt->merge_detect_rename =1;3679}3680/*3681 * Please update $__git_merge_strategy_options in3682 * git-completion.bash when you add new options3683 */3684else3685return-1;3686return0;3687}