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"fetch-object.h" 20 21/* 22 * Error messages expected by scripts out of plumbing commands such as 23 * read-tree. Non-scripted Porcelain is not required to use these messages 24 * and in fact are encouraged to reword them to better suit their particular 25 * situation better. See how "git checkout" and "git merge" replaces 26 * them using setup_unpack_trees_porcelain(), for example. 27 */ 28static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 29/* ERROR_WOULD_OVERWRITE */ 30"Entry '%s' would be overwritten by merge. Cannot merge.", 31 32/* ERROR_NOT_UPTODATE_FILE */ 33"Entry '%s' not uptodate. Cannot merge.", 34 35/* ERROR_NOT_UPTODATE_DIR */ 36"Updating '%s' would lose untracked files in it", 37 38/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 39"Untracked working tree file '%s' would be overwritten by merge.", 40 41/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 42"Untracked working tree file '%s' would be removed by merge.", 43 44/* ERROR_BIND_OVERLAP */ 45"Entry '%s' overlaps with '%s'. Cannot bind.", 46 47/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 48"Entry '%s' not uptodate. Cannot update sparse checkout.", 49 50/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 51"Working tree file '%s' would be overwritten by sparse checkout update.", 52 53/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 54"Working tree file '%s' would be removed by sparse checkout update.", 55 56/* ERROR_WOULD_LOSE_SUBMODULE */ 57"Submodule '%s' cannot checkout new HEAD.", 58}; 59 60#define ERRORMSG(o,type) \ 61 ( ((o) && (o)->msgs[(type)]) \ 62 ? ((o)->msgs[(type)]) \ 63 : (unpack_plumbing_errors[(type)]) ) 64 65static const char*super_prefixed(const char*path) 66{ 67/* 68 * It is necessary and sufficient to have two static buffers 69 * here, as the return value of this function is fed to 70 * error() using the unpack_*_errors[] templates we see above. 71 */ 72static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 73static int super_prefix_len = -1; 74static unsigned idx =ARRAY_SIZE(buf) -1; 75 76if(super_prefix_len <0) { 77const char*super_prefix =get_super_prefix(); 78if(!super_prefix) { 79 super_prefix_len =0; 80}else{ 81int i; 82for(i =0; i <ARRAY_SIZE(buf); i++) 83strbuf_addstr(&buf[i], super_prefix); 84 super_prefix_len = buf[0].len; 85} 86} 87 88if(!super_prefix_len) 89return path; 90 91if(++idx >=ARRAY_SIZE(buf)) 92 idx =0; 93 94strbuf_setlen(&buf[idx], super_prefix_len); 95strbuf_addstr(&buf[idx], path); 96 97return buf[idx].buf; 98} 99 100voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 101const char*cmd) 102{ 103int i; 104const char**msgs = opts->msgs; 105const char*msg; 106 107argv_array_init(&opts->msgs_to_free); 108 109if(!strcmp(cmd,"checkout")) 110 msg = advice_commit_before_merge 111?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 112"Please commit your changes or stash them before you switch branches.") 113:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 114else if(!strcmp(cmd,"merge")) 115 msg = advice_commit_before_merge 116?_("Your local changes to the following files would be overwritten by merge:\n%%s" 117"Please commit your changes or stash them before you merge.") 118:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 119else 120 msg = advice_commit_before_merge 121?_("Your local changes to the following files would be overwritten by%s:\n%%s" 122"Please commit your changes or stash them before you%s.") 123:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 124 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 125argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 126 127 msgs[ERROR_NOT_UPTODATE_DIR] = 128_("Updating the following directories would lose untracked files in them:\n%s"); 129 130if(!strcmp(cmd,"checkout")) 131 msg = advice_commit_before_merge 132?_("The following untracked working tree files would be removed by checkout:\n%%s" 133"Please move or remove them before you switch branches.") 134:_("The following untracked working tree files would be removed by checkout:\n%%s"); 135else if(!strcmp(cmd,"merge")) 136 msg = advice_commit_before_merge 137?_("The following untracked working tree files would be removed by merge:\n%%s" 138"Please move or remove them before you merge.") 139:_("The following untracked working tree files would be removed by merge:\n%%s"); 140else 141 msg = advice_commit_before_merge 142?_("The following untracked working tree files would be removed by%s:\n%%s" 143"Please move or remove them before you%s.") 144:_("The following untracked working tree files would be removed by%s:\n%%s"); 145 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = 146argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 147 148if(!strcmp(cmd,"checkout")) 149 msg = advice_commit_before_merge 150?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 151"Please move or remove them before you switch branches.") 152:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 153else if(!strcmp(cmd,"merge")) 154 msg = advice_commit_before_merge 155?_("The following untracked working tree files would be overwritten by merge:\n%%s" 156"Please move or remove them before you merge.") 157:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 158else 159 msg = advice_commit_before_merge 160?_("The following untracked working tree files would be overwritten by%s:\n%%s" 161"Please move or remove them before you%s.") 162:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 163 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = 164argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd); 165 166/* 167 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 168 * cannot easily display it as a list. 169 */ 170 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 171 172 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 173_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 174 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 175_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 176 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 177_("The following working tree files would be removed by sparse checkout update:\n%s"); 178 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 179_("Cannot update submodule:\n%s"); 180 181 opts->show_all_errors =1; 182/* rejected paths may not have a static buffer */ 183for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 184 opts->unpack_rejects[i].strdup_strings =1; 185} 186 187voidclear_unpack_trees_porcelain(struct unpack_trees_options *opts) 188{ 189argv_array_clear(&opts->msgs_to_free); 190memset(opts->msgs,0,sizeof(opts->msgs)); 191} 192 193static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 194unsigned int set,unsigned int clear) 195{ 196 clear |= CE_HASHED; 197 198if(set & CE_REMOVE) 199 set |= CE_WT_REMOVE; 200 201 ce->ce_flags = (ce->ce_flags & ~clear) | set; 202returnadd_index_entry(&o->result, ce, 203 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 204} 205 206static struct cache_entry *dup_entry(const struct cache_entry *ce) 207{ 208unsigned int size =ce_size(ce); 209struct cache_entry *new_entry =xmalloc(size); 210 211memcpy(new_entry, ce, size); 212return new_entry; 213} 214 215static voidadd_entry(struct unpack_trees_options *o, 216const struct cache_entry *ce, 217unsigned int set,unsigned int clear) 218{ 219do_add_entry(o,dup_entry(ce), set, clear); 220} 221 222/* 223 * add error messages on path <path> 224 * corresponding to the type <e> with the message <msg> 225 * indicating if it should be display in porcelain or not 226 */ 227static intadd_rejected_path(struct unpack_trees_options *o, 228enum unpack_trees_error_types e, 229const char*path) 230{ 231if(!o->show_all_errors) 232returnerror(ERRORMSG(o, e),super_prefixed(path)); 233 234/* 235 * Otherwise, insert in a list for future display by 236 * display_error_msgs() 237 */ 238string_list_append(&o->unpack_rejects[e], path); 239return-1; 240} 241 242/* 243 * display all the error messages stored in a nice way 244 */ 245static voiddisplay_error_msgs(struct unpack_trees_options *o) 246{ 247int e, i; 248int something_displayed =0; 249for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 250struct string_list *rejects = &o->unpack_rejects[e]; 251if(rejects->nr >0) { 252struct strbuf path = STRBUF_INIT; 253 something_displayed =1; 254for(i =0; i < rejects->nr; i++) 255strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 256error(ERRORMSG(o, e),super_prefixed(path.buf)); 257strbuf_release(&path); 258} 259string_list_clear(rejects,0); 260} 261if(something_displayed) 262fprintf(stderr,_("Aborting\n")); 263} 264 265static intcheck_submodule_move_head(const struct cache_entry *ce, 266const char*old_id, 267const char*new_id, 268struct unpack_trees_options *o) 269{ 270unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 271const struct submodule *sub =submodule_from_ce(ce); 272 273if(!sub) 274return0; 275 276if(o->reset) 277 flags |= SUBMODULE_MOVE_HEAD_FORCE; 278 279if(submodule_move_head(ce->name, old_id, new_id, flags)) 280return o->gently ? -1: 281add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 282return0; 283} 284 285/* 286 * Preform the loading of the repository's gitmodules file. This function is 287 * used by 'check_update()' to perform loading of the gitmodules file in two 288 * differnt situations: 289 * (1) before removing entries from the working tree if the gitmodules file has 290 * been marked for removal. This situation is specified by 'state' == NULL. 291 * (2) before checking out entries to the working tree if the gitmodules file 292 * has been marked for update. This situation is specified by 'state' != NULL. 293 */ 294static voidload_gitmodules_file(struct index_state *index, 295struct checkout *state) 296{ 297int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 298 299if(pos >=0) { 300struct cache_entry *ce = index->cache[pos]; 301if(!state && ce->ce_flags & CE_WT_REMOVE) { 302repo_read_gitmodules(the_repository); 303}else if(state && (ce->ce_flags & CE_UPDATE)) { 304submodule_free(the_repository); 305checkout_entry(ce, state, NULL); 306repo_read_gitmodules(the_repository); 307} 308} 309} 310 311/* 312 * Unlink the last component and schedule the leading directories for 313 * removal, such that empty directories get removed. 314 */ 315static voidunlink_entry(const struct cache_entry *ce) 316{ 317const struct submodule *sub =submodule_from_ce(ce); 318if(sub) { 319/* state.force is set at the caller. */ 320submodule_move_head(ce->name,"HEAD", NULL, 321 SUBMODULE_MOVE_HEAD_FORCE); 322} 323if(!check_leading_path(ce->name,ce_namelen(ce))) 324return; 325if(remove_or_warn(ce->ce_mode, ce->name)) 326return; 327schedule_dir_for_removal(ce->name,ce_namelen(ce)); 328} 329 330static struct progress *get_progress(struct unpack_trees_options *o) 331{ 332unsigned cnt =0, total =0; 333struct index_state *index = &o->result; 334 335if(!o->update || !o->verbose_update) 336return NULL; 337 338for(; cnt < index->cache_nr; cnt++) { 339const struct cache_entry *ce = index->cache[cnt]; 340if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 341 total++; 342} 343 344returnstart_delayed_progress(_("Checking out files"), total); 345} 346 347static intcheck_updates(struct unpack_trees_options *o) 348{ 349unsigned cnt =0; 350int errs =0; 351struct progress *progress = NULL; 352struct index_state *index = &o->result; 353struct checkout state = CHECKOUT_INIT; 354int i; 355 356 state.force =1; 357 state.quiet =1; 358 state.refresh_cache =1; 359 state.istate = index; 360 361 progress =get_progress(o); 362 363if(o->update) 364git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 365 366if(should_update_submodules() && o->update && !o->dry_run) 367load_gitmodules_file(index, NULL); 368 369for(i =0; i < index->cache_nr; i++) { 370const struct cache_entry *ce = index->cache[i]; 371 372if(ce->ce_flags & CE_WT_REMOVE) { 373display_progress(progress, ++cnt); 374if(o->update && !o->dry_run) 375unlink_entry(ce); 376} 377} 378remove_marked_cache_entries(index); 379remove_scheduled_dirs(); 380 381if(should_update_submodules() && o->update && !o->dry_run) 382load_gitmodules_file(index, &state); 383 384enable_delayed_checkout(&state); 385if(repository_format_partial_clone && o->update && !o->dry_run) { 386/* 387 * Prefetch the objects that are to be checked out in the loop 388 * below. 389 */ 390struct oid_array to_fetch = OID_ARRAY_INIT; 391int fetch_if_missing_store = fetch_if_missing; 392 fetch_if_missing =0; 393for(i =0; i < index->cache_nr; i++) { 394struct cache_entry *ce = index->cache[i]; 395if((ce->ce_flags & CE_UPDATE) && 396!S_ISGITLINK(ce->ce_mode)) { 397if(!has_object_file(&ce->oid)) 398oid_array_append(&to_fetch, &ce->oid); 399} 400} 401if(to_fetch.nr) 402fetch_objects(repository_format_partial_clone, 403&to_fetch); 404 fetch_if_missing = fetch_if_missing_store; 405oid_array_clear(&to_fetch); 406} 407for(i =0; i < index->cache_nr; i++) { 408struct cache_entry *ce = index->cache[i]; 409 410if(ce->ce_flags & CE_UPDATE) { 411if(ce->ce_flags & CE_WT_REMOVE) 412BUG("both update and delete flags are set on%s", 413 ce->name); 414display_progress(progress, ++cnt); 415 ce->ce_flags &= ~CE_UPDATE; 416if(o->update && !o->dry_run) { 417 errs |=checkout_entry(ce, &state, NULL); 418} 419} 420} 421stop_progress(&progress); 422 errs |=finish_delayed_checkout(&state); 423if(o->update) 424git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 425return errs !=0; 426} 427 428static intverify_uptodate_sparse(const struct cache_entry *ce, 429struct unpack_trees_options *o); 430static intverify_absent_sparse(const struct cache_entry *ce, 431enum unpack_trees_error_types, 432struct unpack_trees_options *o); 433 434static intapply_sparse_checkout(struct index_state *istate, 435struct cache_entry *ce, 436struct unpack_trees_options *o) 437{ 438int was_skip_worktree =ce_skip_worktree(ce); 439 440if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 441 ce->ce_flags |= CE_SKIP_WORKTREE; 442else 443 ce->ce_flags &= ~CE_SKIP_WORKTREE; 444if(was_skip_worktree !=ce_skip_worktree(ce)) { 445 ce->ce_flags |= CE_UPDATE_IN_BASE; 446mark_fsmonitor_invalid(istate, ce); 447 istate->cache_changed |= CE_ENTRY_CHANGED; 448} 449 450/* 451 * if (!was_skip_worktree && !ce_skip_worktree()) { 452 * This is perfectly normal. Move on; 453 * } 454 */ 455 456/* 457 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 458 * area as a result of ce_skip_worktree() shortcuts in 459 * verify_absent() and verify_uptodate(). 460 * Make sure they don't modify worktree if they are already 461 * outside checkout area 462 */ 463if(was_skip_worktree &&ce_skip_worktree(ce)) { 464 ce->ce_flags &= ~CE_UPDATE; 465 466/* 467 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 468 * on to get that file removed from both index and worktree. 469 * If that file is already outside worktree area, don't 470 * bother remove it. 471 */ 472if(ce->ce_flags & CE_REMOVE) 473 ce->ce_flags &= ~CE_WT_REMOVE; 474} 475 476if(!was_skip_worktree &&ce_skip_worktree(ce)) { 477/* 478 * If CE_UPDATE is set, verify_uptodate() must be called already 479 * also stat info may have lost after merged_entry() so calling 480 * verify_uptodate() again may fail 481 */ 482if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 483return-1; 484 ce->ce_flags |= CE_WT_REMOVE; 485 ce->ce_flags &= ~CE_UPDATE; 486} 487if(was_skip_worktree && !ce_skip_worktree(ce)) { 488if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 489return-1; 490 ce->ce_flags |= CE_UPDATE; 491} 492return0; 493} 494 495staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 496struct unpack_trees_options *o) 497{ 498int ret = o->fn(src, o); 499if(ret >0) 500 ret =0; 501return ret; 502} 503 504static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 505{ 506 ce->ce_flags |= CE_UNPACKED; 507 508if(o->cache_bottom < o->src_index->cache_nr && 509 o->src_index->cache[o->cache_bottom] == ce) { 510int bottom = o->cache_bottom; 511while(bottom < o->src_index->cache_nr && 512 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 513 bottom++; 514 o->cache_bottom = bottom; 515} 516} 517 518static voidmark_all_ce_unused(struct index_state *index) 519{ 520int i; 521for(i =0; i < index->cache_nr; i++) 522 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 523} 524 525static intlocate_in_src_index(const struct cache_entry *ce, 526struct unpack_trees_options *o) 527{ 528struct index_state *index = o->src_index; 529int len =ce_namelen(ce); 530int pos =index_name_pos(index, ce->name, len); 531if(pos <0) 532 pos = -1- pos; 533return pos; 534} 535 536/* 537 * We call unpack_index_entry() with an unmerged cache entry 538 * only in diff-index, and it wants a single callback. Skip 539 * the other unmerged entry with the same name. 540 */ 541static voidmark_ce_used_same_name(struct cache_entry *ce, 542struct unpack_trees_options *o) 543{ 544struct index_state *index = o->src_index; 545int len =ce_namelen(ce); 546int pos; 547 548for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 549struct cache_entry *next = index->cache[pos]; 550if(len !=ce_namelen(next) || 551memcmp(ce->name, next->name, len)) 552break; 553mark_ce_used(next, o); 554} 555} 556 557static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 558{ 559const struct index_state *index = o->src_index; 560int pos = o->cache_bottom; 561 562while(pos < index->cache_nr) { 563struct cache_entry *ce = index->cache[pos]; 564if(!(ce->ce_flags & CE_UNPACKED)) 565return ce; 566 pos++; 567} 568return NULL; 569} 570 571static voidadd_same_unmerged(const struct cache_entry *ce, 572struct unpack_trees_options *o) 573{ 574struct index_state *index = o->src_index; 575int len =ce_namelen(ce); 576int pos =index_name_pos(index, ce->name, len); 577 578if(0<= pos) 579die("programming error in a caller of mark_ce_used_same_name"); 580for(pos = -pos -1; pos < index->cache_nr; pos++) { 581struct cache_entry *next = index->cache[pos]; 582if(len !=ce_namelen(next) || 583memcmp(ce->name, next->name, len)) 584break; 585add_entry(o, next,0,0); 586mark_ce_used(next, o); 587} 588} 589 590static intunpack_index_entry(struct cache_entry *ce, 591struct unpack_trees_options *o) 592{ 593const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 594int ret; 595 596 src[0] = ce; 597 598mark_ce_used(ce, o); 599if(ce_stage(ce)) { 600if(o->skip_unmerged) { 601add_entry(o, ce,0,0); 602return0; 603} 604} 605 ret =call_unpack_fn(src, o); 606if(ce_stage(ce)) 607mark_ce_used_same_name(ce, o); 608return ret; 609} 610 611static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 612 613static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 614{ 615struct unpack_trees_options *o = info->data; 616 617if(o->diff_index_cached) 618return; 619 o->cache_bottom = bottom; 620} 621 622static intswitch_cache_bottom(struct traverse_info *info) 623{ 624struct unpack_trees_options *o = info->data; 625int ret, pos; 626 627if(o->diff_index_cached) 628return0; 629 ret = o->cache_bottom; 630 pos =find_cache_pos(info->prev, &info->name); 631 632if(pos < -1) 633 o->cache_bottom = -2- pos; 634else if(pos <0) 635 o->cache_bottom = o->src_index->cache_nr; 636return ret; 637} 638 639staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 640{ 641return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 642} 643 644static inttraverse_trees_recursive(int n,unsigned long dirmask, 645unsigned long df_conflicts, 646struct name_entry *names, 647struct traverse_info *info) 648{ 649int i, ret, bottom; 650int nr_buf =0; 651struct tree_desc t[MAX_UNPACK_TREES]; 652void*buf[MAX_UNPACK_TREES]; 653struct traverse_info newinfo; 654struct name_entry *p; 655 656 p = names; 657while(!p->mode) 658 p++; 659 660 newinfo = *info; 661 newinfo.prev = info; 662 newinfo.pathspec = info->pathspec; 663 newinfo.name = *p; 664 newinfo.pathlen +=tree_entry_len(p) +1; 665 newinfo.df_conflicts |= df_conflicts; 666 667/* 668 * Fetch the tree from the ODB for each peer directory in the 669 * n commits. 670 * 671 * For 2- and 3-way traversals, we try to avoid hitting the 672 * ODB twice for the same OID. This should yield a nice speed 673 * up in checkouts and merges when the commits are similar. 674 * 675 * We don't bother doing the full O(n^2) search for larger n, 676 * because wider traversals don't happen that often and we 677 * avoid the search setup. 678 * 679 * When 2 peer OIDs are the same, we just copy the tree 680 * descriptor data. This implicitly borrows the buffer 681 * data from the earlier cell. 682 */ 683for(i =0; i < n; i++, dirmask >>=1) { 684if(i >0&&are_same_oid(&names[i], &names[i -1])) 685 t[i] = t[i -1]; 686else if(i >1&&are_same_oid(&names[i], &names[i -2])) 687 t[i] = t[i -2]; 688else{ 689const struct object_id *oid = NULL; 690if(dirmask &1) 691 oid = names[i].oid; 692 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 693} 694} 695 696 bottom =switch_cache_bottom(&newinfo); 697 ret =traverse_trees(n, t, &newinfo); 698restore_cache_bottom(&newinfo, bottom); 699 700for(i =0; i < nr_buf; i++) 701free(buf[i]); 702 703return ret; 704} 705 706/* 707 * Compare the traverse-path to the cache entry without actually 708 * having to generate the textual representation of the traverse 709 * path. 710 * 711 * NOTE! This *only* compares up to the size of the traverse path 712 * itself - the caller needs to do the final check for the cache 713 * entry having more data at the end! 714 */ 715static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 716{ 717int len, pathlen, ce_len; 718const char*ce_name; 719 720if(info->prev) { 721int cmp =do_compare_entry_piecewise(ce, info->prev, 722&info->name); 723if(cmp) 724return cmp; 725} 726 pathlen = info->pathlen; 727 ce_len =ce_namelen(ce); 728 729/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 730if(ce_len < pathlen) 731return-1; 732 733 ce_len -= pathlen; 734 ce_name = ce->name + pathlen; 735 736 len =tree_entry_len(n); 737returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 738} 739 740static intdo_compare_entry(const struct cache_entry *ce, 741const struct traverse_info *info, 742const struct name_entry *n) 743{ 744int len, pathlen, ce_len; 745const char*ce_name; 746int cmp; 747 748/* 749 * If we have not precomputed the traverse path, it is quicker 750 * to avoid doing so. But if we have precomputed it, 751 * it is quicker to use the precomputed version. 752 */ 753if(!info->traverse_path) 754returndo_compare_entry_piecewise(ce, info, n); 755 756 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 757if(cmp) 758return cmp; 759 760 pathlen = info->pathlen; 761 ce_len =ce_namelen(ce); 762 763if(ce_len < pathlen) 764return-1; 765 766 ce_len -= pathlen; 767 ce_name = ce->name + pathlen; 768 769 len =tree_entry_len(n); 770returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 771} 772 773static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 774{ 775int cmp =do_compare_entry(ce, info, n); 776if(cmp) 777return cmp; 778 779/* 780 * Even if the beginning compared identically, the ce should 781 * compare as bigger than a directory leading up to it! 782 */ 783returnce_namelen(ce) >traverse_path_len(info, n); 784} 785 786static intce_in_traverse_path(const struct cache_entry *ce, 787const struct traverse_info *info) 788{ 789if(!info->prev) 790return1; 791if(do_compare_entry(ce, info->prev, &info->name)) 792return0; 793/* 794 * If ce (blob) is the same name as the path (which is a tree 795 * we will be descending into), it won't be inside it. 796 */ 797return(info->pathlen <ce_namelen(ce)); 798} 799 800static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 801{ 802int len =traverse_path_len(info, n); 803struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 804 805 ce->ce_mode =create_ce_mode(n->mode); 806 ce->ce_flags =create_ce_flags(stage); 807 ce->ce_namelen = len; 808oidcpy(&ce->oid, n->oid); 809make_traverse_path(ce->name, info, n); 810 811return ce; 812} 813 814static intunpack_nondirectories(int n,unsigned long mask, 815unsigned long dirmask, 816struct cache_entry **src, 817const struct name_entry *names, 818const struct traverse_info *info) 819{ 820int i; 821struct unpack_trees_options *o = info->data; 822unsigned long conflicts = info->df_conflicts | dirmask; 823 824/* Do we have *only* directories? Nothing to do */ 825if(mask == dirmask && !src[0]) 826return0; 827 828/* 829 * Ok, we've filled in up to any potential index entry in src[0], 830 * now do the rest. 831 */ 832for(i =0; i < n; i++) { 833int stage; 834unsigned int bit =1ul<< i; 835if(conflicts & bit) { 836 src[i + o->merge] = o->df_conflict_entry; 837continue; 838} 839if(!(mask & bit)) 840continue; 841if(!o->merge) 842 stage =0; 843else if(i +1< o->head_idx) 844 stage =1; 845else if(i +1> o->head_idx) 846 stage =3; 847else 848 stage =2; 849 src[i + o->merge] =create_ce_entry(info, names + i, stage); 850} 851 852if(o->merge) { 853int rc =call_unpack_fn((const struct cache_entry *const*)src, 854 o); 855for(i =0; i < n; i++) { 856struct cache_entry *ce = src[i + o->merge]; 857if(ce != o->df_conflict_entry) 858free(ce); 859} 860return rc; 861} 862 863for(i =0; i < n; i++) 864if(src[i] && src[i] != o->df_conflict_entry) 865if(do_add_entry(o, src[i],0,0)) 866return-1; 867 868return0; 869} 870 871static intunpack_failed(struct unpack_trees_options *o,const char*message) 872{ 873discard_index(&o->result); 874if(!o->gently && !o->exiting_early) { 875if(message) 876returnerror("%s", message); 877return-1; 878} 879return-1; 880} 881 882/* 883 * The tree traversal is looking at name p. If we have a matching entry, 884 * return it. If name p is a directory in the index, do not return 885 * anything, as we will want to match it when the traversal descends into 886 * the directory. 887 */ 888static intfind_cache_pos(struct traverse_info *info, 889const struct name_entry *p) 890{ 891int pos; 892struct unpack_trees_options *o = info->data; 893struct index_state *index = o->src_index; 894int pfxlen = info->pathlen; 895int p_len =tree_entry_len(p); 896 897for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 898const struct cache_entry *ce = index->cache[pos]; 899const char*ce_name, *ce_slash; 900int cmp, ce_len; 901 902if(ce->ce_flags & CE_UNPACKED) { 903/* 904 * cache_bottom entry is already unpacked, so 905 * we can never match it; don't check it 906 * again. 907 */ 908if(pos == o->cache_bottom) 909++o->cache_bottom; 910continue; 911} 912if(!ce_in_traverse_path(ce, info)) { 913/* 914 * Check if we can skip future cache checks 915 * (because we're already past all possible 916 * entries in the traverse path). 917 */ 918if(info->traverse_path) { 919if(strncmp(ce->name, info->traverse_path, 920 info->pathlen) >0) 921break; 922} 923continue; 924} 925 ce_name = ce->name + pfxlen; 926 ce_slash =strchr(ce_name,'/'); 927if(ce_slash) 928 ce_len = ce_slash - ce_name; 929else 930 ce_len =ce_namelen(ce) - pfxlen; 931 cmp =name_compare(p->path, p_len, ce_name, ce_len); 932/* 933 * Exact match; if we have a directory we need to 934 * delay returning it. 935 */ 936if(!cmp) 937return ce_slash ? -2- pos : pos; 938if(0< cmp) 939continue;/* keep looking */ 940/* 941 * ce_name sorts after p->path; could it be that we 942 * have files under p->path directory in the index? 943 * E.g. ce_name == "t-i", and p->path == "t"; we may 944 * have "t/a" in the index. 945 */ 946if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 947 ce_name[p_len] <'/') 948continue;/* keep looking */ 949break; 950} 951return-1; 952} 953 954static struct cache_entry *find_cache_entry(struct traverse_info *info, 955const struct name_entry *p) 956{ 957int pos =find_cache_pos(info, p); 958struct unpack_trees_options *o = info->data; 959 960if(0<= pos) 961return o->src_index->cache[pos]; 962else 963return NULL; 964} 965 966static voiddebug_path(struct traverse_info *info) 967{ 968if(info->prev) { 969debug_path(info->prev); 970if(*info->prev->name.path) 971putchar('/'); 972} 973printf("%s", info->name.path); 974} 975 976static voiddebug_name_entry(int i,struct name_entry *n) 977{ 978printf("ent#%d %06o%s\n", i, 979 n->path ? n->mode :0, 980 n->path ? n->path :"(missing)"); 981} 982 983static voiddebug_unpack_callback(int n, 984unsigned long mask, 985unsigned long dirmask, 986struct name_entry *names, 987struct traverse_info *info) 988{ 989int i; 990printf("* unpack mask%lu, dirmask%lu, cnt%d", 991 mask, dirmask, n); 992debug_path(info); 993putchar('\n'); 994for(i =0; i < n; i++) 995debug_name_entry(i, names + i); 996} 997 998static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 999{1000struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1001struct unpack_trees_options *o = info->data;1002const struct name_entry *p = names;10031004/* Find first entry with a real name (we could use "mask" too) */1005while(!p->mode)1006 p++;10071008if(o->debug_unpack)1009debug_unpack_callback(n, mask, dirmask, names, info);10101011/* Are we supposed to look at the index too? */1012if(o->merge) {1013while(1) {1014int cmp;1015struct cache_entry *ce;10161017if(o->diff_index_cached)1018 ce =next_cache_entry(o);1019else1020 ce =find_cache_entry(info, p);10211022if(!ce)1023break;1024 cmp =compare_entry(ce, info, p);1025if(cmp <0) {1026if(unpack_index_entry(ce, o) <0)1027returnunpack_failed(o, NULL);1028continue;1029}1030if(!cmp) {1031if(ce_stage(ce)) {1032/*1033 * If we skip unmerged index1034 * entries, we'll skip this1035 * entry *and* the tree1036 * entries associated with it!1037 */1038if(o->skip_unmerged) {1039add_same_unmerged(ce, o);1040return mask;1041}1042}1043 src[0] = ce;1044}1045break;1046}1047}10481049if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1050return-1;10511052if(o->merge && src[0]) {1053if(ce_stage(src[0]))1054mark_ce_used_same_name(src[0], o);1055else1056mark_ce_used(src[0], o);1057}10581059/* Now handle any directories.. */1060if(dirmask) {1061/* special case: "diff-index --cached" looking at a tree */1062if(o->diff_index_cached &&1063 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1064int matches;1065 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1066 names, info);1067/*1068 * Everything under the name matches; skip the1069 * entire hierarchy. diff_index_cached codepath1070 * special cases D/F conflicts in such a way that1071 * it does not do any look-ahead, so this is safe.1072 */1073if(matches) {1074 o->cache_bottom += matches;1075return mask;1076}1077}10781079if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1080 names, info) <0)1081return-1;1082return mask;1083}10841085return mask;1086}10871088static intclear_ce_flags_1(struct cache_entry **cache,int nr,1089struct strbuf *prefix,1090int select_mask,int clear_mask,1091struct exclude_list *el,int defval);10921093/* Whole directory matching */1094static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1095struct strbuf *prefix,1096char*basename,1097int select_mask,int clear_mask,1098struct exclude_list *el,int defval)1099{1100struct cache_entry **cache_end;1101int dtype = DT_DIR;1102int ret =is_excluded_from_list(prefix->buf, prefix->len,1103 basename, &dtype, el, &the_index);1104int rc;11051106strbuf_addch(prefix,'/');11071108/* If undecided, use matching result of parent dir in defval */1109if(ret <0)1110 ret = defval;11111112for(cache_end = cache; cache_end != cache + nr; cache_end++) {1113struct cache_entry *ce = *cache_end;1114if(strncmp(ce->name, prefix->buf, prefix->len))1115break;1116}11171118/*1119 * TODO: check el, if there are no patterns that may conflict1120 * with ret (iow, we know in advance the incl/excl1121 * decision for the entire directory), clear flag here without1122 * calling clear_ce_flags_1(). That function will call1123 * the expensive is_excluded_from_list() on every entry.1124 */1125 rc =clear_ce_flags_1(cache, cache_end - cache,1126 prefix,1127 select_mask, clear_mask,1128 el, ret);1129strbuf_setlen(prefix, prefix->len -1);1130return rc;1131}11321133/*1134 * Traverse the index, find every entry that matches according to1135 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1136 * number of traversed entries.1137 *1138 * If select_mask is non-zero, only entries whose ce_flags has on of1139 * those bits enabled are traversed.1140 *1141 * cache : pointer to an index entry1142 * prefix_len : an offset to its path1143 *1144 * The current path ("prefix") including the trailing '/' is1145 * cache[0]->name[0..(prefix_len-1)]1146 * Top level path has prefix_len zero.1147 */1148static intclear_ce_flags_1(struct cache_entry **cache,int nr,1149struct strbuf *prefix,1150int select_mask,int clear_mask,1151struct exclude_list *el,int defval)1152{1153struct cache_entry **cache_end = cache + nr;11541155/*1156 * Process all entries that have the given prefix and meet1157 * select_mask condition1158 */1159while(cache != cache_end) {1160struct cache_entry *ce = *cache;1161const char*name, *slash;1162int len, dtype, ret;11631164if(select_mask && !(ce->ce_flags & select_mask)) {1165 cache++;1166continue;1167}11681169if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1170break;11711172 name = ce->name + prefix->len;1173 slash =strchr(name,'/');11741175/* If it's a directory, try whole directory match first */1176if(slash) {1177int processed;11781179 len = slash - name;1180strbuf_add(prefix, name, len);11811182 processed =clear_ce_flags_dir(cache, cache_end - cache,1183 prefix,1184 prefix->buf + prefix->len - len,1185 select_mask, clear_mask,1186 el, defval);11871188/* clear_c_f_dir eats a whole dir already? */1189if(processed) {1190 cache += processed;1191strbuf_setlen(prefix, prefix->len - len);1192continue;1193}11941195strbuf_addch(prefix,'/');1196 cache +=clear_ce_flags_1(cache, cache_end - cache,1197 prefix,1198 select_mask, clear_mask, el, defval);1199strbuf_setlen(prefix, prefix->len - len -1);1200continue;1201}12021203/* Non-directory */1204 dtype =ce_to_dtype(ce);1205 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1206 name, &dtype, el, &the_index);1207if(ret <0)1208 ret = defval;1209if(ret >0)1210 ce->ce_flags &= ~clear_mask;1211 cache++;1212}1213return nr - (cache_end - cache);1214}12151216static intclear_ce_flags(struct cache_entry **cache,int nr,1217int select_mask,int clear_mask,1218struct exclude_list *el)1219{1220static struct strbuf prefix = STRBUF_INIT;12211222strbuf_reset(&prefix);12231224returnclear_ce_flags_1(cache, nr,1225&prefix,1226 select_mask, clear_mask,1227 el,0);1228}12291230/*1231 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1232 */1233static voidmark_new_skip_worktree(struct exclude_list *el,1234struct index_state *the_index,1235int select_flag,int skip_wt_flag)1236{1237int i;12381239/*1240 * 1. Pretend the narrowest worktree: only unmerged entries1241 * are checked out1242 */1243for(i =0; i < the_index->cache_nr; i++) {1244struct cache_entry *ce = the_index->cache[i];12451246if(select_flag && !(ce->ce_flags & select_flag))1247continue;12481249if(!ce_stage(ce))1250 ce->ce_flags |= skip_wt_flag;1251else1252 ce->ce_flags &= ~skip_wt_flag;1253}12541255/*1256 * 2. Widen worktree according to sparse-checkout file.1257 * Matched entries will have skip_wt_flag cleared (i.e. "in")1258 */1259clear_ce_flags(the_index->cache, the_index->cache_nr,1260 select_flag, skip_wt_flag, el);1261}12621263static intverify_absent(const struct cache_entry *,1264enum unpack_trees_error_types,1265struct unpack_trees_options *);1266/*1267 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1268 * resulting index, -2 on failure to reflect the changes to the work tree.1269 *1270 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1271 */1272intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1273{1274int i, ret;1275static struct cache_entry *dfc;1276struct exclude_list el;12771278if(len > MAX_UNPACK_TREES)1279die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12801281memset(&el,0,sizeof(el));1282if(!core_apply_sparse_checkout || !o->update)1283 o->skip_sparse_checkout =1;1284if(!o->skip_sparse_checkout) {1285char*sparse =git_pathdup("info/sparse-checkout");1286if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1287 o->skip_sparse_checkout =1;1288else1289 o->el = ⪙1290free(sparse);1291}12921293memset(&o->result,0,sizeof(o->result));1294 o->result.initialized =1;1295 o->result.timestamp.sec = o->src_index->timestamp.sec;1296 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1297 o->result.version = o->src_index->version;1298if(!o->src_index->split_index) {1299 o->result.split_index = NULL;1300}else if(o->src_index == o->dst_index) {1301/*1302 * o->dst_index (and thus o->src_index) will be discarded1303 * and overwritten with o->result at the end of this function,1304 * so just use src_index's split_index to avoid having to1305 * create a new one.1306 */1307 o->result.split_index = o->src_index->split_index;1308 o->result.split_index->refcount++;1309}else{1310 o->result.split_index =init_split_index(&o->result);1311}1312oidcpy(&o->result.oid, &o->src_index->oid);1313 o->merge_size = len;1314mark_all_ce_unused(o->src_index);13151316/*1317 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1318 */1319if(!o->skip_sparse_checkout)1320mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);13211322if(!dfc)1323 dfc =xcalloc(1,cache_entry_size(0));1324 o->df_conflict_entry = dfc;13251326if(len) {1327const char*prefix = o->prefix ? o->prefix :"";1328struct traverse_info info;13291330setup_traverse_info(&info, prefix);1331 info.fn = unpack_callback;1332 info.data = o;1333 info.show_all_errors = o->show_all_errors;1334 info.pathspec = o->pathspec;13351336if(o->prefix) {1337/*1338 * Unpack existing index entries that sort before the1339 * prefix the tree is spliced into. Note that o->merge1340 * is always true in this case.1341 */1342while(1) {1343struct cache_entry *ce =next_cache_entry(o);1344if(!ce)1345break;1346if(ce_in_traverse_path(ce, &info))1347break;1348if(unpack_index_entry(ce, o) <0)1349goto return_failed;1350}1351}13521353if(traverse_trees(len, t, &info) <0)1354goto return_failed;1355}13561357/* Any left-over entries in the index? */1358if(o->merge) {1359while(1) {1360struct cache_entry *ce =next_cache_entry(o);1361if(!ce)1362break;1363if(unpack_index_entry(ce, o) <0)1364goto return_failed;1365}1366}1367mark_all_ce_unused(o->src_index);13681369if(o->trivial_merges_only && o->nontrivial_merge) {1370 ret =unpack_failed(o,"Merge requires file-level merging");1371goto done;1372}13731374if(!o->skip_sparse_checkout) {1375int empty_worktree =1;13761377/*1378 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11379 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1380 * so apply_sparse_checkout() won't attempt to remove it from worktree1381 */1382mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13831384 ret =0;1385for(i =0; i < o->result.cache_nr; i++) {1386struct cache_entry *ce = o->result.cache[i];13871388/*1389 * Entries marked with CE_ADDED in merged_entry() do not have1390 * verify_absent() check (the check is effectively disabled1391 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1392 *1393 * Do the real check now because we have had1394 * correct CE_NEW_SKIP_WORKTREE1395 */1396if(ce->ce_flags & CE_ADDED &&1397verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1398if(!o->show_all_errors)1399goto return_failed;1400 ret = -1;1401}14021403if(apply_sparse_checkout(&o->result, ce, o)) {1404if(!o->show_all_errors)1405goto return_failed;1406 ret = -1;1407}1408if(!ce_skip_worktree(ce))1409 empty_worktree =0;14101411}1412if(ret <0)1413goto return_failed;1414/*1415 * Sparse checkout is meant to narrow down checkout area1416 * but it does not make sense to narrow down to empty working1417 * tree. This is usually a mistake in sparse checkout rules.1418 * Do not allow users to do that.1419 */1420if(o->result.cache_nr && empty_worktree) {1421 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1422goto done;1423}1424}14251426 ret =check_updates(o) ? (-2) :0;1427if(o->dst_index) {1428if(!ret) {1429if(!o->result.cache_tree)1430 o->result.cache_tree =cache_tree();1431if(!cache_tree_fully_valid(o->result.cache_tree))1432cache_tree_update(&o->result,1433 WRITE_TREE_SILENT |1434 WRITE_TREE_REPAIR);1435}1436move_index_extensions(&o->result, o->src_index);1437discard_index(o->dst_index);1438*o->dst_index = o->result;1439}else{1440discard_index(&o->result);1441}1442 o->src_index = NULL;14431444done:1445clear_exclude_list(&el);1446return ret;14471448return_failed:1449if(o->show_all_errors)1450display_error_msgs(o);1451mark_all_ce_unused(o->src_index);1452 ret =unpack_failed(o, NULL);1453if(o->exiting_early)1454 ret =0;1455goto done;1456}14571458/* Here come the merge functions */14591460static intreject_merge(const struct cache_entry *ce,1461struct unpack_trees_options *o)1462{1463return o->gently ? -1:1464add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1465}14661467static intsame(const struct cache_entry *a,const struct cache_entry *b)1468{1469if(!!a != !!b)1470return0;1471if(!a && !b)1472return1;1473if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1474return0;1475return a->ce_mode == b->ce_mode &&1476!oidcmp(&a->oid, &b->oid);1477}147814791480/*1481 * When a CE gets turned into an unmerged entry, we1482 * want it to be up-to-date1483 */1484static intverify_uptodate_1(const struct cache_entry *ce,1485struct unpack_trees_options *o,1486enum unpack_trees_error_types error_type)1487{1488struct stat st;14891490if(o->index_only)1491return0;14921493/*1494 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1495 * if this entry is truly up-to-date because this file may be1496 * overwritten.1497 */1498if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1499;/* keep checking */1500else if(o->reset ||ce_uptodate(ce))1501return0;15021503if(!lstat(ce->name, &st)) {1504int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1505unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);15061507if(submodule_from_ce(ce)) {1508int r =check_submodule_move_head(ce,1509"HEAD",oid_to_hex(&ce->oid), o);1510if(r)1511return o->gently ? -1:1512add_rejected_path(o, error_type, ce->name);1513return0;1514}15151516if(!changed)1517return0;1518/*1519 * Historic default policy was to allow submodule to be out1520 * of sync wrt the superproject index. If the submodule was1521 * not considered interesting above, we don't care here.1522 */1523if(S_ISGITLINK(ce->ce_mode))1524return0;15251526 errno =0;1527}1528if(errno == ENOENT)1529return0;1530return o->gently ? -1:1531add_rejected_path(o, error_type, ce->name);1532}15331534intverify_uptodate(const struct cache_entry *ce,1535struct unpack_trees_options *o)1536{1537if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1538return0;1539returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1540}15411542static intverify_uptodate_sparse(const struct cache_entry *ce,1543struct unpack_trees_options *o)1544{1545returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1546}15471548static voidinvalidate_ce_path(const struct cache_entry *ce,1549struct unpack_trees_options *o)1550{1551if(!ce)1552return;1553cache_tree_invalidate_path(o->src_index, ce->name);1554untracked_cache_invalidate_path(o->src_index, ce->name,1);1555}15561557/*1558 * Check that checking out ce->sha1 in subdir ce->name is not1559 * going to overwrite any working files.1560 *1561 * Currently, git does not checkout subprojects during a superproject1562 * checkout, so it is not going to overwrite anything.1563 */1564static intverify_clean_submodule(const char*old_sha1,1565const struct cache_entry *ce,1566enum unpack_trees_error_types error_type,1567struct unpack_trees_options *o)1568{1569if(!submodule_from_ce(ce))1570return0;15711572returncheck_submodule_move_head(ce, old_sha1,1573oid_to_hex(&ce->oid), o);1574}15751576static intverify_clean_subdirectory(const struct cache_entry *ce,1577enum unpack_trees_error_types error_type,1578struct unpack_trees_options *o)1579{1580/*1581 * we are about to extract "ce->name"; we would not want to lose1582 * anything in the existing directory there.1583 */1584int namelen;1585int i;1586struct dir_struct d;1587char*pathbuf;1588int cnt =0;15891590if(S_ISGITLINK(ce->ce_mode)) {1591struct object_id oid;1592int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1593/*1594 * If we are not going to update the submodule, then1595 * we don't care.1596 */1597if(!sub_head && !oidcmp(&oid, &ce->oid))1598return0;1599returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1600 ce, error_type, o);1601}16021603/*1604 * First let's make sure we do not have a local modification1605 * in that directory.1606 */1607 namelen =ce_namelen(ce);1608for(i =locate_in_src_index(ce, o);1609 i < o->src_index->cache_nr;1610 i++) {1611struct cache_entry *ce2 = o->src_index->cache[i];1612int len =ce_namelen(ce2);1613if(len < namelen ||1614strncmp(ce->name, ce2->name, namelen) ||1615 ce2->name[namelen] !='/')1616break;1617/*1618 * ce2->name is an entry in the subdirectory to be1619 * removed.1620 */1621if(!ce_stage(ce2)) {1622if(verify_uptodate(ce2, o))1623return-1;1624add_entry(o, ce2, CE_REMOVE,0);1625mark_ce_used(ce2, o);1626}1627 cnt++;1628}16291630/*1631 * Then we need to make sure that we do not lose a locally1632 * present file that is not ignored.1633 */1634 pathbuf =xstrfmt("%.*s/", namelen, ce->name);16351636memset(&d,0,sizeof(d));1637if(o->dir)1638 d.exclude_per_dir = o->dir->exclude_per_dir;1639 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1640if(i)1641return o->gently ? -1:1642add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1643free(pathbuf);1644return cnt;1645}16461647/*1648 * This gets called when there was no index entry for the tree entry 'dst',1649 * but we found a file in the working tree that 'lstat()' said was fine,1650 * and we're on a case-insensitive filesystem.1651 *1652 * See if we can find a case-insensitive match in the index that also1653 * matches the stat information, and assume it's that other file!1654 */1655static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1656{1657const struct cache_entry *src;16581659 src =index_file_exists(o->src_index, name, len,1);1660return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1661}16621663static intcheck_ok_to_remove(const char*name,int len,int dtype,1664const struct cache_entry *ce,struct stat *st,1665enum unpack_trees_error_types error_type,1666struct unpack_trees_options *o)1667{1668const struct cache_entry *result;16691670/*1671 * It may be that the 'lstat()' succeeded even though1672 * target 'ce' was absent, because there is an old1673 * entry that is different only in case..1674 *1675 * Ignore that lstat() if it matches.1676 */1677if(ignore_case &&icase_exists(o, name, len, st))1678return0;16791680if(o->dir &&1681is_excluded(o->dir, &the_index, name, &dtype))1682/*1683 * ce->name is explicitly excluded, so it is Ok to1684 * overwrite it.1685 */1686return0;1687if(S_ISDIR(st->st_mode)) {1688/*1689 * We are checking out path "foo" and1690 * found "foo/." in the working tree.1691 * This is tricky -- if we have modified1692 * files that are in "foo/" we would lose1693 * them.1694 */1695if(verify_clean_subdirectory(ce, error_type, o) <0)1696return-1;1697return0;1698}16991700/*1701 * The previous round may already have decided to1702 * delete this path, which is in a subdirectory that1703 * is being replaced with a blob.1704 */1705 result =index_file_exists(&o->result, name, len,0);1706if(result) {1707if(result->ce_flags & CE_REMOVE)1708return0;1709}17101711return o->gently ? -1:1712add_rejected_path(o, error_type, name);1713}17141715/*1716 * We do not want to remove or overwrite a working tree file that1717 * is not tracked, unless it is ignored.1718 */1719static intverify_absent_1(const struct cache_entry *ce,1720enum unpack_trees_error_types error_type,1721struct unpack_trees_options *o)1722{1723int len;1724struct stat st;17251726if(o->index_only || o->reset || !o->update)1727return0;17281729 len =check_leading_path(ce->name,ce_namelen(ce));1730if(!len)1731return0;1732else if(len >0) {1733char*path;1734int ret;17351736 path =xmemdupz(ce->name, len);1737if(lstat(path, &st))1738 ret =error_errno("cannot stat '%s'", path);1739else{1740if(submodule_from_ce(ce))1741 ret =check_submodule_move_head(ce,1742oid_to_hex(&ce->oid),1743 NULL, o);1744else1745 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1746&st, error_type, o);1747}1748free(path);1749return ret;1750}else if(lstat(ce->name, &st)) {1751if(errno != ENOENT)1752returnerror_errno("cannot stat '%s'", ce->name);1753return0;1754}else{1755if(submodule_from_ce(ce))1756returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1757 NULL, o);17581759returncheck_ok_to_remove(ce->name,ce_namelen(ce),1760ce_to_dtype(ce), ce, &st,1761 error_type, o);1762}1763}17641765static intverify_absent(const struct cache_entry *ce,1766enum unpack_trees_error_types error_type,1767struct unpack_trees_options *o)1768{1769if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1770return0;1771returnverify_absent_1(ce, error_type, o);1772}17731774static intverify_absent_sparse(const struct cache_entry *ce,1775enum unpack_trees_error_types error_type,1776struct unpack_trees_options *o)1777{1778enum unpack_trees_error_types orphaned_error = error_type;1779if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1780 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17811782returnverify_absent_1(ce, orphaned_error, o);1783}17841785static intmerged_entry(const struct cache_entry *ce,1786const struct cache_entry *old,1787struct unpack_trees_options *o)1788{1789int update = CE_UPDATE;1790struct cache_entry *merge =dup_entry(ce);17911792if(!old) {1793/*1794 * New index entries. In sparse checkout, the following1795 * verify_absent() will be delayed until after1796 * traverse_trees() finishes in unpack_trees(), then:1797 *1798 * - CE_NEW_SKIP_WORKTREE will be computed correctly1799 * - verify_absent() be called again, this time with1800 * correct CE_NEW_SKIP_WORKTREE1801 *1802 * verify_absent() call here does nothing in sparse1803 * checkout (i.e. o->skip_sparse_checkout == 0)1804 */1805 update |= CE_ADDED;1806 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;18071808if(verify_absent(merge,1809 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1810free(merge);1811return-1;1812}1813invalidate_ce_path(merge, o);18141815if(submodule_from_ce(ce)) {1816int ret =check_submodule_move_head(ce, NULL,1817oid_to_hex(&ce->oid),1818 o);1819if(ret)1820return ret;1821}18221823}else if(!(old->ce_flags & CE_CONFLICTED)) {1824/*1825 * See if we can re-use the old CE directly?1826 * That way we get the uptodate stat info.1827 *1828 * This also removes the UPDATE flag on a match; otherwise1829 * we will end up overwriting local changes in the work tree.1830 */1831if(same(old, merge)) {1832copy_cache_entry(merge, old);1833 update =0;1834}else{1835if(verify_uptodate(old, o)) {1836free(merge);1837return-1;1838}1839/* Migrate old flags over */1840 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1841invalidate_ce_path(old, o);1842}18431844if(submodule_from_ce(ce)) {1845int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1846oid_to_hex(&ce->oid),1847 o);1848if(ret)1849return ret;1850}1851}else{1852/*1853 * Previously unmerged entry left as an existence1854 * marker by read_index_unmerged();1855 */1856invalidate_ce_path(old, o);1857}18581859do_add_entry(o, merge, update, CE_STAGEMASK);1860return1;1861}18621863static intdeleted_entry(const struct cache_entry *ce,1864const struct cache_entry *old,1865struct unpack_trees_options *o)1866{1867/* Did it exist in the index? */1868if(!old) {1869if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1870return-1;1871return0;1872}1873if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1874return-1;1875add_entry(o, ce, CE_REMOVE,0);1876invalidate_ce_path(ce, o);1877return1;1878}18791880static intkeep_entry(const struct cache_entry *ce,1881struct unpack_trees_options *o)1882{1883add_entry(o, ce,0,0);1884return1;1885}18861887#if DBRT_DEBUG1888static voidshow_stage_entry(FILE*o,1889const char*label,const struct cache_entry *ce)1890{1891if(!ce)1892fprintf(o,"%s(missing)\n", label);1893else1894fprintf(o,"%s%06o%s %d\t%s\n",1895 label,1896 ce->ce_mode,1897oid_to_hex(&ce->oid),1898ce_stage(ce),1899 ce->name);1900}1901#endif19021903intthreeway_merge(const struct cache_entry *const*stages,1904struct unpack_trees_options *o)1905{1906const struct cache_entry *index;1907const struct cache_entry *head;1908const struct cache_entry *remote = stages[o->head_idx +1];1909int count;1910int head_match =0;1911int remote_match =0;19121913int df_conflict_head =0;1914int df_conflict_remote =0;19151916int any_anc_missing =0;1917int no_anc_exists =1;1918int i;19191920for(i =1; i < o->head_idx; i++) {1921if(!stages[i] || stages[i] == o->df_conflict_entry)1922 any_anc_missing =1;1923else1924 no_anc_exists =0;1925}19261927 index = stages[0];1928 head = stages[o->head_idx];19291930if(head == o->df_conflict_entry) {1931 df_conflict_head =1;1932 head = NULL;1933}19341935if(remote == o->df_conflict_entry) {1936 df_conflict_remote =1;1937 remote = NULL;1938}19391940/*1941 * First, if there's a #16 situation, note that to prevent #131942 * and #14.1943 */1944if(!same(remote, head)) {1945for(i =1; i < o->head_idx; i++) {1946if(same(stages[i], head)) {1947 head_match = i;1948}1949if(same(stages[i], remote)) {1950 remote_match = i;1951}1952}1953}19541955/*1956 * We start with cases where the index is allowed to match1957 * something other than the head: #14(ALT) and #2ALT, where it1958 * is permitted to match the result instead.1959 */1960/* #14, #14ALT, #2ALT */1961if(remote && !df_conflict_head && head_match && !remote_match) {1962if(index && !same(index, remote) && !same(index, head))1963returnreject_merge(index, o);1964returnmerged_entry(remote, index, o);1965}1966/*1967 * If we have an entry in the index cache, then we want to1968 * make sure that it matches head.1969 */1970if(index && !same(index, head))1971returnreject_merge(index, o);19721973if(head) {1974/* #5ALT, #15 */1975if(same(head, remote))1976returnmerged_entry(head, index, o);1977/* #13, #3ALT */1978if(!df_conflict_remote && remote_match && !head_match)1979returnmerged_entry(head, index, o);1980}19811982/* #1 */1983if(!head && !remote && any_anc_missing)1984return0;19851986/*1987 * Under the "aggressive" rule, we resolve mostly trivial1988 * cases that we historically had git-merge-one-file resolve.1989 */1990if(o->aggressive) {1991int head_deleted = !head;1992int remote_deleted = !remote;1993const struct cache_entry *ce = NULL;19941995if(index)1996 ce = index;1997else if(head)1998 ce = head;1999else if(remote)2000 ce = remote;2001else{2002for(i =1; i < o->head_idx; i++) {2003if(stages[i] && stages[i] != o->df_conflict_entry) {2004 ce = stages[i];2005break;2006}2007}2008}20092010/*2011 * Deleted in both.2012 * Deleted in one and unchanged in the other.2013 */2014if((head_deleted && remote_deleted) ||2015(head_deleted && remote && remote_match) ||2016(remote_deleted && head && head_match)) {2017if(index)2018returndeleted_entry(index, index, o);2019if(ce && !head_deleted) {2020if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2021return-1;2022}2023return0;2024}2025/*2026 * Added in both, identically.2027 */2028if(no_anc_exists && head && remote &&same(head, remote))2029returnmerged_entry(head, index, o);20302031}20322033/* Below are "no merge" cases, which require that the index be2034 * up-to-date to avoid the files getting overwritten with2035 * conflict resolution files.2036 */2037if(index) {2038if(verify_uptodate(index, o))2039return-1;2040}20412042 o->nontrivial_merge =1;20432044/* #2, #3, #4, #6, #7, #9, #10, #11. */2045 count =0;2046if(!head_match || !remote_match) {2047for(i =1; i < o->head_idx; i++) {2048if(stages[i] && stages[i] != o->df_conflict_entry) {2049keep_entry(stages[i], o);2050 count++;2051break;2052}2053}2054}2055#if DBRT_DEBUG2056else{2057fprintf(stderr,"read-tree: warning #16 detected\n");2058show_stage_entry(stderr,"head ", stages[head_match]);2059show_stage_entry(stderr,"remote ", stages[remote_match]);2060}2061#endif2062if(head) { count +=keep_entry(head, o); }2063if(remote) { count +=keep_entry(remote, o); }2064return count;2065}20662067/*2068 * Two-way merge.2069 *2070 * The rule is to "carry forward" what is in the index without losing2071 * information across a "fast-forward", favoring a successful merge2072 * over a merge failure when it makes sense. For details of the2073 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2074 *2075 */2076inttwoway_merge(const struct cache_entry *const*src,2077struct unpack_trees_options *o)2078{2079const struct cache_entry *current = src[0];2080const struct cache_entry *oldtree = src[1];2081const struct cache_entry *newtree = src[2];20822083if(o->merge_size !=2)2084returnerror("Cannot do a twoway merge of%dtrees",2085 o->merge_size);20862087if(oldtree == o->df_conflict_entry)2088 oldtree = NULL;2089if(newtree == o->df_conflict_entry)2090 newtree = NULL;20912092if(current) {2093if(current->ce_flags & CE_CONFLICTED) {2094if(same(oldtree, newtree) || o->reset) {2095if(!newtree)2096returndeleted_entry(current, current, o);2097else2098returnmerged_entry(newtree, current, o);2099}2100returnreject_merge(current, o);2101}else if((!oldtree && !newtree) ||/* 4 and 5 */2102(!oldtree && newtree &&2103same(current, newtree)) ||/* 6 and 7 */2104(oldtree && newtree &&2105same(oldtree, newtree)) ||/* 14 and 15 */2106(oldtree && newtree &&2107!same(oldtree, newtree) &&/* 18 and 19 */2108same(current, newtree))) {2109returnkeep_entry(current, o);2110}else if(oldtree && !newtree &&same(current, oldtree)) {2111/* 10 or 11 */2112returndeleted_entry(oldtree, current, o);2113}else if(oldtree && newtree &&2114same(current, oldtree) && !same(current, newtree)) {2115/* 20 or 21 */2116returnmerged_entry(newtree, current, o);2117}else2118returnreject_merge(current, o);2119}2120else if(newtree) {2121if(oldtree && !o->initial_checkout) {2122/*2123 * deletion of the path was staged;2124 */2125if(same(oldtree, newtree))2126return1;2127returnreject_merge(oldtree, o);2128}2129returnmerged_entry(newtree, current, o);2130}2131returndeleted_entry(oldtree, current, o);2132}21332134/*2135 * Bind merge.2136 *2137 * Keep the index entries at stage0, collapse stage1 but make sure2138 * stage0 does not have anything there.2139 */2140intbind_merge(const struct cache_entry *const*src,2141struct unpack_trees_options *o)2142{2143const struct cache_entry *old = src[0];2144const struct cache_entry *a = src[1];21452146if(o->merge_size !=1)2147returnerror("Cannot do a bind merge of%dtrees",2148 o->merge_size);2149if(a && old)2150return o->gently ? -1:2151error(ERRORMSG(o, ERROR_BIND_OVERLAP),2152super_prefixed(a->name),2153super_prefixed(old->name));2154if(!a)2155returnkeep_entry(old, o);2156else2157returnmerged_entry(a, NULL, o);2158}21592160/*2161 * One-way merge.2162 *2163 * The rule is:2164 * - take the stat information from stage0, take the data from stage12165 */2166intoneway_merge(const struct cache_entry *const*src,2167struct unpack_trees_options *o)2168{2169const struct cache_entry *old = src[0];2170const struct cache_entry *a = src[1];21712172if(o->merge_size !=1)2173returnerror("Cannot do a oneway merge of%dtrees",2174 o->merge_size);21752176if(!a || a == o->df_conflict_entry)2177returndeleted_entry(old, old, o);21782179if(old &&same(old, a)) {2180int update =0;2181if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2182struct stat st;2183if(lstat(old->name, &st) ||2184ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2185 update |= CE_UPDATE;2186}2187if(o->update &&S_ISGITLINK(old->ce_mode) &&2188should_update_submodules() && !verify_uptodate(old, o))2189 update |= CE_UPDATE;2190add_entry(o, old, update,0);2191return0;2192}2193returnmerged_entry(a, old, o);2194}