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 303static struct progress *get_progress(struct unpack_trees_options *o) 304{ 305unsigned cnt =0, total =0; 306struct index_state *index = &o->result; 307 308if(!o->update || !o->verbose_update) 309return NULL; 310 311for(; cnt < index->cache_nr; cnt++) { 312const struct cache_entry *ce = index->cache[cnt]; 313if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 314 total++; 315} 316 317returnstart_delayed_progress(_("Checking out files"), total); 318} 319 320static voidsetup_collided_checkout_detection(struct checkout *state, 321struct index_state *index) 322{ 323int i; 324 325 state->clone =1; 326for(i =0; i < index->cache_nr; i++) 327 index->cache[i]->ce_flags &= ~CE_MATCHED; 328} 329 330static voidreport_collided_checkout(struct index_state *index) 331{ 332struct string_list list = STRING_LIST_INIT_NODUP; 333int i; 334 335for(i =0; i < index->cache_nr; i++) { 336struct cache_entry *ce = index->cache[i]; 337 338if(!(ce->ce_flags & CE_MATCHED)) 339continue; 340 341string_list_append(&list, ce->name); 342 ce->ce_flags &= ~CE_MATCHED; 343} 344 345 list.cmp = fspathcmp; 346string_list_sort(&list); 347 348if(list.nr) { 349warning(_("the following paths have collided (e.g. case-sensitive paths\n" 350"on a case-insensitive filesystem) and only one from the same\n" 351"colliding group is in the working tree:\n")); 352 353for(i =0; i < list.nr; i++) 354fprintf(stderr," '%s'\n", list.items[i].string); 355} 356 357string_list_clear(&list,0); 358} 359 360static intcheck_updates(struct unpack_trees_options *o) 361{ 362unsigned cnt =0; 363int errs =0; 364struct progress *progress; 365struct index_state *index = &o->result; 366struct checkout state = CHECKOUT_INIT; 367int i; 368 369trace_performance_enter(); 370 state.force =1; 371 state.quiet =1; 372 state.refresh_cache =1; 373 state.istate = index; 374 375if(o->clone) 376setup_collided_checkout_detection(&state, index); 377 378 progress =get_progress(o); 379 380if(o->update) 381git_attr_set_direction(GIT_ATTR_CHECKOUT); 382 383if(should_update_submodules() && o->update && !o->dry_run) 384load_gitmodules_file(index, NULL); 385 386for(i =0; i < index->cache_nr; i++) { 387const struct cache_entry *ce = index->cache[i]; 388 389if(ce->ce_flags & CE_WT_REMOVE) { 390display_progress(progress, ++cnt); 391if(o->update && !o->dry_run) 392unlink_entry(ce); 393} 394} 395remove_marked_cache_entries(index,0); 396remove_scheduled_dirs(); 397 398if(should_update_submodules() && o->update && !o->dry_run) 399load_gitmodules_file(index, &state); 400 401enable_delayed_checkout(&state); 402if(repository_format_partial_clone && o->update && !o->dry_run) { 403/* 404 * Prefetch the objects that are to be checked out in the loop 405 * below. 406 */ 407struct oid_array to_fetch = OID_ARRAY_INIT; 408int fetch_if_missing_store = fetch_if_missing; 409 fetch_if_missing =0; 410for(i =0; i < index->cache_nr; i++) { 411struct cache_entry *ce = index->cache[i]; 412if((ce->ce_flags & CE_UPDATE) && 413!S_ISGITLINK(ce->ce_mode)) { 414if(!has_object_file(&ce->oid)) 415oid_array_append(&to_fetch, &ce->oid); 416} 417} 418if(to_fetch.nr) 419fetch_objects(repository_format_partial_clone, 420 to_fetch.oid, to_fetch.nr); 421 fetch_if_missing = fetch_if_missing_store; 422oid_array_clear(&to_fetch); 423} 424for(i =0; i < index->cache_nr; i++) { 425struct cache_entry *ce = index->cache[i]; 426 427if(ce->ce_flags & CE_UPDATE) { 428if(ce->ce_flags & CE_WT_REMOVE) 429BUG("both update and delete flags are set on%s", 430 ce->name); 431display_progress(progress, ++cnt); 432 ce->ce_flags &= ~CE_UPDATE; 433if(o->update && !o->dry_run) { 434 errs |=checkout_entry(ce, &state, NULL); 435} 436} 437} 438stop_progress(&progress); 439 errs |=finish_delayed_checkout(&state); 440if(o->update) 441git_attr_set_direction(GIT_ATTR_CHECKIN); 442 443if(o->clone) 444report_collided_checkout(index); 445 446trace_performance_leave("check_updates"); 447return errs !=0; 448} 449 450static intverify_uptodate_sparse(const struct cache_entry *ce, 451struct unpack_trees_options *o); 452static intverify_absent_sparse(const struct cache_entry *ce, 453enum unpack_trees_error_types, 454struct unpack_trees_options *o); 455 456static intapply_sparse_checkout(struct index_state *istate, 457struct cache_entry *ce, 458struct unpack_trees_options *o) 459{ 460int was_skip_worktree =ce_skip_worktree(ce); 461 462if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 463 ce->ce_flags |= CE_SKIP_WORKTREE; 464else 465 ce->ce_flags &= ~CE_SKIP_WORKTREE; 466if(was_skip_worktree !=ce_skip_worktree(ce)) { 467 ce->ce_flags |= CE_UPDATE_IN_BASE; 468mark_fsmonitor_invalid(istate, ce); 469 istate->cache_changed |= CE_ENTRY_CHANGED; 470} 471 472/* 473 * if (!was_skip_worktree && !ce_skip_worktree()) { 474 * This is perfectly normal. Move on; 475 * } 476 */ 477 478/* 479 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 480 * area as a result of ce_skip_worktree() shortcuts in 481 * verify_absent() and verify_uptodate(). 482 * Make sure they don't modify worktree if they are already 483 * outside checkout area 484 */ 485if(was_skip_worktree &&ce_skip_worktree(ce)) { 486 ce->ce_flags &= ~CE_UPDATE; 487 488/* 489 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 490 * on to get that file removed from both index and worktree. 491 * If that file is already outside worktree area, don't 492 * bother remove it. 493 */ 494if(ce->ce_flags & CE_REMOVE) 495 ce->ce_flags &= ~CE_WT_REMOVE; 496} 497 498if(!was_skip_worktree &&ce_skip_worktree(ce)) { 499/* 500 * If CE_UPDATE is set, verify_uptodate() must be called already 501 * also stat info may have lost after merged_entry() so calling 502 * verify_uptodate() again may fail 503 */ 504if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 505return-1; 506 ce->ce_flags |= CE_WT_REMOVE; 507 ce->ce_flags &= ~CE_UPDATE; 508} 509if(was_skip_worktree && !ce_skip_worktree(ce)) { 510if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 511return-1; 512 ce->ce_flags |= CE_UPDATE; 513} 514return0; 515} 516 517staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 518struct unpack_trees_options *o) 519{ 520int ret = o->fn(src, o); 521if(ret >0) 522 ret =0; 523return ret; 524} 525 526static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 527{ 528 ce->ce_flags |= CE_UNPACKED; 529 530if(o->cache_bottom < o->src_index->cache_nr && 531 o->src_index->cache[o->cache_bottom] == ce) { 532int bottom = o->cache_bottom; 533while(bottom < o->src_index->cache_nr && 534 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 535 bottom++; 536 o->cache_bottom = bottom; 537} 538} 539 540static voidmark_all_ce_unused(struct index_state *index) 541{ 542int i; 543for(i =0; i < index->cache_nr; i++) 544 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 545} 546 547static intlocate_in_src_index(const struct cache_entry *ce, 548struct unpack_trees_options *o) 549{ 550struct index_state *index = o->src_index; 551int len =ce_namelen(ce); 552int pos =index_name_pos(index, ce->name, len); 553if(pos <0) 554 pos = -1- pos; 555return pos; 556} 557 558/* 559 * We call unpack_index_entry() with an unmerged cache entry 560 * only in diff-index, and it wants a single callback. Skip 561 * the other unmerged entry with the same name. 562 */ 563static voidmark_ce_used_same_name(struct cache_entry *ce, 564struct unpack_trees_options *o) 565{ 566struct index_state *index = o->src_index; 567int len =ce_namelen(ce); 568int pos; 569 570for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 571struct cache_entry *next = index->cache[pos]; 572if(len !=ce_namelen(next) || 573memcmp(ce->name, next->name, len)) 574break; 575mark_ce_used(next, o); 576} 577} 578 579static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 580{ 581const struct index_state *index = o->src_index; 582int pos = o->cache_bottom; 583 584while(pos < index->cache_nr) { 585struct cache_entry *ce = index->cache[pos]; 586if(!(ce->ce_flags & CE_UNPACKED)) 587return ce; 588 pos++; 589} 590return NULL; 591} 592 593static voidadd_same_unmerged(const struct cache_entry *ce, 594struct unpack_trees_options *o) 595{ 596struct index_state *index = o->src_index; 597int len =ce_namelen(ce); 598int pos =index_name_pos(index, ce->name, len); 599 600if(0<= pos) 601die("programming error in a caller of mark_ce_used_same_name"); 602for(pos = -pos -1; pos < index->cache_nr; pos++) { 603struct cache_entry *next = index->cache[pos]; 604if(len !=ce_namelen(next) || 605memcmp(ce->name, next->name, len)) 606break; 607add_entry(o, next,0,0); 608mark_ce_used(next, o); 609} 610} 611 612static intunpack_index_entry(struct cache_entry *ce, 613struct unpack_trees_options *o) 614{ 615const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 616int ret; 617 618 src[0] = ce; 619 620mark_ce_used(ce, o); 621if(ce_stage(ce)) { 622if(o->skip_unmerged) { 623add_entry(o, ce,0,0); 624return0; 625} 626} 627 ret =call_unpack_fn(src, o); 628if(ce_stage(ce)) 629mark_ce_used_same_name(ce, o); 630return ret; 631} 632 633static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 634 635static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 636{ 637struct unpack_trees_options *o = info->data; 638 639if(o->diff_index_cached) 640return; 641 o->cache_bottom = bottom; 642} 643 644static intswitch_cache_bottom(struct traverse_info *info) 645{ 646struct unpack_trees_options *o = info->data; 647int ret, pos; 648 649if(o->diff_index_cached) 650return0; 651 ret = o->cache_bottom; 652 pos =find_cache_pos(info->prev, &info->name); 653 654if(pos < -1) 655 o->cache_bottom = -2- pos; 656else if(pos <0) 657 o->cache_bottom = o->src_index->cache_nr; 658return ret; 659} 660 661staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 662{ 663return name_j->oid && name_k->oid &&oideq(name_j->oid, name_k->oid); 664} 665 666static intall_trees_same_as_cache_tree(int n,unsigned long dirmask, 667struct name_entry *names, 668struct traverse_info *info) 669{ 670struct unpack_trees_options *o = info->data; 671int i; 672 673if(!o->merge || dirmask != ((1<< n) -1)) 674return0; 675 676for(i =1; i < n; i++) 677if(!are_same_oid(names, names + i)) 678return0; 679 680returncache_tree_matches_traversal(o->src_index->cache_tree, names, info); 681} 682 683static intindex_pos_by_traverse_info(struct name_entry *names, 684struct traverse_info *info) 685{ 686struct unpack_trees_options *o = info->data; 687int len =traverse_path_len(info, names); 688char*name =xmalloc(len +1/* slash */+1/* NUL */); 689int pos; 690 691make_traverse_path(name, info, names); 692 name[len++] ='/'; 693 name[len] ='\0'; 694 pos =index_name_pos(o->src_index, name, len); 695if(pos >=0) 696BUG("This is a directory and should not exist in index"); 697 pos = -pos -1; 698if(!starts_with(o->src_index->cache[pos]->name, name) || 699(pos >0&&starts_with(o->src_index->cache[pos-1]->name, name))) 700BUG("pos must point at the first entry in this directory"); 701free(name); 702return pos; 703} 704 705/* 706 * Fast path if we detect that all trees are the same as cache-tree at this 707 * path. We'll walk these trees in an iterative loop using cache-tree/index 708 * instead of ODB since we already know what these trees contain. 709 */ 710static inttraverse_by_cache_tree(int pos,int nr_entries,int nr_names, 711struct name_entry *names, 712struct traverse_info *info) 713{ 714struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 715struct unpack_trees_options *o = info->data; 716struct cache_entry *tree_ce = NULL; 717int ce_len =0; 718int i, d; 719 720if(!o->merge) 721BUG("We need cache-tree to do this optimization"); 722 723/* 724 * Do what unpack_callback() and unpack_nondirectories() normally 725 * do. But we walk all paths in an iterative loop instead. 726 * 727 * D/F conflicts and higher stage entries are not a concern 728 * because cache-tree would be invalidated and we would never 729 * get here in the first place. 730 */ 731for(i =0; i < nr_entries; i++) { 732int new_ce_len, len, rc; 733 734 src[0] = o->src_index->cache[pos + i]; 735 736 len =ce_namelen(src[0]); 737 new_ce_len =cache_entry_size(len); 738 739if(new_ce_len > ce_len) { 740 new_ce_len <<=1; 741 tree_ce =xrealloc(tree_ce, new_ce_len); 742memset(tree_ce,0, new_ce_len); 743 ce_len = new_ce_len; 744 745 tree_ce->ce_flags =create_ce_flags(0); 746 747for(d =1; d <= nr_names; d++) 748 src[d] = tree_ce; 749} 750 751 tree_ce->ce_mode = src[0]->ce_mode; 752 tree_ce->ce_namelen = len; 753oidcpy(&tree_ce->oid, &src[0]->oid); 754memcpy(tree_ce->name, src[0]->name, len +1); 755 756 rc =call_unpack_fn((const struct cache_entry *const*)src, o); 757if(rc <0) { 758free(tree_ce); 759return rc; 760} 761 762mark_ce_used(src[0], o); 763} 764free(tree_ce); 765if(o->debug_unpack) 766printf("Unpacked%dentries from%sto%susing cache-tree\n", 767 nr_entries, 768 o->src_index->cache[pos]->name, 769 o->src_index->cache[pos + nr_entries -1]->name); 770return0; 771} 772 773static inttraverse_trees_recursive(int n,unsigned long dirmask, 774unsigned long df_conflicts, 775struct name_entry *names, 776struct traverse_info *info) 777{ 778int i, ret, bottom; 779int nr_buf =0; 780struct tree_desc t[MAX_UNPACK_TREES]; 781void*buf[MAX_UNPACK_TREES]; 782struct traverse_info newinfo; 783struct name_entry *p; 784int nr_entries; 785 786 nr_entries =all_trees_same_as_cache_tree(n, dirmask, names, info); 787if(nr_entries >0) { 788struct unpack_trees_options *o = info->data; 789int pos =index_pos_by_traverse_info(names, info); 790 791if(!o->merge || df_conflicts) 792BUG("Wrong condition to get here buddy"); 793 794/* 795 * All entries up to 'pos' must have been processed 796 * (i.e. marked CE_UNPACKED) at this point. But to be safe, 797 * save and restore cache_bottom anyway to not miss 798 * unprocessed entries before 'pos'. 799 */ 800 bottom = o->cache_bottom; 801 ret =traverse_by_cache_tree(pos, nr_entries, n, names, info); 802 o->cache_bottom = bottom; 803return ret; 804} 805 806 p = names; 807while(!p->mode) 808 p++; 809 810 newinfo = *info; 811 newinfo.prev = info; 812 newinfo.pathspec = info->pathspec; 813 newinfo.name = *p; 814 newinfo.pathlen +=tree_entry_len(p) +1; 815 newinfo.df_conflicts |= df_conflicts; 816 817/* 818 * Fetch the tree from the ODB for each peer directory in the 819 * n commits. 820 * 821 * For 2- and 3-way traversals, we try to avoid hitting the 822 * ODB twice for the same OID. This should yield a nice speed 823 * up in checkouts and merges when the commits are similar. 824 * 825 * We don't bother doing the full O(n^2) search for larger n, 826 * because wider traversals don't happen that often and we 827 * avoid the search setup. 828 * 829 * When 2 peer OIDs are the same, we just copy the tree 830 * descriptor data. This implicitly borrows the buffer 831 * data from the earlier cell. 832 */ 833for(i =0; i < n; i++, dirmask >>=1) { 834if(i >0&&are_same_oid(&names[i], &names[i -1])) 835 t[i] = t[i -1]; 836else if(i >1&&are_same_oid(&names[i], &names[i -2])) 837 t[i] = t[i -2]; 838else{ 839const struct object_id *oid = NULL; 840if(dirmask &1) 841 oid = names[i].oid; 842 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 843} 844} 845 846 bottom =switch_cache_bottom(&newinfo); 847 ret =traverse_trees(n, t, &newinfo); 848restore_cache_bottom(&newinfo, bottom); 849 850for(i =0; i < nr_buf; i++) 851free(buf[i]); 852 853return ret; 854} 855 856/* 857 * Compare the traverse-path to the cache entry without actually 858 * having to generate the textual representation of the traverse 859 * path. 860 * 861 * NOTE! This *only* compares up to the size of the traverse path 862 * itself - the caller needs to do the final check for the cache 863 * entry having more data at the end! 864 */ 865static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 866{ 867int len, pathlen, ce_len; 868const char*ce_name; 869 870if(info->prev) { 871int cmp =do_compare_entry_piecewise(ce, info->prev, 872&info->name); 873if(cmp) 874return cmp; 875} 876 pathlen = info->pathlen; 877 ce_len =ce_namelen(ce); 878 879/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 880if(ce_len < pathlen) 881return-1; 882 883 ce_len -= pathlen; 884 ce_name = ce->name + pathlen; 885 886 len =tree_entry_len(n); 887returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 888} 889 890static intdo_compare_entry(const struct cache_entry *ce, 891const struct traverse_info *info, 892const struct name_entry *n) 893{ 894int len, pathlen, ce_len; 895const char*ce_name; 896int cmp; 897 898/* 899 * If we have not precomputed the traverse path, it is quicker 900 * to avoid doing so. But if we have precomputed it, 901 * it is quicker to use the precomputed version. 902 */ 903if(!info->traverse_path) 904returndo_compare_entry_piecewise(ce, info, n); 905 906 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 907if(cmp) 908return cmp; 909 910 pathlen = info->pathlen; 911 ce_len =ce_namelen(ce); 912 913if(ce_len < pathlen) 914return-1; 915 916 ce_len -= pathlen; 917 ce_name = ce->name + pathlen; 918 919 len =tree_entry_len(n); 920returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 921} 922 923static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 924{ 925int cmp =do_compare_entry(ce, info, n); 926if(cmp) 927return cmp; 928 929/* 930 * Even if the beginning compared identically, the ce should 931 * compare as bigger than a directory leading up to it! 932 */ 933returnce_namelen(ce) >traverse_path_len(info, n); 934} 935 936static intce_in_traverse_path(const struct cache_entry *ce, 937const struct traverse_info *info) 938{ 939if(!info->prev) 940return1; 941if(do_compare_entry(ce, info->prev, &info->name)) 942return0; 943/* 944 * If ce (blob) is the same name as the path (which is a tree 945 * we will be descending into), it won't be inside it. 946 */ 947return(info->pathlen <ce_namelen(ce)); 948} 949 950static struct cache_entry *create_ce_entry(const struct traverse_info *info, 951const struct name_entry *n, 952int stage, 953struct index_state *istate, 954int is_transient) 955{ 956int len =traverse_path_len(info, n); 957struct cache_entry *ce = 958 is_transient ? 959make_empty_transient_cache_entry(len) : 960make_empty_cache_entry(istate, len); 961 962 ce->ce_mode =create_ce_mode(n->mode); 963 ce->ce_flags =create_ce_flags(stage); 964 ce->ce_namelen = len; 965oidcpy(&ce->oid, n->oid); 966make_traverse_path(ce->name, info, n); 967 968return ce; 969} 970 971/* 972 * Note that traverse_by_cache_tree() duplicates some logic in this function 973 * without actually calling it. If you change the logic here you may need to 974 * check and change there as well. 975 */ 976static intunpack_nondirectories(int n,unsigned long mask, 977unsigned long dirmask, 978struct cache_entry **src, 979const struct name_entry *names, 980const struct traverse_info *info) 981{ 982int i; 983struct unpack_trees_options *o = info->data; 984unsigned long conflicts = info->df_conflicts | dirmask; 985 986/* Do we have *only* directories? Nothing to do */ 987if(mask == dirmask && !src[0]) 988return0; 989 990/* 991 * Ok, we've filled in up to any potential index entry in src[0], 992 * now do the rest. 993 */ 994for(i =0; i < n; i++) { 995int stage; 996unsigned int bit =1ul<< i; 997if(conflicts & bit) { 998 src[i + o->merge] = o->df_conflict_entry; 999continue;1000}1001if(!(mask & bit))1002continue;1003if(!o->merge)1004 stage =0;1005else if(i +1< o->head_idx)1006 stage =1;1007else if(i +1> o->head_idx)1008 stage =3;1009else1010 stage =2;10111012/*1013 * If the merge bit is set, then the cache entries are1014 * discarded in the following block. In this case,1015 * construct "transient" cache_entries, as they are1016 * not stored in the index. otherwise construct the1017 * cache entry from the index aware logic.1018 */1019 src[i + o->merge] =create_ce_entry(info, names + i, stage, &o->result, o->merge);1020}10211022if(o->merge) {1023int rc =call_unpack_fn((const struct cache_entry *const*)src,1024 o);1025for(i =0; i < n; i++) {1026struct cache_entry *ce = src[i + o->merge];1027if(ce != o->df_conflict_entry)1028discard_cache_entry(ce);1029}1030return rc;1031}10321033for(i =0; i < n; i++)1034if(src[i] && src[i] != o->df_conflict_entry)1035if(do_add_entry(o, src[i],0,0))1036return-1;10371038return0;1039}10401041static intunpack_failed(struct unpack_trees_options *o,const char*message)1042{1043discard_index(&o->result);1044if(!o->gently && !o->exiting_early) {1045if(message)1046returnerror("%s", message);1047return-1;1048}1049return-1;1050}10511052/*1053 * The tree traversal is looking at name p. If we have a matching entry,1054 * return it. If name p is a directory in the index, do not return1055 * anything, as we will want to match it when the traversal descends into1056 * the directory.1057 */1058static intfind_cache_pos(struct traverse_info *info,1059const struct name_entry *p)1060{1061int pos;1062struct unpack_trees_options *o = info->data;1063struct index_state *index = o->src_index;1064int pfxlen = info->pathlen;1065int p_len =tree_entry_len(p);10661067for(pos = o->cache_bottom; pos < index->cache_nr; pos++) {1068const struct cache_entry *ce = index->cache[pos];1069const char*ce_name, *ce_slash;1070int cmp, ce_len;10711072if(ce->ce_flags & CE_UNPACKED) {1073/*1074 * cache_bottom entry is already unpacked, so1075 * we can never match it; don't check it1076 * again.1077 */1078if(pos == o->cache_bottom)1079++o->cache_bottom;1080continue;1081}1082if(!ce_in_traverse_path(ce, info)) {1083/*1084 * Check if we can skip future cache checks1085 * (because we're already past all possible1086 * entries in the traverse path).1087 */1088if(info->traverse_path) {1089if(strncmp(ce->name, info->traverse_path,1090 info->pathlen) >0)1091break;1092}1093continue;1094}1095 ce_name = ce->name + pfxlen;1096 ce_slash =strchr(ce_name,'/');1097if(ce_slash)1098 ce_len = ce_slash - ce_name;1099else1100 ce_len =ce_namelen(ce) - pfxlen;1101 cmp =name_compare(p->path, p_len, ce_name, ce_len);1102/*1103 * Exact match; if we have a directory we need to1104 * delay returning it.1105 */1106if(!cmp)1107return ce_slash ? -2- pos : pos;1108if(0< cmp)1109continue;/* keep looking */1110/*1111 * ce_name sorts after p->path; could it be that we1112 * have files under p->path directory in the index?1113 * E.g. ce_name == "t-i", and p->path == "t"; we may1114 * have "t/a" in the index.1115 */1116if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&1117 ce_name[p_len] <'/')1118continue;/* keep looking */1119break;1120}1121return-1;1122}11231124static struct cache_entry *find_cache_entry(struct traverse_info *info,1125const struct name_entry *p)1126{1127int pos =find_cache_pos(info, p);1128struct unpack_trees_options *o = info->data;11291130if(0<= pos)1131return o->src_index->cache[pos];1132else1133return NULL;1134}11351136static voiddebug_path(struct traverse_info *info)1137{1138if(info->prev) {1139debug_path(info->prev);1140if(*info->prev->name.path)1141putchar('/');1142}1143printf("%s", info->name.path);1144}11451146static voiddebug_name_entry(int i,struct name_entry *n)1147{1148printf("ent#%d %06o%s\n", i,1149 n->path ? n->mode :0,1150 n->path ? n->path :"(missing)");1151}11521153static voiddebug_unpack_callback(int n,1154unsigned long mask,1155unsigned long dirmask,1156struct name_entry *names,1157struct traverse_info *info)1158{1159int i;1160printf("* unpack mask%lu, dirmask%lu, cnt%d",1161 mask, dirmask, n);1162debug_path(info);1163putchar('\n');1164for(i =0; i < n; i++)1165debug_name_entry(i, names + i);1166}11671168/*1169 * Note that traverse_by_cache_tree() duplicates some logic in this function1170 * without actually calling it. If you change the logic here you may need to1171 * check and change there as well.1172 */1173static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info)1174{1175struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1176struct unpack_trees_options *o = info->data;1177const struct name_entry *p = names;11781179/* Find first entry with a real name (we could use "mask" too) */1180while(!p->mode)1181 p++;11821183if(o->debug_unpack)1184debug_unpack_callback(n, mask, dirmask, names, info);11851186/* Are we supposed to look at the index too? */1187if(o->merge) {1188while(1) {1189int cmp;1190struct cache_entry *ce;11911192if(o->diff_index_cached)1193 ce =next_cache_entry(o);1194else1195 ce =find_cache_entry(info, p);11961197if(!ce)1198break;1199 cmp =compare_entry(ce, info, p);1200if(cmp <0) {1201if(unpack_index_entry(ce, o) <0)1202returnunpack_failed(o, NULL);1203continue;1204}1205if(!cmp) {1206if(ce_stage(ce)) {1207/*1208 * If we skip unmerged index1209 * entries, we'll skip this1210 * entry *and* the tree1211 * entries associated with it!1212 */1213if(o->skip_unmerged) {1214add_same_unmerged(ce, o);1215return mask;1216}1217}1218 src[0] = ce;1219}1220break;1221}1222}12231224if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1225return-1;12261227if(o->merge && src[0]) {1228if(ce_stage(src[0]))1229mark_ce_used_same_name(src[0], o);1230else1231mark_ce_used(src[0], o);1232}12331234/* Now handle any directories.. */1235if(dirmask) {1236/* special case: "diff-index --cached" looking at a tree */1237if(o->diff_index_cached &&1238 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1239int matches;1240 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1241 names, info);1242/*1243 * Everything under the name matches; skip the1244 * entire hierarchy. diff_index_cached codepath1245 * special cases D/F conflicts in such a way that1246 * it does not do any look-ahead, so this is safe.1247 */1248if(matches) {1249 o->cache_bottom += matches;1250return mask;1251}1252}12531254if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1255 names, info) <0)1256return-1;1257return mask;1258}12591260return mask;1261}12621263static intclear_ce_flags_1(struct index_state *istate,1264struct cache_entry **cache,int nr,1265struct strbuf *prefix,1266int select_mask,int clear_mask,1267struct exclude_list *el,int defval);12681269/* Whole directory matching */1270static intclear_ce_flags_dir(struct index_state *istate,1271struct cache_entry **cache,int nr,1272struct strbuf *prefix,1273char*basename,1274int select_mask,int clear_mask,1275struct exclude_list *el,int defval)1276{1277struct cache_entry **cache_end;1278int dtype = DT_DIR;1279int ret =is_excluded_from_list(prefix->buf, prefix->len,1280 basename, &dtype, el, istate);1281int rc;12821283strbuf_addch(prefix,'/');12841285/* If undecided, use matching result of parent dir in defval */1286if(ret <0)1287 ret = defval;12881289for(cache_end = cache; cache_end != cache + nr; cache_end++) {1290struct cache_entry *ce = *cache_end;1291if(strncmp(ce->name, prefix->buf, prefix->len))1292break;1293}12941295/*1296 * TODO: check el, if there are no patterns that may conflict1297 * with ret (iow, we know in advance the incl/excl1298 * decision for the entire directory), clear flag here without1299 * calling clear_ce_flags_1(). That function will call1300 * the expensive is_excluded_from_list() on every entry.1301 */1302 rc =clear_ce_flags_1(istate, cache, cache_end - cache,1303 prefix,1304 select_mask, clear_mask,1305 el, ret);1306strbuf_setlen(prefix, prefix->len -1);1307return rc;1308}13091310/*1311 * Traverse the index, find every entry that matches according to1312 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1313 * number of traversed entries.1314 *1315 * If select_mask is non-zero, only entries whose ce_flags has on of1316 * those bits enabled are traversed.1317 *1318 * cache : pointer to an index entry1319 * prefix_len : an offset to its path1320 *1321 * The current path ("prefix") including the trailing '/' is1322 * cache[0]->name[0..(prefix_len-1)]1323 * Top level path has prefix_len zero.1324 */1325static intclear_ce_flags_1(struct index_state *istate,1326struct cache_entry **cache,int nr,1327struct strbuf *prefix,1328int select_mask,int clear_mask,1329struct exclude_list *el,int defval)1330{1331struct cache_entry **cache_end = cache + nr;13321333/*1334 * Process all entries that have the given prefix and meet1335 * select_mask condition1336 */1337while(cache != cache_end) {1338struct cache_entry *ce = *cache;1339const char*name, *slash;1340int len, dtype, ret;13411342if(select_mask && !(ce->ce_flags & select_mask)) {1343 cache++;1344continue;1345}13461347if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1348break;13491350 name = ce->name + prefix->len;1351 slash =strchr(name,'/');13521353/* If it's a directory, try whole directory match first */1354if(slash) {1355int processed;13561357 len = slash - name;1358strbuf_add(prefix, name, len);13591360 processed =clear_ce_flags_dir(istate, cache, cache_end - cache,1361 prefix,1362 prefix->buf + prefix->len - len,1363 select_mask, clear_mask,1364 el, defval);13651366/* clear_c_f_dir eats a whole dir already? */1367if(processed) {1368 cache += processed;1369strbuf_setlen(prefix, prefix->len - len);1370continue;1371}13721373strbuf_addch(prefix,'/');1374 cache +=clear_ce_flags_1(istate, cache, cache_end - cache,1375 prefix,1376 select_mask, clear_mask, el, defval);1377strbuf_setlen(prefix, prefix->len - len -1);1378continue;1379}13801381/* Non-directory */1382 dtype =ce_to_dtype(ce);1383 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1384 name, &dtype, el, istate);1385if(ret <0)1386 ret = defval;1387if(ret >0)1388 ce->ce_flags &= ~clear_mask;1389 cache++;1390}1391return nr - (cache_end - cache);1392}13931394static intclear_ce_flags(struct index_state *istate,1395int select_mask,int clear_mask,1396struct exclude_list *el)1397{1398static struct strbuf prefix = STRBUF_INIT;13991400strbuf_reset(&prefix);14011402returnclear_ce_flags_1(istate,1403 istate->cache,1404 istate->cache_nr,1405&prefix,1406 select_mask, clear_mask,1407 el,0);1408}14091410/*1411 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1412 */1413static voidmark_new_skip_worktree(struct exclude_list *el,1414struct index_state *istate,1415int select_flag,int skip_wt_flag)1416{1417int i;14181419/*1420 * 1. Pretend the narrowest worktree: only unmerged entries1421 * are checked out1422 */1423for(i =0; i < istate->cache_nr; i++) {1424struct cache_entry *ce = istate->cache[i];14251426if(select_flag && !(ce->ce_flags & select_flag))1427continue;14281429if(!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))1430 ce->ce_flags |= skip_wt_flag;1431else1432 ce->ce_flags &= ~skip_wt_flag;1433}14341435/*1436 * 2. Widen worktree according to sparse-checkout file.1437 * Matched entries will have skip_wt_flag cleared (i.e. "in")1438 */1439clear_ce_flags(istate, select_flag, skip_wt_flag, el);1440}14411442static intverify_absent(const struct cache_entry *,1443enum unpack_trees_error_types,1444struct unpack_trees_options *);1445/*1446 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1447 * resulting index, -2 on failure to reflect the changes to the work tree.1448 *1449 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1450 */1451intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1452{1453int i, ret;1454static struct cache_entry *dfc;1455struct exclude_list el;14561457if(len > MAX_UNPACK_TREES)1458die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);14591460trace_performance_enter();1461memset(&el,0,sizeof(el));1462if(!core_apply_sparse_checkout || !o->update)1463 o->skip_sparse_checkout =1;1464if(!o->skip_sparse_checkout) {1465char*sparse =git_pathdup("info/sparse-checkout");1466if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1467 o->skip_sparse_checkout =1;1468else1469 o->el = ⪙1470free(sparse);1471}14721473memset(&o->result,0,sizeof(o->result));1474 o->result.initialized =1;1475 o->result.timestamp.sec = o->src_index->timestamp.sec;1476 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1477 o->result.version = o->src_index->version;1478if(!o->src_index->split_index) {1479 o->result.split_index = NULL;1480}else if(o->src_index == o->dst_index) {1481/*1482 * o->dst_index (and thus o->src_index) will be discarded1483 * and overwritten with o->result at the end of this function,1484 * so just use src_index's split_index to avoid having to1485 * create a new one.1486 */1487 o->result.split_index = o->src_index->split_index;1488 o->result.split_index->refcount++;1489}else{1490 o->result.split_index =init_split_index(&o->result);1491}1492oidcpy(&o->result.oid, &o->src_index->oid);1493 o->merge_size = len;1494mark_all_ce_unused(o->src_index);14951496/*1497 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1498 */1499if(!o->skip_sparse_checkout)1500mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);15011502if(!dfc)1503 dfc =xcalloc(1,cache_entry_size(0));1504 o->df_conflict_entry = dfc;15051506if(len) {1507const char*prefix = o->prefix ? o->prefix :"";1508struct traverse_info info;15091510setup_traverse_info(&info, prefix);1511 info.fn = unpack_callback;1512 info.data = o;1513 info.show_all_errors = o->show_all_errors;1514 info.pathspec = o->pathspec;15151516if(o->prefix) {1517/*1518 * Unpack existing index entries that sort before the1519 * prefix the tree is spliced into. Note that o->merge1520 * is always true in this case.1521 */1522while(1) {1523struct cache_entry *ce =next_cache_entry(o);1524if(!ce)1525break;1526if(ce_in_traverse_path(ce, &info))1527break;1528if(unpack_index_entry(ce, o) <0)1529goto return_failed;1530}1531}15321533trace_performance_enter();1534 ret =traverse_trees(len, t, &info);1535trace_performance_leave("traverse_trees");1536if(ret <0)1537goto return_failed;1538}15391540/* Any left-over entries in the index? */1541if(o->merge) {1542while(1) {1543struct cache_entry *ce =next_cache_entry(o);1544if(!ce)1545break;1546if(unpack_index_entry(ce, o) <0)1547goto return_failed;1548}1549}1550mark_all_ce_unused(o->src_index);15511552if(o->trivial_merges_only && o->nontrivial_merge) {1553 ret =unpack_failed(o,"Merge requires file-level merging");1554goto done;1555}15561557if(!o->skip_sparse_checkout) {1558int empty_worktree =1;15591560/*1561 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11562 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1563 * so apply_sparse_checkout() won't attempt to remove it from worktree1564 */1565mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);15661567 ret =0;1568for(i =0; i < o->result.cache_nr; i++) {1569struct cache_entry *ce = o->result.cache[i];15701571/*1572 * Entries marked with CE_ADDED in merged_entry() do not have1573 * verify_absent() check (the check is effectively disabled1574 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1575 *1576 * Do the real check now because we have had1577 * correct CE_NEW_SKIP_WORKTREE1578 */1579if(ce->ce_flags & CE_ADDED &&1580verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1581if(!o->show_all_errors)1582goto return_failed;1583 ret = -1;1584}15851586if(apply_sparse_checkout(&o->result, ce, o)) {1587if(!o->show_all_errors)1588goto return_failed;1589 ret = -1;1590}1591if(!ce_skip_worktree(ce))1592 empty_worktree =0;15931594}1595if(ret <0)1596goto return_failed;1597/*1598 * Sparse checkout is meant to narrow down checkout area1599 * but it does not make sense to narrow down to empty working1600 * tree. This is usually a mistake in sparse checkout rules.1601 * Do not allow users to do that.1602 */1603if(o->result.cache_nr && empty_worktree) {1604 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1605goto done;1606}1607}16081609 ret =check_updates(o) ? (-2) :0;1610if(o->dst_index) {1611move_index_extensions(&o->result, o->src_index);1612if(!ret) {1613if(git_env_bool("GIT_TEST_CHECK_CACHE_TREE",0))1614cache_tree_verify(&o->result);1615if(!o->result.cache_tree)1616 o->result.cache_tree =cache_tree();1617if(!cache_tree_fully_valid(o->result.cache_tree))1618cache_tree_update(&o->result,1619 WRITE_TREE_SILENT |1620 WRITE_TREE_REPAIR);1621}1622discard_index(o->dst_index);1623*o->dst_index = o->result;1624}else{1625discard_index(&o->result);1626}1627 o->src_index = NULL;16281629done:1630trace_performance_leave("unpack_trees");1631clear_exclude_list(&el);1632return ret;16331634return_failed:1635if(o->show_all_errors)1636display_error_msgs(o);1637mark_all_ce_unused(o->src_index);1638 ret =unpack_failed(o, NULL);1639if(o->exiting_early)1640 ret =0;1641goto done;1642}16431644/* Here come the merge functions */16451646static intreject_merge(const struct cache_entry *ce,1647struct unpack_trees_options *o)1648{1649return o->gently ? -1:1650add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1651}16521653static intsame(const struct cache_entry *a,const struct cache_entry *b)1654{1655if(!!a != !!b)1656return0;1657if(!a && !b)1658return1;1659if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1660return0;1661return a->ce_mode == b->ce_mode &&1662oideq(&a->oid, &b->oid);1663}166416651666/*1667 * When a CE gets turned into an unmerged entry, we1668 * want it to be up-to-date1669 */1670static intverify_uptodate_1(const struct cache_entry *ce,1671struct unpack_trees_options *o,1672enum unpack_trees_error_types error_type)1673{1674struct stat st;16751676if(o->index_only)1677return0;16781679/*1680 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1681 * if this entry is truly up-to-date because this file may be1682 * overwritten.1683 */1684if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1685;/* keep checking */1686else if(o->reset ||ce_uptodate(ce))1687return0;16881689if(!lstat(ce->name, &st)) {1690int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1691unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);16921693if(submodule_from_ce(ce)) {1694int r =check_submodule_move_head(ce,1695"HEAD",oid_to_hex(&ce->oid), o);1696if(r)1697return o->gently ? -1:1698add_rejected_path(o, error_type, ce->name);1699return0;1700}17011702if(!changed)1703return0;1704/*1705 * Historic default policy was to allow submodule to be out1706 * of sync wrt the superproject index. If the submodule was1707 * not considered interesting above, we don't care here.1708 */1709if(S_ISGITLINK(ce->ce_mode))1710return0;17111712 errno =0;1713}1714if(errno == ENOENT)1715return0;1716return o->gently ? -1:1717add_rejected_path(o, error_type, ce->name);1718}17191720intverify_uptodate(const struct cache_entry *ce,1721struct unpack_trees_options *o)1722{1723if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1724return0;1725returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1726}17271728static intverify_uptodate_sparse(const struct cache_entry *ce,1729struct unpack_trees_options *o)1730{1731returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1732}17331734/*1735 * TODO: We should actually invalidate o->result, not src_index [1].1736 * But since cache tree and untracked cache both are not copied to1737 * o->result until unpacking is complete, we invalidate them on1738 * src_index instead with the assumption that they will be copied to1739 * dst_index at the end.1740 *1741 * [1] src_index->cache_tree is also used in unpack_callback() so if1742 * we invalidate o->result, we need to update it to use1743 * o->result.cache_tree as well.1744 */1745static voidinvalidate_ce_path(const struct cache_entry *ce,1746struct unpack_trees_options *o)1747{1748if(!ce)1749return;1750cache_tree_invalidate_path(o->src_index, ce->name);1751untracked_cache_invalidate_path(o->src_index, ce->name,1);1752}17531754/*1755 * Check that checking out ce->sha1 in subdir ce->name is not1756 * going to overwrite any working files.1757 *1758 * Currently, git does not checkout subprojects during a superproject1759 * checkout, so it is not going to overwrite anything.1760 */1761static intverify_clean_submodule(const char*old_sha1,1762const struct cache_entry *ce,1763enum unpack_trees_error_types error_type,1764struct unpack_trees_options *o)1765{1766if(!submodule_from_ce(ce))1767return0;17681769returncheck_submodule_move_head(ce, old_sha1,1770oid_to_hex(&ce->oid), o);1771}17721773static intverify_clean_subdirectory(const struct cache_entry *ce,1774enum unpack_trees_error_types error_type,1775struct unpack_trees_options *o)1776{1777/*1778 * we are about to extract "ce->name"; we would not want to lose1779 * anything in the existing directory there.1780 */1781int namelen;1782int i;1783struct dir_struct d;1784char*pathbuf;1785int cnt =0;17861787if(S_ISGITLINK(ce->ce_mode)) {1788struct object_id oid;1789int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1790/*1791 * If we are not going to update the submodule, then1792 * we don't care.1793 */1794if(!sub_head &&oideq(&oid, &ce->oid))1795return0;1796returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1797 ce, error_type, o);1798}17991800/*1801 * First let's make sure we do not have a local modification1802 * in that directory.1803 */1804 namelen =ce_namelen(ce);1805for(i =locate_in_src_index(ce, o);1806 i < o->src_index->cache_nr;1807 i++) {1808struct cache_entry *ce2 = o->src_index->cache[i];1809int len =ce_namelen(ce2);1810if(len < namelen ||1811strncmp(ce->name, ce2->name, namelen) ||1812 ce2->name[namelen] !='/')1813break;1814/*1815 * ce2->name is an entry in the subdirectory to be1816 * removed.1817 */1818if(!ce_stage(ce2)) {1819if(verify_uptodate(ce2, o))1820return-1;1821add_entry(o, ce2, CE_REMOVE,0);1822invalidate_ce_path(ce, o);1823mark_ce_used(ce2, o);1824}1825 cnt++;1826}18271828/*1829 * Then we need to make sure that we do not lose a locally1830 * present file that is not ignored.1831 */1832 pathbuf =xstrfmt("%.*s/", namelen, ce->name);18331834memset(&d,0,sizeof(d));1835if(o->dir)1836 d.exclude_per_dir = o->dir->exclude_per_dir;1837 i =read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);1838if(i)1839return o->gently ? -1:1840add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1841free(pathbuf);1842return cnt;1843}18441845/*1846 * This gets called when there was no index entry for the tree entry 'dst',1847 * but we found a file in the working tree that 'lstat()' said was fine,1848 * and we're on a case-insensitive filesystem.1849 *1850 * See if we can find a case-insensitive match in the index that also1851 * matches the stat information, and assume it's that other file!1852 */1853static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1854{1855const struct cache_entry *src;18561857 src =index_file_exists(o->src_index, name, len,1);1858return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1859}18601861static intcheck_ok_to_remove(const char*name,int len,int dtype,1862const struct cache_entry *ce,struct stat *st,1863enum unpack_trees_error_types error_type,1864struct unpack_trees_options *o)1865{1866const struct cache_entry *result;18671868/*1869 * It may be that the 'lstat()' succeeded even though1870 * target 'ce' was absent, because there is an old1871 * entry that is different only in case..1872 *1873 * Ignore that lstat() if it matches.1874 */1875if(ignore_case &&icase_exists(o, name, len, st))1876return0;18771878if(o->dir &&1879is_excluded(o->dir, o->src_index, name, &dtype))1880/*1881 * ce->name is explicitly excluded, so it is Ok to1882 * overwrite it.1883 */1884return0;1885if(S_ISDIR(st->st_mode)) {1886/*1887 * We are checking out path "foo" and1888 * found "foo/." in the working tree.1889 * This is tricky -- if we have modified1890 * files that are in "foo/" we would lose1891 * them.1892 */1893if(verify_clean_subdirectory(ce, error_type, o) <0)1894return-1;1895return0;1896}18971898/*1899 * The previous round may already have decided to1900 * delete this path, which is in a subdirectory that1901 * is being replaced with a blob.1902 */1903 result =index_file_exists(&o->result, name, len,0);1904if(result) {1905if(result->ce_flags & CE_REMOVE)1906return0;1907}19081909return o->gently ? -1:1910add_rejected_path(o, error_type, name);1911}19121913/*1914 * We do not want to remove or overwrite a working tree file that1915 * is not tracked, unless it is ignored.1916 */1917static intverify_absent_1(const struct cache_entry *ce,1918enum unpack_trees_error_types error_type,1919struct unpack_trees_options *o)1920{1921int len;1922struct stat st;19231924if(o->index_only || o->reset || !o->update)1925return0;19261927 len =check_leading_path(ce->name,ce_namelen(ce));1928if(!len)1929return0;1930else if(len >0) {1931char*path;1932int ret;19331934 path =xmemdupz(ce->name, len);1935if(lstat(path, &st))1936 ret =error_errno("cannot stat '%s'", path);1937else{1938if(submodule_from_ce(ce))1939 ret =check_submodule_move_head(ce,1940oid_to_hex(&ce->oid),1941 NULL, o);1942else1943 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1944&st, error_type, o);1945}1946free(path);1947return ret;1948}else if(lstat(ce->name, &st)) {1949if(errno != ENOENT)1950returnerror_errno("cannot stat '%s'", ce->name);1951return0;1952}else{1953if(submodule_from_ce(ce))1954returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1955 NULL, o);19561957returncheck_ok_to_remove(ce->name,ce_namelen(ce),1958ce_to_dtype(ce), ce, &st,1959 error_type, o);1960}1961}19621963static intverify_absent(const struct cache_entry *ce,1964enum unpack_trees_error_types error_type,1965struct unpack_trees_options *o)1966{1967if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1968return0;1969returnverify_absent_1(ce, error_type, o);1970}19711972static intverify_absent_sparse(const struct cache_entry *ce,1973enum unpack_trees_error_types error_type,1974struct unpack_trees_options *o)1975{1976enum unpack_trees_error_types orphaned_error = error_type;1977if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1978 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;19791980returnverify_absent_1(ce, orphaned_error, o);1981}19821983static intmerged_entry(const struct cache_entry *ce,1984const struct cache_entry *old,1985struct unpack_trees_options *o)1986{1987int update = CE_UPDATE;1988struct cache_entry *merge =dup_cache_entry(ce, &o->result);19891990if(!old) {1991/*1992 * New index entries. In sparse checkout, the following1993 * verify_absent() will be delayed until after1994 * traverse_trees() finishes in unpack_trees(), then:1995 *1996 * - CE_NEW_SKIP_WORKTREE will be computed correctly1997 * - verify_absent() be called again, this time with1998 * correct CE_NEW_SKIP_WORKTREE1999 *2000 * verify_absent() call here does nothing in sparse2001 * checkout (i.e. o->skip_sparse_checkout == 0)2002 */2003 update |= CE_ADDED;2004 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;20052006if(verify_absent(merge,2007 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {2008discard_cache_entry(merge);2009return-1;2010}2011invalidate_ce_path(merge, o);20122013if(submodule_from_ce(ce)) {2014int ret =check_submodule_move_head(ce, NULL,2015oid_to_hex(&ce->oid),2016 o);2017if(ret)2018return ret;2019}20202021}else if(!(old->ce_flags & CE_CONFLICTED)) {2022/*2023 * See if we can re-use the old CE directly?2024 * That way we get the uptodate stat info.2025 *2026 * This also removes the UPDATE flag on a match; otherwise2027 * we will end up overwriting local changes in the work tree.2028 */2029if(same(old, merge)) {2030copy_cache_entry(merge, old);2031 update =0;2032}else{2033if(verify_uptodate(old, o)) {2034discard_cache_entry(merge);2035return-1;2036}2037/* Migrate old flags over */2038 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);2039invalidate_ce_path(old, o);2040}20412042if(submodule_from_ce(ce)) {2043int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),2044oid_to_hex(&ce->oid),2045 o);2046if(ret)2047return ret;2048}2049}else{2050/*2051 * Previously unmerged entry left as an existence2052 * marker by read_index_unmerged();2053 */2054invalidate_ce_path(old, o);2055}20562057do_add_entry(o, merge, update, CE_STAGEMASK);2058return1;2059}20602061static intdeleted_entry(const struct cache_entry *ce,2062const struct cache_entry *old,2063struct unpack_trees_options *o)2064{2065/* Did it exist in the index? */2066if(!old) {2067if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2068return-1;2069return0;2070}2071if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))2072return-1;2073add_entry(o, ce, CE_REMOVE,0);2074invalidate_ce_path(ce, o);2075return1;2076}20772078static intkeep_entry(const struct cache_entry *ce,2079struct unpack_trees_options *o)2080{2081add_entry(o, ce,0,0);2082if(ce_stage(ce))2083invalidate_ce_path(ce, o);2084return1;2085}20862087#if DBRT_DEBUG2088static voidshow_stage_entry(FILE*o,2089const char*label,const struct cache_entry *ce)2090{2091if(!ce)2092fprintf(o,"%s(missing)\n", label);2093else2094fprintf(o,"%s%06o%s %d\t%s\n",2095 label,2096 ce->ce_mode,2097oid_to_hex(&ce->oid),2098ce_stage(ce),2099 ce->name);2100}2101#endif21022103intthreeway_merge(const struct cache_entry *const*stages,2104struct unpack_trees_options *o)2105{2106const struct cache_entry *index;2107const struct cache_entry *head;2108const struct cache_entry *remote = stages[o->head_idx +1];2109int count;2110int head_match =0;2111int remote_match =0;21122113int df_conflict_head =0;2114int df_conflict_remote =0;21152116int any_anc_missing =0;2117int no_anc_exists =1;2118int i;21192120for(i =1; i < o->head_idx; i++) {2121if(!stages[i] || stages[i] == o->df_conflict_entry)2122 any_anc_missing =1;2123else2124 no_anc_exists =0;2125}21262127 index = stages[0];2128 head = stages[o->head_idx];21292130if(head == o->df_conflict_entry) {2131 df_conflict_head =1;2132 head = NULL;2133}21342135if(remote == o->df_conflict_entry) {2136 df_conflict_remote =1;2137 remote = NULL;2138}21392140/*2141 * First, if there's a #16 situation, note that to prevent #132142 * and #14.2143 */2144if(!same(remote, head)) {2145for(i =1; i < o->head_idx; i++) {2146if(same(stages[i], head)) {2147 head_match = i;2148}2149if(same(stages[i], remote)) {2150 remote_match = i;2151}2152}2153}21542155/*2156 * We start with cases where the index is allowed to match2157 * something other than the head: #14(ALT) and #2ALT, where it2158 * is permitted to match the result instead.2159 */2160/* #14, #14ALT, #2ALT */2161if(remote && !df_conflict_head && head_match && !remote_match) {2162if(index && !same(index, remote) && !same(index, head))2163returnreject_merge(index, o);2164returnmerged_entry(remote, index, o);2165}2166/*2167 * If we have an entry in the index cache, then we want to2168 * make sure that it matches head.2169 */2170if(index && !same(index, head))2171returnreject_merge(index, o);21722173if(head) {2174/* #5ALT, #15 */2175if(same(head, remote))2176returnmerged_entry(head, index, o);2177/* #13, #3ALT */2178if(!df_conflict_remote && remote_match && !head_match)2179returnmerged_entry(head, index, o);2180}21812182/* #1 */2183if(!head && !remote && any_anc_missing)2184return0;21852186/*2187 * Under the "aggressive" rule, we resolve mostly trivial2188 * cases that we historically had git-merge-one-file resolve.2189 */2190if(o->aggressive) {2191int head_deleted = !head;2192int remote_deleted = !remote;2193const struct cache_entry *ce = NULL;21942195if(index)2196 ce = index;2197else if(head)2198 ce = head;2199else if(remote)2200 ce = remote;2201else{2202for(i =1; i < o->head_idx; i++) {2203if(stages[i] && stages[i] != o->df_conflict_entry) {2204 ce = stages[i];2205break;2206}2207}2208}22092210/*2211 * Deleted in both.2212 * Deleted in one and unchanged in the other.2213 */2214if((head_deleted && remote_deleted) ||2215(head_deleted && remote && remote_match) ||2216(remote_deleted && head && head_match)) {2217if(index)2218returndeleted_entry(index, index, o);2219if(ce && !head_deleted) {2220if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2221return-1;2222}2223return0;2224}2225/*2226 * Added in both, identically.2227 */2228if(no_anc_exists && head && remote &&same(head, remote))2229returnmerged_entry(head, index, o);22302231}22322233/* Below are "no merge" cases, which require that the index be2234 * up-to-date to avoid the files getting overwritten with2235 * conflict resolution files.2236 */2237if(index) {2238if(verify_uptodate(index, o))2239return-1;2240}22412242 o->nontrivial_merge =1;22432244/* #2, #3, #4, #6, #7, #9, #10, #11. */2245 count =0;2246if(!head_match || !remote_match) {2247for(i =1; i < o->head_idx; i++) {2248if(stages[i] && stages[i] != o->df_conflict_entry) {2249keep_entry(stages[i], o);2250 count++;2251break;2252}2253}2254}2255#if DBRT_DEBUG2256else{2257fprintf(stderr,"read-tree: warning #16 detected\n");2258show_stage_entry(stderr,"head ", stages[head_match]);2259show_stage_entry(stderr,"remote ", stages[remote_match]);2260}2261#endif2262if(head) { count +=keep_entry(head, o); }2263if(remote) { count +=keep_entry(remote, o); }2264return count;2265}22662267/*2268 * Two-way merge.2269 *2270 * The rule is to "carry forward" what is in the index without losing2271 * information across a "fast-forward", favoring a successful merge2272 * over a merge failure when it makes sense. For details of the2273 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2274 *2275 */2276inttwoway_merge(const struct cache_entry *const*src,2277struct unpack_trees_options *o)2278{2279const struct cache_entry *current = src[0];2280const struct cache_entry *oldtree = src[1];2281const struct cache_entry *newtree = src[2];22822283if(o->merge_size !=2)2284returnerror("Cannot do a twoway merge of%dtrees",2285 o->merge_size);22862287if(oldtree == o->df_conflict_entry)2288 oldtree = NULL;2289if(newtree == o->df_conflict_entry)2290 newtree = NULL;22912292if(current) {2293if(current->ce_flags & CE_CONFLICTED) {2294if(same(oldtree, newtree) || o->reset) {2295if(!newtree)2296returndeleted_entry(current, current, o);2297else2298returnmerged_entry(newtree, current, o);2299}2300returnreject_merge(current, o);2301}else if((!oldtree && !newtree) ||/* 4 and 5 */2302(!oldtree && newtree &&2303same(current, newtree)) ||/* 6 and 7 */2304(oldtree && newtree &&2305same(oldtree, newtree)) ||/* 14 and 15 */2306(oldtree && newtree &&2307!same(oldtree, newtree) &&/* 18 and 19 */2308same(current, newtree))) {2309returnkeep_entry(current, o);2310}else if(oldtree && !newtree &&same(current, oldtree)) {2311/* 10 or 11 */2312returndeleted_entry(oldtree, current, o);2313}else if(oldtree && newtree &&2314same(current, oldtree) && !same(current, newtree)) {2315/* 20 or 21 */2316returnmerged_entry(newtree, current, o);2317}else2318returnreject_merge(current, o);2319}2320else if(newtree) {2321if(oldtree && !o->initial_checkout) {2322/*2323 * deletion of the path was staged;2324 */2325if(same(oldtree, newtree))2326return1;2327returnreject_merge(oldtree, o);2328}2329returnmerged_entry(newtree, current, o);2330}2331returndeleted_entry(oldtree, current, o);2332}23332334/*2335 * Bind merge.2336 *2337 * Keep the index entries at stage0, collapse stage1 but make sure2338 * stage0 does not have anything there.2339 */2340intbind_merge(const struct cache_entry *const*src,2341struct unpack_trees_options *o)2342{2343const struct cache_entry *old = src[0];2344const struct cache_entry *a = src[1];23452346if(o->merge_size !=1)2347returnerror("Cannot do a bind merge of%dtrees",2348 o->merge_size);2349if(a && old)2350return o->gently ? -1:2351error(ERRORMSG(o, ERROR_BIND_OVERLAP),2352super_prefixed(a->name),2353super_prefixed(old->name));2354if(!a)2355returnkeep_entry(old, o);2356else2357returnmerged_entry(a, NULL, o);2358}23592360/*2361 * One-way merge.2362 *2363 * The rule is:2364 * - take the stat information from stage0, take the data from stage12365 */2366intoneway_merge(const struct cache_entry *const*src,2367struct unpack_trees_options *o)2368{2369const struct cache_entry *old = src[0];2370const struct cache_entry *a = src[1];23712372if(o->merge_size !=1)2373returnerror("Cannot do a oneway merge of%dtrees",2374 o->merge_size);23752376if(!a || a == o->df_conflict_entry)2377returndeleted_entry(old, old, o);23782379if(old &&same(old, a)) {2380int update =0;2381if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2382struct stat st;2383if(lstat(old->name, &st) ||2384ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2385 update |= CE_UPDATE;2386}2387if(o->update &&S_ISGITLINK(old->ce_mode) &&2388should_update_submodules() && !verify_uptodate(old, o))2389 update |= CE_UPDATE;2390add_entry(o, old, update,0);2391return0;2392}2393returnmerged_entry(a, old, o);2394}