1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"config.h" 4#include"dir.h" 5#include"tree.h" 6#include"tree-walk.h" 7#include"cache-tree.h" 8#include"unpack-trees.h" 9#include"progress.h" 10#include"refs.h" 11#include"attr.h" 12#include"split-index.h" 13#include"dir.h" 14#include"submodule.h" 15#include"submodule-config.h" 16 17/* 18 * Error messages expected by scripts out of plumbing commands such as 19 * read-tree. Non-scripted Porcelain is not required to use these messages 20 * and in fact are encouraged to reword them to better suit their particular 21 * situation better. See how "git checkout" and "git merge" replaces 22 * them using setup_unpack_trees_porcelain(), for example. 23 */ 24static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 25/* ERROR_WOULD_OVERWRITE */ 26"Entry '%s' would be overwritten by merge. Cannot merge.", 27 28/* ERROR_NOT_UPTODATE_FILE */ 29"Entry '%s' not uptodate. Cannot merge.", 30 31/* ERROR_NOT_UPTODATE_DIR */ 32"Updating '%s' would lose untracked files in it", 33 34/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 35"Untracked working tree file '%s' would be overwritten by merge.", 36 37/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 38"Untracked working tree file '%s' would be removed by merge.", 39 40/* ERROR_BIND_OVERLAP */ 41"Entry '%s' overlaps with '%s'. Cannot bind.", 42 43/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 44"Entry '%s' not uptodate. Cannot update sparse checkout.", 45 46/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 47"Working tree file '%s' would be overwritten by sparse checkout update.", 48 49/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 50"Working tree file '%s' would be removed by sparse checkout update.", 51 52/* ERROR_WOULD_LOSE_SUBMODULE */ 53"Submodule '%s' cannot checkout new HEAD.", 54}; 55 56#define ERRORMSG(o,type) \ 57 ( ((o) && (o)->msgs[(type)]) \ 58 ? ((o)->msgs[(type)]) \ 59 : (unpack_plumbing_errors[(type)]) ) 60 61static const char*super_prefixed(const char*path) 62{ 63/* 64 * It is necessary and sufficient to have two static buffers 65 * here, as the return value of this function is fed to 66 * error() using the unpack_*_errors[] templates we see above. 67 */ 68static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 69static int super_prefix_len = -1; 70static unsigned idx =ARRAY_SIZE(buf) -1; 71 72if(super_prefix_len <0) { 73const char*super_prefix =get_super_prefix(); 74if(!super_prefix) { 75 super_prefix_len =0; 76}else{ 77int i; 78for(i =0; i <ARRAY_SIZE(buf); i++) 79strbuf_addstr(&buf[i], super_prefix); 80 super_prefix_len = buf[0].len; 81} 82} 83 84if(!super_prefix_len) 85return path; 86 87if(++idx >=ARRAY_SIZE(buf)) 88 idx =0; 89 90strbuf_setlen(&buf[idx], super_prefix_len); 91strbuf_addstr(&buf[idx], path); 92 93return buf[idx].buf; 94} 95 96voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 97const char*cmd) 98{ 99int i; 100const char**msgs = opts->msgs; 101const char*msg; 102 103if(!strcmp(cmd,"checkout")) 104 msg = advice_commit_before_merge 105?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 106"Please commit your changes or stash them before you switch branches.") 107:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 108else if(!strcmp(cmd,"merge")) 109 msg = advice_commit_before_merge 110?_("Your local changes to the following files would be overwritten by merge:\n%%s" 111"Please commit your changes or stash them before you merge.") 112:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 113else 114 msg = advice_commit_before_merge 115?_("Your local changes to the following files would be overwritten by%s:\n%%s" 116"Please commit your changes or stash them before you%s.") 117:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 118 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 119xstrfmt(msg, cmd, cmd); 120 121 msgs[ERROR_NOT_UPTODATE_DIR] = 122_("Updating the following directories would lose untracked files in them:\n%s"); 123 124if(!strcmp(cmd,"checkout")) 125 msg = advice_commit_before_merge 126?_("The following untracked working tree files would be removed by checkout:\n%%s" 127"Please move or remove them before you switch branches.") 128:_("The following untracked working tree files would be removed by checkout:\n%%s"); 129else if(!strcmp(cmd,"merge")) 130 msg = advice_commit_before_merge 131?_("The following untracked working tree files would be removed by merge:\n%%s" 132"Please move or remove them before you merge.") 133:_("The following untracked working tree files would be removed by merge:\n%%s"); 134else 135 msg = advice_commit_before_merge 136?_("The following untracked working tree files would be removed by%s:\n%%s" 137"Please move or remove them before you%s.") 138:_("The following untracked working tree files would be removed by%s:\n%%s"); 139 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 140 141if(!strcmp(cmd,"checkout")) 142 msg = advice_commit_before_merge 143?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 144"Please move or remove them before you switch branches.") 145:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 146else if(!strcmp(cmd,"merge")) 147 msg = advice_commit_before_merge 148?_("The following untracked working tree files would be overwritten by merge:\n%%s" 149"Please move or remove them before you merge.") 150:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 151else 152 msg = advice_commit_before_merge 153?_("The following untracked working tree files would be overwritten by%s:\n%%s" 154"Please move or remove them before you%s.") 155:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 156 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 157 158/* 159 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 160 * cannot easily display it as a list. 161 */ 162 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 163 164 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 165_("Cannot update sparse checkout: the following entries are not up-to-date:\n%s"); 166 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 167_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 168 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 169_("The following working tree files would be removed by sparse checkout update:\n%s"); 170 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 171_("Cannot update submodule:\n%s"); 172 173 opts->show_all_errors =1; 174/* rejected paths may not have a static buffer */ 175for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 176 opts->unpack_rejects[i].strdup_strings =1; 177} 178 179static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 180unsigned int set,unsigned int clear) 181{ 182 clear |= CE_HASHED; 183 184if(set & CE_REMOVE) 185 set |= CE_WT_REMOVE; 186 187 ce->ce_flags = (ce->ce_flags & ~clear) | set; 188returnadd_index_entry(&o->result, ce, 189 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 190} 191 192static struct cache_entry *dup_entry(const struct cache_entry *ce) 193{ 194unsigned int size =ce_size(ce); 195struct cache_entry *new=xmalloc(size); 196 197memcpy(new, ce, size); 198return new; 199} 200 201static voidadd_entry(struct unpack_trees_options *o, 202const struct cache_entry *ce, 203unsigned int set,unsigned int clear) 204{ 205do_add_entry(o,dup_entry(ce), set, clear); 206} 207 208/* 209 * add error messages on path <path> 210 * corresponding to the type <e> with the message <msg> 211 * indicating if it should be display in porcelain or not 212 */ 213static intadd_rejected_path(struct unpack_trees_options *o, 214enum unpack_trees_error_types e, 215const char*path) 216{ 217if(!o->show_all_errors) 218returnerror(ERRORMSG(o, e),super_prefixed(path)); 219 220/* 221 * Otherwise, insert in a list for future display by 222 * display_error_msgs() 223 */ 224string_list_append(&o->unpack_rejects[e], path); 225return-1; 226} 227 228/* 229 * display all the error messages stored in a nice way 230 */ 231static voiddisplay_error_msgs(struct unpack_trees_options *o) 232{ 233int e, i; 234int something_displayed =0; 235for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 236struct string_list *rejects = &o->unpack_rejects[e]; 237if(rejects->nr >0) { 238struct strbuf path = STRBUF_INIT; 239 something_displayed =1; 240for(i =0; i < rejects->nr; i++) 241strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 242error(ERRORMSG(o, e),super_prefixed(path.buf)); 243strbuf_release(&path); 244} 245string_list_clear(rejects,0); 246} 247if(something_displayed) 248fprintf(stderr,_("Aborting\n")); 249} 250 251static intcheck_submodule_move_head(const struct cache_entry *ce, 252const char*old_id, 253const char*new_id, 254struct unpack_trees_options *o) 255{ 256unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 257const struct submodule *sub =submodule_from_ce(ce); 258if(!sub) 259return0; 260 261if(o->reset) 262 flags |= SUBMODULE_MOVE_HEAD_FORCE; 263 264switch(sub->update_strategy.type) { 265case SM_UPDATE_UNSPECIFIED: 266case SM_UPDATE_CHECKOUT: 267if(submodule_move_head(ce->name, old_id, new_id, flags)) 268return o->gently ? -1: 269add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 270return0; 271case SM_UPDATE_NONE: 272return0; 273case SM_UPDATE_REBASE: 274case SM_UPDATE_MERGE: 275case SM_UPDATE_COMMAND: 276default: 277warning(_("submodule update strategy not supported for submodule '%s'"), ce->name); 278return-1; 279} 280} 281 282static voidreload_gitmodules_file(struct index_state *index, 283struct checkout *state) 284{ 285int i; 286for(i =0; i < index->cache_nr; i++) { 287struct cache_entry *ce = index->cache[i]; 288if(ce->ce_flags & CE_UPDATE) { 289int r =strcmp(ce->name,".gitmodules"); 290if(r <0) 291continue; 292else if(r ==0) { 293submodule_free(); 294checkout_entry(ce, state, NULL); 295gitmodules_config(); 296git_config(submodule_config, NULL); 297}else 298break; 299} 300} 301} 302 303/* 304 * Unlink the last component and schedule the leading directories for 305 * removal, such that empty directories get removed. 306 */ 307static voidunlink_entry(const struct cache_entry *ce) 308{ 309const struct submodule *sub =submodule_from_ce(ce); 310if(sub) { 311switch(sub->update_strategy.type) { 312case SM_UPDATE_UNSPECIFIED: 313case SM_UPDATE_CHECKOUT: 314case SM_UPDATE_REBASE: 315case SM_UPDATE_MERGE: 316/* state.force is set at the caller. */ 317submodule_move_head(ce->name,"HEAD", NULL, 318 SUBMODULE_MOVE_HEAD_FORCE); 319break; 320case SM_UPDATE_NONE: 321case SM_UPDATE_COMMAND: 322return;/* Do not touch the submodule. */ 323} 324} 325if(!check_leading_path(ce->name,ce_namelen(ce))) 326return; 327if(remove_or_warn(ce->ce_mode, ce->name)) 328return; 329schedule_dir_for_removal(ce->name,ce_namelen(ce)); 330} 331 332static struct progress *get_progress(struct unpack_trees_options *o) 333{ 334unsigned cnt =0, total =0; 335struct index_state *index = &o->result; 336 337if(!o->update || !o->verbose_update) 338return NULL; 339 340for(; cnt < index->cache_nr; cnt++) { 341const struct cache_entry *ce = index->cache[cnt]; 342if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 343 total++; 344} 345 346returnstart_progress_delay(_("Checking out files"), 347 total,50,1); 348} 349 350static intcheck_updates(struct unpack_trees_options *o) 351{ 352unsigned cnt =0; 353int errs =0; 354struct progress *progress = NULL; 355struct index_state *index = &o->result; 356struct checkout state = CHECKOUT_INIT; 357int i; 358 359 state.force =1; 360 state.quiet =1; 361 state.refresh_cache =1; 362 state.istate = index; 363 364 progress =get_progress(o); 365 366if(o->update) 367git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 368for(i =0; i < index->cache_nr; i++) { 369const struct cache_entry *ce = index->cache[i]; 370 371if(ce->ce_flags & CE_WT_REMOVE) { 372display_progress(progress, ++cnt); 373if(o->update && !o->dry_run) 374unlink_entry(ce); 375} 376} 377remove_marked_cache_entries(index); 378remove_scheduled_dirs(); 379 380if(should_update_submodules() && o->update && !o->dry_run) 381reload_gitmodules_file(index, &state); 382 383enable_delayed_checkout(&state); 384for(i =0; i < index->cache_nr; i++) { 385struct cache_entry *ce = index->cache[i]; 386 387if(ce->ce_flags & CE_UPDATE) { 388if(ce->ce_flags & CE_WT_REMOVE) 389die("BUG: both update and delete flags are set on%s", 390 ce->name); 391display_progress(progress, ++cnt); 392 ce->ce_flags &= ~CE_UPDATE; 393if(o->update && !o->dry_run) { 394 errs |=checkout_entry(ce, &state, NULL); 395} 396} 397} 398 errs |=finish_delayed_checkout(&state); 399stop_progress(&progress); 400if(o->update) 401git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 402return errs !=0; 403} 404 405static intverify_uptodate_sparse(const struct cache_entry *ce, 406struct unpack_trees_options *o); 407static intverify_absent_sparse(const struct cache_entry *ce, 408enum unpack_trees_error_types, 409struct unpack_trees_options *o); 410 411static intapply_sparse_checkout(struct index_state *istate, 412struct cache_entry *ce, 413struct unpack_trees_options *o) 414{ 415int was_skip_worktree =ce_skip_worktree(ce); 416 417if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 418 ce->ce_flags |= CE_SKIP_WORKTREE; 419else 420 ce->ce_flags &= ~CE_SKIP_WORKTREE; 421if(was_skip_worktree !=ce_skip_worktree(ce)) { 422 ce->ce_flags |= CE_UPDATE_IN_BASE; 423 istate->cache_changed |= CE_ENTRY_CHANGED; 424} 425 426/* 427 * if (!was_skip_worktree && !ce_skip_worktree()) { 428 * This is perfectly normal. Move on; 429 * } 430 */ 431 432/* 433 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 434 * area as a result of ce_skip_worktree() shortcuts in 435 * verify_absent() and verify_uptodate(). 436 * Make sure they don't modify worktree if they are already 437 * outside checkout area 438 */ 439if(was_skip_worktree &&ce_skip_worktree(ce)) { 440 ce->ce_flags &= ~CE_UPDATE; 441 442/* 443 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 444 * on to get that file removed from both index and worktree. 445 * If that file is already outside worktree area, don't 446 * bother remove it. 447 */ 448if(ce->ce_flags & CE_REMOVE) 449 ce->ce_flags &= ~CE_WT_REMOVE; 450} 451 452if(!was_skip_worktree &&ce_skip_worktree(ce)) { 453/* 454 * If CE_UPDATE is set, verify_uptodate() must be called already 455 * also stat info may have lost after merged_entry() so calling 456 * verify_uptodate() again may fail 457 */ 458if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 459return-1; 460 ce->ce_flags |= CE_WT_REMOVE; 461 ce->ce_flags &= ~CE_UPDATE; 462} 463if(was_skip_worktree && !ce_skip_worktree(ce)) { 464if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 465return-1; 466 ce->ce_flags |= CE_UPDATE; 467} 468return0; 469} 470 471staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 472struct unpack_trees_options *o) 473{ 474int ret = o->fn(src, o); 475if(ret >0) 476 ret =0; 477return ret; 478} 479 480static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 481{ 482 ce->ce_flags |= CE_UNPACKED; 483 484if(o->cache_bottom < o->src_index->cache_nr && 485 o->src_index->cache[o->cache_bottom] == ce) { 486int bottom = o->cache_bottom; 487while(bottom < o->src_index->cache_nr && 488 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 489 bottom++; 490 o->cache_bottom = bottom; 491} 492} 493 494static voidmark_all_ce_unused(struct index_state *index) 495{ 496int i; 497for(i =0; i < index->cache_nr; i++) 498 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 499} 500 501static intlocate_in_src_index(const struct cache_entry *ce, 502struct unpack_trees_options *o) 503{ 504struct index_state *index = o->src_index; 505int len =ce_namelen(ce); 506int pos =index_name_pos(index, ce->name, len); 507if(pos <0) 508 pos = -1- pos; 509return pos; 510} 511 512/* 513 * We call unpack_index_entry() with an unmerged cache entry 514 * only in diff-index, and it wants a single callback. Skip 515 * the other unmerged entry with the same name. 516 */ 517static voidmark_ce_used_same_name(struct cache_entry *ce, 518struct unpack_trees_options *o) 519{ 520struct index_state *index = o->src_index; 521int len =ce_namelen(ce); 522int pos; 523 524for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 525struct cache_entry *next = index->cache[pos]; 526if(len !=ce_namelen(next) || 527memcmp(ce->name, next->name, len)) 528break; 529mark_ce_used(next, o); 530} 531} 532 533static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 534{ 535const struct index_state *index = o->src_index; 536int pos = o->cache_bottom; 537 538while(pos < index->cache_nr) { 539struct cache_entry *ce = index->cache[pos]; 540if(!(ce->ce_flags & CE_UNPACKED)) 541return ce; 542 pos++; 543} 544return NULL; 545} 546 547static voidadd_same_unmerged(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); 553 554if(0<= pos) 555die("programming error in a caller of mark_ce_used_same_name"); 556for(pos = -pos -1; pos < index->cache_nr; pos++) { 557struct cache_entry *next = index->cache[pos]; 558if(len !=ce_namelen(next) || 559memcmp(ce->name, next->name, len)) 560break; 561add_entry(o, next,0,0); 562mark_ce_used(next, o); 563} 564} 565 566static intunpack_index_entry(struct cache_entry *ce, 567struct unpack_trees_options *o) 568{ 569const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 570int ret; 571 572 src[0] = ce; 573 574mark_ce_used(ce, o); 575if(ce_stage(ce)) { 576if(o->skip_unmerged) { 577add_entry(o, ce,0,0); 578return0; 579} 580} 581 ret =call_unpack_fn(src, o); 582if(ce_stage(ce)) 583mark_ce_used_same_name(ce, o); 584return ret; 585} 586 587static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 588 589static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 590{ 591struct unpack_trees_options *o = info->data; 592 593if(o->diff_index_cached) 594return; 595 o->cache_bottom = bottom; 596} 597 598static intswitch_cache_bottom(struct traverse_info *info) 599{ 600struct unpack_trees_options *o = info->data; 601int ret, pos; 602 603if(o->diff_index_cached) 604return0; 605 ret = o->cache_bottom; 606 pos =find_cache_pos(info->prev, &info->name); 607 608if(pos < -1) 609 o->cache_bottom = -2- pos; 610else if(pos <0) 611 o->cache_bottom = o->src_index->cache_nr; 612return ret; 613} 614 615staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 616{ 617return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 618} 619 620static inttraverse_trees_recursive(int n,unsigned long dirmask, 621unsigned long df_conflicts, 622struct name_entry *names, 623struct traverse_info *info) 624{ 625int i, ret, bottom; 626int nr_buf =0; 627struct tree_desc t[MAX_UNPACK_TREES]; 628void*buf[MAX_UNPACK_TREES]; 629struct traverse_info newinfo; 630struct name_entry *p; 631 632 p = names; 633while(!p->mode) 634 p++; 635 636 newinfo = *info; 637 newinfo.prev = info; 638 newinfo.pathspec = info->pathspec; 639 newinfo.name = *p; 640 newinfo.pathlen +=tree_entry_len(p) +1; 641 newinfo.df_conflicts |= df_conflicts; 642 643/* 644 * Fetch the tree from the ODB for each peer directory in the 645 * n commits. 646 * 647 * For 2- and 3-way traversals, we try to avoid hitting the 648 * ODB twice for the same OID. This should yield a nice speed 649 * up in checkouts and merges when the commits are similar. 650 * 651 * We don't bother doing the full O(n^2) search for larger n, 652 * because wider traversals don't happen that often and we 653 * avoid the search setup. 654 * 655 * When 2 peer OIDs are the same, we just copy the tree 656 * descriptor data. This implicitly borrows the buffer 657 * data from the earlier cell. 658 */ 659for(i =0; i < n; i++, dirmask >>=1) { 660if(i >0&&are_same_oid(&names[i], &names[i -1])) 661 t[i] = t[i -1]; 662else if(i >1&&are_same_oid(&names[i], &names[i -2])) 663 t[i] = t[i -2]; 664else{ 665const unsigned char*sha1 = NULL; 666if(dirmask &1) 667 sha1 = names[i].oid->hash; 668 buf[nr_buf++] =fill_tree_descriptor(t+i, sha1); 669} 670} 671 672 bottom =switch_cache_bottom(&newinfo); 673 ret =traverse_trees(n, t, &newinfo); 674restore_cache_bottom(&newinfo, bottom); 675 676for(i =0; i < nr_buf; i++) 677free(buf[i]); 678 679return ret; 680} 681 682/* 683 * Compare the traverse-path to the cache entry without actually 684 * having to generate the textual representation of the traverse 685 * path. 686 * 687 * NOTE! This *only* compares up to the size of the traverse path 688 * itself - the caller needs to do the final check for the cache 689 * entry having more data at the end! 690 */ 691static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 692{ 693int len, pathlen, ce_len; 694const char*ce_name; 695 696if(info->prev) { 697int cmp =do_compare_entry_piecewise(ce, info->prev, 698&info->name); 699if(cmp) 700return cmp; 701} 702 pathlen = info->pathlen; 703 ce_len =ce_namelen(ce); 704 705/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 706if(ce_len < pathlen) 707return-1; 708 709 ce_len -= pathlen; 710 ce_name = ce->name + pathlen; 711 712 len =tree_entry_len(n); 713returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 714} 715 716static intdo_compare_entry(const struct cache_entry *ce, 717const struct traverse_info *info, 718const struct name_entry *n) 719{ 720int len, pathlen, ce_len; 721const char*ce_name; 722int cmp; 723 724/* 725 * If we have not precomputed the traverse path, it is quicker 726 * to avoid doing so. But if we have precomputed it, 727 * it is quicker to use the precomputed version. 728 */ 729if(!info->traverse_path) 730returndo_compare_entry_piecewise(ce, info, n); 731 732 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 733if(cmp) 734return cmp; 735 736 pathlen = info->pathlen; 737 ce_len =ce_namelen(ce); 738 739if(ce_len < pathlen) 740return-1; 741 742 ce_len -= pathlen; 743 ce_name = ce->name + pathlen; 744 745 len =tree_entry_len(n); 746returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 747} 748 749static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 750{ 751int cmp =do_compare_entry(ce, info, n); 752if(cmp) 753return cmp; 754 755/* 756 * Even if the beginning compared identically, the ce should 757 * compare as bigger than a directory leading up to it! 758 */ 759returnce_namelen(ce) >traverse_path_len(info, n); 760} 761 762static intce_in_traverse_path(const struct cache_entry *ce, 763const struct traverse_info *info) 764{ 765if(!info->prev) 766return1; 767if(do_compare_entry(ce, info->prev, &info->name)) 768return0; 769/* 770 * If ce (blob) is the same name as the path (which is a tree 771 * we will be descending into), it won't be inside it. 772 */ 773return(info->pathlen <ce_namelen(ce)); 774} 775 776static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 777{ 778int len =traverse_path_len(info, n); 779struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 780 781 ce->ce_mode =create_ce_mode(n->mode); 782 ce->ce_flags =create_ce_flags(stage); 783 ce->ce_namelen = len; 784oidcpy(&ce->oid, n->oid); 785make_traverse_path(ce->name, info, n); 786 787return ce; 788} 789 790static intunpack_nondirectories(int n,unsigned long mask, 791unsigned long dirmask, 792struct cache_entry **src, 793const struct name_entry *names, 794const struct traverse_info *info) 795{ 796int i; 797struct unpack_trees_options *o = info->data; 798unsigned long conflicts = info->df_conflicts | dirmask; 799 800/* Do we have *only* directories? Nothing to do */ 801if(mask == dirmask && !src[0]) 802return0; 803 804/* 805 * Ok, we've filled in up to any potential index entry in src[0], 806 * now do the rest. 807 */ 808for(i =0; i < n; i++) { 809int stage; 810unsigned int bit =1ul<< i; 811if(conflicts & bit) { 812 src[i + o->merge] = o->df_conflict_entry; 813continue; 814} 815if(!(mask & bit)) 816continue; 817if(!o->merge) 818 stage =0; 819else if(i +1< o->head_idx) 820 stage =1; 821else if(i +1> o->head_idx) 822 stage =3; 823else 824 stage =2; 825 src[i + o->merge] =create_ce_entry(info, names + i, stage); 826} 827 828if(o->merge) { 829int rc =call_unpack_fn((const struct cache_entry *const*)src, 830 o); 831for(i =0; i < n; i++) { 832struct cache_entry *ce = src[i + o->merge]; 833if(ce != o->df_conflict_entry) 834free(ce); 835} 836return rc; 837} 838 839for(i =0; i < n; i++) 840if(src[i] && src[i] != o->df_conflict_entry) 841if(do_add_entry(o, src[i],0,0)) 842return-1; 843 844return0; 845} 846 847static intunpack_failed(struct unpack_trees_options *o,const char*message) 848{ 849discard_index(&o->result); 850if(!o->gently && !o->exiting_early) { 851if(message) 852returnerror("%s", message); 853return-1; 854} 855return-1; 856} 857 858/* 859 * The tree traversal is looking at name p. If we have a matching entry, 860 * return it. If name p is a directory in the index, do not return 861 * anything, as we will want to match it when the traversal descends into 862 * the directory. 863 */ 864static intfind_cache_pos(struct traverse_info *info, 865const struct name_entry *p) 866{ 867int pos; 868struct unpack_trees_options *o = info->data; 869struct index_state *index = o->src_index; 870int pfxlen = info->pathlen; 871int p_len =tree_entry_len(p); 872 873for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 874const struct cache_entry *ce = index->cache[pos]; 875const char*ce_name, *ce_slash; 876int cmp, ce_len; 877 878if(ce->ce_flags & CE_UNPACKED) { 879/* 880 * cache_bottom entry is already unpacked, so 881 * we can never match it; don't check it 882 * again. 883 */ 884if(pos == o->cache_bottom) 885++o->cache_bottom; 886continue; 887} 888if(!ce_in_traverse_path(ce, info)) { 889/* 890 * Check if we can skip future cache checks 891 * (because we're already past all possible 892 * entries in the traverse path). 893 */ 894if(info->traverse_path) { 895if(strncmp(ce->name, info->traverse_path, 896 info->pathlen) >0) 897break; 898} 899continue; 900} 901 ce_name = ce->name + pfxlen; 902 ce_slash =strchr(ce_name,'/'); 903if(ce_slash) 904 ce_len = ce_slash - ce_name; 905else 906 ce_len =ce_namelen(ce) - pfxlen; 907 cmp =name_compare(p->path, p_len, ce_name, ce_len); 908/* 909 * Exact match; if we have a directory we need to 910 * delay returning it. 911 */ 912if(!cmp) 913return ce_slash ? -2- pos : pos; 914if(0< cmp) 915continue;/* keep looking */ 916/* 917 * ce_name sorts after p->path; could it be that we 918 * have files under p->path directory in the index? 919 * E.g. ce_name == "t-i", and p->path == "t"; we may 920 * have "t/a" in the index. 921 */ 922if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 923 ce_name[p_len] <'/') 924continue;/* keep looking */ 925break; 926} 927return-1; 928} 929 930static struct cache_entry *find_cache_entry(struct traverse_info *info, 931const struct name_entry *p) 932{ 933int pos =find_cache_pos(info, p); 934struct unpack_trees_options *o = info->data; 935 936if(0<= pos) 937return o->src_index->cache[pos]; 938else 939return NULL; 940} 941 942static voiddebug_path(struct traverse_info *info) 943{ 944if(info->prev) { 945debug_path(info->prev); 946if(*info->prev->name.path) 947putchar('/'); 948} 949printf("%s", info->name.path); 950} 951 952static voiddebug_name_entry(int i,struct name_entry *n) 953{ 954printf("ent#%d %06o%s\n", i, 955 n->path ? n->mode :0, 956 n->path ? n->path :"(missing)"); 957} 958 959static voiddebug_unpack_callback(int n, 960unsigned long mask, 961unsigned long dirmask, 962struct name_entry *names, 963struct traverse_info *info) 964{ 965int i; 966printf("* unpack mask%lu, dirmask%lu, cnt%d", 967 mask, dirmask, n); 968debug_path(info); 969putchar('\n'); 970for(i =0; i < n; i++) 971debug_name_entry(i, names + i); 972} 973 974static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 975{ 976struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 977struct unpack_trees_options *o = info->data; 978const struct name_entry *p = names; 979 980/* Find first entry with a real name (we could use "mask" too) */ 981while(!p->mode) 982 p++; 983 984if(o->debug_unpack) 985debug_unpack_callback(n, mask, dirmask, names, info); 986 987/* Are we supposed to look at the index too? */ 988if(o->merge) { 989while(1) { 990int cmp; 991struct cache_entry *ce; 992 993if(o->diff_index_cached) 994 ce =next_cache_entry(o); 995else 996 ce =find_cache_entry(info, p); 997 998if(!ce) 999break;1000 cmp =compare_entry(ce, info, p);1001if(cmp <0) {1002if(unpack_index_entry(ce, o) <0)1003returnunpack_failed(o, NULL);1004continue;1005}1006if(!cmp) {1007if(ce_stage(ce)) {1008/*1009 * If we skip unmerged index1010 * entries, we'll skip this1011 * entry *and* the tree1012 * entries associated with it!1013 */1014if(o->skip_unmerged) {1015add_same_unmerged(ce, o);1016return mask;1017}1018}1019 src[0] = ce;1020}1021break;1022}1023}10241025if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1026return-1;10271028if(o->merge && src[0]) {1029if(ce_stage(src[0]))1030mark_ce_used_same_name(src[0], o);1031else1032mark_ce_used(src[0], o);1033}10341035/* Now handle any directories.. */1036if(dirmask) {1037/* special case: "diff-index --cached" looking at a tree */1038if(o->diff_index_cached &&1039 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1040int matches;1041 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1042 names, info);1043/*1044 * Everything under the name matches; skip the1045 * entire hierarchy. diff_index_cached codepath1046 * special cases D/F conflicts in such a way that1047 * it does not do any look-ahead, so this is safe.1048 */1049if(matches) {1050 o->cache_bottom += matches;1051return mask;1052}1053}10541055if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1056 names, info) <0)1057return-1;1058return mask;1059}10601061return mask;1062}10631064static intclear_ce_flags_1(struct cache_entry **cache,int nr,1065struct strbuf *prefix,1066int select_mask,int clear_mask,1067struct exclude_list *el,int defval);10681069/* Whole directory matching */1070static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1071struct strbuf *prefix,1072char*basename,1073int select_mask,int clear_mask,1074struct exclude_list *el,int defval)1075{1076struct cache_entry **cache_end;1077int dtype = DT_DIR;1078int ret =is_excluded_from_list(prefix->buf, prefix->len,1079 basename, &dtype, el, &the_index);1080int rc;10811082strbuf_addch(prefix,'/');10831084/* If undecided, use matching result of parent dir in defval */1085if(ret <0)1086 ret = defval;10871088for(cache_end = cache; cache_end != cache + nr; cache_end++) {1089struct cache_entry *ce = *cache_end;1090if(strncmp(ce->name, prefix->buf, prefix->len))1091break;1092}10931094/*1095 * TODO: check el, if there are no patterns that may conflict1096 * with ret (iow, we know in advance the incl/excl1097 * decision for the entire directory), clear flag here without1098 * calling clear_ce_flags_1(). That function will call1099 * the expensive is_excluded_from_list() on every entry.1100 */1101 rc =clear_ce_flags_1(cache, cache_end - cache,1102 prefix,1103 select_mask, clear_mask,1104 el, ret);1105strbuf_setlen(prefix, prefix->len -1);1106return rc;1107}11081109/*1110 * Traverse the index, find every entry that matches according to1111 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1112 * number of traversed entries.1113 *1114 * If select_mask is non-zero, only entries whose ce_flags has on of1115 * those bits enabled are traversed.1116 *1117 * cache : pointer to an index entry1118 * prefix_len : an offset to its path1119 *1120 * The current path ("prefix") including the trailing '/' is1121 * cache[0]->name[0..(prefix_len-1)]1122 * Top level path has prefix_len zero.1123 */1124static intclear_ce_flags_1(struct cache_entry **cache,int nr,1125struct strbuf *prefix,1126int select_mask,int clear_mask,1127struct exclude_list *el,int defval)1128{1129struct cache_entry **cache_end = cache + nr;11301131/*1132 * Process all entries that have the given prefix and meet1133 * select_mask condition1134 */1135while(cache != cache_end) {1136struct cache_entry *ce = *cache;1137const char*name, *slash;1138int len, dtype, ret;11391140if(select_mask && !(ce->ce_flags & select_mask)) {1141 cache++;1142continue;1143}11441145if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1146break;11471148 name = ce->name + prefix->len;1149 slash =strchr(name,'/');11501151/* If it's a directory, try whole directory match first */1152if(slash) {1153int processed;11541155 len = slash - name;1156strbuf_add(prefix, name, len);11571158 processed =clear_ce_flags_dir(cache, cache_end - cache,1159 prefix,1160 prefix->buf + prefix->len - len,1161 select_mask, clear_mask,1162 el, defval);11631164/* clear_c_f_dir eats a whole dir already? */1165if(processed) {1166 cache += processed;1167strbuf_setlen(prefix, prefix->len - len);1168continue;1169}11701171strbuf_addch(prefix,'/');1172 cache +=clear_ce_flags_1(cache, cache_end - cache,1173 prefix,1174 select_mask, clear_mask, el, defval);1175strbuf_setlen(prefix, prefix->len - len -1);1176continue;1177}11781179/* Non-directory */1180 dtype =ce_to_dtype(ce);1181 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1182 name, &dtype, el, &the_index);1183if(ret <0)1184 ret = defval;1185if(ret >0)1186 ce->ce_flags &= ~clear_mask;1187 cache++;1188}1189return nr - (cache_end - cache);1190}11911192static intclear_ce_flags(struct cache_entry **cache,int nr,1193int select_mask,int clear_mask,1194struct exclude_list *el)1195{1196static struct strbuf prefix = STRBUF_INIT;11971198strbuf_reset(&prefix);11991200returnclear_ce_flags_1(cache, nr,1201&prefix,1202 select_mask, clear_mask,1203 el,0);1204}12051206/*1207 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1208 */1209static voidmark_new_skip_worktree(struct exclude_list *el,1210struct index_state *the_index,1211int select_flag,int skip_wt_flag)1212{1213int i;12141215/*1216 * 1. Pretend the narrowest worktree: only unmerged entries1217 * are checked out1218 */1219for(i =0; i < the_index->cache_nr; i++) {1220struct cache_entry *ce = the_index->cache[i];12211222if(select_flag && !(ce->ce_flags & select_flag))1223continue;12241225if(!ce_stage(ce))1226 ce->ce_flags |= skip_wt_flag;1227else1228 ce->ce_flags &= ~skip_wt_flag;1229}12301231/*1232 * 2. Widen worktree according to sparse-checkout file.1233 * Matched entries will have skip_wt_flag cleared (i.e. "in")1234 */1235clear_ce_flags(the_index->cache, the_index->cache_nr,1236 select_flag, skip_wt_flag, el);1237}12381239static intverify_absent(const struct cache_entry *,1240enum unpack_trees_error_types,1241struct unpack_trees_options *);1242/*1243 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1244 * resulting index, -2 on failure to reflect the changes to the work tree.1245 *1246 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1247 */1248intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1249{1250int i, ret;1251static struct cache_entry *dfc;1252struct exclude_list el;12531254if(len > MAX_UNPACK_TREES)1255die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12561257memset(&el,0,sizeof(el));1258if(!core_apply_sparse_checkout || !o->update)1259 o->skip_sparse_checkout =1;1260if(!o->skip_sparse_checkout) {1261char*sparse =git_pathdup("info/sparse-checkout");1262if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1263 o->skip_sparse_checkout =1;1264else1265 o->el = ⪙1266free(sparse);1267}12681269memset(&o->result,0,sizeof(o->result));1270 o->result.initialized =1;1271 o->result.timestamp.sec = o->src_index->timestamp.sec;1272 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1273 o->result.version = o->src_index->version;1274 o->result.split_index = o->src_index->split_index;1275if(o->result.split_index)1276 o->result.split_index->refcount++;1277hashcpy(o->result.sha1, o->src_index->sha1);1278 o->merge_size = len;1279mark_all_ce_unused(o->src_index);12801281/*1282 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1283 */1284if(!o->skip_sparse_checkout)1285mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);12861287if(!dfc)1288 dfc =xcalloc(1,cache_entry_size(0));1289 o->df_conflict_entry = dfc;12901291if(len) {1292const char*prefix = o->prefix ? o->prefix :"";1293struct traverse_info info;12941295setup_traverse_info(&info, prefix);1296 info.fn = unpack_callback;1297 info.data = o;1298 info.show_all_errors = o->show_all_errors;1299 info.pathspec = o->pathspec;13001301if(o->prefix) {1302/*1303 * Unpack existing index entries that sort before the1304 * prefix the tree is spliced into. Note that o->merge1305 * is always true in this case.1306 */1307while(1) {1308struct cache_entry *ce =next_cache_entry(o);1309if(!ce)1310break;1311if(ce_in_traverse_path(ce, &info))1312break;1313if(unpack_index_entry(ce, o) <0)1314goto return_failed;1315}1316}13171318if(traverse_trees(len, t, &info) <0)1319goto return_failed;1320}13211322/* Any left-over entries in the index? */1323if(o->merge) {1324while(1) {1325struct cache_entry *ce =next_cache_entry(o);1326if(!ce)1327break;1328if(unpack_index_entry(ce, o) <0)1329goto return_failed;1330}1331}1332mark_all_ce_unused(o->src_index);13331334if(o->trivial_merges_only && o->nontrivial_merge) {1335 ret =unpack_failed(o,"Merge requires file-level merging");1336goto done;1337}13381339if(!o->skip_sparse_checkout) {1340int empty_worktree =1;13411342/*1343 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11344 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1345 * so apply_sparse_checkout() won't attempt to remove it from worktree1346 */1347mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13481349 ret =0;1350for(i =0; i < o->result.cache_nr; i++) {1351struct cache_entry *ce = o->result.cache[i];13521353/*1354 * Entries marked with CE_ADDED in merged_entry() do not have1355 * verify_absent() check (the check is effectively disabled1356 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1357 *1358 * Do the real check now because we have had1359 * correct CE_NEW_SKIP_WORKTREE1360 */1361if(ce->ce_flags & CE_ADDED &&1362verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1363if(!o->show_all_errors)1364goto return_failed;1365 ret = -1;1366}13671368if(apply_sparse_checkout(&o->result, ce, o)) {1369if(!o->show_all_errors)1370goto return_failed;1371 ret = -1;1372}1373if(!ce_skip_worktree(ce))1374 empty_worktree =0;13751376}1377if(ret <0)1378goto return_failed;1379/*1380 * Sparse checkout is meant to narrow down checkout area1381 * but it does not make sense to narrow down to empty working1382 * tree. This is usually a mistake in sparse checkout rules.1383 * Do not allow users to do that.1384 */1385if(o->result.cache_nr && empty_worktree) {1386 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1387goto done;1388}1389}13901391 o->src_index = NULL;1392 ret =check_updates(o) ? (-2) :0;1393if(o->dst_index) {1394if(!ret) {1395if(!o->result.cache_tree)1396 o->result.cache_tree =cache_tree();1397if(!cache_tree_fully_valid(o->result.cache_tree))1398cache_tree_update(&o->result,1399 WRITE_TREE_SILENT |1400 WRITE_TREE_REPAIR);1401}1402move_index_extensions(&o->result, o->dst_index);1403discard_index(o->dst_index);1404*o->dst_index = o->result;1405}else{1406discard_index(&o->result);1407}14081409done:1410clear_exclude_list(&el);1411return ret;14121413return_failed:1414if(o->show_all_errors)1415display_error_msgs(o);1416mark_all_ce_unused(o->src_index);1417 ret =unpack_failed(o, NULL);1418if(o->exiting_early)1419 ret =0;1420goto done;1421}14221423/* Here come the merge functions */14241425static intreject_merge(const struct cache_entry *ce,1426struct unpack_trees_options *o)1427{1428return o->gently ? -1:1429add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1430}14311432static intsame(const struct cache_entry *a,const struct cache_entry *b)1433{1434if(!!a != !!b)1435return0;1436if(!a && !b)1437return1;1438if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1439return0;1440return a->ce_mode == b->ce_mode &&1441!oidcmp(&a->oid, &b->oid);1442}144314441445/*1446 * When a CE gets turned into an unmerged entry, we1447 * want it to be up-to-date1448 */1449static intverify_uptodate_1(const struct cache_entry *ce,1450struct unpack_trees_options *o,1451enum unpack_trees_error_types error_type)1452{1453struct stat st;14541455if(o->index_only)1456return0;14571458/*1459 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1460 * if this entry is truly up-to-date because this file may be1461 * overwritten.1462 */1463if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1464;/* keep checking */1465else if(o->reset ||ce_uptodate(ce))1466return0;14671468if(!lstat(ce->name, &st)) {1469int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1470unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);14711472if(submodule_from_ce(ce)) {1473int r =check_submodule_move_head(ce,1474"HEAD",oid_to_hex(&ce->oid), o);1475if(r)1476return o->gently ? -1:1477add_rejected_path(o, error_type, ce->name);1478return0;1479}14801481if(!changed)1482return0;1483/*1484 * Historic default policy was to allow submodule to be out1485 * of sync wrt the superproject index. If the submodule was1486 * not considered interesting above, we don't care here.1487 */1488if(S_ISGITLINK(ce->ce_mode))1489return0;14901491 errno =0;1492}1493if(errno == ENOENT)1494return0;1495return o->gently ? -1:1496add_rejected_path(o, error_type, ce->name);1497}14981499static intverify_uptodate(const struct cache_entry *ce,1500struct unpack_trees_options *o)1501{1502if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1503return0;1504returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1505}15061507static intverify_uptodate_sparse(const struct cache_entry *ce,1508struct unpack_trees_options *o)1509{1510returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1511}15121513static voidinvalidate_ce_path(const struct cache_entry *ce,1514struct unpack_trees_options *o)1515{1516if(!ce)1517return;1518cache_tree_invalidate_path(o->src_index, ce->name);1519untracked_cache_invalidate_path(o->src_index, ce->name);1520}15211522/*1523 * Check that checking out ce->sha1 in subdir ce->name is not1524 * going to overwrite any working files.1525 *1526 * Currently, git does not checkout subprojects during a superproject1527 * checkout, so it is not going to overwrite anything.1528 */1529static intverify_clean_submodule(const char*old_sha1,1530const struct cache_entry *ce,1531enum unpack_trees_error_types error_type,1532struct unpack_trees_options *o)1533{1534if(!submodule_from_ce(ce))1535return0;15361537returncheck_submodule_move_head(ce, old_sha1,1538oid_to_hex(&ce->oid), o);1539}15401541static intverify_clean_subdirectory(const struct cache_entry *ce,1542enum unpack_trees_error_types error_type,1543struct unpack_trees_options *o)1544{1545/*1546 * we are about to extract "ce->name"; we would not want to lose1547 * anything in the existing directory there.1548 */1549int namelen;1550int i;1551struct dir_struct d;1552char*pathbuf;1553int cnt =0;15541555if(S_ISGITLINK(ce->ce_mode)) {1556unsigned char sha1[20];1557int sub_head =resolve_gitlink_ref(ce->name,"HEAD", sha1);1558/*1559 * If we are not going to update the submodule, then1560 * we don't care.1561 */1562if(!sub_head && !hashcmp(sha1, ce->oid.hash))1563return0;1564returnverify_clean_submodule(sub_head ? NULL :sha1_to_hex(sha1),1565 ce, error_type, o);1566}15671568/*1569 * First let's make sure we do not have a local modification1570 * in that directory.1571 */1572 namelen =ce_namelen(ce);1573for(i =locate_in_src_index(ce, o);1574 i < o->src_index->cache_nr;1575 i++) {1576struct cache_entry *ce2 = o->src_index->cache[i];1577int len =ce_namelen(ce2);1578if(len < namelen ||1579strncmp(ce->name, ce2->name, namelen) ||1580 ce2->name[namelen] !='/')1581break;1582/*1583 * ce2->name is an entry in the subdirectory to be1584 * removed.1585 */1586if(!ce_stage(ce2)) {1587if(verify_uptodate(ce2, o))1588return-1;1589add_entry(o, ce2, CE_REMOVE,0);1590mark_ce_used(ce2, o);1591}1592 cnt++;1593}15941595/*1596 * Then we need to make sure that we do not lose a locally1597 * present file that is not ignored.1598 */1599 pathbuf =xstrfmt("%.*s/", namelen, ce->name);16001601memset(&d,0,sizeof(d));1602if(o->dir)1603 d.exclude_per_dir = o->dir->exclude_per_dir;1604 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1605if(i)1606return o->gently ? -1:1607add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1608free(pathbuf);1609return cnt;1610}16111612/*1613 * This gets called when there was no index entry for the tree entry 'dst',1614 * but we found a file in the working tree that 'lstat()' said was fine,1615 * and we're on a case-insensitive filesystem.1616 *1617 * See if we can find a case-insensitive match in the index that also1618 * matches the stat information, and assume it's that other file!1619 */1620static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1621{1622const struct cache_entry *src;16231624 src =index_file_exists(o->src_index, name, len,1);1625return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1626}16271628static intcheck_ok_to_remove(const char*name,int len,int dtype,1629const struct cache_entry *ce,struct stat *st,1630enum unpack_trees_error_types error_type,1631struct unpack_trees_options *o)1632{1633const struct cache_entry *result;16341635/*1636 * It may be that the 'lstat()' succeeded even though1637 * target 'ce' was absent, because there is an old1638 * entry that is different only in case..1639 *1640 * Ignore that lstat() if it matches.1641 */1642if(ignore_case &&icase_exists(o, name, len, st))1643return0;16441645if(o->dir &&1646is_excluded(o->dir, &the_index, name, &dtype))1647/*1648 * ce->name is explicitly excluded, so it is Ok to1649 * overwrite it.1650 */1651return0;1652if(S_ISDIR(st->st_mode)) {1653/*1654 * We are checking out path "foo" and1655 * found "foo/." in the working tree.1656 * This is tricky -- if we have modified1657 * files that are in "foo/" we would lose1658 * them.1659 */1660if(verify_clean_subdirectory(ce, error_type, o) <0)1661return-1;1662return0;1663}16641665/*1666 * The previous round may already have decided to1667 * delete this path, which is in a subdirectory that1668 * is being replaced with a blob.1669 */1670 result =index_file_exists(&o->result, name, len,0);1671if(result) {1672if(result->ce_flags & CE_REMOVE)1673return0;1674}16751676return o->gently ? -1:1677add_rejected_path(o, error_type, name);1678}16791680/*1681 * We do not want to remove or overwrite a working tree file that1682 * is not tracked, unless it is ignored.1683 */1684static intverify_absent_1(const struct cache_entry *ce,1685enum unpack_trees_error_types error_type,1686struct unpack_trees_options *o)1687{1688int len;1689struct stat st;16901691if(o->index_only || o->reset || !o->update)1692return0;16931694 len =check_leading_path(ce->name,ce_namelen(ce));1695if(!len)1696return0;1697else if(len >0) {1698char*path;1699int ret;17001701 path =xmemdupz(ce->name, len);1702if(lstat(path, &st))1703 ret =error_errno("cannot stat '%s'", path);1704else{1705if(submodule_from_ce(ce))1706 ret =check_submodule_move_head(ce,1707oid_to_hex(&ce->oid),1708 NULL, o);1709else1710 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1711&st, error_type, o);1712}1713free(path);1714return ret;1715}else if(lstat(ce->name, &st)) {1716if(errno != ENOENT)1717returnerror_errno("cannot stat '%s'", ce->name);1718return0;1719}else{1720if(submodule_from_ce(ce))1721returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1722 NULL, o);17231724returncheck_ok_to_remove(ce->name,ce_namelen(ce),1725ce_to_dtype(ce), ce, &st,1726 error_type, o);1727}1728}17291730static intverify_absent(const struct cache_entry *ce,1731enum unpack_trees_error_types error_type,1732struct unpack_trees_options *o)1733{1734if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1735return0;1736returnverify_absent_1(ce, error_type, o);1737}17381739static intverify_absent_sparse(const struct cache_entry *ce,1740enum unpack_trees_error_types error_type,1741struct unpack_trees_options *o)1742{1743enum unpack_trees_error_types orphaned_error = error_type;1744if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1745 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17461747returnverify_absent_1(ce, orphaned_error, o);1748}17491750static intmerged_entry(const struct cache_entry *ce,1751const struct cache_entry *old,1752struct unpack_trees_options *o)1753{1754int update = CE_UPDATE;1755struct cache_entry *merge =dup_entry(ce);17561757if(!old) {1758/*1759 * New index entries. In sparse checkout, the following1760 * verify_absent() will be delayed until after1761 * traverse_trees() finishes in unpack_trees(), then:1762 *1763 * - CE_NEW_SKIP_WORKTREE will be computed correctly1764 * - verify_absent() be called again, this time with1765 * correct CE_NEW_SKIP_WORKTREE1766 *1767 * verify_absent() call here does nothing in sparse1768 * checkout (i.e. o->skip_sparse_checkout == 0)1769 */1770 update |= CE_ADDED;1771 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;17721773if(verify_absent(merge,1774 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1775free(merge);1776return-1;1777}1778invalidate_ce_path(merge, o);17791780if(submodule_from_ce(ce)) {1781int ret =check_submodule_move_head(ce, NULL,1782oid_to_hex(&ce->oid),1783 o);1784if(ret)1785return ret;1786}17871788}else if(!(old->ce_flags & CE_CONFLICTED)) {1789/*1790 * See if we can re-use the old CE directly?1791 * That way we get the uptodate stat info.1792 *1793 * This also removes the UPDATE flag on a match; otherwise1794 * we will end up overwriting local changes in the work tree.1795 */1796if(same(old, merge)) {1797copy_cache_entry(merge, old);1798 update =0;1799}else{1800if(verify_uptodate(old, o)) {1801free(merge);1802return-1;1803}1804/* Migrate old flags over */1805 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1806invalidate_ce_path(old, o);1807}18081809if(submodule_from_ce(ce)) {1810int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1811oid_to_hex(&ce->oid),1812 o);1813if(ret)1814return ret;1815}1816}else{1817/*1818 * Previously unmerged entry left as an existence1819 * marker by read_index_unmerged();1820 */1821invalidate_ce_path(old, o);1822}18231824do_add_entry(o, merge, update, CE_STAGEMASK);1825return1;1826}18271828static intdeleted_entry(const struct cache_entry *ce,1829const struct cache_entry *old,1830struct unpack_trees_options *o)1831{1832/* Did it exist in the index? */1833if(!old) {1834if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1835return-1;1836return0;1837}1838if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1839return-1;1840add_entry(o, ce, CE_REMOVE,0);1841invalidate_ce_path(ce, o);1842return1;1843}18441845static intkeep_entry(const struct cache_entry *ce,1846struct unpack_trees_options *o)1847{1848add_entry(o, ce,0,0);1849return1;1850}18511852#if DBRT_DEBUG1853static voidshow_stage_entry(FILE*o,1854const char*label,const struct cache_entry *ce)1855{1856if(!ce)1857fprintf(o,"%s(missing)\n", label);1858else1859fprintf(o,"%s%06o%s %d\t%s\n",1860 label,1861 ce->ce_mode,1862oid_to_hex(&ce->oid),1863ce_stage(ce),1864 ce->name);1865}1866#endif18671868intthreeway_merge(const struct cache_entry *const*stages,1869struct unpack_trees_options *o)1870{1871const struct cache_entry *index;1872const struct cache_entry *head;1873const struct cache_entry *remote = stages[o->head_idx +1];1874int count;1875int head_match =0;1876int remote_match =0;18771878int df_conflict_head =0;1879int df_conflict_remote =0;18801881int any_anc_missing =0;1882int no_anc_exists =1;1883int i;18841885for(i =1; i < o->head_idx; i++) {1886if(!stages[i] || stages[i] == o->df_conflict_entry)1887 any_anc_missing =1;1888else1889 no_anc_exists =0;1890}18911892 index = stages[0];1893 head = stages[o->head_idx];18941895if(head == o->df_conflict_entry) {1896 df_conflict_head =1;1897 head = NULL;1898}18991900if(remote == o->df_conflict_entry) {1901 df_conflict_remote =1;1902 remote = NULL;1903}19041905/*1906 * First, if there's a #16 situation, note that to prevent #131907 * and #14.1908 */1909if(!same(remote, head)) {1910for(i =1; i < o->head_idx; i++) {1911if(same(stages[i], head)) {1912 head_match = i;1913}1914if(same(stages[i], remote)) {1915 remote_match = i;1916}1917}1918}19191920/*1921 * We start with cases where the index is allowed to match1922 * something other than the head: #14(ALT) and #2ALT, where it1923 * is permitted to match the result instead.1924 */1925/* #14, #14ALT, #2ALT */1926if(remote && !df_conflict_head && head_match && !remote_match) {1927if(index && !same(index, remote) && !same(index, head))1928returnreject_merge(index, o);1929returnmerged_entry(remote, index, o);1930}1931/*1932 * If we have an entry in the index cache, then we want to1933 * make sure that it matches head.1934 */1935if(index && !same(index, head))1936returnreject_merge(index, o);19371938if(head) {1939/* #5ALT, #15 */1940if(same(head, remote))1941returnmerged_entry(head, index, o);1942/* #13, #3ALT */1943if(!df_conflict_remote && remote_match && !head_match)1944returnmerged_entry(head, index, o);1945}19461947/* #1 */1948if(!head && !remote && any_anc_missing)1949return0;19501951/*1952 * Under the "aggressive" rule, we resolve mostly trivial1953 * cases that we historically had git-merge-one-file resolve.1954 */1955if(o->aggressive) {1956int head_deleted = !head;1957int remote_deleted = !remote;1958const struct cache_entry *ce = NULL;19591960if(index)1961 ce = index;1962else if(head)1963 ce = head;1964else if(remote)1965 ce = remote;1966else{1967for(i =1; i < o->head_idx; i++) {1968if(stages[i] && stages[i] != o->df_conflict_entry) {1969 ce = stages[i];1970break;1971}1972}1973}19741975/*1976 * Deleted in both.1977 * Deleted in one and unchanged in the other.1978 */1979if((head_deleted && remote_deleted) ||1980(head_deleted && remote && remote_match) ||1981(remote_deleted && head && head_match)) {1982if(index)1983returndeleted_entry(index, index, o);1984if(ce && !head_deleted) {1985if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1986return-1;1987}1988return0;1989}1990/*1991 * Added in both, identically.1992 */1993if(no_anc_exists && head && remote &&same(head, remote))1994returnmerged_entry(head, index, o);19951996}19971998/* Below are "no merge" cases, which require that the index be1999 * up-to-date to avoid the files getting overwritten with2000 * conflict resolution files.2001 */2002if(index) {2003if(verify_uptodate(index, o))2004return-1;2005}20062007 o->nontrivial_merge =1;20082009/* #2, #3, #4, #6, #7, #9, #10, #11. */2010 count =0;2011if(!head_match || !remote_match) {2012for(i =1; i < o->head_idx; i++) {2013if(stages[i] && stages[i] != o->df_conflict_entry) {2014keep_entry(stages[i], o);2015 count++;2016break;2017}2018}2019}2020#if DBRT_DEBUG2021else{2022fprintf(stderr,"read-tree: warning #16 detected\n");2023show_stage_entry(stderr,"head ", stages[head_match]);2024show_stage_entry(stderr,"remote ", stages[remote_match]);2025}2026#endif2027if(head) { count +=keep_entry(head, o); }2028if(remote) { count +=keep_entry(remote, o); }2029return count;2030}20312032/*2033 * Two-way merge.2034 *2035 * The rule is to "carry forward" what is in the index without losing2036 * information across a "fast-forward", favoring a successful merge2037 * over a merge failure when it makes sense. For details of the2038 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2039 *2040 */2041inttwoway_merge(const struct cache_entry *const*src,2042struct unpack_trees_options *o)2043{2044const struct cache_entry *current = src[0];2045const struct cache_entry *oldtree = src[1];2046const struct cache_entry *newtree = src[2];20472048if(o->merge_size !=2)2049returnerror("Cannot do a twoway merge of%dtrees",2050 o->merge_size);20512052if(oldtree == o->df_conflict_entry)2053 oldtree = NULL;2054if(newtree == o->df_conflict_entry)2055 newtree = NULL;20562057if(current) {2058if(current->ce_flags & CE_CONFLICTED) {2059if(same(oldtree, newtree) || o->reset) {2060if(!newtree)2061returndeleted_entry(current, current, o);2062else2063returnmerged_entry(newtree, current, o);2064}2065returnreject_merge(current, o);2066}else if((!oldtree && !newtree) ||/* 4 and 5 */2067(!oldtree && newtree &&2068same(current, newtree)) ||/* 6 and 7 */2069(oldtree && newtree &&2070same(oldtree, newtree)) ||/* 14 and 15 */2071(oldtree && newtree &&2072!same(oldtree, newtree) &&/* 18 and 19 */2073same(current, newtree))) {2074returnkeep_entry(current, o);2075}else if(oldtree && !newtree &&same(current, oldtree)) {2076/* 10 or 11 */2077returndeleted_entry(oldtree, current, o);2078}else if(oldtree && newtree &&2079same(current, oldtree) && !same(current, newtree)) {2080/* 20 or 21 */2081returnmerged_entry(newtree, current, o);2082}else2083returnreject_merge(current, o);2084}2085else if(newtree) {2086if(oldtree && !o->initial_checkout) {2087/*2088 * deletion of the path was staged;2089 */2090if(same(oldtree, newtree))2091return1;2092returnreject_merge(oldtree, o);2093}2094returnmerged_entry(newtree, current, o);2095}2096returndeleted_entry(oldtree, current, o);2097}20982099/*2100 * Bind merge.2101 *2102 * Keep the index entries at stage0, collapse stage1 but make sure2103 * stage0 does not have anything there.2104 */2105intbind_merge(const struct cache_entry *const*src,2106struct unpack_trees_options *o)2107{2108const struct cache_entry *old = src[0];2109const struct cache_entry *a = src[1];21102111if(o->merge_size !=1)2112returnerror("Cannot do a bind merge of%dtrees",2113 o->merge_size);2114if(a && old)2115return o->gently ? -1:2116error(ERRORMSG(o, ERROR_BIND_OVERLAP),2117super_prefixed(a->name),2118super_prefixed(old->name));2119if(!a)2120returnkeep_entry(old, o);2121else2122returnmerged_entry(a, NULL, o);2123}21242125/*2126 * One-way merge.2127 *2128 * The rule is:2129 * - take the stat information from stage0, take the data from stage12130 */2131intoneway_merge(const struct cache_entry *const*src,2132struct unpack_trees_options *o)2133{2134const struct cache_entry *old = src[0];2135const struct cache_entry *a = src[1];21362137if(o->merge_size !=1)2138returnerror("Cannot do a oneway merge of%dtrees",2139 o->merge_size);21402141if(!a || a == o->df_conflict_entry)2142returndeleted_entry(old, old, o);21432144if(old &&same(old, a)) {2145int update =0;2146if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2147struct stat st;2148if(lstat(old->name, &st) ||2149ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2150 update |= CE_UPDATE;2151}2152add_entry(o, old, update,0);2153return0;2154}2155returnmerged_entry(a, old, o);2156}