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 383for(i =0; i < index->cache_nr; i++) { 384struct cache_entry *ce = index->cache[i]; 385 386if(ce->ce_flags & CE_UPDATE) { 387if(ce->ce_flags & CE_WT_REMOVE) 388die("BUG: both update and delete flags are set on%s", 389 ce->name); 390display_progress(progress, ++cnt); 391 ce->ce_flags &= ~CE_UPDATE; 392if(o->update && !o->dry_run) { 393 errs |=checkout_entry(ce, &state, NULL); 394} 395} 396} 397stop_progress(&progress); 398if(o->update) 399git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 400return errs !=0; 401} 402 403static intverify_uptodate_sparse(const struct cache_entry *ce, 404struct unpack_trees_options *o); 405static intverify_absent_sparse(const struct cache_entry *ce, 406enum unpack_trees_error_types, 407struct unpack_trees_options *o); 408 409static intapply_sparse_checkout(struct index_state *istate, 410struct cache_entry *ce, 411struct unpack_trees_options *o) 412{ 413int was_skip_worktree =ce_skip_worktree(ce); 414 415if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 416 ce->ce_flags |= CE_SKIP_WORKTREE; 417else 418 ce->ce_flags &= ~CE_SKIP_WORKTREE; 419if(was_skip_worktree !=ce_skip_worktree(ce)) { 420 ce->ce_flags |= CE_UPDATE_IN_BASE; 421 istate->cache_changed |= CE_ENTRY_CHANGED; 422} 423 424/* 425 * if (!was_skip_worktree && !ce_skip_worktree()) { 426 * This is perfectly normal. Move on; 427 * } 428 */ 429 430/* 431 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 432 * area as a result of ce_skip_worktree() shortcuts in 433 * verify_absent() and verify_uptodate(). 434 * Make sure they don't modify worktree if they are already 435 * outside checkout area 436 */ 437if(was_skip_worktree &&ce_skip_worktree(ce)) { 438 ce->ce_flags &= ~CE_UPDATE; 439 440/* 441 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 442 * on to get that file removed from both index and worktree. 443 * If that file is already outside worktree area, don't 444 * bother remove it. 445 */ 446if(ce->ce_flags & CE_REMOVE) 447 ce->ce_flags &= ~CE_WT_REMOVE; 448} 449 450if(!was_skip_worktree &&ce_skip_worktree(ce)) { 451/* 452 * If CE_UPDATE is set, verify_uptodate() must be called already 453 * also stat info may have lost after merged_entry() so calling 454 * verify_uptodate() again may fail 455 */ 456if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 457return-1; 458 ce->ce_flags |= CE_WT_REMOVE; 459 ce->ce_flags &= ~CE_UPDATE; 460} 461if(was_skip_worktree && !ce_skip_worktree(ce)) { 462if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 463return-1; 464 ce->ce_flags |= CE_UPDATE; 465} 466return0; 467} 468 469staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 470struct unpack_trees_options *o) 471{ 472int ret = o->fn(src, o); 473if(ret >0) 474 ret =0; 475return ret; 476} 477 478static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 479{ 480 ce->ce_flags |= CE_UNPACKED; 481 482if(o->cache_bottom < o->src_index->cache_nr && 483 o->src_index->cache[o->cache_bottom] == ce) { 484int bottom = o->cache_bottom; 485while(bottom < o->src_index->cache_nr && 486 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 487 bottom++; 488 o->cache_bottom = bottom; 489} 490} 491 492static voidmark_all_ce_unused(struct index_state *index) 493{ 494int i; 495for(i =0; i < index->cache_nr; i++) 496 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 497} 498 499static intlocate_in_src_index(const struct cache_entry *ce, 500struct unpack_trees_options *o) 501{ 502struct index_state *index = o->src_index; 503int len =ce_namelen(ce); 504int pos =index_name_pos(index, ce->name, len); 505if(pos <0) 506 pos = -1- pos; 507return pos; 508} 509 510/* 511 * We call unpack_index_entry() with an unmerged cache entry 512 * only in diff-index, and it wants a single callback. Skip 513 * the other unmerged entry with the same name. 514 */ 515static voidmark_ce_used_same_name(struct cache_entry *ce, 516struct unpack_trees_options *o) 517{ 518struct index_state *index = o->src_index; 519int len =ce_namelen(ce); 520int pos; 521 522for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 523struct cache_entry *next = index->cache[pos]; 524if(len !=ce_namelen(next) || 525memcmp(ce->name, next->name, len)) 526break; 527mark_ce_used(next, o); 528} 529} 530 531static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 532{ 533const struct index_state *index = o->src_index; 534int pos = o->cache_bottom; 535 536while(pos < index->cache_nr) { 537struct cache_entry *ce = index->cache[pos]; 538if(!(ce->ce_flags & CE_UNPACKED)) 539return ce; 540 pos++; 541} 542return NULL; 543} 544 545static voidadd_same_unmerged(const struct cache_entry *ce, 546struct unpack_trees_options *o) 547{ 548struct index_state *index = o->src_index; 549int len =ce_namelen(ce); 550int pos =index_name_pos(index, ce->name, len); 551 552if(0<= pos) 553die("programming error in a caller of mark_ce_used_same_name"); 554for(pos = -pos -1; pos < index->cache_nr; pos++) { 555struct cache_entry *next = index->cache[pos]; 556if(len !=ce_namelen(next) || 557memcmp(ce->name, next->name, len)) 558break; 559add_entry(o, next,0,0); 560mark_ce_used(next, o); 561} 562} 563 564static intunpack_index_entry(struct cache_entry *ce, 565struct unpack_trees_options *o) 566{ 567const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 568int ret; 569 570 src[0] = ce; 571 572mark_ce_used(ce, o); 573if(ce_stage(ce)) { 574if(o->skip_unmerged) { 575add_entry(o, ce,0,0); 576return0; 577} 578} 579 ret =call_unpack_fn(src, o); 580if(ce_stage(ce)) 581mark_ce_used_same_name(ce, o); 582return ret; 583} 584 585static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 586 587static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 588{ 589struct unpack_trees_options *o = info->data; 590 591if(o->diff_index_cached) 592return; 593 o->cache_bottom = bottom; 594} 595 596static intswitch_cache_bottom(struct traverse_info *info) 597{ 598struct unpack_trees_options *o = info->data; 599int ret, pos; 600 601if(o->diff_index_cached) 602return0; 603 ret = o->cache_bottom; 604 pos =find_cache_pos(info->prev, &info->name); 605 606if(pos < -1) 607 o->cache_bottom = -2- pos; 608else if(pos <0) 609 o->cache_bottom = o->src_index->cache_nr; 610return ret; 611} 612 613staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 614{ 615return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 616} 617 618static inttraverse_trees_recursive(int n,unsigned long dirmask, 619unsigned long df_conflicts, 620struct name_entry *names, 621struct traverse_info *info) 622{ 623int i, ret, bottom; 624int nr_buf =0; 625struct tree_desc t[MAX_UNPACK_TREES]; 626void*buf[MAX_UNPACK_TREES]; 627struct traverse_info newinfo; 628struct name_entry *p; 629 630 p = names; 631while(!p->mode) 632 p++; 633 634 newinfo = *info; 635 newinfo.prev = info; 636 newinfo.pathspec = info->pathspec; 637 newinfo.name = *p; 638 newinfo.pathlen +=tree_entry_len(p) +1; 639 newinfo.df_conflicts |= df_conflicts; 640 641/* 642 * Fetch the tree from the ODB for each peer directory in the 643 * n commits. 644 * 645 * For 2- and 3-way traversals, we try to avoid hitting the 646 * ODB twice for the same OID. This should yield a nice speed 647 * up in checkouts and merges when the commits are similar. 648 * 649 * We don't bother doing the full O(n^2) search for larger n, 650 * because wider traversals don't happen that often and we 651 * avoid the search setup. 652 * 653 * When 2 peer OIDs are the same, we just copy the tree 654 * descriptor data. This implicitly borrows the buffer 655 * data from the earlier cell. 656 */ 657for(i =0; i < n; i++, dirmask >>=1) { 658if(i >0&&are_same_oid(&names[i], &names[i -1])) 659 t[i] = t[i -1]; 660else if(i >1&&are_same_oid(&names[i], &names[i -2])) 661 t[i] = t[i -2]; 662else{ 663const unsigned char*sha1 = NULL; 664if(dirmask &1) 665 sha1 = names[i].oid->hash; 666 buf[nr_buf++] =fill_tree_descriptor(t+i, sha1); 667} 668} 669 670 bottom =switch_cache_bottom(&newinfo); 671 ret =traverse_trees(n, t, &newinfo); 672restore_cache_bottom(&newinfo, bottom); 673 674for(i =0; i < nr_buf; i++) 675free(buf[i]); 676 677return ret; 678} 679 680/* 681 * Compare the traverse-path to the cache entry without actually 682 * having to generate the textual representation of the traverse 683 * path. 684 * 685 * NOTE! This *only* compares up to the size of the traverse path 686 * itself - the caller needs to do the final check for the cache 687 * entry having more data at the end! 688 */ 689static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 690{ 691int len, pathlen, ce_len; 692const char*ce_name; 693 694if(info->prev) { 695int cmp =do_compare_entry_piecewise(ce, info->prev, 696&info->name); 697if(cmp) 698return cmp; 699} 700 pathlen = info->pathlen; 701 ce_len =ce_namelen(ce); 702 703/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 704if(ce_len < pathlen) 705return-1; 706 707 ce_len -= pathlen; 708 ce_name = ce->name + pathlen; 709 710 len =tree_entry_len(n); 711returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 712} 713 714static intdo_compare_entry(const struct cache_entry *ce, 715const struct traverse_info *info, 716const struct name_entry *n) 717{ 718int len, pathlen, ce_len; 719const char*ce_name; 720int cmp; 721 722/* 723 * If we have not precomputed the traverse path, it is quicker 724 * to avoid doing so. But if we have precomputed it, 725 * it is quicker to use the precomputed version. 726 */ 727if(!info->traverse_path) 728returndo_compare_entry_piecewise(ce, info, n); 729 730 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 731if(cmp) 732return cmp; 733 734 pathlen = info->pathlen; 735 ce_len =ce_namelen(ce); 736 737if(ce_len < pathlen) 738return-1; 739 740 ce_len -= pathlen; 741 ce_name = ce->name + pathlen; 742 743 len =tree_entry_len(n); 744returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 745} 746 747static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 748{ 749int cmp =do_compare_entry(ce, info, n); 750if(cmp) 751return cmp; 752 753/* 754 * Even if the beginning compared identically, the ce should 755 * compare as bigger than a directory leading up to it! 756 */ 757returnce_namelen(ce) >traverse_path_len(info, n); 758} 759 760static intce_in_traverse_path(const struct cache_entry *ce, 761const struct traverse_info *info) 762{ 763if(!info->prev) 764return1; 765if(do_compare_entry(ce, info->prev, &info->name)) 766return0; 767/* 768 * If ce (blob) is the same name as the path (which is a tree 769 * we will be descending into), it won't be inside it. 770 */ 771return(info->pathlen <ce_namelen(ce)); 772} 773 774static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 775{ 776int len =traverse_path_len(info, n); 777struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 778 779 ce->ce_mode =create_ce_mode(n->mode); 780 ce->ce_flags =create_ce_flags(stage); 781 ce->ce_namelen = len; 782oidcpy(&ce->oid, n->oid); 783make_traverse_path(ce->name, info, n); 784 785return ce; 786} 787 788static intunpack_nondirectories(int n,unsigned long mask, 789unsigned long dirmask, 790struct cache_entry **src, 791const struct name_entry *names, 792const struct traverse_info *info) 793{ 794int i; 795struct unpack_trees_options *o = info->data; 796unsigned long conflicts = info->df_conflicts | dirmask; 797 798/* Do we have *only* directories? Nothing to do */ 799if(mask == dirmask && !src[0]) 800return0; 801 802/* 803 * Ok, we've filled in up to any potential index entry in src[0], 804 * now do the rest. 805 */ 806for(i =0; i < n; i++) { 807int stage; 808unsigned int bit =1ul<< i; 809if(conflicts & bit) { 810 src[i + o->merge] = o->df_conflict_entry; 811continue; 812} 813if(!(mask & bit)) 814continue; 815if(!o->merge) 816 stage =0; 817else if(i +1< o->head_idx) 818 stage =1; 819else if(i +1> o->head_idx) 820 stage =3; 821else 822 stage =2; 823 src[i + o->merge] =create_ce_entry(info, names + i, stage); 824} 825 826if(o->merge) { 827int rc =call_unpack_fn((const struct cache_entry *const*)src, 828 o); 829for(i =0; i < n; i++) { 830struct cache_entry *ce = src[i + o->merge]; 831if(ce != o->df_conflict_entry) 832free(ce); 833} 834return rc; 835} 836 837for(i =0; i < n; i++) 838if(src[i] && src[i] != o->df_conflict_entry) 839if(do_add_entry(o, src[i],0,0)) 840return-1; 841 842return0; 843} 844 845static intunpack_failed(struct unpack_trees_options *o,const char*message) 846{ 847discard_index(&o->result); 848if(!o->gently && !o->exiting_early) { 849if(message) 850returnerror("%s", message); 851return-1; 852} 853return-1; 854} 855 856/* 857 * The tree traversal is looking at name p. If we have a matching entry, 858 * return it. If name p is a directory in the index, do not return 859 * anything, as we will want to match it when the traversal descends into 860 * the directory. 861 */ 862static intfind_cache_pos(struct traverse_info *info, 863const struct name_entry *p) 864{ 865int pos; 866struct unpack_trees_options *o = info->data; 867struct index_state *index = o->src_index; 868int pfxlen = info->pathlen; 869int p_len =tree_entry_len(p); 870 871for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 872const struct cache_entry *ce = index->cache[pos]; 873const char*ce_name, *ce_slash; 874int cmp, ce_len; 875 876if(ce->ce_flags & CE_UNPACKED) { 877/* 878 * cache_bottom entry is already unpacked, so 879 * we can never match it; don't check it 880 * again. 881 */ 882if(pos == o->cache_bottom) 883++o->cache_bottom; 884continue; 885} 886if(!ce_in_traverse_path(ce, info)) { 887/* 888 * Check if we can skip future cache checks 889 * (because we're already past all possible 890 * entries in the traverse path). 891 */ 892if(info->traverse_path) { 893if(strncmp(ce->name, info->traverse_path, 894 info->pathlen) >0) 895break; 896} 897continue; 898} 899 ce_name = ce->name + pfxlen; 900 ce_slash =strchr(ce_name,'/'); 901if(ce_slash) 902 ce_len = ce_slash - ce_name; 903else 904 ce_len =ce_namelen(ce) - pfxlen; 905 cmp =name_compare(p->path, p_len, ce_name, ce_len); 906/* 907 * Exact match; if we have a directory we need to 908 * delay returning it. 909 */ 910if(!cmp) 911return ce_slash ? -2- pos : pos; 912if(0< cmp) 913continue;/* keep looking */ 914/* 915 * ce_name sorts after p->path; could it be that we 916 * have files under p->path directory in the index? 917 * E.g. ce_name == "t-i", and p->path == "t"; we may 918 * have "t/a" in the index. 919 */ 920if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 921 ce_name[p_len] <'/') 922continue;/* keep looking */ 923break; 924} 925return-1; 926} 927 928static struct cache_entry *find_cache_entry(struct traverse_info *info, 929const struct name_entry *p) 930{ 931int pos =find_cache_pos(info, p); 932struct unpack_trees_options *o = info->data; 933 934if(0<= pos) 935return o->src_index->cache[pos]; 936else 937return NULL; 938} 939 940static voiddebug_path(struct traverse_info *info) 941{ 942if(info->prev) { 943debug_path(info->prev); 944if(*info->prev->name.path) 945putchar('/'); 946} 947printf("%s", info->name.path); 948} 949 950static voiddebug_name_entry(int i,struct name_entry *n) 951{ 952printf("ent#%d %06o%s\n", i, 953 n->path ? n->mode :0, 954 n->path ? n->path :"(missing)"); 955} 956 957static voiddebug_unpack_callback(int n, 958unsigned long mask, 959unsigned long dirmask, 960struct name_entry *names, 961struct traverse_info *info) 962{ 963int i; 964printf("* unpack mask%lu, dirmask%lu, cnt%d", 965 mask, dirmask, n); 966debug_path(info); 967putchar('\n'); 968for(i =0; i < n; i++) 969debug_name_entry(i, names + i); 970} 971 972static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 973{ 974struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 975struct unpack_trees_options *o = info->data; 976const struct name_entry *p = names; 977 978/* Find first entry with a real name (we could use "mask" too) */ 979while(!p->mode) 980 p++; 981 982if(o->debug_unpack) 983debug_unpack_callback(n, mask, dirmask, names, info); 984 985/* Are we supposed to look at the index too? */ 986if(o->merge) { 987while(1) { 988int cmp; 989struct cache_entry *ce; 990 991if(o->diff_index_cached) 992 ce =next_cache_entry(o); 993else 994 ce =find_cache_entry(info, p); 995 996if(!ce) 997break; 998 cmp =compare_entry(ce, info, p); 999if(cmp <0) {1000if(unpack_index_entry(ce, o) <0)1001returnunpack_failed(o, NULL);1002continue;1003}1004if(!cmp) {1005if(ce_stage(ce)) {1006/*1007 * If we skip unmerged index1008 * entries, we'll skip this1009 * entry *and* the tree1010 * entries associated with it!1011 */1012if(o->skip_unmerged) {1013add_same_unmerged(ce, o);1014return mask;1015}1016}1017 src[0] = ce;1018}1019break;1020}1021}10221023if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1024return-1;10251026if(o->merge && src[0]) {1027if(ce_stage(src[0]))1028mark_ce_used_same_name(src[0], o);1029else1030mark_ce_used(src[0], o);1031}10321033/* Now handle any directories.. */1034if(dirmask) {1035/* special case: "diff-index --cached" looking at a tree */1036if(o->diff_index_cached &&1037 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1038int matches;1039 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1040 names, info);1041/*1042 * Everything under the name matches; skip the1043 * entire hierarchy. diff_index_cached codepath1044 * special cases D/F conflicts in such a way that1045 * it does not do any look-ahead, so this is safe.1046 */1047if(matches) {1048 o->cache_bottom += matches;1049return mask;1050}1051}10521053if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1054 names, info) <0)1055return-1;1056return mask;1057}10581059return mask;1060}10611062static intclear_ce_flags_1(struct cache_entry **cache,int nr,1063struct strbuf *prefix,1064int select_mask,int clear_mask,1065struct exclude_list *el,int defval);10661067/* Whole directory matching */1068static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1069struct strbuf *prefix,1070char*basename,1071int select_mask,int clear_mask,1072struct exclude_list *el,int defval)1073{1074struct cache_entry **cache_end;1075int dtype = DT_DIR;1076int ret =is_excluded_from_list(prefix->buf, prefix->len,1077 basename, &dtype, el, &the_index);1078int rc;10791080strbuf_addch(prefix,'/');10811082/* If undecided, use matching result of parent dir in defval */1083if(ret <0)1084 ret = defval;10851086for(cache_end = cache; cache_end != cache + nr; cache_end++) {1087struct cache_entry *ce = *cache_end;1088if(strncmp(ce->name, prefix->buf, prefix->len))1089break;1090}10911092/*1093 * TODO: check el, if there are no patterns that may conflict1094 * with ret (iow, we know in advance the incl/excl1095 * decision for the entire directory), clear flag here without1096 * calling clear_ce_flags_1(). That function will call1097 * the expensive is_excluded_from_list() on every entry.1098 */1099 rc =clear_ce_flags_1(cache, cache_end - cache,1100 prefix,1101 select_mask, clear_mask,1102 el, ret);1103strbuf_setlen(prefix, prefix->len -1);1104return rc;1105}11061107/*1108 * Traverse the index, find every entry that matches according to1109 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1110 * number of traversed entries.1111 *1112 * If select_mask is non-zero, only entries whose ce_flags has on of1113 * those bits enabled are traversed.1114 *1115 * cache : pointer to an index entry1116 * prefix_len : an offset to its path1117 *1118 * The current path ("prefix") including the trailing '/' is1119 * cache[0]->name[0..(prefix_len-1)]1120 * Top level path has prefix_len zero.1121 */1122static intclear_ce_flags_1(struct cache_entry **cache,int nr,1123struct strbuf *prefix,1124int select_mask,int clear_mask,1125struct exclude_list *el,int defval)1126{1127struct cache_entry **cache_end = cache + nr;11281129/*1130 * Process all entries that have the given prefix and meet1131 * select_mask condition1132 */1133while(cache != cache_end) {1134struct cache_entry *ce = *cache;1135const char*name, *slash;1136int len, dtype, ret;11371138if(select_mask && !(ce->ce_flags & select_mask)) {1139 cache++;1140continue;1141}11421143if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1144break;11451146 name = ce->name + prefix->len;1147 slash =strchr(name,'/');11481149/* If it's a directory, try whole directory match first */1150if(slash) {1151int processed;11521153 len = slash - name;1154strbuf_add(prefix, name, len);11551156 processed =clear_ce_flags_dir(cache, cache_end - cache,1157 prefix,1158 prefix->buf + prefix->len - len,1159 select_mask, clear_mask,1160 el, defval);11611162/* clear_c_f_dir eats a whole dir already? */1163if(processed) {1164 cache += processed;1165strbuf_setlen(prefix, prefix->len - len);1166continue;1167}11681169strbuf_addch(prefix,'/');1170 cache +=clear_ce_flags_1(cache, cache_end - cache,1171 prefix,1172 select_mask, clear_mask, el, defval);1173strbuf_setlen(prefix, prefix->len - len -1);1174continue;1175}11761177/* Non-directory */1178 dtype =ce_to_dtype(ce);1179 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1180 name, &dtype, el, &the_index);1181if(ret <0)1182 ret = defval;1183if(ret >0)1184 ce->ce_flags &= ~clear_mask;1185 cache++;1186}1187return nr - (cache_end - cache);1188}11891190static intclear_ce_flags(struct cache_entry **cache,int nr,1191int select_mask,int clear_mask,1192struct exclude_list *el)1193{1194static struct strbuf prefix = STRBUF_INIT;11951196strbuf_reset(&prefix);11971198returnclear_ce_flags_1(cache, nr,1199&prefix,1200 select_mask, clear_mask,1201 el,0);1202}12031204/*1205 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1206 */1207static voidmark_new_skip_worktree(struct exclude_list *el,1208struct index_state *the_index,1209int select_flag,int skip_wt_flag)1210{1211int i;12121213/*1214 * 1. Pretend the narrowest worktree: only unmerged entries1215 * are checked out1216 */1217for(i =0; i < the_index->cache_nr; i++) {1218struct cache_entry *ce = the_index->cache[i];12191220if(select_flag && !(ce->ce_flags & select_flag))1221continue;12221223if(!ce_stage(ce))1224 ce->ce_flags |= skip_wt_flag;1225else1226 ce->ce_flags &= ~skip_wt_flag;1227}12281229/*1230 * 2. Widen worktree according to sparse-checkout file.1231 * Matched entries will have skip_wt_flag cleared (i.e. "in")1232 */1233clear_ce_flags(the_index->cache, the_index->cache_nr,1234 select_flag, skip_wt_flag, el);1235}12361237static intverify_absent(const struct cache_entry *,1238enum unpack_trees_error_types,1239struct unpack_trees_options *);1240/*1241 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1242 * resulting index, -2 on failure to reflect the changes to the work tree.1243 *1244 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1245 */1246intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1247{1248int i, ret;1249static struct cache_entry *dfc;1250struct exclude_list el;12511252if(len > MAX_UNPACK_TREES)1253die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12541255memset(&el,0,sizeof(el));1256if(!core_apply_sparse_checkout || !o->update)1257 o->skip_sparse_checkout =1;1258if(!o->skip_sparse_checkout) {1259char*sparse =git_pathdup("info/sparse-checkout");1260if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1261 o->skip_sparse_checkout =1;1262else1263 o->el = ⪙1264free(sparse);1265}12661267memset(&o->result,0,sizeof(o->result));1268 o->result.initialized =1;1269 o->result.timestamp.sec = o->src_index->timestamp.sec;1270 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1271 o->result.version = o->src_index->version;1272 o->result.split_index = o->src_index->split_index;1273if(o->result.split_index)1274 o->result.split_index->refcount++;1275hashcpy(o->result.sha1, o->src_index->sha1);1276 o->merge_size = len;1277mark_all_ce_unused(o->src_index);12781279/*1280 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1281 */1282if(!o->skip_sparse_checkout)1283mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);12841285if(!dfc)1286 dfc =xcalloc(1,cache_entry_size(0));1287 o->df_conflict_entry = dfc;12881289if(len) {1290const char*prefix = o->prefix ? o->prefix :"";1291struct traverse_info info;12921293setup_traverse_info(&info, prefix);1294 info.fn = unpack_callback;1295 info.data = o;1296 info.show_all_errors = o->show_all_errors;1297 info.pathspec = o->pathspec;12981299if(o->prefix) {1300/*1301 * Unpack existing index entries that sort before the1302 * prefix the tree is spliced into. Note that o->merge1303 * is always true in this case.1304 */1305while(1) {1306struct cache_entry *ce =next_cache_entry(o);1307if(!ce)1308break;1309if(ce_in_traverse_path(ce, &info))1310break;1311if(unpack_index_entry(ce, o) <0)1312goto return_failed;1313}1314}13151316if(traverse_trees(len, t, &info) <0)1317goto return_failed;1318}13191320/* Any left-over entries in the index? */1321if(o->merge) {1322while(1) {1323struct cache_entry *ce =next_cache_entry(o);1324if(!ce)1325break;1326if(unpack_index_entry(ce, o) <0)1327goto return_failed;1328}1329}1330mark_all_ce_unused(o->src_index);13311332if(o->trivial_merges_only && o->nontrivial_merge) {1333 ret =unpack_failed(o,"Merge requires file-level merging");1334goto done;1335}13361337if(!o->skip_sparse_checkout) {1338int empty_worktree =1;13391340/*1341 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11342 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1343 * so apply_sparse_checkout() won't attempt to remove it from worktree1344 */1345mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13461347 ret =0;1348for(i =0; i < o->result.cache_nr; i++) {1349struct cache_entry *ce = o->result.cache[i];13501351/*1352 * Entries marked with CE_ADDED in merged_entry() do not have1353 * verify_absent() check (the check is effectively disabled1354 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1355 *1356 * Do the real check now because we have had1357 * correct CE_NEW_SKIP_WORKTREE1358 */1359if(ce->ce_flags & CE_ADDED &&1360verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1361if(!o->show_all_errors)1362goto return_failed;1363 ret = -1;1364}13651366if(apply_sparse_checkout(&o->result, ce, o)) {1367if(!o->show_all_errors)1368goto return_failed;1369 ret = -1;1370}1371if(!ce_skip_worktree(ce))1372 empty_worktree =0;13731374}1375if(ret <0)1376goto return_failed;1377/*1378 * Sparse checkout is meant to narrow down checkout area1379 * but it does not make sense to narrow down to empty working1380 * tree. This is usually a mistake in sparse checkout rules.1381 * Do not allow users to do that.1382 */1383if(o->result.cache_nr && empty_worktree) {1384 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1385goto done;1386}1387}13881389 o->src_index = NULL;1390 ret =check_updates(o) ? (-2) :0;1391if(o->dst_index) {1392if(!ret) {1393if(!o->result.cache_tree)1394 o->result.cache_tree =cache_tree();1395if(!cache_tree_fully_valid(o->result.cache_tree))1396cache_tree_update(&o->result,1397 WRITE_TREE_SILENT |1398 WRITE_TREE_REPAIR);1399}1400move_index_extensions(&o->result, o->dst_index);1401discard_index(o->dst_index);1402*o->dst_index = o->result;1403}else{1404discard_index(&o->result);1405}14061407done:1408clear_exclude_list(&el);1409return ret;14101411return_failed:1412if(o->show_all_errors)1413display_error_msgs(o);1414mark_all_ce_unused(o->src_index);1415 ret =unpack_failed(o, NULL);1416if(o->exiting_early)1417 ret =0;1418goto done;1419}14201421/* Here come the merge functions */14221423static intreject_merge(const struct cache_entry *ce,1424struct unpack_trees_options *o)1425{1426return o->gently ? -1:1427add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1428}14291430static intsame(const struct cache_entry *a,const struct cache_entry *b)1431{1432if(!!a != !!b)1433return0;1434if(!a && !b)1435return1;1436if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1437return0;1438return a->ce_mode == b->ce_mode &&1439!oidcmp(&a->oid, &b->oid);1440}144114421443/*1444 * When a CE gets turned into an unmerged entry, we1445 * want it to be up-to-date1446 */1447static intverify_uptodate_1(const struct cache_entry *ce,1448struct unpack_trees_options *o,1449enum unpack_trees_error_types error_type)1450{1451struct stat st;14521453if(o->index_only)1454return0;14551456/*1457 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1458 * if this entry is truly up-to-date because this file may be1459 * overwritten.1460 */1461if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1462;/* keep checking */1463else if(o->reset ||ce_uptodate(ce))1464return0;14651466if(!lstat(ce->name, &st)) {1467int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1468unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);14691470if(submodule_from_ce(ce)) {1471int r =check_submodule_move_head(ce,1472"HEAD",oid_to_hex(&ce->oid), o);1473if(r)1474return o->gently ? -1:1475add_rejected_path(o, error_type, ce->name);1476return0;1477}14781479if(!changed)1480return0;1481/*1482 * Historic default policy was to allow submodule to be out1483 * of sync wrt the superproject index. If the submodule was1484 * not considered interesting above, we don't care here.1485 */1486if(S_ISGITLINK(ce->ce_mode))1487return0;14881489 errno =0;1490}1491if(errno == ENOENT)1492return0;1493return o->gently ? -1:1494add_rejected_path(o, error_type, ce->name);1495}14961497static intverify_uptodate(const struct cache_entry *ce,1498struct unpack_trees_options *o)1499{1500if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1501return0;1502returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1503}15041505static intverify_uptodate_sparse(const struct cache_entry *ce,1506struct unpack_trees_options *o)1507{1508returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1509}15101511static voidinvalidate_ce_path(const struct cache_entry *ce,1512struct unpack_trees_options *o)1513{1514if(!ce)1515return;1516cache_tree_invalidate_path(o->src_index, ce->name);1517untracked_cache_invalidate_path(o->src_index, ce->name);1518}15191520/*1521 * Check that checking out ce->sha1 in subdir ce->name is not1522 * going to overwrite any working files.1523 *1524 * Currently, git does not checkout subprojects during a superproject1525 * checkout, so it is not going to overwrite anything.1526 */1527static intverify_clean_submodule(const char*old_sha1,1528const struct cache_entry *ce,1529enum unpack_trees_error_types error_type,1530struct unpack_trees_options *o)1531{1532if(!submodule_from_ce(ce))1533return0;15341535returncheck_submodule_move_head(ce, old_sha1,1536oid_to_hex(&ce->oid), o);1537}15381539static intverify_clean_subdirectory(const struct cache_entry *ce,1540enum unpack_trees_error_types error_type,1541struct unpack_trees_options *o)1542{1543/*1544 * we are about to extract "ce->name"; we would not want to lose1545 * anything in the existing directory there.1546 */1547int namelen;1548int i;1549struct dir_struct d;1550char*pathbuf;1551int cnt =0;15521553if(S_ISGITLINK(ce->ce_mode)) {1554unsigned char sha1[20];1555int sub_head =resolve_gitlink_ref(ce->name,"HEAD", sha1);1556/*1557 * If we are not going to update the submodule, then1558 * we don't care.1559 */1560if(!sub_head && !hashcmp(sha1, ce->oid.hash))1561return0;1562returnverify_clean_submodule(sub_head ? NULL :sha1_to_hex(sha1),1563 ce, error_type, o);1564}15651566/*1567 * First let's make sure we do not have a local modification1568 * in that directory.1569 */1570 namelen =ce_namelen(ce);1571for(i =locate_in_src_index(ce, o);1572 i < o->src_index->cache_nr;1573 i++) {1574struct cache_entry *ce2 = o->src_index->cache[i];1575int len =ce_namelen(ce2);1576if(len < namelen ||1577strncmp(ce->name, ce2->name, namelen) ||1578 ce2->name[namelen] !='/')1579break;1580/*1581 * ce2->name is an entry in the subdirectory to be1582 * removed.1583 */1584if(!ce_stage(ce2)) {1585if(verify_uptodate(ce2, o))1586return-1;1587add_entry(o, ce2, CE_REMOVE,0);1588mark_ce_used(ce2, o);1589}1590 cnt++;1591}15921593/*1594 * Then we need to make sure that we do not lose a locally1595 * present file that is not ignored.1596 */1597 pathbuf =xstrfmt("%.*s/", namelen, ce->name);15981599memset(&d,0,sizeof(d));1600if(o->dir)1601 d.exclude_per_dir = o->dir->exclude_per_dir;1602 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1603if(i)1604return o->gently ? -1:1605add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1606free(pathbuf);1607return cnt;1608}16091610/*1611 * This gets called when there was no index entry for the tree entry 'dst',1612 * but we found a file in the working tree that 'lstat()' said was fine,1613 * and we're on a case-insensitive filesystem.1614 *1615 * See if we can find a case-insensitive match in the index that also1616 * matches the stat information, and assume it's that other file!1617 */1618static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1619{1620const struct cache_entry *src;16211622 src =index_file_exists(o->src_index, name, len,1);1623return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1624}16251626static intcheck_ok_to_remove(const char*name,int len,int dtype,1627const struct cache_entry *ce,struct stat *st,1628enum unpack_trees_error_types error_type,1629struct unpack_trees_options *o)1630{1631const struct cache_entry *result;16321633/*1634 * It may be that the 'lstat()' succeeded even though1635 * target 'ce' was absent, because there is an old1636 * entry that is different only in case..1637 *1638 * Ignore that lstat() if it matches.1639 */1640if(ignore_case &&icase_exists(o, name, len, st))1641return0;16421643if(o->dir &&1644is_excluded(o->dir, &the_index, name, &dtype))1645/*1646 * ce->name is explicitly excluded, so it is Ok to1647 * overwrite it.1648 */1649return0;1650if(S_ISDIR(st->st_mode)) {1651/*1652 * We are checking out path "foo" and1653 * found "foo/." in the working tree.1654 * This is tricky -- if we have modified1655 * files that are in "foo/" we would lose1656 * them.1657 */1658if(verify_clean_subdirectory(ce, error_type, o) <0)1659return-1;1660return0;1661}16621663/*1664 * The previous round may already have decided to1665 * delete this path, which is in a subdirectory that1666 * is being replaced with a blob.1667 */1668 result =index_file_exists(&o->result, name, len,0);1669if(result) {1670if(result->ce_flags & CE_REMOVE)1671return0;1672}16731674return o->gently ? -1:1675add_rejected_path(o, error_type, name);1676}16771678/*1679 * We do not want to remove or overwrite a working tree file that1680 * is not tracked, unless it is ignored.1681 */1682static intverify_absent_1(const struct cache_entry *ce,1683enum unpack_trees_error_types error_type,1684struct unpack_trees_options *o)1685{1686int len;1687struct stat st;16881689if(o->index_only || o->reset || !o->update)1690return0;16911692 len =check_leading_path(ce->name,ce_namelen(ce));1693if(!len)1694return0;1695else if(len >0) {1696char*path;1697int ret;16981699 path =xmemdupz(ce->name, len);1700if(lstat(path, &st))1701 ret =error_errno("cannot stat '%s'", path);1702else{1703if(submodule_from_ce(ce))1704 ret =check_submodule_move_head(ce,1705oid_to_hex(&ce->oid),1706 NULL, o);1707else1708 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1709&st, error_type, o);1710}1711free(path);1712return ret;1713}else if(lstat(ce->name, &st)) {1714if(errno != ENOENT)1715returnerror_errno("cannot stat '%s'", ce->name);1716return0;1717}else{1718if(submodule_from_ce(ce))1719returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1720 NULL, o);17211722returncheck_ok_to_remove(ce->name,ce_namelen(ce),1723ce_to_dtype(ce), ce, &st,1724 error_type, o);1725}1726}17271728static intverify_absent(const struct cache_entry *ce,1729enum unpack_trees_error_types error_type,1730struct unpack_trees_options *o)1731{1732if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1733return0;1734returnverify_absent_1(ce, error_type, o);1735}17361737static intverify_absent_sparse(const struct cache_entry *ce,1738enum unpack_trees_error_types error_type,1739struct unpack_trees_options *o)1740{1741enum unpack_trees_error_types orphaned_error = error_type;1742if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1743 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17441745returnverify_absent_1(ce, orphaned_error, o);1746}17471748static intmerged_entry(const struct cache_entry *ce,1749const struct cache_entry *old,1750struct unpack_trees_options *o)1751{1752int update = CE_UPDATE;1753struct cache_entry *merge =dup_entry(ce);17541755if(!old) {1756/*1757 * New index entries. In sparse checkout, the following1758 * verify_absent() will be delayed until after1759 * traverse_trees() finishes in unpack_trees(), then:1760 *1761 * - CE_NEW_SKIP_WORKTREE will be computed correctly1762 * - verify_absent() be called again, this time with1763 * correct CE_NEW_SKIP_WORKTREE1764 *1765 * verify_absent() call here does nothing in sparse1766 * checkout (i.e. o->skip_sparse_checkout == 0)1767 */1768 update |= CE_ADDED;1769 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;17701771if(verify_absent(merge,1772 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1773free(merge);1774return-1;1775}1776invalidate_ce_path(merge, o);17771778if(submodule_from_ce(ce)) {1779int ret =check_submodule_move_head(ce, NULL,1780oid_to_hex(&ce->oid),1781 o);1782if(ret)1783return ret;1784}17851786}else if(!(old->ce_flags & CE_CONFLICTED)) {1787/*1788 * See if we can re-use the old CE directly?1789 * That way we get the uptodate stat info.1790 *1791 * This also removes the UPDATE flag on a match; otherwise1792 * we will end up overwriting local changes in the work tree.1793 */1794if(same(old, merge)) {1795copy_cache_entry(merge, old);1796 update =0;1797}else{1798if(verify_uptodate(old, o)) {1799free(merge);1800return-1;1801}1802/* Migrate old flags over */1803 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1804invalidate_ce_path(old, o);1805}18061807if(submodule_from_ce(ce)) {1808int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1809oid_to_hex(&ce->oid),1810 o);1811if(ret)1812return ret;1813}1814}else{1815/*1816 * Previously unmerged entry left as an existence1817 * marker by read_index_unmerged();1818 */1819invalidate_ce_path(old, o);1820}18211822do_add_entry(o, merge, update, CE_STAGEMASK);1823return1;1824}18251826static intdeleted_entry(const struct cache_entry *ce,1827const struct cache_entry *old,1828struct unpack_trees_options *o)1829{1830/* Did it exist in the index? */1831if(!old) {1832if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1833return-1;1834return0;1835}1836if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1837return-1;1838add_entry(o, ce, CE_REMOVE,0);1839invalidate_ce_path(ce, o);1840return1;1841}18421843static intkeep_entry(const struct cache_entry *ce,1844struct unpack_trees_options *o)1845{1846add_entry(o, ce,0,0);1847return1;1848}18491850#if DBRT_DEBUG1851static voidshow_stage_entry(FILE*o,1852const char*label,const struct cache_entry *ce)1853{1854if(!ce)1855fprintf(o,"%s(missing)\n", label);1856else1857fprintf(o,"%s%06o%s %d\t%s\n",1858 label,1859 ce->ce_mode,1860oid_to_hex(&ce->oid),1861ce_stage(ce),1862 ce->name);1863}1864#endif18651866intthreeway_merge(const struct cache_entry *const*stages,1867struct unpack_trees_options *o)1868{1869const struct cache_entry *index;1870const struct cache_entry *head;1871const struct cache_entry *remote = stages[o->head_idx +1];1872int count;1873int head_match =0;1874int remote_match =0;18751876int df_conflict_head =0;1877int df_conflict_remote =0;18781879int any_anc_missing =0;1880int no_anc_exists =1;1881int i;18821883for(i =1; i < o->head_idx; i++) {1884if(!stages[i] || stages[i] == o->df_conflict_entry)1885 any_anc_missing =1;1886else1887 no_anc_exists =0;1888}18891890 index = stages[0];1891 head = stages[o->head_idx];18921893if(head == o->df_conflict_entry) {1894 df_conflict_head =1;1895 head = NULL;1896}18971898if(remote == o->df_conflict_entry) {1899 df_conflict_remote =1;1900 remote = NULL;1901}19021903/*1904 * First, if there's a #16 situation, note that to prevent #131905 * and #14.1906 */1907if(!same(remote, head)) {1908for(i =1; i < o->head_idx; i++) {1909if(same(stages[i], head)) {1910 head_match = i;1911}1912if(same(stages[i], remote)) {1913 remote_match = i;1914}1915}1916}19171918/*1919 * We start with cases where the index is allowed to match1920 * something other than the head: #14(ALT) and #2ALT, where it1921 * is permitted to match the result instead.1922 */1923/* #14, #14ALT, #2ALT */1924if(remote && !df_conflict_head && head_match && !remote_match) {1925if(index && !same(index, remote) && !same(index, head))1926returnreject_merge(index, o);1927returnmerged_entry(remote, index, o);1928}1929/*1930 * If we have an entry in the index cache, then we want to1931 * make sure that it matches head.1932 */1933if(index && !same(index, head))1934returnreject_merge(index, o);19351936if(head) {1937/* #5ALT, #15 */1938if(same(head, remote))1939returnmerged_entry(head, index, o);1940/* #13, #3ALT */1941if(!df_conflict_remote && remote_match && !head_match)1942returnmerged_entry(head, index, o);1943}19441945/* #1 */1946if(!head && !remote && any_anc_missing)1947return0;19481949/*1950 * Under the "aggressive" rule, we resolve mostly trivial1951 * cases that we historically had git-merge-one-file resolve.1952 */1953if(o->aggressive) {1954int head_deleted = !head;1955int remote_deleted = !remote;1956const struct cache_entry *ce = NULL;19571958if(index)1959 ce = index;1960else if(head)1961 ce = head;1962else if(remote)1963 ce = remote;1964else{1965for(i =1; i < o->head_idx; i++) {1966if(stages[i] && stages[i] != o->df_conflict_entry) {1967 ce = stages[i];1968break;1969}1970}1971}19721973/*1974 * Deleted in both.1975 * Deleted in one and unchanged in the other.1976 */1977if((head_deleted && remote_deleted) ||1978(head_deleted && remote && remote_match) ||1979(remote_deleted && head && head_match)) {1980if(index)1981returndeleted_entry(index, index, o);1982if(ce && !head_deleted) {1983if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1984return-1;1985}1986return0;1987}1988/*1989 * Added in both, identically.1990 */1991if(no_anc_exists && head && remote &&same(head, remote))1992returnmerged_entry(head, index, o);19931994}19951996/* Below are "no merge" cases, which require that the index be1997 * up-to-date to avoid the files getting overwritten with1998 * conflict resolution files.1999 */2000if(index) {2001if(verify_uptodate(index, o))2002return-1;2003}20042005 o->nontrivial_merge =1;20062007/* #2, #3, #4, #6, #7, #9, #10, #11. */2008 count =0;2009if(!head_match || !remote_match) {2010for(i =1; i < o->head_idx; i++) {2011if(stages[i] && stages[i] != o->df_conflict_entry) {2012keep_entry(stages[i], o);2013 count++;2014break;2015}2016}2017}2018#if DBRT_DEBUG2019else{2020fprintf(stderr,"read-tree: warning #16 detected\n");2021show_stage_entry(stderr,"head ", stages[head_match]);2022show_stage_entry(stderr,"remote ", stages[remote_match]);2023}2024#endif2025if(head) { count +=keep_entry(head, o); }2026if(remote) { count +=keep_entry(remote, o); }2027return count;2028}20292030/*2031 * Two-way merge.2032 *2033 * The rule is to "carry forward" what is in the index without losing2034 * information across a "fast-forward", favoring a successful merge2035 * over a merge failure when it makes sense. For details of the2036 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2037 *2038 */2039inttwoway_merge(const struct cache_entry *const*src,2040struct unpack_trees_options *o)2041{2042const struct cache_entry *current = src[0];2043const struct cache_entry *oldtree = src[1];2044const struct cache_entry *newtree = src[2];20452046if(o->merge_size !=2)2047returnerror("Cannot do a twoway merge of%dtrees",2048 o->merge_size);20492050if(oldtree == o->df_conflict_entry)2051 oldtree = NULL;2052if(newtree == o->df_conflict_entry)2053 newtree = NULL;20542055if(current) {2056if(current->ce_flags & CE_CONFLICTED) {2057if(same(oldtree, newtree) || o->reset) {2058if(!newtree)2059returndeleted_entry(current, current, o);2060else2061returnmerged_entry(newtree, current, o);2062}2063returnreject_merge(current, o);2064}else if((!oldtree && !newtree) ||/* 4 and 5 */2065(!oldtree && newtree &&2066same(current, newtree)) ||/* 6 and 7 */2067(oldtree && newtree &&2068same(oldtree, newtree)) ||/* 14 and 15 */2069(oldtree && newtree &&2070!same(oldtree, newtree) &&/* 18 and 19 */2071same(current, newtree))) {2072returnkeep_entry(current, o);2073}else if(oldtree && !newtree &&same(current, oldtree)) {2074/* 10 or 11 */2075returndeleted_entry(oldtree, current, o);2076}else if(oldtree && newtree &&2077same(current, oldtree) && !same(current, newtree)) {2078/* 20 or 21 */2079returnmerged_entry(newtree, current, o);2080}else2081returnreject_merge(current, o);2082}2083else if(newtree) {2084if(oldtree && !o->initial_checkout) {2085/*2086 * deletion of the path was staged;2087 */2088if(same(oldtree, newtree))2089return1;2090returnreject_merge(oldtree, o);2091}2092returnmerged_entry(newtree, current, o);2093}2094returndeleted_entry(oldtree, current, o);2095}20962097/*2098 * Bind merge.2099 *2100 * Keep the index entries at stage0, collapse stage1 but make sure2101 * stage0 does not have anything there.2102 */2103intbind_merge(const struct cache_entry *const*src,2104struct unpack_trees_options *o)2105{2106const struct cache_entry *old = src[0];2107const struct cache_entry *a = src[1];21082109if(o->merge_size !=1)2110returnerror("Cannot do a bind merge of%dtrees",2111 o->merge_size);2112if(a && old)2113return o->gently ? -1:2114error(ERRORMSG(o, ERROR_BIND_OVERLAP),2115super_prefixed(a->name),2116super_prefixed(old->name));2117if(!a)2118returnkeep_entry(old, o);2119else2120returnmerged_entry(a, NULL, o);2121}21222123/*2124 * One-way merge.2125 *2126 * The rule is:2127 * - take the stat information from stage0, take the data from stage12128 */2129intoneway_merge(const struct cache_entry *const*src,2130struct unpack_trees_options *o)2131{2132const struct cache_entry *old = src[0];2133const struct cache_entry *a = src[1];21342135if(o->merge_size !=1)2136returnerror("Cannot do a oneway merge of%dtrees",2137 o->merge_size);21382139if(!a || a == o->df_conflict_entry)2140returndeleted_entry(old, old, o);21412142if(old &&same(old, a)) {2143int update =0;2144if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2145struct stat st;2146if(lstat(old->name, &st) ||2147ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2148 update |= CE_UPDATE;2149}2150add_entry(o, old, update,0);2151return0;2152}2153returnmerged_entry(a, old, o);2154}