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 534static inttraverse_trees_recursive(int n,unsigned long dirmask, 535unsigned long df_conflicts, 536struct name_entry *names, 537struct traverse_info *info) 538{ 539int i, ret, bottom; 540struct tree_desc t[MAX_UNPACK_TREES]; 541void*buf[MAX_UNPACK_TREES]; 542struct traverse_info newinfo; 543struct name_entry *p; 544 545 p = names; 546while(!p->mode) 547 p++; 548 549 newinfo = *info; 550 newinfo.prev = info; 551 newinfo.pathspec = info->pathspec; 552 newinfo.name = *p; 553 newinfo.pathlen +=tree_entry_len(p) +1; 554 newinfo.df_conflicts |= df_conflicts; 555 556for(i =0; i < n; i++, dirmask >>=1) { 557const unsigned char*sha1 = NULL; 558if(dirmask &1) 559 sha1 = names[i].oid->hash; 560 buf[i] =fill_tree_descriptor(t+i, sha1); 561} 562 563 bottom =switch_cache_bottom(&newinfo); 564 ret =traverse_trees(n, t, &newinfo); 565restore_cache_bottom(&newinfo, bottom); 566 567for(i =0; i < n; i++) 568free(buf[i]); 569 570return ret; 571} 572 573/* 574 * Compare the traverse-path to the cache entry without actually 575 * having to generate the textual representation of the traverse 576 * path. 577 * 578 * NOTE! This *only* compares up to the size of the traverse path 579 * itself - the caller needs to do the final check for the cache 580 * entry having more data at the end! 581 */ 582static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 583{ 584int len, pathlen, ce_len; 585const char*ce_name; 586 587if(info->prev) { 588int cmp =do_compare_entry_piecewise(ce, info->prev, 589&info->name); 590if(cmp) 591return cmp; 592} 593 pathlen = info->pathlen; 594 ce_len =ce_namelen(ce); 595 596/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 597if(ce_len < pathlen) 598return-1; 599 600 ce_len -= pathlen; 601 ce_name = ce->name + pathlen; 602 603 len =tree_entry_len(n); 604returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 605} 606 607static intdo_compare_entry(const struct cache_entry *ce, 608const struct traverse_info *info, 609const struct name_entry *n) 610{ 611int len, pathlen, ce_len; 612const char*ce_name; 613int cmp; 614 615/* 616 * If we have not precomputed the traverse path, it is quicker 617 * to avoid doing so. But if we have precomputed it, 618 * it is quicker to use the precomputed version. 619 */ 620if(!info->traverse_path) 621returndo_compare_entry_piecewise(ce, info, n); 622 623 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 624if(cmp) 625return cmp; 626 627 pathlen = info->pathlen; 628 ce_len =ce_namelen(ce); 629 630if(ce_len < pathlen) 631return-1; 632 633 ce_len -= pathlen; 634 ce_name = ce->name + pathlen; 635 636 len =tree_entry_len(n); 637returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 638} 639 640static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 641{ 642int cmp =do_compare_entry(ce, info, n); 643if(cmp) 644return cmp; 645 646/* 647 * Even if the beginning compared identically, the ce should 648 * compare as bigger than a directory leading up to it! 649 */ 650returnce_namelen(ce) >traverse_path_len(info, n); 651} 652 653static intce_in_traverse_path(const struct cache_entry *ce, 654const struct traverse_info *info) 655{ 656if(!info->prev) 657return1; 658if(do_compare_entry(ce, info->prev, &info->name)) 659return0; 660/* 661 * If ce (blob) is the same name as the path (which is a tree 662 * we will be descending into), it won't be inside it. 663 */ 664return(info->pathlen <ce_namelen(ce)); 665} 666 667static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 668{ 669int len =traverse_path_len(info, n); 670struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 671 672 ce->ce_mode =create_ce_mode(n->mode); 673 ce->ce_flags =create_ce_flags(stage); 674 ce->ce_namelen = len; 675oidcpy(&ce->oid, n->oid); 676make_traverse_path(ce->name, info, n); 677 678return ce; 679} 680 681static intunpack_nondirectories(int n,unsigned long mask, 682unsigned long dirmask, 683struct cache_entry **src, 684const struct name_entry *names, 685const struct traverse_info *info) 686{ 687int i; 688struct unpack_trees_options *o = info->data; 689unsigned long conflicts = info->df_conflicts | dirmask; 690 691/* Do we have *only* directories? Nothing to do */ 692if(mask == dirmask && !src[0]) 693return0; 694 695/* 696 * Ok, we've filled in up to any potential index entry in src[0], 697 * now do the rest. 698 */ 699for(i =0; i < n; i++) { 700int stage; 701unsigned int bit =1ul<< i; 702if(conflicts & bit) { 703 src[i + o->merge] = o->df_conflict_entry; 704continue; 705} 706if(!(mask & bit)) 707continue; 708if(!o->merge) 709 stage =0; 710else if(i +1< o->head_idx) 711 stage =1; 712else if(i +1> o->head_idx) 713 stage =3; 714else 715 stage =2; 716 src[i + o->merge] =create_ce_entry(info, names + i, stage); 717} 718 719if(o->merge) { 720int rc =call_unpack_fn((const struct cache_entry *const*)src, 721 o); 722for(i =0; i < n; i++) { 723struct cache_entry *ce = src[i + o->merge]; 724if(ce != o->df_conflict_entry) 725free(ce); 726} 727return rc; 728} 729 730for(i =0; i < n; i++) 731if(src[i] && src[i] != o->df_conflict_entry) 732if(do_add_entry(o, src[i],0,0)) 733return-1; 734 735return0; 736} 737 738static intunpack_failed(struct unpack_trees_options *o,const char*message) 739{ 740discard_index(&o->result); 741if(!o->gently && !o->exiting_early) { 742if(message) 743returnerror("%s", message); 744return-1; 745} 746return-1; 747} 748 749/* 750 * The tree traversal is looking at name p. If we have a matching entry, 751 * return it. If name p is a directory in the index, do not return 752 * anything, as we will want to match it when the traversal descends into 753 * the directory. 754 */ 755static intfind_cache_pos(struct traverse_info *info, 756const struct name_entry *p) 757{ 758int pos; 759struct unpack_trees_options *o = info->data; 760struct index_state *index = o->src_index; 761int pfxlen = info->pathlen; 762int p_len =tree_entry_len(p); 763 764for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 765const struct cache_entry *ce = index->cache[pos]; 766const char*ce_name, *ce_slash; 767int cmp, ce_len; 768 769if(ce->ce_flags & CE_UNPACKED) { 770/* 771 * cache_bottom entry is already unpacked, so 772 * we can never match it; don't check it 773 * again. 774 */ 775if(pos == o->cache_bottom) 776++o->cache_bottom; 777continue; 778} 779if(!ce_in_traverse_path(ce, info)) { 780/* 781 * Check if we can skip future cache checks 782 * (because we're already past all possible 783 * entries in the traverse path). 784 */ 785if(info->traverse_path) { 786if(strncmp(ce->name, info->traverse_path, 787 info->pathlen) >0) 788break; 789} 790continue; 791} 792 ce_name = ce->name + pfxlen; 793 ce_slash =strchr(ce_name,'/'); 794if(ce_slash) 795 ce_len = ce_slash - ce_name; 796else 797 ce_len =ce_namelen(ce) - pfxlen; 798 cmp =name_compare(p->path, p_len, ce_name, ce_len); 799/* 800 * Exact match; if we have a directory we need to 801 * delay returning it. 802 */ 803if(!cmp) 804return ce_slash ? -2- pos : pos; 805if(0< cmp) 806continue;/* keep looking */ 807/* 808 * ce_name sorts after p->path; could it be that we 809 * have files under p->path directory in the index? 810 * E.g. ce_name == "t-i", and p->path == "t"; we may 811 * have "t/a" in the index. 812 */ 813if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 814 ce_name[p_len] <'/') 815continue;/* keep looking */ 816break; 817} 818return-1; 819} 820 821static struct cache_entry *find_cache_entry(struct traverse_info *info, 822const struct name_entry *p) 823{ 824int pos =find_cache_pos(info, p); 825struct unpack_trees_options *o = info->data; 826 827if(0<= pos) 828return o->src_index->cache[pos]; 829else 830return NULL; 831} 832 833static voiddebug_path(struct traverse_info *info) 834{ 835if(info->prev) { 836debug_path(info->prev); 837if(*info->prev->name.path) 838putchar('/'); 839} 840printf("%s", info->name.path); 841} 842 843static voiddebug_name_entry(int i,struct name_entry *n) 844{ 845printf("ent#%d %06o%s\n", i, 846 n->path ? n->mode :0, 847 n->path ? n->path :"(missing)"); 848} 849 850static voiddebug_unpack_callback(int n, 851unsigned long mask, 852unsigned long dirmask, 853struct name_entry *names, 854struct traverse_info *info) 855{ 856int i; 857printf("* unpack mask%lu, dirmask%lu, cnt%d", 858 mask, dirmask, n); 859debug_path(info); 860putchar('\n'); 861for(i =0; i < n; i++) 862debug_name_entry(i, names + i); 863} 864 865static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 866{ 867struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 868struct unpack_trees_options *o = info->data; 869const struct name_entry *p = names; 870 871/* Find first entry with a real name (we could use "mask" too) */ 872while(!p->mode) 873 p++; 874 875if(o->debug_unpack) 876debug_unpack_callback(n, mask, dirmask, names, info); 877 878/* Are we supposed to look at the index too? */ 879if(o->merge) { 880while(1) { 881int cmp; 882struct cache_entry *ce; 883 884if(o->diff_index_cached) 885 ce =next_cache_entry(o); 886else 887 ce =find_cache_entry(info, p); 888 889if(!ce) 890break; 891 cmp =compare_entry(ce, info, p); 892if(cmp <0) { 893if(unpack_index_entry(ce, o) <0) 894returnunpack_failed(o, NULL); 895continue; 896} 897if(!cmp) { 898if(ce_stage(ce)) { 899/* 900 * If we skip unmerged index 901 * entries, we'll skip this 902 * entry *and* the tree 903 * entries associated with it! 904 */ 905if(o->skip_unmerged) { 906add_same_unmerged(ce, o); 907return mask; 908} 909} 910 src[0] = ce; 911} 912break; 913} 914} 915 916if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0) 917return-1; 918 919if(o->merge && src[0]) { 920if(ce_stage(src[0])) 921mark_ce_used_same_name(src[0], o); 922else 923mark_ce_used(src[0], o); 924} 925 926/* Now handle any directories.. */ 927if(dirmask) { 928/* special case: "diff-index --cached" looking at a tree */ 929if(o->diff_index_cached && 930 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) { 931int matches; 932 matches =cache_tree_matches_traversal(o->src_index->cache_tree, 933 names, info); 934/* 935 * Everything under the name matches; skip the 936 * entire hierarchy. diff_index_cached codepath 937 * special cases D/F conflicts in such a way that 938 * it does not do any look-ahead, so this is safe. 939 */ 940if(matches) { 941 o->cache_bottom += matches; 942return mask; 943} 944} 945 946if(traverse_trees_recursive(n, dirmask, mask & ~dirmask, 947 names, info) <0) 948return-1; 949return mask; 950} 951 952return mask; 953} 954 955static intclear_ce_flags_1(struct cache_entry **cache,int nr, 956struct strbuf *prefix, 957int select_mask,int clear_mask, 958struct exclude_list *el,int defval); 959 960/* Whole directory matching */ 961static intclear_ce_flags_dir(struct cache_entry **cache,int nr, 962struct strbuf *prefix, 963char*basename, 964int select_mask,int clear_mask, 965struct exclude_list *el,int defval) 966{ 967struct cache_entry **cache_end; 968int dtype = DT_DIR; 969int ret =is_excluded_from_list(prefix->buf, prefix->len, 970 basename, &dtype, el); 971int rc; 972 973strbuf_addch(prefix,'/'); 974 975/* If undecided, use matching result of parent dir in defval */ 976if(ret <0) 977 ret = defval; 978 979for(cache_end = cache; cache_end != cache + nr; cache_end++) { 980struct cache_entry *ce = *cache_end; 981if(strncmp(ce->name, prefix->buf, prefix->len)) 982break; 983} 984 985/* 986 * TODO: check el, if there are no patterns that may conflict 987 * with ret (iow, we know in advance the incl/excl 988 * decision for the entire directory), clear flag here without 989 * calling clear_ce_flags_1(). That function will call 990 * the expensive is_excluded_from_list() on every entry. 991 */ 992 rc =clear_ce_flags_1(cache, cache_end - cache, 993 prefix, 994 select_mask, clear_mask, 995 el, ret); 996strbuf_setlen(prefix, prefix->len -1); 997return rc; 998} 9991000/*1001 * Traverse the index, find every entry that matches according to1002 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1003 * number of traversed entries.1004 *1005 * If select_mask is non-zero, only entries whose ce_flags has on of1006 * those bits enabled are traversed.1007 *1008 * cache : pointer to an index entry1009 * prefix_len : an offset to its path1010 *1011 * The current path ("prefix") including the trailing '/' is1012 * cache[0]->name[0..(prefix_len-1)]1013 * Top level path has prefix_len zero.1014 */1015static intclear_ce_flags_1(struct cache_entry **cache,int nr,1016struct strbuf *prefix,1017int select_mask,int clear_mask,1018struct exclude_list *el,int defval)1019{1020struct cache_entry **cache_end = cache + nr;10211022/*1023 * Process all entries that have the given prefix and meet1024 * select_mask condition1025 */1026while(cache != cache_end) {1027struct cache_entry *ce = *cache;1028const char*name, *slash;1029int len, dtype, ret;10301031if(select_mask && !(ce->ce_flags & select_mask)) {1032 cache++;1033continue;1034}10351036if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1037break;10381039 name = ce->name + prefix->len;1040 slash =strchr(name,'/');10411042/* If it's a directory, try whole directory match first */1043if(slash) {1044int processed;10451046 len = slash - name;1047strbuf_add(prefix, name, len);10481049 processed =clear_ce_flags_dir(cache, cache_end - cache,1050 prefix,1051 prefix->buf + prefix->len - len,1052 select_mask, clear_mask,1053 el, defval);10541055/* clear_c_f_dir eats a whole dir already? */1056if(processed) {1057 cache += processed;1058strbuf_setlen(prefix, prefix->len - len);1059continue;1060}10611062strbuf_addch(prefix,'/');1063 cache +=clear_ce_flags_1(cache, cache_end - cache,1064 prefix,1065 select_mask, clear_mask, el, defval);1066strbuf_setlen(prefix, prefix->len - len -1);1067continue;1068}10691070/* Non-directory */1071 dtype =ce_to_dtype(ce);1072 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1073 name, &dtype, el);1074if(ret <0)1075 ret = defval;1076if(ret >0)1077 ce->ce_flags &= ~clear_mask;1078 cache++;1079}1080return nr - (cache_end - cache);1081}10821083static intclear_ce_flags(struct cache_entry **cache,int nr,1084int select_mask,int clear_mask,1085struct exclude_list *el)1086{1087static struct strbuf prefix = STRBUF_INIT;10881089strbuf_reset(&prefix);10901091returnclear_ce_flags_1(cache, nr,1092&prefix,1093 select_mask, clear_mask,1094 el,0);1095}10961097/*1098 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1099 */1100static voidmark_new_skip_worktree(struct exclude_list *el,1101struct index_state *the_index,1102int select_flag,int skip_wt_flag)1103{1104int i;11051106/*1107 * 1. Pretend the narrowest worktree: only unmerged entries1108 * are checked out1109 */1110for(i =0; i < the_index->cache_nr; i++) {1111struct cache_entry *ce = the_index->cache[i];11121113if(select_flag && !(ce->ce_flags & select_flag))1114continue;11151116if(!ce_stage(ce))1117 ce->ce_flags |= skip_wt_flag;1118else1119 ce->ce_flags &= ~skip_wt_flag;1120}11211122/*1123 * 2. Widen worktree according to sparse-checkout file.1124 * Matched entries will have skip_wt_flag cleared (i.e. "in")1125 */1126clear_ce_flags(the_index->cache, the_index->cache_nr,1127 select_flag, skip_wt_flag, el);1128}11291130static intverify_absent(const struct cache_entry *,1131enum unpack_trees_error_types,1132struct unpack_trees_options *);1133/*1134 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1135 * resulting index, -2 on failure to reflect the changes to the work tree.1136 *1137 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1138 */1139intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1140{1141int i, ret;1142static struct cache_entry *dfc;1143struct exclude_list el;11441145if(len > MAX_UNPACK_TREES)1146die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);11471148memset(&el,0,sizeof(el));1149if(!core_apply_sparse_checkout || !o->update)1150 o->skip_sparse_checkout =1;1151if(!o->skip_sparse_checkout) {1152char*sparse =git_pathdup("info/sparse-checkout");1153if(add_excludes_from_file_to_list(sparse,"",0, &el,0) <0)1154 o->skip_sparse_checkout =1;1155else1156 o->el = ⪙1157free(sparse);1158}11591160memset(&o->result,0,sizeof(o->result));1161 o->result.initialized =1;1162 o->result.timestamp.sec = o->src_index->timestamp.sec;1163 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1164 o->result.version = o->src_index->version;1165 o->result.split_index = o->src_index->split_index;1166if(o->result.split_index)1167 o->result.split_index->refcount++;1168hashcpy(o->result.sha1, o->src_index->sha1);1169 o->merge_size = len;1170mark_all_ce_unused(o->src_index);11711172/*1173 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1174 */1175if(!o->skip_sparse_checkout)1176mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);11771178if(!dfc)1179 dfc =xcalloc(1,cache_entry_size(0));1180 o->df_conflict_entry = dfc;11811182if(len) {1183const char*prefix = o->prefix ? o->prefix :"";1184struct traverse_info info;11851186setup_traverse_info(&info, prefix);1187 info.fn = unpack_callback;1188 info.data = o;1189 info.show_all_errors = o->show_all_errors;1190 info.pathspec = o->pathspec;11911192if(o->prefix) {1193/*1194 * Unpack existing index entries that sort before the1195 * prefix the tree is spliced into. Note that o->merge1196 * is always true in this case.1197 */1198while(1) {1199struct cache_entry *ce =next_cache_entry(o);1200if(!ce)1201break;1202if(ce_in_traverse_path(ce, &info))1203break;1204if(unpack_index_entry(ce, o) <0)1205goto return_failed;1206}1207}12081209if(traverse_trees(len, t, &info) <0)1210goto return_failed;1211}12121213/* Any left-over entries in the index? */1214if(o->merge) {1215while(1) {1216struct cache_entry *ce =next_cache_entry(o);1217if(!ce)1218break;1219if(unpack_index_entry(ce, o) <0)1220goto return_failed;1221}1222}1223mark_all_ce_unused(o->src_index);12241225if(o->trivial_merges_only && o->nontrivial_merge) {1226 ret =unpack_failed(o,"Merge requires file-level merging");1227goto done;1228}12291230if(!o->skip_sparse_checkout) {1231int empty_worktree =1;12321233/*1234 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11235 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1236 * so apply_sparse_checkout() won't attempt to remove it from worktree1237 */1238mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);12391240 ret =0;1241for(i =0; i < o->result.cache_nr; i++) {1242struct cache_entry *ce = o->result.cache[i];12431244/*1245 * Entries marked with CE_ADDED in merged_entry() do not have1246 * verify_absent() check (the check is effectively disabled1247 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1248 *1249 * Do the real check now because we have had1250 * correct CE_NEW_SKIP_WORKTREE1251 */1252if(ce->ce_flags & CE_ADDED &&1253verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1254if(!o->show_all_errors)1255goto return_failed;1256 ret = -1;1257}12581259if(apply_sparse_checkout(&o->result, ce, o)) {1260if(!o->show_all_errors)1261goto return_failed;1262 ret = -1;1263}1264if(!ce_skip_worktree(ce))1265 empty_worktree =0;12661267}1268if(ret <0)1269goto return_failed;1270/*1271 * Sparse checkout is meant to narrow down checkout area1272 * but it does not make sense to narrow down to empty working1273 * tree. This is usually a mistake in sparse checkout rules.1274 * Do not allow users to do that.1275 */1276if(o->result.cache_nr && empty_worktree) {1277 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1278goto done;1279}1280}12811282 o->src_index = NULL;1283 ret =check_updates(o) ? (-2) :0;1284if(o->dst_index) {1285if(!ret) {1286if(!o->result.cache_tree)1287 o->result.cache_tree =cache_tree();1288if(!cache_tree_fully_valid(o->result.cache_tree))1289cache_tree_update(&o->result,1290 WRITE_TREE_SILENT |1291 WRITE_TREE_REPAIR);1292}1293discard_index(o->dst_index);1294*o->dst_index = o->result;1295}else{1296discard_index(&o->result);1297}12981299done:1300clear_exclude_list(&el);1301return ret;13021303return_failed:1304if(o->show_all_errors)1305display_error_msgs(o);1306mark_all_ce_unused(o->src_index);1307 ret =unpack_failed(o, NULL);1308if(o->exiting_early)1309 ret =0;1310goto done;1311}13121313/* Here come the merge functions */13141315static intreject_merge(const struct cache_entry *ce,1316struct unpack_trees_options *o)1317{1318return o->gently ? -1:1319add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1320}13211322static intsame(const struct cache_entry *a,const struct cache_entry *b)1323{1324if(!!a != !!b)1325return0;1326if(!a && !b)1327return1;1328if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1329return0;1330return a->ce_mode == b->ce_mode &&1331!oidcmp(&a->oid, &b->oid);1332}133313341335/*1336 * When a CE gets turned into an unmerged entry, we1337 * want it to be up-to-date1338 */1339static intverify_uptodate_1(const struct cache_entry *ce,1340struct unpack_trees_options *o,1341enum unpack_trees_error_types error_type)1342{1343struct stat st;13441345if(o->index_only)1346return0;13471348/*1349 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1350 * if this entry is truly up-to-date because this file may be1351 * overwritten.1352 */1353if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1354;/* keep checking */1355else if(o->reset ||ce_uptodate(ce))1356return0;13571358if(!lstat(ce->name, &st)) {1359int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1360unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);1361if(!changed)1362return0;1363/*1364 * NEEDSWORK: the current default policy is to allow1365 * submodule to be out of sync wrt the superproject1366 * index. This needs to be tightened later for1367 * submodules that are marked to be automatically1368 * checked out.1369 */1370if(S_ISGITLINK(ce->ce_mode))1371return0;1372 errno =0;1373}1374if(errno == ENOENT)1375return0;1376return o->gently ? -1:1377add_rejected_path(o, error_type, ce->name);1378}13791380static intverify_uptodate(const struct cache_entry *ce,1381struct unpack_trees_options *o)1382{1383if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1384return0;1385returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1386}13871388static intverify_uptodate_sparse(const struct cache_entry *ce,1389struct unpack_trees_options *o)1390{1391returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1392}13931394static voidinvalidate_ce_path(const struct cache_entry *ce,1395struct unpack_trees_options *o)1396{1397if(!ce)1398return;1399cache_tree_invalidate_path(o->src_index, ce->name);1400untracked_cache_invalidate_path(o->src_index, ce->name);1401}14021403/*1404 * Check that checking out ce->sha1 in subdir ce->name is not1405 * going to overwrite any working files.1406 *1407 * Currently, git does not checkout subprojects during a superproject1408 * checkout, so it is not going to overwrite anything.1409 */1410static intverify_clean_submodule(const struct cache_entry *ce,1411enum unpack_trees_error_types error_type,1412struct unpack_trees_options *o)1413{1414return0;1415}14161417static intverify_clean_subdirectory(const struct cache_entry *ce,1418enum unpack_trees_error_types error_type,1419struct unpack_trees_options *o)1420{1421/*1422 * we are about to extract "ce->name"; we would not want to lose1423 * anything in the existing directory there.1424 */1425int namelen;1426int i;1427struct dir_struct d;1428char*pathbuf;1429int cnt =0;1430unsigned char sha1[20];14311432if(S_ISGITLINK(ce->ce_mode) &&1433resolve_gitlink_ref(ce->name,"HEAD", sha1) ==0) {1434/* If we are not going to update the submodule, then1435 * we don't care.1436 */1437if(!hashcmp(sha1, ce->oid.hash))1438return0;1439returnverify_clean_submodule(ce, error_type, o);1440}14411442/*1443 * First let's make sure we do not have a local modification1444 * in that directory.1445 */1446 namelen =ce_namelen(ce);1447for(i =locate_in_src_index(ce, o);1448 i < o->src_index->cache_nr;1449 i++) {1450struct cache_entry *ce2 = o->src_index->cache[i];1451int len =ce_namelen(ce2);1452if(len < namelen ||1453strncmp(ce->name, ce2->name, namelen) ||1454 ce2->name[namelen] !='/')1455break;1456/*1457 * ce2->name is an entry in the subdirectory to be1458 * removed.1459 */1460if(!ce_stage(ce2)) {1461if(verify_uptodate(ce2, o))1462return-1;1463add_entry(o, ce2, CE_REMOVE,0);1464mark_ce_used(ce2, o);1465}1466 cnt++;1467}14681469/*1470 * Then we need to make sure that we do not lose a locally1471 * present file that is not ignored.1472 */1473 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14741475memset(&d,0,sizeof(d));1476if(o->dir)1477 d.exclude_per_dir = o->dir->exclude_per_dir;1478 i =read_directory(&d, pathbuf, namelen+1, NULL);1479if(i)1480return o->gently ? -1:1481add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1482free(pathbuf);1483return cnt;1484}14851486/*1487 * This gets called when there was no index entry for the tree entry 'dst',1488 * but we found a file in the working tree that 'lstat()' said was fine,1489 * and we're on a case-insensitive filesystem.1490 *1491 * See if we can find a case-insensitive match in the index that also1492 * matches the stat information, and assume it's that other file!1493 */1494static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1495{1496const struct cache_entry *src;14971498 src =index_file_exists(o->src_index, name, len,1);1499return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1500}15011502static intcheck_ok_to_remove(const char*name,int len,int dtype,1503const struct cache_entry *ce,struct stat *st,1504enum unpack_trees_error_types error_type,1505struct unpack_trees_options *o)1506{1507const struct cache_entry *result;15081509/*1510 * It may be that the 'lstat()' succeeded even though1511 * target 'ce' was absent, because there is an old1512 * entry that is different only in case..1513 *1514 * Ignore that lstat() if it matches.1515 */1516if(ignore_case &&icase_exists(o, name, len, st))1517return0;15181519if(o->dir &&1520is_excluded(o->dir, name, &dtype))1521/*1522 * ce->name is explicitly excluded, so it is Ok to1523 * overwrite it.1524 */1525return0;1526if(S_ISDIR(st->st_mode)) {1527/*1528 * We are checking out path "foo" and1529 * found "foo/." in the working tree.1530 * This is tricky -- if we have modified1531 * files that are in "foo/" we would lose1532 * them.1533 */1534if(verify_clean_subdirectory(ce, error_type, o) <0)1535return-1;1536return0;1537}15381539/*1540 * The previous round may already have decided to1541 * delete this path, which is in a subdirectory that1542 * is being replaced with a blob.1543 */1544 result =index_file_exists(&o->result, name, len,0);1545if(result) {1546if(result->ce_flags & CE_REMOVE)1547return0;1548}15491550return o->gently ? -1:1551add_rejected_path(o, error_type, name);1552}15531554/*1555 * We do not want to remove or overwrite a working tree file that1556 * is not tracked, unless it is ignored.1557 */1558static intverify_absent_1(const struct cache_entry *ce,1559enum unpack_trees_error_types error_type,1560struct unpack_trees_options *o)1561{1562int len;1563struct stat st;15641565if(o->index_only || o->reset || !o->update)1566return0;15671568 len =check_leading_path(ce->name,ce_namelen(ce));1569if(!len)1570return0;1571else if(len >0) {1572char*path;1573int ret;15741575 path =xmemdupz(ce->name, len);1576if(lstat(path, &st))1577 ret =error_errno("cannot stat '%s'", path);1578else1579 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1580&st, error_type, o);1581free(path);1582return ret;1583}else if(lstat(ce->name, &st)) {1584if(errno != ENOENT)1585returnerror_errno("cannot stat '%s'", ce->name);1586return0;1587}else{1588returncheck_ok_to_remove(ce->name,ce_namelen(ce),1589ce_to_dtype(ce), ce, &st,1590 error_type, o);1591}1592}15931594static intverify_absent(const struct cache_entry *ce,1595enum unpack_trees_error_types error_type,1596struct unpack_trees_options *o)1597{1598if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1599return0;1600returnverify_absent_1(ce, error_type, o);1601}16021603static intverify_absent_sparse(const struct cache_entry *ce,1604enum unpack_trees_error_types error_type,1605struct unpack_trees_options *o)1606{1607enum unpack_trees_error_types orphaned_error = error_type;1608if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1609 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;16101611returnverify_absent_1(ce, orphaned_error, o);1612}16131614static intmerged_entry(const struct cache_entry *ce,1615const struct cache_entry *old,1616struct unpack_trees_options *o)1617{1618int update = CE_UPDATE;1619struct cache_entry *merge =dup_entry(ce);16201621if(!old) {1622/*1623 * New index entries. In sparse checkout, the following1624 * verify_absent() will be delayed until after1625 * traverse_trees() finishes in unpack_trees(), then:1626 *1627 * - CE_NEW_SKIP_WORKTREE will be computed correctly1628 * - verify_absent() be called again, this time with1629 * correct CE_NEW_SKIP_WORKTREE1630 *1631 * verify_absent() call here does nothing in sparse1632 * checkout (i.e. o->skip_sparse_checkout == 0)1633 */1634 update |= CE_ADDED;1635 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;16361637if(verify_absent(merge,1638 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1639free(merge);1640return-1;1641}1642invalidate_ce_path(merge, o);1643}else if(!(old->ce_flags & CE_CONFLICTED)) {1644/*1645 * See if we can re-use the old CE directly?1646 * That way we get the uptodate stat info.1647 *1648 * This also removes the UPDATE flag on a match; otherwise1649 * we will end up overwriting local changes in the work tree.1650 */1651if(same(old, merge)) {1652copy_cache_entry(merge, old);1653 update =0;1654}else{1655if(verify_uptodate(old, o)) {1656free(merge);1657return-1;1658}1659/* Migrate old flags over */1660 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1661invalidate_ce_path(old, o);1662}1663}else{1664/*1665 * Previously unmerged entry left as an existence1666 * marker by read_index_unmerged();1667 */1668invalidate_ce_path(old, o);1669}16701671do_add_entry(o, merge, update, CE_STAGEMASK);1672return1;1673}16741675static intdeleted_entry(const struct cache_entry *ce,1676const struct cache_entry *old,1677struct unpack_trees_options *o)1678{1679/* Did it exist in the index? */1680if(!old) {1681if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1682return-1;1683return0;1684}1685if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1686return-1;1687add_entry(o, ce, CE_REMOVE,0);1688invalidate_ce_path(ce, o);1689return1;1690}16911692static intkeep_entry(const struct cache_entry *ce,1693struct unpack_trees_options *o)1694{1695add_entry(o, ce,0,0);1696return1;1697}16981699#if DBRT_DEBUG1700static voidshow_stage_entry(FILE*o,1701const char*label,const struct cache_entry *ce)1702{1703if(!ce)1704fprintf(o,"%s(missing)\n", label);1705else1706fprintf(o,"%s%06o%s %d\t%s\n",1707 label,1708 ce->ce_mode,1709oid_to_hex(&ce->oid),1710ce_stage(ce),1711 ce->name);1712}1713#endif17141715intthreeway_merge(const struct cache_entry *const*stages,1716struct unpack_trees_options *o)1717{1718const struct cache_entry *index;1719const struct cache_entry *head;1720const struct cache_entry *remote = stages[o->head_idx +1];1721int count;1722int head_match =0;1723int remote_match =0;17241725int df_conflict_head =0;1726int df_conflict_remote =0;17271728int any_anc_missing =0;1729int no_anc_exists =1;1730int i;17311732for(i =1; i < o->head_idx; i++) {1733if(!stages[i] || stages[i] == o->df_conflict_entry)1734 any_anc_missing =1;1735else1736 no_anc_exists =0;1737}17381739 index = stages[0];1740 head = stages[o->head_idx];17411742if(head == o->df_conflict_entry) {1743 df_conflict_head =1;1744 head = NULL;1745}17461747if(remote == o->df_conflict_entry) {1748 df_conflict_remote =1;1749 remote = NULL;1750}17511752/*1753 * First, if there's a #16 situation, note that to prevent #131754 * and #14.1755 */1756if(!same(remote, head)) {1757for(i =1; i < o->head_idx; i++) {1758if(same(stages[i], head)) {1759 head_match = i;1760}1761if(same(stages[i], remote)) {1762 remote_match = i;1763}1764}1765}17661767/*1768 * We start with cases where the index is allowed to match1769 * something other than the head: #14(ALT) and #2ALT, where it1770 * is permitted to match the result instead.1771 */1772/* #14, #14ALT, #2ALT */1773if(remote && !df_conflict_head && head_match && !remote_match) {1774if(index && !same(index, remote) && !same(index, head))1775returnreject_merge(index, o);1776returnmerged_entry(remote, index, o);1777}1778/*1779 * If we have an entry in the index cache, then we want to1780 * make sure that it matches head.1781 */1782if(index && !same(index, head))1783returnreject_merge(index, o);17841785if(head) {1786/* #5ALT, #15 */1787if(same(head, remote))1788returnmerged_entry(head, index, o);1789/* #13, #3ALT */1790if(!df_conflict_remote && remote_match && !head_match)1791returnmerged_entry(head, index, o);1792}17931794/* #1 */1795if(!head && !remote && any_anc_missing)1796return0;17971798/*1799 * Under the "aggressive" rule, we resolve mostly trivial1800 * cases that we historically had git-merge-one-file resolve.1801 */1802if(o->aggressive) {1803int head_deleted = !head;1804int remote_deleted = !remote;1805const struct cache_entry *ce = NULL;18061807if(index)1808 ce = index;1809else if(head)1810 ce = head;1811else if(remote)1812 ce = remote;1813else{1814for(i =1; i < o->head_idx; i++) {1815if(stages[i] && stages[i] != o->df_conflict_entry) {1816 ce = stages[i];1817break;1818}1819}1820}18211822/*1823 * Deleted in both.1824 * Deleted in one and unchanged in the other.1825 */1826if((head_deleted && remote_deleted) ||1827(head_deleted && remote && remote_match) ||1828(remote_deleted && head && head_match)) {1829if(index)1830returndeleted_entry(index, index, o);1831if(ce && !head_deleted) {1832if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1833return-1;1834}1835return0;1836}1837/*1838 * Added in both, identically.1839 */1840if(no_anc_exists && head && remote &&same(head, remote))1841returnmerged_entry(head, index, o);18421843}18441845/* Below are "no merge" cases, which require that the index be1846 * up-to-date to avoid the files getting overwritten with1847 * conflict resolution files.1848 */1849if(index) {1850if(verify_uptodate(index, o))1851return-1;1852}18531854 o->nontrivial_merge =1;18551856/* #2, #3, #4, #6, #7, #9, #10, #11. */1857 count =0;1858if(!head_match || !remote_match) {1859for(i =1; i < o->head_idx; i++) {1860if(stages[i] && stages[i] != o->df_conflict_entry) {1861keep_entry(stages[i], o);1862 count++;1863break;1864}1865}1866}1867#if DBRT_DEBUG1868else{1869fprintf(stderr,"read-tree: warning #16 detected\n");1870show_stage_entry(stderr,"head ", stages[head_match]);1871show_stage_entry(stderr,"remote ", stages[remote_match]);1872}1873#endif1874if(head) { count +=keep_entry(head, o); }1875if(remote) { count +=keep_entry(remote, o); }1876return count;1877}18781879/*1880 * Two-way merge.1881 *1882 * The rule is to "carry forward" what is in the index without losing1883 * information across a "fast-forward", favoring a successful merge1884 * over a merge failure when it makes sense. For details of the1885 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1886 *1887 */1888inttwoway_merge(const struct cache_entry *const*src,1889struct unpack_trees_options *o)1890{1891const struct cache_entry *current = src[0];1892const struct cache_entry *oldtree = src[1];1893const struct cache_entry *newtree = src[2];18941895if(o->merge_size !=2)1896returnerror("Cannot do a twoway merge of%dtrees",1897 o->merge_size);18981899if(oldtree == o->df_conflict_entry)1900 oldtree = NULL;1901if(newtree == o->df_conflict_entry)1902 newtree = NULL;19031904if(current) {1905if(current->ce_flags & CE_CONFLICTED) {1906if(same(oldtree, newtree) || o->reset) {1907if(!newtree)1908returndeleted_entry(current, current, o);1909else1910returnmerged_entry(newtree, current, o);1911}1912returnreject_merge(current, o);1913}else if((!oldtree && !newtree) ||/* 4 and 5 */1914(!oldtree && newtree &&1915same(current, newtree)) ||/* 6 and 7 */1916(oldtree && newtree &&1917same(oldtree, newtree)) ||/* 14 and 15 */1918(oldtree && newtree &&1919!same(oldtree, newtree) &&/* 18 and 19 */1920same(current, newtree))) {1921returnkeep_entry(current, o);1922}else if(oldtree && !newtree &&same(current, oldtree)) {1923/* 10 or 11 */1924returndeleted_entry(oldtree, current, o);1925}else if(oldtree && newtree &&1926same(current, oldtree) && !same(current, newtree)) {1927/* 20 or 21 */1928returnmerged_entry(newtree, current, o);1929}else1930returnreject_merge(current, o);1931}1932else if(newtree) {1933if(oldtree && !o->initial_checkout) {1934/*1935 * deletion of the path was staged;1936 */1937if(same(oldtree, newtree))1938return1;1939returnreject_merge(oldtree, o);1940}1941returnmerged_entry(newtree, current, o);1942}1943returndeleted_entry(oldtree, current, o);1944}19451946/*1947 * Bind merge.1948 *1949 * Keep the index entries at stage0, collapse stage1 but make sure1950 * stage0 does not have anything there.1951 */1952intbind_merge(const struct cache_entry *const*src,1953struct unpack_trees_options *o)1954{1955const struct cache_entry *old = src[0];1956const struct cache_entry *a = src[1];19571958if(o->merge_size !=1)1959returnerror("Cannot do a bind merge of%dtrees",1960 o->merge_size);1961if(a && old)1962return o->gently ? -1:1963error(ERRORMSG(o, ERROR_BIND_OVERLAP),1964super_prefixed(a->name),1965super_prefixed(old->name));1966if(!a)1967returnkeep_entry(old, o);1968else1969returnmerged_entry(a, NULL, o);1970}19711972/*1973 * One-way merge.1974 *1975 * The rule is:1976 * - take the stat information from stage0, take the data from stage11977 */1978intoneway_merge(const struct cache_entry *const*src,1979struct unpack_trees_options *o)1980{1981const struct cache_entry *old = src[0];1982const struct cache_entry *a = src[1];19831984if(o->merge_size !=1)1985returnerror("Cannot do a oneway merge of%dtrees",1986 o->merge_size);19871988if(!a || a == o->df_conflict_entry)1989returndeleted_entry(old, old, o);19901991if(old &&same(old, a)) {1992int update =0;1993if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1994struct stat st;1995if(lstat(old->name, &st) ||1996ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))1997 update |= CE_UPDATE;1998}1999add_entry(o, old, update,0);2000return0;2001}2002returnmerged_entry(a, old, o);2003}