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"commit.h" 12#include"blob.h" 13#include"builtin.h" 14#include"tree-walk.h" 15#include"diff.h" 16#include"diffcore.h" 17#include"tag.h" 18#include"unpack-trees.h" 19#include"string-list.h" 20#include"xdiff-interface.h" 21#include"ll-merge.h" 22#include"attr.h" 23#include"merge-recursive.h" 24#include"dir.h" 25#include"submodule.h" 26 27struct path_hashmap_entry { 28struct hashmap_entry e; 29char path[FLEX_ARRAY]; 30}; 31 32static intpath_hashmap_cmp(const void*cmp_data, 33const void*entry, 34const void*entry_or_key, 35const void*keydata) 36{ 37const struct path_hashmap_entry *a = entry; 38const struct path_hashmap_entry *b = entry_or_key; 39const char*key = keydata; 40 41if(ignore_case) 42returnstrcasecmp(a->path, key ? key : b->path); 43else 44returnstrcmp(a->path, key ? key : b->path); 45} 46 47static unsigned intpath_hash(const char*path) 48{ 49return ignore_case ?strihash(path) :strhash(path); 50} 51 52static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap, 53char*dir) 54{ 55struct dir_rename_entry key; 56 57if(dir == NULL) 58return NULL; 59hashmap_entry_init(&key,strhash(dir)); 60 key.dir = dir; 61returnhashmap_get(hashmap, &key, NULL); 62} 63 64static intdir_rename_cmp(const void*unused_cmp_data, 65const void*entry, 66const void*entry_or_key, 67const void*unused_keydata) 68{ 69const struct dir_rename_entry *e1 = entry; 70const struct dir_rename_entry *e2 = entry_or_key; 71 72returnstrcmp(e1->dir, e2->dir); 73} 74 75static voiddir_rename_init(struct hashmap *map) 76{ 77hashmap_init(map, dir_rename_cmp, NULL,0); 78} 79 80static voiddir_rename_entry_init(struct dir_rename_entry *entry, 81char*directory) 82{ 83hashmap_entry_init(entry,strhash(directory)); 84 entry->dir = directory; 85 entry->non_unique_new_dir =0; 86strbuf_init(&entry->new_dir,0); 87string_list_init(&entry->possible_new_dirs,0); 88} 89 90static struct collision_entry *collision_find_entry(struct hashmap *hashmap, 91char*target_file) 92{ 93struct collision_entry key; 94 95hashmap_entry_init(&key,strhash(target_file)); 96 key.target_file = target_file; 97returnhashmap_get(hashmap, &key, NULL); 98} 99 100static intcollision_cmp(void*unused_cmp_data, 101const struct collision_entry *e1, 102const struct collision_entry *e2, 103const void*unused_keydata) 104{ 105returnstrcmp(e1->target_file, e2->target_file); 106} 107 108static voidcollision_init(struct hashmap *map) 109{ 110hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL,0); 111} 112 113static voidflush_output(struct merge_options *o) 114{ 115if(o->buffer_output <2&& o->obuf.len) { 116fputs(o->obuf.buf, stdout); 117strbuf_reset(&o->obuf); 118} 119} 120 121static interr(struct merge_options *o,const char*err, ...) 122{ 123va_list params; 124 125if(o->buffer_output <2) 126flush_output(o); 127else{ 128strbuf_complete(&o->obuf,'\n'); 129strbuf_addstr(&o->obuf,"error: "); 130} 131va_start(params, err); 132strbuf_vaddf(&o->obuf, err, params); 133va_end(params); 134if(o->buffer_output >1) 135strbuf_addch(&o->obuf,'\n'); 136else{ 137error("%s", o->obuf.buf); 138strbuf_reset(&o->obuf); 139} 140 141return-1; 142} 143 144static struct tree *shift_tree_object(struct tree *one,struct tree *two, 145const char*subtree_shift) 146{ 147struct object_id shifted; 148 149if(!*subtree_shift) { 150shift_tree(&one->object.oid, &two->object.oid, &shifted,0); 151}else{ 152shift_tree_by(&one->object.oid, &two->object.oid, &shifted, 153 subtree_shift); 154} 155if(!oidcmp(&two->object.oid, &shifted)) 156return two; 157returnlookup_tree(&shifted); 158} 159 160static struct commit *make_virtual_commit(struct tree *tree,const char*comment) 161{ 162struct commit *commit =alloc_commit_node(); 163 164set_merge_remote_desc(commit, comment, (struct object *)commit); 165 commit->tree = tree; 166 commit->object.parsed =1; 167return commit; 168} 169 170/* 171 * Since we use get_tree_entry(), which does not put the read object into 172 * the object pool, we cannot rely on a == b. 173 */ 174static intoid_eq(const struct object_id *a,const struct object_id *b) 175{ 176if(!a && !b) 177return2; 178return a && b &&oidcmp(a, b) ==0; 179} 180 181enum rename_type { 182 RENAME_NORMAL =0, 183 RENAME_DIR, 184 RENAME_DELETE, 185 RENAME_ONE_FILE_TO_ONE, 186 RENAME_ONE_FILE_TO_TWO, 187 RENAME_TWO_FILES_TO_ONE 188}; 189 190struct rename_conflict_info { 191enum rename_type rename_type; 192struct diff_filepair *pair1; 193struct diff_filepair *pair2; 194const char*branch1; 195const char*branch2; 196struct stage_data *dst_entry1; 197struct stage_data *dst_entry2; 198struct diff_filespec ren1_other; 199struct diff_filespec ren2_other; 200}; 201 202/* 203 * Since we want to write the index eventually, we cannot reuse the index 204 * for these (temporary) data. 205 */ 206struct stage_data { 207struct{ 208unsigned mode; 209struct object_id oid; 210} stages[4]; 211struct rename_conflict_info *rename_conflict_info; 212unsigned processed:1; 213}; 214 215staticinlinevoidsetup_rename_conflict_info(enum rename_type rename_type, 216struct diff_filepair *pair1, 217struct diff_filepair *pair2, 218const char*branch1, 219const char*branch2, 220struct stage_data *dst_entry1, 221struct stage_data *dst_entry2, 222struct merge_options *o, 223struct stage_data *src_entry1, 224struct stage_data *src_entry2) 225{ 226struct rename_conflict_info *ci =xcalloc(1,sizeof(struct rename_conflict_info)); 227 ci->rename_type = rename_type; 228 ci->pair1 = pair1; 229 ci->branch1 = branch1; 230 ci->branch2 = branch2; 231 232 ci->dst_entry1 = dst_entry1; 233 dst_entry1->rename_conflict_info = ci; 234 dst_entry1->processed =0; 235 236assert(!pair2 == !dst_entry2); 237if(dst_entry2) { 238 ci->dst_entry2 = dst_entry2; 239 ci->pair2 = pair2; 240 dst_entry2->rename_conflict_info = ci; 241} 242 243if(rename_type == RENAME_TWO_FILES_TO_ONE) { 244/* 245 * For each rename, there could have been 246 * modifications on the side of history where that 247 * file was not renamed. 248 */ 249int ostage1 = o->branch1 == branch1 ?3:2; 250int ostage2 = ostage1 ^1; 251 252 ci->ren1_other.path = pair1->one->path; 253oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid); 254 ci->ren1_other.mode = src_entry1->stages[ostage1].mode; 255 256 ci->ren2_other.path = pair2->one->path; 257oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid); 258 ci->ren2_other.mode = src_entry2->stages[ostage2].mode; 259} 260} 261 262static intshow(struct merge_options *o,int v) 263{ 264return(!o->call_depth && o->verbosity >= v) || o->verbosity >=5; 265} 266 267__attribute__((format(printf,3,4))) 268static voidoutput(struct merge_options *o,int v,const char*fmt, ...) 269{ 270va_list ap; 271 272if(!show(o, v)) 273return; 274 275strbuf_addchars(&o->obuf,' ', o->call_depth *2); 276 277va_start(ap, fmt); 278strbuf_vaddf(&o->obuf, fmt, ap); 279va_end(ap); 280 281strbuf_addch(&o->obuf,'\n'); 282if(!o->buffer_output) 283flush_output(o); 284} 285 286static voidoutput_commit_title(struct merge_options *o,struct commit *commit) 287{ 288strbuf_addchars(&o->obuf,' ', o->call_depth *2); 289if(commit->util) 290strbuf_addf(&o->obuf,"virtual%s\n", 291merge_remote_util(commit)->name); 292else{ 293strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid, 294 DEFAULT_ABBREV); 295strbuf_addch(&o->obuf,' '); 296if(parse_commit(commit) !=0) 297strbuf_addstr(&o->obuf,_("(bad commit)\n")); 298else{ 299const char*title; 300const char*msg =get_commit_buffer(commit, NULL); 301int len =find_commit_subject(msg, &title); 302if(len) 303strbuf_addf(&o->obuf,"%.*s\n", len, title); 304unuse_commit_buffer(commit, msg); 305} 306} 307flush_output(o); 308} 309 310static intadd_cacheinfo(struct merge_options *o, 311unsigned int mode,const struct object_id *oid, 312const char*path,int stage,int refresh,int options) 313{ 314struct cache_entry *ce; 315int ret; 316 317 ce =make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage,0); 318if(!ce) 319returnerr(o,_("addinfo_cache failed for path '%s'"), path); 320 321 ret =add_cache_entry(ce, options); 322if(refresh) { 323struct cache_entry *nce; 324 325 nce =refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); 326if(!nce) 327returnerr(o,_("addinfo_cache failed for path '%s'"), path); 328if(nce != ce) 329 ret =add_cache_entry(nce, options); 330} 331return ret; 332} 333 334static voidinit_tree_desc_from_tree(struct tree_desc *desc,struct tree *tree) 335{ 336parse_tree(tree); 337init_tree_desc(desc, tree->buffer, tree->size); 338} 339 340static intgit_merge_trees(int index_only, 341struct tree *common, 342struct tree *head, 343struct tree *merge) 344{ 345int rc; 346struct tree_desc t[3]; 347struct unpack_trees_options opts; 348 349memset(&opts,0,sizeof(opts)); 350if(index_only) 351 opts.index_only =1; 352else 353 opts.update =1; 354 opts.merge =1; 355 opts.head_idx =2; 356 opts.fn = threeway_merge; 357 opts.src_index = &the_index; 358 opts.dst_index = &the_index; 359setup_unpack_trees_porcelain(&opts,"merge"); 360 361init_tree_desc_from_tree(t+0, common); 362init_tree_desc_from_tree(t+1, head); 363init_tree_desc_from_tree(t+2, merge); 364 365 rc =unpack_trees(3, t, &opts); 366cache_tree_free(&active_cache_tree); 367return rc; 368} 369 370struct tree *write_tree_from_memory(struct merge_options *o) 371{ 372struct tree *result = NULL; 373 374if(unmerged_cache()) { 375int i; 376fprintf(stderr,"BUG: There are unmerged index entries:\n"); 377for(i =0; i < active_nr; i++) { 378const struct cache_entry *ce = active_cache[i]; 379if(ce_stage(ce)) 380fprintf(stderr,"BUG:%d%.*s\n",ce_stage(ce), 381(int)ce_namelen(ce), ce->name); 382} 383die("BUG: unmerged index entries in merge-recursive.c"); 384} 385 386if(!active_cache_tree) 387 active_cache_tree =cache_tree(); 388 389if(!cache_tree_fully_valid(active_cache_tree) && 390cache_tree_update(&the_index,0) <0) { 391err(o,_("error building trees")); 392return NULL; 393} 394 395 result =lookup_tree(&active_cache_tree->oid); 396 397return result; 398} 399 400static intsave_files_dirs(const struct object_id *oid, 401struct strbuf *base,const char*path, 402unsigned int mode,int stage,void*context) 403{ 404struct path_hashmap_entry *entry; 405int baselen = base->len; 406struct merge_options *o = context; 407 408strbuf_addstr(base, path); 409 410FLEX_ALLOC_MEM(entry, path, base->buf, base->len); 411hashmap_entry_init(entry,path_hash(entry->path)); 412hashmap_add(&o->current_file_dir_set, entry); 413 414strbuf_setlen(base, baselen); 415return(S_ISDIR(mode) ? READ_TREE_RECURSIVE :0); 416} 417 418static voidget_files_dirs(struct merge_options *o,struct tree *tree) 419{ 420struct pathspec match_all; 421memset(&match_all,0,sizeof(match_all)); 422read_tree_recursive(tree,"",0,0, &match_all, save_files_dirs, o); 423} 424 425static intget_tree_entry_if_blob(const struct object_id *tree, 426const char*path, 427struct object_id *hashy, 428unsigned int*mode_o) 429{ 430int ret; 431 432 ret =get_tree_entry(tree, path, hashy, mode_o); 433if(S_ISDIR(*mode_o)) { 434oidcpy(hashy, &null_oid); 435*mode_o =0; 436} 437return ret; 438} 439 440/* 441 * Returns an index_entry instance which doesn't have to correspond to 442 * a real cache entry in Git's index. 443 */ 444static struct stage_data *insert_stage_data(const char*path, 445struct tree *o,struct tree *a,struct tree *b, 446struct string_list *entries) 447{ 448struct string_list_item *item; 449struct stage_data *e =xcalloc(1,sizeof(struct stage_data)); 450get_tree_entry_if_blob(&o->object.oid, path, 451&e->stages[1].oid, &e->stages[1].mode); 452get_tree_entry_if_blob(&a->object.oid, path, 453&e->stages[2].oid, &e->stages[2].mode); 454get_tree_entry_if_blob(&b->object.oid, path, 455&e->stages[3].oid, &e->stages[3].mode); 456 item =string_list_insert(entries, path); 457 item->util = e; 458return e; 459} 460 461/* 462 * Create a dictionary mapping file names to stage_data objects. The 463 * dictionary contains one entry for every path with a non-zero stage entry. 464 */ 465static struct string_list *get_unmerged(void) 466{ 467struct string_list *unmerged =xcalloc(1,sizeof(struct string_list)); 468int i; 469 470 unmerged->strdup_strings =1; 471 472for(i =0; i < active_nr; i++) { 473struct string_list_item *item; 474struct stage_data *e; 475const struct cache_entry *ce = active_cache[i]; 476if(!ce_stage(ce)) 477continue; 478 479 item =string_list_lookup(unmerged, ce->name); 480if(!item) { 481 item =string_list_insert(unmerged, ce->name); 482 item->util =xcalloc(1,sizeof(struct stage_data)); 483} 484 e = item->util; 485 e->stages[ce_stage(ce)].mode = ce->ce_mode; 486oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); 487} 488 489return unmerged; 490} 491 492static intstring_list_df_name_compare(const char*one,const char*two) 493{ 494int onelen =strlen(one); 495int twolen =strlen(two); 496/* 497 * Here we only care that entries for D/F conflicts are 498 * adjacent, in particular with the file of the D/F conflict 499 * appearing before files below the corresponding directory. 500 * The order of the rest of the list is irrelevant for us. 501 * 502 * To achieve this, we sort with df_name_compare and provide 503 * the mode S_IFDIR so that D/F conflicts will sort correctly. 504 * We use the mode S_IFDIR for everything else for simplicity, 505 * since in other cases any changes in their order due to 506 * sorting cause no problems for us. 507 */ 508int cmp =df_name_compare(one, onelen, S_IFDIR, 509 two, twolen, S_IFDIR); 510/* 511 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure 512 * that 'foo' comes before 'foo/bar'. 513 */ 514if(cmp) 515return cmp; 516return onelen - twolen; 517} 518 519static voidrecord_df_conflict_files(struct merge_options *o, 520struct string_list *entries) 521{ 522/* If there is a D/F conflict and the file for such a conflict 523 * currently exist in the working tree, we want to allow it to be 524 * removed to make room for the corresponding directory if needed. 525 * The files underneath the directories of such D/F conflicts will 526 * be processed before the corresponding file involved in the D/F 527 * conflict. If the D/F directory ends up being removed by the 528 * merge, then we won't have to touch the D/F file. If the D/F 529 * directory needs to be written to the working copy, then the D/F 530 * file will simply be removed (in make_room_for_path()) to make 531 * room for the necessary paths. Note that if both the directory 532 * and the file need to be present, then the D/F file will be 533 * reinstated with a new unique name at the time it is processed. 534 */ 535struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; 536const char*last_file = NULL; 537int last_len =0; 538int i; 539 540/* 541 * If we're merging merge-bases, we don't want to bother with 542 * any working directory changes. 543 */ 544if(o->call_depth) 545return; 546 547/* Ensure D/F conflicts are adjacent in the entries list. */ 548for(i =0; i < entries->nr; i++) { 549struct string_list_item *next = &entries->items[i]; 550string_list_append(&df_sorted_entries, next->string)->util = 551 next->util; 552} 553 df_sorted_entries.cmp = string_list_df_name_compare; 554string_list_sort(&df_sorted_entries); 555 556string_list_clear(&o->df_conflict_file_set,1); 557for(i =0; i < df_sorted_entries.nr; i++) { 558const char*path = df_sorted_entries.items[i].string; 559int len =strlen(path); 560struct stage_data *e = df_sorted_entries.items[i].util; 561 562/* 563 * Check if last_file & path correspond to a D/F conflict; 564 * i.e. whether path is last_file+'/'+<something>. 565 * If so, record that it's okay to remove last_file to make 566 * room for path and friends if needed. 567 */ 568if(last_file && 569 len > last_len && 570memcmp(path, last_file, last_len) ==0&& 571 path[last_len] =='/') { 572string_list_insert(&o->df_conflict_file_set, last_file); 573} 574 575/* 576 * Determine whether path could exist as a file in the 577 * working directory as a possible D/F conflict. This 578 * will only occur when it exists in stage 2 as a 579 * file. 580 */ 581if(S_ISREG(e->stages[2].mode) ||S_ISLNK(e->stages[2].mode)) { 582 last_file = path; 583 last_len = len; 584}else{ 585 last_file = NULL; 586} 587} 588string_list_clear(&df_sorted_entries,0); 589} 590 591struct rename { 592struct diff_filepair *pair; 593/* 594 * Purpose of src_entry and dst_entry: 595 * 596 * If 'before' is renamed to 'after' then src_entry will contain 597 * the versions of 'before' from the merge_base, HEAD, and MERGE in 598 * stages 1, 2, and 3; dst_entry will contain the respective 599 * versions of 'after' in corresponding locations. Thus, we have a 600 * total of six modes and oids, though some will be null. (Stage 0 601 * is ignored; we're interested in handling conflicts.) 602 * 603 * Since we don't turn on break-rewrites by default, neither 604 * src_entry nor dst_entry can have all three of their stages have 605 * non-null oids, meaning at most four of the six will be non-null. 606 * Also, since this is a rename, both src_entry and dst_entry will 607 * have at least one non-null oid, meaning at least two will be 608 * non-null. Of the six oids, a typical rename will have three be 609 * non-null. Only two implies a rename/delete, and four implies a 610 * rename/add. 611 */ 612struct stage_data *src_entry; 613struct stage_data *dst_entry; 614unsigned add_turned_into_rename:1; 615unsigned processed:1; 616}; 617 618static intupdate_stages(struct merge_options *opt,const char*path, 619const struct diff_filespec *o, 620const struct diff_filespec *a, 621const struct diff_filespec *b) 622{ 623 624/* 625 * NOTE: It is usually a bad idea to call update_stages on a path 626 * before calling update_file on that same path, since it can 627 * sometimes lead to spurious "refusing to lose untracked file..." 628 * messages from update_file (via make_room_for path via 629 * would_lose_untracked). Instead, reverse the order of the calls 630 * (executing update_file first and then update_stages). 631 */ 632int clear =1; 633int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; 634if(clear) 635if(remove_file_from_cache(path)) 636return-1; 637if(o) 638if(add_cacheinfo(opt, o->mode, &o->oid, path,1,0, options)) 639return-1; 640if(a) 641if(add_cacheinfo(opt, a->mode, &a->oid, path,2,0, options)) 642return-1; 643if(b) 644if(add_cacheinfo(opt, b->mode, &b->oid, path,3,0, options)) 645return-1; 646return0; 647} 648 649static intupdate_stages_for_stage_data(struct merge_options *opt, 650const char*path, 651const struct stage_data *stage_data) 652{ 653struct diff_filespec o, a, b; 654 655 o.mode = stage_data->stages[1].mode; 656oidcpy(&o.oid, &stage_data->stages[1].oid); 657 658 a.mode = stage_data->stages[2].mode; 659oidcpy(&a.oid, &stage_data->stages[2].oid); 660 661 b.mode = stage_data->stages[3].mode; 662oidcpy(&b.oid, &stage_data->stages[3].oid); 663 664returnupdate_stages(opt, path, 665is_null_oid(&o.oid) ? NULL : &o, 666is_null_oid(&a.oid) ? NULL : &a, 667is_null_oid(&b.oid) ? NULL : &b); 668} 669 670static voidupdate_entry(struct stage_data *entry, 671struct diff_filespec *o, 672struct diff_filespec *a, 673struct diff_filespec *b) 674{ 675 entry->processed =0; 676 entry->stages[1].mode = o->mode; 677 entry->stages[2].mode = a->mode; 678 entry->stages[3].mode = b->mode; 679oidcpy(&entry->stages[1].oid, &o->oid); 680oidcpy(&entry->stages[2].oid, &a->oid); 681oidcpy(&entry->stages[3].oid, &b->oid); 682} 683 684static intremove_file(struct merge_options *o,int clean, 685const char*path,int no_wd) 686{ 687int update_cache = o->call_depth || clean; 688int update_working_directory = !o->call_depth && !no_wd; 689 690if(update_cache) { 691if(remove_file_from_cache(path)) 692return-1; 693} 694if(update_working_directory) { 695if(ignore_case) { 696struct cache_entry *ce; 697 ce =cache_file_exists(path,strlen(path), ignore_case); 698if(ce &&ce_stage(ce) ==0&&strcmp(path, ce->name)) 699return0; 700} 701if(remove_path(path)) 702return-1; 703} 704return0; 705} 706 707/* add a string to a strbuf, but converting "/" to "_" */ 708static voidadd_flattened_path(struct strbuf *out,const char*s) 709{ 710size_t i = out->len; 711strbuf_addstr(out, s); 712for(; i < out->len; i++) 713if(out->buf[i] =='/') 714 out->buf[i] ='_'; 715} 716 717static char*unique_path(struct merge_options *o,const char*path,const char*branch) 718{ 719struct path_hashmap_entry *entry; 720struct strbuf newpath = STRBUF_INIT; 721int suffix =0; 722size_t base_len; 723 724strbuf_addf(&newpath,"%s~", path); 725add_flattened_path(&newpath, branch); 726 727 base_len = newpath.len; 728while(hashmap_get_from_hash(&o->current_file_dir_set, 729path_hash(newpath.buf), newpath.buf) || 730(!o->call_depth &&file_exists(newpath.buf))) { 731strbuf_setlen(&newpath, base_len); 732strbuf_addf(&newpath,"_%d", suffix++); 733} 734 735FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); 736hashmap_entry_init(entry,path_hash(entry->path)); 737hashmap_add(&o->current_file_dir_set, entry); 738returnstrbuf_detach(&newpath, NULL); 739} 740 741/** 742 * Check whether a directory in the index is in the way of an incoming 743 * file. Return 1 if so. If check_working_copy is non-zero, also 744 * check the working directory. If empty_ok is non-zero, also return 745 * 0 in the case where the working-tree dir exists but is empty. 746 */ 747static intdir_in_way(const char*path,int check_working_copy,int empty_ok) 748{ 749int pos; 750struct strbuf dirpath = STRBUF_INIT; 751struct stat st; 752 753strbuf_addstr(&dirpath, path); 754strbuf_addch(&dirpath,'/'); 755 756 pos =cache_name_pos(dirpath.buf, dirpath.len); 757 758if(pos <0) 759 pos = -1- pos; 760if(pos < active_nr && 761!strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) { 762strbuf_release(&dirpath); 763return1; 764} 765 766strbuf_release(&dirpath); 767return check_working_copy && !lstat(path, &st) &&S_ISDIR(st.st_mode) && 768!(empty_ok &&is_empty_dir(path)); 769} 770 771static intwas_tracked(const char*path) 772{ 773int pos =cache_name_pos(path,strlen(path)); 774 775if(0<= pos) 776/* we have been tracking this path */ 777return1; 778 779/* 780 * Look for an unmerged entry for the path, 781 * specifically stage #2, which would indicate 782 * that "our" side before the merge started 783 * had the path tracked (and resulted in a conflict). 784 */ 785for(pos = -1- pos; 786 pos < active_nr && !strcmp(path, active_cache[pos]->name); 787 pos++) 788if(ce_stage(active_cache[pos]) ==2) 789return1; 790return0; 791} 792 793static intwould_lose_untracked(const char*path) 794{ 795return!was_tracked(path) &&file_exists(path); 796} 797 798static intmake_room_for_path(struct merge_options *o,const char*path) 799{ 800int status, i; 801const char*msg =_("failed to create path '%s'%s"); 802 803/* Unlink any D/F conflict files that are in the way */ 804for(i =0; i < o->df_conflict_file_set.nr; i++) { 805const char*df_path = o->df_conflict_file_set.items[i].string; 806size_t pathlen =strlen(path); 807size_t df_pathlen =strlen(df_path); 808if(df_pathlen < pathlen && 809 path[df_pathlen] =='/'&& 810strncmp(path, df_path, df_pathlen) ==0) { 811output(o,3, 812_("Removing%sto make room for subdirectory\n"), 813 df_path); 814unlink(df_path); 815unsorted_string_list_delete_item(&o->df_conflict_file_set, 816 i,0); 817break; 818} 819} 820 821/* Make sure leading directories are created */ 822 status =safe_create_leading_directories_const(path); 823if(status) { 824if(status == SCLD_EXISTS) 825/* something else exists */ 826returnerr(o, msg, path,_(": perhaps a D/F conflict?")); 827returnerr(o, msg, path,""); 828} 829 830/* 831 * Do not unlink a file in the work tree if we are not 832 * tracking it. 833 */ 834if(would_lose_untracked(path)) 835returnerr(o,_("refusing to lose untracked file at '%s'"), 836 path); 837 838/* Successful unlink is good.. */ 839if(!unlink(path)) 840return0; 841/* .. and so is no existing file */ 842if(errno == ENOENT) 843return0; 844/* .. but not some other error (who really cares what?) */ 845returnerr(o, msg, path,_(": perhaps a D/F conflict?")); 846} 847 848static intupdate_file_flags(struct merge_options *o, 849const struct object_id *oid, 850unsigned mode, 851const char*path, 852int update_cache, 853int update_wd) 854{ 855int ret =0; 856 857if(o->call_depth) 858 update_wd =0; 859 860if(update_wd) { 861enum object_type type; 862void*buf; 863unsigned long size; 864 865if(S_ISGITLINK(mode)) { 866/* 867 * We may later decide to recursively descend into 868 * the submodule directory and update its index 869 * and/or work tree, but we do not do that now. 870 */ 871 update_wd =0; 872goto update_index; 873} 874 875 buf =read_object_file(oid, &type, &size); 876if(!buf) 877returnerr(o,_("cannot read object%s'%s'"),oid_to_hex(oid), path); 878if(type != OBJ_BLOB) { 879 ret =err(o,_("blob expected for%s'%s'"),oid_to_hex(oid), path); 880goto free_buf; 881} 882if(S_ISREG(mode)) { 883struct strbuf strbuf = STRBUF_INIT; 884if(convert_to_working_tree(path, buf, size, &strbuf)) { 885free(buf); 886 size = strbuf.len; 887 buf =strbuf_detach(&strbuf, NULL); 888} 889} 890 891if(make_room_for_path(o, path) <0) { 892 update_wd =0; 893goto free_buf; 894} 895if(S_ISREG(mode) || (!has_symlinks &&S_ISLNK(mode))) { 896int fd; 897if(mode &0100) 898 mode =0777; 899else 900 mode =0666; 901 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); 902if(fd <0) { 903 ret =err(o,_("failed to open '%s':%s"), 904 path,strerror(errno)); 905goto free_buf; 906} 907write_in_full(fd, buf, size); 908close(fd); 909}else if(S_ISLNK(mode)) { 910char*lnk =xmemdupz(buf, size); 911safe_create_leading_directories_const(path); 912unlink(path); 913if(symlink(lnk, path)) 914 ret =err(o,_("failed to symlink '%s':%s"), 915 path,strerror(errno)); 916free(lnk); 917}else 918 ret =err(o, 919_("do not know what to do with%06o%s'%s'"), 920 mode,oid_to_hex(oid), path); 921 free_buf: 922free(buf); 923} 924 update_index: 925if(!ret && update_cache) 926add_cacheinfo(o, mode, oid, path,0, update_wd, ADD_CACHE_OK_TO_ADD); 927return ret; 928} 929 930static intupdate_file(struct merge_options *o, 931int clean, 932const struct object_id *oid, 933unsigned mode, 934const char*path) 935{ 936returnupdate_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth); 937} 938 939/* Low level file merging, update and removal */ 940 941struct merge_file_info { 942struct object_id oid; 943unsigned mode; 944unsigned clean:1, 945 merge:1; 946}; 947 948static intmerge_3way(struct merge_options *o, 949 mmbuffer_t *result_buf, 950const struct diff_filespec *one, 951const struct diff_filespec *a, 952const struct diff_filespec *b, 953const char*branch1, 954const char*branch2) 955{ 956 mmfile_t orig, src1, src2; 957struct ll_merge_options ll_opts = {0}; 958char*base_name, *name1, *name2; 959int merge_status; 960 961 ll_opts.renormalize = o->renormalize; 962 ll_opts.xdl_opts = o->xdl_opts; 963 964if(o->call_depth) { 965 ll_opts.virtual_ancestor =1; 966 ll_opts.variant =0; 967}else{ 968switch(o->recursive_variant) { 969case MERGE_RECURSIVE_OURS: 970 ll_opts.variant = XDL_MERGE_FAVOR_OURS; 971break; 972case MERGE_RECURSIVE_THEIRS: 973 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS; 974break; 975default: 976 ll_opts.variant =0; 977break; 978} 979} 980 981if(strcmp(a->path, b->path) || 982(o->ancestor != NULL &&strcmp(a->path, one->path) !=0)) { 983 base_name = o->ancestor == NULL ? NULL : 984mkpathdup("%s:%s", o->ancestor, one->path); 985 name1 =mkpathdup("%s:%s", branch1, a->path); 986 name2 =mkpathdup("%s:%s", branch2, b->path); 987}else{ 988 base_name = o->ancestor == NULL ? NULL : 989mkpathdup("%s", o->ancestor); 990 name1 =mkpathdup("%s", branch1); 991 name2 =mkpathdup("%s", branch2); 992} 993 994read_mmblob(&orig, &one->oid); 995read_mmblob(&src1, &a->oid); 996read_mmblob(&src2, &b->oid); 997 998 merge_status =ll_merge(result_buf, a->path, &orig, base_name, 999&src1, name1, &src2, name2, &ll_opts);10001001free(base_name);1002free(name1);1003free(name2);1004free(orig.ptr);1005free(src1.ptr);1006free(src2.ptr);1007return merge_status;1008}10091010static intmerge_file_1(struct merge_options *o,1011const struct diff_filespec *one,1012const struct diff_filespec *a,1013const struct diff_filespec *b,1014const char*branch1,1015const char*branch2,1016struct merge_file_info *result)1017{1018 result->merge =0;1019 result->clean =1;10201021if((S_IFMT & a->mode) != (S_IFMT & b->mode)) {1022 result->clean =0;1023if(S_ISREG(a->mode)) {1024 result->mode = a->mode;1025oidcpy(&result->oid, &a->oid);1026}else{1027 result->mode = b->mode;1028oidcpy(&result->oid, &b->oid);1029}1030}else{1031if(!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))1032 result->merge =1;10331034/*1035 * Merge modes1036 */1037if(a->mode == b->mode || a->mode == one->mode)1038 result->mode = b->mode;1039else{1040 result->mode = a->mode;1041if(b->mode != one->mode) {1042 result->clean =0;1043 result->merge =1;1044}1045}10461047if(oid_eq(&a->oid, &b->oid) ||oid_eq(&a->oid, &one->oid))1048oidcpy(&result->oid, &b->oid);1049else if(oid_eq(&b->oid, &one->oid))1050oidcpy(&result->oid, &a->oid);1051else if(S_ISREG(a->mode)) {1052 mmbuffer_t result_buf;1053int ret =0, merge_status;10541055 merge_status =merge_3way(o, &result_buf, one, a, b,1056 branch1, branch2);10571058if((merge_status <0) || !result_buf.ptr)1059 ret =err(o,_("Failed to execute internal merge"));10601061if(!ret &&1062write_object_file(result_buf.ptr, result_buf.size,1063 blob_type, &result->oid))1064 ret =err(o,_("Unable to add%sto database"),1065 a->path);10661067free(result_buf.ptr);1068if(ret)1069return ret;1070 result->clean = (merge_status ==0);1071}else if(S_ISGITLINK(a->mode)) {1072 result->clean =merge_submodule(&result->oid,1073 one->path,1074&one->oid,1075&a->oid,1076&b->oid,1077!o->call_depth);1078}else if(S_ISLNK(a->mode)) {1079switch(o->recursive_variant) {1080case MERGE_RECURSIVE_NORMAL:1081oidcpy(&result->oid, &a->oid);1082if(!oid_eq(&a->oid, &b->oid))1083 result->clean =0;1084break;1085case MERGE_RECURSIVE_OURS:1086oidcpy(&result->oid, &a->oid);1087break;1088case MERGE_RECURSIVE_THEIRS:1089oidcpy(&result->oid, &b->oid);1090break;1091}1092}else1093die("BUG: unsupported object type in the tree");1094}10951096return0;1097}10981099static intmerge_file_special_markers(struct merge_options *o,1100const struct diff_filespec *one,1101const struct diff_filespec *a,1102const struct diff_filespec *b,1103const char*branch1,1104const char*filename1,1105const char*branch2,1106const char*filename2,1107struct merge_file_info *mfi)1108{1109char*side1 = NULL;1110char*side2 = NULL;1111int ret;11121113if(filename1)1114 side1 =xstrfmt("%s:%s", branch1, filename1);1115if(filename2)1116 side2 =xstrfmt("%s:%s", branch2, filename2);11171118 ret =merge_file_1(o, one, a, b,1119 side1 ? side1 : branch1,1120 side2 ? side2 : branch2, mfi);1121free(side1);1122free(side2);1123return ret;1124}11251126static intmerge_file_one(struct merge_options *o,1127const char*path,1128const struct object_id *o_oid,int o_mode,1129const struct object_id *a_oid,int a_mode,1130const struct object_id *b_oid,int b_mode,1131const char*branch1,1132const char*branch2,1133struct merge_file_info *mfi)1134{1135struct diff_filespec one, a, b;11361137 one.path = a.path = b.path = (char*)path;1138oidcpy(&one.oid, o_oid);1139 one.mode = o_mode;1140oidcpy(&a.oid, a_oid);1141 a.mode = a_mode;1142oidcpy(&b.oid, b_oid);1143 b.mode = b_mode;1144returnmerge_file_1(o, &one, &a, &b, branch1, branch2, mfi);1145}11461147static intconflict_rename_dir(struct merge_options *o,1148struct diff_filepair *pair,1149const char*rename_branch,1150const char*other_branch)1151{1152const struct diff_filespec *dest = pair->two;11531154if(!o->call_depth &&would_lose_untracked(dest->path)) {1155char*alt_path =unique_path(o, dest->path, rename_branch);11561157output(o,1,_("Error: Refusing to lose untracked file at%s; "1158"writing to%sinstead."),1159 dest->path, alt_path);1160/*1161 * Write the file in worktree at alt_path, but not in the1162 * index. Instead, write to dest->path for the index but1163 * only at the higher appropriate stage.1164 */1165if(update_file(o,0, &dest->oid, dest->mode, alt_path))1166return-1;1167free(alt_path);1168returnupdate_stages(o, dest->path, NULL,1169 rename_branch == o->branch1 ? dest : NULL,1170 rename_branch == o->branch1 ? NULL : dest);1171}11721173/* Update dest->path both in index and in worktree */1174if(update_file(o,1, &dest->oid, dest->mode, dest->path))1175return-1;1176return0;1177}11781179static inthandle_change_delete(struct merge_options *o,1180const char*path,const char*old_path,1181const struct object_id *o_oid,int o_mode,1182const struct object_id *changed_oid,1183int changed_mode,1184const char*change_branch,1185const char*delete_branch,1186const char*change,const char*change_past)1187{1188char*alt_path = NULL;1189const char*update_path = path;1190int ret =0;11911192if(dir_in_way(path, !o->call_depth,0) ||1193(!o->call_depth &&would_lose_untracked(path))) {1194 update_path = alt_path =unique_path(o, path, change_branch);1195}11961197if(o->call_depth) {1198/*1199 * We cannot arbitrarily accept either a_sha or b_sha as1200 * correct; since there is no true "middle point" between1201 * them, simply reuse the base version for virtual merge base.1202 */1203 ret =remove_file_from_cache(path);1204if(!ret)1205 ret =update_file(o,0, o_oid, o_mode, update_path);1206}else{1207if(!alt_path) {1208if(!old_path) {1209output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1210"and%sin%s. Version%sof%sleft in tree."),1211 change, path, delete_branch, change_past,1212 change_branch, change_branch, path);1213}else{1214output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1215"and%sto%sin%s. Version%sof%sleft in tree."),1216 change, old_path, delete_branch, change_past, path,1217 change_branch, change_branch, path);1218}1219}else{1220if(!old_path) {1221output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1222"and%sin%s. Version%sof%sleft in tree at%s."),1223 change, path, delete_branch, change_past,1224 change_branch, change_branch, path, alt_path);1225}else{1226output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1227"and%sto%sin%s. Version%sof%sleft in tree at%s."),1228 change, old_path, delete_branch, change_past, path,1229 change_branch, change_branch, path, alt_path);1230}1231}1232/*1233 * No need to call update_file() on path when change_branch ==1234 * o->branch1 && !alt_path, since that would needlessly touch1235 * path. We could call update_file_flags() with update_cache=01236 * and update_wd=0, but that's a no-op.1237 */1238if(change_branch != o->branch1 || alt_path)1239 ret =update_file(o,0, changed_oid, changed_mode, update_path);1240}1241free(alt_path);12421243return ret;1244}12451246static intconflict_rename_delete(struct merge_options *o,1247struct diff_filepair *pair,1248const char*rename_branch,1249const char*delete_branch)1250{1251const struct diff_filespec *orig = pair->one;1252const struct diff_filespec *dest = pair->two;12531254if(handle_change_delete(o,1255 o->call_depth ? orig->path : dest->path,1256 o->call_depth ? NULL : orig->path,1257&orig->oid, orig->mode,1258&dest->oid, dest->mode,1259 rename_branch, delete_branch,1260_("rename"),_("renamed")))1261return-1;12621263if(o->call_depth)1264returnremove_file_from_cache(dest->path);1265else1266returnupdate_stages(o, dest->path, NULL,1267 rename_branch == o->branch1 ? dest : NULL,1268 rename_branch == o->branch1 ? NULL : dest);1269}12701271static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1272struct stage_data *entry,1273int stage)1274{1275struct object_id *oid = &entry->stages[stage].oid;1276unsigned mode = entry->stages[stage].mode;1277if(mode ==0||is_null_oid(oid))1278return NULL;1279oidcpy(&target->oid, oid);1280 target->mode = mode;1281return target;1282}12831284static inthandle_file(struct merge_options *o,1285struct diff_filespec *rename,1286int stage,1287struct rename_conflict_info *ci)1288{1289char*dst_name = rename->path;1290struct stage_data *dst_entry;1291const char*cur_branch, *other_branch;1292struct diff_filespec other;1293struct diff_filespec *add;1294int ret;12951296if(stage ==2) {1297 dst_entry = ci->dst_entry1;1298 cur_branch = ci->branch1;1299 other_branch = ci->branch2;1300}else{1301 dst_entry = ci->dst_entry2;1302 cur_branch = ci->branch2;1303 other_branch = ci->branch1;1304}13051306 add =filespec_from_entry(&other, dst_entry, stage ^1);1307if(add) {1308char*add_name =unique_path(o, rename->path, other_branch);1309if(update_file(o,0, &add->oid, add->mode, add_name))1310return-1;13111312remove_file(o,0, rename->path,0);1313 dst_name =unique_path(o, rename->path, cur_branch);1314}else{1315if(dir_in_way(rename->path, !o->call_depth,0)) {1316 dst_name =unique_path(o, rename->path, cur_branch);1317output(o,1,_("%sis a directory in%sadding as%sinstead"),1318 rename->path, other_branch, dst_name);1319}else if(!o->call_depth &&1320would_lose_untracked(rename->path)) {1321 dst_name =unique_path(o, rename->path, cur_branch);1322output(o,1,_("Refusing to lose untracked file at%s; "1323"adding as%sinstead"),1324 rename->path, dst_name);1325}1326}1327if((ret =update_file(o,0, &rename->oid, rename->mode, dst_name)))1328;/* fall through, do allow dst_name to be released */1329else if(stage ==2)1330 ret =update_stages(o, rename->path, NULL, rename, add);1331else1332 ret =update_stages(o, rename->path, NULL, add, rename);13331334if(dst_name != rename->path)1335free(dst_name);13361337return ret;1338}13391340static intconflict_rename_rename_1to2(struct merge_options *o,1341struct rename_conflict_info *ci)1342{1343/* One file was renamed in both branches, but to different names. */1344struct diff_filespec *one = ci->pair1->one;1345struct diff_filespec *a = ci->pair1->two;1346struct diff_filespec *b = ci->pair2->two;13471348output(o,1,_("CONFLICT (rename/rename): "1349"Rename\"%s\"->\"%s\"in branch\"%s\""1350"rename\"%s\"->\"%s\"in\"%s\"%s"),1351 one->path, a->path, ci->branch1,1352 one->path, b->path, ci->branch2,1353 o->call_depth ?_(" (left unresolved)") :"");1354if(o->call_depth) {1355struct merge_file_info mfi;1356struct diff_filespec other;1357struct diff_filespec *add;1358if(merge_file_one(o, one->path,1359&one->oid, one->mode,1360&a->oid, a->mode,1361&b->oid, b->mode,1362 ci->branch1, ci->branch2, &mfi))1363return-1;13641365/*1366 * FIXME: For rename/add-source conflicts (if we could detect1367 * such), this is wrong. We should instead find a unique1368 * pathname and then either rename the add-source file to that1369 * unique path, or use that unique path instead of src here.1370 */1371if(update_file(o,0, &mfi.oid, mfi.mode, one->path))1372return-1;13731374/*1375 * Above, we put the merged content at the merge-base's1376 * path. Now we usually need to delete both a->path and1377 * b->path. However, the rename on each side of the merge1378 * could also be involved in a rename/add conflict. In1379 * such cases, we should keep the added file around,1380 * resolving the conflict at that path in its favor.1381 */1382 add =filespec_from_entry(&other, ci->dst_entry1,2^1);1383if(add) {1384if(update_file(o,0, &add->oid, add->mode, a->path))1385return-1;1386}1387else1388remove_file_from_cache(a->path);1389 add =filespec_from_entry(&other, ci->dst_entry2,3^1);1390if(add) {1391if(update_file(o,0, &add->oid, add->mode, b->path))1392return-1;1393}1394else1395remove_file_from_cache(b->path);1396}else if(handle_file(o, a,2, ci) ||handle_file(o, b,3, ci))1397return-1;13981399return0;1400}14011402static intconflict_rename_rename_2to1(struct merge_options *o,1403struct rename_conflict_info *ci)1404{1405/* Two files, a & b, were renamed to the same thing, c. */1406struct diff_filespec *a = ci->pair1->one;1407struct diff_filespec *b = ci->pair2->one;1408struct diff_filespec *c1 = ci->pair1->two;1409struct diff_filespec *c2 = ci->pair2->two;1410char*path = c1->path;/* == c2->path */1411struct merge_file_info mfi_c1;1412struct merge_file_info mfi_c2;1413int ret;14141415output(o,1,_("CONFLICT (rename/rename): "1416"Rename%s->%sin%s. "1417"Rename%s->%sin%s"),1418 a->path, c1->path, ci->branch1,1419 b->path, c2->path, ci->branch2);14201421remove_file(o,1, a->path, o->call_depth ||would_lose_untracked(a->path));1422remove_file(o,1, b->path, o->call_depth ||would_lose_untracked(b->path));14231424if(merge_file_special_markers(o, a, c1, &ci->ren1_other,1425 o->branch1, c1->path,1426 o->branch2, ci->ren1_other.path, &mfi_c1) ||1427merge_file_special_markers(o, b, &ci->ren2_other, c2,1428 o->branch1, ci->ren2_other.path,1429 o->branch2, c2->path, &mfi_c2))1430return-1;14311432if(o->call_depth) {1433/*1434 * If mfi_c1.clean && mfi_c2.clean, then it might make1435 * sense to do a two-way merge of those results. But, I1436 * think in all cases, it makes sense to have the virtual1437 * merge base just undo the renames; they can be detected1438 * again later for the non-recursive merge.1439 */1440remove_file(o,0, path,0);1441 ret =update_file(o,0, &mfi_c1.oid, mfi_c1.mode, a->path);1442if(!ret)1443 ret =update_file(o,0, &mfi_c2.oid, mfi_c2.mode,1444 b->path);1445}else{1446char*new_path1 =unique_path(o, path, ci->branch1);1447char*new_path2 =unique_path(o, path, ci->branch2);1448output(o,1,_("Renaming%sto%sand%sto%sinstead"),1449 a->path, new_path1, b->path, new_path2);1450if(would_lose_untracked(path))1451/*1452 * Only way we get here is if both renames were from1453 * a directory rename AND user had an untracked file1454 * at the location where both files end up after the1455 * two directory renames. See testcase 10d of t6043.1456 */1457output(o,1,_("Refusing to lose untracked file at "1458"%s, even though it's in the way."),1459 path);1460else1461remove_file(o,0, path,0);1462 ret =update_file(o,0, &mfi_c1.oid, mfi_c1.mode, new_path1);1463if(!ret)1464 ret =update_file(o,0, &mfi_c2.oid, mfi_c2.mode,1465 new_path2);1466/*1467 * unpack_trees() actually populates the index for us for1468 * "normal" rename/rename(2to1) situtations so that the1469 * correct entries are at the higher stages, which would1470 * make the call below to update_stages_for_stage_data1471 * unnecessary. However, if either of the renames came1472 * from a directory rename, then unpack_trees() will not1473 * have gotten the right data loaded into the index, so we1474 * need to do so now. (While it'd be tempting to move this1475 * call to update_stages_for_stage_data() to1476 * apply_directory_rename_modifications(), that would break1477 * our intermediate calls to would_lose_untracked() since1478 * those rely on the current in-memory index. See also the1479 * big "NOTE" in update_stages()).1480 */1481if(update_stages_for_stage_data(o, path, ci->dst_entry1))1482 ret = -1;14831484free(new_path2);1485free(new_path1);1486}14871488return ret;1489}14901491/*1492 * Get the diff_filepairs changed between o_tree and tree.1493 */1494static struct diff_queue_struct *get_diffpairs(struct merge_options *o,1495struct tree *o_tree,1496struct tree *tree)1497{1498struct diff_queue_struct *ret;1499struct diff_options opts;15001501diff_setup(&opts);1502 opts.flags.recursive =1;1503 opts.flags.rename_empty =0;1504 opts.detect_rename = DIFF_DETECT_RENAME;1505 opts.rename_limit = o->merge_rename_limit >=0? o->merge_rename_limit :1506 o->diff_rename_limit >=0? o->diff_rename_limit :15071000;1508 opts.rename_score = o->rename_score;1509 opts.show_rename_progress = o->show_rename_progress;1510 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1511diff_setup_done(&opts);1512diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1513diffcore_std(&opts);1514if(opts.needed_rename_limit > o->needed_rename_limit)1515 o->needed_rename_limit = opts.needed_rename_limit;15161517 ret =xmalloc(sizeof(*ret));1518*ret = diff_queued_diff;15191520 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1521 diff_queued_diff.nr =0;1522 diff_queued_diff.queue = NULL;1523diff_flush(&opts);1524return ret;1525}15261527static inttree_has_path(struct tree *tree,const char*path)1528{1529struct object_id hashy;1530unsigned int mode_o;15311532return!get_tree_entry(&tree->object.oid, path,1533&hashy, &mode_o);1534}15351536/*1537 * Return a new string that replaces the beginning portion (which matches1538 * entry->dir), with entry->new_dir. In perl-speak:1539 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1540 * NOTE:1541 * Caller must ensure that old_path starts with entry->dir + '/'.1542 */1543static char*apply_dir_rename(struct dir_rename_entry *entry,1544const char*old_path)1545{1546struct strbuf new_path = STRBUF_INIT;1547int oldlen, newlen;15481549if(entry->non_unique_new_dir)1550return NULL;15511552 oldlen =strlen(entry->dir);1553 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1554strbuf_grow(&new_path, newlen);1555strbuf_addbuf(&new_path, &entry->new_dir);1556strbuf_addstr(&new_path, &old_path[oldlen]);15571558returnstrbuf_detach(&new_path, NULL);1559}15601561static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1562char**old_dir,char**new_dir)1563{1564char*end_of_old, *end_of_new;1565int old_len, new_len;15661567*old_dir = NULL;1568*new_dir = NULL;15691570/*1571 * For1572 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1573 * the "e/foo.c" part is the same, we just want to know that1574 * "a/b/c/d" was renamed to "a/b/some/thing/else"1575 * so, for this example, this function returns "a/b/c/d" in1576 * *old_dir and "a/b/some/thing/else" in *new_dir.1577 *1578 * Also, if the basename of the file changed, we don't care. We1579 * want to know which portion of the directory, if any, changed.1580 */1581 end_of_old =strrchr(old_path,'/');1582 end_of_new =strrchr(new_path,'/');15831584if(end_of_old == NULL || end_of_new == NULL)1585return;1586while(*--end_of_new == *--end_of_old &&1587 end_of_old != old_path &&1588 end_of_new != new_path)1589;/* Do nothing; all in the while loop */1590/*1591 * We've found the first non-matching character in the directory1592 * paths. That means the current directory we were comparing1593 * represents the rename. Move end_of_old and end_of_new back1594 * to the full directory name.1595 */1596if(*end_of_old =='/')1597 end_of_old++;1598if(*end_of_old !='/')1599 end_of_new++;1600 end_of_old =strchr(end_of_old,'/');1601 end_of_new =strchr(end_of_new,'/');16021603/*1604 * It may have been the case that old_path and new_path were the same1605 * directory all along. Don't claim a rename if they're the same.1606 */1607 old_len = end_of_old - old_path;1608 new_len = end_of_new - new_path;16091610if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {1611*old_dir =xstrndup(old_path, old_len);1612*new_dir =xstrndup(new_path, new_len);1613}1614}16151616static voidremove_hashmap_entries(struct hashmap *dir_renames,1617struct string_list *items_to_remove)1618{1619int i;1620struct dir_rename_entry *entry;16211622for(i =0; i < items_to_remove->nr; i++) {1623 entry = items_to_remove->items[i].util;1624hashmap_remove(dir_renames, entry, NULL);1625}1626string_list_clear(items_to_remove,0);1627}16281629/*1630 * See if there is a directory rename for path, and if there are any file1631 * level conflicts for the renamed location. If there is a rename and1632 * there are no conflicts, return the new name. Otherwise, return NULL.1633 */1634static char*handle_path_level_conflicts(struct merge_options *o,1635const char*path,1636struct dir_rename_entry *entry,1637struct hashmap *collisions,1638struct tree *tree)1639{1640char*new_path = NULL;1641struct collision_entry *collision_ent;1642int clean =1;1643struct strbuf collision_paths = STRBUF_INIT;16441645/*1646 * entry has the mapping of old directory name to new directory name1647 * that we want to apply to path.1648 */1649 new_path =apply_dir_rename(entry, path);16501651if(!new_path) {1652/* This should only happen when entry->non_unique_new_dir set */1653if(!entry->non_unique_new_dir)1654BUG("entry->non_unqiue_dir not set and !new_path");1655output(o,1,_("CONFLICT (directory rename split): "1656"Unclear where to place%sbecause directory "1657"%swas renamed to multiple other directories, "1658"with no destination getting a majority of the "1659"files."),1660 path, entry->dir);1661 clean =0;1662return NULL;1663}16641665/*1666 * The caller needs to have ensured that it has pre-populated1667 * collisions with all paths that map to new_path. Do a quick check1668 * to ensure that's the case.1669 */1670 collision_ent =collision_find_entry(collisions, new_path);1671if(collision_ent == NULL)1672BUG("collision_ent is NULL");16731674/*1675 * Check for one-sided add/add/.../add conflicts, i.e.1676 * where implicit renames from the other side doing1677 * directory rename(s) can affect this side of history1678 * to put multiple paths into the same location. Warn1679 * and bail on directory renames for such paths.1680 */1681if(collision_ent->reported_already) {1682 clean =0;1683}else if(tree_has_path(tree, new_path)) {1684 collision_ent->reported_already =1;1685strbuf_add_separated_string_list(&collision_paths,", ",1686&collision_ent->source_files);1687output(o,1,_("CONFLICT (implicit dir rename): Existing "1688"file/dir at%sin the way of implicit "1689"directory rename(s) putting the following "1690"path(s) there:%s."),1691 new_path, collision_paths.buf);1692 clean =0;1693}else if(collision_ent->source_files.nr >1) {1694 collision_ent->reported_already =1;1695strbuf_add_separated_string_list(&collision_paths,", ",1696&collision_ent->source_files);1697output(o,1,_("CONFLICT (implicit dir rename): Cannot map "1698"more than one path to%s; implicit directory "1699"renames tried to put these paths there:%s"),1700 new_path, collision_paths.buf);1701 clean =0;1702}17031704/* Free memory we no longer need */1705strbuf_release(&collision_paths);1706if(!clean && new_path) {1707free(new_path);1708return NULL;1709}17101711return new_path;1712}17131714/*1715 * There are a couple things we want to do at the directory level:1716 * 1. Check for both sides renaming to the same thing, in order to avoid1717 * implicit renaming of files that should be left in place. (See1718 * testcase 6b in t6043 for details.)1719 * 2. Prune directory renames if there are still files left in the1720 * the original directory. These represent a partial directory rename,1721 * i.e. a rename where only some of the files within the directory1722 * were renamed elsewhere. (Technically, this could be done earlier1723 * in get_directory_renames(), except that would prevent us from1724 * doing the previous check and thus failing testcase 6b.)1725 * 3. Check for rename/rename(1to2) conflicts (at the directory level).1726 * In the future, we could potentially record this info as well and1727 * omit reporting rename/rename(1to2) conflicts for each path within1728 * the affected directories, thus cleaning up the merge output.1729 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the1730 * directory level, because merging directories is fine. If it1731 * causes conflicts for files within those merged directories, then1732 * that should be detected at the individual path level.1733 */1734static voidhandle_directory_level_conflicts(struct merge_options *o,1735struct hashmap *dir_re_head,1736struct tree *head,1737struct hashmap *dir_re_merge,1738struct tree *merge)1739{1740struct hashmap_iter iter;1741struct dir_rename_entry *head_ent;1742struct dir_rename_entry *merge_ent;17431744struct string_list remove_from_head = STRING_LIST_INIT_NODUP;1745struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;17461747hashmap_iter_init(dir_re_head, &iter);1748while((head_ent =hashmap_iter_next(&iter))) {1749 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);1750if(merge_ent &&1751!head_ent->non_unique_new_dir &&1752!merge_ent->non_unique_new_dir &&1753!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {1754/* 1. Renamed identically; remove it from both sides */1755string_list_append(&remove_from_head,1756 head_ent->dir)->util = head_ent;1757strbuf_release(&head_ent->new_dir);1758string_list_append(&remove_from_merge,1759 merge_ent->dir)->util = merge_ent;1760strbuf_release(&merge_ent->new_dir);1761}else if(tree_has_path(head, head_ent->dir)) {1762/* 2. This wasn't a directory rename after all */1763string_list_append(&remove_from_head,1764 head_ent->dir)->util = head_ent;1765strbuf_release(&head_ent->new_dir);1766}1767}17681769remove_hashmap_entries(dir_re_head, &remove_from_head);1770remove_hashmap_entries(dir_re_merge, &remove_from_merge);17711772hashmap_iter_init(dir_re_merge, &iter);1773while((merge_ent =hashmap_iter_next(&iter))) {1774 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);1775if(tree_has_path(merge, merge_ent->dir)) {1776/* 2. This wasn't a directory rename after all */1777string_list_append(&remove_from_merge,1778 merge_ent->dir)->util = merge_ent;1779}else if(head_ent &&1780!head_ent->non_unique_new_dir &&1781!merge_ent->non_unique_new_dir) {1782/* 3. rename/rename(1to2) */1783/*1784 * We can assume it's not rename/rename(1to1) because1785 * that was case (1), already checked above. So we1786 * know that head_ent->new_dir and merge_ent->new_dir1787 * are different strings.1788 */1789output(o,1,_("CONFLICT (rename/rename): "1790"Rename directory%s->%sin%s. "1791"Rename directory%s->%sin%s"),1792 head_ent->dir, head_ent->new_dir.buf, o->branch1,1793 head_ent->dir, merge_ent->new_dir.buf, o->branch2);1794string_list_append(&remove_from_head,1795 head_ent->dir)->util = head_ent;1796strbuf_release(&head_ent->new_dir);1797string_list_append(&remove_from_merge,1798 merge_ent->dir)->util = merge_ent;1799strbuf_release(&merge_ent->new_dir);1800}1801}18021803remove_hashmap_entries(dir_re_head, &remove_from_head);1804remove_hashmap_entries(dir_re_merge, &remove_from_merge);1805}18061807static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs,1808struct tree *tree)1809{1810struct hashmap *dir_renames;1811struct hashmap_iter iter;1812struct dir_rename_entry *entry;1813int i;18141815/*1816 * Typically, we think of a directory rename as all files from a1817 * certain directory being moved to a target directory. However,1818 * what if someone first moved two files from the original1819 * directory in one commit, and then renamed the directory1820 * somewhere else in a later commit? At merge time, we just know1821 * that files from the original directory went to two different1822 * places, and that the bulk of them ended up in the same place.1823 * We want each directory rename to represent where the bulk of the1824 * files from that directory end up; this function exists to find1825 * where the bulk of the files went.1826 *1827 * The first loop below simply iterates through the list of file1828 * renames, finding out how often each directory rename pair1829 * possibility occurs.1830 */1831 dir_renames =xmalloc(sizeof(*dir_renames));1832dir_rename_init(dir_renames);1833for(i =0; i < pairs->nr; ++i) {1834struct string_list_item *item;1835int*count;1836struct diff_filepair *pair = pairs->queue[i];1837char*old_dir, *new_dir;18381839/* File not part of directory rename if it wasn't renamed */1840if(pair->status !='R')1841continue;18421843get_renamed_dir_portion(pair->one->path, pair->two->path,1844&old_dir, &new_dir);1845if(!old_dir)1846/* Directory didn't change at all; ignore this one. */1847continue;18481849 entry =dir_rename_find_entry(dir_renames, old_dir);1850if(!entry) {1851 entry =xmalloc(sizeof(*entry));1852dir_rename_entry_init(entry, old_dir);1853hashmap_put(dir_renames, entry);1854}else{1855free(old_dir);1856}1857 item =string_list_lookup(&entry->possible_new_dirs, new_dir);1858if(!item) {1859 item =string_list_insert(&entry->possible_new_dirs,1860 new_dir);1861 item->util =xcalloc(1,sizeof(int));1862}else{1863free(new_dir);1864}1865 count = item->util;1866*count +=1;1867}18681869/*1870 * For each directory with files moved out of it, we find out which1871 * target directory received the most files so we can declare it to1872 * be the "winning" target location for the directory rename. This1873 * winner gets recorded in new_dir. If there is no winner1874 * (multiple target directories received the same number of files),1875 * we set non_unique_new_dir. Once we've determined the winner (or1876 * that there is no winner), we no longer need possible_new_dirs.1877 */1878hashmap_iter_init(dir_renames, &iter);1879while((entry =hashmap_iter_next(&iter))) {1880int max =0;1881int bad_max =0;1882char*best = NULL;18831884for(i =0; i < entry->possible_new_dirs.nr; i++) {1885int*count = entry->possible_new_dirs.items[i].util;18861887if(*count == max)1888 bad_max = max;1889else if(*count > max) {1890 max = *count;1891 best = entry->possible_new_dirs.items[i].string;1892}1893}1894if(bad_max == max)1895 entry->non_unique_new_dir =1;1896else{1897assert(entry->new_dir.len ==0);1898strbuf_addstr(&entry->new_dir, best);1899}1900/*1901 * The relevant directory sub-portion of the original full1902 * filepaths were xstrndup'ed before inserting into1903 * possible_new_dirs, and instead of manually iterating the1904 * list and free'ing each, just lie and tell1905 * possible_new_dirs that it did the strdup'ing so that it1906 * will free them for us.1907 */1908 entry->possible_new_dirs.strdup_strings =1;1909string_list_clear(&entry->possible_new_dirs,1);1910}19111912return dir_renames;1913}19141915static struct dir_rename_entry *check_dir_renamed(const char*path,1916struct hashmap *dir_renames)1917{1918char temp[PATH_MAX];1919char*end;1920struct dir_rename_entry *entry;19211922strcpy(temp, path);1923while((end =strrchr(temp,'/'))) {1924*end ='\0';1925 entry =dir_rename_find_entry(dir_renames, temp);1926if(entry)1927return entry;1928}1929return NULL;1930}19311932static voidcompute_collisions(struct hashmap *collisions,1933struct hashmap *dir_renames,1934struct diff_queue_struct *pairs)1935{1936int i;19371938/*1939 * Multiple files can be mapped to the same path due to directory1940 * renames done by the other side of history. Since that other1941 * side of history could have merged multiple directories into one,1942 * if our side of history added the same file basename to each of1943 * those directories, then all N of them would get implicitly1944 * renamed by the directory rename detection into the same path,1945 * and we'd get an add/add/.../add conflict, and all those adds1946 * from *this* side of history. This is not representable in the1947 * index, and users aren't going to easily be able to make sense of1948 * it. So we need to provide a good warning about what's1949 * happening, and fall back to no-directory-rename detection1950 * behavior for those paths.1951 *1952 * See testcases 9e and all of section 5 from t6043 for examples.1953 */1954collision_init(collisions);19551956for(i =0; i < pairs->nr; ++i) {1957struct dir_rename_entry *dir_rename_ent;1958struct collision_entry *collision_ent;1959char*new_path;1960struct diff_filepair *pair = pairs->queue[i];19611962if(pair->status =='D')1963continue;1964 dir_rename_ent =check_dir_renamed(pair->two->path,1965 dir_renames);1966if(!dir_rename_ent)1967continue;19681969 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);1970if(!new_path)1971/*1972 * dir_rename_ent->non_unique_new_path is true, which1973 * means there is no directory rename for us to use,1974 * which means it won't cause us any additional1975 * collisions.1976 */1977continue;1978 collision_ent =collision_find_entry(collisions, new_path);1979if(!collision_ent) {1980 collision_ent =xcalloc(1,1981sizeof(struct collision_entry));1982hashmap_entry_init(collision_ent,strhash(new_path));1983hashmap_put(collisions, collision_ent);1984 collision_ent->target_file = new_path;1985}else{1986free(new_path);1987}1988string_list_insert(&collision_ent->source_files,1989 pair->two->path);1990}1991}19921993static char*check_for_directory_rename(struct merge_options *o,1994const char*path,1995struct tree *tree,1996struct hashmap *dir_renames,1997struct hashmap *dir_rename_exclusions,1998struct hashmap *collisions,1999int*clean_merge)2000{2001char*new_path = NULL;2002struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);2003struct dir_rename_entry *oentry = NULL;20042005if(!entry)2006return new_path;20072008/*2009 * This next part is a little weird. We do not want to do an2010 * implicit rename into a directory we renamed on our side, because2011 * that will result in a spurious rename/rename(1to2) conflict. An2012 * example:2013 * Base commit: dumbdir/afile, otherdir/bfile2014 * Side 1: smrtdir/afile, otherdir/bfile2015 * Side 2: dumbdir/afile, dumbdir/bfile2016 * Here, while working on Side 1, we could notice that otherdir was2017 * renamed/merged to dumbdir, and change the diff_filepair for2018 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2019 * 2 will notice the rename from dumbdir to smrtdir, and do the2020 * transitive rename to move it from dumbdir/bfile to2021 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2022 * smrtdir, a rename/rename(1to2) conflict. We really just want2023 * the file to end up in smrtdir. And the way to achieve that is2024 * to not let Side1 do the rename to dumbdir, since we know that is2025 * the source of one of our directory renames.2026 *2027 * That's why oentry and dir_rename_exclusions is here.2028 *2029 * As it turns out, this also prevents N-way transient rename2030 * confusion; See testcases 9c and 9d of t6043.2031 */2032 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2033if(oentry) {2034output(o,1,_("WARNING: Avoiding applying%s->%srename "2035"to%s, because%sitself was renamed."),2036 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2037}else{2038 new_path =handle_path_level_conflicts(o, path, entry,2039 collisions, tree);2040*clean_merge &= (new_path != NULL);2041}20422043return new_path;2044}20452046static voidapply_directory_rename_modifications(struct merge_options *o,2047struct diff_filepair *pair,2048char*new_path,2049struct rename *re,2050struct tree *tree,2051struct tree *o_tree,2052struct tree *a_tree,2053struct tree *b_tree,2054struct string_list *entries,2055int*clean)2056{2057struct string_list_item *item;2058int stage = (tree == a_tree ?2:3);20592060/*2061 * In all cases where we can do directory rename detection,2062 * unpack_trees() will have read pair->two->path into the2063 * index and the working copy. We need to remove it so that2064 * we can instead place it at new_path. It is guaranteed to2065 * not be untracked (unpack_trees() would have errored out2066 * saying the file would have been overwritten), but it might2067 * be dirty, though.2068 */2069remove_file(o,1, pair->two->path,0/* no_wd */);20702071/* Find or create a new re->dst_entry */2072 item =string_list_lookup(entries, new_path);2073if(item) {2074/*2075 * Since we're renaming on this side of history, and it's2076 * due to a directory rename on the other side of history2077 * (which we only allow when the directory in question no2078 * longer exists on the other side of history), the2079 * original entry for re->dst_entry is no longer2080 * necessary...2081 */2082 re->dst_entry->processed =1;20832084/*2085 * ...because we'll be using this new one.2086 */2087 re->dst_entry = item->util;2088}else{2089/*2090 * re->dst_entry is for the before-dir-rename path, and we2091 * need it to hold information for the after-dir-rename2092 * path. Before creating a new entry, we need to mark the2093 * old one as unnecessary (...unless it is shared by2094 * src_entry, i.e. this didn't use to be a rename, in which2095 * case we can just allow the normal processing to happen2096 * for it).2097 */2098if(pair->status =='R')2099 re->dst_entry->processed =1;21002101 re->dst_entry =insert_stage_data(new_path,2102 o_tree, a_tree, b_tree,2103 entries);2104 item =string_list_insert(entries, new_path);2105 item->util = re->dst_entry;2106}21072108/*2109 * Update the stage_data with the information about the path we are2110 * moving into place. That slot will be empty and available for us2111 * to write to because of the collision checks in2112 * handle_path_level_conflicts(). In other words,2113 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2114 * open for us to write to.2115 *2116 * It may be tempting to actually update the index at this point as2117 * well, using update_stages_for_stage_data(), but as per the big2118 * "NOTE" in update_stages(), doing so will modify the current2119 * in-memory index which will break calls to would_lose_untracked()2120 * that we need to make. Instead, we need to just make sure that2121 * the various conflict_rename_*() functions update the index2122 * explicitly rather than relying on unpack_trees() to have done it.2123 */2124get_tree_entry(&tree->object.oid,2125 pair->two->path,2126&re->dst_entry->stages[stage].oid,2127&re->dst_entry->stages[stage].mode);21282129/* Update pair status */2130if(pair->status =='A') {2131/*2132 * Recording rename information for this add makes it look2133 * like a rename/delete conflict. Make sure we can2134 * correctly handle this as an add that was moved to a new2135 * directory instead of reporting a rename/delete conflict.2136 */2137 re->add_turned_into_rename =1;2138}2139/*2140 * We don't actually look at pair->status again, but it seems2141 * pedagogically correct to adjust it.2142 */2143 pair->status ='R';21442145/*2146 * Finally, record the new location.2147 */2148 pair->two->path = new_path;2149}21502151/*2152 * Get information of all renames which occurred in 'pairs', making use of2153 * any implicit directory renames inferred from the other side of history.2154 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2155 * to be able to associate the correct cache entries with the rename2156 * information; tree is always equal to either a_tree or b_tree.2157 */2158static struct string_list *get_renames(struct merge_options *o,2159struct diff_queue_struct *pairs,2160struct hashmap *dir_renames,2161struct hashmap *dir_rename_exclusions,2162struct tree *tree,2163struct tree *o_tree,2164struct tree *a_tree,2165struct tree *b_tree,2166struct string_list *entries,2167int*clean_merge)2168{2169int i;2170struct hashmap collisions;2171struct hashmap_iter iter;2172struct collision_entry *e;2173struct string_list *renames;21742175compute_collisions(&collisions, dir_renames, pairs);2176 renames =xcalloc(1,sizeof(struct string_list));21772178for(i =0; i < pairs->nr; ++i) {2179struct string_list_item *item;2180struct rename *re;2181struct diff_filepair *pair = pairs->queue[i];2182char*new_path;/* non-NULL only with directory renames */21832184if(pair->status =='D') {2185diff_free_filepair(pair);2186continue;2187}2188 new_path =check_for_directory_rename(o, pair->two->path, tree,2189 dir_renames,2190 dir_rename_exclusions,2191&collisions,2192 clean_merge);2193if(pair->status !='R'&& !new_path) {2194diff_free_filepair(pair);2195continue;2196}21972198 re =xmalloc(sizeof(*re));2199 re->processed =0;2200 re->add_turned_into_rename =0;2201 re->pair = pair;2202 item =string_list_lookup(entries, re->pair->one->path);2203if(!item)2204 re->src_entry =insert_stage_data(re->pair->one->path,2205 o_tree, a_tree, b_tree, entries);2206else2207 re->src_entry = item->util;22082209 item =string_list_lookup(entries, re->pair->two->path);2210if(!item)2211 re->dst_entry =insert_stage_data(re->pair->two->path,2212 o_tree, a_tree, b_tree, entries);2213else2214 re->dst_entry = item->util;2215 item =string_list_insert(renames, pair->one->path);2216 item->util = re;2217if(new_path)2218apply_directory_rename_modifications(o, pair, new_path,2219 re, tree, o_tree,2220 a_tree, b_tree,2221 entries,2222 clean_merge);2223}22242225hashmap_iter_init(&collisions, &iter);2226while((e =hashmap_iter_next(&iter))) {2227free(e->target_file);2228string_list_clear(&e->source_files,0);2229}2230hashmap_free(&collisions,1);2231return renames;2232}22332234static intprocess_renames(struct merge_options *o,2235struct string_list *a_renames,2236struct string_list *b_renames)2237{2238int clean_merge =1, i, j;2239struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2240struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2241const struct rename *sre;22422243for(i =0; i < a_renames->nr; i++) {2244 sre = a_renames->items[i].util;2245string_list_insert(&a_by_dst, sre->pair->two->path)->util2246= (void*)sre;2247}2248for(i =0; i < b_renames->nr; i++) {2249 sre = b_renames->items[i].util;2250string_list_insert(&b_by_dst, sre->pair->two->path)->util2251= (void*)sre;2252}22532254for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2255struct string_list *renames1, *renames2Dst;2256struct rename *ren1 = NULL, *ren2 = NULL;2257const char*branch1, *branch2;2258const char*ren1_src, *ren1_dst;2259struct string_list_item *lookup;22602261if(i >= a_renames->nr) {2262 ren2 = b_renames->items[j++].util;2263}else if(j >= b_renames->nr) {2264 ren1 = a_renames->items[i++].util;2265}else{2266int compare =strcmp(a_renames->items[i].string,2267 b_renames->items[j].string);2268if(compare <=0)2269 ren1 = a_renames->items[i++].util;2270if(compare >=0)2271 ren2 = b_renames->items[j++].util;2272}22732274/* TODO: refactor, so that 1/2 are not needed */2275if(ren1) {2276 renames1 = a_renames;2277 renames2Dst = &b_by_dst;2278 branch1 = o->branch1;2279 branch2 = o->branch2;2280}else{2281 renames1 = b_renames;2282 renames2Dst = &a_by_dst;2283 branch1 = o->branch2;2284 branch2 = o->branch1;2285SWAP(ren2, ren1);2286}22872288if(ren1->processed)2289continue;2290 ren1->processed =1;2291 ren1->dst_entry->processed =1;2292/* BUG: We should only mark src_entry as processed if we2293 * are not dealing with a rename + add-source case.2294 */2295 ren1->src_entry->processed =1;22962297 ren1_src = ren1->pair->one->path;2298 ren1_dst = ren1->pair->two->path;22992300if(ren2) {2301/* One file renamed on both sides */2302const char*ren2_src = ren2->pair->one->path;2303const char*ren2_dst = ren2->pair->two->path;2304enum rename_type rename_type;2305if(strcmp(ren1_src, ren2_src) !=0)2306die("BUG: ren1_src != ren2_src");2307 ren2->dst_entry->processed =1;2308 ren2->processed =1;2309if(strcmp(ren1_dst, ren2_dst) !=0) {2310 rename_type = RENAME_ONE_FILE_TO_TWO;2311 clean_merge =0;2312}else{2313 rename_type = RENAME_ONE_FILE_TO_ONE;2314/* BUG: We should only remove ren1_src in2315 * the base stage (think of rename +2316 * add-source cases).2317 */2318remove_file(o,1, ren1_src,1);2319update_entry(ren1->dst_entry,2320 ren1->pair->one,2321 ren1->pair->two,2322 ren2->pair->two);2323}2324setup_rename_conflict_info(rename_type,2325 ren1->pair,2326 ren2->pair,2327 branch1,2328 branch2,2329 ren1->dst_entry,2330 ren2->dst_entry,2331 o,2332 NULL,2333 NULL);2334}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2335/* Two different files renamed to the same thing */2336char*ren2_dst;2337 ren2 = lookup->util;2338 ren2_dst = ren2->pair->two->path;2339if(strcmp(ren1_dst, ren2_dst) !=0)2340die("BUG: ren1_dst != ren2_dst");23412342 clean_merge =0;2343 ren2->processed =1;2344/*2345 * BUG: We should only mark src_entry as processed2346 * if we are not dealing with a rename + add-source2347 * case.2348 */2349 ren2->src_entry->processed =1;23502351setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2352 ren1->pair,2353 ren2->pair,2354 branch1,2355 branch2,2356 ren1->dst_entry,2357 ren2->dst_entry,2358 o,2359 ren1->src_entry,2360 ren2->src_entry);23612362}else{2363/* Renamed in 1, maybe changed in 2 */2364/* we only use sha1 and mode of these */2365struct diff_filespec src_other, dst_other;2366int try_merge;23672368/*2369 * unpack_trees loads entries from common-commit2370 * into stage 1, from head-commit into stage 2, and2371 * from merge-commit into stage 3. We keep track2372 * of which side corresponds to the rename.2373 */2374int renamed_stage = a_renames == renames1 ?2:3;2375int other_stage = a_renames == renames1 ?3:2;23762377/* BUG: We should only remove ren1_src in the base2378 * stage and in other_stage (think of rename +2379 * add-source case).2380 */2381remove_file(o,1, ren1_src,2382 renamed_stage ==2|| !was_tracked(ren1_src));23832384oidcpy(&src_other.oid,2385&ren1->src_entry->stages[other_stage].oid);2386 src_other.mode = ren1->src_entry->stages[other_stage].mode;2387oidcpy(&dst_other.oid,2388&ren1->dst_entry->stages[other_stage].oid);2389 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2390 try_merge =0;23912392if(oid_eq(&src_other.oid, &null_oid) &&2393 ren1->add_turned_into_rename) {2394setup_rename_conflict_info(RENAME_DIR,2395 ren1->pair,2396 NULL,2397 branch1,2398 branch2,2399 ren1->dst_entry,2400 NULL,2401 o,2402 NULL,2403 NULL);2404}else if(oid_eq(&src_other.oid, &null_oid)) {2405setup_rename_conflict_info(RENAME_DELETE,2406 ren1->pair,2407 NULL,2408 branch1,2409 branch2,2410 ren1->dst_entry,2411 NULL,2412 o,2413 NULL,2414 NULL);2415}else if((dst_other.mode == ren1->pair->two->mode) &&2416oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2417/*2418 * Added file on the other side identical to2419 * the file being renamed: clean merge.2420 * Also, there is no need to overwrite the2421 * file already in the working copy, so call2422 * update_file_flags() instead of2423 * update_file().2424 */2425if(update_file_flags(o,2426&ren1->pair->two->oid,2427 ren1->pair->two->mode,2428 ren1_dst,24291,/* update_cache */24300/* update_wd */))2431 clean_merge = -1;2432}else if(!oid_eq(&dst_other.oid, &null_oid)) {2433 clean_merge =0;2434 try_merge =1;2435output(o,1,_("CONFLICT (rename/add): Rename%s->%sin%s. "2436"%sadded in%s"),2437 ren1_src, ren1_dst, branch1,2438 ren1_dst, branch2);2439if(o->call_depth) {2440struct merge_file_info mfi;2441if(merge_file_one(o, ren1_dst, &null_oid,0,2442&ren1->pair->two->oid,2443 ren1->pair->two->mode,2444&dst_other.oid,2445 dst_other.mode,2446 branch1, branch2, &mfi)) {2447 clean_merge = -1;2448goto cleanup_and_return;2449}2450output(o,1,_("Adding merged%s"), ren1_dst);2451if(update_file(o,0, &mfi.oid,2452 mfi.mode, ren1_dst))2453 clean_merge = -1;2454 try_merge =0;2455}else{2456char*new_path =unique_path(o, ren1_dst, branch2);2457output(o,1,_("Adding as%sinstead"), new_path);2458if(update_file(o,0, &dst_other.oid,2459 dst_other.mode, new_path))2460 clean_merge = -1;2461free(new_path);2462}2463}else2464 try_merge =1;24652466if(clean_merge <0)2467goto cleanup_and_return;2468if(try_merge) {2469struct diff_filespec *one, *a, *b;2470 src_other.path = (char*)ren1_src;24712472 one = ren1->pair->one;2473if(a_renames == renames1) {2474 a = ren1->pair->two;2475 b = &src_other;2476}else{2477 b = ren1->pair->two;2478 a = &src_other;2479}2480update_entry(ren1->dst_entry, one, a, b);2481setup_rename_conflict_info(RENAME_NORMAL,2482 ren1->pair,2483 NULL,2484 branch1,2485 NULL,2486 ren1->dst_entry,2487 NULL,2488 o,2489 NULL,2490 NULL);2491}2492}2493}2494cleanup_and_return:2495string_list_clear(&a_by_dst,0);2496string_list_clear(&b_by_dst,0);24972498return clean_merge;2499}25002501struct rename_info {2502struct string_list *head_renames;2503struct string_list *merge_renames;2504};25052506static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2507struct hashmap *dir_renames)2508{2509struct hashmap_iter iter;2510struct dir_rename_entry *e;25112512hashmap_iter_init(dir_renames, &iter);2513while((e =hashmap_iter_next(&iter))) {2514free(e->dir);2515strbuf_release(&e->new_dir);2516/* possible_new_dirs already cleared in get_directory_renames */2517}2518hashmap_free(dir_renames,1);2519free(dir_renames);25202521free(pairs->queue);2522free(pairs);2523}25242525static inthandle_renames(struct merge_options *o,2526struct tree *common,2527struct tree *head,2528struct tree *merge,2529struct string_list *entries,2530struct rename_info *ri)2531{2532struct diff_queue_struct *head_pairs, *merge_pairs;2533struct hashmap *dir_re_head, *dir_re_merge;2534int clean =1;25352536 ri->head_renames = NULL;2537 ri->merge_renames = NULL;25382539if(!o->detect_rename)2540return1;25412542 head_pairs =get_diffpairs(o, common, head);2543 merge_pairs =get_diffpairs(o, common, merge);25442545 dir_re_head =get_directory_renames(head_pairs, head);2546 dir_re_merge =get_directory_renames(merge_pairs, merge);25472548handle_directory_level_conflicts(o,2549 dir_re_head, head,2550 dir_re_merge, merge);25512552 ri->head_renames =get_renames(o, head_pairs,2553 dir_re_merge, dir_re_head, head,2554 common, head, merge, entries,2555&clean);2556if(clean <0)2557goto cleanup;2558 ri->merge_renames =get_renames(o, merge_pairs,2559 dir_re_head, dir_re_merge, merge,2560 common, head, merge, entries,2561&clean);2562if(clean <0)2563goto cleanup;2564 clean &=process_renames(o, ri->head_renames, ri->merge_renames);25652566cleanup:2567/*2568 * Some cleanup is deferred until cleanup_renames() because the2569 * data structures are still needed and referenced in2570 * process_entry(). But there are a few things we can free now.2571 */2572initial_cleanup_rename(head_pairs, dir_re_head);2573initial_cleanup_rename(merge_pairs, dir_re_merge);25742575return clean;2576}25772578static voidfinal_cleanup_rename(struct string_list *rename)2579{2580const struct rename *re;2581int i;25822583if(rename == NULL)2584return;25852586for(i =0; i < rename->nr; i++) {2587 re = rename->items[i].util;2588diff_free_filepair(re->pair);2589}2590string_list_clear(rename,1);2591free(rename);2592}25932594static voidfinal_cleanup_renames(struct rename_info *re_info)2595{2596final_cleanup_rename(re_info->head_renames);2597final_cleanup_rename(re_info->merge_renames);2598}25992600static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)2601{2602return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;2603}26042605static intread_oid_strbuf(struct merge_options *o,2606const struct object_id *oid,struct strbuf *dst)2607{2608void*buf;2609enum object_type type;2610unsigned long size;2611 buf =read_object_file(oid, &type, &size);2612if(!buf)2613returnerr(o,_("cannot read object%s"),oid_to_hex(oid));2614if(type != OBJ_BLOB) {2615free(buf);2616returnerr(o,_("object%sis not a blob"),oid_to_hex(oid));2617}2618strbuf_attach(dst, buf, size, size +1);2619return0;2620}26212622static intblob_unchanged(struct merge_options *opt,2623const struct object_id *o_oid,2624unsigned o_mode,2625const struct object_id *a_oid,2626unsigned a_mode,2627int renormalize,const char*path)2628{2629struct strbuf o = STRBUF_INIT;2630struct strbuf a = STRBUF_INIT;2631int ret =0;/* assume changed for safety */26322633if(a_mode != o_mode)2634return0;2635if(oid_eq(o_oid, a_oid))2636return1;2637if(!renormalize)2638return0;26392640assert(o_oid && a_oid);2641if(read_oid_strbuf(opt, o_oid, &o) ||read_oid_strbuf(opt, a_oid, &a))2642goto error_return;2643/*2644 * Note: binary | is used so that both renormalizations are2645 * performed. Comparison can be skipped if both files are2646 * unchanged since their sha1s have already been compared.2647 */2648if(renormalize_buffer(&the_index, path, o.buf, o.len, &o) |2649renormalize_buffer(&the_index, path, a.buf, a.len, &a))2650 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));26512652error_return:2653strbuf_release(&o);2654strbuf_release(&a);2655return ret;2656}26572658static inthandle_modify_delete(struct merge_options *o,2659const char*path,2660struct object_id *o_oid,int o_mode,2661struct object_id *a_oid,int a_mode,2662struct object_id *b_oid,int b_mode)2663{2664const char*modify_branch, *delete_branch;2665struct object_id *changed_oid;2666int changed_mode;26672668if(a_oid) {2669 modify_branch = o->branch1;2670 delete_branch = o->branch2;2671 changed_oid = a_oid;2672 changed_mode = a_mode;2673}else{2674 modify_branch = o->branch2;2675 delete_branch = o->branch1;2676 changed_oid = b_oid;2677 changed_mode = b_mode;2678}26792680returnhandle_change_delete(o,2681 path, NULL,2682 o_oid, o_mode,2683 changed_oid, changed_mode,2684 modify_branch, delete_branch,2685_("modify"),_("modified"));2686}26872688static intmerge_content(struct merge_options *o,2689const char*path,2690struct object_id *o_oid,int o_mode,2691struct object_id *a_oid,int a_mode,2692struct object_id *b_oid,int b_mode,2693struct rename_conflict_info *rename_conflict_info)2694{2695const char*reason =_("content");2696const char*path1 = NULL, *path2 = NULL;2697struct merge_file_info mfi;2698struct diff_filespec one, a, b;2699unsigned df_conflict_remains =0;27002701if(!o_oid) {2702 reason =_("add/add");2703 o_oid = (struct object_id *)&null_oid;2704}2705 one.path = a.path = b.path = (char*)path;2706oidcpy(&one.oid, o_oid);2707 one.mode = o_mode;2708oidcpy(&a.oid, a_oid);2709 a.mode = a_mode;2710oidcpy(&b.oid, b_oid);2711 b.mode = b_mode;27122713if(rename_conflict_info) {2714struct diff_filepair *pair1 = rename_conflict_info->pair1;27152716 path1 = (o->branch1 == rename_conflict_info->branch1) ?2717 pair1->two->path : pair1->one->path;2718/* If rename_conflict_info->pair2 != NULL, we are in2719 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a2720 * normal rename.2721 */2722 path2 = (rename_conflict_info->pair2 ||2723 o->branch2 == rename_conflict_info->branch1) ?2724 pair1->two->path : pair1->one->path;27252726if(dir_in_way(path, !o->call_depth,2727S_ISGITLINK(pair1->two->mode)))2728 df_conflict_remains =1;2729}2730if(merge_file_special_markers(o, &one, &a, &b,2731 o->branch1, path1,2732 o->branch2, path2, &mfi))2733return-1;27342735if(mfi.clean && !df_conflict_remains &&2736oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {2737int path_renamed_outside_HEAD;2738output(o,3,_("Skipped%s(merged same as existing)"), path);2739/*2740 * The content merge resulted in the same file contents we2741 * already had. We can return early if those file contents2742 * are recorded at the correct path (which may not be true2743 * if the merge involves a rename).2744 */2745 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);2746if(!path_renamed_outside_HEAD) {2747add_cacheinfo(o, mfi.mode, &mfi.oid, path,27480, (!o->call_depth),0);2749return mfi.clean;2750}2751}else2752output(o,2,_("Auto-merging%s"), path);27532754if(!mfi.clean) {2755if(S_ISGITLINK(mfi.mode))2756 reason =_("submodule");2757output(o,1,_("CONFLICT (%s): Merge conflict in%s"),2758 reason, path);2759if(rename_conflict_info && !df_conflict_remains)2760if(update_stages(o, path, &one, &a, &b))2761return-1;2762}27632764if(df_conflict_remains) {2765char*new_path;2766if(o->call_depth) {2767remove_file_from_cache(path);2768}else{2769if(!mfi.clean) {2770if(update_stages(o, path, &one, &a, &b))2771return-1;2772}else{2773int file_from_stage2 =was_tracked(path);2774struct diff_filespec merged;2775oidcpy(&merged.oid, &mfi.oid);2776 merged.mode = mfi.mode;27772778if(update_stages(o, path, NULL,2779 file_from_stage2 ? &merged : NULL,2780 file_from_stage2 ? NULL : &merged))2781return-1;2782}27832784}2785 new_path =unique_path(o, path, rename_conflict_info->branch1);2786output(o,1,_("Adding as%sinstead"), new_path);2787if(update_file(o,0, &mfi.oid, mfi.mode, new_path)) {2788free(new_path);2789return-1;2790}2791free(new_path);2792 mfi.clean =0;2793}else if(update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))2794return-1;2795return mfi.clean;2796}27972798/* Per entry merge function */2799static intprocess_entry(struct merge_options *o,2800const char*path,struct stage_data *entry)2801{2802int clean_merge =1;2803int normalize = o->renormalize;2804unsigned o_mode = entry->stages[1].mode;2805unsigned a_mode = entry->stages[2].mode;2806unsigned b_mode = entry->stages[3].mode;2807struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);2808struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);2809struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);28102811 entry->processed =1;2812if(entry->rename_conflict_info) {2813struct rename_conflict_info *conflict_info = entry->rename_conflict_info;2814switch(conflict_info->rename_type) {2815case RENAME_NORMAL:2816case RENAME_ONE_FILE_TO_ONE:2817 clean_merge =merge_content(o, path,2818 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,2819 conflict_info);2820break;2821case RENAME_DIR:2822 clean_merge =1;2823if(conflict_rename_dir(o,2824 conflict_info->pair1,2825 conflict_info->branch1,2826 conflict_info->branch2))2827 clean_merge = -1;2828break;2829case RENAME_DELETE:2830 clean_merge =0;2831if(conflict_rename_delete(o,2832 conflict_info->pair1,2833 conflict_info->branch1,2834 conflict_info->branch2))2835 clean_merge = -1;2836break;2837case RENAME_ONE_FILE_TO_TWO:2838 clean_merge =0;2839if(conflict_rename_rename_1to2(o, conflict_info))2840 clean_merge = -1;2841break;2842case RENAME_TWO_FILES_TO_ONE:2843 clean_merge =0;2844if(conflict_rename_rename_2to1(o, conflict_info))2845 clean_merge = -1;2846break;2847default:2848 entry->processed =0;2849break;2850}2851}else if(o_oid && (!a_oid || !b_oid)) {2852/* Case A: Deleted in one */2853if((!a_oid && !b_oid) ||2854(!b_oid &&blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||2855(!a_oid &&blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {2856/* Deleted in both or deleted in one and2857 * unchanged in the other */2858if(a_oid)2859output(o,2,_("Removing%s"), path);2860/* do not touch working file if it did not exist */2861remove_file(o,1, path, !a_oid);2862}else{2863/* Modify/delete; deleted side may have put a directory in the way */2864 clean_merge =0;2865if(handle_modify_delete(o, path, o_oid, o_mode,2866 a_oid, a_mode, b_oid, b_mode))2867 clean_merge = -1;2868}2869}else if((!o_oid && a_oid && !b_oid) ||2870(!o_oid && !a_oid && b_oid)) {2871/* Case B: Added in one. */2872/* [nothing|directory] -> ([nothing|directory], file) */28732874const char*add_branch;2875const char*other_branch;2876unsigned mode;2877const struct object_id *oid;2878const char*conf;28792880if(a_oid) {2881 add_branch = o->branch1;2882 other_branch = o->branch2;2883 mode = a_mode;2884 oid = a_oid;2885 conf =_("file/directory");2886}else{2887 add_branch = o->branch2;2888 other_branch = o->branch1;2889 mode = b_mode;2890 oid = b_oid;2891 conf =_("directory/file");2892}2893if(dir_in_way(path,2894!o->call_depth && !S_ISGITLINK(a_mode),28950)) {2896char*new_path =unique_path(o, path, add_branch);2897 clean_merge =0;2898output(o,1,_("CONFLICT (%s): There is a directory with name%sin%s. "2899"Adding%sas%s"),2900 conf, path, other_branch, path, new_path);2901if(update_file(o,0, oid, mode, new_path))2902 clean_merge = -1;2903else if(o->call_depth)2904remove_file_from_cache(path);2905free(new_path);2906}else{2907output(o,2,_("Adding%s"), path);2908/* do not overwrite file if already present */2909if(update_file_flags(o, oid, mode, path,1, !a_oid))2910 clean_merge = -1;2911}2912}else if(a_oid && b_oid) {2913/* Case C: Added in both (check for same permissions) and */2914/* case D: Modified in both, but differently. */2915 clean_merge =merge_content(o, path,2916 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,2917 NULL);2918}else if(!o_oid && !a_oid && !b_oid) {2919/*2920 * this entry was deleted altogether. a_mode == 0 means2921 * we had that path and want to actively remove it.2922 */2923remove_file(o,1, path, !a_mode);2924}else2925die("BUG: fatal merge failure, shouldn't happen.");29262927return clean_merge;2928}29292930intmerge_trees(struct merge_options *o,2931struct tree *head,2932struct tree *merge,2933struct tree *common,2934struct tree **result)2935{2936int code, clean;29372938if(o->subtree_shift) {2939 merge =shift_tree_object(head, merge, o->subtree_shift);2940 common =shift_tree_object(head, common, o->subtree_shift);2941}29422943if(oid_eq(&common->object.oid, &merge->object.oid)) {2944struct strbuf sb = STRBUF_INIT;29452946if(!o->call_depth &&index_has_changes(&sb)) {2947err(o,_("Dirty index: cannot merge (dirty:%s)"),2948 sb.buf);2949return0;2950}2951output(o,0,_("Already up to date!"));2952*result = head;2953return1;2954}29552956 code =git_merge_trees(o->call_depth, common, head, merge);29572958if(code !=0) {2959if(show(o,4) || o->call_depth)2960err(o,_("merging of trees%sand%sfailed"),2961oid_to_hex(&head->object.oid),2962oid_to_hex(&merge->object.oid));2963return-1;2964}29652966if(unmerged_cache()) {2967struct string_list *entries;2968struct rename_info re_info;2969int i;2970/*2971 * Only need the hashmap while processing entries, so2972 * initialize it here and free it when we are done running2973 * through the entries. Keeping it in the merge_options as2974 * opposed to decaring a local hashmap is for convenience2975 * so that we don't have to pass it to around.2976 */2977hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL,512);2978get_files_dirs(o, head);2979get_files_dirs(o, merge);29802981 entries =get_unmerged();2982 clean =handle_renames(o, common, head, merge, entries,2983&re_info);2984record_df_conflict_files(o, entries);2985if(clean <0)2986goto cleanup;2987for(i = entries->nr-1;0<= i; i--) {2988const char*path = entries->items[i].string;2989struct stage_data *e = entries->items[i].util;2990if(!e->processed) {2991int ret =process_entry(o, path, e);2992if(!ret)2993 clean =0;2994else if(ret <0) {2995 clean = ret;2996goto cleanup;2997}2998}2999}3000for(i =0; i < entries->nr; i++) {3001struct stage_data *e = entries->items[i].util;3002if(!e->processed)3003die("BUG: unprocessed path???%s",3004 entries->items[i].string);3005}30063007cleanup:3008final_cleanup_renames(&re_info);30093010string_list_clear(entries,1);3011free(entries);30123013hashmap_free(&o->current_file_dir_set,1);30143015if(clean <0)3016return clean;3017}3018else3019 clean =1;30203021if(o->call_depth && !(*result =write_tree_from_memory(o)))3022return-1;30233024return clean;3025}30263027static struct commit_list *reverse_commit_list(struct commit_list *list)3028{3029struct commit_list *next = NULL, *current, *backup;3030for(current = list; current; current = backup) {3031 backup = current->next;3032 current->next = next;3033 next = current;3034}3035return next;3036}30373038/*3039 * Merge the commits h1 and h2, return the resulting virtual3040 * commit object and a flag indicating the cleanness of the merge.3041 */3042intmerge_recursive(struct merge_options *o,3043struct commit *h1,3044struct commit *h2,3045struct commit_list *ca,3046struct commit **result)3047{3048struct commit_list *iter;3049struct commit *merged_common_ancestors;3050struct tree *mrtree;3051int clean;30523053if(show(o,4)) {3054output(o,4,_("Merging:"));3055output_commit_title(o, h1);3056output_commit_title(o, h2);3057}30583059if(!ca) {3060 ca =get_merge_bases(h1, h2);3061 ca =reverse_commit_list(ca);3062}30633064if(show(o,5)) {3065unsigned cnt =commit_list_count(ca);30663067output(o,5,Q_("found%ucommon ancestor:",3068"found%ucommon ancestors:", cnt), cnt);3069for(iter = ca; iter; iter = iter->next)3070output_commit_title(o, iter->item);3071}30723073 merged_common_ancestors =pop_commit(&ca);3074if(merged_common_ancestors == NULL) {3075/* if there is no common ancestor, use an empty tree */3076struct tree *tree;30773078 tree =lookup_tree(the_hash_algo->empty_tree);3079 merged_common_ancestors =make_virtual_commit(tree,"ancestor");3080}30813082for(iter = ca; iter; iter = iter->next) {3083const char*saved_b1, *saved_b2;3084 o->call_depth++;3085/*3086 * When the merge fails, the result contains files3087 * with conflict markers. The cleanness flag is3088 * ignored (unless indicating an error), it was never3089 * actually used, as result of merge_trees has always3090 * overwritten it: the committed "conflicts" were3091 * already resolved.3092 */3093discard_cache();3094 saved_b1 = o->branch1;3095 saved_b2 = o->branch2;3096 o->branch1 ="Temporary merge branch 1";3097 o->branch2 ="Temporary merge branch 2";3098if(merge_recursive(o, merged_common_ancestors, iter->item,3099 NULL, &merged_common_ancestors) <0)3100return-1;3101 o->branch1 = saved_b1;3102 o->branch2 = saved_b2;3103 o->call_depth--;31043105if(!merged_common_ancestors)3106returnerr(o,_("merge returned no commit"));3107}31083109discard_cache();3110if(!o->call_depth)3111read_cache();31123113 o->ancestor ="merged common ancestors";3114 clean =merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,3115&mrtree);3116if(clean <0) {3117flush_output(o);3118return clean;3119}31203121if(o->call_depth) {3122*result =make_virtual_commit(mrtree,"merged tree");3123commit_list_insert(h1, &(*result)->parents);3124commit_list_insert(h2, &(*result)->parents->next);3125}3126flush_output(o);3127if(!o->call_depth && o->buffer_output <2)3128strbuf_release(&o->obuf);3129if(show(o,2))3130diff_warn_rename_limit("merge.renamelimit",3131 o->needed_rename_limit,0);3132return clean;3133}31343135static struct commit *get_ref(const struct object_id *oid,const char*name)3136{3137struct object *object;31383139 object =deref_tag(parse_object(oid), name,strlen(name));3140if(!object)3141return NULL;3142if(object->type == OBJ_TREE)3143returnmake_virtual_commit((struct tree*)object, name);3144if(object->type != OBJ_COMMIT)3145return NULL;3146if(parse_commit((struct commit *)object))3147return NULL;3148return(struct commit *)object;3149}31503151intmerge_recursive_generic(struct merge_options *o,3152const struct object_id *head,3153const struct object_id *merge,3154int num_base_list,3155const struct object_id **base_list,3156struct commit **result)3157{3158int clean;3159struct lock_file lock = LOCK_INIT;3160struct commit *head_commit =get_ref(head, o->branch1);3161struct commit *next_commit =get_ref(merge, o->branch2);3162struct commit_list *ca = NULL;31633164if(base_list) {3165int i;3166for(i =0; i < num_base_list; ++i) {3167struct commit *base;3168if(!(base =get_ref(base_list[i],oid_to_hex(base_list[i]))))3169returnerr(o,_("Could not parse object '%s'"),3170oid_to_hex(base_list[i]));3171commit_list_insert(base, &ca);3172}3173}31743175hold_locked_index(&lock, LOCK_DIE_ON_ERROR);3176 clean =merge_recursive(o, head_commit, next_commit, ca,3177 result);3178if(clean <0) {3179rollback_lock_file(&lock);3180return clean;3181}31823183if(write_locked_index(&the_index, &lock,3184 COMMIT_LOCK | SKIP_IF_UNCHANGED))3185returnerr(o,_("Unable to write index."));31863187return clean ?0:1;3188}31893190static voidmerge_recursive_config(struct merge_options *o)3191{3192git_config_get_int("merge.verbosity", &o->verbosity);3193git_config_get_int("diff.renamelimit", &o->diff_rename_limit);3194git_config_get_int("merge.renamelimit", &o->merge_rename_limit);3195git_config(git_xmerge_config, NULL);3196}31973198voidinit_merge_options(struct merge_options *o)3199{3200const char*merge_verbosity;3201memset(o,0,sizeof(struct merge_options));3202 o->verbosity =2;3203 o->buffer_output =1;3204 o->diff_rename_limit = -1;3205 o->merge_rename_limit = -1;3206 o->renormalize =0;3207 o->detect_rename =1;3208merge_recursive_config(o);3209 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3210if(merge_verbosity)3211 o->verbosity =strtol(merge_verbosity, NULL,10);3212if(o->verbosity >=5)3213 o->buffer_output =0;3214strbuf_init(&o->obuf,0);3215string_list_init(&o->df_conflict_file_set,1);3216}32173218intparse_merge_opt(struct merge_options *o,const char*s)3219{3220const char*arg;32213222if(!s || !*s)3223return-1;3224if(!strcmp(s,"ours"))3225 o->recursive_variant = MERGE_RECURSIVE_OURS;3226else if(!strcmp(s,"theirs"))3227 o->recursive_variant = MERGE_RECURSIVE_THEIRS;3228else if(!strcmp(s,"subtree"))3229 o->subtree_shift ="";3230else if(skip_prefix(s,"subtree=", &arg))3231 o->subtree_shift = arg;3232else if(!strcmp(s,"patience"))3233 o->xdl_opts =DIFF_WITH_ALG(o, PATIENCE_DIFF);3234else if(!strcmp(s,"histogram"))3235 o->xdl_opts =DIFF_WITH_ALG(o, HISTOGRAM_DIFF);3236else if(skip_prefix(s,"diff-algorithm=", &arg)) {3237long value =parse_algorithm_value(arg);3238if(value <0)3239return-1;3240/* clear out previous settings */3241DIFF_XDL_CLR(o, NEED_MINIMAL);3242 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3243 o->xdl_opts |= value;3244}3245else if(!strcmp(s,"ignore-space-change"))3246DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);3247else if(!strcmp(s,"ignore-all-space"))3248DIFF_XDL_SET(o, IGNORE_WHITESPACE);3249else if(!strcmp(s,"ignore-space-at-eol"))3250DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);3251else if(!strcmp(s,"ignore-cr-at-eol"))3252DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);3253else if(!strcmp(s,"renormalize"))3254 o->renormalize =1;3255else if(!strcmp(s,"no-renormalize"))3256 o->renormalize =0;3257else if(!strcmp(s,"no-renames"))3258 o->detect_rename =0;3259else if(!strcmp(s,"find-renames")) {3260 o->detect_rename =1;3261 o->rename_score =0;3262}3263else if(skip_prefix(s,"find-renames=", &arg) ||3264skip_prefix(s,"rename-threshold=", &arg)) {3265if((o->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3266return-1;3267 o->detect_rename =1;3268}3269else3270return-1;3271return0;3272}