1#include"cache.h" 2#include"argv-array.h" 3#include"repository.h" 4#include"config.h" 5#include"dir.h" 6#include"tree.h" 7#include"tree-walk.h" 8#include"cache-tree.h" 9#include"unpack-trees.h" 10#include"progress.h" 11#include"refs.h" 12#include"attr.h" 13#include"split-index.h" 14#include"dir.h" 15#include"submodule.h" 16#include"submodule-config.h" 17#include"fsmonitor.h" 18#include"object-store.h" 19#include"fetch-object.h" 20 21/* 22 * Error messages expected by scripts out of plumbing commands such as 23 * read-tree. Non-scripted Porcelain is not required to use these messages 24 * and in fact are encouraged to reword them to better suit their particular 25 * situation better. See how "git checkout" and "git merge" replaces 26 * them using setup_unpack_trees_porcelain(), for example. 27 */ 28static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 29/* ERROR_WOULD_OVERWRITE */ 30"Entry '%s' would be overwritten by merge. Cannot merge.", 31 32/* ERROR_NOT_UPTODATE_FILE */ 33"Entry '%s' not uptodate. Cannot merge.", 34 35/* ERROR_NOT_UPTODATE_DIR */ 36"Updating '%s' would lose untracked files in it", 37 38/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 39"Untracked working tree file '%s' would be overwritten by merge.", 40 41/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 42"Untracked working tree file '%s' would be removed by merge.", 43 44/* ERROR_BIND_OVERLAP */ 45"Entry '%s' overlaps with '%s'. Cannot bind.", 46 47/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 48"Entry '%s' not uptodate. Cannot update sparse checkout.", 49 50/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 51"Working tree file '%s' would be overwritten by sparse checkout update.", 52 53/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 54"Working tree file '%s' would be removed by sparse checkout update.", 55 56/* ERROR_WOULD_LOSE_SUBMODULE */ 57"Submodule '%s' cannot checkout new HEAD.", 58}; 59 60#define ERRORMSG(o,type) \ 61 ( ((o) && (o)->msgs[(type)]) \ 62 ? ((o)->msgs[(type)]) \ 63 : (unpack_plumbing_errors[(type)]) ) 64 65static const char*super_prefixed(const char*path) 66{ 67/* 68 * It is necessary and sufficient to have two static buffers 69 * here, as the return value of this function is fed to 70 * error() using the unpack_*_errors[] templates we see above. 71 */ 72static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 73static int super_prefix_len = -1; 74static unsigned idx =ARRAY_SIZE(buf) -1; 75 76if(super_prefix_len <0) { 77const char*super_prefix =get_super_prefix(); 78if(!super_prefix) { 79 super_prefix_len =0; 80}else{ 81int i; 82for(i =0; i <ARRAY_SIZE(buf); i++) 83strbuf_addstr(&buf[i], super_prefix); 84 super_prefix_len = buf[0].len; 85} 86} 87 88if(!super_prefix_len) 89return path; 90 91if(++idx >=ARRAY_SIZE(buf)) 92 idx =0; 93 94strbuf_setlen(&buf[idx], super_prefix_len); 95strbuf_addstr(&buf[idx], path); 96 97return buf[idx].buf; 98} 99 100voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 101const char*cmd) 102{ 103int i; 104const char**msgs = opts->msgs; 105const char*msg; 106 107argv_array_init(&opts->msgs_to_free); 108 109if(!strcmp(cmd,"checkout")) 110 msg = advice_commit_before_merge 111?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 112"Please commit your changes or stash them before you switch branches.") 113:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 114else if(!strcmp(cmd,"merge")) 115 msg = advice_commit_before_merge 116?_("Your local changes to the following files would be overwritten by merge:\n%%s" 117"Please commit your changes or stash them before you merge.") 118:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 119else 120 msg = advice_commit_before_merge 121?_("Your local changes to the following files would be overwritten by%s:\n%%s" 122"Please commit your changes or stash them before you%s.") 123:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 124 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 125argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 126 127 msgs[ERROR_NOT_UPTODATE_DIR] = 128_("Updating the following directories would lose untracked files in them:\n%s"); 129 130if(!strcmp(cmd,"checkout")) 131 msg = advice_commit_before_merge 132?_("The following untracked working tree files would be removed by checkout:\n%%s" 133"Please move or remove them before you switch branches.") 134:_("The following untracked working tree files would be removed by checkout:\n%%s"); 135else if(!strcmp(cmd,"merge")) 136 msg = advice_commit_before_merge 137?_("The following untracked working tree files would be removed by merge:\n%%s" 138"Please move or remove them before you merge.") 139:_("The following untracked working tree files would be removed by merge:\n%%s"); 140else 141 msg = advice_commit_before_merge 142?_("The following untracked working tree files would be removed by%s:\n%%s" 143"Please move or remove them before you%s.") 144:_("The following untracked working tree files would be removed by%s:\n%%s"); 145 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = 146argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 147 148if(!strcmp(cmd,"checkout")) 149 msg = advice_commit_before_merge 150?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 151"Please move or remove them before you switch branches.") 152:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 153else if(!strcmp(cmd,"merge")) 154 msg = advice_commit_before_merge 155?_("The following untracked working tree files would be overwritten by merge:\n%%s" 156"Please move or remove them before you merge.") 157:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 158else 159 msg = advice_commit_before_merge 160?_("The following untracked working tree files would be overwritten by%s:\n%%s" 161"Please move or remove them before you%s.") 162:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 163 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = 164argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 165 166/* 167 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 168 * cannot easily display it as a list. 169 */ 170 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 171 172 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 173_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 174 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 175_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 176 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 177_("The following working tree files would be removed by sparse checkout update:\n%s"); 178 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 179_("Cannot update submodule:\n%s"); 180 181 opts->show_all_errors =1; 182/* rejected paths may not have a static buffer */ 183for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 184 opts->unpack_rejects[i].strdup_strings =1; 185} 186 187voidclear_unpack_trees_porcelain(struct unpack_trees_options *opts) 188{ 189argv_array_clear(&opts->msgs_to_free); 190memset(opts->msgs,0,sizeof(opts->msgs)); 191} 192 193static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 194unsigned int set,unsigned int clear) 195{ 196 clear |= CE_HASHED; 197 198if(set & CE_REMOVE) 199 set |= CE_WT_REMOVE; 200 201 ce->ce_flags = (ce->ce_flags & ~clear) | set; 202returnadd_index_entry(&o->result, ce, 203 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 204} 205 206static voidadd_entry(struct unpack_trees_options *o, 207const struct cache_entry *ce, 208unsigned int set,unsigned int clear) 209{ 210do_add_entry(o,dup_cache_entry(ce, &o->result), set, clear); 211} 212 213/* 214 * add error messages on path <path> 215 * corresponding to the type <e> with the message <msg> 216 * indicating if it should be display in porcelain or not 217 */ 218static intadd_rejected_path(struct unpack_trees_options *o, 219enum unpack_trees_error_types e, 220const char*path) 221{ 222if(!o->show_all_errors) 223returnerror(ERRORMSG(o, e),super_prefixed(path)); 224 225/* 226 * Otherwise, insert in a list for future display by 227 * display_error_msgs() 228 */ 229string_list_append(&o->unpack_rejects[e], path); 230return-1; 231} 232 233/* 234 * display all the error messages stored in a nice way 235 */ 236static voiddisplay_error_msgs(struct unpack_trees_options *o) 237{ 238int e, i; 239int something_displayed =0; 240for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 241struct string_list *rejects = &o->unpack_rejects[e]; 242if(rejects->nr >0) { 243struct strbuf path = STRBUF_INIT; 244 something_displayed =1; 245for(i =0; i < rejects->nr; i++) 246strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 247error(ERRORMSG(o, e),super_prefixed(path.buf)); 248strbuf_release(&path); 249} 250string_list_clear(rejects,0); 251} 252if(something_displayed) 253fprintf(stderr,_("Aborting\n")); 254} 255 256static intcheck_submodule_move_head(const struct cache_entry *ce, 257const char*old_id, 258const char*new_id, 259struct unpack_trees_options *o) 260{ 261unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 262const struct submodule *sub =submodule_from_ce(ce); 263 264if(!sub) 265return0; 266 267if(o->reset) 268 flags |= SUBMODULE_MOVE_HEAD_FORCE; 269 270if(submodule_move_head(ce->name, old_id, new_id, flags)) 271return o->gently ? -1: 272add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 273return0; 274} 275 276/* 277 * Preform the loading of the repository's gitmodules file. This function is 278 * used by 'check_update()' to perform loading of the gitmodules file in two 279 * differnt situations: 280 * (1) before removing entries from the working tree if the gitmodules file has 281 * been marked for removal. This situation is specified by 'state' == NULL. 282 * (2) before checking out entries to the working tree if the gitmodules file 283 * has been marked for update. This situation is specified by 'state' != NULL. 284 */ 285static voidload_gitmodules_file(struct index_state *index, 286struct checkout *state) 287{ 288int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 289 290if(pos >=0) { 291struct cache_entry *ce = index->cache[pos]; 292if(!state && ce->ce_flags & CE_WT_REMOVE) { 293repo_read_gitmodules(the_repository); 294}else if(state && (ce->ce_flags & CE_UPDATE)) { 295submodule_free(the_repository); 296checkout_entry(ce, state, NULL, NULL); 297repo_read_gitmodules(the_repository); 298} 299} 300} 301 302/* 303 * Unlink the last component and schedule the leading directories for 304 * removal, such that empty directories get removed. 305 */ 306static voidunlink_entry(const struct cache_entry *ce) 307{ 308const struct submodule *sub =submodule_from_ce(ce); 309if(sub) { 310/* state.force is set at the caller. */ 311submodule_move_head(ce->name,"HEAD", NULL, 312 SUBMODULE_MOVE_HEAD_FORCE); 313} 314if(!check_leading_path(ce->name,ce_namelen(ce))) 315return; 316if(remove_or_warn(ce->ce_mode, ce->name)) 317return; 318schedule_dir_for_removal(ce->name,ce_namelen(ce)); 319} 320 321static struct progress *get_progress(struct unpack_trees_options *o) 322{ 323unsigned cnt =0, total =0; 324struct index_state *index = &o->result; 325 326if(!o->update || !o->verbose_update) 327return NULL; 328 329for(; cnt < index->cache_nr; cnt++) { 330const struct cache_entry *ce = index->cache[cnt]; 331if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 332 total++; 333} 334 335returnstart_delayed_progress(_("Checking out files"), total); 336} 337 338static voidsetup_collided_checkout_detection(struct checkout *state, 339struct index_state *index) 340{ 341int i; 342 343 state->clone =1; 344for(i =0; i < index->cache_nr; i++) 345 index->cache[i]->ce_flags &= ~CE_MATCHED; 346} 347 348static voidreport_collided_checkout(struct index_state *index) 349{ 350struct string_list list = STRING_LIST_INIT_NODUP; 351int i; 352 353for(i =0; i < index->cache_nr; i++) { 354struct cache_entry *ce = index->cache[i]; 355 356if(!(ce->ce_flags & CE_MATCHED)) 357continue; 358 359string_list_append(&list, ce->name); 360 ce->ce_flags &= ~CE_MATCHED; 361} 362 363 list.cmp = fspathcmp; 364string_list_sort(&list); 365 366if(list.nr) { 367warning(_("the following paths have collided (e.g. case-sensitive paths\n" 368"on a case-insensitive filesystem) and only one from the same\n" 369"colliding group is in the working tree:\n")); 370 371for(i =0; i < list.nr; i++) 372fprintf(stderr," '%s'\n", list.items[i].string); 373} 374 375string_list_clear(&list,0); 376} 377 378static intcheck_updates(struct unpack_trees_options *o) 379{ 380unsigned cnt =0; 381int errs =0; 382struct progress *progress; 383struct index_state *index = &o->result; 384struct checkout state = CHECKOUT_INIT; 385int i; 386 387trace_performance_enter(); 388 state.force =1; 389 state.quiet =1; 390 state.refresh_cache =1; 391 state.istate = index; 392 393if(o->clone) 394setup_collided_checkout_detection(&state, index); 395 396 progress =get_progress(o); 397 398if(o->update) 399git_attr_set_direction(GIT_ATTR_CHECKOUT); 400 401if(should_update_submodules() && o->update && !o->dry_run) 402load_gitmodules_file(index, NULL); 403 404for(i =0; i < index->cache_nr; i++) { 405const struct cache_entry *ce = index->cache[i]; 406 407if(ce->ce_flags & CE_WT_REMOVE) { 408display_progress(progress, ++cnt); 409if(o->update && !o->dry_run) 410unlink_entry(ce); 411} 412} 413remove_marked_cache_entries(index); 414remove_scheduled_dirs(); 415 416if(should_update_submodules() && o->update && !o->dry_run) 417load_gitmodules_file(index, &state); 418 419enable_delayed_checkout(&state); 420if(repository_format_partial_clone && o->update && !o->dry_run) { 421/* 422 * Prefetch the objects that are to be checked out in the loop 423 * below. 424 */ 425struct oid_array to_fetch = OID_ARRAY_INIT; 426int fetch_if_missing_store = fetch_if_missing; 427 fetch_if_missing =0; 428for(i =0; i < index->cache_nr; i++) { 429struct cache_entry *ce = index->cache[i]; 430if((ce->ce_flags & CE_UPDATE) && 431!S_ISGITLINK(ce->ce_mode)) { 432if(!has_object_file(&ce->oid)) 433oid_array_append(&to_fetch, &ce->oid); 434} 435} 436if(to_fetch.nr) 437fetch_objects(repository_format_partial_clone, 438 to_fetch.oid, to_fetch.nr); 439 fetch_if_missing = fetch_if_missing_store; 440oid_array_clear(&to_fetch); 441} 442for(i =0; i < index->cache_nr; i++) { 443struct cache_entry *ce = index->cache[i]; 444 445if(ce->ce_flags & CE_UPDATE) { 446if(ce->ce_flags & CE_WT_REMOVE) 447BUG("both update and delete flags are set on%s", 448 ce->name); 449display_progress(progress, ++cnt); 450 ce->ce_flags &= ~CE_UPDATE; 451if(o->update && !o->dry_run) { 452 errs |=checkout_entry(ce, &state, NULL, NULL); 453} 454} 455} 456stop_progress(&progress); 457 errs |=finish_delayed_checkout(&state, NULL); 458if(o->update) 459git_attr_set_direction(GIT_ATTR_CHECKIN); 460 461if(o->clone) 462report_collided_checkout(index); 463 464trace_performance_leave("check_updates"); 465return errs !=0; 466} 467 468static intverify_uptodate_sparse(const struct cache_entry *ce, 469struct unpack_trees_options *o); 470static intverify_absent_sparse(const struct cache_entry *ce, 471enum unpack_trees_error_types, 472struct unpack_trees_options *o); 473 474static intapply_sparse_checkout(struct index_state *istate, 475struct cache_entry *ce, 476struct unpack_trees_options *o) 477{ 478int was_skip_worktree =ce_skip_worktree(ce); 479 480if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 481 ce->ce_flags |= CE_SKIP_WORKTREE; 482else 483 ce->ce_flags &= ~CE_SKIP_WORKTREE; 484if(was_skip_worktree !=ce_skip_worktree(ce)) { 485 ce->ce_flags |= CE_UPDATE_IN_BASE; 486mark_fsmonitor_invalid(istate, ce); 487 istate->cache_changed |= CE_ENTRY_CHANGED; 488} 489 490/* 491 * if (!was_skip_worktree && !ce_skip_worktree()) { 492 * This is perfectly normal. Move on; 493 * } 494 */ 495 496/* 497 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 498 * area as a result of ce_skip_worktree() shortcuts in 499 * verify_absent() and verify_uptodate(). 500 * Make sure they don't modify worktree if they are already 501 * outside checkout area 502 */ 503if(was_skip_worktree &&ce_skip_worktree(ce)) { 504 ce->ce_flags &= ~CE_UPDATE; 505 506/* 507 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 508 * on to get that file removed from both index and worktree. 509 * If that file is already outside worktree area, don't 510 * bother remove it. 511 */ 512if(ce->ce_flags & CE_REMOVE) 513 ce->ce_flags &= ~CE_WT_REMOVE; 514} 515 516if(!was_skip_worktree &&ce_skip_worktree(ce)) { 517/* 518 * If CE_UPDATE is set, verify_uptodate() must be called already 519 * also stat info may have lost after merged_entry() so calling 520 * verify_uptodate() again may fail 521 */ 522if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 523return-1; 524 ce->ce_flags |= CE_WT_REMOVE; 525 ce->ce_flags &= ~CE_UPDATE; 526} 527if(was_skip_worktree && !ce_skip_worktree(ce)) { 528if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 529return-1; 530 ce->ce_flags |= CE_UPDATE; 531} 532return0; 533} 534 535staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 536struct unpack_trees_options *o) 537{ 538int ret = o->fn(src, o); 539if(ret >0) 540 ret =0; 541return ret; 542} 543 544static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 545{ 546 ce->ce_flags |= CE_UNPACKED; 547 548if(o->cache_bottom < o->src_index->cache_nr && 549 o->src_index->cache[o->cache_bottom] == ce) { 550int bottom = o->cache_bottom; 551while(bottom < o->src_index->cache_nr && 552 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 553 bottom++; 554 o->cache_bottom = bottom; 555} 556} 557 558static voidmark_all_ce_unused(struct index_state *index) 559{ 560int i; 561for(i =0; i < index->cache_nr; i++) 562 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 563} 564 565static intlocate_in_src_index(const struct cache_entry *ce, 566struct unpack_trees_options *o) 567{ 568struct index_state *index = o->src_index; 569int len =ce_namelen(ce); 570int pos =index_name_pos(index, ce->name, len); 571if(pos <0) 572 pos = -1- pos; 573return pos; 574} 575 576/* 577 * We call unpack_index_entry() with an unmerged cache entry 578 * only in diff-index, and it wants a single callback. Skip 579 * the other unmerged entry with the same name. 580 */ 581static voidmark_ce_used_same_name(struct cache_entry *ce, 582struct unpack_trees_options *o) 583{ 584struct index_state *index = o->src_index; 585int len =ce_namelen(ce); 586int pos; 587 588for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 589struct cache_entry *next = index->cache[pos]; 590if(len !=ce_namelen(next) || 591memcmp(ce->name, next->name, len)) 592break; 593mark_ce_used(next, o); 594} 595} 596 597static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 598{ 599const struct index_state *index = o->src_index; 600int pos = o->cache_bottom; 601 602while(pos < index->cache_nr) { 603struct cache_entry *ce = index->cache[pos]; 604if(!(ce->ce_flags & CE_UNPACKED)) 605return ce; 606 pos++; 607} 608return NULL; 609} 610 611static voidadd_same_unmerged(const struct cache_entry *ce, 612struct unpack_trees_options *o) 613{ 614struct index_state *index = o->src_index; 615int len =ce_namelen(ce); 616int pos =index_name_pos(index, ce->name, len); 617 618if(0<= pos) 619die("programming error in a caller of mark_ce_used_same_name"); 620for(pos = -pos -1; pos < index->cache_nr; pos++) { 621struct cache_entry *next = index->cache[pos]; 622if(len !=ce_namelen(next) || 623memcmp(ce->name, next->name, len)) 624break; 625add_entry(o, next,0,0); 626mark_ce_used(next, o); 627} 628} 629 630static intunpack_index_entry(struct cache_entry *ce, 631struct unpack_trees_options *o) 632{ 633const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 634int ret; 635 636 src[0] = ce; 637 638mark_ce_used(ce, o); 639if(ce_stage(ce)) { 640if(o->skip_unmerged) { 641add_entry(o, ce,0,0); 642return0; 643} 644} 645 ret =call_unpack_fn(src, o); 646if(ce_stage(ce)) 647mark_ce_used_same_name(ce, o); 648return ret; 649} 650 651static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 652 653static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 654{ 655struct unpack_trees_options *o = info->data; 656 657if(o->diff_index_cached) 658return; 659 o->cache_bottom = bottom; 660} 661 662static intswitch_cache_bottom(struct traverse_info *info) 663{ 664struct unpack_trees_options *o = info->data; 665int ret, pos; 666 667if(o->diff_index_cached) 668return0; 669 ret = o->cache_bottom; 670 pos =find_cache_pos(info->prev, &info->name); 671 672if(pos < -1) 673 o->cache_bottom = -2- pos; 674else if(pos <0) 675 o->cache_bottom = o->src_index->cache_nr; 676return ret; 677} 678 679staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 680{ 681return!is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) &&oideq(&name_j->oid, &name_k->oid); 682} 683 684static intall_trees_same_as_cache_tree(int n,unsigned long dirmask, 685struct name_entry *names, 686struct traverse_info *info) 687{ 688struct unpack_trees_options *o = info->data; 689int i; 690 691if(!o->merge || dirmask != ((1<< n) -1)) 692return0; 693 694for(i =1; i < n; i++) 695if(!are_same_oid(names, names + i)) 696return0; 697 698returncache_tree_matches_traversal(o->src_index->cache_tree, names, info); 699} 700 701static intindex_pos_by_traverse_info(struct name_entry *names, 702struct traverse_info *info) 703{ 704struct unpack_trees_options *o = info->data; 705int len =traverse_path_len(info, names); 706char*name =xmalloc(len +1/* slash */+1/* NUL */); 707int pos; 708 709make_traverse_path(name, info, names); 710 name[len++] ='/'; 711 name[len] ='\0'; 712 pos =index_name_pos(o->src_index, name, len); 713if(pos >=0) 714BUG("This is a directory and should not exist in index"); 715 pos = -pos -1; 716if(!starts_with(o->src_index->cache[pos]->name, name) || 717(pos >0&&starts_with(o->src_index->cache[pos-1]->name, name))) 718BUG("pos must point at the first entry in this directory"); 719free(name); 720return pos; 721} 722 723/* 724 * Fast path if we detect that all trees are the same as cache-tree at this 725 * path. We'll walk these trees in an iterative loop using cache-tree/index 726 * instead of ODB since we already know what these trees contain. 727 */ 728static inttraverse_by_cache_tree(int pos,int nr_entries,int nr_names, 729struct name_entry *names, 730struct traverse_info *info) 731{ 732struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 733struct unpack_trees_options *o = info->data; 734struct cache_entry *tree_ce = NULL; 735int ce_len =0; 736int i, d; 737 738if(!o->merge) 739BUG("We need cache-tree to do this optimization"); 740 741/* 742 * Do what unpack_callback() and unpack_nondirectories() normally 743 * do. But we walk all paths in an iterative loop instead. 744 * 745 * D/F conflicts and higher stage entries are not a concern 746 * because cache-tree would be invalidated and we would never 747 * get here in the first place. 748 */ 749for(i =0; i < nr_entries; i++) { 750int new_ce_len, len, rc; 751 752 src[0] = o->src_index->cache[pos + i]; 753 754 len =ce_namelen(src[0]); 755 new_ce_len =cache_entry_size(len); 756 757if(new_ce_len > ce_len) { 758 new_ce_len <<=1; 759 tree_ce =xrealloc(tree_ce, new_ce_len); 760memset(tree_ce,0, new_ce_len); 761 ce_len = new_ce_len; 762 763 tree_ce->ce_flags =create_ce_flags(0); 764 765for(d =1; d <= nr_names; d++) 766 src[d] = tree_ce; 767} 768 769 tree_ce->ce_mode = src[0]->ce_mode; 770 tree_ce->ce_namelen = len; 771oidcpy(&tree_ce->oid, &src[0]->oid); 772memcpy(tree_ce->name, src[0]->name, len +1); 773 774 rc =call_unpack_fn((const struct cache_entry *const*)src, o); 775if(rc <0) { 776free(tree_ce); 777return rc; 778} 779 780mark_ce_used(src[0], o); 781} 782free(tree_ce); 783if(o->debug_unpack) 784printf("Unpacked%dentries from%sto%susing cache-tree\n", 785 nr_entries, 786 o->src_index->cache[pos]->name, 787 o->src_index->cache[pos + nr_entries -1]->name); 788return0; 789} 790 791static inttraverse_trees_recursive(int n,unsigned long dirmask, 792unsigned long df_conflicts, 793struct name_entry *names, 794struct traverse_info *info) 795{ 796struct unpack_trees_options *o = info->data; 797int i, ret, bottom; 798int nr_buf =0; 799struct tree_desc t[MAX_UNPACK_TREES]; 800void*buf[MAX_UNPACK_TREES]; 801struct traverse_info newinfo; 802struct name_entry *p; 803int nr_entries; 804 805 nr_entries =all_trees_same_as_cache_tree(n, dirmask, names, info); 806if(nr_entries >0) { 807int pos =index_pos_by_traverse_info(names, info); 808 809if(!o->merge || df_conflicts) 810BUG("Wrong condition to get here buddy"); 811 812/* 813 * All entries up to 'pos' must have been processed 814 * (i.e. marked CE_UNPACKED) at this point. But to be safe, 815 * save and restore cache_bottom anyway to not miss 816 * unprocessed entries before 'pos'. 817 */ 818 bottom = o->cache_bottom; 819 ret =traverse_by_cache_tree(pos, nr_entries, n, names, info); 820 o->cache_bottom = bottom; 821return ret; 822} 823 824 p = names; 825while(!p->mode) 826 p++; 827 828 newinfo = *info; 829 newinfo.prev = info; 830 newinfo.pathspec = info->pathspec; 831 newinfo.name = *p; 832 newinfo.pathlen +=tree_entry_len(p) +1; 833 newinfo.df_conflicts |= df_conflicts; 834 835/* 836 * Fetch the tree from the ODB for each peer directory in the 837 * n commits. 838 * 839 * For 2- and 3-way traversals, we try to avoid hitting the 840 * ODB twice for the same OID. This should yield a nice speed 841 * up in checkouts and merges when the commits are similar. 842 * 843 * We don't bother doing the full O(n^2) search for larger n, 844 * because wider traversals don't happen that often and we 845 * avoid the search setup. 846 * 847 * When 2 peer OIDs are the same, we just copy the tree 848 * descriptor data. This implicitly borrows the buffer 849 * data from the earlier cell. 850 */ 851for(i =0; i < n; i++, dirmask >>=1) { 852if(i >0&&are_same_oid(&names[i], &names[i -1])) 853 t[i] = t[i -1]; 854else if(i >1&&are_same_oid(&names[i], &names[i -2])) 855 t[i] = t[i -2]; 856else{ 857const struct object_id *oid = NULL; 858if(dirmask &1) 859 oid = &names[i].oid; 860 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 861} 862} 863 864 bottom =switch_cache_bottom(&newinfo); 865 ret =traverse_trees(o->src_index, n, t, &newinfo); 866restore_cache_bottom(&newinfo, bottom); 867 868for(i =0; i < nr_buf; i++) 869free(buf[i]); 870 871return ret; 872} 873 874/* 875 * Compare the traverse-path to the cache entry without actually 876 * having to generate the textual representation of the traverse 877 * path. 878 * 879 * NOTE! This *only* compares up to the size of the traverse path 880 * itself - the caller needs to do the final check for the cache 881 * entry having more data at the end! 882 */ 883static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 884{ 885int len, pathlen, ce_len; 886const char*ce_name; 887 888if(info->prev) { 889int cmp =do_compare_entry_piecewise(ce, info->prev, 890&info->name); 891if(cmp) 892return cmp; 893} 894 pathlen = info->pathlen; 895 ce_len =ce_namelen(ce); 896 897/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 898if(ce_len < pathlen) 899return-1; 900 901 ce_len -= pathlen; 902 ce_name = ce->name + pathlen; 903 904 len =tree_entry_len(n); 905returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 906} 907 908static intdo_compare_entry(const struct cache_entry *ce, 909const struct traverse_info *info, 910const struct name_entry *n) 911{ 912int len, pathlen, ce_len; 913const char*ce_name; 914int cmp; 915 916/* 917 * If we have not precomputed the traverse path, it is quicker 918 * to avoid doing so. But if we have precomputed it, 919 * it is quicker to use the precomputed version. 920 */ 921if(!info->traverse_path) 922returndo_compare_entry_piecewise(ce, info, n); 923 924 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 925if(cmp) 926return cmp; 927 928 pathlen = info->pathlen; 929 ce_len =ce_namelen(ce); 930 931if(ce_len < pathlen) 932return-1; 933 934 ce_len -= pathlen; 935 ce_name = ce->name + pathlen; 936 937 len =tree_entry_len(n); 938returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 939} 940 941static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 942{ 943int cmp =do_compare_entry(ce, info, n); 944if(cmp) 945return cmp; 946 947/* 948 * Even if the beginning compared identically, the ce should 949 * compare as bigger than a directory leading up to it! 950 */ 951returnce_namelen(ce) >traverse_path_len(info, n); 952} 953 954static intce_in_traverse_path(const struct cache_entry *ce, 955const struct traverse_info *info) 956{ 957if(!info->prev) 958return1; 959if(do_compare_entry(ce, info->prev, &info->name)) 960return0; 961/* 962 * If ce (blob) is the same name as the path (which is a tree 963 * we will be descending into), it won't be inside it. 964 */ 965return(info->pathlen <ce_namelen(ce)); 966} 967 968static struct cache_entry *create_ce_entry(const struct traverse_info *info, 969const struct name_entry *n, 970int stage, 971struct index_state *istate, 972int is_transient) 973{ 974int len =traverse_path_len(info, n); 975struct cache_entry *ce = 976 is_transient ? 977make_empty_transient_cache_entry(len) : 978make_empty_cache_entry(istate, len); 979 980 ce->ce_mode =create_ce_mode(n->mode); 981 ce->ce_flags =create_ce_flags(stage); 982 ce->ce_namelen = len; 983oidcpy(&ce->oid, &n->oid); 984make_traverse_path(ce->name, info, n); 985 986return ce; 987} 988 989/* 990 * Note that traverse_by_cache_tree() duplicates some logic in this function 991 * without actually calling it. If you change the logic here you may need to 992 * check and change there as well. 993 */ 994static intunpack_nondirectories(int n,unsigned long mask, 995unsigned long dirmask, 996struct cache_entry **src, 997const struct name_entry *names, 998const struct traverse_info *info) 999{1000int i;1001struct unpack_trees_options *o = info->data;1002unsigned long conflicts = info->df_conflicts | dirmask;10031004/* Do we have *only* directories? Nothing to do */1005if(mask == dirmask && !src[0])1006return0;10071008/*1009 * Ok, we've filled in up to any potential index entry in src[0],1010 * now do the rest.1011 */1012for(i =0; i < n; i++) {1013int stage;1014unsigned int bit =1ul<< i;1015if(conflicts & bit) {1016 src[i + o->merge] = o->df_conflict_entry;1017continue;1018}1019if(!(mask & bit))1020continue;1021if(!o->merge)1022 stage =0;1023else if(i +1< o->head_idx)1024 stage =1;1025else if(i +1> o->head_idx)1026 stage =3;1027else1028 stage =2;10291030/*1031 * If the merge bit is set, then the cache entries are1032 * discarded in the following block. In this case,1033 * construct "transient" cache_entries, as they are1034 * not stored in the index. otherwise construct the1035 * cache entry from the index aware logic.1036 */1037 src[i + o->merge] =create_ce_entry(info, names + i, stage, &o->result, o->merge);1038}10391040if(o->merge) {1041int rc =call_unpack_fn((const struct cache_entry *const*)src,1042 o);1043for(i =0; i < n; i++) {1044struct cache_entry *ce = src[i + o->merge];1045if(ce != o->df_conflict_entry)1046discard_cache_entry(ce);1047}1048return rc;1049}10501051for(i =0; i < n; i++)1052if(src[i] && src[i] != o->df_conflict_entry)1053if(do_add_entry(o, src[i],0,0))1054return-1;10551056return0;1057}10581059static intunpack_failed(struct unpack_trees_options *o,const char*message)1060{1061discard_index(&o->result);1062if(!o->gently && !o->exiting_early) {1063if(message)1064returnerror("%s", message);1065return-1;1066}1067return-1;1068}10691070/*1071 * The tree traversal is looking at name p. If we have a matching entry,1072 * return it. If name p is a directory in the index, do not return1073 * anything, as we will want to match it when the traversal descends into1074 * the directory.1075 */1076static intfind_cache_pos(struct traverse_info *info,1077const struct name_entry *p)1078{1079int pos;1080struct unpack_trees_options *o = info->data;1081struct index_state *index = o->src_index;1082int pfxlen = info->pathlen;1083int p_len =tree_entry_len(p);10841085for(pos = o->cache_bottom; pos < index->cache_nr; pos++) {1086const struct cache_entry *ce = index->cache[pos];1087const char*ce_name, *ce_slash;1088int cmp, ce_len;10891090if(ce->ce_flags & CE_UNPACKED) {1091/*1092 * cache_bottom entry is already unpacked, so1093 * we can never match it; don't check it1094 * again.1095 */1096if(pos == o->cache_bottom)1097++o->cache_bottom;1098continue;1099}1100if(!ce_in_traverse_path(ce, info)) {1101/*1102 * Check if we can skip future cache checks1103 * (because we're already past all possible1104 * entries in the traverse path).1105 */1106if(info->traverse_path) {1107if(strncmp(ce->name, info->traverse_path,1108 info->pathlen) >0)1109break;1110}1111continue;1112}1113 ce_name = ce->name + pfxlen;1114 ce_slash =strchr(ce_name,'/');1115if(ce_slash)1116 ce_len = ce_slash - ce_name;1117else1118 ce_len =ce_namelen(ce) - pfxlen;1119 cmp =name_compare(p->path, p_len, ce_name, ce_len);1120/*1121 * Exact match; if we have a directory we need to1122 * delay returning it.1123 */1124if(!cmp)1125return ce_slash ? -2- pos : pos;1126if(0< cmp)1127continue;/* keep looking */1128/*1129 * ce_name sorts after p->path; could it be that we1130 * have files under p->path directory in the index?1131 * E.g. ce_name == "t-i", and p->path == "t"; we may1132 * have "t/a" in the index.1133 */1134if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&1135 ce_name[p_len] <'/')1136continue;/* keep looking */1137break;1138}1139return-1;1140}11411142static struct cache_entry *find_cache_entry(struct traverse_info *info,1143const struct name_entry *p)1144{1145int pos =find_cache_pos(info, p);1146struct unpack_trees_options *o = info->data;11471148if(0<= pos)1149return o->src_index->cache[pos];1150else1151return NULL;1152}11531154static voiddebug_path(struct traverse_info *info)1155{1156if(info->prev) {1157debug_path(info->prev);1158if(*info->prev->name.path)1159putchar('/');1160}1161printf("%s", info->name.path);1162}11631164static voiddebug_name_entry(int i,struct name_entry *n)1165{1166printf("ent#%d %06o%s\n", i,1167 n->path ? n->mode :0,1168 n->path ? n->path :"(missing)");1169}11701171static voiddebug_unpack_callback(int n,1172unsigned long mask,1173unsigned long dirmask,1174struct name_entry *names,1175struct traverse_info *info)1176{1177int i;1178printf("* unpack mask%lu, dirmask%lu, cnt%d",1179 mask, dirmask, n);1180debug_path(info);1181putchar('\n');1182for(i =0; i < n; i++)1183debug_name_entry(i, names + i);1184}11851186/*1187 * Note that traverse_by_cache_tree() duplicates some logic in this function1188 * without actually calling it. If you change the logic here you may need to1189 * check and change there as well.1190 */1191static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info)1192{1193struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1194struct unpack_trees_options *o = info->data;1195const struct name_entry *p = names;11961197/* Find first entry with a real name (we could use "mask" too) */1198while(!p->mode)1199 p++;12001201if(o->debug_unpack)1202debug_unpack_callback(n, mask, dirmask, names, info);12031204/* Are we supposed to look at the index too? */1205if(o->merge) {1206while(1) {1207int cmp;1208struct cache_entry *ce;12091210if(o->diff_index_cached)1211 ce =next_cache_entry(o);1212else1213 ce =find_cache_entry(info, p);12141215if(!ce)1216break;1217 cmp =compare_entry(ce, info, p);1218if(cmp <0) {1219if(unpack_index_entry(ce, o) <0)1220returnunpack_failed(o, NULL);1221continue;1222}1223if(!cmp) {1224if(ce_stage(ce)) {1225/*1226 * If we skip unmerged index1227 * entries, we'll skip this1228 * entry *and* the tree1229 * entries associated with it!1230 */1231if(o->skip_unmerged) {1232add_same_unmerged(ce, o);1233return mask;1234}1235}1236 src[0] = ce;1237}1238break;1239}1240}12411242if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1243return-1;12441245if(o->merge && src[0]) {1246if(ce_stage(src[0]))1247mark_ce_used_same_name(src[0], o);1248else1249mark_ce_used(src[0], o);1250}12511252/* Now handle any directories.. */1253if(dirmask) {1254/* special case: "diff-index --cached" looking at a tree */1255if(o->diff_index_cached &&1256 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1257int matches;1258 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1259 names, info);1260/*1261 * Everything under the name matches; skip the1262 * entire hierarchy. diff_index_cached codepath1263 * special cases D/F conflicts in such a way that1264 * it does not do any look-ahead, so this is safe.1265 */1266if(matches) {1267 o->cache_bottom += matches;1268return mask;1269}1270}12711272if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1273 names, info) <0)1274return-1;1275return mask;1276}12771278return mask;1279}12801281static intclear_ce_flags_1(struct index_state *istate,1282struct cache_entry **cache,int nr,1283struct strbuf *prefix,1284int select_mask,int clear_mask,1285struct exclude_list *el,int defval);12861287/* Whole directory matching */1288static intclear_ce_flags_dir(struct index_state *istate,1289struct cache_entry **cache,int nr,1290struct strbuf *prefix,1291char*basename,1292int select_mask,int clear_mask,1293struct exclude_list *el,int defval)1294{1295struct cache_entry **cache_end;1296int dtype = DT_DIR;1297int ret =is_excluded_from_list(prefix->buf, prefix->len,1298 basename, &dtype, el, istate);1299int rc;13001301strbuf_addch(prefix,'/');13021303/* If undecided, use matching result of parent dir in defval */1304if(ret <0)1305 ret = defval;13061307for(cache_end = cache; cache_end != cache + nr; cache_end++) {1308struct cache_entry *ce = *cache_end;1309if(strncmp(ce->name, prefix->buf, prefix->len))1310break;1311}13121313/*1314 * TODO: check el, if there are no patterns that may conflict1315 * with ret (iow, we know in advance the incl/excl1316 * decision for the entire directory), clear flag here without1317 * calling clear_ce_flags_1(). That function will call1318 * the expensive is_excluded_from_list() on every entry.1319 */1320 rc =clear_ce_flags_1(istate, cache, cache_end - cache,1321 prefix,1322 select_mask, clear_mask,1323 el, ret);1324strbuf_setlen(prefix, prefix->len -1);1325return rc;1326}13271328/*1329 * Traverse the index, find every entry that matches according to1330 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1331 * number of traversed entries.1332 *1333 * If select_mask is non-zero, only entries whose ce_flags has on of1334 * those bits enabled are traversed.1335 *1336 * cache : pointer to an index entry1337 * prefix_len : an offset to its path1338 *1339 * The current path ("prefix") including the trailing '/' is1340 * cache[0]->name[0..(prefix_len-1)]1341 * Top level path has prefix_len zero.1342 */1343static intclear_ce_flags_1(struct index_state *istate,1344struct cache_entry **cache,int nr,1345struct strbuf *prefix,1346int select_mask,int clear_mask,1347struct exclude_list *el,int defval)1348{1349struct cache_entry **cache_end = cache + nr;13501351/*1352 * Process all entries that have the given prefix and meet1353 * select_mask condition1354 */1355while(cache != cache_end) {1356struct cache_entry *ce = *cache;1357const char*name, *slash;1358int len, dtype, ret;13591360if(select_mask && !(ce->ce_flags & select_mask)) {1361 cache++;1362continue;1363}13641365if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1366break;13671368 name = ce->name + prefix->len;1369 slash =strchr(name,'/');13701371/* If it's a directory, try whole directory match first */1372if(slash) {1373int processed;13741375 len = slash - name;1376strbuf_add(prefix, name, len);13771378 processed =clear_ce_flags_dir(istate, cache, cache_end - cache,1379 prefix,1380 prefix->buf + prefix->len - len,1381 select_mask, clear_mask,1382 el, defval);13831384/* clear_c_f_dir eats a whole dir already? */1385if(processed) {1386 cache += processed;1387strbuf_setlen(prefix, prefix->len - len);1388continue;1389}13901391strbuf_addch(prefix,'/');1392 cache +=clear_ce_flags_1(istate, cache, cache_end - cache,1393 prefix,1394 select_mask, clear_mask, el, defval);1395strbuf_setlen(prefix, prefix->len - len -1);1396continue;1397}13981399/* Non-directory */1400 dtype =ce_to_dtype(ce);1401 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1402 name, &dtype, el, istate);1403if(ret <0)1404 ret = defval;1405if(ret >0)1406 ce->ce_flags &= ~clear_mask;1407 cache++;1408}1409return nr - (cache_end - cache);1410}14111412static intclear_ce_flags(struct index_state *istate,1413int select_mask,int clear_mask,1414struct exclude_list *el)1415{1416static struct strbuf prefix = STRBUF_INIT;14171418strbuf_reset(&prefix);14191420returnclear_ce_flags_1(istate,1421 istate->cache,1422 istate->cache_nr,1423&prefix,1424 select_mask, clear_mask,1425 el,0);1426}14271428/*1429 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1430 */1431static voidmark_new_skip_worktree(struct exclude_list *el,1432struct index_state *istate,1433int select_flag,int skip_wt_flag)1434{1435int i;14361437/*1438 * 1. Pretend the narrowest worktree: only unmerged entries1439 * are checked out1440 */1441for(i =0; i < istate->cache_nr; i++) {1442struct cache_entry *ce = istate->cache[i];14431444if(select_flag && !(ce->ce_flags & select_flag))1445continue;14461447if(!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))1448 ce->ce_flags |= skip_wt_flag;1449else1450 ce->ce_flags &= ~skip_wt_flag;1451}14521453/*1454 * 2. Widen worktree according to sparse-checkout file.1455 * Matched entries will have skip_wt_flag cleared (i.e. "in")1456 */1457clear_ce_flags(istate, select_flag, skip_wt_flag, el);1458}14591460static intverify_absent(const struct cache_entry *,1461enum unpack_trees_error_types,1462struct unpack_trees_options *);1463/*1464 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1465 * resulting index, -2 on failure to reflect the changes to the work tree.1466 *1467 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1468 */1469intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1470{1471int i, ret;1472static struct cache_entry *dfc;1473struct exclude_list el;14741475if(len > MAX_UNPACK_TREES)1476die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);14771478trace_performance_enter();1479memset(&el,0,sizeof(el));1480if(!core_apply_sparse_checkout || !o->update)1481 o->skip_sparse_checkout =1;1482if(!o->skip_sparse_checkout) {1483char*sparse =git_pathdup("info/sparse-checkout");1484if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1485 o->skip_sparse_checkout =1;1486else1487 o->el = ⪙1488free(sparse);1489}14901491memset(&o->result,0,sizeof(o->result));1492 o->result.initialized =1;1493 o->result.timestamp.sec = o->src_index->timestamp.sec;1494 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1495 o->result.version = o->src_index->version;1496if(!o->src_index->split_index) {1497 o->result.split_index = NULL;1498}else if(o->src_index == o->dst_index) {1499/*1500 * o->dst_index (and thus o->src_index) will be discarded1501 * and overwritten with o->result at the end of this function,1502 * so just use src_index's split_index to avoid having to1503 * create a new one.1504 */1505 o->result.split_index = o->src_index->split_index;1506 o->result.split_index->refcount++;1507}else{1508 o->result.split_index =init_split_index(&o->result);1509}1510oidcpy(&o->result.oid, &o->src_index->oid);1511 o->merge_size = len;1512mark_all_ce_unused(o->src_index);15131514/*1515 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1516 */1517if(!o->skip_sparse_checkout)1518mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);15191520if(!dfc)1521 dfc =xcalloc(1,cache_entry_size(0));1522 o->df_conflict_entry = dfc;15231524if(len) {1525const char*prefix = o->prefix ? o->prefix :"";1526struct traverse_info info;15271528setup_traverse_info(&info, prefix);1529 info.fn = unpack_callback;1530 info.data = o;1531 info.show_all_errors = o->show_all_errors;1532 info.pathspec = o->pathspec;15331534if(o->prefix) {1535/*1536 * Unpack existing index entries that sort before the1537 * prefix the tree is spliced into. Note that o->merge1538 * is always true in this case.1539 */1540while(1) {1541struct cache_entry *ce =next_cache_entry(o);1542if(!ce)1543break;1544if(ce_in_traverse_path(ce, &info))1545break;1546if(unpack_index_entry(ce, o) <0)1547goto return_failed;1548}1549}15501551trace_performance_enter();1552 ret =traverse_trees(o->src_index, len, t, &info);1553trace_performance_leave("traverse_trees");1554if(ret <0)1555goto return_failed;1556}15571558/* Any left-over entries in the index? */1559if(o->merge) {1560while(1) {1561struct cache_entry *ce =next_cache_entry(o);1562if(!ce)1563break;1564if(unpack_index_entry(ce, o) <0)1565goto return_failed;1566}1567}1568mark_all_ce_unused(o->src_index);15691570if(o->trivial_merges_only && o->nontrivial_merge) {1571 ret =unpack_failed(o,"Merge requires file-level merging");1572goto done;1573}15741575if(!o->skip_sparse_checkout) {1576int empty_worktree =1;15771578/*1579 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11580 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1581 * so apply_sparse_checkout() won't attempt to remove it from worktree1582 */1583mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);15841585 ret =0;1586for(i =0; i < o->result.cache_nr; i++) {1587struct cache_entry *ce = o->result.cache[i];15881589/*1590 * Entries marked with CE_ADDED in merged_entry() do not have1591 * verify_absent() check (the check is effectively disabled1592 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1593 *1594 * Do the real check now because we have had1595 * correct CE_NEW_SKIP_WORKTREE1596 */1597if(ce->ce_flags & CE_ADDED &&1598verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1599if(!o->show_all_errors)1600goto return_failed;1601 ret = -1;1602}16031604if(apply_sparse_checkout(&o->result, ce, o)) {1605if(!o->show_all_errors)1606goto return_failed;1607 ret = -1;1608}1609if(!ce_skip_worktree(ce))1610 empty_worktree =0;16111612}1613if(ret <0)1614goto return_failed;1615/*1616 * Sparse checkout is meant to narrow down checkout area1617 * but it does not make sense to narrow down to empty working1618 * tree. This is usually a mistake in sparse checkout rules.1619 * Do not allow users to do that.1620 */1621if(o->result.cache_nr && empty_worktree) {1622 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1623goto done;1624}1625}16261627 ret =check_updates(o) ? (-2) :0;1628if(o->dst_index) {1629move_index_extensions(&o->result, o->src_index);1630if(!ret) {1631if(git_env_bool("GIT_TEST_CHECK_CACHE_TREE",0))1632cache_tree_verify(the_repository, &o->result);1633if(!o->result.cache_tree)1634 o->result.cache_tree =cache_tree();1635if(!cache_tree_fully_valid(o->result.cache_tree))1636cache_tree_update(&o->result,1637 WRITE_TREE_SILENT |1638 WRITE_TREE_REPAIR);1639}1640discard_index(o->dst_index);1641*o->dst_index = o->result;1642}else{1643discard_index(&o->result);1644}1645 o->src_index = NULL;16461647done:1648trace_performance_leave("unpack_trees");1649clear_exclude_list(&el);1650return ret;16511652return_failed:1653if(o->show_all_errors)1654display_error_msgs(o);1655mark_all_ce_unused(o->src_index);1656 ret =unpack_failed(o, NULL);1657if(o->exiting_early)1658 ret =0;1659goto done;1660}16611662/* Here come the merge functions */16631664static intreject_merge(const struct cache_entry *ce,1665struct unpack_trees_options *o)1666{1667return o->gently ? -1:1668add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1669}16701671static intsame(const struct cache_entry *a,const struct cache_entry *b)1672{1673if(!!a != !!b)1674return0;1675if(!a && !b)1676return1;1677if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1678return0;1679return a->ce_mode == b->ce_mode &&1680oideq(&a->oid, &b->oid);1681}168216831684/*1685 * When a CE gets turned into an unmerged entry, we1686 * want it to be up-to-date1687 */1688static intverify_uptodate_1(const struct cache_entry *ce,1689struct unpack_trees_options *o,1690enum unpack_trees_error_types error_type)1691{1692struct stat st;16931694if(o->index_only)1695return0;16961697/*1698 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1699 * if this entry is truly up-to-date because this file may be1700 * overwritten.1701 */1702if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1703;/* keep checking */1704else if(o->reset ||ce_uptodate(ce))1705return0;17061707if(!lstat(ce->name, &st)) {1708int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1709unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);17101711if(submodule_from_ce(ce)) {1712int r =check_submodule_move_head(ce,1713"HEAD",oid_to_hex(&ce->oid), o);1714if(r)1715return o->gently ? -1:1716add_rejected_path(o, error_type, ce->name);1717return0;1718}17191720if(!changed)1721return0;1722/*1723 * Historic default policy was to allow submodule to be out1724 * of sync wrt the superproject index. If the submodule was1725 * not considered interesting above, we don't care here.1726 */1727if(S_ISGITLINK(ce->ce_mode))1728return0;17291730 errno =0;1731}1732if(errno == ENOENT)1733return0;1734return o->gently ? -1:1735add_rejected_path(o, error_type, ce->name);1736}17371738intverify_uptodate(const struct cache_entry *ce,1739struct unpack_trees_options *o)1740{1741if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1742return0;1743returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1744}17451746static intverify_uptodate_sparse(const struct cache_entry *ce,1747struct unpack_trees_options *o)1748{1749returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1750}17511752/*1753 * TODO: We should actually invalidate o->result, not src_index [1].1754 * But since cache tree and untracked cache both are not copied to1755 * o->result until unpacking is complete, we invalidate them on1756 * src_index instead with the assumption that they will be copied to1757 * dst_index at the end.1758 *1759 * [1] src_index->cache_tree is also used in unpack_callback() so if1760 * we invalidate o->result, we need to update it to use1761 * o->result.cache_tree as well.1762 */1763static voidinvalidate_ce_path(const struct cache_entry *ce,1764struct unpack_trees_options *o)1765{1766if(!ce)1767return;1768cache_tree_invalidate_path(o->src_index, ce->name);1769untracked_cache_invalidate_path(o->src_index, ce->name,1);1770}17711772/*1773 * Check that checking out ce->sha1 in subdir ce->name is not1774 * going to overwrite any working files.1775 *1776 * Currently, git does not checkout subprojects during a superproject1777 * checkout, so it is not going to overwrite anything.1778 */1779static intverify_clean_submodule(const char*old_sha1,1780const struct cache_entry *ce,1781enum unpack_trees_error_types error_type,1782struct unpack_trees_options *o)1783{1784if(!submodule_from_ce(ce))1785return0;17861787returncheck_submodule_move_head(ce, old_sha1,1788oid_to_hex(&ce->oid), o);1789}17901791static intverify_clean_subdirectory(const struct cache_entry *ce,1792enum unpack_trees_error_types error_type,1793struct unpack_trees_options *o)1794{1795/*1796 * we are about to extract "ce->name"; we would not want to lose1797 * anything in the existing directory there.1798 */1799int namelen;1800int i;1801struct dir_struct d;1802char*pathbuf;1803int cnt =0;18041805if(S_ISGITLINK(ce->ce_mode)) {1806struct object_id oid;1807int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1808/*1809 * If we are not going to update the submodule, then1810 * we don't care.1811 */1812if(!sub_head &&oideq(&oid, &ce->oid))1813return0;1814returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1815 ce, error_type, o);1816}18171818/*1819 * First let's make sure we do not have a local modification1820 * in that directory.1821 */1822 namelen =ce_namelen(ce);1823for(i =locate_in_src_index(ce, o);1824 i < o->src_index->cache_nr;1825 i++) {1826struct cache_entry *ce2 = o->src_index->cache[i];1827int len =ce_namelen(ce2);1828if(len < namelen ||1829strncmp(ce->name, ce2->name, namelen) ||1830 ce2->name[namelen] !='/')1831break;1832/*1833 * ce2->name is an entry in the subdirectory to be1834 * removed.1835 */1836if(!ce_stage(ce2)) {1837if(verify_uptodate(ce2, o))1838return-1;1839add_entry(o, ce2, CE_REMOVE,0);1840invalidate_ce_path(ce, o);1841mark_ce_used(ce2, o);1842}1843 cnt++;1844}18451846/*1847 * Then we need to make sure that we do not lose a locally1848 * present file that is not ignored.1849 */1850 pathbuf =xstrfmt("%.*s/", namelen, ce->name);18511852memset(&d,0,sizeof(d));1853if(o->dir)1854 d.exclude_per_dir = o->dir->exclude_per_dir;1855 i =read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);1856if(i)1857return o->gently ? -1:1858add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1859free(pathbuf);1860return cnt;1861}18621863/*1864 * This gets called when there was no index entry for the tree entry 'dst',1865 * but we found a file in the working tree that 'lstat()' said was fine,1866 * and we're on a case-insensitive filesystem.1867 *1868 * See if we can find a case-insensitive match in the index that also1869 * matches the stat information, and assume it's that other file!1870 */1871static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1872{1873const struct cache_entry *src;18741875 src =index_file_exists(o->src_index, name, len,1);1876return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1877}18781879static intcheck_ok_to_remove(const char*name,int len,int dtype,1880const struct cache_entry *ce,struct stat *st,1881enum unpack_trees_error_types error_type,1882struct unpack_trees_options *o)1883{1884const struct cache_entry *result;18851886/*1887 * It may be that the 'lstat()' succeeded even though1888 * target 'ce' was absent, because there is an old1889 * entry that is different only in case..1890 *1891 * Ignore that lstat() if it matches.1892 */1893if(ignore_case &&icase_exists(o, name, len, st))1894return0;18951896if(o->dir &&1897is_excluded(o->dir, o->src_index, name, &dtype))1898/*1899 * ce->name is explicitly excluded, so it is Ok to1900 * overwrite it.1901 */1902return0;1903if(S_ISDIR(st->st_mode)) {1904/*1905 * We are checking out path "foo" and1906 * found "foo/." in the working tree.1907 * This is tricky -- if we have modified1908 * files that are in "foo/" we would lose1909 * them.1910 */1911if(verify_clean_subdirectory(ce, error_type, o) <0)1912return-1;1913return0;1914}19151916/*1917 * The previous round may already have decided to1918 * delete this path, which is in a subdirectory that1919 * is being replaced with a blob.1920 */1921 result =index_file_exists(&o->result, name, len,0);1922if(result) {1923if(result->ce_flags & CE_REMOVE)1924return0;1925}19261927return o->gently ? -1:1928add_rejected_path(o, error_type, name);1929}19301931/*1932 * We do not want to remove or overwrite a working tree file that1933 * is not tracked, unless it is ignored.1934 */1935static intverify_absent_1(const struct cache_entry *ce,1936enum unpack_trees_error_types error_type,1937struct unpack_trees_options *o)1938{1939int len;1940struct stat st;19411942if(o->index_only || o->reset || !o->update)1943return0;19441945 len =check_leading_path(ce->name,ce_namelen(ce));1946if(!len)1947return0;1948else if(len >0) {1949char*path;1950int ret;19511952 path =xmemdupz(ce->name, len);1953if(lstat(path, &st))1954 ret =error_errno("cannot stat '%s'", path);1955else{1956if(submodule_from_ce(ce))1957 ret =check_submodule_move_head(ce,1958oid_to_hex(&ce->oid),1959 NULL, o);1960else1961 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1962&st, error_type, o);1963}1964free(path);1965return ret;1966}else if(lstat(ce->name, &st)) {1967if(errno != ENOENT)1968returnerror_errno("cannot stat '%s'", ce->name);1969return0;1970}else{1971if(submodule_from_ce(ce))1972returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1973 NULL, o);19741975returncheck_ok_to_remove(ce->name,ce_namelen(ce),1976ce_to_dtype(ce), ce, &st,1977 error_type, o);1978}1979}19801981static intverify_absent(const struct cache_entry *ce,1982enum unpack_trees_error_types error_type,1983struct unpack_trees_options *o)1984{1985if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1986return0;1987returnverify_absent_1(ce, error_type, o);1988}19891990static intverify_absent_sparse(const struct cache_entry *ce,1991enum unpack_trees_error_types error_type,1992struct unpack_trees_options *o)1993{1994enum unpack_trees_error_types orphaned_error = error_type;1995if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1996 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;19971998returnverify_absent_1(ce, orphaned_error, o);1999}20002001static intmerged_entry(const struct cache_entry *ce,2002const struct cache_entry *old,2003struct unpack_trees_options *o)2004{2005int update = CE_UPDATE;2006struct cache_entry *merge =dup_cache_entry(ce, &o->result);20072008if(!old) {2009/*2010 * New index entries. In sparse checkout, the following2011 * verify_absent() will be delayed until after2012 * traverse_trees() finishes in unpack_trees(), then:2013 *2014 * - CE_NEW_SKIP_WORKTREE will be computed correctly2015 * - verify_absent() be called again, this time with2016 * correct CE_NEW_SKIP_WORKTREE2017 *2018 * verify_absent() call here does nothing in sparse2019 * checkout (i.e. o->skip_sparse_checkout == 0)2020 */2021 update |= CE_ADDED;2022 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;20232024if(verify_absent(merge,2025 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {2026discard_cache_entry(merge);2027return-1;2028}2029invalidate_ce_path(merge, o);20302031if(submodule_from_ce(ce)) {2032int ret =check_submodule_move_head(ce, NULL,2033oid_to_hex(&ce->oid),2034 o);2035if(ret)2036return ret;2037}20382039}else if(!(old->ce_flags & CE_CONFLICTED)) {2040/*2041 * See if we can re-use the old CE directly?2042 * That way we get the uptodate stat info.2043 *2044 * This also removes the UPDATE flag on a match; otherwise2045 * we will end up overwriting local changes in the work tree.2046 */2047if(same(old, merge)) {2048copy_cache_entry(merge, old);2049 update =0;2050}else{2051if(verify_uptodate(old, o)) {2052discard_cache_entry(merge);2053return-1;2054}2055/* Migrate old flags over */2056 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);2057invalidate_ce_path(old, o);2058}20592060if(submodule_from_ce(ce)) {2061int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),2062oid_to_hex(&ce->oid),2063 o);2064if(ret)2065return ret;2066}2067}else{2068/*2069 * Previously unmerged entry left as an existence2070 * marker by read_index_unmerged();2071 */2072invalidate_ce_path(old, o);2073}20742075do_add_entry(o, merge, update, CE_STAGEMASK);2076return1;2077}20782079static intdeleted_entry(const struct cache_entry *ce,2080const struct cache_entry *old,2081struct unpack_trees_options *o)2082{2083/* Did it exist in the index? */2084if(!old) {2085if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2086return-1;2087return0;2088}2089if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))2090return-1;2091add_entry(o, ce, CE_REMOVE,0);2092invalidate_ce_path(ce, o);2093return1;2094}20952096static intkeep_entry(const struct cache_entry *ce,2097struct unpack_trees_options *o)2098{2099add_entry(o, ce,0,0);2100if(ce_stage(ce))2101invalidate_ce_path(ce, o);2102return1;2103}21042105#if DBRT_DEBUG2106static voidshow_stage_entry(FILE*o,2107const char*label,const struct cache_entry *ce)2108{2109if(!ce)2110fprintf(o,"%s(missing)\n", label);2111else2112fprintf(o,"%s%06o%s %d\t%s\n",2113 label,2114 ce->ce_mode,2115oid_to_hex(&ce->oid),2116ce_stage(ce),2117 ce->name);2118}2119#endif21202121intthreeway_merge(const struct cache_entry *const*stages,2122struct unpack_trees_options *o)2123{2124const struct cache_entry *index;2125const struct cache_entry *head;2126const struct cache_entry *remote = stages[o->head_idx +1];2127int count;2128int head_match =0;2129int remote_match =0;21302131int df_conflict_head =0;2132int df_conflict_remote =0;21332134int any_anc_missing =0;2135int no_anc_exists =1;2136int i;21372138for(i =1; i < o->head_idx; i++) {2139if(!stages[i] || stages[i] == o->df_conflict_entry)2140 any_anc_missing =1;2141else2142 no_anc_exists =0;2143}21442145 index = stages[0];2146 head = stages[o->head_idx];21472148if(head == o->df_conflict_entry) {2149 df_conflict_head =1;2150 head = NULL;2151}21522153if(remote == o->df_conflict_entry) {2154 df_conflict_remote =1;2155 remote = NULL;2156}21572158/*2159 * First, if there's a #16 situation, note that to prevent #132160 * and #14.2161 */2162if(!same(remote, head)) {2163for(i =1; i < o->head_idx; i++) {2164if(same(stages[i], head)) {2165 head_match = i;2166}2167if(same(stages[i], remote)) {2168 remote_match = i;2169}2170}2171}21722173/*2174 * We start with cases where the index is allowed to match2175 * something other than the head: #14(ALT) and #2ALT, where it2176 * is permitted to match the result instead.2177 */2178/* #14, #14ALT, #2ALT */2179if(remote && !df_conflict_head && head_match && !remote_match) {2180if(index && !same(index, remote) && !same(index, head))2181returnreject_merge(index, o);2182returnmerged_entry(remote, index, o);2183}2184/*2185 * If we have an entry in the index cache, then we want to2186 * make sure that it matches head.2187 */2188if(index && !same(index, head))2189returnreject_merge(index, o);21902191if(head) {2192/* #5ALT, #15 */2193if(same(head, remote))2194returnmerged_entry(head, index, o);2195/* #13, #3ALT */2196if(!df_conflict_remote && remote_match && !head_match)2197returnmerged_entry(head, index, o);2198}21992200/* #1 */2201if(!head && !remote && any_anc_missing)2202return0;22032204/*2205 * Under the "aggressive" rule, we resolve mostly trivial2206 * cases that we historically had git-merge-one-file resolve.2207 */2208if(o->aggressive) {2209int head_deleted = !head;2210int remote_deleted = !remote;2211const struct cache_entry *ce = NULL;22122213if(index)2214 ce = index;2215else if(head)2216 ce = head;2217else if(remote)2218 ce = remote;2219else{2220for(i =1; i < o->head_idx; i++) {2221if(stages[i] && stages[i] != o->df_conflict_entry) {2222 ce = stages[i];2223break;2224}2225}2226}22272228/*2229 * Deleted in both.2230 * Deleted in one and unchanged in the other.2231 */2232if((head_deleted && remote_deleted) ||2233(head_deleted && remote && remote_match) ||2234(remote_deleted && head && head_match)) {2235if(index)2236returndeleted_entry(index, index, o);2237if(ce && !head_deleted) {2238if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2239return-1;2240}2241return0;2242}2243/*2244 * Added in both, identically.2245 */2246if(no_anc_exists && head && remote &&same(head, remote))2247returnmerged_entry(head, index, o);22482249}22502251/* Below are "no merge" cases, which require that the index be2252 * up-to-date to avoid the files getting overwritten with2253 * conflict resolution files.2254 */2255if(index) {2256if(verify_uptodate(index, o))2257return-1;2258}22592260 o->nontrivial_merge =1;22612262/* #2, #3, #4, #6, #7, #9, #10, #11. */2263 count =0;2264if(!head_match || !remote_match) {2265for(i =1; i < o->head_idx; i++) {2266if(stages[i] && stages[i] != o->df_conflict_entry) {2267keep_entry(stages[i], o);2268 count++;2269break;2270}2271}2272}2273#if DBRT_DEBUG2274else{2275fprintf(stderr,"read-tree: warning #16 detected\n");2276show_stage_entry(stderr,"head ", stages[head_match]);2277show_stage_entry(stderr,"remote ", stages[remote_match]);2278}2279#endif2280if(head) { count +=keep_entry(head, o); }2281if(remote) { count +=keep_entry(remote, o); }2282return count;2283}22842285/*2286 * Two-way merge.2287 *2288 * The rule is to "carry forward" what is in the index without losing2289 * information across a "fast-forward", favoring a successful merge2290 * over a merge failure when it makes sense. For details of the2291 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2292 *2293 */2294inttwoway_merge(const struct cache_entry *const*src,2295struct unpack_trees_options *o)2296{2297const struct cache_entry *current = src[0];2298const struct cache_entry *oldtree = src[1];2299const struct cache_entry *newtree = src[2];23002301if(o->merge_size !=2)2302returnerror("Cannot do a twoway merge of%dtrees",2303 o->merge_size);23042305if(oldtree == o->df_conflict_entry)2306 oldtree = NULL;2307if(newtree == o->df_conflict_entry)2308 newtree = NULL;23092310if(current) {2311if(current->ce_flags & CE_CONFLICTED) {2312if(same(oldtree, newtree) || o->reset) {2313if(!newtree)2314returndeleted_entry(current, current, o);2315else2316returnmerged_entry(newtree, current, o);2317}2318returnreject_merge(current, o);2319}else if((!oldtree && !newtree) ||/* 4 and 5 */2320(!oldtree && newtree &&2321same(current, newtree)) ||/* 6 and 7 */2322(oldtree && newtree &&2323same(oldtree, newtree)) ||/* 14 and 15 */2324(oldtree && newtree &&2325!same(oldtree, newtree) &&/* 18 and 19 */2326same(current, newtree))) {2327returnkeep_entry(current, o);2328}else if(oldtree && !newtree &&same(current, oldtree)) {2329/* 10 or 11 */2330returndeleted_entry(oldtree, current, o);2331}else if(oldtree && newtree &&2332same(current, oldtree) && !same(current, newtree)) {2333/* 20 or 21 */2334returnmerged_entry(newtree, current, o);2335}else2336returnreject_merge(current, o);2337}2338else if(newtree) {2339if(oldtree && !o->initial_checkout) {2340/*2341 * deletion of the path was staged;2342 */2343if(same(oldtree, newtree))2344return1;2345returnreject_merge(oldtree, o);2346}2347returnmerged_entry(newtree, current, o);2348}2349returndeleted_entry(oldtree, current, o);2350}23512352/*2353 * Bind merge.2354 *2355 * Keep the index entries at stage0, collapse stage1 but make sure2356 * stage0 does not have anything there.2357 */2358intbind_merge(const struct cache_entry *const*src,2359struct unpack_trees_options *o)2360{2361const struct cache_entry *old = src[0];2362const struct cache_entry *a = src[1];23632364if(o->merge_size !=1)2365returnerror("Cannot do a bind merge of%dtrees",2366 o->merge_size);2367if(a && old)2368return o->gently ? -1:2369error(ERRORMSG(o, ERROR_BIND_OVERLAP),2370super_prefixed(a->name),2371super_prefixed(old->name));2372if(!a)2373returnkeep_entry(old, o);2374else2375returnmerged_entry(a, NULL, o);2376}23772378/*2379 * One-way merge.2380 *2381 * The rule is:2382 * - take the stat information from stage0, take the data from stage12383 */2384intoneway_merge(const struct cache_entry *const*src,2385struct unpack_trees_options *o)2386{2387const struct cache_entry *old = src[0];2388const struct cache_entry *a = src[1];23892390if(o->merge_size !=1)2391returnerror("Cannot do a oneway merge of%dtrees",2392 o->merge_size);23932394if(!a || a == o->df_conflict_entry)2395returndeleted_entry(old, old, o);23962397if(old &&same(old, a)) {2398int update =0;2399if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2400struct stat st;2401if(lstat(old->name, &st) ||2402ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2403 update |= CE_UPDATE;2404}2405if(o->update &&S_ISGITLINK(old->ce_mode) &&2406should_update_submodules() && !verify_uptodate(old, o))2407 update |= CE_UPDATE;2408add_entry(o, old, update,0);2409return0;2410}2411returnmerged_entry(a, old, o);2412}