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 55static const char*super_prefixed(const char*path) 56{ 57/* 58 * It is necessary and sufficient to have two static buffers 59 * here, as the return value of this function is fed to 60 * error() using the unpack_*_errors[] templates we see above. 61 */ 62static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 63static int super_prefix_len = -1; 64static unsigned idx =ARRAY_SIZE(buf) -1; 65 66if(super_prefix_len <0) { 67const char*super_prefix =get_super_prefix(); 68if(!super_prefix) { 69 super_prefix_len =0; 70}else{ 71int i; 72for(i =0; i <ARRAY_SIZE(buf); i++) 73strbuf_addstr(&buf[i], super_prefix); 74 super_prefix_len = buf[0].len; 75} 76} 77 78if(!super_prefix_len) 79return path; 80 81if(++idx >=ARRAY_SIZE(buf)) 82 idx =0; 83 84strbuf_setlen(&buf[idx], super_prefix_len); 85strbuf_addstr(&buf[idx], path); 86 87return buf[idx].buf; 88} 89 90voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 91const char*cmd) 92{ 93int i; 94const char**msgs = opts->msgs; 95const char*msg; 96 97if(!strcmp(cmd,"checkout")) 98 msg = advice_commit_before_merge 99?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 100"Please commit your changes or stash them before you switch branches.") 101:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 102else if(!strcmp(cmd,"merge")) 103 msg = advice_commit_before_merge 104?_("Your local changes to the following files would be overwritten by merge:\n%%s" 105"Please commit your changes or stash them before you merge.") 106:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 107else 108 msg = advice_commit_before_merge 109?_("Your local changes to the following files would be overwritten by%s:\n%%s" 110"Please commit your changes or stash them before you%s.") 111:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 112 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 113xstrfmt(msg, cmd, cmd); 114 115 msgs[ERROR_NOT_UPTODATE_DIR] = 116_("Updating the following directories would lose untracked files in them:\n%s"); 117 118if(!strcmp(cmd,"checkout")) 119 msg = advice_commit_before_merge 120?_("The following untracked working tree files would be removed by checkout:\n%%s" 121"Please move or remove them before you switch branches.") 122:_("The following untracked working tree files would be removed by checkout:\n%%s"); 123else if(!strcmp(cmd,"merge")) 124 msg = advice_commit_before_merge 125?_("The following untracked working tree files would be removed by merge:\n%%s" 126"Please move or remove them before you merge.") 127:_("The following untracked working tree files would be removed by merge:\n%%s"); 128else 129 msg = advice_commit_before_merge 130?_("The following untracked working tree files would be removed by%s:\n%%s" 131"Please move or remove them before you%s.") 132:_("The following untracked working tree files would be removed by%s:\n%%s"); 133 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 134 135if(!strcmp(cmd,"checkout")) 136 msg = advice_commit_before_merge 137?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 138"Please move or remove them before you switch branches.") 139:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 140else if(!strcmp(cmd,"merge")) 141 msg = advice_commit_before_merge 142?_("The following untracked working tree files would be overwritten by merge:\n%%s" 143"Please move or remove them before you merge.") 144:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 145else 146 msg = advice_commit_before_merge 147?_("The following untracked working tree files would be overwritten by%s:\n%%s" 148"Please move or remove them before you%s.") 149:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 150 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 151 152/* 153 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 154 * cannot easily display it as a list. 155 */ 156 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 157 158 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 159_("Cannot update sparse checkout: the following entries are not up-to-date:\n%s"); 160 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 161_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 162 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 163_("The following working tree files would be removed by sparse checkout update:\n%s"); 164 165 opts->show_all_errors =1; 166/* rejected paths may not have a static buffer */ 167for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 168 opts->unpack_rejects[i].strdup_strings =1; 169} 170 171static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 172unsigned int set,unsigned int clear) 173{ 174 clear |= CE_HASHED; 175 176if(set & CE_REMOVE) 177 set |= CE_WT_REMOVE; 178 179 ce->ce_flags = (ce->ce_flags & ~clear) | set; 180returnadd_index_entry(&o->result, ce, 181 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 182} 183 184static struct cache_entry *dup_entry(const struct cache_entry *ce) 185{ 186unsigned int size =ce_size(ce); 187struct cache_entry *new=xmalloc(size); 188 189memcpy(new, ce, size); 190return new; 191} 192 193static voidadd_entry(struct unpack_trees_options *o, 194const struct cache_entry *ce, 195unsigned int set,unsigned int clear) 196{ 197do_add_entry(o,dup_entry(ce), set, clear); 198} 199 200/* 201 * add error messages on path <path> 202 * corresponding to the type <e> with the message <msg> 203 * indicating if it should be display in porcelain or not 204 */ 205static intadd_rejected_path(struct unpack_trees_options *o, 206enum unpack_trees_error_types e, 207const char*path) 208{ 209if(!o->show_all_errors) 210returnerror(ERRORMSG(o, e),super_prefixed(path)); 211 212/* 213 * Otherwise, insert in a list for future display by 214 * display_error_msgs() 215 */ 216string_list_append(&o->unpack_rejects[e], path); 217return-1; 218} 219 220/* 221 * display all the error messages stored in a nice way 222 */ 223static voiddisplay_error_msgs(struct unpack_trees_options *o) 224{ 225int e, i; 226int something_displayed =0; 227for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 228struct string_list *rejects = &o->unpack_rejects[e]; 229if(rejects->nr >0) { 230struct strbuf path = STRBUF_INIT; 231 something_displayed =1; 232for(i =0; i < rejects->nr; i++) 233strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 234error(ERRORMSG(o, e),super_prefixed(path.buf)); 235strbuf_release(&path); 236} 237string_list_clear(rejects,0); 238} 239if(something_displayed) 240fprintf(stderr,_("Aborting\n")); 241} 242 243/* 244 * Unlink the last component and schedule the leading directories for 245 * removal, such that empty directories get removed. 246 */ 247static voidunlink_entry(const struct cache_entry *ce) 248{ 249if(!check_leading_path(ce->name,ce_namelen(ce))) 250return; 251if(remove_or_warn(ce->ce_mode, ce->name)) 252return; 253schedule_dir_for_removal(ce->name,ce_namelen(ce)); 254} 255 256static intcheck_updates(struct unpack_trees_options *o, 257const struct checkout *state) 258{ 259unsigned cnt =0, total =0; 260struct progress *progress = NULL; 261struct index_state *index = &o->result; 262int i; 263int errs =0; 264 265if(o->update && o->verbose_update) { 266for(total = cnt =0; cnt < index->cache_nr; cnt++) { 267const struct cache_entry *ce = index->cache[cnt]; 268if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 269 total++; 270} 271 272 progress =start_progress_delay(_("Checking out files"), 273 total,50,1); 274 cnt =0; 275} 276 277if(o->update) 278git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result); 279for(i =0; i < index->cache_nr; i++) { 280const struct cache_entry *ce = index->cache[i]; 281 282if(ce->ce_flags & CE_WT_REMOVE) { 283display_progress(progress, ++cnt); 284if(o->update && !o->dry_run) 285unlink_entry(ce); 286continue; 287} 288} 289remove_marked_cache_entries(&o->result); 290remove_scheduled_dirs(); 291 292for(i =0; i < index->cache_nr; i++) { 293struct cache_entry *ce = index->cache[i]; 294 295if(ce->ce_flags & CE_UPDATE) { 296if(ce->ce_flags & CE_WT_REMOVE) 297die("BUG: both update and delete flags are set on%s", 298 ce->name); 299display_progress(progress, ++cnt); 300 ce->ce_flags &= ~CE_UPDATE; 301if(o->update && !o->dry_run) { 302 errs |=checkout_entry(ce, state, NULL); 303} 304} 305} 306stop_progress(&progress); 307if(o->update) 308git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 309return errs !=0; 310} 311 312static intverify_uptodate_sparse(const struct cache_entry *ce, 313struct unpack_trees_options *o); 314static intverify_absent_sparse(const struct cache_entry *ce, 315enum unpack_trees_error_types, 316struct unpack_trees_options *o); 317 318static intapply_sparse_checkout(struct index_state *istate, 319struct cache_entry *ce, 320struct unpack_trees_options *o) 321{ 322int was_skip_worktree =ce_skip_worktree(ce); 323 324if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 325 ce->ce_flags |= CE_SKIP_WORKTREE; 326else 327 ce->ce_flags &= ~CE_SKIP_WORKTREE; 328if(was_skip_worktree !=ce_skip_worktree(ce)) { 329 ce->ce_flags |= CE_UPDATE_IN_BASE; 330 istate->cache_changed |= CE_ENTRY_CHANGED; 331} 332 333/* 334 * if (!was_skip_worktree && !ce_skip_worktree()) { 335 * This is perfectly normal. Move on; 336 * } 337 */ 338 339/* 340 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 341 * area as a result of ce_skip_worktree() shortcuts in 342 * verify_absent() and verify_uptodate(). 343 * Make sure they don't modify worktree if they are already 344 * outside checkout area 345 */ 346if(was_skip_worktree &&ce_skip_worktree(ce)) { 347 ce->ce_flags &= ~CE_UPDATE; 348 349/* 350 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 351 * on to get that file removed from both index and worktree. 352 * If that file is already outside worktree area, don't 353 * bother remove it. 354 */ 355if(ce->ce_flags & CE_REMOVE) 356 ce->ce_flags &= ~CE_WT_REMOVE; 357} 358 359if(!was_skip_worktree &&ce_skip_worktree(ce)) { 360/* 361 * If CE_UPDATE is set, verify_uptodate() must be called already 362 * also stat info may have lost after merged_entry() so calling 363 * verify_uptodate() again may fail 364 */ 365if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 366return-1; 367 ce->ce_flags |= CE_WT_REMOVE; 368 ce->ce_flags &= ~CE_UPDATE; 369} 370if(was_skip_worktree && !ce_skip_worktree(ce)) { 371if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 372return-1; 373 ce->ce_flags |= CE_UPDATE; 374} 375return0; 376} 377 378staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 379struct unpack_trees_options *o) 380{ 381int ret = o->fn(src, o); 382if(ret >0) 383 ret =0; 384return ret; 385} 386 387static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 388{ 389 ce->ce_flags |= CE_UNPACKED; 390 391if(o->cache_bottom < o->src_index->cache_nr && 392 o->src_index->cache[o->cache_bottom] == ce) { 393int bottom = o->cache_bottom; 394while(bottom < o->src_index->cache_nr && 395 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 396 bottom++; 397 o->cache_bottom = bottom; 398} 399} 400 401static voidmark_all_ce_unused(struct index_state *index) 402{ 403int i; 404for(i =0; i < index->cache_nr; i++) 405 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 406} 407 408static intlocate_in_src_index(const struct cache_entry *ce, 409struct unpack_trees_options *o) 410{ 411struct index_state *index = o->src_index; 412int len =ce_namelen(ce); 413int pos =index_name_pos(index, ce->name, len); 414if(pos <0) 415 pos = -1- pos; 416return pos; 417} 418 419/* 420 * We call unpack_index_entry() with an unmerged cache entry 421 * only in diff-index, and it wants a single callback. Skip 422 * the other unmerged entry with the same name. 423 */ 424static voidmark_ce_used_same_name(struct cache_entry *ce, 425struct unpack_trees_options *o) 426{ 427struct index_state *index = o->src_index; 428int len =ce_namelen(ce); 429int pos; 430 431for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 432struct cache_entry *next = index->cache[pos]; 433if(len !=ce_namelen(next) || 434memcmp(ce->name, next->name, len)) 435break; 436mark_ce_used(next, o); 437} 438} 439 440static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 441{ 442const struct index_state *index = o->src_index; 443int pos = o->cache_bottom; 444 445while(pos < index->cache_nr) { 446struct cache_entry *ce = index->cache[pos]; 447if(!(ce->ce_flags & CE_UNPACKED)) 448return ce; 449 pos++; 450} 451return NULL; 452} 453 454static voidadd_same_unmerged(const struct cache_entry *ce, 455struct unpack_trees_options *o) 456{ 457struct index_state *index = o->src_index; 458int len =ce_namelen(ce); 459int pos =index_name_pos(index, ce->name, len); 460 461if(0<= pos) 462die("programming error in a caller of mark_ce_used_same_name"); 463for(pos = -pos -1; pos < index->cache_nr; pos++) { 464struct cache_entry *next = index->cache[pos]; 465if(len !=ce_namelen(next) || 466memcmp(ce->name, next->name, len)) 467break; 468add_entry(o, next,0,0); 469mark_ce_used(next, o); 470} 471} 472 473static intunpack_index_entry(struct cache_entry *ce, 474struct unpack_trees_options *o) 475{ 476const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 477int ret; 478 479 src[0] = ce; 480 481mark_ce_used(ce, o); 482if(ce_stage(ce)) { 483if(o->skip_unmerged) { 484add_entry(o, ce,0,0); 485return0; 486} 487} 488 ret =call_unpack_fn(src, o); 489if(ce_stage(ce)) 490mark_ce_used_same_name(ce, o); 491return ret; 492} 493 494static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 495 496static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 497{ 498struct unpack_trees_options *o = info->data; 499 500if(o->diff_index_cached) 501return; 502 o->cache_bottom = bottom; 503} 504 505static intswitch_cache_bottom(struct traverse_info *info) 506{ 507struct unpack_trees_options *o = info->data; 508int ret, pos; 509 510if(o->diff_index_cached) 511return0; 512 ret = o->cache_bottom; 513 pos =find_cache_pos(info->prev, &info->name); 514 515if(pos < -1) 516 o->cache_bottom = -2- pos; 517else if(pos <0) 518 o->cache_bottom = o->src_index->cache_nr; 519return ret; 520} 521 522static inttraverse_trees_recursive(int n,unsigned long dirmask, 523unsigned long df_conflicts, 524struct name_entry *names, 525struct traverse_info *info) 526{ 527int i, ret, bottom; 528struct tree_desc t[MAX_UNPACK_TREES]; 529void*buf[MAX_UNPACK_TREES]; 530struct traverse_info newinfo; 531struct name_entry *p; 532 533 p = names; 534while(!p->mode) 535 p++; 536 537 newinfo = *info; 538 newinfo.prev = info; 539 newinfo.pathspec = info->pathspec; 540 newinfo.name = *p; 541 newinfo.pathlen +=tree_entry_len(p) +1; 542 newinfo.df_conflicts |= df_conflicts; 543 544for(i =0; i < n; i++, dirmask >>=1) { 545const unsigned char*sha1 = NULL; 546if(dirmask &1) 547 sha1 = names[i].oid->hash; 548 buf[i] =fill_tree_descriptor(t+i, sha1); 549} 550 551 bottom =switch_cache_bottom(&newinfo); 552 ret =traverse_trees(n, t, &newinfo); 553restore_cache_bottom(&newinfo, bottom); 554 555for(i =0; i < n; i++) 556free(buf[i]); 557 558return ret; 559} 560 561/* 562 * Compare the traverse-path to the cache entry without actually 563 * having to generate the textual representation of the traverse 564 * path. 565 * 566 * NOTE! This *only* compares up to the size of the traverse path 567 * itself - the caller needs to do the final check for the cache 568 * entry having more data at the end! 569 */ 570static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 571{ 572int len, pathlen, ce_len; 573const char*ce_name; 574 575if(info->prev) { 576int cmp =do_compare_entry_piecewise(ce, info->prev, 577&info->name); 578if(cmp) 579return cmp; 580} 581 pathlen = info->pathlen; 582 ce_len =ce_namelen(ce); 583 584/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 585if(ce_len < pathlen) 586return-1; 587 588 ce_len -= pathlen; 589 ce_name = ce->name + pathlen; 590 591 len =tree_entry_len(n); 592returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 593} 594 595static intdo_compare_entry(const struct cache_entry *ce, 596const struct traverse_info *info, 597const struct name_entry *n) 598{ 599int len, pathlen, ce_len; 600const char*ce_name; 601int cmp; 602 603/* 604 * If we have not precomputed the traverse path, it is quicker 605 * to avoid doing so. But if we have precomputed it, 606 * it is quicker to use the precomputed version. 607 */ 608if(!info->traverse_path) 609returndo_compare_entry_piecewise(ce, info, n); 610 611 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 612if(cmp) 613return cmp; 614 615 pathlen = info->pathlen; 616 ce_len =ce_namelen(ce); 617 618if(ce_len < pathlen) 619return-1; 620 621 ce_len -= pathlen; 622 ce_name = ce->name + pathlen; 623 624 len =tree_entry_len(n); 625returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 626} 627 628static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 629{ 630int cmp =do_compare_entry(ce, info, n); 631if(cmp) 632return cmp; 633 634/* 635 * Even if the beginning compared identically, the ce should 636 * compare as bigger than a directory leading up to it! 637 */ 638returnce_namelen(ce) >traverse_path_len(info, n); 639} 640 641static intce_in_traverse_path(const struct cache_entry *ce, 642const struct traverse_info *info) 643{ 644if(!info->prev) 645return1; 646if(do_compare_entry(ce, info->prev, &info->name)) 647return0; 648/* 649 * If ce (blob) is the same name as the path (which is a tree 650 * we will be descending into), it won't be inside it. 651 */ 652return(info->pathlen <ce_namelen(ce)); 653} 654 655static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 656{ 657int len =traverse_path_len(info, n); 658struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 659 660 ce->ce_mode =create_ce_mode(n->mode); 661 ce->ce_flags =create_ce_flags(stage); 662 ce->ce_namelen = len; 663oidcpy(&ce->oid, n->oid); 664make_traverse_path(ce->name, info, n); 665 666return ce; 667} 668 669static intunpack_nondirectories(int n,unsigned long mask, 670unsigned long dirmask, 671struct cache_entry **src, 672const struct name_entry *names, 673const struct traverse_info *info) 674{ 675int i; 676struct unpack_trees_options *o = info->data; 677unsigned long conflicts = info->df_conflicts | dirmask; 678 679/* Do we have *only* directories? Nothing to do */ 680if(mask == dirmask && !src[0]) 681return0; 682 683/* 684 * Ok, we've filled in up to any potential index entry in src[0], 685 * now do the rest. 686 */ 687for(i =0; i < n; i++) { 688int stage; 689unsigned int bit =1ul<< i; 690if(conflicts & bit) { 691 src[i + o->merge] = o->df_conflict_entry; 692continue; 693} 694if(!(mask & bit)) 695continue; 696if(!o->merge) 697 stage =0; 698else if(i +1< o->head_idx) 699 stage =1; 700else if(i +1> o->head_idx) 701 stage =3; 702else 703 stage =2; 704 src[i + o->merge] =create_ce_entry(info, names + i, stage); 705} 706 707if(o->merge) { 708int rc =call_unpack_fn((const struct cache_entry *const*)src, 709 o); 710for(i =0; i < n; i++) { 711struct cache_entry *ce = src[i + o->merge]; 712if(ce != o->df_conflict_entry) 713free(ce); 714} 715return rc; 716} 717 718for(i =0; i < n; i++) 719if(src[i] && src[i] != o->df_conflict_entry) 720if(do_add_entry(o, src[i],0,0)) 721return-1; 722 723return0; 724} 725 726static intunpack_failed(struct unpack_trees_options *o,const char*message) 727{ 728discard_index(&o->result); 729if(!o->gently && !o->exiting_early) { 730if(message) 731returnerror("%s", message); 732return-1; 733} 734return-1; 735} 736 737/* 738 * The tree traversal is looking at name p. If we have a matching entry, 739 * return it. If name p is a directory in the index, do not return 740 * anything, as we will want to match it when the traversal descends into 741 * the directory. 742 */ 743static intfind_cache_pos(struct traverse_info *info, 744const struct name_entry *p) 745{ 746int pos; 747struct unpack_trees_options *o = info->data; 748struct index_state *index = o->src_index; 749int pfxlen = info->pathlen; 750int p_len =tree_entry_len(p); 751 752for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 753const struct cache_entry *ce = index->cache[pos]; 754const char*ce_name, *ce_slash; 755int cmp, ce_len; 756 757if(ce->ce_flags & CE_UNPACKED) { 758/* 759 * cache_bottom entry is already unpacked, so 760 * we can never match it; don't check it 761 * again. 762 */ 763if(pos == o->cache_bottom) 764++o->cache_bottom; 765continue; 766} 767if(!ce_in_traverse_path(ce, info)) { 768/* 769 * Check if we can skip future cache checks 770 * (because we're already past all possible 771 * entries in the traverse path). 772 */ 773if(info->traverse_path) { 774if(strncmp(ce->name, info->traverse_path, 775 info->pathlen) >0) 776break; 777} 778continue; 779} 780 ce_name = ce->name + pfxlen; 781 ce_slash =strchr(ce_name,'/'); 782if(ce_slash) 783 ce_len = ce_slash - ce_name; 784else 785 ce_len =ce_namelen(ce) - pfxlen; 786 cmp =name_compare(p->path, p_len, ce_name, ce_len); 787/* 788 * Exact match; if we have a directory we need to 789 * delay returning it. 790 */ 791if(!cmp) 792return ce_slash ? -2- pos : pos; 793if(0< cmp) 794continue;/* keep looking */ 795/* 796 * ce_name sorts after p->path; could it be that we 797 * have files under p->path directory in the index? 798 * E.g. ce_name == "t-i", and p->path == "t"; we may 799 * have "t/a" in the index. 800 */ 801if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 802 ce_name[p_len] <'/') 803continue;/* keep looking */ 804break; 805} 806return-1; 807} 808 809static struct cache_entry *find_cache_entry(struct traverse_info *info, 810const struct name_entry *p) 811{ 812int pos =find_cache_pos(info, p); 813struct unpack_trees_options *o = info->data; 814 815if(0<= pos) 816return o->src_index->cache[pos]; 817else 818return NULL; 819} 820 821static voiddebug_path(struct traverse_info *info) 822{ 823if(info->prev) { 824debug_path(info->prev); 825if(*info->prev->name.path) 826putchar('/'); 827} 828printf("%s", info->name.path); 829} 830 831static voiddebug_name_entry(int i,struct name_entry *n) 832{ 833printf("ent#%d %06o%s\n", i, 834 n->path ? n->mode :0, 835 n->path ? n->path :"(missing)"); 836} 837 838static voiddebug_unpack_callback(int n, 839unsigned long mask, 840unsigned long dirmask, 841struct name_entry *names, 842struct traverse_info *info) 843{ 844int i; 845printf("* unpack mask%lu, dirmask%lu, cnt%d", 846 mask, dirmask, n); 847debug_path(info); 848putchar('\n'); 849for(i =0; i < n; i++) 850debug_name_entry(i, names + i); 851} 852 853static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 854{ 855struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 856struct unpack_trees_options *o = info->data; 857const struct name_entry *p = names; 858 859/* Find first entry with a real name (we could use "mask" too) */ 860while(!p->mode) 861 p++; 862 863if(o->debug_unpack) 864debug_unpack_callback(n, mask, dirmask, names, info); 865 866/* Are we supposed to look at the index too? */ 867if(o->merge) { 868while(1) { 869int cmp; 870struct cache_entry *ce; 871 872if(o->diff_index_cached) 873 ce =next_cache_entry(o); 874else 875 ce =find_cache_entry(info, p); 876 877if(!ce) 878break; 879 cmp =compare_entry(ce, info, p); 880if(cmp <0) { 881if(unpack_index_entry(ce, o) <0) 882returnunpack_failed(o, NULL); 883continue; 884} 885if(!cmp) { 886if(ce_stage(ce)) { 887/* 888 * If we skip unmerged index 889 * entries, we'll skip this 890 * entry *and* the tree 891 * entries associated with it! 892 */ 893if(o->skip_unmerged) { 894add_same_unmerged(ce, o); 895return mask; 896} 897} 898 src[0] = ce; 899} 900break; 901} 902} 903 904if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 905return-1; 906 907if(o->merge && src[0]) { 908if(ce_stage(src[0])) 909mark_ce_used_same_name(src[0], o); 910else 911mark_ce_used(src[0], o); 912} 913 914/* Now handle any directories.. */ 915if(dirmask) { 916/* special case: "diff-index --cached" looking at a tree */ 917if(o->diff_index_cached && 918 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 919int matches; 920 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 921 names, info); 922/* 923 * Everything under the name matches; skip the 924 * entire hierarchy. diff_index_cached codepath 925 * special cases D/F conflicts in such a way that 926 * it does not do any look-ahead, so this is safe. 927 */ 928if(matches) { 929 o->cache_bottom += matches; 930return mask; 931} 932} 933 934if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 935 names, info) <0) 936return-1; 937return mask; 938} 939 940return mask; 941} 942 943static intclear_ce_flags_1(struct cache_entry **cache,int nr, 944struct strbuf *prefix, 945int select_mask,int clear_mask, 946struct exclude_list *el,int defval); 947 948/* Whole directory matching */ 949static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 950struct strbuf *prefix, 951char*basename, 952int select_mask,int clear_mask, 953struct exclude_list *el,int defval) 954{ 955struct cache_entry **cache_end; 956int dtype = DT_DIR; 957int ret =is_excluded_from_list(prefix->buf, prefix->len, 958 basename, &dtype, el); 959int rc; 960 961strbuf_addch(prefix,'/'); 962 963/* If undecided, use matching result of parent dir in defval */ 964if(ret <0) 965 ret = defval; 966 967for(cache_end = cache; cache_end != cache + nr; cache_end++) { 968struct cache_entry *ce = *cache_end; 969if(strncmp(ce->name, prefix->buf, prefix->len)) 970break; 971} 972 973/* 974 * TODO: check el, if there are no patterns that may conflict 975 * with ret (iow, we know in advance the incl/excl 976 * decision for the entire directory), clear flag here without 977 * calling clear_ce_flags_1(). That function will call 978 * the expensive is_excluded_from_list() on every entry. 979 */ 980 rc =clear_ce_flags_1(cache, cache_end - cache, 981 prefix, 982 select_mask, clear_mask, 983 el, ret); 984strbuf_setlen(prefix, prefix->len -1); 985return rc; 986} 987 988/* 989 * Traverse the index, find every entry that matches according to 990 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the 991 * number of traversed entries. 992 * 993 * If select_mask is non-zero, only entries whose ce_flags has on of 994 * those bits enabled are traversed. 995 * 996 * cache : pointer to an index entry 997 * prefix_len : an offset to its path 998 * 999 * The current path ("prefix") including the trailing '/' is1000 * cache[0]->name[0..(prefix_len-1)]1001 * Top level path has prefix_len zero.1002 */1003static intclear_ce_flags_1(struct cache_entry **cache,int nr,1004struct strbuf *prefix,1005int select_mask,int clear_mask,1006struct exclude_list *el,int defval)1007{1008struct cache_entry **cache_end = cache + nr;10091010/*1011 * Process all entries that have the given prefix and meet1012 * select_mask condition1013 */1014while(cache != cache_end) {1015struct cache_entry *ce = *cache;1016const char*name, *slash;1017int len, dtype, ret;10181019if(select_mask && !(ce->ce_flags & select_mask)) {1020 cache++;1021continue;1022}10231024if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1025break;10261027 name = ce->name + prefix->len;1028 slash =strchr(name,'/');10291030/* If it's a directory, try whole directory match first */1031if(slash) {1032int processed;10331034 len = slash - name;1035strbuf_add(prefix, name, len);10361037 processed =clear_ce_flags_dir(cache, cache_end - cache,1038 prefix,1039 prefix->buf + prefix->len - len,1040 select_mask, clear_mask,1041 el, defval);10421043/* clear_c_f_dir eats a whole dir already? */1044if(processed) {1045 cache += processed;1046strbuf_setlen(prefix, prefix->len - len);1047continue;1048}10491050strbuf_addch(prefix,'/');1051 cache +=clear_ce_flags_1(cache, cache_end - cache,1052 prefix,1053 select_mask, clear_mask, el, defval);1054strbuf_setlen(prefix, prefix->len - len -1);1055continue;1056}10571058/* Non-directory */1059 dtype =ce_to_dtype(ce);1060 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1061 name, &dtype, el);1062if(ret <0)1063 ret = defval;1064if(ret >0)1065 ce->ce_flags &= ~clear_mask;1066 cache++;1067}1068return nr - (cache_end - cache);1069}10701071static intclear_ce_flags(struct cache_entry **cache,int nr,1072int select_mask,int clear_mask,1073struct exclude_list *el)1074{1075static struct strbuf prefix = STRBUF_INIT;10761077strbuf_reset(&prefix);10781079returnclear_ce_flags_1(cache, nr,1080&prefix,1081 select_mask, clear_mask,1082 el,0);1083}10841085/*1086 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1087 */1088static voidmark_new_skip_worktree(struct exclude_list *el,1089struct index_state *the_index,1090int select_flag,int skip_wt_flag)1091{1092int i;10931094/*1095 * 1. Pretend the narrowest worktree: only unmerged entries1096 * are checked out1097 */1098for(i =0; i < the_index->cache_nr; i++) {1099struct cache_entry *ce = the_index->cache[i];11001101if(select_flag && !(ce->ce_flags & select_flag))1102continue;11031104if(!ce_stage(ce))1105 ce->ce_flags |= skip_wt_flag;1106else1107 ce->ce_flags &= ~skip_wt_flag;1108}11091110/*1111 * 2. Widen worktree according to sparse-checkout file.1112 * Matched entries will have skip_wt_flag cleared (i.e. "in")1113 */1114clear_ce_flags(the_index->cache, the_index->cache_nr,1115 select_flag, skip_wt_flag, el);1116}11171118static intverify_absent(const struct cache_entry *,1119enum unpack_trees_error_types,1120struct unpack_trees_options *);1121/*1122 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1123 * resulting index, -2 on failure to reflect the changes to the work tree.1124 *1125 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1126 */1127intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1128{1129int i, ret;1130static struct cache_entry *dfc;1131struct exclude_list el;1132struct checkout state = CHECKOUT_INIT;11331134if(len > MAX_UNPACK_TREES)1135die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);1136 state.force =1;1137 state.quiet =1;1138 state.refresh_cache =1;1139 state.istate = &o->result;11401141memset(&el,0,sizeof(el));1142if(!core_apply_sparse_checkout || !o->update)1143 o->skip_sparse_checkout =1;1144if(!o->skip_sparse_checkout) {1145char*sparse =git_pathdup("info/sparse-checkout");1146if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1147 o->skip_sparse_checkout =1;1148else1149 o->el = ⪙1150free(sparse);1151}11521153memset(&o->result,0,sizeof(o->result));1154 o->result.initialized =1;1155 o->result.timestamp.sec = o->src_index->timestamp.sec;1156 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1157 o->result.version = o->src_index->version;1158 o->result.split_index = o->src_index->split_index;1159if(o->result.split_index)1160 o->result.split_index->refcount++;1161hashcpy(o->result.sha1, o->src_index->sha1);1162 o->merge_size = len;1163mark_all_ce_unused(o->src_index);11641165/*1166 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1167 */1168if(!o->skip_sparse_checkout)1169mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);11701171if(!dfc)1172 dfc =xcalloc(1,cache_entry_size(0));1173 o->df_conflict_entry = dfc;11741175if(len) {1176const char*prefix = o->prefix ? o->prefix :"";1177struct traverse_info info;11781179setup_traverse_info(&info, prefix);1180 info.fn = unpack_callback;1181 info.data = o;1182 info.show_all_errors = o->show_all_errors;1183 info.pathspec = o->pathspec;11841185if(o->prefix) {1186/*1187 * Unpack existing index entries that sort before the1188 * prefix the tree is spliced into. Note that o->merge1189 * is always true in this case.1190 */1191while(1) {1192struct cache_entry *ce =next_cache_entry(o);1193if(!ce)1194break;1195if(ce_in_traverse_path(ce, &info))1196break;1197if(unpack_index_entry(ce, o) <0)1198goto return_failed;1199}1200}12011202if(traverse_trees(len, t, &info) <0)1203goto return_failed;1204}12051206/* Any left-over entries in the index? */1207if(o->merge) {1208while(1) {1209struct cache_entry *ce =next_cache_entry(o);1210if(!ce)1211break;1212if(unpack_index_entry(ce, o) <0)1213goto return_failed;1214}1215}1216mark_all_ce_unused(o->src_index);12171218if(o->trivial_merges_only && o->nontrivial_merge) {1219 ret =unpack_failed(o,"Merge requires file-level merging");1220goto done;1221}12221223if(!o->skip_sparse_checkout) {1224int empty_worktree =1;12251226/*1227 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11228 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1229 * so apply_sparse_checkout() won't attempt to remove it from worktree1230 */1231mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);12321233 ret =0;1234for(i =0; i < o->result.cache_nr; i++) {1235struct cache_entry *ce = o->result.cache[i];12361237/*1238 * Entries marked with CE_ADDED in merged_entry() do not have1239 * verify_absent() check (the check is effectively disabled1240 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1241 *1242 * Do the real check now because we have had1243 * correct CE_NEW_SKIP_WORKTREE1244 */1245if(ce->ce_flags & CE_ADDED &&1246verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1247if(!o->show_all_errors)1248goto return_failed;1249 ret = -1;1250}12511252if(apply_sparse_checkout(&o->result, ce, o)) {1253if(!o->show_all_errors)1254goto return_failed;1255 ret = -1;1256}1257if(!ce_skip_worktree(ce))1258 empty_worktree =0;12591260}1261if(ret <0)1262goto return_failed;1263/*1264 * Sparse checkout is meant to narrow down checkout area1265 * but it does not make sense to narrow down to empty working1266 * tree. This is usually a mistake in sparse checkout rules.1267 * Do not allow users to do that.1268 */1269if(o->result.cache_nr && empty_worktree) {1270 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1271goto done;1272}1273}12741275 o->src_index = NULL;1276 ret =check_updates(o, &state) ? (-2) :0;1277if(o->dst_index) {1278if(!ret) {1279if(!o->result.cache_tree)1280 o->result.cache_tree =cache_tree();1281if(!cache_tree_fully_valid(o->result.cache_tree))1282cache_tree_update(&o->result,1283 WRITE_TREE_SILENT |1284 WRITE_TREE_REPAIR);1285}1286discard_index(o->dst_index);1287*o->dst_index = o->result;1288}else{1289discard_index(&o->result);1290}12911292done:1293clear_exclude_list(&el);1294return ret;12951296return_failed:1297if(o->show_all_errors)1298display_error_msgs(o);1299mark_all_ce_unused(o->src_index);1300 ret =unpack_failed(o, NULL);1301if(o->exiting_early)1302 ret =0;1303goto done;1304}13051306/* Here come the merge functions */13071308static intreject_merge(const struct cache_entry *ce,1309struct unpack_trees_options *o)1310{1311return o->gently ? -1:1312add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1313}13141315static intsame(const struct cache_entry *a,const struct cache_entry *b)1316{1317if(!!a != !!b)1318return0;1319if(!a && !b)1320return1;1321if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1322return0;1323return a->ce_mode == b->ce_mode &&1324!oidcmp(&a->oid, &b->oid);1325}132613271328/*1329 * When a CE gets turned into an unmerged entry, we1330 * want it to be up-to-date1331 */1332static intverify_uptodate_1(const struct cache_entry *ce,1333struct unpack_trees_options *o,1334enum unpack_trees_error_types error_type)1335{1336struct stat st;13371338if(o->index_only)1339return0;13401341/*1342 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1343 * if this entry is truly up-to-date because this file may be1344 * overwritten.1345 */1346if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1347;/* keep checking */1348else if(o->reset ||ce_uptodate(ce))1349return0;13501351if(!lstat(ce->name, &st)) {1352int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1353unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1354if(!changed)1355return0;1356/*1357 * NEEDSWORK: the current default policy is to allow1358 * submodule to be out of sync wrt the superproject1359 * index. This needs to be tightened later for1360 * submodules that are marked to be automatically1361 * checked out.1362 */1363if(S_ISGITLINK(ce->ce_mode))1364return0;1365 errno =0;1366}1367if(errno == ENOENT)1368return0;1369return o->gently ? -1:1370add_rejected_path(o, error_type, ce->name);1371}13721373static intverify_uptodate(const struct cache_entry *ce,1374struct unpack_trees_options *o)1375{1376if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1377return0;1378returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1379}13801381static intverify_uptodate_sparse(const struct cache_entry *ce,1382struct unpack_trees_options *o)1383{1384returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1385}13861387static voidinvalidate_ce_path(const struct cache_entry *ce,1388struct unpack_trees_options *o)1389{1390if(!ce)1391return;1392cache_tree_invalidate_path(o->src_index, ce->name);1393untracked_cache_invalidate_path(o->src_index, ce->name);1394}13951396/*1397 * Check that checking out ce->sha1 in subdir ce->name is not1398 * going to overwrite any working files.1399 *1400 * Currently, git does not checkout subprojects during a superproject1401 * checkout, so it is not going to overwrite anything.1402 */1403static intverify_clean_submodule(const struct cache_entry *ce,1404enum unpack_trees_error_types error_type,1405struct unpack_trees_options *o)1406{1407return0;1408}14091410static intverify_clean_subdirectory(const struct cache_entry *ce,1411enum unpack_trees_error_types error_type,1412struct unpack_trees_options *o)1413{1414/*1415 * we are about to extract "ce->name"; we would not want to lose1416 * anything in the existing directory there.1417 */1418int namelen;1419int i;1420struct dir_struct d;1421char*pathbuf;1422int cnt =0;1423unsigned char sha1[20];14241425if(S_ISGITLINK(ce->ce_mode) &&1426resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1427/* If we are not going to update the submodule, then1428 * we don't care.1429 */1430if(!hashcmp(sha1, ce->oid.hash))1431return0;1432returnverify_clean_submodule(ce, error_type, o);1433}14341435/*1436 * First let's make sure we do not have a local modification1437 * in that directory.1438 */1439 namelen =ce_namelen(ce);1440for(i =locate_in_src_index(ce, o);1441 i < o->src_index->cache_nr;1442 i++) {1443struct cache_entry *ce2 = o->src_index->cache[i];1444int len =ce_namelen(ce2);1445if(len < namelen ||1446strncmp(ce->name, ce2->name, namelen) ||1447 ce2->name[namelen] !='/')1448break;1449/*1450 * ce2->name is an entry in the subdirectory to be1451 * removed.1452 */1453if(!ce_stage(ce2)) {1454if(verify_uptodate(ce2, o))1455return-1;1456add_entry(o, ce2, CE_REMOVE,0);1457mark_ce_used(ce2, o);1458}1459 cnt++;1460}14611462/*1463 * Then we need to make sure that we do not lose a locally1464 * present file that is not ignored.1465 */1466 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14671468memset(&d,0,sizeof(d));1469if(o->dir)1470 d.exclude_per_dir = o->dir->exclude_per_dir;1471 i =read_directory(&d, pathbuf, namelen+1, NULL);1472if(i)1473return o->gently ? -1:1474add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1475free(pathbuf);1476return cnt;1477}14781479/*1480 * This gets called when there was no index entry for the tree entry 'dst',1481 * but we found a file in the working tree that 'lstat()' said was fine,1482 * and we're on a case-insensitive filesystem.1483 *1484 * See if we can find a case-insensitive match in the index that also1485 * matches the stat information, and assume it's that other file!1486 */1487static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1488{1489const struct cache_entry *src;14901491 src =index_file_exists(o->src_index, name, len,1);1492return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1493}14941495static intcheck_ok_to_remove(const char*name,int len,int dtype,1496const struct cache_entry *ce,struct stat *st,1497enum unpack_trees_error_types error_type,1498struct unpack_trees_options *o)1499{1500const struct cache_entry *result;15011502/*1503 * It may be that the 'lstat()' succeeded even though1504 * target 'ce' was absent, because there is an old1505 * entry that is different only in case..1506 *1507 * Ignore that lstat() if it matches.1508 */1509if(ignore_case &&icase_exists(o, name, len, st))1510return0;15111512if(o->dir &&1513is_excluded(o->dir, name, &dtype))1514/*1515 * ce->name is explicitly excluded, so it is Ok to1516 * overwrite it.1517 */1518return0;1519if(S_ISDIR(st->st_mode)) {1520/*1521 * We are checking out path "foo" and1522 * found "foo/." in the working tree.1523 * This is tricky -- if we have modified1524 * files that are in "foo/" we would lose1525 * them.1526 */1527if(verify_clean_subdirectory(ce, error_type, o) <0)1528return-1;1529return0;1530}15311532/*1533 * The previous round may already have decided to1534 * delete this path, which is in a subdirectory that1535 * is being replaced with a blob.1536 */1537 result =index_file_exists(&o->result, name, len,0);1538if(result) {1539if(result->ce_flags & CE_REMOVE)1540return0;1541}15421543return o->gently ? -1:1544add_rejected_path(o, error_type, name);1545}15461547/*1548 * We do not want to remove or overwrite a working tree file that1549 * is not tracked, unless it is ignored.1550 */1551static intverify_absent_1(const struct cache_entry *ce,1552enum unpack_trees_error_types error_type,1553struct unpack_trees_options *o)1554{1555int len;1556struct stat st;15571558if(o->index_only || o->reset || !o->update)1559return0;15601561 len =check_leading_path(ce->name,ce_namelen(ce));1562if(!len)1563return0;1564else if(len >0) {1565char*path;1566int ret;15671568 path =xmemdupz(ce->name, len);1569if(lstat(path, &st))1570 ret =error_errno("cannot stat '%s'", path);1571else1572 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1573&st, error_type, o);1574free(path);1575return ret;1576}else if(lstat(ce->name, &st)) {1577if(errno != ENOENT)1578returnerror_errno("cannot stat '%s'", ce->name);1579return0;1580}else{1581returncheck_ok_to_remove(ce->name,ce_namelen(ce),1582ce_to_dtype(ce), ce, &st,1583 error_type, o);1584}1585}15861587static intverify_absent(const struct cache_entry *ce,1588enum unpack_trees_error_types error_type,1589struct unpack_trees_options *o)1590{1591if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1592return0;1593returnverify_absent_1(ce, error_type, o);1594}15951596static intverify_absent_sparse(const struct cache_entry *ce,1597enum unpack_trees_error_types error_type,1598struct unpack_trees_options *o)1599{1600enum unpack_trees_error_types orphaned_error = error_type;1601if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1602 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;16031604returnverify_absent_1(ce, orphaned_error, o);1605}16061607static intmerged_entry(const struct cache_entry *ce,1608const struct cache_entry *old,1609struct unpack_trees_options *o)1610{1611int update = CE_UPDATE;1612struct cache_entry *merge =dup_entry(ce);16131614if(!old) {1615/*1616 * New index entries. In sparse checkout, the following1617 * verify_absent() will be delayed until after1618 * traverse_trees() finishes in unpack_trees(), then:1619 *1620 * - CE_NEW_SKIP_WORKTREE will be computed correctly1621 * - verify_absent() be called again, this time with1622 * correct CE_NEW_SKIP_WORKTREE1623 *1624 * verify_absent() call here does nothing in sparse1625 * checkout (i.e. o->skip_sparse_checkout == 0)1626 */1627 update |= CE_ADDED;1628 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;16291630if(verify_absent(merge,1631 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1632free(merge);1633return-1;1634}1635invalidate_ce_path(merge, o);1636}else if(!(old->ce_flags & CE_CONFLICTED)) {1637/*1638 * See if we can re-use the old CE directly?1639 * That way we get the uptodate stat info.1640 *1641 * This also removes the UPDATE flag on a match; otherwise1642 * we will end up overwriting local changes in the work tree.1643 */1644if(same(old, merge)) {1645copy_cache_entry(merge, old);1646 update =0;1647}else{1648if(verify_uptodate(old, o)) {1649free(merge);1650return-1;1651}1652/* Migrate old flags over */1653 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1654invalidate_ce_path(old, o);1655}1656}else{1657/*1658 * Previously unmerged entry left as an existence1659 * marker by read_index_unmerged();1660 */1661invalidate_ce_path(old, o);1662}16631664do_add_entry(o, merge, update, CE_STAGEMASK);1665return1;1666}16671668static intdeleted_entry(const struct cache_entry *ce,1669const struct cache_entry *old,1670struct unpack_trees_options *o)1671{1672/* Did it exist in the index? */1673if(!old) {1674if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1675return-1;1676return0;1677}1678if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1679return-1;1680add_entry(o, ce, CE_REMOVE,0);1681invalidate_ce_path(ce, o);1682return1;1683}16841685static intkeep_entry(const struct cache_entry *ce,1686struct unpack_trees_options *o)1687{1688add_entry(o, ce,0,0);1689return1;1690}16911692#if DBRT_DEBUG1693static voidshow_stage_entry(FILE*o,1694const char*label,const struct cache_entry *ce)1695{1696if(!ce)1697fprintf(o,"%s(missing)\n", label);1698else1699fprintf(o,"%s%06o%s %d\t%s\n",1700 label,1701 ce->ce_mode,1702oid_to_hex(&ce->oid),1703ce_stage(ce),1704 ce->name);1705}1706#endif17071708intthreeway_merge(const struct cache_entry *const*stages,1709struct unpack_trees_options *o)1710{1711const struct cache_entry *index;1712const struct cache_entry *head;1713const struct cache_entry *remote = stages[o->head_idx +1];1714int count;1715int head_match =0;1716int remote_match =0;17171718int df_conflict_head =0;1719int df_conflict_remote =0;17201721int any_anc_missing =0;1722int no_anc_exists =1;1723int i;17241725for(i =1; i < o->head_idx; i++) {1726if(!stages[i] || stages[i] == o->df_conflict_entry)1727 any_anc_missing =1;1728else1729 no_anc_exists =0;1730}17311732 index = stages[0];1733 head = stages[o->head_idx];17341735if(head == o->df_conflict_entry) {1736 df_conflict_head =1;1737 head = NULL;1738}17391740if(remote == o->df_conflict_entry) {1741 df_conflict_remote =1;1742 remote = NULL;1743}17441745/*1746 * First, if there's a #16 situation, note that to prevent #131747 * and #14.1748 */1749if(!same(remote, head)) {1750for(i =1; i < o->head_idx; i++) {1751if(same(stages[i], head)) {1752 head_match = i;1753}1754if(same(stages[i], remote)) {1755 remote_match = i;1756}1757}1758}17591760/*1761 * We start with cases where the index is allowed to match1762 * something other than the head: #14(ALT) and #2ALT, where it1763 * is permitted to match the result instead.1764 */1765/* #14, #14ALT, #2ALT */1766if(remote && !df_conflict_head && head_match && !remote_match) {1767if(index && !same(index, remote) && !same(index, head))1768returnreject_merge(index, o);1769returnmerged_entry(remote, index, o);1770}1771/*1772 * If we have an entry in the index cache, then we want to1773 * make sure that it matches head.1774 */1775if(index && !same(index, head))1776returnreject_merge(index, o);17771778if(head) {1779/* #5ALT, #15 */1780if(same(head, remote))1781returnmerged_entry(head, index, o);1782/* #13, #3ALT */1783if(!df_conflict_remote && remote_match && !head_match)1784returnmerged_entry(head, index, o);1785}17861787/* #1 */1788if(!head && !remote && any_anc_missing)1789return0;17901791/*1792 * Under the "aggressive" rule, we resolve mostly trivial1793 * cases that we historically had git-merge-one-file resolve.1794 */1795if(o->aggressive) {1796int head_deleted = !head;1797int remote_deleted = !remote;1798const struct cache_entry *ce = NULL;17991800if(index)1801 ce = index;1802else if(head)1803 ce = head;1804else if(remote)1805 ce = remote;1806else{1807for(i =1; i < o->head_idx; i++) {1808if(stages[i] && stages[i] != o->df_conflict_entry) {1809 ce = stages[i];1810break;1811}1812}1813}18141815/*1816 * Deleted in both.1817 * Deleted in one and unchanged in the other.1818 */1819if((head_deleted && remote_deleted) ||1820(head_deleted && remote && remote_match) ||1821(remote_deleted && head && head_match)) {1822if(index)1823returndeleted_entry(index, index, o);1824if(ce && !head_deleted) {1825if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1826return-1;1827}1828return0;1829}1830/*1831 * Added in both, identically.1832 */1833if(no_anc_exists && head && remote &&same(head, remote))1834returnmerged_entry(head, index, o);18351836}18371838/* Below are "no merge" cases, which require that the index be1839 * up-to-date to avoid the files getting overwritten with1840 * conflict resolution files.1841 */1842if(index) {1843if(verify_uptodate(index, o))1844return-1;1845}18461847 o->nontrivial_merge =1;18481849/* #2, #3, #4, #6, #7, #9, #10, #11. */1850 count =0;1851if(!head_match || !remote_match) {1852for(i =1; i < o->head_idx; i++) {1853if(stages[i] && stages[i] != o->df_conflict_entry) {1854keep_entry(stages[i], o);1855 count++;1856break;1857}1858}1859}1860#if DBRT_DEBUG1861else{1862fprintf(stderr,"read-tree: warning #16 detected\n");1863show_stage_entry(stderr,"head ", stages[head_match]);1864show_stage_entry(stderr,"remote ", stages[remote_match]);1865}1866#endif1867if(head) { count +=keep_entry(head, o); }1868if(remote) { count +=keep_entry(remote, o); }1869return count;1870}18711872/*1873 * Two-way merge.1874 *1875 * The rule is to "carry forward" what is in the index without losing1876 * information across a "fast-forward", favoring a successful merge1877 * over a merge failure when it makes sense. For details of the1878 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1879 *1880 */1881inttwoway_merge(const struct cache_entry *const*src,1882struct unpack_trees_options *o)1883{1884const struct cache_entry *current = src[0];1885const struct cache_entry *oldtree = src[1];1886const struct cache_entry *newtree = src[2];18871888if(o->merge_size !=2)1889returnerror("Cannot do a twoway merge of%dtrees",1890 o->merge_size);18911892if(oldtree == o->df_conflict_entry)1893 oldtree = NULL;1894if(newtree == o->df_conflict_entry)1895 newtree = NULL;18961897if(current) {1898if(current->ce_flags & CE_CONFLICTED) {1899if(same(oldtree, newtree) || o->reset) {1900if(!newtree)1901returndeleted_entry(current, current, o);1902else1903returnmerged_entry(newtree, current, o);1904}1905returnreject_merge(current, o);1906}else if((!oldtree && !newtree) ||/* 4 and 5 */1907(!oldtree && newtree &&1908same(current, newtree)) ||/* 6 and 7 */1909(oldtree && newtree &&1910same(oldtree, newtree)) ||/* 14 and 15 */1911(oldtree && newtree &&1912!same(oldtree, newtree) &&/* 18 and 19 */1913same(current, newtree))) {1914returnkeep_entry(current, o);1915}else if(oldtree && !newtree &&same(current, oldtree)) {1916/* 10 or 11 */1917returndeleted_entry(oldtree, current, o);1918}else if(oldtree && newtree &&1919same(current, oldtree) && !same(current, newtree)) {1920/* 20 or 21 */1921returnmerged_entry(newtree, current, o);1922}else1923returnreject_merge(current, o);1924}1925else if(newtree) {1926if(oldtree && !o->initial_checkout) {1927/*1928 * deletion of the path was staged;1929 */1930if(same(oldtree, newtree))1931return1;1932returnreject_merge(oldtree, o);1933}1934returnmerged_entry(newtree, current, o);1935}1936returndeleted_entry(oldtree, current, o);1937}19381939/*1940 * Bind merge.1941 *1942 * Keep the index entries at stage0, collapse stage1 but make sure1943 * stage0 does not have anything there.1944 */1945intbind_merge(const struct cache_entry *const*src,1946struct unpack_trees_options *o)1947{1948const struct cache_entry *old = src[0];1949const struct cache_entry *a = src[1];19501951if(o->merge_size !=1)1952returnerror("Cannot do a bind merge of%dtrees",1953 o->merge_size);1954if(a && old)1955return o->gently ? -1:1956error(ERRORMSG(o, ERROR_BIND_OVERLAP),1957super_prefixed(a->name),1958super_prefixed(old->name));1959if(!a)1960returnkeep_entry(old, o);1961else1962returnmerged_entry(a, NULL, o);1963}19641965/*1966 * One-way merge.1967 *1968 * The rule is:1969 * - take the stat information from stage0, take the data from stage11970 */1971intoneway_merge(const struct cache_entry *const*src,1972struct unpack_trees_options *o)1973{1974const struct cache_entry *old = src[0];1975const struct cache_entry *a = src[1];19761977if(o->merge_size !=1)1978returnerror("Cannot do a oneway merge of%dtrees",1979 o->merge_size);19801981if(!a || a == o->df_conflict_entry)1982returndeleted_entry(old, old, o);19831984if(old &&same(old, a)) {1985int update =0;1986if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1987struct stat st;1988if(lstat(old->name, &st) ||1989ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))1990 update |= CE_UPDATE;1991}1992add_entry(o, old, update,0);1993return0;1994}1995returnmerged_entry(a, old, o);1996}