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 diff_filepair *pair,1361const char*rename_branch)1362{1363/*1364 * Handle file adds that need to be renamed due to directory rename1365 * detection. This differs from handle_rename_normal, because1366 * there is no content merge to do; just move the file into the1367 * desired final location.1368 */1369const struct diff_filespec *dest = pair->two;13701371if(!opt->call_depth &&would_lose_untracked(opt, dest->path)) {1372char*alt_path =unique_path(opt, dest->path, rename_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 rename_branch == opt->branch1 ? dest : NULL,1387 rename_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 diff_filepair *pair,1480const char*rename_branch,1481const char*delete_branch)1482{1483const struct diff_filespec *orig = pair->one;1484const struct diff_filespec *dest = pair->two;14851486if(handle_change_delete(opt,1487 opt->call_depth ? orig->path : dest->path,1488 opt->call_depth ? NULL : orig->path,1489&orig->oid, orig->mode,1490&dest->oid, dest->mode,1491 rename_branch, delete_branch,1492_("rename"),_("renamed")))1493return-1;14941495if(opt->call_depth)1496returnremove_file_from_index(opt->repo->index, dest->path);1497else1498returnupdate_stages(opt, dest->path, NULL,1499 rename_branch == opt->branch1 ? dest : NULL,1500 rename_branch == opt->branch1 ? NULL : dest);1501}15021503static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1504struct stage_data *entry,1505int stage)1506{1507struct object_id *oid = &entry->stages[stage].oid;1508unsigned mode = entry->stages[stage].mode;1509if(mode ==0||is_null_oid(oid))1510return NULL;1511oidcpy(&target->oid, oid);1512 target->mode = mode;1513return target;1514}15151516static inthandle_file_collision(struct merge_options *opt,1517const char*collide_path,1518const char*prev_path1,1519const char*prev_path2,1520const char*branch1,const char*branch2,1521const struct object_id *a_oid,1522unsigned int a_mode,1523const struct object_id *b_oid,1524unsigned int b_mode)1525{1526struct merge_file_info mfi;1527struct diff_filespec null, a, b;1528char*alt_path = NULL;1529const char*update_path = collide_path;15301531/*1532 * It's easiest to get the correct things into stage 2 and 3, and1533 * to make sure that the content merge puts HEAD before the other1534 * branch if we just ensure that branch1 == opt->branch1. So, simply1535 * flip arguments around if we don't have that.1536 */1537if(branch1 != opt->branch1) {1538returnhandle_file_collision(opt, collide_path,1539 prev_path2, prev_path1,1540 branch2, branch1,1541 b_oid, b_mode,1542 a_oid, a_mode);1543}15441545/*1546 * In the recursive case, we just opt to undo renames1547 */1548if(opt->call_depth && (prev_path1 || prev_path2)) {1549/* Put first file (a_oid, a_mode) in its original spot */1550if(prev_path1) {1551if(update_file(opt,1, a_oid, a_mode, prev_path1))1552return-1;1553}else{1554if(update_file(opt,1, a_oid, a_mode, collide_path))1555return-1;1556}15571558/* Put second file (b_oid, b_mode) in its original spot */1559if(prev_path2) {1560if(update_file(opt,1, b_oid, b_mode, prev_path2))1561return-1;1562}else{1563if(update_file(opt,1, b_oid, b_mode, collide_path))1564return-1;1565}15661567/* Don't leave something at collision path if unrenaming both */1568if(prev_path1 && prev_path2)1569remove_file(opt,1, collide_path,0);15701571return0;1572}15731574/* Remove rename sources if rename/add or rename/rename(2to1) */1575if(prev_path1)1576remove_file(opt,1, prev_path1,1577 opt->call_depth ||would_lose_untracked(opt, prev_path1));1578if(prev_path2)1579remove_file(opt,1, prev_path2,1580 opt->call_depth ||would_lose_untracked(opt, prev_path2));15811582/*1583 * Remove the collision path, if it wouldn't cause dirty contents1584 * or an untracked file to get lost. We'll either overwrite with1585 * merged contents, or just write out to differently named files.1586 */1587if(was_dirty(opt, collide_path)) {1588output(opt,1,_("Refusing to lose dirty file at%s"),1589 collide_path);1590 update_path = alt_path =unique_path(opt, collide_path,"merged");1591}else if(would_lose_untracked(opt, collide_path)) {1592/*1593 * Only way we get here is if both renames were from1594 * a directory rename AND user had an untracked file1595 * at the location where both files end up after the1596 * two directory renames. See testcase 10d of t6043.1597 */1598output(opt,1,_("Refusing to lose untracked file at "1599"%s, even though it's in the way."),1600 collide_path);1601 update_path = alt_path =unique_path(opt, collide_path,"merged");1602}else{1603/*1604 * FIXME: It's possible that the two files are identical1605 * and that the current working copy happens to match, in1606 * which case we are unnecessarily touching the working1607 * tree file. It's not a likely enough scenario that I1608 * want to code up the checks for it and a better fix is1609 * available if we restructure how unpack_trees() and1610 * merge-recursive interoperate anyway, so punting for1611 * now...1612 */1613remove_file(opt,0, collide_path,0);1614}16151616/* Store things in diff_filespecs for functions that need it */1617memset(&a,0,sizeof(struct diff_filespec));1618memset(&b,0,sizeof(struct diff_filespec));1619 null.path = a.path = b.path = (char*)collide_path;1620oidcpy(&null.oid, &null_oid);1621 null.mode =0;1622oidcpy(&a.oid, a_oid);1623 a.mode = a_mode;1624 a.oid_valid =1;1625oidcpy(&b.oid, b_oid);1626 b.mode = b_mode;1627 b.oid_valid =1;16281629if(merge_mode_and_contents(opt, &null, &a, &b, collide_path,1630 branch1, branch2, opt->call_depth *2, &mfi))1631return-1;1632 mfi.clean &= !alt_path;1633if(update_file(opt, mfi.clean, &mfi.oid, mfi.mode, update_path))1634return-1;1635if(!mfi.clean && !opt->call_depth &&1636update_stages(opt, collide_path, NULL, &a, &b))1637return-1;1638free(alt_path);1639/*1640 * FIXME: If both a & b both started with conflicts (only possible1641 * if they came from a rename/rename(2to1)), but had IDENTICAL1642 * contents including those conflicts, then in the next line we claim1643 * it was clean. If someone cares about this case, we should have the1644 * caller notify us if we started with conflicts.1645 */1646return mfi.clean;1647}16481649static inthandle_rename_add(struct merge_options *opt,1650struct rename_conflict_info *ci)1651{1652/* a was renamed to c, and a separate c was added. */1653struct diff_filespec *a = ci->ren1->pair->one;1654struct diff_filespec *c = ci->ren1->pair->two;1655struct diff_filespec tmp;1656char*path = c->path;1657char*prev_path_desc;1658struct merge_file_info mfi;16591660const char*rename_branch = ci->ren1->branch;1661const char*add_branch = (opt->branch1 == rename_branch ?1662 opt->branch2 : opt->branch1);1663int other_stage = (ci->ren1->branch == opt->branch1 ?3:2);16641665output(opt,1,_("CONFLICT (rename/add): "1666"Rename%s->%sin%s. Added%sin%s"),1667 a->path, c->path, rename_branch,1668 c->path, add_branch);16691670filespec_from_entry(&tmp, ci->ren1->src_entry, other_stage);1671 tmp.path = a->path;16721673 prev_path_desc =xstrfmt("version of%sfrom%s", path, a->path);1674if(merge_mode_and_contents(opt, a, c, &tmp,1675 prev_path_desc,1676 opt->branch1, opt->branch2,16771+ opt->call_depth *2, &mfi))1678return-1;1679free(prev_path_desc);16801681returnhandle_file_collision(opt,1682 c->path, a->path, NULL,1683 rename_branch, add_branch,1684&mfi.oid, mfi.mode,1685&ci->ren1->dst_entry->stages[other_stage].oid,1686 ci->ren1->dst_entry->stages[other_stage].mode);1687}16881689static char*find_path_for_conflict(struct merge_options *opt,1690const char*path,1691const char*branch1,1692const char*branch2)1693{1694char*new_path = NULL;1695if(dir_in_way(opt->repo->index, path, !opt->call_depth,0)) {1696 new_path =unique_path(opt, path, branch1);1697output(opt,1,_("%sis a directory in%sadding "1698"as%sinstead"),1699 path, branch2, new_path);1700}else if(would_lose_untracked(opt, path)) {1701 new_path =unique_path(opt, path, branch1);1702output(opt,1,_("Refusing to lose untracked file"1703" at%s; adding as%sinstead"),1704 path, new_path);1705}17061707return new_path;1708}17091710static inthandle_rename_rename_1to2(struct merge_options *opt,1711struct rename_conflict_info *ci)1712{1713/* One file was renamed in both branches, but to different names. */1714struct merge_file_info mfi;1715struct diff_filespec other;1716struct diff_filespec *add;1717struct diff_filespec *o = ci->ren1->pair->one;1718struct diff_filespec *a = ci->ren1->pair->two;1719struct diff_filespec *b = ci->ren2->pair->two;1720char*path_desc;17211722output(opt,1,_("CONFLICT (rename/rename): "1723"Rename\"%s\"->\"%s\"in branch\"%s\""1724"rename\"%s\"->\"%s\"in\"%s\"%s"),1725 o->path, a->path, ci->ren1->branch,1726 o->path, b->path, ci->ren2->branch,1727 opt->call_depth ?_(" (left unresolved)") :"");17281729 path_desc =xstrfmt("%sand%s, both renamed from%s",1730 a->path, b->path, o->path);1731if(merge_mode_and_contents(opt, o, a, b, path_desc,1732 ci->ren1->branch, ci->ren2->branch,1733 opt->call_depth *2, &mfi))1734return-1;1735free(path_desc);17361737if(opt->call_depth) {1738/*1739 * FIXME: For rename/add-source conflicts (if we could detect1740 * such), this is wrong. We should instead find a unique1741 * pathname and then either rename the add-source file to that1742 * unique path, or use that unique path instead of src here.1743 */1744if(update_file(opt,0, &mfi.oid, mfi.mode, o->path))1745return-1;17461747/*1748 * Above, we put the merged content at the merge-base's1749 * path. Now we usually need to delete both a->path and1750 * b->path. However, the rename on each side of the merge1751 * could also be involved in a rename/add conflict. In1752 * such cases, we should keep the added file around,1753 * resolving the conflict at that path in its favor.1754 */1755 add =filespec_from_entry(&other, ci->ren1->dst_entry,2^1);1756if(add) {1757if(update_file(opt,0, &add->oid, add->mode, a->path))1758return-1;1759}1760else1761remove_file_from_index(opt->repo->index, a->path);1762 add =filespec_from_entry(&other, ci->ren2->dst_entry,3^1);1763if(add) {1764if(update_file(opt,0, &add->oid, add->mode, b->path))1765return-1;1766}1767else1768remove_file_from_index(opt->repo->index, b->path);1769}else{1770/*1771 * For each destination path, we need to see if there is a1772 * rename/add collision. If not, we can write the file out1773 * to the specified location.1774 */1775 add =filespec_from_entry(&other, ci->ren1->dst_entry,2^1);1776if(add) {1777if(handle_file_collision(opt, a->path,1778 NULL, NULL,1779 ci->ren1->branch,1780 ci->ren2->branch,1781&mfi.oid, mfi.mode,1782&add->oid, add->mode) <0)1783return-1;1784}else{1785char*new_path =find_path_for_conflict(opt, a->path,1786 ci->ren1->branch,1787 ci->ren2->branch);1788if(update_file(opt,0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))1789return-1;1790free(new_path);1791if(update_stages(opt, a->path, NULL, a, NULL))1792return-1;1793}17941795 add =filespec_from_entry(&other, ci->ren2->dst_entry,3^1);1796if(add) {1797if(handle_file_collision(opt, b->path,1798 NULL, NULL,1799 ci->ren1->branch,1800 ci->ren2->branch,1801&add->oid, add->mode,1802&mfi.oid, mfi.mode) <0)1803return-1;1804}else{1805char*new_path =find_path_for_conflict(opt, b->path,1806 ci->ren2->branch,1807 ci->ren1->branch);1808if(update_file(opt,0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))1809return-1;1810free(new_path);1811if(update_stages(opt, b->path, NULL, NULL, b))1812return-1;1813}1814}18151816return0;1817}18181819static inthandle_rename_rename_2to1(struct merge_options *opt,1820struct rename_conflict_info *ci)1821{1822/* Two files, a & b, were renamed to the same thing, c. */1823struct diff_filespec *a = ci->ren1->pair->one;1824struct diff_filespec *b = ci->ren2->pair->one;1825struct diff_filespec *c1 = ci->ren1->pair->two;1826struct diff_filespec *c2 = ci->ren2->pair->two;1827struct diff_filespec tmp1, tmp2;1828char*path = c1->path;/* == c2->path */1829char*path_side_1_desc;1830char*path_side_2_desc;1831struct merge_file_info mfi_c1;1832struct merge_file_info mfi_c2;18331834output(opt,1,_("CONFLICT (rename/rename): "1835"Rename%s->%sin%s. "1836"Rename%s->%sin%s"),1837 a->path, c1->path, ci->ren1->branch,1838 b->path, c2->path, ci->ren2->branch);18391840filespec_from_entry(&tmp1, ci->ren1->src_entry,3);1841 tmp1.path = a->path;1842filespec_from_entry(&tmp2, ci->ren2->src_entry,2);1843 tmp2.path = b->path;18441845 path_side_1_desc =xstrfmt("version of%sfrom%s", path, a->path);1846 path_side_2_desc =xstrfmt("version of%sfrom%s", path, b->path);1847if(merge_mode_and_contents(opt, a, c1, &tmp1, path_side_1_desc,1848 opt->branch1, opt->branch2,18491+ opt->call_depth *2, &mfi_c1) ||1850merge_mode_and_contents(opt, b, &tmp2, c2, path_side_2_desc,1851 opt->branch1, opt->branch2,18521+ opt->call_depth *2, &mfi_c2))1853return-1;1854free(path_side_1_desc);1855free(path_side_2_desc);18561857returnhandle_file_collision(opt, path, a->path, b->path,1858 ci->ren1->branch, ci->ren2->branch,1859&mfi_c1.oid, mfi_c1.mode,1860&mfi_c2.oid, mfi_c2.mode);1861}18621863/*1864 * Get the diff_filepairs changed between o_tree and tree.1865 */1866static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,1867struct tree *o_tree,1868struct tree *tree)1869{1870struct diff_queue_struct *ret;1871struct diff_options opts;18721873repo_diff_setup(opt->repo, &opts);1874 opts.flags.recursive =1;1875 opts.flags.rename_empty =0;1876 opts.detect_rename =merge_detect_rename(opt);1877/*1878 * We do not have logic to handle the detection of copies. In1879 * fact, it may not even make sense to add such logic: would we1880 * really want a change to a base file to be propagated through1881 * multiple other files by a merge?1882 */1883if(opts.detect_rename > DIFF_DETECT_RENAME)1884 opts.detect_rename = DIFF_DETECT_RENAME;1885 opts.rename_limit = opt->merge_rename_limit >=0? opt->merge_rename_limit :1886 opt->diff_rename_limit >=0? opt->diff_rename_limit :18871000;1888 opts.rename_score = opt->rename_score;1889 opts.show_rename_progress = opt->show_rename_progress;1890 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1891diff_setup_done(&opts);1892diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1893diffcore_std(&opts);1894if(opts.needed_rename_limit > opt->needed_rename_limit)1895 opt->needed_rename_limit = opts.needed_rename_limit;18961897 ret =xmalloc(sizeof(*ret));1898*ret = diff_queued_diff;18991900 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1901 diff_queued_diff.nr =0;1902 diff_queued_diff.queue = NULL;1903diff_flush(&opts);1904return ret;1905}19061907static inttree_has_path(struct tree *tree,const char*path)1908{1909struct object_id hashy;1910unsigned short mode_o;19111912return!get_tree_entry(&tree->object.oid, path,1913&hashy, &mode_o);1914}19151916/*1917 * Return a new string that replaces the beginning portion (which matches1918 * entry->dir), with entry->new_dir. In perl-speak:1919 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1920 * NOTE:1921 * Caller must ensure that old_path starts with entry->dir + '/'.1922 */1923static char*apply_dir_rename(struct dir_rename_entry *entry,1924const char*old_path)1925{1926struct strbuf new_path = STRBUF_INIT;1927int oldlen, newlen;19281929if(entry->non_unique_new_dir)1930return NULL;19311932 oldlen =strlen(entry->dir);1933 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1934strbuf_grow(&new_path, newlen);1935strbuf_addbuf(&new_path, &entry->new_dir);1936strbuf_addstr(&new_path, &old_path[oldlen]);19371938returnstrbuf_detach(&new_path, NULL);1939}19401941static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1942char**old_dir,char**new_dir)1943{1944char*end_of_old, *end_of_new;1945int old_len, new_len;19461947*old_dir = NULL;1948*new_dir = NULL;19491950/*1951 * For1952 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1953 * the "e/foo.c" part is the same, we just want to know that1954 * "a/b/c/d" was renamed to "a/b/some/thing/else"1955 * so, for this example, this function returns "a/b/c/d" in1956 * *old_dir and "a/b/some/thing/else" in *new_dir.1957 *1958 * Also, if the basename of the file changed, we don't care. We1959 * want to know which portion of the directory, if any, changed.1960 */1961 end_of_old =strrchr(old_path,'/');1962 end_of_new =strrchr(new_path,'/');19631964if(end_of_old == NULL || end_of_new == NULL)1965return;1966while(*--end_of_new == *--end_of_old &&1967 end_of_old != old_path &&1968 end_of_new != new_path)1969;/* Do nothing; all in the while loop */1970/*1971 * We've found the first non-matching character in the directory1972 * paths. That means the current directory we were comparing1973 * represents the rename. Move end_of_old and end_of_new back1974 * to the full directory name.1975 */1976if(*end_of_old =='/')1977 end_of_old++;1978if(*end_of_old !='/')1979 end_of_new++;1980 end_of_old =strchr(end_of_old,'/');1981 end_of_new =strchr(end_of_new,'/');19821983/*1984 * It may have been the case that old_path and new_path were the same1985 * directory all along. Don't claim a rename if they're the same.1986 */1987 old_len = end_of_old - old_path;1988 new_len = end_of_new - new_path;19891990if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {1991*old_dir =xstrndup(old_path, old_len);1992*new_dir =xstrndup(new_path, new_len);1993}1994}19951996static voidremove_hashmap_entries(struct hashmap *dir_renames,1997struct string_list *items_to_remove)1998{1999int i;2000struct dir_rename_entry *entry;20012002for(i =0; i < items_to_remove->nr; i++) {2003 entry = items_to_remove->items[i].util;2004hashmap_remove(dir_renames, entry, NULL);2005}2006string_list_clear(items_to_remove,0);2007}20082009/*2010 * See if there is a directory rename for path, and if there are any file2011 * level conflicts for the renamed location. If there is a rename and2012 * there are no conflicts, return the new name. Otherwise, return NULL.2013 */2014static char*handle_path_level_conflicts(struct merge_options *opt,2015const char*path,2016struct dir_rename_entry *entry,2017struct hashmap *collisions,2018struct tree *tree)2019{2020char*new_path = NULL;2021struct collision_entry *collision_ent;2022int clean =1;2023struct strbuf collision_paths = STRBUF_INIT;20242025/*2026 * entry has the mapping of old directory name to new directory name2027 * that we want to apply to path.2028 */2029 new_path =apply_dir_rename(entry, path);20302031if(!new_path) {2032/* This should only happen when entry->non_unique_new_dir set */2033if(!entry->non_unique_new_dir)2034BUG("entry->non_unqiue_dir not set and !new_path");2035output(opt,1,_("CONFLICT (directory rename split): "2036"Unclear where to place%sbecause directory "2037"%swas renamed to multiple other directories, "2038"with no destination getting a majority of the "2039"files."),2040 path, entry->dir);2041 clean =0;2042return NULL;2043}20442045/*2046 * The caller needs to have ensured that it has pre-populated2047 * collisions with all paths that map to new_path. Do a quick check2048 * to ensure that's the case.2049 */2050 collision_ent =collision_find_entry(collisions, new_path);2051if(collision_ent == NULL)2052BUG("collision_ent is NULL");20532054/*2055 * Check for one-sided add/add/.../add conflicts, i.e.2056 * where implicit renames from the other side doing2057 * directory rename(s) can affect this side of history2058 * to put multiple paths into the same location. Warn2059 * and bail on directory renames for such paths.2060 */2061if(collision_ent->reported_already) {2062 clean =0;2063}else if(tree_has_path(tree, new_path)) {2064 collision_ent->reported_already =1;2065strbuf_add_separated_string_list(&collision_paths,", ",2066&collision_ent->source_files);2067output(opt,1,_("CONFLICT (implicit dir rename): Existing "2068"file/dir at%sin the way of implicit "2069"directory rename(s) putting the following "2070"path(s) there:%s."),2071 new_path, collision_paths.buf);2072 clean =0;2073}else if(collision_ent->source_files.nr >1) {2074 collision_ent->reported_already =1;2075strbuf_add_separated_string_list(&collision_paths,", ",2076&collision_ent->source_files);2077output(opt,1,_("CONFLICT (implicit dir rename): Cannot map "2078"more than one path to%s; implicit directory "2079"renames tried to put these paths there:%s"),2080 new_path, collision_paths.buf);2081 clean =0;2082}20832084/* Free memory we no longer need */2085strbuf_release(&collision_paths);2086if(!clean && new_path) {2087free(new_path);2088return NULL;2089}20902091return new_path;2092}20932094/*2095 * There are a couple things we want to do at the directory level:2096 * 1. Check for both sides renaming to the same thing, in order to avoid2097 * implicit renaming of files that should be left in place. (See2098 * testcase 6b in t6043 for details.)2099 * 2. Prune directory renames if there are still files left in the2100 * the original directory. These represent a partial directory rename,2101 * i.e. a rename where only some of the files within the directory2102 * were renamed elsewhere. (Technically, this could be done earlier2103 * in get_directory_renames(), except that would prevent us from2104 * doing the previous check and thus failing testcase 6b.)2105 * 3. Check for rename/rename(1to2) conflicts (at the directory level).2106 * In the future, we could potentially record this info as well and2107 * omit reporting rename/rename(1to2) conflicts for each path within2108 * the affected directories, thus cleaning up the merge output.2109 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the2110 * directory level, because merging directories is fine. If it2111 * causes conflicts for files within those merged directories, then2112 * that should be detected at the individual path level.2113 */2114static voidhandle_directory_level_conflicts(struct merge_options *opt,2115struct hashmap *dir_re_head,2116struct tree *head,2117struct hashmap *dir_re_merge,2118struct tree *merge)2119{2120struct hashmap_iter iter;2121struct dir_rename_entry *head_ent;2122struct dir_rename_entry *merge_ent;21232124struct string_list remove_from_head = STRING_LIST_INIT_NODUP;2125struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;21262127hashmap_iter_init(dir_re_head, &iter);2128while((head_ent =hashmap_iter_next(&iter))) {2129 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);2130if(merge_ent &&2131!head_ent->non_unique_new_dir &&2132!merge_ent->non_unique_new_dir &&2133!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {2134/* 1. Renamed identically; remove it from both sides */2135string_list_append(&remove_from_head,2136 head_ent->dir)->util = head_ent;2137strbuf_release(&head_ent->new_dir);2138string_list_append(&remove_from_merge,2139 merge_ent->dir)->util = merge_ent;2140strbuf_release(&merge_ent->new_dir);2141}else if(tree_has_path(head, head_ent->dir)) {2142/* 2. This wasn't a directory rename after all */2143string_list_append(&remove_from_head,2144 head_ent->dir)->util = head_ent;2145strbuf_release(&head_ent->new_dir);2146}2147}21482149remove_hashmap_entries(dir_re_head, &remove_from_head);2150remove_hashmap_entries(dir_re_merge, &remove_from_merge);21512152hashmap_iter_init(dir_re_merge, &iter);2153while((merge_ent =hashmap_iter_next(&iter))) {2154 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);2155if(tree_has_path(merge, merge_ent->dir)) {2156/* 2. This wasn't a directory rename after all */2157string_list_append(&remove_from_merge,2158 merge_ent->dir)->util = merge_ent;2159}else if(head_ent &&2160!head_ent->non_unique_new_dir &&2161!merge_ent->non_unique_new_dir) {2162/* 3. rename/rename(1to2) */2163/*2164 * We can assume it's not rename/rename(1to1) because2165 * that was case (1), already checked above. So we2166 * know that head_ent->new_dir and merge_ent->new_dir2167 * are different strings.2168 */2169output(opt,1,_("CONFLICT (rename/rename): "2170"Rename directory%s->%sin%s. "2171"Rename directory%s->%sin%s"),2172 head_ent->dir, head_ent->new_dir.buf, opt->branch1,2173 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);2174string_list_append(&remove_from_head,2175 head_ent->dir)->util = head_ent;2176strbuf_release(&head_ent->new_dir);2177string_list_append(&remove_from_merge,2178 merge_ent->dir)->util = merge_ent;2179strbuf_release(&merge_ent->new_dir);2180}2181}21822183remove_hashmap_entries(dir_re_head, &remove_from_head);2184remove_hashmap_entries(dir_re_merge, &remove_from_merge);2185}21862187static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)2188{2189struct hashmap *dir_renames;2190struct hashmap_iter iter;2191struct dir_rename_entry *entry;2192int i;21932194/*2195 * Typically, we think of a directory rename as all files from a2196 * certain directory being moved to a target directory. However,2197 * what if someone first moved two files from the original2198 * directory in one commit, and then renamed the directory2199 * somewhere else in a later commit? At merge time, we just know2200 * that files from the original directory went to two different2201 * places, and that the bulk of them ended up in the same place.2202 * We want each directory rename to represent where the bulk of the2203 * files from that directory end up; this function exists to find2204 * where the bulk of the files went.2205 *2206 * The first loop below simply iterates through the list of file2207 * renames, finding out how often each directory rename pair2208 * possibility occurs.2209 */2210 dir_renames =xmalloc(sizeof(*dir_renames));2211dir_rename_init(dir_renames);2212for(i =0; i < pairs->nr; ++i) {2213struct string_list_item *item;2214int*count;2215struct diff_filepair *pair = pairs->queue[i];2216char*old_dir, *new_dir;22172218/* File not part of directory rename if it wasn't renamed */2219if(pair->status !='R')2220continue;22212222get_renamed_dir_portion(pair->one->path, pair->two->path,2223&old_dir, &new_dir);2224if(!old_dir)2225/* Directory didn't change at all; ignore this one. */2226continue;22272228 entry =dir_rename_find_entry(dir_renames, old_dir);2229if(!entry) {2230 entry =xmalloc(sizeof(*entry));2231dir_rename_entry_init(entry, old_dir);2232hashmap_put(dir_renames, entry);2233}else{2234free(old_dir);2235}2236 item =string_list_lookup(&entry->possible_new_dirs, new_dir);2237if(!item) {2238 item =string_list_insert(&entry->possible_new_dirs,2239 new_dir);2240 item->util =xcalloc(1,sizeof(int));2241}else{2242free(new_dir);2243}2244 count = item->util;2245*count +=1;2246}22472248/*2249 * For each directory with files moved out of it, we find out which2250 * target directory received the most files so we can declare it to2251 * be the "winning" target location for the directory rename. This2252 * winner gets recorded in new_dir. If there is no winner2253 * (multiple target directories received the same number of files),2254 * we set non_unique_new_dir. Once we've determined the winner (or2255 * that there is no winner), we no longer need possible_new_dirs.2256 */2257hashmap_iter_init(dir_renames, &iter);2258while((entry =hashmap_iter_next(&iter))) {2259int max =0;2260int bad_max =0;2261char*best = NULL;22622263for(i =0; i < entry->possible_new_dirs.nr; i++) {2264int*count = entry->possible_new_dirs.items[i].util;22652266if(*count == max)2267 bad_max = max;2268else if(*count > max) {2269 max = *count;2270 best = entry->possible_new_dirs.items[i].string;2271}2272}2273if(bad_max == max)2274 entry->non_unique_new_dir =1;2275else{2276assert(entry->new_dir.len ==0);2277strbuf_addstr(&entry->new_dir, best);2278}2279/*2280 * The relevant directory sub-portion of the original full2281 * filepaths were xstrndup'ed before inserting into2282 * possible_new_dirs, and instead of manually iterating the2283 * list and free'ing each, just lie and tell2284 * possible_new_dirs that it did the strdup'ing so that it2285 * will free them for us.2286 */2287 entry->possible_new_dirs.strdup_strings =1;2288string_list_clear(&entry->possible_new_dirs,1);2289}22902291return dir_renames;2292}22932294static struct dir_rename_entry *check_dir_renamed(const char*path,2295struct hashmap *dir_renames)2296{2297char*temp =xstrdup(path);2298char*end;2299struct dir_rename_entry *entry = NULL;23002301while((end =strrchr(temp,'/'))) {2302*end ='\0';2303 entry =dir_rename_find_entry(dir_renames, temp);2304if(entry)2305break;2306}2307free(temp);2308return entry;2309}23102311static voidcompute_collisions(struct hashmap *collisions,2312struct hashmap *dir_renames,2313struct diff_queue_struct *pairs)2314{2315int i;23162317/*2318 * Multiple files can be mapped to the same path due to directory2319 * renames done by the other side of history. Since that other2320 * side of history could have merged multiple directories into one,2321 * if our side of history added the same file basename to each of2322 * those directories, then all N of them would get implicitly2323 * renamed by the directory rename detection into the same path,2324 * and we'd get an add/add/.../add conflict, and all those adds2325 * from *this* side of history. This is not representable in the2326 * index, and users aren't going to easily be able to make sense of2327 * it. So we need to provide a good warning about what's2328 * happening, and fall back to no-directory-rename detection2329 * behavior for those paths.2330 *2331 * See testcases 9e and all of section 5 from t6043 for examples.2332 */2333collision_init(collisions);23342335for(i =0; i < pairs->nr; ++i) {2336struct dir_rename_entry *dir_rename_ent;2337struct collision_entry *collision_ent;2338char*new_path;2339struct diff_filepair *pair = pairs->queue[i];23402341if(pair->status !='A'&& pair->status !='R')2342continue;2343 dir_rename_ent =check_dir_renamed(pair->two->path,2344 dir_renames);2345if(!dir_rename_ent)2346continue;23472348 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);2349if(!new_path)2350/*2351 * dir_rename_ent->non_unique_new_path is true, which2352 * means there is no directory rename for us to use,2353 * which means it won't cause us any additional2354 * collisions.2355 */2356continue;2357 collision_ent =collision_find_entry(collisions, new_path);2358if(!collision_ent) {2359 collision_ent =xcalloc(1,2360sizeof(struct collision_entry));2361hashmap_entry_init(collision_ent,strhash(new_path));2362hashmap_put(collisions, collision_ent);2363 collision_ent->target_file = new_path;2364}else{2365free(new_path);2366}2367string_list_insert(&collision_ent->source_files,2368 pair->two->path);2369}2370}23712372static char*check_for_directory_rename(struct merge_options *opt,2373const char*path,2374struct tree *tree,2375struct hashmap *dir_renames,2376struct hashmap *dir_rename_exclusions,2377struct hashmap *collisions,2378int*clean_merge)2379{2380char*new_path = NULL;2381struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2382struct dir_rename_entry *oentry = NULL;23832384if(!entry)2385return new_path;23862387/*2388 * This next part is a little weird. We do not want to do an2389 * implicit rename into a directory we renamed on our side, because2390 * that will result in a spurious rename/rename(1to2) conflict. An2391 * example:2392 * Base commit: dumbdir/afile, otherdir/bfile2393 * Side 1: smrtdir/afile, otherdir/bfile2394 * Side 2: dumbdir/afile, dumbdir/bfile2395 * Here, while working on Side 1, we could notice that otherdir was2396 * renamed/merged to dumbdir, and change the diff_filepair for2397 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2398 * 2 will notice the rename from dumbdir to smrtdir, and do the2399 * transitive rename to move it from dumbdir/bfile to2400 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2401 * smrtdir, a rename/rename(1to2) conflict. We really just want2402 * the file to end up in smrtdir. And the way to achieve that is2403 * to not let Side1 do the rename to dumbdir, since we know that is2404 * the source of one of our directory renames.2405 *2406 * That's why oentry and dir_rename_exclusions is here.2407 *2408 * As it turns out, this also prevents N-way transient rename2409 * confusion; See testcases 9c and 9d of t6043.2410 */2411 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2412if(oentry) {2413output(opt,1,_("WARNING: Avoiding applying%s->%srename "2414"to%s, because%sitself was renamed."),2415 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2416}else{2417 new_path =handle_path_level_conflicts(opt, path, entry,2418 collisions, tree);2419*clean_merge &= (new_path != NULL);2420}24212422return new_path;2423}24242425static voidapply_directory_rename_modifications(struct merge_options *opt,2426struct diff_filepair *pair,2427char*new_path,2428struct rename *re,2429struct tree *tree,2430struct tree *o_tree,2431struct tree *a_tree,2432struct tree *b_tree,2433struct string_list *entries)2434{2435struct string_list_item *item;2436int stage = (tree == a_tree ?2:3);2437int update_wd;24382439/*2440 * In all cases where we can do directory rename detection,2441 * unpack_trees() will have read pair->two->path into the2442 * index and the working copy. We need to remove it so that2443 * we can instead place it at new_path. It is guaranteed to2444 * not be untracked (unpack_trees() would have errored out2445 * saying the file would have been overwritten), but it might2446 * be dirty, though.2447 */2448 update_wd = !was_dirty(opt, pair->two->path);2449if(!update_wd)2450output(opt,1,_("Refusing to lose dirty file at%s"),2451 pair->two->path);2452remove_file(opt,1, pair->two->path, !update_wd);24532454/* Find or create a new re->dst_entry */2455 item =string_list_lookup(entries, new_path);2456if(item) {2457/*2458 * Since we're renaming on this side of history, and it's2459 * due to a directory rename on the other side of history2460 * (which we only allow when the directory in question no2461 * longer exists on the other side of history), the2462 * original entry for re->dst_entry is no longer2463 * necessary...2464 */2465 re->dst_entry->processed =1;24662467/*2468 * ...because we'll be using this new one.2469 */2470 re->dst_entry = item->util;2471}else{2472/*2473 * re->dst_entry is for the before-dir-rename path, and we2474 * need it to hold information for the after-dir-rename2475 * path. Before creating a new entry, we need to mark the2476 * old one as unnecessary (...unless it is shared by2477 * src_entry, i.e. this didn't use to be a rename, in which2478 * case we can just allow the normal processing to happen2479 * for it).2480 */2481if(pair->status =='R')2482 re->dst_entry->processed =1;24832484 re->dst_entry =insert_stage_data(new_path,2485 o_tree, a_tree, b_tree,2486 entries);2487 item =string_list_insert(entries, new_path);2488 item->util = re->dst_entry;2489}24902491/*2492 * Update the stage_data with the information about the path we are2493 * moving into place. That slot will be empty and available for us2494 * to write to because of the collision checks in2495 * handle_path_level_conflicts(). In other words,2496 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2497 * open for us to write to.2498 *2499 * It may be tempting to actually update the index at this point as2500 * well, using update_stages_for_stage_data(), but as per the big2501 * "NOTE" in update_stages(), doing so will modify the current2502 * in-memory index which will break calls to would_lose_untracked()2503 * that we need to make. Instead, we need to just make sure that2504 * the various handle_rename_*() functions update the index2505 * explicitly rather than relying on unpack_trees() to have done it.2506 */2507get_tree_entry(&tree->object.oid,2508 pair->two->path,2509&re->dst_entry->stages[stage].oid,2510&re->dst_entry->stages[stage].mode);25112512/* Update pair status */2513if(pair->status =='A') {2514/*2515 * Recording rename information for this add makes it look2516 * like a rename/delete conflict. Make sure we can2517 * correctly handle this as an add that was moved to a new2518 * directory instead of reporting a rename/delete conflict.2519 */2520 re->add_turned_into_rename =1;2521}2522/*2523 * We don't actually look at pair->status again, but it seems2524 * pedagogically correct to adjust it.2525 */2526 pair->status ='R';25272528/*2529 * Finally, record the new location.2530 */2531 pair->two->path = new_path;2532}25332534/*2535 * Get information of all renames which occurred in 'pairs', making use of2536 * any implicit directory renames inferred from the other side of history.2537 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2538 * to be able to associate the correct cache entries with the rename2539 * information; tree is always equal to either a_tree or b_tree.2540 */2541static struct string_list *get_renames(struct merge_options *opt,2542const char*branch,2543struct diff_queue_struct *pairs,2544struct hashmap *dir_renames,2545struct hashmap *dir_rename_exclusions,2546struct tree *tree,2547struct tree *o_tree,2548struct tree *a_tree,2549struct tree *b_tree,2550struct string_list *entries,2551int*clean_merge)2552{2553int i;2554struct hashmap collisions;2555struct hashmap_iter iter;2556struct collision_entry *e;2557struct string_list *renames;25582559compute_collisions(&collisions, dir_renames, pairs);2560 renames =xcalloc(1,sizeof(struct string_list));25612562for(i =0; i < pairs->nr; ++i) {2563struct string_list_item *item;2564struct rename *re;2565struct diff_filepair *pair = pairs->queue[i];2566char*new_path;/* non-NULL only with directory renames */25672568if(pair->status !='A'&& pair->status !='R') {2569diff_free_filepair(pair);2570continue;2571}2572 new_path =check_for_directory_rename(opt, pair->two->path, tree,2573 dir_renames,2574 dir_rename_exclusions,2575&collisions,2576 clean_merge);2577if(pair->status !='R'&& !new_path) {2578diff_free_filepair(pair);2579continue;2580}25812582 re =xmalloc(sizeof(*re));2583 re->processed =0;2584 re->add_turned_into_rename =0;2585 re->pair = pair;2586 re->branch = branch;2587 item =string_list_lookup(entries, re->pair->one->path);2588if(!item)2589 re->src_entry =insert_stage_data(re->pair->one->path,2590 o_tree, a_tree, b_tree, entries);2591else2592 re->src_entry = item->util;25932594 item =string_list_lookup(entries, re->pair->two->path);2595if(!item)2596 re->dst_entry =insert_stage_data(re->pair->two->path,2597 o_tree, a_tree, b_tree, entries);2598else2599 re->dst_entry = item->util;2600 item =string_list_insert(renames, pair->one->path);2601 item->util = re;2602if(new_path)2603apply_directory_rename_modifications(opt, pair, new_path,2604 re, tree, o_tree,2605 a_tree, b_tree,2606 entries);2607}26082609hashmap_iter_init(&collisions, &iter);2610while((e =hashmap_iter_next(&iter))) {2611free(e->target_file);2612string_list_clear(&e->source_files,0);2613}2614hashmap_free(&collisions,1);2615return renames;2616}26172618static intprocess_renames(struct merge_options *opt,2619struct string_list *a_renames,2620struct string_list *b_renames)2621{2622int clean_merge =1, i, j;2623struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2624struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2625const struct rename *sre;26262627for(i =0; i < a_renames->nr; i++) {2628 sre = a_renames->items[i].util;2629string_list_insert(&a_by_dst, sre->pair->two->path)->util2630= (void*)sre;2631}2632for(i =0; i < b_renames->nr; i++) {2633 sre = b_renames->items[i].util;2634string_list_insert(&b_by_dst, sre->pair->two->path)->util2635= (void*)sre;2636}26372638for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2639struct string_list *renames1, *renames2Dst;2640struct rename *ren1 = NULL, *ren2 = NULL;2641const char*ren1_src, *ren1_dst;2642struct string_list_item *lookup;26432644if(i >= a_renames->nr) {2645 ren2 = b_renames->items[j++].util;2646}else if(j >= b_renames->nr) {2647 ren1 = a_renames->items[i++].util;2648}else{2649int compare =strcmp(a_renames->items[i].string,2650 b_renames->items[j].string);2651if(compare <=0)2652 ren1 = a_renames->items[i++].util;2653if(compare >=0)2654 ren2 = b_renames->items[j++].util;2655}26562657/* TODO: refactor, so that 1/2 are not needed */2658if(ren1) {2659 renames1 = a_renames;2660 renames2Dst = &b_by_dst;2661}else{2662 renames1 = b_renames;2663 renames2Dst = &a_by_dst;2664SWAP(ren2, ren1);2665}26662667if(ren1->processed)2668continue;2669 ren1->processed =1;2670 ren1->dst_entry->processed =1;2671/* BUG: We should only mark src_entry as processed if we2672 * are not dealing with a rename + add-source case.2673 */2674 ren1->src_entry->processed =1;26752676 ren1_src = ren1->pair->one->path;2677 ren1_dst = ren1->pair->two->path;26782679if(ren2) {2680/* One file renamed on both sides */2681const char*ren2_src = ren2->pair->one->path;2682const char*ren2_dst = ren2->pair->two->path;2683enum rename_type rename_type;2684if(strcmp(ren1_src, ren2_src) !=0)2685BUG("ren1_src != ren2_src");2686 ren2->dst_entry->processed =1;2687 ren2->processed =1;2688if(strcmp(ren1_dst, ren2_dst) !=0) {2689 rename_type = RENAME_ONE_FILE_TO_TWO;2690 clean_merge =0;2691}else{2692 rename_type = RENAME_ONE_FILE_TO_ONE;2693/* BUG: We should only remove ren1_src in2694 * the base stage (think of rename +2695 * add-source cases).2696 */2697remove_file(opt,1, ren1_src,1);2698update_entry(ren1->dst_entry,2699 ren1->pair->one,2700 ren1->pair->two,2701 ren2->pair->two);2702}2703setup_rename_conflict_info(rename_type, opt, ren1, ren2);2704}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2705/* Two different files renamed to the same thing */2706char*ren2_dst;2707 ren2 = lookup->util;2708 ren2_dst = ren2->pair->two->path;2709if(strcmp(ren1_dst, ren2_dst) !=0)2710BUG("ren1_dst != ren2_dst");27112712 clean_merge =0;2713 ren2->processed =1;2714/*2715 * BUG: We should only mark src_entry as processed2716 * if we are not dealing with a rename + add-source2717 * case.2718 */2719 ren2->src_entry->processed =1;27202721setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2722 opt, ren1, ren2);27232724}else{2725/* Renamed in 1, maybe changed in 2 */2726/* we only use sha1 and mode of these */2727struct diff_filespec src_other, dst_other;2728int try_merge;27292730/*2731 * unpack_trees loads entries from common-commit2732 * into stage 1, from head-commit into stage 2, and2733 * from merge-commit into stage 3. We keep track2734 * of which side corresponds to the rename.2735 */2736int renamed_stage = a_renames == renames1 ?2:3;2737int other_stage = a_renames == renames1 ?3:2;27382739/* BUG: We should only remove ren1_src in the base2740 * stage and in other_stage (think of rename +2741 * add-source case).2742 */2743remove_file(opt,1, ren1_src,2744 renamed_stage ==2|| !was_tracked(opt, ren1_src));27452746oidcpy(&src_other.oid,2747&ren1->src_entry->stages[other_stage].oid);2748 src_other.mode = ren1->src_entry->stages[other_stage].mode;2749oidcpy(&dst_other.oid,2750&ren1->dst_entry->stages[other_stage].oid);2751 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2752 try_merge =0;27532754if(oid_eq(&src_other.oid, &null_oid) &&2755 ren1->add_turned_into_rename) {2756setup_rename_conflict_info(RENAME_VIA_DIR,2757 opt, ren1, NULL);2758}else if(oid_eq(&src_other.oid, &null_oid)) {2759setup_rename_conflict_info(RENAME_DELETE,2760 opt, ren1, NULL);2761}else if((dst_other.mode == ren1->pair->two->mode) &&2762oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2763/*2764 * Added file on the other side identical to2765 * the file being renamed: clean merge.2766 * Also, there is no need to overwrite the2767 * file already in the working copy, so call2768 * update_file_flags() instead of2769 * update_file().2770 */2771if(update_file_flags(opt,2772&ren1->pair->two->oid,2773 ren1->pair->two->mode,2774 ren1_dst,27751,/* update_cache */27760/* update_wd */))2777 clean_merge = -1;2778}else if(!oid_eq(&dst_other.oid, &null_oid)) {2779/*2780 * Probably not a clean merge, but it's2781 * premature to set clean_merge to 0 here,2782 * because if the rename merges cleanly and2783 * the merge exactly matches the newly added2784 * file, then the merge will be clean.2785 */2786setup_rename_conflict_info(RENAME_ADD,2787 opt, ren1, NULL);2788}else2789 try_merge =1;27902791if(clean_merge <0)2792goto cleanup_and_return;2793if(try_merge) {2794struct diff_filespec *o, *a, *b;2795 src_other.path = (char*)ren1_src;27962797 o = ren1->pair->one;2798if(a_renames == renames1) {2799 a = ren1->pair->two;2800 b = &src_other;2801}else{2802 b = ren1->pair->two;2803 a = &src_other;2804}2805update_entry(ren1->dst_entry, o, a, b);2806setup_rename_conflict_info(RENAME_NORMAL,2807 opt, ren1, NULL);2808}2809}2810}2811cleanup_and_return:2812string_list_clear(&a_by_dst,0);2813string_list_clear(&b_by_dst,0);28142815return clean_merge;2816}28172818struct rename_info {2819struct string_list *head_renames;2820struct string_list *merge_renames;2821};28222823static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2824struct hashmap *dir_renames)2825{2826struct hashmap_iter iter;2827struct dir_rename_entry *e;28282829hashmap_iter_init(dir_renames, &iter);2830while((e =hashmap_iter_next(&iter))) {2831free(e->dir);2832strbuf_release(&e->new_dir);2833/* possible_new_dirs already cleared in get_directory_renames */2834}2835hashmap_free(dir_renames,1);2836free(dir_renames);28372838free(pairs->queue);2839free(pairs);2840}28412842static intdetect_and_process_renames(struct merge_options *opt,2843struct tree *common,2844struct tree *head,2845struct tree *merge,2846struct string_list *entries,2847struct rename_info *ri)2848{2849struct diff_queue_struct *head_pairs, *merge_pairs;2850struct hashmap *dir_re_head, *dir_re_merge;2851int clean =1;28522853 ri->head_renames = NULL;2854 ri->merge_renames = NULL;28552856if(!merge_detect_rename(opt))2857return1;28582859 head_pairs =get_diffpairs(opt, common, head);2860 merge_pairs =get_diffpairs(opt, common, merge);28612862if(opt->detect_directory_renames) {2863 dir_re_head =get_directory_renames(head_pairs);2864 dir_re_merge =get_directory_renames(merge_pairs);28652866handle_directory_level_conflicts(opt,2867 dir_re_head, head,2868 dir_re_merge, merge);2869}else{2870 dir_re_head =xmalloc(sizeof(*dir_re_head));2871 dir_re_merge =xmalloc(sizeof(*dir_re_merge));2872dir_rename_init(dir_re_head);2873dir_rename_init(dir_re_merge);2874}28752876 ri->head_renames =get_renames(opt, opt->branch1, head_pairs,2877 dir_re_merge, dir_re_head, head,2878 common, head, merge, entries,2879&clean);2880if(clean <0)2881goto cleanup;2882 ri->merge_renames =get_renames(opt, opt->branch2, merge_pairs,2883 dir_re_head, dir_re_merge, merge,2884 common, head, merge, entries,2885&clean);2886if(clean <0)2887goto cleanup;2888 clean &=process_renames(opt, ri->head_renames, ri->merge_renames);28892890cleanup:2891/*2892 * Some cleanup is deferred until cleanup_renames() because the2893 * data structures are still needed and referenced in2894 * process_entry(). But there are a few things we can free now.2895 */2896initial_cleanup_rename(head_pairs, dir_re_head);2897initial_cleanup_rename(merge_pairs, dir_re_merge);28982899return clean;2900}29012902static voidfinal_cleanup_rename(struct string_list *rename)2903{2904const struct rename *re;2905int i;29062907if(rename == NULL)2908return;29092910for(i =0; i < rename->nr; i++) {2911 re = rename->items[i].util;2912diff_free_filepair(re->pair);2913}2914string_list_clear(rename,1);2915free(rename);2916}29172918static voidfinal_cleanup_renames(struct rename_info *re_info)2919{2920final_cleanup_rename(re_info->head_renames);2921final_cleanup_rename(re_info->merge_renames);2922}29232924static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)2925{2926return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;2927}29282929static intread_oid_strbuf(struct merge_options *opt,2930const struct object_id *oid,2931struct strbuf *dst)2932{2933void*buf;2934enum object_type type;2935unsigned long size;2936 buf =read_object_file(oid, &type, &size);2937if(!buf)2938returnerr(opt,_("cannot read object%s"),oid_to_hex(oid));2939if(type != OBJ_BLOB) {2940free(buf);2941returnerr(opt,_("object%sis not a blob"),oid_to_hex(oid));2942}2943strbuf_attach(dst, buf, size, size +1);2944return0;2945}29462947static intblob_unchanged(struct merge_options *opt,2948const struct object_id *o_oid,2949unsigned o_mode,2950const struct object_id *a_oid,2951unsigned a_mode,2952int renormalize,const char*path)2953{2954struct strbuf obuf = STRBUF_INIT;2955struct strbuf abuf = STRBUF_INIT;2956int ret =0;/* assume changed for safety */2957const struct index_state *idx = opt->repo->index;29582959if(a_mode != o_mode)2960return0;2961if(oid_eq(o_oid, a_oid))2962return1;2963if(!renormalize)2964return0;29652966assert(o_oid && a_oid);2967if(read_oid_strbuf(opt, o_oid, &obuf) ||2968read_oid_strbuf(opt, a_oid, &abuf))2969goto error_return;2970/*2971 * Note: binary | is used so that both renormalizations are2972 * performed. Comparison can be skipped if both files are2973 * unchanged since their sha1s have already been compared.2974 */2975if(renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |2976renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))2977 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));29782979error_return:2980strbuf_release(&obuf);2981strbuf_release(&abuf);2982return ret;2983}29842985static inthandle_modify_delete(struct merge_options *opt,2986const char*path,2987struct object_id *o_oid,int o_mode,2988struct object_id *a_oid,int a_mode,2989struct object_id *b_oid,int b_mode)2990{2991const char*modify_branch, *delete_branch;2992struct object_id *changed_oid;2993int changed_mode;29942995if(a_oid) {2996 modify_branch = opt->branch1;2997 delete_branch = opt->branch2;2998 changed_oid = a_oid;2999 changed_mode = a_mode;3000}else{3001 modify_branch = opt->branch2;3002 delete_branch = opt->branch1;3003 changed_oid = b_oid;3004 changed_mode = b_mode;3005}30063007returnhandle_change_delete(opt,3008 path, NULL,3009 o_oid, o_mode,3010 changed_oid, changed_mode,3011 modify_branch, delete_branch,3012_("modify"),_("modified"));3013}30143015static inthandle_content_merge(struct merge_options *opt,3016const char*path,3017int is_dirty,3018struct object_id *o_oid,int o_mode,3019struct object_id *a_oid,int a_mode,3020struct object_id *b_oid,int b_mode,3021struct rename_conflict_info *ci)3022{3023const char*reason =_("content");3024const char*path1 = NULL, *path2 = NULL;3025struct merge_file_info mfi;3026struct diff_filespec one, a, b;3027unsigned df_conflict_remains =0;30283029if(!o_oid) {3030 reason =_("add/add");3031 o_oid = (struct object_id *)&null_oid;3032}3033 one.path = a.path = b.path = (char*)path;3034oidcpy(&one.oid, o_oid);3035 one.mode = o_mode;3036oidcpy(&a.oid, a_oid);3037 a.mode = a_mode;3038oidcpy(&b.oid, b_oid);3039 b.mode = b_mode;30403041if(ci) {3042struct diff_filepair *pair1 = ci->ren1->pair;30433044 path1 = (opt->branch1 == ci->ren1->branch) ?3045 pair1->two->path : pair1->one->path;3046/* If ci->ren2->pair != NULL, we are in3047 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a3048 * normal rename.3049 */3050 path2 = ((ci->ren2 && ci->ren2->pair) ||3051 opt->branch2 == ci->ren1->branch) ?3052 pair1->two->path : pair1->one->path;3053 one.path = pair1->one->path;3054 a.path = (char*)path1;3055 b.path = (char*)path2;30563057if(dir_in_way(opt->repo->index, path, !opt->call_depth,3058S_ISGITLINK(pair1->two->mode)))3059 df_conflict_remains =1;3060}3061if(merge_mode_and_contents(opt, &one, &a, &b, path,3062 opt->branch1, opt->branch2,3063 opt->call_depth *2, &mfi))3064return-1;30653066/*3067 * We can skip updating the working tree file iff:3068 * a) The merge is clean3069 * b) The merge matches what was in HEAD (content, mode, pathname)3070 * c) The target path is usable (i.e. not involved in D/F conflict)3071 */3072if(mfi.clean &&3073was_tracked_and_matches(opt, path, &mfi.oid, mfi.mode) &&3074!df_conflict_remains) {3075int pos;3076struct cache_entry *ce;30773078output(opt,3,_("Skipped%s(merged same as existing)"), path);3079if(add_cacheinfo(opt, mfi.mode, &mfi.oid, path,30800, (!opt->call_depth && !is_dirty),0))3081return-1;3082/*3083 * However, add_cacheinfo() will delete the old cache entry3084 * and add a new one. We need to copy over any skip_worktree3085 * flag to avoid making the file appear as if it were3086 * deleted by the user.3087 */3088 pos =index_name_pos(&opt->orig_index, path,strlen(path));3089 ce = opt->orig_index.cache[pos];3090if(ce_skip_worktree(ce)) {3091 pos =index_name_pos(opt->repo->index, path,strlen(path));3092 ce = opt->repo->index->cache[pos];3093 ce->ce_flags |= CE_SKIP_WORKTREE;3094}3095return mfi.clean;3096}30973098if(!mfi.clean) {3099if(S_ISGITLINK(mfi.mode))3100 reason =_("submodule");3101output(opt,1,_("CONFLICT (%s): Merge conflict in%s"),3102 reason, path);3103if(ci && !df_conflict_remains)3104if(update_stages(opt, path, &one, &a, &b))3105return-1;3106}31073108if(df_conflict_remains || is_dirty) {3109char*new_path;3110if(opt->call_depth) {3111remove_file_from_index(opt->repo->index, path);3112}else{3113if(!mfi.clean) {3114if(update_stages(opt, path, &one, &a, &b))3115return-1;3116}else{3117int file_from_stage2 =was_tracked(opt, path);3118struct diff_filespec merged;3119oidcpy(&merged.oid, &mfi.oid);3120 merged.mode = mfi.mode;31213122if(update_stages(opt, path, NULL,3123 file_from_stage2 ? &merged : NULL,3124 file_from_stage2 ? NULL : &merged))3125return-1;3126}31273128}3129 new_path =unique_path(opt, path, ci->ren1->branch);3130if(is_dirty) {3131output(opt,1,_("Refusing to lose dirty file at%s"),3132 path);3133}3134output(opt,1,_("Adding as%sinstead"), new_path);3135if(update_file(opt,0, &mfi.oid, mfi.mode, new_path)) {3136free(new_path);3137return-1;3138}3139free(new_path);3140 mfi.clean =0;3141}else if(update_file(opt, mfi.clean, &mfi.oid, mfi.mode, path))3142return-1;3143return!is_dirty && mfi.clean;3144}31453146static inthandle_rename_normal(struct merge_options *opt,3147const char*path,3148struct object_id *o_oid,unsigned int o_mode,3149struct object_id *a_oid,unsigned int a_mode,3150struct object_id *b_oid,unsigned int b_mode,3151struct rename_conflict_info *ci)3152{3153/* Merge the content and write it out */3154returnhandle_content_merge(opt, path,was_dirty(opt, path),3155 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,3156 ci);3157}31583159/* Per entry merge function */3160static intprocess_entry(struct merge_options *opt,3161const char*path,struct stage_data *entry)3162{3163int clean_merge =1;3164int normalize = opt->renormalize;3165unsigned o_mode = entry->stages[1].mode;3166unsigned a_mode = entry->stages[2].mode;3167unsigned b_mode = entry->stages[3].mode;3168struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);3169struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);3170struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);31713172 entry->processed =1;3173if(entry->rename_conflict_info) {3174struct rename_conflict_info *ci = entry->rename_conflict_info;3175switch(ci->rename_type) {3176case RENAME_NORMAL:3177case RENAME_ONE_FILE_TO_ONE:3178 clean_merge =handle_rename_normal(opt,3179 path,3180 o_oid, o_mode,3181 a_oid, a_mode,3182 b_oid, b_mode,3183 ci);3184break;3185case RENAME_VIA_DIR:3186 clean_merge =1;3187if(handle_rename_via_dir(opt, ci->ren1->pair,3188 ci->ren1->branch))3189 clean_merge = -1;3190break;3191case RENAME_ADD:3192/*3193 * Probably unclean merge, but if the renamed file3194 * merges cleanly and the result can then be3195 * two-way merged cleanly with the added file, I3196 * guess it's a clean merge?3197 */3198 clean_merge =handle_rename_add(opt, ci);3199break;3200case RENAME_DELETE:3201 clean_merge =0;3202if(handle_rename_delete(opt, ci->ren1->pair,3203 ci->ren1->branch,3204 ci->ren1->branch == opt->branch1 ? opt->branch2 : opt->branch1))3205 clean_merge = -1;3206break;3207case RENAME_ONE_FILE_TO_TWO:3208 clean_merge =0;3209if(handle_rename_rename_1to2(opt, ci))3210 clean_merge = -1;3211break;3212case RENAME_TWO_FILES_TO_ONE:3213/*3214 * Probably unclean merge, but if the two renamed3215 * files merge cleanly and the two resulting files3216 * can then be two-way merged cleanly, I guess it's3217 * a clean merge?3218 */3219 clean_merge =handle_rename_rename_2to1(opt, ci);3220break;3221default:3222 entry->processed =0;3223break;3224}3225}else if(o_oid && (!a_oid || !b_oid)) {3226/* Case A: Deleted in one */3227if((!a_oid && !b_oid) ||3228(!b_oid &&blob_unchanged(opt, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||3229(!a_oid &&blob_unchanged(opt, o_oid, o_mode, b_oid, b_mode, normalize, path))) {3230/* Deleted in both or deleted in one and3231 * unchanged in the other */3232if(a_oid)3233output(opt,2,_("Removing%s"), path);3234/* do not touch working file if it did not exist */3235remove_file(opt,1, path, !a_oid);3236}else{3237/* Modify/delete; deleted side may have put a directory in the way */3238 clean_merge =0;3239if(handle_modify_delete(opt, path, o_oid, o_mode,3240 a_oid, a_mode, b_oid, b_mode))3241 clean_merge = -1;3242}3243}else if((!o_oid && a_oid && !b_oid) ||3244(!o_oid && !a_oid && b_oid)) {3245/* Case B: Added in one. */3246/* [nothing|directory] -> ([nothing|directory], file) */32473248const char*add_branch;3249const char*other_branch;3250unsigned mode;3251const struct object_id *oid;3252const char*conf;32533254if(a_oid) {3255 add_branch = opt->branch1;3256 other_branch = opt->branch2;3257 mode = a_mode;3258 oid = a_oid;3259 conf =_("file/directory");3260}else{3261 add_branch = opt->branch2;3262 other_branch = opt->branch1;3263 mode = b_mode;3264 oid = b_oid;3265 conf =_("directory/file");3266}3267if(dir_in_way(opt->repo->index, path,3268!opt->call_depth && !S_ISGITLINK(a_mode),32690)) {3270char*new_path =unique_path(opt, path, add_branch);3271 clean_merge =0;3272output(opt,1,_("CONFLICT (%s): There is a directory with name%sin%s. "3273"Adding%sas%s"),3274 conf, path, other_branch, path, new_path);3275if(update_file(opt,0, oid, mode, new_path))3276 clean_merge = -1;3277else if(opt->call_depth)3278remove_file_from_index(opt->repo->index, path);3279free(new_path);3280}else{3281output(opt,2,_("Adding%s"), path);3282/* do not overwrite file if already present */3283if(update_file_flags(opt, oid, mode, path,1, !a_oid))3284 clean_merge = -1;3285}3286}else if(a_oid && b_oid) {3287if(!o_oid) {3288/* Case C: Added in both (check for same permissions) */3289output(opt,1,3290_("CONFLICT (add/add): Merge conflict in%s"),3291 path);3292 clean_merge =handle_file_collision(opt,3293 path, NULL, NULL,3294 opt->branch1,3295 opt->branch2,3296 a_oid, a_mode,3297 b_oid, b_mode);3298}else{3299/* case D: Modified in both, but differently. */3300int is_dirty =0;/* unpack_trees would have bailed if dirty */3301 clean_merge =handle_content_merge(opt, path,3302 is_dirty,3303 o_oid, o_mode,3304 a_oid, a_mode,3305 b_oid, b_mode,3306 NULL);3307}3308}else if(!o_oid && !a_oid && !b_oid) {3309/*3310 * this entry was deleted altogether. a_mode == 0 means3311 * we had that path and want to actively remove it.3312 */3313remove_file(opt,1, path, !a_mode);3314}else3315BUG("fatal merge failure, shouldn't happen.");33163317return clean_merge;3318}33193320intmerge_trees(struct merge_options *opt,3321struct tree *head,3322struct tree *merge,3323struct tree *common,3324struct tree **result)3325{3326struct index_state *istate = opt->repo->index;3327int code, clean;3328struct strbuf sb = STRBUF_INIT;33293330if(!opt->call_depth &&repo_index_has_changes(opt->repo, head, &sb)) {3331err(opt,_("Your local changes to the following files would be overwritten by merge:\n%s"),3332 sb.buf);3333return-1;3334}33353336if(opt->subtree_shift) {3337 merge =shift_tree_object(opt->repo, head, merge, opt->subtree_shift);3338 common =shift_tree_object(opt->repo, head, common, opt->subtree_shift);3339}33403341if(oid_eq(&common->object.oid, &merge->object.oid)) {3342output(opt,0,_("Already up to date!"));3343*result = head;3344return1;3345}33463347 code =unpack_trees_start(opt, common, head, merge);33483349if(code !=0) {3350if(show(opt,4) || opt->call_depth)3351err(opt,_("merging of trees%sand%sfailed"),3352oid_to_hex(&head->object.oid),3353oid_to_hex(&merge->object.oid));3354unpack_trees_finish(opt);3355return-1;3356}33573358if(unmerged_index(istate)) {3359struct string_list *entries;3360struct rename_info re_info;3361int i;3362/*3363 * Only need the hashmap while processing entries, so3364 * initialize it here and free it when we are done running3365 * through the entries. Keeping it in the merge_options as3366 * opposed to decaring a local hashmap is for convenience3367 * so that we don't have to pass it to around.3368 */3369hashmap_init(&opt->current_file_dir_set, path_hashmap_cmp, NULL,512);3370get_files_dirs(opt, head);3371get_files_dirs(opt, merge);33723373 entries =get_unmerged(opt->repo->index);3374 clean =detect_and_process_renames(opt, common, head, merge,3375 entries, &re_info);3376record_df_conflict_files(opt, entries);3377if(clean <0)3378goto cleanup;3379for(i = entries->nr-1;0<= i; i--) {3380const char*path = entries->items[i].string;3381struct stage_data *e = entries->items[i].util;3382if(!e->processed) {3383int ret =process_entry(opt, path, e);3384if(!ret)3385 clean =0;3386else if(ret <0) {3387 clean = ret;3388goto cleanup;3389}3390}3391}3392for(i =0; i < entries->nr; i++) {3393struct stage_data *e = entries->items[i].util;3394if(!e->processed)3395BUG("unprocessed path???%s",3396 entries->items[i].string);3397}33983399 cleanup:3400final_cleanup_renames(&re_info);34013402string_list_clear(entries,1);3403free(entries);34043405hashmap_free(&opt->current_file_dir_set,1);34063407if(clean <0) {3408unpack_trees_finish(opt);3409return clean;3410}3411}3412else3413 clean =1;34143415unpack_trees_finish(opt);34163417if(opt->call_depth && !(*result =write_tree_from_memory(opt)))3418return-1;34193420return clean;3421}34223423static struct commit_list *reverse_commit_list(struct commit_list *list)3424{3425struct commit_list *next = NULL, *current, *backup;3426for(current = list; current; current = backup) {3427 backup = current->next;3428 current->next = next;3429 next = current;3430}3431return next;3432}34333434/*3435 * Merge the commits h1 and h2, return the resulting virtual3436 * commit object and a flag indicating the cleanness of the merge.3437 */3438intmerge_recursive(struct merge_options *opt,3439struct commit *h1,3440struct commit *h2,3441struct commit_list *ca,3442struct commit **result)3443{3444struct commit_list *iter;3445struct commit *merged_common_ancestors;3446struct tree *mrtree;3447int clean;34483449if(show(opt,4)) {3450output(opt,4,_("Merging:"));3451output_commit_title(opt, h1);3452output_commit_title(opt, h2);3453}34543455if(!ca) {3456 ca =get_merge_bases(h1, h2);3457 ca =reverse_commit_list(ca);3458}34593460if(show(opt,5)) {3461unsigned cnt =commit_list_count(ca);34623463output(opt,5,Q_("found%ucommon ancestor:",3464"found%ucommon ancestors:", cnt), cnt);3465for(iter = ca; iter; iter = iter->next)3466output_commit_title(opt, iter->item);3467}34683469 merged_common_ancestors =pop_commit(&ca);3470if(merged_common_ancestors == NULL) {3471/* if there is no common ancestor, use an empty tree */3472struct tree *tree;34733474 tree =lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);3475 merged_common_ancestors =make_virtual_commit(opt->repo, tree,"ancestor");3476}34773478for(iter = ca; iter; iter = iter->next) {3479const char*saved_b1, *saved_b2;3480 opt->call_depth++;3481/*3482 * When the merge fails, the result contains files3483 * with conflict markers. The cleanness flag is3484 * ignored (unless indicating an error), it was never3485 * actually used, as result of merge_trees has always3486 * overwritten it: the committed "conflicts" were3487 * already resolved.3488 */3489discard_index(opt->repo->index);3490 saved_b1 = opt->branch1;3491 saved_b2 = opt->branch2;3492 opt->branch1 ="Temporary merge branch 1";3493 opt->branch2 ="Temporary merge branch 2";3494if(merge_recursive(opt, merged_common_ancestors, iter->item,3495 NULL, &merged_common_ancestors) <0)3496return-1;3497 opt->branch1 = saved_b1;3498 opt->branch2 = saved_b2;3499 opt->call_depth--;35003501if(!merged_common_ancestors)3502returnerr(opt,_("merge returned no commit"));3503}35043505discard_index(opt->repo->index);3506if(!opt->call_depth)3507repo_read_index(opt->repo);35083509 opt->ancestor ="merged common ancestors";3510 clean =merge_trees(opt,get_commit_tree(h1),get_commit_tree(h2),3511get_commit_tree(merged_common_ancestors),3512&mrtree);3513if(clean <0) {3514flush_output(opt);3515return clean;3516}35173518if(opt->call_depth) {3519*result =make_virtual_commit(opt->repo, mrtree,"merged tree");3520commit_list_insert(h1, &(*result)->parents);3521commit_list_insert(h2, &(*result)->parents->next);3522}3523flush_output(opt);3524if(!opt->call_depth && opt->buffer_output <2)3525strbuf_release(&opt->obuf);3526if(show(opt,2))3527diff_warn_rename_limit("merge.renamelimit",3528 opt->needed_rename_limit,0);3529return clean;3530}35313532static struct commit *get_ref(struct repository *repo,const struct object_id *oid,3533const char*name)3534{3535struct object *object;35363537 object =deref_tag(repo,parse_object(repo, oid),3538 name,strlen(name));3539if(!object)3540return NULL;3541if(object->type == OBJ_TREE)3542returnmake_virtual_commit(repo, (struct tree*)object, name);3543if(object->type != OBJ_COMMIT)3544return NULL;3545if(parse_commit((struct commit *)object))3546return NULL;3547return(struct commit *)object;3548}35493550intmerge_recursive_generic(struct merge_options *opt,3551const struct object_id *head,3552const struct object_id *merge,3553int num_base_list,3554const struct object_id **base_list,3555struct commit **result)3556{3557int clean;3558struct lock_file lock = LOCK_INIT;3559struct commit *head_commit =get_ref(opt->repo, head, opt->branch1);3560struct commit *next_commit =get_ref(opt->repo, merge, opt->branch2);3561struct commit_list *ca = NULL;35623563if(base_list) {3564int i;3565for(i =0; i < num_base_list; ++i) {3566struct commit *base;3567if(!(base =get_ref(opt->repo, base_list[i],oid_to_hex(base_list[i]))))3568returnerr(opt,_("Could not parse object '%s'"),3569oid_to_hex(base_list[i]));3570commit_list_insert(base, &ca);3571}3572}35733574repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);3575 clean =merge_recursive(opt, head_commit, next_commit, ca,3576 result);3577if(clean <0) {3578rollback_lock_file(&lock);3579return clean;3580}35813582if(write_locked_index(opt->repo->index, &lock,3583 COMMIT_LOCK | SKIP_IF_UNCHANGED))3584returnerr(opt,_("Unable to write index."));35853586return clean ?0:1;3587}35883589static voidmerge_recursive_config(struct merge_options *opt)3590{3591char*value = NULL;3592git_config_get_int("merge.verbosity", &opt->verbosity);3593git_config_get_int("diff.renamelimit", &opt->diff_rename_limit);3594git_config_get_int("merge.renamelimit", &opt->merge_rename_limit);3595if(!git_config_get_string("diff.renames", &value)) {3596 opt->diff_detect_rename =git_config_rename("diff.renames", value);3597free(value);3598}3599if(!git_config_get_string("merge.renames", &value)) {3600 opt->merge_detect_rename =git_config_rename("merge.renames", value);3601free(value);3602}3603git_config(git_xmerge_config, NULL);3604}36053606voidinit_merge_options(struct merge_options *opt,3607struct repository *repo)3608{3609const char*merge_verbosity;3610memset(opt,0,sizeof(struct merge_options));3611 opt->repo = repo;3612 opt->verbosity =2;3613 opt->buffer_output =1;3614 opt->diff_rename_limit = -1;3615 opt->merge_rename_limit = -1;3616 opt->renormalize =0;3617 opt->diff_detect_rename = -1;3618 opt->merge_detect_rename = -1;3619 opt->detect_directory_renames =1;3620merge_recursive_config(opt);3621 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3622if(merge_verbosity)3623 opt->verbosity =strtol(merge_verbosity, NULL,10);3624if(opt->verbosity >=5)3625 opt->buffer_output =0;3626strbuf_init(&opt->obuf,0);3627string_list_init(&opt->df_conflict_file_set,1);3628}36293630intparse_merge_opt(struct merge_options *opt,const char*s)3631{3632const char*arg;36333634if(!s || !*s)3635return-1;3636if(!strcmp(s,"ours"))3637 opt->recursive_variant = MERGE_RECURSIVE_OURS;3638else if(!strcmp(s,"theirs"))3639 opt->recursive_variant = MERGE_RECURSIVE_THEIRS;3640else if(!strcmp(s,"subtree"))3641 opt->subtree_shift ="";3642else if(skip_prefix(s,"subtree=", &arg))3643 opt->subtree_shift = arg;3644else if(!strcmp(s,"patience"))3645 opt->xdl_opts =DIFF_WITH_ALG(opt, PATIENCE_DIFF);3646else if(!strcmp(s,"histogram"))3647 opt->xdl_opts =DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);3648else if(skip_prefix(s,"diff-algorithm=", &arg)) {3649long value =parse_algorithm_value(arg);3650if(value <0)3651return-1;3652/* clear out previous settings */3653DIFF_XDL_CLR(opt, NEED_MINIMAL);3654 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3655 opt->xdl_opts |= value;3656}3657else if(!strcmp(s,"ignore-space-change"))3658DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);3659else if(!strcmp(s,"ignore-all-space"))3660DIFF_XDL_SET(opt, IGNORE_WHITESPACE);3661else if(!strcmp(s,"ignore-space-at-eol"))3662DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);3663else if(!strcmp(s,"ignore-cr-at-eol"))3664DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);3665else if(!strcmp(s,"renormalize"))3666 opt->renormalize =1;3667else if(!strcmp(s,"no-renormalize"))3668 opt->renormalize =0;3669else if(!strcmp(s,"no-renames"))3670 opt->merge_detect_rename =0;3671else if(!strcmp(s,"find-renames")) {3672 opt->merge_detect_rename =1;3673 opt->rename_score =0;3674}3675else if(skip_prefix(s,"find-renames=", &arg) ||3676skip_prefix(s,"rename-threshold=", &arg)) {3677if((opt->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3678return-1;3679 opt->merge_detect_rename =1;3680}3681/*3682 * Please update $__git_merge_strategy_options in3683 * git-completion.bash when you add new options3684 */3685else3686return-1;3687return0;3688}