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 struct progress *get_progress(struct unpack_trees_options *o) 257{ 258unsigned cnt =0, total =0; 259struct index_state *index = &o->result; 260 261if(!o->update || !o->verbose_update) 262return NULL; 263 264for(; cnt < index->cache_nr; cnt++) { 265const struct cache_entry *ce = index->cache[cnt]; 266if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 267 total++; 268} 269 270returnstart_progress_delay(_("Checking out files"), 271 total,50,1); 272} 273 274static intcheck_updates(struct unpack_trees_options *o) 275{ 276unsigned cnt =0; 277int errs =0; 278struct progress *progress = NULL; 279struct index_state *index = &o->result; 280struct checkout state = CHECKOUT_INIT; 281int i; 282 283 state.force =1; 284 state.quiet =1; 285 state.refresh_cache =1; 286 state.istate = index; 287 288 progress =get_progress(o); 289 290if(o->update) 291git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 292for(i =0; i < index->cache_nr; i++) { 293const struct cache_entry *ce = index->cache[i]; 294 295if(ce->ce_flags & CE_WT_REMOVE) { 296display_progress(progress, ++cnt); 297if(o->update && !o->dry_run) 298unlink_entry(ce); 299} 300} 301remove_marked_cache_entries(index); 302remove_scheduled_dirs(); 303 304for(i =0; i < index->cache_nr; i++) { 305struct cache_entry *ce = index->cache[i]; 306 307if(ce->ce_flags & CE_UPDATE) { 308if(ce->ce_flags & CE_WT_REMOVE) 309die("BUG: both update and delete flags are set on%s", 310 ce->name); 311display_progress(progress, ++cnt); 312 ce->ce_flags &= ~CE_UPDATE; 313if(o->update && !o->dry_run) { 314 errs |=checkout_entry(ce, &state, NULL); 315} 316} 317} 318stop_progress(&progress); 319if(o->update) 320git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 321return errs !=0; 322} 323 324static intverify_uptodate_sparse(const struct cache_entry *ce, 325struct unpack_trees_options *o); 326static intverify_absent_sparse(const struct cache_entry *ce, 327enum unpack_trees_error_types, 328struct unpack_trees_options *o); 329 330static intapply_sparse_checkout(struct index_state *istate, 331struct cache_entry *ce, 332struct unpack_trees_options *o) 333{ 334int was_skip_worktree =ce_skip_worktree(ce); 335 336if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 337 ce->ce_flags |= CE_SKIP_WORKTREE; 338else 339 ce->ce_flags &= ~CE_SKIP_WORKTREE; 340if(was_skip_worktree !=ce_skip_worktree(ce)) { 341 ce->ce_flags |= CE_UPDATE_IN_BASE; 342 istate->cache_changed |= CE_ENTRY_CHANGED; 343} 344 345/* 346 * if (!was_skip_worktree && !ce_skip_worktree()) { 347 * This is perfectly normal. Move on; 348 * } 349 */ 350 351/* 352 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 353 * area as a result of ce_skip_worktree() shortcuts in 354 * verify_absent() and verify_uptodate(). 355 * Make sure they don't modify worktree if they are already 356 * outside checkout area 357 */ 358if(was_skip_worktree &&ce_skip_worktree(ce)) { 359 ce->ce_flags &= ~CE_UPDATE; 360 361/* 362 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 363 * on to get that file removed from both index and worktree. 364 * If that file is already outside worktree area, don't 365 * bother remove it. 366 */ 367if(ce->ce_flags & CE_REMOVE) 368 ce->ce_flags &= ~CE_WT_REMOVE; 369} 370 371if(!was_skip_worktree &&ce_skip_worktree(ce)) { 372/* 373 * If CE_UPDATE is set, verify_uptodate() must be called already 374 * also stat info may have lost after merged_entry() so calling 375 * verify_uptodate() again may fail 376 */ 377if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 378return-1; 379 ce->ce_flags |= CE_WT_REMOVE; 380 ce->ce_flags &= ~CE_UPDATE; 381} 382if(was_skip_worktree && !ce_skip_worktree(ce)) { 383if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 384return-1; 385 ce->ce_flags |= CE_UPDATE; 386} 387return0; 388} 389 390staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 391struct unpack_trees_options *o) 392{ 393int ret = o->fn(src, o); 394if(ret >0) 395 ret =0; 396return ret; 397} 398 399static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 400{ 401 ce->ce_flags |= CE_UNPACKED; 402 403if(o->cache_bottom < o->src_index->cache_nr && 404 o->src_index->cache[o->cache_bottom] == ce) { 405int bottom = o->cache_bottom; 406while(bottom < o->src_index->cache_nr && 407 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 408 bottom++; 409 o->cache_bottom = bottom; 410} 411} 412 413static voidmark_all_ce_unused(struct index_state *index) 414{ 415int i; 416for(i =0; i < index->cache_nr; i++) 417 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 418} 419 420static intlocate_in_src_index(const struct cache_entry *ce, 421struct unpack_trees_options *o) 422{ 423struct index_state *index = o->src_index; 424int len =ce_namelen(ce); 425int pos =index_name_pos(index, ce->name, len); 426if(pos <0) 427 pos = -1- pos; 428return pos; 429} 430 431/* 432 * We call unpack_index_entry() with an unmerged cache entry 433 * only in diff-index, and it wants a single callback. Skip 434 * the other unmerged entry with the same name. 435 */ 436static voidmark_ce_used_same_name(struct cache_entry *ce, 437struct unpack_trees_options *o) 438{ 439struct index_state *index = o->src_index; 440int len =ce_namelen(ce); 441int pos; 442 443for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 444struct cache_entry *next = index->cache[pos]; 445if(len !=ce_namelen(next) || 446memcmp(ce->name, next->name, len)) 447break; 448mark_ce_used(next, o); 449} 450} 451 452static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 453{ 454const struct index_state *index = o->src_index; 455int pos = o->cache_bottom; 456 457while(pos < index->cache_nr) { 458struct cache_entry *ce = index->cache[pos]; 459if(!(ce->ce_flags & CE_UNPACKED)) 460return ce; 461 pos++; 462} 463return NULL; 464} 465 466static voidadd_same_unmerged(const struct cache_entry *ce, 467struct unpack_trees_options *o) 468{ 469struct index_state *index = o->src_index; 470int len =ce_namelen(ce); 471int pos =index_name_pos(index, ce->name, len); 472 473if(0<= pos) 474die("programming error in a caller of mark_ce_used_same_name"); 475for(pos = -pos -1; pos < index->cache_nr; pos++) { 476struct cache_entry *next = index->cache[pos]; 477if(len !=ce_namelen(next) || 478memcmp(ce->name, next->name, len)) 479break; 480add_entry(o, next,0,0); 481mark_ce_used(next, o); 482} 483} 484 485static intunpack_index_entry(struct cache_entry *ce, 486struct unpack_trees_options *o) 487{ 488const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 489int ret; 490 491 src[0] = ce; 492 493mark_ce_used(ce, o); 494if(ce_stage(ce)) { 495if(o->skip_unmerged) { 496add_entry(o, ce,0,0); 497return0; 498} 499} 500 ret =call_unpack_fn(src, o); 501if(ce_stage(ce)) 502mark_ce_used_same_name(ce, o); 503return ret; 504} 505 506static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 507 508static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 509{ 510struct unpack_trees_options *o = info->data; 511 512if(o->diff_index_cached) 513return; 514 o->cache_bottom = bottom; 515} 516 517static intswitch_cache_bottom(struct traverse_info *info) 518{ 519struct unpack_trees_options *o = info->data; 520int ret, pos; 521 522if(o->diff_index_cached) 523return0; 524 ret = o->cache_bottom; 525 pos =find_cache_pos(info->prev, &info->name); 526 527if(pos < -1) 528 o->cache_bottom = -2- pos; 529else if(pos <0) 530 o->cache_bottom = o->src_index->cache_nr; 531return ret; 532} 533 534staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 535{ 536return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 537} 538 539static inttraverse_trees_recursive(int n,unsigned long dirmask, 540unsigned long df_conflicts, 541struct name_entry *names, 542struct traverse_info *info) 543{ 544int i, ret, bottom; 545int nr_buf =0; 546struct tree_desc t[MAX_UNPACK_TREES]; 547void*buf[MAX_UNPACK_TREES]; 548struct traverse_info newinfo; 549struct name_entry *p; 550 551 p = names; 552while(!p->mode) 553 p++; 554 555 newinfo = *info; 556 newinfo.prev = info; 557 newinfo.pathspec = info->pathspec; 558 newinfo.name = *p; 559 newinfo.pathlen +=tree_entry_len(p) +1; 560 newinfo.df_conflicts |= df_conflicts; 561 562/* 563 * Fetch the tree from the ODB for each peer directory in the 564 * n commits. 565 * 566 * For 2- and 3-way traversals, we try to avoid hitting the 567 * ODB twice for the same OID. This should yield a nice speed 568 * up in checkouts and merges when the commits are similar. 569 * 570 * We don't bother doing the full O(n^2) search for larger n, 571 * because wider traversals don't happen that often and we 572 * avoid the search setup. 573 * 574 * When 2 peer OIDs are the same, we just copy the tree 575 * descriptor data. This implicitly borrows the buffer 576 * data from the earlier cell. 577 */ 578for(i =0; i < n; i++, dirmask >>=1) { 579if(i >0&&are_same_oid(&names[i], &names[i -1])) 580 t[i] = t[i -1]; 581else if(i >1&&are_same_oid(&names[i], &names[i -2])) 582 t[i] = t[i -2]; 583else{ 584const unsigned char*sha1 = NULL; 585if(dirmask &1) 586 sha1 = names[i].oid->hash; 587 buf[nr_buf++] =fill_tree_descriptor(t+i, sha1); 588} 589} 590 591 bottom =switch_cache_bottom(&newinfo); 592 ret =traverse_trees(n, t, &newinfo); 593restore_cache_bottom(&newinfo, bottom); 594 595for(i =0; i < nr_buf; i++) 596free(buf[i]); 597 598return ret; 599} 600 601/* 602 * Compare the traverse-path to the cache entry without actually 603 * having to generate the textual representation of the traverse 604 * path. 605 * 606 * NOTE! This *only* compares up to the size of the traverse path 607 * itself - the caller needs to do the final check for the cache 608 * entry having more data at the end! 609 */ 610static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 611{ 612int len, pathlen, ce_len; 613const char*ce_name; 614 615if(info->prev) { 616int cmp =do_compare_entry_piecewise(ce, info->prev, 617&info->name); 618if(cmp) 619return cmp; 620} 621 pathlen = info->pathlen; 622 ce_len =ce_namelen(ce); 623 624/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 625if(ce_len < pathlen) 626return-1; 627 628 ce_len -= pathlen; 629 ce_name = ce->name + pathlen; 630 631 len =tree_entry_len(n); 632returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 633} 634 635static intdo_compare_entry(const struct cache_entry *ce, 636const struct traverse_info *info, 637const struct name_entry *n) 638{ 639int len, pathlen, ce_len; 640const char*ce_name; 641int cmp; 642 643/* 644 * If we have not precomputed the traverse path, it is quicker 645 * to avoid doing so. But if we have precomputed it, 646 * it is quicker to use the precomputed version. 647 */ 648if(!info->traverse_path) 649returndo_compare_entry_piecewise(ce, info, n); 650 651 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 652if(cmp) 653return cmp; 654 655 pathlen = info->pathlen; 656 ce_len =ce_namelen(ce); 657 658if(ce_len < pathlen) 659return-1; 660 661 ce_len -= pathlen; 662 ce_name = ce->name + pathlen; 663 664 len =tree_entry_len(n); 665returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 666} 667 668static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 669{ 670int cmp =do_compare_entry(ce, info, n); 671if(cmp) 672return cmp; 673 674/* 675 * Even if the beginning compared identically, the ce should 676 * compare as bigger than a directory leading up to it! 677 */ 678returnce_namelen(ce) >traverse_path_len(info, n); 679} 680 681static intce_in_traverse_path(const struct cache_entry *ce, 682const struct traverse_info *info) 683{ 684if(!info->prev) 685return1; 686if(do_compare_entry(ce, info->prev, &info->name)) 687return0; 688/* 689 * If ce (blob) is the same name as the path (which is a tree 690 * we will be descending into), it won't be inside it. 691 */ 692return(info->pathlen <ce_namelen(ce)); 693} 694 695static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 696{ 697int len =traverse_path_len(info, n); 698struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 699 700 ce->ce_mode =create_ce_mode(n->mode); 701 ce->ce_flags =create_ce_flags(stage); 702 ce->ce_namelen = len; 703oidcpy(&ce->oid, n->oid); 704make_traverse_path(ce->name, info, n); 705 706return ce; 707} 708 709static intunpack_nondirectories(int n,unsigned long mask, 710unsigned long dirmask, 711struct cache_entry **src, 712const struct name_entry *names, 713const struct traverse_info *info) 714{ 715int i; 716struct unpack_trees_options *o = info->data; 717unsigned long conflicts = info->df_conflicts | dirmask; 718 719/* Do we have *only* directories? Nothing to do */ 720if(mask == dirmask && !src[0]) 721return0; 722 723/* 724 * Ok, we've filled in up to any potential index entry in src[0], 725 * now do the rest. 726 */ 727for(i =0; i < n; i++) { 728int stage; 729unsigned int bit =1ul<< i; 730if(conflicts & bit) { 731 src[i + o->merge] = o->df_conflict_entry; 732continue; 733} 734if(!(mask & bit)) 735continue; 736if(!o->merge) 737 stage =0; 738else if(i +1< o->head_idx) 739 stage =1; 740else if(i +1> o->head_idx) 741 stage =3; 742else 743 stage =2; 744 src[i + o->merge] =create_ce_entry(info, names + i, stage); 745} 746 747if(o->merge) { 748int rc =call_unpack_fn((const struct cache_entry *const*)src, 749 o); 750for(i =0; i < n; i++) { 751struct cache_entry *ce = src[i + o->merge]; 752if(ce != o->df_conflict_entry) 753free(ce); 754} 755return rc; 756} 757 758for(i =0; i < n; i++) 759if(src[i] && src[i] != o->df_conflict_entry) 760if(do_add_entry(o, src[i],0,0)) 761return-1; 762 763return0; 764} 765 766static intunpack_failed(struct unpack_trees_options *o,const char*message) 767{ 768discard_index(&o->result); 769if(!o->gently && !o->exiting_early) { 770if(message) 771returnerror("%s", message); 772return-1; 773} 774return-1; 775} 776 777/* 778 * The tree traversal is looking at name p. If we have a matching entry, 779 * return it. If name p is a directory in the index, do not return 780 * anything, as we will want to match it when the traversal descends into 781 * the directory. 782 */ 783static intfind_cache_pos(struct traverse_info *info, 784const struct name_entry *p) 785{ 786int pos; 787struct unpack_trees_options *o = info->data; 788struct index_state *index = o->src_index; 789int pfxlen = info->pathlen; 790int p_len =tree_entry_len(p); 791 792for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 793const struct cache_entry *ce = index->cache[pos]; 794const char*ce_name, *ce_slash; 795int cmp, ce_len; 796 797if(ce->ce_flags & CE_UNPACKED) { 798/* 799 * cache_bottom entry is already unpacked, so 800 * we can never match it; don't check it 801 * again. 802 */ 803if(pos == o->cache_bottom) 804++o->cache_bottom; 805continue; 806} 807if(!ce_in_traverse_path(ce, info)) { 808/* 809 * Check if we can skip future cache checks 810 * (because we're already past all possible 811 * entries in the traverse path). 812 */ 813if(info->traverse_path) { 814if(strncmp(ce->name, info->traverse_path, 815 info->pathlen) >0) 816break; 817} 818continue; 819} 820 ce_name = ce->name + pfxlen; 821 ce_slash =strchr(ce_name,'/'); 822if(ce_slash) 823 ce_len = ce_slash - ce_name; 824else 825 ce_len =ce_namelen(ce) - pfxlen; 826 cmp =name_compare(p->path, p_len, ce_name, ce_len); 827/* 828 * Exact match; if we have a directory we need to 829 * delay returning it. 830 */ 831if(!cmp) 832return ce_slash ? -2- pos : pos; 833if(0< cmp) 834continue;/* keep looking */ 835/* 836 * ce_name sorts after p->path; could it be that we 837 * have files under p->path directory in the index? 838 * E.g. ce_name == "t-i", and p->path == "t"; we may 839 * have "t/a" in the index. 840 */ 841if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 842 ce_name[p_len] <'/') 843continue;/* keep looking */ 844break; 845} 846return-1; 847} 848 849static struct cache_entry *find_cache_entry(struct traverse_info *info, 850const struct name_entry *p) 851{ 852int pos =find_cache_pos(info, p); 853struct unpack_trees_options *o = info->data; 854 855if(0<= pos) 856return o->src_index->cache[pos]; 857else 858return NULL; 859} 860 861static voiddebug_path(struct traverse_info *info) 862{ 863if(info->prev) { 864debug_path(info->prev); 865if(*info->prev->name.path) 866putchar('/'); 867} 868printf("%s", info->name.path); 869} 870 871static voiddebug_name_entry(int i,struct name_entry *n) 872{ 873printf("ent#%d %06o%s\n", i, 874 n->path ? n->mode :0, 875 n->path ? n->path :"(missing)"); 876} 877 878static voiddebug_unpack_callback(int n, 879unsigned long mask, 880unsigned long dirmask, 881struct name_entry *names, 882struct traverse_info *info) 883{ 884int i; 885printf("* unpack mask%lu, dirmask%lu, cnt%d", 886 mask, dirmask, n); 887debug_path(info); 888putchar('\n'); 889for(i =0; i < n; i++) 890debug_name_entry(i, names + i); 891} 892 893static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 894{ 895struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 896struct unpack_trees_options *o = info->data; 897const struct name_entry *p = names; 898 899/* Find first entry with a real name (we could use "mask" too) */ 900while(!p->mode) 901 p++; 902 903if(o->debug_unpack) 904debug_unpack_callback(n, mask, dirmask, names, info); 905 906/* Are we supposed to look at the index too? */ 907if(o->merge) { 908while(1) { 909int cmp; 910struct cache_entry *ce; 911 912if(o->diff_index_cached) 913 ce =next_cache_entry(o); 914else 915 ce =find_cache_entry(info, p); 916 917if(!ce) 918break; 919 cmp =compare_entry(ce, info, p); 920if(cmp <0) { 921if(unpack_index_entry(ce, o) <0) 922returnunpack_failed(o, NULL); 923continue; 924} 925if(!cmp) { 926if(ce_stage(ce)) { 927/* 928 * If we skip unmerged index 929 * entries, we'll skip this 930 * entry *and* the tree 931 * entries associated with it! 932 */ 933if(o->skip_unmerged) { 934add_same_unmerged(ce, o); 935return mask; 936} 937} 938 src[0] = ce; 939} 940break; 941} 942} 943 944if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 945return-1; 946 947if(o->merge && src[0]) { 948if(ce_stage(src[0])) 949mark_ce_used_same_name(src[0], o); 950else 951mark_ce_used(src[0], o); 952} 953 954/* Now handle any directories.. */ 955if(dirmask) { 956/* special case: "diff-index --cached" looking at a tree */ 957if(o->diff_index_cached && 958 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 959int matches; 960 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 961 names, info); 962/* 963 * Everything under the name matches; skip the 964 * entire hierarchy. diff_index_cached codepath 965 * special cases D/F conflicts in such a way that 966 * it does not do any look-ahead, so this is safe. 967 */ 968if(matches) { 969 o->cache_bottom += matches; 970return mask; 971} 972} 973 974if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 975 names, info) <0) 976return-1; 977return mask; 978} 979 980return mask; 981} 982 983static intclear_ce_flags_1(struct cache_entry **cache,int nr, 984struct strbuf *prefix, 985int select_mask,int clear_mask, 986struct exclude_list *el,int defval); 987 988/* Whole directory matching */ 989static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 990struct strbuf *prefix, 991char*basename, 992int select_mask,int clear_mask, 993struct exclude_list *el,int defval) 994{ 995struct cache_entry **cache_end; 996int dtype = DT_DIR; 997int ret =is_excluded_from_list(prefix->buf, prefix->len, 998 basename, &dtype, el); 999int rc;10001001strbuf_addch(prefix,'/');10021003/* If undecided, use matching result of parent dir in defval */1004if(ret <0)1005 ret = defval;10061007for(cache_end = cache; cache_end != cache + nr; cache_end++) {1008struct cache_entry *ce = *cache_end;1009if(strncmp(ce->name, prefix->buf, prefix->len))1010break;1011}10121013/*1014 * TODO: check el, if there are no patterns that may conflict1015 * with ret (iow, we know in advance the incl/excl1016 * decision for the entire directory), clear flag here without1017 * calling clear_ce_flags_1(). That function will call1018 * the expensive is_excluded_from_list() on every entry.1019 */1020 rc =clear_ce_flags_1(cache, cache_end - cache,1021 prefix,1022 select_mask, clear_mask,1023 el, ret);1024strbuf_setlen(prefix, prefix->len -1);1025return rc;1026}10271028/*1029 * Traverse the index, find every entry that matches according to1030 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1031 * number of traversed entries.1032 *1033 * If select_mask is non-zero, only entries whose ce_flags has on of1034 * those bits enabled are traversed.1035 *1036 * cache : pointer to an index entry1037 * prefix_len : an offset to its path1038 *1039 * The current path ("prefix") including the trailing '/' is1040 * cache[0]->name[0..(prefix_len-1)]1041 * Top level path has prefix_len zero.1042 */1043static intclear_ce_flags_1(struct cache_entry **cache,int nr,1044struct strbuf *prefix,1045int select_mask,int clear_mask,1046struct exclude_list *el,int defval)1047{1048struct cache_entry **cache_end = cache + nr;10491050/*1051 * Process all entries that have the given prefix and meet1052 * select_mask condition1053 */1054while(cache != cache_end) {1055struct cache_entry *ce = *cache;1056const char*name, *slash;1057int len, dtype, ret;10581059if(select_mask && !(ce->ce_flags & select_mask)) {1060 cache++;1061continue;1062}10631064if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1065break;10661067 name = ce->name + prefix->len;1068 slash =strchr(name,'/');10691070/* If it's a directory, try whole directory match first */1071if(slash) {1072int processed;10731074 len = slash - name;1075strbuf_add(prefix, name, len);10761077 processed =clear_ce_flags_dir(cache, cache_end - cache,1078 prefix,1079 prefix->buf + prefix->len - len,1080 select_mask, clear_mask,1081 el, defval);10821083/* clear_c_f_dir eats a whole dir already? */1084if(processed) {1085 cache += processed;1086strbuf_setlen(prefix, prefix->len - len);1087continue;1088}10891090strbuf_addch(prefix,'/');1091 cache +=clear_ce_flags_1(cache, cache_end - cache,1092 prefix,1093 select_mask, clear_mask, el, defval);1094strbuf_setlen(prefix, prefix->len - len -1);1095continue;1096}10971098/* Non-directory */1099 dtype =ce_to_dtype(ce);1100 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1101 name, &dtype, el);1102if(ret <0)1103 ret = defval;1104if(ret >0)1105 ce->ce_flags &= ~clear_mask;1106 cache++;1107}1108return nr - (cache_end - cache);1109}11101111static intclear_ce_flags(struct cache_entry **cache,int nr,1112int select_mask,int clear_mask,1113struct exclude_list *el)1114{1115static struct strbuf prefix = STRBUF_INIT;11161117strbuf_reset(&prefix);11181119returnclear_ce_flags_1(cache, nr,1120&prefix,1121 select_mask, clear_mask,1122 el,0);1123}11241125/*1126 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1127 */1128static voidmark_new_skip_worktree(struct exclude_list *el,1129struct index_state *the_index,1130int select_flag,int skip_wt_flag)1131{1132int i;11331134/*1135 * 1. Pretend the narrowest worktree: only unmerged entries1136 * are checked out1137 */1138for(i =0; i < the_index->cache_nr; i++) {1139struct cache_entry *ce = the_index->cache[i];11401141if(select_flag && !(ce->ce_flags & select_flag))1142continue;11431144if(!ce_stage(ce))1145 ce->ce_flags |= skip_wt_flag;1146else1147 ce->ce_flags &= ~skip_wt_flag;1148}11491150/*1151 * 2. Widen worktree according to sparse-checkout file.1152 * Matched entries will have skip_wt_flag cleared (i.e. "in")1153 */1154clear_ce_flags(the_index->cache, the_index->cache_nr,1155 select_flag, skip_wt_flag, el);1156}11571158static intverify_absent(const struct cache_entry *,1159enum unpack_trees_error_types,1160struct unpack_trees_options *);1161/*1162 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1163 * resulting index, -2 on failure to reflect the changes to the work tree.1164 *1165 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1166 */1167intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1168{1169int i, ret;1170static struct cache_entry *dfc;1171struct exclude_list el;11721173if(len > MAX_UNPACK_TREES)1174die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);11751176memset(&el,0,sizeof(el));1177if(!core_apply_sparse_checkout || !o->update)1178 o->skip_sparse_checkout =1;1179if(!o->skip_sparse_checkout) {1180char*sparse =git_pathdup("info/sparse-checkout");1181if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1182 o->skip_sparse_checkout =1;1183else1184 o->el = ⪙1185free(sparse);1186}11871188memset(&o->result,0,sizeof(o->result));1189 o->result.initialized =1;1190 o->result.timestamp.sec = o->src_index->timestamp.sec;1191 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1192 o->result.version = o->src_index->version;1193 o->result.split_index = o->src_index->split_index;1194if(o->result.split_index)1195 o->result.split_index->refcount++;1196hashcpy(o->result.sha1, o->src_index->sha1);1197 o->merge_size = len;1198mark_all_ce_unused(o->src_index);11991200/*1201 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1202 */1203if(!o->skip_sparse_checkout)1204mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);12051206if(!dfc)1207 dfc =xcalloc(1,cache_entry_size(0));1208 o->df_conflict_entry = dfc;12091210if(len) {1211const char*prefix = o->prefix ? o->prefix :"";1212struct traverse_info info;12131214setup_traverse_info(&info, prefix);1215 info.fn = unpack_callback;1216 info.data = o;1217 info.show_all_errors = o->show_all_errors;1218 info.pathspec = o->pathspec;12191220if(o->prefix) {1221/*1222 * Unpack existing index entries that sort before the1223 * prefix the tree is spliced into. Note that o->merge1224 * is always true in this case.1225 */1226while(1) {1227struct cache_entry *ce =next_cache_entry(o);1228if(!ce)1229break;1230if(ce_in_traverse_path(ce, &info))1231break;1232if(unpack_index_entry(ce, o) <0)1233goto return_failed;1234}1235}12361237if(traverse_trees(len, t, &info) <0)1238goto return_failed;1239}12401241/* Any left-over entries in the index? */1242if(o->merge) {1243while(1) {1244struct cache_entry *ce =next_cache_entry(o);1245if(!ce)1246break;1247if(unpack_index_entry(ce, o) <0)1248goto return_failed;1249}1250}1251mark_all_ce_unused(o->src_index);12521253if(o->trivial_merges_only && o->nontrivial_merge) {1254 ret =unpack_failed(o,"Merge requires file-level merging");1255goto done;1256}12571258if(!o->skip_sparse_checkout) {1259int empty_worktree =1;12601261/*1262 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11263 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1264 * so apply_sparse_checkout() won't attempt to remove it from worktree1265 */1266mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);12671268 ret =0;1269for(i =0; i < o->result.cache_nr; i++) {1270struct cache_entry *ce = o->result.cache[i];12711272/*1273 * Entries marked with CE_ADDED in merged_entry() do not have1274 * verify_absent() check (the check is effectively disabled1275 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1276 *1277 * Do the real check now because we have had1278 * correct CE_NEW_SKIP_WORKTREE1279 */1280if(ce->ce_flags & CE_ADDED &&1281verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1282if(!o->show_all_errors)1283goto return_failed;1284 ret = -1;1285}12861287if(apply_sparse_checkout(&o->result, ce, o)) {1288if(!o->show_all_errors)1289goto return_failed;1290 ret = -1;1291}1292if(!ce_skip_worktree(ce))1293 empty_worktree =0;12941295}1296if(ret <0)1297goto return_failed;1298/*1299 * Sparse checkout is meant to narrow down checkout area1300 * but it does not make sense to narrow down to empty working1301 * tree. This is usually a mistake in sparse checkout rules.1302 * Do not allow users to do that.1303 */1304if(o->result.cache_nr && empty_worktree) {1305 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1306goto done;1307}1308}13091310 o->src_index = NULL;1311 ret =check_updates(o) ? (-2) :0;1312if(o->dst_index) {1313if(!ret) {1314if(!o->result.cache_tree)1315 o->result.cache_tree =cache_tree();1316if(!cache_tree_fully_valid(o->result.cache_tree))1317cache_tree_update(&o->result,1318 WRITE_TREE_SILENT |1319 WRITE_TREE_REPAIR);1320}1321discard_index(o->dst_index);1322*o->dst_index = o->result;1323}else{1324discard_index(&o->result);1325}13261327done:1328clear_exclude_list(&el);1329return ret;13301331return_failed:1332if(o->show_all_errors)1333display_error_msgs(o);1334mark_all_ce_unused(o->src_index);1335 ret =unpack_failed(o, NULL);1336if(o->exiting_early)1337 ret =0;1338goto done;1339}13401341/* Here come the merge functions */13421343static intreject_merge(const struct cache_entry *ce,1344struct unpack_trees_options *o)1345{1346return o->gently ? -1:1347add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1348}13491350static intsame(const struct cache_entry *a,const struct cache_entry *b)1351{1352if(!!a != !!b)1353return0;1354if(!a && !b)1355return1;1356if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1357return0;1358return a->ce_mode == b->ce_mode &&1359!oidcmp(&a->oid, &b->oid);1360}136113621363/*1364 * When a CE gets turned into an unmerged entry, we1365 * want it to be up-to-date1366 */1367static intverify_uptodate_1(const struct cache_entry *ce,1368struct unpack_trees_options *o,1369enum unpack_trees_error_types error_type)1370{1371struct stat st;13721373if(o->index_only)1374return0;13751376/*1377 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1378 * if this entry is truly up-to-date because this file may be1379 * overwritten.1380 */1381if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1382;/* keep checking */1383else if(o->reset ||ce_uptodate(ce))1384return0;13851386if(!lstat(ce->name, &st)) {1387int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1388unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1389if(!changed)1390return0;1391/*1392 * NEEDSWORK: the current default policy is to allow1393 * submodule to be out of sync wrt the superproject1394 * index. This needs to be tightened later for1395 * submodules that are marked to be automatically1396 * checked out.1397 */1398if(S_ISGITLINK(ce->ce_mode))1399return0;1400 errno =0;1401}1402if(errno == ENOENT)1403return0;1404return o->gently ? -1:1405add_rejected_path(o, error_type, ce->name);1406}14071408static intverify_uptodate(const struct cache_entry *ce,1409struct unpack_trees_options *o)1410{1411if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1412return0;1413returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1414}14151416static intverify_uptodate_sparse(const struct cache_entry *ce,1417struct unpack_trees_options *o)1418{1419returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1420}14211422static voidinvalidate_ce_path(const struct cache_entry *ce,1423struct unpack_trees_options *o)1424{1425if(!ce)1426return;1427cache_tree_invalidate_path(o->src_index, ce->name);1428untracked_cache_invalidate_path(o->src_index, ce->name);1429}14301431/*1432 * Check that checking out ce->sha1 in subdir ce->name is not1433 * going to overwrite any working files.1434 *1435 * Currently, git does not checkout subprojects during a superproject1436 * checkout, so it is not going to overwrite anything.1437 */1438static intverify_clean_submodule(const struct cache_entry *ce,1439enum unpack_trees_error_types error_type,1440struct unpack_trees_options *o)1441{1442return0;1443}14441445static intverify_clean_subdirectory(const struct cache_entry *ce,1446enum unpack_trees_error_types error_type,1447struct unpack_trees_options *o)1448{1449/*1450 * we are about to extract "ce->name"; we would not want to lose1451 * anything in the existing directory there.1452 */1453int namelen;1454int i;1455struct dir_struct d;1456char*pathbuf;1457int cnt =0;1458unsigned char sha1[20];14591460if(S_ISGITLINK(ce->ce_mode) &&1461resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1462/* If we are not going to update the submodule, then1463 * we don't care.1464 */1465if(!hashcmp(sha1, ce->oid.hash))1466return0;1467returnverify_clean_submodule(ce, error_type, o);1468}14691470/*1471 * First let's make sure we do not have a local modification1472 * in that directory.1473 */1474 namelen =ce_namelen(ce);1475for(i =locate_in_src_index(ce, o);1476 i < o->src_index->cache_nr;1477 i++) {1478struct cache_entry *ce2 = o->src_index->cache[i];1479int len =ce_namelen(ce2);1480if(len < namelen ||1481strncmp(ce->name, ce2->name, namelen) ||1482 ce2->name[namelen] !='/')1483break;1484/*1485 * ce2->name is an entry in the subdirectory to be1486 * removed.1487 */1488if(!ce_stage(ce2)) {1489if(verify_uptodate(ce2, o))1490return-1;1491add_entry(o, ce2, CE_REMOVE,0);1492mark_ce_used(ce2, o);1493}1494 cnt++;1495}14961497/*1498 * Then we need to make sure that we do not lose a locally1499 * present file that is not ignored.1500 */1501 pathbuf =xstrfmt("%.*s/", namelen, ce->name);15021503memset(&d,0,sizeof(d));1504if(o->dir)1505 d.exclude_per_dir = o->dir->exclude_per_dir;1506 i =read_directory(&d, pathbuf, namelen+1, NULL);1507if(i)1508return o->gently ? -1:1509add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1510free(pathbuf);1511return cnt;1512}15131514/*1515 * This gets called when there was no index entry for the tree entry 'dst',1516 * but we found a file in the working tree that 'lstat()' said was fine,1517 * and we're on a case-insensitive filesystem.1518 *1519 * See if we can find a case-insensitive match in the index that also1520 * matches the stat information, and assume it's that other file!1521 */1522static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1523{1524const struct cache_entry *src;15251526 src =index_file_exists(o->src_index, name, len,1);1527return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1528}15291530static intcheck_ok_to_remove(const char*name,int len,int dtype,1531const struct cache_entry *ce,struct stat *st,1532enum unpack_trees_error_types error_type,1533struct unpack_trees_options *o)1534{1535const struct cache_entry *result;15361537/*1538 * It may be that the 'lstat()' succeeded even though1539 * target 'ce' was absent, because there is an old1540 * entry that is different only in case..1541 *1542 * Ignore that lstat() if it matches.1543 */1544if(ignore_case &&icase_exists(o, name, len, st))1545return0;15461547if(o->dir &&1548is_excluded(o->dir, name, &dtype))1549/*1550 * ce->name is explicitly excluded, so it is Ok to1551 * overwrite it.1552 */1553return0;1554if(S_ISDIR(st->st_mode)) {1555/*1556 * We are checking out path "foo" and1557 * found "foo/." in the working tree.1558 * This is tricky -- if we have modified1559 * files that are in "foo/" we would lose1560 * them.1561 */1562if(verify_clean_subdirectory(ce, error_type, o) <0)1563return-1;1564return0;1565}15661567/*1568 * The previous round may already have decided to1569 * delete this path, which is in a subdirectory that1570 * is being replaced with a blob.1571 */1572 result =index_file_exists(&o->result, name, len,0);1573if(result) {1574if(result->ce_flags & CE_REMOVE)1575return0;1576}15771578return o->gently ? -1:1579add_rejected_path(o, error_type, name);1580}15811582/*1583 * We do not want to remove or overwrite a working tree file that1584 * is not tracked, unless it is ignored.1585 */1586static intverify_absent_1(const struct cache_entry *ce,1587enum unpack_trees_error_types error_type,1588struct unpack_trees_options *o)1589{1590int len;1591struct stat st;15921593if(o->index_only || o->reset || !o->update)1594return0;15951596 len =check_leading_path(ce->name,ce_namelen(ce));1597if(!len)1598return0;1599else if(len >0) {1600char*path;1601int ret;16021603 path =xmemdupz(ce->name, len);1604if(lstat(path, &st))1605 ret =error_errno("cannot stat '%s'", path);1606else1607 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1608&st, error_type, o);1609free(path);1610return ret;1611}else if(lstat(ce->name, &st)) {1612if(errno != ENOENT)1613returnerror_errno("cannot stat '%s'", ce->name);1614return0;1615}else{1616returncheck_ok_to_remove(ce->name,ce_namelen(ce),1617ce_to_dtype(ce), ce, &st,1618 error_type, o);1619}1620}16211622static intverify_absent(const struct cache_entry *ce,1623enum unpack_trees_error_types error_type,1624struct unpack_trees_options *o)1625{1626if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1627return0;1628returnverify_absent_1(ce, error_type, o);1629}16301631static intverify_absent_sparse(const struct cache_entry *ce,1632enum unpack_trees_error_types error_type,1633struct unpack_trees_options *o)1634{1635enum unpack_trees_error_types orphaned_error = error_type;1636if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1637 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;16381639returnverify_absent_1(ce, orphaned_error, o);1640}16411642static intmerged_entry(const struct cache_entry *ce,1643const struct cache_entry *old,1644struct unpack_trees_options *o)1645{1646int update = CE_UPDATE;1647struct cache_entry *merge =dup_entry(ce);16481649if(!old) {1650/*1651 * New index entries. In sparse checkout, the following1652 * verify_absent() will be delayed until after1653 * traverse_trees() finishes in unpack_trees(), then:1654 *1655 * - CE_NEW_SKIP_WORKTREE will be computed correctly1656 * - verify_absent() be called again, this time with1657 * correct CE_NEW_SKIP_WORKTREE1658 *1659 * verify_absent() call here does nothing in sparse1660 * checkout (i.e. o->skip_sparse_checkout == 0)1661 */1662 update |= CE_ADDED;1663 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;16641665if(verify_absent(merge,1666 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1667free(merge);1668return-1;1669}1670invalidate_ce_path(merge, o);1671}else if(!(old->ce_flags & CE_CONFLICTED)) {1672/*1673 * See if we can re-use the old CE directly?1674 * That way we get the uptodate stat info.1675 *1676 * This also removes the UPDATE flag on a match; otherwise1677 * we will end up overwriting local changes in the work tree.1678 */1679if(same(old, merge)) {1680copy_cache_entry(merge, old);1681 update =0;1682}else{1683if(verify_uptodate(old, o)) {1684free(merge);1685return-1;1686}1687/* Migrate old flags over */1688 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1689invalidate_ce_path(old, o);1690}1691}else{1692/*1693 * Previously unmerged entry left as an existence1694 * marker by read_index_unmerged();1695 */1696invalidate_ce_path(old, o);1697}16981699do_add_entry(o, merge, update, CE_STAGEMASK);1700return1;1701}17021703static intdeleted_entry(const struct cache_entry *ce,1704const struct cache_entry *old,1705struct unpack_trees_options *o)1706{1707/* Did it exist in the index? */1708if(!old) {1709if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1710return-1;1711return0;1712}1713if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1714return-1;1715add_entry(o, ce, CE_REMOVE,0);1716invalidate_ce_path(ce, o);1717return1;1718}17191720static intkeep_entry(const struct cache_entry *ce,1721struct unpack_trees_options *o)1722{1723add_entry(o, ce,0,0);1724return1;1725}17261727#if DBRT_DEBUG1728static voidshow_stage_entry(FILE*o,1729const char*label,const struct cache_entry *ce)1730{1731if(!ce)1732fprintf(o,"%s(missing)\n", label);1733else1734fprintf(o,"%s%06o%s %d\t%s\n",1735 label,1736 ce->ce_mode,1737oid_to_hex(&ce->oid),1738ce_stage(ce),1739 ce->name);1740}1741#endif17421743intthreeway_merge(const struct cache_entry *const*stages,1744struct unpack_trees_options *o)1745{1746const struct cache_entry *index;1747const struct cache_entry *head;1748const struct cache_entry *remote = stages[o->head_idx +1];1749int count;1750int head_match =0;1751int remote_match =0;17521753int df_conflict_head =0;1754int df_conflict_remote =0;17551756int any_anc_missing =0;1757int no_anc_exists =1;1758int i;17591760for(i =1; i < o->head_idx; i++) {1761if(!stages[i] || stages[i] == o->df_conflict_entry)1762 any_anc_missing =1;1763else1764 no_anc_exists =0;1765}17661767 index = stages[0];1768 head = stages[o->head_idx];17691770if(head == o->df_conflict_entry) {1771 df_conflict_head =1;1772 head = NULL;1773}17741775if(remote == o->df_conflict_entry) {1776 df_conflict_remote =1;1777 remote = NULL;1778}17791780/*1781 * First, if there's a #16 situation, note that to prevent #131782 * and #14.1783 */1784if(!same(remote, head)) {1785for(i =1; i < o->head_idx; i++) {1786if(same(stages[i], head)) {1787 head_match = i;1788}1789if(same(stages[i], remote)) {1790 remote_match = i;1791}1792}1793}17941795/*1796 * We start with cases where the index is allowed to match1797 * something other than the head: #14(ALT) and #2ALT, where it1798 * is permitted to match the result instead.1799 */1800/* #14, #14ALT, #2ALT */1801if(remote && !df_conflict_head && head_match && !remote_match) {1802if(index && !same(index, remote) && !same(index, head))1803returnreject_merge(index, o);1804returnmerged_entry(remote, index, o);1805}1806/*1807 * If we have an entry in the index cache, then we want to1808 * make sure that it matches head.1809 */1810if(index && !same(index, head))1811returnreject_merge(index, o);18121813if(head) {1814/* #5ALT, #15 */1815if(same(head, remote))1816returnmerged_entry(head, index, o);1817/* #13, #3ALT */1818if(!df_conflict_remote && remote_match && !head_match)1819returnmerged_entry(head, index, o);1820}18211822/* #1 */1823if(!head && !remote && any_anc_missing)1824return0;18251826/*1827 * Under the "aggressive" rule, we resolve mostly trivial1828 * cases that we historically had git-merge-one-file resolve.1829 */1830if(o->aggressive) {1831int head_deleted = !head;1832int remote_deleted = !remote;1833const struct cache_entry *ce = NULL;18341835if(index)1836 ce = index;1837else if(head)1838 ce = head;1839else if(remote)1840 ce = remote;1841else{1842for(i =1; i < o->head_idx; i++) {1843if(stages[i] && stages[i] != o->df_conflict_entry) {1844 ce = stages[i];1845break;1846}1847}1848}18491850/*1851 * Deleted in both.1852 * Deleted in one and unchanged in the other.1853 */1854if((head_deleted && remote_deleted) ||1855(head_deleted && remote && remote_match) ||1856(remote_deleted && head && head_match)) {1857if(index)1858returndeleted_entry(index, index, o);1859if(ce && !head_deleted) {1860if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1861return-1;1862}1863return0;1864}1865/*1866 * Added in both, identically.1867 */1868if(no_anc_exists && head && remote &&same(head, remote))1869returnmerged_entry(head, index, o);18701871}18721873/* Below are "no merge" cases, which require that the index be1874 * up-to-date to avoid the files getting overwritten with1875 * conflict resolution files.1876 */1877if(index) {1878if(verify_uptodate(index, o))1879return-1;1880}18811882 o->nontrivial_merge =1;18831884/* #2, #3, #4, #6, #7, #9, #10, #11. */1885 count =0;1886if(!head_match || !remote_match) {1887for(i =1; i < o->head_idx; i++) {1888if(stages[i] && stages[i] != o->df_conflict_entry) {1889keep_entry(stages[i], o);1890 count++;1891break;1892}1893}1894}1895#if DBRT_DEBUG1896else{1897fprintf(stderr,"read-tree: warning #16 detected\n");1898show_stage_entry(stderr,"head ", stages[head_match]);1899show_stage_entry(stderr,"remote ", stages[remote_match]);1900}1901#endif1902if(head) { count +=keep_entry(head, o); }1903if(remote) { count +=keep_entry(remote, o); }1904return count;1905}19061907/*1908 * Two-way merge.1909 *1910 * The rule is to "carry forward" what is in the index without losing1911 * information across a "fast-forward", favoring a successful merge1912 * over a merge failure when it makes sense. For details of the1913 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1914 *1915 */1916inttwoway_merge(const struct cache_entry *const*src,1917struct unpack_trees_options *o)1918{1919const struct cache_entry *current = src[0];1920const struct cache_entry *oldtree = src[1];1921const struct cache_entry *newtree = src[2];19221923if(o->merge_size !=2)1924returnerror("Cannot do a twoway merge of%dtrees",1925 o->merge_size);19261927if(oldtree == o->df_conflict_entry)1928 oldtree = NULL;1929if(newtree == o->df_conflict_entry)1930 newtree = NULL;19311932if(current) {1933if(current->ce_flags & CE_CONFLICTED) {1934if(same(oldtree, newtree) || o->reset) {1935if(!newtree)1936returndeleted_entry(current, current, o);1937else1938returnmerged_entry(newtree, current, o);1939}1940returnreject_merge(current, o);1941}else if((!oldtree && !newtree) ||/* 4 and 5 */1942(!oldtree && newtree &&1943same(current, newtree)) ||/* 6 and 7 */1944(oldtree && newtree &&1945same(oldtree, newtree)) ||/* 14 and 15 */1946(oldtree && newtree &&1947!same(oldtree, newtree) &&/* 18 and 19 */1948same(current, newtree))) {1949returnkeep_entry(current, o);1950}else if(oldtree && !newtree &&same(current, oldtree)) {1951/* 10 or 11 */1952returndeleted_entry(oldtree, current, o);1953}else if(oldtree && newtree &&1954same(current, oldtree) && !same(current, newtree)) {1955/* 20 or 21 */1956returnmerged_entry(newtree, current, o);1957}else1958returnreject_merge(current, o);1959}1960else if(newtree) {1961if(oldtree && !o->initial_checkout) {1962/*1963 * deletion of the path was staged;1964 */1965if(same(oldtree, newtree))1966return1;1967returnreject_merge(oldtree, o);1968}1969returnmerged_entry(newtree, current, o);1970}1971returndeleted_entry(oldtree, current, o);1972}19731974/*1975 * Bind merge.1976 *1977 * Keep the index entries at stage0, collapse stage1 but make sure1978 * stage0 does not have anything there.1979 */1980intbind_merge(const struct cache_entry *const*src,1981struct unpack_trees_options *o)1982{1983const struct cache_entry *old = src[0];1984const struct cache_entry *a = src[1];19851986if(o->merge_size !=1)1987returnerror("Cannot do a bind merge of%dtrees",1988 o->merge_size);1989if(a && old)1990return o->gently ? -1:1991error(ERRORMSG(o, ERROR_BIND_OVERLAP),1992super_prefixed(a->name),1993super_prefixed(old->name));1994if(!a)1995returnkeep_entry(old, o);1996else1997returnmerged_entry(a, NULL, o);1998}19992000/*2001 * One-way merge.2002 *2003 * The rule is:2004 * - take the stat information from stage0, take the data from stage12005 */2006intoneway_merge(const struct cache_entry *const*src,2007struct unpack_trees_options *o)2008{2009const struct cache_entry *old = src[0];2010const struct cache_entry *a = src[1];20112012if(o->merge_size !=1)2013returnerror("Cannot do a oneway merge of%dtrees",2014 o->merge_size);20152016if(!a || a == o->df_conflict_entry)2017returndeleted_entry(old, old, o);20182019if(old &&same(old, a)) {2020int update =0;2021if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2022struct stat st;2023if(lstat(old->name, &st) ||2024ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2025 update |= CE_UPDATE;2026}2027add_entry(o, old, update,0);2028return0;2029}2030returnmerged_entry(a, old, o);2031}