1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"dir.h" 4#include"tree.h" 5#include"tree-walk.h" 6#include"cache-tree.h" 7#include"unpack-trees.h" 8#include"progress.h" 9#include"refs.h" 10#include"attr.h" 11#include"split-index.h" 12#include"dir.h" 13 14/* 15 * Error messages expected by scripts out of plumbing commands such as 16 * read-tree. Non-scripted Porcelain is not required to use these messages 17 * and in fact are encouraged to reword them to better suit their particular 18 * situation better. See how "git checkout" and "git merge" replaces 19 * them using setup_unpack_trees_porcelain(), for example. 20 */ 21static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 22/* ERROR_WOULD_OVERWRITE */ 23"Entry '%s' would be overwritten by merge. Cannot merge.", 24 25/* ERROR_NOT_UPTODATE_FILE */ 26"Entry '%s' not uptodate. Cannot merge.", 27 28/* ERROR_NOT_UPTODATE_DIR */ 29"Updating '%s' would lose untracked files in it", 30 31/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 32"Untracked working tree file '%s' would be overwritten by merge.", 33 34/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 35"Untracked working tree file '%s' would be removed by merge.", 36 37/* ERROR_BIND_OVERLAP */ 38"Entry '%s' overlaps with '%s'. Cannot bind.", 39 40/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 41"Entry '%s' not uptodate. Cannot update sparse checkout.", 42 43/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 44"Working tree file '%s' would be overwritten by sparse checkout update.", 45 46/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 47"Working tree file '%s' would be removed by sparse checkout update.", 48}; 49 50#define ERRORMSG(o,type) \ 51 ( ((o) && (o)->msgs[(type)]) \ 52 ? ((o)->msgs[(type)]) \ 53 : (unpack_plumbing_errors[(type)]) ) 54 55voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 56const char*cmd) 57{ 58int i; 59const char**msgs = opts->msgs; 60const char*msg; 61 62if(!strcmp(cmd,"checkout")) 63 msg = advice_commit_before_merge 64?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 65"Please commit your changes or stash them before you switch branches.") 66:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 67else if(!strcmp(cmd,"merge")) 68 msg = advice_commit_before_merge 69?_("Your local changes to the following files would be overwritten by merge:\n%%s" 70"Please commit your changes or stash them before you merge.") 71:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 72else 73 msg = advice_commit_before_merge 74?_("Your local changes to the following files would be overwritten by%s:\n%%s" 75"Please commit your changes or stash them before you%s.") 76:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 77 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 78xstrfmt(msg, cmd, cmd); 79 80 msgs[ERROR_NOT_UPTODATE_DIR] = 81_("Updating the following directories would lose untracked files in it:\n%s"); 82 83if(!strcmp(cmd,"checkout")) 84 msg = advice_commit_before_merge 85?_("The following untracked working tree files would be removed by checkout:\n%%s" 86"Please move or remove them before you switch branches.") 87:_("The following untracked working tree files would be removed by checkout:\n%%s"); 88else if(!strcmp(cmd,"merge")) 89 msg = advice_commit_before_merge 90?_("The following untracked working tree files would be removed by merge:\n%%s" 91"Please move or remove them before you merge.") 92:_("The following untracked working tree files would be removed by merge:\n%%s"); 93else 94 msg = advice_commit_before_merge 95?_("The following untracked working tree files would be removed by%s:\n%%s" 96"Please move or remove them before you%s.") 97:_("The following untracked working tree files would be removed by%s:\n%%s"); 98 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 99 100if(!strcmp(cmd,"checkout")) 101 msg = advice_commit_before_merge 102?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 103"Please move or remove them before you switch branches.") 104:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 105else if(!strcmp(cmd,"merge")) 106 msg = advice_commit_before_merge 107?_("The following untracked working tree files would be overwritten by merge:\n%%s" 108"Please move or remove them before you merge.") 109:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 110else 111 msg = advice_commit_before_merge 112?_("The following untracked working tree files would be overwritten by%s:\n%%s" 113"Please move or remove them before you%s.") 114:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 115 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 116 117/* 118 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 119 * cannot easily display it as a list. 120 */ 121 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 122 123 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 124_("Cannot update sparse checkout: the following entries are not up-to-date:\n%s"); 125 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 126_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 127 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 128_("The following working tree files would be removed by sparse checkout update:\n%s"); 129 130 opts->show_all_errors =1; 131/* rejected paths may not have a static buffer */ 132for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 133 opts->unpack_rejects[i].strdup_strings =1; 134} 135 136static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 137unsigned int set,unsigned int clear) 138{ 139 clear |= CE_HASHED; 140 141if(set & CE_REMOVE) 142 set |= CE_WT_REMOVE; 143 144 ce->ce_flags = (ce->ce_flags & ~clear) | set; 145returnadd_index_entry(&o->result, ce, 146 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 147} 148 149static struct cache_entry *dup_entry(const struct cache_entry *ce) 150{ 151unsigned int size =ce_size(ce); 152struct cache_entry *new=xmalloc(size); 153 154memcpy(new, ce, size); 155return new; 156} 157 158static voidadd_entry(struct unpack_trees_options *o, 159const struct cache_entry *ce, 160unsigned int set,unsigned int clear) 161{ 162do_add_entry(o,dup_entry(ce), set, clear); 163} 164 165/* 166 * add error messages on path <path> 167 * corresponding to the type <e> with the message <msg> 168 * indicating if it should be display in porcelain or not 169 */ 170static intadd_rejected_path(struct unpack_trees_options *o, 171enum unpack_trees_error_types e, 172const char*path) 173{ 174if(!o->show_all_errors) 175returnerror(ERRORMSG(o, e), path); 176 177/* 178 * Otherwise, insert in a list for future display by 179 * display_error_msgs() 180 */ 181string_list_append(&o->unpack_rejects[e], path); 182return-1; 183} 184 185/* 186 * display all the error messages stored in a nice way 187 */ 188static voiddisplay_error_msgs(struct unpack_trees_options *o) 189{ 190int e, i; 191int something_displayed =0; 192for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 193struct string_list *rejects = &o->unpack_rejects[e]; 194if(rejects->nr >0) { 195struct strbuf path = STRBUF_INIT; 196 something_displayed =1; 197for(i =0; i < rejects->nr; i++) 198strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 199error(ERRORMSG(o, e), path.buf); 200strbuf_release(&path); 201} 202string_list_clear(rejects,0); 203} 204if(something_displayed) 205fprintf(stderr,_("Aborting\n")); 206} 207 208/* 209 * Unlink the last component and schedule the leading directories for 210 * removal, such that empty directories get removed. 211 */ 212static voidunlink_entry(const struct cache_entry *ce) 213{ 214if(!check_leading_path(ce->name,ce_namelen(ce))) 215return; 216if(remove_or_warn(ce->ce_mode, ce->name)) 217return; 218schedule_dir_for_removal(ce->name,ce_namelen(ce)); 219} 220 221static intcheck_updates(struct unpack_trees_options *o) 222{ 223unsigned cnt =0, total =0; 224int errs =0; 225struct progress *progress = NULL; 226struct index_state *index = &o->result; 227struct checkout state = CHECKOUT_INIT; 228int i; 229 230 state.force =1; 231 state.quiet =1; 232 state.refresh_cache =1; 233 state.istate = index; 234 235if(o->update && o->verbose_update) { 236for(total = cnt =0; cnt < index->cache_nr; cnt++) { 237const struct cache_entry *ce = index->cache[cnt]; 238if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 239 total++; 240} 241 242 progress =start_progress_delay(_("Checking out files"), 243 total,50,1); 244 cnt =0; 245} 246 247if(o->update) 248git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 249for(i =0; i < index->cache_nr; i++) { 250const struct cache_entry *ce = index->cache[i]; 251 252if(ce->ce_flags & CE_WT_REMOVE) { 253display_progress(progress, ++cnt); 254if(o->update && !o->dry_run) 255unlink_entry(ce); 256} 257} 258remove_marked_cache_entries(index); 259remove_scheduled_dirs(); 260 261for(i =0; i < index->cache_nr; i++) { 262struct cache_entry *ce = index->cache[i]; 263 264if(ce->ce_flags & CE_UPDATE) { 265if(ce->ce_flags & CE_WT_REMOVE) 266die("BUG: both update and delete flags are set on%s", 267 ce->name); 268display_progress(progress, ++cnt); 269 ce->ce_flags &= ~CE_UPDATE; 270if(o->update && !o->dry_run) { 271 errs |=checkout_entry(ce, &state, NULL); 272} 273} 274} 275stop_progress(&progress); 276if(o->update) 277git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 278return errs !=0; 279} 280 281static intverify_uptodate_sparse(const struct cache_entry *ce, 282struct unpack_trees_options *o); 283static intverify_absent_sparse(const struct cache_entry *ce, 284enum unpack_trees_error_types, 285struct unpack_trees_options *o); 286 287static intapply_sparse_checkout(struct index_state *istate, 288struct cache_entry *ce, 289struct unpack_trees_options *o) 290{ 291int was_skip_worktree =ce_skip_worktree(ce); 292 293if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 294 ce->ce_flags |= CE_SKIP_WORKTREE; 295else 296 ce->ce_flags &= ~CE_SKIP_WORKTREE; 297if(was_skip_worktree !=ce_skip_worktree(ce)) { 298 ce->ce_flags |= CE_UPDATE_IN_BASE; 299 istate->cache_changed |= CE_ENTRY_CHANGED; 300} 301 302/* 303 * if (!was_skip_worktree && !ce_skip_worktree()) { 304 * This is perfectly normal. Move on; 305 * } 306 */ 307 308/* 309 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 310 * area as a result of ce_skip_worktree() shortcuts in 311 * verify_absent() and verify_uptodate(). 312 * Make sure they don't modify worktree if they are already 313 * outside checkout area 314 */ 315if(was_skip_worktree &&ce_skip_worktree(ce)) { 316 ce->ce_flags &= ~CE_UPDATE; 317 318/* 319 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 320 * on to get that file removed from both index and worktree. 321 * If that file is already outside worktree area, don't 322 * bother remove it. 323 */ 324if(ce->ce_flags & CE_REMOVE) 325 ce->ce_flags &= ~CE_WT_REMOVE; 326} 327 328if(!was_skip_worktree &&ce_skip_worktree(ce)) { 329/* 330 * If CE_UPDATE is set, verify_uptodate() must be called already 331 * also stat info may have lost after merged_entry() so calling 332 * verify_uptodate() again may fail 333 */ 334if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 335return-1; 336 ce->ce_flags |= CE_WT_REMOVE; 337 ce->ce_flags &= ~CE_UPDATE; 338} 339if(was_skip_worktree && !ce_skip_worktree(ce)) { 340if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 341return-1; 342 ce->ce_flags |= CE_UPDATE; 343} 344return0; 345} 346 347staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 348struct unpack_trees_options *o) 349{ 350int ret = o->fn(src, o); 351if(ret >0) 352 ret =0; 353return ret; 354} 355 356static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 357{ 358 ce->ce_flags |= CE_UNPACKED; 359 360if(o->cache_bottom < o->src_index->cache_nr && 361 o->src_index->cache[o->cache_bottom] == ce) { 362int bottom = o->cache_bottom; 363while(bottom < o->src_index->cache_nr && 364 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 365 bottom++; 366 o->cache_bottom = bottom; 367} 368} 369 370static voidmark_all_ce_unused(struct index_state *index) 371{ 372int i; 373for(i =0; i < index->cache_nr; i++) 374 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 375} 376 377static intlocate_in_src_index(const struct cache_entry *ce, 378struct unpack_trees_options *o) 379{ 380struct index_state *index = o->src_index; 381int len =ce_namelen(ce); 382int pos =index_name_pos(index, ce->name, len); 383if(pos <0) 384 pos = -1- pos; 385return pos; 386} 387 388/* 389 * We call unpack_index_entry() with an unmerged cache entry 390 * only in diff-index, and it wants a single callback. Skip 391 * the other unmerged entry with the same name. 392 */ 393static voidmark_ce_used_same_name(struct cache_entry *ce, 394struct unpack_trees_options *o) 395{ 396struct index_state *index = o->src_index; 397int len =ce_namelen(ce); 398int pos; 399 400for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 401struct cache_entry *next = index->cache[pos]; 402if(len !=ce_namelen(next) || 403memcmp(ce->name, next->name, len)) 404break; 405mark_ce_used(next, o); 406} 407} 408 409static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 410{ 411const struct index_state *index = o->src_index; 412int pos = o->cache_bottom; 413 414while(pos < index->cache_nr) { 415struct cache_entry *ce = index->cache[pos]; 416if(!(ce->ce_flags & CE_UNPACKED)) 417return ce; 418 pos++; 419} 420return NULL; 421} 422 423static voidadd_same_unmerged(const struct cache_entry *ce, 424struct unpack_trees_options *o) 425{ 426struct index_state *index = o->src_index; 427int len =ce_namelen(ce); 428int pos =index_name_pos(index, ce->name, len); 429 430if(0<= pos) 431die("programming error in a caller of mark_ce_used_same_name"); 432for(pos = -pos -1; pos < index->cache_nr; pos++) { 433struct cache_entry *next = index->cache[pos]; 434if(len !=ce_namelen(next) || 435memcmp(ce->name, next->name, len)) 436break; 437add_entry(o, next,0,0); 438mark_ce_used(next, o); 439} 440} 441 442static intunpack_index_entry(struct cache_entry *ce, 443struct unpack_trees_options *o) 444{ 445const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 446int ret; 447 448 src[0] = ce; 449 450mark_ce_used(ce, o); 451if(ce_stage(ce)) { 452if(o->skip_unmerged) { 453add_entry(o, ce,0,0); 454return0; 455} 456} 457 ret =call_unpack_fn(src, o); 458if(ce_stage(ce)) 459mark_ce_used_same_name(ce, o); 460return ret; 461} 462 463static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 464 465static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 466{ 467struct unpack_trees_options *o = info->data; 468 469if(o->diff_index_cached) 470return; 471 o->cache_bottom = bottom; 472} 473 474static intswitch_cache_bottom(struct traverse_info *info) 475{ 476struct unpack_trees_options *o = info->data; 477int ret, pos; 478 479if(o->diff_index_cached) 480return0; 481 ret = o->cache_bottom; 482 pos =find_cache_pos(info->prev, &info->name); 483 484if(pos < -1) 485 o->cache_bottom = -2- pos; 486else if(pos <0) 487 o->cache_bottom = o->src_index->cache_nr; 488return ret; 489} 490 491static inttraverse_trees_recursive(int n,unsigned long dirmask, 492unsigned long df_conflicts, 493struct name_entry *names, 494struct traverse_info *info) 495{ 496int i, ret, bottom; 497struct tree_desc t[MAX_UNPACK_TREES]; 498void*buf[MAX_UNPACK_TREES]; 499struct traverse_info newinfo; 500struct name_entry *p; 501 502 p = names; 503while(!p->mode) 504 p++; 505 506 newinfo = *info; 507 newinfo.prev = info; 508 newinfo.pathspec = info->pathspec; 509 newinfo.name = *p; 510 newinfo.pathlen +=tree_entry_len(p) +1; 511 newinfo.df_conflicts |= df_conflicts; 512 513for(i =0; i < n; i++, dirmask >>=1) { 514const unsigned char*sha1 = NULL; 515if(dirmask &1) 516 sha1 = names[i].oid->hash; 517 buf[i] =fill_tree_descriptor(t+i, sha1); 518} 519 520 bottom =switch_cache_bottom(&newinfo); 521 ret =traverse_trees(n, t, &newinfo); 522restore_cache_bottom(&newinfo, bottom); 523 524for(i =0; i < n; i++) 525free(buf[i]); 526 527return ret; 528} 529 530/* 531 * Compare the traverse-path to the cache entry without actually 532 * having to generate the textual representation of the traverse 533 * path. 534 * 535 * NOTE! This *only* compares up to the size of the traverse path 536 * itself - the caller needs to do the final check for the cache 537 * entry having more data at the end! 538 */ 539static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 540{ 541int len, pathlen, ce_len; 542const char*ce_name; 543 544if(info->prev) { 545int cmp =do_compare_entry_piecewise(ce, info->prev, 546&info->name); 547if(cmp) 548return cmp; 549} 550 pathlen = info->pathlen; 551 ce_len =ce_namelen(ce); 552 553/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 554if(ce_len < pathlen) 555return-1; 556 557 ce_len -= pathlen; 558 ce_name = ce->name + pathlen; 559 560 len =tree_entry_len(n); 561returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 562} 563 564static intdo_compare_entry(const struct cache_entry *ce, 565const struct traverse_info *info, 566const struct name_entry *n) 567{ 568int len, pathlen, ce_len; 569const char*ce_name; 570int cmp; 571 572/* 573 * If we have not precomputed the traverse path, it is quicker 574 * to avoid doing so. But if we have precomputed it, 575 * it is quicker to use the precomputed version. 576 */ 577if(!info->traverse_path) 578returndo_compare_entry_piecewise(ce, info, n); 579 580 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 581if(cmp) 582return cmp; 583 584 pathlen = info->pathlen; 585 ce_len =ce_namelen(ce); 586 587if(ce_len < pathlen) 588return-1; 589 590 ce_len -= pathlen; 591 ce_name = ce->name + pathlen; 592 593 len =tree_entry_len(n); 594returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 595} 596 597static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 598{ 599int cmp =do_compare_entry(ce, info, n); 600if(cmp) 601return cmp; 602 603/* 604 * Even if the beginning compared identically, the ce should 605 * compare as bigger than a directory leading up to it! 606 */ 607returnce_namelen(ce) >traverse_path_len(info, n); 608} 609 610static intce_in_traverse_path(const struct cache_entry *ce, 611const struct traverse_info *info) 612{ 613if(!info->prev) 614return1; 615if(do_compare_entry(ce, info->prev, &info->name)) 616return0; 617/* 618 * If ce (blob) is the same name as the path (which is a tree 619 * we will be descending into), it won't be inside it. 620 */ 621return(info->pathlen <ce_namelen(ce)); 622} 623 624static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 625{ 626int len =traverse_path_len(info, n); 627struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 628 629 ce->ce_mode =create_ce_mode(n->mode); 630 ce->ce_flags =create_ce_flags(stage); 631 ce->ce_namelen = len; 632oidcpy(&ce->oid, n->oid); 633make_traverse_path(ce->name, info, n); 634 635return ce; 636} 637 638static intunpack_nondirectories(int n,unsigned long mask, 639unsigned long dirmask, 640struct cache_entry **src, 641const struct name_entry *names, 642const struct traverse_info *info) 643{ 644int i; 645struct unpack_trees_options *o = info->data; 646unsigned long conflicts = info->df_conflicts | dirmask; 647 648/* Do we have *only* directories? Nothing to do */ 649if(mask == dirmask && !src[0]) 650return0; 651 652/* 653 * Ok, we've filled in up to any potential index entry in src[0], 654 * now do the rest. 655 */ 656for(i =0; i < n; i++) { 657int stage; 658unsigned int bit =1ul<< i; 659if(conflicts & bit) { 660 src[i + o->merge] = o->df_conflict_entry; 661continue; 662} 663if(!(mask & bit)) 664continue; 665if(!o->merge) 666 stage =0; 667else if(i +1< o->head_idx) 668 stage =1; 669else if(i +1> o->head_idx) 670 stage =3; 671else 672 stage =2; 673 src[i + o->merge] =create_ce_entry(info, names + i, stage); 674} 675 676if(o->merge) { 677int rc =call_unpack_fn((const struct cache_entry *const*)src, 678 o); 679for(i =0; i < n; i++) { 680struct cache_entry *ce = src[i + o->merge]; 681if(ce != o->df_conflict_entry) 682free(ce); 683} 684return rc; 685} 686 687for(i =0; i < n; i++) 688if(src[i] && src[i] != o->df_conflict_entry) 689if(do_add_entry(o, src[i],0,0)) 690return-1; 691 692return0; 693} 694 695static intunpack_failed(struct unpack_trees_options *o,const char*message) 696{ 697discard_index(&o->result); 698if(!o->gently && !o->exiting_early) { 699if(message) 700returnerror("%s", message); 701return-1; 702} 703return-1; 704} 705 706/* 707 * The tree traversal is looking at name p. If we have a matching entry, 708 * return it. If name p is a directory in the index, do not return 709 * anything, as we will want to match it when the traversal descends into 710 * the directory. 711 */ 712static intfind_cache_pos(struct traverse_info *info, 713const struct name_entry *p) 714{ 715int pos; 716struct unpack_trees_options *o = info->data; 717struct index_state *index = o->src_index; 718int pfxlen = info->pathlen; 719int p_len =tree_entry_len(p); 720 721for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 722const struct cache_entry *ce = index->cache[pos]; 723const char*ce_name, *ce_slash; 724int cmp, ce_len; 725 726if(ce->ce_flags & CE_UNPACKED) { 727/* 728 * cache_bottom entry is already unpacked, so 729 * we can never match it; don't check it 730 * again. 731 */ 732if(pos == o->cache_bottom) 733++o->cache_bottom; 734continue; 735} 736if(!ce_in_traverse_path(ce, info)) { 737/* 738 * Check if we can skip future cache checks 739 * (because we're already past all possible 740 * entries in the traverse path). 741 */ 742if(info->traverse_path) { 743if(strncmp(ce->name, info->traverse_path, 744 info->pathlen) >0) 745break; 746} 747continue; 748} 749 ce_name = ce->name + pfxlen; 750 ce_slash =strchr(ce_name,'/'); 751if(ce_slash) 752 ce_len = ce_slash - ce_name; 753else 754 ce_len =ce_namelen(ce) - pfxlen; 755 cmp =name_compare(p->path, p_len, ce_name, ce_len); 756/* 757 * Exact match; if we have a directory we need to 758 * delay returning it. 759 */ 760if(!cmp) 761return ce_slash ? -2- pos : pos; 762if(0< cmp) 763continue;/* keep looking */ 764/* 765 * ce_name sorts after p->path; could it be that we 766 * have files under p->path directory in the index? 767 * E.g. ce_name == "t-i", and p->path == "t"; we may 768 * have "t/a" in the index. 769 */ 770if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 771 ce_name[p_len] <'/') 772continue;/* keep looking */ 773break; 774} 775return-1; 776} 777 778static struct cache_entry *find_cache_entry(struct traverse_info *info, 779const struct name_entry *p) 780{ 781int pos =find_cache_pos(info, p); 782struct unpack_trees_options *o = info->data; 783 784if(0<= pos) 785return o->src_index->cache[pos]; 786else 787return NULL; 788} 789 790static voiddebug_path(struct traverse_info *info) 791{ 792if(info->prev) { 793debug_path(info->prev); 794if(*info->prev->name.path) 795putchar('/'); 796} 797printf("%s", info->name.path); 798} 799 800static voiddebug_name_entry(int i,struct name_entry *n) 801{ 802printf("ent#%d %06o%s\n", i, 803 n->path ? n->mode :0, 804 n->path ? n->path :"(missing)"); 805} 806 807static voiddebug_unpack_callback(int n, 808unsigned long mask, 809unsigned long dirmask, 810struct name_entry *names, 811struct traverse_info *info) 812{ 813int i; 814printf("* unpack mask%lu, dirmask%lu, cnt%d", 815 mask, dirmask, n); 816debug_path(info); 817putchar('\n'); 818for(i =0; i < n; i++) 819debug_name_entry(i, names + i); 820} 821 822static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 823{ 824struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 825struct unpack_trees_options *o = info->data; 826const struct name_entry *p = names; 827 828/* Find first entry with a real name (we could use "mask" too) */ 829while(!p->mode) 830 p++; 831 832if(o->debug_unpack) 833debug_unpack_callback(n, mask, dirmask, names, info); 834 835/* Are we supposed to look at the index too? */ 836if(o->merge) { 837while(1) { 838int cmp; 839struct cache_entry *ce; 840 841if(o->diff_index_cached) 842 ce =next_cache_entry(o); 843else 844 ce =find_cache_entry(info, p); 845 846if(!ce) 847break; 848 cmp =compare_entry(ce, info, p); 849if(cmp <0) { 850if(unpack_index_entry(ce, o) <0) 851returnunpack_failed(o, NULL); 852continue; 853} 854if(!cmp) { 855if(ce_stage(ce)) { 856/* 857 * If we skip unmerged index 858 * entries, we'll skip this 859 * entry *and* the tree 860 * entries associated with it! 861 */ 862if(o->skip_unmerged) { 863add_same_unmerged(ce, o); 864return mask; 865} 866} 867 src[0] = ce; 868} 869break; 870} 871} 872 873if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 874return-1; 875 876if(o->merge && src[0]) { 877if(ce_stage(src[0])) 878mark_ce_used_same_name(src[0], o); 879else 880mark_ce_used(src[0], o); 881} 882 883/* Now handle any directories.. */ 884if(dirmask) { 885/* special case: "diff-index --cached" looking at a tree */ 886if(o->diff_index_cached && 887 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 888int matches; 889 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 890 names, info); 891/* 892 * Everything under the name matches; skip the 893 * entire hierarchy. diff_index_cached codepath 894 * special cases D/F conflicts in such a way that 895 * it does not do any look-ahead, so this is safe. 896 */ 897if(matches) { 898 o->cache_bottom += matches; 899return mask; 900} 901} 902 903if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 904 names, info) <0) 905return-1; 906return mask; 907} 908 909return mask; 910} 911 912static intclear_ce_flags_1(struct cache_entry **cache,int nr, 913struct strbuf *prefix, 914int select_mask,int clear_mask, 915struct exclude_list *el,int defval); 916 917/* Whole directory matching */ 918static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 919struct strbuf *prefix, 920char*basename, 921int select_mask,int clear_mask, 922struct exclude_list *el,int defval) 923{ 924struct cache_entry **cache_end; 925int dtype = DT_DIR; 926int ret =is_excluded_from_list(prefix->buf, prefix->len, 927 basename, &dtype, el); 928int rc; 929 930strbuf_addch(prefix,'/'); 931 932/* If undecided, use matching result of parent dir in defval */ 933if(ret <0) 934 ret = defval; 935 936for(cache_end = cache; cache_end != cache + nr; cache_end++) { 937struct cache_entry *ce = *cache_end; 938if(strncmp(ce->name, prefix->buf, prefix->len)) 939break; 940} 941 942/* 943 * TODO: check el, if there are no patterns that may conflict 944 * with ret (iow, we know in advance the incl/excl 945 * decision for the entire directory), clear flag here without 946 * calling clear_ce_flags_1(). That function will call 947 * the expensive is_excluded_from_list() on every entry. 948 */ 949 rc =clear_ce_flags_1(cache, cache_end - cache, 950 prefix, 951 select_mask, clear_mask, 952 el, ret); 953strbuf_setlen(prefix, prefix->len -1); 954return rc; 955} 956 957/* 958 * Traverse the index, find every entry that matches according to 959 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the 960 * number of traversed entries. 961 * 962 * If select_mask is non-zero, only entries whose ce_flags has on of 963 * those bits enabled are traversed. 964 * 965 * cache : pointer to an index entry 966 * prefix_len : an offset to its path 967 * 968 * The current path ("prefix") including the trailing '/' is 969 * cache[0]->name[0..(prefix_len-1)] 970 * Top level path has prefix_len zero. 971 */ 972static intclear_ce_flags_1(struct cache_entry **cache,int nr, 973struct strbuf *prefix, 974int select_mask,int clear_mask, 975struct exclude_list *el,int defval) 976{ 977struct cache_entry **cache_end = cache + nr; 978 979/* 980 * Process all entries that have the given prefix and meet 981 * select_mask condition 982 */ 983while(cache != cache_end) { 984struct cache_entry *ce = *cache; 985const char*name, *slash; 986int len, dtype, ret; 987 988if(select_mask && !(ce->ce_flags & select_mask)) { 989 cache++; 990continue; 991} 992 993if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len)) 994break; 995 996 name = ce->name + prefix->len; 997 slash =strchr(name,'/'); 998 999/* If it's a directory, try whole directory match first */1000if(slash) {1001int processed;10021003 len = slash - name;1004strbuf_add(prefix, name, len);10051006 processed =clear_ce_flags_dir(cache, cache_end - cache,1007 prefix,1008 prefix->buf + prefix->len - len,1009 select_mask, clear_mask,1010 el, defval);10111012/* clear_c_f_dir eats a whole dir already? */1013if(processed) {1014 cache += processed;1015strbuf_setlen(prefix, prefix->len - len);1016continue;1017}10181019strbuf_addch(prefix,'/');1020 cache +=clear_ce_flags_1(cache, cache_end - cache,1021 prefix,1022 select_mask, clear_mask, el, defval);1023strbuf_setlen(prefix, prefix->len - len -1);1024continue;1025}10261027/* Non-directory */1028 dtype =ce_to_dtype(ce);1029 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1030 name, &dtype, el);1031if(ret <0)1032 ret = defval;1033if(ret >0)1034 ce->ce_flags &= ~clear_mask;1035 cache++;1036}1037return nr - (cache_end - cache);1038}10391040static intclear_ce_flags(struct cache_entry **cache,int nr,1041int select_mask,int clear_mask,1042struct exclude_list *el)1043{1044static struct strbuf prefix = STRBUF_INIT;10451046strbuf_reset(&prefix);10471048returnclear_ce_flags_1(cache, nr,1049&prefix,1050 select_mask, clear_mask,1051 el,0);1052}10531054/*1055 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1056 */1057static voidmark_new_skip_worktree(struct exclude_list *el,1058struct index_state *the_index,1059int select_flag,int skip_wt_flag)1060{1061int i;10621063/*1064 * 1. Pretend the narrowest worktree: only unmerged entries1065 * are checked out1066 */1067for(i =0; i < the_index->cache_nr; i++) {1068struct cache_entry *ce = the_index->cache[i];10691070if(select_flag && !(ce->ce_flags & select_flag))1071continue;10721073if(!ce_stage(ce))1074 ce->ce_flags |= skip_wt_flag;1075else1076 ce->ce_flags &= ~skip_wt_flag;1077}10781079/*1080 * 2. Widen worktree according to sparse-checkout file.1081 * Matched entries will have skip_wt_flag cleared (i.e. "in")1082 */1083clear_ce_flags(the_index->cache, the_index->cache_nr,1084 select_flag, skip_wt_flag, el);1085}10861087static intverify_absent(const struct cache_entry *,1088enum unpack_trees_error_types,1089struct unpack_trees_options *);1090/*1091 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1092 * resulting index, -2 on failure to reflect the changes to the work tree.1093 *1094 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1095 */1096intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1097{1098int i, ret;1099static struct cache_entry *dfc;1100struct exclude_list el;11011102if(len > MAX_UNPACK_TREES)1103die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);11041105memset(&el,0,sizeof(el));1106if(!core_apply_sparse_checkout || !o->update)1107 o->skip_sparse_checkout =1;1108if(!o->skip_sparse_checkout) {1109char*sparse =git_pathdup("info/sparse-checkout");1110if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1111 o->skip_sparse_checkout =1;1112else1113 o->el = ⪙1114free(sparse);1115}11161117memset(&o->result,0,sizeof(o->result));1118 o->result.initialized =1;1119 o->result.timestamp.sec = o->src_index->timestamp.sec;1120 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1121 o->result.version = o->src_index->version;1122 o->result.split_index = o->src_index->split_index;1123if(o->result.split_index)1124 o->result.split_index->refcount++;1125hashcpy(o->result.sha1, o->src_index->sha1);1126 o->merge_size = len;1127mark_all_ce_unused(o->src_index);11281129/*1130 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1131 */1132if(!o->skip_sparse_checkout)1133mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);11341135if(!dfc)1136 dfc =xcalloc(1,cache_entry_size(0));1137 o->df_conflict_entry = dfc;11381139if(len) {1140const char*prefix = o->prefix ? o->prefix :"";1141struct traverse_info info;11421143setup_traverse_info(&info, prefix);1144 info.fn = unpack_callback;1145 info.data = o;1146 info.show_all_errors = o->show_all_errors;1147 info.pathspec = o->pathspec;11481149if(o->prefix) {1150/*1151 * Unpack existing index entries that sort before the1152 * prefix the tree is spliced into. Note that o->merge1153 * is always true in this case.1154 */1155while(1) {1156struct cache_entry *ce =next_cache_entry(o);1157if(!ce)1158break;1159if(ce_in_traverse_path(ce, &info))1160break;1161if(unpack_index_entry(ce, o) <0)1162goto return_failed;1163}1164}11651166if(traverse_trees(len, t, &info) <0)1167goto return_failed;1168}11691170/* Any left-over entries in the index? */1171if(o->merge) {1172while(1) {1173struct cache_entry *ce =next_cache_entry(o);1174if(!ce)1175break;1176if(unpack_index_entry(ce, o) <0)1177goto return_failed;1178}1179}1180mark_all_ce_unused(o->src_index);11811182if(o->trivial_merges_only && o->nontrivial_merge) {1183 ret =unpack_failed(o,"Merge requires file-level merging");1184goto done;1185}11861187if(!o->skip_sparse_checkout) {1188int empty_worktree =1;11891190/*1191 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11192 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1193 * so apply_sparse_checkout() won't attempt to remove it from worktree1194 */1195mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);11961197 ret =0;1198for(i =0; i < o->result.cache_nr; i++) {1199struct cache_entry *ce = o->result.cache[i];12001201/*1202 * Entries marked with CE_ADDED in merged_entry() do not have1203 * verify_absent() check (the check is effectively disabled1204 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1205 *1206 * Do the real check now because we have had1207 * correct CE_NEW_SKIP_WORKTREE1208 */1209if(ce->ce_flags & CE_ADDED &&1210verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1211if(!o->show_all_errors)1212goto return_failed;1213 ret = -1;1214}12151216if(apply_sparse_checkout(&o->result, ce, o)) {1217if(!o->show_all_errors)1218goto return_failed;1219 ret = -1;1220}1221if(!ce_skip_worktree(ce))1222 empty_worktree =0;12231224}1225if(ret <0)1226goto return_failed;1227/*1228 * Sparse checkout is meant to narrow down checkout area1229 * but it does not make sense to narrow down to empty working1230 * tree. This is usually a mistake in sparse checkout rules.1231 * Do not allow users to do that.1232 */1233if(o->result.cache_nr && empty_worktree) {1234 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1235goto done;1236}1237}12381239 o->src_index = NULL;1240 ret =check_updates(o) ? (-2) :0;1241if(o->dst_index) {1242if(!ret) {1243if(!o->result.cache_tree)1244 o->result.cache_tree =cache_tree();1245if(!cache_tree_fully_valid(o->result.cache_tree))1246cache_tree_update(&o->result,1247 WRITE_TREE_SILENT |1248 WRITE_TREE_REPAIR);1249}1250discard_index(o->dst_index);1251*o->dst_index = o->result;1252}else{1253discard_index(&o->result);1254}12551256done:1257clear_exclude_list(&el);1258return ret;12591260return_failed:1261if(o->show_all_errors)1262display_error_msgs(o);1263mark_all_ce_unused(o->src_index);1264 ret =unpack_failed(o, NULL);1265if(o->exiting_early)1266 ret =0;1267goto done;1268}12691270/* Here come the merge functions */12711272static intreject_merge(const struct cache_entry *ce,1273struct unpack_trees_options *o)1274{1275return o->gently ? -1:1276add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1277}12781279static intsame(const struct cache_entry *a,const struct cache_entry *b)1280{1281if(!!a != !!b)1282return0;1283if(!a && !b)1284return1;1285if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1286return0;1287return a->ce_mode == b->ce_mode &&1288!oidcmp(&a->oid, &b->oid);1289}129012911292/*1293 * When a CE gets turned into an unmerged entry, we1294 * want it to be up-to-date1295 */1296static intverify_uptodate_1(const struct cache_entry *ce,1297struct unpack_trees_options *o,1298enum unpack_trees_error_types error_type)1299{1300struct stat st;13011302if(o->index_only)1303return0;13041305/*1306 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1307 * if this entry is truly up-to-date because this file may be1308 * overwritten.1309 */1310if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1311;/* keep checking */1312else if(o->reset ||ce_uptodate(ce))1313return0;13141315if(!lstat(ce->name, &st)) {1316int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1317unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1318if(!changed)1319return0;1320/*1321 * NEEDSWORK: the current default policy is to allow1322 * submodule to be out of sync wrt the superproject1323 * index. This needs to be tightened later for1324 * submodules that are marked to be automatically1325 * checked out.1326 */1327if(S_ISGITLINK(ce->ce_mode))1328return0;1329 errno =0;1330}1331if(errno == ENOENT)1332return0;1333return o->gently ? -1:1334add_rejected_path(o, error_type, ce->name);1335}13361337static intverify_uptodate(const struct cache_entry *ce,1338struct unpack_trees_options *o)1339{1340if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1341return0;1342returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1343}13441345static intverify_uptodate_sparse(const struct cache_entry *ce,1346struct unpack_trees_options *o)1347{1348returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1349}13501351static voidinvalidate_ce_path(const struct cache_entry *ce,1352struct unpack_trees_options *o)1353{1354if(!ce)1355return;1356cache_tree_invalidate_path(o->src_index, ce->name);1357untracked_cache_invalidate_path(o->src_index, ce->name);1358}13591360/*1361 * Check that checking out ce->sha1 in subdir ce->name is not1362 * going to overwrite any working files.1363 *1364 * Currently, git does not checkout subprojects during a superproject1365 * checkout, so it is not going to overwrite anything.1366 */1367static intverify_clean_submodule(const struct cache_entry *ce,1368enum unpack_trees_error_types error_type,1369struct unpack_trees_options *o)1370{1371return0;1372}13731374static intverify_clean_subdirectory(const struct cache_entry *ce,1375enum unpack_trees_error_types error_type,1376struct unpack_trees_options *o)1377{1378/*1379 * we are about to extract "ce->name"; we would not want to lose1380 * anything in the existing directory there.1381 */1382int namelen;1383int i;1384struct dir_struct d;1385char*pathbuf;1386int cnt =0;1387unsigned char sha1[20];13881389if(S_ISGITLINK(ce->ce_mode) &&1390resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1391/* If we are not going to update the submodule, then1392 * we don't care.1393 */1394if(!hashcmp(sha1, ce->oid.hash))1395return0;1396returnverify_clean_submodule(ce, error_type, o);1397}13981399/*1400 * First let's make sure we do not have a local modification1401 * in that directory.1402 */1403 namelen =ce_namelen(ce);1404for(i =locate_in_src_index(ce, o);1405 i < o->src_index->cache_nr;1406 i++) {1407struct cache_entry *ce2 = o->src_index->cache[i];1408int len =ce_namelen(ce2);1409if(len < namelen ||1410strncmp(ce->name, ce2->name, namelen) ||1411 ce2->name[namelen] !='/')1412break;1413/*1414 * ce2->name is an entry in the subdirectory to be1415 * removed.1416 */1417if(!ce_stage(ce2)) {1418if(verify_uptodate(ce2, o))1419return-1;1420add_entry(o, ce2, CE_REMOVE,0);1421mark_ce_used(ce2, o);1422}1423 cnt++;1424}14251426/*1427 * Then we need to make sure that we do not lose a locally1428 * present file that is not ignored.1429 */1430 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14311432memset(&d,0,sizeof(d));1433if(o->dir)1434 d.exclude_per_dir = o->dir->exclude_per_dir;1435 i =read_directory(&d, pathbuf, namelen+1, NULL);1436if(i)1437return o->gently ? -1:1438add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1439free(pathbuf);1440return cnt;1441}14421443/*1444 * This gets called when there was no index entry for the tree entry 'dst',1445 * but we found a file in the working tree that 'lstat()' said was fine,1446 * and we're on a case-insensitive filesystem.1447 *1448 * See if we can find a case-insensitive match in the index that also1449 * matches the stat information, and assume it's that other file!1450 */1451static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1452{1453const struct cache_entry *src;14541455 src =index_file_exists(o->src_index, name, len,1);1456return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1457}14581459static intcheck_ok_to_remove(const char*name,int len,int dtype,1460const struct cache_entry *ce,struct stat *st,1461enum unpack_trees_error_types error_type,1462struct unpack_trees_options *o)1463{1464const struct cache_entry *result;14651466/*1467 * It may be that the 'lstat()' succeeded even though1468 * target 'ce' was absent, because there is an old1469 * entry that is different only in case..1470 *1471 * Ignore that lstat() if it matches.1472 */1473if(ignore_case &&icase_exists(o, name, len, st))1474return0;14751476if(o->dir &&1477is_excluded(o->dir, name, &dtype))1478/*1479 * ce->name is explicitly excluded, so it is Ok to1480 * overwrite it.1481 */1482return0;1483if(S_ISDIR(st->st_mode)) {1484/*1485 * We are checking out path "foo" and1486 * found "foo/." in the working tree.1487 * This is tricky -- if we have modified1488 * files that are in "foo/" we would lose1489 * them.1490 */1491if(verify_clean_subdirectory(ce, error_type, o) <0)1492return-1;1493return0;1494}14951496/*1497 * The previous round may already have decided to1498 * delete this path, which is in a subdirectory that1499 * is being replaced with a blob.1500 */1501 result =index_file_exists(&o->result, name, len,0);1502if(result) {1503if(result->ce_flags & CE_REMOVE)1504return0;1505}15061507return o->gently ? -1:1508add_rejected_path(o, error_type, name);1509}15101511/*1512 * We do not want to remove or overwrite a working tree file that1513 * is not tracked, unless it is ignored.1514 */1515static intverify_absent_1(const struct cache_entry *ce,1516enum unpack_trees_error_types error_type,1517struct unpack_trees_options *o)1518{1519int len;1520struct stat st;15211522if(o->index_only || o->reset || !o->update)1523return0;15241525 len =check_leading_path(ce->name,ce_namelen(ce));1526if(!len)1527return0;1528else if(len >0) {1529char*path;1530int ret;15311532 path =xmemdupz(ce->name, len);1533if(lstat(path, &st))1534 ret =error_errno("cannot stat '%s'", path);1535else1536 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1537&st, error_type, o);1538free(path);1539return ret;1540}else if(lstat(ce->name, &st)) {1541if(errno != ENOENT)1542returnerror_errno("cannot stat '%s'", ce->name);1543return0;1544}else{1545returncheck_ok_to_remove(ce->name,ce_namelen(ce),1546ce_to_dtype(ce), ce, &st,1547 error_type, o);1548}1549}15501551static intverify_absent(const struct cache_entry *ce,1552enum unpack_trees_error_types error_type,1553struct unpack_trees_options *o)1554{1555if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1556return0;1557returnverify_absent_1(ce, error_type, o);1558}15591560static intverify_absent_sparse(const struct cache_entry *ce,1561enum unpack_trees_error_types error_type,1562struct unpack_trees_options *o)1563{1564enum unpack_trees_error_types orphaned_error = error_type;1565if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1566 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;15671568returnverify_absent_1(ce, orphaned_error, o);1569}15701571static intmerged_entry(const struct cache_entry *ce,1572const struct cache_entry *old,1573struct unpack_trees_options *o)1574{1575int update = CE_UPDATE;1576struct cache_entry *merge =dup_entry(ce);15771578if(!old) {1579/*1580 * New index entries. In sparse checkout, the following1581 * verify_absent() will be delayed until after1582 * traverse_trees() finishes in unpack_trees(), then:1583 *1584 * - CE_NEW_SKIP_WORKTREE will be computed correctly1585 * - verify_absent() be called again, this time with1586 * correct CE_NEW_SKIP_WORKTREE1587 *1588 * verify_absent() call here does nothing in sparse1589 * checkout (i.e. o->skip_sparse_checkout == 0)1590 */1591 update |= CE_ADDED;1592 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;15931594if(verify_absent(merge,1595 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1596free(merge);1597return-1;1598}1599invalidate_ce_path(merge, o);1600}else if(!(old->ce_flags & CE_CONFLICTED)) {1601/*1602 * See if we can re-use the old CE directly?1603 * That way we get the uptodate stat info.1604 *1605 * This also removes the UPDATE flag on a match; otherwise1606 * we will end up overwriting local changes in the work tree.1607 */1608if(same(old, merge)) {1609copy_cache_entry(merge, old);1610 update =0;1611}else{1612if(verify_uptodate(old, o)) {1613free(merge);1614return-1;1615}1616/* Migrate old flags over */1617 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1618invalidate_ce_path(old, o);1619}1620}else{1621/*1622 * Previously unmerged entry left as an existence1623 * marker by read_index_unmerged();1624 */1625invalidate_ce_path(old, o);1626}16271628do_add_entry(o, merge, update, CE_STAGEMASK);1629return1;1630}16311632static intdeleted_entry(const struct cache_entry *ce,1633const struct cache_entry *old,1634struct unpack_trees_options *o)1635{1636/* Did it exist in the index? */1637if(!old) {1638if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1639return-1;1640return0;1641}1642if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1643return-1;1644add_entry(o, ce, CE_REMOVE,0);1645invalidate_ce_path(ce, o);1646return1;1647}16481649static intkeep_entry(const struct cache_entry *ce,1650struct unpack_trees_options *o)1651{1652add_entry(o, ce,0,0);1653return1;1654}16551656#if DBRT_DEBUG1657static voidshow_stage_entry(FILE*o,1658const char*label,const struct cache_entry *ce)1659{1660if(!ce)1661fprintf(o,"%s(missing)\n", label);1662else1663fprintf(o,"%s%06o%s %d\t%s\n",1664 label,1665 ce->ce_mode,1666oid_to_hex(&ce->oid),1667ce_stage(ce),1668 ce->name);1669}1670#endif16711672intthreeway_merge(const struct cache_entry *const*stages,1673struct unpack_trees_options *o)1674{1675const struct cache_entry *index;1676const struct cache_entry *head;1677const struct cache_entry *remote = stages[o->head_idx +1];1678int count;1679int head_match =0;1680int remote_match =0;16811682int df_conflict_head =0;1683int df_conflict_remote =0;16841685int any_anc_missing =0;1686int no_anc_exists =1;1687int i;16881689for(i =1; i < o->head_idx; i++) {1690if(!stages[i] || stages[i] == o->df_conflict_entry)1691 any_anc_missing =1;1692else1693 no_anc_exists =0;1694}16951696 index = stages[0];1697 head = stages[o->head_idx];16981699if(head == o->df_conflict_entry) {1700 df_conflict_head =1;1701 head = NULL;1702}17031704if(remote == o->df_conflict_entry) {1705 df_conflict_remote =1;1706 remote = NULL;1707}17081709/*1710 * First, if there's a #16 situation, note that to prevent #131711 * and #14.1712 */1713if(!same(remote, head)) {1714for(i =1; i < o->head_idx; i++) {1715if(same(stages[i], head)) {1716 head_match = i;1717}1718if(same(stages[i], remote)) {1719 remote_match = i;1720}1721}1722}17231724/*1725 * We start with cases where the index is allowed to match1726 * something other than the head: #14(ALT) and #2ALT, where it1727 * is permitted to match the result instead.1728 */1729/* #14, #14ALT, #2ALT */1730if(remote && !df_conflict_head && head_match && !remote_match) {1731if(index && !same(index, remote) && !same(index, head))1732returnreject_merge(index, o);1733returnmerged_entry(remote, index, o);1734}1735/*1736 * If we have an entry in the index cache, then we want to1737 * make sure that it matches head.1738 */1739if(index && !same(index, head))1740returnreject_merge(index, o);17411742if(head) {1743/* #5ALT, #15 */1744if(same(head, remote))1745returnmerged_entry(head, index, o);1746/* #13, #3ALT */1747if(!df_conflict_remote && remote_match && !head_match)1748returnmerged_entry(head, index, o);1749}17501751/* #1 */1752if(!head && !remote && any_anc_missing)1753return0;17541755/*1756 * Under the "aggressive" rule, we resolve mostly trivial1757 * cases that we historically had git-merge-one-file resolve.1758 */1759if(o->aggressive) {1760int head_deleted = !head;1761int remote_deleted = !remote;1762const struct cache_entry *ce = NULL;17631764if(index)1765 ce = index;1766else if(head)1767 ce = head;1768else if(remote)1769 ce = remote;1770else{1771for(i =1; i < o->head_idx; i++) {1772if(stages[i] && stages[i] != o->df_conflict_entry) {1773 ce = stages[i];1774break;1775}1776}1777}17781779/*1780 * Deleted in both.1781 * Deleted in one and unchanged in the other.1782 */1783if((head_deleted && remote_deleted) ||1784(head_deleted && remote && remote_match) ||1785(remote_deleted && head && head_match)) {1786if(index)1787returndeleted_entry(index, index, o);1788if(ce && !head_deleted) {1789if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1790return-1;1791}1792return0;1793}1794/*1795 * Added in both, identically.1796 */1797if(no_anc_exists && head && remote &&same(head, remote))1798returnmerged_entry(head, index, o);17991800}18011802/* Below are "no merge" cases, which require that the index be1803 * up-to-date to avoid the files getting overwritten with1804 * conflict resolution files.1805 */1806if(index) {1807if(verify_uptodate(index, o))1808return-1;1809}18101811 o->nontrivial_merge =1;18121813/* #2, #3, #4, #6, #7, #9, #10, #11. */1814 count =0;1815if(!head_match || !remote_match) {1816for(i =1; i < o->head_idx; i++) {1817if(stages[i] && stages[i] != o->df_conflict_entry) {1818keep_entry(stages[i], o);1819 count++;1820break;1821}1822}1823}1824#if DBRT_DEBUG1825else{1826fprintf(stderr,"read-tree: warning #16 detected\n");1827show_stage_entry(stderr,"head ", stages[head_match]);1828show_stage_entry(stderr,"remote ", stages[remote_match]);1829}1830#endif1831if(head) { count +=keep_entry(head, o); }1832if(remote) { count +=keep_entry(remote, o); }1833return count;1834}18351836/*1837 * Two-way merge.1838 *1839 * The rule is to "carry forward" what is in the index without losing1840 * information across a "fast-forward", favoring a successful merge1841 * over a merge failure when it makes sense. For details of the1842 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1843 *1844 */1845inttwoway_merge(const struct cache_entry *const*src,1846struct unpack_trees_options *o)1847{1848const struct cache_entry *current = src[0];1849const struct cache_entry *oldtree = src[1];1850const struct cache_entry *newtree = src[2];18511852if(o->merge_size !=2)1853returnerror("Cannot do a twoway merge of%dtrees",1854 o->merge_size);18551856if(oldtree == o->df_conflict_entry)1857 oldtree = NULL;1858if(newtree == o->df_conflict_entry)1859 newtree = NULL;18601861if(current) {1862if(current->ce_flags & CE_CONFLICTED) {1863if(same(oldtree, newtree) || o->reset) {1864if(!newtree)1865returndeleted_entry(current, current, o);1866else1867returnmerged_entry(newtree, current, o);1868}1869returnreject_merge(current, o);1870}else if((!oldtree && !newtree) ||/* 4 and 5 */1871(!oldtree && newtree &&1872same(current, newtree)) ||/* 6 and 7 */1873(oldtree && newtree &&1874same(oldtree, newtree)) ||/* 14 and 15 */1875(oldtree && newtree &&1876!same(oldtree, newtree) &&/* 18 and 19 */1877same(current, newtree))) {1878returnkeep_entry(current, o);1879}else if(oldtree && !newtree &&same(current, oldtree)) {1880/* 10 or 11 */1881returndeleted_entry(oldtree, current, o);1882}else if(oldtree && newtree &&1883same(current, oldtree) && !same(current, newtree)) {1884/* 20 or 21 */1885returnmerged_entry(newtree, current, o);1886}else1887returnreject_merge(current, o);1888}1889else if(newtree) {1890if(oldtree && !o->initial_checkout) {1891/*1892 * deletion of the path was staged;1893 */1894if(same(oldtree, newtree))1895return1;1896returnreject_merge(oldtree, o);1897}1898returnmerged_entry(newtree, current, o);1899}1900returndeleted_entry(oldtree, current, o);1901}19021903/*1904 * Bind merge.1905 *1906 * Keep the index entries at stage0, collapse stage1 but make sure1907 * stage0 does not have anything there.1908 */1909intbind_merge(const struct cache_entry *const*src,1910struct unpack_trees_options *o)1911{1912const struct cache_entry *old = src[0];1913const struct cache_entry *a = src[1];19141915if(o->merge_size !=1)1916returnerror("Cannot do a bind merge of%dtrees",1917 o->merge_size);1918if(a && old)1919return o->gently ? -1:1920error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);1921if(!a)1922returnkeep_entry(old, o);1923else1924returnmerged_entry(a, NULL, o);1925}19261927/*1928 * One-way merge.1929 *1930 * The rule is:1931 * - take the stat information from stage0, take the data from stage11932 */1933intoneway_merge(const struct cache_entry *const*src,1934struct unpack_trees_options *o)1935{1936const struct cache_entry *old = src[0];1937const struct cache_entry *a = src[1];19381939if(o->merge_size !=1)1940returnerror("Cannot do a oneway merge of%dtrees",1941 o->merge_size);19421943if(!a || a == o->df_conflict_entry)1944returndeleted_entry(old, old, o);19451946if(old &&same(old, a)) {1947int update =0;1948if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1949struct stat st;1950if(lstat(old->name, &st) ||1951ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))1952 update |= CE_UPDATE;1953}1954add_entry(o, old, update,0);1955return0;1956}1957returnmerged_entry(a, old, o);1958}