1#define NO_THE_INDEX_COMPATIBILITY_MACROS 2#include"cache.h" 3#include"repository.h" 4#include"config.h" 5#include"dir.h" 6#include"tree.h" 7#include"tree-walk.h" 8#include"cache-tree.h" 9#include"unpack-trees.h" 10#include"progress.h" 11#include"refs.h" 12#include"attr.h" 13#include"split-index.h" 14#include"dir.h" 15#include"submodule.h" 16#include"submodule-config.h" 17 18/* 19 * Error messages expected by scripts out of plumbing commands such as 20 * read-tree. Non-scripted Porcelain is not required to use these messages 21 * and in fact are encouraged to reword them to better suit their particular 22 * situation better. See how "git checkout" and "git merge" replaces 23 * them using setup_unpack_trees_porcelain(), for example. 24 */ 25static const char*unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = { 26/* ERROR_WOULD_OVERWRITE */ 27"Entry '%s' would be overwritten by merge. Cannot merge.", 28 29/* ERROR_NOT_UPTODATE_FILE */ 30"Entry '%s' not uptodate. Cannot merge.", 31 32/* ERROR_NOT_UPTODATE_DIR */ 33"Updating '%s' would lose untracked files in it", 34 35/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ 36"Untracked working tree file '%s' would be overwritten by merge.", 37 38/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ 39"Untracked working tree file '%s' would be removed by merge.", 40 41/* ERROR_BIND_OVERLAP */ 42"Entry '%s' overlaps with '%s'. Cannot bind.", 43 44/* ERROR_SPARSE_NOT_UPTODATE_FILE */ 45"Entry '%s' not uptodate. Cannot update sparse checkout.", 46 47/* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */ 48"Working tree file '%s' would be overwritten by sparse checkout update.", 49 50/* ERROR_WOULD_LOSE_ORPHANED_REMOVED */ 51"Working tree file '%s' would be removed by sparse checkout update.", 52 53/* ERROR_WOULD_LOSE_SUBMODULE */ 54"Submodule '%s' cannot checkout new HEAD.", 55}; 56 57#define ERRORMSG(o,type) \ 58 ( ((o) && (o)->msgs[(type)]) \ 59 ? ((o)->msgs[(type)]) \ 60 : (unpack_plumbing_errors[(type)]) ) 61 62static const char*super_prefixed(const char*path) 63{ 64/* 65 * It is necessary and sufficient to have two static buffers 66 * here, as the return value of this function is fed to 67 * error() using the unpack_*_errors[] templates we see above. 68 */ 69static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; 70static int super_prefix_len = -1; 71static unsigned idx =ARRAY_SIZE(buf) -1; 72 73if(super_prefix_len <0) { 74const char*super_prefix =get_super_prefix(); 75if(!super_prefix) { 76 super_prefix_len =0; 77}else{ 78int i; 79for(i =0; i <ARRAY_SIZE(buf); i++) 80strbuf_addstr(&buf[i], super_prefix); 81 super_prefix_len = buf[0].len; 82} 83} 84 85if(!super_prefix_len) 86return path; 87 88if(++idx >=ARRAY_SIZE(buf)) 89 idx =0; 90 91strbuf_setlen(&buf[idx], super_prefix_len); 92strbuf_addstr(&buf[idx], path); 93 94return buf[idx].buf; 95} 96 97voidsetup_unpack_trees_porcelain(struct unpack_trees_options *opts, 98const char*cmd) 99{ 100int i; 101const char**msgs = opts->msgs; 102const char*msg; 103 104if(!strcmp(cmd,"checkout")) 105 msg = advice_commit_before_merge 106?_("Your local changes to the following files would be overwritten by checkout:\n%%s" 107"Please commit your changes or stash them before you switch branches.") 108:_("Your local changes to the following files would be overwritten by checkout:\n%%s"); 109else if(!strcmp(cmd,"merge")) 110 msg = advice_commit_before_merge 111?_("Your local changes to the following files would be overwritten by merge:\n%%s" 112"Please commit your changes or stash them before you merge.") 113:_("Your local changes to the following files would be overwritten by merge:\n%%s"); 114else 115 msg = advice_commit_before_merge 116?_("Your local changes to the following files would be overwritten by%s:\n%%s" 117"Please commit your changes or stash them before you%s.") 118:_("Your local changes to the following files would be overwritten by%s:\n%%s"); 119 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = 120xstrfmt(msg, cmd, cmd); 121 122 msgs[ERROR_NOT_UPTODATE_DIR] = 123_("Updating the following directories would lose untracked files in them:\n%s"); 124 125if(!strcmp(cmd,"checkout")) 126 msg = advice_commit_before_merge 127?_("The following untracked working tree files would be removed by checkout:\n%%s" 128"Please move or remove them before you switch branches.") 129:_("The following untracked working tree files would be removed by checkout:\n%%s"); 130else if(!strcmp(cmd,"merge")) 131 msg = advice_commit_before_merge 132?_("The following untracked working tree files would be removed by merge:\n%%s" 133"Please move or remove them before you merge.") 134:_("The following untracked working tree files would be removed by merge:\n%%s"); 135else 136 msg = advice_commit_before_merge 137?_("The following untracked working tree files would be removed by%s:\n%%s" 138"Please move or remove them before you%s.") 139:_("The following untracked working tree files would be removed by%s:\n%%s"); 140 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =xstrfmt(msg, cmd, cmd); 141 142if(!strcmp(cmd,"checkout")) 143 msg = advice_commit_before_merge 144?_("The following untracked working tree files would be overwritten by checkout:\n%%s" 145"Please move or remove them before you switch branches.") 146:_("The following untracked working tree files would be overwritten by checkout:\n%%s"); 147else if(!strcmp(cmd,"merge")) 148 msg = advice_commit_before_merge 149?_("The following untracked working tree files would be overwritten by merge:\n%%s" 150"Please move or remove them before you merge.") 151:_("The following untracked working tree files would be overwritten by merge:\n%%s"); 152else 153 msg = advice_commit_before_merge 154?_("The following untracked working tree files would be overwritten by%s:\n%%s" 155"Please move or remove them before you%s.") 156:_("The following untracked working tree files would be overwritten by%s:\n%%s"); 157 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =xstrfmt(msg, cmd, cmd); 158 159/* 160 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we 161 * cannot easily display it as a list. 162 */ 163 msgs[ERROR_BIND_OVERLAP] =_("Entry '%s' overlaps with '%s'. Cannot bind."); 164 165 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] = 166_("Cannot update sparse checkout: the following entries are not up to date:\n%s"); 167 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] = 168_("The following working tree files would be overwritten by sparse checkout update:\n%s"); 169 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] = 170_("The following working tree files would be removed by sparse checkout update:\n%s"); 171 msgs[ERROR_WOULD_LOSE_SUBMODULE] = 172_("Cannot update submodule:\n%s"); 173 174 opts->show_all_errors =1; 175/* rejected paths may not have a static buffer */ 176for(i =0; i <ARRAY_SIZE(opts->unpack_rejects); i++) 177 opts->unpack_rejects[i].strdup_strings =1; 178} 179 180static intdo_add_entry(struct unpack_trees_options *o,struct cache_entry *ce, 181unsigned int set,unsigned int clear) 182{ 183 clear |= CE_HASHED; 184 185if(set & CE_REMOVE) 186 set |= CE_WT_REMOVE; 187 188 ce->ce_flags = (ce->ce_flags & ~clear) | set; 189returnadd_index_entry(&o->result, ce, 190 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 191} 192 193static struct cache_entry *dup_entry(const struct cache_entry *ce) 194{ 195unsigned int size =ce_size(ce); 196struct cache_entry *new=xmalloc(size); 197 198memcpy(new, ce, size); 199return new; 200} 201 202static voidadd_entry(struct unpack_trees_options *o, 203const struct cache_entry *ce, 204unsigned int set,unsigned int clear) 205{ 206do_add_entry(o,dup_entry(ce), set, clear); 207} 208 209/* 210 * add error messages on path <path> 211 * corresponding to the type <e> with the message <msg> 212 * indicating if it should be display in porcelain or not 213 */ 214static intadd_rejected_path(struct unpack_trees_options *o, 215enum unpack_trees_error_types e, 216const char*path) 217{ 218if(!o->show_all_errors) 219returnerror(ERRORMSG(o, e),super_prefixed(path)); 220 221/* 222 * Otherwise, insert in a list for future display by 223 * display_error_msgs() 224 */ 225string_list_append(&o->unpack_rejects[e], path); 226return-1; 227} 228 229/* 230 * display all the error messages stored in a nice way 231 */ 232static voiddisplay_error_msgs(struct unpack_trees_options *o) 233{ 234int e, i; 235int something_displayed =0; 236for(e =0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { 237struct string_list *rejects = &o->unpack_rejects[e]; 238if(rejects->nr >0) { 239struct strbuf path = STRBUF_INIT; 240 something_displayed =1; 241for(i =0; i < rejects->nr; i++) 242strbuf_addf(&path,"\t%s\n", rejects->items[i].string); 243error(ERRORMSG(o, e),super_prefixed(path.buf)); 244strbuf_release(&path); 245} 246string_list_clear(rejects,0); 247} 248if(something_displayed) 249fprintf(stderr,_("Aborting\n")); 250} 251 252static intcheck_submodule_move_head(const struct cache_entry *ce, 253const char*old_id, 254const char*new_id, 255struct unpack_trees_options *o) 256{ 257unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; 258const struct submodule *sub =submodule_from_ce(ce); 259 260if(!sub) 261return0; 262 263if(o->reset) 264 flags |= SUBMODULE_MOVE_HEAD_FORCE; 265 266if(submodule_move_head(ce->name, old_id, new_id, flags)) 267return o->gently ? -1: 268add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); 269return0; 270} 271 272/* 273 * Preform the loading of the repository's gitmodules file. This function is 274 * used by 'check_update()' to perform loading of the gitmodules file in two 275 * differnt situations: 276 * (1) before removing entries from the working tree if the gitmodules file has 277 * been marked for removal. This situation is specified by 'state' == NULL. 278 * (2) before checking out entries to the working tree if the gitmodules file 279 * has been marked for update. This situation is specified by 'state' != NULL. 280 */ 281static voidload_gitmodules_file(struct index_state *index, 282struct checkout *state) 283{ 284int pos =index_name_pos(index, GITMODULES_FILE,strlen(GITMODULES_FILE)); 285 286if(pos >=0) { 287struct cache_entry *ce = index->cache[pos]; 288if(!state && ce->ce_flags & CE_WT_REMOVE) { 289repo_read_gitmodules(the_repository); 290}else if(state && (ce->ce_flags & CE_UPDATE)) { 291submodule_free(); 292checkout_entry(ce, state, NULL); 293repo_read_gitmodules(the_repository); 294} 295} 296} 297 298/* 299 * Unlink the last component and schedule the leading directories for 300 * removal, such that empty directories get removed. 301 */ 302static voidunlink_entry(const struct cache_entry *ce) 303{ 304const struct submodule *sub =submodule_from_ce(ce); 305if(sub) { 306/* state.force is set at the caller. */ 307submodule_move_head(ce->name,"HEAD", NULL, 308 SUBMODULE_MOVE_HEAD_FORCE); 309} 310if(!check_leading_path(ce->name,ce_namelen(ce))) 311return; 312if(remove_or_warn(ce->ce_mode, ce->name)) 313return; 314schedule_dir_for_removal(ce->name,ce_namelen(ce)); 315} 316 317static struct progress *get_progress(struct unpack_trees_options *o) 318{ 319unsigned cnt =0, total =0; 320struct index_state *index = &o->result; 321 322if(!o->update || !o->verbose_update) 323return NULL; 324 325for(; cnt < index->cache_nr; cnt++) { 326const struct cache_entry *ce = index->cache[cnt]; 327if(ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) 328 total++; 329} 330 331returnstart_delayed_progress(_("Checking out files"), total); 332} 333 334static intcheck_updates(struct unpack_trees_options *o) 335{ 336unsigned cnt =0; 337int errs =0; 338struct progress *progress = NULL; 339struct index_state *index = &o->result; 340struct checkout state = CHECKOUT_INIT; 341int i; 342 343 state.force =1; 344 state.quiet =1; 345 state.refresh_cache =1; 346 state.istate = index; 347 348 progress =get_progress(o); 349 350if(o->update) 351git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 352 353if(should_update_submodules() && o->update && !o->dry_run) 354load_gitmodules_file(index, NULL); 355 356for(i =0; i < index->cache_nr; i++) { 357const struct cache_entry *ce = index->cache[i]; 358 359if(ce->ce_flags & CE_WT_REMOVE) { 360display_progress(progress, ++cnt); 361if(o->update && !o->dry_run) 362unlink_entry(ce); 363} 364} 365remove_marked_cache_entries(index); 366remove_scheduled_dirs(); 367 368if(should_update_submodules() && o->update && !o->dry_run) 369load_gitmodules_file(index, &state); 370 371enable_delayed_checkout(&state); 372for(i =0; i < index->cache_nr; i++) { 373struct cache_entry *ce = index->cache[i]; 374 375if(ce->ce_flags & CE_UPDATE) { 376if(ce->ce_flags & CE_WT_REMOVE) 377die("BUG: both update and delete flags are set on%s", 378 ce->name); 379display_progress(progress, ++cnt); 380 ce->ce_flags &= ~CE_UPDATE; 381if(o->update && !o->dry_run) { 382 errs |=checkout_entry(ce, &state, NULL); 383} 384} 385} 386stop_progress(&progress); 387 errs |=finish_delayed_checkout(&state); 388if(o->update) 389git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 390return errs !=0; 391} 392 393static intverify_uptodate_sparse(const struct cache_entry *ce, 394struct unpack_trees_options *o); 395static intverify_absent_sparse(const struct cache_entry *ce, 396enum unpack_trees_error_types, 397struct unpack_trees_options *o); 398 399static intapply_sparse_checkout(struct index_state *istate, 400struct cache_entry *ce, 401struct unpack_trees_options *o) 402{ 403int was_skip_worktree =ce_skip_worktree(ce); 404 405if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 406 ce->ce_flags |= CE_SKIP_WORKTREE; 407else 408 ce->ce_flags &= ~CE_SKIP_WORKTREE; 409if(was_skip_worktree !=ce_skip_worktree(ce)) { 410 ce->ce_flags |= CE_UPDATE_IN_BASE; 411 istate->cache_changed |= CE_ENTRY_CHANGED; 412} 413 414/* 415 * if (!was_skip_worktree && !ce_skip_worktree()) { 416 * This is perfectly normal. Move on; 417 * } 418 */ 419 420/* 421 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 422 * area as a result of ce_skip_worktree() shortcuts in 423 * verify_absent() and verify_uptodate(). 424 * Make sure they don't modify worktree if they are already 425 * outside checkout area 426 */ 427if(was_skip_worktree &&ce_skip_worktree(ce)) { 428 ce->ce_flags &= ~CE_UPDATE; 429 430/* 431 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 432 * on to get that file removed from both index and worktree. 433 * If that file is already outside worktree area, don't 434 * bother remove it. 435 */ 436if(ce->ce_flags & CE_REMOVE) 437 ce->ce_flags &= ~CE_WT_REMOVE; 438} 439 440if(!was_skip_worktree &&ce_skip_worktree(ce)) { 441/* 442 * If CE_UPDATE is set, verify_uptodate() must be called already 443 * also stat info may have lost after merged_entry() so calling 444 * verify_uptodate() again may fail 445 */ 446if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 447return-1; 448 ce->ce_flags |= CE_WT_REMOVE; 449 ce->ce_flags &= ~CE_UPDATE; 450} 451if(was_skip_worktree && !ce_skip_worktree(ce)) { 452if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 453return-1; 454 ce->ce_flags |= CE_UPDATE; 455} 456return0; 457} 458 459staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 460struct unpack_trees_options *o) 461{ 462int ret = o->fn(src, o); 463if(ret >0) 464 ret =0; 465return ret; 466} 467 468static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 469{ 470 ce->ce_flags |= CE_UNPACKED; 471 472if(o->cache_bottom < o->src_index->cache_nr && 473 o->src_index->cache[o->cache_bottom] == ce) { 474int bottom = o->cache_bottom; 475while(bottom < o->src_index->cache_nr && 476 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 477 bottom++; 478 o->cache_bottom = bottom; 479} 480} 481 482static voidmark_all_ce_unused(struct index_state *index) 483{ 484int i; 485for(i =0; i < index->cache_nr; i++) 486 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 487} 488 489static intlocate_in_src_index(const struct cache_entry *ce, 490struct unpack_trees_options *o) 491{ 492struct index_state *index = o->src_index; 493int len =ce_namelen(ce); 494int pos =index_name_pos(index, ce->name, len); 495if(pos <0) 496 pos = -1- pos; 497return pos; 498} 499 500/* 501 * We call unpack_index_entry() with an unmerged cache entry 502 * only in diff-index, and it wants a single callback. Skip 503 * the other unmerged entry with the same name. 504 */ 505static voidmark_ce_used_same_name(struct cache_entry *ce, 506struct unpack_trees_options *o) 507{ 508struct index_state *index = o->src_index; 509int len =ce_namelen(ce); 510int pos; 511 512for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 513struct cache_entry *next = index->cache[pos]; 514if(len !=ce_namelen(next) || 515memcmp(ce->name, next->name, len)) 516break; 517mark_ce_used(next, o); 518} 519} 520 521static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 522{ 523const struct index_state *index = o->src_index; 524int pos = o->cache_bottom; 525 526while(pos < index->cache_nr) { 527struct cache_entry *ce = index->cache[pos]; 528if(!(ce->ce_flags & CE_UNPACKED)) 529return ce; 530 pos++; 531} 532return NULL; 533} 534 535static voidadd_same_unmerged(const struct cache_entry *ce, 536struct unpack_trees_options *o) 537{ 538struct index_state *index = o->src_index; 539int len =ce_namelen(ce); 540int pos =index_name_pos(index, ce->name, len); 541 542if(0<= pos) 543die("programming error in a caller of mark_ce_used_same_name"); 544for(pos = -pos -1; pos < index->cache_nr; pos++) { 545struct cache_entry *next = index->cache[pos]; 546if(len !=ce_namelen(next) || 547memcmp(ce->name, next->name, len)) 548break; 549add_entry(o, next,0,0); 550mark_ce_used(next, o); 551} 552} 553 554static intunpack_index_entry(struct cache_entry *ce, 555struct unpack_trees_options *o) 556{ 557const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 558int ret; 559 560 src[0] = ce; 561 562mark_ce_used(ce, o); 563if(ce_stage(ce)) { 564if(o->skip_unmerged) { 565add_entry(o, ce,0,0); 566return0; 567} 568} 569 ret =call_unpack_fn(src, o); 570if(ce_stage(ce)) 571mark_ce_used_same_name(ce, o); 572return ret; 573} 574 575static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 576 577static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 578{ 579struct unpack_trees_options *o = info->data; 580 581if(o->diff_index_cached) 582return; 583 o->cache_bottom = bottom; 584} 585 586static intswitch_cache_bottom(struct traverse_info *info) 587{ 588struct unpack_trees_options *o = info->data; 589int ret, pos; 590 591if(o->diff_index_cached) 592return0; 593 ret = o->cache_bottom; 594 pos =find_cache_pos(info->prev, &info->name); 595 596if(pos < -1) 597 o->cache_bottom = -2- pos; 598else if(pos <0) 599 o->cache_bottom = o->src_index->cache_nr; 600return ret; 601} 602 603staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 604{ 605return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 606} 607 608static inttraverse_trees_recursive(int n,unsigned long dirmask, 609unsigned long df_conflicts, 610struct name_entry *names, 611struct traverse_info *info) 612{ 613int i, ret, bottom; 614int nr_buf =0; 615struct tree_desc t[MAX_UNPACK_TREES]; 616void*buf[MAX_UNPACK_TREES]; 617struct traverse_info newinfo; 618struct name_entry *p; 619 620 p = names; 621while(!p->mode) 622 p++; 623 624 newinfo = *info; 625 newinfo.prev = info; 626 newinfo.pathspec = info->pathspec; 627 newinfo.name = *p; 628 newinfo.pathlen +=tree_entry_len(p) +1; 629 newinfo.df_conflicts |= df_conflicts; 630 631/* 632 * Fetch the tree from the ODB for each peer directory in the 633 * n commits. 634 * 635 * For 2- and 3-way traversals, we try to avoid hitting the 636 * ODB twice for the same OID. This should yield a nice speed 637 * up in checkouts and merges when the commits are similar. 638 * 639 * We don't bother doing the full O(n^2) search for larger n, 640 * because wider traversals don't happen that often and we 641 * avoid the search setup. 642 * 643 * When 2 peer OIDs are the same, we just copy the tree 644 * descriptor data. This implicitly borrows the buffer 645 * data from the earlier cell. 646 */ 647for(i =0; i < n; i++, dirmask >>=1) { 648if(i >0&&are_same_oid(&names[i], &names[i -1])) 649 t[i] = t[i -1]; 650else if(i >1&&are_same_oid(&names[i], &names[i -2])) 651 t[i] = t[i -2]; 652else{ 653const struct object_id *oid = NULL; 654if(dirmask &1) 655 oid = names[i].oid; 656 buf[nr_buf++] =fill_tree_descriptor(t + i, oid); 657} 658} 659 660 bottom =switch_cache_bottom(&newinfo); 661 ret =traverse_trees(n, t, &newinfo); 662restore_cache_bottom(&newinfo, bottom); 663 664for(i =0; i < nr_buf; i++) 665free(buf[i]); 666 667return ret; 668} 669 670/* 671 * Compare the traverse-path to the cache entry without actually 672 * having to generate the textual representation of the traverse 673 * path. 674 * 675 * NOTE! This *only* compares up to the size of the traverse path 676 * itself - the caller needs to do the final check for the cache 677 * entry having more data at the end! 678 */ 679static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 680{ 681int len, pathlen, ce_len; 682const char*ce_name; 683 684if(info->prev) { 685int cmp =do_compare_entry_piecewise(ce, info->prev, 686&info->name); 687if(cmp) 688return cmp; 689} 690 pathlen = info->pathlen; 691 ce_len =ce_namelen(ce); 692 693/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 694if(ce_len < pathlen) 695return-1; 696 697 ce_len -= pathlen; 698 ce_name = ce->name + pathlen; 699 700 len =tree_entry_len(n); 701returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 702} 703 704static intdo_compare_entry(const struct cache_entry *ce, 705const struct traverse_info *info, 706const struct name_entry *n) 707{ 708int len, pathlen, ce_len; 709const char*ce_name; 710int cmp; 711 712/* 713 * If we have not precomputed the traverse path, it is quicker 714 * to avoid doing so. But if we have precomputed it, 715 * it is quicker to use the precomputed version. 716 */ 717if(!info->traverse_path) 718returndo_compare_entry_piecewise(ce, info, n); 719 720 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 721if(cmp) 722return cmp; 723 724 pathlen = info->pathlen; 725 ce_len =ce_namelen(ce); 726 727if(ce_len < pathlen) 728return-1; 729 730 ce_len -= pathlen; 731 ce_name = ce->name + pathlen; 732 733 len =tree_entry_len(n); 734returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 735} 736 737static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 738{ 739int cmp =do_compare_entry(ce, info, n); 740if(cmp) 741return cmp; 742 743/* 744 * Even if the beginning compared identically, the ce should 745 * compare as bigger than a directory leading up to it! 746 */ 747returnce_namelen(ce) >traverse_path_len(info, n); 748} 749 750static intce_in_traverse_path(const struct cache_entry *ce, 751const struct traverse_info *info) 752{ 753if(!info->prev) 754return1; 755if(do_compare_entry(ce, info->prev, &info->name)) 756return0; 757/* 758 * If ce (blob) is the same name as the path (which is a tree 759 * we will be descending into), it won't be inside it. 760 */ 761return(info->pathlen <ce_namelen(ce)); 762} 763 764static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 765{ 766int len =traverse_path_len(info, n); 767struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 768 769 ce->ce_mode =create_ce_mode(n->mode); 770 ce->ce_flags =create_ce_flags(stage); 771 ce->ce_namelen = len; 772oidcpy(&ce->oid, n->oid); 773make_traverse_path(ce->name, info, n); 774 775return ce; 776} 777 778static intunpack_nondirectories(int n,unsigned long mask, 779unsigned long dirmask, 780struct cache_entry **src, 781const struct name_entry *names, 782const struct traverse_info *info) 783{ 784int i; 785struct unpack_trees_options *o = info->data; 786unsigned long conflicts = info->df_conflicts | dirmask; 787 788/* Do we have *only* directories? Nothing to do */ 789if(mask == dirmask && !src[0]) 790return0; 791 792/* 793 * Ok, we've filled in up to any potential index entry in src[0], 794 * now do the rest. 795 */ 796for(i =0; i < n; i++) { 797int stage; 798unsigned int bit =1ul<< i; 799if(conflicts & bit) { 800 src[i + o->merge] = o->df_conflict_entry; 801continue; 802} 803if(!(mask & bit)) 804continue; 805if(!o->merge) 806 stage =0; 807else if(i +1< o->head_idx) 808 stage =1; 809else if(i +1> o->head_idx) 810 stage =3; 811else 812 stage =2; 813 src[i + o->merge] =create_ce_entry(info, names + i, stage); 814} 815 816if(o->merge) { 817int rc =call_unpack_fn((const struct cache_entry *const*)src, 818 o); 819for(i =0; i < n; i++) { 820struct cache_entry *ce = src[i + o->merge]; 821if(ce != o->df_conflict_entry) 822free(ce); 823} 824return rc; 825} 826 827for(i =0; i < n; i++) 828if(src[i] && src[i] != o->df_conflict_entry) 829if(do_add_entry(o, src[i],0,0)) 830return-1; 831 832return0; 833} 834 835static intunpack_failed(struct unpack_trees_options *o,const char*message) 836{ 837discard_index(&o->result); 838if(!o->gently && !o->exiting_early) { 839if(message) 840returnerror("%s", message); 841return-1; 842} 843return-1; 844} 845 846/* 847 * The tree traversal is looking at name p. If we have a matching entry, 848 * return it. If name p is a directory in the index, do not return 849 * anything, as we will want to match it when the traversal descends into 850 * the directory. 851 */ 852static intfind_cache_pos(struct traverse_info *info, 853const struct name_entry *p) 854{ 855int pos; 856struct unpack_trees_options *o = info->data; 857struct index_state *index = o->src_index; 858int pfxlen = info->pathlen; 859int p_len =tree_entry_len(p); 860 861for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 862const struct cache_entry *ce = index->cache[pos]; 863const char*ce_name, *ce_slash; 864int cmp, ce_len; 865 866if(ce->ce_flags & CE_UNPACKED) { 867/* 868 * cache_bottom entry is already unpacked, so 869 * we can never match it; don't check it 870 * again. 871 */ 872if(pos == o->cache_bottom) 873++o->cache_bottom; 874continue; 875} 876if(!ce_in_traverse_path(ce, info)) { 877/* 878 * Check if we can skip future cache checks 879 * (because we're already past all possible 880 * entries in the traverse path). 881 */ 882if(info->traverse_path) { 883if(strncmp(ce->name, info->traverse_path, 884 info->pathlen) >0) 885break; 886} 887continue; 888} 889 ce_name = ce->name + pfxlen; 890 ce_slash =strchr(ce_name,'/'); 891if(ce_slash) 892 ce_len = ce_slash - ce_name; 893else 894 ce_len =ce_namelen(ce) - pfxlen; 895 cmp =name_compare(p->path, p_len, ce_name, ce_len); 896/* 897 * Exact match; if we have a directory we need to 898 * delay returning it. 899 */ 900if(!cmp) 901return ce_slash ? -2- pos : pos; 902if(0< cmp) 903continue;/* keep looking */ 904/* 905 * ce_name sorts after p->path; could it be that we 906 * have files under p->path directory in the index? 907 * E.g. ce_name == "t-i", and p->path == "t"; we may 908 * have "t/a" in the index. 909 */ 910if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 911 ce_name[p_len] <'/') 912continue;/* keep looking */ 913break; 914} 915return-1; 916} 917 918static struct cache_entry *find_cache_entry(struct traverse_info *info, 919const struct name_entry *p) 920{ 921int pos =find_cache_pos(info, p); 922struct unpack_trees_options *o = info->data; 923 924if(0<= pos) 925return o->src_index->cache[pos]; 926else 927return NULL; 928} 929 930static voiddebug_path(struct traverse_info *info) 931{ 932if(info->prev) { 933debug_path(info->prev); 934if(*info->prev->name.path) 935putchar('/'); 936} 937printf("%s", info->name.path); 938} 939 940static voiddebug_name_entry(int i,struct name_entry *n) 941{ 942printf("ent#%d %06o%s\n", i, 943 n->path ? n->mode :0, 944 n->path ? n->path :"(missing)"); 945} 946 947static voiddebug_unpack_callback(int n, 948unsigned long mask, 949unsigned long dirmask, 950struct name_entry *names, 951struct traverse_info *info) 952{ 953int i; 954printf("* unpack mask%lu, dirmask%lu, cnt%d", 955 mask, dirmask, n); 956debug_path(info); 957putchar('\n'); 958for(i =0; i < n; i++) 959debug_name_entry(i, names + i); 960} 961 962static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 963{ 964struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 965struct unpack_trees_options *o = info->data; 966const struct name_entry *p = names; 967 968/* Find first entry with a real name (we could use "mask" too) */ 969while(!p->mode) 970 p++; 971 972if(o->debug_unpack) 973debug_unpack_callback(n, mask, dirmask, names, info); 974 975/* Are we supposed to look at the index too? */ 976if(o->merge) { 977while(1) { 978int cmp; 979struct cache_entry *ce; 980 981if(o->diff_index_cached) 982 ce =next_cache_entry(o); 983else 984 ce =find_cache_entry(info, p); 985 986if(!ce) 987break; 988 cmp =compare_entry(ce, info, p); 989if(cmp <0) { 990if(unpack_index_entry(ce, o) <0) 991returnunpack_failed(o, NULL); 992continue; 993} 994if(!cmp) { 995if(ce_stage(ce)) { 996/* 997 * If we skip unmerged index 998 * entries, we'll skip this 999 * entry *and* the tree1000 * entries associated with it!1001 */1002if(o->skip_unmerged) {1003add_same_unmerged(ce, o);1004return mask;1005}1006}1007 src[0] = ce;1008}1009break;1010}1011}10121013if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1014return-1;10151016if(o->merge && src[0]) {1017if(ce_stage(src[0]))1018mark_ce_used_same_name(src[0], o);1019else1020mark_ce_used(src[0], o);1021}10221023/* Now handle any directories.. */1024if(dirmask) {1025/* special case: "diff-index --cached" looking at a tree */1026if(o->diff_index_cached &&1027 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1028int matches;1029 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1030 names, info);1031/*1032 * Everything under the name matches; skip the1033 * entire hierarchy. diff_index_cached codepath1034 * special cases D/F conflicts in such a way that1035 * it does not do any look-ahead, so this is safe.1036 */1037if(matches) {1038 o->cache_bottom += matches;1039return mask;1040}1041}10421043if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1044 names, info) <0)1045return-1;1046return mask;1047}10481049return mask;1050}10511052static intclear_ce_flags_1(struct cache_entry **cache,int nr,1053struct strbuf *prefix,1054int select_mask,int clear_mask,1055struct exclude_list *el,int defval);10561057/* Whole directory matching */1058static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1059struct strbuf *prefix,1060char*basename,1061int select_mask,int clear_mask,1062struct exclude_list *el,int defval)1063{1064struct cache_entry **cache_end;1065int dtype = DT_DIR;1066int ret =is_excluded_from_list(prefix->buf, prefix->len,1067 basename, &dtype, el, &the_index);1068int rc;10691070strbuf_addch(prefix,'/');10711072/* If undecided, use matching result of parent dir in defval */1073if(ret <0)1074 ret = defval;10751076for(cache_end = cache; cache_end != cache + nr; cache_end++) {1077struct cache_entry *ce = *cache_end;1078if(strncmp(ce->name, prefix->buf, prefix->len))1079break;1080}10811082/*1083 * TODO: check el, if there are no patterns that may conflict1084 * with ret (iow, we know in advance the incl/excl1085 * decision for the entire directory), clear flag here without1086 * calling clear_ce_flags_1(). That function will call1087 * the expensive is_excluded_from_list() on every entry.1088 */1089 rc =clear_ce_flags_1(cache, cache_end - cache,1090 prefix,1091 select_mask, clear_mask,1092 el, ret);1093strbuf_setlen(prefix, prefix->len -1);1094return rc;1095}10961097/*1098 * Traverse the index, find every entry that matches according to1099 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1100 * number of traversed entries.1101 *1102 * If select_mask is non-zero, only entries whose ce_flags has on of1103 * those bits enabled are traversed.1104 *1105 * cache : pointer to an index entry1106 * prefix_len : an offset to its path1107 *1108 * The current path ("prefix") including the trailing '/' is1109 * cache[0]->name[0..(prefix_len-1)]1110 * Top level path has prefix_len zero.1111 */1112static intclear_ce_flags_1(struct cache_entry **cache,int nr,1113struct strbuf *prefix,1114int select_mask,int clear_mask,1115struct exclude_list *el,int defval)1116{1117struct cache_entry **cache_end = cache + nr;11181119/*1120 * Process all entries that have the given prefix and meet1121 * select_mask condition1122 */1123while(cache != cache_end) {1124struct cache_entry *ce = *cache;1125const char*name, *slash;1126int len, dtype, ret;11271128if(select_mask && !(ce->ce_flags & select_mask)) {1129 cache++;1130continue;1131}11321133if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1134break;11351136 name = ce->name + prefix->len;1137 slash =strchr(name,'/');11381139/* If it's a directory, try whole directory match first */1140if(slash) {1141int processed;11421143 len = slash - name;1144strbuf_add(prefix, name, len);11451146 processed =clear_ce_flags_dir(cache, cache_end - cache,1147 prefix,1148 prefix->buf + prefix->len - len,1149 select_mask, clear_mask,1150 el, defval);11511152/* clear_c_f_dir eats a whole dir already? */1153if(processed) {1154 cache += processed;1155strbuf_setlen(prefix, prefix->len - len);1156continue;1157}11581159strbuf_addch(prefix,'/');1160 cache +=clear_ce_flags_1(cache, cache_end - cache,1161 prefix,1162 select_mask, clear_mask, el, defval);1163strbuf_setlen(prefix, prefix->len - len -1);1164continue;1165}11661167/* Non-directory */1168 dtype =ce_to_dtype(ce);1169 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1170 name, &dtype, el, &the_index);1171if(ret <0)1172 ret = defval;1173if(ret >0)1174 ce->ce_flags &= ~clear_mask;1175 cache++;1176}1177return nr - (cache_end - cache);1178}11791180static intclear_ce_flags(struct cache_entry **cache,int nr,1181int select_mask,int clear_mask,1182struct exclude_list *el)1183{1184static struct strbuf prefix = STRBUF_INIT;11851186strbuf_reset(&prefix);11871188returnclear_ce_flags_1(cache, nr,1189&prefix,1190 select_mask, clear_mask,1191 el,0);1192}11931194/*1195 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1196 */1197static voidmark_new_skip_worktree(struct exclude_list *el,1198struct index_state *the_index,1199int select_flag,int skip_wt_flag)1200{1201int i;12021203/*1204 * 1. Pretend the narrowest worktree: only unmerged entries1205 * are checked out1206 */1207for(i =0; i < the_index->cache_nr; i++) {1208struct cache_entry *ce = the_index->cache[i];12091210if(select_flag && !(ce->ce_flags & select_flag))1211continue;12121213if(!ce_stage(ce))1214 ce->ce_flags |= skip_wt_flag;1215else1216 ce->ce_flags &= ~skip_wt_flag;1217}12181219/*1220 * 2. Widen worktree according to sparse-checkout file.1221 * Matched entries will have skip_wt_flag cleared (i.e. "in")1222 */1223clear_ce_flags(the_index->cache, the_index->cache_nr,1224 select_flag, skip_wt_flag, el);1225}12261227static intverify_absent(const struct cache_entry *,1228enum unpack_trees_error_types,1229struct unpack_trees_options *);1230/*1231 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1232 * resulting index, -2 on failure to reflect the changes to the work tree.1233 *1234 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1235 */1236intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1237{1238int i, ret;1239static struct cache_entry *dfc;1240struct exclude_list el;12411242if(len > MAX_UNPACK_TREES)1243die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12441245memset(&el,0,sizeof(el));1246if(!core_apply_sparse_checkout || !o->update)1247 o->skip_sparse_checkout =1;1248if(!o->skip_sparse_checkout) {1249char*sparse =git_pathdup("info/sparse-checkout");1250if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1251 o->skip_sparse_checkout =1;1252else1253 o->el = ⪙1254free(sparse);1255}12561257memset(&o->result,0,sizeof(o->result));1258 o->result.initialized =1;1259 o->result.timestamp.sec = o->src_index->timestamp.sec;1260 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1261 o->result.version = o->src_index->version;1262 o->result.split_index = o->src_index->split_index;1263if(o->result.split_index)1264 o->result.split_index->refcount++;1265hashcpy(o->result.sha1, o->src_index->sha1);1266 o->merge_size = len;1267mark_all_ce_unused(o->src_index);12681269/*1270 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1271 */1272if(!o->skip_sparse_checkout)1273mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);12741275if(!dfc)1276 dfc =xcalloc(1,cache_entry_size(0));1277 o->df_conflict_entry = dfc;12781279if(len) {1280const char*prefix = o->prefix ? o->prefix :"";1281struct traverse_info info;12821283setup_traverse_info(&info, prefix);1284 info.fn = unpack_callback;1285 info.data = o;1286 info.show_all_errors = o->show_all_errors;1287 info.pathspec = o->pathspec;12881289if(o->prefix) {1290/*1291 * Unpack existing index entries that sort before the1292 * prefix the tree is spliced into. Note that o->merge1293 * is always true in this case.1294 */1295while(1) {1296struct cache_entry *ce =next_cache_entry(o);1297if(!ce)1298break;1299if(ce_in_traverse_path(ce, &info))1300break;1301if(unpack_index_entry(ce, o) <0)1302goto return_failed;1303}1304}13051306if(traverse_trees(len, t, &info) <0)1307goto return_failed;1308}13091310/* Any left-over entries in the index? */1311if(o->merge) {1312while(1) {1313struct cache_entry *ce =next_cache_entry(o);1314if(!ce)1315break;1316if(unpack_index_entry(ce, o) <0)1317goto return_failed;1318}1319}1320mark_all_ce_unused(o->src_index);13211322if(o->trivial_merges_only && o->nontrivial_merge) {1323 ret =unpack_failed(o,"Merge requires file-level merging");1324goto done;1325}13261327if(!o->skip_sparse_checkout) {1328int empty_worktree =1;13291330/*1331 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11332 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1333 * so apply_sparse_checkout() won't attempt to remove it from worktree1334 */1335mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13361337 ret =0;1338for(i =0; i < o->result.cache_nr; i++) {1339struct cache_entry *ce = o->result.cache[i];13401341/*1342 * Entries marked with CE_ADDED in merged_entry() do not have1343 * verify_absent() check (the check is effectively disabled1344 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1345 *1346 * Do the real check now because we have had1347 * correct CE_NEW_SKIP_WORKTREE1348 */1349if(ce->ce_flags & CE_ADDED &&1350verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1351if(!o->show_all_errors)1352goto return_failed;1353 ret = -1;1354}13551356if(apply_sparse_checkout(&o->result, ce, o)) {1357if(!o->show_all_errors)1358goto return_failed;1359 ret = -1;1360}1361if(!ce_skip_worktree(ce))1362 empty_worktree =0;13631364}1365if(ret <0)1366goto return_failed;1367/*1368 * Sparse checkout is meant to narrow down checkout area1369 * but it does not make sense to narrow down to empty working1370 * tree. This is usually a mistake in sparse checkout rules.1371 * Do not allow users to do that.1372 */1373if(o->result.cache_nr && empty_worktree) {1374 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1375goto done;1376}1377}13781379 o->src_index = NULL;1380 ret =check_updates(o) ? (-2) :0;1381if(o->dst_index) {1382if(!ret) {1383if(!o->result.cache_tree)1384 o->result.cache_tree =cache_tree();1385if(!cache_tree_fully_valid(o->result.cache_tree))1386cache_tree_update(&o->result,1387 WRITE_TREE_SILENT |1388 WRITE_TREE_REPAIR);1389}1390move_index_extensions(&o->result, o->dst_index);1391discard_index(o->dst_index);1392*o->dst_index = o->result;1393}else{1394discard_index(&o->result);1395}13961397done:1398clear_exclude_list(&el);1399return ret;14001401return_failed:1402if(o->show_all_errors)1403display_error_msgs(o);1404mark_all_ce_unused(o->src_index);1405 ret =unpack_failed(o, NULL);1406if(o->exiting_early)1407 ret =0;1408goto done;1409}14101411/* Here come the merge functions */14121413static intreject_merge(const struct cache_entry *ce,1414struct unpack_trees_options *o)1415{1416return o->gently ? -1:1417add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1418}14191420static intsame(const struct cache_entry *a,const struct cache_entry *b)1421{1422if(!!a != !!b)1423return0;1424if(!a && !b)1425return1;1426if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1427return0;1428return a->ce_mode == b->ce_mode &&1429!oidcmp(&a->oid, &b->oid);1430}143114321433/*1434 * When a CE gets turned into an unmerged entry, we1435 * want it to be up-to-date1436 */1437static intverify_uptodate_1(const struct cache_entry *ce,1438struct unpack_trees_options *o,1439enum unpack_trees_error_types error_type)1440{1441struct stat st;14421443if(o->index_only)1444return0;14451446/*1447 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1448 * if this entry is truly up-to-date because this file may be1449 * overwritten.1450 */1451if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1452;/* keep checking */1453else if(o->reset ||ce_uptodate(ce))1454return0;14551456if(!lstat(ce->name, &st)) {1457int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1458unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);14591460if(submodule_from_ce(ce)) {1461int r =check_submodule_move_head(ce,1462"HEAD",oid_to_hex(&ce->oid), o);1463if(r)1464return o->gently ? -1:1465add_rejected_path(o, error_type, ce->name);1466return0;1467}14681469if(!changed)1470return0;1471/*1472 * Historic default policy was to allow submodule to be out1473 * of sync wrt the superproject index. If the submodule was1474 * not considered interesting above, we don't care here.1475 */1476if(S_ISGITLINK(ce->ce_mode))1477return0;14781479 errno =0;1480}1481if(errno == ENOENT)1482return0;1483return o->gently ? -1:1484add_rejected_path(o, error_type, ce->name);1485}14861487static intverify_uptodate(const struct cache_entry *ce,1488struct unpack_trees_options *o)1489{1490if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1491return0;1492returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1493}14941495static intverify_uptodate_sparse(const struct cache_entry *ce,1496struct unpack_trees_options *o)1497{1498returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1499}15001501static voidinvalidate_ce_path(const struct cache_entry *ce,1502struct unpack_trees_options *o)1503{1504if(!ce)1505return;1506cache_tree_invalidate_path(o->src_index, ce->name);1507untracked_cache_invalidate_path(o->src_index, ce->name);1508}15091510/*1511 * Check that checking out ce->sha1 in subdir ce->name is not1512 * going to overwrite any working files.1513 *1514 * Currently, git does not checkout subprojects during a superproject1515 * checkout, so it is not going to overwrite anything.1516 */1517static intverify_clean_submodule(const char*old_sha1,1518const struct cache_entry *ce,1519enum unpack_trees_error_types error_type,1520struct unpack_trees_options *o)1521{1522if(!submodule_from_ce(ce))1523return0;15241525returncheck_submodule_move_head(ce, old_sha1,1526oid_to_hex(&ce->oid), o);1527}15281529static intverify_clean_subdirectory(const struct cache_entry *ce,1530enum unpack_trees_error_types error_type,1531struct unpack_trees_options *o)1532{1533/*1534 * we are about to extract "ce->name"; we would not want to lose1535 * anything in the existing directory there.1536 */1537int namelen;1538int i;1539struct dir_struct d;1540char*pathbuf;1541int cnt =0;15421543if(S_ISGITLINK(ce->ce_mode)) {1544unsigned char sha1[20];1545int sub_head =resolve_gitlink_ref(ce->name,"HEAD", sha1);1546/*1547 * If we are not going to update the submodule, then1548 * we don't care.1549 */1550if(!sub_head && !hashcmp(sha1, ce->oid.hash))1551return0;1552returnverify_clean_submodule(sub_head ? NULL :sha1_to_hex(sha1),1553 ce, error_type, o);1554}15551556/*1557 * First let's make sure we do not have a local modification1558 * in that directory.1559 */1560 namelen =ce_namelen(ce);1561for(i =locate_in_src_index(ce, o);1562 i < o->src_index->cache_nr;1563 i++) {1564struct cache_entry *ce2 = o->src_index->cache[i];1565int len =ce_namelen(ce2);1566if(len < namelen ||1567strncmp(ce->name, ce2->name, namelen) ||1568 ce2->name[namelen] !='/')1569break;1570/*1571 * ce2->name is an entry in the subdirectory to be1572 * removed.1573 */1574if(!ce_stage(ce2)) {1575if(verify_uptodate(ce2, o))1576return-1;1577add_entry(o, ce2, CE_REMOVE,0);1578mark_ce_used(ce2, o);1579}1580 cnt++;1581}15821583/*1584 * Then we need to make sure that we do not lose a locally1585 * present file that is not ignored.1586 */1587 pathbuf =xstrfmt("%.*s/", namelen, ce->name);15881589memset(&d,0,sizeof(d));1590if(o->dir)1591 d.exclude_per_dir = o->dir->exclude_per_dir;1592 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1593if(i)1594return o->gently ? -1:1595add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1596free(pathbuf);1597return cnt;1598}15991600/*1601 * This gets called when there was no index entry for the tree entry 'dst',1602 * but we found a file in the working tree that 'lstat()' said was fine,1603 * and we're on a case-insensitive filesystem.1604 *1605 * See if we can find a case-insensitive match in the index that also1606 * matches the stat information, and assume it's that other file!1607 */1608static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1609{1610const struct cache_entry *src;16111612 src =index_file_exists(o->src_index, name, len,1);1613return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1614}16151616static intcheck_ok_to_remove(const char*name,int len,int dtype,1617const struct cache_entry *ce,struct stat *st,1618enum unpack_trees_error_types error_type,1619struct unpack_trees_options *o)1620{1621const struct cache_entry *result;16221623/*1624 * It may be that the 'lstat()' succeeded even though1625 * target 'ce' was absent, because there is an old1626 * entry that is different only in case..1627 *1628 * Ignore that lstat() if it matches.1629 */1630if(ignore_case &&icase_exists(o, name, len, st))1631return0;16321633if(o->dir &&1634is_excluded(o->dir, &the_index, name, &dtype))1635/*1636 * ce->name is explicitly excluded, so it is Ok to1637 * overwrite it.1638 */1639return0;1640if(S_ISDIR(st->st_mode)) {1641/*1642 * We are checking out path "foo" and1643 * found "foo/." in the working tree.1644 * This is tricky -- if we have modified1645 * files that are in "foo/" we would lose1646 * them.1647 */1648if(verify_clean_subdirectory(ce, error_type, o) <0)1649return-1;1650return0;1651}16521653/*1654 * The previous round may already have decided to1655 * delete this path, which is in a subdirectory that1656 * is being replaced with a blob.1657 */1658 result =index_file_exists(&o->result, name, len,0);1659if(result) {1660if(result->ce_flags & CE_REMOVE)1661return0;1662}16631664return o->gently ? -1:1665add_rejected_path(o, error_type, name);1666}16671668/*1669 * We do not want to remove or overwrite a working tree file that1670 * is not tracked, unless it is ignored.1671 */1672static intverify_absent_1(const struct cache_entry *ce,1673enum unpack_trees_error_types error_type,1674struct unpack_trees_options *o)1675{1676int len;1677struct stat st;16781679if(o->index_only || o->reset || !o->update)1680return0;16811682 len =check_leading_path(ce->name,ce_namelen(ce));1683if(!len)1684return0;1685else if(len >0) {1686char*path;1687int ret;16881689 path =xmemdupz(ce->name, len);1690if(lstat(path, &st))1691 ret =error_errno("cannot stat '%s'", path);1692else{1693if(submodule_from_ce(ce))1694 ret =check_submodule_move_head(ce,1695oid_to_hex(&ce->oid),1696 NULL, o);1697else1698 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1699&st, error_type, o);1700}1701free(path);1702return ret;1703}else if(lstat(ce->name, &st)) {1704if(errno != ENOENT)1705returnerror_errno("cannot stat '%s'", ce->name);1706return0;1707}else{1708if(submodule_from_ce(ce))1709returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1710 NULL, o);17111712returncheck_ok_to_remove(ce->name,ce_namelen(ce),1713ce_to_dtype(ce), ce, &st,1714 error_type, o);1715}1716}17171718static intverify_absent(const struct cache_entry *ce,1719enum unpack_trees_error_types error_type,1720struct unpack_trees_options *o)1721{1722if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1723return0;1724returnverify_absent_1(ce, error_type, o);1725}17261727static intverify_absent_sparse(const struct cache_entry *ce,1728enum unpack_trees_error_types error_type,1729struct unpack_trees_options *o)1730{1731enum unpack_trees_error_types orphaned_error = error_type;1732if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1733 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17341735returnverify_absent_1(ce, orphaned_error, o);1736}17371738static intmerged_entry(const struct cache_entry *ce,1739const struct cache_entry *old,1740struct unpack_trees_options *o)1741{1742int update = CE_UPDATE;1743struct cache_entry *merge =dup_entry(ce);17441745if(!old) {1746/*1747 * New index entries. In sparse checkout, the following1748 * verify_absent() will be delayed until after1749 * traverse_trees() finishes in unpack_trees(), then:1750 *1751 * - CE_NEW_SKIP_WORKTREE will be computed correctly1752 * - verify_absent() be called again, this time with1753 * correct CE_NEW_SKIP_WORKTREE1754 *1755 * verify_absent() call here does nothing in sparse1756 * checkout (i.e. o->skip_sparse_checkout == 0)1757 */1758 update |= CE_ADDED;1759 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;17601761if(verify_absent(merge,1762 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1763free(merge);1764return-1;1765}1766invalidate_ce_path(merge, o);17671768if(submodule_from_ce(ce)) {1769int ret =check_submodule_move_head(ce, NULL,1770oid_to_hex(&ce->oid),1771 o);1772if(ret)1773return ret;1774}17751776}else if(!(old->ce_flags & CE_CONFLICTED)) {1777/*1778 * See if we can re-use the old CE directly?1779 * That way we get the uptodate stat info.1780 *1781 * This also removes the UPDATE flag on a match; otherwise1782 * we will end up overwriting local changes in the work tree.1783 */1784if(same(old, merge)) {1785copy_cache_entry(merge, old);1786 update =0;1787}else{1788if(verify_uptodate(old, o)) {1789free(merge);1790return-1;1791}1792/* Migrate old flags over */1793 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1794invalidate_ce_path(old, o);1795}17961797if(submodule_from_ce(ce)) {1798int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1799oid_to_hex(&ce->oid),1800 o);1801if(ret)1802return ret;1803}1804}else{1805/*1806 * Previously unmerged entry left as an existence1807 * marker by read_index_unmerged();1808 */1809invalidate_ce_path(old, o);1810}18111812do_add_entry(o, merge, update, CE_STAGEMASK);1813return1;1814}18151816static intdeleted_entry(const struct cache_entry *ce,1817const struct cache_entry *old,1818struct unpack_trees_options *o)1819{1820/* Did it exist in the index? */1821if(!old) {1822if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1823return-1;1824return0;1825}1826if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1827return-1;1828add_entry(o, ce, CE_REMOVE,0);1829invalidate_ce_path(ce, o);1830return1;1831}18321833static intkeep_entry(const struct cache_entry *ce,1834struct unpack_trees_options *o)1835{1836add_entry(o, ce,0,0);1837return1;1838}18391840#if DBRT_DEBUG1841static voidshow_stage_entry(FILE*o,1842const char*label,const struct cache_entry *ce)1843{1844if(!ce)1845fprintf(o,"%s(missing)\n", label);1846else1847fprintf(o,"%s%06o%s %d\t%s\n",1848 label,1849 ce->ce_mode,1850oid_to_hex(&ce->oid),1851ce_stage(ce),1852 ce->name);1853}1854#endif18551856intthreeway_merge(const struct cache_entry *const*stages,1857struct unpack_trees_options *o)1858{1859const struct cache_entry *index;1860const struct cache_entry *head;1861const struct cache_entry *remote = stages[o->head_idx +1];1862int count;1863int head_match =0;1864int remote_match =0;18651866int df_conflict_head =0;1867int df_conflict_remote =0;18681869int any_anc_missing =0;1870int no_anc_exists =1;1871int i;18721873for(i =1; i < o->head_idx; i++) {1874if(!stages[i] || stages[i] == o->df_conflict_entry)1875 any_anc_missing =1;1876else1877 no_anc_exists =0;1878}18791880 index = stages[0];1881 head = stages[o->head_idx];18821883if(head == o->df_conflict_entry) {1884 df_conflict_head =1;1885 head = NULL;1886}18871888if(remote == o->df_conflict_entry) {1889 df_conflict_remote =1;1890 remote = NULL;1891}18921893/*1894 * First, if there's a #16 situation, note that to prevent #131895 * and #14.1896 */1897if(!same(remote, head)) {1898for(i =1; i < o->head_idx; i++) {1899if(same(stages[i], head)) {1900 head_match = i;1901}1902if(same(stages[i], remote)) {1903 remote_match = i;1904}1905}1906}19071908/*1909 * We start with cases where the index is allowed to match1910 * something other than the head: #14(ALT) and #2ALT, where it1911 * is permitted to match the result instead.1912 */1913/* #14, #14ALT, #2ALT */1914if(remote && !df_conflict_head && head_match && !remote_match) {1915if(index && !same(index, remote) && !same(index, head))1916returnreject_merge(index, o);1917returnmerged_entry(remote, index, o);1918}1919/*1920 * If we have an entry in the index cache, then we want to1921 * make sure that it matches head.1922 */1923if(index && !same(index, head))1924returnreject_merge(index, o);19251926if(head) {1927/* #5ALT, #15 */1928if(same(head, remote))1929returnmerged_entry(head, index, o);1930/* #13, #3ALT */1931if(!df_conflict_remote && remote_match && !head_match)1932returnmerged_entry(head, index, o);1933}19341935/* #1 */1936if(!head && !remote && any_anc_missing)1937return0;19381939/*1940 * Under the "aggressive" rule, we resolve mostly trivial1941 * cases that we historically had git-merge-one-file resolve.1942 */1943if(o->aggressive) {1944int head_deleted = !head;1945int remote_deleted = !remote;1946const struct cache_entry *ce = NULL;19471948if(index)1949 ce = index;1950else if(head)1951 ce = head;1952else if(remote)1953 ce = remote;1954else{1955for(i =1; i < o->head_idx; i++) {1956if(stages[i] && stages[i] != o->df_conflict_entry) {1957 ce = stages[i];1958break;1959}1960}1961}19621963/*1964 * Deleted in both.1965 * Deleted in one and unchanged in the other.1966 */1967if((head_deleted && remote_deleted) ||1968(head_deleted && remote && remote_match) ||1969(remote_deleted && head && head_match)) {1970if(index)1971returndeleted_entry(index, index, o);1972if(ce && !head_deleted) {1973if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1974return-1;1975}1976return0;1977}1978/*1979 * Added in both, identically.1980 */1981if(no_anc_exists && head && remote &&same(head, remote))1982returnmerged_entry(head, index, o);19831984}19851986/* Below are "no merge" cases, which require that the index be1987 * up-to-date to avoid the files getting overwritten with1988 * conflict resolution files.1989 */1990if(index) {1991if(verify_uptodate(index, o))1992return-1;1993}19941995 o->nontrivial_merge =1;19961997/* #2, #3, #4, #6, #7, #9, #10, #11. */1998 count =0;1999if(!head_match || !remote_match) {2000for(i =1; i < o->head_idx; i++) {2001if(stages[i] && stages[i] != o->df_conflict_entry) {2002keep_entry(stages[i], o);2003 count++;2004break;2005}2006}2007}2008#if DBRT_DEBUG2009else{2010fprintf(stderr,"read-tree: warning #16 detected\n");2011show_stage_entry(stderr,"head ", stages[head_match]);2012show_stage_entry(stderr,"remote ", stages[remote_match]);2013}2014#endif2015if(head) { count +=keep_entry(head, o); }2016if(remote) { count +=keep_entry(remote, o); }2017return count;2018}20192020/*2021 * Two-way merge.2022 *2023 * The rule is to "carry forward" what is in the index without losing2024 * information across a "fast-forward", favoring a successful merge2025 * over a merge failure when it makes sense. For details of the2026 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2027 *2028 */2029inttwoway_merge(const struct cache_entry *const*src,2030struct unpack_trees_options *o)2031{2032const struct cache_entry *current = src[0];2033const struct cache_entry *oldtree = src[1];2034const struct cache_entry *newtree = src[2];20352036if(o->merge_size !=2)2037returnerror("Cannot do a twoway merge of%dtrees",2038 o->merge_size);20392040if(oldtree == o->df_conflict_entry)2041 oldtree = NULL;2042if(newtree == o->df_conflict_entry)2043 newtree = NULL;20442045if(current) {2046if(current->ce_flags & CE_CONFLICTED) {2047if(same(oldtree, newtree) || o->reset) {2048if(!newtree)2049returndeleted_entry(current, current, o);2050else2051returnmerged_entry(newtree, current, o);2052}2053returnreject_merge(current, o);2054}else if((!oldtree && !newtree) ||/* 4 and 5 */2055(!oldtree && newtree &&2056same(current, newtree)) ||/* 6 and 7 */2057(oldtree && newtree &&2058same(oldtree, newtree)) ||/* 14 and 15 */2059(oldtree && newtree &&2060!same(oldtree, newtree) &&/* 18 and 19 */2061same(current, newtree))) {2062returnkeep_entry(current, o);2063}else if(oldtree && !newtree &&same(current, oldtree)) {2064/* 10 or 11 */2065returndeleted_entry(oldtree, current, o);2066}else if(oldtree && newtree &&2067same(current, oldtree) && !same(current, newtree)) {2068/* 20 or 21 */2069returnmerged_entry(newtree, current, o);2070}else2071returnreject_merge(current, o);2072}2073else if(newtree) {2074if(oldtree && !o->initial_checkout) {2075/*2076 * deletion of the path was staged;2077 */2078if(same(oldtree, newtree))2079return1;2080returnreject_merge(oldtree, o);2081}2082returnmerged_entry(newtree, current, o);2083}2084returndeleted_entry(oldtree, current, o);2085}20862087/*2088 * Bind merge.2089 *2090 * Keep the index entries at stage0, collapse stage1 but make sure2091 * stage0 does not have anything there.2092 */2093intbind_merge(const struct cache_entry *const*src,2094struct unpack_trees_options *o)2095{2096const struct cache_entry *old = src[0];2097const struct cache_entry *a = src[1];20982099if(o->merge_size !=1)2100returnerror("Cannot do a bind merge of%dtrees",2101 o->merge_size);2102if(a && old)2103return o->gently ? -1:2104error(ERRORMSG(o, ERROR_BIND_OVERLAP),2105super_prefixed(a->name),2106super_prefixed(old->name));2107if(!a)2108returnkeep_entry(old, o);2109else2110returnmerged_entry(a, NULL, o);2111}21122113/*2114 * One-way merge.2115 *2116 * The rule is:2117 * - take the stat information from stage0, take the data from stage12118 */2119intoneway_merge(const struct cache_entry *const*src,2120struct unpack_trees_options *o)2121{2122const struct cache_entry *old = src[0];2123const struct cache_entry *a = src[1];21242125if(o->merge_size !=1)2126returnerror("Cannot do a oneway merge of%dtrees",2127 o->merge_size);21282129if(!a || a == o->df_conflict_entry)2130returndeleted_entry(old, old, o);21312132if(old &&same(old, a)) {2133int update =0;2134if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2135struct stat st;2136if(lstat(old->name, &st) ||2137ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2138 update |= CE_UPDATE;2139}2140add_entry(o, old, update,0);2141return0;2142}2143returnmerged_entry(a, old, o);2144}