1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"dir.h" 4#include"tree.h" 5#include"tree-walk.h" 6#include"cache-tree.h" 7#include"unpack-trees.h" 8#include"progress.h" 9#include"refs.h" 10#include"attr.h" 11#include"split-index.h" 12#include"dir.h" 13 14/* 15 * Error messages expected by scripts out of plumbing commands such as 16 * read-tree. Non-scripted Porcelain is not required to use these messages 17 * and in fact are encouraged to reword them to better suit their particular 18 * situation better. See how "git checkout" and "git merge" replaces 19 * them using setup_unpack_trees_porcelain(), for example. 20 */ 21static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 22/* ERROR_WOULD_OVERWRITE */ 23"Entry '%s' would be overwritten by merge. Cannot merge.", 24 25/* ERROR_NOT_UPTODATE_FILE */ 26"Entry '%s' not uptodate. Cannot merge.", 27 28/* ERROR_NOT_UPTODATE_DIR */ 29"Updating '%s' would lose untracked files in it", 30 31/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 32"Untracked working tree file '%s' would be overwritten by merge.", 33 34/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 35"Untracked working tree file '%s' would be removed by merge.", 36 37/* ERROR_BIND_OVERLAP */ 38"Entry '%s' overlaps with '%s'. Cannot bind.", 39 40/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 41"Entry '%s' not uptodate. Cannot update sparse checkout.", 42 43/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 44"Working tree file '%s' would be overwritten by sparse checkout update.", 45 46/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 47"Working tree file '%s' would be removed by sparse checkout update.", 48}; 49 50#define ERRORMSG(o,type) \ 51 ( ((o) && (o)->msgs[(type)]) \ 52 ? ((o)->msgs[(type)]) \ 53 : (unpack_plumbing_errors[(type)]) ) 54 55voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 56const char*cmd) 57{ 58int i; 59const char**msgs = opts->msgs; 60const char*msg; 61 62if(!strcmp(cmd,"checkout")) 63 msg = advice_commit_before_merge 64?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 65"Please commit your changes or stash them before you switch branches.") 66:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 67else if(!strcmp(cmd,"merge")) 68 msg = advice_commit_before_merge 69?_("Your local changes to the following files would be overwritten by merge:\n%%s" 70"Please commit your changes or stash them before you merge.") 71:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 72else 73 msg = advice_commit_before_merge 74?_("Your local changes to the following files would be overwritten by%s:\n%%s" 75"Please commit your changes or stash them before you%s.") 76:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 77 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 78xstrfmt(msg, cmd, cmd); 79 80 msgs[ERROR_NOT_UPTODATE_DIR] = 81_("Updating the following directories would lose untracked files in it:\n%s"); 82 83if(!strcmp(cmd,"checkout")) 84 msg = advice_commit_before_merge 85?_("The following untracked working tree files would be removed by checkout:\n%%s" 86"Please move or remove them before you switch branches.") 87:_("The following untracked working tree files would be removed by checkout:\n%%s"); 88else if(!strcmp(cmd,"merge")) 89 msg = advice_commit_before_merge 90?_("The following untracked working tree files would be removed by merge:\n%%s" 91"Please move or remove them before you merge.") 92:_("The following untracked working tree files would be removed by merge:\n%%s"); 93else 94 msg = advice_commit_before_merge 95?_("The following untracked working tree files would be removed by%s:\n%%s" 96"Please move or remove them before you%s.") 97:_("The following untracked working tree files would be removed by%s:\n%%s"); 98 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 99 100if(!strcmp(cmd,"checkout")) 101 msg = advice_commit_before_merge 102?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 103"Please move or remove them before you switch branches.") 104:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 105else if(!strcmp(cmd,"merge")) 106 msg = advice_commit_before_merge 107?_("The following untracked working tree files would be overwritten by merge:\n%%s" 108"Please move or remove them before you merge.") 109:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 110else 111 msg = advice_commit_before_merge 112?_("The following untracked working tree files would be overwritten by%s:\n%%s" 113"Please move or remove them before you%s.") 114:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 115 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 116 117/* 118 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 119 * cannot easily display it as a list. 120 */ 121 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 122 123 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 124_("Cannot update sparse checkout: the following entries are not up-to-date:\n%s"); 125 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 126_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 127 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 128_("The following working tree files would be removed by sparse checkout update:\n%s"); 129 130 opts->show_all_errors =1; 131/* rejected paths may not have a static buffer */ 132for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 133 opts->unpack_rejects[i].strdup_strings =1; 134} 135 136static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 137unsigned int set,unsigned int clear) 138{ 139 clear |= CE_HASHED; 140 141if(set & CE_REMOVE) 142 set |= CE_WT_REMOVE; 143 144 ce->ce_flags = (ce->ce_flags & ~clear) | set; 145returnadd_index_entry(&o->result, ce, 146 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 147} 148 149static struct cache_entry *dup_entry(const struct cache_entry *ce) 150{ 151unsigned int size =ce_size(ce); 152struct cache_entry *new=xmalloc(size); 153 154memcpy(new, ce, size); 155return new; 156} 157 158static voidadd_entry(struct unpack_trees_options *o, 159const struct cache_entry *ce, 160unsigned int set,unsigned int clear) 161{ 162do_add_entry(o,dup_entry(ce), set, clear); 163} 164 165/* 166 * add error messages on path <path> 167 * corresponding to the type <e> with the message <msg> 168 * indicating if it should be display in porcelain or not 169 */ 170static intadd_rejected_path(struct unpack_trees_options *o, 171enum unpack_trees_error_types e, 172const char*path) 173{ 174if(!o->show_all_errors) 175returnerror(ERRORMSG(o, e), path); 176 177/* 178 * Otherwise, insert in a list for future display by 179 * display_error_msgs() 180 */ 181string_list_append(&o->unpack_rejects[e], path); 182return-1; 183} 184 185/* 186 * display all the error messages stored in a nice way 187 */ 188static voiddisplay_error_msgs(struct unpack_trees_options *o) 189{ 190int e, i; 191int something_displayed =0; 192for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 193struct string_list *rejects = &o->unpack_rejects[e]; 194if(rejects->nr >0) { 195struct strbuf path = STRBUF_INIT; 196 something_displayed =1; 197for(i =0; i < rejects->nr; i++) 198strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 199error(ERRORMSG(o, e), path.buf); 200strbuf_release(&path); 201} 202string_list_clear(rejects,0); 203} 204if(something_displayed) 205fprintf(stderr,_("Aborting\n")); 206} 207 208/* 209 * Unlink the last component and schedule the leading directories for 210 * removal, such that empty directories get removed. 211 */ 212static voidunlink_entry(const struct cache_entry *ce) 213{ 214if(!check_leading_path(ce->name,ce_namelen(ce))) 215return; 216if(remove_or_warn(ce->ce_mode, ce->name)) 217return; 218schedule_dir_for_removal(ce->name,ce_namelen(ce)); 219} 220 221static intcheck_updates(struct unpack_trees_options *o) 222{ 223unsigned cnt =0, total =0; 224int errs =0; 225struct progress *progress = NULL; 226struct index_state *index = &o->result; 227struct checkout state = CHECKOUT_INIT; 228int i; 229 230 state.force =1; 231 state.quiet =1; 232 state.refresh_cache =1; 233 state.istate = index; 234 235if(o->update && o->verbose_update) { 236for(total = cnt =0; cnt < index->cache_nr; cnt++) { 237const struct cache_entry *ce = index->cache[cnt]; 238if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 239 total++; 240} 241 242 progress =start_progress_delay(_("Checking out files"), 243 total,50,1); 244 cnt =0; 245} 246 247if(o->update) 248git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 249for(i =0; i < index->cache_nr; i++) { 250const struct cache_entry *ce = index->cache[i]; 251 252if(ce->ce_flags & CE_WT_REMOVE) { 253display_progress(progress, ++cnt); 254if(o->update && !o->dry_run) 255unlink_entry(ce); 256continue; 257} 258} 259remove_marked_cache_entries(index); 260remove_scheduled_dirs(); 261 262for(i =0; i < index->cache_nr; i++) { 263struct cache_entry *ce = index->cache[i]; 264 265if(ce->ce_flags & CE_UPDATE) { 266if(ce->ce_flags & CE_WT_REMOVE) 267die("BUG: both update and delete flags are set on%s", 268 ce->name); 269display_progress(progress, ++cnt); 270 ce->ce_flags &= ~CE_UPDATE; 271if(o->update && !o->dry_run) { 272 errs |=checkout_entry(ce, &state, NULL); 273} 274} 275} 276stop_progress(&progress); 277if(o->update) 278git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 279return errs !=0; 280} 281 282static intverify_uptodate_sparse(const struct cache_entry *ce, 283struct unpack_trees_options *o); 284static intverify_absent_sparse(const struct cache_entry *ce, 285enum unpack_trees_error_types, 286struct unpack_trees_options *o); 287 288static intapply_sparse_checkout(struct index_state *istate, 289struct cache_entry *ce, 290struct unpack_trees_options *o) 291{ 292int was_skip_worktree =ce_skip_worktree(ce); 293 294if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 295 ce->ce_flags |= CE_SKIP_WORKTREE; 296else 297 ce->ce_flags &= ~CE_SKIP_WORKTREE; 298if(was_skip_worktree !=ce_skip_worktree(ce)) { 299 ce->ce_flags |= CE_UPDATE_IN_BASE; 300 istate->cache_changed |= CE_ENTRY_CHANGED; 301} 302 303/* 304 * if (!was_skip_worktree && !ce_skip_worktree()) { 305 * This is perfectly normal. Move on; 306 * } 307 */ 308 309/* 310 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 311 * area as a result of ce_skip_worktree() shortcuts in 312 * verify_absent() and verify_uptodate(). 313 * Make sure they don't modify worktree if they are already 314 * outside checkout area 315 */ 316if(was_skip_worktree &&ce_skip_worktree(ce)) { 317 ce->ce_flags &= ~CE_UPDATE; 318 319/* 320 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 321 * on to get that file removed from both index and worktree. 322 * If that file is already outside worktree area, don't 323 * bother remove it. 324 */ 325if(ce->ce_flags & CE_REMOVE) 326 ce->ce_flags &= ~CE_WT_REMOVE; 327} 328 329if(!was_skip_worktree &&ce_skip_worktree(ce)) { 330/* 331 * If CE_UPDATE is set, verify_uptodate() must be called already 332 * also stat info may have lost after merged_entry() so calling 333 * verify_uptodate() again may fail 334 */ 335if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 336return-1; 337 ce->ce_flags |= CE_WT_REMOVE; 338 ce->ce_flags &= ~CE_UPDATE; 339} 340if(was_skip_worktree && !ce_skip_worktree(ce)) { 341if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 342return-1; 343 ce->ce_flags |= CE_UPDATE; 344} 345return0; 346} 347 348staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 349struct unpack_trees_options *o) 350{ 351int ret = o->fn(src, o); 352if(ret >0) 353 ret =0; 354return ret; 355} 356 357static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 358{ 359 ce->ce_flags |= CE_UNPACKED; 360 361if(o->cache_bottom < o->src_index->cache_nr && 362 o->src_index->cache[o->cache_bottom] == ce) { 363int bottom = o->cache_bottom; 364while(bottom < o->src_index->cache_nr && 365 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 366 bottom++; 367 o->cache_bottom = bottom; 368} 369} 370 371static voidmark_all_ce_unused(struct index_state *index) 372{ 373int i; 374for(i =0; i < index->cache_nr; i++) 375 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 376} 377 378static intlocate_in_src_index(const struct cache_entry *ce, 379struct unpack_trees_options *o) 380{ 381struct index_state *index = o->src_index; 382int len =ce_namelen(ce); 383int pos =index_name_pos(index, ce->name, len); 384if(pos <0) 385 pos = -1- pos; 386return pos; 387} 388 389/* 390 * We call unpack_index_entry() with an unmerged cache entry 391 * only in diff-index, and it wants a single callback. Skip 392 * the other unmerged entry with the same name. 393 */ 394static voidmark_ce_used_same_name(struct cache_entry *ce, 395struct unpack_trees_options *o) 396{ 397struct index_state *index = o->src_index; 398int len =ce_namelen(ce); 399int pos; 400 401for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 402struct cache_entry *next = index->cache[pos]; 403if(len !=ce_namelen(next) || 404memcmp(ce->name, next->name, len)) 405break; 406mark_ce_used(next, o); 407} 408} 409 410static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 411{ 412const struct index_state *index = o->src_index; 413int pos = o->cache_bottom; 414 415while(pos < index->cache_nr) { 416struct cache_entry *ce = index->cache[pos]; 417if(!(ce->ce_flags & CE_UNPACKED)) 418return ce; 419 pos++; 420} 421return NULL; 422} 423 424static voidadd_same_unmerged(const struct cache_entry *ce, 425struct unpack_trees_options *o) 426{ 427struct index_state *index = o->src_index; 428int len =ce_namelen(ce); 429int pos =index_name_pos(index, ce->name, len); 430 431if(0<= pos) 432die("programming error in a caller of mark_ce_used_same_name"); 433for(pos = -pos -1; pos < index->cache_nr; pos++) { 434struct cache_entry *next = index->cache[pos]; 435if(len !=ce_namelen(next) || 436memcmp(ce->name, next->name, len)) 437break; 438add_entry(o, next,0,0); 439mark_ce_used(next, o); 440} 441} 442 443static intunpack_index_entry(struct cache_entry *ce, 444struct unpack_trees_options *o) 445{ 446const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 447int ret; 448 449 src[0] = ce; 450 451mark_ce_used(ce, o); 452if(ce_stage(ce)) { 453if(o->skip_unmerged) { 454add_entry(o, ce,0,0); 455return0; 456} 457} 458 ret =call_unpack_fn(src, o); 459if(ce_stage(ce)) 460mark_ce_used_same_name(ce, o); 461return ret; 462} 463 464static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 465 466static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 467{ 468struct unpack_trees_options *o = info->data; 469 470if(o->diff_index_cached) 471return; 472 o->cache_bottom = bottom; 473} 474 475static intswitch_cache_bottom(struct traverse_info *info) 476{ 477struct unpack_trees_options *o = info->data; 478int ret, pos; 479 480if(o->diff_index_cached) 481return0; 482 ret = o->cache_bottom; 483 pos =find_cache_pos(info->prev, &info->name); 484 485if(pos < -1) 486 o->cache_bottom = -2- pos; 487else if(pos <0) 488 o->cache_bottom = o->src_index->cache_nr; 489return ret; 490} 491 492static inttraverse_trees_recursive(int n,unsigned long dirmask, 493unsigned long df_conflicts, 494struct name_entry *names, 495struct traverse_info *info) 496{ 497int i, ret, bottom; 498struct tree_desc t[MAX_UNPACK_TREES]; 499void*buf[MAX_UNPACK_TREES]; 500struct traverse_info newinfo; 501struct name_entry *p; 502 503 p = names; 504while(!p->mode) 505 p++; 506 507 newinfo = *info; 508 newinfo.prev = info; 509 newinfo.pathspec = info->pathspec; 510 newinfo.name = *p; 511 newinfo.pathlen +=tree_entry_len(p) +1; 512 newinfo.df_conflicts |= df_conflicts; 513 514for(i =0; i < n; i++, dirmask >>=1) { 515const unsigned char*sha1 = NULL; 516if(dirmask &1) 517 sha1 = names[i].oid->hash; 518 buf[i] =fill_tree_descriptor(t+i, sha1); 519} 520 521 bottom =switch_cache_bottom(&newinfo); 522 ret =traverse_trees(n, t, &newinfo); 523restore_cache_bottom(&newinfo, bottom); 524 525for(i =0; i < n; i++) 526free(buf[i]); 527 528return ret; 529} 530 531/* 532 * Compare the traverse-path to the cache entry without actually 533 * having to generate the textual representation of the traverse 534 * path. 535 * 536 * NOTE! This *only* compares up to the size of the traverse path 537 * itself - the caller needs to do the final check for the cache 538 * entry having more data at the end! 539 */ 540static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 541{ 542int len, pathlen, ce_len; 543const char*ce_name; 544 545if(info->prev) { 546int cmp =do_compare_entry_piecewise(ce, info->prev, 547&info->name); 548if(cmp) 549return cmp; 550} 551 pathlen = info->pathlen; 552 ce_len =ce_namelen(ce); 553 554/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 555if(ce_len < pathlen) 556return-1; 557 558 ce_len -= pathlen; 559 ce_name = ce->name + pathlen; 560 561 len =tree_entry_len(n); 562returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 563} 564 565static intdo_compare_entry(const struct cache_entry *ce, 566const struct traverse_info *info, 567const struct name_entry *n) 568{ 569int len, pathlen, ce_len; 570const char*ce_name; 571int cmp; 572 573/* 574 * If we have not precomputed the traverse path, it is quicker 575 * to avoid doing so. But if we have precomputed it, 576 * it is quicker to use the precomputed version. 577 */ 578if(!info->traverse_path) 579returndo_compare_entry_piecewise(ce, info, n); 580 581 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 582if(cmp) 583return cmp; 584 585 pathlen = info->pathlen; 586 ce_len =ce_namelen(ce); 587 588if(ce_len < pathlen) 589return-1; 590 591 ce_len -= pathlen; 592 ce_name = ce->name + pathlen; 593 594 len =tree_entry_len(n); 595returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 596} 597 598static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 599{ 600int cmp =do_compare_entry(ce, info, n); 601if(cmp) 602return cmp; 603 604/* 605 * Even if the beginning compared identically, the ce should 606 * compare as bigger than a directory leading up to it! 607 */ 608returnce_namelen(ce) >traverse_path_len(info, n); 609} 610 611static intce_in_traverse_path(const struct cache_entry *ce, 612const struct traverse_info *info) 613{ 614if(!info->prev) 615return1; 616if(do_compare_entry(ce, info->prev, &info->name)) 617return0; 618/* 619 * If ce (blob) is the same name as the path (which is a tree 620 * we will be descending into), it won't be inside it. 621 */ 622return(info->pathlen <ce_namelen(ce)); 623} 624 625static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 626{ 627int len =traverse_path_len(info, n); 628struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 629 630 ce->ce_mode =create_ce_mode(n->mode); 631 ce->ce_flags =create_ce_flags(stage); 632 ce->ce_namelen = len; 633oidcpy(&ce->oid, n->oid); 634make_traverse_path(ce->name, info, n); 635 636return ce; 637} 638 639static intunpack_nondirectories(int n,unsigned long mask, 640unsigned long dirmask, 641struct cache_entry **src, 642const struct name_entry *names, 643const struct traverse_info *info) 644{ 645int i; 646struct unpack_trees_options *o = info->data; 647unsigned long conflicts = info->df_conflicts | dirmask; 648 649/* Do we have *only* directories? Nothing to do */ 650if(mask == dirmask && !src[0]) 651return0; 652 653/* 654 * Ok, we've filled in up to any potential index entry in src[0], 655 * now do the rest. 656 */ 657for(i =0; i < n; i++) { 658int stage; 659unsigned int bit =1ul<< i; 660if(conflicts & bit) { 661 src[i + o->merge] = o->df_conflict_entry; 662continue; 663} 664if(!(mask & bit)) 665continue; 666if(!o->merge) 667 stage =0; 668else if(i +1< o->head_idx) 669 stage =1; 670else if(i +1> o->head_idx) 671 stage =3; 672else 673 stage =2; 674 src[i + o->merge] =create_ce_entry(info, names + i, stage); 675} 676 677if(o->merge) { 678int rc =call_unpack_fn((const struct cache_entry *const*)src, 679 o); 680for(i =0; i < n; i++) { 681struct cache_entry *ce = src[i + o->merge]; 682if(ce != o->df_conflict_entry) 683free(ce); 684} 685return rc; 686} 687 688for(i =0; i < n; i++) 689if(src[i] && src[i] != o->df_conflict_entry) 690if(do_add_entry(o, src[i],0,0)) 691return-1; 692 693return0; 694} 695 696static intunpack_failed(struct unpack_trees_options *o,const char*message) 697{ 698discard_index(&o->result); 699if(!o->gently && !o->exiting_early) { 700if(message) 701returnerror("%s", message); 702return-1; 703} 704return-1; 705} 706 707/* 708 * The tree traversal is looking at name p. If we have a matching entry, 709 * return it. If name p is a directory in the index, do not return 710 * anything, as we will want to match it when the traversal descends into 711 * the directory. 712 */ 713static intfind_cache_pos(struct traverse_info *info, 714const struct name_entry *p) 715{ 716int pos; 717struct unpack_trees_options *o = info->data; 718struct index_state *index = o->src_index; 719int pfxlen = info->pathlen; 720int p_len =tree_entry_len(p); 721 722for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 723const struct cache_entry *ce = index->cache[pos]; 724const char*ce_name, *ce_slash; 725int cmp, ce_len; 726 727if(ce->ce_flags & CE_UNPACKED) { 728/* 729 * cache_bottom entry is already unpacked, so 730 * we can never match it; don't check it 731 * again. 732 */ 733if(pos == o->cache_bottom) 734++o->cache_bottom; 735continue; 736} 737if(!ce_in_traverse_path(ce, info)) { 738/* 739 * Check if we can skip future cache checks 740 * (because we're already past all possible 741 * entries in the traverse path). 742 */ 743if(info->traverse_path) { 744if(strncmp(ce->name, info->traverse_path, 745 info->pathlen) >0) 746break; 747} 748continue; 749} 750 ce_name = ce->name + pfxlen; 751 ce_slash =strchr(ce_name,'/'); 752if(ce_slash) 753 ce_len = ce_slash - ce_name; 754else 755 ce_len =ce_namelen(ce) - pfxlen; 756 cmp =name_compare(p->path, p_len, ce_name, ce_len); 757/* 758 * Exact match; if we have a directory we need to 759 * delay returning it. 760 */ 761if(!cmp) 762return ce_slash ? -2- pos : pos; 763if(0< cmp) 764continue;/* keep looking */ 765/* 766 * ce_name sorts after p->path; could it be that we 767 * have files under p->path directory in the index? 768 * E.g. ce_name == "t-i", and p->path == "t"; we may 769 * have "t/a" in the index. 770 */ 771if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 772 ce_name[p_len] <'/') 773continue;/* keep looking */ 774break; 775} 776return-1; 777} 778 779static struct cache_entry *find_cache_entry(struct traverse_info *info, 780const struct name_entry *p) 781{ 782int pos =find_cache_pos(info, p); 783struct unpack_trees_options *o = info->data; 784 785if(0<= pos) 786return o->src_index->cache[pos]; 787else 788return NULL; 789} 790 791static voiddebug_path(struct traverse_info *info) 792{ 793if(info->prev) { 794debug_path(info->prev); 795if(*info->prev->name.path) 796putchar('/'); 797} 798printf("%s", info->name.path); 799} 800 801static voiddebug_name_entry(int i,struct name_entry *n) 802{ 803printf("ent#%d %06o%s\n", i, 804 n->path ? n->mode :0, 805 n->path ? n->path :"(missing)"); 806} 807 808static voiddebug_unpack_callback(int n, 809unsigned long mask, 810unsigned long dirmask, 811struct name_entry *names, 812struct traverse_info *info) 813{ 814int i; 815printf("* unpack mask%lu, dirmask%lu, cnt%d", 816 mask, dirmask, n); 817debug_path(info); 818putchar('\n'); 819for(i =0; i < n; i++) 820debug_name_entry(i, names + i); 821} 822 823static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 824{ 825struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 826struct unpack_trees_options *o = info->data; 827const struct name_entry *p = names; 828 829/* Find first entry with a real name (we could use "mask" too) */ 830while(!p->mode) 831 p++; 832 833if(o->debug_unpack) 834debug_unpack_callback(n, mask, dirmask, names, info); 835 836/* Are we supposed to look at the index too? */ 837if(o->merge) { 838while(1) { 839int cmp; 840struct cache_entry *ce; 841 842if(o->diff_index_cached) 843 ce =next_cache_entry(o); 844else 845 ce =find_cache_entry(info, p); 846 847if(!ce) 848break; 849 cmp =compare_entry(ce, info, p); 850if(cmp <0) { 851if(unpack_index_entry(ce, o) <0) 852returnunpack_failed(o, NULL); 853continue; 854} 855if(!cmp) { 856if(ce_stage(ce)) { 857/* 858 * If we skip unmerged index 859 * entries, we'll skip this 860 * entry *and* the tree 861 * entries associated with it! 862 */ 863if(o->skip_unmerged) { 864add_same_unmerged(ce, o); 865return mask; 866} 867} 868 src[0] = ce; 869} 870break; 871} 872} 873 874if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 875return-1; 876 877if(o->merge && src[0]) { 878if(ce_stage(src[0])) 879mark_ce_used_same_name(src[0], o); 880else 881mark_ce_used(src[0], o); 882} 883 884/* Now handle any directories.. */ 885if(dirmask) { 886/* special case: "diff-index --cached" looking at a tree */ 887if(o->diff_index_cached && 888 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 889int matches; 890 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 891 names, info); 892/* 893 * Everything under the name matches; skip the 894 * entire hierarchy. diff_index_cached codepath 895 * special cases D/F conflicts in such a way that 896 * it does not do any look-ahead, so this is safe. 897 */ 898if(matches) { 899 o->cache_bottom += matches; 900return mask; 901} 902} 903 904if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 905 names, info) <0) 906return-1; 907return mask; 908} 909 910return mask; 911} 912 913static intclear_ce_flags_1(struct cache_entry **cache,int nr, 914struct strbuf *prefix, 915int select_mask,int clear_mask, 916struct exclude_list *el,int defval); 917 918/* Whole directory matching */ 919static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 920struct strbuf *prefix, 921char*basename, 922int select_mask,int clear_mask, 923struct exclude_list *el,int defval) 924{ 925struct cache_entry **cache_end; 926int dtype = DT_DIR; 927int ret =is_excluded_from_list(prefix->buf, prefix->len, 928 basename, &dtype, el); 929int rc; 930 931strbuf_addch(prefix,'/'); 932 933/* If undecided, use matching result of parent dir in defval */ 934if(ret <0) 935 ret = defval; 936 937for(cache_end = cache; cache_end != cache + nr; cache_end++) { 938struct cache_entry *ce = *cache_end; 939if(strncmp(ce->name, prefix->buf, prefix->len)) 940break; 941} 942 943/* 944 * TODO: check el, if there are no patterns that may conflict 945 * with ret (iow, we know in advance the incl/excl 946 * decision for the entire directory), clear flag here without 947 * calling clear_ce_flags_1(). That function will call 948 * the expensive is_excluded_from_list() on every entry. 949 */ 950 rc =clear_ce_flags_1(cache, cache_end - cache, 951 prefix, 952 select_mask, clear_mask, 953 el, ret); 954strbuf_setlen(prefix, prefix->len -1); 955return rc; 956} 957 958/* 959 * Traverse the index, find every entry that matches according to 960 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the 961 * number of traversed entries. 962 * 963 * If select_mask is non-zero, only entries whose ce_flags has on of 964 * those bits enabled are traversed. 965 * 966 * cache : pointer to an index entry 967 * prefix_len : an offset to its path 968 * 969 * The current path ("prefix") including the trailing '/' is 970 * cache[0]->name[0..(prefix_len-1)] 971 * Top level path has prefix_len zero. 972 */ 973static intclear_ce_flags_1(struct cache_entry **cache,int nr, 974struct strbuf *prefix, 975int select_mask,int clear_mask, 976struct exclude_list *el,int defval) 977{ 978struct cache_entry **cache_end = cache + nr; 979 980/* 981 * Process all entries that have the given prefix and meet 982 * select_mask condition 983 */ 984while(cache != cache_end) { 985struct cache_entry *ce = *cache; 986const char*name, *slash; 987int len, dtype, ret; 988 989if(select_mask && !(ce->ce_flags & select_mask)) { 990 cache++; 991continue; 992} 993 994if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len)) 995break; 996 997 name = ce->name + prefix->len; 998 slash =strchr(name,'/'); 9991000/* If it's a directory, try whole directory match first */1001if(slash) {1002int processed;10031004 len = slash - name;1005strbuf_add(prefix, name, len);10061007 processed =clear_ce_flags_dir(cache, cache_end - cache,1008 prefix,1009 prefix->buf + prefix->len - len,1010 select_mask, clear_mask,1011 el, defval);10121013/* clear_c_f_dir eats a whole dir already? */1014if(processed) {1015 cache += processed;1016strbuf_setlen(prefix, prefix->len - len);1017continue;1018}10191020strbuf_addch(prefix,'/');1021 cache +=clear_ce_flags_1(cache, cache_end - cache,1022 prefix,1023 select_mask, clear_mask, el, defval);1024strbuf_setlen(prefix, prefix->len - len -1);1025continue;1026}10271028/* Non-directory */1029 dtype =ce_to_dtype(ce);1030 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1031 name, &dtype, el);1032if(ret <0)1033 ret = defval;1034if(ret >0)1035 ce->ce_flags &= ~clear_mask;1036 cache++;1037}1038return nr - (cache_end - cache);1039}10401041static intclear_ce_flags(struct cache_entry **cache,int nr,1042int select_mask,int clear_mask,1043struct exclude_list *el)1044{1045static struct strbuf prefix = STRBUF_INIT;10461047strbuf_reset(&prefix);10481049returnclear_ce_flags_1(cache, nr,1050&prefix,1051 select_mask, clear_mask,1052 el,0);1053}10541055/*1056 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1057 */1058static voidmark_new_skip_worktree(struct exclude_list *el,1059struct index_state *the_index,1060int select_flag,int skip_wt_flag)1061{1062int i;10631064/*1065 * 1. Pretend the narrowest worktree: only unmerged entries1066 * are checked out1067 */1068for(i =0; i < the_index->cache_nr; i++) {1069struct cache_entry *ce = the_index->cache[i];10701071if(select_flag && !(ce->ce_flags & select_flag))1072continue;10731074if(!ce_stage(ce))1075 ce->ce_flags |= skip_wt_flag;1076else1077 ce->ce_flags &= ~skip_wt_flag;1078}10791080/*1081 * 2. Widen worktree according to sparse-checkout file.1082 * Matched entries will have skip_wt_flag cleared (i.e. "in")1083 */1084clear_ce_flags(the_index->cache, the_index->cache_nr,1085 select_flag, skip_wt_flag, el);1086}10871088static intverify_absent(const struct cache_entry *,1089enum unpack_trees_error_types,1090struct unpack_trees_options *);1091/*1092 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1093 * resulting index, -2 on failure to reflect the changes to the work tree.1094 *1095 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1096 */1097intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1098{1099int i, ret;1100static struct cache_entry *dfc;1101struct exclude_list el;11021103if(len > MAX_UNPACK_TREES)1104die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);11051106memset(&el,0,sizeof(el));1107if(!core_apply_sparse_checkout || !o->update)1108 o->skip_sparse_checkout =1;1109if(!o->skip_sparse_checkout) {1110char*sparse =git_pathdup("info/sparse-checkout");1111if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1112 o->skip_sparse_checkout =1;1113else1114 o->el = ⪙1115free(sparse);1116}11171118memset(&o->result,0,sizeof(o->result));1119 o->result.initialized =1;1120 o->result.timestamp.sec = o->src_index->timestamp.sec;1121 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1122 o->result.version = o->src_index->version;1123 o->result.split_index = o->src_index->split_index;1124if(o->result.split_index)1125 o->result.split_index->refcount++;1126hashcpy(o->result.sha1, o->src_index->sha1);1127 o->merge_size = len;1128mark_all_ce_unused(o->src_index);11291130/*1131 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1132 */1133if(!o->skip_sparse_checkout)1134mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);11351136if(!dfc)1137 dfc =xcalloc(1,cache_entry_size(0));1138 o->df_conflict_entry = dfc;11391140if(len) {1141const char*prefix = o->prefix ? o->prefix :"";1142struct traverse_info info;11431144setup_traverse_info(&info, prefix);1145 info.fn = unpack_callback;1146 info.data = o;1147 info.show_all_errors = o->show_all_errors;1148 info.pathspec = o->pathspec;11491150if(o->prefix) {1151/*1152 * Unpack existing index entries that sort before the1153 * prefix the tree is spliced into. Note that o->merge1154 * is always true in this case.1155 */1156while(1) {1157struct cache_entry *ce =next_cache_entry(o);1158if(!ce)1159break;1160if(ce_in_traverse_path(ce, &info))1161break;1162if(unpack_index_entry(ce, o) <0)1163goto return_failed;1164}1165}11661167if(traverse_trees(len, t, &info) <0)1168goto return_failed;1169}11701171/* Any left-over entries in the index? */1172if(o->merge) {1173while(1) {1174struct cache_entry *ce =next_cache_entry(o);1175if(!ce)1176break;1177if(unpack_index_entry(ce, o) <0)1178goto return_failed;1179}1180}1181mark_all_ce_unused(o->src_index);11821183if(o->trivial_merges_only && o->nontrivial_merge) {1184 ret =unpack_failed(o,"Merge requires file-level merging");1185goto done;1186}11871188if(!o->skip_sparse_checkout) {1189int empty_worktree =1;11901191/*1192 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11193 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1194 * so apply_sparse_checkout() won't attempt to remove it from worktree1195 */1196mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);11971198 ret =0;1199for(i =0; i < o->result.cache_nr; i++) {1200struct cache_entry *ce = o->result.cache[i];12011202/*1203 * Entries marked with CE_ADDED in merged_entry() do not have1204 * verify_absent() check (the check is effectively disabled1205 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1206 *1207 * Do the real check now because we have had1208 * correct CE_NEW_SKIP_WORKTREE1209 */1210if(ce->ce_flags & CE_ADDED &&1211verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1212if(!o->show_all_errors)1213goto return_failed;1214 ret = -1;1215}12161217if(apply_sparse_checkout(&o->result, ce, o)) {1218if(!o->show_all_errors)1219goto return_failed;1220 ret = -1;1221}1222if(!ce_skip_worktree(ce))1223 empty_worktree =0;12241225}1226if(ret <0)1227goto return_failed;1228/*1229 * Sparse checkout is meant to narrow down checkout area1230 * but it does not make sense to narrow down to empty working1231 * tree. This is usually a mistake in sparse checkout rules.1232 * Do not allow users to do that.1233 */1234if(o->result.cache_nr && empty_worktree) {1235 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1236goto done;1237}1238}12391240 o->src_index = NULL;1241 ret =check_updates(o) ? (-2) :0;1242if(o->dst_index) {1243if(!ret) {1244if(!o->result.cache_tree)1245 o->result.cache_tree =cache_tree();1246if(!cache_tree_fully_valid(o->result.cache_tree))1247cache_tree_update(&o->result,1248 WRITE_TREE_SILENT |1249 WRITE_TREE_REPAIR);1250}1251discard_index(o->dst_index);1252*o->dst_index = o->result;1253}else{1254discard_index(&o->result);1255}12561257done:1258clear_exclude_list(&el);1259return ret;12601261return_failed:1262if(o->show_all_errors)1263display_error_msgs(o);1264mark_all_ce_unused(o->src_index);1265 ret =unpack_failed(o, NULL);1266if(o->exiting_early)1267 ret =0;1268goto done;1269}12701271/* Here come the merge functions */12721273static intreject_merge(const struct cache_entry *ce,1274struct unpack_trees_options *o)1275{1276return o->gently ? -1:1277add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1278}12791280static intsame(const struct cache_entry *a,const struct cache_entry *b)1281{1282if(!!a != !!b)1283return0;1284if(!a && !b)1285return1;1286if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1287return0;1288return a->ce_mode == b->ce_mode &&1289!oidcmp(&a->oid, &b->oid);1290}129112921293/*1294 * When a CE gets turned into an unmerged entry, we1295 * want it to be up-to-date1296 */1297static intverify_uptodate_1(const struct cache_entry *ce,1298struct unpack_trees_options *o,1299enum unpack_trees_error_types error_type)1300{1301struct stat st;13021303if(o->index_only)1304return0;13051306/*1307 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1308 * if this entry is truly up-to-date because this file may be1309 * overwritten.1310 */1311if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1312;/* keep checking */1313else if(o->reset ||ce_uptodate(ce))1314return0;13151316if(!lstat(ce->name, &st)) {1317int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1318unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1319if(!changed)1320return0;1321/*1322 * NEEDSWORK: the current default policy is to allow1323 * submodule to be out of sync wrt the superproject1324 * index. This needs to be tightened later for1325 * submodules that are marked to be automatically1326 * checked out.1327 */1328if(S_ISGITLINK(ce->ce_mode))1329return0;1330 errno =0;1331}1332if(errno == ENOENT)1333return0;1334return o->gently ? -1:1335add_rejected_path(o, error_type, ce->name);1336}13371338static intverify_uptodate(const struct cache_entry *ce,1339struct unpack_trees_options *o)1340{1341if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1342return0;1343returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1344}13451346static intverify_uptodate_sparse(const struct cache_entry *ce,1347struct unpack_trees_options *o)1348{1349returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1350}13511352static voidinvalidate_ce_path(const struct cache_entry *ce,1353struct unpack_trees_options *o)1354{1355if(!ce)1356return;1357cache_tree_invalidate_path(o->src_index, ce->name);1358untracked_cache_invalidate_path(o->src_index, ce->name);1359}13601361/*1362 * Check that checking out ce->sha1 in subdir ce->name is not1363 * going to overwrite any working files.1364 *1365 * Currently, git does not checkout subprojects during a superproject1366 * checkout, so it is not going to overwrite anything.1367 */1368static intverify_clean_submodule(const struct cache_entry *ce,1369enum unpack_trees_error_types error_type,1370struct unpack_trees_options *o)1371{1372return0;1373}13741375static intverify_clean_subdirectory(const struct cache_entry *ce,1376enum unpack_trees_error_types error_type,1377struct unpack_trees_options *o)1378{1379/*1380 * we are about to extract "ce->name"; we would not want to lose1381 * anything in the existing directory there.1382 */1383int namelen;1384int i;1385struct dir_struct d;1386char*pathbuf;1387int cnt =0;1388unsigned char sha1[20];13891390if(S_ISGITLINK(ce->ce_mode) &&1391resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1392/* If we are not going to update the submodule, then1393 * we don't care.1394 */1395if(!hashcmp(sha1, ce->oid.hash))1396return0;1397returnverify_clean_submodule(ce, error_type, o);1398}13991400/*1401 * First let's make sure we do not have a local modification1402 * in that directory.1403 */1404 namelen =ce_namelen(ce);1405for(i =locate_in_src_index(ce, o);1406 i < o->src_index->cache_nr;1407 i++) {1408struct cache_entry *ce2 = o->src_index->cache[i];1409int len =ce_namelen(ce2);1410if(len < namelen ||1411strncmp(ce->name, ce2->name, namelen) ||1412 ce2->name[namelen] !='/')1413break;1414/*1415 * ce2->name is an entry in the subdirectory to be1416 * removed.1417 */1418if(!ce_stage(ce2)) {1419if(verify_uptodate(ce2, o))1420return-1;1421add_entry(o, ce2, CE_REMOVE,0);1422mark_ce_used(ce2, o);1423}1424 cnt++;1425}14261427/*1428 * Then we need to make sure that we do not lose a locally1429 * present file that is not ignored.1430 */1431 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14321433memset(&d,0,sizeof(d));1434if(o->dir)1435 d.exclude_per_dir = o->dir->exclude_per_dir;1436 i =read_directory(&d, pathbuf, namelen+1, NULL);1437if(i)1438return o->gently ? -1:1439add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1440free(pathbuf);1441return cnt;1442}14431444/*1445 * This gets called when there was no index entry for the tree entry 'dst',1446 * but we found a file in the working tree that 'lstat()' said was fine,1447 * and we're on a case-insensitive filesystem.1448 *1449 * See if we can find a case-insensitive match in the index that also1450 * matches the stat information, and assume it's that other file!1451 */1452static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1453{1454const struct cache_entry *src;14551456 src =index_file_exists(o->src_index, name, len,1);1457return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1458}14591460static intcheck_ok_to_remove(const char*name,int len,int dtype,1461const struct cache_entry *ce,struct stat *st,1462enum unpack_trees_error_types error_type,1463struct unpack_trees_options *o)1464{1465const struct cache_entry *result;14661467/*1468 * It may be that the 'lstat()' succeeded even though1469 * target 'ce' was absent, because there is an old1470 * entry that is different only in case..1471 *1472 * Ignore that lstat() if it matches.1473 */1474if(ignore_case &&icase_exists(o, name, len, st))1475return0;14761477if(o->dir &&1478is_excluded(o->dir, name, &dtype))1479/*1480 * ce->name is explicitly excluded, so it is Ok to1481 * overwrite it.1482 */1483return0;1484if(S_ISDIR(st->st_mode)) {1485/*1486 * We are checking out path "foo" and1487 * found "foo/." in the working tree.1488 * This is tricky -- if we have modified1489 * files that are in "foo/" we would lose1490 * them.1491 */1492if(verify_clean_subdirectory(ce, error_type, o) <0)1493return-1;1494return0;1495}14961497/*1498 * The previous round may already have decided to1499 * delete this path, which is in a subdirectory that1500 * is being replaced with a blob.1501 */1502 result =index_file_exists(&o->result, name, len,0);1503if(result) {1504if(result->ce_flags & CE_REMOVE)1505return0;1506}15071508return o->gently ? -1:1509add_rejected_path(o, error_type, name);1510}15111512/*1513 * We do not want to remove or overwrite a working tree file that1514 * is not tracked, unless it is ignored.1515 */1516static intverify_absent_1(const struct cache_entry *ce,1517enum unpack_trees_error_types error_type,1518struct unpack_trees_options *o)1519{1520int len;1521struct stat st;15221523if(o->index_only || o->reset || !o->update)1524return0;15251526 len =check_leading_path(ce->name,ce_namelen(ce));1527if(!len)1528return0;1529else if(len >0) {1530char*path;1531int ret;15321533 path =xmemdupz(ce->name, len);1534if(lstat(path, &st))1535 ret =error_errno("cannot stat '%s'", path);1536else1537 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1538&st, error_type, o);1539free(path);1540return ret;1541}else if(lstat(ce->name, &st)) {1542if(errno != ENOENT)1543returnerror_errno("cannot stat '%s'", ce->name);1544return0;1545}else{1546returncheck_ok_to_remove(ce->name,ce_namelen(ce),1547ce_to_dtype(ce), ce, &st,1548 error_type, o);1549}1550}15511552static intverify_absent(const struct cache_entry *ce,1553enum unpack_trees_error_types error_type,1554struct unpack_trees_options *o)1555{1556if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1557return0;1558returnverify_absent_1(ce, error_type, o);1559}15601561static intverify_absent_sparse(const struct cache_entry *ce,1562enum unpack_trees_error_types error_type,1563struct unpack_trees_options *o)1564{1565enum unpack_trees_error_types orphaned_error = error_type;1566if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1567 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;15681569returnverify_absent_1(ce, orphaned_error, o);1570}15711572static intmerged_entry(const struct cache_entry *ce,1573const struct cache_entry *old,1574struct unpack_trees_options *o)1575{1576int update = CE_UPDATE;1577struct cache_entry *merge =dup_entry(ce);15781579if(!old) {1580/*1581 * New index entries. In sparse checkout, the following1582 * verify_absent() will be delayed until after1583 * traverse_trees() finishes in unpack_trees(), then:1584 *1585 * - CE_NEW_SKIP_WORKTREE will be computed correctly1586 * - verify_absent() be called again, this time with1587 * correct CE_NEW_SKIP_WORKTREE1588 *1589 * verify_absent() call here does nothing in sparse1590 * checkout (i.e. o->skip_sparse_checkout == 0)1591 */1592 update |= CE_ADDED;1593 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;15941595if(verify_absent(merge,1596 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1597free(merge);1598return-1;1599}1600invalidate_ce_path(merge, o);1601}else if(!(old->ce_flags & CE_CONFLICTED)) {1602/*1603 * See if we can re-use the old CE directly?1604 * That way we get the uptodate stat info.1605 *1606 * This also removes the UPDATE flag on a match; otherwise1607 * we will end up overwriting local changes in the work tree.1608 */1609if(same(old, merge)) {1610copy_cache_entry(merge, old);1611 update =0;1612}else{1613if(verify_uptodate(old, o)) {1614free(merge);1615return-1;1616}1617/* Migrate old flags over */1618 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1619invalidate_ce_path(old, o);1620}1621}else{1622/*1623 * Previously unmerged entry left as an existence1624 * marker by read_index_unmerged();1625 */1626invalidate_ce_path(old, o);1627}16281629do_add_entry(o, merge, update, CE_STAGEMASK);1630return1;1631}16321633static intdeleted_entry(const struct cache_entry *ce,1634const struct cache_entry *old,1635struct unpack_trees_options *o)1636{1637/* Did it exist in the index? */1638if(!old) {1639if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1640return-1;1641return0;1642}1643if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1644return-1;1645add_entry(o, ce, CE_REMOVE,0);1646invalidate_ce_path(ce, o);1647return1;1648}16491650static intkeep_entry(const struct cache_entry *ce,1651struct unpack_trees_options *o)1652{1653add_entry(o, ce,0,0);1654return1;1655}16561657#if DBRT_DEBUG1658static voidshow_stage_entry(FILE*o,1659const char*label,const struct cache_entry *ce)1660{1661if(!ce)1662fprintf(o,"%s(missing)\n", label);1663else1664fprintf(o,"%s%06o%s %d\t%s\n",1665 label,1666 ce->ce_mode,1667oid_to_hex(&ce->oid),1668ce_stage(ce),1669 ce->name);1670}1671#endif16721673intthreeway_merge(const struct cache_entry *const*stages,1674struct unpack_trees_options *o)1675{1676const struct cache_entry *index;1677const struct cache_entry *head;1678const struct cache_entry *remote = stages[o->head_idx +1];1679int count;1680int head_match =0;1681int remote_match =0;16821683int df_conflict_head =0;1684int df_conflict_remote =0;16851686int any_anc_missing =0;1687int no_anc_exists =1;1688int i;16891690for(i =1; i < o->head_idx; i++) {1691if(!stages[i] || stages[i] == o->df_conflict_entry)1692 any_anc_missing =1;1693else1694 no_anc_exists =0;1695}16961697 index = stages[0];1698 head = stages[o->head_idx];16991700if(head == o->df_conflict_entry) {1701 df_conflict_head =1;1702 head = NULL;1703}17041705if(remote == o->df_conflict_entry) {1706 df_conflict_remote =1;1707 remote = NULL;1708}17091710/*1711 * First, if there's a #16 situation, note that to prevent #131712 * and #14.1713 */1714if(!same(remote, head)) {1715for(i =1; i < o->head_idx; i++) {1716if(same(stages[i], head)) {1717 head_match = i;1718}1719if(same(stages[i], remote)) {1720 remote_match = i;1721}1722}1723}17241725/*1726 * We start with cases where the index is allowed to match1727 * something other than the head: #14(ALT) and #2ALT, where it1728 * is permitted to match the result instead.1729 */1730/* #14, #14ALT, #2ALT */1731if(remote && !df_conflict_head && head_match && !remote_match) {1732if(index && !same(index, remote) && !same(index, head))1733returnreject_merge(index, o);1734returnmerged_entry(remote, index, o);1735}1736/*1737 * If we have an entry in the index cache, then we want to1738 * make sure that it matches head.1739 */1740if(index && !same(index, head))1741returnreject_merge(index, o);17421743if(head) {1744/* #5ALT, #15 */1745if(same(head, remote))1746returnmerged_entry(head, index, o);1747/* #13, #3ALT */1748if(!df_conflict_remote && remote_match && !head_match)1749returnmerged_entry(head, index, o);1750}17511752/* #1 */1753if(!head && !remote && any_anc_missing)1754return0;17551756/*1757 * Under the "aggressive" rule, we resolve mostly trivial1758 * cases that we historically had git-merge-one-file resolve.1759 */1760if(o->aggressive) {1761int head_deleted = !head;1762int remote_deleted = !remote;1763const struct cache_entry *ce = NULL;17641765if(index)1766 ce = index;1767else if(head)1768 ce = head;1769else if(remote)1770 ce = remote;1771else{1772for(i =1; i < o->head_idx; i++) {1773if(stages[i] && stages[i] != o->df_conflict_entry) {1774 ce = stages[i];1775break;1776}1777}1778}17791780/*1781 * Deleted in both.1782 * Deleted in one and unchanged in the other.1783 */1784if((head_deleted && remote_deleted) ||1785(head_deleted && remote && remote_match) ||1786(remote_deleted && head && head_match)) {1787if(index)1788returndeleted_entry(index, index, o);1789if(ce && !head_deleted) {1790if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1791return-1;1792}1793return0;1794}1795/*1796 * Added in both, identically.1797 */1798if(no_anc_exists && head && remote &&same(head, remote))1799returnmerged_entry(head, index, o);18001801}18021803/* Below are "no merge" cases, which require that the index be1804 * up-to-date to avoid the files getting overwritten with1805 * conflict resolution files.1806 */1807if(index) {1808if(verify_uptodate(index, o))1809return-1;1810}18111812 o->nontrivial_merge =1;18131814/* #2, #3, #4, #6, #7, #9, #10, #11. */1815 count =0;1816if(!head_match || !remote_match) {1817for(i =1; i < o->head_idx; i++) {1818if(stages[i] && stages[i] != o->df_conflict_entry) {1819keep_entry(stages[i], o);1820 count++;1821break;1822}1823}1824}1825#if DBRT_DEBUG1826else{1827fprintf(stderr,"read-tree: warning #16 detected\n");1828show_stage_entry(stderr,"head ", stages[head_match]);1829show_stage_entry(stderr,"remote ", stages[remote_match]);1830}1831#endif1832if(head) { count +=keep_entry(head, o); }1833if(remote) { count +=keep_entry(remote, o); }1834return count;1835}18361837/*1838 * Two-way merge.1839 *1840 * The rule is to "carry forward" what is in the index without losing1841 * information across a "fast-forward", favoring a successful merge1842 * over a merge failure when it makes sense. For details of the1843 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1844 *1845 */1846inttwoway_merge(const struct cache_entry *const*src,1847struct unpack_trees_options *o)1848{1849const struct cache_entry *current = src[0];1850const struct cache_entry *oldtree = src[1];1851const struct cache_entry *newtree = src[2];18521853if(o->merge_size !=2)1854returnerror("Cannot do a twoway merge of%dtrees",1855 o->merge_size);18561857if(oldtree == o->df_conflict_entry)1858 oldtree = NULL;1859if(newtree == o->df_conflict_entry)1860 newtree = NULL;18611862if(current) {1863if(current->ce_flags & CE_CONFLICTED) {1864if(same(oldtree, newtree) || o->reset) {1865if(!newtree)1866returndeleted_entry(current, current, o);1867else1868returnmerged_entry(newtree, current, o);1869}1870returnreject_merge(current, o);1871}else if((!oldtree && !newtree) ||/* 4 and 5 */1872(!oldtree && newtree &&1873same(current, newtree)) ||/* 6 and 7 */1874(oldtree && newtree &&1875same(oldtree, newtree)) ||/* 14 and 15 */1876(oldtree && newtree &&1877!same(oldtree, newtree) &&/* 18 and 19 */1878same(current, newtree))) {1879returnkeep_entry(current, o);1880}else if(oldtree && !newtree &&same(current, oldtree)) {1881/* 10 or 11 */1882returndeleted_entry(oldtree, current, o);1883}else if(oldtree && newtree &&1884same(current, oldtree) && !same(current, newtree)) {1885/* 20 or 21 */1886returnmerged_entry(newtree, current, o);1887}else1888returnreject_merge(current, o);1889}1890else if(newtree) {1891if(oldtree && !o->initial_checkout) {1892/*1893 * deletion of the path was staged;1894 */1895if(same(oldtree, newtree))1896return1;1897returnreject_merge(oldtree, o);1898}1899returnmerged_entry(newtree, current, o);1900}1901returndeleted_entry(oldtree, current, o);1902}19031904/*1905 * Bind merge.1906 *1907 * Keep the index entries at stage0, collapse stage1 but make sure1908 * stage0 does not have anything there.1909 */1910intbind_merge(const struct cache_entry *const*src,1911struct unpack_trees_options *o)1912{1913const struct cache_entry *old = src[0];1914const struct cache_entry *a = src[1];19151916if(o->merge_size !=1)1917returnerror("Cannot do a bind merge of%dtrees",1918 o->merge_size);1919if(a && old)1920return o->gently ? -1:1921error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);1922if(!a)1923returnkeep_entry(old, o);1924else1925returnmerged_entry(a, NULL, o);1926}19271928/*1929 * One-way merge.1930 *1931 * The rule is:1932 * - take the stat information from stage0, take the data from stage11933 */1934intoneway_merge(const struct cache_entry *const*src,1935struct unpack_trees_options *o)1936{1937const struct cache_entry *old = src[0];1938const struct cache_entry *a = src[1];19391940if(o->merge_size !=1)1941returnerror("Cannot do a oneway merge of%dtrees",1942 o->merge_size);19431944if(!a || a == o->df_conflict_entry)1945returndeleted_entry(old, old, o);19461947if(old &&same(old, a)) {1948int update =0;1949if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1950struct stat st;1951if(lstat(old->name, &st) ||1952ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))1953 update |= CE_UPDATE;1954}1955add_entry(o, old, update,0);1956return0;1957}1958returnmerged_entry(a, old, o);1959}