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.hash, 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 unsigned char*sha1, 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 unsigned char*tree, 426const char*path, 427unsigned char*hashy, 428unsigned int*mode_o) 429{ 430int ret; 431 432 ret =get_tree_entry(tree, path, hashy, mode_o); 433if(S_ISDIR(*mode_o)) { 434hashcpy(hashy, null_sha1); 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.hash, path, 451 e->stages[1].oid.hash, &e->stages[1].mode); 452get_tree_entry_if_blob(a->object.oid.hash, path, 453 e->stages[2].oid.hash, &e->stages[2].mode); 454get_tree_entry_if_blob(b->object.oid.hash, path, 455 e->stages[3].oid.hash, &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_sha1_file(oid->hash, &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 &&write_sha1_file(result_buf.ptr, result_buf.size,1062 blob_type, result->oid.hash))1063 ret =err(o,_("Unable to add%sto database"),1064 a->path);10651066free(result_buf.ptr);1067if(ret)1068return ret;1069 result->clean = (merge_status ==0);1070}else if(S_ISGITLINK(a->mode)) {1071 result->clean =merge_submodule(&result->oid,1072 one->path,1073&one->oid,1074&a->oid,1075&b->oid,1076!o->call_depth);1077}else if(S_ISLNK(a->mode)) {1078oidcpy(&result->oid, &a->oid);10791080if(!oid_eq(&a->oid, &b->oid))1081 result->clean =0;1082}else1083die("BUG: unsupported object type in the tree");1084}10851086return0;1087}10881089static intmerge_file_special_markers(struct merge_options *o,1090const struct diff_filespec *one,1091const struct diff_filespec *a,1092const struct diff_filespec *b,1093const char*branch1,1094const char*filename1,1095const char*branch2,1096const char*filename2,1097struct merge_file_info *mfi)1098{1099char*side1 = NULL;1100char*side2 = NULL;1101int ret;11021103if(filename1)1104 side1 =xstrfmt("%s:%s", branch1, filename1);1105if(filename2)1106 side2 =xstrfmt("%s:%s", branch2, filename2);11071108 ret =merge_file_1(o, one, a, b,1109 side1 ? side1 : branch1,1110 side2 ? side2 : branch2, mfi);1111free(side1);1112free(side2);1113return ret;1114}11151116static intmerge_file_one(struct merge_options *o,1117const char*path,1118const struct object_id *o_oid,int o_mode,1119const struct object_id *a_oid,int a_mode,1120const struct object_id *b_oid,int b_mode,1121const char*branch1,1122const char*branch2,1123struct merge_file_info *mfi)1124{1125struct diff_filespec one, a, b;11261127 one.path = a.path = b.path = (char*)path;1128oidcpy(&one.oid, o_oid);1129 one.mode = o_mode;1130oidcpy(&a.oid, a_oid);1131 a.mode = a_mode;1132oidcpy(&b.oid, b_oid);1133 b.mode = b_mode;1134returnmerge_file_1(o, &one, &a, &b, branch1, branch2, mfi);1135}11361137static intconflict_rename_dir(struct merge_options *o,1138struct diff_filepair *pair,1139const char*rename_branch,1140const char*other_branch)1141{1142const struct diff_filespec *dest = pair->two;11431144if(!o->call_depth &&would_lose_untracked(dest->path)) {1145char*alt_path =unique_path(o, dest->path, rename_branch);11461147output(o,1,_("Error: Refusing to lose untracked file at%s; "1148"writing to%sinstead."),1149 dest->path, alt_path);1150/*1151 * Write the file in worktree at alt_path, but not in the1152 * index. Instead, write to dest->path for the index but1153 * only at the higher appropriate stage.1154 */1155if(update_file(o,0, &dest->oid, dest->mode, alt_path))1156return-1;1157free(alt_path);1158returnupdate_stages(o, dest->path, NULL,1159 rename_branch == o->branch1 ? dest : NULL,1160 rename_branch == o->branch1 ? NULL : dest);1161}11621163/* Update dest->path both in index and in worktree */1164if(update_file(o,1, &dest->oid, dest->mode, dest->path))1165return-1;1166return0;1167}11681169static inthandle_change_delete(struct merge_options *o,1170const char*path,const char*old_path,1171const struct object_id *o_oid,int o_mode,1172const struct object_id *changed_oid,1173int changed_mode,1174const char*change_branch,1175const char*delete_branch,1176const char*change,const char*change_past)1177{1178char*alt_path = NULL;1179const char*update_path = path;1180int ret =0;11811182if(dir_in_way(path, !o->call_depth,0) ||1183(!o->call_depth &&would_lose_untracked(path))) {1184 update_path = alt_path =unique_path(o, path, change_branch);1185}11861187if(o->call_depth) {1188/*1189 * We cannot arbitrarily accept either a_sha or b_sha as1190 * correct; since there is no true "middle point" between1191 * them, simply reuse the base version for virtual merge base.1192 */1193 ret =remove_file_from_cache(path);1194if(!ret)1195 ret =update_file(o,0, o_oid, o_mode, update_path);1196}else{1197if(!alt_path) {1198if(!old_path) {1199output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1200"and%sin%s. Version%sof%sleft in tree."),1201 change, path, delete_branch, change_past,1202 change_branch, change_branch, path);1203}else{1204output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1205"and%sto%sin%s. Version%sof%sleft in tree."),1206 change, old_path, delete_branch, change_past, path,1207 change_branch, change_branch, path);1208}1209}else{1210if(!old_path) {1211output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1212"and%sin%s. Version%sof%sleft in tree at%s."),1213 change, path, delete_branch, change_past,1214 change_branch, change_branch, path, alt_path);1215}else{1216output(o,1,_("CONFLICT (%s/delete):%sdeleted in%s"1217"and%sto%sin%s. Version%sof%sleft in tree at%s."),1218 change, old_path, delete_branch, change_past, path,1219 change_branch, change_branch, path, alt_path);1220}1221}1222/*1223 * No need to call update_file() on path when change_branch ==1224 * o->branch1 && !alt_path, since that would needlessly touch1225 * path. We could call update_file_flags() with update_cache=01226 * and update_wd=0, but that's a no-op.1227 */1228if(change_branch != o->branch1 || alt_path)1229 ret =update_file(o,0, changed_oid, changed_mode, update_path);1230}1231free(alt_path);12321233return ret;1234}12351236static intconflict_rename_delete(struct merge_options *o,1237struct diff_filepair *pair,1238const char*rename_branch,1239const char*delete_branch)1240{1241const struct diff_filespec *orig = pair->one;1242const struct diff_filespec *dest = pair->two;12431244if(handle_change_delete(o,1245 o->call_depth ? orig->path : dest->path,1246 o->call_depth ? NULL : orig->path,1247&orig->oid, orig->mode,1248&dest->oid, dest->mode,1249 rename_branch, delete_branch,1250_("rename"),_("renamed")))1251return-1;12521253if(o->call_depth)1254returnremove_file_from_cache(dest->path);1255else1256returnupdate_stages(o, dest->path, NULL,1257 rename_branch == o->branch1 ? dest : NULL,1258 rename_branch == o->branch1 ? NULL : dest);1259}12601261static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,1262struct stage_data *entry,1263int stage)1264{1265struct object_id *oid = &entry->stages[stage].oid;1266unsigned mode = entry->stages[stage].mode;1267if(mode ==0||is_null_oid(oid))1268return NULL;1269oidcpy(&target->oid, oid);1270 target->mode = mode;1271return target;1272}12731274static inthandle_file(struct merge_options *o,1275struct diff_filespec *rename,1276int stage,1277struct rename_conflict_info *ci)1278{1279char*dst_name = rename->path;1280struct stage_data *dst_entry;1281const char*cur_branch, *other_branch;1282struct diff_filespec other;1283struct diff_filespec *add;1284int ret;12851286if(stage ==2) {1287 dst_entry = ci->dst_entry1;1288 cur_branch = ci->branch1;1289 other_branch = ci->branch2;1290}else{1291 dst_entry = ci->dst_entry2;1292 cur_branch = ci->branch2;1293 other_branch = ci->branch1;1294}12951296 add =filespec_from_entry(&other, dst_entry, stage ^1);1297if(add) {1298char*add_name =unique_path(o, rename->path, other_branch);1299if(update_file(o,0, &add->oid, add->mode, add_name))1300return-1;13011302remove_file(o,0, rename->path,0);1303 dst_name =unique_path(o, rename->path, cur_branch);1304}else{1305if(dir_in_way(rename->path, !o->call_depth,0)) {1306 dst_name =unique_path(o, rename->path, cur_branch);1307output(o,1,_("%sis a directory in%sadding as%sinstead"),1308 rename->path, other_branch, dst_name);1309}else if(!o->call_depth &&1310would_lose_untracked(rename->path)) {1311 dst_name =unique_path(o, rename->path, cur_branch);1312output(o,1,_("Refusing to lose untracked file at%s; "1313"adding as%sinstead"),1314 rename->path, dst_name);1315}1316}1317if((ret =update_file(o,0, &rename->oid, rename->mode, dst_name)))1318;/* fall through, do allow dst_name to be released */1319else if(stage ==2)1320 ret =update_stages(o, rename->path, NULL, rename, add);1321else1322 ret =update_stages(o, rename->path, NULL, add, rename);13231324if(dst_name != rename->path)1325free(dst_name);13261327return ret;1328}13291330static intconflict_rename_rename_1to2(struct merge_options *o,1331struct rename_conflict_info *ci)1332{1333/* One file was renamed in both branches, but to different names. */1334struct diff_filespec *one = ci->pair1->one;1335struct diff_filespec *a = ci->pair1->two;1336struct diff_filespec *b = ci->pair2->two;13371338output(o,1,_("CONFLICT (rename/rename): "1339"Rename\"%s\"->\"%s\"in branch\"%s\""1340"rename\"%s\"->\"%s\"in\"%s\"%s"),1341 one->path, a->path, ci->branch1,1342 one->path, b->path, ci->branch2,1343 o->call_depth ?_(" (left unresolved)") :"");1344if(o->call_depth) {1345struct merge_file_info mfi;1346struct diff_filespec other;1347struct diff_filespec *add;1348if(merge_file_one(o, one->path,1349&one->oid, one->mode,1350&a->oid, a->mode,1351&b->oid, b->mode,1352 ci->branch1, ci->branch2, &mfi))1353return-1;13541355/*1356 * FIXME: For rename/add-source conflicts (if we could detect1357 * such), this is wrong. We should instead find a unique1358 * pathname and then either rename the add-source file to that1359 * unique path, or use that unique path instead of src here.1360 */1361if(update_file(o,0, &mfi.oid, mfi.mode, one->path))1362return-1;13631364/*1365 * Above, we put the merged content at the merge-base's1366 * path. Now we usually need to delete both a->path and1367 * b->path. However, the rename on each side of the merge1368 * could also be involved in a rename/add conflict. In1369 * such cases, we should keep the added file around,1370 * resolving the conflict at that path in its favor.1371 */1372 add =filespec_from_entry(&other, ci->dst_entry1,2^1);1373if(add) {1374if(update_file(o,0, &add->oid, add->mode, a->path))1375return-1;1376}1377else1378remove_file_from_cache(a->path);1379 add =filespec_from_entry(&other, ci->dst_entry2,3^1);1380if(add) {1381if(update_file(o,0, &add->oid, add->mode, b->path))1382return-1;1383}1384else1385remove_file_from_cache(b->path);1386}else if(handle_file(o, a,2, ci) ||handle_file(o, b,3, ci))1387return-1;13881389return0;1390}13911392static intconflict_rename_rename_2to1(struct merge_options *o,1393struct rename_conflict_info *ci)1394{1395/* Two files, a & b, were renamed to the same thing, c. */1396struct diff_filespec *a = ci->pair1->one;1397struct diff_filespec *b = ci->pair2->one;1398struct diff_filespec *c1 = ci->pair1->two;1399struct diff_filespec *c2 = ci->pair2->two;1400char*path = c1->path;/* == c2->path */1401struct merge_file_info mfi_c1;1402struct merge_file_info mfi_c2;1403int ret;14041405output(o,1,_("CONFLICT (rename/rename): "1406"Rename%s->%sin%s. "1407"Rename%s->%sin%s"),1408 a->path, c1->path, ci->branch1,1409 b->path, c2->path, ci->branch2);14101411remove_file(o,1, a->path, o->call_depth ||would_lose_untracked(a->path));1412remove_file(o,1, b->path, o->call_depth ||would_lose_untracked(b->path));14131414if(merge_file_special_markers(o, a, c1, &ci->ren1_other,1415 o->branch1, c1->path,1416 o->branch2, ci->ren1_other.path, &mfi_c1) ||1417merge_file_special_markers(o, b, &ci->ren2_other, c2,1418 o->branch1, ci->ren2_other.path,1419 o->branch2, c2->path, &mfi_c2))1420return-1;14211422if(o->call_depth) {1423/*1424 * If mfi_c1.clean && mfi_c2.clean, then it might make1425 * sense to do a two-way merge of those results. But, I1426 * think in all cases, it makes sense to have the virtual1427 * merge base just undo the renames; they can be detected1428 * again later for the non-recursive merge.1429 */1430remove_file(o,0, path,0);1431 ret =update_file(o,0, &mfi_c1.oid, mfi_c1.mode, a->path);1432if(!ret)1433 ret =update_file(o,0, &mfi_c2.oid, mfi_c2.mode,1434 b->path);1435}else{1436char*new_path1 =unique_path(o, path, ci->branch1);1437char*new_path2 =unique_path(o, path, ci->branch2);1438output(o,1,_("Renaming%sto%sand%sto%sinstead"),1439 a->path, new_path1, b->path, new_path2);1440if(would_lose_untracked(path))1441/*1442 * Only way we get here is if both renames were from1443 * a directory rename AND user had an untracked file1444 * at the location where both files end up after the1445 * two directory renames. See testcase 10d of t6043.1446 */1447output(o,1,_("Refusing to lose untracked file at "1448"%s, even though it's in the way."),1449 path);1450else1451remove_file(o,0, path,0);1452 ret =update_file(o,0, &mfi_c1.oid, mfi_c1.mode, new_path1);1453if(!ret)1454 ret =update_file(o,0, &mfi_c2.oid, mfi_c2.mode,1455 new_path2);1456/*1457 * unpack_trees() actually populates the index for us for1458 * "normal" rename/rename(2to1) situtations so that the1459 * correct entries are at the higher stages, which would1460 * make the call below to update_stages_for_stage_data1461 * unnecessary. However, if either of the renames came1462 * from a directory rename, then unpack_trees() will not1463 * have gotten the right data loaded into the index, so we1464 * need to do so now. (While it'd be tempting to move this1465 * call to update_stages_for_stage_data() to1466 * apply_directory_rename_modifications(), that would break1467 * our intermediate calls to would_lose_untracked() since1468 * those rely on the current in-memory index. See also the1469 * big "NOTE" in update_stages()).1470 */1471if(update_stages_for_stage_data(o, path, ci->dst_entry1))1472 ret = -1;14731474free(new_path2);1475free(new_path1);1476}14771478return ret;1479}14801481/*1482 * Get the diff_filepairs changed between o_tree and tree.1483 */1484static struct diff_queue_struct *get_diffpairs(struct merge_options *o,1485struct tree *o_tree,1486struct tree *tree)1487{1488struct diff_queue_struct *ret;1489struct diff_options opts;14901491diff_setup(&opts);1492 opts.flags.recursive =1;1493 opts.flags.rename_empty =0;1494 opts.detect_rename = DIFF_DETECT_RENAME;1495 opts.rename_limit = o->merge_rename_limit >=0? o->merge_rename_limit :1496 o->diff_rename_limit >=0? o->diff_rename_limit :14971000;1498 opts.rename_score = o->rename_score;1499 opts.show_rename_progress = o->show_rename_progress;1500 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1501diff_setup_done(&opts);1502diff_tree_oid(&o_tree->object.oid, &tree->object.oid,"", &opts);1503diffcore_std(&opts);1504if(opts.needed_rename_limit > o->needed_rename_limit)1505 o->needed_rename_limit = opts.needed_rename_limit;15061507 ret =xmalloc(sizeof(*ret));1508*ret = diff_queued_diff;15091510 opts.output_format = DIFF_FORMAT_NO_OUTPUT;1511 diff_queued_diff.nr =0;1512 diff_queued_diff.queue = NULL;1513diff_flush(&opts);1514return ret;1515}15161517static inttree_has_path(struct tree *tree,const char*path)1518{1519unsigned char hashy[GIT_MAX_RAWSZ];1520unsigned int mode_o;15211522return!get_tree_entry(tree->object.oid.hash, path,1523 hashy, &mode_o);1524}15251526/*1527 * Return a new string that replaces the beginning portion (which matches1528 * entry->dir), with entry->new_dir. In perl-speak:1529 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);1530 * NOTE:1531 * Caller must ensure that old_path starts with entry->dir + '/'.1532 */1533static char*apply_dir_rename(struct dir_rename_entry *entry,1534const char*old_path)1535{1536struct strbuf new_path = STRBUF_INIT;1537int oldlen, newlen;15381539if(entry->non_unique_new_dir)1540return NULL;15411542 oldlen =strlen(entry->dir);1543 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) +1;1544strbuf_grow(&new_path, newlen);1545strbuf_addbuf(&new_path, &entry->new_dir);1546strbuf_addstr(&new_path, &old_path[oldlen]);15471548returnstrbuf_detach(&new_path, NULL);1549}15501551static voidget_renamed_dir_portion(const char*old_path,const char*new_path,1552char**old_dir,char**new_dir)1553{1554char*end_of_old, *end_of_new;1555int old_len, new_len;15561557*old_dir = NULL;1558*new_dir = NULL;15591560/*1561 * For1562 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"1563 * the "e/foo.c" part is the same, we just want to know that1564 * "a/b/c/d" was renamed to "a/b/some/thing/else"1565 * so, for this example, this function returns "a/b/c/d" in1566 * *old_dir and "a/b/some/thing/else" in *new_dir.1567 *1568 * Also, if the basename of the file changed, we don't care. We1569 * want to know which portion of the directory, if any, changed.1570 */1571 end_of_old =strrchr(old_path,'/');1572 end_of_new =strrchr(new_path,'/');15731574if(end_of_old == NULL || end_of_new == NULL)1575return;1576while(*--end_of_new == *--end_of_old &&1577 end_of_old != old_path &&1578 end_of_new != new_path)1579;/* Do nothing; all in the while loop */1580/*1581 * We've found the first non-matching character in the directory1582 * paths. That means the current directory we were comparing1583 * represents the rename. Move end_of_old and end_of_new back1584 * to the full directory name.1585 */1586if(*end_of_old =='/')1587 end_of_old++;1588if(*end_of_old !='/')1589 end_of_new++;1590 end_of_old =strchr(end_of_old,'/');1591 end_of_new =strchr(end_of_new,'/');15921593/*1594 * It may have been the case that old_path and new_path were the same1595 * directory all along. Don't claim a rename if they're the same.1596 */1597 old_len = end_of_old - old_path;1598 new_len = end_of_new - new_path;15991600if(old_len != new_len ||strncmp(old_path, new_path, old_len)) {1601*old_dir =xstrndup(old_path, old_len);1602*new_dir =xstrndup(new_path, new_len);1603}1604}16051606static voidremove_hashmap_entries(struct hashmap *dir_renames,1607struct string_list *items_to_remove)1608{1609int i;1610struct dir_rename_entry *entry;16111612for(i =0; i < items_to_remove->nr; i++) {1613 entry = items_to_remove->items[i].util;1614hashmap_remove(dir_renames, entry, NULL);1615}1616string_list_clear(items_to_remove,0);1617}16181619/*1620 * See if there is a directory rename for path, and if there are any file1621 * level conflicts for the renamed location. If there is a rename and1622 * there are no conflicts, return the new name. Otherwise, return NULL.1623 */1624static char*handle_path_level_conflicts(struct merge_options *o,1625const char*path,1626struct dir_rename_entry *entry,1627struct hashmap *collisions,1628struct tree *tree)1629{1630char*new_path = NULL;1631struct collision_entry *collision_ent;1632int clean =1;1633struct strbuf collision_paths = STRBUF_INIT;16341635/*1636 * entry has the mapping of old directory name to new directory name1637 * that we want to apply to path.1638 */1639 new_path =apply_dir_rename(entry, path);16401641if(!new_path) {1642/* This should only happen when entry->non_unique_new_dir set */1643if(!entry->non_unique_new_dir)1644BUG("entry->non_unqiue_dir not set and !new_path");1645output(o,1,_("CONFLICT (directory rename split): "1646"Unclear where to place%sbecause directory "1647"%swas renamed to multiple other directories, "1648"with no destination getting a majority of the "1649"files."),1650 path, entry->dir);1651 clean =0;1652return NULL;1653}16541655/*1656 * The caller needs to have ensured that it has pre-populated1657 * collisions with all paths that map to new_path. Do a quick check1658 * to ensure that's the case.1659 */1660 collision_ent =collision_find_entry(collisions, new_path);1661if(collision_ent == NULL)1662BUG("collision_ent is NULL");16631664/*1665 * Check for one-sided add/add/.../add conflicts, i.e.1666 * where implicit renames from the other side doing1667 * directory rename(s) can affect this side of history1668 * to put multiple paths into the same location. Warn1669 * and bail on directory renames for such paths.1670 */1671if(collision_ent->reported_already) {1672 clean =0;1673}else if(tree_has_path(tree, new_path)) {1674 collision_ent->reported_already =1;1675strbuf_add_separated_string_list(&collision_paths,", ",1676&collision_ent->source_files);1677output(o,1,_("CONFLICT (implicit dir rename): Existing "1678"file/dir at%sin the way of implicit "1679"directory rename(s) putting the following "1680"path(s) there:%s."),1681 new_path, collision_paths.buf);1682 clean =0;1683}else if(collision_ent->source_files.nr >1) {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): Cannot map "1688"more than one path to%s; implicit directory "1689"renames tried to put these paths there:%s"),1690 new_path, collision_paths.buf);1691 clean =0;1692}16931694/* Free memory we no longer need */1695strbuf_release(&collision_paths);1696if(!clean && new_path) {1697free(new_path);1698return NULL;1699}17001701return new_path;1702}17031704/*1705 * There are a couple things we want to do at the directory level:1706 * 1. Check for both sides renaming to the same thing, in order to avoid1707 * implicit renaming of files that should be left in place. (See1708 * testcase 6b in t6043 for details.)1709 * 2. Prune directory renames if there are still files left in the1710 * the original directory. These represent a partial directory rename,1711 * i.e. a rename where only some of the files within the directory1712 * were renamed elsewhere. (Technically, this could be done earlier1713 * in get_directory_renames(), except that would prevent us from1714 * doing the previous check and thus failing testcase 6b.)1715 * 3. Check for rename/rename(1to2) conflicts (at the directory level).1716 * In the future, we could potentially record this info as well and1717 * omit reporting rename/rename(1to2) conflicts for each path within1718 * the affected directories, thus cleaning up the merge output.1719 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the1720 * directory level, because merging directories is fine. If it1721 * causes conflicts for files within those merged directories, then1722 * that should be detected at the individual path level.1723 */1724static voidhandle_directory_level_conflicts(struct merge_options *o,1725struct hashmap *dir_re_head,1726struct tree *head,1727struct hashmap *dir_re_merge,1728struct tree *merge)1729{1730struct hashmap_iter iter;1731struct dir_rename_entry *head_ent;1732struct dir_rename_entry *merge_ent;17331734struct string_list remove_from_head = STRING_LIST_INIT_NODUP;1735struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;17361737hashmap_iter_init(dir_re_head, &iter);1738while((head_ent =hashmap_iter_next(&iter))) {1739 merge_ent =dir_rename_find_entry(dir_re_merge, head_ent->dir);1740if(merge_ent &&1741!head_ent->non_unique_new_dir &&1742!merge_ent->non_unique_new_dir &&1743!strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {1744/* 1. Renamed identically; remove it from both sides */1745string_list_append(&remove_from_head,1746 head_ent->dir)->util = head_ent;1747strbuf_release(&head_ent->new_dir);1748string_list_append(&remove_from_merge,1749 merge_ent->dir)->util = merge_ent;1750strbuf_release(&merge_ent->new_dir);1751}else if(tree_has_path(head, head_ent->dir)) {1752/* 2. This wasn't a directory rename after all */1753string_list_append(&remove_from_head,1754 head_ent->dir)->util = head_ent;1755strbuf_release(&head_ent->new_dir);1756}1757}17581759remove_hashmap_entries(dir_re_head, &remove_from_head);1760remove_hashmap_entries(dir_re_merge, &remove_from_merge);17611762hashmap_iter_init(dir_re_merge, &iter);1763while((merge_ent =hashmap_iter_next(&iter))) {1764 head_ent =dir_rename_find_entry(dir_re_head, merge_ent->dir);1765if(tree_has_path(merge, merge_ent->dir)) {1766/* 2. This wasn't a directory rename after all */1767string_list_append(&remove_from_merge,1768 merge_ent->dir)->util = merge_ent;1769}else if(head_ent &&1770!head_ent->non_unique_new_dir &&1771!merge_ent->non_unique_new_dir) {1772/* 3. rename/rename(1to2) */1773/*1774 * We can assume it's not rename/rename(1to1) because1775 * that was case (1), already checked above. So we1776 * know that head_ent->new_dir and merge_ent->new_dir1777 * are different strings.1778 */1779output(o,1,_("CONFLICT (rename/rename): "1780"Rename directory%s->%sin%s. "1781"Rename directory%s->%sin%s"),1782 head_ent->dir, head_ent->new_dir.buf, o->branch1,1783 head_ent->dir, merge_ent->new_dir.buf, o->branch2);1784string_list_append(&remove_from_head,1785 head_ent->dir)->util = head_ent;1786strbuf_release(&head_ent->new_dir);1787string_list_append(&remove_from_merge,1788 merge_ent->dir)->util = merge_ent;1789strbuf_release(&merge_ent->new_dir);1790}1791}17921793remove_hashmap_entries(dir_re_head, &remove_from_head);1794remove_hashmap_entries(dir_re_merge, &remove_from_merge);1795}17961797static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs,1798struct tree *tree)1799{1800struct hashmap *dir_renames;1801struct hashmap_iter iter;1802struct dir_rename_entry *entry;1803int i;18041805/*1806 * Typically, we think of a directory rename as all files from a1807 * certain directory being moved to a target directory. However,1808 * what if someone first moved two files from the original1809 * directory in one commit, and then renamed the directory1810 * somewhere else in a later commit? At merge time, we just know1811 * that files from the original directory went to two different1812 * places, and that the bulk of them ended up in the same place.1813 * We want each directory rename to represent where the bulk of the1814 * files from that directory end up; this function exists to find1815 * where the bulk of the files went.1816 *1817 * The first loop below simply iterates through the list of file1818 * renames, finding out how often each directory rename pair1819 * possibility occurs.1820 */1821 dir_renames =xmalloc(sizeof(struct hashmap));1822dir_rename_init(dir_renames);1823for(i =0; i < pairs->nr; ++i) {1824struct string_list_item *item;1825int*count;1826struct diff_filepair *pair = pairs->queue[i];1827char*old_dir, *new_dir;18281829/* File not part of directory rename if it wasn't renamed */1830if(pair->status !='R')1831continue;18321833get_renamed_dir_portion(pair->one->path, pair->two->path,1834&old_dir, &new_dir);1835if(!old_dir)1836/* Directory didn't change at all; ignore this one. */1837continue;18381839 entry =dir_rename_find_entry(dir_renames, old_dir);1840if(!entry) {1841 entry =xmalloc(sizeof(struct dir_rename_entry));1842dir_rename_entry_init(entry, old_dir);1843hashmap_put(dir_renames, entry);1844}else{1845free(old_dir);1846}1847 item =string_list_lookup(&entry->possible_new_dirs, new_dir);1848if(!item) {1849 item =string_list_insert(&entry->possible_new_dirs,1850 new_dir);1851 item->util =xcalloc(1,sizeof(int));1852}else{1853free(new_dir);1854}1855 count = item->util;1856*count +=1;1857}18581859/*1860 * For each directory with files moved out of it, we find out which1861 * target directory received the most files so we can declare it to1862 * be the "winning" target location for the directory rename. This1863 * winner gets recorded in new_dir. If there is no winner1864 * (multiple target directories received the same number of files),1865 * we set non_unique_new_dir. Once we've determined the winner (or1866 * that there is no winner), we no longer need possible_new_dirs.1867 */1868hashmap_iter_init(dir_renames, &iter);1869while((entry =hashmap_iter_next(&iter))) {1870int max =0;1871int bad_max =0;1872char*best = NULL;18731874for(i =0; i < entry->possible_new_dirs.nr; i++) {1875int*count = entry->possible_new_dirs.items[i].util;18761877if(*count == max)1878 bad_max = max;1879else if(*count > max) {1880 max = *count;1881 best = entry->possible_new_dirs.items[i].string;1882}1883}1884if(bad_max == max)1885 entry->non_unique_new_dir =1;1886else{1887assert(entry->new_dir.len ==0);1888strbuf_addstr(&entry->new_dir, best);1889}1890/*1891 * The relevant directory sub-portion of the original full1892 * filepaths were xstrndup'ed before inserting into1893 * possible_new_dirs, and instead of manually iterating the1894 * list and free'ing each, just lie and tell1895 * possible_new_dirs that it did the strdup'ing so that it1896 * will free them for us.1897 */1898 entry->possible_new_dirs.strdup_strings =1;1899string_list_clear(&entry->possible_new_dirs,1);1900}19011902return dir_renames;1903}19041905static struct dir_rename_entry *check_dir_renamed(const char*path,1906struct hashmap *dir_renames)1907{1908char temp[PATH_MAX];1909char*end;1910struct dir_rename_entry *entry;19111912strcpy(temp, path);1913while((end =strrchr(temp,'/'))) {1914*end ='\0';1915 entry =dir_rename_find_entry(dir_renames, temp);1916if(entry)1917return entry;1918}1919return NULL;1920}19211922static voidcompute_collisions(struct hashmap *collisions,1923struct hashmap *dir_renames,1924struct diff_queue_struct *pairs)1925{1926int i;19271928/*1929 * Multiple files can be mapped to the same path due to directory1930 * renames done by the other side of history. Since that other1931 * side of history could have merged multiple directories into one,1932 * if our side of history added the same file basename to each of1933 * those directories, then all N of them would get implicitly1934 * renamed by the directory rename detection into the same path,1935 * and we'd get an add/add/.../add conflict, and all those adds1936 * from *this* side of history. This is not representable in the1937 * index, and users aren't going to easily be able to make sense of1938 * it. So we need to provide a good warning about what's1939 * happening, and fall back to no-directory-rename detection1940 * behavior for those paths.1941 *1942 * See testcases 9e and all of section 5 from t6043 for examples.1943 */1944collision_init(collisions);19451946for(i =0; i < pairs->nr; ++i) {1947struct dir_rename_entry *dir_rename_ent;1948struct collision_entry *collision_ent;1949char*new_path;1950struct diff_filepair *pair = pairs->queue[i];19511952if(pair->status =='D')1953continue;1954 dir_rename_ent =check_dir_renamed(pair->two->path,1955 dir_renames);1956if(!dir_rename_ent)1957continue;19581959 new_path =apply_dir_rename(dir_rename_ent, pair->two->path);1960if(!new_path)1961/*1962 * dir_rename_ent->non_unique_new_path is true, which1963 * means there is no directory rename for us to use,1964 * which means it won't cause us any additional1965 * collisions.1966 */1967continue;1968 collision_ent =collision_find_entry(collisions, new_path);1969if(!collision_ent) {1970 collision_ent =xcalloc(1,1971sizeof(struct collision_entry));1972hashmap_entry_init(collision_ent,strhash(new_path));1973hashmap_put(collisions, collision_ent);1974 collision_ent->target_file = new_path;1975}else{1976free(new_path);1977}1978string_list_insert(&collision_ent->source_files,1979 pair->two->path);1980}1981}19821983static char*check_for_directory_rename(struct merge_options *o,1984const char*path,1985struct tree *tree,1986struct hashmap *dir_renames,1987struct hashmap *dir_rename_exclusions,1988struct hashmap *collisions,1989int*clean_merge)1990{1991char*new_path = NULL;1992struct dir_rename_entry *entry =check_dir_renamed(path, dir_renames);1993struct dir_rename_entry *oentry = NULL;19941995if(!entry)1996return new_path;19971998/*1999 * This next part is a little weird. We do not want to do an2000 * implicit rename into a directory we renamed on our side, because2001 * that will result in a spurious rename/rename(1to2) conflict. An2002 * example:2003 * Base commit: dumbdir/afile, otherdir/bfile2004 * Side 1: smrtdir/afile, otherdir/bfile2005 * Side 2: dumbdir/afile, dumbdir/bfile2006 * Here, while working on Side 1, we could notice that otherdir was2007 * renamed/merged to dumbdir, and change the diff_filepair for2008 * otherdir/bfile into a rename into dumbdir/bfile. However, Side2009 * 2 will notice the rename from dumbdir to smrtdir, and do the2010 * transitive rename to move it from dumbdir/bfile to2011 * smrtdir/bfile. That gives us bfile in dumbdir vs being in2012 * smrtdir, a rename/rename(1to2) conflict. We really just want2013 * the file to end up in smrtdir. And the way to achieve that is2014 * to not let Side1 do the rename to dumbdir, since we know that is2015 * the source of one of our directory renames.2016 *2017 * That's why oentry and dir_rename_exclusions is here.2018 *2019 * As it turns out, this also prevents N-way transient rename2020 * confusion; See testcases 9c and 9d of t6043.2021 */2022 oentry =dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);2023if(oentry) {2024output(o,1,_("WARNING: Avoiding applying%s->%srename "2025"to%s, because%sitself was renamed."),2026 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);2027}else{2028 new_path =handle_path_level_conflicts(o, path, entry,2029 collisions, tree);2030*clean_merge &= (new_path != NULL);2031}20322033return new_path;2034}20352036static voidapply_directory_rename_modifications(struct merge_options *o,2037struct diff_filepair *pair,2038char*new_path,2039struct rename *re,2040struct tree *tree,2041struct tree *o_tree,2042struct tree *a_tree,2043struct tree *b_tree,2044struct string_list *entries,2045int*clean)2046{2047struct string_list_item *item;2048int stage = (tree == a_tree ?2:3);20492050/*2051 * In all cases where we can do directory rename detection,2052 * unpack_trees() will have read pair->two->path into the2053 * index and the working copy. We need to remove it so that2054 * we can instead place it at new_path. It is guaranteed to2055 * not be untracked (unpack_trees() would have errored out2056 * saying the file would have been overwritten), but it might2057 * be dirty, though.2058 */2059remove_file(o,1, pair->two->path,0/* no_wd */);20602061/* Find or create a new re->dst_entry */2062 item =string_list_lookup(entries, new_path);2063if(item) {2064/*2065 * Since we're renaming on this side of history, and it's2066 * due to a directory rename on the other side of history2067 * (which we only allow when the directory in question no2068 * longer exists on the other side of history), the2069 * original entry for re->dst_entry is no longer2070 * necessary...2071 */2072 re->dst_entry->processed =1;20732074/*2075 * ...because we'll be using this new one.2076 */2077 re->dst_entry = item->util;2078}else{2079/*2080 * re->dst_entry is for the before-dir-rename path, and we2081 * need it to hold information for the after-dir-rename2082 * path. Before creating a new entry, we need to mark the2083 * old one as unnecessary (...unless it is shared by2084 * src_entry, i.e. this didn't use to be a rename, in which2085 * case we can just allow the normal processing to happen2086 * for it).2087 */2088if(pair->status =='R')2089 re->dst_entry->processed =1;20902091 re->dst_entry =insert_stage_data(new_path,2092 o_tree, a_tree, b_tree,2093 entries);2094 item =string_list_insert(entries, new_path);2095 item->util = re->dst_entry;2096}20972098/*2099 * Update the stage_data with the information about the path we are2100 * moving into place. That slot will be empty and available for us2101 * to write to because of the collision checks in2102 * handle_path_level_conflicts(). In other words,2103 * re->dst_entry->stages[stage].oid will be the null_oid, so it's2104 * open for us to write to.2105 *2106 * It may be tempting to actually update the index at this point as2107 * well, using update_stages_for_stage_data(), but as per the big2108 * "NOTE" in update_stages(), doing so will modify the current2109 * in-memory index which will break calls to would_lose_untracked()2110 * that we need to make. Instead, we need to just make sure that2111 * the various conflict_rename_*() functions update the index2112 * explicitly rather than relying on unpack_trees() to have done it.2113 */2114get_tree_entry(tree->object.oid.hash,2115 pair->two->path,2116 re->dst_entry->stages[stage].oid.hash,2117&re->dst_entry->stages[stage].mode);21182119/* Update pair status */2120if(pair->status =='A') {2121/*2122 * Recording rename information for this add makes it look2123 * like a rename/delete conflict. Make sure we can2124 * correctly handle this as an add that was moved to a new2125 * directory instead of reporting a rename/delete conflict.2126 */2127 re->add_turned_into_rename =1;2128}2129/*2130 * We don't actually look at pair->status again, but it seems2131 * pedagogically correct to adjust it.2132 */2133 pair->status ='R';21342135/*2136 * Finally, record the new location.2137 */2138 pair->two->path = new_path;2139}21402141/*2142 * Get information of all renames which occurred in 'pairs', making use of2143 * any implicit directory renames inferred from the other side of history.2144 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')2145 * to be able to associate the correct cache entries with the rename2146 * information; tree is always equal to either a_tree or b_tree.2147 */2148static struct string_list *get_renames(struct merge_options *o,2149struct diff_queue_struct *pairs,2150struct hashmap *dir_renames,2151struct hashmap *dir_rename_exclusions,2152struct tree *tree,2153struct tree *o_tree,2154struct tree *a_tree,2155struct tree *b_tree,2156struct string_list *entries,2157int*clean_merge)2158{2159int i;2160struct hashmap collisions;2161struct hashmap_iter iter;2162struct collision_entry *e;2163struct string_list *renames;21642165compute_collisions(&collisions, dir_renames, pairs);2166 renames =xcalloc(1,sizeof(struct string_list));21672168for(i =0; i < pairs->nr; ++i) {2169struct string_list_item *item;2170struct rename *re;2171struct diff_filepair *pair = pairs->queue[i];2172char*new_path;/* non-NULL only with directory renames */21732174if(pair->status =='D') {2175diff_free_filepair(pair);2176continue;2177}2178 new_path =check_for_directory_rename(o, pair->two->path, tree,2179 dir_renames,2180 dir_rename_exclusions,2181&collisions,2182 clean_merge);2183if(pair->status !='R'&& !new_path) {2184diff_free_filepair(pair);2185continue;2186}21872188 re =xmalloc(sizeof(*re));2189 re->processed =0;2190 re->add_turned_into_rename =0;2191 re->pair = pair;2192 item =string_list_lookup(entries, re->pair->one->path);2193if(!item)2194 re->src_entry =insert_stage_data(re->pair->one->path,2195 o_tree, a_tree, b_tree, entries);2196else2197 re->src_entry = item->util;21982199 item =string_list_lookup(entries, re->pair->two->path);2200if(!item)2201 re->dst_entry =insert_stage_data(re->pair->two->path,2202 o_tree, a_tree, b_tree, entries);2203else2204 re->dst_entry = item->util;2205 item =string_list_insert(renames, pair->one->path);2206 item->util = re;2207if(new_path)2208apply_directory_rename_modifications(o, pair, new_path,2209 re, tree, o_tree,2210 a_tree, b_tree,2211 entries,2212 clean_merge);2213}22142215hashmap_iter_init(&collisions, &iter);2216while((e =hashmap_iter_next(&iter))) {2217free(e->target_file);2218string_list_clear(&e->source_files,0);2219}2220hashmap_free(&collisions,1);2221return renames;2222}22232224static intprocess_renames(struct merge_options *o,2225struct string_list *a_renames,2226struct string_list *b_renames)2227{2228int clean_merge =1, i, j;2229struct string_list a_by_dst = STRING_LIST_INIT_NODUP;2230struct string_list b_by_dst = STRING_LIST_INIT_NODUP;2231const struct rename *sre;22322233for(i =0; i < a_renames->nr; i++) {2234 sre = a_renames->items[i].util;2235string_list_insert(&a_by_dst, sre->pair->two->path)->util2236= (void*)sre;2237}2238for(i =0; i < b_renames->nr; i++) {2239 sre = b_renames->items[i].util;2240string_list_insert(&b_by_dst, sre->pair->two->path)->util2241= (void*)sre;2242}22432244for(i =0, j =0; i < a_renames->nr || j < b_renames->nr;) {2245struct string_list *renames1, *renames2Dst;2246struct rename *ren1 = NULL, *ren2 = NULL;2247const char*branch1, *branch2;2248const char*ren1_src, *ren1_dst;2249struct string_list_item *lookup;22502251if(i >= a_renames->nr) {2252 ren2 = b_renames->items[j++].util;2253}else if(j >= b_renames->nr) {2254 ren1 = a_renames->items[i++].util;2255}else{2256int compare =strcmp(a_renames->items[i].string,2257 b_renames->items[j].string);2258if(compare <=0)2259 ren1 = a_renames->items[i++].util;2260if(compare >=0)2261 ren2 = b_renames->items[j++].util;2262}22632264/* TODO: refactor, so that 1/2 are not needed */2265if(ren1) {2266 renames1 = a_renames;2267 renames2Dst = &b_by_dst;2268 branch1 = o->branch1;2269 branch2 = o->branch2;2270}else{2271 renames1 = b_renames;2272 renames2Dst = &a_by_dst;2273 branch1 = o->branch2;2274 branch2 = o->branch1;2275SWAP(ren2, ren1);2276}22772278if(ren1->processed)2279continue;2280 ren1->processed =1;2281 ren1->dst_entry->processed =1;2282/* BUG: We should only mark src_entry as processed if we2283 * are not dealing with a rename + add-source case.2284 */2285 ren1->src_entry->processed =1;22862287 ren1_src = ren1->pair->one->path;2288 ren1_dst = ren1->pair->two->path;22892290if(ren2) {2291/* One file renamed on both sides */2292const char*ren2_src = ren2->pair->one->path;2293const char*ren2_dst = ren2->pair->two->path;2294enum rename_type rename_type;2295if(strcmp(ren1_src, ren2_src) !=0)2296die("BUG: ren1_src != ren2_src");2297 ren2->dst_entry->processed =1;2298 ren2->processed =1;2299if(strcmp(ren1_dst, ren2_dst) !=0) {2300 rename_type = RENAME_ONE_FILE_TO_TWO;2301 clean_merge =0;2302}else{2303 rename_type = RENAME_ONE_FILE_TO_ONE;2304/* BUG: We should only remove ren1_src in2305 * the base stage (think of rename +2306 * add-source cases).2307 */2308remove_file(o,1, ren1_src,1);2309update_entry(ren1->dst_entry,2310 ren1->pair->one,2311 ren1->pair->two,2312 ren2->pair->two);2313}2314setup_rename_conflict_info(rename_type,2315 ren1->pair,2316 ren2->pair,2317 branch1,2318 branch2,2319 ren1->dst_entry,2320 ren2->dst_entry,2321 o,2322 NULL,2323 NULL);2324}else if((lookup =string_list_lookup(renames2Dst, ren1_dst))) {2325/* Two different files renamed to the same thing */2326char*ren2_dst;2327 ren2 = lookup->util;2328 ren2_dst = ren2->pair->two->path;2329if(strcmp(ren1_dst, ren2_dst) !=0)2330die("BUG: ren1_dst != ren2_dst");23312332 clean_merge =0;2333 ren2->processed =1;2334/*2335 * BUG: We should only mark src_entry as processed2336 * if we are not dealing with a rename + add-source2337 * case.2338 */2339 ren2->src_entry->processed =1;23402341setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,2342 ren1->pair,2343 ren2->pair,2344 branch1,2345 branch2,2346 ren1->dst_entry,2347 ren2->dst_entry,2348 o,2349 ren1->src_entry,2350 ren2->src_entry);23512352}else{2353/* Renamed in 1, maybe changed in 2 */2354/* we only use sha1 and mode of these */2355struct diff_filespec src_other, dst_other;2356int try_merge;23572358/*2359 * unpack_trees loads entries from common-commit2360 * into stage 1, from head-commit into stage 2, and2361 * from merge-commit into stage 3. We keep track2362 * of which side corresponds to the rename.2363 */2364int renamed_stage = a_renames == renames1 ?2:3;2365int other_stage = a_renames == renames1 ?3:2;23662367/* BUG: We should only remove ren1_src in the base2368 * stage and in other_stage (think of rename +2369 * add-source case).2370 */2371remove_file(o,1, ren1_src,2372 renamed_stage ==2|| !was_tracked(ren1_src));23732374oidcpy(&src_other.oid,2375&ren1->src_entry->stages[other_stage].oid);2376 src_other.mode = ren1->src_entry->stages[other_stage].mode;2377oidcpy(&dst_other.oid,2378&ren1->dst_entry->stages[other_stage].oid);2379 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;2380 try_merge =0;23812382if(oid_eq(&src_other.oid, &null_oid) &&2383 ren1->add_turned_into_rename) {2384setup_rename_conflict_info(RENAME_DIR,2385 ren1->pair,2386 NULL,2387 branch1,2388 branch2,2389 ren1->dst_entry,2390 NULL,2391 o,2392 NULL,2393 NULL);2394}else if(oid_eq(&src_other.oid, &null_oid)) {2395setup_rename_conflict_info(RENAME_DELETE,2396 ren1->pair,2397 NULL,2398 branch1,2399 branch2,2400 ren1->dst_entry,2401 NULL,2402 o,2403 NULL,2404 NULL);2405}else if((dst_other.mode == ren1->pair->two->mode) &&2406oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {2407/*2408 * Added file on the other side identical to2409 * the file being renamed: clean merge.2410 * Also, there is no need to overwrite the2411 * file already in the working copy, so call2412 * update_file_flags() instead of2413 * update_file().2414 */2415if(update_file_flags(o,2416&ren1->pair->two->oid,2417 ren1->pair->two->mode,2418 ren1_dst,24191,/* update_cache */24200/* update_wd */))2421 clean_merge = -1;2422}else if(!oid_eq(&dst_other.oid, &null_oid)) {2423 clean_merge =0;2424 try_merge =1;2425output(o,1,_("CONFLICT (rename/add): Rename%s->%sin%s. "2426"%sadded in%s"),2427 ren1_src, ren1_dst, branch1,2428 ren1_dst, branch2);2429if(o->call_depth) {2430struct merge_file_info mfi;2431if(merge_file_one(o, ren1_dst, &null_oid,0,2432&ren1->pair->two->oid,2433 ren1->pair->two->mode,2434&dst_other.oid,2435 dst_other.mode,2436 branch1, branch2, &mfi)) {2437 clean_merge = -1;2438goto cleanup_and_return;2439}2440output(o,1,_("Adding merged%s"), ren1_dst);2441if(update_file(o,0, &mfi.oid,2442 mfi.mode, ren1_dst))2443 clean_merge = -1;2444 try_merge =0;2445}else{2446char*new_path =unique_path(o, ren1_dst, branch2);2447output(o,1,_("Adding as%sinstead"), new_path);2448if(update_file(o,0, &dst_other.oid,2449 dst_other.mode, new_path))2450 clean_merge = -1;2451free(new_path);2452}2453}else2454 try_merge =1;24552456if(clean_merge <0)2457goto cleanup_and_return;2458if(try_merge) {2459struct diff_filespec *one, *a, *b;2460 src_other.path = (char*)ren1_src;24612462 one = ren1->pair->one;2463if(a_renames == renames1) {2464 a = ren1->pair->two;2465 b = &src_other;2466}else{2467 b = ren1->pair->two;2468 a = &src_other;2469}2470update_entry(ren1->dst_entry, one, a, b);2471setup_rename_conflict_info(RENAME_NORMAL,2472 ren1->pair,2473 NULL,2474 branch1,2475 NULL,2476 ren1->dst_entry,2477 NULL,2478 o,2479 NULL,2480 NULL);2481}2482}2483}2484cleanup_and_return:2485string_list_clear(&a_by_dst,0);2486string_list_clear(&b_by_dst,0);24872488return clean_merge;2489}24902491struct rename_info {2492struct string_list *head_renames;2493struct string_list *merge_renames;2494};24952496static voidinitial_cleanup_rename(struct diff_queue_struct *pairs,2497struct hashmap *dir_renames)2498{2499struct hashmap_iter iter;2500struct dir_rename_entry *e;25012502hashmap_iter_init(dir_renames, &iter);2503while((e =hashmap_iter_next(&iter))) {2504free(e->dir);2505strbuf_release(&e->new_dir);2506/* possible_new_dirs already cleared in get_directory_renames */2507}2508hashmap_free(dir_renames,1);2509free(dir_renames);25102511free(pairs->queue);2512free(pairs);2513}25142515static inthandle_renames(struct merge_options *o,2516struct tree *common,2517struct tree *head,2518struct tree *merge,2519struct string_list *entries,2520struct rename_info *ri)2521{2522struct diff_queue_struct *head_pairs, *merge_pairs;2523struct hashmap *dir_re_head, *dir_re_merge;2524int clean =1;25252526 ri->head_renames = NULL;2527 ri->merge_renames = NULL;25282529if(!o->detect_rename)2530return1;25312532 head_pairs =get_diffpairs(o, common, head);2533 merge_pairs =get_diffpairs(o, common, merge);25342535 dir_re_head =get_directory_renames(head_pairs, head);2536 dir_re_merge =get_directory_renames(merge_pairs, merge);25372538handle_directory_level_conflicts(o,2539 dir_re_head, head,2540 dir_re_merge, merge);25412542 ri->head_renames =get_renames(o, head_pairs,2543 dir_re_merge, dir_re_head, head,2544 common, head, merge, entries,2545&clean);2546if(clean <0)2547goto cleanup;2548 ri->merge_renames =get_renames(o, merge_pairs,2549 dir_re_head, dir_re_merge, merge,2550 common, head, merge, entries,2551&clean);2552if(clean <0)2553goto cleanup;2554 clean &=process_renames(o, ri->head_renames, ri->merge_renames);25552556cleanup:2557/*2558 * Some cleanup is deferred until cleanup_renames() because the2559 * data structures are still needed and referenced in2560 * process_entry(). But there are a few things we can free now.2561 */2562initial_cleanup_rename(head_pairs, dir_re_head);2563initial_cleanup_rename(merge_pairs, dir_re_merge);25642565return clean;2566}25672568static voidfinal_cleanup_rename(struct string_list *rename)2569{2570const struct rename *re;2571int i;25722573if(rename == NULL)2574return;25752576for(i =0; i < rename->nr; i++) {2577 re = rename->items[i].util;2578diff_free_filepair(re->pair);2579}2580string_list_clear(rename,1);2581free(rename);2582}25832584static voidfinal_cleanup_renames(struct rename_info *re_info)2585{2586final_cleanup_rename(re_info->head_renames);2587final_cleanup_rename(re_info->merge_renames);2588}25892590static struct object_id *stage_oid(const struct object_id *oid,unsigned mode)2591{2592return(is_null_oid(oid) || mode ==0) ? NULL: (struct object_id *)oid;2593}25942595static intread_oid_strbuf(struct merge_options *o,2596const struct object_id *oid,struct strbuf *dst)2597{2598void*buf;2599enum object_type type;2600unsigned long size;2601 buf =read_sha1_file(oid->hash, &type, &size);2602if(!buf)2603returnerr(o,_("cannot read object%s"),oid_to_hex(oid));2604if(type != OBJ_BLOB) {2605free(buf);2606returnerr(o,_("object%sis not a blob"),oid_to_hex(oid));2607}2608strbuf_attach(dst, buf, size, size +1);2609return0;2610}26112612static intblob_unchanged(struct merge_options *opt,2613const struct object_id *o_oid,2614unsigned o_mode,2615const struct object_id *a_oid,2616unsigned a_mode,2617int renormalize,const char*path)2618{2619struct strbuf o = STRBUF_INIT;2620struct strbuf a = STRBUF_INIT;2621int ret =0;/* assume changed for safety */26222623if(a_mode != o_mode)2624return0;2625if(oid_eq(o_oid, a_oid))2626return1;2627if(!renormalize)2628return0;26292630assert(o_oid && a_oid);2631if(read_oid_strbuf(opt, o_oid, &o) ||read_oid_strbuf(opt, a_oid, &a))2632goto error_return;2633/*2634 * Note: binary | is used so that both renormalizations are2635 * performed. Comparison can be skipped if both files are2636 * unchanged since their sha1s have already been compared.2637 */2638if(renormalize_buffer(&the_index, path, o.buf, o.len, &o) |2639renormalize_buffer(&the_index, path, a.buf, a.len, &a))2640 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));26412642error_return:2643strbuf_release(&o);2644strbuf_release(&a);2645return ret;2646}26472648static inthandle_modify_delete(struct merge_options *o,2649const char*path,2650struct object_id *o_oid,int o_mode,2651struct object_id *a_oid,int a_mode,2652struct object_id *b_oid,int b_mode)2653{2654const char*modify_branch, *delete_branch;2655struct object_id *changed_oid;2656int changed_mode;26572658if(a_oid) {2659 modify_branch = o->branch1;2660 delete_branch = o->branch2;2661 changed_oid = a_oid;2662 changed_mode = a_mode;2663}else{2664 modify_branch = o->branch2;2665 delete_branch = o->branch1;2666 changed_oid = b_oid;2667 changed_mode = b_mode;2668}26692670returnhandle_change_delete(o,2671 path, NULL,2672 o_oid, o_mode,2673 changed_oid, changed_mode,2674 modify_branch, delete_branch,2675_("modify"),_("modified"));2676}26772678static intmerge_content(struct merge_options *o,2679const char*path,2680struct object_id *o_oid,int o_mode,2681struct object_id *a_oid,int a_mode,2682struct object_id *b_oid,int b_mode,2683struct rename_conflict_info *rename_conflict_info)2684{2685const char*reason =_("content");2686const char*path1 = NULL, *path2 = NULL;2687struct merge_file_info mfi;2688struct diff_filespec one, a, b;2689unsigned df_conflict_remains =0;26902691if(!o_oid) {2692 reason =_("add/add");2693 o_oid = (struct object_id *)&null_oid;2694}2695 one.path = a.path = b.path = (char*)path;2696oidcpy(&one.oid, o_oid);2697 one.mode = o_mode;2698oidcpy(&a.oid, a_oid);2699 a.mode = a_mode;2700oidcpy(&b.oid, b_oid);2701 b.mode = b_mode;27022703if(rename_conflict_info) {2704struct diff_filepair *pair1 = rename_conflict_info->pair1;27052706 path1 = (o->branch1 == rename_conflict_info->branch1) ?2707 pair1->two->path : pair1->one->path;2708/* If rename_conflict_info->pair2 != NULL, we are in2709 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a2710 * normal rename.2711 */2712 path2 = (rename_conflict_info->pair2 ||2713 o->branch2 == rename_conflict_info->branch1) ?2714 pair1->two->path : pair1->one->path;27152716if(dir_in_way(path, !o->call_depth,2717S_ISGITLINK(pair1->two->mode)))2718 df_conflict_remains =1;2719}2720if(merge_file_special_markers(o, &one, &a, &b,2721 o->branch1, path1,2722 o->branch2, path2, &mfi))2723return-1;27242725if(mfi.clean && !df_conflict_remains &&2726oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {2727int path_renamed_outside_HEAD;2728output(o,3,_("Skipped%s(merged same as existing)"), path);2729/*2730 * The content merge resulted in the same file contents we2731 * already had. We can return early if those file contents2732 * are recorded at the correct path (which may not be true2733 * if the merge involves a rename).2734 */2735 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);2736if(!path_renamed_outside_HEAD) {2737add_cacheinfo(o, mfi.mode, &mfi.oid, path,27380, (!o->call_depth),0);2739return mfi.clean;2740}2741}else2742output(o,2,_("Auto-merging%s"), path);27432744if(!mfi.clean) {2745if(S_ISGITLINK(mfi.mode))2746 reason =_("submodule");2747output(o,1,_("CONFLICT (%s): Merge conflict in%s"),2748 reason, path);2749if(rename_conflict_info && !df_conflict_remains)2750if(update_stages(o, path, &one, &a, &b))2751return-1;2752}27532754if(df_conflict_remains) {2755char*new_path;2756if(o->call_depth) {2757remove_file_from_cache(path);2758}else{2759if(!mfi.clean) {2760if(update_stages(o, path, &one, &a, &b))2761return-1;2762}else{2763int file_from_stage2 =was_tracked(path);2764struct diff_filespec merged;2765oidcpy(&merged.oid, &mfi.oid);2766 merged.mode = mfi.mode;27672768if(update_stages(o, path, NULL,2769 file_from_stage2 ? &merged : NULL,2770 file_from_stage2 ? NULL : &merged))2771return-1;2772}27732774}2775 new_path =unique_path(o, path, rename_conflict_info->branch1);2776output(o,1,_("Adding as%sinstead"), new_path);2777if(update_file(o,0, &mfi.oid, mfi.mode, new_path)) {2778free(new_path);2779return-1;2780}2781free(new_path);2782 mfi.clean =0;2783}else if(update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))2784return-1;2785return mfi.clean;2786}27872788/* Per entry merge function */2789static intprocess_entry(struct merge_options *o,2790const char*path,struct stage_data *entry)2791{2792int clean_merge =1;2793int normalize = o->renormalize;2794unsigned o_mode = entry->stages[1].mode;2795unsigned a_mode = entry->stages[2].mode;2796unsigned b_mode = entry->stages[3].mode;2797struct object_id *o_oid =stage_oid(&entry->stages[1].oid, o_mode);2798struct object_id *a_oid =stage_oid(&entry->stages[2].oid, a_mode);2799struct object_id *b_oid =stage_oid(&entry->stages[3].oid, b_mode);28002801 entry->processed =1;2802if(entry->rename_conflict_info) {2803struct rename_conflict_info *conflict_info = entry->rename_conflict_info;2804switch(conflict_info->rename_type) {2805case RENAME_NORMAL:2806case RENAME_ONE_FILE_TO_ONE:2807 clean_merge =merge_content(o, path,2808 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,2809 conflict_info);2810break;2811case RENAME_DIR:2812 clean_merge =1;2813if(conflict_rename_dir(o,2814 conflict_info->pair1,2815 conflict_info->branch1,2816 conflict_info->branch2))2817 clean_merge = -1;2818break;2819case RENAME_DELETE:2820 clean_merge =0;2821if(conflict_rename_delete(o,2822 conflict_info->pair1,2823 conflict_info->branch1,2824 conflict_info->branch2))2825 clean_merge = -1;2826break;2827case RENAME_ONE_FILE_TO_TWO:2828 clean_merge =0;2829if(conflict_rename_rename_1to2(o, conflict_info))2830 clean_merge = -1;2831break;2832case RENAME_TWO_FILES_TO_ONE:2833 clean_merge =0;2834if(conflict_rename_rename_2to1(o, conflict_info))2835 clean_merge = -1;2836break;2837default:2838 entry->processed =0;2839break;2840}2841}else if(o_oid && (!a_oid || !b_oid)) {2842/* Case A: Deleted in one */2843if((!a_oid && !b_oid) ||2844(!b_oid &&blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||2845(!a_oid &&blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {2846/* Deleted in both or deleted in one and2847 * unchanged in the other */2848if(a_oid)2849output(o,2,_("Removing%s"), path);2850/* do not touch working file if it did not exist */2851remove_file(o,1, path, !a_oid);2852}else{2853/* Modify/delete; deleted side may have put a directory in the way */2854 clean_merge =0;2855if(handle_modify_delete(o, path, o_oid, o_mode,2856 a_oid, a_mode, b_oid, b_mode))2857 clean_merge = -1;2858}2859}else if((!o_oid && a_oid && !b_oid) ||2860(!o_oid && !a_oid && b_oid)) {2861/* Case B: Added in one. */2862/* [nothing|directory] -> ([nothing|directory], file) */28632864const char*add_branch;2865const char*other_branch;2866unsigned mode;2867const struct object_id *oid;2868const char*conf;28692870if(a_oid) {2871 add_branch = o->branch1;2872 other_branch = o->branch2;2873 mode = a_mode;2874 oid = a_oid;2875 conf =_("file/directory");2876}else{2877 add_branch = o->branch2;2878 other_branch = o->branch1;2879 mode = b_mode;2880 oid = b_oid;2881 conf =_("directory/file");2882}2883if(dir_in_way(path,2884!o->call_depth && !S_ISGITLINK(a_mode),28850)) {2886char*new_path =unique_path(o, path, add_branch);2887 clean_merge =0;2888output(o,1,_("CONFLICT (%s): There is a directory with name%sin%s. "2889"Adding%sas%s"),2890 conf, path, other_branch, path, new_path);2891if(update_file(o,0, oid, mode, new_path))2892 clean_merge = -1;2893else if(o->call_depth)2894remove_file_from_cache(path);2895free(new_path);2896}else{2897output(o,2,_("Adding%s"), path);2898/* do not overwrite file if already present */2899if(update_file_flags(o, oid, mode, path,1, !a_oid))2900 clean_merge = -1;2901}2902}else if(a_oid && b_oid) {2903/* Case C: Added in both (check for same permissions) and */2904/* case D: Modified in both, but differently. */2905 clean_merge =merge_content(o, path,2906 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,2907 NULL);2908}else if(!o_oid && !a_oid && !b_oid) {2909/*2910 * this entry was deleted altogether. a_mode == 0 means2911 * we had that path and want to actively remove it.2912 */2913remove_file(o,1, path, !a_mode);2914}else2915die("BUG: fatal merge failure, shouldn't happen.");29162917return clean_merge;2918}29192920intmerge_trees(struct merge_options *o,2921struct tree *head,2922struct tree *merge,2923struct tree *common,2924struct tree **result)2925{2926int code, clean;29272928if(o->subtree_shift) {2929 merge =shift_tree_object(head, merge, o->subtree_shift);2930 common =shift_tree_object(head, common, o->subtree_shift);2931}29322933if(oid_eq(&common->object.oid, &merge->object.oid)) {2934struct strbuf sb = STRBUF_INIT;29352936if(!o->call_depth &&index_has_changes(&sb)) {2937err(o,_("Dirty index: cannot merge (dirty:%s)"),2938 sb.buf);2939return0;2940}2941output(o,0,_("Already up to date!"));2942*result = head;2943return1;2944}29452946 code =git_merge_trees(o->call_depth, common, head, merge);29472948if(code !=0) {2949if(show(o,4) || o->call_depth)2950err(o,_("merging of trees%sand%sfailed"),2951oid_to_hex(&head->object.oid),2952oid_to_hex(&merge->object.oid));2953return-1;2954}29552956if(unmerged_cache()) {2957struct string_list *entries;2958struct rename_info re_info;2959int i;2960/*2961 * Only need the hashmap while processing entries, so2962 * initialize it here and free it when we are done running2963 * through the entries. Keeping it in the merge_options as2964 * opposed to decaring a local hashmap is for convenience2965 * so that we don't have to pass it to around.2966 */2967hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL,512);2968get_files_dirs(o, head);2969get_files_dirs(o, merge);29702971 entries =get_unmerged();2972 clean =handle_renames(o, common, head, merge, entries,2973&re_info);2974record_df_conflict_files(o, entries);2975if(clean <0)2976goto cleanup;2977for(i = entries->nr-1;0<= i; i--) {2978const char*path = entries->items[i].string;2979struct stage_data *e = entries->items[i].util;2980if(!e->processed) {2981int ret =process_entry(o, path, e);2982if(!ret)2983 clean =0;2984else if(ret <0) {2985 clean = ret;2986goto cleanup;2987}2988}2989}2990for(i =0; i < entries->nr; i++) {2991struct stage_data *e = entries->items[i].util;2992if(!e->processed)2993die("BUG: unprocessed path???%s",2994 entries->items[i].string);2995}29962997cleanup:2998final_cleanup_renames(&re_info);29993000string_list_clear(entries,1);3001free(entries);30023003hashmap_free(&o->current_file_dir_set,1);30043005if(clean <0)3006return clean;3007}3008else3009 clean =1;30103011if(o->call_depth && !(*result =write_tree_from_memory(o)))3012return-1;30133014return clean;3015}30163017static struct commit_list *reverse_commit_list(struct commit_list *list)3018{3019struct commit_list *next = NULL, *current, *backup;3020for(current = list; current; current = backup) {3021 backup = current->next;3022 current->next = next;3023 next = current;3024}3025return next;3026}30273028/*3029 * Merge the commits h1 and h2, return the resulting virtual3030 * commit object and a flag indicating the cleanness of the merge.3031 */3032intmerge_recursive(struct merge_options *o,3033struct commit *h1,3034struct commit *h2,3035struct commit_list *ca,3036struct commit **result)3037{3038struct commit_list *iter;3039struct commit *merged_common_ancestors;3040struct tree *mrtree = mrtree;3041int clean;30423043if(show(o,4)) {3044output(o,4,_("Merging:"));3045output_commit_title(o, h1);3046output_commit_title(o, h2);3047}30483049if(!ca) {3050 ca =get_merge_bases(h1, h2);3051 ca =reverse_commit_list(ca);3052}30533054if(show(o,5)) {3055unsigned cnt =commit_list_count(ca);30563057output(o,5,Q_("found%ucommon ancestor:",3058"found%ucommon ancestors:", cnt), cnt);3059for(iter = ca; iter; iter = iter->next)3060output_commit_title(o, iter->item);3061}30623063 merged_common_ancestors =pop_commit(&ca);3064if(merged_common_ancestors == NULL) {3065/* if there is no common ancestor, use an empty tree */3066struct tree *tree;30673068 tree =lookup_tree(the_hash_algo->empty_tree);3069 merged_common_ancestors =make_virtual_commit(tree,"ancestor");3070}30713072for(iter = ca; iter; iter = iter->next) {3073const char*saved_b1, *saved_b2;3074 o->call_depth++;3075/*3076 * When the merge fails, the result contains files3077 * with conflict markers. The cleanness flag is3078 * ignored (unless indicating an error), it was never3079 * actually used, as result of merge_trees has always3080 * overwritten it: the committed "conflicts" were3081 * already resolved.3082 */3083discard_cache();3084 saved_b1 = o->branch1;3085 saved_b2 = o->branch2;3086 o->branch1 ="Temporary merge branch 1";3087 o->branch2 ="Temporary merge branch 2";3088if(merge_recursive(o, merged_common_ancestors, iter->item,3089 NULL, &merged_common_ancestors) <0)3090return-1;3091 o->branch1 = saved_b1;3092 o->branch2 = saved_b2;3093 o->call_depth--;30943095if(!merged_common_ancestors)3096returnerr(o,_("merge returned no commit"));3097}30983099discard_cache();3100if(!o->call_depth)3101read_cache();31023103 o->ancestor ="merged common ancestors";3104 clean =merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,3105&mrtree);3106if(clean <0) {3107flush_output(o);3108return clean;3109}31103111if(o->call_depth) {3112*result =make_virtual_commit(mrtree,"merged tree");3113commit_list_insert(h1, &(*result)->parents);3114commit_list_insert(h2, &(*result)->parents->next);3115}3116flush_output(o);3117if(!o->call_depth && o->buffer_output <2)3118strbuf_release(&o->obuf);3119if(show(o,2))3120diff_warn_rename_limit("merge.renamelimit",3121 o->needed_rename_limit,0);3122return clean;3123}31243125static struct commit *get_ref(const struct object_id *oid,const char*name)3126{3127struct object *object;31283129 object =deref_tag(parse_object(oid), name,strlen(name));3130if(!object)3131return NULL;3132if(object->type == OBJ_TREE)3133returnmake_virtual_commit((struct tree*)object, name);3134if(object->type != OBJ_COMMIT)3135return NULL;3136if(parse_commit((struct commit *)object))3137return NULL;3138return(struct commit *)object;3139}31403141intmerge_recursive_generic(struct merge_options *o,3142const struct object_id *head,3143const struct object_id *merge,3144int num_base_list,3145const struct object_id **base_list,3146struct commit **result)3147{3148int clean;3149struct lock_file lock = LOCK_INIT;3150struct commit *head_commit =get_ref(head, o->branch1);3151struct commit *next_commit =get_ref(merge, o->branch2);3152struct commit_list *ca = NULL;31533154if(base_list) {3155int i;3156for(i =0; i < num_base_list; ++i) {3157struct commit *base;3158if(!(base =get_ref(base_list[i],oid_to_hex(base_list[i]))))3159returnerr(o,_("Could not parse object '%s'"),3160oid_to_hex(base_list[i]));3161commit_list_insert(base, &ca);3162}3163}31643165hold_locked_index(&lock, LOCK_DIE_ON_ERROR);3166 clean =merge_recursive(o, head_commit, next_commit, ca,3167 result);3168if(clean <0)3169return clean;31703171if(active_cache_changed &&3172write_locked_index(&the_index, &lock, COMMIT_LOCK))3173returnerr(o,_("Unable to write index."));31743175return clean ?0:1;3176}31773178static voidmerge_recursive_config(struct merge_options *o)3179{3180git_config_get_int("merge.verbosity", &o->verbosity);3181git_config_get_int("diff.renamelimit", &o->diff_rename_limit);3182git_config_get_int("merge.renamelimit", &o->merge_rename_limit);3183git_config(git_xmerge_config, NULL);3184}31853186voidinit_merge_options(struct merge_options *o)3187{3188const char*merge_verbosity;3189memset(o,0,sizeof(struct merge_options));3190 o->verbosity =2;3191 o->buffer_output =1;3192 o->diff_rename_limit = -1;3193 o->merge_rename_limit = -1;3194 o->renormalize =0;3195 o->detect_rename =1;3196merge_recursive_config(o);3197 merge_verbosity =getenv("GIT_MERGE_VERBOSITY");3198if(merge_verbosity)3199 o->verbosity =strtol(merge_verbosity, NULL,10);3200if(o->verbosity >=5)3201 o->buffer_output =0;3202strbuf_init(&o->obuf,0);3203string_list_init(&o->df_conflict_file_set,1);3204}32053206intparse_merge_opt(struct merge_options *o,const char*s)3207{3208const char*arg;32093210if(!s || !*s)3211return-1;3212if(!strcmp(s,"ours"))3213 o->recursive_variant = MERGE_RECURSIVE_OURS;3214else if(!strcmp(s,"theirs"))3215 o->recursive_variant = MERGE_RECURSIVE_THEIRS;3216else if(!strcmp(s,"subtree"))3217 o->subtree_shift ="";3218else if(skip_prefix(s,"subtree=", &arg))3219 o->subtree_shift = arg;3220else if(!strcmp(s,"patience"))3221 o->xdl_opts =DIFF_WITH_ALG(o, PATIENCE_DIFF);3222else if(!strcmp(s,"histogram"))3223 o->xdl_opts =DIFF_WITH_ALG(o, HISTOGRAM_DIFF);3224else if(skip_prefix(s,"diff-algorithm=", &arg)) {3225long value =parse_algorithm_value(arg);3226if(value <0)3227return-1;3228/* clear out previous settings */3229DIFF_XDL_CLR(o, NEED_MINIMAL);3230 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;3231 o->xdl_opts |= value;3232}3233else if(!strcmp(s,"ignore-space-change"))3234DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);3235else if(!strcmp(s,"ignore-all-space"))3236DIFF_XDL_SET(o, IGNORE_WHITESPACE);3237else if(!strcmp(s,"ignore-space-at-eol"))3238DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);3239else if(!strcmp(s,"ignore-cr-at-eol"))3240DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);3241else if(!strcmp(s,"renormalize"))3242 o->renormalize =1;3243else if(!strcmp(s,"no-renormalize"))3244 o->renormalize =0;3245else if(!strcmp(s,"no-renames"))3246 o->detect_rename =0;3247else if(!strcmp(s,"find-renames")) {3248 o->detect_rename =1;3249 o->rename_score =0;3250}3251else if(skip_prefix(s,"find-renames=", &arg) ||3252skip_prefix(s,"rename-threshold=", &arg)) {3253if((o->rename_score =parse_rename_score(&arg)) == -1|| *arg !=0)3254return-1;3255 o->detect_rename =1;3256}3257else3258return-1;3259return0;3260}