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 them:\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 struct progress *get_progress(struct unpack_trees_options *o) 222{ 223unsigned cnt =0, total =0; 224struct index_state *index = &o->result; 225 226if(!o->update || !o->verbose_update) 227return NULL; 228 229for(; cnt < index->cache_nr; cnt++) { 230const struct cache_entry *ce = index->cache[cnt]; 231if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 232 total++; 233} 234 235returnstart_progress_delay(_("Checking out files"), 236 total,50,1); 237} 238 239static intcheck_updates(struct unpack_trees_options *o) 240{ 241unsigned cnt =0; 242int errs =0; 243struct progress *progress = NULL; 244struct index_state *index = &o->result; 245struct checkout state = CHECKOUT_INIT; 246int i; 247 248 state.force =1; 249 state.quiet =1; 250 state.refresh_cache =1; 251 state.istate = index; 252 253 progress =get_progress(o); 254 255if(o->update) 256git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 257for(i =0; i < index->cache_nr; i++) { 258const struct cache_entry *ce = index->cache[i]; 259 260if(ce->ce_flags & CE_WT_REMOVE) { 261display_progress(progress, ++cnt); 262if(o->update && !o->dry_run) 263unlink_entry(ce); 264} 265} 266remove_marked_cache_entries(index); 267remove_scheduled_dirs(); 268 269for(i =0; i < index->cache_nr; i++) { 270struct cache_entry *ce = index->cache[i]; 271 272if(ce->ce_flags & CE_UPDATE) { 273if(ce->ce_flags & CE_WT_REMOVE) 274die("BUG: both update and delete flags are set on%s", 275 ce->name); 276display_progress(progress, ++cnt); 277 ce->ce_flags &= ~CE_UPDATE; 278if(o->update && !o->dry_run) { 279 errs |=checkout_entry(ce, &state, NULL); 280} 281} 282} 283stop_progress(&progress); 284if(o->update) 285git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 286return errs !=0; 287} 288 289static intverify_uptodate_sparse(const struct cache_entry *ce, 290struct unpack_trees_options *o); 291static intverify_absent_sparse(const struct cache_entry *ce, 292enum unpack_trees_error_types, 293struct unpack_trees_options *o); 294 295static intapply_sparse_checkout(struct index_state *istate, 296struct cache_entry *ce, 297struct unpack_trees_options *o) 298{ 299int was_skip_worktree =ce_skip_worktree(ce); 300 301if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 302 ce->ce_flags |= CE_SKIP_WORKTREE; 303else 304 ce->ce_flags &= ~CE_SKIP_WORKTREE; 305if(was_skip_worktree !=ce_skip_worktree(ce)) { 306 ce->ce_flags |= CE_UPDATE_IN_BASE; 307 istate->cache_changed |= CE_ENTRY_CHANGED; 308} 309 310/* 311 * if (!was_skip_worktree && !ce_skip_worktree()) { 312 * This is perfectly normal. Move on; 313 * } 314 */ 315 316/* 317 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 318 * area as a result of ce_skip_worktree() shortcuts in 319 * verify_absent() and verify_uptodate(). 320 * Make sure they don't modify worktree if they are already 321 * outside checkout area 322 */ 323if(was_skip_worktree &&ce_skip_worktree(ce)) { 324 ce->ce_flags &= ~CE_UPDATE; 325 326/* 327 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 328 * on to get that file removed from both index and worktree. 329 * If that file is already outside worktree area, don't 330 * bother remove it. 331 */ 332if(ce->ce_flags & CE_REMOVE) 333 ce->ce_flags &= ~CE_WT_REMOVE; 334} 335 336if(!was_skip_worktree &&ce_skip_worktree(ce)) { 337/* 338 * If CE_UPDATE is set, verify_uptodate() must be called already 339 * also stat info may have lost after merged_entry() so calling 340 * verify_uptodate() again may fail 341 */ 342if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 343return-1; 344 ce->ce_flags |= CE_WT_REMOVE; 345 ce->ce_flags &= ~CE_UPDATE; 346} 347if(was_skip_worktree && !ce_skip_worktree(ce)) { 348if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 349return-1; 350 ce->ce_flags |= CE_UPDATE; 351} 352return0; 353} 354 355staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 356struct unpack_trees_options *o) 357{ 358int ret = o->fn(src, o); 359if(ret >0) 360 ret =0; 361return ret; 362} 363 364static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 365{ 366 ce->ce_flags |= CE_UNPACKED; 367 368if(o->cache_bottom < o->src_index->cache_nr && 369 o->src_index->cache[o->cache_bottom] == ce) { 370int bottom = o->cache_bottom; 371while(bottom < o->src_index->cache_nr && 372 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 373 bottom++; 374 o->cache_bottom = bottom; 375} 376} 377 378static voidmark_all_ce_unused(struct index_state *index) 379{ 380int i; 381for(i =0; i < index->cache_nr; i++) 382 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 383} 384 385static intlocate_in_src_index(const struct cache_entry *ce, 386struct unpack_trees_options *o) 387{ 388struct index_state *index = o->src_index; 389int len =ce_namelen(ce); 390int pos =index_name_pos(index, ce->name, len); 391if(pos <0) 392 pos = -1- pos; 393return pos; 394} 395 396/* 397 * We call unpack_index_entry() with an unmerged cache entry 398 * only in diff-index, and it wants a single callback. Skip 399 * the other unmerged entry with the same name. 400 */ 401static voidmark_ce_used_same_name(struct cache_entry *ce, 402struct unpack_trees_options *o) 403{ 404struct index_state *index = o->src_index; 405int len =ce_namelen(ce); 406int pos; 407 408for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 409struct cache_entry *next = index->cache[pos]; 410if(len !=ce_namelen(next) || 411memcmp(ce->name, next->name, len)) 412break; 413mark_ce_used(next, o); 414} 415} 416 417static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 418{ 419const struct index_state *index = o->src_index; 420int pos = o->cache_bottom; 421 422while(pos < index->cache_nr) { 423struct cache_entry *ce = index->cache[pos]; 424if(!(ce->ce_flags & CE_UNPACKED)) 425return ce; 426 pos++; 427} 428return NULL; 429} 430 431static voidadd_same_unmerged(const struct cache_entry *ce, 432struct unpack_trees_options *o) 433{ 434struct index_state *index = o->src_index; 435int len =ce_namelen(ce); 436int pos =index_name_pos(index, ce->name, len); 437 438if(0<= pos) 439die("programming error in a caller of mark_ce_used_same_name"); 440for(pos = -pos -1; pos < index->cache_nr; pos++) { 441struct cache_entry *next = index->cache[pos]; 442if(len !=ce_namelen(next) || 443memcmp(ce->name, next->name, len)) 444break; 445add_entry(o, next,0,0); 446mark_ce_used(next, o); 447} 448} 449 450static intunpack_index_entry(struct cache_entry *ce, 451struct unpack_trees_options *o) 452{ 453const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 454int ret; 455 456 src[0] = ce; 457 458mark_ce_used(ce, o); 459if(ce_stage(ce)) { 460if(o->skip_unmerged) { 461add_entry(o, ce,0,0); 462return0; 463} 464} 465 ret =call_unpack_fn(src, o); 466if(ce_stage(ce)) 467mark_ce_used_same_name(ce, o); 468return ret; 469} 470 471static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 472 473static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 474{ 475struct unpack_trees_options *o = info->data; 476 477if(o->diff_index_cached) 478return; 479 o->cache_bottom = bottom; 480} 481 482static intswitch_cache_bottom(struct traverse_info *info) 483{ 484struct unpack_trees_options *o = info->data; 485int ret, pos; 486 487if(o->diff_index_cached) 488return0; 489 ret = o->cache_bottom; 490 pos =find_cache_pos(info->prev, &info->name); 491 492if(pos < -1) 493 o->cache_bottom = -2- pos; 494else if(pos <0) 495 o->cache_bottom = o->src_index->cache_nr; 496return ret; 497} 498 499static inttraverse_trees_recursive(int n,unsigned long dirmask, 500unsigned long df_conflicts, 501struct name_entry *names, 502struct traverse_info *info) 503{ 504int i, ret, bottom; 505struct tree_desc t[MAX_UNPACK_TREES]; 506void*buf[MAX_UNPACK_TREES]; 507struct traverse_info newinfo; 508struct name_entry *p; 509 510 p = names; 511while(!p->mode) 512 p++; 513 514 newinfo = *info; 515 newinfo.prev = info; 516 newinfo.pathspec = info->pathspec; 517 newinfo.name = *p; 518 newinfo.pathlen +=tree_entry_len(p) +1; 519 newinfo.df_conflicts |= df_conflicts; 520 521for(i =0; i < n; i++, dirmask >>=1) { 522const unsigned char*sha1 = NULL; 523if(dirmask &1) 524 sha1 = names[i].oid->hash; 525 buf[i] =fill_tree_descriptor(t+i, sha1); 526} 527 528 bottom =switch_cache_bottom(&newinfo); 529 ret =traverse_trees(n, t, &newinfo); 530restore_cache_bottom(&newinfo, bottom); 531 532for(i =0; i < n; i++) 533free(buf[i]); 534 535return ret; 536} 537 538/* 539 * Compare the traverse-path to the cache entry without actually 540 * having to generate the textual representation of the traverse 541 * path. 542 * 543 * NOTE! This *only* compares up to the size of the traverse path 544 * itself - the caller needs to do the final check for the cache 545 * entry having more data at the end! 546 */ 547static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 548{ 549int len, pathlen, ce_len; 550const char*ce_name; 551 552if(info->prev) { 553int cmp =do_compare_entry_piecewise(ce, info->prev, 554&info->name); 555if(cmp) 556return cmp; 557} 558 pathlen = info->pathlen; 559 ce_len =ce_namelen(ce); 560 561/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 562if(ce_len < pathlen) 563return-1; 564 565 ce_len -= pathlen; 566 ce_name = ce->name + pathlen; 567 568 len =tree_entry_len(n); 569returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 570} 571 572static intdo_compare_entry(const struct cache_entry *ce, 573const struct traverse_info *info, 574const struct name_entry *n) 575{ 576int len, pathlen, ce_len; 577const char*ce_name; 578int cmp; 579 580/* 581 * If we have not precomputed the traverse path, it is quicker 582 * to avoid doing so. But if we have precomputed it, 583 * it is quicker to use the precomputed version. 584 */ 585if(!info->traverse_path) 586returndo_compare_entry_piecewise(ce, info, n); 587 588 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 589if(cmp) 590return cmp; 591 592 pathlen = info->pathlen; 593 ce_len =ce_namelen(ce); 594 595if(ce_len < pathlen) 596return-1; 597 598 ce_len -= pathlen; 599 ce_name = ce->name + pathlen; 600 601 len =tree_entry_len(n); 602returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 603} 604 605static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 606{ 607int cmp =do_compare_entry(ce, info, n); 608if(cmp) 609return cmp; 610 611/* 612 * Even if the beginning compared identically, the ce should 613 * compare as bigger than a directory leading up to it! 614 */ 615returnce_namelen(ce) >traverse_path_len(info, n); 616} 617 618static intce_in_traverse_path(const struct cache_entry *ce, 619const struct traverse_info *info) 620{ 621if(!info->prev) 622return1; 623if(do_compare_entry(ce, info->prev, &info->name)) 624return0; 625/* 626 * If ce (blob) is the same name as the path (which is a tree 627 * we will be descending into), it won't be inside it. 628 */ 629return(info->pathlen <ce_namelen(ce)); 630} 631 632static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 633{ 634int len =traverse_path_len(info, n); 635struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 636 637 ce->ce_mode =create_ce_mode(n->mode); 638 ce->ce_flags =create_ce_flags(stage); 639 ce->ce_namelen = len; 640oidcpy(&ce->oid, n->oid); 641make_traverse_path(ce->name, info, n); 642 643return ce; 644} 645 646static intunpack_nondirectories(int n,unsigned long mask, 647unsigned long dirmask, 648struct cache_entry **src, 649const struct name_entry *names, 650const struct traverse_info *info) 651{ 652int i; 653struct unpack_trees_options *o = info->data; 654unsigned long conflicts = info->df_conflicts | dirmask; 655 656/* Do we have *only* directories? Nothing to do */ 657if(mask == dirmask && !src[0]) 658return0; 659 660/* 661 * Ok, we've filled in up to any potential index entry in src[0], 662 * now do the rest. 663 */ 664for(i =0; i < n; i++) { 665int stage; 666unsigned int bit =1ul<< i; 667if(conflicts & bit) { 668 src[i + o->merge] = o->df_conflict_entry; 669continue; 670} 671if(!(mask & bit)) 672continue; 673if(!o->merge) 674 stage =0; 675else if(i +1< o->head_idx) 676 stage =1; 677else if(i +1> o->head_idx) 678 stage =3; 679else 680 stage =2; 681 src[i + o->merge] =create_ce_entry(info, names + i, stage); 682} 683 684if(o->merge) { 685int rc =call_unpack_fn((const struct cache_entry *const*)src, 686 o); 687for(i =0; i < n; i++) { 688struct cache_entry *ce = src[i + o->merge]; 689if(ce != o->df_conflict_entry) 690free(ce); 691} 692return rc; 693} 694 695for(i =0; i < n; i++) 696if(src[i] && src[i] != o->df_conflict_entry) 697if(do_add_entry(o, src[i],0,0)) 698return-1; 699 700return0; 701} 702 703static intunpack_failed(struct unpack_trees_options *o,const char*message) 704{ 705discard_index(&o->result); 706if(!o->gently && !o->exiting_early) { 707if(message) 708returnerror("%s", message); 709return-1; 710} 711return-1; 712} 713 714/* 715 * The tree traversal is looking at name p. If we have a matching entry, 716 * return it. If name p is a directory in the index, do not return 717 * anything, as we will want to match it when the traversal descends into 718 * the directory. 719 */ 720static intfind_cache_pos(struct traverse_info *info, 721const struct name_entry *p) 722{ 723int pos; 724struct unpack_trees_options *o = info->data; 725struct index_state *index = o->src_index; 726int pfxlen = info->pathlen; 727int p_len =tree_entry_len(p); 728 729for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 730const struct cache_entry *ce = index->cache[pos]; 731const char*ce_name, *ce_slash; 732int cmp, ce_len; 733 734if(ce->ce_flags & CE_UNPACKED) { 735/* 736 * cache_bottom entry is already unpacked, so 737 * we can never match it; don't check it 738 * again. 739 */ 740if(pos == o->cache_bottom) 741++o->cache_bottom; 742continue; 743} 744if(!ce_in_traverse_path(ce, info)) { 745/* 746 * Check if we can skip future cache checks 747 * (because we're already past all possible 748 * entries in the traverse path). 749 */ 750if(info->traverse_path) { 751if(strncmp(ce->name, info->traverse_path, 752 info->pathlen) >0) 753break; 754} 755continue; 756} 757 ce_name = ce->name + pfxlen; 758 ce_slash =strchr(ce_name,'/'); 759if(ce_slash) 760 ce_len = ce_slash - ce_name; 761else 762 ce_len =ce_namelen(ce) - pfxlen; 763 cmp =name_compare(p->path, p_len, ce_name, ce_len); 764/* 765 * Exact match; if we have a directory we need to 766 * delay returning it. 767 */ 768if(!cmp) 769return ce_slash ? -2- pos : pos; 770if(0< cmp) 771continue;/* keep looking */ 772/* 773 * ce_name sorts after p->path; could it be that we 774 * have files under p->path directory in the index? 775 * E.g. ce_name == "t-i", and p->path == "t"; we may 776 * have "t/a" in the index. 777 */ 778if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 779 ce_name[p_len] <'/') 780continue;/* keep looking */ 781break; 782} 783return-1; 784} 785 786static struct cache_entry *find_cache_entry(struct traverse_info *info, 787const struct name_entry *p) 788{ 789int pos =find_cache_pos(info, p); 790struct unpack_trees_options *o = info->data; 791 792if(0<= pos) 793return o->src_index->cache[pos]; 794else 795return NULL; 796} 797 798static voiddebug_path(struct traverse_info *info) 799{ 800if(info->prev) { 801debug_path(info->prev); 802if(*info->prev->name.path) 803putchar('/'); 804} 805printf("%s", info->name.path); 806} 807 808static voiddebug_name_entry(int i,struct name_entry *n) 809{ 810printf("ent#%d %06o%s\n", i, 811 n->path ? n->mode :0, 812 n->path ? n->path :"(missing)"); 813} 814 815static voiddebug_unpack_callback(int n, 816unsigned long mask, 817unsigned long dirmask, 818struct name_entry *names, 819struct traverse_info *info) 820{ 821int i; 822printf("* unpack mask%lu, dirmask%lu, cnt%d", 823 mask, dirmask, n); 824debug_path(info); 825putchar('\n'); 826for(i =0; i < n; i++) 827debug_name_entry(i, names + i); 828} 829 830static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 831{ 832struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 833struct unpack_trees_options *o = info->data; 834const struct name_entry *p = names; 835 836/* Find first entry with a real name (we could use "mask" too) */ 837while(!p->mode) 838 p++; 839 840if(o->debug_unpack) 841debug_unpack_callback(n, mask, dirmask, names, info); 842 843/* Are we supposed to look at the index too? */ 844if(o->merge) { 845while(1) { 846int cmp; 847struct cache_entry *ce; 848 849if(o->diff_index_cached) 850 ce =next_cache_entry(o); 851else 852 ce =find_cache_entry(info, p); 853 854if(!ce) 855break; 856 cmp =compare_entry(ce, info, p); 857if(cmp <0) { 858if(unpack_index_entry(ce, o) <0) 859returnunpack_failed(o, NULL); 860continue; 861} 862if(!cmp) { 863if(ce_stage(ce)) { 864/* 865 * If we skip unmerged index 866 * entries, we'll skip this 867 * entry *and* the tree 868 * entries associated with it! 869 */ 870if(o->skip_unmerged) { 871add_same_unmerged(ce, o); 872return mask; 873} 874} 875 src[0] = ce; 876} 877break; 878} 879} 880 881if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 882return-1; 883 884if(o->merge && src[0]) { 885if(ce_stage(src[0])) 886mark_ce_used_same_name(src[0], o); 887else 888mark_ce_used(src[0], o); 889} 890 891/* Now handle any directories.. */ 892if(dirmask) { 893/* special case: "diff-index --cached" looking at a tree */ 894if(o->diff_index_cached && 895 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 896int matches; 897 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 898 names, info); 899/* 900 * Everything under the name matches; skip the 901 * entire hierarchy. diff_index_cached codepath 902 * special cases D/F conflicts in such a way that 903 * it does not do any look-ahead, so this is safe. 904 */ 905if(matches) { 906 o->cache_bottom += matches; 907return mask; 908} 909} 910 911if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 912 names, info) <0) 913return-1; 914return mask; 915} 916 917return mask; 918} 919 920static intclear_ce_flags_1(struct cache_entry **cache,int nr, 921struct strbuf *prefix, 922int select_mask,int clear_mask, 923struct exclude_list *el,int defval); 924 925/* Whole directory matching */ 926static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 927struct strbuf *prefix, 928char*basename, 929int select_mask,int clear_mask, 930struct exclude_list *el,int defval) 931{ 932struct cache_entry **cache_end; 933int dtype = DT_DIR; 934int ret =is_excluded_from_list(prefix->buf, prefix->len, 935 basename, &dtype, el); 936int rc; 937 938strbuf_addch(prefix,'/'); 939 940/* If undecided, use matching result of parent dir in defval */ 941if(ret <0) 942 ret = defval; 943 944for(cache_end = cache; cache_end != cache + nr; cache_end++) { 945struct cache_entry *ce = *cache_end; 946if(strncmp(ce->name, prefix->buf, prefix->len)) 947break; 948} 949 950/* 951 * TODO: check el, if there are no patterns that may conflict 952 * with ret (iow, we know in advance the incl/excl 953 * decision for the entire directory), clear flag here without 954 * calling clear_ce_flags_1(). That function will call 955 * the expensive is_excluded_from_list() on every entry. 956 */ 957 rc =clear_ce_flags_1(cache, cache_end - cache, 958 prefix, 959 select_mask, clear_mask, 960 el, ret); 961strbuf_setlen(prefix, prefix->len -1); 962return rc; 963} 964 965/* 966 * Traverse the index, find every entry that matches according to 967 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the 968 * number of traversed entries. 969 * 970 * If select_mask is non-zero, only entries whose ce_flags has on of 971 * those bits enabled are traversed. 972 * 973 * cache : pointer to an index entry 974 * prefix_len : an offset to its path 975 * 976 * The current path ("prefix") including the trailing '/' is 977 * cache[0]->name[0..(prefix_len-1)] 978 * Top level path has prefix_len zero. 979 */ 980static intclear_ce_flags_1(struct cache_entry **cache,int nr, 981struct strbuf *prefix, 982int select_mask,int clear_mask, 983struct exclude_list *el,int defval) 984{ 985struct cache_entry **cache_end = cache + nr; 986 987/* 988 * Process all entries that have the given prefix and meet 989 * select_mask condition 990 */ 991while(cache != cache_end) { 992struct cache_entry *ce = *cache; 993const char*name, *slash; 994int len, dtype, ret; 995 996if(select_mask && !(ce->ce_flags & select_mask)) { 997 cache++; 998continue; 999}10001001if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1002break;10031004 name = ce->name + prefix->len;1005 slash =strchr(name,'/');10061007/* If it's a directory, try whole directory match first */1008if(slash) {1009int processed;10101011 len = slash - name;1012strbuf_add(prefix, name, len);10131014 processed =clear_ce_flags_dir(cache, cache_end - cache,1015 prefix,1016 prefix->buf + prefix->len - len,1017 select_mask, clear_mask,1018 el, defval);10191020/* clear_c_f_dir eats a whole dir already? */1021if(processed) {1022 cache += processed;1023strbuf_setlen(prefix, prefix->len - len);1024continue;1025}10261027strbuf_addch(prefix,'/');1028 cache +=clear_ce_flags_1(cache, cache_end - cache,1029 prefix,1030 select_mask, clear_mask, el, defval);1031strbuf_setlen(prefix, prefix->len - len -1);1032continue;1033}10341035/* Non-directory */1036 dtype =ce_to_dtype(ce);1037 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1038 name, &dtype, el);1039if(ret <0)1040 ret = defval;1041if(ret >0)1042 ce->ce_flags &= ~clear_mask;1043 cache++;1044}1045return nr - (cache_end - cache);1046}10471048static intclear_ce_flags(struct cache_entry **cache,int nr,1049int select_mask,int clear_mask,1050struct exclude_list *el)1051{1052static struct strbuf prefix = STRBUF_INIT;10531054strbuf_reset(&prefix);10551056returnclear_ce_flags_1(cache, nr,1057&prefix,1058 select_mask, clear_mask,1059 el,0);1060}10611062/*1063 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1064 */1065static voidmark_new_skip_worktree(struct exclude_list *el,1066struct index_state *the_index,1067int select_flag,int skip_wt_flag)1068{1069int i;10701071/*1072 * 1. Pretend the narrowest worktree: only unmerged entries1073 * are checked out1074 */1075for(i =0; i < the_index->cache_nr; i++) {1076struct cache_entry *ce = the_index->cache[i];10771078if(select_flag && !(ce->ce_flags & select_flag))1079continue;10801081if(!ce_stage(ce))1082 ce->ce_flags |= skip_wt_flag;1083else1084 ce->ce_flags &= ~skip_wt_flag;1085}10861087/*1088 * 2. Widen worktree according to sparse-checkout file.1089 * Matched entries will have skip_wt_flag cleared (i.e. "in")1090 */1091clear_ce_flags(the_index->cache, the_index->cache_nr,1092 select_flag, skip_wt_flag, el);1093}10941095static intverify_absent(const struct cache_entry *,1096enum unpack_trees_error_types,1097struct unpack_trees_options *);1098/*1099 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1100 * resulting index, -2 on failure to reflect the changes to the work tree.1101 *1102 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1103 */1104intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1105{1106int i, ret;1107static struct cache_entry *dfc;1108struct exclude_list el;11091110if(len > MAX_UNPACK_TREES)1111die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);11121113memset(&el,0,sizeof(el));1114if(!core_apply_sparse_checkout || !o->update)1115 o->skip_sparse_checkout =1;1116if(!o->skip_sparse_checkout) {1117char*sparse =git_pathdup("info/sparse-checkout");1118if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1119 o->skip_sparse_checkout =1;1120else1121 o->el = ⪙1122free(sparse);1123}11241125memset(&o->result,0,sizeof(o->result));1126 o->result.initialized =1;1127 o->result.timestamp.sec = o->src_index->timestamp.sec;1128 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1129 o->result.version = o->src_index->version;1130 o->result.split_index = o->src_index->split_index;1131if(o->result.split_index)1132 o->result.split_index->refcount++;1133hashcpy(o->result.sha1, o->src_index->sha1);1134 o->merge_size = len;1135mark_all_ce_unused(o->src_index);11361137/*1138 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1139 */1140if(!o->skip_sparse_checkout)1141mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);11421143if(!dfc)1144 dfc =xcalloc(1,cache_entry_size(0));1145 o->df_conflict_entry = dfc;11461147if(len) {1148const char*prefix = o->prefix ? o->prefix :"";1149struct traverse_info info;11501151setup_traverse_info(&info, prefix);1152 info.fn = unpack_callback;1153 info.data = o;1154 info.show_all_errors = o->show_all_errors;1155 info.pathspec = o->pathspec;11561157if(o->prefix) {1158/*1159 * Unpack existing index entries that sort before the1160 * prefix the tree is spliced into. Note that o->merge1161 * is always true in this case.1162 */1163while(1) {1164struct cache_entry *ce =next_cache_entry(o);1165if(!ce)1166break;1167if(ce_in_traverse_path(ce, &info))1168break;1169if(unpack_index_entry(ce, o) <0)1170goto return_failed;1171}1172}11731174if(traverse_trees(len, t, &info) <0)1175goto return_failed;1176}11771178/* Any left-over entries in the index? */1179if(o->merge) {1180while(1) {1181struct cache_entry *ce =next_cache_entry(o);1182if(!ce)1183break;1184if(unpack_index_entry(ce, o) <0)1185goto return_failed;1186}1187}1188mark_all_ce_unused(o->src_index);11891190if(o->trivial_merges_only && o->nontrivial_merge) {1191 ret =unpack_failed(o,"Merge requires file-level merging");1192goto done;1193}11941195if(!o->skip_sparse_checkout) {1196int empty_worktree =1;11971198/*1199 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11200 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1201 * so apply_sparse_checkout() won't attempt to remove it from worktree1202 */1203mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);12041205 ret =0;1206for(i =0; i < o->result.cache_nr; i++) {1207struct cache_entry *ce = o->result.cache[i];12081209/*1210 * Entries marked with CE_ADDED in merged_entry() do not have1211 * verify_absent() check (the check is effectively disabled1212 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1213 *1214 * Do the real check now because we have had1215 * correct CE_NEW_SKIP_WORKTREE1216 */1217if(ce->ce_flags & CE_ADDED &&1218verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1219if(!o->show_all_errors)1220goto return_failed;1221 ret = -1;1222}12231224if(apply_sparse_checkout(&o->result, ce, o)) {1225if(!o->show_all_errors)1226goto return_failed;1227 ret = -1;1228}1229if(!ce_skip_worktree(ce))1230 empty_worktree =0;12311232}1233if(ret <0)1234goto return_failed;1235/*1236 * Sparse checkout is meant to narrow down checkout area1237 * but it does not make sense to narrow down to empty working1238 * tree. This is usually a mistake in sparse checkout rules.1239 * Do not allow users to do that.1240 */1241if(o->result.cache_nr && empty_worktree) {1242 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1243goto done;1244}1245}12461247 o->src_index = NULL;1248 ret =check_updates(o) ? (-2) :0;1249if(o->dst_index) {1250if(!ret) {1251if(!o->result.cache_tree)1252 o->result.cache_tree =cache_tree();1253if(!cache_tree_fully_valid(o->result.cache_tree))1254cache_tree_update(&o->result,1255 WRITE_TREE_SILENT |1256 WRITE_TREE_REPAIR);1257}1258discard_index(o->dst_index);1259*o->dst_index = o->result;1260}else{1261discard_index(&o->result);1262}12631264done:1265clear_exclude_list(&el);1266return ret;12671268return_failed:1269if(o->show_all_errors)1270display_error_msgs(o);1271mark_all_ce_unused(o->src_index);1272 ret =unpack_failed(o, NULL);1273if(o->exiting_early)1274 ret =0;1275goto done;1276}12771278/* Here come the merge functions */12791280static intreject_merge(const struct cache_entry *ce,1281struct unpack_trees_options *o)1282{1283return o->gently ? -1:1284add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1285}12861287static intsame(const struct cache_entry *a,const struct cache_entry *b)1288{1289if(!!a != !!b)1290return0;1291if(!a && !b)1292return1;1293if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1294return0;1295return a->ce_mode == b->ce_mode &&1296!oidcmp(&a->oid, &b->oid);1297}129812991300/*1301 * When a CE gets turned into an unmerged entry, we1302 * want it to be up-to-date1303 */1304static intverify_uptodate_1(const struct cache_entry *ce,1305struct unpack_trees_options *o,1306enum unpack_trees_error_types error_type)1307{1308struct stat st;13091310if(o->index_only)1311return0;13121313/*1314 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1315 * if this entry is truly up-to-date because this file may be1316 * overwritten.1317 */1318if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1319;/* keep checking */1320else if(o->reset ||ce_uptodate(ce))1321return0;13221323if(!lstat(ce->name, &st)) {1324int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1325unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1326if(!changed)1327return0;1328/*1329 * NEEDSWORK: the current default policy is to allow1330 * submodule to be out of sync wrt the superproject1331 * index. This needs to be tightened later for1332 * submodules that are marked to be automatically1333 * checked out.1334 */1335if(S_ISGITLINK(ce->ce_mode))1336return0;1337 errno =0;1338}1339if(errno == ENOENT)1340return0;1341return o->gently ? -1:1342add_rejected_path(o, error_type, ce->name);1343}13441345static intverify_uptodate(const struct cache_entry *ce,1346struct unpack_trees_options *o)1347{1348if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1349return0;1350returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1351}13521353static intverify_uptodate_sparse(const struct cache_entry *ce,1354struct unpack_trees_options *o)1355{1356returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1357}13581359static voidinvalidate_ce_path(const struct cache_entry *ce,1360struct unpack_trees_options *o)1361{1362if(!ce)1363return;1364cache_tree_invalidate_path(o->src_index, ce->name);1365untracked_cache_invalidate_path(o->src_index, ce->name);1366}13671368/*1369 * Check that checking out ce->sha1 in subdir ce->name is not1370 * going to overwrite any working files.1371 *1372 * Currently, git does not checkout subprojects during a superproject1373 * checkout, so it is not going to overwrite anything.1374 */1375static intverify_clean_submodule(const struct cache_entry *ce,1376enum unpack_trees_error_types error_type,1377struct unpack_trees_options *o)1378{1379return0;1380}13811382static intverify_clean_subdirectory(const struct cache_entry *ce,1383enum unpack_trees_error_types error_type,1384struct unpack_trees_options *o)1385{1386/*1387 * we are about to extract "ce->name"; we would not want to lose1388 * anything in the existing directory there.1389 */1390int namelen;1391int i;1392struct dir_struct d;1393char*pathbuf;1394int cnt =0;1395unsigned char sha1[20];13961397if(S_ISGITLINK(ce->ce_mode) &&1398resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1399/* If we are not going to update the submodule, then1400 * we don't care.1401 */1402if(!hashcmp(sha1, ce->oid.hash))1403return0;1404returnverify_clean_submodule(ce, error_type, o);1405}14061407/*1408 * First let's make sure we do not have a local modification1409 * in that directory.1410 */1411 namelen =ce_namelen(ce);1412for(i =locate_in_src_index(ce, o);1413 i < o->src_index->cache_nr;1414 i++) {1415struct cache_entry *ce2 = o->src_index->cache[i];1416int len =ce_namelen(ce2);1417if(len < namelen ||1418strncmp(ce->name, ce2->name, namelen) ||1419 ce2->name[namelen] !='/')1420break;1421/*1422 * ce2->name is an entry in the subdirectory to be1423 * removed.1424 */1425if(!ce_stage(ce2)) {1426if(verify_uptodate(ce2, o))1427return-1;1428add_entry(o, ce2, CE_REMOVE,0);1429mark_ce_used(ce2, o);1430}1431 cnt++;1432}14331434/*1435 * Then we need to make sure that we do not lose a locally1436 * present file that is not ignored.1437 */1438 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14391440memset(&d,0,sizeof(d));1441if(o->dir)1442 d.exclude_per_dir = o->dir->exclude_per_dir;1443 i =read_directory(&d, pathbuf, namelen+1, NULL);1444if(i)1445return o->gently ? -1:1446add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1447free(pathbuf);1448return cnt;1449}14501451/*1452 * This gets called when there was no index entry for the tree entry 'dst',1453 * but we found a file in the working tree that 'lstat()' said was fine,1454 * and we're on a case-insensitive filesystem.1455 *1456 * See if we can find a case-insensitive match in the index that also1457 * matches the stat information, and assume it's that other file!1458 */1459static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1460{1461const struct cache_entry *src;14621463 src =index_file_exists(o->src_index, name, len,1);1464return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1465}14661467static intcheck_ok_to_remove(const char*name,int len,int dtype,1468const struct cache_entry *ce,struct stat *st,1469enum unpack_trees_error_types error_type,1470struct unpack_trees_options *o)1471{1472const struct cache_entry *result;14731474/*1475 * It may be that the 'lstat()' succeeded even though1476 * target 'ce' was absent, because there is an old1477 * entry that is different only in case..1478 *1479 * Ignore that lstat() if it matches.1480 */1481if(ignore_case &&icase_exists(o, name, len, st))1482return0;14831484if(o->dir &&1485is_excluded(o->dir, name, &dtype))1486/*1487 * ce->name is explicitly excluded, so it is Ok to1488 * overwrite it.1489 */1490return0;1491if(S_ISDIR(st->st_mode)) {1492/*1493 * We are checking out path "foo" and1494 * found "foo/." in the working tree.1495 * This is tricky -- if we have modified1496 * files that are in "foo/" we would lose1497 * them.1498 */1499if(verify_clean_subdirectory(ce, error_type, o) <0)1500return-1;1501return0;1502}15031504/*1505 * The previous round may already have decided to1506 * delete this path, which is in a subdirectory that1507 * is being replaced with a blob.1508 */1509 result =index_file_exists(&o->result, name, len,0);1510if(result) {1511if(result->ce_flags & CE_REMOVE)1512return0;1513}15141515return o->gently ? -1:1516add_rejected_path(o, error_type, name);1517}15181519/*1520 * We do not want to remove or overwrite a working tree file that1521 * is not tracked, unless it is ignored.1522 */1523static intverify_absent_1(const struct cache_entry *ce,1524enum unpack_trees_error_types error_type,1525struct unpack_trees_options *o)1526{1527int len;1528struct stat st;15291530if(o->index_only || o->reset || !o->update)1531return0;15321533 len =check_leading_path(ce->name,ce_namelen(ce));1534if(!len)1535return0;1536else if(len >0) {1537char*path;1538int ret;15391540 path =xmemdupz(ce->name, len);1541if(lstat(path, &st))1542 ret =error_errno("cannot stat '%s'", path);1543else1544 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1545&st, error_type, o);1546free(path);1547return ret;1548}else if(lstat(ce->name, &st)) {1549if(errno != ENOENT)1550returnerror_errno("cannot stat '%s'", ce->name);1551return0;1552}else{1553returncheck_ok_to_remove(ce->name,ce_namelen(ce),1554ce_to_dtype(ce), ce, &st,1555 error_type, o);1556}1557}15581559static intverify_absent(const struct cache_entry *ce,1560enum unpack_trees_error_types error_type,1561struct unpack_trees_options *o)1562{1563if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1564return0;1565returnverify_absent_1(ce, error_type, o);1566}15671568static intverify_absent_sparse(const struct cache_entry *ce,1569enum unpack_trees_error_types error_type,1570struct unpack_trees_options *o)1571{1572enum unpack_trees_error_types orphaned_error = error_type;1573if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1574 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;15751576returnverify_absent_1(ce, orphaned_error, o);1577}15781579static intmerged_entry(const struct cache_entry *ce,1580const struct cache_entry *old,1581struct unpack_trees_options *o)1582{1583int update = CE_UPDATE;1584struct cache_entry *merge =dup_entry(ce);15851586if(!old) {1587/*1588 * New index entries. In sparse checkout, the following1589 * verify_absent() will be delayed until after1590 * traverse_trees() finishes in unpack_trees(), then:1591 *1592 * - CE_NEW_SKIP_WORKTREE will be computed correctly1593 * - verify_absent() be called again, this time with1594 * correct CE_NEW_SKIP_WORKTREE1595 *1596 * verify_absent() call here does nothing in sparse1597 * checkout (i.e. o->skip_sparse_checkout == 0)1598 */1599 update |= CE_ADDED;1600 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;16011602if(verify_absent(merge,1603 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1604free(merge);1605return-1;1606}1607invalidate_ce_path(merge, o);1608}else if(!(old->ce_flags & CE_CONFLICTED)) {1609/*1610 * See if we can re-use the old CE directly?1611 * That way we get the uptodate stat info.1612 *1613 * This also removes the UPDATE flag on a match; otherwise1614 * we will end up overwriting local changes in the work tree.1615 */1616if(same(old, merge)) {1617copy_cache_entry(merge, old);1618 update =0;1619}else{1620if(verify_uptodate(old, o)) {1621free(merge);1622return-1;1623}1624/* Migrate old flags over */1625 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1626invalidate_ce_path(old, o);1627}1628}else{1629/*1630 * Previously unmerged entry left as an existence1631 * marker by read_index_unmerged();1632 */1633invalidate_ce_path(old, o);1634}16351636do_add_entry(o, merge, update, CE_STAGEMASK);1637return1;1638}16391640static intdeleted_entry(const struct cache_entry *ce,1641const struct cache_entry *old,1642struct unpack_trees_options *o)1643{1644/* Did it exist in the index? */1645if(!old) {1646if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1647return-1;1648return0;1649}1650if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1651return-1;1652add_entry(o, ce, CE_REMOVE,0);1653invalidate_ce_path(ce, o);1654return1;1655}16561657static intkeep_entry(const struct cache_entry *ce,1658struct unpack_trees_options *o)1659{1660add_entry(o, ce,0,0);1661return1;1662}16631664#if DBRT_DEBUG1665static voidshow_stage_entry(FILE*o,1666const char*label,const struct cache_entry *ce)1667{1668if(!ce)1669fprintf(o,"%s(missing)\n", label);1670else1671fprintf(o,"%s%06o%s %d\t%s\n",1672 label,1673 ce->ce_mode,1674oid_to_hex(&ce->oid),1675ce_stage(ce),1676 ce->name);1677}1678#endif16791680intthreeway_merge(const struct cache_entry *const*stages,1681struct unpack_trees_options *o)1682{1683const struct cache_entry *index;1684const struct cache_entry *head;1685const struct cache_entry *remote = stages[o->head_idx +1];1686int count;1687int head_match =0;1688int remote_match =0;16891690int df_conflict_head =0;1691int df_conflict_remote =0;16921693int any_anc_missing =0;1694int no_anc_exists =1;1695int i;16961697for(i =1; i < o->head_idx; i++) {1698if(!stages[i] || stages[i] == o->df_conflict_entry)1699 any_anc_missing =1;1700else1701 no_anc_exists =0;1702}17031704 index = stages[0];1705 head = stages[o->head_idx];17061707if(head == o->df_conflict_entry) {1708 df_conflict_head =1;1709 head = NULL;1710}17111712if(remote == o->df_conflict_entry) {1713 df_conflict_remote =1;1714 remote = NULL;1715}17161717/*1718 * First, if there's a #16 situation, note that to prevent #131719 * and #14.1720 */1721if(!same(remote, head)) {1722for(i =1; i < o->head_idx; i++) {1723if(same(stages[i], head)) {1724 head_match = i;1725}1726if(same(stages[i], remote)) {1727 remote_match = i;1728}1729}1730}17311732/*1733 * We start with cases where the index is allowed to match1734 * something other than the head: #14(ALT) and #2ALT, where it1735 * is permitted to match the result instead.1736 */1737/* #14, #14ALT, #2ALT */1738if(remote && !df_conflict_head && head_match && !remote_match) {1739if(index && !same(index, remote) && !same(index, head))1740returnreject_merge(index, o);1741returnmerged_entry(remote, index, o);1742}1743/*1744 * If we have an entry in the index cache, then we want to1745 * make sure that it matches head.1746 */1747if(index && !same(index, head))1748returnreject_merge(index, o);17491750if(head) {1751/* #5ALT, #15 */1752if(same(head, remote))1753returnmerged_entry(head, index, o);1754/* #13, #3ALT */1755if(!df_conflict_remote && remote_match && !head_match)1756returnmerged_entry(head, index, o);1757}17581759/* #1 */1760if(!head && !remote && any_anc_missing)1761return0;17621763/*1764 * Under the "aggressive" rule, we resolve mostly trivial1765 * cases that we historically had git-merge-one-file resolve.1766 */1767if(o->aggressive) {1768int head_deleted = !head;1769int remote_deleted = !remote;1770const struct cache_entry *ce = NULL;17711772if(index)1773 ce = index;1774else if(head)1775 ce = head;1776else if(remote)1777 ce = remote;1778else{1779for(i =1; i < o->head_idx; i++) {1780if(stages[i] && stages[i] != o->df_conflict_entry) {1781 ce = stages[i];1782break;1783}1784}1785}17861787/*1788 * Deleted in both.1789 * Deleted in one and unchanged in the other.1790 */1791if((head_deleted && remote_deleted) ||1792(head_deleted && remote && remote_match) ||1793(remote_deleted && head && head_match)) {1794if(index)1795returndeleted_entry(index, index, o);1796if(ce && !head_deleted) {1797if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1798return-1;1799}1800return0;1801}1802/*1803 * Added in both, identically.1804 */1805if(no_anc_exists && head && remote &&same(head, remote))1806returnmerged_entry(head, index, o);18071808}18091810/* Below are "no merge" cases, which require that the index be1811 * up-to-date to avoid the files getting overwritten with1812 * conflict resolution files.1813 */1814if(index) {1815if(verify_uptodate(index, o))1816return-1;1817}18181819 o->nontrivial_merge =1;18201821/* #2, #3, #4, #6, #7, #9, #10, #11. */1822 count =0;1823if(!head_match || !remote_match) {1824for(i =1; i < o->head_idx; i++) {1825if(stages[i] && stages[i] != o->df_conflict_entry) {1826keep_entry(stages[i], o);1827 count++;1828break;1829}1830}1831}1832#if DBRT_DEBUG1833else{1834fprintf(stderr,"read-tree: warning #16 detected\n");1835show_stage_entry(stderr,"head ", stages[head_match]);1836show_stage_entry(stderr,"remote ", stages[remote_match]);1837}1838#endif1839if(head) { count +=keep_entry(head, o); }1840if(remote) { count +=keep_entry(remote, o); }1841return count;1842}18431844/*1845 * Two-way merge.1846 *1847 * The rule is to "carry forward" what is in the index without losing1848 * information across a "fast-forward", favoring a successful merge1849 * over a merge failure when it makes sense. For details of the1850 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1851 *1852 */1853inttwoway_merge(const struct cache_entry *const*src,1854struct unpack_trees_options *o)1855{1856const struct cache_entry *current = src[0];1857const struct cache_entry *oldtree = src[1];1858const struct cache_entry *newtree = src[2];18591860if(o->merge_size !=2)1861returnerror("Cannot do a twoway merge of%dtrees",1862 o->merge_size);18631864if(oldtree == o->df_conflict_entry)1865 oldtree = NULL;1866if(newtree == o->df_conflict_entry)1867 newtree = NULL;18681869if(current) {1870if(current->ce_flags & CE_CONFLICTED) {1871if(same(oldtree, newtree) || o->reset) {1872if(!newtree)1873returndeleted_entry(current, current, o);1874else1875returnmerged_entry(newtree, current, o);1876}1877returnreject_merge(current, o);1878}else if((!oldtree && !newtree) ||/* 4 and 5 */1879(!oldtree && newtree &&1880same(current, newtree)) ||/* 6 and 7 */1881(oldtree && newtree &&1882same(oldtree, newtree)) ||/* 14 and 15 */1883(oldtree && newtree &&1884!same(oldtree, newtree) &&/* 18 and 19 */1885same(current, newtree))) {1886returnkeep_entry(current, o);1887}else if(oldtree && !newtree &&same(current, oldtree)) {1888/* 10 or 11 */1889returndeleted_entry(oldtree, current, o);1890}else if(oldtree && newtree &&1891same(current, oldtree) && !same(current, newtree)) {1892/* 20 or 21 */1893returnmerged_entry(newtree, current, o);1894}else1895returnreject_merge(current, o);1896}1897else if(newtree) {1898if(oldtree && !o->initial_checkout) {1899/*1900 * deletion of the path was staged;1901 */1902if(same(oldtree, newtree))1903return1;1904returnreject_merge(oldtree, o);1905}1906returnmerged_entry(newtree, current, o);1907}1908returndeleted_entry(oldtree, current, o);1909}19101911/*1912 * Bind merge.1913 *1914 * Keep the index entries at stage0, collapse stage1 but make sure1915 * stage0 does not have anything there.1916 */1917intbind_merge(const struct cache_entry *const*src,1918struct unpack_trees_options *o)1919{1920const struct cache_entry *old = src[0];1921const struct cache_entry *a = src[1];19221923if(o->merge_size !=1)1924returnerror("Cannot do a bind merge of%dtrees",1925 o->merge_size);1926if(a && old)1927return o->gently ? -1:1928error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);1929if(!a)1930returnkeep_entry(old, o);1931else1932returnmerged_entry(a, NULL, o);1933}19341935/*1936 * One-way merge.1937 *1938 * The rule is:1939 * - take the stat information from stage0, take the data from stage11940 */1941intoneway_merge(const struct cache_entry *const*src,1942struct unpack_trees_options *o)1943{1944const struct cache_entry *old = src[0];1945const struct cache_entry *a = src[1];19461947if(o->merge_size !=1)1948returnerror("Cannot do a oneway merge of%dtrees",1949 o->merge_size);19501951if(!a || a == o->df_conflict_entry)1952returndeleted_entry(old, old, o);19531954if(old &&same(old, a)) {1955int update =0;1956if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1957struct stat st;1958if(lstat(old->name, &st) ||1959ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))1960 update |= CE_UPDATE;1961}1962add_entry(o, old, update,0);1963return0;1964}1965returnmerged_entry(a, old, o);1966}