1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"repository.h" 4#include"config.h" 5#include"dir.h" 6#include"tree.h" 7#include"tree-walk.h" 8#include"cache-tree.h" 9#include"unpack-trees.h" 10#include"progress.h" 11#include"refs.h" 12#include"attr.h" 13#include"split-index.h" 14#include"dir.h" 15#include"submodule.h" 16#include"submodule-config.h" 17#include"fsmonitor.h" 18 19/* 20 * Error messages expected by scripts out of plumbing commands such as 21 * read-tree. Non-scripted Porcelain is not required to use these messages 22 * and in fact are encouraged to reword them to better suit their particular 23 * situation better. See how "git checkout" and "git merge" replaces 24 * them using setup_unpack_trees_porcelain(), for example. 25 */ 26static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 27/* ERROR_WOULD_OVERWRITE */ 28"Entry '%s' would be overwritten by merge. Cannot merge.", 29 30/* ERROR_NOT_UPTODATE_FILE */ 31"Entry '%s' not uptodate. Cannot merge.", 32 33/* ERROR_NOT_UPTODATE_DIR */ 34"Updating '%s' would lose untracked files in it", 35 36/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 37"Untracked working tree file '%s' would be overwritten by merge.", 38 39/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 40"Untracked working tree file '%s' would be removed by merge.", 41 42/* ERROR_BIND_OVERLAP */ 43"Entry '%s' overlaps with '%s'. Cannot bind.", 44 45/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 46"Entry '%s' not uptodate. Cannot update sparse checkout.", 47 48/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 49"Working tree file '%s' would be overwritten by sparse checkout update.", 50 51/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 52"Working tree file '%s' would be removed by sparse checkout update.", 53 54/* ERROR_WOULD_LOSE_SUBMODULE */ 55"Submodule '%s' cannot checkout new HEAD.", 56}; 57 58#define ERRORMSG(o,type) \ 59 ( ((o) && (o)->msgs[(type)]) \ 60 ? ((o)->msgs[(type)]) \ 61 : (unpack_plumbing_errors[(type)]) ) 62 63static const char*super_prefixed(const char*path) 64{ 65/* 66 * It is necessary and sufficient to have two static buffers 67 * here, as the return value of this function is fed to 68 * error() using the unpack_*_errors[] templates we see above. 69 */ 70static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 71static int super_prefix_len = -1; 72static unsigned idx =ARRAY_SIZE(buf) -1; 73 74if(super_prefix_len <0) { 75const char*super_prefix =get_super_prefix(); 76if(!super_prefix) { 77 super_prefix_len =0; 78}else{ 79int i; 80for(i =0; i <ARRAY_SIZE(buf); i++) 81strbuf_addstr(&buf[i], super_prefix); 82 super_prefix_len = buf[0].len; 83} 84} 85 86if(!super_prefix_len) 87return path; 88 89if(++idx >=ARRAY_SIZE(buf)) 90 idx =0; 91 92strbuf_setlen(&buf[idx], super_prefix_len); 93strbuf_addstr(&buf[idx], path); 94 95return buf[idx].buf; 96} 97 98voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 99const char*cmd) 100{ 101int i; 102const char**msgs = opts->msgs; 103const char*msg; 104 105if(!strcmp(cmd,"checkout")) 106 msg = advice_commit_before_merge 107?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 108"Please commit your changes or stash them before you switch branches.") 109:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 110else if(!strcmp(cmd,"merge")) 111 msg = advice_commit_before_merge 112?_("Your local changes to the following files would be overwritten by merge:\n%%s" 113"Please commit your changes or stash them before you merge.") 114:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 115else 116 msg = advice_commit_before_merge 117?_("Your local changes to the following files would be overwritten by%s:\n%%s" 118"Please commit your changes or stash them before you%s.") 119:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 120 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 121xstrfmt(msg, cmd, cmd); 122 123 msgs[ERROR_NOT_UPTODATE_DIR] = 124_("Updating the following directories would lose untracked files in them:\n%s"); 125 126if(!strcmp(cmd,"checkout")) 127 msg = advice_commit_before_merge 128?_("The following untracked working tree files would be removed by checkout:\n%%s" 129"Please move or remove them before you switch branches.") 130:_("The following untracked working tree files would be removed by checkout:\n%%s"); 131else if(!strcmp(cmd,"merge")) 132 msg = advice_commit_before_merge 133?_("The following untracked working tree files would be removed by merge:\n%%s" 134"Please move or remove them before you merge.") 135:_("The following untracked working tree files would be removed by merge:\n%%s"); 136else 137 msg = advice_commit_before_merge 138?_("The following untracked working tree files would be removed by%s:\n%%s" 139"Please move or remove them before you%s.") 140:_("The following untracked working tree files would be removed by%s:\n%%s"); 141 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 142 143if(!strcmp(cmd,"checkout")) 144 msg = advice_commit_before_merge 145?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 146"Please move or remove them before you switch branches.") 147:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 148else if(!strcmp(cmd,"merge")) 149 msg = advice_commit_before_merge 150?_("The following untracked working tree files would be overwritten by merge:\n%%s" 151"Please move or remove them before you merge.") 152:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 153else 154 msg = advice_commit_before_merge 155?_("The following untracked working tree files would be overwritten by%s:\n%%s" 156"Please move or remove them before you%s.") 157:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 158 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 159 160/* 161 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 162 * cannot easily display it as a list. 163 */ 164 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 165 166 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 167_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 168 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 169_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 170 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 171_("The following working tree files would be removed by sparse checkout update:\n%s"); 172 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 173_("Cannot update submodule:\n%s"); 174 175 opts->show_all_errors =1; 176/* rejected paths may not have a static buffer */ 177for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 178 opts->unpack_rejects[i].strdup_strings =1; 179} 180 181static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 182unsigned int set,unsigned int clear) 183{ 184 clear |= CE_HASHED; 185 186if(set & CE_REMOVE) 187 set |= CE_WT_REMOVE; 188 189 ce->ce_flags = (ce->ce_flags & ~clear) | set; 190returnadd_index_entry(&o->result, ce, 191 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 192} 193 194static struct cache_entry *dup_entry(const struct cache_entry *ce) 195{ 196unsigned int size =ce_size(ce); 197struct cache_entry *new=xmalloc(size); 198 199memcpy(new, ce, size); 200return new; 201} 202 203static voidadd_entry(struct unpack_trees_options *o, 204const struct cache_entry *ce, 205unsigned int set,unsigned int clear) 206{ 207do_add_entry(o,dup_entry(ce), set, clear); 208} 209 210/* 211 * add error messages on path <path> 212 * corresponding to the type <e> with the message <msg> 213 * indicating if it should be display in porcelain or not 214 */ 215static intadd_rejected_path(struct unpack_trees_options *o, 216enum unpack_trees_error_types e, 217const char*path) 218{ 219if(!o->show_all_errors) 220returnerror(ERRORMSG(o, e),super_prefixed(path)); 221 222/* 223 * Otherwise, insert in a list for future display by 224 * display_error_msgs() 225 */ 226string_list_append(&o->unpack_rejects[e], path); 227return-1; 228} 229 230/* 231 * display all the error messages stored in a nice way 232 */ 233static voiddisplay_error_msgs(struct unpack_trees_options *o) 234{ 235int e, i; 236int something_displayed =0; 237for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 238struct string_list *rejects = &o->unpack_rejects[e]; 239if(rejects->nr >0) { 240struct strbuf path = STRBUF_INIT; 241 something_displayed =1; 242for(i =0; i < rejects->nr; i++) 243strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 244error(ERRORMSG(o, e),super_prefixed(path.buf)); 245strbuf_release(&path); 246} 247string_list_clear(rejects,0); 248} 249if(something_displayed) 250fprintf(stderr,_("Aborting\n")); 251} 252 253static intcheck_submodule_move_head(const struct cache_entry *ce, 254const char*old_id, 255const char*new_id, 256struct unpack_trees_options *o) 257{ 258unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 259const struct submodule *sub =submodule_from_ce(ce); 260 261if(!sub) 262return0; 263 264if(o->reset) 265 flags |= SUBMODULE_MOVE_HEAD_FORCE; 266 267if(submodule_move_head(ce->name, old_id, new_id, flags)) 268return o->gently ? -1: 269add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 270return0; 271} 272 273/* 274 * Preform the loading of the repository's gitmodules file. This function is 275 * used by 'check_update()' to perform loading of the gitmodules file in two 276 * differnt situations: 277 * (1) before removing entries from the working tree if the gitmodules file has 278 * been marked for removal. This situation is specified by 'state' == NULL. 279 * (2) before checking out entries to the working tree if the gitmodules file 280 * has been marked for update. This situation is specified by 'state' != NULL. 281 */ 282static voidload_gitmodules_file(struct index_state *index, 283struct checkout *state) 284{ 285int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 286 287if(pos >=0) { 288struct cache_entry *ce = index->cache[pos]; 289if(!state && ce->ce_flags & CE_WT_REMOVE) { 290repo_read_gitmodules(the_repository); 291}else if(state && (ce->ce_flags & CE_UPDATE)) { 292submodule_free(); 293checkout_entry(ce, state, NULL); 294repo_read_gitmodules(the_repository); 295} 296} 297} 298 299/* 300 * Unlink the last component and schedule the leading directories for 301 * removal, such that empty directories get removed. 302 */ 303static voidunlink_entry(const struct cache_entry *ce) 304{ 305const struct submodule *sub =submodule_from_ce(ce); 306if(sub) { 307/* state.force is set at the caller. */ 308submodule_move_head(ce->name,"HEAD", NULL, 309 SUBMODULE_MOVE_HEAD_FORCE); 310} 311if(!check_leading_path(ce->name,ce_namelen(ce))) 312return; 313if(remove_or_warn(ce->ce_mode, ce->name)) 314return; 315schedule_dir_for_removal(ce->name,ce_namelen(ce)); 316} 317 318static struct progress *get_progress(struct unpack_trees_options *o) 319{ 320unsigned cnt =0, total =0; 321struct index_state *index = &o->result; 322 323if(!o->update || !o->verbose_update) 324return NULL; 325 326for(; cnt < index->cache_nr; cnt++) { 327const struct cache_entry *ce = index->cache[cnt]; 328if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 329 total++; 330} 331 332returnstart_delayed_progress(_("Checking out files"), total); 333} 334 335static intcheck_updates(struct unpack_trees_options *o) 336{ 337unsigned cnt =0; 338int errs =0; 339struct progress *progress = NULL; 340struct index_state *index = &o->result; 341struct checkout state = CHECKOUT_INIT; 342int i; 343 344 state.force =1; 345 state.quiet =1; 346 state.refresh_cache =1; 347 state.istate = index; 348 349 progress =get_progress(o); 350 351if(o->update) 352git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 353 354if(should_update_submodules() && o->update && !o->dry_run) 355load_gitmodules_file(index, NULL); 356 357for(i =0; i < index->cache_nr; i++) { 358const struct cache_entry *ce = index->cache[i]; 359 360if(ce->ce_flags & CE_WT_REMOVE) { 361display_progress(progress, ++cnt); 362if(o->update && !o->dry_run) 363unlink_entry(ce); 364} 365} 366remove_marked_cache_entries(index); 367remove_scheduled_dirs(); 368 369if(should_update_submodules() && o->update && !o->dry_run) 370load_gitmodules_file(index, &state); 371 372enable_delayed_checkout(&state); 373for(i =0; i < index->cache_nr; i++) { 374struct cache_entry *ce = index->cache[i]; 375 376if(ce->ce_flags & CE_UPDATE) { 377if(ce->ce_flags & CE_WT_REMOVE) 378die("BUG: both update and delete flags are set on%s", 379 ce->name); 380display_progress(progress, ++cnt); 381 ce->ce_flags &= ~CE_UPDATE; 382if(o->update && !o->dry_run) { 383 errs |=checkout_entry(ce, &state, NULL); 384} 385} 386} 387stop_progress(&progress); 388 errs |=finish_delayed_checkout(&state); 389if(o->update) 390git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 391return errs !=0; 392} 393 394static intverify_uptodate_sparse(const struct cache_entry *ce, 395struct unpack_trees_options *o); 396static intverify_absent_sparse(const struct cache_entry *ce, 397enum unpack_trees_error_types, 398struct unpack_trees_options *o); 399 400static intapply_sparse_checkout(struct index_state *istate, 401struct cache_entry *ce, 402struct unpack_trees_options *o) 403{ 404int was_skip_worktree =ce_skip_worktree(ce); 405 406if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 407 ce->ce_flags |= CE_SKIP_WORKTREE; 408else 409 ce->ce_flags &= ~CE_SKIP_WORKTREE; 410if(was_skip_worktree !=ce_skip_worktree(ce)) { 411 ce->ce_flags |= CE_UPDATE_IN_BASE; 412mark_fsmonitor_invalid(istate, ce); 413 istate->cache_changed |= CE_ENTRY_CHANGED; 414} 415 416/* 417 * if (!was_skip_worktree && !ce_skip_worktree()) { 418 * This is perfectly normal. Move on; 419 * } 420 */ 421 422/* 423 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 424 * area as a result of ce_skip_worktree() shortcuts in 425 * verify_absent() and verify_uptodate(). 426 * Make sure they don't modify worktree if they are already 427 * outside checkout area 428 */ 429if(was_skip_worktree &&ce_skip_worktree(ce)) { 430 ce->ce_flags &= ~CE_UPDATE; 431 432/* 433 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 434 * on to get that file removed from both index and worktree. 435 * If that file is already outside worktree area, don't 436 * bother remove it. 437 */ 438if(ce->ce_flags & CE_REMOVE) 439 ce->ce_flags &= ~CE_WT_REMOVE; 440} 441 442if(!was_skip_worktree &&ce_skip_worktree(ce)) { 443/* 444 * If CE_UPDATE is set, verify_uptodate() must be called already 445 * also stat info may have lost after merged_entry() so calling 446 * verify_uptodate() again may fail 447 */ 448if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 449return-1; 450 ce->ce_flags |= CE_WT_REMOVE; 451 ce->ce_flags &= ~CE_UPDATE; 452} 453if(was_skip_worktree && !ce_skip_worktree(ce)) { 454if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 455return-1; 456 ce->ce_flags |= CE_UPDATE; 457} 458return0; 459} 460 461staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 462struct unpack_trees_options *o) 463{ 464int ret = o->fn(src, o); 465if(ret >0) 466 ret =0; 467return ret; 468} 469 470static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 471{ 472 ce->ce_flags |= CE_UNPACKED; 473 474if(o->cache_bottom < o->src_index->cache_nr && 475 o->src_index->cache[o->cache_bottom] == ce) { 476int bottom = o->cache_bottom; 477while(bottom < o->src_index->cache_nr && 478 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 479 bottom++; 480 o->cache_bottom = bottom; 481} 482} 483 484static voidmark_all_ce_unused(struct index_state *index) 485{ 486int i; 487for(i =0; i < index->cache_nr; i++) 488 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 489} 490 491static intlocate_in_src_index(const struct cache_entry *ce, 492struct unpack_trees_options *o) 493{ 494struct index_state *index = o->src_index; 495int len =ce_namelen(ce); 496int pos =index_name_pos(index, ce->name, len); 497if(pos <0) 498 pos = -1- pos; 499return pos; 500} 501 502/* 503 * We call unpack_index_entry() with an unmerged cache entry 504 * only in diff-index, and it wants a single callback. Skip 505 * the other unmerged entry with the same name. 506 */ 507static voidmark_ce_used_same_name(struct cache_entry *ce, 508struct unpack_trees_options *o) 509{ 510struct index_state *index = o->src_index; 511int len =ce_namelen(ce); 512int pos; 513 514for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 515struct cache_entry *next = index->cache[pos]; 516if(len !=ce_namelen(next) || 517memcmp(ce->name, next->name, len)) 518break; 519mark_ce_used(next, o); 520} 521} 522 523static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 524{ 525const struct index_state *index = o->src_index; 526int pos = o->cache_bottom; 527 528while(pos < index->cache_nr) { 529struct cache_entry *ce = index->cache[pos]; 530if(!(ce->ce_flags & CE_UNPACKED)) 531return ce; 532 pos++; 533} 534return NULL; 535} 536 537static voidadd_same_unmerged(const struct cache_entry *ce, 538struct unpack_trees_options *o) 539{ 540struct index_state *index = o->src_index; 541int len =ce_namelen(ce); 542int pos =index_name_pos(index, ce->name, len); 543 544if(0<= pos) 545die("programming error in a caller of mark_ce_used_same_name"); 546for(pos = -pos -1; pos < index->cache_nr; pos++) { 547struct cache_entry *next = index->cache[pos]; 548if(len !=ce_namelen(next) || 549memcmp(ce->name, next->name, len)) 550break; 551add_entry(o, next,0,0); 552mark_ce_used(next, o); 553} 554} 555 556static intunpack_index_entry(struct cache_entry *ce, 557struct unpack_trees_options *o) 558{ 559const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 560int ret; 561 562 src[0] = ce; 563 564mark_ce_used(ce, o); 565if(ce_stage(ce)) { 566if(o->skip_unmerged) { 567add_entry(o, ce,0,0); 568return0; 569} 570} 571 ret =call_unpack_fn(src, o); 572if(ce_stage(ce)) 573mark_ce_used_same_name(ce, o); 574return ret; 575} 576 577static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 578 579static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 580{ 581struct unpack_trees_options *o = info->data; 582 583if(o->diff_index_cached) 584return; 585 o->cache_bottom = bottom; 586} 587 588static intswitch_cache_bottom(struct traverse_info *info) 589{ 590struct unpack_trees_options *o = info->data; 591int ret, pos; 592 593if(o->diff_index_cached) 594return0; 595 ret = o->cache_bottom; 596 pos =find_cache_pos(info->prev, &info->name); 597 598if(pos < -1) 599 o->cache_bottom = -2- pos; 600else if(pos <0) 601 o->cache_bottom = o->src_index->cache_nr; 602return ret; 603} 604 605staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 606{ 607return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 608} 609 610static inttraverse_trees_recursive(int n,unsigned long dirmask, 611unsigned long df_conflicts, 612struct name_entry *names, 613struct traverse_info *info) 614{ 615int i, ret, bottom; 616int nr_buf =0; 617struct tree_desc t[MAX_UNPACK_TREES]; 618void*buf[MAX_UNPACK_TREES]; 619struct traverse_info newinfo; 620struct name_entry *p; 621 622 p = names; 623while(!p->mode) 624 p++; 625 626 newinfo = *info; 627 newinfo.prev = info; 628 newinfo.pathspec = info->pathspec; 629 newinfo.name = *p; 630 newinfo.pathlen +=tree_entry_len(p) +1; 631 newinfo.df_conflicts |= df_conflicts; 632 633/* 634 * Fetch the tree from the ODB for each peer directory in the 635 * n commits. 636 * 637 * For 2- and 3-way traversals, we try to avoid hitting the 638 * ODB twice for the same OID. This should yield a nice speed 639 * up in checkouts and merges when the commits are similar. 640 * 641 * We don't bother doing the full O(n^2) search for larger n, 642 * because wider traversals don't happen that often and we 643 * avoid the search setup. 644 * 645 * When 2 peer OIDs are the same, we just copy the tree 646 * descriptor data. This implicitly borrows the buffer 647 * data from the earlier cell. 648 */ 649for(i =0; i < n; i++, dirmask >>=1) { 650if(i >0&&are_same_oid(&names[i], &names[i -1])) 651 t[i] = t[i -1]; 652else if(i >1&&are_same_oid(&names[i], &names[i -2])) 653 t[i] = t[i -2]; 654else{ 655const struct object_id *oid = NULL; 656if(dirmask &1) 657 oid = names[i].oid; 658 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 659} 660} 661 662 bottom =switch_cache_bottom(&newinfo); 663 ret =traverse_trees(n, t, &newinfo); 664restore_cache_bottom(&newinfo, bottom); 665 666for(i =0; i < nr_buf; i++) 667free(buf[i]); 668 669return ret; 670} 671 672/* 673 * Compare the traverse-path to the cache entry without actually 674 * having to generate the textual representation of the traverse 675 * path. 676 * 677 * NOTE! This *only* compares up to the size of the traverse path 678 * itself - the caller needs to do the final check for the cache 679 * entry having more data at the end! 680 */ 681static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 682{ 683int len, pathlen, ce_len; 684const char*ce_name; 685 686if(info->prev) { 687int cmp =do_compare_entry_piecewise(ce, info->prev, 688&info->name); 689if(cmp) 690return cmp; 691} 692 pathlen = info->pathlen; 693 ce_len =ce_namelen(ce); 694 695/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 696if(ce_len < pathlen) 697return-1; 698 699 ce_len -= pathlen; 700 ce_name = ce->name + pathlen; 701 702 len =tree_entry_len(n); 703returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 704} 705 706static intdo_compare_entry(const struct cache_entry *ce, 707const struct traverse_info *info, 708const struct name_entry *n) 709{ 710int len, pathlen, ce_len; 711const char*ce_name; 712int cmp; 713 714/* 715 * If we have not precomputed the traverse path, it is quicker 716 * to avoid doing so. But if we have precomputed it, 717 * it is quicker to use the precomputed version. 718 */ 719if(!info->traverse_path) 720returndo_compare_entry_piecewise(ce, info, n); 721 722 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 723if(cmp) 724return cmp; 725 726 pathlen = info->pathlen; 727 ce_len =ce_namelen(ce); 728 729if(ce_len < pathlen) 730return-1; 731 732 ce_len -= pathlen; 733 ce_name = ce->name + pathlen; 734 735 len =tree_entry_len(n); 736returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 737} 738 739static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 740{ 741int cmp =do_compare_entry(ce, info, n); 742if(cmp) 743return cmp; 744 745/* 746 * Even if the beginning compared identically, the ce should 747 * compare as bigger than a directory leading up to it! 748 */ 749returnce_namelen(ce) >traverse_path_len(info, n); 750} 751 752static intce_in_traverse_path(const struct cache_entry *ce, 753const struct traverse_info *info) 754{ 755if(!info->prev) 756return1; 757if(do_compare_entry(ce, info->prev, &info->name)) 758return0; 759/* 760 * If ce (blob) is the same name as the path (which is a tree 761 * we will be descending into), it won't be inside it. 762 */ 763return(info->pathlen <ce_namelen(ce)); 764} 765 766static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 767{ 768int len =traverse_path_len(info, n); 769struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 770 771 ce->ce_mode =create_ce_mode(n->mode); 772 ce->ce_flags =create_ce_flags(stage); 773 ce->ce_namelen = len; 774oidcpy(&ce->oid, n->oid); 775make_traverse_path(ce->name, info, n); 776 777return ce; 778} 779 780static intunpack_nondirectories(int n,unsigned long mask, 781unsigned long dirmask, 782struct cache_entry **src, 783const struct name_entry *names, 784const struct traverse_info *info) 785{ 786int i; 787struct unpack_trees_options *o = info->data; 788unsigned long conflicts = info->df_conflicts | dirmask; 789 790/* Do we have *only* directories? Nothing to do */ 791if(mask == dirmask && !src[0]) 792return0; 793 794/* 795 * Ok, we've filled in up to any potential index entry in src[0], 796 * now do the rest. 797 */ 798for(i =0; i < n; i++) { 799int stage; 800unsigned int bit =1ul<< i; 801if(conflicts & bit) { 802 src[i + o->merge] = o->df_conflict_entry; 803continue; 804} 805if(!(mask & bit)) 806continue; 807if(!o->merge) 808 stage =0; 809else if(i +1< o->head_idx) 810 stage =1; 811else if(i +1> o->head_idx) 812 stage =3; 813else 814 stage =2; 815 src[i + o->merge] =create_ce_entry(info, names + i, stage); 816} 817 818if(o->merge) { 819int rc =call_unpack_fn((const struct cache_entry *const*)src, 820 o); 821for(i =0; i < n; i++) { 822struct cache_entry *ce = src[i + o->merge]; 823if(ce != o->df_conflict_entry) 824free(ce); 825} 826return rc; 827} 828 829for(i =0; i < n; i++) 830if(src[i] && src[i] != o->df_conflict_entry) 831if(do_add_entry(o, src[i],0,0)) 832return-1; 833 834return0; 835} 836 837static intunpack_failed(struct unpack_trees_options *o,const char*message) 838{ 839discard_index(&o->result); 840if(!o->gently && !o->exiting_early) { 841if(message) 842returnerror("%s", message); 843return-1; 844} 845return-1; 846} 847 848/* 849 * The tree traversal is looking at name p. If we have a matching entry, 850 * return it. If name p is a directory in the index, do not return 851 * anything, as we will want to match it when the traversal descends into 852 * the directory. 853 */ 854static intfind_cache_pos(struct traverse_info *info, 855const struct name_entry *p) 856{ 857int pos; 858struct unpack_trees_options *o = info->data; 859struct index_state *index = o->src_index; 860int pfxlen = info->pathlen; 861int p_len =tree_entry_len(p); 862 863for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 864const struct cache_entry *ce = index->cache[pos]; 865const char*ce_name, *ce_slash; 866int cmp, ce_len; 867 868if(ce->ce_flags & CE_UNPACKED) { 869/* 870 * cache_bottom entry is already unpacked, so 871 * we can never match it; don't check it 872 * again. 873 */ 874if(pos == o->cache_bottom) 875++o->cache_bottom; 876continue; 877} 878if(!ce_in_traverse_path(ce, info)) { 879/* 880 * Check if we can skip future cache checks 881 * (because we're already past all possible 882 * entries in the traverse path). 883 */ 884if(info->traverse_path) { 885if(strncmp(ce->name, info->traverse_path, 886 info->pathlen) >0) 887break; 888} 889continue; 890} 891 ce_name = ce->name + pfxlen; 892 ce_slash =strchr(ce_name,'/'); 893if(ce_slash) 894 ce_len = ce_slash - ce_name; 895else 896 ce_len =ce_namelen(ce) - pfxlen; 897 cmp =name_compare(p->path, p_len, ce_name, ce_len); 898/* 899 * Exact match; if we have a directory we need to 900 * delay returning it. 901 */ 902if(!cmp) 903return ce_slash ? -2- pos : pos; 904if(0< cmp) 905continue;/* keep looking */ 906/* 907 * ce_name sorts after p->path; could it be that we 908 * have files under p->path directory in the index? 909 * E.g. ce_name == "t-i", and p->path == "t"; we may 910 * have "t/a" in the index. 911 */ 912if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 913 ce_name[p_len] <'/') 914continue;/* keep looking */ 915break; 916} 917return-1; 918} 919 920static struct cache_entry *find_cache_entry(struct traverse_info *info, 921const struct name_entry *p) 922{ 923int pos =find_cache_pos(info, p); 924struct unpack_trees_options *o = info->data; 925 926if(0<= pos) 927return o->src_index->cache[pos]; 928else 929return NULL; 930} 931 932static voiddebug_path(struct traverse_info *info) 933{ 934if(info->prev) { 935debug_path(info->prev); 936if(*info->prev->name.path) 937putchar('/'); 938} 939printf("%s", info->name.path); 940} 941 942static voiddebug_name_entry(int i,struct name_entry *n) 943{ 944printf("ent#%d %06o%s\n", i, 945 n->path ? n->mode :0, 946 n->path ? n->path :"(missing)"); 947} 948 949static voiddebug_unpack_callback(int n, 950unsigned long mask, 951unsigned long dirmask, 952struct name_entry *names, 953struct traverse_info *info) 954{ 955int i; 956printf("* unpack mask%lu, dirmask%lu, cnt%d", 957 mask, dirmask, n); 958debug_path(info); 959putchar('\n'); 960for(i =0; i < n; i++) 961debug_name_entry(i, names + i); 962} 963 964static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 965{ 966struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 967struct unpack_trees_options *o = info->data; 968const struct name_entry *p = names; 969 970/* Find first entry with a real name (we could use "mask" too) */ 971while(!p->mode) 972 p++; 973 974if(o->debug_unpack) 975debug_unpack_callback(n, mask, dirmask, names, info); 976 977/* Are we supposed to look at the index too? */ 978if(o->merge) { 979while(1) { 980int cmp; 981struct cache_entry *ce; 982 983if(o->diff_index_cached) 984 ce =next_cache_entry(o); 985else 986 ce =find_cache_entry(info, p); 987 988if(!ce) 989break; 990 cmp =compare_entry(ce, info, p); 991if(cmp <0) { 992if(unpack_index_entry(ce, o) <0) 993returnunpack_failed(o, NULL); 994continue; 995} 996if(!cmp) { 997if(ce_stage(ce)) { 998/* 999 * If we skip unmerged index1000 * entries, we'll skip this1001 * entry *and* the tree1002 * entries associated with it!1003 */1004if(o->skip_unmerged) {1005add_same_unmerged(ce, o);1006return mask;1007}1008}1009 src[0] = ce;1010}1011break;1012}1013}10141015if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1016return-1;10171018if(o->merge && src[0]) {1019if(ce_stage(src[0]))1020mark_ce_used_same_name(src[0], o);1021else1022mark_ce_used(src[0], o);1023}10241025/* Now handle any directories.. */1026if(dirmask) {1027/* special case: "diff-index --cached" looking at a tree */1028if(o->diff_index_cached &&1029 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1030int matches;1031 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1032 names, info);1033/*1034 * Everything under the name matches; skip the1035 * entire hierarchy. diff_index_cached codepath1036 * special cases D/F conflicts in such a way that1037 * it does not do any look-ahead, so this is safe.1038 */1039if(matches) {1040 o->cache_bottom += matches;1041return mask;1042}1043}10441045if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1046 names, info) <0)1047return-1;1048return mask;1049}10501051return mask;1052}10531054static intclear_ce_flags_1(struct cache_entry **cache,int nr,1055struct strbuf *prefix,1056int select_mask,int clear_mask,1057struct exclude_list *el,int defval);10581059/* Whole directory matching */1060static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1061struct strbuf *prefix,1062char*basename,1063int select_mask,int clear_mask,1064struct exclude_list *el,int defval)1065{1066struct cache_entry **cache_end;1067int dtype = DT_DIR;1068int ret =is_excluded_from_list(prefix->buf, prefix->len,1069 basename, &dtype, el, &the_index);1070int rc;10711072strbuf_addch(prefix,'/');10731074/* If undecided, use matching result of parent dir in defval */1075if(ret <0)1076 ret = defval;10771078for(cache_end = cache; cache_end != cache + nr; cache_end++) {1079struct cache_entry *ce = *cache_end;1080if(strncmp(ce->name, prefix->buf, prefix->len))1081break;1082}10831084/*1085 * TODO: check el, if there are no patterns that may conflict1086 * with ret (iow, we know in advance the incl/excl1087 * decision for the entire directory), clear flag here without1088 * calling clear_ce_flags_1(). That function will call1089 * the expensive is_excluded_from_list() on every entry.1090 */1091 rc =clear_ce_flags_1(cache, cache_end - cache,1092 prefix,1093 select_mask, clear_mask,1094 el, ret);1095strbuf_setlen(prefix, prefix->len -1);1096return rc;1097}10981099/*1100 * Traverse the index, find every entry that matches according to1101 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1102 * number of traversed entries.1103 *1104 * If select_mask is non-zero, only entries whose ce_flags has on of1105 * those bits enabled are traversed.1106 *1107 * cache : pointer to an index entry1108 * prefix_len : an offset to its path1109 *1110 * The current path ("prefix") including the trailing '/' is1111 * cache[0]->name[0..(prefix_len-1)]1112 * Top level path has prefix_len zero.1113 */1114static intclear_ce_flags_1(struct cache_entry **cache,int nr,1115struct strbuf *prefix,1116int select_mask,int clear_mask,1117struct exclude_list *el,int defval)1118{1119struct cache_entry **cache_end = cache + nr;11201121/*1122 * Process all entries that have the given prefix and meet1123 * select_mask condition1124 */1125while(cache != cache_end) {1126struct cache_entry *ce = *cache;1127const char*name, *slash;1128int len, dtype, ret;11291130if(select_mask && !(ce->ce_flags & select_mask)) {1131 cache++;1132continue;1133}11341135if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1136break;11371138 name = ce->name + prefix->len;1139 slash =strchr(name,'/');11401141/* If it's a directory, try whole directory match first */1142if(slash) {1143int processed;11441145 len = slash - name;1146strbuf_add(prefix, name, len);11471148 processed =clear_ce_flags_dir(cache, cache_end - cache,1149 prefix,1150 prefix->buf + prefix->len - len,1151 select_mask, clear_mask,1152 el, defval);11531154/* clear_c_f_dir eats a whole dir already? */1155if(processed) {1156 cache += processed;1157strbuf_setlen(prefix, prefix->len - len);1158continue;1159}11601161strbuf_addch(prefix,'/');1162 cache +=clear_ce_flags_1(cache, cache_end - cache,1163 prefix,1164 select_mask, clear_mask, el, defval);1165strbuf_setlen(prefix, prefix->len - len -1);1166continue;1167}11681169/* Non-directory */1170 dtype =ce_to_dtype(ce);1171 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1172 name, &dtype, el, &the_index);1173if(ret <0)1174 ret = defval;1175if(ret >0)1176 ce->ce_flags &= ~clear_mask;1177 cache++;1178}1179return nr - (cache_end - cache);1180}11811182static intclear_ce_flags(struct cache_entry **cache,int nr,1183int select_mask,int clear_mask,1184struct exclude_list *el)1185{1186static struct strbuf prefix = STRBUF_INIT;11871188strbuf_reset(&prefix);11891190returnclear_ce_flags_1(cache, nr,1191&prefix,1192 select_mask, clear_mask,1193 el,0);1194}11951196/*1197 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1198 */1199static voidmark_new_skip_worktree(struct exclude_list *el,1200struct index_state *the_index,1201int select_flag,int skip_wt_flag)1202{1203int i;12041205/*1206 * 1. Pretend the narrowest worktree: only unmerged entries1207 * are checked out1208 */1209for(i =0; i < the_index->cache_nr; i++) {1210struct cache_entry *ce = the_index->cache[i];12111212if(select_flag && !(ce->ce_flags & select_flag))1213continue;12141215if(!ce_stage(ce))1216 ce->ce_flags |= skip_wt_flag;1217else1218 ce->ce_flags &= ~skip_wt_flag;1219}12201221/*1222 * 2. Widen worktree according to sparse-checkout file.1223 * Matched entries will have skip_wt_flag cleared (i.e. "in")1224 */1225clear_ce_flags(the_index->cache, the_index->cache_nr,1226 select_flag, skip_wt_flag, el);1227}12281229static intverify_absent(const struct cache_entry *,1230enum unpack_trees_error_types,1231struct unpack_trees_options *);1232/*1233 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1234 * resulting index, -2 on failure to reflect the changes to the work tree.1235 *1236 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1237 */1238intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1239{1240int i, ret;1241static struct cache_entry *dfc;1242struct exclude_list el;12431244if(len > MAX_UNPACK_TREES)1245die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12461247memset(&el,0,sizeof(el));1248if(!core_apply_sparse_checkout || !o->update)1249 o->skip_sparse_checkout =1;1250if(!o->skip_sparse_checkout) {1251char*sparse =git_pathdup("info/sparse-checkout");1252if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1253 o->skip_sparse_checkout =1;1254else1255 o->el = ⪙1256free(sparse);1257}12581259memset(&o->result,0,sizeof(o->result));1260 o->result.initialized =1;1261 o->result.timestamp.sec = o->src_index->timestamp.sec;1262 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1263 o->result.version = o->src_index->version;1264 o->result.split_index = o->src_index->split_index;1265if(o->result.split_index)1266 o->result.split_index->refcount++;1267hashcpy(o->result.sha1, o->src_index->sha1);1268 o->merge_size = len;1269mark_all_ce_unused(o->src_index);12701271/*1272 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1273 */1274if(!o->skip_sparse_checkout)1275mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);12761277if(!dfc)1278 dfc =xcalloc(1,cache_entry_size(0));1279 o->df_conflict_entry = dfc;12801281if(len) {1282const char*prefix = o->prefix ? o->prefix :"";1283struct traverse_info info;12841285setup_traverse_info(&info, prefix);1286 info.fn = unpack_callback;1287 info.data = o;1288 info.show_all_errors = o->show_all_errors;1289 info.pathspec = o->pathspec;12901291if(o->prefix) {1292/*1293 * Unpack existing index entries that sort before the1294 * prefix the tree is spliced into. Note that o->merge1295 * is always true in this case.1296 */1297while(1) {1298struct cache_entry *ce =next_cache_entry(o);1299if(!ce)1300break;1301if(ce_in_traverse_path(ce, &info))1302break;1303if(unpack_index_entry(ce, o) <0)1304goto return_failed;1305}1306}13071308if(traverse_trees(len, t, &info) <0)1309goto return_failed;1310}13111312/* Any left-over entries in the index? */1313if(o->merge) {1314while(1) {1315struct cache_entry *ce =next_cache_entry(o);1316if(!ce)1317break;1318if(unpack_index_entry(ce, o) <0)1319goto return_failed;1320}1321}1322mark_all_ce_unused(o->src_index);13231324if(o->trivial_merges_only && o->nontrivial_merge) {1325 ret =unpack_failed(o,"Merge requires file-level merging");1326goto done;1327}13281329if(!o->skip_sparse_checkout) {1330int empty_worktree =1;13311332/*1333 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11334 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1335 * so apply_sparse_checkout() won't attempt to remove it from worktree1336 */1337mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13381339 ret =0;1340for(i =0; i < o->result.cache_nr; i++) {1341struct cache_entry *ce = o->result.cache[i];13421343/*1344 * Entries marked with CE_ADDED in merged_entry() do not have1345 * verify_absent() check (the check is effectively disabled1346 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1347 *1348 * Do the real check now because we have had1349 * correct CE_NEW_SKIP_WORKTREE1350 */1351if(ce->ce_flags & CE_ADDED &&1352verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1353if(!o->show_all_errors)1354goto return_failed;1355 ret = -1;1356}13571358if(apply_sparse_checkout(&o->result, ce, o)) {1359if(!o->show_all_errors)1360goto return_failed;1361 ret = -1;1362}1363if(!ce_skip_worktree(ce))1364 empty_worktree =0;13651366}1367if(ret <0)1368goto return_failed;1369/*1370 * Sparse checkout is meant to narrow down checkout area1371 * but it does not make sense to narrow down to empty working1372 * tree. This is usually a mistake in sparse checkout rules.1373 * Do not allow users to do that.1374 */1375if(o->result.cache_nr && empty_worktree) {1376 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1377goto done;1378}1379}13801381 o->src_index = NULL;1382 ret =check_updates(o) ? (-2) :0;1383if(o->dst_index) {1384if(!ret) {1385if(!o->result.cache_tree)1386 o->result.cache_tree =cache_tree();1387if(!cache_tree_fully_valid(o->result.cache_tree))1388cache_tree_update(&o->result,1389 WRITE_TREE_SILENT |1390 WRITE_TREE_REPAIR);1391}1392move_index_extensions(&o->result, o->dst_index);1393discard_index(o->dst_index);1394*o->dst_index = o->result;1395}else{1396discard_index(&o->result);1397}13981399done:1400clear_exclude_list(&el);1401return ret;14021403return_failed:1404if(o->show_all_errors)1405display_error_msgs(o);1406mark_all_ce_unused(o->src_index);1407 ret =unpack_failed(o, NULL);1408if(o->exiting_early)1409 ret =0;1410goto done;1411}14121413/* Here come the merge functions */14141415static intreject_merge(const struct cache_entry *ce,1416struct unpack_trees_options *o)1417{1418return o->gently ? -1:1419add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1420}14211422static intsame(const struct cache_entry *a,const struct cache_entry *b)1423{1424if(!!a != !!b)1425return0;1426if(!a && !b)1427return1;1428if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1429return0;1430return a->ce_mode == b->ce_mode &&1431!oidcmp(&a->oid, &b->oid);1432}143314341435/*1436 * When a CE gets turned into an unmerged entry, we1437 * want it to be up-to-date1438 */1439static intverify_uptodate_1(const struct cache_entry *ce,1440struct unpack_trees_options *o,1441enum unpack_trees_error_types error_type)1442{1443struct stat st;14441445if(o->index_only)1446return0;14471448/*1449 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1450 * if this entry is truly up-to-date because this file may be1451 * overwritten.1452 */1453if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1454;/* keep checking */1455else if(o->reset ||ce_uptodate(ce))1456return0;14571458if(!lstat(ce->name, &st)) {1459int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1460unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);14611462if(submodule_from_ce(ce)) {1463int r =check_submodule_move_head(ce,1464"HEAD",oid_to_hex(&ce->oid), o);1465if(r)1466return o->gently ? -1:1467add_rejected_path(o, error_type, ce->name);1468return0;1469}14701471if(!changed)1472return0;1473/*1474 * Historic default policy was to allow submodule to be out1475 * of sync wrt the superproject index. If the submodule was1476 * not considered interesting above, we don't care here.1477 */1478if(S_ISGITLINK(ce->ce_mode))1479return0;14801481 errno =0;1482}1483if(errno == ENOENT)1484return0;1485return o->gently ? -1:1486add_rejected_path(o, error_type, ce->name);1487}14881489static intverify_uptodate(const struct cache_entry *ce,1490struct unpack_trees_options *o)1491{1492if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1493return0;1494returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1495}14961497static intverify_uptodate_sparse(const struct cache_entry *ce,1498struct unpack_trees_options *o)1499{1500returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1501}15021503static voidinvalidate_ce_path(const struct cache_entry *ce,1504struct unpack_trees_options *o)1505{1506if(!ce)1507return;1508cache_tree_invalidate_path(o->src_index, ce->name);1509untracked_cache_invalidate_path(o->src_index, ce->name);1510}15111512/*1513 * Check that checking out ce->sha1 in subdir ce->name is not1514 * going to overwrite any working files.1515 *1516 * Currently, git does not checkout subprojects during a superproject1517 * checkout, so it is not going to overwrite anything.1518 */1519static intverify_clean_submodule(const char*old_sha1,1520const struct cache_entry *ce,1521enum unpack_trees_error_types error_type,1522struct unpack_trees_options *o)1523{1524if(!submodule_from_ce(ce))1525return0;15261527returncheck_submodule_move_head(ce, old_sha1,1528oid_to_hex(&ce->oid), o);1529}15301531static intverify_clean_subdirectory(const struct cache_entry *ce,1532enum unpack_trees_error_types error_type,1533struct unpack_trees_options *o)1534{1535/*1536 * we are about to extract "ce->name"; we would not want to lose1537 * anything in the existing directory there.1538 */1539int namelen;1540int i;1541struct dir_struct d;1542char*pathbuf;1543int cnt =0;15441545if(S_ISGITLINK(ce->ce_mode)) {1546struct object_id oid;1547int sub_head =resolve_gitlink_ref(ce->name,"HEAD", &oid);1548/*1549 * If we are not going to update the submodule, then1550 * we don't care.1551 */1552if(!sub_head && !oidcmp(&oid, &ce->oid))1553return0;1554returnverify_clean_submodule(sub_head ? NULL :oid_to_hex(&oid),1555 ce, error_type, o);1556}15571558/*1559 * First let's make sure we do not have a local modification1560 * in that directory.1561 */1562 namelen =ce_namelen(ce);1563for(i =locate_in_src_index(ce, o);1564 i < o->src_index->cache_nr;1565 i++) {1566struct cache_entry *ce2 = o->src_index->cache[i];1567int len =ce_namelen(ce2);1568if(len < namelen ||1569strncmp(ce->name, ce2->name, namelen) ||1570 ce2->name[namelen] !='/')1571break;1572/*1573 * ce2->name is an entry in the subdirectory to be1574 * removed.1575 */1576if(!ce_stage(ce2)) {1577if(verify_uptodate(ce2, o))1578return-1;1579add_entry(o, ce2, CE_REMOVE,0);1580mark_ce_used(ce2, o);1581}1582 cnt++;1583}15841585/*1586 * Then we need to make sure that we do not lose a locally1587 * present file that is not ignored.1588 */1589 pathbuf =xstrfmt("%.*s/", namelen, ce->name);15901591memset(&d,0,sizeof(d));1592if(o->dir)1593 d.exclude_per_dir = o->dir->exclude_per_dir;1594 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1595if(i)1596return o->gently ? -1:1597add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1598free(pathbuf);1599return cnt;1600}16011602/*1603 * This gets called when there was no index entry for the tree entry 'dst',1604 * but we found a file in the working tree that 'lstat()' said was fine,1605 * and we're on a case-insensitive filesystem.1606 *1607 * See if we can find a case-insensitive match in the index that also1608 * matches the stat information, and assume it's that other file!1609 */1610static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1611{1612const struct cache_entry *src;16131614 src =index_file_exists(o->src_index, name, len,1);1615return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1616}16171618static intcheck_ok_to_remove(const char*name,int len,int dtype,1619const struct cache_entry *ce,struct stat *st,1620enum unpack_trees_error_types error_type,1621struct unpack_trees_options *o)1622{1623const struct cache_entry *result;16241625/*1626 * It may be that the 'lstat()' succeeded even though1627 * target 'ce' was absent, because there is an old1628 * entry that is different only in case..1629 *1630 * Ignore that lstat() if it matches.1631 */1632if(ignore_case &&icase_exists(o, name, len, st))1633return0;16341635if(o->dir &&1636is_excluded(o->dir, &the_index, name, &dtype))1637/*1638 * ce->name is explicitly excluded, so it is Ok to1639 * overwrite it.1640 */1641return0;1642if(S_ISDIR(st->st_mode)) {1643/*1644 * We are checking out path "foo" and1645 * found "foo/." in the working tree.1646 * This is tricky -- if we have modified1647 * files that are in "foo/" we would lose1648 * them.1649 */1650if(verify_clean_subdirectory(ce, error_type, o) <0)1651return-1;1652return0;1653}16541655/*1656 * The previous round may already have decided to1657 * delete this path, which is in a subdirectory that1658 * is being replaced with a blob.1659 */1660 result =index_file_exists(&o->result, name, len,0);1661if(result) {1662if(result->ce_flags & CE_REMOVE)1663return0;1664}16651666return o->gently ? -1:1667add_rejected_path(o, error_type, name);1668}16691670/*1671 * We do not want to remove or overwrite a working tree file that1672 * is not tracked, unless it is ignored.1673 */1674static intverify_absent_1(const struct cache_entry *ce,1675enum unpack_trees_error_types error_type,1676struct unpack_trees_options *o)1677{1678int len;1679struct stat st;16801681if(o->index_only || o->reset || !o->update)1682return0;16831684 len =check_leading_path(ce->name,ce_namelen(ce));1685if(!len)1686return0;1687else if(len >0) {1688char*path;1689int ret;16901691 path =xmemdupz(ce->name, len);1692if(lstat(path, &st))1693 ret =error_errno("cannot stat '%s'", path);1694else{1695if(submodule_from_ce(ce))1696 ret =check_submodule_move_head(ce,1697oid_to_hex(&ce->oid),1698 NULL, o);1699else1700 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1701&st, error_type, o);1702}1703free(path);1704return ret;1705}else if(lstat(ce->name, &st)) {1706if(errno != ENOENT)1707returnerror_errno("cannot stat '%s'", ce->name);1708return0;1709}else{1710if(submodule_from_ce(ce))1711returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1712 NULL, o);17131714returncheck_ok_to_remove(ce->name,ce_namelen(ce),1715ce_to_dtype(ce), ce, &st,1716 error_type, o);1717}1718}17191720static intverify_absent(const struct cache_entry *ce,1721enum unpack_trees_error_types error_type,1722struct unpack_trees_options *o)1723{1724if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1725return0;1726returnverify_absent_1(ce, error_type, o);1727}17281729static intverify_absent_sparse(const struct cache_entry *ce,1730enum unpack_trees_error_types error_type,1731struct unpack_trees_options *o)1732{1733enum unpack_trees_error_types orphaned_error = error_type;1734if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1735 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17361737returnverify_absent_1(ce, orphaned_error, o);1738}17391740static intmerged_entry(const struct cache_entry *ce,1741const struct cache_entry *old,1742struct unpack_trees_options *o)1743{1744int update = CE_UPDATE;1745struct cache_entry *merge =dup_entry(ce);17461747if(!old) {1748/*1749 * New index entries. In sparse checkout, the following1750 * verify_absent() will be delayed until after1751 * traverse_trees() finishes in unpack_trees(), then:1752 *1753 * - CE_NEW_SKIP_WORKTREE will be computed correctly1754 * - verify_absent() be called again, this time with1755 * correct CE_NEW_SKIP_WORKTREE1756 *1757 * verify_absent() call here does nothing in sparse1758 * checkout (i.e. o->skip_sparse_checkout == 0)1759 */1760 update |= CE_ADDED;1761 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;17621763if(verify_absent(merge,1764 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1765free(merge);1766return-1;1767}1768invalidate_ce_path(merge, o);17691770if(submodule_from_ce(ce)) {1771int ret =check_submodule_move_head(ce, NULL,1772oid_to_hex(&ce->oid),1773 o);1774if(ret)1775return ret;1776}17771778}else if(!(old->ce_flags & CE_CONFLICTED)) {1779/*1780 * See if we can re-use the old CE directly?1781 * That way we get the uptodate stat info.1782 *1783 * This also removes the UPDATE flag on a match; otherwise1784 * we will end up overwriting local changes in the work tree.1785 */1786if(same(old, merge)) {1787copy_cache_entry(merge, old);1788 update =0;1789}else{1790if(verify_uptodate(old, o)) {1791free(merge);1792return-1;1793}1794/* Migrate old flags over */1795 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1796invalidate_ce_path(old, o);1797}17981799if(submodule_from_ce(ce)) {1800int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1801oid_to_hex(&ce->oid),1802 o);1803if(ret)1804return ret;1805}1806}else{1807/*1808 * Previously unmerged entry left as an existence1809 * marker by read_index_unmerged();1810 */1811invalidate_ce_path(old, o);1812}18131814do_add_entry(o, merge, update, CE_STAGEMASK);1815return1;1816}18171818static intdeleted_entry(const struct cache_entry *ce,1819const struct cache_entry *old,1820struct unpack_trees_options *o)1821{1822/* Did it exist in the index? */1823if(!old) {1824if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1825return-1;1826return0;1827}1828if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1829return-1;1830add_entry(o, ce, CE_REMOVE,0);1831invalidate_ce_path(ce, o);1832return1;1833}18341835static intkeep_entry(const struct cache_entry *ce,1836struct unpack_trees_options *o)1837{1838add_entry(o, ce,0,0);1839return1;1840}18411842#if DBRT_DEBUG1843static voidshow_stage_entry(FILE*o,1844const char*label,const struct cache_entry *ce)1845{1846if(!ce)1847fprintf(o,"%s(missing)\n", label);1848else1849fprintf(o,"%s%06o%s %d\t%s\n",1850 label,1851 ce->ce_mode,1852oid_to_hex(&ce->oid),1853ce_stage(ce),1854 ce->name);1855}1856#endif18571858intthreeway_merge(const struct cache_entry *const*stages,1859struct unpack_trees_options *o)1860{1861const struct cache_entry *index;1862const struct cache_entry *head;1863const struct cache_entry *remote = stages[o->head_idx +1];1864int count;1865int head_match =0;1866int remote_match =0;18671868int df_conflict_head =0;1869int df_conflict_remote =0;18701871int any_anc_missing =0;1872int no_anc_exists =1;1873int i;18741875for(i =1; i < o->head_idx; i++) {1876if(!stages[i] || stages[i] == o->df_conflict_entry)1877 any_anc_missing =1;1878else1879 no_anc_exists =0;1880}18811882 index = stages[0];1883 head = stages[o->head_idx];18841885if(head == o->df_conflict_entry) {1886 df_conflict_head =1;1887 head = NULL;1888}18891890if(remote == o->df_conflict_entry) {1891 df_conflict_remote =1;1892 remote = NULL;1893}18941895/*1896 * First, if there's a #16 situation, note that to prevent #131897 * and #14.1898 */1899if(!same(remote, head)) {1900for(i =1; i < o->head_idx; i++) {1901if(same(stages[i], head)) {1902 head_match = i;1903}1904if(same(stages[i], remote)) {1905 remote_match = i;1906}1907}1908}19091910/*1911 * We start with cases where the index is allowed to match1912 * something other than the head: #14(ALT) and #2ALT, where it1913 * is permitted to match the result instead.1914 */1915/* #14, #14ALT, #2ALT */1916if(remote && !df_conflict_head && head_match && !remote_match) {1917if(index && !same(index, remote) && !same(index, head))1918returnreject_merge(index, o);1919returnmerged_entry(remote, index, o);1920}1921/*1922 * If we have an entry in the index cache, then we want to1923 * make sure that it matches head.1924 */1925if(index && !same(index, head))1926returnreject_merge(index, o);19271928if(head) {1929/* #5ALT, #15 */1930if(same(head, remote))1931returnmerged_entry(head, index, o);1932/* #13, #3ALT */1933if(!df_conflict_remote && remote_match && !head_match)1934returnmerged_entry(head, index, o);1935}19361937/* #1 */1938if(!head && !remote && any_anc_missing)1939return0;19401941/*1942 * Under the "aggressive" rule, we resolve mostly trivial1943 * cases that we historically had git-merge-one-file resolve.1944 */1945if(o->aggressive) {1946int head_deleted = !head;1947int remote_deleted = !remote;1948const struct cache_entry *ce = NULL;19491950if(index)1951 ce = index;1952else if(head)1953 ce = head;1954else if(remote)1955 ce = remote;1956else{1957for(i =1; i < o->head_idx; i++) {1958if(stages[i] && stages[i] != o->df_conflict_entry) {1959 ce = stages[i];1960break;1961}1962}1963}19641965/*1966 * Deleted in both.1967 * Deleted in one and unchanged in the other.1968 */1969if((head_deleted && remote_deleted) ||1970(head_deleted && remote && remote_match) ||1971(remote_deleted && head && head_match)) {1972if(index)1973returndeleted_entry(index, index, o);1974if(ce && !head_deleted) {1975if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1976return-1;1977}1978return0;1979}1980/*1981 * Added in both, identically.1982 */1983if(no_anc_exists && head && remote &&same(head, remote))1984returnmerged_entry(head, index, o);19851986}19871988/* Below are "no merge" cases, which require that the index be1989 * up-to-date to avoid the files getting overwritten with1990 * conflict resolution files.1991 */1992if(index) {1993if(verify_uptodate(index, o))1994return-1;1995}19961997 o->nontrivial_merge =1;19981999/* #2, #3, #4, #6, #7, #9, #10, #11. */2000 count =0;2001if(!head_match || !remote_match) {2002for(i =1; i < o->head_idx; i++) {2003if(stages[i] && stages[i] != o->df_conflict_entry) {2004keep_entry(stages[i], o);2005 count++;2006break;2007}2008}2009}2010#if DBRT_DEBUG2011else{2012fprintf(stderr,"read-tree: warning #16 detected\n");2013show_stage_entry(stderr,"head ", stages[head_match]);2014show_stage_entry(stderr,"remote ", stages[remote_match]);2015}2016#endif2017if(head) { count +=keep_entry(head, o); }2018if(remote) { count +=keep_entry(remote, o); }2019return count;2020}20212022/*2023 * Two-way merge.2024 *2025 * The rule is to "carry forward" what is in the index without losing2026 * information across a "fast-forward", favoring a successful merge2027 * over a merge failure when it makes sense. For details of the2028 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2029 *2030 */2031inttwoway_merge(const struct cache_entry *const*src,2032struct unpack_trees_options *o)2033{2034const struct cache_entry *current = src[0];2035const struct cache_entry *oldtree = src[1];2036const struct cache_entry *newtree = src[2];20372038if(o->merge_size !=2)2039returnerror("Cannot do a twoway merge of%dtrees",2040 o->merge_size);20412042if(oldtree == o->df_conflict_entry)2043 oldtree = NULL;2044if(newtree == o->df_conflict_entry)2045 newtree = NULL;20462047if(current) {2048if(current->ce_flags & CE_CONFLICTED) {2049if(same(oldtree, newtree) || o->reset) {2050if(!newtree)2051returndeleted_entry(current, current, o);2052else2053returnmerged_entry(newtree, current, o);2054}2055returnreject_merge(current, o);2056}else if((!oldtree && !newtree) ||/* 4 and 5 */2057(!oldtree && newtree &&2058same(current, newtree)) ||/* 6 and 7 */2059(oldtree && newtree &&2060same(oldtree, newtree)) ||/* 14 and 15 */2061(oldtree && newtree &&2062!same(oldtree, newtree) &&/* 18 and 19 */2063same(current, newtree))) {2064returnkeep_entry(current, o);2065}else if(oldtree && !newtree &&same(current, oldtree)) {2066/* 10 or 11 */2067returndeleted_entry(oldtree, current, o);2068}else if(oldtree && newtree &&2069same(current, oldtree) && !same(current, newtree)) {2070/* 20 or 21 */2071returnmerged_entry(newtree, current, o);2072}else2073returnreject_merge(current, o);2074}2075else if(newtree) {2076if(oldtree && !o->initial_checkout) {2077/*2078 * deletion of the path was staged;2079 */2080if(same(oldtree, newtree))2081return1;2082returnreject_merge(oldtree, o);2083}2084returnmerged_entry(newtree, current, o);2085}2086returndeleted_entry(oldtree, current, o);2087}20882089/*2090 * Bind merge.2091 *2092 * Keep the index entries at stage0, collapse stage1 but make sure2093 * stage0 does not have anything there.2094 */2095intbind_merge(const struct cache_entry *const*src,2096struct unpack_trees_options *o)2097{2098const struct cache_entry *old = src[0];2099const struct cache_entry *a = src[1];21002101if(o->merge_size !=1)2102returnerror("Cannot do a bind merge of%dtrees",2103 o->merge_size);2104if(a && old)2105return o->gently ? -1:2106error(ERRORMSG(o, ERROR_BIND_OVERLAP),2107super_prefixed(a->name),2108super_prefixed(old->name));2109if(!a)2110returnkeep_entry(old, o);2111else2112returnmerged_entry(a, NULL, o);2113}21142115/*2116 * One-way merge.2117 *2118 * The rule is:2119 * - take the stat information from stage0, take the data from stage12120 */2121intoneway_merge(const struct cache_entry *const*src,2122struct unpack_trees_options *o)2123{2124const struct cache_entry *old = src[0];2125const struct cache_entry *a = src[1];21262127if(o->merge_size !=1)2128returnerror("Cannot do a oneway merge of%dtrees",2129 o->merge_size);21302131if(!a || a == o->df_conflict_entry)2132returndeleted_entry(old, old, o);21332134if(old &&same(old, a)) {2135int update =0;2136if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2137struct stat st;2138if(lstat(old->name, &st) ||2139ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2140 update |= CE_UPDATE;2141}2142if(o->update &&S_ISGITLINK(old->ce_mode) &&2143should_update_submodules() && !verify_uptodate(old, o))2144 update |= CE_UPDATE;2145add_entry(o, old, update,0);2146return0;2147}2148returnmerged_entry(a, old, o);2149}