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 char*old_sha1,1411const struct cache_entry *ce,1412enum unpack_trees_error_types error_type,1413struct unpack_trees_options *o)1414{1415return0;1416}14171418static intverify_clean_subdirectory(const struct cache_entry *ce,1419enum unpack_trees_error_types error_type,1420struct unpack_trees_options *o)1421{1422/*1423 * we are about to extract "ce->name"; we would not want to lose1424 * anything in the existing directory there.1425 */1426int namelen;1427int i;1428struct dir_struct d;1429char*pathbuf;1430int cnt =0;14311432if(S_ISGITLINK(ce->ce_mode)) {1433unsigned char sha1[20];1434int sub_head =resolve_gitlink_ref(ce->name,"HEAD", sha1);1435/*1436 * If we are not going to update the submodule, then1437 * we don't care.1438 */1439if(!sub_head && !hashcmp(sha1, ce->oid.hash))1440return0;1441returnverify_clean_submodule(sub_head ? NULL :sha1_to_hex(sha1),1442 ce, error_type, o);1443}14441445/*1446 * First let's make sure we do not have a local modification1447 * in that directory.1448 */1449 namelen =ce_namelen(ce);1450for(i =locate_in_src_index(ce, o);1451 i < o->src_index->cache_nr;1452 i++) {1453struct cache_entry *ce2 = o->src_index->cache[i];1454int len =ce_namelen(ce2);1455if(len < namelen ||1456strncmp(ce->name, ce2->name, namelen) ||1457 ce2->name[namelen] !='/')1458break;1459/*1460 * ce2->name is an entry in the subdirectory to be1461 * removed.1462 */1463if(!ce_stage(ce2)) {1464if(verify_uptodate(ce2, o))1465return-1;1466add_entry(o, ce2, CE_REMOVE,0);1467mark_ce_used(ce2, o);1468}1469 cnt++;1470}14711472/*1473 * Then we need to make sure that we do not lose a locally1474 * present file that is not ignored.1475 */1476 pathbuf =xstrfmt("%.*s/", namelen, ce->name);14771478memset(&d,0,sizeof(d));1479if(o->dir)1480 d.exclude_per_dir = o->dir->exclude_per_dir;1481 i =read_directory(&d, pathbuf, namelen+1, NULL);1482if(i)1483return o->gently ? -1:1484add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1485free(pathbuf);1486return cnt;1487}14881489/*1490 * This gets called when there was no index entry for the tree entry 'dst',1491 * but we found a file in the working tree that 'lstat()' said was fine,1492 * and we're on a case-insensitive filesystem.1493 *1494 * See if we can find a case-insensitive match in the index that also1495 * matches the stat information, and assume it's that other file!1496 */1497static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1498{1499const struct cache_entry *src;15001501 src =index_file_exists(o->src_index, name, len,1);1502return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1503}15041505static intcheck_ok_to_remove(const char*name,int len,int dtype,1506const struct cache_entry *ce,struct stat *st,1507enum unpack_trees_error_types error_type,1508struct unpack_trees_options *o)1509{1510const struct cache_entry *result;15111512/*1513 * It may be that the 'lstat()' succeeded even though1514 * target 'ce' was absent, because there is an old1515 * entry that is different only in case..1516 *1517 * Ignore that lstat() if it matches.1518 */1519if(ignore_case &&icase_exists(o, name, len, st))1520return0;15211522if(o->dir &&1523is_excluded(o->dir, name, &dtype))1524/*1525 * ce->name is explicitly excluded, so it is Ok to1526 * overwrite it.1527 */1528return0;1529if(S_ISDIR(st->st_mode)) {1530/*1531 * We are checking out path "foo" and1532 * found "foo/." in the working tree.1533 * This is tricky -- if we have modified1534 * files that are in "foo/" we would lose1535 * them.1536 */1537if(verify_clean_subdirectory(ce, error_type, o) <0)1538return-1;1539return0;1540}15411542/*1543 * The previous round may already have decided to1544 * delete this path, which is in a subdirectory that1545 * is being replaced with a blob.1546 */1547 result =index_file_exists(&o->result, name, len,0);1548if(result) {1549if(result->ce_flags & CE_REMOVE)1550return0;1551}15521553return o->gently ? -1:1554add_rejected_path(o, error_type, name);1555}15561557/*1558 * We do not want to remove or overwrite a working tree file that1559 * is not tracked, unless it is ignored.1560 */1561static intverify_absent_1(const struct cache_entry *ce,1562enum unpack_trees_error_types error_type,1563struct unpack_trees_options *o)1564{1565int len;1566struct stat st;15671568if(o->index_only || o->reset || !o->update)1569return0;15701571 len =check_leading_path(ce->name,ce_namelen(ce));1572if(!len)1573return0;1574else if(len >0) {1575char*path;1576int ret;15771578 path =xmemdupz(ce->name, len);1579if(lstat(path, &st))1580 ret =error_errno("cannot stat '%s'", path);1581else1582 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1583&st, error_type, o);1584free(path);1585return ret;1586}else if(lstat(ce->name, &st)) {1587if(errno != ENOENT)1588returnerror_errno("cannot stat '%s'", ce->name);1589return0;1590}else{1591returncheck_ok_to_remove(ce->name,ce_namelen(ce),1592ce_to_dtype(ce), ce, &st,1593 error_type, o);1594}1595}15961597static intverify_absent(const struct cache_entry *ce,1598enum unpack_trees_error_types error_type,1599struct unpack_trees_options *o)1600{1601if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1602return0;1603returnverify_absent_1(ce, error_type, o);1604}16051606static intverify_absent_sparse(const struct cache_entry *ce,1607enum unpack_trees_error_types error_type,1608struct unpack_trees_options *o)1609{1610enum unpack_trees_error_types orphaned_error = error_type;1611if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1612 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;16131614returnverify_absent_1(ce, orphaned_error, o);1615}16161617static intmerged_entry(const struct cache_entry *ce,1618const struct cache_entry *old,1619struct unpack_trees_options *o)1620{1621int update = CE_UPDATE;1622struct cache_entry *merge =dup_entry(ce);16231624if(!old) {1625/*1626 * New index entries. In sparse checkout, the following1627 * verify_absent() will be delayed until after1628 * traverse_trees() finishes in unpack_trees(), then:1629 *1630 * - CE_NEW_SKIP_WORKTREE will be computed correctly1631 * - verify_absent() be called again, this time with1632 * correct CE_NEW_SKIP_WORKTREE1633 *1634 * verify_absent() call here does nothing in sparse1635 * checkout (i.e. o->skip_sparse_checkout == 0)1636 */1637 update |= CE_ADDED;1638 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;16391640if(verify_absent(merge,1641 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1642free(merge);1643return-1;1644}1645invalidate_ce_path(merge, o);1646}else if(!(old->ce_flags & CE_CONFLICTED)) {1647/*1648 * See if we can re-use the old CE directly?1649 * That way we get the uptodate stat info.1650 *1651 * This also removes the UPDATE flag on a match; otherwise1652 * we will end up overwriting local changes in the work tree.1653 */1654if(same(old, merge)) {1655copy_cache_entry(merge, old);1656 update =0;1657}else{1658if(verify_uptodate(old, o)) {1659free(merge);1660return-1;1661}1662/* Migrate old flags over */1663 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1664invalidate_ce_path(old, o);1665}1666}else{1667/*1668 * Previously unmerged entry left as an existence1669 * marker by read_index_unmerged();1670 */1671invalidate_ce_path(old, o);1672}16731674do_add_entry(o, merge, update, CE_STAGEMASK);1675return1;1676}16771678static intdeleted_entry(const struct cache_entry *ce,1679const struct cache_entry *old,1680struct unpack_trees_options *o)1681{1682/* Did it exist in the index? */1683if(!old) {1684if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1685return-1;1686return0;1687}1688if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1689return-1;1690add_entry(o, ce, CE_REMOVE,0);1691invalidate_ce_path(ce, o);1692return1;1693}16941695static intkeep_entry(const struct cache_entry *ce,1696struct unpack_trees_options *o)1697{1698add_entry(o, ce,0,0);1699return1;1700}17011702#if DBRT_DEBUG1703static voidshow_stage_entry(FILE*o,1704const char*label,const struct cache_entry *ce)1705{1706if(!ce)1707fprintf(o,"%s(missing)\n", label);1708else1709fprintf(o,"%s%06o%s %d\t%s\n",1710 label,1711 ce->ce_mode,1712oid_to_hex(&ce->oid),1713ce_stage(ce),1714 ce->name);1715}1716#endif17171718intthreeway_merge(const struct cache_entry *const*stages,1719struct unpack_trees_options *o)1720{1721const struct cache_entry *index;1722const struct cache_entry *head;1723const struct cache_entry *remote = stages[o->head_idx +1];1724int count;1725int head_match =0;1726int remote_match =0;17271728int df_conflict_head =0;1729int df_conflict_remote =0;17301731int any_anc_missing =0;1732int no_anc_exists =1;1733int i;17341735for(i =1; i < o->head_idx; i++) {1736if(!stages[i] || stages[i] == o->df_conflict_entry)1737 any_anc_missing =1;1738else1739 no_anc_exists =0;1740}17411742 index = stages[0];1743 head = stages[o->head_idx];17441745if(head == o->df_conflict_entry) {1746 df_conflict_head =1;1747 head = NULL;1748}17491750if(remote == o->df_conflict_entry) {1751 df_conflict_remote =1;1752 remote = NULL;1753}17541755/*1756 * First, if there's a #16 situation, note that to prevent #131757 * and #14.1758 */1759if(!same(remote, head)) {1760for(i =1; i < o->head_idx; i++) {1761if(same(stages[i], head)) {1762 head_match = i;1763}1764if(same(stages[i], remote)) {1765 remote_match = i;1766}1767}1768}17691770/*1771 * We start with cases where the index is allowed to match1772 * something other than the head: #14(ALT) and #2ALT, where it1773 * is permitted to match the result instead.1774 */1775/* #14, #14ALT, #2ALT */1776if(remote && !df_conflict_head && head_match && !remote_match) {1777if(index && !same(index, remote) && !same(index, head))1778returnreject_merge(index, o);1779returnmerged_entry(remote, index, o);1780}1781/*1782 * If we have an entry in the index cache, then we want to1783 * make sure that it matches head.1784 */1785if(index && !same(index, head))1786returnreject_merge(index, o);17871788if(head) {1789/* #5ALT, #15 */1790if(same(head, remote))1791returnmerged_entry(head, index, o);1792/* #13, #3ALT */1793if(!df_conflict_remote && remote_match && !head_match)1794returnmerged_entry(head, index, o);1795}17961797/* #1 */1798if(!head && !remote && any_anc_missing)1799return0;18001801/*1802 * Under the "aggressive" rule, we resolve mostly trivial1803 * cases that we historically had git-merge-one-file resolve.1804 */1805if(o->aggressive) {1806int head_deleted = !head;1807int remote_deleted = !remote;1808const struct cache_entry *ce = NULL;18091810if(index)1811 ce = index;1812else if(head)1813 ce = head;1814else if(remote)1815 ce = remote;1816else{1817for(i =1; i < o->head_idx; i++) {1818if(stages[i] && stages[i] != o->df_conflict_entry) {1819 ce = stages[i];1820break;1821}1822}1823}18241825/*1826 * Deleted in both.1827 * Deleted in one and unchanged in the other.1828 */1829if((head_deleted && remote_deleted) ||1830(head_deleted && remote && remote_match) ||1831(remote_deleted && head && head_match)) {1832if(index)1833returndeleted_entry(index, index, o);1834if(ce && !head_deleted) {1835if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1836return-1;1837}1838return0;1839}1840/*1841 * Added in both, identically.1842 */1843if(no_anc_exists && head && remote &&same(head, remote))1844returnmerged_entry(head, index, o);18451846}18471848/* Below are "no merge" cases, which require that the index be1849 * up-to-date to avoid the files getting overwritten with1850 * conflict resolution files.1851 */1852if(index) {1853if(verify_uptodate(index, o))1854return-1;1855}18561857 o->nontrivial_merge =1;18581859/* #2, #3, #4, #6, #7, #9, #10, #11. */1860 count =0;1861if(!head_match || !remote_match) {1862for(i =1; i < o->head_idx; i++) {1863if(stages[i] && stages[i] != o->df_conflict_entry) {1864keep_entry(stages[i], o);1865 count++;1866break;1867}1868}1869}1870#if DBRT_DEBUG1871else{1872fprintf(stderr,"read-tree: warning #16 detected\n");1873show_stage_entry(stderr,"head ", stages[head_match]);1874show_stage_entry(stderr,"remote ", stages[remote_match]);1875}1876#endif1877if(head) { count +=keep_entry(head, o); }1878if(remote) { count +=keep_entry(remote, o); }1879return count;1880}18811882/*1883 * Two-way merge.1884 *1885 * The rule is to "carry forward" what is in the index without losing1886 * information across a "fast-forward", favoring a successful merge1887 * over a merge failure when it makes sense. For details of the1888 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.1889 *1890 */1891inttwoway_merge(const struct cache_entry *const*src,1892struct unpack_trees_options *o)1893{1894const struct cache_entry *current = src[0];1895const struct cache_entry *oldtree = src[1];1896const struct cache_entry *newtree = src[2];18971898if(o->merge_size !=2)1899returnerror("Cannot do a twoway merge of%dtrees",1900 o->merge_size);19011902if(oldtree == o->df_conflict_entry)1903 oldtree = NULL;1904if(newtree == o->df_conflict_entry)1905 newtree = NULL;19061907if(current) {1908if(current->ce_flags & CE_CONFLICTED) {1909if(same(oldtree, newtree) || o->reset) {1910if(!newtree)1911returndeleted_entry(current, current, o);1912else1913returnmerged_entry(newtree, current, o);1914}1915returnreject_merge(current, o);1916}else if((!oldtree && !newtree) ||/* 4 and 5 */1917(!oldtree && newtree &&1918same(current, newtree)) ||/* 6 and 7 */1919(oldtree && newtree &&1920same(oldtree, newtree)) ||/* 14 and 15 */1921(oldtree && newtree &&1922!same(oldtree, newtree) &&/* 18 and 19 */1923same(current, newtree))) {1924returnkeep_entry(current, o);1925}else if(oldtree && !newtree &&same(current, oldtree)) {1926/* 10 or 11 */1927returndeleted_entry(oldtree, current, o);1928}else if(oldtree && newtree &&1929same(current, oldtree) && !same(current, newtree)) {1930/* 20 or 21 */1931returnmerged_entry(newtree, current, o);1932}else1933returnreject_merge(current, o);1934}1935else if(newtree) {1936if(oldtree && !o->initial_checkout) {1937/*1938 * deletion of the path was staged;1939 */1940if(same(oldtree, newtree))1941return1;1942returnreject_merge(oldtree, o);1943}1944returnmerged_entry(newtree, current, o);1945}1946returndeleted_entry(oldtree, current, o);1947}19481949/*1950 * Bind merge.1951 *1952 * Keep the index entries at stage0, collapse stage1 but make sure1953 * stage0 does not have anything there.1954 */1955intbind_merge(const struct cache_entry *const*src,1956struct unpack_trees_options *o)1957{1958const struct cache_entry *old = src[0];1959const struct cache_entry *a = src[1];19601961if(o->merge_size !=1)1962returnerror("Cannot do a bind merge of%dtrees",1963 o->merge_size);1964if(a && old)1965return o->gently ? -1:1966error(ERRORMSG(o, ERROR_BIND_OVERLAP),1967super_prefixed(a->name),1968super_prefixed(old->name));1969if(!a)1970returnkeep_entry(old, o);1971else1972returnmerged_entry(a, NULL, o);1973}19741975/*1976 * One-way merge.1977 *1978 * The rule is:1979 * - take the stat information from stage0, take the data from stage11980 */1981intoneway_merge(const struct cache_entry *const*src,1982struct unpack_trees_options *o)1983{1984const struct cache_entry *old = src[0];1985const struct cache_entry *a = src[1];19861987if(o->merge_size !=1)1988returnerror("Cannot do a oneway merge of%dtrees",1989 o->merge_size);19901991if(!a || a == o->df_conflict_entry)1992returndeleted_entry(old, old, o);19931994if(old &&same(old, a)) {1995int update =0;1996if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {1997struct stat st;1998if(lstat(old->name, &st) ||1999ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2000 update |= CE_UPDATE;2001}2002add_entry(o, old, update,0);2003return0;2004}2005returnmerged_entry(a, old, o);2006}