1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"argv-array.h" 4#include"repository.h" 5#include"config.h" 6#include"dir.h" 7#include"tree.h" 8#include"tree-walk.h" 9#include"cache-tree.h" 10#include"unpack-trees.h" 11#include"progress.h" 12#include"refs.h" 13#include"attr.h" 14#include"split-index.h" 15#include"dir.h" 16#include"submodule.h" 17#include"submodule-config.h" 18#include"fsmonitor.h" 19#include"object-store.h" 20#include"fetch-object.h" 21 22/* 23 * Error messages expected by scripts out of plumbing commands such as 24 * read-tree. Non-scripted Porcelain is not required to use these messages 25 * and in fact are encouraged to reword them to better suit their particular 26 * situation better. See how "git checkout" and "git merge" replaces 27 * them using setup_unpack_trees_porcelain(), for example. 28 */ 29static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 30/* ERROR_WOULD_OVERWRITE */ 31"Entry '%s' would be overwritten by merge. Cannot merge.", 32 33/* ERROR_NOT_UPTODATE_FILE */ 34"Entry '%s' not uptodate. Cannot merge.", 35 36/* ERROR_NOT_UPTODATE_DIR */ 37"Updating '%s' would lose untracked files in it", 38 39/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 40"Untracked working tree file '%s' would be overwritten by merge.", 41 42/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 43"Untracked working tree file '%s' would be removed by merge.", 44 45/* ERROR_BIND_OVERLAP */ 46"Entry '%s' overlaps with '%s'. Cannot bind.", 47 48/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 49"Entry '%s' not uptodate. Cannot update sparse checkout.", 50 51/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 52"Working tree file '%s' would be overwritten by sparse checkout update.", 53 54/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 55"Working tree file '%s' would be removed by sparse checkout update.", 56 57/* ERROR_WOULD_LOSE_SUBMODULE */ 58"Submodule '%s' cannot checkout new HEAD.", 59}; 60 61#define ERRORMSG(o,type) \ 62 ( ((o) && (o)->msgs[(type)]) \ 63 ? ((o)->msgs[(type)]) \ 64 : (unpack_plumbing_errors[(type)]) ) 65 66static const char*super_prefixed(const char*path) 67{ 68/* 69 * It is necessary and sufficient to have two static buffers 70 * here, as the return value of this function is fed to 71 * error() using the unpack_*_errors[] templates we see above. 72 */ 73static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 74static int super_prefix_len = -1; 75static unsigned idx =ARRAY_SIZE(buf) -1; 76 77if(super_prefix_len <0) { 78const char*super_prefix =get_super_prefix(); 79if(!super_prefix) { 80 super_prefix_len =0; 81}else{ 82int i; 83for(i =0; i <ARRAY_SIZE(buf); i++) 84strbuf_addstr(&buf[i], super_prefix); 85 super_prefix_len = buf[0].len; 86} 87} 88 89if(!super_prefix_len) 90return path; 91 92if(++idx >=ARRAY_SIZE(buf)) 93 idx =0; 94 95strbuf_setlen(&buf[idx], super_prefix_len); 96strbuf_addstr(&buf[idx], path); 97 98return buf[idx].buf; 99} 100 101voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 102const char*cmd) 103{ 104int i; 105const char**msgs = opts->msgs; 106const char*msg; 107 108argv_array_init(&opts->msgs_to_free); 109 110if(!strcmp(cmd,"checkout")) 111 msg = advice_commit_before_merge 112?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 113"Please commit your changes or stash them before you switch branches.") 114:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 115else if(!strcmp(cmd,"merge")) 116 msg = advice_commit_before_merge 117?_("Your local changes to the following files would be overwritten by merge:\n%%s" 118"Please commit your changes or stash them before you merge.") 119:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 120else 121 msg = advice_commit_before_merge 122?_("Your local changes to the following files would be overwritten by%s:\n%%s" 123"Please commit your changes or stash them before you%s.") 124:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 125 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 126argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 127 128 msgs[ERROR_NOT_UPTODATE_DIR] = 129_("Updating the following directories would lose untracked files in them:\n%s"); 130 131if(!strcmp(cmd,"checkout")) 132 msg = advice_commit_before_merge 133?_("The following untracked working tree files would be removed by checkout:\n%%s" 134"Please move or remove them before you switch branches.") 135:_("The following untracked working tree files would be removed by checkout:\n%%s"); 136else if(!strcmp(cmd,"merge")) 137 msg = advice_commit_before_merge 138?_("The following untracked working tree files would be removed by merge:\n%%s" 139"Please move or remove them before you merge.") 140:_("The following untracked working tree files would be removed by merge:\n%%s"); 141else 142 msg = advice_commit_before_merge 143?_("The following untracked working tree files would be removed by%s:\n%%s" 144"Please move or remove them before you%s.") 145:_("The following untracked working tree files would be removed by%s:\n%%s"); 146 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = 147argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 148 149if(!strcmp(cmd,"checkout")) 150 msg = advice_commit_before_merge 151?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 152"Please move or remove them before you switch branches.") 153:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 154else if(!strcmp(cmd,"merge")) 155 msg = advice_commit_before_merge 156?_("The following untracked working tree files would be overwritten by merge:\n%%s" 157"Please move or remove them before you merge.") 158:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 159else 160 msg = advice_commit_before_merge 161?_("The following untracked working tree files would be overwritten by%s:\n%%s" 162"Please move or remove them before you%s.") 163:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 164 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = 165argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 166 167/* 168 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 169 * cannot easily display it as a list. 170 */ 171 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 172 173 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 174_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 175 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 176_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 177 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 178_("The following working tree files would be removed by sparse checkout update:\n%s"); 179 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 180_("Cannot update submodule:\n%s"); 181 182 opts->show_all_errors =1; 183/* rejected paths may not have a static buffer */ 184for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 185 opts->unpack_rejects[i].strdup_strings =1; 186} 187 188voidclear_unpack_trees_porcelain(struct unpack_trees_options *opts) 189{ 190argv_array_clear(&opts->msgs_to_free); 191memset(opts->msgs,0,sizeof(opts->msgs)); 192} 193 194static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 195unsigned int set,unsigned int clear) 196{ 197 clear |= CE_HASHED; 198 199if(set & CE_REMOVE) 200 set |= CE_WT_REMOVE; 201 202 ce->ce_flags = (ce->ce_flags & ~clear) | set; 203returnadd_index_entry(&o->result, ce, 204 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 205} 206 207static struct cache_entry *dup_entry(const struct cache_entry *ce) 208{ 209unsigned int size =ce_size(ce); 210struct cache_entry *new_entry =xmalloc(size); 211 212memcpy(new_entry, ce, size); 213return new_entry; 214} 215 216static voidadd_entry(struct unpack_trees_options *o, 217const struct cache_entry *ce, 218unsigned int set,unsigned int clear) 219{ 220do_add_entry(o,dup_entry(ce), set, clear); 221} 222 223/* 224 * add error messages on path <path> 225 * corresponding to the type <e> with the message <msg> 226 * indicating if it should be display in porcelain or not 227 */ 228static intadd_rejected_path(struct unpack_trees_options *o, 229enum unpack_trees_error_types e, 230const char*path) 231{ 232if(!o->show_all_errors) 233returnerror(ERRORMSG(o, e),super_prefixed(path)); 234 235/* 236 * Otherwise, insert in a list for future display by 237 * display_error_msgs() 238 */ 239string_list_append(&o->unpack_rejects[e], path); 240return-1; 241} 242 243/* 244 * display all the error messages stored in a nice way 245 */ 246static voiddisplay_error_msgs(struct unpack_trees_options *o) 247{ 248int e, i; 249int something_displayed =0; 250for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 251struct string_list *rejects = &o->unpack_rejects[e]; 252if(rejects->nr >0) { 253struct strbuf path = STRBUF_INIT; 254 something_displayed =1; 255for(i =0; i < rejects->nr; i++) 256strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 257error(ERRORMSG(o, e),super_prefixed(path.buf)); 258strbuf_release(&path); 259} 260string_list_clear(rejects,0); 261} 262if(something_displayed) 263fprintf(stderr,_("Aborting\n")); 264} 265 266static intcheck_submodule_move_head(const struct cache_entry *ce, 267const char*old_id, 268const char*new_id, 269struct unpack_trees_options *o) 270{ 271unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 272const struct submodule *sub =submodule_from_ce(ce); 273 274if(!sub) 275return0; 276 277if(o->reset) 278 flags |= SUBMODULE_MOVE_HEAD_FORCE; 279 280if(submodule_move_head(ce->name, old_id, new_id, flags)) 281return o->gently ? -1: 282add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 283return0; 284} 285 286/* 287 * Preform the loading of the repository's gitmodules file. This function is 288 * used by 'check_update()' to perform loading of the gitmodules file in two 289 * differnt situations: 290 * (1) before removing entries from the working tree if the gitmodules file has 291 * been marked for removal. This situation is specified by 'state' == NULL. 292 * (2) before checking out entries to the working tree if the gitmodules file 293 * has been marked for update. This situation is specified by 'state' != NULL. 294 */ 295static voidload_gitmodules_file(struct index_state *index, 296struct checkout *state) 297{ 298int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 299 300if(pos >=0) { 301struct cache_entry *ce = index->cache[pos]; 302if(!state && ce->ce_flags & CE_WT_REMOVE) { 303repo_read_gitmodules(the_repository); 304}else if(state && (ce->ce_flags & CE_UPDATE)) { 305submodule_free(the_repository); 306checkout_entry(ce, state, NULL); 307repo_read_gitmodules(the_repository); 308} 309} 310} 311 312/* 313 * Unlink the last component and schedule the leading directories for 314 * removal, such that empty directories get removed. 315 */ 316static voidunlink_entry(const struct cache_entry *ce) 317{ 318const struct submodule *sub =submodule_from_ce(ce); 319if(sub) { 320/* state.force is set at the caller. */ 321submodule_move_head(ce->name,"HEAD", NULL, 322 SUBMODULE_MOVE_HEAD_FORCE); 323} 324if(!check_leading_path(ce->name,ce_namelen(ce))) 325return; 326if(remove_or_warn(ce->ce_mode, ce->name)) 327return; 328schedule_dir_for_removal(ce->name,ce_namelen(ce)); 329} 330 331static struct progress *get_progress(struct unpack_trees_options *o) 332{ 333unsigned cnt =0, total =0; 334struct index_state *index = &o->result; 335 336if(!o->update || !o->verbose_update) 337return NULL; 338 339for(; cnt < index->cache_nr; cnt++) { 340const struct cache_entry *ce = index->cache[cnt]; 341if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 342 total++; 343} 344 345returnstart_delayed_progress(_("Checking out files"), total); 346} 347 348static intcheck_updates(struct unpack_trees_options *o) 349{ 350unsigned cnt =0; 351int errs =0; 352struct progress *progress = NULL; 353struct index_state *index = &o->result; 354struct checkout state = CHECKOUT_INIT; 355int i; 356 357 state.force =1; 358 state.quiet =1; 359 state.refresh_cache =1; 360 state.istate = index; 361 362 progress =get_progress(o); 363 364if(o->update) 365git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 366 367if(should_update_submodules() && o->update && !o->dry_run) 368load_gitmodules_file(index, NULL); 369 370for(i =0; i < index->cache_nr; i++) { 371const struct cache_entry *ce = index->cache[i]; 372 373if(ce->ce_flags & CE_WT_REMOVE) { 374display_progress(progress, ++cnt); 375if(o->update && !o->dry_run) 376unlink_entry(ce); 377} 378} 379remove_marked_cache_entries(index); 380remove_scheduled_dirs(); 381 382if(should_update_submodules() && o->update && !o->dry_run) 383load_gitmodules_file(index, &state); 384 385enable_delayed_checkout(&state); 386if(repository_format_partial_clone && o->update && !o->dry_run) { 387/* 388 * Prefetch the objects that are to be checked out in the loop 389 * below. 390 */ 391struct oid_array to_fetch = OID_ARRAY_INIT; 392int fetch_if_missing_store = fetch_if_missing; 393 fetch_if_missing =0; 394for(i =0; i < index->cache_nr; i++) { 395struct cache_entry *ce = index->cache[i]; 396if((ce->ce_flags & CE_UPDATE) && 397!S_ISGITLINK(ce->ce_mode)) { 398if(!has_object_file(&ce->oid)) 399oid_array_append(&to_fetch, &ce->oid); 400} 401} 402if(to_fetch.nr) 403fetch_objects(repository_format_partial_clone, 404&to_fetch); 405 fetch_if_missing = fetch_if_missing_store; 406oid_array_clear(&to_fetch); 407} 408for(i =0; i < index->cache_nr; i++) { 409struct cache_entry *ce = index->cache[i]; 410 411if(ce->ce_flags & CE_UPDATE) { 412if(ce->ce_flags & CE_WT_REMOVE) 413BUG("both update and delete flags are set on%s", 414 ce->name); 415display_progress(progress, ++cnt); 416 ce->ce_flags &= ~CE_UPDATE; 417if(o->update && !o->dry_run) { 418 errs |=checkout_entry(ce, &state, NULL); 419} 420} 421} 422stop_progress(&progress); 423 errs |=finish_delayed_checkout(&state); 424if(o->update) 425git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 426return errs !=0; 427} 428 429static intverify_uptodate_sparse(const struct cache_entry *ce, 430struct unpack_trees_options *o); 431static intverify_absent_sparse(const struct cache_entry *ce, 432enum unpack_trees_error_types, 433struct unpack_trees_options *o); 434 435static intapply_sparse_checkout(struct index_state *istate, 436struct cache_entry *ce, 437struct unpack_trees_options *o) 438{ 439int was_skip_worktree =ce_skip_worktree(ce); 440 441if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 442 ce->ce_flags |= CE_SKIP_WORKTREE; 443else 444 ce->ce_flags &= ~CE_SKIP_WORKTREE; 445if(was_skip_worktree !=ce_skip_worktree(ce)) { 446 ce->ce_flags |= CE_UPDATE_IN_BASE; 447mark_fsmonitor_invalid(istate, ce); 448 istate->cache_changed |= CE_ENTRY_CHANGED; 449} 450 451/* 452 * if (!was_skip_worktree && !ce_skip_worktree()) { 453 * This is perfectly normal. Move on; 454 * } 455 */ 456 457/* 458 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 459 * area as a result of ce_skip_worktree() shortcuts in 460 * verify_absent() and verify_uptodate(). 461 * Make sure they don't modify worktree if they are already 462 * outside checkout area 463 */ 464if(was_skip_worktree &&ce_skip_worktree(ce)) { 465 ce->ce_flags &= ~CE_UPDATE; 466 467/* 468 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 469 * on to get that file removed from both index and worktree. 470 * If that file is already outside worktree area, don't 471 * bother remove it. 472 */ 473if(ce->ce_flags & CE_REMOVE) 474 ce->ce_flags &= ~CE_WT_REMOVE; 475} 476 477if(!was_skip_worktree &&ce_skip_worktree(ce)) { 478/* 479 * If CE_UPDATE is set, verify_uptodate() must be called already 480 * also stat info may have lost after merged_entry() so calling 481 * verify_uptodate() again may fail 482 */ 483if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 484return-1; 485 ce->ce_flags |= CE_WT_REMOVE; 486 ce->ce_flags &= ~CE_UPDATE; 487} 488if(was_skip_worktree && !ce_skip_worktree(ce)) { 489if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 490return-1; 491 ce->ce_flags |= CE_UPDATE; 492} 493return0; 494} 495 496staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 497struct unpack_trees_options *o) 498{ 499int ret = o->fn(src, o); 500if(ret >0) 501 ret =0; 502return ret; 503} 504 505static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 506{ 507 ce->ce_flags |= CE_UNPACKED; 508 509if(o->cache_bottom < o->src_index->cache_nr && 510 o->src_index->cache[o->cache_bottom] == ce) { 511int bottom = o->cache_bottom; 512while(bottom < o->src_index->cache_nr && 513 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 514 bottom++; 515 o->cache_bottom = bottom; 516} 517} 518 519static voidmark_all_ce_unused(struct index_state *index) 520{ 521int i; 522for(i =0; i < index->cache_nr; i++) 523 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 524} 525 526static intlocate_in_src_index(const struct cache_entry *ce, 527struct unpack_trees_options *o) 528{ 529struct index_state *index = o->src_index; 530int len =ce_namelen(ce); 531int pos =index_name_pos(index, ce->name, len); 532if(pos <0) 533 pos = -1- pos; 534return pos; 535} 536 537/* 538 * We call unpack_index_entry() with an unmerged cache entry 539 * only in diff-index, and it wants a single callback. Skip 540 * the other unmerged entry with the same name. 541 */ 542static voidmark_ce_used_same_name(struct cache_entry *ce, 543struct unpack_trees_options *o) 544{ 545struct index_state *index = o->src_index; 546int len =ce_namelen(ce); 547int pos; 548 549for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 550struct cache_entry *next = index->cache[pos]; 551if(len !=ce_namelen(next) || 552memcmp(ce->name, next->name, len)) 553break; 554mark_ce_used(next, o); 555} 556} 557 558static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 559{ 560const struct index_state *index = o->src_index; 561int pos = o->cache_bottom; 562 563while(pos < index->cache_nr) { 564struct cache_entry *ce = index->cache[pos]; 565if(!(ce->ce_flags & CE_UNPACKED)) 566return ce; 567 pos++; 568} 569return NULL; 570} 571 572static voidadd_same_unmerged(const struct cache_entry *ce, 573struct unpack_trees_options *o) 574{ 575struct index_state *index = o->src_index; 576int len =ce_namelen(ce); 577int pos =index_name_pos(index, ce->name, len); 578 579if(0<= pos) 580die("programming error in a caller of mark_ce_used_same_name"); 581for(pos = -pos -1; pos < index->cache_nr; pos++) { 582struct cache_entry *next = index->cache[pos]; 583if(len !=ce_namelen(next) || 584memcmp(ce->name, next->name, len)) 585break; 586add_entry(o, next,0,0); 587mark_ce_used(next, o); 588} 589} 590 591static intunpack_index_entry(struct cache_entry *ce, 592struct unpack_trees_options *o) 593{ 594const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 595int ret; 596 597 src[0] = ce; 598 599mark_ce_used(ce, o); 600if(ce_stage(ce)) { 601if(o->skip_unmerged) { 602add_entry(o, ce,0,0); 603return0; 604} 605} 606 ret =call_unpack_fn(src, o); 607if(ce_stage(ce)) 608mark_ce_used_same_name(ce, o); 609return ret; 610} 611 612static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 613 614static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 615{ 616struct unpack_trees_options *o = info->data; 617 618if(o->diff_index_cached) 619return; 620 o->cache_bottom = bottom; 621} 622 623static intswitch_cache_bottom(struct traverse_info *info) 624{ 625struct unpack_trees_options *o = info->data; 626int ret, pos; 627 628if(o->diff_index_cached) 629return0; 630 ret = o->cache_bottom; 631 pos =find_cache_pos(info->prev, &info->name); 632 633if(pos < -1) 634 o->cache_bottom = -2- pos; 635else if(pos <0) 636 o->cache_bottom = o->src_index->cache_nr; 637return ret; 638} 639 640staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 641{ 642return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 643} 644 645static inttraverse_trees_recursive(int n,unsigned long dirmask, 646unsigned long df_conflicts, 647struct name_entry *names, 648struct traverse_info *info) 649{ 650int i, ret, bottom; 651int nr_buf =0; 652struct tree_desc t[MAX_UNPACK_TREES]; 653void*buf[MAX_UNPACK_TREES]; 654struct traverse_info newinfo; 655struct name_entry *p; 656 657 p = names; 658while(!p->mode) 659 p++; 660 661 newinfo = *info; 662 newinfo.prev = info; 663 newinfo.pathspec = info->pathspec; 664 newinfo.name = *p; 665 newinfo.pathlen +=tree_entry_len(p) +1; 666 newinfo.df_conflicts |= df_conflicts; 667 668/* 669 * Fetch the tree from the ODB for each peer directory in the 670 * n commits. 671 * 672 * For 2- and 3-way traversals, we try to avoid hitting the 673 * ODB twice for the same OID. This should yield a nice speed 674 * up in checkouts and merges when the commits are similar. 675 * 676 * We don't bother doing the full O(n^2) search for larger n, 677 * because wider traversals don't happen that often and we 678 * avoid the search setup. 679 * 680 * When 2 peer OIDs are the same, we just copy the tree 681 * descriptor data. This implicitly borrows the buffer 682 * data from the earlier cell. 683 */ 684for(i =0; i < n; i++, dirmask >>=1) { 685if(i >0&&are_same_oid(&names[i], &names[i -1])) 686 t[i] = t[i -1]; 687else if(i >1&&are_same_oid(&names[i], &names[i -2])) 688 t[i] = t[i -2]; 689else{ 690const struct object_id *oid = NULL; 691if(dirmask &1) 692 oid = names[i].oid; 693 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 694} 695} 696 697 bottom =switch_cache_bottom(&newinfo); 698 ret =traverse_trees(n, t, &newinfo); 699restore_cache_bottom(&newinfo, bottom); 700 701for(i =0; i < nr_buf; i++) 702free(buf[i]); 703 704return ret; 705} 706 707/* 708 * Compare the traverse-path to the cache entry without actually 709 * having to generate the textual representation of the traverse 710 * path. 711 * 712 * NOTE! This *only* compares up to the size of the traverse path 713 * itself - the caller needs to do the final check for the cache 714 * entry having more data at the end! 715 */ 716static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 717{ 718int len, pathlen, ce_len; 719const char*ce_name; 720 721if(info->prev) { 722int cmp =do_compare_entry_piecewise(ce, info->prev, 723&info->name); 724if(cmp) 725return cmp; 726} 727 pathlen = info->pathlen; 728 ce_len =ce_namelen(ce); 729 730/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 731if(ce_len < pathlen) 732return-1; 733 734 ce_len -= pathlen; 735 ce_name = ce->name + pathlen; 736 737 len =tree_entry_len(n); 738returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 739} 740 741static intdo_compare_entry(const struct cache_entry *ce, 742const struct traverse_info *info, 743const struct name_entry *n) 744{ 745int len, pathlen, ce_len; 746const char*ce_name; 747int cmp; 748 749/* 750 * If we have not precomputed the traverse path, it is quicker 751 * to avoid doing so. But if we have precomputed it, 752 * it is quicker to use the precomputed version. 753 */ 754if(!info->traverse_path) 755returndo_compare_entry_piecewise(ce, info, n); 756 757 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 758if(cmp) 759return cmp; 760 761 pathlen = info->pathlen; 762 ce_len =ce_namelen(ce); 763 764if(ce_len < pathlen) 765return-1; 766 767 ce_len -= pathlen; 768 ce_name = ce->name + pathlen; 769 770 len =tree_entry_len(n); 771returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 772} 773 774static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 775{ 776int cmp =do_compare_entry(ce, info, n); 777if(cmp) 778return cmp; 779 780/* 781 * Even if the beginning compared identically, the ce should 782 * compare as bigger than a directory leading up to it! 783 */ 784returnce_namelen(ce) >traverse_path_len(info, n); 785} 786 787static intce_in_traverse_path(const struct cache_entry *ce, 788const struct traverse_info *info) 789{ 790if(!info->prev) 791return1; 792if(do_compare_entry(ce, info->prev, &info->name)) 793return0; 794/* 795 * If ce (blob) is the same name as the path (which is a tree 796 * we will be descending into), it won't be inside it. 797 */ 798return(info->pathlen <ce_namelen(ce)); 799} 800 801static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 802{ 803int len =traverse_path_len(info, n); 804struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 805 806 ce->ce_mode =create_ce_mode(n->mode); 807 ce->ce_flags =create_ce_flags(stage); 808 ce->ce_namelen = len; 809oidcpy(&ce->oid, n->oid); 810make_traverse_path(ce->name, info, n); 811 812return ce; 813} 814 815static intunpack_nondirectories(int n,unsigned long mask, 816unsigned long dirmask, 817struct cache_entry **src, 818const struct name_entry *names, 819const struct traverse_info *info) 820{ 821int i; 822struct unpack_trees_options *o = info->data; 823unsigned long conflicts = info->df_conflicts | dirmask; 824 825/* Do we have *only* directories? Nothing to do */ 826if(mask == dirmask && !src[0]) 827return0; 828 829/* 830 * Ok, we've filled in up to any potential index entry in src[0], 831 * now do the rest. 832 */ 833for(i =0; i < n; i++) { 834int stage; 835unsigned int bit =1ul<< i; 836if(conflicts & bit) { 837 src[i + o->merge] = o->df_conflict_entry; 838continue; 839} 840if(!(mask & bit)) 841continue; 842if(!o->merge) 843 stage =0; 844else if(i +1< o->head_idx) 845 stage =1; 846else if(i +1> o->head_idx) 847 stage =3; 848else 849 stage =2; 850 src[i + o->merge] =create_ce_entry(info, names + i, stage); 851} 852 853if(o->merge) { 854int rc =call_unpack_fn((const struct cache_entry *const*)src, 855 o); 856for(i =0; i < n; i++) { 857struct cache_entry *ce = src[i + o->merge]; 858if(ce != o->df_conflict_entry) 859free(ce); 860} 861return rc; 862} 863 864for(i =0; i < n; i++) 865if(src[i] && src[i] != o->df_conflict_entry) 866if(do_add_entry(o, src[i],0,0)) 867return-1; 868 869return0; 870} 871 872static intunpack_failed(struct unpack_trees_options *o,const char*message) 873{ 874discard_index(&o->result); 875if(!o->gently && !o->exiting_early) { 876if(message) 877returnerror("%s", message); 878return-1; 879} 880return-1; 881} 882 883/* 884 * The tree traversal is looking at name p. If we have a matching entry, 885 * return it. If name p is a directory in the index, do not return 886 * anything, as we will want to match it when the traversal descends into 887 * the directory. 888 */ 889static intfind_cache_pos(struct traverse_info *info, 890const struct name_entry *p) 891{ 892int pos; 893struct unpack_trees_options *o = info->data; 894struct index_state *index = o->src_index; 895int pfxlen = info->pathlen; 896int p_len =tree_entry_len(p); 897 898for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 899const struct cache_entry *ce = index->cache[pos]; 900const char*ce_name, *ce_slash; 901int cmp, ce_len; 902 903if(ce->ce_flags & CE_UNPACKED) { 904/* 905 * cache_bottom entry is already unpacked, so 906 * we can never match it; don't check it 907 * again. 908 */ 909if(pos == o->cache_bottom) 910++o->cache_bottom; 911continue; 912} 913if(!ce_in_traverse_path(ce, info)) { 914/* 915 * Check if we can skip future cache checks 916 * (because we're already past all possible 917 * entries in the traverse path). 918 */ 919if(info->traverse_path) { 920if(strncmp(ce->name, info->traverse_path, 921 info->pathlen) >0) 922break; 923} 924continue; 925} 926 ce_name = ce->name + pfxlen; 927 ce_slash =strchr(ce_name,'/'); 928if(ce_slash) 929 ce_len = ce_slash - ce_name; 930else 931 ce_len =ce_namelen(ce) - pfxlen; 932 cmp =name_compare(p->path, p_len, ce_name, ce_len); 933/* 934 * Exact match; if we have a directory we need to 935 * delay returning it. 936 */ 937if(!cmp) 938return ce_slash ? -2- pos : pos; 939if(0< cmp) 940continue;/* keep looking */ 941/* 942 * ce_name sorts after p->path; could it be that we 943 * have files under p->path directory in the index? 944 * E.g. ce_name == "t-i", and p->path == "t"; we may 945 * have "t/a" in the index. 946 */ 947if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 948 ce_name[p_len] <'/') 949continue;/* keep looking */ 950break; 951} 952return-1; 953} 954 955static struct cache_entry *find_cache_entry(struct traverse_info *info, 956const struct name_entry *p) 957{ 958int pos =find_cache_pos(info, p); 959struct unpack_trees_options *o = info->data; 960 961if(0<= pos) 962return o->src_index->cache[pos]; 963else 964return NULL; 965} 966 967static voiddebug_path(struct traverse_info *info) 968{ 969if(info->prev) { 970debug_path(info->prev); 971if(*info->prev->name.path) 972putchar('/'); 973} 974printf("%s", info->name.path); 975} 976 977static voiddebug_name_entry(int i,struct name_entry *n) 978{ 979printf("ent#%d %06o%s\n", i, 980 n->path ? n->mode :0, 981 n->path ? n->path :"(missing)"); 982} 983 984static voiddebug_unpack_callback(int n, 985unsigned long mask, 986unsigned long dirmask, 987struct name_entry *names, 988struct traverse_info *info) 989{ 990int i; 991printf("* unpack mask%lu, dirmask%lu, cnt%d", 992 mask, dirmask, n); 993debug_path(info); 994putchar('\n'); 995for(i =0; i < n; i++) 996debug_name_entry(i, names + i); 997} 998 999static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info)1000{1001struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1002struct unpack_trees_options *o = info->data;1003const struct name_entry *p = names;10041005/* Find first entry with a real name (we could use "mask" too) */1006while(!p->mode)1007 p++;10081009if(o->debug_unpack)1010debug_unpack_callback(n, mask, dirmask, names, info);10111012/* Are we supposed to look at the index too? */1013if(o->merge) {1014while(1) {1015int cmp;1016struct cache_entry *ce;10171018if(o->diff_index_cached)1019 ce =next_cache_entry(o);1020else1021 ce =find_cache_entry(info, p);10221023if(!ce)1024break;1025 cmp =compare_entry(ce, info, p);1026if(cmp <0) {1027if(unpack_index_entry(ce, o) <0)1028returnunpack_failed(o, NULL);1029continue;1030}1031if(!cmp) {1032if(ce_stage(ce)) {1033/*1034 * If we skip unmerged index1035 * entries, we'll skip this1036 * entry *and* the tree1037 * entries associated with it!1038 */1039if(o->skip_unmerged) {1040add_same_unmerged(ce, o);1041return mask;1042}1043}1044 src[0] = ce;1045}1046break;1047}1048}10491050if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1051return-1;10521053if(o->merge && src[0]) {1054if(ce_stage(src[0]))1055mark_ce_used_same_name(src[0], o);1056else1057mark_ce_used(src[0], o);1058}10591060/* Now handle any directories.. */1061if(dirmask) {1062/* special case: "diff-index --cached" looking at a tree */1063if(o->diff_index_cached &&1064 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1065int matches;1066 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1067 names, info);1068/*1069 * Everything under the name matches; skip the1070 * entire hierarchy. diff_index_cached codepath1071 * special cases D/F conflicts in such a way that1072 * it does not do any look-ahead, so this is safe.1073 */1074if(matches) {1075 o->cache_bottom += matches;1076return mask;1077}1078}10791080if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1081 names, info) <0)1082return-1;1083return mask;1084}10851086return mask;1087}10881089static intclear_ce_flags_1(struct cache_entry **cache,int nr,1090struct strbuf *prefix,1091int select_mask,int clear_mask,1092struct exclude_list *el,int defval);10931094/* Whole directory matching */1095static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1096struct strbuf *prefix,1097char*basename,1098int select_mask,int clear_mask,1099struct exclude_list *el,int defval)1100{1101struct cache_entry **cache_end;1102int dtype = DT_DIR;1103int ret =is_excluded_from_list(prefix->buf, prefix->len,1104 basename, &dtype, el, &the_index);1105int rc;11061107strbuf_addch(prefix,'/');11081109/* If undecided, use matching result of parent dir in defval */1110if(ret <0)1111 ret = defval;11121113for(cache_end = cache; cache_end != cache + nr; cache_end++) {1114struct cache_entry *ce = *cache_end;1115if(strncmp(ce->name, prefix->buf, prefix->len))1116break;1117}11181119/*1120 * TODO: check el, if there are no patterns that may conflict1121 * with ret (iow, we know in advance the incl/excl1122 * decision for the entire directory), clear flag here without1123 * calling clear_ce_flags_1(). That function will call1124 * the expensive is_excluded_from_list() on every entry.1125 */1126 rc =clear_ce_flags_1(cache, cache_end - cache,1127 prefix,1128 select_mask, clear_mask,1129 el, ret);1130strbuf_setlen(prefix, prefix->len -1);1131return rc;1132}11331134/*1135 * Traverse the index, find every entry that matches according to1136 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1137 * number of traversed entries.1138 *1139 * If select_mask is non-zero, only entries whose ce_flags has on of1140 * those bits enabled are traversed.1141 *1142 * cache : pointer to an index entry1143 * prefix_len : an offset to its path1144 *1145 * The current path ("prefix") including the trailing '/' is1146 * cache[0]->name[0..(prefix_len-1)]1147 * Top level path has prefix_len zero.1148 */1149static intclear_ce_flags_1(struct cache_entry **cache,int nr,1150struct strbuf *prefix,1151int select_mask,int clear_mask,1152struct exclude_list *el,int defval)1153{1154struct cache_entry **cache_end = cache + nr;11551156/*1157 * Process all entries that have the given prefix and meet1158 * select_mask condition1159 */1160while(cache != cache_end) {1161struct cache_entry *ce = *cache;1162const char*name, *slash;1163int len, dtype, ret;11641165if(select_mask && !(ce->ce_flags & select_mask)) {1166 cache++;1167continue;1168}11691170if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1171break;11721173 name = ce->name + prefix->len;1174 slash =strchr(name,'/');11751176/* If it's a directory, try whole directory match first */1177if(slash) {1178int processed;11791180 len = slash - name;1181strbuf_add(prefix, name, len);11821183 processed =clear_ce_flags_dir(cache, cache_end - cache,1184 prefix,1185 prefix->buf + prefix->len - len,1186 select_mask, clear_mask,1187 el, defval);11881189/* clear_c_f_dir eats a whole dir already? */1190if(processed) {1191 cache += processed;1192strbuf_setlen(prefix, prefix->len - len);1193continue;1194}11951196strbuf_addch(prefix,'/');1197 cache +=clear_ce_flags_1(cache, cache_end - cache,1198 prefix,1199 select_mask, clear_mask, el, defval);1200strbuf_setlen(prefix, prefix->len - len -1);1201continue;1202}12031204/* Non-directory */1205 dtype =ce_to_dtype(ce);1206 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1207 name, &dtype, el, &the_index);1208if(ret <0)1209 ret = defval;1210if(ret >0)1211 ce->ce_flags &= ~clear_mask;1212 cache++;1213}1214return nr - (cache_end - cache);1215}12161217static intclear_ce_flags(struct cache_entry **cache,int nr,1218int select_mask,int clear_mask,1219struct exclude_list *el)1220{1221static struct strbuf prefix = STRBUF_INIT;12221223strbuf_reset(&prefix);12241225returnclear_ce_flags_1(cache, nr,1226&prefix,1227 select_mask, clear_mask,1228 el,0);1229}12301231/*1232 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1233 */1234static voidmark_new_skip_worktree(struct exclude_list *el,1235struct index_state *the_index,1236int select_flag,int skip_wt_flag)1237{1238int i;12391240/*1241 * 1. Pretend the narrowest worktree: only unmerged entries1242 * are checked out1243 */1244for(i =0; i < the_index->cache_nr; i++) {1245struct cache_entry *ce = the_index->cache[i];12461247if(select_flag && !(ce->ce_flags & select_flag))1248continue;12491250if(!ce_stage(ce))1251 ce->ce_flags |= skip_wt_flag;1252else1253 ce->ce_flags &= ~skip_wt_flag;1254}12551256/*1257 * 2. Widen worktree according to sparse-checkout file.1258 * Matched entries will have skip_wt_flag cleared (i.e. "in")1259 */1260clear_ce_flags(the_index->cache, the_index->cache_nr,1261 select_flag, skip_wt_flag, el);1262}12631264static intverify_absent(const struct cache_entry *,1265enum unpack_trees_error_types,1266struct unpack_trees_options *);1267/*1268 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1269 * resulting index, -2 on failure to reflect the changes to the work tree.1270 *1271 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1272 */1273intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1274{1275int i, ret;1276static struct cache_entry *dfc;1277struct exclude_list el;12781279if(len > MAX_UNPACK_TREES)1280die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12811282memset(&el,0,sizeof(el));1283if(!core_apply_sparse_checkout || !o->update)1284 o->skip_sparse_checkout =1;1285if(!o->skip_sparse_checkout) {1286char*sparse =git_pathdup("info/sparse-checkout");1287if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1288 o->skip_sparse_checkout =1;1289else1290 o->el = ⪙1291free(sparse);1292}12931294memset(&o->result,0,sizeof(o->result));1295 o->result.initialized =1;1296 o->result.timestamp.sec = o->src_index->timestamp.sec;1297 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1298 o->result.version = o->src_index->version;1299if(!o->src_index->split_index) {1300 o->result.split_index = NULL;1301}else if(o->src_index == o->dst_index) {1302/*1303 * o->dst_index (and thus o->src_index) will be discarded1304 * and overwritten with o->result at the end of this function,1305 * so just use src_index's split_index to avoid having to1306 * create a new one.1307 */1308 o->result.split_index = o->src_index->split_index;1309 o->result.split_index->refcount++;1310}else{1311 o->result.split_index =init_split_index(&o->result);1312}1313oidcpy(&o->result.oid, &o->src_index->oid);1314 o->merge_size = len;1315mark_all_ce_unused(o->src_index);13161317/*1318 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1319 */1320if(!o->skip_sparse_checkout)1321mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);13221323if(!dfc)1324 dfc =xcalloc(1,cache_entry_size(0));1325 o->df_conflict_entry = dfc;13261327if(len) {1328const char*prefix = o->prefix ? o->prefix :"";1329struct traverse_info info;13301331setup_traverse_info(&info, prefix);1332 info.fn = unpack_callback;1333 info.data = o;1334 info.show_all_errors = o->show_all_errors;1335 info.pathspec = o->pathspec;13361337if(o->prefix) {1338/*1339 * Unpack existing index entries that sort before the1340 * prefix the tree is spliced into. Note that o->merge1341 * is always true in this case.1342 */1343while(1) {1344struct cache_entry *ce =next_cache_entry(o);1345if(!ce)1346break;1347if(ce_in_traverse_path(ce, &info))1348break;1349if(unpack_index_entry(ce, o) <0)1350goto return_failed;1351}1352}13531354if(traverse_trees(len, t, &info) <0)1355goto return_failed;1356}13571358/* Any left-over entries in the index? */1359if(o->merge) {1360while(1) {1361struct cache_entry *ce =next_cache_entry(o);1362if(!ce)1363break;1364if(unpack_index_entry(ce, o) <0)1365goto return_failed;1366}1367}1368mark_all_ce_unused(o->src_index);13691370if(o->trivial_merges_only && o->nontrivial_merge) {1371 ret =unpack_failed(o,"Merge requires file-level merging");1372goto done;1373}13741375if(!o->skip_sparse_checkout) {1376int empty_worktree =1;13771378/*1379 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11380 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1381 * so apply_sparse_checkout() won't attempt to remove it from worktree1382 */1383mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13841385 ret =0;1386for(i =0; i < o->result.cache_nr; i++) {1387struct cache_entry *ce = o->result.cache[i];13881389/*1390 * Entries marked with CE_ADDED in merged_entry() do not have1391 * verify_absent() check (the check is effectively disabled1392 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1393 *1394 * Do the real check now because we have had1395 * correct CE_NEW_SKIP_WORKTREE1396 */1397if(ce->ce_flags & CE_ADDED &&1398verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1399if(!o->show_all_errors)1400goto return_failed;1401 ret = -1;1402}14031404if(apply_sparse_checkout(&o->result, ce, o)) {1405if(!o->show_all_errors)1406goto return_failed;1407 ret = -1;1408}1409if(!ce_skip_worktree(ce))1410 empty_worktree =0;14111412}1413if(ret <0)1414goto return_failed;1415/*1416 * Sparse checkout is meant to narrow down checkout area1417 * but it does not make sense to narrow down to empty working1418 * tree. This is usually a mistake in sparse checkout rules.1419 * Do not allow users to do that.1420 */1421if(o->result.cache_nr && empty_worktree) {1422 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1423goto done;1424}1425}14261427 ret =check_updates(o) ? (-2) :0;1428if(o->dst_index) {1429if(!ret) {1430if(!o->result.cache_tree)1431 o->result.cache_tree =cache_tree();1432if(!cache_tree_fully_valid(o->result.cache_tree))1433cache_tree_update(&o->result,1434 WRITE_TREE_SILENT |1435 WRITE_TREE_REPAIR);1436}1437move_index_extensions(&o->result, o->src_index);1438discard_index(o->dst_index);1439*o->dst_index = o->result;1440}else{1441discard_index(&o->result);1442}1443 o->src_index = NULL;14441445done:1446clear_exclude_list(&el);1447return ret;14481449return_failed:1450if(o->show_all_errors)1451display_error_msgs(o);1452mark_all_ce_unused(o->src_index);1453 ret =unpack_failed(o, NULL);1454if(o->exiting_early)1455 ret =0;1456goto done;1457}14581459/* Here come the merge functions */14601461static intreject_merge(const struct cache_entry *ce,1462struct unpack_trees_options *o)1463{1464return o->gently ? -1:1465add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1466}14671468static intsame(const struct cache_entry *a,const struct cache_entry *b)1469{1470if(!!a != !!b)1471return0;1472if(!a && !b)1473return1;1474if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1475return0;1476return a->ce_mode == b->ce_mode &&1477!oidcmp(&a->oid, &b->oid);1478}147914801481/*1482 * When a CE gets turned into an unmerged entry, we1483 * want it to be up-to-date1484 */1485static intverify_uptodate_1(const struct cache_entry *ce,1486struct unpack_trees_options *o,1487enum unpack_trees_error_types error_type)1488{1489struct stat st;14901491if(o->index_only)1492return0;14931494/*1495 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1496 * if this entry is truly up-to-date because this file may be1497 * overwritten.1498 */1499if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1500;/* keep checking */1501else if(o->reset ||ce_uptodate(ce))1502return0;15031504if(!lstat(ce->name, &st)) {1505int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1506unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);15071508if(submodule_from_ce(ce)) {1509int r =check_submodule_move_head(ce,1510"HEAD",oid_to_hex(&ce->oid), o);1511if(r)1512return o->gently ? -1:1513add_rejected_path(o, error_type, ce->name);1514return0;1515}15161517if(!changed)1518return0;1519/*1520 * Historic default policy was to allow submodule to be out1521 * of sync wrt the superproject index. If the submodule was1522 * not considered interesting above, we don't care here.1523 */1524if(S_ISGITLINK(ce->ce_mode))1525return0;15261527 errno =0;1528}1529if(errno == ENOENT)1530return0;1531return o->gently ? -1:1532add_rejected_path(o, error_type, ce->name);1533}15341535intverify_uptodate(const struct cache_entry *ce,1536struct unpack_trees_options *o)1537{1538if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1539return0;1540returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1541}15421543static intverify_uptodate_sparse(const struct cache_entry *ce,1544struct unpack_trees_options *o)1545{1546returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1547}15481549static voidinvalidate_ce_path(const struct cache_entry *ce,1550struct unpack_trees_options *o)1551{1552if(!ce)1553return;1554cache_tree_invalidate_path(o->src_index, ce->name);1555untracked_cache_invalidate_path(o->src_index, ce->name,1);1556}15571558/*1559 * Check that checking out ce->sha1 in subdir ce->name is not1560 * going to overwrite any working files.1561 *1562 * Currently, git does not checkout subprojects during a superproject1563 * checkout, so it is not going to overwrite anything.1564 */1565static intverify_clean_submodule(const char*old_sha1,1566const struct cache_entry *ce,1567enum unpack_trees_error_types error_type,1568struct unpack_trees_options *o)1569{1570if(!submodule_from_ce(ce))1571return0;15721573returncheck_submodule_move_head(ce, old_sha1,1574oid_to_hex(&ce->oid), o);1575}15761577static intverify_clean_subdirectory(const struct cache_entry *ce,1578enum unpack_trees_error_types error_type,1579struct unpack_trees_options *o)1580{1581/*1582 * we are about to extract "ce->name"; we would not want to lose1583 * anything in the existing directory there.1584 */1585int namelen;1586int i;1587struct dir_struct d;1588char*pathbuf;1589int cnt =0;15901591if(S_ISGITLINK(ce->ce_mode)) {1592struct object_id oid;1593int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1594/*1595 * If we are not going to update the submodule, then1596 * we don't care.1597 */1598if(!sub_head && !oidcmp(&oid, &ce->oid))1599return0;1600returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1601 ce, error_type, o);1602}16031604/*1605 * First let's make sure we do not have a local modification1606 * in that directory.1607 */1608 namelen =ce_namelen(ce);1609for(i =locate_in_src_index(ce, o);1610 i < o->src_index->cache_nr;1611 i++) {1612struct cache_entry *ce2 = o->src_index->cache[i];1613int len =ce_namelen(ce2);1614if(len < namelen ||1615strncmp(ce->name, ce2->name, namelen) ||1616 ce2->name[namelen] !='/')1617break;1618/*1619 * ce2->name is an entry in the subdirectory to be1620 * removed.1621 */1622if(!ce_stage(ce2)) {1623if(verify_uptodate(ce2, o))1624return-1;1625add_entry(o, ce2, CE_REMOVE,0);1626mark_ce_used(ce2, o);1627}1628 cnt++;1629}16301631/*1632 * Then we need to make sure that we do not lose a locally1633 * present file that is not ignored.1634 */1635 pathbuf =xstrfmt("%.*s/", namelen, ce->name);16361637memset(&d,0,sizeof(d));1638if(o->dir)1639 d.exclude_per_dir = o->dir->exclude_per_dir;1640 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1641if(i)1642return o->gently ? -1:1643add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1644free(pathbuf);1645return cnt;1646}16471648/*1649 * This gets called when there was no index entry for the tree entry 'dst',1650 * but we found a file in the working tree that 'lstat()' said was fine,1651 * and we're on a case-insensitive filesystem.1652 *1653 * See if we can find a case-insensitive match in the index that also1654 * matches the stat information, and assume it's that other file!1655 */1656static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1657{1658const struct cache_entry *src;16591660 src =index_file_exists(o->src_index, name, len,1);1661return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1662}16631664static intcheck_ok_to_remove(const char*name,int len,int dtype,1665const struct cache_entry *ce,struct stat *st,1666enum unpack_trees_error_types error_type,1667struct unpack_trees_options *o)1668{1669const struct cache_entry *result;16701671/*1672 * It may be that the 'lstat()' succeeded even though1673 * target 'ce' was absent, because there is an old1674 * entry that is different only in case..1675 *1676 * Ignore that lstat() if it matches.1677 */1678if(ignore_case &&icase_exists(o, name, len, st))1679return0;16801681if(o->dir &&1682is_excluded(o->dir, &the_index, name, &dtype))1683/*1684 * ce->name is explicitly excluded, so it is Ok to1685 * overwrite it.1686 */1687return0;1688if(S_ISDIR(st->st_mode)) {1689/*1690 * We are checking out path "foo" and1691 * found "foo/." in the working tree.1692 * This is tricky -- if we have modified1693 * files that are in "foo/" we would lose1694 * them.1695 */1696if(verify_clean_subdirectory(ce, error_type, o) <0)1697return-1;1698return0;1699}17001701/*1702 * The previous round may already have decided to1703 * delete this path, which is in a subdirectory that1704 * is being replaced with a blob.1705 */1706 result =index_file_exists(&o->result, name, len,0);1707if(result) {1708if(result->ce_flags & CE_REMOVE)1709return0;1710}17111712return o->gently ? -1:1713add_rejected_path(o, error_type, name);1714}17151716/*1717 * We do not want to remove or overwrite a working tree file that1718 * is not tracked, unless it is ignored.1719 */1720static intverify_absent_1(const struct cache_entry *ce,1721enum unpack_trees_error_types error_type,1722struct unpack_trees_options *o)1723{1724int len;1725struct stat st;17261727if(o->index_only || o->reset || !o->update)1728return0;17291730 len =check_leading_path(ce->name,ce_namelen(ce));1731if(!len)1732return0;1733else if(len >0) {1734char*path;1735int ret;17361737 path =xmemdupz(ce->name, len);1738if(lstat(path, &st))1739 ret =error_errno("cannot stat '%s'", path);1740else{1741if(submodule_from_ce(ce))1742 ret =check_submodule_move_head(ce,1743oid_to_hex(&ce->oid),1744 NULL, o);1745else1746 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1747&st, error_type, o);1748}1749free(path);1750return ret;1751}else if(lstat(ce->name, &st)) {1752if(errno != ENOENT)1753returnerror_errno("cannot stat '%s'", ce->name);1754return0;1755}else{1756if(submodule_from_ce(ce))1757returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1758 NULL, o);17591760returncheck_ok_to_remove(ce->name,ce_namelen(ce),1761ce_to_dtype(ce), ce, &st,1762 error_type, o);1763}1764}17651766static intverify_absent(const struct cache_entry *ce,1767enum unpack_trees_error_types error_type,1768struct unpack_trees_options *o)1769{1770if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1771return0;1772returnverify_absent_1(ce, error_type, o);1773}17741775static intverify_absent_sparse(const struct cache_entry *ce,1776enum unpack_trees_error_types error_type,1777struct unpack_trees_options *o)1778{1779enum unpack_trees_error_types orphaned_error = error_type;1780if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1781 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17821783returnverify_absent_1(ce, orphaned_error, o);1784}17851786static intmerged_entry(const struct cache_entry *ce,1787const struct cache_entry *old,1788struct unpack_trees_options *o)1789{1790int update = CE_UPDATE;1791struct cache_entry *merge =dup_entry(ce);17921793if(!old) {1794/*1795 * New index entries. In sparse checkout, the following1796 * verify_absent() will be delayed until after1797 * traverse_trees() finishes in unpack_trees(), then:1798 *1799 * - CE_NEW_SKIP_WORKTREE will be computed correctly1800 * - verify_absent() be called again, this time with1801 * correct CE_NEW_SKIP_WORKTREE1802 *1803 * verify_absent() call here does nothing in sparse1804 * checkout (i.e. o->skip_sparse_checkout == 0)1805 */1806 update |= CE_ADDED;1807 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;18081809if(verify_absent(merge,1810 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1811free(merge);1812return-1;1813}1814invalidate_ce_path(merge, o);18151816if(submodule_from_ce(ce)) {1817int ret =check_submodule_move_head(ce, NULL,1818oid_to_hex(&ce->oid),1819 o);1820if(ret)1821return ret;1822}18231824}else if(!(old->ce_flags & CE_CONFLICTED)) {1825/*1826 * See if we can re-use the old CE directly?1827 * That way we get the uptodate stat info.1828 *1829 * This also removes the UPDATE flag on a match; otherwise1830 * we will end up overwriting local changes in the work tree.1831 */1832if(same(old, merge)) {1833copy_cache_entry(merge, old);1834 update =0;1835}else{1836if(verify_uptodate(old, o)) {1837free(merge);1838return-1;1839}1840/* Migrate old flags over */1841 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1842invalidate_ce_path(old, o);1843}18441845if(submodule_from_ce(ce)) {1846int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1847oid_to_hex(&ce->oid),1848 o);1849if(ret)1850return ret;1851}1852}else{1853/*1854 * Previously unmerged entry left as an existence1855 * marker by read_index_unmerged();1856 */1857invalidate_ce_path(old, o);1858}18591860do_add_entry(o, merge, update, CE_STAGEMASK);1861return1;1862}18631864static intdeleted_entry(const struct cache_entry *ce,1865const struct cache_entry *old,1866struct unpack_trees_options *o)1867{1868/* Did it exist in the index? */1869if(!old) {1870if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1871return-1;1872return0;1873}1874if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1875return-1;1876add_entry(o, ce, CE_REMOVE,0);1877invalidate_ce_path(ce, o);1878return1;1879}18801881static intkeep_entry(const struct cache_entry *ce,1882struct unpack_trees_options *o)1883{1884add_entry(o, ce,0,0);1885return1;1886}18871888#if DBRT_DEBUG1889static voidshow_stage_entry(FILE*o,1890const char*label,const struct cache_entry *ce)1891{1892if(!ce)1893fprintf(o,"%s(missing)\n", label);1894else1895fprintf(o,"%s%06o%s %d\t%s\n",1896 label,1897 ce->ce_mode,1898oid_to_hex(&ce->oid),1899ce_stage(ce),1900 ce->name);1901}1902#endif19031904intthreeway_merge(const struct cache_entry *const*stages,1905struct unpack_trees_options *o)1906{1907const struct cache_entry *index;1908const struct cache_entry *head;1909const struct cache_entry *remote = stages[o->head_idx +1];1910int count;1911int head_match =0;1912int remote_match =0;19131914int df_conflict_head =0;1915int df_conflict_remote =0;19161917int any_anc_missing =0;1918int no_anc_exists =1;1919int i;19201921for(i =1; i < o->head_idx; i++) {1922if(!stages[i] || stages[i] == o->df_conflict_entry)1923 any_anc_missing =1;1924else1925 no_anc_exists =0;1926}19271928 index = stages[0];1929 head = stages[o->head_idx];19301931if(head == o->df_conflict_entry) {1932 df_conflict_head =1;1933 head = NULL;1934}19351936if(remote == o->df_conflict_entry) {1937 df_conflict_remote =1;1938 remote = NULL;1939}19401941/*1942 * First, if there's a #16 situation, note that to prevent #131943 * and #14.1944 */1945if(!same(remote, head)) {1946for(i =1; i < o->head_idx; i++) {1947if(same(stages[i], head)) {1948 head_match = i;1949}1950if(same(stages[i], remote)) {1951 remote_match = i;1952}1953}1954}19551956/*1957 * We start with cases where the index is allowed to match1958 * something other than the head: #14(ALT) and #2ALT, where it1959 * is permitted to match the result instead.1960 */1961/* #14, #14ALT, #2ALT */1962if(remote && !df_conflict_head && head_match && !remote_match) {1963if(index && !same(index, remote) && !same(index, head))1964returnreject_merge(index, o);1965returnmerged_entry(remote, index, o);1966}1967/*1968 * If we have an entry in the index cache, then we want to1969 * make sure that it matches head.1970 */1971if(index && !same(index, head))1972returnreject_merge(index, o);19731974if(head) {1975/* #5ALT, #15 */1976if(same(head, remote))1977returnmerged_entry(head, index, o);1978/* #13, #3ALT */1979if(!df_conflict_remote && remote_match && !head_match)1980returnmerged_entry(head, index, o);1981}19821983/* #1 */1984if(!head && !remote && any_anc_missing)1985return0;19861987/*1988 * Under the "aggressive" rule, we resolve mostly trivial1989 * cases that we historically had git-merge-one-file resolve.1990 */1991if(o->aggressive) {1992int head_deleted = !head;1993int remote_deleted = !remote;1994const struct cache_entry *ce = NULL;19951996if(index)1997 ce = index;1998else if(head)1999 ce = head;2000else if(remote)2001 ce = remote;2002else{2003for(i =1; i < o->head_idx; i++) {2004if(stages[i] && stages[i] != o->df_conflict_entry) {2005 ce = stages[i];2006break;2007}2008}2009}20102011/*2012 * Deleted in both.2013 * Deleted in one and unchanged in the other.2014 */2015if((head_deleted && remote_deleted) ||2016(head_deleted && remote && remote_match) ||2017(remote_deleted && head && head_match)) {2018if(index)2019returndeleted_entry(index, index, o);2020if(ce && !head_deleted) {2021if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2022return-1;2023}2024return0;2025}2026/*2027 * Added in both, identically.2028 */2029if(no_anc_exists && head && remote &&same(head, remote))2030returnmerged_entry(head, index, o);20312032}20332034/* Below are "no merge" cases, which require that the index be2035 * up-to-date to avoid the files getting overwritten with2036 * conflict resolution files.2037 */2038if(index) {2039if(verify_uptodate(index, o))2040return-1;2041}20422043 o->nontrivial_merge =1;20442045/* #2, #3, #4, #6, #7, #9, #10, #11. */2046 count =0;2047if(!head_match || !remote_match) {2048for(i =1; i < o->head_idx; i++) {2049if(stages[i] && stages[i] != o->df_conflict_entry) {2050keep_entry(stages[i], o);2051 count++;2052break;2053}2054}2055}2056#if DBRT_DEBUG2057else{2058fprintf(stderr,"read-tree: warning #16 detected\n");2059show_stage_entry(stderr,"head ", stages[head_match]);2060show_stage_entry(stderr,"remote ", stages[remote_match]);2061}2062#endif2063if(head) { count +=keep_entry(head, o); }2064if(remote) { count +=keep_entry(remote, o); }2065return count;2066}20672068/*2069 * Two-way merge.2070 *2071 * The rule is to "carry forward" what is in the index without losing2072 * information across a "fast-forward", favoring a successful merge2073 * over a merge failure when it makes sense. For details of the2074 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2075 *2076 */2077inttwoway_merge(const struct cache_entry *const*src,2078struct unpack_trees_options *o)2079{2080const struct cache_entry *current = src[0];2081const struct cache_entry *oldtree = src[1];2082const struct cache_entry *newtree = src[2];20832084if(o->merge_size !=2)2085returnerror("Cannot do a twoway merge of%dtrees",2086 o->merge_size);20872088if(oldtree == o->df_conflict_entry)2089 oldtree = NULL;2090if(newtree == o->df_conflict_entry)2091 newtree = NULL;20922093if(current) {2094if(current->ce_flags & CE_CONFLICTED) {2095if(same(oldtree, newtree) || o->reset) {2096if(!newtree)2097returndeleted_entry(current, current, o);2098else2099returnmerged_entry(newtree, current, o);2100}2101returnreject_merge(current, o);2102}else if((!oldtree && !newtree) ||/* 4 and 5 */2103(!oldtree && newtree &&2104same(current, newtree)) ||/* 6 and 7 */2105(oldtree && newtree &&2106same(oldtree, newtree)) ||/* 14 and 15 */2107(oldtree && newtree &&2108!same(oldtree, newtree) &&/* 18 and 19 */2109same(current, newtree))) {2110returnkeep_entry(current, o);2111}else if(oldtree && !newtree &&same(current, oldtree)) {2112/* 10 or 11 */2113returndeleted_entry(oldtree, current, o);2114}else if(oldtree && newtree &&2115same(current, oldtree) && !same(current, newtree)) {2116/* 20 or 21 */2117returnmerged_entry(newtree, current, o);2118}else2119returnreject_merge(current, o);2120}2121else if(newtree) {2122if(oldtree && !o->initial_checkout) {2123/*2124 * deletion of the path was staged;2125 */2126if(same(oldtree, newtree))2127return1;2128returnreject_merge(oldtree, o);2129}2130returnmerged_entry(newtree, current, o);2131}2132returndeleted_entry(oldtree, current, o);2133}21342135/*2136 * Bind merge.2137 *2138 * Keep the index entries at stage0, collapse stage1 but make sure2139 * stage0 does not have anything there.2140 */2141intbind_merge(const struct cache_entry *const*src,2142struct unpack_trees_options *o)2143{2144const struct cache_entry *old = src[0];2145const struct cache_entry *a = src[1];21462147if(o->merge_size !=1)2148returnerror("Cannot do a bind merge of%dtrees",2149 o->merge_size);2150if(a && old)2151return o->gently ? -1:2152error(ERRORMSG(o, ERROR_BIND_OVERLAP),2153super_prefixed(a->name),2154super_prefixed(old->name));2155if(!a)2156returnkeep_entry(old, o);2157else2158returnmerged_entry(a, NULL, o);2159}21602161/*2162 * One-way merge.2163 *2164 * The rule is:2165 * - take the stat information from stage0, take the data from stage12166 */2167intoneway_merge(const struct cache_entry *const*src,2168struct unpack_trees_options *o)2169{2170const struct cache_entry *old = src[0];2171const struct cache_entry *a = src[1];21722173if(o->merge_size !=1)2174returnerror("Cannot do a oneway merge of%dtrees",2175 o->merge_size);21762177if(!a || a == o->df_conflict_entry)2178returndeleted_entry(old, old, o);21792180if(old &&same(old, a)) {2181int update =0;2182if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2183struct stat st;2184if(lstat(old->name, &st) ||2185ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2186 update |= CE_UPDATE;2187}2188if(o->update &&S_ISGITLINK(old->ce_mode) &&2189should_update_submodules() && !verify_uptodate(old, o))2190 update |= CE_UPDATE;2191add_entry(o, old, update,0);2192return0;2193}2194returnmerged_entry(a, old, o);2195}