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 voidadd_entry(struct unpack_trees_options *o, 208const struct cache_entry *ce, 209unsigned int set,unsigned int clear) 210{ 211do_add_entry(o,dup_cache_entry(ce, &o->result), set, clear); 212} 213 214/* 215 * add error messages on path <path> 216 * corresponding to the type <e> with the message <msg> 217 * indicating if it should be display in porcelain or not 218 */ 219static intadd_rejected_path(struct unpack_trees_options *o, 220enum unpack_trees_error_types e, 221const char*path) 222{ 223if(!o->show_all_errors) 224returnerror(ERRORMSG(o, e),super_prefixed(path)); 225 226/* 227 * Otherwise, insert in a list for future display by 228 * display_error_msgs() 229 */ 230string_list_append(&o->unpack_rejects[e], path); 231return-1; 232} 233 234/* 235 * display all the error messages stored in a nice way 236 */ 237static voiddisplay_error_msgs(struct unpack_trees_options *o) 238{ 239int e, i; 240int something_displayed =0; 241for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 242struct string_list *rejects = &o->unpack_rejects[e]; 243if(rejects->nr >0) { 244struct strbuf path = STRBUF_INIT; 245 something_displayed =1; 246for(i =0; i < rejects->nr; i++) 247strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 248error(ERRORMSG(o, e),super_prefixed(path.buf)); 249strbuf_release(&path); 250} 251string_list_clear(rejects,0); 252} 253if(something_displayed) 254fprintf(stderr,_("Aborting\n")); 255} 256 257static intcheck_submodule_move_head(const struct cache_entry *ce, 258const char*old_id, 259const char*new_id, 260struct unpack_trees_options *o) 261{ 262unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 263const struct submodule *sub =submodule_from_ce(ce); 264 265if(!sub) 266return0; 267 268if(o->reset) 269 flags |= SUBMODULE_MOVE_HEAD_FORCE; 270 271if(submodule_move_head(ce->name, old_id, new_id, flags)) 272return o->gently ? -1: 273add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 274return0; 275} 276 277/* 278 * Preform the loading of the repository's gitmodules file. This function is 279 * used by 'check_update()' to perform loading of the gitmodules file in two 280 * differnt situations: 281 * (1) before removing entries from the working tree if the gitmodules file has 282 * been marked for removal. This situation is specified by 'state' == NULL. 283 * (2) before checking out entries to the working tree if the gitmodules file 284 * has been marked for update. This situation is specified by 'state' != NULL. 285 */ 286static voidload_gitmodules_file(struct index_state *index, 287struct checkout *state) 288{ 289int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 290 291if(pos >=0) { 292struct cache_entry *ce = index->cache[pos]; 293if(!state && ce->ce_flags & CE_WT_REMOVE) { 294repo_read_gitmodules(the_repository); 295}else if(state && (ce->ce_flags & CE_UPDATE)) { 296submodule_free(the_repository); 297checkout_entry(ce, state, NULL); 298repo_read_gitmodules(the_repository); 299} 300} 301} 302 303/* 304 * Unlink the last component and schedule the leading directories for 305 * removal, such that empty directories get removed. 306 */ 307static voidunlink_entry(const struct cache_entry *ce) 308{ 309const struct submodule *sub =submodule_from_ce(ce); 310if(sub) { 311/* state.force is set at the caller. */ 312submodule_move_head(ce->name,"HEAD", NULL, 313 SUBMODULE_MOVE_HEAD_FORCE); 314} 315if(!check_leading_path(ce->name,ce_namelen(ce))) 316return; 317if(remove_or_warn(ce->ce_mode, ce->name)) 318return; 319schedule_dir_for_removal(ce->name,ce_namelen(ce)); 320} 321 322static struct progress *get_progress(struct unpack_trees_options *o) 323{ 324unsigned cnt =0, total =0; 325struct index_state *index = &o->result; 326 327if(!o->update || !o->verbose_update) 328return NULL; 329 330for(; cnt < index->cache_nr; cnt++) { 331const struct cache_entry *ce = index->cache[cnt]; 332if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 333 total++; 334} 335 336returnstart_delayed_progress(_("Checking out files"), total); 337} 338 339static voidsetup_collided_checkout_detection(struct checkout *state, 340struct index_state *index) 341{ 342int i; 343 344 state->clone =1; 345for(i =0; i < index->cache_nr; i++) 346 index->cache[i]->ce_flags &= ~CE_MATCHED; 347} 348 349static voidreport_collided_checkout(struct index_state *index) 350{ 351struct string_list list = STRING_LIST_INIT_NODUP; 352int i; 353 354for(i =0; i < index->cache_nr; i++) { 355struct cache_entry *ce = index->cache[i]; 356 357if(!(ce->ce_flags & CE_MATCHED)) 358continue; 359 360string_list_append(&list, ce->name); 361 ce->ce_flags &= ~CE_MATCHED; 362} 363 364 list.cmp = fspathcmp; 365string_list_sort(&list); 366 367if(list.nr) { 368warning(_("the following paths have collided (e.g. case-sensitive paths\n" 369"on a case-insensitive filesystem) and only one from the same\n" 370"colliding group is in the working tree:\n")); 371 372for(i =0; i < list.nr; i++) 373fprintf(stderr," '%s'\n", list.items[i].string); 374} 375 376string_list_clear(&list,0); 377} 378 379static intcheck_updates(struct unpack_trees_options *o) 380{ 381unsigned cnt =0; 382int errs =0; 383struct progress *progress = NULL; 384struct index_state *index = &o->result; 385struct checkout state = CHECKOUT_INIT; 386int i; 387 388 state.force =1; 389 state.quiet =1; 390 state.refresh_cache =1; 391 state.istate = index; 392 393if(o->clone) 394setup_collided_checkout_detection(&state, index); 395 396 progress =get_progress(o); 397 398if(o->update) 399git_attr_set_direction(GIT_ATTR_CHECKOUT); 400 401if(should_update_submodules() && o->update && !o->dry_run) 402load_gitmodules_file(index, NULL); 403 404for(i =0; i < index->cache_nr; i++) { 405const struct cache_entry *ce = index->cache[i]; 406 407if(ce->ce_flags & CE_WT_REMOVE) { 408display_progress(progress, ++cnt); 409if(o->update && !o->dry_run) 410unlink_entry(ce); 411} 412} 413remove_marked_cache_entries(index); 414remove_scheduled_dirs(); 415 416if(should_update_submodules() && o->update && !o->dry_run) 417load_gitmodules_file(index, &state); 418 419enable_delayed_checkout(&state); 420if(repository_format_partial_clone && o->update && !o->dry_run) { 421/* 422 * Prefetch the objects that are to be checked out in the loop 423 * below. 424 */ 425struct oid_array to_fetch = OID_ARRAY_INIT; 426int fetch_if_missing_store = fetch_if_missing; 427 fetch_if_missing =0; 428for(i =0; i < index->cache_nr; i++) { 429struct cache_entry *ce = index->cache[i]; 430if((ce->ce_flags & CE_UPDATE) && 431!S_ISGITLINK(ce->ce_mode)) { 432if(!has_object_file(&ce->oid)) 433oid_array_append(&to_fetch, &ce->oid); 434} 435} 436if(to_fetch.nr) 437fetch_objects(repository_format_partial_clone, 438&to_fetch); 439 fetch_if_missing = fetch_if_missing_store; 440oid_array_clear(&to_fetch); 441} 442for(i =0; i < index->cache_nr; i++) { 443struct cache_entry *ce = index->cache[i]; 444 445if(ce->ce_flags & CE_UPDATE) { 446if(ce->ce_flags & CE_WT_REMOVE) 447BUG("both update and delete flags are set on%s", 448 ce->name); 449display_progress(progress, ++cnt); 450 ce->ce_flags &= ~CE_UPDATE; 451if(o->update && !o->dry_run) { 452 errs |=checkout_entry(ce, &state, NULL); 453} 454} 455} 456stop_progress(&progress); 457 errs |=finish_delayed_checkout(&state); 458if(o->update) 459git_attr_set_direction(GIT_ATTR_CHECKIN); 460 461if(o->clone) 462report_collided_checkout(index); 463 464return errs !=0; 465} 466 467static intverify_uptodate_sparse(const struct cache_entry *ce, 468struct unpack_trees_options *o); 469static intverify_absent_sparse(const struct cache_entry *ce, 470enum unpack_trees_error_types, 471struct unpack_trees_options *o); 472 473static intapply_sparse_checkout(struct index_state *istate, 474struct cache_entry *ce, 475struct unpack_trees_options *o) 476{ 477int was_skip_worktree =ce_skip_worktree(ce); 478 479if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 480 ce->ce_flags |= CE_SKIP_WORKTREE; 481else 482 ce->ce_flags &= ~CE_SKIP_WORKTREE; 483if(was_skip_worktree !=ce_skip_worktree(ce)) { 484 ce->ce_flags |= CE_UPDATE_IN_BASE; 485mark_fsmonitor_invalid(istate, ce); 486 istate->cache_changed |= CE_ENTRY_CHANGED; 487} 488 489/* 490 * if (!was_skip_worktree && !ce_skip_worktree()) { 491 * This is perfectly normal. Move on; 492 * } 493 */ 494 495/* 496 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 497 * area as a result of ce_skip_worktree() shortcuts in 498 * verify_absent() and verify_uptodate(). 499 * Make sure they don't modify worktree if they are already 500 * outside checkout area 501 */ 502if(was_skip_worktree &&ce_skip_worktree(ce)) { 503 ce->ce_flags &= ~CE_UPDATE; 504 505/* 506 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 507 * on to get that file removed from both index and worktree. 508 * If that file is already outside worktree area, don't 509 * bother remove it. 510 */ 511if(ce->ce_flags & CE_REMOVE) 512 ce->ce_flags &= ~CE_WT_REMOVE; 513} 514 515if(!was_skip_worktree &&ce_skip_worktree(ce)) { 516/* 517 * If CE_UPDATE is set, verify_uptodate() must be called already 518 * also stat info may have lost after merged_entry() so calling 519 * verify_uptodate() again may fail 520 */ 521if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 522return-1; 523 ce->ce_flags |= CE_WT_REMOVE; 524 ce->ce_flags &= ~CE_UPDATE; 525} 526if(was_skip_worktree && !ce_skip_worktree(ce)) { 527if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 528return-1; 529 ce->ce_flags |= CE_UPDATE; 530} 531return0; 532} 533 534staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 535struct unpack_trees_options *o) 536{ 537int ret = o->fn(src, o); 538if(ret >0) 539 ret =0; 540return ret; 541} 542 543static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 544{ 545 ce->ce_flags |= CE_UNPACKED; 546 547if(o->cache_bottom < o->src_index->cache_nr && 548 o->src_index->cache[o->cache_bottom] == ce) { 549int bottom = o->cache_bottom; 550while(bottom < o->src_index->cache_nr && 551 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 552 bottom++; 553 o->cache_bottom = bottom; 554} 555} 556 557static voidmark_all_ce_unused(struct index_state *index) 558{ 559int i; 560for(i =0; i < index->cache_nr; i++) 561 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 562} 563 564static intlocate_in_src_index(const struct cache_entry *ce, 565struct unpack_trees_options *o) 566{ 567struct index_state *index = o->src_index; 568int len =ce_namelen(ce); 569int pos =index_name_pos(index, ce->name, len); 570if(pos <0) 571 pos = -1- pos; 572return pos; 573} 574 575/* 576 * We call unpack_index_entry() with an unmerged cache entry 577 * only in diff-index, and it wants a single callback. Skip 578 * the other unmerged entry with the same name. 579 */ 580static voidmark_ce_used_same_name(struct cache_entry *ce, 581struct unpack_trees_options *o) 582{ 583struct index_state *index = o->src_index; 584int len =ce_namelen(ce); 585int pos; 586 587for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 588struct cache_entry *next = index->cache[pos]; 589if(len !=ce_namelen(next) || 590memcmp(ce->name, next->name, len)) 591break; 592mark_ce_used(next, o); 593} 594} 595 596static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 597{ 598const struct index_state *index = o->src_index; 599int pos = o->cache_bottom; 600 601while(pos < index->cache_nr) { 602struct cache_entry *ce = index->cache[pos]; 603if(!(ce->ce_flags & CE_UNPACKED)) 604return ce; 605 pos++; 606} 607return NULL; 608} 609 610static voidadd_same_unmerged(const struct cache_entry *ce, 611struct unpack_trees_options *o) 612{ 613struct index_state *index = o->src_index; 614int len =ce_namelen(ce); 615int pos =index_name_pos(index, ce->name, len); 616 617if(0<= pos) 618die("programming error in a caller of mark_ce_used_same_name"); 619for(pos = -pos -1; pos < index->cache_nr; pos++) { 620struct cache_entry *next = index->cache[pos]; 621if(len !=ce_namelen(next) || 622memcmp(ce->name, next->name, len)) 623break; 624add_entry(o, next,0,0); 625mark_ce_used(next, o); 626} 627} 628 629static intunpack_index_entry(struct cache_entry *ce, 630struct unpack_trees_options *o) 631{ 632const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 633int ret; 634 635 src[0] = ce; 636 637mark_ce_used(ce, o); 638if(ce_stage(ce)) { 639if(o->skip_unmerged) { 640add_entry(o, ce,0,0); 641return0; 642} 643} 644 ret =call_unpack_fn(src, o); 645if(ce_stage(ce)) 646mark_ce_used_same_name(ce, o); 647return ret; 648} 649 650static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 651 652static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 653{ 654struct unpack_trees_options *o = info->data; 655 656if(o->diff_index_cached) 657return; 658 o->cache_bottom = bottom; 659} 660 661static intswitch_cache_bottom(struct traverse_info *info) 662{ 663struct unpack_trees_options *o = info->data; 664int ret, pos; 665 666if(o->diff_index_cached) 667return0; 668 ret = o->cache_bottom; 669 pos =find_cache_pos(info->prev, &info->name); 670 671if(pos < -1) 672 o->cache_bottom = -2- pos; 673else if(pos <0) 674 o->cache_bottom = o->src_index->cache_nr; 675return ret; 676} 677 678staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 679{ 680return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 681} 682 683static inttraverse_trees_recursive(int n,unsigned long dirmask, 684unsigned long df_conflicts, 685struct name_entry *names, 686struct traverse_info *info) 687{ 688int i, ret, bottom; 689int nr_buf =0; 690struct tree_desc t[MAX_UNPACK_TREES]; 691void*buf[MAX_UNPACK_TREES]; 692struct traverse_info newinfo; 693struct name_entry *p; 694 695 p = names; 696while(!p->mode) 697 p++; 698 699 newinfo = *info; 700 newinfo.prev = info; 701 newinfo.pathspec = info->pathspec; 702 newinfo.name = *p; 703 newinfo.pathlen +=tree_entry_len(p) +1; 704 newinfo.df_conflicts |= df_conflicts; 705 706/* 707 * Fetch the tree from the ODB for each peer directory in the 708 * n commits. 709 * 710 * For 2- and 3-way traversals, we try to avoid hitting the 711 * ODB twice for the same OID. This should yield a nice speed 712 * up in checkouts and merges when the commits are similar. 713 * 714 * We don't bother doing the full O(n^2) search for larger n, 715 * because wider traversals don't happen that often and we 716 * avoid the search setup. 717 * 718 * When 2 peer OIDs are the same, we just copy the tree 719 * descriptor data. This implicitly borrows the buffer 720 * data from the earlier cell. 721 */ 722for(i =0; i < n; i++, dirmask >>=1) { 723if(i >0&&are_same_oid(&names[i], &names[i -1])) 724 t[i] = t[i -1]; 725else if(i >1&&are_same_oid(&names[i], &names[i -2])) 726 t[i] = t[i -2]; 727else{ 728const struct object_id *oid = NULL; 729if(dirmask &1) 730 oid = names[i].oid; 731 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 732} 733} 734 735 bottom =switch_cache_bottom(&newinfo); 736 ret =traverse_trees(n, t, &newinfo); 737restore_cache_bottom(&newinfo, bottom); 738 739for(i =0; i < nr_buf; i++) 740free(buf[i]); 741 742return ret; 743} 744 745/* 746 * Compare the traverse-path to the cache entry without actually 747 * having to generate the textual representation of the traverse 748 * path. 749 * 750 * NOTE! This *only* compares up to the size of the traverse path 751 * itself - the caller needs to do the final check for the cache 752 * entry having more data at the end! 753 */ 754static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 755{ 756int len, pathlen, ce_len; 757const char*ce_name; 758 759if(info->prev) { 760int cmp =do_compare_entry_piecewise(ce, info->prev, 761&info->name); 762if(cmp) 763return cmp; 764} 765 pathlen = info->pathlen; 766 ce_len =ce_namelen(ce); 767 768/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 769if(ce_len < pathlen) 770return-1; 771 772 ce_len -= pathlen; 773 ce_name = ce->name + pathlen; 774 775 len =tree_entry_len(n); 776returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 777} 778 779static intdo_compare_entry(const struct cache_entry *ce, 780const struct traverse_info *info, 781const struct name_entry *n) 782{ 783int len, pathlen, ce_len; 784const char*ce_name; 785int cmp; 786 787/* 788 * If we have not precomputed the traverse path, it is quicker 789 * to avoid doing so. But if we have precomputed it, 790 * it is quicker to use the precomputed version. 791 */ 792if(!info->traverse_path) 793returndo_compare_entry_piecewise(ce, info, n); 794 795 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 796if(cmp) 797return cmp; 798 799 pathlen = info->pathlen; 800 ce_len =ce_namelen(ce); 801 802if(ce_len < pathlen) 803return-1; 804 805 ce_len -= pathlen; 806 ce_name = ce->name + pathlen; 807 808 len =tree_entry_len(n); 809returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 810} 811 812static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 813{ 814int cmp =do_compare_entry(ce, info, n); 815if(cmp) 816return cmp; 817 818/* 819 * Even if the beginning compared identically, the ce should 820 * compare as bigger than a directory leading up to it! 821 */ 822returnce_namelen(ce) >traverse_path_len(info, n); 823} 824 825static intce_in_traverse_path(const struct cache_entry *ce, 826const struct traverse_info *info) 827{ 828if(!info->prev) 829return1; 830if(do_compare_entry(ce, info->prev, &info->name)) 831return0; 832/* 833 * If ce (blob) is the same name as the path (which is a tree 834 * we will be descending into), it won't be inside it. 835 */ 836return(info->pathlen <ce_namelen(ce)); 837} 838 839static struct cache_entry *create_ce_entry(const struct traverse_info *info, 840const struct name_entry *n, 841int stage, 842struct index_state *istate, 843int is_transient) 844{ 845int len =traverse_path_len(info, n); 846struct cache_entry *ce = 847 is_transient ? 848make_empty_transient_cache_entry(len) : 849make_empty_cache_entry(istate, len); 850 851 ce->ce_mode =create_ce_mode(n->mode); 852 ce->ce_flags =create_ce_flags(stage); 853 ce->ce_namelen = len; 854oidcpy(&ce->oid, n->oid); 855make_traverse_path(ce->name, info, n); 856 857return ce; 858} 859 860static intunpack_nondirectories(int n,unsigned long mask, 861unsigned long dirmask, 862struct cache_entry **src, 863const struct name_entry *names, 864const struct traverse_info *info) 865{ 866int i; 867struct unpack_trees_options *o = info->data; 868unsigned long conflicts = info->df_conflicts | dirmask; 869 870/* Do we have *only* directories? Nothing to do */ 871if(mask == dirmask && !src[0]) 872return0; 873 874/* 875 * Ok, we've filled in up to any potential index entry in src[0], 876 * now do the rest. 877 */ 878for(i =0; i < n; i++) { 879int stage; 880unsigned int bit =1ul<< i; 881if(conflicts & bit) { 882 src[i + o->merge] = o->df_conflict_entry; 883continue; 884} 885if(!(mask & bit)) 886continue; 887if(!o->merge) 888 stage =0; 889else if(i +1< o->head_idx) 890 stage =1; 891else if(i +1> o->head_idx) 892 stage =3; 893else 894 stage =2; 895 896/* 897 * If the merge bit is set, then the cache entries are 898 * discarded in the following block. In this case, 899 * construct "transient" cache_entries, as they are 900 * not stored in the index. otherwise construct the 901 * cache entry from the index aware logic. 902 */ 903 src[i + o->merge] =create_ce_entry(info, names + i, stage, &o->result, o->merge); 904} 905 906if(o->merge) { 907int rc =call_unpack_fn((const struct cache_entry *const*)src, 908 o); 909for(i =0; i < n; i++) { 910struct cache_entry *ce = src[i + o->merge]; 911if(ce != o->df_conflict_entry) 912discard_cache_entry(ce); 913} 914return rc; 915} 916 917for(i =0; i < n; i++) 918if(src[i] && src[i] != o->df_conflict_entry) 919if(do_add_entry(o, src[i],0,0)) 920return-1; 921 922return0; 923} 924 925static intunpack_failed(struct unpack_trees_options *o,const char*message) 926{ 927discard_index(&o->result); 928if(!o->gently && !o->exiting_early) { 929if(message) 930returnerror("%s", message); 931return-1; 932} 933return-1; 934} 935 936/* 937 * The tree traversal is looking at name p. If we have a matching entry, 938 * return it. If name p is a directory in the index, do not return 939 * anything, as we will want to match it when the traversal descends into 940 * the directory. 941 */ 942static intfind_cache_pos(struct traverse_info *info, 943const struct name_entry *p) 944{ 945int pos; 946struct unpack_trees_options *o = info->data; 947struct index_state *index = o->src_index; 948int pfxlen = info->pathlen; 949int p_len =tree_entry_len(p); 950 951for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 952const struct cache_entry *ce = index->cache[pos]; 953const char*ce_name, *ce_slash; 954int cmp, ce_len; 955 956if(ce->ce_flags & CE_UNPACKED) { 957/* 958 * cache_bottom entry is already unpacked, so 959 * we can never match it; don't check it 960 * again. 961 */ 962if(pos == o->cache_bottom) 963++o->cache_bottom; 964continue; 965} 966if(!ce_in_traverse_path(ce, info)) { 967/* 968 * Check if we can skip future cache checks 969 * (because we're already past all possible 970 * entries in the traverse path). 971 */ 972if(info->traverse_path) { 973if(strncmp(ce->name, info->traverse_path, 974 info->pathlen) >0) 975break; 976} 977continue; 978} 979 ce_name = ce->name + pfxlen; 980 ce_slash =strchr(ce_name,'/'); 981if(ce_slash) 982 ce_len = ce_slash - ce_name; 983else 984 ce_len =ce_namelen(ce) - pfxlen; 985 cmp =name_compare(p->path, p_len, ce_name, ce_len); 986/* 987 * Exact match; if we have a directory we need to 988 * delay returning it. 989 */ 990if(!cmp) 991return ce_slash ? -2- pos : pos; 992if(0< cmp) 993continue;/* keep looking */ 994/* 995 * ce_name sorts after p->path; could it be that we 996 * have files under p->path directory in the index? 997 * E.g. ce_name == "t-i", and p->path == "t"; we may 998 * have "t/a" in the index. 999 */1000if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&1001 ce_name[p_len] <'/')1002continue;/* keep looking */1003break;1004}1005return-1;1006}10071008static struct cache_entry *find_cache_entry(struct traverse_info *info,1009const struct name_entry *p)1010{1011int pos =find_cache_pos(info, p);1012struct unpack_trees_options *o = info->data;10131014if(0<= pos)1015return o->src_index->cache[pos];1016else1017return NULL;1018}10191020static voiddebug_path(struct traverse_info *info)1021{1022if(info->prev) {1023debug_path(info->prev);1024if(*info->prev->name.path)1025putchar('/');1026}1027printf("%s", info->name.path);1028}10291030static voiddebug_name_entry(int i,struct name_entry *n)1031{1032printf("ent#%d %06o%s\n", i,1033 n->path ? n->mode :0,1034 n->path ? n->path :"(missing)");1035}10361037static voiddebug_unpack_callback(int n,1038unsigned long mask,1039unsigned long dirmask,1040struct name_entry *names,1041struct traverse_info *info)1042{1043int i;1044printf("* unpack mask%lu, dirmask%lu, cnt%d",1045 mask, dirmask, n);1046debug_path(info);1047putchar('\n');1048for(i =0; i < n; i++)1049debug_name_entry(i, names + i);1050}10511052static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info)1053{1054struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, };1055struct unpack_trees_options *o = info->data;1056const struct name_entry *p = names;10571058/* Find first entry with a real name (we could use "mask" too) */1059while(!p->mode)1060 p++;10611062if(o->debug_unpack)1063debug_unpack_callback(n, mask, dirmask, names, info);10641065/* Are we supposed to look at the index too? */1066if(o->merge) {1067while(1) {1068int cmp;1069struct cache_entry *ce;10701071if(o->diff_index_cached)1072 ce =next_cache_entry(o);1073else1074 ce =find_cache_entry(info, p);10751076if(!ce)1077break;1078 cmp =compare_entry(ce, info, p);1079if(cmp <0) {1080if(unpack_index_entry(ce, o) <0)1081returnunpack_failed(o, NULL);1082continue;1083}1084if(!cmp) {1085if(ce_stage(ce)) {1086/*1087 * If we skip unmerged index1088 * entries, we'll skip this1089 * entry *and* the tree1090 * entries associated with it!1091 */1092if(o->skip_unmerged) {1093add_same_unmerged(ce, o);1094return mask;1095}1096}1097 src[0] = ce;1098}1099break;1100}1101}11021103if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1104return-1;11051106if(o->merge && src[0]) {1107if(ce_stage(src[0]))1108mark_ce_used_same_name(src[0], o);1109else1110mark_ce_used(src[0], o);1111}11121113/* Now handle any directories.. */1114if(dirmask) {1115/* special case: "diff-index --cached" looking at a tree */1116if(o->diff_index_cached &&1117 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1118int matches;1119 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1120 names, info);1121/*1122 * Everything under the name matches; skip the1123 * entire hierarchy. diff_index_cached codepath1124 * special cases D/F conflicts in such a way that1125 * it does not do any look-ahead, so this is safe.1126 */1127if(matches) {1128 o->cache_bottom += matches;1129return mask;1130}1131}11321133if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1134 names, info) <0)1135return-1;1136return mask;1137}11381139return mask;1140}11411142static intclear_ce_flags_1(struct index_state *istate,1143struct cache_entry **cache,int nr,1144struct strbuf *prefix,1145int select_mask,int clear_mask,1146struct exclude_list *el,int defval);11471148/* Whole directory matching */1149static intclear_ce_flags_dir(struct index_state *istate,1150struct cache_entry **cache,int nr,1151struct strbuf *prefix,1152char*basename,1153int select_mask,int clear_mask,1154struct exclude_list *el,int defval)1155{1156struct cache_entry **cache_end;1157int dtype = DT_DIR;1158int ret =is_excluded_from_list(prefix->buf, prefix->len,1159 basename, &dtype, el, istate);1160int rc;11611162strbuf_addch(prefix,'/');11631164/* If undecided, use matching result of parent dir in defval */1165if(ret <0)1166 ret = defval;11671168for(cache_end = cache; cache_end != cache + nr; cache_end++) {1169struct cache_entry *ce = *cache_end;1170if(strncmp(ce->name, prefix->buf, prefix->len))1171break;1172}11731174/*1175 * TODO: check el, if there are no patterns that may conflict1176 * with ret (iow, we know in advance the incl/excl1177 * decision for the entire directory), clear flag here without1178 * calling clear_ce_flags_1(). That function will call1179 * the expensive is_excluded_from_list() on every entry.1180 */1181 rc =clear_ce_flags_1(istate, cache, cache_end - cache,1182 prefix,1183 select_mask, clear_mask,1184 el, ret);1185strbuf_setlen(prefix, prefix->len -1);1186return rc;1187}11881189/*1190 * Traverse the index, find every entry that matches according to1191 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1192 * number of traversed entries.1193 *1194 * If select_mask is non-zero, only entries whose ce_flags has on of1195 * those bits enabled are traversed.1196 *1197 * cache : pointer to an index entry1198 * prefix_len : an offset to its path1199 *1200 * The current path ("prefix") including the trailing '/' is1201 * cache[0]->name[0..(prefix_len-1)]1202 * Top level path has prefix_len zero.1203 */1204static intclear_ce_flags_1(struct index_state *istate,1205struct cache_entry **cache,int nr,1206struct strbuf *prefix,1207int select_mask,int clear_mask,1208struct exclude_list *el,int defval)1209{1210struct cache_entry **cache_end = cache + nr;12111212/*1213 * Process all entries that have the given prefix and meet1214 * select_mask condition1215 */1216while(cache != cache_end) {1217struct cache_entry *ce = *cache;1218const char*name, *slash;1219int len, dtype, ret;12201221if(select_mask && !(ce->ce_flags & select_mask)) {1222 cache++;1223continue;1224}12251226if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1227break;12281229 name = ce->name + prefix->len;1230 slash =strchr(name,'/');12311232/* If it's a directory, try whole directory match first */1233if(slash) {1234int processed;12351236 len = slash - name;1237strbuf_add(prefix, name, len);12381239 processed =clear_ce_flags_dir(istate, cache, cache_end - cache,1240 prefix,1241 prefix->buf + prefix->len - len,1242 select_mask, clear_mask,1243 el, defval);12441245/* clear_c_f_dir eats a whole dir already? */1246if(processed) {1247 cache += processed;1248strbuf_setlen(prefix, prefix->len - len);1249continue;1250}12511252strbuf_addch(prefix,'/');1253 cache +=clear_ce_flags_1(istate, cache, cache_end - cache,1254 prefix,1255 select_mask, clear_mask, el, defval);1256strbuf_setlen(prefix, prefix->len - len -1);1257continue;1258}12591260/* Non-directory */1261 dtype =ce_to_dtype(ce);1262 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1263 name, &dtype, el, istate);1264if(ret <0)1265 ret = defval;1266if(ret >0)1267 ce->ce_flags &= ~clear_mask;1268 cache++;1269}1270return nr - (cache_end - cache);1271}12721273static intclear_ce_flags(struct index_state *istate,1274int select_mask,int clear_mask,1275struct exclude_list *el)1276{1277static struct strbuf prefix = STRBUF_INIT;12781279strbuf_reset(&prefix);12801281returnclear_ce_flags_1(istate,1282 istate->cache,1283 istate->cache_nr,1284&prefix,1285 select_mask, clear_mask,1286 el,0);1287}12881289/*1290 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1291 */1292static voidmark_new_skip_worktree(struct exclude_list *el,1293struct index_state *istate,1294int select_flag,int skip_wt_flag)1295{1296int i;12971298/*1299 * 1. Pretend the narrowest worktree: only unmerged entries1300 * are checked out1301 */1302for(i =0; i < istate->cache_nr; i++) {1303struct cache_entry *ce = istate->cache[i];13041305if(select_flag && !(ce->ce_flags & select_flag))1306continue;13071308if(!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))1309 ce->ce_flags |= skip_wt_flag;1310else1311 ce->ce_flags &= ~skip_wt_flag;1312}13131314/*1315 * 2. Widen worktree according to sparse-checkout file.1316 * Matched entries will have skip_wt_flag cleared (i.e. "in")1317 */1318clear_ce_flags(istate, select_flag, skip_wt_flag, el);1319}13201321static intverify_absent(const struct cache_entry *,1322enum unpack_trees_error_types,1323struct unpack_trees_options *);1324/*1325 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1326 * resulting index, -2 on failure to reflect the changes to the work tree.1327 *1328 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1329 */1330intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1331{1332int i, ret;1333static struct cache_entry *dfc;1334struct exclude_list el;13351336if(len > MAX_UNPACK_TREES)1337die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);13381339memset(&el,0,sizeof(el));1340if(!core_apply_sparse_checkout || !o->update)1341 o->skip_sparse_checkout =1;1342if(!o->skip_sparse_checkout) {1343char*sparse =git_pathdup("info/sparse-checkout");1344if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1345 o->skip_sparse_checkout =1;1346else1347 o->el = ⪙1348free(sparse);1349}13501351memset(&o->result,0,sizeof(o->result));1352 o->result.initialized =1;1353 o->result.timestamp.sec = o->src_index->timestamp.sec;1354 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1355 o->result.version = o->src_index->version;1356if(!o->src_index->split_index) {1357 o->result.split_index = NULL;1358}else if(o->src_index == o->dst_index) {1359/*1360 * o->dst_index (and thus o->src_index) will be discarded1361 * and overwritten with o->result at the end of this function,1362 * so just use src_index's split_index to avoid having to1363 * create a new one.1364 */1365 o->result.split_index = o->src_index->split_index;1366 o->result.split_index->refcount++;1367}else{1368 o->result.split_index =init_split_index(&o->result);1369}1370oidcpy(&o->result.oid, &o->src_index->oid);1371 o->merge_size = len;1372mark_all_ce_unused(o->src_index);13731374/*1375 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1376 */1377if(!o->skip_sparse_checkout)1378mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);13791380if(!dfc)1381 dfc =xcalloc(1,cache_entry_size(0));1382 o->df_conflict_entry = dfc;13831384if(len) {1385const char*prefix = o->prefix ? o->prefix :"";1386struct traverse_info info;13871388setup_traverse_info(&info, prefix);1389 info.fn = unpack_callback;1390 info.data = o;1391 info.show_all_errors = o->show_all_errors;1392 info.pathspec = o->pathspec;13931394if(o->prefix) {1395/*1396 * Unpack existing index entries that sort before the1397 * prefix the tree is spliced into. Note that o->merge1398 * is always true in this case.1399 */1400while(1) {1401struct cache_entry *ce =next_cache_entry(o);1402if(!ce)1403break;1404if(ce_in_traverse_path(ce, &info))1405break;1406if(unpack_index_entry(ce, o) <0)1407goto return_failed;1408}1409}14101411if(traverse_trees(len, t, &info) <0)1412goto return_failed;1413}14141415/* Any left-over entries in the index? */1416if(o->merge) {1417while(1) {1418struct cache_entry *ce =next_cache_entry(o);1419if(!ce)1420break;1421if(unpack_index_entry(ce, o) <0)1422goto return_failed;1423}1424}1425mark_all_ce_unused(o->src_index);14261427if(o->trivial_merges_only && o->nontrivial_merge) {1428 ret =unpack_failed(o,"Merge requires file-level merging");1429goto done;1430}14311432if(!o->skip_sparse_checkout) {1433int empty_worktree =1;14341435/*1436 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11437 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1438 * so apply_sparse_checkout() won't attempt to remove it from worktree1439 */1440mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);14411442 ret =0;1443for(i =0; i < o->result.cache_nr; i++) {1444struct cache_entry *ce = o->result.cache[i];14451446/*1447 * Entries marked with CE_ADDED in merged_entry() do not have1448 * verify_absent() check (the check is effectively disabled1449 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1450 *1451 * Do the real check now because we have had1452 * correct CE_NEW_SKIP_WORKTREE1453 */1454if(ce->ce_flags & CE_ADDED &&1455verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1456if(!o->show_all_errors)1457goto return_failed;1458 ret = -1;1459}14601461if(apply_sparse_checkout(&o->result, ce, o)) {1462if(!o->show_all_errors)1463goto return_failed;1464 ret = -1;1465}1466if(!ce_skip_worktree(ce))1467 empty_worktree =0;14681469}1470if(ret <0)1471goto return_failed;1472/*1473 * Sparse checkout is meant to narrow down checkout area1474 * but it does not make sense to narrow down to empty working1475 * tree. This is usually a mistake in sparse checkout rules.1476 * Do not allow users to do that.1477 */1478if(o->result.cache_nr && empty_worktree) {1479 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1480goto done;1481}1482}14831484 ret =check_updates(o) ? (-2) :0;1485if(o->dst_index) {1486if(!ret) {1487if(!o->result.cache_tree)1488 o->result.cache_tree =cache_tree();1489if(!cache_tree_fully_valid(o->result.cache_tree))1490cache_tree_update(&o->result,1491 WRITE_TREE_SILENT |1492 WRITE_TREE_REPAIR);1493}1494move_index_extensions(&o->result, o->src_index);1495discard_index(o->dst_index);1496*o->dst_index = o->result;1497}else{1498discard_index(&o->result);1499}1500 o->src_index = NULL;15011502done:1503clear_exclude_list(&el);1504return ret;15051506return_failed:1507if(o->show_all_errors)1508display_error_msgs(o);1509mark_all_ce_unused(o->src_index);1510 ret =unpack_failed(o, NULL);1511if(o->exiting_early)1512 ret =0;1513goto done;1514}15151516/* Here come the merge functions */15171518static intreject_merge(const struct cache_entry *ce,1519struct unpack_trees_options *o)1520{1521return o->gently ? -1:1522add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1523}15241525static intsame(const struct cache_entry *a,const struct cache_entry *b)1526{1527if(!!a != !!b)1528return0;1529if(!a && !b)1530return1;1531if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1532return0;1533return a->ce_mode == b->ce_mode &&1534!oidcmp(&a->oid, &b->oid);1535}153615371538/*1539 * When a CE gets turned into an unmerged entry, we1540 * want it to be up-to-date1541 */1542static intverify_uptodate_1(const struct cache_entry *ce,1543struct unpack_trees_options *o,1544enum unpack_trees_error_types error_type)1545{1546struct stat st;15471548if(o->index_only)1549return0;15501551/*1552 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1553 * if this entry is truly up-to-date because this file may be1554 * overwritten.1555 */1556if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1557;/* keep checking */1558else if(o->reset ||ce_uptodate(ce))1559return0;15601561if(!lstat(ce->name, &st)) {1562int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1563unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);15641565if(submodule_from_ce(ce)) {1566int r =check_submodule_move_head(ce,1567"HEAD",oid_to_hex(&ce->oid), o);1568if(r)1569return o->gently ? -1:1570add_rejected_path(o, error_type, ce->name);1571return0;1572}15731574if(!changed)1575return0;1576/*1577 * Historic default policy was to allow submodule to be out1578 * of sync wrt the superproject index. If the submodule was1579 * not considered interesting above, we don't care here.1580 */1581if(S_ISGITLINK(ce->ce_mode))1582return0;15831584 errno =0;1585}1586if(errno == ENOENT)1587return0;1588return o->gently ? -1:1589add_rejected_path(o, error_type, ce->name);1590}15911592intverify_uptodate(const struct cache_entry *ce,1593struct unpack_trees_options *o)1594{1595if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1596return0;1597returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1598}15991600static intverify_uptodate_sparse(const struct cache_entry *ce,1601struct unpack_trees_options *o)1602{1603returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1604}16051606/*1607 * TODO: We should actually invalidate o->result, not src_index [1].1608 * But since cache tree and untracked cache both are not copied to1609 * o->result until unpacking is complete, we invalidate them on1610 * src_index instead with the assumption that they will be copied to1611 * dst_index at the end.1612 *1613 * [1] src_index->cache_tree is also used in unpack_callback() so if1614 * we invalidate o->result, we need to update it to use1615 * o->result.cache_tree as well.1616 */1617static voidinvalidate_ce_path(const struct cache_entry *ce,1618struct unpack_trees_options *o)1619{1620if(!ce)1621return;1622cache_tree_invalidate_path(o->src_index, ce->name);1623untracked_cache_invalidate_path(o->src_index, ce->name,1);1624}16251626/*1627 * Check that checking out ce->sha1 in subdir ce->name is not1628 * going to overwrite any working files.1629 *1630 * Currently, git does not checkout subprojects during a superproject1631 * checkout, so it is not going to overwrite anything.1632 */1633static intverify_clean_submodule(const char*old_sha1,1634const struct cache_entry *ce,1635enum unpack_trees_error_types error_type,1636struct unpack_trees_options *o)1637{1638if(!submodule_from_ce(ce))1639return0;16401641returncheck_submodule_move_head(ce, old_sha1,1642oid_to_hex(&ce->oid), o);1643}16441645static intverify_clean_subdirectory(const struct cache_entry *ce,1646enum unpack_trees_error_types error_type,1647struct unpack_trees_options *o)1648{1649/*1650 * we are about to extract "ce->name"; we would not want to lose1651 * anything in the existing directory there.1652 */1653int namelen;1654int i;1655struct dir_struct d;1656char*pathbuf;1657int cnt =0;16581659if(S_ISGITLINK(ce->ce_mode)) {1660struct object_id oid;1661int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1662/*1663 * If we are not going to update the submodule, then1664 * we don't care.1665 */1666if(!sub_head && !oidcmp(&oid, &ce->oid))1667return0;1668returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1669 ce, error_type, o);1670}16711672/*1673 * First let's make sure we do not have a local modification1674 * in that directory.1675 */1676 namelen =ce_namelen(ce);1677for(i =locate_in_src_index(ce, o);1678 i < o->src_index->cache_nr;1679 i++) {1680struct cache_entry *ce2 = o->src_index->cache[i];1681int len =ce_namelen(ce2);1682if(len < namelen ||1683strncmp(ce->name, ce2->name, namelen) ||1684 ce2->name[namelen] !='/')1685break;1686/*1687 * ce2->name is an entry in the subdirectory to be1688 * removed.1689 */1690if(!ce_stage(ce2)) {1691if(verify_uptodate(ce2, o))1692return-1;1693add_entry(o, ce2, CE_REMOVE,0);1694mark_ce_used(ce2, o);1695}1696 cnt++;1697}16981699/*1700 * Then we need to make sure that we do not lose a locally1701 * present file that is not ignored.1702 */1703 pathbuf =xstrfmt("%.*s/", namelen, ce->name);17041705memset(&d,0,sizeof(d));1706if(o->dir)1707 d.exclude_per_dir = o->dir->exclude_per_dir;1708 i =read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);1709if(i)1710return o->gently ? -1:1711add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1712free(pathbuf);1713return cnt;1714}17151716/*1717 * This gets called when there was no index entry for the tree entry 'dst',1718 * but we found a file in the working tree that 'lstat()' said was fine,1719 * and we're on a case-insensitive filesystem.1720 *1721 * See if we can find a case-insensitive match in the index that also1722 * matches the stat information, and assume it's that other file!1723 */1724static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1725{1726const struct cache_entry *src;17271728 src =index_file_exists(o->src_index, name, len,1);1729return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1730}17311732static intcheck_ok_to_remove(const char*name,int len,int dtype,1733const struct cache_entry *ce,struct stat *st,1734enum unpack_trees_error_types error_type,1735struct unpack_trees_options *o)1736{1737const struct cache_entry *result;17381739/*1740 * It may be that the 'lstat()' succeeded even though1741 * target 'ce' was absent, because there is an old1742 * entry that is different only in case..1743 *1744 * Ignore that lstat() if it matches.1745 */1746if(ignore_case &&icase_exists(o, name, len, st))1747return0;17481749if(o->dir &&1750is_excluded(o->dir, o->src_index, name, &dtype))1751/*1752 * ce->name is explicitly excluded, so it is Ok to1753 * overwrite it.1754 */1755return0;1756if(S_ISDIR(st->st_mode)) {1757/*1758 * We are checking out path "foo" and1759 * found "foo/." in the working tree.1760 * This is tricky -- if we have modified1761 * files that are in "foo/" we would lose1762 * them.1763 */1764if(verify_clean_subdirectory(ce, error_type, o) <0)1765return-1;1766return0;1767}17681769/*1770 * The previous round may already have decided to1771 * delete this path, which is in a subdirectory that1772 * is being replaced with a blob.1773 */1774 result =index_file_exists(&o->result, name, len,0);1775if(result) {1776if(result->ce_flags & CE_REMOVE)1777return0;1778}17791780return o->gently ? -1:1781add_rejected_path(o, error_type, name);1782}17831784/*1785 * We do not want to remove or overwrite a working tree file that1786 * is not tracked, unless it is ignored.1787 */1788static intverify_absent_1(const struct cache_entry *ce,1789enum unpack_trees_error_types error_type,1790struct unpack_trees_options *o)1791{1792int len;1793struct stat st;17941795if(o->index_only || o->reset || !o->update)1796return0;17971798 len =check_leading_path(ce->name,ce_namelen(ce));1799if(!len)1800return0;1801else if(len >0) {1802char*path;1803int ret;18041805 path =xmemdupz(ce->name, len);1806if(lstat(path, &st))1807 ret =error_errno("cannot stat '%s'", path);1808else{1809if(submodule_from_ce(ce))1810 ret =check_submodule_move_head(ce,1811oid_to_hex(&ce->oid),1812 NULL, o);1813else1814 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1815&st, error_type, o);1816}1817free(path);1818return ret;1819}else if(lstat(ce->name, &st)) {1820if(errno != ENOENT)1821returnerror_errno("cannot stat '%s'", ce->name);1822return0;1823}else{1824if(submodule_from_ce(ce))1825returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1826 NULL, o);18271828returncheck_ok_to_remove(ce->name,ce_namelen(ce),1829ce_to_dtype(ce), ce, &st,1830 error_type, o);1831}1832}18331834static intverify_absent(const struct cache_entry *ce,1835enum unpack_trees_error_types error_type,1836struct unpack_trees_options *o)1837{1838if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1839return0;1840returnverify_absent_1(ce, error_type, o);1841}18421843static intverify_absent_sparse(const struct cache_entry *ce,1844enum unpack_trees_error_types error_type,1845struct unpack_trees_options *o)1846{1847enum unpack_trees_error_types orphaned_error = error_type;1848if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1849 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;18501851returnverify_absent_1(ce, orphaned_error, o);1852}18531854static intmerged_entry(const struct cache_entry *ce,1855const struct cache_entry *old,1856struct unpack_trees_options *o)1857{1858int update = CE_UPDATE;1859struct cache_entry *merge =dup_cache_entry(ce, &o->result);18601861if(!old) {1862/*1863 * New index entries. In sparse checkout, the following1864 * verify_absent() will be delayed until after1865 * traverse_trees() finishes in unpack_trees(), then:1866 *1867 * - CE_NEW_SKIP_WORKTREE will be computed correctly1868 * - verify_absent() be called again, this time with1869 * correct CE_NEW_SKIP_WORKTREE1870 *1871 * verify_absent() call here does nothing in sparse1872 * checkout (i.e. o->skip_sparse_checkout == 0)1873 */1874 update |= CE_ADDED;1875 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;18761877if(verify_absent(merge,1878 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1879discard_cache_entry(merge);1880return-1;1881}1882invalidate_ce_path(merge, o);18831884if(submodule_from_ce(ce)) {1885int ret =check_submodule_move_head(ce, NULL,1886oid_to_hex(&ce->oid),1887 o);1888if(ret)1889return ret;1890}18911892}else if(!(old->ce_flags & CE_CONFLICTED)) {1893/*1894 * See if we can re-use the old CE directly?1895 * That way we get the uptodate stat info.1896 *1897 * This also removes the UPDATE flag on a match; otherwise1898 * we will end up overwriting local changes in the work tree.1899 */1900if(same(old, merge)) {1901copy_cache_entry(merge, old);1902 update =0;1903}else{1904if(verify_uptodate(old, o)) {1905discard_cache_entry(merge);1906return-1;1907}1908/* Migrate old flags over */1909 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1910invalidate_ce_path(old, o);1911}19121913if(submodule_from_ce(ce)) {1914int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1915oid_to_hex(&ce->oid),1916 o);1917if(ret)1918return ret;1919}1920}else{1921/*1922 * Previously unmerged entry left as an existence1923 * marker by read_index_unmerged();1924 */1925invalidate_ce_path(old, o);1926}19271928do_add_entry(o, merge, update, CE_STAGEMASK);1929return1;1930}19311932static intdeleted_entry(const struct cache_entry *ce,1933const struct cache_entry *old,1934struct unpack_trees_options *o)1935{1936/* Did it exist in the index? */1937if(!old) {1938if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1939return-1;1940return0;1941}1942if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1943return-1;1944add_entry(o, ce, CE_REMOVE,0);1945invalidate_ce_path(ce, o);1946return1;1947}19481949static intkeep_entry(const struct cache_entry *ce,1950struct unpack_trees_options *o)1951{1952add_entry(o, ce,0,0);1953return1;1954}19551956#if DBRT_DEBUG1957static voidshow_stage_entry(FILE*o,1958const char*label,const struct cache_entry *ce)1959{1960if(!ce)1961fprintf(o,"%s(missing)\n", label);1962else1963fprintf(o,"%s%06o%s %d\t%s\n",1964 label,1965 ce->ce_mode,1966oid_to_hex(&ce->oid),1967ce_stage(ce),1968 ce->name);1969}1970#endif19711972intthreeway_merge(const struct cache_entry *const*stages,1973struct unpack_trees_options *o)1974{1975const struct cache_entry *index;1976const struct cache_entry *head;1977const struct cache_entry *remote = stages[o->head_idx +1];1978int count;1979int head_match =0;1980int remote_match =0;19811982int df_conflict_head =0;1983int df_conflict_remote =0;19841985int any_anc_missing =0;1986int no_anc_exists =1;1987int i;19881989for(i =1; i < o->head_idx; i++) {1990if(!stages[i] || stages[i] == o->df_conflict_entry)1991 any_anc_missing =1;1992else1993 no_anc_exists =0;1994}19951996 index = stages[0];1997 head = stages[o->head_idx];19981999if(head == o->df_conflict_entry) {2000 df_conflict_head =1;2001 head = NULL;2002}20032004if(remote == o->df_conflict_entry) {2005 df_conflict_remote =1;2006 remote = NULL;2007}20082009/*2010 * First, if there's a #16 situation, note that to prevent #132011 * and #14.2012 */2013if(!same(remote, head)) {2014for(i =1; i < o->head_idx; i++) {2015if(same(stages[i], head)) {2016 head_match = i;2017}2018if(same(stages[i], remote)) {2019 remote_match = i;2020}2021}2022}20232024/*2025 * We start with cases where the index is allowed to match2026 * something other than the head: #14(ALT) and #2ALT, where it2027 * is permitted to match the result instead.2028 */2029/* #14, #14ALT, #2ALT */2030if(remote && !df_conflict_head && head_match && !remote_match) {2031if(index && !same(index, remote) && !same(index, head))2032returnreject_merge(index, o);2033returnmerged_entry(remote, index, o);2034}2035/*2036 * If we have an entry in the index cache, then we want to2037 * make sure that it matches head.2038 */2039if(index && !same(index, head))2040returnreject_merge(index, o);20412042if(head) {2043/* #5ALT, #15 */2044if(same(head, remote))2045returnmerged_entry(head, index, o);2046/* #13, #3ALT */2047if(!df_conflict_remote && remote_match && !head_match)2048returnmerged_entry(head, index, o);2049}20502051/* #1 */2052if(!head && !remote && any_anc_missing)2053return0;20542055/*2056 * Under the "aggressive" rule, we resolve mostly trivial2057 * cases that we historically had git-merge-one-file resolve.2058 */2059if(o->aggressive) {2060int head_deleted = !head;2061int remote_deleted = !remote;2062const struct cache_entry *ce = NULL;20632064if(index)2065 ce = index;2066else if(head)2067 ce = head;2068else if(remote)2069 ce = remote;2070else{2071for(i =1; i < o->head_idx; i++) {2072if(stages[i] && stages[i] != o->df_conflict_entry) {2073 ce = stages[i];2074break;2075}2076}2077}20782079/*2080 * Deleted in both.2081 * Deleted in one and unchanged in the other.2082 */2083if((head_deleted && remote_deleted) ||2084(head_deleted && remote && remote_match) ||2085(remote_deleted && head && head_match)) {2086if(index)2087returndeleted_entry(index, index, o);2088if(ce && !head_deleted) {2089if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2090return-1;2091}2092return0;2093}2094/*2095 * Added in both, identically.2096 */2097if(no_anc_exists && head && remote &&same(head, remote))2098returnmerged_entry(head, index, o);20992100}21012102/* Below are "no merge" cases, which require that the index be2103 * up-to-date to avoid the files getting overwritten with2104 * conflict resolution files.2105 */2106if(index) {2107if(verify_uptodate(index, o))2108return-1;2109}21102111 o->nontrivial_merge =1;21122113/* #2, #3, #4, #6, #7, #9, #10, #11. */2114 count =0;2115if(!head_match || !remote_match) {2116for(i =1; i < o->head_idx; i++) {2117if(stages[i] && stages[i] != o->df_conflict_entry) {2118keep_entry(stages[i], o);2119 count++;2120break;2121}2122}2123}2124#if DBRT_DEBUG2125else{2126fprintf(stderr,"read-tree: warning #16 detected\n");2127show_stage_entry(stderr,"head ", stages[head_match]);2128show_stage_entry(stderr,"remote ", stages[remote_match]);2129}2130#endif2131if(head) { count +=keep_entry(head, o); }2132if(remote) { count +=keep_entry(remote, o); }2133return count;2134}21352136/*2137 * Two-way merge.2138 *2139 * The rule is to "carry forward" what is in the index without losing2140 * information across a "fast-forward", favoring a successful merge2141 * over a merge failure when it makes sense. For details of the2142 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2143 *2144 */2145inttwoway_merge(const struct cache_entry *const*src,2146struct unpack_trees_options *o)2147{2148const struct cache_entry *current = src[0];2149const struct cache_entry *oldtree = src[1];2150const struct cache_entry *newtree = src[2];21512152if(o->merge_size !=2)2153returnerror("Cannot do a twoway merge of%dtrees",2154 o->merge_size);21552156if(oldtree == o->df_conflict_entry)2157 oldtree = NULL;2158if(newtree == o->df_conflict_entry)2159 newtree = NULL;21602161if(current) {2162if(current->ce_flags & CE_CONFLICTED) {2163if(same(oldtree, newtree) || o->reset) {2164if(!newtree)2165returndeleted_entry(current, current, o);2166else2167returnmerged_entry(newtree, current, o);2168}2169returnreject_merge(current, o);2170}else if((!oldtree && !newtree) ||/* 4 and 5 */2171(!oldtree && newtree &&2172same(current, newtree)) ||/* 6 and 7 */2173(oldtree && newtree &&2174same(oldtree, newtree)) ||/* 14 and 15 */2175(oldtree && newtree &&2176!same(oldtree, newtree) &&/* 18 and 19 */2177same(current, newtree))) {2178returnkeep_entry(current, o);2179}else if(oldtree && !newtree &&same(current, oldtree)) {2180/* 10 or 11 */2181returndeleted_entry(oldtree, current, o);2182}else if(oldtree && newtree &&2183same(current, oldtree) && !same(current, newtree)) {2184/* 20 or 21 */2185returnmerged_entry(newtree, current, o);2186}else2187returnreject_merge(current, o);2188}2189else if(newtree) {2190if(oldtree && !o->initial_checkout) {2191/*2192 * deletion of the path was staged;2193 */2194if(same(oldtree, newtree))2195return1;2196returnreject_merge(oldtree, o);2197}2198returnmerged_entry(newtree, current, o);2199}2200returndeleted_entry(oldtree, current, o);2201}22022203/*2204 * Bind merge.2205 *2206 * Keep the index entries at stage0, collapse stage1 but make sure2207 * stage0 does not have anything there.2208 */2209intbind_merge(const struct cache_entry *const*src,2210struct unpack_trees_options *o)2211{2212const struct cache_entry *old = src[0];2213const struct cache_entry *a = src[1];22142215if(o->merge_size !=1)2216returnerror("Cannot do a bind merge of%dtrees",2217 o->merge_size);2218if(a && old)2219return o->gently ? -1:2220error(ERRORMSG(o, ERROR_BIND_OVERLAP),2221super_prefixed(a->name),2222super_prefixed(old->name));2223if(!a)2224returnkeep_entry(old, o);2225else2226returnmerged_entry(a, NULL, o);2227}22282229/*2230 * One-way merge.2231 *2232 * The rule is:2233 * - take the stat information from stage0, take the data from stage12234 */2235intoneway_merge(const struct cache_entry *const*src,2236struct unpack_trees_options *o)2237{2238const struct cache_entry *old = src[0];2239const struct cache_entry *a = src[1];22402241if(o->merge_size !=1)2242returnerror("Cannot do a oneway merge of%dtrees",2243 o->merge_size);22442245if(!a || a == o->df_conflict_entry)2246returndeleted_entry(old, old, o);22472248if(old &&same(old, a)) {2249int update =0;2250if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2251struct stat st;2252if(lstat(old->name, &st) ||2253ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2254 update |= CE_UPDATE;2255}2256if(o->update &&S_ISGITLINK(old->ce_mode) &&2257should_update_submodules() && !verify_uptodate(old, o))2258 update |= CE_UPDATE;2259add_entry(o, old, update,0);2260return0;2261}2262returnmerged_entry(a, old, o);2263}