1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"repository.h" 4#include"config.h" 5#include"dir.h" 6#include"tree.h" 7#include"tree-walk.h" 8#include"cache-tree.h" 9#include"unpack-trees.h" 10#include"progress.h" 11#include"refs.h" 12#include"attr.h" 13#include"split-index.h" 14#include"dir.h" 15#include"submodule.h" 16#include"submodule-config.h" 17#include"fsmonitor.h" 18#include"fetch-object.h" 19 20/* 21 * Error messages expected by scripts out of plumbing commands such as 22 * read-tree. Non-scripted Porcelain is not required to use these messages 23 * and in fact are encouraged to reword them to better suit their particular 24 * situation better. See how "git checkout" and "git merge" replaces 25 * them using setup_unpack_trees_porcelain(), for example. 26 */ 27static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 28/* ERROR_WOULD_OVERWRITE */ 29"Entry '%s' would be overwritten by merge. Cannot merge.", 30 31/* ERROR_NOT_UPTODATE_FILE */ 32"Entry '%s' not uptodate. Cannot merge.", 33 34/* ERROR_NOT_UPTODATE_DIR */ 35"Updating '%s' would lose untracked files in it", 36 37/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 38"Untracked working tree file '%s' would be overwritten by merge.", 39 40/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 41"Untracked working tree file '%s' would be removed by merge.", 42 43/* ERROR_BIND_OVERLAP */ 44"Entry '%s' overlaps with '%s'. Cannot bind.", 45 46/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 47"Entry '%s' not uptodate. Cannot update sparse checkout.", 48 49/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 50"Working tree file '%s' would be overwritten by sparse checkout update.", 51 52/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 53"Working tree file '%s' would be removed by sparse checkout update.", 54 55/* ERROR_WOULD_LOSE_SUBMODULE */ 56"Submodule '%s' cannot checkout new HEAD.", 57}; 58 59#define ERRORMSG(o,type) \ 60 ( ((o) && (o)->msgs[(type)]) \ 61 ? ((o)->msgs[(type)]) \ 62 : (unpack_plumbing_errors[(type)]) ) 63 64static const char*super_prefixed(const char*path) 65{ 66/* 67 * It is necessary and sufficient to have two static buffers 68 * here, as the return value of this function is fed to 69 * error() using the unpack_*_errors[] templates we see above. 70 */ 71static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 72static int super_prefix_len = -1; 73static unsigned idx =ARRAY_SIZE(buf) -1; 74 75if(super_prefix_len <0) { 76const char*super_prefix =get_super_prefix(); 77if(!super_prefix) { 78 super_prefix_len =0; 79}else{ 80int i; 81for(i =0; i <ARRAY_SIZE(buf); i++) 82strbuf_addstr(&buf[i], super_prefix); 83 super_prefix_len = buf[0].len; 84} 85} 86 87if(!super_prefix_len) 88return path; 89 90if(++idx >=ARRAY_SIZE(buf)) 91 idx =0; 92 93strbuf_setlen(&buf[idx], super_prefix_len); 94strbuf_addstr(&buf[idx], path); 95 96return buf[idx].buf; 97} 98 99voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 100const char*cmd) 101{ 102int i; 103const char**msgs = opts->msgs; 104const char*msg; 105 106if(!strcmp(cmd,"checkout")) 107 msg = advice_commit_before_merge 108?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 109"Please commit your changes or stash them before you switch branches.") 110:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 111else if(!strcmp(cmd,"merge")) 112 msg = advice_commit_before_merge 113?_("Your local changes to the following files would be overwritten by merge:\n%%s" 114"Please commit your changes or stash them before you merge.") 115:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 116else 117 msg = advice_commit_before_merge 118?_("Your local changes to the following files would be overwritten by%s:\n%%s" 119"Please commit your changes or stash them before you%s.") 120:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 121 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 122xstrfmt(msg, cmd, cmd); 123 124 msgs[ERROR_NOT_UPTODATE_DIR] = 125_("Updating the following directories would lose untracked files in them:\n%s"); 126 127if(!strcmp(cmd,"checkout")) 128 msg = advice_commit_before_merge 129?_("The following untracked working tree files would be removed by checkout:\n%%s" 130"Please move or remove them before you switch branches.") 131:_("The following untracked working tree files would be removed by checkout:\n%%s"); 132else if(!strcmp(cmd,"merge")) 133 msg = advice_commit_before_merge 134?_("The following untracked working tree files would be removed by merge:\n%%s" 135"Please move or remove them before you merge.") 136:_("The following untracked working tree files would be removed by merge:\n%%s"); 137else 138 msg = advice_commit_before_merge 139?_("The following untracked working tree files would be removed by%s:\n%%s" 140"Please move or remove them before you%s.") 141:_("The following untracked working tree files would be removed by%s:\n%%s"); 142 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 143 144if(!strcmp(cmd,"checkout")) 145 msg = advice_commit_before_merge 146?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 147"Please move or remove them before you switch branches.") 148:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 149else if(!strcmp(cmd,"merge")) 150 msg = advice_commit_before_merge 151?_("The following untracked working tree files would be overwritten by merge:\n%%s" 152"Please move or remove them before you merge.") 153:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 154else 155 msg = advice_commit_before_merge 156?_("The following untracked working tree files would be overwritten by%s:\n%%s" 157"Please move or remove them before you%s.") 158:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 159 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 160 161/* 162 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 163 * cannot easily display it as a list. 164 */ 165 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 166 167 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 168_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 169 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 170_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 171 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 172_("The following working tree files would be removed by sparse checkout update:\n%s"); 173 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 174_("Cannot update submodule:\n%s"); 175 176 opts->show_all_errors =1; 177/* rejected paths may not have a static buffer */ 178for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 179 opts->unpack_rejects[i].strdup_strings =1; 180} 181 182static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 183unsigned int set,unsigned int clear) 184{ 185 clear |= CE_HASHED; 186 187if(set & CE_REMOVE) 188 set |= CE_WT_REMOVE; 189 190 ce->ce_flags = (ce->ce_flags & ~clear) | set; 191returnadd_index_entry(&o->result, ce, 192 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 193} 194 195static struct cache_entry *dup_entry(const struct cache_entry *ce) 196{ 197unsigned int size =ce_size(ce); 198struct cache_entry *new_entry =xmalloc(size); 199 200memcpy(new_entry, ce, size); 201return new_entry; 202} 203 204static voidadd_entry(struct unpack_trees_options *o, 205const struct cache_entry *ce, 206unsigned int set,unsigned int clear) 207{ 208do_add_entry(o,dup_entry(ce), set, clear); 209} 210 211/* 212 * add error messages on path <path> 213 * corresponding to the type <e> with the message <msg> 214 * indicating if it should be display in porcelain or not 215 */ 216static intadd_rejected_path(struct unpack_trees_options *o, 217enum unpack_trees_error_types e, 218const char*path) 219{ 220if(!o->show_all_errors) 221returnerror(ERRORMSG(o, e),super_prefixed(path)); 222 223/* 224 * Otherwise, insert in a list for future display by 225 * display_error_msgs() 226 */ 227string_list_append(&o->unpack_rejects[e], path); 228return-1; 229} 230 231/* 232 * display all the error messages stored in a nice way 233 */ 234static voiddisplay_error_msgs(struct unpack_trees_options *o) 235{ 236int e, i; 237int something_displayed =0; 238for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 239struct string_list *rejects = &o->unpack_rejects[e]; 240if(rejects->nr >0) { 241struct strbuf path = STRBUF_INIT; 242 something_displayed =1; 243for(i =0; i < rejects->nr; i++) 244strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 245error(ERRORMSG(o, e),super_prefixed(path.buf)); 246strbuf_release(&path); 247} 248string_list_clear(rejects,0); 249} 250if(something_displayed) 251fprintf(stderr,_("Aborting\n")); 252} 253 254static intcheck_submodule_move_head(const struct cache_entry *ce, 255const char*old_id, 256const char*new_id, 257struct unpack_trees_options *o) 258{ 259unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 260const struct submodule *sub =submodule_from_ce(ce); 261 262if(!sub) 263return0; 264 265if(o->reset) 266 flags |= SUBMODULE_MOVE_HEAD_FORCE; 267 268if(submodule_move_head(ce->name, old_id, new_id, flags)) 269return o->gently ? -1: 270add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 271return0; 272} 273 274/* 275 * Preform the loading of the repository's gitmodules file. This function is 276 * used by 'check_update()' to perform loading of the gitmodules file in two 277 * differnt situations: 278 * (1) before removing entries from the working tree if the gitmodules file has 279 * been marked for removal. This situation is specified by 'state' == NULL. 280 * (2) before checking out entries to the working tree if the gitmodules file 281 * has been marked for update. This situation is specified by 'state' != NULL. 282 */ 283static voidload_gitmodules_file(struct index_state *index, 284struct checkout *state) 285{ 286int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 287 288if(pos >=0) { 289struct cache_entry *ce = index->cache[pos]; 290if(!state && ce->ce_flags & CE_WT_REMOVE) { 291repo_read_gitmodules(the_repository); 292}else if(state && (ce->ce_flags & CE_UPDATE)) { 293submodule_free(the_repository); 294checkout_entry(ce, state, NULL); 295repo_read_gitmodules(the_repository); 296} 297} 298} 299 300/* 301 * Unlink the last component and schedule the leading directories for 302 * removal, such that empty directories get removed. 303 */ 304static voidunlink_entry(const struct cache_entry *ce) 305{ 306const struct submodule *sub =submodule_from_ce(ce); 307if(sub) { 308/* state.force is set at the caller. */ 309submodule_move_head(ce->name,"HEAD", NULL, 310 SUBMODULE_MOVE_HEAD_FORCE); 311} 312if(!check_leading_path(ce->name,ce_namelen(ce))) 313return; 314if(remove_or_warn(ce->ce_mode, ce->name)) 315return; 316schedule_dir_for_removal(ce->name,ce_namelen(ce)); 317} 318 319static struct progress *get_progress(struct unpack_trees_options *o) 320{ 321unsigned cnt =0, total =0; 322struct index_state *index = &o->result; 323 324if(!o->update || !o->verbose_update) 325return NULL; 326 327for(; cnt < index->cache_nr; cnt++) { 328const struct cache_entry *ce = index->cache[cnt]; 329if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 330 total++; 331} 332 333returnstart_delayed_progress(_("Checking out files"), total); 334} 335 336static intcheck_updates(struct unpack_trees_options *o) 337{ 338unsigned cnt =0; 339int errs =0; 340struct progress *progress = NULL; 341struct index_state *index = &o->result; 342struct checkout state = CHECKOUT_INIT; 343int i; 344 345 state.force =1; 346 state.quiet =1; 347 state.refresh_cache =1; 348 state.istate = index; 349 350 progress =get_progress(o); 351 352if(o->update) 353git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 354 355if(should_update_submodules() && o->update && !o->dry_run) 356load_gitmodules_file(index, NULL); 357 358for(i =0; i < index->cache_nr; i++) { 359const struct cache_entry *ce = index->cache[i]; 360 361if(ce->ce_flags & CE_WT_REMOVE) { 362display_progress(progress, ++cnt); 363if(o->update && !o->dry_run) 364unlink_entry(ce); 365} 366} 367remove_marked_cache_entries(index); 368remove_scheduled_dirs(); 369 370if(should_update_submodules() && o->update && !o->dry_run) 371load_gitmodules_file(index, &state); 372 373enable_delayed_checkout(&state); 374if(repository_format_partial_clone && o->update && !o->dry_run) { 375/* 376 * Prefetch the objects that are to be checked out in the loop 377 * below. 378 */ 379struct oid_array to_fetch = OID_ARRAY_INIT; 380int fetch_if_missing_store = fetch_if_missing; 381 fetch_if_missing =0; 382for(i =0; i < index->cache_nr; i++) { 383struct cache_entry *ce = index->cache[i]; 384if((ce->ce_flags & CE_UPDATE) && 385!S_ISGITLINK(ce->ce_mode)) { 386if(!has_object_file(&ce->oid)) 387oid_array_append(&to_fetch, &ce->oid); 388} 389} 390if(to_fetch.nr) 391fetch_objects(repository_format_partial_clone, 392&to_fetch); 393 fetch_if_missing = fetch_if_missing_store; 394oid_array_clear(&to_fetch); 395} 396for(i =0; i < index->cache_nr; i++) { 397struct cache_entry *ce = index->cache[i]; 398 399if(ce->ce_flags & CE_UPDATE) { 400if(ce->ce_flags & CE_WT_REMOVE) 401BUG("both update and delete flags are set on%s", 402 ce->name); 403display_progress(progress, ++cnt); 404 ce->ce_flags &= ~CE_UPDATE; 405if(o->update && !o->dry_run) { 406 errs |=checkout_entry(ce, &state, NULL); 407} 408} 409} 410stop_progress(&progress); 411 errs |=finish_delayed_checkout(&state); 412if(o->update) 413git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 414return errs !=0; 415} 416 417static intverify_uptodate_sparse(const struct cache_entry *ce, 418struct unpack_trees_options *o); 419static intverify_absent_sparse(const struct cache_entry *ce, 420enum unpack_trees_error_types, 421struct unpack_trees_options *o); 422 423static intapply_sparse_checkout(struct index_state *istate, 424struct cache_entry *ce, 425struct unpack_trees_options *o) 426{ 427int was_skip_worktree =ce_skip_worktree(ce); 428 429if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 430 ce->ce_flags |= CE_SKIP_WORKTREE; 431else 432 ce->ce_flags &= ~CE_SKIP_WORKTREE; 433if(was_skip_worktree !=ce_skip_worktree(ce)) { 434 ce->ce_flags |= CE_UPDATE_IN_BASE; 435mark_fsmonitor_invalid(istate, ce); 436 istate->cache_changed |= CE_ENTRY_CHANGED; 437} 438 439/* 440 * if (!was_skip_worktree && !ce_skip_worktree()) { 441 * This is perfectly normal. Move on; 442 * } 443 */ 444 445/* 446 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 447 * area as a result of ce_skip_worktree() shortcuts in 448 * verify_absent() and verify_uptodate(). 449 * Make sure they don't modify worktree if they are already 450 * outside checkout area 451 */ 452if(was_skip_worktree &&ce_skip_worktree(ce)) { 453 ce->ce_flags &= ~CE_UPDATE; 454 455/* 456 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 457 * on to get that file removed from both index and worktree. 458 * If that file is already outside worktree area, don't 459 * bother remove it. 460 */ 461if(ce->ce_flags & CE_REMOVE) 462 ce->ce_flags &= ~CE_WT_REMOVE; 463} 464 465if(!was_skip_worktree &&ce_skip_worktree(ce)) { 466/* 467 * If CE_UPDATE is set, verify_uptodate() must be called already 468 * also stat info may have lost after merged_entry() so calling 469 * verify_uptodate() again may fail 470 */ 471if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 472return-1; 473 ce->ce_flags |= CE_WT_REMOVE; 474 ce->ce_flags &= ~CE_UPDATE; 475} 476if(was_skip_worktree && !ce_skip_worktree(ce)) { 477if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 478return-1; 479 ce->ce_flags |= CE_UPDATE; 480} 481return0; 482} 483 484staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 485struct unpack_trees_options *o) 486{ 487int ret = o->fn(src, o); 488if(ret >0) 489 ret =0; 490return ret; 491} 492 493static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 494{ 495 ce->ce_flags |= CE_UNPACKED; 496 497if(o->cache_bottom < o->src_index->cache_nr && 498 o->src_index->cache[o->cache_bottom] == ce) { 499int bottom = o->cache_bottom; 500while(bottom < o->src_index->cache_nr && 501 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 502 bottom++; 503 o->cache_bottom = bottom; 504} 505} 506 507static voidmark_all_ce_unused(struct index_state *index) 508{ 509int i; 510for(i =0; i < index->cache_nr; i++) 511 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 512} 513 514static intlocate_in_src_index(const struct cache_entry *ce, 515struct unpack_trees_options *o) 516{ 517struct index_state *index = o->src_index; 518int len =ce_namelen(ce); 519int pos =index_name_pos(index, ce->name, len); 520if(pos <0) 521 pos = -1- pos; 522return pos; 523} 524 525/* 526 * We call unpack_index_entry() with an unmerged cache entry 527 * only in diff-index, and it wants a single callback. Skip 528 * the other unmerged entry with the same name. 529 */ 530static voidmark_ce_used_same_name(struct cache_entry *ce, 531struct unpack_trees_options *o) 532{ 533struct index_state *index = o->src_index; 534int len =ce_namelen(ce); 535int pos; 536 537for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 538struct cache_entry *next = index->cache[pos]; 539if(len !=ce_namelen(next) || 540memcmp(ce->name, next->name, len)) 541break; 542mark_ce_used(next, o); 543} 544} 545 546static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 547{ 548const struct index_state *index = o->src_index; 549int pos = o->cache_bottom; 550 551while(pos < index->cache_nr) { 552struct cache_entry *ce = index->cache[pos]; 553if(!(ce->ce_flags & CE_UNPACKED)) 554return ce; 555 pos++; 556} 557return NULL; 558} 559 560static voidadd_same_unmerged(const struct cache_entry *ce, 561struct unpack_trees_options *o) 562{ 563struct index_state *index = o->src_index; 564int len =ce_namelen(ce); 565int pos =index_name_pos(index, ce->name, len); 566 567if(0<= pos) 568die("programming error in a caller of mark_ce_used_same_name"); 569for(pos = -pos -1; pos < index->cache_nr; pos++) { 570struct cache_entry *next = index->cache[pos]; 571if(len !=ce_namelen(next) || 572memcmp(ce->name, next->name, len)) 573break; 574add_entry(o, next,0,0); 575mark_ce_used(next, o); 576} 577} 578 579static intunpack_index_entry(struct cache_entry *ce, 580struct unpack_trees_options *o) 581{ 582const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 583int ret; 584 585 src[0] = ce; 586 587mark_ce_used(ce, o); 588if(ce_stage(ce)) { 589if(o->skip_unmerged) { 590add_entry(o, ce,0,0); 591return0; 592} 593} 594 ret =call_unpack_fn(src, o); 595if(ce_stage(ce)) 596mark_ce_used_same_name(ce, o); 597return ret; 598} 599 600static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 601 602static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 603{ 604struct unpack_trees_options *o = info->data; 605 606if(o->diff_index_cached) 607return; 608 o->cache_bottom = bottom; 609} 610 611static intswitch_cache_bottom(struct traverse_info *info) 612{ 613struct unpack_trees_options *o = info->data; 614int ret, pos; 615 616if(o->diff_index_cached) 617return0; 618 ret = o->cache_bottom; 619 pos =find_cache_pos(info->prev, &info->name); 620 621if(pos < -1) 622 o->cache_bottom = -2- pos; 623else if(pos <0) 624 o->cache_bottom = o->src_index->cache_nr; 625return ret; 626} 627 628staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 629{ 630return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 631} 632 633static inttraverse_trees_recursive(int n,unsigned long dirmask, 634unsigned long df_conflicts, 635struct name_entry *names, 636struct traverse_info *info) 637{ 638int i, ret, bottom; 639int nr_buf =0; 640struct tree_desc t[MAX_UNPACK_TREES]; 641void*buf[MAX_UNPACK_TREES]; 642struct traverse_info newinfo; 643struct name_entry *p; 644 645 p = names; 646while(!p->mode) 647 p++; 648 649 newinfo = *info; 650 newinfo.prev = info; 651 newinfo.pathspec = info->pathspec; 652 newinfo.name = *p; 653 newinfo.pathlen +=tree_entry_len(p) +1; 654 newinfo.df_conflicts |= df_conflicts; 655 656/* 657 * Fetch the tree from the ODB for each peer directory in the 658 * n commits. 659 * 660 * For 2- and 3-way traversals, we try to avoid hitting the 661 * ODB twice for the same OID. This should yield a nice speed 662 * up in checkouts and merges when the commits are similar. 663 * 664 * We don't bother doing the full O(n^2) search for larger n, 665 * because wider traversals don't happen that often and we 666 * avoid the search setup. 667 * 668 * When 2 peer OIDs are the same, we just copy the tree 669 * descriptor data. This implicitly borrows the buffer 670 * data from the earlier cell. 671 */ 672for(i =0; i < n; i++, dirmask >>=1) { 673if(i >0&&are_same_oid(&names[i], &names[i -1])) 674 t[i] = t[i -1]; 675else if(i >1&&are_same_oid(&names[i], &names[i -2])) 676 t[i] = t[i -2]; 677else{ 678const struct object_id *oid = NULL; 679if(dirmask &1) 680 oid = names[i].oid; 681 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 682} 683} 684 685 bottom =switch_cache_bottom(&newinfo); 686 ret =traverse_trees(n, t, &newinfo); 687restore_cache_bottom(&newinfo, bottom); 688 689for(i =0; i < nr_buf; i++) 690free(buf[i]); 691 692return ret; 693} 694 695/* 696 * Compare the traverse-path to the cache entry without actually 697 * having to generate the textual representation of the traverse 698 * path. 699 * 700 * NOTE! This *only* compares up to the size of the traverse path 701 * itself - the caller needs to do the final check for the cache 702 * entry having more data at the end! 703 */ 704static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 705{ 706int len, pathlen, ce_len; 707const char*ce_name; 708 709if(info->prev) { 710int cmp =do_compare_entry_piecewise(ce, info->prev, 711&info->name); 712if(cmp) 713return cmp; 714} 715 pathlen = info->pathlen; 716 ce_len =ce_namelen(ce); 717 718/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 719if(ce_len < pathlen) 720return-1; 721 722 ce_len -= pathlen; 723 ce_name = ce->name + pathlen; 724 725 len =tree_entry_len(n); 726returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 727} 728 729static intdo_compare_entry(const struct cache_entry *ce, 730const struct traverse_info *info, 731const struct name_entry *n) 732{ 733int len, pathlen, ce_len; 734const char*ce_name; 735int cmp; 736 737/* 738 * If we have not precomputed the traverse path, it is quicker 739 * to avoid doing so. But if we have precomputed it, 740 * it is quicker to use the precomputed version. 741 */ 742if(!info->traverse_path) 743returndo_compare_entry_piecewise(ce, info, n); 744 745 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 746if(cmp) 747return cmp; 748 749 pathlen = info->pathlen; 750 ce_len =ce_namelen(ce); 751 752if(ce_len < pathlen) 753return-1; 754 755 ce_len -= pathlen; 756 ce_name = ce->name + pathlen; 757 758 len =tree_entry_len(n); 759returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 760} 761 762static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 763{ 764int cmp =do_compare_entry(ce, info, n); 765if(cmp) 766return cmp; 767 768/* 769 * Even if the beginning compared identically, the ce should 770 * compare as bigger than a directory leading up to it! 771 */ 772returnce_namelen(ce) >traverse_path_len(info, n); 773} 774 775static intce_in_traverse_path(const struct cache_entry *ce, 776const struct traverse_info *info) 777{ 778if(!info->prev) 779return1; 780if(do_compare_entry(ce, info->prev, &info->name)) 781return0; 782/* 783 * If ce (blob) is the same name as the path (which is a tree 784 * we will be descending into), it won't be inside it. 785 */ 786return(info->pathlen <ce_namelen(ce)); 787} 788 789static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 790{ 791int len =traverse_path_len(info, n); 792struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 793 794 ce->ce_mode =create_ce_mode(n->mode); 795 ce->ce_flags =create_ce_flags(stage); 796 ce->ce_namelen = len; 797oidcpy(&ce->oid, n->oid); 798make_traverse_path(ce->name, info, n); 799 800return ce; 801} 802 803static intunpack_nondirectories(int n,unsigned long mask, 804unsigned long dirmask, 805struct cache_entry **src, 806const struct name_entry *names, 807const struct traverse_info *info) 808{ 809int i; 810struct unpack_trees_options *o = info->data; 811unsigned long conflicts = info->df_conflicts | dirmask; 812 813/* Do we have *only* directories? Nothing to do */ 814if(mask == dirmask && !src[0]) 815return0; 816 817/* 818 * Ok, we've filled in up to any potential index entry in src[0], 819 * now do the rest. 820 */ 821for(i =0; i < n; i++) { 822int stage; 823unsigned int bit =1ul<< i; 824if(conflicts & bit) { 825 src[i + o->merge] = o->df_conflict_entry; 826continue; 827} 828if(!(mask & bit)) 829continue; 830if(!o->merge) 831 stage =0; 832else if(i +1< o->head_idx) 833 stage =1; 834else if(i +1> o->head_idx) 835 stage =3; 836else 837 stage =2; 838 src[i + o->merge] =create_ce_entry(info, names + i, stage); 839} 840 841if(o->merge) { 842int rc =call_unpack_fn((const struct cache_entry *const*)src, 843 o); 844for(i =0; i < n; i++) { 845struct cache_entry *ce = src[i + o->merge]; 846if(ce != o->df_conflict_entry) 847free(ce); 848} 849return rc; 850} 851 852for(i =0; i < n; i++) 853if(src[i] && src[i] != o->df_conflict_entry) 854if(do_add_entry(o, src[i],0,0)) 855return-1; 856 857return0; 858} 859 860static intunpack_failed(struct unpack_trees_options *o,const char*message) 861{ 862discard_index(&o->result); 863if(!o->gently && !o->exiting_early) { 864if(message) 865returnerror("%s", message); 866return-1; 867} 868return-1; 869} 870 871/* 872 * The tree traversal is looking at name p. If we have a matching entry, 873 * return it. If name p is a directory in the index, do not return 874 * anything, as we will want to match it when the traversal descends into 875 * the directory. 876 */ 877static intfind_cache_pos(struct traverse_info *info, 878const struct name_entry *p) 879{ 880int pos; 881struct unpack_trees_options *o = info->data; 882struct index_state *index = o->src_index; 883int pfxlen = info->pathlen; 884int p_len =tree_entry_len(p); 885 886for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 887const struct cache_entry *ce = index->cache[pos]; 888const char*ce_name, *ce_slash; 889int cmp, ce_len; 890 891if(ce->ce_flags & CE_UNPACKED) { 892/* 893 * cache_bottom entry is already unpacked, so 894 * we can never match it; don't check it 895 * again. 896 */ 897if(pos == o->cache_bottom) 898++o->cache_bottom; 899continue; 900} 901if(!ce_in_traverse_path(ce, info)) { 902/* 903 * Check if we can skip future cache checks 904 * (because we're already past all possible 905 * entries in the traverse path). 906 */ 907if(info->traverse_path) { 908if(strncmp(ce->name, info->traverse_path, 909 info->pathlen) >0) 910break; 911} 912continue; 913} 914 ce_name = ce->name + pfxlen; 915 ce_slash =strchr(ce_name,'/'); 916if(ce_slash) 917 ce_len = ce_slash - ce_name; 918else 919 ce_len =ce_namelen(ce) - pfxlen; 920 cmp =name_compare(p->path, p_len, ce_name, ce_len); 921/* 922 * Exact match; if we have a directory we need to 923 * delay returning it. 924 */ 925if(!cmp) 926return ce_slash ? -2- pos : pos; 927if(0< cmp) 928continue;/* keep looking */ 929/* 930 * ce_name sorts after p->path; could it be that we 931 * have files under p->path directory in the index? 932 * E.g. ce_name == "t-i", and p->path == "t"; we may 933 * have "t/a" in the index. 934 */ 935if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 936 ce_name[p_len] <'/') 937continue;/* keep looking */ 938break; 939} 940return-1; 941} 942 943static struct cache_entry *find_cache_entry(struct traverse_info *info, 944const struct name_entry *p) 945{ 946int pos =find_cache_pos(info, p); 947struct unpack_trees_options *o = info->data; 948 949if(0<= pos) 950return o->src_index->cache[pos]; 951else 952return NULL; 953} 954 955static voiddebug_path(struct traverse_info *info) 956{ 957if(info->prev) { 958debug_path(info->prev); 959if(*info->prev->name.path) 960putchar('/'); 961} 962printf("%s", info->name.path); 963} 964 965static voiddebug_name_entry(int i,struct name_entry *n) 966{ 967printf("ent#%d %06o%s\n", i, 968 n->path ? n->mode :0, 969 n->path ? n->path :"(missing)"); 970} 971 972static voiddebug_unpack_callback(int n, 973unsigned long mask, 974unsigned long dirmask, 975struct name_entry *names, 976struct traverse_info *info) 977{ 978int i; 979printf("* unpack mask%lu, dirmask%lu, cnt%d", 980 mask, dirmask, n); 981debug_path(info); 982putchar('\n'); 983for(i =0; i < n; i++) 984debug_name_entry(i, names + i); 985} 986 987static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 988{ 989struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 990struct unpack_trees_options *o = info->data; 991const struct name_entry *p = names; 992 993/* Find first entry with a real name (we could use "mask" too) */ 994while(!p->mode) 995 p++; 996 997if(o->debug_unpack) 998debug_unpack_callback(n, mask, dirmask, names, info); 9991000/* Are we supposed to look at the index too? */1001if(o->merge) {1002while(1) {1003int cmp;1004struct cache_entry *ce;10051006if(o->diff_index_cached)1007 ce =next_cache_entry(o);1008else1009 ce =find_cache_entry(info, p);10101011if(!ce)1012break;1013 cmp =compare_entry(ce, info, p);1014if(cmp <0) {1015if(unpack_index_entry(ce, o) <0)1016returnunpack_failed(o, NULL);1017continue;1018}1019if(!cmp) {1020if(ce_stage(ce)) {1021/*1022 * If we skip unmerged index1023 * entries, we'll skip this1024 * entry *and* the tree1025 * entries associated with it!1026 */1027if(o->skip_unmerged) {1028add_same_unmerged(ce, o);1029return mask;1030}1031}1032 src[0] = ce;1033}1034break;1035}1036}10371038if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1039return-1;10401041if(o->merge && src[0]) {1042if(ce_stage(src[0]))1043mark_ce_used_same_name(src[0], o);1044else1045mark_ce_used(src[0], o);1046}10471048/* Now handle any directories.. */1049if(dirmask) {1050/* special case: "diff-index --cached" looking at a tree */1051if(o->diff_index_cached &&1052 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1053int matches;1054 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1055 names, info);1056/*1057 * Everything under the name matches; skip the1058 * entire hierarchy. diff_index_cached codepath1059 * special cases D/F conflicts in such a way that1060 * it does not do any look-ahead, so this is safe.1061 */1062if(matches) {1063 o->cache_bottom += matches;1064return mask;1065}1066}10671068if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1069 names, info) <0)1070return-1;1071return mask;1072}10731074return mask;1075}10761077static intclear_ce_flags_1(struct cache_entry **cache,int nr,1078struct strbuf *prefix,1079int select_mask,int clear_mask,1080struct exclude_list *el,int defval);10811082/* Whole directory matching */1083static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1084struct strbuf *prefix,1085char*basename,1086int select_mask,int clear_mask,1087struct exclude_list *el,int defval)1088{1089struct cache_entry **cache_end;1090int dtype = DT_DIR;1091int ret =is_excluded_from_list(prefix->buf, prefix->len,1092 basename, &dtype, el, &the_index);1093int rc;10941095strbuf_addch(prefix,'/');10961097/* If undecided, use matching result of parent dir in defval */1098if(ret <0)1099 ret = defval;11001101for(cache_end = cache; cache_end != cache + nr; cache_end++) {1102struct cache_entry *ce = *cache_end;1103if(strncmp(ce->name, prefix->buf, prefix->len))1104break;1105}11061107/*1108 * TODO: check el, if there are no patterns that may conflict1109 * with ret (iow, we know in advance the incl/excl1110 * decision for the entire directory), clear flag here without1111 * calling clear_ce_flags_1(). That function will call1112 * the expensive is_excluded_from_list() on every entry.1113 */1114 rc =clear_ce_flags_1(cache, cache_end - cache,1115 prefix,1116 select_mask, clear_mask,1117 el, ret);1118strbuf_setlen(prefix, prefix->len -1);1119return rc;1120}11211122/*1123 * Traverse the index, find every entry that matches according to1124 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1125 * number of traversed entries.1126 *1127 * If select_mask is non-zero, only entries whose ce_flags has on of1128 * those bits enabled are traversed.1129 *1130 * cache : pointer to an index entry1131 * prefix_len : an offset to its path1132 *1133 * The current path ("prefix") including the trailing '/' is1134 * cache[0]->name[0..(prefix_len-1)]1135 * Top level path has prefix_len zero.1136 */1137static intclear_ce_flags_1(struct cache_entry **cache,int nr,1138struct strbuf *prefix,1139int select_mask,int clear_mask,1140struct exclude_list *el,int defval)1141{1142struct cache_entry **cache_end = cache + nr;11431144/*1145 * Process all entries that have the given prefix and meet1146 * select_mask condition1147 */1148while(cache != cache_end) {1149struct cache_entry *ce = *cache;1150const char*name, *slash;1151int len, dtype, ret;11521153if(select_mask && !(ce->ce_flags & select_mask)) {1154 cache++;1155continue;1156}11571158if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1159break;11601161 name = ce->name + prefix->len;1162 slash =strchr(name,'/');11631164/* If it's a directory, try whole directory match first */1165if(slash) {1166int processed;11671168 len = slash - name;1169strbuf_add(prefix, name, len);11701171 processed =clear_ce_flags_dir(cache, cache_end - cache,1172 prefix,1173 prefix->buf + prefix->len - len,1174 select_mask, clear_mask,1175 el, defval);11761177/* clear_c_f_dir eats a whole dir already? */1178if(processed) {1179 cache += processed;1180strbuf_setlen(prefix, prefix->len - len);1181continue;1182}11831184strbuf_addch(prefix,'/');1185 cache +=clear_ce_flags_1(cache, cache_end - cache,1186 prefix,1187 select_mask, clear_mask, el, defval);1188strbuf_setlen(prefix, prefix->len - len -1);1189continue;1190}11911192/* Non-directory */1193 dtype =ce_to_dtype(ce);1194 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1195 name, &dtype, el, &the_index);1196if(ret <0)1197 ret = defval;1198if(ret >0)1199 ce->ce_flags &= ~clear_mask;1200 cache++;1201}1202return nr - (cache_end - cache);1203}12041205static intclear_ce_flags(struct cache_entry **cache,int nr,1206int select_mask,int clear_mask,1207struct exclude_list *el)1208{1209static struct strbuf prefix = STRBUF_INIT;12101211strbuf_reset(&prefix);12121213returnclear_ce_flags_1(cache, nr,1214&prefix,1215 select_mask, clear_mask,1216 el,0);1217}12181219/*1220 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1221 */1222static voidmark_new_skip_worktree(struct exclude_list *el,1223struct index_state *the_index,1224int select_flag,int skip_wt_flag)1225{1226int i;12271228/*1229 * 1. Pretend the narrowest worktree: only unmerged entries1230 * are checked out1231 */1232for(i =0; i < the_index->cache_nr; i++) {1233struct cache_entry *ce = the_index->cache[i];12341235if(select_flag && !(ce->ce_flags & select_flag))1236continue;12371238if(!ce_stage(ce))1239 ce->ce_flags |= skip_wt_flag;1240else1241 ce->ce_flags &= ~skip_wt_flag;1242}12431244/*1245 * 2. Widen worktree according to sparse-checkout file.1246 * Matched entries will have skip_wt_flag cleared (i.e. "in")1247 */1248clear_ce_flags(the_index->cache, the_index->cache_nr,1249 select_flag, skip_wt_flag, el);1250}12511252static intverify_absent(const struct cache_entry *,1253enum unpack_trees_error_types,1254struct unpack_trees_options *);1255/*1256 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1257 * resulting index, -2 on failure to reflect the changes to the work tree.1258 *1259 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1260 */1261intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1262{1263int i, ret;1264static struct cache_entry *dfc;1265struct exclude_list el;12661267if(len > MAX_UNPACK_TREES)1268die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12691270memset(&el,0,sizeof(el));1271if(!core_apply_sparse_checkout || !o->update)1272 o->skip_sparse_checkout =1;1273if(!o->skip_sparse_checkout) {1274char*sparse =git_pathdup("info/sparse-checkout");1275if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1276 o->skip_sparse_checkout =1;1277else1278 o->el = ⪙1279free(sparse);1280}12811282memset(&o->result,0,sizeof(o->result));1283 o->result.initialized =1;1284 o->result.timestamp.sec = o->src_index->timestamp.sec;1285 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1286 o->result.version = o->src_index->version;1287if(!o->src_index->split_index) {1288 o->result.split_index = NULL;1289}else if(o->src_index == o->dst_index) {1290/*1291 * o->dst_index (and thus o->src_index) will be discarded1292 * and overwritten with o->result at the end of this function,1293 * so just use src_index's split_index to avoid having to1294 * create a new one.1295 */1296 o->result.split_index = o->src_index->split_index;1297 o->result.split_index->refcount++;1298}else{1299 o->result.split_index =init_split_index(&o->result);1300}1301oidcpy(&o->result.oid, &o->src_index->oid);1302 o->merge_size = len;1303mark_all_ce_unused(o->src_index);13041305/*1306 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1307 */1308if(!o->skip_sparse_checkout)1309mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);13101311if(!dfc)1312 dfc =xcalloc(1,cache_entry_size(0));1313 o->df_conflict_entry = dfc;13141315if(len) {1316const char*prefix = o->prefix ? o->prefix :"";1317struct traverse_info info;13181319setup_traverse_info(&info, prefix);1320 info.fn = unpack_callback;1321 info.data = o;1322 info.show_all_errors = o->show_all_errors;1323 info.pathspec = o->pathspec;13241325if(o->prefix) {1326/*1327 * Unpack existing index entries that sort before the1328 * prefix the tree is spliced into. Note that o->merge1329 * is always true in this case.1330 */1331while(1) {1332struct cache_entry *ce =next_cache_entry(o);1333if(!ce)1334break;1335if(ce_in_traverse_path(ce, &info))1336break;1337if(unpack_index_entry(ce, o) <0)1338goto return_failed;1339}1340}13411342if(traverse_trees(len, t, &info) <0)1343goto return_failed;1344}13451346/* Any left-over entries in the index? */1347if(o->merge) {1348while(1) {1349struct cache_entry *ce =next_cache_entry(o);1350if(!ce)1351break;1352if(unpack_index_entry(ce, o) <0)1353goto return_failed;1354}1355}1356mark_all_ce_unused(o->src_index);13571358if(o->trivial_merges_only && o->nontrivial_merge) {1359 ret =unpack_failed(o,"Merge requires file-level merging");1360goto done;1361}13621363if(!o->skip_sparse_checkout) {1364int empty_worktree =1;13651366/*1367 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11368 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1369 * so apply_sparse_checkout() won't attempt to remove it from worktree1370 */1371mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13721373 ret =0;1374for(i =0; i < o->result.cache_nr; i++) {1375struct cache_entry *ce = o->result.cache[i];13761377/*1378 * Entries marked with CE_ADDED in merged_entry() do not have1379 * verify_absent() check (the check is effectively disabled1380 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1381 *1382 * Do the real check now because we have had1383 * correct CE_NEW_SKIP_WORKTREE1384 */1385if(ce->ce_flags & CE_ADDED &&1386verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1387if(!o->show_all_errors)1388goto return_failed;1389 ret = -1;1390}13911392if(apply_sparse_checkout(&o->result, ce, o)) {1393if(!o->show_all_errors)1394goto return_failed;1395 ret = -1;1396}1397if(!ce_skip_worktree(ce))1398 empty_worktree =0;13991400}1401if(ret <0)1402goto return_failed;1403/*1404 * Sparse checkout is meant to narrow down checkout area1405 * but it does not make sense to narrow down to empty working1406 * tree. This is usually a mistake in sparse checkout rules.1407 * Do not allow users to do that.1408 */1409if(o->result.cache_nr && empty_worktree) {1410 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1411goto done;1412}1413}14141415 ret =check_updates(o) ? (-2) :0;1416if(o->dst_index) {1417if(!ret) {1418if(!o->result.cache_tree)1419 o->result.cache_tree =cache_tree();1420if(!cache_tree_fully_valid(o->result.cache_tree))1421cache_tree_update(&o->result,1422 WRITE_TREE_SILENT |1423 WRITE_TREE_REPAIR);1424}1425move_index_extensions(&o->result, o->src_index);1426discard_index(o->dst_index);1427*o->dst_index = o->result;1428}else{1429discard_index(&o->result);1430}1431 o->src_index = NULL;14321433done:1434clear_exclude_list(&el);1435return ret;14361437return_failed:1438if(o->show_all_errors)1439display_error_msgs(o);1440mark_all_ce_unused(o->src_index);1441 ret =unpack_failed(o, NULL);1442if(o->exiting_early)1443 ret =0;1444goto done;1445}14461447/* Here come the merge functions */14481449static intreject_merge(const struct cache_entry *ce,1450struct unpack_trees_options *o)1451{1452return o->gently ? -1:1453add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1454}14551456static intsame(const struct cache_entry *a,const struct cache_entry *b)1457{1458if(!!a != !!b)1459return0;1460if(!a && !b)1461return1;1462if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1463return0;1464return a->ce_mode == b->ce_mode &&1465!oidcmp(&a->oid, &b->oid);1466}146714681469/*1470 * When a CE gets turned into an unmerged entry, we1471 * want it to be up-to-date1472 */1473static intverify_uptodate_1(const struct cache_entry *ce,1474struct unpack_trees_options *o,1475enum unpack_trees_error_types error_type)1476{1477struct stat st;14781479if(o->index_only)1480return0;14811482/*1483 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1484 * if this entry is truly up-to-date because this file may be1485 * overwritten.1486 */1487if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1488;/* keep checking */1489else if(o->reset ||ce_uptodate(ce))1490return0;14911492if(!lstat(ce->name, &st)) {1493int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1494unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);14951496if(submodule_from_ce(ce)) {1497int r =check_submodule_move_head(ce,1498"HEAD",oid_to_hex(&ce->oid), o);1499if(r)1500return o->gently ? -1:1501add_rejected_path(o, error_type, ce->name);1502return0;1503}15041505if(!changed)1506return0;1507/*1508 * Historic default policy was to allow submodule to be out1509 * of sync wrt the superproject index. If the submodule was1510 * not considered interesting above, we don't care here.1511 */1512if(S_ISGITLINK(ce->ce_mode))1513return0;15141515 errno =0;1516}1517if(errno == ENOENT)1518return0;1519return o->gently ? -1:1520add_rejected_path(o, error_type, ce->name);1521}15221523intverify_uptodate(const struct cache_entry *ce,1524struct unpack_trees_options *o)1525{1526if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1527return0;1528returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1529}15301531static intverify_uptodate_sparse(const struct cache_entry *ce,1532struct unpack_trees_options *o)1533{1534returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1535}15361537static voidinvalidate_ce_path(const struct cache_entry *ce,1538struct unpack_trees_options *o)1539{1540if(!ce)1541return;1542cache_tree_invalidate_path(o->src_index, ce->name);1543untracked_cache_invalidate_path(o->src_index, ce->name,1);1544}15451546/*1547 * Check that checking out ce->sha1 in subdir ce->name is not1548 * going to overwrite any working files.1549 *1550 * Currently, git does not checkout subprojects during a superproject1551 * checkout, so it is not going to overwrite anything.1552 */1553static intverify_clean_submodule(const char*old_sha1,1554const struct cache_entry *ce,1555enum unpack_trees_error_types error_type,1556struct unpack_trees_options *o)1557{1558if(!submodule_from_ce(ce))1559return0;15601561returncheck_submodule_move_head(ce, old_sha1,1562oid_to_hex(&ce->oid), o);1563}15641565static intverify_clean_subdirectory(const struct cache_entry *ce,1566enum unpack_trees_error_types error_type,1567struct unpack_trees_options *o)1568{1569/*1570 * we are about to extract "ce->name"; we would not want to lose1571 * anything in the existing directory there.1572 */1573int namelen;1574int i;1575struct dir_struct d;1576char*pathbuf;1577int cnt =0;15781579if(S_ISGITLINK(ce->ce_mode)) {1580struct object_id oid;1581int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1582/*1583 * If we are not going to update the submodule, then1584 * we don't care.1585 */1586if(!sub_head && !oidcmp(&oid, &ce->oid))1587return0;1588returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1589 ce, error_type, o);1590}15911592/*1593 * First let's make sure we do not have a local modification1594 * in that directory.1595 */1596 namelen =ce_namelen(ce);1597for(i =locate_in_src_index(ce, o);1598 i < o->src_index->cache_nr;1599 i++) {1600struct cache_entry *ce2 = o->src_index->cache[i];1601int len =ce_namelen(ce2);1602if(len < namelen ||1603strncmp(ce->name, ce2->name, namelen) ||1604 ce2->name[namelen] !='/')1605break;1606/*1607 * ce2->name is an entry in the subdirectory to be1608 * removed.1609 */1610if(!ce_stage(ce2)) {1611if(verify_uptodate(ce2, o))1612return-1;1613add_entry(o, ce2, CE_REMOVE,0);1614mark_ce_used(ce2, o);1615}1616 cnt++;1617}16181619/*1620 * Then we need to make sure that we do not lose a locally1621 * present file that is not ignored.1622 */1623 pathbuf =xstrfmt("%.*s/", namelen, ce->name);16241625memset(&d,0,sizeof(d));1626if(o->dir)1627 d.exclude_per_dir = o->dir->exclude_per_dir;1628 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1629if(i)1630return o->gently ? -1:1631add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1632free(pathbuf);1633return cnt;1634}16351636/*1637 * This gets called when there was no index entry for the tree entry 'dst',1638 * but we found a file in the working tree that 'lstat()' said was fine,1639 * and we're on a case-insensitive filesystem.1640 *1641 * See if we can find a case-insensitive match in the index that also1642 * matches the stat information, and assume it's that other file!1643 */1644static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1645{1646const struct cache_entry *src;16471648 src =index_file_exists(o->src_index, name, len,1);1649return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1650}16511652static intcheck_ok_to_remove(const char*name,int len,int dtype,1653const struct cache_entry *ce,struct stat *st,1654enum unpack_trees_error_types error_type,1655struct unpack_trees_options *o)1656{1657const struct cache_entry *result;16581659/*1660 * It may be that the 'lstat()' succeeded even though1661 * target 'ce' was absent, because there is an old1662 * entry that is different only in case..1663 *1664 * Ignore that lstat() if it matches.1665 */1666if(ignore_case &&icase_exists(o, name, len, st))1667return0;16681669if(o->dir &&1670is_excluded(o->dir, &the_index, name, &dtype))1671/*1672 * ce->name is explicitly excluded, so it is Ok to1673 * overwrite it.1674 */1675return0;1676if(S_ISDIR(st->st_mode)) {1677/*1678 * We are checking out path "foo" and1679 * found "foo/." in the working tree.1680 * This is tricky -- if we have modified1681 * files that are in "foo/" we would lose1682 * them.1683 */1684if(verify_clean_subdirectory(ce, error_type, o) <0)1685return-1;1686return0;1687}16881689/*1690 * The previous round may already have decided to1691 * delete this path, which is in a subdirectory that1692 * is being replaced with a blob.1693 */1694 result =index_file_exists(&o->result, name, len,0);1695if(result) {1696if(result->ce_flags & CE_REMOVE)1697return0;1698}16991700return o->gently ? -1:1701add_rejected_path(o, error_type, name);1702}17031704/*1705 * We do not want to remove or overwrite a working tree file that1706 * is not tracked, unless it is ignored.1707 */1708static intverify_absent_1(const struct cache_entry *ce,1709enum unpack_trees_error_types error_type,1710struct unpack_trees_options *o)1711{1712int len;1713struct stat st;17141715if(o->index_only || o->reset || !o->update)1716return0;17171718 len =check_leading_path(ce->name,ce_namelen(ce));1719if(!len)1720return0;1721else if(len >0) {1722char*path;1723int ret;17241725 path =xmemdupz(ce->name, len);1726if(lstat(path, &st))1727 ret =error_errno("cannot stat '%s'", path);1728else{1729if(submodule_from_ce(ce))1730 ret =check_submodule_move_head(ce,1731oid_to_hex(&ce->oid),1732 NULL, o);1733else1734 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1735&st, error_type, o);1736}1737free(path);1738return ret;1739}else if(lstat(ce->name, &st)) {1740if(errno != ENOENT)1741returnerror_errno("cannot stat '%s'", ce->name);1742return0;1743}else{1744if(submodule_from_ce(ce))1745returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1746 NULL, o);17471748returncheck_ok_to_remove(ce->name,ce_namelen(ce),1749ce_to_dtype(ce), ce, &st,1750 error_type, o);1751}1752}17531754static intverify_absent(const struct cache_entry *ce,1755enum unpack_trees_error_types error_type,1756struct unpack_trees_options *o)1757{1758if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1759return0;1760returnverify_absent_1(ce, error_type, o);1761}17621763static intverify_absent_sparse(const struct cache_entry *ce,1764enum unpack_trees_error_types error_type,1765struct unpack_trees_options *o)1766{1767enum unpack_trees_error_types orphaned_error = error_type;1768if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1769 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17701771returnverify_absent_1(ce, orphaned_error, o);1772}17731774static intmerged_entry(const struct cache_entry *ce,1775const struct cache_entry *old,1776struct unpack_trees_options *o)1777{1778int update = CE_UPDATE;1779struct cache_entry *merge =dup_entry(ce);17801781if(!old) {1782/*1783 * New index entries. In sparse checkout, the following1784 * verify_absent() will be delayed until after1785 * traverse_trees() finishes in unpack_trees(), then:1786 *1787 * - CE_NEW_SKIP_WORKTREE will be computed correctly1788 * - verify_absent() be called again, this time with1789 * correct CE_NEW_SKIP_WORKTREE1790 *1791 * verify_absent() call here does nothing in sparse1792 * checkout (i.e. o->skip_sparse_checkout == 0)1793 */1794 update |= CE_ADDED;1795 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;17961797if(verify_absent(merge,1798 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1799free(merge);1800return-1;1801}1802invalidate_ce_path(merge, o);18031804if(submodule_from_ce(ce)) {1805int ret =check_submodule_move_head(ce, NULL,1806oid_to_hex(&ce->oid),1807 o);1808if(ret)1809return ret;1810}18111812}else if(!(old->ce_flags & CE_CONFLICTED)) {1813/*1814 * See if we can re-use the old CE directly?1815 * That way we get the uptodate stat info.1816 *1817 * This also removes the UPDATE flag on a match; otherwise1818 * we will end up overwriting local changes in the work tree.1819 */1820if(same(old, merge)) {1821copy_cache_entry(merge, old);1822 update =0;1823}else{1824if(verify_uptodate(old, o)) {1825free(merge);1826return-1;1827}1828/* Migrate old flags over */1829 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1830invalidate_ce_path(old, o);1831}18321833if(submodule_from_ce(ce)) {1834int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1835oid_to_hex(&ce->oid),1836 o);1837if(ret)1838return ret;1839}1840}else{1841/*1842 * Previously unmerged entry left as an existence1843 * marker by read_index_unmerged();1844 */1845invalidate_ce_path(old, o);1846}18471848do_add_entry(o, merge, update, CE_STAGEMASK);1849return1;1850}18511852static intdeleted_entry(const struct cache_entry *ce,1853const struct cache_entry *old,1854struct unpack_trees_options *o)1855{1856/* Did it exist in the index? */1857if(!old) {1858if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1859return-1;1860return0;1861}1862if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1863return-1;1864add_entry(o, ce, CE_REMOVE,0);1865invalidate_ce_path(ce, o);1866return1;1867}18681869static intkeep_entry(const struct cache_entry *ce,1870struct unpack_trees_options *o)1871{1872add_entry(o, ce,0,0);1873return1;1874}18751876#if DBRT_DEBUG1877static voidshow_stage_entry(FILE*o,1878const char*label,const struct cache_entry *ce)1879{1880if(!ce)1881fprintf(o,"%s(missing)\n", label);1882else1883fprintf(o,"%s%06o%s %d\t%s\n",1884 label,1885 ce->ce_mode,1886oid_to_hex(&ce->oid),1887ce_stage(ce),1888 ce->name);1889}1890#endif18911892intthreeway_merge(const struct cache_entry *const*stages,1893struct unpack_trees_options *o)1894{1895const struct cache_entry *index;1896const struct cache_entry *head;1897const struct cache_entry *remote = stages[o->head_idx +1];1898int count;1899int head_match =0;1900int remote_match =0;19011902int df_conflict_head =0;1903int df_conflict_remote =0;19041905int any_anc_missing =0;1906int no_anc_exists =1;1907int i;19081909for(i =1; i < o->head_idx; i++) {1910if(!stages[i] || stages[i] == o->df_conflict_entry)1911 any_anc_missing =1;1912else1913 no_anc_exists =0;1914}19151916 index = stages[0];1917 head = stages[o->head_idx];19181919if(head == o->df_conflict_entry) {1920 df_conflict_head =1;1921 head = NULL;1922}19231924if(remote == o->df_conflict_entry) {1925 df_conflict_remote =1;1926 remote = NULL;1927}19281929/*1930 * First, if there's a #16 situation, note that to prevent #131931 * and #14.1932 */1933if(!same(remote, head)) {1934for(i =1; i < o->head_idx; i++) {1935if(same(stages[i], head)) {1936 head_match = i;1937}1938if(same(stages[i], remote)) {1939 remote_match = i;1940}1941}1942}19431944/*1945 * We start with cases where the index is allowed to match1946 * something other than the head: #14(ALT) and #2ALT, where it1947 * is permitted to match the result instead.1948 */1949/* #14, #14ALT, #2ALT */1950if(remote && !df_conflict_head && head_match && !remote_match) {1951if(index && !same(index, remote) && !same(index, head))1952returnreject_merge(index, o);1953returnmerged_entry(remote, index, o);1954}1955/*1956 * If we have an entry in the index cache, then we want to1957 * make sure that it matches head.1958 */1959if(index && !same(index, head))1960returnreject_merge(index, o);19611962if(head) {1963/* #5ALT, #15 */1964if(same(head, remote))1965returnmerged_entry(head, index, o);1966/* #13, #3ALT */1967if(!df_conflict_remote && remote_match && !head_match)1968returnmerged_entry(head, index, o);1969}19701971/* #1 */1972if(!head && !remote && any_anc_missing)1973return0;19741975/*1976 * Under the "aggressive" rule, we resolve mostly trivial1977 * cases that we historically had git-merge-one-file resolve.1978 */1979if(o->aggressive) {1980int head_deleted = !head;1981int remote_deleted = !remote;1982const struct cache_entry *ce = NULL;19831984if(index)1985 ce = index;1986else if(head)1987 ce = head;1988else if(remote)1989 ce = remote;1990else{1991for(i =1; i < o->head_idx; i++) {1992if(stages[i] && stages[i] != o->df_conflict_entry) {1993 ce = stages[i];1994break;1995}1996}1997}19981999/*2000 * Deleted in both.2001 * Deleted in one and unchanged in the other.2002 */2003if((head_deleted && remote_deleted) ||2004(head_deleted && remote && remote_match) ||2005(remote_deleted && head && head_match)) {2006if(index)2007returndeleted_entry(index, index, o);2008if(ce && !head_deleted) {2009if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2010return-1;2011}2012return0;2013}2014/*2015 * Added in both, identically.2016 */2017if(no_anc_exists && head && remote &&same(head, remote))2018returnmerged_entry(head, index, o);20192020}20212022/* Below are "no merge" cases, which require that the index be2023 * up-to-date to avoid the files getting overwritten with2024 * conflict resolution files.2025 */2026if(index) {2027if(verify_uptodate(index, o))2028return-1;2029}20302031 o->nontrivial_merge =1;20322033/* #2, #3, #4, #6, #7, #9, #10, #11. */2034 count =0;2035if(!head_match || !remote_match) {2036for(i =1; i < o->head_idx; i++) {2037if(stages[i] && stages[i] != o->df_conflict_entry) {2038keep_entry(stages[i], o);2039 count++;2040break;2041}2042}2043}2044#if DBRT_DEBUG2045else{2046fprintf(stderr,"read-tree: warning #16 detected\n");2047show_stage_entry(stderr,"head ", stages[head_match]);2048show_stage_entry(stderr,"remote ", stages[remote_match]);2049}2050#endif2051if(head) { count +=keep_entry(head, o); }2052if(remote) { count +=keep_entry(remote, o); }2053return count;2054}20552056/*2057 * Two-way merge.2058 *2059 * The rule is to "carry forward" what is in the index without losing2060 * information across a "fast-forward", favoring a successful merge2061 * over a merge failure when it makes sense. For details of the2062 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2063 *2064 */2065inttwoway_merge(const struct cache_entry *const*src,2066struct unpack_trees_options *o)2067{2068const struct cache_entry *current = src[0];2069const struct cache_entry *oldtree = src[1];2070const struct cache_entry *newtree = src[2];20712072if(o->merge_size !=2)2073returnerror("Cannot do a twoway merge of%dtrees",2074 o->merge_size);20752076if(oldtree == o->df_conflict_entry)2077 oldtree = NULL;2078if(newtree == o->df_conflict_entry)2079 newtree = NULL;20802081if(current) {2082if(current->ce_flags & CE_CONFLICTED) {2083if(same(oldtree, newtree) || o->reset) {2084if(!newtree)2085returndeleted_entry(current, current, o);2086else2087returnmerged_entry(newtree, current, o);2088}2089returnreject_merge(current, o);2090}else if((!oldtree && !newtree) ||/* 4 and 5 */2091(!oldtree && newtree &&2092same(current, newtree)) ||/* 6 and 7 */2093(oldtree && newtree &&2094same(oldtree, newtree)) ||/* 14 and 15 */2095(oldtree && newtree &&2096!same(oldtree, newtree) &&/* 18 and 19 */2097same(current, newtree))) {2098returnkeep_entry(current, o);2099}else if(oldtree && !newtree &&same(current, oldtree)) {2100/* 10 or 11 */2101returndeleted_entry(oldtree, current, o);2102}else if(oldtree && newtree &&2103same(current, oldtree) && !same(current, newtree)) {2104/* 20 or 21 */2105returnmerged_entry(newtree, current, o);2106}else2107returnreject_merge(current, o);2108}2109else if(newtree) {2110if(oldtree && !o->initial_checkout) {2111/*2112 * deletion of the path was staged;2113 */2114if(same(oldtree, newtree))2115return1;2116returnreject_merge(oldtree, o);2117}2118returnmerged_entry(newtree, current, o);2119}2120returndeleted_entry(oldtree, current, o);2121}21222123/*2124 * Bind merge.2125 *2126 * Keep the index entries at stage0, collapse stage1 but make sure2127 * stage0 does not have anything there.2128 */2129intbind_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 bind merge of%dtrees",2137 o->merge_size);2138if(a && old)2139return o->gently ? -1:2140error(ERRORMSG(o, ERROR_BIND_OVERLAP),2141super_prefixed(a->name),2142super_prefixed(old->name));2143if(!a)2144returnkeep_entry(old, o);2145else2146returnmerged_entry(a, NULL, o);2147}21482149/*2150 * One-way merge.2151 *2152 * The rule is:2153 * - take the stat information from stage0, take the data from stage12154 */2155intoneway_merge(const struct cache_entry *const*src,2156struct unpack_trees_options *o)2157{2158const struct cache_entry *old = src[0];2159const struct cache_entry *a = src[1];21602161if(o->merge_size !=1)2162returnerror("Cannot do a oneway merge of%dtrees",2163 o->merge_size);21642165if(!a || a == o->df_conflict_entry)2166returndeleted_entry(old, old, o);21672168if(old &&same(old, a)) {2169int update =0;2170if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2171struct stat st;2172if(lstat(old->name, &st) ||2173ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2174 update |= CE_UPDATE;2175}2176if(o->update &&S_ISGITLINK(old->ce_mode) &&2177should_update_submodules() && !verify_uptodate(old, o))2178 update |= CE_UPDATE;2179add_entry(o, old, update,0);2180return0;2181}2182returnmerged_entry(a, old, o);2183}