1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"argv-array.h" 4#include"repository.h" 5#include"config.h" 6#include"dir.h" 7#include"tree.h" 8#include"tree-walk.h" 9#include"cache-tree.h" 10#include"unpack-trees.h" 11#include"progress.h" 12#include"refs.h" 13#include"attr.h" 14#include"split-index.h" 15#include"dir.h" 16#include"submodule.h" 17#include"submodule-config.h" 18#include"fsmonitor.h" 19#include"object-store.h" 20#include"fetch-object.h" 21 22/* 23 * Error messages expected by scripts out of plumbing commands such as 24 * read-tree. Non-scripted Porcelain is not required to use these messages 25 * and in fact are encouraged to reword them to better suit their particular 26 * situation better. See how "git checkout" and "git merge" replaces 27 * them using setup_unpack_trees_porcelain(), for example. 28 */ 29static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 30/* ERROR_WOULD_OVERWRITE */ 31"Entry '%s' would be overwritten by merge. Cannot merge.", 32 33/* ERROR_NOT_UPTODATE_FILE */ 34"Entry '%s' not uptodate. Cannot merge.", 35 36/* ERROR_NOT_UPTODATE_DIR */ 37"Updating '%s' would lose untracked files in it", 38 39/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 40"Untracked working tree file '%s' would be overwritten by merge.", 41 42/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 43"Untracked working tree file '%s' would be removed by merge.", 44 45/* ERROR_BIND_OVERLAP */ 46"Entry '%s' overlaps with '%s'. Cannot bind.", 47 48/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 49"Entry '%s' not uptodate. Cannot update sparse checkout.", 50 51/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 52"Working tree file '%s' would be overwritten by sparse checkout update.", 53 54/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 55"Working tree file '%s' would be removed by sparse checkout update.", 56 57/* ERROR_WOULD_LOSE_SUBMODULE */ 58"Submodule '%s' cannot checkout new HEAD.", 59}; 60 61#define ERRORMSG(o,type) \ 62 ( ((o) && (o)->msgs[(type)]) \ 63 ? ((o)->msgs[(type)]) \ 64 : (unpack_plumbing_errors[(type)]) ) 65 66static const char*super_prefixed(const char*path) 67{ 68/* 69 * It is necessary and sufficient to have two static buffers 70 * here, as the return value of this function is fed to 71 * error() using the unpack_*_errors[] templates we see above. 72 */ 73static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 74static int super_prefix_len = -1; 75static unsigned idx =ARRAY_SIZE(buf) -1; 76 77if(super_prefix_len <0) { 78const char*super_prefix =get_super_prefix(); 79if(!super_prefix) { 80 super_prefix_len =0; 81}else{ 82int i; 83for(i =0; i <ARRAY_SIZE(buf); i++) 84strbuf_addstr(&buf[i], super_prefix); 85 super_prefix_len = buf[0].len; 86} 87} 88 89if(!super_prefix_len) 90return path; 91 92if(++idx >=ARRAY_SIZE(buf)) 93 idx =0; 94 95strbuf_setlen(&buf[idx], super_prefix_len); 96strbuf_addstr(&buf[idx], path); 97 98return buf[idx].buf; 99} 100 101voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 102const char*cmd) 103{ 104int i; 105const char**msgs = opts->msgs; 106const char*msg; 107 108argv_array_init(&opts->msgs_to_free); 109 110if(!strcmp(cmd,"checkout")) 111 msg = advice_commit_before_merge 112?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 113"Please commit your changes or stash them before you switch branches.") 114:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 115else if(!strcmp(cmd,"merge")) 116 msg = advice_commit_before_merge 117?_("Your local changes to the following files would be overwritten by merge:\n%%s" 118"Please commit your changes or stash them before you merge.") 119:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 120else 121 msg = advice_commit_before_merge 122?_("Your local changes to the following files would be overwritten by%s:\n%%s" 123"Please commit your changes or stash them before you%s.") 124:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 125 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 126argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 127 128 msgs[ERROR_NOT_UPTODATE_DIR] = 129_("Updating the following directories would lose untracked files in them:\n%s"); 130 131if(!strcmp(cmd,"checkout")) 132 msg = advice_commit_before_merge 133?_("The following untracked working tree files would be removed by checkout:\n%%s" 134"Please move or remove them before you switch branches.") 135:_("The following untracked working tree files would be removed by checkout:\n%%s"); 136else if(!strcmp(cmd,"merge")) 137 msg = advice_commit_before_merge 138?_("The following untracked working tree files would be removed by merge:\n%%s" 139"Please move or remove them before you merge.") 140:_("The following untracked working tree files would be removed by merge:\n%%s"); 141else 142 msg = advice_commit_before_merge 143?_("The following untracked working tree files would be removed by%s:\n%%s" 144"Please move or remove them before you%s.") 145:_("The following untracked working tree files would be removed by%s:\n%%s"); 146 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = 147argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 148 149if(!strcmp(cmd,"checkout")) 150 msg = advice_commit_before_merge 151?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 152"Please move or remove them before you switch branches.") 153:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 154else if(!strcmp(cmd,"merge")) 155 msg = advice_commit_before_merge 156?_("The following untracked working tree files would be overwritten by merge:\n%%s" 157"Please move or remove them before you merge.") 158:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 159else 160 msg = advice_commit_before_merge 161?_("The following untracked working tree files would be overwritten by%s:\n%%s" 162"Please move or remove them before you%s.") 163:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 164 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = 165argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 166 167/* 168 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 169 * cannot easily display it as a list. 170 */ 171 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 172 173 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 174_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 175 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 176_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 177 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 178_("The following working tree files would be removed by sparse checkout update:\n%s"); 179 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 180_("Cannot update submodule:\n%s"); 181 182 opts->show_all_errors =1; 183/* rejected paths may not have a static buffer */ 184for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 185 opts->unpack_rejects[i].strdup_strings =1; 186} 187 188voidclear_unpack_trees_porcelain(struct unpack_trees_options *opts) 189{ 190argv_array_clear(&opts->msgs_to_free); 191memset(opts->msgs,0,sizeof(opts->msgs)); 192} 193 194static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 195unsigned int set,unsigned int clear) 196{ 197 clear |= CE_HASHED; 198 199if(set & CE_REMOVE) 200 set |= CE_WT_REMOVE; 201 202 ce->ce_flags = (ce->ce_flags & ~clear) | set; 203returnadd_index_entry(&o->result, ce, 204 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 205} 206 207static voidadd_entry(struct unpack_trees_options *o, 208const struct cache_entry *ce, 209unsigned int set,unsigned int clear) 210{ 211do_add_entry(o,dup_cache_entry(ce, &o->result), set, clear); 212} 213 214/* 215 * add error messages on path <path> 216 * corresponding to the type <e> with the message <msg> 217 * indicating if it should be display in porcelain or not 218 */ 219static intadd_rejected_path(struct unpack_trees_options *o, 220enum unpack_trees_error_types e, 221const char*path) 222{ 223if(!o->show_all_errors) 224returnerror(ERRORMSG(o, e),super_prefixed(path)); 225 226/* 227 * Otherwise, insert in a list for future display by 228 * display_error_msgs() 229 */ 230string_list_append(&o->unpack_rejects[e], path); 231return-1; 232} 233 234/* 235 * display all the error messages stored in a nice way 236 */ 237static voiddisplay_error_msgs(struct unpack_trees_options *o) 238{ 239int e, i; 240int something_displayed =0; 241for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 242struct string_list *rejects = &o->unpack_rejects[e]; 243if(rejects->nr >0) { 244struct strbuf path = STRBUF_INIT; 245 something_displayed =1; 246for(i =0; i < rejects->nr; i++) 247strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 248error(ERRORMSG(o, e),super_prefixed(path.buf)); 249strbuf_release(&path); 250} 251string_list_clear(rejects,0); 252} 253if(something_displayed) 254fprintf(stderr,_("Aborting\n")); 255} 256 257static intcheck_submodule_move_head(const struct cache_entry *ce, 258const char*old_id, 259const char*new_id, 260struct unpack_trees_options *o) 261{ 262unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 263const struct submodule *sub =submodule_from_ce(ce); 264 265if(!sub) 266return0; 267 268if(o->reset) 269 flags |= SUBMODULE_MOVE_HEAD_FORCE; 270 271if(submodule_move_head(ce->name, old_id, new_id, flags)) 272return o->gently ? -1: 273add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 274return0; 275} 276 277/* 278 * Preform the loading of the repository's gitmodules file. This function is 279 * used by 'check_update()' to perform loading of the gitmodules file in two 280 * differnt situations: 281 * (1) before removing entries from the working tree if the gitmodules file has 282 * been marked for removal. This situation is specified by 'state' == NULL. 283 * (2) before checking out entries to the working tree if the gitmodules file 284 * has been marked for update. This situation is specified by 'state' != NULL. 285 */ 286static voidload_gitmodules_file(struct index_state *index, 287struct checkout *state) 288{ 289int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 290 291if(pos >=0) { 292struct cache_entry *ce = index->cache[pos]; 293if(!state && ce->ce_flags & CE_WT_REMOVE) { 294repo_read_gitmodules(the_repository); 295}else if(state && (ce->ce_flags & CE_UPDATE)) { 296submodule_free(the_repository); 297checkout_entry(ce, state, NULL); 298repo_read_gitmodules(the_repository); 299} 300} 301} 302 303/* 304 * Unlink the last component and schedule the leading directories for 305 * removal, such that empty directories get removed. 306 */ 307static voidunlink_entry(const struct cache_entry *ce) 308{ 309const struct submodule *sub =submodule_from_ce(ce); 310if(sub) { 311/* state.force is set at the caller. */ 312submodule_move_head(ce->name,"HEAD", NULL, 313 SUBMODULE_MOVE_HEAD_FORCE); 314} 315if(!check_leading_path(ce->name,ce_namelen(ce))) 316return; 317if(remove_or_warn(ce->ce_mode, ce->name)) 318return; 319schedule_dir_for_removal(ce->name,ce_namelen(ce)); 320} 321 322static struct progress *get_progress(struct unpack_trees_options *o) 323{ 324unsigned cnt =0, total =0; 325struct index_state *index = &o->result; 326 327if(!o->update || !o->verbose_update) 328return NULL; 329 330for(; cnt < index->cache_nr; cnt++) { 331const struct cache_entry *ce = index->cache[cnt]; 332if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 333 total++; 334} 335 336returnstart_delayed_progress(_("Checking out files"), total); 337} 338 339static voidsetup_collided_checkout_detection(struct checkout *state, 340struct index_state *index) 341{ 342int i; 343 344 state->clone =1; 345for(i =0; i < index->cache_nr; i++) 346 index->cache[i]->ce_flags &= ~CE_MATCHED; 347} 348 349static voidreport_collided_checkout(struct index_state *index) 350{ 351struct string_list list = STRING_LIST_INIT_NODUP; 352int i; 353 354for(i =0; i < index->cache_nr; i++) { 355struct cache_entry *ce = index->cache[i]; 356 357if(!(ce->ce_flags & CE_MATCHED)) 358continue; 359 360string_list_append(&list, ce->name); 361 ce->ce_flags &= ~CE_MATCHED; 362} 363 364 list.cmp = fspathcmp; 365string_list_sort(&list); 366 367if(list.nr) { 368warning(_("the following paths have collided (e.g. case-sensitive paths\n" 369"on a case-insensitive filesystem) and only one from the same\n" 370"colliding group is in the working tree:\n")); 371 372for(i =0; i < list.nr; i++) 373fprintf(stderr," '%s'\n", list.items[i].string); 374} 375 376string_list_clear(&list,0); 377} 378 379static intcheck_updates(struct unpack_trees_options *o) 380{ 381unsigned cnt =0; 382int errs =0; 383struct progress *progress; 384struct index_state *index = &o->result; 385struct checkout state = CHECKOUT_INIT; 386int i; 387 388trace_performance_enter(); 389 state.force =1; 390 state.quiet =1; 391 state.refresh_cache =1; 392 state.istate = index; 393 394if(o->clone) 395setup_collided_checkout_detection(&state, index); 396 397 progress =get_progress(o); 398 399if(o->update) 400git_attr_set_direction(GIT_ATTR_CHECKOUT); 401 402if(should_update_submodules() && o->update && !o->dry_run) 403load_gitmodules_file(index, NULL); 404 405for(i =0; i < index->cache_nr; i++) { 406const struct cache_entry *ce = index->cache[i]; 407 408if(ce->ce_flags & CE_WT_REMOVE) { 409display_progress(progress, ++cnt); 410if(o->update && !o->dry_run) 411unlink_entry(ce); 412} 413} 414remove_marked_cache_entries(index); 415remove_scheduled_dirs(); 416 417if(should_update_submodules() && o->update && !o->dry_run) 418load_gitmodules_file(index, &state); 419 420enable_delayed_checkout(&state); 421if(repository_format_partial_clone && o->update && !o->dry_run) { 422/* 423 * Prefetch the objects that are to be checked out in the loop 424 * below. 425 */ 426struct oid_array to_fetch = OID_ARRAY_INIT; 427int fetch_if_missing_store = fetch_if_missing; 428 fetch_if_missing =0; 429for(i =0; i < index->cache_nr; i++) { 430struct cache_entry *ce = index->cache[i]; 431if((ce->ce_flags & CE_UPDATE) && 432!S_ISGITLINK(ce->ce_mode)) { 433if(!has_object_file(&ce->oid)) 434oid_array_append(&to_fetch, &ce->oid); 435} 436} 437if(to_fetch.nr) 438fetch_objects(repository_format_partial_clone, 439 to_fetch.oid, to_fetch.nr); 440 fetch_if_missing = fetch_if_missing_store; 441oid_array_clear(&to_fetch); 442} 443for(i =0; i < index->cache_nr; i++) { 444struct cache_entry *ce = index->cache[i]; 445 446if(ce->ce_flags & CE_UPDATE) { 447if(ce->ce_flags & CE_WT_REMOVE) 448BUG("both update and delete flags are set on%s", 449 ce->name); 450display_progress(progress, ++cnt); 451 ce->ce_flags &= ~CE_UPDATE; 452if(o->update && !o->dry_run) { 453 errs |=checkout_entry(ce, &state, NULL); 454} 455} 456} 457stop_progress(&progress); 458 errs |=finish_delayed_checkout(&state); 459if(o->update) 460git_attr_set_direction(GIT_ATTR_CHECKIN); 461 462if(o->clone) 463report_collided_checkout(index); 464 465trace_performance_leave("check_updates"); 466return errs !=0; 467} 468 469static intverify_uptodate_sparse(const struct cache_entry *ce, 470struct unpack_trees_options *o); 471static intverify_absent_sparse(const struct cache_entry *ce, 472enum unpack_trees_error_types, 473struct unpack_trees_options *o); 474 475static intapply_sparse_checkout(struct index_state *istate, 476struct cache_entry *ce, 477struct unpack_trees_options *o) 478{ 479int was_skip_worktree =ce_skip_worktree(ce); 480 481if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 482 ce->ce_flags |= CE_SKIP_WORKTREE; 483else 484 ce->ce_flags &= ~CE_SKIP_WORKTREE; 485if(was_skip_worktree !=ce_skip_worktree(ce)) { 486 ce->ce_flags |= CE_UPDATE_IN_BASE; 487mark_fsmonitor_invalid(istate, ce); 488 istate->cache_changed |= CE_ENTRY_CHANGED; 489} 490 491/* 492 * if (!was_skip_worktree && !ce_skip_worktree()) { 493 * This is perfectly normal. Move on; 494 * } 495 */ 496 497/* 498 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 499 * area as a result of ce_skip_worktree() shortcuts in 500 * verify_absent() and verify_uptodate(). 501 * Make sure they don't modify worktree if they are already 502 * outside checkout area 503 */ 504if(was_skip_worktree &&ce_skip_worktree(ce)) { 505 ce->ce_flags &= ~CE_UPDATE; 506 507/* 508 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 509 * on to get that file removed from both index and worktree. 510 * If that file is already outside worktree area, don't 511 * bother remove it. 512 */ 513if(ce->ce_flags & CE_REMOVE) 514 ce->ce_flags &= ~CE_WT_REMOVE; 515} 516 517if(!was_skip_worktree &&ce_skip_worktree(ce)) { 518/* 519 * If CE_UPDATE is set, verify_uptodate() must be called already 520 * also stat info may have lost after merged_entry() so calling 521 * verify_uptodate() again may fail 522 */ 523if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 524return-1; 525 ce->ce_flags |= CE_WT_REMOVE; 526 ce->ce_flags &= ~CE_UPDATE; 527} 528if(was_skip_worktree && !ce_skip_worktree(ce)) { 529if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 530return-1; 531 ce->ce_flags |= CE_UPDATE; 532} 533return0; 534} 535 536staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 537struct unpack_trees_options *o) 538{ 539int ret = o->fn(src, o); 540if(ret >0) 541 ret =0; 542return ret; 543} 544 545static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 546{ 547 ce->ce_flags |= CE_UNPACKED; 548 549if(o->cache_bottom < o->src_index->cache_nr && 550 o->src_index->cache[o->cache_bottom] == ce) { 551int bottom = o->cache_bottom; 552while(bottom < o->src_index->cache_nr && 553 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 554 bottom++; 555 o->cache_bottom = bottom; 556} 557} 558 559static voidmark_all_ce_unused(struct index_state *index) 560{ 561int i; 562for(i =0; i < index->cache_nr; i++) 563 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 564} 565 566static intlocate_in_src_index(const struct cache_entry *ce, 567struct unpack_trees_options *o) 568{ 569struct index_state *index = o->src_index; 570int len =ce_namelen(ce); 571int pos =index_name_pos(index, ce->name, len); 572if(pos <0) 573 pos = -1- pos; 574return pos; 575} 576 577/* 578 * We call unpack_index_entry() with an unmerged cache entry 579 * only in diff-index, and it wants a single callback. Skip 580 * the other unmerged entry with the same name. 581 */ 582static voidmark_ce_used_same_name(struct cache_entry *ce, 583struct unpack_trees_options *o) 584{ 585struct index_state *index = o->src_index; 586int len =ce_namelen(ce); 587int pos; 588 589for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 590struct cache_entry *next = index->cache[pos]; 591if(len !=ce_namelen(next) || 592memcmp(ce->name, next->name, len)) 593break; 594mark_ce_used(next, o); 595} 596} 597 598static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 599{ 600const struct index_state *index = o->src_index; 601int pos = o->cache_bottom; 602 603while(pos < index->cache_nr) { 604struct cache_entry *ce = index->cache[pos]; 605if(!(ce->ce_flags & CE_UNPACKED)) 606return ce; 607 pos++; 608} 609return NULL; 610} 611 612static voidadd_same_unmerged(const struct cache_entry *ce, 613struct unpack_trees_options *o) 614{ 615struct index_state *index = o->src_index; 616int len =ce_namelen(ce); 617int pos =index_name_pos(index, ce->name, len); 618 619if(0<= pos) 620die("programming error in a caller of mark_ce_used_same_name"); 621for(pos = -pos -1; pos < index->cache_nr; pos++) { 622struct cache_entry *next = index->cache[pos]; 623if(len !=ce_namelen(next) || 624memcmp(ce->name, next->name, len)) 625break; 626add_entry(o, next,0,0); 627mark_ce_used(next, o); 628} 629} 630 631static intunpack_index_entry(struct cache_entry *ce, 632struct unpack_trees_options *o) 633{ 634const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 635int ret; 636 637 src[0] = ce; 638 639mark_ce_used(ce, o); 640if(ce_stage(ce)) { 641if(o->skip_unmerged) { 642add_entry(o, ce,0,0); 643return0; 644} 645} 646 ret =call_unpack_fn(src, o); 647if(ce_stage(ce)) 648mark_ce_used_same_name(ce, o); 649return ret; 650} 651 652static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 653 654static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 655{ 656struct unpack_trees_options *o = info->data; 657 658if(o->diff_index_cached) 659return; 660 o->cache_bottom = bottom; 661} 662 663static intswitch_cache_bottom(struct traverse_info *info) 664{ 665struct unpack_trees_options *o = info->data; 666int ret, pos; 667 668if(o->diff_index_cached) 669return0; 670 ret = o->cache_bottom; 671 pos =find_cache_pos(info->prev, &info->name); 672 673if(pos < -1) 674 o->cache_bottom = -2- pos; 675else if(pos <0) 676 o->cache_bottom = o->src_index->cache_nr; 677return ret; 678} 679 680staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 681{ 682return name_j->oid && name_k->oid &&oideq(name_j->oid, name_k->oid); 683} 684 685static intall_trees_same_as_cache_tree(int n,unsigned long dirmask, 686struct name_entry *names, 687struct traverse_info *info) 688{ 689struct unpack_trees_options *o = info->data; 690int i; 691 692if(!o->merge || dirmask != ((1<< n) -1)) 693return0; 694 695for(i =1; i < n; i++) 696if(!are_same_oid(names, names + i)) 697return0; 698 699returncache_tree_matches_traversal(o->src_index->cache_tree, names, info); 700} 701 702static intindex_pos_by_traverse_info(struct name_entry *names, 703struct traverse_info *info) 704{ 705struct unpack_trees_options *o = info->data; 706int len =traverse_path_len(info, names); 707char*name =xmalloc(len +1/* slash */+1/* NUL */); 708int pos; 709 710make_traverse_path(name, info, names); 711 name[len++] ='/'; 712 name[len] ='\0'; 713 pos =index_name_pos(o->src_index, name, len); 714if(pos >=0) 715BUG("This is a directory and should not exist in index"); 716 pos = -pos -1; 717if(!starts_with(o->src_index->cache[pos]->name, name) || 718(pos >0&&starts_with(o->src_index->cache[pos-1]->name, name))) 719BUG("pos must point at the first entry in this directory"); 720free(name); 721return pos; 722} 723 724/* 725 * Fast path if we detect that all trees are the same as cache-tree at this 726 * path. We'll walk these trees in an iterative loop using cache-tree/index 727 * instead of ODB since we already know what these trees contain. 728 */ 729static inttraverse_by_cache_tree(int pos,int nr_entries,int nr_names, 730struct name_entry *names, 731struct traverse_info *info) 732{ 733struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 734struct unpack_trees_options *o = info->data; 735struct cache_entry *tree_ce = NULL; 736int ce_len =0; 737int i, d; 738 739if(!o->merge) 740BUG("We need cache-tree to do this optimization"); 741 742/* 743 * Do what unpack_callback() and unpack_nondirectories() normally 744 * do. But we walk all paths in an iterative loop instead. 745 * 746 * D/F conflicts and higher stage entries are not a concern 747 * because cache-tree would be invalidated and we would never 748 * get here in the first place. 749 */ 750for(i =0; i < nr_entries; i++) { 751int new_ce_len, len, rc; 752 753 src[0] = o->src_index->cache[pos + i]; 754 755 len =ce_namelen(src[0]); 756 new_ce_len =cache_entry_size(len); 757 758if(new_ce_len > ce_len) { 759 new_ce_len <<=1; 760 tree_ce =xrealloc(tree_ce, new_ce_len); 761memset(tree_ce,0, new_ce_len); 762 ce_len = new_ce_len; 763 764 tree_ce->ce_flags =create_ce_flags(0); 765 766for(d =1; d <= nr_names; d++) 767 src[d] = tree_ce; 768} 769 770 tree_ce->ce_mode = src[0]->ce_mode; 771 tree_ce->ce_namelen = len; 772oidcpy(&tree_ce->oid, &src[0]->oid); 773memcpy(tree_ce->name, src[0]->name, len +1); 774 775 rc =call_unpack_fn((const struct cache_entry *const*)src, o); 776if(rc <0) { 777free(tree_ce); 778return rc; 779} 780 781mark_ce_used(src[0], o); 782} 783free(tree_ce); 784if(o->debug_unpack) 785printf("Unpacked%dentries from%sto%susing cache-tree\n", 786 nr_entries, 787 o->src_index->cache[pos]->name, 788 o->src_index->cache[pos + nr_entries -1]->name); 789return0; 790} 791 792static inttraverse_trees_recursive(int n,unsigned long dirmask, 793unsigned long df_conflicts, 794struct name_entry *names, 795struct traverse_info *info) 796{ 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) { 807struct unpack_trees_options *o = info->data; 808int pos =index_pos_by_traverse_info(names, info); 809 810if(!o->merge || df_conflicts) 811BUG("Wrong condition to get here buddy"); 812 813/* 814 * All entries up to 'pos' must have been processed 815 * (i.e. marked CE_UNPACKED) at this point. But to be safe, 816 * save and restore cache_bottom anyway to not miss 817 * unprocessed entries before 'pos'. 818 */ 819 bottom = o->cache_bottom; 820 ret =traverse_by_cache_tree(pos, nr_entries, n, names, info); 821 o->cache_bottom = bottom; 822return ret; 823} 824 825 p = names; 826while(!p->mode) 827 p++; 828 829 newinfo = *info; 830 newinfo.prev = info; 831 newinfo.pathspec = info->pathspec; 832 newinfo.name = *p; 833 newinfo.pathlen +=tree_entry_len(p) +1; 834 newinfo.df_conflicts |= df_conflicts; 835 836/* 837 * Fetch the tree from the ODB for each peer directory in the 838 * n commits. 839 * 840 * For 2- and 3-way traversals, we try to avoid hitting the 841 * ODB twice for the same OID. This should yield a nice speed 842 * up in checkouts and merges when the commits are similar. 843 * 844 * We don't bother doing the full O(n^2) search for larger n, 845 * because wider traversals don't happen that often and we 846 * avoid the search setup. 847 * 848 * When 2 peer OIDs are the same, we just copy the tree 849 * descriptor data. This implicitly borrows the buffer 850 * data from the earlier cell. 851 */ 852for(i =0; i < n; i++, dirmask >>=1) { 853if(i >0&&are_same_oid(&names[i], &names[i -1])) 854 t[i] = t[i -1]; 855else if(i >1&&are_same_oid(&names[i], &names[i -2])) 856 t[i] = t[i -2]; 857else{ 858const struct object_id *oid = NULL; 859if(dirmask &1) 860 oid = names[i].oid; 861 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 862} 863} 864 865 bottom =switch_cache_bottom(&newinfo); 866 ret =traverse_trees(n, t, &newinfo); 867restore_cache_bottom(&newinfo, bottom); 868 869for(i =0; i < nr_buf; i++) 870free(buf[i]); 871 872return ret; 873} 874 875/* 876 * Compare the traverse-path to the cache entry without actually 877 * having to generate the textual representation of the traverse 878 * path. 879 * 880 * NOTE! This *only* compares up to the size of the traverse path 881 * itself - the caller needs to do the final check for the cache 882 * entry having more data at the end! 883 */ 884static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 885{ 886int len, pathlen, ce_len; 887const char*ce_name; 888 889if(info->prev) { 890int cmp =do_compare_entry_piecewise(ce, info->prev, 891&info->name); 892if(cmp) 893return cmp; 894} 895 pathlen = info->pathlen; 896 ce_len =ce_namelen(ce); 897 898/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 899if(ce_len < pathlen) 900return-1; 901 902 ce_len -= pathlen; 903 ce_name = ce->name + pathlen; 904 905 len =tree_entry_len(n); 906returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 907} 908 909static intdo_compare_entry(const struct cache_entry *ce, 910const struct traverse_info *info, 911const struct name_entry *n) 912{ 913int len, pathlen, ce_len; 914const char*ce_name; 915int cmp; 916 917/* 918 * If we have not precomputed the traverse path, it is quicker 919 * to avoid doing so. But if we have precomputed it, 920 * it is quicker to use the precomputed version. 921 */ 922if(!info->traverse_path) 923returndo_compare_entry_piecewise(ce, info, n); 924 925 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 926if(cmp) 927return cmp; 928 929 pathlen = info->pathlen; 930 ce_len =ce_namelen(ce); 931 932if(ce_len < pathlen) 933return-1; 934 935 ce_len -= pathlen; 936 ce_name = ce->name + pathlen; 937 938 len =tree_entry_len(n); 939returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 940} 941 942static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 943{ 944int cmp =do_compare_entry(ce, info, n); 945if(cmp) 946return cmp; 947 948/* 949 * Even if the beginning compared identically, the ce should 950 * compare as bigger than a directory leading up to it! 951 */ 952returnce_namelen(ce) >traverse_path_len(info, n); 953} 954 955static intce_in_traverse_path(const struct cache_entry *ce, 956const struct traverse_info *info) 957{ 958if(!info->prev) 959return1; 960if(do_compare_entry(ce, info->prev, &info->name)) 961return0; 962/* 963 * If ce (blob) is the same name as the path (which is a tree 964 * we will be descending into), it won't be inside it. 965 */ 966return(info->pathlen <ce_namelen(ce)); 967} 968 969static struct cache_entry *create_ce_entry(const struct traverse_info *info, 970const struct name_entry *n, 971int stage, 972struct index_state *istate, 973int is_transient) 974{ 975int len =traverse_path_len(info, n); 976struct cache_entry *ce = 977 is_transient ? 978make_empty_transient_cache_entry(len) : 979make_empty_cache_entry(istate, len); 980 981 ce->ce_mode =create_ce_mode(n->mode); 982 ce->ce_flags =create_ce_flags(stage); 983 ce->ce_namelen = len; 984oidcpy(&ce->oid, n->oid); 985make_traverse_path(ce->name, info, n); 986 987return ce; 988} 989 990/* 991 * Note that traverse_by_cache_tree() duplicates some logic in this function 992 * without actually calling it. If you change the logic here you may need to 993 * check and change there as well. 994 */ 995static intunpack_nondirectories(int n,unsigned long mask, 996unsigned long dirmask, 997struct cache_entry **src, 998const struct name_entry *names, 999const struct traverse_info *info)1000{1001int i;1002struct unpack_trees_options *o = info->data;1003unsigned long conflicts = info->df_conflicts | dirmask;10041005/* Do we have *only* directories? Nothing to do */1006if(mask == dirmask && !src[0])1007return0;10081009/*1010 * Ok, we've filled in up to any potential index entry in src[0],1011 * now do the rest.1012 */1013for(i =0; i < n; i++) {1014int stage;1015unsigned int bit =1ul<< i;1016if(conflicts & bit) {1017 src[i + o->merge] = o->df_conflict_entry;1018continue;1019}1020if(!(mask & bit))1021continue;1022if(!o->merge)1023 stage =0;1024else if(i +1< o->head_idx)1025 stage =1;1026else if(i +1> o->head_idx)1027 stage =3;1028else1029 stage =2;10301031/*1032 * If the merge bit is set, then the cache entries are1033 * discarded in the following block. In this case,1034 * construct "transient" cache_entries, as they are1035 * not stored in the index. otherwise construct the1036 * cache entry from the index aware logic.1037 */1038 src[i + o->merge] =create_ce_entry(info, names + i, stage, &o->result, o->merge);1039}10401041if(o->merge) {1042int rc =call_unpack_fn((const struct cache_entry *const*)src,1043 o);1044for(i =0; i < n; i++) {1045struct cache_entry *ce = src[i + o->merge];1046if(ce != o->df_conflict_entry)1047discard_cache_entry(ce);1048}1049return rc;1050}10511052for(i =0; i < n; i++)1053if(src[i] && src[i] != o->df_conflict_entry)1054if(do_add_entry(o, src[i],0,0))1055return-1;10561057return0;1058}10591060static intunpack_failed(struct unpack_trees_options *o,const char*message)1061{1062discard_index(&o->result);1063if(!o->gently && !o->exiting_early) {1064if(message)1065returnerror("%s", message);1066return-1;1067}1068return-1;1069}10701071/*1072 * The tree traversal is looking at name p. If we have a matching entry,1073 * return it. If name p is a directory in the index, do not return1074 * anything, as we will want to match it when the traversal descends into1075 * the directory.1076 */1077static intfind_cache_pos(struct traverse_info *info,1078const struct name_entry *p)1079{1080int pos;1081struct unpack_trees_options *o = info->data;1082struct index_state *index = o->src_index;1083int pfxlen = info->pathlen;1084int p_len =tree_entry_len(p);10851086for(pos = o->cache_bottom; pos < index->cache_nr; pos++) {1087const struct cache_entry *ce = index->cache[pos];1088const char*ce_name, *ce_slash;1089int cmp, ce_len;10901091if(ce->ce_flags & CE_UNPACKED) {1092/*1093 * cache_bottom entry is already unpacked, so1094 * we can never match it; don't check it1095 * again.1096 */1097if(pos == o->cache_bottom)1098++o->cache_bottom;1099continue;1100}1101if(!ce_in_traverse_path(ce, info)) {1102/*1103 * Check if we can skip future cache checks1104 * (because we're already past all possible1105 * entries in the traverse path).1106 */1107if(info->traverse_path) {1108if(strncmp(ce->name, info->traverse_path,1109 info->pathlen) >0)1110break;1111}1112continue;1113}1114 ce_name = ce->name + pfxlen;1115 ce_slash =strchr(ce_name,'/');1116if(ce_slash)1117 ce_len = ce_slash - ce_name;1118else1119 ce_len =ce_namelen(ce) - pfxlen;1120 cmp =name_compare(p->path, p_len, ce_name, ce_len);1121/*1122 * Exact match; if we have a directory we need to1123 * delay returning it.1124 */1125if(!cmp)1126return ce_slash ? -2- pos : pos;1127if(0< cmp)1128continue;/* keep looking */1129/*1130 * ce_name sorts after p->path; could it be that we1131 * have files under p->path directory in the index?1132 * E.g. ce_name == "t-i", and p->path == "t"; we may1133 * have "t/a" in the index.1134 */1135if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&1136 ce_name[p_len] <'/')1137continue;/* keep looking */1138break;1139}1140return-1;1141}11421143static struct cache_entry *find_cache_entry(struct traverse_info *info,1144const struct name_entry *p)1145{1146int pos =find_cache_pos(info, p);1147struct unpack_trees_options *o = info->data;11481149if(0<= pos)1150return o->src_index->cache[pos];1151else1152return NULL;1153}11541155static voiddebug_path(struct traverse_info *info)1156{1157if(info->prev) {1158debug_path(info->prev);1159if(*info->prev->name.path)1160putchar('/');1161}1162printf("%s", info->name.path);1163}11641165static voiddebug_name_entry(int i,struct name_entry *n)1166{1167printf("ent#%d %06o%s\n", i,1168 n->path ? n->mode :0,1169 n->path ? n->path :"(missing)");1170}11711172static voiddebug_unpack_callback(int n,1173unsigned long mask,1174unsigned long dirmask,1175struct name_entry *names,1176struct traverse_info *info)1177{1178int i;1179printf("* unpack mask%lu, dirmask%lu, cnt%d",1180 mask, dirmask, n);1181debug_path(info);1182putchar('\n');1183for(i =0; i < n; i++)1184debug_name_entry(i, names + i);1185}11861187/*1188 * Note that traverse_by_cache_tree() duplicates some logic in this function1189 * without actually calling it. If you change the logic here you may need to1190 * check and change there as well.1191 */1192static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info)1193{1194struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1195struct unpack_trees_options *o = info->data;1196const struct name_entry *p = names;11971198/* Find first entry with a real name (we could use "mask" too) */1199while(!p->mode)1200 p++;12011202if(o->debug_unpack)1203debug_unpack_callback(n, mask, dirmask, names, info);12041205/* Are we supposed to look at the index too? */1206if(o->merge) {1207while(1) {1208int cmp;1209struct cache_entry *ce;12101211if(o->diff_index_cached)1212 ce =next_cache_entry(o);1213else1214 ce =find_cache_entry(info, p);12151216if(!ce)1217break;1218 cmp =compare_entry(ce, info, p);1219if(cmp <0) {1220if(unpack_index_entry(ce, o) <0)1221returnunpack_failed(o, NULL);1222continue;1223}1224if(!cmp) {1225if(ce_stage(ce)) {1226/*1227 * If we skip unmerged index1228 * entries, we'll skip this1229 * entry *and* the tree1230 * entries associated with it!1231 */1232if(o->skip_unmerged) {1233add_same_unmerged(ce, o);1234return mask;1235}1236}1237 src[0] = ce;1238}1239break;1240}1241}12421243if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1244return-1;12451246if(o->merge && src[0]) {1247if(ce_stage(src[0]))1248mark_ce_used_same_name(src[0], o);1249else1250mark_ce_used(src[0], o);1251}12521253/* Now handle any directories.. */1254if(dirmask) {1255/* special case: "diff-index --cached" looking at a tree */1256if(o->diff_index_cached &&1257 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1258int matches;1259 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1260 names, info);1261/*1262 * Everything under the name matches; skip the1263 * entire hierarchy. diff_index_cached codepath1264 * special cases D/F conflicts in such a way that1265 * it does not do any look-ahead, so this is safe.1266 */1267if(matches) {1268 o->cache_bottom += matches;1269return mask;1270}1271}12721273if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1274 names, info) <0)1275return-1;1276return mask;1277}12781279return mask;1280}12811282static intclear_ce_flags_1(struct index_state *istate,1283struct cache_entry **cache,int nr,1284struct strbuf *prefix,1285int select_mask,int clear_mask,1286struct exclude_list *el,int defval);12871288/* Whole directory matching */1289static intclear_ce_flags_dir(struct index_state *istate,1290struct cache_entry **cache,int nr,1291struct strbuf *prefix,1292char*basename,1293int select_mask,int clear_mask,1294struct exclude_list *el,int defval)1295{1296struct cache_entry **cache_end;1297int dtype = DT_DIR;1298int ret =is_excluded_from_list(prefix->buf, prefix->len,1299 basename, &dtype, el, istate);1300int rc;13011302strbuf_addch(prefix,'/');13031304/* If undecided, use matching result of parent dir in defval */1305if(ret <0)1306 ret = defval;13071308for(cache_end = cache; cache_end != cache + nr; cache_end++) {1309struct cache_entry *ce = *cache_end;1310if(strncmp(ce->name, prefix->buf, prefix->len))1311break;1312}13131314/*1315 * TODO: check el, if there are no patterns that may conflict1316 * with ret (iow, we know in advance the incl/excl1317 * decision for the entire directory), clear flag here without1318 * calling clear_ce_flags_1(). That function will call1319 * the expensive is_excluded_from_list() on every entry.1320 */1321 rc =clear_ce_flags_1(istate, cache, cache_end - cache,1322 prefix,1323 select_mask, clear_mask,1324 el, ret);1325strbuf_setlen(prefix, prefix->len -1);1326return rc;1327}13281329/*1330 * Traverse the index, find every entry that matches according to1331 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1332 * number of traversed entries.1333 *1334 * If select_mask is non-zero, only entries whose ce_flags has on of1335 * those bits enabled are traversed.1336 *1337 * cache : pointer to an index entry1338 * prefix_len : an offset to its path1339 *1340 * The current path ("prefix") including the trailing '/' is1341 * cache[0]->name[0..(prefix_len-1)]1342 * Top level path has prefix_len zero.1343 */1344static intclear_ce_flags_1(struct index_state *istate,1345struct cache_entry **cache,int nr,1346struct strbuf *prefix,1347int select_mask,int clear_mask,1348struct exclude_list *el,int defval)1349{1350struct cache_entry **cache_end = cache + nr;13511352/*1353 * Process all entries that have the given prefix and meet1354 * select_mask condition1355 */1356while(cache != cache_end) {1357struct cache_entry *ce = *cache;1358const char*name, *slash;1359int len, dtype, ret;13601361if(select_mask && !(ce->ce_flags & select_mask)) {1362 cache++;1363continue;1364}13651366if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1367break;13681369 name = ce->name + prefix->len;1370 slash =strchr(name,'/');13711372/* If it's a directory, try whole directory match first */1373if(slash) {1374int processed;13751376 len = slash - name;1377strbuf_add(prefix, name, len);13781379 processed =clear_ce_flags_dir(istate, cache, cache_end - cache,1380 prefix,1381 prefix->buf + prefix->len - len,1382 select_mask, clear_mask,1383 el, defval);13841385/* clear_c_f_dir eats a whole dir already? */1386if(processed) {1387 cache += processed;1388strbuf_setlen(prefix, prefix->len - len);1389continue;1390}13911392strbuf_addch(prefix,'/');1393 cache +=clear_ce_flags_1(istate, cache, cache_end - cache,1394 prefix,1395 select_mask, clear_mask, el, defval);1396strbuf_setlen(prefix, prefix->len - len -1);1397continue;1398}13991400/* Non-directory */1401 dtype =ce_to_dtype(ce);1402 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1403 name, &dtype, el, istate);1404if(ret <0)1405 ret = defval;1406if(ret >0)1407 ce->ce_flags &= ~clear_mask;1408 cache++;1409}1410return nr - (cache_end - cache);1411}14121413static intclear_ce_flags(struct index_state *istate,1414int select_mask,int clear_mask,1415struct exclude_list *el)1416{1417static struct strbuf prefix = STRBUF_INIT;14181419strbuf_reset(&prefix);14201421returnclear_ce_flags_1(istate,1422 istate->cache,1423 istate->cache_nr,1424&prefix,1425 select_mask, clear_mask,1426 el,0);1427}14281429/*1430 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1431 */1432static voidmark_new_skip_worktree(struct exclude_list *el,1433struct index_state *istate,1434int select_flag,int skip_wt_flag)1435{1436int i;14371438/*1439 * 1. Pretend the narrowest worktree: only unmerged entries1440 * are checked out1441 */1442for(i =0; i < istate->cache_nr; i++) {1443struct cache_entry *ce = istate->cache[i];14441445if(select_flag && !(ce->ce_flags & select_flag))1446continue;14471448if(!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))1449 ce->ce_flags |= skip_wt_flag;1450else1451 ce->ce_flags &= ~skip_wt_flag;1452}14531454/*1455 * 2. Widen worktree according to sparse-checkout file.1456 * Matched entries will have skip_wt_flag cleared (i.e. "in")1457 */1458clear_ce_flags(istate, select_flag, skip_wt_flag, el);1459}14601461static intverify_absent(const struct cache_entry *,1462enum unpack_trees_error_types,1463struct unpack_trees_options *);1464/*1465 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1466 * resulting index, -2 on failure to reflect the changes to the work tree.1467 *1468 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1469 */1470intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1471{1472int i, ret;1473static struct cache_entry *dfc;1474struct exclude_list el;14751476if(len > MAX_UNPACK_TREES)1477die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);14781479trace_performance_enter();1480memset(&el,0,sizeof(el));1481if(!core_apply_sparse_checkout || !o->update)1482 o->skip_sparse_checkout =1;1483if(!o->skip_sparse_checkout) {1484char*sparse =git_pathdup("info/sparse-checkout");1485if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1486 o->skip_sparse_checkout =1;1487else1488 o->el = ⪙1489free(sparse);1490}14911492memset(&o->result,0,sizeof(o->result));1493 o->result.initialized =1;1494 o->result.timestamp.sec = o->src_index->timestamp.sec;1495 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1496 o->result.version = o->src_index->version;1497if(!o->src_index->split_index) {1498 o->result.split_index = NULL;1499}else if(o->src_index == o->dst_index) {1500/*1501 * o->dst_index (and thus o->src_index) will be discarded1502 * and overwritten with o->result at the end of this function,1503 * so just use src_index's split_index to avoid having to1504 * create a new one.1505 */1506 o->result.split_index = o->src_index->split_index;1507 o->result.split_index->refcount++;1508}else{1509 o->result.split_index =init_split_index(&o->result);1510}1511oidcpy(&o->result.oid, &o->src_index->oid);1512 o->merge_size = len;1513mark_all_ce_unused(o->src_index);15141515/*1516 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1517 */1518if(!o->skip_sparse_checkout)1519mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);15201521if(!dfc)1522 dfc =xcalloc(1,cache_entry_size(0));1523 o->df_conflict_entry = dfc;15241525if(len) {1526const char*prefix = o->prefix ? o->prefix :"";1527struct traverse_info info;15281529setup_traverse_info(&info, prefix);1530 info.fn = unpack_callback;1531 info.data = o;1532 info.show_all_errors = o->show_all_errors;1533 info.pathspec = o->pathspec;15341535if(o->prefix) {1536/*1537 * Unpack existing index entries that sort before the1538 * prefix the tree is spliced into. Note that o->merge1539 * is always true in this case.1540 */1541while(1) {1542struct cache_entry *ce =next_cache_entry(o);1543if(!ce)1544break;1545if(ce_in_traverse_path(ce, &info))1546break;1547if(unpack_index_entry(ce, o) <0)1548goto return_failed;1549}1550}15511552trace_performance_enter();1553 ret =traverse_trees(len, t, &info);1554trace_performance_leave("traverse_trees");1555if(ret <0)1556goto return_failed;1557}15581559/* Any left-over entries in the index? */1560if(o->merge) {1561while(1) {1562struct cache_entry *ce =next_cache_entry(o);1563if(!ce)1564break;1565if(unpack_index_entry(ce, o) <0)1566goto return_failed;1567}1568}1569mark_all_ce_unused(o->src_index);15701571if(o->trivial_merges_only && o->nontrivial_merge) {1572 ret =unpack_failed(o,"Merge requires file-level merging");1573goto done;1574}15751576if(!o->skip_sparse_checkout) {1577int empty_worktree =1;15781579/*1580 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11581 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1582 * so apply_sparse_checkout() won't attempt to remove it from worktree1583 */1584mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);15851586 ret =0;1587for(i =0; i < o->result.cache_nr; i++) {1588struct cache_entry *ce = o->result.cache[i];15891590/*1591 * Entries marked with CE_ADDED in merged_entry() do not have1592 * verify_absent() check (the check is effectively disabled1593 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1594 *1595 * Do the real check now because we have had1596 * correct CE_NEW_SKIP_WORKTREE1597 */1598if(ce->ce_flags & CE_ADDED &&1599verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1600if(!o->show_all_errors)1601goto return_failed;1602 ret = -1;1603}16041605if(apply_sparse_checkout(&o->result, ce, o)) {1606if(!o->show_all_errors)1607goto return_failed;1608 ret = -1;1609}1610if(!ce_skip_worktree(ce))1611 empty_worktree =0;16121613}1614if(ret <0)1615goto return_failed;1616/*1617 * Sparse checkout is meant to narrow down checkout area1618 * but it does not make sense to narrow down to empty working1619 * tree. This is usually a mistake in sparse checkout rules.1620 * Do not allow users to do that.1621 */1622if(o->result.cache_nr && empty_worktree) {1623 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1624goto done;1625}1626}16271628 ret =check_updates(o) ? (-2) :0;1629if(o->dst_index) {1630move_index_extensions(&o->result, o->src_index);1631if(!ret) {1632if(git_env_bool("GIT_TEST_CHECK_CACHE_TREE",0))1633cache_tree_verify(&o->result);1634if(!o->result.cache_tree)1635 o->result.cache_tree =cache_tree();1636if(!cache_tree_fully_valid(o->result.cache_tree))1637cache_tree_update(&o->result,1638 WRITE_TREE_SILENT |1639 WRITE_TREE_REPAIR);1640}1641discard_index(o->dst_index);1642*o->dst_index = o->result;1643}else{1644discard_index(&o->result);1645}1646 o->src_index = NULL;16471648done:1649trace_performance_leave("unpack_trees");1650clear_exclude_list(&el);1651return ret;16521653return_failed:1654if(o->show_all_errors)1655display_error_msgs(o);1656mark_all_ce_unused(o->src_index);1657 ret =unpack_failed(o, NULL);1658if(o->exiting_early)1659 ret =0;1660goto done;1661}16621663/* Here come the merge functions */16641665static intreject_merge(const struct cache_entry *ce,1666struct unpack_trees_options *o)1667{1668return o->gently ? -1:1669add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1670}16711672static intsame(const struct cache_entry *a,const struct cache_entry *b)1673{1674if(!!a != !!b)1675return0;1676if(!a && !b)1677return1;1678if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1679return0;1680return a->ce_mode == b->ce_mode &&1681oideq(&a->oid, &b->oid);1682}168316841685/*1686 * When a CE gets turned into an unmerged entry, we1687 * want it to be up-to-date1688 */1689static intverify_uptodate_1(const struct cache_entry *ce,1690struct unpack_trees_options *o,1691enum unpack_trees_error_types error_type)1692{1693struct stat st;16941695if(o->index_only)1696return0;16971698/*1699 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1700 * if this entry is truly up-to-date because this file may be1701 * overwritten.1702 */1703if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1704;/* keep checking */1705else if(o->reset ||ce_uptodate(ce))1706return0;17071708if(!lstat(ce->name, &st)) {1709int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1710unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);17111712if(submodule_from_ce(ce)) {1713int r =check_submodule_move_head(ce,1714"HEAD",oid_to_hex(&ce->oid), o);1715if(r)1716return o->gently ? -1:1717add_rejected_path(o, error_type, ce->name);1718return0;1719}17201721if(!changed)1722return0;1723/*1724 * Historic default policy was to allow submodule to be out1725 * of sync wrt the superproject index. If the submodule was1726 * not considered interesting above, we don't care here.1727 */1728if(S_ISGITLINK(ce->ce_mode))1729return0;17301731 errno =0;1732}1733if(errno == ENOENT)1734return0;1735return o->gently ? -1:1736add_rejected_path(o, error_type, ce->name);1737}17381739intverify_uptodate(const struct cache_entry *ce,1740struct unpack_trees_options *o)1741{1742if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1743return0;1744returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1745}17461747static intverify_uptodate_sparse(const struct cache_entry *ce,1748struct unpack_trees_options *o)1749{1750returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1751}17521753/*1754 * TODO: We should actually invalidate o->result, not src_index [1].1755 * But since cache tree and untracked cache both are not copied to1756 * o->result until unpacking is complete, we invalidate them on1757 * src_index instead with the assumption that they will be copied to1758 * dst_index at the end.1759 *1760 * [1] src_index->cache_tree is also used in unpack_callback() so if1761 * we invalidate o->result, we need to update it to use1762 * o->result.cache_tree as well.1763 */1764static voidinvalidate_ce_path(const struct cache_entry *ce,1765struct unpack_trees_options *o)1766{1767if(!ce)1768return;1769cache_tree_invalidate_path(o->src_index, ce->name);1770untracked_cache_invalidate_path(o->src_index, ce->name,1);1771}17721773/*1774 * Check that checking out ce->sha1 in subdir ce->name is not1775 * going to overwrite any working files.1776 *1777 * Currently, git does not checkout subprojects during a superproject1778 * checkout, so it is not going to overwrite anything.1779 */1780static intverify_clean_submodule(const char*old_sha1,1781const struct cache_entry *ce,1782enum unpack_trees_error_types error_type,1783struct unpack_trees_options *o)1784{1785if(!submodule_from_ce(ce))1786return0;17871788returncheck_submodule_move_head(ce, old_sha1,1789oid_to_hex(&ce->oid), o);1790}17911792static intverify_clean_subdirectory(const struct cache_entry *ce,1793enum unpack_trees_error_types error_type,1794struct unpack_trees_options *o)1795{1796/*1797 * we are about to extract "ce->name"; we would not want to lose1798 * anything in the existing directory there.1799 */1800int namelen;1801int i;1802struct dir_struct d;1803char*pathbuf;1804int cnt =0;18051806if(S_ISGITLINK(ce->ce_mode)) {1807struct object_id oid;1808int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1809/*1810 * If we are not going to update the submodule, then1811 * we don't care.1812 */1813if(!sub_head &&oideq(&oid, &ce->oid))1814return0;1815returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1816 ce, error_type, o);1817}18181819/*1820 * First let's make sure we do not have a local modification1821 * in that directory.1822 */1823 namelen =ce_namelen(ce);1824for(i =locate_in_src_index(ce, o);1825 i < o->src_index->cache_nr;1826 i++) {1827struct cache_entry *ce2 = o->src_index->cache[i];1828int len =ce_namelen(ce2);1829if(len < namelen ||1830strncmp(ce->name, ce2->name, namelen) ||1831 ce2->name[namelen] !='/')1832break;1833/*1834 * ce2->name is an entry in the subdirectory to be1835 * removed.1836 */1837if(!ce_stage(ce2)) {1838if(verify_uptodate(ce2, o))1839return-1;1840add_entry(o, ce2, CE_REMOVE,0);1841invalidate_ce_path(ce, o);1842mark_ce_used(ce2, o);1843}1844 cnt++;1845}18461847/*1848 * Then we need to make sure that we do not lose a locally1849 * present file that is not ignored.1850 */1851 pathbuf =xstrfmt("%.*s/", namelen, ce->name);18521853memset(&d,0,sizeof(d));1854if(o->dir)1855 d.exclude_per_dir = o->dir->exclude_per_dir;1856 i =read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);1857if(i)1858return o->gently ? -1:1859add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1860free(pathbuf);1861return cnt;1862}18631864/*1865 * This gets called when there was no index entry for the tree entry 'dst',1866 * but we found a file in the working tree that 'lstat()' said was fine,1867 * and we're on a case-insensitive filesystem.1868 *1869 * See if we can find a case-insensitive match in the index that also1870 * matches the stat information, and assume it's that other file!1871 */1872static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1873{1874const struct cache_entry *src;18751876 src =index_file_exists(o->src_index, name, len,1);1877return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1878}18791880static intcheck_ok_to_remove(const char*name,int len,int dtype,1881const struct cache_entry *ce,struct stat *st,1882enum unpack_trees_error_types error_type,1883struct unpack_trees_options *o)1884{1885const struct cache_entry *result;18861887/*1888 * It may be that the 'lstat()' succeeded even though1889 * target 'ce' was absent, because there is an old1890 * entry that is different only in case..1891 *1892 * Ignore that lstat() if it matches.1893 */1894if(ignore_case &&icase_exists(o, name, len, st))1895return0;18961897if(o->dir &&1898is_excluded(o->dir, o->src_index, name, &dtype))1899/*1900 * ce->name is explicitly excluded, so it is Ok to1901 * overwrite it.1902 */1903return0;1904if(S_ISDIR(st->st_mode)) {1905/*1906 * We are checking out path "foo" and1907 * found "foo/." in the working tree.1908 * This is tricky -- if we have modified1909 * files that are in "foo/" we would lose1910 * them.1911 */1912if(verify_clean_subdirectory(ce, error_type, o) <0)1913return-1;1914return0;1915}19161917/*1918 * The previous round may already have decided to1919 * delete this path, which is in a subdirectory that1920 * is being replaced with a blob.1921 */1922 result =index_file_exists(&o->result, name, len,0);1923if(result) {1924if(result->ce_flags & CE_REMOVE)1925return0;1926}19271928return o->gently ? -1:1929add_rejected_path(o, error_type, name);1930}19311932/*1933 * We do not want to remove or overwrite a working tree file that1934 * is not tracked, unless it is ignored.1935 */1936static intverify_absent_1(const struct cache_entry *ce,1937enum unpack_trees_error_types error_type,1938struct unpack_trees_options *o)1939{1940int len;1941struct stat st;19421943if(o->index_only || o->reset || !o->update)1944return0;19451946 len =check_leading_path(ce->name,ce_namelen(ce));1947if(!len)1948return0;1949else if(len >0) {1950char*path;1951int ret;19521953 path =xmemdupz(ce->name, len);1954if(lstat(path, &st))1955 ret =error_errno("cannot stat '%s'", path);1956else{1957if(submodule_from_ce(ce))1958 ret =check_submodule_move_head(ce,1959oid_to_hex(&ce->oid),1960 NULL, o);1961else1962 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1963&st, error_type, o);1964}1965free(path);1966return ret;1967}else if(lstat(ce->name, &st)) {1968if(errno != ENOENT)1969returnerror_errno("cannot stat '%s'", ce->name);1970return0;1971}else{1972if(submodule_from_ce(ce))1973returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1974 NULL, o);19751976returncheck_ok_to_remove(ce->name,ce_namelen(ce),1977ce_to_dtype(ce), ce, &st,1978 error_type, o);1979}1980}19811982static intverify_absent(const struct cache_entry *ce,1983enum unpack_trees_error_types error_type,1984struct unpack_trees_options *o)1985{1986if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1987return0;1988returnverify_absent_1(ce, error_type, o);1989}19901991static intverify_absent_sparse(const struct cache_entry *ce,1992enum unpack_trees_error_types error_type,1993struct unpack_trees_options *o)1994{1995enum unpack_trees_error_types orphaned_error = error_type;1996if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1997 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;19981999returnverify_absent_1(ce, orphaned_error, o);2000}20012002static intmerged_entry(const struct cache_entry *ce,2003const struct cache_entry *old,2004struct unpack_trees_options *o)2005{2006int update = CE_UPDATE;2007struct cache_entry *merge =dup_cache_entry(ce, &o->result);20082009if(!old) {2010/*2011 * New index entries. In sparse checkout, the following2012 * verify_absent() will be delayed until after2013 * traverse_trees() finishes in unpack_trees(), then:2014 *2015 * - CE_NEW_SKIP_WORKTREE will be computed correctly2016 * - verify_absent() be called again, this time with2017 * correct CE_NEW_SKIP_WORKTREE2018 *2019 * verify_absent() call here does nothing in sparse2020 * checkout (i.e. o->skip_sparse_checkout == 0)2021 */2022 update |= CE_ADDED;2023 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;20242025if(verify_absent(merge,2026 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {2027discard_cache_entry(merge);2028return-1;2029}2030invalidate_ce_path(merge, o);20312032if(submodule_from_ce(ce)) {2033int ret =check_submodule_move_head(ce, NULL,2034oid_to_hex(&ce->oid),2035 o);2036if(ret)2037return ret;2038}20392040}else if(!(old->ce_flags & CE_CONFLICTED)) {2041/*2042 * See if we can re-use the old CE directly?2043 * That way we get the uptodate stat info.2044 *2045 * This also removes the UPDATE flag on a match; otherwise2046 * we will end up overwriting local changes in the work tree.2047 */2048if(same(old, merge)) {2049copy_cache_entry(merge, old);2050 update =0;2051}else{2052if(verify_uptodate(old, o)) {2053discard_cache_entry(merge);2054return-1;2055}2056/* Migrate old flags over */2057 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);2058invalidate_ce_path(old, o);2059}20602061if(submodule_from_ce(ce)) {2062int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),2063oid_to_hex(&ce->oid),2064 o);2065if(ret)2066return ret;2067}2068}else{2069/*2070 * Previously unmerged entry left as an existence2071 * marker by read_index_unmerged();2072 */2073invalidate_ce_path(old, o);2074}20752076do_add_entry(o, merge, update, CE_STAGEMASK);2077return1;2078}20792080static intdeleted_entry(const struct cache_entry *ce,2081const struct cache_entry *old,2082struct unpack_trees_options *o)2083{2084/* Did it exist in the index? */2085if(!old) {2086if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2087return-1;2088return0;2089}2090if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))2091return-1;2092add_entry(o, ce, CE_REMOVE,0);2093invalidate_ce_path(ce, o);2094return1;2095}20962097static intkeep_entry(const struct cache_entry *ce,2098struct unpack_trees_options *o)2099{2100add_entry(o, ce,0,0);2101if(ce_stage(ce))2102invalidate_ce_path(ce, o);2103return1;2104}21052106#if DBRT_DEBUG2107static voidshow_stage_entry(FILE*o,2108const char*label,const struct cache_entry *ce)2109{2110if(!ce)2111fprintf(o,"%s(missing)\n", label);2112else2113fprintf(o,"%s%06o%s %d\t%s\n",2114 label,2115 ce->ce_mode,2116oid_to_hex(&ce->oid),2117ce_stage(ce),2118 ce->name);2119}2120#endif21212122intthreeway_merge(const struct cache_entry *const*stages,2123struct unpack_trees_options *o)2124{2125const struct cache_entry *index;2126const struct cache_entry *head;2127const struct cache_entry *remote = stages[o->head_idx +1];2128int count;2129int head_match =0;2130int remote_match =0;21312132int df_conflict_head =0;2133int df_conflict_remote =0;21342135int any_anc_missing =0;2136int no_anc_exists =1;2137int i;21382139for(i =1; i < o->head_idx; i++) {2140if(!stages[i] || stages[i] == o->df_conflict_entry)2141 any_anc_missing =1;2142else2143 no_anc_exists =0;2144}21452146 index = stages[0];2147 head = stages[o->head_idx];21482149if(head == o->df_conflict_entry) {2150 df_conflict_head =1;2151 head = NULL;2152}21532154if(remote == o->df_conflict_entry) {2155 df_conflict_remote =1;2156 remote = NULL;2157}21582159/*2160 * First, if there's a #16 situation, note that to prevent #132161 * and #14.2162 */2163if(!same(remote, head)) {2164for(i =1; i < o->head_idx; i++) {2165if(same(stages[i], head)) {2166 head_match = i;2167}2168if(same(stages[i], remote)) {2169 remote_match = i;2170}2171}2172}21732174/*2175 * We start with cases where the index is allowed to match2176 * something other than the head: #14(ALT) and #2ALT, where it2177 * is permitted to match the result instead.2178 */2179/* #14, #14ALT, #2ALT */2180if(remote && !df_conflict_head && head_match && !remote_match) {2181if(index && !same(index, remote) && !same(index, head))2182returnreject_merge(index, o);2183returnmerged_entry(remote, index, o);2184}2185/*2186 * If we have an entry in the index cache, then we want to2187 * make sure that it matches head.2188 */2189if(index && !same(index, head))2190returnreject_merge(index, o);21912192if(head) {2193/* #5ALT, #15 */2194if(same(head, remote))2195returnmerged_entry(head, index, o);2196/* #13, #3ALT */2197if(!df_conflict_remote && remote_match && !head_match)2198returnmerged_entry(head, index, o);2199}22002201/* #1 */2202if(!head && !remote && any_anc_missing)2203return0;22042205/*2206 * Under the "aggressive" rule, we resolve mostly trivial2207 * cases that we historically had git-merge-one-file resolve.2208 */2209if(o->aggressive) {2210int head_deleted = !head;2211int remote_deleted = !remote;2212const struct cache_entry *ce = NULL;22132214if(index)2215 ce = index;2216else if(head)2217 ce = head;2218else if(remote)2219 ce = remote;2220else{2221for(i =1; i < o->head_idx; i++) {2222if(stages[i] && stages[i] != o->df_conflict_entry) {2223 ce = stages[i];2224break;2225}2226}2227}22282229/*2230 * Deleted in both.2231 * Deleted in one and unchanged in the other.2232 */2233if((head_deleted && remote_deleted) ||2234(head_deleted && remote && remote_match) ||2235(remote_deleted && head && head_match)) {2236if(index)2237returndeleted_entry(index, index, o);2238if(ce && !head_deleted) {2239if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2240return-1;2241}2242return0;2243}2244/*2245 * Added in both, identically.2246 */2247if(no_anc_exists && head && remote &&same(head, remote))2248returnmerged_entry(head, index, o);22492250}22512252/* Below are "no merge" cases, which require that the index be2253 * up-to-date to avoid the files getting overwritten with2254 * conflict resolution files.2255 */2256if(index) {2257if(verify_uptodate(index, o))2258return-1;2259}22602261 o->nontrivial_merge =1;22622263/* #2, #3, #4, #6, #7, #9, #10, #11. */2264 count =0;2265if(!head_match || !remote_match) {2266for(i =1; i < o->head_idx; i++) {2267if(stages[i] && stages[i] != o->df_conflict_entry) {2268keep_entry(stages[i], o);2269 count++;2270break;2271}2272}2273}2274#if DBRT_DEBUG2275else{2276fprintf(stderr,"read-tree: warning #16 detected\n");2277show_stage_entry(stderr,"head ", stages[head_match]);2278show_stage_entry(stderr,"remote ", stages[remote_match]);2279}2280#endif2281if(head) { count +=keep_entry(head, o); }2282if(remote) { count +=keep_entry(remote, o); }2283return count;2284}22852286/*2287 * Two-way merge.2288 *2289 * The rule is to "carry forward" what is in the index without losing2290 * information across a "fast-forward", favoring a successful merge2291 * over a merge failure when it makes sense. For details of the2292 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2293 *2294 */2295inttwoway_merge(const struct cache_entry *const*src,2296struct unpack_trees_options *o)2297{2298const struct cache_entry *current = src[0];2299const struct cache_entry *oldtree = src[1];2300const struct cache_entry *newtree = src[2];23012302if(o->merge_size !=2)2303returnerror("Cannot do a twoway merge of%dtrees",2304 o->merge_size);23052306if(oldtree == o->df_conflict_entry)2307 oldtree = NULL;2308if(newtree == o->df_conflict_entry)2309 newtree = NULL;23102311if(current) {2312if(current->ce_flags & CE_CONFLICTED) {2313if(same(oldtree, newtree) || o->reset) {2314if(!newtree)2315returndeleted_entry(current, current, o);2316else2317returnmerged_entry(newtree, current, o);2318}2319returnreject_merge(current, o);2320}else if((!oldtree && !newtree) ||/* 4 and 5 */2321(!oldtree && newtree &&2322same(current, newtree)) ||/* 6 and 7 */2323(oldtree && newtree &&2324same(oldtree, newtree)) ||/* 14 and 15 */2325(oldtree && newtree &&2326!same(oldtree, newtree) &&/* 18 and 19 */2327same(current, newtree))) {2328returnkeep_entry(current, o);2329}else if(oldtree && !newtree &&same(current, oldtree)) {2330/* 10 or 11 */2331returndeleted_entry(oldtree, current, o);2332}else if(oldtree && newtree &&2333same(current, oldtree) && !same(current, newtree)) {2334/* 20 or 21 */2335returnmerged_entry(newtree, current, o);2336}else2337returnreject_merge(current, o);2338}2339else if(newtree) {2340if(oldtree && !o->initial_checkout) {2341/*2342 * deletion of the path was staged;2343 */2344if(same(oldtree, newtree))2345return1;2346returnreject_merge(oldtree, o);2347}2348returnmerged_entry(newtree, current, o);2349}2350returndeleted_entry(oldtree, current, o);2351}23522353/*2354 * Bind merge.2355 *2356 * Keep the index entries at stage0, collapse stage1 but make sure2357 * stage0 does not have anything there.2358 */2359intbind_merge(const struct cache_entry *const*src,2360struct unpack_trees_options *o)2361{2362const struct cache_entry *old = src[0];2363const struct cache_entry *a = src[1];23642365if(o->merge_size !=1)2366returnerror("Cannot do a bind merge of%dtrees",2367 o->merge_size);2368if(a && old)2369return o->gently ? -1:2370error(ERRORMSG(o, ERROR_BIND_OVERLAP),2371super_prefixed(a->name),2372super_prefixed(old->name));2373if(!a)2374returnkeep_entry(old, o);2375else2376returnmerged_entry(a, NULL, o);2377}23782379/*2380 * One-way merge.2381 *2382 * The rule is:2383 * - take the stat information from stage0, take the data from stage12384 */2385intoneway_merge(const struct cache_entry *const*src,2386struct unpack_trees_options *o)2387{2388const struct cache_entry *old = src[0];2389const struct cache_entry *a = src[1];23902391if(o->merge_size !=1)2392returnerror("Cannot do a oneway merge of%dtrees",2393 o->merge_size);23942395if(!a || a == o->df_conflict_entry)2396returndeleted_entry(old, old, o);23972398if(old &&same(old, a)) {2399int update =0;2400if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2401struct stat st;2402if(lstat(old->name, &st) ||2403ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2404 update |= CE_UPDATE;2405}2406if(o->update &&S_ISGITLINK(old->ce_mode) &&2407should_update_submodules() && !verify_uptodate(old, o))2408 update |= CE_UPDATE;2409add_entry(o, old, update,0);2410return0;2411}2412returnmerged_entry(a, old, o);2413}