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, 222const struct checkout *state) 223{ 224unsigned cnt =0, total =0; 225struct progress *progress = NULL; 226struct index_state *index = &o->result; 227int i; 228int errs =0; 229 230if(o->update && o->verbose_update) { 231for(total = cnt =0; cnt < index->cache_nr; cnt++) { 232const struct cache_entry *ce = index->cache[cnt]; 233if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 234 total++; 235} 236 237 progress =start_progress_delay(_("Checking out files"), 238 total,50,1); 239 cnt =0; 240} 241 242if(o->update) 243git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result); 244for(i =0; i < index->cache_nr; i++) { 245const struct cache_entry *ce = index->cache[i]; 246 247if(ce->ce_flags & CE_WT_REMOVE) { 248display_progress(progress, ++cnt); 249if(o->update && !o->dry_run) 250unlink_entry(ce); 251continue; 252} 253} 254remove_marked_cache_entries(&o->result); 255remove_scheduled_dirs(); 256 257for(i =0; i < index->cache_nr; i++) { 258struct cache_entry *ce = index->cache[i]; 259 260if(ce->ce_flags & CE_UPDATE) { 261if(ce->ce_flags & CE_WT_REMOVE) 262die("BUG: both update and delete flags are set on%s", 263 ce->name); 264display_progress(progress, ++cnt); 265 ce->ce_flags &= ~CE_UPDATE; 266if(o->update && !o->dry_run) { 267 errs |=checkout_entry(ce, state, NULL); 268} 269} 270} 271stop_progress(&progress); 272if(o->update) 273git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 274return errs !=0; 275} 276 277static intverify_uptodate_sparse(const struct cache_entry *ce, 278struct unpack_trees_options *o); 279static intverify_absent_sparse(const struct cache_entry *ce, 280enum unpack_trees_error_types, 281struct unpack_trees_options *o); 282 283static intapply_sparse_checkout(struct index_state *istate, 284struct cache_entry *ce, 285struct unpack_trees_options *o) 286{ 287int was_skip_worktree =ce_skip_worktree(ce); 288 289if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 290 ce->ce_flags |= CE_SKIP_WORKTREE; 291else 292 ce->ce_flags &= ~CE_SKIP_WORKTREE; 293if(was_skip_worktree !=ce_skip_worktree(ce)) { 294 ce->ce_flags |= CE_UPDATE_IN_BASE; 295 istate->cache_changed |= CE_ENTRY_CHANGED; 296} 297 298/* 299 * if (!was_skip_worktree && !ce_skip_worktree()) { 300 * This is perfectly normal. Move on; 301 * } 302 */ 303 304/* 305 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 306 * area as a result of ce_skip_worktree() shortcuts in 307 * verify_absent() and verify_uptodate(). 308 * Make sure they don't modify worktree if they are already 309 * outside checkout area 310 */ 311if(was_skip_worktree &&ce_skip_worktree(ce)) { 312 ce->ce_flags &= ~CE_UPDATE; 313 314/* 315 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 316 * on to get that file removed from both index and worktree. 317 * If that file is already outside worktree area, don't 318 * bother remove it. 319 */ 320if(ce->ce_flags & CE_REMOVE) 321 ce->ce_flags &= ~CE_WT_REMOVE; 322} 323 324if(!was_skip_worktree &&ce_skip_worktree(ce)) { 325/* 326 * If CE_UPDATE is set, verify_uptodate() must be called already 327 * also stat info may have lost after merged_entry() so calling 328 * verify_uptodate() again may fail 329 */ 330if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 331return-1; 332 ce->ce_flags |= CE_WT_REMOVE; 333 ce->ce_flags &= ~CE_UPDATE; 334} 335if(was_skip_worktree && !ce_skip_worktree(ce)) { 336if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 337return-1; 338 ce->ce_flags |= CE_UPDATE; 339} 340return0; 341} 342 343staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 344struct unpack_trees_options *o) 345{ 346int ret = o->fn(src, o); 347if(ret >0) 348 ret =0; 349return ret; 350} 351 352static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 353{ 354 ce->ce_flags |= CE_UNPACKED; 355 356if(o->cache_bottom < o->src_index->cache_nr && 357 o->src_index->cache[o->cache_bottom] == ce) { 358int bottom = o->cache_bottom; 359while(bottom < o->src_index->cache_nr && 360 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 361 bottom++; 362 o->cache_bottom = bottom; 363} 364} 365 366static voidmark_all_ce_unused(struct index_state *index) 367{ 368int i; 369for(i =0; i < index->cache_nr; i++) 370 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 371} 372 373static intlocate_in_src_index(const struct cache_entry *ce, 374struct unpack_trees_options *o) 375{ 376struct index_state *index = o->src_index; 377int len =ce_namelen(ce); 378int pos =index_name_pos(index, ce->name, len); 379if(pos <0) 380 pos = -1- pos; 381return pos; 382} 383 384/* 385 * We call unpack_index_entry() with an unmerged cache entry 386 * only in diff-index, and it wants a single callback. Skip 387 * the other unmerged entry with the same name. 388 */ 389static voidmark_ce_used_same_name(struct cache_entry *ce, 390struct unpack_trees_options *o) 391{ 392struct index_state *index = o->src_index; 393int len =ce_namelen(ce); 394int pos; 395 396for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 397struct cache_entry *next = index->cache[pos]; 398if(len !=ce_namelen(next) || 399memcmp(ce->name, next->name, len)) 400break; 401mark_ce_used(next, o); 402} 403} 404 405static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 406{ 407const struct index_state *index = o->src_index; 408int pos = o->cache_bottom; 409 410while(pos < index->cache_nr) { 411struct cache_entry *ce = index->cache[pos]; 412if(!(ce->ce_flags & CE_UNPACKED)) 413return ce; 414 pos++; 415} 416return NULL; 417} 418 419static voidadd_same_unmerged(const struct cache_entry *ce, 420struct unpack_trees_options *o) 421{ 422struct index_state *index = o->src_index; 423int len =ce_namelen(ce); 424int pos =index_name_pos(index, ce->name, len); 425 426if(0<= pos) 427die("programming error in a caller of mark_ce_used_same_name"); 428for(pos = -pos -1; pos < index->cache_nr; pos++) { 429struct cache_entry *next = index->cache[pos]; 430if(len !=ce_namelen(next) || 431memcmp(ce->name, next->name, len)) 432break; 433add_entry(o, next,0,0); 434mark_ce_used(next, o); 435} 436} 437 438static intunpack_index_entry(struct cache_entry *ce, 439struct unpack_trees_options *o) 440{ 441const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 442int ret; 443 444 src[0] = ce; 445 446mark_ce_used(ce, o); 447if(ce_stage(ce)) { 448if(o->skip_unmerged) { 449add_entry(o, ce,0,0); 450return0; 451} 452} 453 ret =call_unpack_fn(src, o); 454if(ce_stage(ce)) 455mark_ce_used_same_name(ce, o); 456return ret; 457} 458 459static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 460 461static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 462{ 463struct unpack_trees_options *o = info->data; 464 465if(o->diff_index_cached) 466return; 467 o->cache_bottom = bottom; 468} 469 470static intswitch_cache_bottom(struct traverse_info *info) 471{ 472struct unpack_trees_options *o = info->data; 473int ret, pos; 474 475if(o->diff_index_cached) 476return0; 477 ret = o->cache_bottom; 478 pos =find_cache_pos(info->prev, &info->name); 479 480if(pos < -1) 481 o->cache_bottom = -2- pos; 482else if(pos <0) 483 o->cache_bottom = o->src_index->cache_nr; 484return ret; 485} 486 487static inttraverse_trees_recursive(int n,unsigned long dirmask, 488unsigned long df_conflicts, 489struct name_entry *names, 490struct traverse_info *info) 491{ 492int i, ret, bottom; 493struct tree_desc t[MAX_UNPACK_TREES]; 494void*buf[MAX_UNPACK_TREES]; 495struct traverse_info newinfo; 496struct name_entry *p; 497 498 p = names; 499while(!p->mode) 500 p++; 501 502 newinfo = *info; 503 newinfo.prev = info; 504 newinfo.pathspec = info->pathspec; 505 newinfo.name = *p; 506 newinfo.pathlen +=tree_entry_len(p) +1; 507 newinfo.df_conflicts |= df_conflicts; 508 509for(i =0; i < n; i++, dirmask >>=1) { 510const unsigned char*sha1 = NULL; 511if(dirmask &1) 512 sha1 = names[i].oid->hash; 513 buf[i] =fill_tree_descriptor(t+i, sha1); 514} 515 516 bottom =switch_cache_bottom(&newinfo); 517 ret =traverse_trees(n, t, &newinfo); 518restore_cache_bottom(&newinfo, bottom); 519 520for(i =0; i < n; i++) 521free(buf[i]); 522 523return ret; 524} 525 526/* 527 * Compare the traverse-path to the cache entry without actually 528 * having to generate the textual representation of the traverse 529 * path. 530 * 531 * NOTE! This *only* compares up to the size of the traverse path 532 * itself - the caller needs to do the final check for the cache 533 * entry having more data at the end! 534 */ 535static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 536{ 537int len, pathlen, ce_len; 538const char*ce_name; 539 540if(info->prev) { 541int cmp =do_compare_entry_piecewise(ce, info->prev, 542&info->name); 543if(cmp) 544return cmp; 545} 546 pathlen = info->pathlen; 547 ce_len =ce_namelen(ce); 548 549/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 550if(ce_len < pathlen) 551return-1; 552 553 ce_len -= pathlen; 554 ce_name = ce->name + pathlen; 555 556 len =tree_entry_len(n); 557returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 558} 559 560static intdo_compare_entry(const struct cache_entry *ce, 561const struct traverse_info *info, 562const struct name_entry *n) 563{ 564int len, pathlen, ce_len; 565const char*ce_name; 566int cmp; 567 568/* 569 * If we have not precomputed the traverse path, it is quicker 570 * to avoid doing so. But if we have precomputed it, 571 * it is quicker to use the precomputed version. 572 */ 573if(!info->traverse_path) 574returndo_compare_entry_piecewise(ce, info, n); 575 576 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 577if(cmp) 578return cmp; 579 580 pathlen = info->pathlen; 581 ce_len =ce_namelen(ce); 582 583if(ce_len < pathlen) 584return-1; 585 586 ce_len -= pathlen; 587 ce_name = ce->name + pathlen; 588 589 len =tree_entry_len(n); 590returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 591} 592 593static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 594{ 595int cmp =do_compare_entry(ce, info, n); 596if(cmp) 597return cmp; 598 599/* 600 * Even if the beginning compared identically, the ce should 601 * compare as bigger than a directory leading up to it! 602 */ 603returnce_namelen(ce) >traverse_path_len(info, n); 604} 605 606static intce_in_traverse_path(const struct cache_entry *ce, 607const struct traverse_info *info) 608{ 609if(!info->prev) 610return1; 611if(do_compare_entry(ce, info->prev, &info->name)) 612return0; 613/* 614 * If ce (blob) is the same name as the path (which is a tree 615 * we will be descending into), it won't be inside it. 616 */ 617return(info->pathlen <ce_namelen(ce)); 618} 619 620static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 621{ 622int len =traverse_path_len(info, n); 623struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 624 625 ce->ce_mode =create_ce_mode(n->mode); 626 ce->ce_flags =create_ce_flags(stage); 627 ce->ce_namelen = len; 628hashcpy(ce->sha1, n->oid->hash); 629make_traverse_path(ce->name, info, n); 630 631return ce; 632} 633 634static intunpack_nondirectories(int n,unsigned long mask, 635unsigned long dirmask, 636struct cache_entry **src, 637const struct name_entry *names, 638const struct traverse_info *info) 639{ 640int i; 641struct unpack_trees_options *o = info->data; 642unsigned long conflicts = info->df_conflicts | dirmask; 643 644/* Do we have *only* directories? Nothing to do */ 645if(mask == dirmask && !src[0]) 646return0; 647 648/* 649 * Ok, we've filled in up to any potential index entry in src[0], 650 * now do the rest. 651 */ 652for(i =0; i < n; i++) { 653int stage; 654unsigned int bit =1ul<< i; 655if(conflicts & bit) { 656 src[i + o->merge] = o->df_conflict_entry; 657continue; 658} 659if(!(mask & bit)) 660continue; 661if(!o->merge) 662 stage =0; 663else if(i +1< o->head_idx) 664 stage =1; 665else if(i +1> o->head_idx) 666 stage =3; 667else 668 stage =2; 669 src[i + o->merge] =create_ce_entry(info, names + i, stage); 670} 671 672if(o->merge) { 673int rc =call_unpack_fn((const struct cache_entry *const*)src, 674 o); 675for(i =0; i < n; i++) { 676struct cache_entry *ce = src[i + o->merge]; 677if(ce != o->df_conflict_entry) 678free(ce); 679} 680return rc; 681} 682 683for(i =0; i < n; i++) 684if(src[i] && src[i] != o->df_conflict_entry) 685if(do_add_entry(o, src[i],0,0)) 686return-1; 687 688return0; 689} 690 691static intunpack_failed(struct unpack_trees_options *o,const char*message) 692{ 693discard_index(&o->result); 694if(!o->gently && !o->exiting_early) { 695if(message) 696returnerror("%s", message); 697return-1; 698} 699return-1; 700} 701 702/* 703 * The tree traversal is looking at name p. If we have a matching entry, 704 * return it. If name p is a directory in the index, do not return 705 * anything, as we will want to match it when the traversal descends into 706 * the directory. 707 */ 708static intfind_cache_pos(struct traverse_info *info, 709const struct name_entry *p) 710{ 711int pos; 712struct unpack_trees_options *o = info->data; 713struct index_state *index = o->src_index; 714int pfxlen = info->pathlen; 715int p_len =tree_entry_len(p); 716 717for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 718const struct cache_entry *ce = index->cache[pos]; 719const char*ce_name, *ce_slash; 720int cmp, ce_len; 721 722if(ce->ce_flags & CE_UNPACKED) { 723/* 724 * cache_bottom entry is already unpacked, so 725 * we can never match it; don't check it 726 * again. 727 */ 728if(pos == o->cache_bottom) 729++o->cache_bottom; 730continue; 731} 732if(!ce_in_traverse_path(ce, info)) { 733/* 734 * Check if we can skip future cache checks 735 * (because we're already past all possible 736 * entries in the traverse path). 737 */ 738if(info->traverse_path) { 739if(strncmp(ce->name, info->traverse_path, 740 info->pathlen) >0) 741break; 742} 743continue; 744} 745 ce_name = ce->name + pfxlen; 746 ce_slash =strchr(ce_name,'/'); 747if(ce_slash) 748 ce_len = ce_slash - ce_name; 749else 750 ce_len =ce_namelen(ce) - pfxlen; 751 cmp =name_compare(p->path, p_len, ce_name, ce_len); 752/* 753 * Exact match; if we have a directory we need to 754 * delay returning it. 755 */ 756if(!cmp) 757return ce_slash ? -2- pos : pos; 758if(0< cmp) 759continue;/* keep looking */ 760/* 761 * ce_name sorts after p->path; could it be that we 762 * have files under p->path directory in the index? 763 * E.g. ce_name == "t-i", and p->path == "t"; we may 764 * have "t/a" in the index. 765 */ 766if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 767 ce_name[p_len] <'/') 768continue;/* keep looking */ 769break; 770} 771return-1; 772} 773 774static struct cache_entry *find_cache_entry(struct traverse_info *info, 775const struct name_entry *p) 776{ 777int pos =find_cache_pos(info, p); 778struct unpack_trees_options *o = info->data; 779 780if(0<= pos) 781return o->src_index->cache[pos]; 782else 783return NULL; 784} 785 786static voiddebug_path(struct traverse_info *info) 787{ 788if(info->prev) { 789debug_path(info->prev); 790if(*info->prev->name.path) 791putchar('/'); 792} 793printf("%s", info->name.path); 794} 795 796static voiddebug_name_entry(int i,struct name_entry *n) 797{ 798printf("ent#%d %06o%s\n", i, 799 n->path ? n->mode :0, 800 n->path ? n->path :"(missing)"); 801} 802 803static voiddebug_unpack_callback(int n, 804unsigned long mask, 805unsigned long dirmask, 806struct name_entry *names, 807struct traverse_info *info) 808{ 809int i; 810printf("* unpack mask%lu, dirmask%lu, cnt%d", 811 mask, dirmask, n); 812debug_path(info); 813putchar('\n'); 814for(i =0; i < n; i++) 815debug_name_entry(i, names + i); 816} 817 818static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 819{ 820struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 821struct unpack_trees_options *o = info->data; 822const struct name_entry *p = names; 823 824/* Find first entry with a real name (we could use "mask" too) */ 825while(!p->mode) 826 p++; 827 828if(o->debug_unpack) 829debug_unpack_callback(n, mask, dirmask, names, info); 830 831/* Are we supposed to look at the index too? */ 832if(o->merge) { 833while(1) { 834int cmp; 835struct cache_entry *ce; 836 837if(o->diff_index_cached) 838 ce =next_cache_entry(o); 839else 840 ce =find_cache_entry(info, p); 841 842if(!ce) 843break; 844 cmp =compare_entry(ce, info, p); 845if(cmp <0) { 846if(unpack_index_entry(ce, o) <0) 847returnunpack_failed(o, NULL); 848continue; 849} 850if(!cmp) { 851if(ce_stage(ce)) { 852/* 853 * If we skip unmerged index 854 * entries, we'll skip this 855 * entry *and* the tree 856 * entries associated with it! 857 */ 858if(o->skip_unmerged) { 859add_same_unmerged(ce, o); 860return mask; 861} 862} 863 src[0] = ce; 864} 865break; 866} 867} 868 869if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 870return-1; 871 872if(o->merge && src[0]) { 873if(ce_stage(src[0])) 874mark_ce_used_same_name(src[0], o); 875else 876mark_ce_used(src[0], o); 877} 878 879/* Now handle any directories.. */ 880if(dirmask) { 881/* special case: "diff-index --cached" looking at a tree */ 882if(o->diff_index_cached && 883 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 884int matches; 885 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 886 names, info); 887/* 888 * Everything under the name matches; skip the 889 * entire hierarchy. diff_index_cached codepath 890 * special cases D/F conflicts in such a way that 891 * it does not do any look-ahead, so this is safe. 892 */ 893if(matches) { 894 o->cache_bottom += matches; 895return mask; 896} 897} 898 899if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 900 names, info) <0) 901return-1; 902return mask; 903} 904 905return mask; 906} 907 908static intclear_ce_flags_1(struct cache_entry **cache,int nr, 909struct strbuf *prefix, 910int select_mask,int clear_mask, 911struct exclude_list *el,int defval); 912 913/* Whole directory matching */ 914static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 915struct strbuf *prefix, 916char*basename, 917int select_mask,int clear_mask, 918struct exclude_list *el,int defval) 919{ 920struct cache_entry **cache_end; 921int dtype = DT_DIR; 922int ret =is_excluded_from_list(prefix->buf, prefix->len, 923 basename, &dtype, el); 924int rc; 925 926strbuf_addch(prefix,'/'); 927 928/* If undecided, use matching result of parent dir in defval */ 929if(ret <0) 930 ret = defval; 931 932for(cache_end = cache; cache_end != cache + nr; cache_end++) { 933struct cache_entry *ce = *cache_end; 934if(strncmp(ce->name, prefix->buf, prefix->len)) 935break; 936} 937 938/* 939 * TODO: check el, if there are no patterns that may conflict 940 * with ret (iow, we know in advance the incl/excl 941 * decision for the entire directory), clear flag here without 942 * calling clear_ce_flags_1(). That function will call 943 * the expensive is_excluded_from_list() on every entry. 944 */ 945 rc =clear_ce_flags_1(cache, cache_end - cache, 946 prefix, 947 select_mask, clear_mask, 948 el, ret); 949strbuf_setlen(prefix, prefix->len -1); 950return rc; 951} 952 953/* 954 * Traverse the index, find every entry that matches according to 955 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the 956 * number of traversed entries. 957 * 958 * If select_mask is non-zero, only entries whose ce_flags has on of 959 * those bits enabled are traversed. 960 * 961 * cache : pointer to an index entry 962 * prefix_len : an offset to its path 963 * 964 * The current path ("prefix") including the trailing '/' is 965 * cache[0]->name[0..(prefix_len-1)] 966 * Top level path has prefix_len zero. 967 */ 968static intclear_ce_flags_1(struct cache_entry **cache,int nr, 969struct strbuf *prefix, 970int select_mask,int clear_mask, 971struct exclude_list *el,int defval) 972{ 973struct cache_entry **cache_end = cache + nr; 974 975/* 976 * Process all entries that have the given prefix and meet 977 * select_mask condition 978 */ 979while(cache != cache_end) { 980struct cache_entry *ce = *cache; 981const char*name, *slash; 982int len, dtype, ret; 983 984if(select_mask && !(ce->ce_flags & select_mask)) { 985 cache++; 986continue; 987} 988 989if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len)) 990break; 991 992 name = ce->name + prefix->len; 993 slash =strchr(name,'/'); 994 995/* If it's a directory, try whole directory match first */ 996if(slash) { 997int processed; 998 999 len = slash - name;1000strbuf_add(prefix, name, len);10011002 processed =clear_ce_flags_dir(cache, cache_end - cache,1003 prefix,1004 prefix->buf + prefix->len - len,1005 select_mask, clear_mask,1006 el, defval);10071008/* clear_c_f_dir eats a whole dir already? */1009if(processed) {1010 cache += processed;1011strbuf_setlen(prefix, prefix->len - len);1012continue;1013}10141015strbuf_addch(prefix,'/');1016 cache +=clear_ce_flags_1(cache, cache_end - cache,1017 prefix,1018 select_mask, clear_mask, el, defval);1019strbuf_setlen(prefix, prefix->len - len -1);1020continue;1021}10221023/* Non-directory */1024 dtype =ce_to_dtype(ce);1025 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1026 name, &dtype, el);1027if(ret <0)1028 ret = defval;1029if(ret >0)1030 ce->ce_flags &= ~clear_mask;1031 cache++;1032}1033return nr - (cache_end - cache);1034}10351036static intclear_ce_flags(struct cache_entry **cache,int nr,1037int select_mask,int clear_mask,1038struct exclude_list *el)1039{1040static struct strbuf prefix = STRBUF_INIT;10411042strbuf_reset(&prefix);10431044returnclear_ce_flags_1(cache, nr,1045&prefix,1046 select_mask, clear_mask,1047 el,0);1048}10491050/*1051 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1052 */1053static voidmark_new_skip_worktree(struct exclude_list *el,1054struct index_state *the_index,1055int select_flag,int skip_wt_flag)1056{1057int i;10581059/*1060 * 1. Pretend the narrowest worktree: only unmerged entries1061 * are checked out1062 */1063for(i =0; i < the_index->cache_nr; i++) {1064struct cache_entry *ce = the_index->cache[i];10651066if(select_flag && !(ce->ce_flags & select_flag))1067continue;10681069if(!ce_stage(ce))1070 ce->ce_flags |= skip_wt_flag;1071else1072 ce->ce_flags &= ~skip_wt_flag;1073}10741075/*1076 * 2. Widen worktree according to sparse-checkout file.1077 * Matched entries will have skip_wt_flag cleared (i.e. "in")1078 */1079clear_ce_flags(the_index->cache, the_index->cache_nr,1080 select_flag, skip_wt_flag, el);1081}10821083static intverify_absent(const struct cache_entry *,1084enum unpack_trees_error_types,1085struct unpack_trees_options *);1086/*1087 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1088 * resulting index, -2 on failure to reflect the changes to the work tree.1089 *1090 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1091 */1092intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1093{1094int i, ret;1095static struct cache_entry *dfc;1096struct exclude_list el;1097struct checkout state;10981099if(len > MAX_UNPACK_TREES)1100die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);1101memset(&state,0,sizeof(state));1102 state.base_dir ="";1103 state.force =1;1104 state.quiet =1;1105 state.refresh_cache =1;1106 state.istate = &o->result;11071108memset(&el,0,sizeof(el));1109if(!core_apply_sparse_checkout || !o->update)1110 o->skip_sparse_checkout =1;1111if(!o->skip_sparse_checkout) {1112char*sparse =git_pathdup("info/sparse-checkout");1113if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1114 o->skip_sparse_checkout =1;1115else1116 o->el = ⪙1117free(sparse);1118}11191120memset(&o->result,0,sizeof(o->result));1121 o->result.initialized =1;1122 o->result.timestamp.sec = o->src_index->timestamp.sec;1123 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1124 o->result.version = o->src_index->version;1125 o->result.split_index = o->src_index->split_index;1126if(o->result.split_index)1127 o->result.split_index->refcount++;1128hashcpy(o->result.sha1, o->src_index->sha1);1129 o->merge_size = len;1130mark_all_ce_unused(o->src_index);11311132/*1133 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1134 */1135if(!o->skip_sparse_checkout)1136mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);11371138if(!dfc)1139 dfc =xcalloc(1,cache_entry_size(0));1140 o->df_conflict_entry = dfc;11411142if(len) {1143const char*prefix = o->prefix ? o->prefix :"";1144struct traverse_info info;11451146setup_traverse_info(&info, prefix);1147 info.fn = unpack_callback;1148 info.data = o;1149 info.show_all_errors = o->show_all_errors;1150 info.pathspec = o->pathspec;11511152if(o->prefix) {1153/*1154 * Unpack existing index entries that sort before the1155 * prefix the tree is spliced into. Note that o->merge1156 * is always true in this case.1157 */1158while(1) {1159struct cache_entry *ce =next_cache_entry(o);1160if(!ce)1161break;1162if(ce_in_traverse_path(ce, &info))1163break;1164if(unpack_index_entry(ce, o) <0)1165goto return_failed;1166}1167}11681169if(traverse_trees(len, t, &info) <0)1170goto return_failed;1171}11721173/* Any left-over entries in the index? */1174if(o->merge) {1175while(1) {1176struct cache_entry *ce =next_cache_entry(o);1177if(!ce)1178break;1179if(unpack_index_entry(ce, o) <0)1180goto return_failed;1181}1182}1183mark_all_ce_unused(o->src_index);11841185if(o->trivial_merges_only && o->nontrivial_merge) {1186 ret =unpack_failed(o,"Merge requires file-level merging");1187goto done;1188}11891190if(!o->skip_sparse_checkout) {1191int empty_worktree =1;11921193/*1194 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11195 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1196 * so apply_sparse_checkout() won't attempt to remove it from worktree1197 */1198mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);11991200 ret =0;1201for(i =0; i < o->result.cache_nr; i++) {1202struct cache_entry *ce = o->result.cache[i];12031204/*1205 * Entries marked with CE_ADDED in merged_entry() do not have1206 * verify_absent() check (the check is effectively disabled1207 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1208 *1209 * Do the real check now because we have had1210 * correct CE_NEW_SKIP_WORKTREE1211 */1212if(ce->ce_flags & CE_ADDED &&1213verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1214if(!o->show_all_errors)1215goto return_failed;1216 ret = -1;1217}12181219if(apply_sparse_checkout(&o->result, ce, o)) {1220if(!o->show_all_errors)1221goto return_failed;1222 ret = -1;1223}1224if(!ce_skip_worktree(ce))1225 empty_worktree =0;12261227}1228if(ret <0)1229goto return_failed;1230/*1231 * Sparse checkout is meant to narrow down checkout area1232 * but it does not make sense to narrow down to empty working1233 * tree. This is usually a mistake in sparse checkout rules.1234 * Do not allow users to do that.1235 */1236if(o->result.cache_nr && empty_worktree) {1237 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1238goto done;1239}1240}12411242 o->src_index = NULL;1243 ret =check_updates(o, &state) ? (-2) :0;1244if(o->dst_index) {1245if(!ret) {1246if(!o->result.cache_tree)1247 o->result.cache_tree =cache_tree();1248if(!cache_tree_fully_valid(o->result.cache_tree))1249cache_tree_update(&o->result,1250 WRITE_TREE_SILENT |1251 WRITE_TREE_REPAIR);1252}1253discard_index(o->dst_index);1254*o->dst_index = o->result;1255}else{1256discard_index(&o->result);1257}12581259done:1260clear_exclude_list(&el);1261return ret;12621263return_failed:1264if(o->show_all_errors)1265display_error_msgs(o);1266mark_all_ce_unused(o->src_index);1267 ret =unpack_failed(o, NULL);1268if(o->exiting_early)1269 ret =0;1270goto done;1271}12721273/* Here come the merge functions */12741275static intreject_merge(const struct cache_entry *ce,1276struct unpack_trees_options *o)1277{1278return o->gently ? -1:1279add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1280}12811282static intsame(const struct cache_entry *a,const struct cache_entry *b)1283{1284if(!!a != !!b)1285return0;1286if(!a && !b)1287return1;1288if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1289return0;1290return a->ce_mode == b->ce_mode &&1291!hashcmp(a->sha1, b->sha1);1292}129312941295/*1296 * When a CE gets turned into an unmerged entry, we1297 * want it to be up-to-date1298 */1299static intverify_uptodate_1(const struct cache_entry *ce,1300struct unpack_trees_options *o,1301enum unpack_trees_error_types error_type)1302{1303struct stat st;13041305if(o->index_only)1306return0;13071308/*1309 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1310 * if this entry is truly up-to-date because this file may be1311 * overwritten.1312 */1313if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1314;/* keep checking */1315else if(o->reset ||ce_uptodate(ce))1316return0;13171318if(!lstat(ce->name, &st)) {1319int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1320unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1321if(!changed)1322return0;1323/*1324 * NEEDSWORK: the current default policy is to allow1325 * submodule to be out of sync wrt the superproject1326 * index. This needs to be tightened later for1327 * submodules that are marked to be automatically1328 * checked out.1329 */1330if(S_ISGITLINK(ce->ce_mode))1331return0;1332 errno =0;1333}1334if(errno == ENOENT)1335return0;1336return o->gently ? -1:1337add_rejected_path(o, error_type, ce->name);1338}13391340static intverify_uptodate(const struct cache_entry *ce,1341struct unpack_trees_options *o)1342{1343if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1344return0;1345returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1346}13471348static intverify_uptodate_sparse(const struct cache_entry *ce,1349struct unpack_trees_options *o)1350{1351returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1352}13531354static voidinvalidate_ce_path(const struct cache_entry *ce,1355struct unpack_trees_options *o)1356{1357if(!ce)1358return;1359cache_tree_invalidate_path(o->src_index, ce->name);1360untracked_cache_invalidate_path(o->src_index, ce->name);1361}13621363/*1364 * Check that checking out ce->sha1 in subdir ce->name is not1365 * going to overwrite any working files.1366 *1367 * Currently, git does not checkout subprojects during a superproject1368 * checkout, so it is not going to overwrite anything.1369 */1370static intverify_clean_submodule(const struct cache_entry *ce,1371enum unpack_trees_error_types error_type,1372struct unpack_trees_options *o)1373{1374return0;1375}13761377static intverify_clean_subdirectory(const struct cache_entry *ce,1378enum unpack_trees_error_types error_type,1379struct unpack_trees_options *o)1380{1381/*1382 * we are about to extract "ce->name"; we would not want to lose1383 * anything in the existing directory there.1384 */1385int namelen;1386int i;1387struct dir_struct d;1388char*pathbuf;1389int cnt =0;1390unsigned char sha1[20];13911392if(S_ISGITLINK(ce->ce_mode) &&1393resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1394/* If we are not going to update the submodule, then1395 * we don't care.1396 */1397if(!hashcmp(sha1, ce->sha1))1398return0;1399returnverify_clean_submodule(ce, error_type, o);1400}14011402/*1403 * First let's make sure we do not have a local modification1404 * in that directory.1405 */1406 namelen =ce_namelen(ce);1407for(i =locate_in_src_index(ce, o);1408 i < o->src_index->cache_nr;1409 i++) {1410struct cache_entry *ce2 = o->src_index->cache[i];1411int len =ce_namelen(ce2);1412if(len < namelen ||1413strncmp(ce->name, ce2->name, namelen) ||1414 ce2->name[namelen] !='/')1415break;1416/*1417 * ce2->name is an entry in the subdirectory to be1418 * removed.1419 */1420if(!ce_stage(ce2)) {1421if(verify_uptodate(ce2, o))1422return-1;1423add_entry(o, ce2, CE_REMOVE,0);1424mark_ce_used(ce2, o);1425}1426 cnt++;1427}14281429/*1430 * Then we need to make sure that we do not lose a locally1431 * present file that is not ignored.1432 */1433 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14341435memset(&d,0,sizeof(d));1436if(o->dir)1437 d.exclude_per_dir = o->dir->exclude_per_dir;1438 i =read_directory(&d, pathbuf, namelen+1, NULL);1439if(i)1440return o->gently ? -1:1441add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1442free(pathbuf);1443return cnt;1444}14451446/*1447 * This gets called when there was no index entry for the tree entry 'dst',1448 * but we found a file in the working tree that 'lstat()' said was fine,1449 * and we're on a case-insensitive filesystem.1450 *1451 * See if we can find a case-insensitive match in the index that also1452 * matches the stat information, and assume it's that other file!1453 */1454static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1455{1456const struct cache_entry *src;14571458 src =index_file_exists(o->src_index, name, len,1);1459return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1460}14611462static intcheck_ok_to_remove(const char*name,int len,int dtype,1463const struct cache_entry *ce,struct stat *st,1464enum unpack_trees_error_types error_type,1465struct unpack_trees_options *o)1466{1467const struct cache_entry *result;14681469/*1470 * It may be that the 'lstat()' succeeded even though1471 * target 'ce' was absent, because there is an old1472 * entry that is different only in case..1473 *1474 * Ignore that lstat() if it matches.1475 */1476if(ignore_case &&icase_exists(o, name, len, st))1477return0;14781479if(o->dir &&1480is_excluded(o->dir, name, &dtype))1481/*1482 * ce->name is explicitly excluded, so it is Ok to1483 * overwrite it.1484 */1485return0;1486if(S_ISDIR(st->st_mode)) {1487/*1488 * We are checking out path "foo" and1489 * found "foo/." in the working tree.1490 * This is tricky -- if we have modified1491 * files that are in "foo/" we would lose1492 * them.1493 */1494if(verify_clean_subdirectory(ce, error_type, o) <0)1495return-1;1496return0;1497}14981499/*1500 * The previous round may already have decided to1501 * delete this path, which is in a subdirectory that1502 * is being replaced with a blob.1503 */1504 result =index_file_exists(&o->result, name, len,0);1505if(result) {1506if(result->ce_flags & CE_REMOVE)1507return0;1508}15091510return o->gently ? -1:1511add_rejected_path(o, error_type, name);1512}15131514/*1515 * We do not want to remove or overwrite a working tree file that1516 * is not tracked, unless it is ignored.1517 */1518static intverify_absent_1(const struct cache_entry *ce,1519enum unpack_trees_error_types error_type,1520struct unpack_trees_options *o)1521{1522int len;1523struct stat st;15241525if(o->index_only || o->reset || !o->update)1526return0;15271528 len =check_leading_path(ce->name,ce_namelen(ce));1529if(!len)1530return0;1531else if(len >0) {1532char*path;1533int ret;15341535 path =xmemdupz(ce->name, len);1536if(lstat(path, &st))1537 ret =error_errno("cannot stat '%s'", path);1538else1539 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1540&st, error_type, o);1541free(path);1542return ret;1543}else if(lstat(ce->name, &st)) {1544if(errno != ENOENT)1545returnerror_errno("cannot stat '%s'", ce->name);1546return0;1547}else{1548returncheck_ok_to_remove(ce->name,ce_namelen(ce),1549ce_to_dtype(ce), ce, &st,1550 error_type, o);1551}1552}15531554static intverify_absent(const struct cache_entry *ce,1555enum unpack_trees_error_types error_type,1556struct unpack_trees_options *o)1557{1558if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1559return0;1560returnverify_absent_1(ce, error_type, o);1561}15621563static intverify_absent_sparse(const struct cache_entry *ce,1564enum unpack_trees_error_types error_type,1565struct unpack_trees_options *o)1566{1567enum unpack_trees_error_types orphaned_error = error_type;1568if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1569 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;15701571returnverify_absent_1(ce, orphaned_error, o);1572}15731574static intmerged_entry(const struct cache_entry *ce,1575const struct cache_entry *old,1576struct unpack_trees_options *o)1577{1578int update = CE_UPDATE;1579struct cache_entry *merge =dup_entry(ce);15801581if(!old) {1582/*1583 * New index entries. In sparse checkout, the following1584 * verify_absent() will be delayed until after1585 * traverse_trees() finishes in unpack_trees(), then:1586 *1587 * - CE_NEW_SKIP_WORKTREE will be computed correctly1588 * - verify_absent() be called again, this time with1589 * correct CE_NEW_SKIP_WORKTREE1590 *1591 * verify_absent() call here does nothing in sparse1592 * checkout (i.e. o->skip_sparse_checkout == 0)1593 */1594 update |= CE_ADDED;1595 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;15961597if(verify_absent(merge,1598 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1599free(merge);1600return-1;1601}1602invalidate_ce_path(merge, o);1603}else if(!(old->ce_flags & CE_CONFLICTED)) {1604/*1605 * See if we can re-use the old CE directly?1606 * That way we get the uptodate stat info.1607 *1608 * This also removes the UPDATE flag on a match; otherwise1609 * we will end up overwriting local changes in the work tree.1610 */1611if(same(old, merge)) {1612copy_cache_entry(merge, old);1613 update =0;1614}else{1615if(verify_uptodate(old, o)) {1616free(merge);1617return-1;1618}1619/* Migrate old flags over */1620 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1621invalidate_ce_path(old, o);1622}1623}else{1624/*1625 * Previously unmerged entry left as an existence1626 * marker by read_index_unmerged();1627 */1628invalidate_ce_path(old, o);1629}16301631do_add_entry(o, merge, update, CE_STAGEMASK);1632return1;1633}16341635static intdeleted_entry(const struct cache_entry *ce,1636const struct cache_entry *old,1637struct unpack_trees_options *o)1638{1639/* Did it exist in the index? */1640if(!old) {1641if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1642return-1;1643return0;1644}1645if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1646return-1;1647add_entry(o, ce, CE_REMOVE,0);1648invalidate_ce_path(ce, o);1649return1;1650}16511652static intkeep_entry(const struct cache_entry *ce,1653struct unpack_trees_options *o)1654{1655add_entry(o, ce,0,0);1656return1;1657}16581659#if DBRT_DEBUG1660static voidshow_stage_entry(FILE*o,1661const char*label,const struct cache_entry *ce)1662{1663if(!ce)1664fprintf(o,"%s(missing)\n", label);1665else1666fprintf(o,"%s%06o%s %d\t%s\n",1667 label,1668 ce->ce_mode,1669sha1_to_hex(ce->sha1),1670ce_stage(ce),1671 ce->name);1672}1673#endif16741675intthreeway_merge(const struct cache_entry *const*stages,1676struct unpack_trees_options *o)1677{1678const struct cache_entry *index;1679const struct cache_entry *head;1680const struct cache_entry *remote = stages[o->head_idx +1];1681int count;1682int head_match =0;1683int remote_match =0;16841685int df_conflict_head =0;1686int df_conflict_remote =0;16871688int any_anc_missing =0;1689int no_anc_exists =1;1690int i;16911692for(i =1; i < o->head_idx; i++) {1693if(!stages[i] || stages[i] == o->df_conflict_entry)1694 any_anc_missing =1;1695else1696 no_anc_exists =0;1697}16981699 index = stages[0];1700 head = stages[o->head_idx];17011702if(head == o->df_conflict_entry) {1703 df_conflict_head =1;1704 head = NULL;1705}17061707if(remote == o->df_conflict_entry) {1708 df_conflict_remote =1;1709 remote = NULL;1710}17111712/*1713 * First, if there's a #16 situation, note that to prevent #131714 * and #14.1715 */1716if(!same(remote, head)) {1717for(i =1; i < o->head_idx; i++) {1718if(same(stages[i], head)) {1719 head_match = i;1720}1721if(same(stages[i], remote)) {1722 remote_match = i;1723}1724}1725}17261727/*1728 * We start with cases where the index is allowed to match1729 * something other than the head: #14(ALT) and #2ALT, where it1730 * is permitted to match the result instead.1731 */1732/* #14, #14ALT, #2ALT */1733if(remote && !df_conflict_head && head_match && !remote_match) {1734if(index && !same(index, remote) && !same(index, head))1735returnreject_merge(index, o);1736returnmerged_entry(remote, index, o);1737}1738/*1739 * If we have an entry in the index cache, then we want to1740 * make sure that it matches head.1741 */1742if(index && !same(index, head))1743returnreject_merge(index, o);17441745if(head) {1746/* #5ALT, #15 */1747if(same(head, remote))1748returnmerged_entry(head, index, o);1749/* #13, #3ALT */1750if(!df_conflict_remote && remote_match && !head_match)1751returnmerged_entry(head, index, o);1752}17531754/* #1 */1755if(!head && !remote && any_anc_missing)1756return0;17571758/*1759 * Under the "aggressive" rule, we resolve mostly trivial1760 * cases that we historically had git-merge-one-file resolve.1761 */1762if(o->aggressive) {1763int head_deleted = !head;1764int remote_deleted = !remote;1765const struct cache_entry *ce = NULL;17661767if(index)1768 ce = index;1769else if(head)1770 ce = head;1771else if(remote)1772 ce = remote;1773else{1774for(i =1; i < o->head_idx; i++) {1775if(stages[i] && stages[i] != o->df_conflict_entry) {1776 ce = stages[i];1777break;1778}1779}1780}17811782/*1783 * Deleted in both.1784 * Deleted in one and unchanged in the other.1785 */1786if((head_deleted && remote_deleted) ||1787(head_deleted && remote && remote_match) ||1788(remote_deleted && head && head_match)) {1789if(index)1790returndeleted_entry(index, index, o);1791if(ce && !head_deleted) {1792if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1793return-1;1794}1795return0;1796}1797/*1798 * Added in both, identically.1799 */1800if(no_anc_exists && head && remote &&same(head, remote))1801returnmerged_entry(head, index, o);18021803}18041805/* Below are "no merge" cases, which require that the index be1806 * up-to-date to avoid the files getting overwritten with1807 * conflict resolution files.1808 */1809if(index) {1810if(verify_uptodate(index, o))1811return-1;1812}18131814 o->nontrivial_merge =1;18151816/* #2, #3, #4, #6, #7, #9, #10, #11. */1817 count =0;1818if(!head_match || !remote_match) {1819for(i =1; i < o->head_idx; i++) {1820if(stages[i] && stages[i] != o->df_conflict_entry) {1821keep_entry(stages[i], o);1822 count++;1823break;1824}1825}1826}1827#if DBRT_DEBUG1828else{1829fprintf(stderr,"read-tree: warning #16 detected\n");1830show_stage_entry(stderr,"head ", stages[head_match]);1831show_stage_entry(stderr,"remote ", stages[remote_match]);1832}1833#endif1834if(head) { count +=keep_entry(head, o); }1835if(remote) { count +=keep_entry(remote, o); }1836return count;1837}18381839/*1840 * Two-way merge.1841 *1842 * The rule is to "carry forward" what is in the index without losing1843 * information across a "fast-forward", favoring a successful merge1844 * over a merge failure when it makes sense. For details of the1845 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1846 *1847 */1848inttwoway_merge(const struct cache_entry *const*src,1849struct unpack_trees_options *o)1850{1851const struct cache_entry *current = src[0];1852const struct cache_entry *oldtree = src[1];1853const struct cache_entry *newtree = src[2];18541855if(o->merge_size !=2)1856returnerror("Cannot do a twoway merge of%dtrees",1857 o->merge_size);18581859if(oldtree == o->df_conflict_entry)1860 oldtree = NULL;1861if(newtree == o->df_conflict_entry)1862 newtree = NULL;18631864if(current) {1865if(current->ce_flags & CE_CONFLICTED) {1866if(same(oldtree, newtree) || o->reset) {1867if(!newtree)1868returndeleted_entry(current, current, o);1869else1870returnmerged_entry(newtree, current, o);1871}1872returnreject_merge(current, o);1873}else if((!oldtree && !newtree) ||/* 4 and 5 */1874(!oldtree && newtree &&1875same(current, newtree)) ||/* 6 and 7 */1876(oldtree && newtree &&1877same(oldtree, newtree)) ||/* 14 and 15 */1878(oldtree && newtree &&1879!same(oldtree, newtree) &&/* 18 and 19 */1880same(current, newtree))) {1881returnkeep_entry(current, o);1882}else if(oldtree && !newtree &&same(current, oldtree)) {1883/* 10 or 11 */1884returndeleted_entry(oldtree, current, o);1885}else if(oldtree && newtree &&1886same(current, oldtree) && !same(current, newtree)) {1887/* 20 or 21 */1888returnmerged_entry(newtree, current, o);1889}else1890returnreject_merge(current, o);1891}1892else if(newtree) {1893if(oldtree && !o->initial_checkout) {1894/*1895 * deletion of the path was staged;1896 */1897if(same(oldtree, newtree))1898return1;1899returnreject_merge(oldtree, o);1900}1901returnmerged_entry(newtree, current, o);1902}1903returndeleted_entry(oldtree, current, o);1904}19051906/*1907 * Bind merge.1908 *1909 * Keep the index entries at stage0, collapse stage1 but make sure1910 * stage0 does not have anything there.1911 */1912intbind_merge(const struct cache_entry *const*src,1913struct unpack_trees_options *o)1914{1915const struct cache_entry *old = src[0];1916const struct cache_entry *a = src[1];19171918if(o->merge_size !=1)1919returnerror("Cannot do a bind merge of%dtrees",1920 o->merge_size);1921if(a && old)1922return o->gently ? -1:1923error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);1924if(!a)1925returnkeep_entry(old, o);1926else1927returnmerged_entry(a, NULL, o);1928}19291930/*1931 * One-way merge.1932 *1933 * The rule is:1934 * - take the stat information from stage0, take the data from stage11935 */1936intoneway_merge(const struct cache_entry *const*src,1937struct unpack_trees_options *o)1938{1939const struct cache_entry *old = src[0];1940const struct cache_entry *a = src[1];19411942if(o->merge_size !=1)1943returnerror("Cannot do a oneway merge of%dtrees",1944 o->merge_size);19451946if(!a || a == o->df_conflict_entry)1947returndeleted_entry(old, old, o);19481949if(old &&same(old, a)) {1950int update =0;1951if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1952struct stat st;1953if(lstat(old->name, &st) ||1954ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))1955 update |= CE_UPDATE;1956}1957add_entry(o, old, update,0);1958return0;1959}1960returnmerged_entry(a, old, o);1961}