1#include"cache.h" 2#include"argv-array.h" 3#include"repository.h" 4#include"config.h" 5#include"dir.h" 6#include"tree.h" 7#include"tree-walk.h" 8#include"cache-tree.h" 9#include"unpack-trees.h" 10#include"progress.h" 11#include"refs.h" 12#include"attr.h" 13#include"split-index.h" 14#include"dir.h" 15#include"submodule.h" 16#include"submodule-config.h" 17#include"fsmonitor.h" 18#include"object-store.h" 19#include"fetch-object.h" 20 21/* 22 * Error messages expected by scripts out of plumbing commands such as 23 * read-tree. Non-scripted Porcelain is not required to use these messages 24 * and in fact are encouraged to reword them to better suit their particular 25 * situation better. See how "git checkout" and "git merge" replaces 26 * them using setup_unpack_trees_porcelain(), for example. 27 */ 28static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 29/* ERROR_WOULD_OVERWRITE */ 30"Entry '%s' would be overwritten by merge. Cannot merge.", 31 32/* ERROR_NOT_UPTODATE_FILE */ 33"Entry '%s' not uptodate. Cannot merge.", 34 35/* ERROR_NOT_UPTODATE_DIR */ 36"Updating '%s' would lose untracked files in it", 37 38/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 39"Untracked working tree file '%s' would be overwritten by merge.", 40 41/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 42"Untracked working tree file '%s' would be removed by merge.", 43 44/* ERROR_BIND_OVERLAP */ 45"Entry '%s' overlaps with '%s'. Cannot bind.", 46 47/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 48"Entry '%s' not uptodate. Cannot update sparse checkout.", 49 50/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 51"Working tree file '%s' would be overwritten by sparse checkout update.", 52 53/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 54"Working tree file '%s' would be removed by sparse checkout update.", 55 56/* ERROR_WOULD_LOSE_SUBMODULE */ 57"Submodule '%s' cannot checkout new HEAD.", 58}; 59 60#define ERRORMSG(o,type) \ 61 ( ((o) && (o)->msgs[(type)]) \ 62 ? ((o)->msgs[(type)]) \ 63 : (unpack_plumbing_errors[(type)]) ) 64 65static const char*super_prefixed(const char*path) 66{ 67/* 68 * It is necessary and sufficient to have two static buffers 69 * here, as the return value of this function is fed to 70 * error() using the unpack_*_errors[] templates we see above. 71 */ 72static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 73static int super_prefix_len = -1; 74static unsigned idx =ARRAY_SIZE(buf) -1; 75 76if(super_prefix_len <0) { 77const char*super_prefix =get_super_prefix(); 78if(!super_prefix) { 79 super_prefix_len =0; 80}else{ 81int i; 82for(i =0; i <ARRAY_SIZE(buf); i++) 83strbuf_addstr(&buf[i], super_prefix); 84 super_prefix_len = buf[0].len; 85} 86} 87 88if(!super_prefix_len) 89return path; 90 91if(++idx >=ARRAY_SIZE(buf)) 92 idx =0; 93 94strbuf_setlen(&buf[idx], super_prefix_len); 95strbuf_addstr(&buf[idx], path); 96 97return buf[idx].buf; 98} 99 100voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 101const char*cmd) 102{ 103int i; 104const char**msgs = opts->msgs; 105const char*msg; 106 107argv_array_init(&opts->msgs_to_free); 108 109if(!strcmp(cmd,"checkout")) 110 msg = advice_commit_before_merge 111?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 112"Please commit your changes or stash them before you switch branches.") 113:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 114else if(!strcmp(cmd,"merge")) 115 msg = advice_commit_before_merge 116?_("Your local changes to the following files would be overwritten by merge:\n%%s" 117"Please commit your changes or stash them before you merge.") 118:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 119else 120 msg = advice_commit_before_merge 121?_("Your local changes to the following files would be overwritten by%s:\n%%s" 122"Please commit your changes or stash them before you%s.") 123:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 124 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 125argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 126 127 msgs[ERROR_NOT_UPTODATE_DIR] = 128_("Updating the following directories would lose untracked files in them:\n%s"); 129 130if(!strcmp(cmd,"checkout")) 131 msg = advice_commit_before_merge 132?_("The following untracked working tree files would be removed by checkout:\n%%s" 133"Please move or remove them before you switch branches.") 134:_("The following untracked working tree files would be removed by checkout:\n%%s"); 135else if(!strcmp(cmd,"merge")) 136 msg = advice_commit_before_merge 137?_("The following untracked working tree files would be removed by merge:\n%%s" 138"Please move or remove them before you merge.") 139:_("The following untracked working tree files would be removed by merge:\n%%s"); 140else 141 msg = advice_commit_before_merge 142?_("The following untracked working tree files would be removed by%s:\n%%s" 143"Please move or remove them before you%s.") 144:_("The following untracked working tree files would be removed by%s:\n%%s"); 145 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = 146argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 147 148if(!strcmp(cmd,"checkout")) 149 msg = advice_commit_before_merge 150?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 151"Please move or remove them before you switch branches.") 152:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 153else if(!strcmp(cmd,"merge")) 154 msg = advice_commit_before_merge 155?_("The following untracked working tree files would be overwritten by merge:\n%%s" 156"Please move or remove them before you merge.") 157:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 158else 159 msg = advice_commit_before_merge 160?_("The following untracked working tree files would be overwritten by%s:\n%%s" 161"Please move or remove them before you%s.") 162:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 163 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = 164argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 165 166/* 167 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 168 * cannot easily display it as a list. 169 */ 170 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 171 172 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 173_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 174 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 175_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 176 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 177_("The following working tree files would be removed by sparse checkout update:\n%s"); 178 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 179_("Cannot update submodule:\n%s"); 180 181 opts->show_all_errors =1; 182/* rejected paths may not have a static buffer */ 183for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 184 opts->unpack_rejects[i].strdup_strings =1; 185} 186 187voidclear_unpack_trees_porcelain(struct unpack_trees_options *opts) 188{ 189argv_array_clear(&opts->msgs_to_free); 190memset(opts->msgs,0,sizeof(opts->msgs)); 191} 192 193static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 194unsigned int set,unsigned int clear) 195{ 196 clear |= CE_HASHED; 197 198if(set & CE_REMOVE) 199 set |= CE_WT_REMOVE; 200 201 ce->ce_flags = (ce->ce_flags & ~clear) | set; 202returnadd_index_entry(&o->result, ce, 203 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 204} 205 206static voidadd_entry(struct unpack_trees_options *o, 207const struct cache_entry *ce, 208unsigned int set,unsigned int clear) 209{ 210do_add_entry(o,dup_cache_entry(ce, &o->result), set, clear); 211} 212 213/* 214 * add error messages on path <path> 215 * corresponding to the type <e> with the message <msg> 216 * indicating if it should be display in porcelain or not 217 */ 218static intadd_rejected_path(struct unpack_trees_options *o, 219enum unpack_trees_error_types e, 220const char*path) 221{ 222if(!o->show_all_errors) 223returnerror(ERRORMSG(o, e),super_prefixed(path)); 224 225/* 226 * Otherwise, insert in a list for future display by 227 * display_error_msgs() 228 */ 229string_list_append(&o->unpack_rejects[e], path); 230return-1; 231} 232 233/* 234 * display all the error messages stored in a nice way 235 */ 236static voiddisplay_error_msgs(struct unpack_trees_options *o) 237{ 238int e, i; 239int something_displayed =0; 240for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 241struct string_list *rejects = &o->unpack_rejects[e]; 242if(rejects->nr >0) { 243struct strbuf path = STRBUF_INIT; 244 something_displayed =1; 245for(i =0; i < rejects->nr; i++) 246strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 247error(ERRORMSG(o, e),super_prefixed(path.buf)); 248strbuf_release(&path); 249} 250string_list_clear(rejects,0); 251} 252if(something_displayed) 253fprintf(stderr,_("Aborting\n")); 254} 255 256static intcheck_submodule_move_head(const struct cache_entry *ce, 257const char*old_id, 258const char*new_id, 259struct unpack_trees_options *o) 260{ 261unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 262const struct submodule *sub =submodule_from_ce(ce); 263 264if(!sub) 265return0; 266 267if(o->reset) 268 flags |= SUBMODULE_MOVE_HEAD_FORCE; 269 270if(submodule_move_head(ce->name, old_id, new_id, flags)) 271return o->gently ? -1: 272add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 273return0; 274} 275 276/* 277 * Preform the loading of the repository's gitmodules file. This function is 278 * used by 'check_update()' to perform loading of the gitmodules file in two 279 * differnt situations: 280 * (1) before removing entries from the working tree if the gitmodules file has 281 * been marked for removal. This situation is specified by 'state' == NULL. 282 * (2) before checking out entries to the working tree if the gitmodules file 283 * has been marked for update. This situation is specified by 'state' != NULL. 284 */ 285static voidload_gitmodules_file(struct index_state *index, 286struct checkout *state) 287{ 288int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 289 290if(pos >=0) { 291struct cache_entry *ce = index->cache[pos]; 292if(!state && ce->ce_flags & CE_WT_REMOVE) { 293repo_read_gitmodules(the_repository); 294}else if(state && (ce->ce_flags & CE_UPDATE)) { 295submodule_free(the_repository); 296checkout_entry(ce, state, NULL, NULL); 297repo_read_gitmodules(the_repository); 298} 299} 300} 301 302static struct progress *get_progress(struct unpack_trees_options *o) 303{ 304unsigned cnt =0, total =0; 305struct index_state *index = &o->result; 306 307if(!o->update || !o->verbose_update) 308return NULL; 309 310for(; cnt < index->cache_nr; cnt++) { 311const struct cache_entry *ce = index->cache[cnt]; 312if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 313 total++; 314} 315 316returnstart_delayed_progress(_("Checking out files"), total); 317} 318 319static voidsetup_collided_checkout_detection(struct checkout *state, 320struct index_state *index) 321{ 322int i; 323 324 state->clone =1; 325for(i =0; i < index->cache_nr; i++) 326 index->cache[i]->ce_flags &= ~CE_MATCHED; 327} 328 329static voidreport_collided_checkout(struct index_state *index) 330{ 331struct string_list list = STRING_LIST_INIT_NODUP; 332int i; 333 334for(i =0; i < index->cache_nr; i++) { 335struct cache_entry *ce = index->cache[i]; 336 337if(!(ce->ce_flags & CE_MATCHED)) 338continue; 339 340string_list_append(&list, ce->name); 341 ce->ce_flags &= ~CE_MATCHED; 342} 343 344 list.cmp = fspathcmp; 345string_list_sort(&list); 346 347if(list.nr) { 348warning(_("the following paths have collided (e.g. case-sensitive paths\n" 349"on a case-insensitive filesystem) and only one from the same\n" 350"colliding group is in the working tree:\n")); 351 352for(i =0; i < list.nr; i++) 353fprintf(stderr," '%s'\n", list.items[i].string); 354} 355 356string_list_clear(&list,0); 357} 358 359static intcheck_updates(struct unpack_trees_options *o) 360{ 361unsigned cnt =0; 362int errs =0; 363struct progress *progress; 364struct index_state *index = &o->result; 365struct checkout state = CHECKOUT_INIT; 366int i; 367 368trace_performance_enter(); 369 state.force =1; 370 state.quiet =1; 371 state.refresh_cache =1; 372 state.istate = index; 373 374if(o->clone) 375setup_collided_checkout_detection(&state, index); 376 377 progress =get_progress(o); 378 379if(o->update) 380git_attr_set_direction(GIT_ATTR_CHECKOUT); 381 382if(should_update_submodules() && o->update && !o->dry_run) 383load_gitmodules_file(index, NULL); 384 385for(i =0; i < index->cache_nr; i++) { 386const struct cache_entry *ce = index->cache[i]; 387 388if(ce->ce_flags & CE_WT_REMOVE) { 389display_progress(progress, ++cnt); 390if(o->update && !o->dry_run) 391unlink_entry(ce); 392} 393} 394remove_marked_cache_entries(index,0); 395remove_scheduled_dirs(); 396 397if(should_update_submodules() && o->update && !o->dry_run) 398load_gitmodules_file(index, &state); 399 400enable_delayed_checkout(&state); 401if(repository_format_partial_clone && o->update && !o->dry_run) { 402/* 403 * Prefetch the objects that are to be checked out in the loop 404 * below. 405 */ 406struct oid_array to_fetch = OID_ARRAY_INIT; 407int fetch_if_missing_store = fetch_if_missing; 408 fetch_if_missing =0; 409for(i =0; i < index->cache_nr; i++) { 410struct cache_entry *ce = index->cache[i]; 411if((ce->ce_flags & CE_UPDATE) && 412!S_ISGITLINK(ce->ce_mode)) { 413if(!has_object_file(&ce->oid)) 414oid_array_append(&to_fetch, &ce->oid); 415} 416} 417if(to_fetch.nr) 418fetch_objects(repository_format_partial_clone, 419 to_fetch.oid, to_fetch.nr); 420 fetch_if_missing = fetch_if_missing_store; 421oid_array_clear(&to_fetch); 422} 423for(i =0; i < index->cache_nr; i++) { 424struct cache_entry *ce = index->cache[i]; 425 426if(ce->ce_flags & CE_UPDATE) { 427if(ce->ce_flags & CE_WT_REMOVE) 428BUG("both update and delete flags are set on%s", 429 ce->name); 430display_progress(progress, ++cnt); 431 ce->ce_flags &= ~CE_UPDATE; 432if(o->update && !o->dry_run) { 433 errs |=checkout_entry(ce, &state, NULL, NULL); 434} 435} 436} 437stop_progress(&progress); 438 errs |=finish_delayed_checkout(&state, NULL); 439if(o->update) 440git_attr_set_direction(GIT_ATTR_CHECKIN); 441 442if(o->clone) 443report_collided_checkout(index); 444 445trace_performance_leave("check_updates"); 446return errs !=0; 447} 448 449static intverify_uptodate_sparse(const struct cache_entry *ce, 450struct unpack_trees_options *o); 451static intverify_absent_sparse(const struct cache_entry *ce, 452enum unpack_trees_error_types, 453struct unpack_trees_options *o); 454 455static intapply_sparse_checkout(struct index_state *istate, 456struct cache_entry *ce, 457struct unpack_trees_options *o) 458{ 459int was_skip_worktree =ce_skip_worktree(ce); 460 461if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 462 ce->ce_flags |= CE_SKIP_WORKTREE; 463else 464 ce->ce_flags &= ~CE_SKIP_WORKTREE; 465if(was_skip_worktree !=ce_skip_worktree(ce)) { 466 ce->ce_flags |= CE_UPDATE_IN_BASE; 467mark_fsmonitor_invalid(istate, ce); 468 istate->cache_changed |= CE_ENTRY_CHANGED; 469} 470 471/* 472 * if (!was_skip_worktree && !ce_skip_worktree()) { 473 * This is perfectly normal. Move on; 474 * } 475 */ 476 477/* 478 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 479 * area as a result of ce_skip_worktree() shortcuts in 480 * verify_absent() and verify_uptodate(). 481 * Make sure they don't modify worktree if they are already 482 * outside checkout area 483 */ 484if(was_skip_worktree &&ce_skip_worktree(ce)) { 485 ce->ce_flags &= ~CE_UPDATE; 486 487/* 488 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 489 * on to get that file removed from both index and worktree. 490 * If that file is already outside worktree area, don't 491 * bother remove it. 492 */ 493if(ce->ce_flags & CE_REMOVE) 494 ce->ce_flags &= ~CE_WT_REMOVE; 495} 496 497if(!was_skip_worktree &&ce_skip_worktree(ce)) { 498/* 499 * If CE_UPDATE is set, verify_uptodate() must be called already 500 * also stat info may have lost after merged_entry() so calling 501 * verify_uptodate() again may fail 502 */ 503if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 504return-1; 505 ce->ce_flags |= CE_WT_REMOVE; 506 ce->ce_flags &= ~CE_UPDATE; 507} 508if(was_skip_worktree && !ce_skip_worktree(ce)) { 509if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 510return-1; 511 ce->ce_flags |= CE_UPDATE; 512} 513return0; 514} 515 516staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 517struct unpack_trees_options *o) 518{ 519int ret = o->fn(src, o); 520if(ret >0) 521 ret =0; 522return ret; 523} 524 525static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 526{ 527 ce->ce_flags |= CE_UNPACKED; 528 529if(o->cache_bottom < o->src_index->cache_nr && 530 o->src_index->cache[o->cache_bottom] == ce) { 531int bottom = o->cache_bottom; 532while(bottom < o->src_index->cache_nr && 533 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 534 bottom++; 535 o->cache_bottom = bottom; 536} 537} 538 539static voidmark_all_ce_unused(struct index_state *index) 540{ 541int i; 542for(i =0; i < index->cache_nr; i++) 543 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 544} 545 546static intlocate_in_src_index(const struct cache_entry *ce, 547struct unpack_trees_options *o) 548{ 549struct index_state *index = o->src_index; 550int len =ce_namelen(ce); 551int pos =index_name_pos(index, ce->name, len); 552if(pos <0) 553 pos = -1- pos; 554return pos; 555} 556 557/* 558 * We call unpack_index_entry() with an unmerged cache entry 559 * only in diff-index, and it wants a single callback. Skip 560 * the other unmerged entry with the same name. 561 */ 562static voidmark_ce_used_same_name(struct cache_entry *ce, 563struct unpack_trees_options *o) 564{ 565struct index_state *index = o->src_index; 566int len =ce_namelen(ce); 567int pos; 568 569for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 570struct cache_entry *next = index->cache[pos]; 571if(len !=ce_namelen(next) || 572memcmp(ce->name, next->name, len)) 573break; 574mark_ce_used(next, o); 575} 576} 577 578static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 579{ 580const struct index_state *index = o->src_index; 581int pos = o->cache_bottom; 582 583while(pos < index->cache_nr) { 584struct cache_entry *ce = index->cache[pos]; 585if(!(ce->ce_flags & CE_UNPACKED)) 586return ce; 587 pos++; 588} 589return NULL; 590} 591 592static voidadd_same_unmerged(const struct cache_entry *ce, 593struct unpack_trees_options *o) 594{ 595struct index_state *index = o->src_index; 596int len =ce_namelen(ce); 597int pos =index_name_pos(index, ce->name, len); 598 599if(0<= pos) 600die("programming error in a caller of mark_ce_used_same_name"); 601for(pos = -pos -1; pos < index->cache_nr; pos++) { 602struct cache_entry *next = index->cache[pos]; 603if(len !=ce_namelen(next) || 604memcmp(ce->name, next->name, len)) 605break; 606add_entry(o, next,0,0); 607mark_ce_used(next, o); 608} 609} 610 611static intunpack_index_entry(struct cache_entry *ce, 612struct unpack_trees_options *o) 613{ 614const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 615int ret; 616 617 src[0] = ce; 618 619mark_ce_used(ce, o); 620if(ce_stage(ce)) { 621if(o->skip_unmerged) { 622add_entry(o, ce,0,0); 623return0; 624} 625} 626 ret =call_unpack_fn(src, o); 627if(ce_stage(ce)) 628mark_ce_used_same_name(ce, o); 629return ret; 630} 631 632static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 633 634static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 635{ 636struct unpack_trees_options *o = info->data; 637 638if(o->diff_index_cached) 639return; 640 o->cache_bottom = bottom; 641} 642 643static intswitch_cache_bottom(struct traverse_info *info) 644{ 645struct unpack_trees_options *o = info->data; 646int ret, pos; 647 648if(o->diff_index_cached) 649return0; 650 ret = o->cache_bottom; 651 pos =find_cache_pos(info->prev, &info->name); 652 653if(pos < -1) 654 o->cache_bottom = -2- pos; 655else if(pos <0) 656 o->cache_bottom = o->src_index->cache_nr; 657return ret; 658} 659 660staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 661{ 662return!is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) &&oideq(&name_j->oid, &name_k->oid); 663} 664 665static intall_trees_same_as_cache_tree(int n,unsigned long dirmask, 666struct name_entry *names, 667struct traverse_info *info) 668{ 669struct unpack_trees_options *o = info->data; 670int i; 671 672if(!o->merge || dirmask != ((1<< n) -1)) 673return0; 674 675for(i =1; i < n; i++) 676if(!are_same_oid(names, names + i)) 677return0; 678 679returncache_tree_matches_traversal(o->src_index->cache_tree, names, info); 680} 681 682static intindex_pos_by_traverse_info(struct name_entry *names, 683struct traverse_info *info) 684{ 685struct unpack_trees_options *o = info->data; 686int len =traverse_path_len(info, names); 687char*name =xmalloc(len +1/* slash */+1/* NUL */); 688int pos; 689 690make_traverse_path(name, info, names); 691 name[len++] ='/'; 692 name[len] ='\0'; 693 pos =index_name_pos(o->src_index, name, len); 694if(pos >=0) 695BUG("This is a directory and should not exist in index"); 696 pos = -pos -1; 697if(!starts_with(o->src_index->cache[pos]->name, name) || 698(pos >0&&starts_with(o->src_index->cache[pos-1]->name, name))) 699BUG("pos must point at the first entry in this directory"); 700free(name); 701return pos; 702} 703 704/* 705 * Fast path if we detect that all trees are the same as cache-tree at this 706 * path. We'll walk these trees in an iterative loop using cache-tree/index 707 * instead of ODB since we already know what these trees contain. 708 */ 709static inttraverse_by_cache_tree(int pos,int nr_entries,int nr_names, 710struct traverse_info *info) 711{ 712struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 713struct unpack_trees_options *o = info->data; 714struct cache_entry *tree_ce = NULL; 715int ce_len =0; 716int i, d; 717 718if(!o->merge) 719BUG("We need cache-tree to do this optimization"); 720 721/* 722 * Do what unpack_callback() and unpack_nondirectories() normally 723 * do. But we walk all paths in an iterative loop instead. 724 * 725 * D/F conflicts and higher stage entries are not a concern 726 * because cache-tree would be invalidated and we would never 727 * get here in the first place. 728 */ 729for(i =0; i < nr_entries; i++) { 730int new_ce_len, len, rc; 731 732 src[0] = o->src_index->cache[pos + i]; 733 734 len =ce_namelen(src[0]); 735 new_ce_len =cache_entry_size(len); 736 737if(new_ce_len > ce_len) { 738 new_ce_len <<=1; 739 tree_ce =xrealloc(tree_ce, new_ce_len); 740memset(tree_ce,0, new_ce_len); 741 ce_len = new_ce_len; 742 743 tree_ce->ce_flags =create_ce_flags(0); 744 745for(d =1; d <= nr_names; d++) 746 src[d] = tree_ce; 747} 748 749 tree_ce->ce_mode = src[0]->ce_mode; 750 tree_ce->ce_namelen = len; 751oidcpy(&tree_ce->oid, &src[0]->oid); 752memcpy(tree_ce->name, src[0]->name, len +1); 753 754 rc =call_unpack_fn((const struct cache_entry *const*)src, o); 755if(rc <0) { 756free(tree_ce); 757return rc; 758} 759 760mark_ce_used(src[0], o); 761} 762free(tree_ce); 763if(o->debug_unpack) 764printf("Unpacked%dentries from%sto%susing cache-tree\n", 765 nr_entries, 766 o->src_index->cache[pos]->name, 767 o->src_index->cache[pos + nr_entries -1]->name); 768return0; 769} 770 771static inttraverse_trees_recursive(int n,unsigned long dirmask, 772unsigned long df_conflicts, 773struct name_entry *names, 774struct traverse_info *info) 775{ 776struct unpack_trees_options *o = info->data; 777int i, ret, bottom; 778int nr_buf =0; 779struct tree_desc t[MAX_UNPACK_TREES]; 780void*buf[MAX_UNPACK_TREES]; 781struct traverse_info newinfo; 782struct name_entry *p; 783int nr_entries; 784 785 nr_entries =all_trees_same_as_cache_tree(n, dirmask, names, info); 786if(nr_entries >0) { 787int pos =index_pos_by_traverse_info(names, info); 788 789if(!o->merge || df_conflicts) 790BUG("Wrong condition to get here buddy"); 791 792/* 793 * All entries up to 'pos' must have been processed 794 * (i.e. marked CE_UNPACKED) at this point. But to be safe, 795 * save and restore cache_bottom anyway to not miss 796 * unprocessed entries before 'pos'. 797 */ 798 bottom = o->cache_bottom; 799 ret =traverse_by_cache_tree(pos, nr_entries, n, info); 800 o->cache_bottom = bottom; 801return ret; 802} 803 804 p = names; 805while(!p->mode) 806 p++; 807 808 newinfo = *info; 809 newinfo.prev = info; 810 newinfo.pathspec = info->pathspec; 811 newinfo.name = *p; 812 newinfo.pathlen +=tree_entry_len(p) +1; 813 newinfo.df_conflicts |= df_conflicts; 814 815/* 816 * Fetch the tree from the ODB for each peer directory in the 817 * n commits. 818 * 819 * For 2- and 3-way traversals, we try to avoid hitting the 820 * ODB twice for the same OID. This should yield a nice speed 821 * up in checkouts and merges when the commits are similar. 822 * 823 * We don't bother doing the full O(n^2) search for larger n, 824 * because wider traversals don't happen that often and we 825 * avoid the search setup. 826 * 827 * When 2 peer OIDs are the same, we just copy the tree 828 * descriptor data. This implicitly borrows the buffer 829 * data from the earlier cell. 830 */ 831for(i =0; i < n; i++, dirmask >>=1) { 832if(i >0&&are_same_oid(&names[i], &names[i -1])) 833 t[i] = t[i -1]; 834else if(i >1&&are_same_oid(&names[i], &names[i -2])) 835 t[i] = t[i -2]; 836else{ 837const struct object_id *oid = NULL; 838if(dirmask &1) 839 oid = &names[i].oid; 840 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 841} 842} 843 844 bottom =switch_cache_bottom(&newinfo); 845 ret =traverse_trees(o->src_index, n, t, &newinfo); 846restore_cache_bottom(&newinfo, bottom); 847 848for(i =0; i < nr_buf; i++) 849free(buf[i]); 850 851return ret; 852} 853 854/* 855 * Compare the traverse-path to the cache entry without actually 856 * having to generate the textual representation of the traverse 857 * path. 858 * 859 * NOTE! This *only* compares up to the size of the traverse path 860 * itself - the caller needs to do the final check for the cache 861 * entry having more data at the end! 862 */ 863static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 864{ 865int len, pathlen, ce_len; 866const char*ce_name; 867 868if(info->prev) { 869int cmp =do_compare_entry_piecewise(ce, info->prev, 870&info->name); 871if(cmp) 872return cmp; 873} 874 pathlen = info->pathlen; 875 ce_len =ce_namelen(ce); 876 877/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 878if(ce_len < pathlen) 879return-1; 880 881 ce_len -= pathlen; 882 ce_name = ce->name + pathlen; 883 884 len =tree_entry_len(n); 885returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 886} 887 888static intdo_compare_entry(const struct cache_entry *ce, 889const struct traverse_info *info, 890const struct name_entry *n) 891{ 892int len, pathlen, ce_len; 893const char*ce_name; 894int cmp; 895 896/* 897 * If we have not precomputed the traverse path, it is quicker 898 * to avoid doing so. But if we have precomputed it, 899 * it is quicker to use the precomputed version. 900 */ 901if(!info->traverse_path) 902returndo_compare_entry_piecewise(ce, info, n); 903 904 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 905if(cmp) 906return cmp; 907 908 pathlen = info->pathlen; 909 ce_len =ce_namelen(ce); 910 911if(ce_len < pathlen) 912return-1; 913 914 ce_len -= pathlen; 915 ce_name = ce->name + pathlen; 916 917 len =tree_entry_len(n); 918returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 919} 920 921static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 922{ 923int cmp =do_compare_entry(ce, info, n); 924if(cmp) 925return cmp; 926 927/* 928 * Even if the beginning compared identically, the ce should 929 * compare as bigger than a directory leading up to it! 930 */ 931returnce_namelen(ce) >traverse_path_len(info, n); 932} 933 934static intce_in_traverse_path(const struct cache_entry *ce, 935const struct traverse_info *info) 936{ 937if(!info->prev) 938return1; 939if(do_compare_entry(ce, info->prev, &info->name)) 940return0; 941/* 942 * If ce (blob) is the same name as the path (which is a tree 943 * we will be descending into), it won't be inside it. 944 */ 945return(info->pathlen <ce_namelen(ce)); 946} 947 948static struct cache_entry *create_ce_entry(const struct traverse_info *info, 949const struct name_entry *n, 950int stage, 951struct index_state *istate, 952int is_transient) 953{ 954int len =traverse_path_len(info, n); 955struct cache_entry *ce = 956 is_transient ? 957make_empty_transient_cache_entry(len) : 958make_empty_cache_entry(istate, len); 959 960 ce->ce_mode =create_ce_mode(n->mode); 961 ce->ce_flags =create_ce_flags(stage); 962 ce->ce_namelen = len; 963oidcpy(&ce->oid, &n->oid); 964make_traverse_path(ce->name, info, n); 965 966return ce; 967} 968 969/* 970 * Note that traverse_by_cache_tree() duplicates some logic in this function 971 * without actually calling it. If you change the logic here you may need to 972 * check and change there as well. 973 */ 974static intunpack_nondirectories(int n,unsigned long mask, 975unsigned long dirmask, 976struct cache_entry **src, 977const struct name_entry *names, 978const struct traverse_info *info) 979{ 980int i; 981struct unpack_trees_options *o = info->data; 982unsigned long conflicts = info->df_conflicts | dirmask; 983 984/* Do we have *only* directories? Nothing to do */ 985if(mask == dirmask && !src[0]) 986return0; 987 988/* 989 * Ok, we've filled in up to any potential index entry in src[0], 990 * now do the rest. 991 */ 992for(i =0; i < n; i++) { 993int stage; 994unsigned int bit =1ul<< i; 995if(conflicts & bit) { 996 src[i + o->merge] = o->df_conflict_entry; 997continue; 998} 999if(!(mask & bit))1000continue;1001if(!o->merge)1002 stage =0;1003else if(i +1< o->head_idx)1004 stage =1;1005else if(i +1> o->head_idx)1006 stage =3;1007else1008 stage =2;10091010/*1011 * If the merge bit is set, then the cache entries are1012 * discarded in the following block. In this case,1013 * construct "transient" cache_entries, as they are1014 * not stored in the index. otherwise construct the1015 * cache entry from the index aware logic.1016 */1017 src[i + o->merge] =create_ce_entry(info, names + i, stage, &o->result, o->merge);1018}10191020if(o->merge) {1021int rc =call_unpack_fn((const struct cache_entry *const*)src,1022 o);1023for(i =0; i < n; i++) {1024struct cache_entry *ce = src[i + o->merge];1025if(ce != o->df_conflict_entry)1026discard_cache_entry(ce);1027}1028return rc;1029}10301031for(i =0; i < n; i++)1032if(src[i] && src[i] != o->df_conflict_entry)1033if(do_add_entry(o, src[i],0,0))1034return-1;10351036return0;1037}10381039static intunpack_failed(struct unpack_trees_options *o,const char*message)1040{1041discard_index(&o->result);1042if(!o->gently && !o->exiting_early) {1043if(message)1044returnerror("%s", message);1045return-1;1046}1047return-1;1048}10491050/*1051 * The tree traversal is looking at name p. If we have a matching entry,1052 * return it. If name p is a directory in the index, do not return1053 * anything, as we will want to match it when the traversal descends into1054 * the directory.1055 */1056static intfind_cache_pos(struct traverse_info *info,1057const struct name_entry *p)1058{1059int pos;1060struct unpack_trees_options *o = info->data;1061struct index_state *index = o->src_index;1062int pfxlen = info->pathlen;1063int p_len =tree_entry_len(p);10641065for(pos = o->cache_bottom; pos < index->cache_nr; pos++) {1066const struct cache_entry *ce = index->cache[pos];1067const char*ce_name, *ce_slash;1068int cmp, ce_len;10691070if(ce->ce_flags & CE_UNPACKED) {1071/*1072 * cache_bottom entry is already unpacked, so1073 * we can never match it; don't check it1074 * again.1075 */1076if(pos == o->cache_bottom)1077++o->cache_bottom;1078continue;1079}1080if(!ce_in_traverse_path(ce, info)) {1081/*1082 * Check if we can skip future cache checks1083 * (because we're already past all possible1084 * entries in the traverse path).1085 */1086if(info->traverse_path) {1087if(strncmp(ce->name, info->traverse_path,1088 info->pathlen) >0)1089break;1090}1091continue;1092}1093 ce_name = ce->name + pfxlen;1094 ce_slash =strchr(ce_name,'/');1095if(ce_slash)1096 ce_len = ce_slash - ce_name;1097else1098 ce_len =ce_namelen(ce) - pfxlen;1099 cmp =name_compare(p->path, p_len, ce_name, ce_len);1100/*1101 * Exact match; if we have a directory we need to1102 * delay returning it.1103 */1104if(!cmp)1105return ce_slash ? -2- pos : pos;1106if(0< cmp)1107continue;/* keep looking */1108/*1109 * ce_name sorts after p->path; could it be that we1110 * have files under p->path directory in the index?1111 * E.g. ce_name == "t-i", and p->path == "t"; we may1112 * have "t/a" in the index.1113 */1114if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&1115 ce_name[p_len] <'/')1116continue;/* keep looking */1117break;1118}1119return-1;1120}11211122static struct cache_entry *find_cache_entry(struct traverse_info *info,1123const struct name_entry *p)1124{1125int pos =find_cache_pos(info, p);1126struct unpack_trees_options *o = info->data;11271128if(0<= pos)1129return o->src_index->cache[pos];1130else1131return NULL;1132}11331134static voiddebug_path(struct traverse_info *info)1135{1136if(info->prev) {1137debug_path(info->prev);1138if(*info->prev->name.path)1139putchar('/');1140}1141printf("%s", info->name.path);1142}11431144static voiddebug_name_entry(int i,struct name_entry *n)1145{1146printf("ent#%d %06o%s\n", i,1147 n->path ? n->mode :0,1148 n->path ? n->path :"(missing)");1149}11501151static voiddebug_unpack_callback(int n,1152unsigned long mask,1153unsigned long dirmask,1154struct name_entry *names,1155struct traverse_info *info)1156{1157int i;1158printf("* unpack mask%lu, dirmask%lu, cnt%d",1159 mask, dirmask, n);1160debug_path(info);1161putchar('\n');1162for(i =0; i < n; i++)1163debug_name_entry(i, names + i);1164}11651166/*1167 * Note that traverse_by_cache_tree() duplicates some logic in this function1168 * without actually calling it. If you change the logic here you may need to1169 * check and change there as well.1170 */1171static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info)1172{1173struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1174struct unpack_trees_options *o = info->data;1175const struct name_entry *p = names;11761177/* Find first entry with a real name (we could use "mask" too) */1178while(!p->mode)1179 p++;11801181if(o->debug_unpack)1182debug_unpack_callback(n, mask, dirmask, names, info);11831184/* Are we supposed to look at the index too? */1185if(o->merge) {1186while(1) {1187int cmp;1188struct cache_entry *ce;11891190if(o->diff_index_cached)1191 ce =next_cache_entry(o);1192else1193 ce =find_cache_entry(info, p);11941195if(!ce)1196break;1197 cmp =compare_entry(ce, info, p);1198if(cmp <0) {1199if(unpack_index_entry(ce, o) <0)1200returnunpack_failed(o, NULL);1201continue;1202}1203if(!cmp) {1204if(ce_stage(ce)) {1205/*1206 * If we skip unmerged index1207 * entries, we'll skip this1208 * entry *and* the tree1209 * entries associated with it!1210 */1211if(o->skip_unmerged) {1212add_same_unmerged(ce, o);1213return mask;1214}1215}1216 src[0] = ce;1217}1218break;1219}1220}12211222if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1223return-1;12241225if(o->merge && src[0]) {1226if(ce_stage(src[0]))1227mark_ce_used_same_name(src[0], o);1228else1229mark_ce_used(src[0], o);1230}12311232/* Now handle any directories.. */1233if(dirmask) {1234/* special case: "diff-index --cached" looking at a tree */1235if(o->diff_index_cached &&1236 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1237int matches;1238 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1239 names, info);1240/*1241 * Everything under the name matches; skip the1242 * entire hierarchy. diff_index_cached codepath1243 * special cases D/F conflicts in such a way that1244 * it does not do any look-ahead, so this is safe.1245 */1246if(matches) {1247 o->cache_bottom += matches;1248return mask;1249}1250}12511252if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1253 names, info) <0)1254return-1;1255return mask;1256}12571258return mask;1259}12601261static intclear_ce_flags_1(struct index_state *istate,1262struct cache_entry **cache,int nr,1263struct strbuf *prefix,1264int select_mask,int clear_mask,1265struct exclude_list *el,int defval);12661267/* Whole directory matching */1268static intclear_ce_flags_dir(struct index_state *istate,1269struct cache_entry **cache,int nr,1270struct strbuf *prefix,1271char*basename,1272int select_mask,int clear_mask,1273struct exclude_list *el,int defval)1274{1275struct cache_entry **cache_end;1276int dtype = DT_DIR;1277int ret =is_excluded_from_list(prefix->buf, prefix->len,1278 basename, &dtype, el, istate);1279int rc;12801281strbuf_addch(prefix,'/');12821283/* If undecided, use matching result of parent dir in defval */1284if(ret <0)1285 ret = defval;12861287for(cache_end = cache; cache_end != cache + nr; cache_end++) {1288struct cache_entry *ce = *cache_end;1289if(strncmp(ce->name, prefix->buf, prefix->len))1290break;1291}12921293/*1294 * TODO: check el, if there are no patterns that may conflict1295 * with ret (iow, we know in advance the incl/excl1296 * decision for the entire directory), clear flag here without1297 * calling clear_ce_flags_1(). That function will call1298 * the expensive is_excluded_from_list() on every entry.1299 */1300 rc =clear_ce_flags_1(istate, cache, cache_end - cache,1301 prefix,1302 select_mask, clear_mask,1303 el, ret);1304strbuf_setlen(prefix, prefix->len -1);1305return rc;1306}13071308/*1309 * Traverse the index, find every entry that matches according to1310 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1311 * number of traversed entries.1312 *1313 * If select_mask is non-zero, only entries whose ce_flags has on of1314 * those bits enabled are traversed.1315 *1316 * cache : pointer to an index entry1317 * prefix_len : an offset to its path1318 *1319 * The current path ("prefix") including the trailing '/' is1320 * cache[0]->name[0..(prefix_len-1)]1321 * Top level path has prefix_len zero.1322 */1323static intclear_ce_flags_1(struct index_state *istate,1324struct cache_entry **cache,int nr,1325struct strbuf *prefix,1326int select_mask,int clear_mask,1327struct exclude_list *el,int defval)1328{1329struct cache_entry **cache_end = cache + nr;13301331/*1332 * Process all entries that have the given prefix and meet1333 * select_mask condition1334 */1335while(cache != cache_end) {1336struct cache_entry *ce = *cache;1337const char*name, *slash;1338int len, dtype, ret;13391340if(select_mask && !(ce->ce_flags & select_mask)) {1341 cache++;1342continue;1343}13441345if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1346break;13471348 name = ce->name + prefix->len;1349 slash =strchr(name,'/');13501351/* If it's a directory, try whole directory match first */1352if(slash) {1353int processed;13541355 len = slash - name;1356strbuf_add(prefix, name, len);13571358 processed =clear_ce_flags_dir(istate, cache, cache_end - cache,1359 prefix,1360 prefix->buf + prefix->len - len,1361 select_mask, clear_mask,1362 el, defval);13631364/* clear_c_f_dir eats a whole dir already? */1365if(processed) {1366 cache += processed;1367strbuf_setlen(prefix, prefix->len - len);1368continue;1369}13701371strbuf_addch(prefix,'/');1372 cache +=clear_ce_flags_1(istate, cache, cache_end - cache,1373 prefix,1374 select_mask, clear_mask, el, defval);1375strbuf_setlen(prefix, prefix->len - len -1);1376continue;1377}13781379/* Non-directory */1380 dtype =ce_to_dtype(ce);1381 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1382 name, &dtype, el, istate);1383if(ret <0)1384 ret = defval;1385if(ret >0)1386 ce->ce_flags &= ~clear_mask;1387 cache++;1388}1389return nr - (cache_end - cache);1390}13911392static intclear_ce_flags(struct index_state *istate,1393int select_mask,int clear_mask,1394struct exclude_list *el)1395{1396static struct strbuf prefix = STRBUF_INIT;13971398strbuf_reset(&prefix);13991400returnclear_ce_flags_1(istate,1401 istate->cache,1402 istate->cache_nr,1403&prefix,1404 select_mask, clear_mask,1405 el,0);1406}14071408/*1409 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1410 */1411static voidmark_new_skip_worktree(struct exclude_list *el,1412struct index_state *istate,1413int select_flag,int skip_wt_flag)1414{1415int i;14161417/*1418 * 1. Pretend the narrowest worktree: only unmerged entries1419 * are checked out1420 */1421for(i =0; i < istate->cache_nr; i++) {1422struct cache_entry *ce = istate->cache[i];14231424if(select_flag && !(ce->ce_flags & select_flag))1425continue;14261427if(!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))1428 ce->ce_flags |= skip_wt_flag;1429else1430 ce->ce_flags &= ~skip_wt_flag;1431}14321433/*1434 * 2. Widen worktree according to sparse-checkout file.1435 * Matched entries will have skip_wt_flag cleared (i.e. "in")1436 */1437clear_ce_flags(istate, select_flag, skip_wt_flag, el);1438}14391440static intverify_absent(const struct cache_entry *,1441enum unpack_trees_error_types,1442struct unpack_trees_options *);1443/*1444 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1445 * resulting index, -2 on failure to reflect the changes to the work tree.1446 *1447 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1448 */1449intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1450{1451int i, ret;1452static struct cache_entry *dfc;1453struct exclude_list el;14541455if(len > MAX_UNPACK_TREES)1456die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);14571458trace_performance_enter();1459memset(&el,0,sizeof(el));1460if(!core_apply_sparse_checkout || !o->update)1461 o->skip_sparse_checkout =1;1462if(!o->skip_sparse_checkout) {1463char*sparse =git_pathdup("info/sparse-checkout");1464if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1465 o->skip_sparse_checkout =1;1466else1467 o->el = ⪙1468free(sparse);1469}14701471memset(&o->result,0,sizeof(o->result));1472 o->result.initialized =1;1473 o->result.timestamp.sec = o->src_index->timestamp.sec;1474 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1475 o->result.version = o->src_index->version;1476if(!o->src_index->split_index) {1477 o->result.split_index = NULL;1478}else if(o->src_index == o->dst_index) {1479/*1480 * o->dst_index (and thus o->src_index) will be discarded1481 * and overwritten with o->result at the end of this function,1482 * so just use src_index's split_index to avoid having to1483 * create a new one.1484 */1485 o->result.split_index = o->src_index->split_index;1486 o->result.split_index->refcount++;1487}else{1488 o->result.split_index =init_split_index(&o->result);1489}1490oidcpy(&o->result.oid, &o->src_index->oid);1491 o->merge_size = len;1492mark_all_ce_unused(o->src_index);14931494/*1495 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1496 */1497if(!o->skip_sparse_checkout)1498mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);14991500if(!dfc)1501 dfc =xcalloc(1,cache_entry_size(0));1502 o->df_conflict_entry = dfc;15031504if(len) {1505const char*prefix = o->prefix ? o->prefix :"";1506struct traverse_info info;15071508setup_traverse_info(&info, prefix);1509 info.fn = unpack_callback;1510 info.data = o;1511 info.show_all_errors = o->show_all_errors;1512 info.pathspec = o->pathspec;15131514if(o->prefix) {1515/*1516 * Unpack existing index entries that sort before the1517 * prefix the tree is spliced into. Note that o->merge1518 * is always true in this case.1519 */1520while(1) {1521struct cache_entry *ce =next_cache_entry(o);1522if(!ce)1523break;1524if(ce_in_traverse_path(ce, &info))1525break;1526if(unpack_index_entry(ce, o) <0)1527goto return_failed;1528}1529}15301531trace_performance_enter();1532 ret =traverse_trees(o->src_index, len, t, &info);1533trace_performance_leave("traverse_trees");1534if(ret <0)1535goto return_failed;1536}15371538/* Any left-over entries in the index? */1539if(o->merge) {1540while(1) {1541struct cache_entry *ce =next_cache_entry(o);1542if(!ce)1543break;1544if(unpack_index_entry(ce, o) <0)1545goto return_failed;1546}1547}1548mark_all_ce_unused(o->src_index);15491550if(o->trivial_merges_only && o->nontrivial_merge) {1551 ret =unpack_failed(o,"Merge requires file-level merging");1552goto done;1553}15541555if(!o->skip_sparse_checkout) {1556int empty_worktree =1;15571558/*1559 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11560 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1561 * so apply_sparse_checkout() won't attempt to remove it from worktree1562 */1563mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);15641565 ret =0;1566for(i =0; i < o->result.cache_nr; i++) {1567struct cache_entry *ce = o->result.cache[i];15681569/*1570 * Entries marked with CE_ADDED in merged_entry() do not have1571 * verify_absent() check (the check is effectively disabled1572 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1573 *1574 * Do the real check now because we have had1575 * correct CE_NEW_SKIP_WORKTREE1576 */1577if(ce->ce_flags & CE_ADDED &&1578verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1579if(!o->show_all_errors)1580goto return_failed;1581 ret = -1;1582}15831584if(apply_sparse_checkout(&o->result, ce, o)) {1585if(!o->show_all_errors)1586goto return_failed;1587 ret = -1;1588}1589if(!ce_skip_worktree(ce))1590 empty_worktree =0;15911592}1593if(ret <0)1594goto return_failed;1595/*1596 * Sparse checkout is meant to narrow down checkout area1597 * but it does not make sense to narrow down to empty working1598 * tree. This is usually a mistake in sparse checkout rules.1599 * Do not allow users to do that.1600 */1601if(o->result.cache_nr && empty_worktree) {1602 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1603goto done;1604}1605}16061607 ret =check_updates(o) ? (-2) :0;1608if(o->dst_index) {1609move_index_extensions(&o->result, o->src_index);1610if(!ret) {1611if(git_env_bool("GIT_TEST_CHECK_CACHE_TREE",0))1612cache_tree_verify(the_repository, &o->result);1613if(!o->result.cache_tree)1614 o->result.cache_tree =cache_tree();1615if(!cache_tree_fully_valid(o->result.cache_tree))1616cache_tree_update(&o->result,1617 WRITE_TREE_SILENT |1618 WRITE_TREE_REPAIR);1619}16201621 o->result.updated_workdir =1;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,1763struct unpack_trees_options *o)1764{1765if(!submodule_from_ce(ce))1766return0;17671768returncheck_submodule_move_head(ce, old_sha1,1769oid_to_hex(&ce->oid), o);1770}17711772static intverify_clean_subdirectory(const struct cache_entry *ce,1773struct unpack_trees_options *o)1774{1775/*1776 * we are about to extract "ce->name"; we would not want to lose1777 * anything in the existing directory there.1778 */1779int namelen;1780int i;1781struct dir_struct d;1782char*pathbuf;1783int cnt =0;17841785if(S_ISGITLINK(ce->ce_mode)) {1786struct object_id oid;1787int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1788/*1789 * If we are not going to update the submodule, then1790 * we don't care.1791 */1792if(!sub_head &&oideq(&oid, &ce->oid))1793return0;1794returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1795 ce, o);1796}17971798/*1799 * First let's make sure we do not have a local modification1800 * in that directory.1801 */1802 namelen =ce_namelen(ce);1803for(i =locate_in_src_index(ce, o);1804 i < o->src_index->cache_nr;1805 i++) {1806struct cache_entry *ce2 = o->src_index->cache[i];1807int len =ce_namelen(ce2);1808if(len < namelen ||1809strncmp(ce->name, ce2->name, namelen) ||1810 ce2->name[namelen] !='/')1811break;1812/*1813 * ce2->name is an entry in the subdirectory to be1814 * removed.1815 */1816if(!ce_stage(ce2)) {1817if(verify_uptodate(ce2, o))1818return-1;1819add_entry(o, ce2, CE_REMOVE,0);1820invalidate_ce_path(ce, o);1821mark_ce_used(ce2, o);1822}1823 cnt++;1824}18251826/*1827 * Then we need to make sure that we do not lose a locally1828 * present file that is not ignored.1829 */1830 pathbuf =xstrfmt("%.*s/", namelen, ce->name);18311832memset(&d,0,sizeof(d));1833if(o->dir)1834 d.exclude_per_dir = o->dir->exclude_per_dir;1835 i =read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);1836if(i)1837return o->gently ? -1:1838add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1839free(pathbuf);1840return cnt;1841}18421843/*1844 * This gets called when there was no index entry for the tree entry 'dst',1845 * but we found a file in the working tree that 'lstat()' said was fine,1846 * and we're on a case-insensitive filesystem.1847 *1848 * See if we can find a case-insensitive match in the index that also1849 * matches the stat information, and assume it's that other file!1850 */1851static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1852{1853const struct cache_entry *src;18541855 src =index_file_exists(o->src_index, name, len,1);1856return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1857}18581859static intcheck_ok_to_remove(const char*name,int len,int dtype,1860const struct cache_entry *ce,struct stat *st,1861enum unpack_trees_error_types error_type,1862struct unpack_trees_options *o)1863{1864const struct cache_entry *result;18651866/*1867 * It may be that the 'lstat()' succeeded even though1868 * target 'ce' was absent, because there is an old1869 * entry that is different only in case..1870 *1871 * Ignore that lstat() if it matches.1872 */1873if(ignore_case &&icase_exists(o, name, len, st))1874return0;18751876if(o->dir &&1877is_excluded(o->dir, o->src_index, name, &dtype))1878/*1879 * ce->name is explicitly excluded, so it is Ok to1880 * overwrite it.1881 */1882return0;1883if(S_ISDIR(st->st_mode)) {1884/*1885 * We are checking out path "foo" and1886 * found "foo/." in the working tree.1887 * This is tricky -- if we have modified1888 * files that are in "foo/" we would lose1889 * them.1890 */1891if(verify_clean_subdirectory(ce, o) <0)1892return-1;1893return0;1894}18951896/*1897 * The previous round may already have decided to1898 * delete this path, which is in a subdirectory that1899 * is being replaced with a blob.1900 */1901 result =index_file_exists(&o->result, name, len,0);1902if(result) {1903if(result->ce_flags & CE_REMOVE)1904return0;1905}19061907return o->gently ? -1:1908add_rejected_path(o, error_type, name);1909}19101911/*1912 * We do not want to remove or overwrite a working tree file that1913 * is not tracked, unless it is ignored.1914 */1915static intverify_absent_1(const struct cache_entry *ce,1916enum unpack_trees_error_types error_type,1917struct unpack_trees_options *o)1918{1919int len;1920struct stat st;19211922if(o->index_only || o->reset || !o->update)1923return0;19241925 len =check_leading_path(ce->name,ce_namelen(ce));1926if(!len)1927return0;1928else if(len >0) {1929char*path;1930int ret;19311932 path =xmemdupz(ce->name, len);1933if(lstat(path, &st))1934 ret =error_errno("cannot stat '%s'", path);1935else{1936if(submodule_from_ce(ce))1937 ret =check_submodule_move_head(ce,1938oid_to_hex(&ce->oid),1939 NULL, o);1940else1941 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1942&st, error_type, o);1943}1944free(path);1945return ret;1946}else if(lstat(ce->name, &st)) {1947if(errno != ENOENT)1948returnerror_errno("cannot stat '%s'", ce->name);1949return0;1950}else{1951if(submodule_from_ce(ce))1952returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1953 NULL, o);19541955returncheck_ok_to_remove(ce->name,ce_namelen(ce),1956ce_to_dtype(ce), ce, &st,1957 error_type, o);1958}1959}19601961static intverify_absent(const struct cache_entry *ce,1962enum unpack_trees_error_types error_type,1963struct unpack_trees_options *o)1964{1965if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1966return0;1967returnverify_absent_1(ce, error_type, o);1968}19691970static intverify_absent_sparse(const struct cache_entry *ce,1971enum unpack_trees_error_types error_type,1972struct unpack_trees_options *o)1973{1974enum unpack_trees_error_types orphaned_error = error_type;1975if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1976 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;19771978returnverify_absent_1(ce, orphaned_error, o);1979}19801981static intmerged_entry(const struct cache_entry *ce,1982const struct cache_entry *old,1983struct unpack_trees_options *o)1984{1985int update = CE_UPDATE;1986struct cache_entry *merge =dup_cache_entry(ce, &o->result);19871988if(!old) {1989/*1990 * New index entries. In sparse checkout, the following1991 * verify_absent() will be delayed until after1992 * traverse_trees() finishes in unpack_trees(), then:1993 *1994 * - CE_NEW_SKIP_WORKTREE will be computed correctly1995 * - verify_absent() be called again, this time with1996 * correct CE_NEW_SKIP_WORKTREE1997 *1998 * verify_absent() call here does nothing in sparse1999 * checkout (i.e. o->skip_sparse_checkout == 0)2000 */2001 update |= CE_ADDED;2002 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;20032004if(verify_absent(merge,2005 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {2006discard_cache_entry(merge);2007return-1;2008}2009invalidate_ce_path(merge, o);20102011if(submodule_from_ce(ce)) {2012int ret =check_submodule_move_head(ce, NULL,2013oid_to_hex(&ce->oid),2014 o);2015if(ret)2016return ret;2017}20182019}else if(!(old->ce_flags & CE_CONFLICTED)) {2020/*2021 * See if we can re-use the old CE directly?2022 * That way we get the uptodate stat info.2023 *2024 * This also removes the UPDATE flag on a match; otherwise2025 * we will end up overwriting local changes in the work tree.2026 */2027if(same(old, merge)) {2028copy_cache_entry(merge, old);2029 update =0;2030}else{2031if(verify_uptodate(old, o)) {2032discard_cache_entry(merge);2033return-1;2034}2035/* Migrate old flags over */2036 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);2037invalidate_ce_path(old, o);2038}20392040if(submodule_from_ce(ce)) {2041int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),2042oid_to_hex(&ce->oid),2043 o);2044if(ret)2045return ret;2046}2047}else{2048/*2049 * Previously unmerged entry left as an existence2050 * marker by read_index_unmerged();2051 */2052invalidate_ce_path(old, o);2053}20542055do_add_entry(o, merge, update, CE_STAGEMASK);2056return1;2057}20582059static intdeleted_entry(const struct cache_entry *ce,2060const struct cache_entry *old,2061struct unpack_trees_options *o)2062{2063/* Did it exist in the index? */2064if(!old) {2065if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2066return-1;2067return0;2068}2069if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))2070return-1;2071add_entry(o, ce, CE_REMOVE,0);2072invalidate_ce_path(ce, o);2073return1;2074}20752076static intkeep_entry(const struct cache_entry *ce,2077struct unpack_trees_options *o)2078{2079add_entry(o, ce,0,0);2080if(ce_stage(ce))2081invalidate_ce_path(ce, o);2082return1;2083}20842085#if DBRT_DEBUG2086static voidshow_stage_entry(FILE*o,2087const char*label,const struct cache_entry *ce)2088{2089if(!ce)2090fprintf(o,"%s(missing)\n", label);2091else2092fprintf(o,"%s%06o%s %d\t%s\n",2093 label,2094 ce->ce_mode,2095oid_to_hex(&ce->oid),2096ce_stage(ce),2097 ce->name);2098}2099#endif21002101intthreeway_merge(const struct cache_entry *const*stages,2102struct unpack_trees_options *o)2103{2104const struct cache_entry *index;2105const struct cache_entry *head;2106const struct cache_entry *remote = stages[o->head_idx +1];2107int count;2108int head_match =0;2109int remote_match =0;21102111int df_conflict_head =0;2112int df_conflict_remote =0;21132114int any_anc_missing =0;2115int no_anc_exists =1;2116int i;21172118for(i =1; i < o->head_idx; i++) {2119if(!stages[i] || stages[i] == o->df_conflict_entry)2120 any_anc_missing =1;2121else2122 no_anc_exists =0;2123}21242125 index = stages[0];2126 head = stages[o->head_idx];21272128if(head == o->df_conflict_entry) {2129 df_conflict_head =1;2130 head = NULL;2131}21322133if(remote == o->df_conflict_entry) {2134 df_conflict_remote =1;2135 remote = NULL;2136}21372138/*2139 * First, if there's a #16 situation, note that to prevent #132140 * and #14.2141 */2142if(!same(remote, head)) {2143for(i =1; i < o->head_idx; i++) {2144if(same(stages[i], head)) {2145 head_match = i;2146}2147if(same(stages[i], remote)) {2148 remote_match = i;2149}2150}2151}21522153/*2154 * We start with cases where the index is allowed to match2155 * something other than the head: #14(ALT) and #2ALT, where it2156 * is permitted to match the result instead.2157 */2158/* #14, #14ALT, #2ALT */2159if(remote && !df_conflict_head && head_match && !remote_match) {2160if(index && !same(index, remote) && !same(index, head))2161returnreject_merge(index, o);2162returnmerged_entry(remote, index, o);2163}2164/*2165 * If we have an entry in the index cache, then we want to2166 * make sure that it matches head.2167 */2168if(index && !same(index, head))2169returnreject_merge(index, o);21702171if(head) {2172/* #5ALT, #15 */2173if(same(head, remote))2174returnmerged_entry(head, index, o);2175/* #13, #3ALT */2176if(!df_conflict_remote && remote_match && !head_match)2177returnmerged_entry(head, index, o);2178}21792180/* #1 */2181if(!head && !remote && any_anc_missing)2182return0;21832184/*2185 * Under the "aggressive" rule, we resolve mostly trivial2186 * cases that we historically had git-merge-one-file resolve.2187 */2188if(o->aggressive) {2189int head_deleted = !head;2190int remote_deleted = !remote;2191const struct cache_entry *ce = NULL;21922193if(index)2194 ce = index;2195else if(head)2196 ce = head;2197else if(remote)2198 ce = remote;2199else{2200for(i =1; i < o->head_idx; i++) {2201if(stages[i] && stages[i] != o->df_conflict_entry) {2202 ce = stages[i];2203break;2204}2205}2206}22072208/*2209 * Deleted in both.2210 * Deleted in one and unchanged in the other.2211 */2212if((head_deleted && remote_deleted) ||2213(head_deleted && remote && remote_match) ||2214(remote_deleted && head && head_match)) {2215if(index)2216returndeleted_entry(index, index, o);2217if(ce && !head_deleted) {2218if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2219return-1;2220}2221return0;2222}2223/*2224 * Added in both, identically.2225 */2226if(no_anc_exists && head && remote &&same(head, remote))2227returnmerged_entry(head, index, o);22282229}22302231/* Below are "no merge" cases, which require that the index be2232 * up-to-date to avoid the files getting overwritten with2233 * conflict resolution files.2234 */2235if(index) {2236if(verify_uptodate(index, o))2237return-1;2238}22392240 o->nontrivial_merge =1;22412242/* #2, #3, #4, #6, #7, #9, #10, #11. */2243 count =0;2244if(!head_match || !remote_match) {2245for(i =1; i < o->head_idx; i++) {2246if(stages[i] && stages[i] != o->df_conflict_entry) {2247keep_entry(stages[i], o);2248 count++;2249break;2250}2251}2252}2253#if DBRT_DEBUG2254else{2255fprintf(stderr,"read-tree: warning #16 detected\n");2256show_stage_entry(stderr,"head ", stages[head_match]);2257show_stage_entry(stderr,"remote ", stages[remote_match]);2258}2259#endif2260if(head) { count +=keep_entry(head, o); }2261if(remote) { count +=keep_entry(remote, o); }2262return count;2263}22642265/*2266 * Two-way merge.2267 *2268 * The rule is to "carry forward" what is in the index without losing2269 * information across a "fast-forward", favoring a successful merge2270 * over a merge failure when it makes sense. For details of the2271 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2272 *2273 */2274inttwoway_merge(const struct cache_entry *const*src,2275struct unpack_trees_options *o)2276{2277const struct cache_entry *current = src[0];2278const struct cache_entry *oldtree = src[1];2279const struct cache_entry *newtree = src[2];22802281if(o->merge_size !=2)2282returnerror("Cannot do a twoway merge of%dtrees",2283 o->merge_size);22842285if(oldtree == o->df_conflict_entry)2286 oldtree = NULL;2287if(newtree == o->df_conflict_entry)2288 newtree = NULL;22892290if(current) {2291if(current->ce_flags & CE_CONFLICTED) {2292if(same(oldtree, newtree) || o->reset) {2293if(!newtree)2294returndeleted_entry(current, current, o);2295else2296returnmerged_entry(newtree, current, o);2297}2298returnreject_merge(current, o);2299}else if((!oldtree && !newtree) ||/* 4 and 5 */2300(!oldtree && newtree &&2301same(current, newtree)) ||/* 6 and 7 */2302(oldtree && newtree &&2303same(oldtree, newtree)) ||/* 14 and 15 */2304(oldtree && newtree &&2305!same(oldtree, newtree) &&/* 18 and 19 */2306same(current, newtree))) {2307returnkeep_entry(current, o);2308}else if(oldtree && !newtree &&same(current, oldtree)) {2309/* 10 or 11 */2310returndeleted_entry(oldtree, current, o);2311}else if(oldtree && newtree &&2312same(current, oldtree) && !same(current, newtree)) {2313/* 20 or 21 */2314returnmerged_entry(newtree, current, o);2315}else2316returnreject_merge(current, o);2317}2318else if(newtree) {2319if(oldtree && !o->initial_checkout) {2320/*2321 * deletion of the path was staged;2322 */2323if(same(oldtree, newtree))2324return1;2325returnreject_merge(oldtree, o);2326}2327returnmerged_entry(newtree, current, o);2328}2329returndeleted_entry(oldtree, current, o);2330}23312332/*2333 * Bind merge.2334 *2335 * Keep the index entries at stage0, collapse stage1 but make sure2336 * stage0 does not have anything there.2337 */2338intbind_merge(const struct cache_entry *const*src,2339struct unpack_trees_options *o)2340{2341const struct cache_entry *old = src[0];2342const struct cache_entry *a = src[1];23432344if(o->merge_size !=1)2345returnerror("Cannot do a bind merge of%dtrees",2346 o->merge_size);2347if(a && old)2348return o->gently ? -1:2349error(ERRORMSG(o, ERROR_BIND_OVERLAP),2350super_prefixed(a->name),2351super_prefixed(old->name));2352if(!a)2353returnkeep_entry(old, o);2354else2355returnmerged_entry(a, NULL, o);2356}23572358/*2359 * One-way merge.2360 *2361 * The rule is:2362 * - take the stat information from stage0, take the data from stage12363 */2364intoneway_merge(const struct cache_entry *const*src,2365struct unpack_trees_options *o)2366{2367const struct cache_entry *old = src[0];2368const struct cache_entry *a = src[1];23692370if(o->merge_size !=1)2371returnerror("Cannot do a oneway merge of%dtrees",2372 o->merge_size);23732374if(!a || a == o->df_conflict_entry)2375returndeleted_entry(old, old, o);23762377if(old &&same(old, a)) {2378int update =0;2379if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2380struct stat st;2381if(lstat(old->name, &st) ||2382ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2383 update |= CE_UPDATE;2384}2385if(o->update &&S_ISGITLINK(old->ce_mode) &&2386should_update_submodules() && !verify_uptodate(old, o))2387 update |= CE_UPDATE;2388add_entry(o, old, update, CE_STAGEMASK);2389return0;2390}2391returnmerged_entry(a, old, o);2392}