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_progress_delay(_("Checking out files"), 332 total,50,1); 333} 334 335static intcheck_updates(struct unpack_trees_options *o) 336{ 337unsigned cnt =0; 338int errs =0; 339struct progress *progress = NULL; 340struct index_state *index = &o->result; 341struct checkout state = CHECKOUT_INIT; 342int i; 343 344 state.force =1; 345 state.quiet =1; 346 state.refresh_cache =1; 347 state.istate = index; 348 349 progress =get_progress(o); 350 351if(o->update) 352git_attr_set_direction(GIT_ATTR_CHECKOUT, index); 353 354if(should_update_submodules() && o->update && !o->dry_run) 355load_gitmodules_file(index, NULL); 356 357for(i =0; i < index->cache_nr; i++) { 358const struct cache_entry *ce = index->cache[i]; 359 360if(ce->ce_flags & CE_WT_REMOVE) { 361display_progress(progress, ++cnt); 362if(o->update && !o->dry_run) 363unlink_entry(ce); 364} 365} 366remove_marked_cache_entries(index); 367remove_scheduled_dirs(); 368 369if(should_update_submodules() && o->update && !o->dry_run) 370load_gitmodules_file(index, &state); 371 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); 387if(o->update) 388git_attr_set_direction(GIT_ATTR_CHECKIN, NULL); 389return errs !=0; 390} 391 392static intverify_uptodate_sparse(const struct cache_entry *ce, 393struct unpack_trees_options *o); 394static intverify_absent_sparse(const struct cache_entry *ce, 395enum unpack_trees_error_types, 396struct unpack_trees_options *o); 397 398static intapply_sparse_checkout(struct index_state *istate, 399struct cache_entry *ce, 400struct unpack_trees_options *o) 401{ 402int was_skip_worktree =ce_skip_worktree(ce); 403 404if(ce->ce_flags & CE_NEW_SKIP_WORKTREE) 405 ce->ce_flags |= CE_SKIP_WORKTREE; 406else 407 ce->ce_flags &= ~CE_SKIP_WORKTREE; 408if(was_skip_worktree !=ce_skip_worktree(ce)) { 409 ce->ce_flags |= CE_UPDATE_IN_BASE; 410 istate->cache_changed |= CE_ENTRY_CHANGED; 411} 412 413/* 414 * if (!was_skip_worktree && !ce_skip_worktree()) { 415 * This is perfectly normal. Move on; 416 * } 417 */ 418 419/* 420 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout 421 * area as a result of ce_skip_worktree() shortcuts in 422 * verify_absent() and verify_uptodate(). 423 * Make sure they don't modify worktree if they are already 424 * outside checkout area 425 */ 426if(was_skip_worktree &&ce_skip_worktree(ce)) { 427 ce->ce_flags &= ~CE_UPDATE; 428 429/* 430 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also 431 * on to get that file removed from both index and worktree. 432 * If that file is already outside worktree area, don't 433 * bother remove it. 434 */ 435if(ce->ce_flags & CE_REMOVE) 436 ce->ce_flags &= ~CE_WT_REMOVE; 437} 438 439if(!was_skip_worktree &&ce_skip_worktree(ce)) { 440/* 441 * If CE_UPDATE is set, verify_uptodate() must be called already 442 * also stat info may have lost after merged_entry() so calling 443 * verify_uptodate() again may fail 444 */ 445if(!(ce->ce_flags & CE_UPDATE) &&verify_uptodate_sparse(ce, o)) 446return-1; 447 ce->ce_flags |= CE_WT_REMOVE; 448 ce->ce_flags &= ~CE_UPDATE; 449} 450if(was_skip_worktree && !ce_skip_worktree(ce)) { 451if(verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) 452return-1; 453 ce->ce_flags |= CE_UPDATE; 454} 455return0; 456} 457 458staticinlineintcall_unpack_fn(const struct cache_entry *const*src, 459struct unpack_trees_options *o) 460{ 461int ret = o->fn(src, o); 462if(ret >0) 463 ret =0; 464return ret; 465} 466 467static voidmark_ce_used(struct cache_entry *ce,struct unpack_trees_options *o) 468{ 469 ce->ce_flags |= CE_UNPACKED; 470 471if(o->cache_bottom < o->src_index->cache_nr && 472 o->src_index->cache[o->cache_bottom] == ce) { 473int bottom = o->cache_bottom; 474while(bottom < o->src_index->cache_nr && 475 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) 476 bottom++; 477 o->cache_bottom = bottom; 478} 479} 480 481static voidmark_all_ce_unused(struct index_state *index) 482{ 483int i; 484for(i =0; i < index->cache_nr; i++) 485 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); 486} 487 488static intlocate_in_src_index(const struct cache_entry *ce, 489struct unpack_trees_options *o) 490{ 491struct index_state *index = o->src_index; 492int len =ce_namelen(ce); 493int pos =index_name_pos(index, ce->name, len); 494if(pos <0) 495 pos = -1- pos; 496return pos; 497} 498 499/* 500 * We call unpack_index_entry() with an unmerged cache entry 501 * only in diff-index, and it wants a single callback. Skip 502 * the other unmerged entry with the same name. 503 */ 504static voidmark_ce_used_same_name(struct cache_entry *ce, 505struct unpack_trees_options *o) 506{ 507struct index_state *index = o->src_index; 508int len =ce_namelen(ce); 509int pos; 510 511for(pos =locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { 512struct cache_entry *next = index->cache[pos]; 513if(len !=ce_namelen(next) || 514memcmp(ce->name, next->name, len)) 515break; 516mark_ce_used(next, o); 517} 518} 519 520static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) 521{ 522const struct index_state *index = o->src_index; 523int pos = o->cache_bottom; 524 525while(pos < index->cache_nr) { 526struct cache_entry *ce = index->cache[pos]; 527if(!(ce->ce_flags & CE_UNPACKED)) 528return ce; 529 pos++; 530} 531return NULL; 532} 533 534static voidadd_same_unmerged(const struct cache_entry *ce, 535struct unpack_trees_options *o) 536{ 537struct index_state *index = o->src_index; 538int len =ce_namelen(ce); 539int pos =index_name_pos(index, ce->name, len); 540 541if(0<= pos) 542die("programming error in a caller of mark_ce_used_same_name"); 543for(pos = -pos -1; pos < index->cache_nr; pos++) { 544struct cache_entry *next = index->cache[pos]; 545if(len !=ce_namelen(next) || 546memcmp(ce->name, next->name, len)) 547break; 548add_entry(o, next,0,0); 549mark_ce_used(next, o); 550} 551} 552 553static intunpack_index_entry(struct cache_entry *ce, 554struct unpack_trees_options *o) 555{ 556const struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 557int ret; 558 559 src[0] = ce; 560 561mark_ce_used(ce, o); 562if(ce_stage(ce)) { 563if(o->skip_unmerged) { 564add_entry(o, ce,0,0); 565return0; 566} 567} 568 ret =call_unpack_fn(src, o); 569if(ce_stage(ce)) 570mark_ce_used_same_name(ce, o); 571return ret; 572} 573 574static intfind_cache_pos(struct traverse_info *,const struct name_entry *); 575 576static voidrestore_cache_bottom(struct traverse_info *info,int bottom) 577{ 578struct unpack_trees_options *o = info->data; 579 580if(o->diff_index_cached) 581return; 582 o->cache_bottom = bottom; 583} 584 585static intswitch_cache_bottom(struct traverse_info *info) 586{ 587struct unpack_trees_options *o = info->data; 588int ret, pos; 589 590if(o->diff_index_cached) 591return0; 592 ret = o->cache_bottom; 593 pos =find_cache_pos(info->prev, &info->name); 594 595if(pos < -1) 596 o->cache_bottom = -2- pos; 597else if(pos <0) 598 o->cache_bottom = o->src_index->cache_nr; 599return ret; 600} 601 602staticinlineintare_same_oid(struct name_entry *name_j,struct name_entry *name_k) 603{ 604return name_j->oid && name_k->oid && !oidcmp(name_j->oid, name_k->oid); 605} 606 607static inttraverse_trees_recursive(int n,unsigned long dirmask, 608unsigned long df_conflicts, 609struct name_entry *names, 610struct traverse_info *info) 611{ 612int i, ret, bottom; 613int nr_buf =0; 614struct tree_desc t[MAX_UNPACK_TREES]; 615void*buf[MAX_UNPACK_TREES]; 616struct traverse_info newinfo; 617struct name_entry *p; 618 619 p = names; 620while(!p->mode) 621 p++; 622 623 newinfo = *info; 624 newinfo.prev = info; 625 newinfo.pathspec = info->pathspec; 626 newinfo.name = *p; 627 newinfo.pathlen +=tree_entry_len(p) +1; 628 newinfo.df_conflicts |= df_conflicts; 629 630/* 631 * Fetch the tree from the ODB for each peer directory in the 632 * n commits. 633 * 634 * For 2- and 3-way traversals, we try to avoid hitting the 635 * ODB twice for the same OID. This should yield a nice speed 636 * up in checkouts and merges when the commits are similar. 637 * 638 * We don't bother doing the full O(n^2) search for larger n, 639 * because wider traversals don't happen that often and we 640 * avoid the search setup. 641 * 642 * When 2 peer OIDs are the same, we just copy the tree 643 * descriptor data. This implicitly borrows the buffer 644 * data from the earlier cell. 645 */ 646for(i =0; i < n; i++, dirmask >>=1) { 647if(i >0&&are_same_oid(&names[i], &names[i -1])) 648 t[i] = t[i -1]; 649else if(i >1&&are_same_oid(&names[i], &names[i -2])) 650 t[i] = t[i -2]; 651else{ 652const unsigned char*sha1 = NULL; 653if(dirmask &1) 654 sha1 = names[i].oid->hash; 655 buf[nr_buf++] =fill_tree_descriptor(t+i, sha1); 656} 657} 658 659 bottom =switch_cache_bottom(&newinfo); 660 ret =traverse_trees(n, t, &newinfo); 661restore_cache_bottom(&newinfo, bottom); 662 663for(i =0; i < nr_buf; i++) 664free(buf[i]); 665 666return ret; 667} 668 669/* 670 * Compare the traverse-path to the cache entry without actually 671 * having to generate the textual representation of the traverse 672 * path. 673 * 674 * NOTE! This *only* compares up to the size of the traverse path 675 * itself - the caller needs to do the final check for the cache 676 * entry having more data at the end! 677 */ 678static intdo_compare_entry_piecewise(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 679{ 680int len, pathlen, ce_len; 681const char*ce_name; 682 683if(info->prev) { 684int cmp =do_compare_entry_piecewise(ce, info->prev, 685&info->name); 686if(cmp) 687return cmp; 688} 689 pathlen = info->pathlen; 690 ce_len =ce_namelen(ce); 691 692/* If ce_len < pathlen then we must have previously hit "name == directory" entry */ 693if(ce_len < pathlen) 694return-1; 695 696 ce_len -= pathlen; 697 ce_name = ce->name + pathlen; 698 699 len =tree_entry_len(n); 700returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 701} 702 703static intdo_compare_entry(const struct cache_entry *ce, 704const struct traverse_info *info, 705const struct name_entry *n) 706{ 707int len, pathlen, ce_len; 708const char*ce_name; 709int cmp; 710 711/* 712 * If we have not precomputed the traverse path, it is quicker 713 * to avoid doing so. But if we have precomputed it, 714 * it is quicker to use the precomputed version. 715 */ 716if(!info->traverse_path) 717returndo_compare_entry_piecewise(ce, info, n); 718 719 cmp =strncmp(ce->name, info->traverse_path, info->pathlen); 720if(cmp) 721return cmp; 722 723 pathlen = info->pathlen; 724 ce_len =ce_namelen(ce); 725 726if(ce_len < pathlen) 727return-1; 728 729 ce_len -= pathlen; 730 ce_name = ce->name + pathlen; 731 732 len =tree_entry_len(n); 733returndf_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); 734} 735 736static intcompare_entry(const struct cache_entry *ce,const struct traverse_info *info,const struct name_entry *n) 737{ 738int cmp =do_compare_entry(ce, info, n); 739if(cmp) 740return cmp; 741 742/* 743 * Even if the beginning compared identically, the ce should 744 * compare as bigger than a directory leading up to it! 745 */ 746returnce_namelen(ce) >traverse_path_len(info, n); 747} 748 749static intce_in_traverse_path(const struct cache_entry *ce, 750const struct traverse_info *info) 751{ 752if(!info->prev) 753return1; 754if(do_compare_entry(ce, info->prev, &info->name)) 755return0; 756/* 757 * If ce (blob) is the same name as the path (which is a tree 758 * we will be descending into), it won't be inside it. 759 */ 760return(info->pathlen <ce_namelen(ce)); 761} 762 763static struct cache_entry *create_ce_entry(const struct traverse_info *info,const struct name_entry *n,int stage) 764{ 765int len =traverse_path_len(info, n); 766struct cache_entry *ce =xcalloc(1,cache_entry_size(len)); 767 768 ce->ce_mode =create_ce_mode(n->mode); 769 ce->ce_flags =create_ce_flags(stage); 770 ce->ce_namelen = len; 771oidcpy(&ce->oid, n->oid); 772make_traverse_path(ce->name, info, n); 773 774return ce; 775} 776 777static intunpack_nondirectories(int n,unsigned long mask, 778unsigned long dirmask, 779struct cache_entry **src, 780const struct name_entry *names, 781const struct traverse_info *info) 782{ 783int i; 784struct unpack_trees_options *o = info->data; 785unsigned long conflicts = info->df_conflicts | dirmask; 786 787/* Do we have *only* directories? Nothing to do */ 788if(mask == dirmask && !src[0]) 789return0; 790 791/* 792 * Ok, we've filled in up to any potential index entry in src[0], 793 * now do the rest. 794 */ 795for(i =0; i < n; i++) { 796int stage; 797unsigned int bit =1ul<< i; 798if(conflicts & bit) { 799 src[i + o->merge] = o->df_conflict_entry; 800continue; 801} 802if(!(mask & bit)) 803continue; 804if(!o->merge) 805 stage =0; 806else if(i +1< o->head_idx) 807 stage =1; 808else if(i +1> o->head_idx) 809 stage =3; 810else 811 stage =2; 812 src[i + o->merge] =create_ce_entry(info, names + i, stage); 813} 814 815if(o->merge) { 816int rc =call_unpack_fn((const struct cache_entry *const*)src, 817 o); 818for(i =0; i < n; i++) { 819struct cache_entry *ce = src[i + o->merge]; 820if(ce != o->df_conflict_entry) 821free(ce); 822} 823return rc; 824} 825 826for(i =0; i < n; i++) 827if(src[i] && src[i] != o->df_conflict_entry) 828if(do_add_entry(o, src[i],0,0)) 829return-1; 830 831return0; 832} 833 834static intunpack_failed(struct unpack_trees_options *o,const char*message) 835{ 836discard_index(&o->result); 837if(!o->gently && !o->exiting_early) { 838if(message) 839returnerror("%s", message); 840return-1; 841} 842return-1; 843} 844 845/* 846 * The tree traversal is looking at name p. If we have a matching entry, 847 * return it. If name p is a directory in the index, do not return 848 * anything, as we will want to match it when the traversal descends into 849 * the directory. 850 */ 851static intfind_cache_pos(struct traverse_info *info, 852const struct name_entry *p) 853{ 854int pos; 855struct unpack_trees_options *o = info->data; 856struct index_state *index = o->src_index; 857int pfxlen = info->pathlen; 858int p_len =tree_entry_len(p); 859 860for(pos = o->cache_bottom; pos < index->cache_nr; pos++) { 861const struct cache_entry *ce = index->cache[pos]; 862const char*ce_name, *ce_slash; 863int cmp, ce_len; 864 865if(ce->ce_flags & CE_UNPACKED) { 866/* 867 * cache_bottom entry is already unpacked, so 868 * we can never match it; don't check it 869 * again. 870 */ 871if(pos == o->cache_bottom) 872++o->cache_bottom; 873continue; 874} 875if(!ce_in_traverse_path(ce, info)) { 876/* 877 * Check if we can skip future cache checks 878 * (because we're already past all possible 879 * entries in the traverse path). 880 */ 881if(info->traverse_path) { 882if(strncmp(ce->name, info->traverse_path, 883 info->pathlen) >0) 884break; 885} 886continue; 887} 888 ce_name = ce->name + pfxlen; 889 ce_slash =strchr(ce_name,'/'); 890if(ce_slash) 891 ce_len = ce_slash - ce_name; 892else 893 ce_len =ce_namelen(ce) - pfxlen; 894 cmp =name_compare(p->path, p_len, ce_name, ce_len); 895/* 896 * Exact match; if we have a directory we need to 897 * delay returning it. 898 */ 899if(!cmp) 900return ce_slash ? -2- pos : pos; 901if(0< cmp) 902continue;/* keep looking */ 903/* 904 * ce_name sorts after p->path; could it be that we 905 * have files under p->path directory in the index? 906 * E.g. ce_name == "t-i", and p->path == "t"; we may 907 * have "t/a" in the index. 908 */ 909if(p_len < ce_len && !memcmp(ce_name, p->path, p_len) && 910 ce_name[p_len] <'/') 911continue;/* keep looking */ 912break; 913} 914return-1; 915} 916 917static struct cache_entry *find_cache_entry(struct traverse_info *info, 918const struct name_entry *p) 919{ 920int pos =find_cache_pos(info, p); 921struct unpack_trees_options *o = info->data; 922 923if(0<= pos) 924return o->src_index->cache[pos]; 925else 926return NULL; 927} 928 929static voiddebug_path(struct traverse_info *info) 930{ 931if(info->prev) { 932debug_path(info->prev); 933if(*info->prev->name.path) 934putchar('/'); 935} 936printf("%s", info->name.path); 937} 938 939static voiddebug_name_entry(int i,struct name_entry *n) 940{ 941printf("ent#%d %06o%s\n", i, 942 n->path ? n->mode :0, 943 n->path ? n->path :"(missing)"); 944} 945 946static voiddebug_unpack_callback(int n, 947unsigned long mask, 948unsigned long dirmask, 949struct name_entry *names, 950struct traverse_info *info) 951{ 952int i; 953printf("* unpack mask%lu, dirmask%lu, cnt%d", 954 mask, dirmask, n); 955debug_path(info); 956putchar('\n'); 957for(i =0; i < n; i++) 958debug_name_entry(i, names + i); 959} 960 961static intunpack_callback(int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) 962{ 963struct cache_entry *src[MAX_UNPACK_TREES +1] = { NULL, }; 964struct unpack_trees_options *o = info->data; 965const struct name_entry *p = names; 966 967/* Find first entry with a real name (we could use "mask" too) */ 968while(!p->mode) 969 p++; 970 971if(o->debug_unpack) 972debug_unpack_callback(n, mask, dirmask, names, info); 973 974/* Are we supposed to look at the index too? */ 975if(o->merge) { 976while(1) { 977int cmp; 978struct cache_entry *ce; 979 980if(o->diff_index_cached) 981 ce =next_cache_entry(o); 982else 983 ce =find_cache_entry(info, p); 984 985if(!ce) 986break; 987 cmp =compare_entry(ce, info, p); 988if(cmp <0) { 989if(unpack_index_entry(ce, o) <0) 990returnunpack_failed(o, NULL); 991continue; 992} 993if(!cmp) { 994if(ce_stage(ce)) { 995/* 996 * If we skip unmerged index 997 * entries, we'll skip this 998 * entry *and* the tree 999 * entries associated with it!1000 */1001if(o->skip_unmerged) {1002add_same_unmerged(ce, o);1003return mask;1004}1005}1006 src[0] = ce;1007}1008break;1009}1010}10111012if(unpack_nondirectories(n, mask, dirmask, src, names, info) <0)1013return-1;10141015if(o->merge && src[0]) {1016if(ce_stage(src[0]))1017mark_ce_used_same_name(src[0], o);1018else1019mark_ce_used(src[0], o);1020}10211022/* Now handle any directories.. */1023if(dirmask) {1024/* special case: "diff-index --cached" looking at a tree */1025if(o->diff_index_cached &&1026 n ==1&& dirmask ==1&&S_ISDIR(names->mode)) {1027int matches;1028 matches =cache_tree_matches_traversal(o->src_index->cache_tree,1029 names, info);1030/*1031 * Everything under the name matches; skip the1032 * entire hierarchy. diff_index_cached codepath1033 * special cases D/F conflicts in such a way that1034 * it does not do any look-ahead, so this is safe.1035 */1036if(matches) {1037 o->cache_bottom += matches;1038return mask;1039}1040}10411042if(traverse_trees_recursive(n, dirmask, mask & ~dirmask,1043 names, info) <0)1044return-1;1045return mask;1046}10471048return mask;1049}10501051static intclear_ce_flags_1(struct cache_entry **cache,int nr,1052struct strbuf *prefix,1053int select_mask,int clear_mask,1054struct exclude_list *el,int defval);10551056/* Whole directory matching */1057static intclear_ce_flags_dir(struct cache_entry **cache,int nr,1058struct strbuf *prefix,1059char*basename,1060int select_mask,int clear_mask,1061struct exclude_list *el,int defval)1062{1063struct cache_entry **cache_end;1064int dtype = DT_DIR;1065int ret =is_excluded_from_list(prefix->buf, prefix->len,1066 basename, &dtype, el, &the_index);1067int rc;10681069strbuf_addch(prefix,'/');10701071/* If undecided, use matching result of parent dir in defval */1072if(ret <0)1073 ret = defval;10741075for(cache_end = cache; cache_end != cache + nr; cache_end++) {1076struct cache_entry *ce = *cache_end;1077if(strncmp(ce->name, prefix->buf, prefix->len))1078break;1079}10801081/*1082 * TODO: check el, if there are no patterns that may conflict1083 * with ret (iow, we know in advance the incl/excl1084 * decision for the entire directory), clear flag here without1085 * calling clear_ce_flags_1(). That function will call1086 * the expensive is_excluded_from_list() on every entry.1087 */1088 rc =clear_ce_flags_1(cache, cache_end - cache,1089 prefix,1090 select_mask, clear_mask,1091 el, ret);1092strbuf_setlen(prefix, prefix->len -1);1093return rc;1094}10951096/*1097 * Traverse the index, find every entry that matches according to1098 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the1099 * number of traversed entries.1100 *1101 * If select_mask is non-zero, only entries whose ce_flags has on of1102 * those bits enabled are traversed.1103 *1104 * cache : pointer to an index entry1105 * prefix_len : an offset to its path1106 *1107 * The current path ("prefix") including the trailing '/' is1108 * cache[0]->name[0..(prefix_len-1)]1109 * Top level path has prefix_len zero.1110 */1111static intclear_ce_flags_1(struct cache_entry **cache,int nr,1112struct strbuf *prefix,1113int select_mask,int clear_mask,1114struct exclude_list *el,int defval)1115{1116struct cache_entry **cache_end = cache + nr;11171118/*1119 * Process all entries that have the given prefix and meet1120 * select_mask condition1121 */1122while(cache != cache_end) {1123struct cache_entry *ce = *cache;1124const char*name, *slash;1125int len, dtype, ret;11261127if(select_mask && !(ce->ce_flags & select_mask)) {1128 cache++;1129continue;1130}11311132if(prefix->len &&strncmp(ce->name, prefix->buf, prefix->len))1133break;11341135 name = ce->name + prefix->len;1136 slash =strchr(name,'/');11371138/* If it's a directory, try whole directory match first */1139if(slash) {1140int processed;11411142 len = slash - name;1143strbuf_add(prefix, name, len);11441145 processed =clear_ce_flags_dir(cache, cache_end - cache,1146 prefix,1147 prefix->buf + prefix->len - len,1148 select_mask, clear_mask,1149 el, defval);11501151/* clear_c_f_dir eats a whole dir already? */1152if(processed) {1153 cache += processed;1154strbuf_setlen(prefix, prefix->len - len);1155continue;1156}11571158strbuf_addch(prefix,'/');1159 cache +=clear_ce_flags_1(cache, cache_end - cache,1160 prefix,1161 select_mask, clear_mask, el, defval);1162strbuf_setlen(prefix, prefix->len - len -1);1163continue;1164}11651166/* Non-directory */1167 dtype =ce_to_dtype(ce);1168 ret =is_excluded_from_list(ce->name,ce_namelen(ce),1169 name, &dtype, el, &the_index);1170if(ret <0)1171 ret = defval;1172if(ret >0)1173 ce->ce_flags &= ~clear_mask;1174 cache++;1175}1176return nr - (cache_end - cache);1177}11781179static intclear_ce_flags(struct cache_entry **cache,int nr,1180int select_mask,int clear_mask,1181struct exclude_list *el)1182{1183static struct strbuf prefix = STRBUF_INIT;11841185strbuf_reset(&prefix);11861187returnclear_ce_flags_1(cache, nr,1188&prefix,1189 select_mask, clear_mask,1190 el,0);1191}11921193/*1194 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout1195 */1196static voidmark_new_skip_worktree(struct exclude_list *el,1197struct index_state *the_index,1198int select_flag,int skip_wt_flag)1199{1200int i;12011202/*1203 * 1. Pretend the narrowest worktree: only unmerged entries1204 * are checked out1205 */1206for(i =0; i < the_index->cache_nr; i++) {1207struct cache_entry *ce = the_index->cache[i];12081209if(select_flag && !(ce->ce_flags & select_flag))1210continue;12111212if(!ce_stage(ce))1213 ce->ce_flags |= skip_wt_flag;1214else1215 ce->ce_flags &= ~skip_wt_flag;1216}12171218/*1219 * 2. Widen worktree according to sparse-checkout file.1220 * Matched entries will have skip_wt_flag cleared (i.e. "in")1221 */1222clear_ce_flags(the_index->cache, the_index->cache_nr,1223 select_flag, skip_wt_flag, el);1224}12251226static intverify_absent(const struct cache_entry *,1227enum unpack_trees_error_types,1228struct unpack_trees_options *);1229/*1230 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the1231 * resulting index, -2 on failure to reflect the changes to the work tree.1232 *1233 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally1234 */1235intunpack_trees(unsigned len,struct tree_desc *t,struct unpack_trees_options *o)1236{1237int i, ret;1238static struct cache_entry *dfc;1239struct exclude_list el;12401241if(len > MAX_UNPACK_TREES)1242die("unpack_trees takes at most%dtrees", MAX_UNPACK_TREES);12431244memset(&el,0,sizeof(el));1245if(!core_apply_sparse_checkout || !o->update)1246 o->skip_sparse_checkout =1;1247if(!o->skip_sparse_checkout) {1248char*sparse =git_pathdup("info/sparse-checkout");1249if(add_excludes_from_file_to_list(sparse,"",0, &el, NULL) <0)1250 o->skip_sparse_checkout =1;1251else1252 o->el = ⪙1253free(sparse);1254}12551256memset(&o->result,0,sizeof(o->result));1257 o->result.initialized =1;1258 o->result.timestamp.sec = o->src_index->timestamp.sec;1259 o->result.timestamp.nsec = o->src_index->timestamp.nsec;1260 o->result.version = o->src_index->version;1261 o->result.split_index = o->src_index->split_index;1262if(o->result.split_index)1263 o->result.split_index->refcount++;1264hashcpy(o->result.sha1, o->src_index->sha1);1265 o->merge_size = len;1266mark_all_ce_unused(o->src_index);12671268/*1269 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries1270 */1271if(!o->skip_sparse_checkout)1272mark_new_skip_worktree(o->el, o->src_index,0, CE_NEW_SKIP_WORKTREE);12731274if(!dfc)1275 dfc =xcalloc(1,cache_entry_size(0));1276 o->df_conflict_entry = dfc;12771278if(len) {1279const char*prefix = o->prefix ? o->prefix :"";1280struct traverse_info info;12811282setup_traverse_info(&info, prefix);1283 info.fn = unpack_callback;1284 info.data = o;1285 info.show_all_errors = o->show_all_errors;1286 info.pathspec = o->pathspec;12871288if(o->prefix) {1289/*1290 * Unpack existing index entries that sort before the1291 * prefix the tree is spliced into. Note that o->merge1292 * is always true in this case.1293 */1294while(1) {1295struct cache_entry *ce =next_cache_entry(o);1296if(!ce)1297break;1298if(ce_in_traverse_path(ce, &info))1299break;1300if(unpack_index_entry(ce, o) <0)1301goto return_failed;1302}1303}13041305if(traverse_trees(len, t, &info) <0)1306goto return_failed;1307}13081309/* Any left-over entries in the index? */1310if(o->merge) {1311while(1) {1312struct cache_entry *ce =next_cache_entry(o);1313if(!ce)1314break;1315if(unpack_index_entry(ce, o) <0)1316goto return_failed;1317}1318}1319mark_all_ce_unused(o->src_index);13201321if(o->trivial_merges_only && o->nontrivial_merge) {1322 ret =unpack_failed(o,"Merge requires file-level merging");1323goto done;1324}13251326if(!o->skip_sparse_checkout) {1327int empty_worktree =1;13281329/*1330 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #11331 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE1332 * so apply_sparse_checkout() won't attempt to remove it from worktree1333 */1334mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);13351336 ret =0;1337for(i =0; i < o->result.cache_nr; i++) {1338struct cache_entry *ce = o->result.cache[i];13391340/*1341 * Entries marked with CE_ADDED in merged_entry() do not have1342 * verify_absent() check (the check is effectively disabled1343 * because CE_NEW_SKIP_WORKTREE is set unconditionally).1344 *1345 * Do the real check now because we have had1346 * correct CE_NEW_SKIP_WORKTREE1347 */1348if(ce->ce_flags & CE_ADDED &&1349verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1350if(!o->show_all_errors)1351goto return_failed;1352 ret = -1;1353}13541355if(apply_sparse_checkout(&o->result, ce, o)) {1356if(!o->show_all_errors)1357goto return_failed;1358 ret = -1;1359}1360if(!ce_skip_worktree(ce))1361 empty_worktree =0;13621363}1364if(ret <0)1365goto return_failed;1366/*1367 * Sparse checkout is meant to narrow down checkout area1368 * but it does not make sense to narrow down to empty working1369 * tree. This is usually a mistake in sparse checkout rules.1370 * Do not allow users to do that.1371 */1372if(o->result.cache_nr && empty_worktree) {1373 ret =unpack_failed(o,"Sparse checkout leaves no entry on working directory");1374goto done;1375}1376}13771378 o->src_index = NULL;1379 ret =check_updates(o) ? (-2) :0;1380if(o->dst_index) {1381if(!ret) {1382if(!o->result.cache_tree)1383 o->result.cache_tree =cache_tree();1384if(!cache_tree_fully_valid(o->result.cache_tree))1385cache_tree_update(&o->result,1386 WRITE_TREE_SILENT |1387 WRITE_TREE_REPAIR);1388}1389move_index_extensions(&o->result, o->dst_index);1390discard_index(o->dst_index);1391*o->dst_index = o->result;1392}else{1393discard_index(&o->result);1394}13951396done:1397clear_exclude_list(&el);1398return ret;13991400return_failed:1401if(o->show_all_errors)1402display_error_msgs(o);1403mark_all_ce_unused(o->src_index);1404 ret =unpack_failed(o, NULL);1405if(o->exiting_early)1406 ret =0;1407goto done;1408}14091410/* Here come the merge functions */14111412static intreject_merge(const struct cache_entry *ce,1413struct unpack_trees_options *o)1414{1415return o->gently ? -1:1416add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);1417}14181419static intsame(const struct cache_entry *a,const struct cache_entry *b)1420{1421if(!!a != !!b)1422return0;1423if(!a && !b)1424return1;1425if((a->ce_flags | b->ce_flags) & CE_CONFLICTED)1426return0;1427return a->ce_mode == b->ce_mode &&1428!oidcmp(&a->oid, &b->oid);1429}143014311432/*1433 * When a CE gets turned into an unmerged entry, we1434 * want it to be up-to-date1435 */1436static intverify_uptodate_1(const struct cache_entry *ce,1437struct unpack_trees_options *o,1438enum unpack_trees_error_types error_type)1439{1440struct stat st;14411442if(o->index_only)1443return0;14441445/*1446 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again1447 * if this entry is truly up-to-date because this file may be1448 * overwritten.1449 */1450if((ce->ce_flags & CE_VALID) ||ce_skip_worktree(ce))1451;/* keep checking */1452else if(o->reset ||ce_uptodate(ce))1453return0;14541455if(!lstat(ce->name, &st)) {1456int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;1457unsigned changed =ie_match_stat(o->src_index, ce, &st, flags);14581459if(submodule_from_ce(ce)) {1460int r =check_submodule_move_head(ce,1461"HEAD",oid_to_hex(&ce->oid), o);1462if(r)1463return o->gently ? -1:1464add_rejected_path(o, error_type, ce->name);1465return0;1466}14671468if(!changed)1469return0;1470/*1471 * Historic default policy was to allow submodule to be out1472 * of sync wrt the superproject index. If the submodule was1473 * not considered interesting above, we don't care here.1474 */1475if(S_ISGITLINK(ce->ce_mode))1476return0;14771478 errno =0;1479}1480if(errno == ENOENT)1481return0;1482return o->gently ? -1:1483add_rejected_path(o, error_type, ce->name);1484}14851486static intverify_uptodate(const struct cache_entry *ce,1487struct unpack_trees_options *o)1488{1489if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1490return0;1491returnverify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);1492}14931494static intverify_uptodate_sparse(const struct cache_entry *ce,1495struct unpack_trees_options *o)1496{1497returnverify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);1498}14991500static voidinvalidate_ce_path(const struct cache_entry *ce,1501struct unpack_trees_options *o)1502{1503if(!ce)1504return;1505cache_tree_invalidate_path(o->src_index, ce->name);1506untracked_cache_invalidate_path(o->src_index, ce->name);1507}15081509/*1510 * Check that checking out ce->sha1 in subdir ce->name is not1511 * going to overwrite any working files.1512 *1513 * Currently, git does not checkout subprojects during a superproject1514 * checkout, so it is not going to overwrite anything.1515 */1516static intverify_clean_submodule(const char*old_sha1,1517const struct cache_entry *ce,1518enum unpack_trees_error_types error_type,1519struct unpack_trees_options *o)1520{1521if(!submodule_from_ce(ce))1522return0;15231524returncheck_submodule_move_head(ce, old_sha1,1525oid_to_hex(&ce->oid), o);1526}15271528static intverify_clean_subdirectory(const struct cache_entry *ce,1529enum unpack_trees_error_types error_type,1530struct unpack_trees_options *o)1531{1532/*1533 * we are about to extract "ce->name"; we would not want to lose1534 * anything in the existing directory there.1535 */1536int namelen;1537int i;1538struct dir_struct d;1539char*pathbuf;1540int cnt =0;15411542if(S_ISGITLINK(ce->ce_mode)) {1543unsigned char sha1[20];1544int sub_head =resolve_gitlink_ref(ce->name,"HEAD", sha1);1545/*1546 * If we are not going to update the submodule, then1547 * we don't care.1548 */1549if(!sub_head && !hashcmp(sha1, ce->oid.hash))1550return0;1551returnverify_clean_submodule(sub_head ? NULL :sha1_to_hex(sha1),1552 ce, error_type, o);1553}15541555/*1556 * First let's make sure we do not have a local modification1557 * in that directory.1558 */1559 namelen =ce_namelen(ce);1560for(i =locate_in_src_index(ce, o);1561 i < o->src_index->cache_nr;1562 i++) {1563struct cache_entry *ce2 = o->src_index->cache[i];1564int len =ce_namelen(ce2);1565if(len < namelen ||1566strncmp(ce->name, ce2->name, namelen) ||1567 ce2->name[namelen] !='/')1568break;1569/*1570 * ce2->name is an entry in the subdirectory to be1571 * removed.1572 */1573if(!ce_stage(ce2)) {1574if(verify_uptodate(ce2, o))1575return-1;1576add_entry(o, ce2, CE_REMOVE,0);1577mark_ce_used(ce2, o);1578}1579 cnt++;1580}15811582/*1583 * Then we need to make sure that we do not lose a locally1584 * present file that is not ignored.1585 */1586 pathbuf =xstrfmt("%.*s/", namelen, ce->name);15871588memset(&d,0,sizeof(d));1589if(o->dir)1590 d.exclude_per_dir = o->dir->exclude_per_dir;1591 i =read_directory(&d, &the_index, pathbuf, namelen+1, NULL);1592if(i)1593return o->gently ? -1:1594add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);1595free(pathbuf);1596return cnt;1597}15981599/*1600 * This gets called when there was no index entry for the tree entry 'dst',1601 * but we found a file in the working tree that 'lstat()' said was fine,1602 * and we're on a case-insensitive filesystem.1603 *1604 * See if we can find a case-insensitive match in the index that also1605 * matches the stat information, and assume it's that other file!1606 */1607static inticase_exists(struct unpack_trees_options *o,const char*name,int len,struct stat *st)1608{1609const struct cache_entry *src;16101611 src =index_file_exists(o->src_index, name, len,1);1612return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);1613}16141615static intcheck_ok_to_remove(const char*name,int len,int dtype,1616const struct cache_entry *ce,struct stat *st,1617enum unpack_trees_error_types error_type,1618struct unpack_trees_options *o)1619{1620const struct cache_entry *result;16211622/*1623 * It may be that the 'lstat()' succeeded even though1624 * target 'ce' was absent, because there is an old1625 * entry that is different only in case..1626 *1627 * Ignore that lstat() if it matches.1628 */1629if(ignore_case &&icase_exists(o, name, len, st))1630return0;16311632if(o->dir &&1633is_excluded(o->dir, &the_index, name, &dtype))1634/*1635 * ce->name is explicitly excluded, so it is Ok to1636 * overwrite it.1637 */1638return0;1639if(S_ISDIR(st->st_mode)) {1640/*1641 * We are checking out path "foo" and1642 * found "foo/." in the working tree.1643 * This is tricky -- if we have modified1644 * files that are in "foo/" we would lose1645 * them.1646 */1647if(verify_clean_subdirectory(ce, error_type, o) <0)1648return-1;1649return0;1650}16511652/*1653 * The previous round may already have decided to1654 * delete this path, which is in a subdirectory that1655 * is being replaced with a blob.1656 */1657 result =index_file_exists(&o->result, name, len,0);1658if(result) {1659if(result->ce_flags & CE_REMOVE)1660return0;1661}16621663return o->gently ? -1:1664add_rejected_path(o, error_type, name);1665}16661667/*1668 * We do not want to remove or overwrite a working tree file that1669 * is not tracked, unless it is ignored.1670 */1671static intverify_absent_1(const struct cache_entry *ce,1672enum unpack_trees_error_types error_type,1673struct unpack_trees_options *o)1674{1675int len;1676struct stat st;16771678if(o->index_only || o->reset || !o->update)1679return0;16801681 len =check_leading_path(ce->name,ce_namelen(ce));1682if(!len)1683return0;1684else if(len >0) {1685char*path;1686int ret;16871688 path =xmemdupz(ce->name, len);1689if(lstat(path, &st))1690 ret =error_errno("cannot stat '%s'", path);1691else{1692if(submodule_from_ce(ce))1693 ret =check_submodule_move_head(ce,1694oid_to_hex(&ce->oid),1695 NULL, o);1696else1697 ret =check_ok_to_remove(path, len, DT_UNKNOWN, NULL,1698&st, error_type, o);1699}1700free(path);1701return ret;1702}else if(lstat(ce->name, &st)) {1703if(errno != ENOENT)1704returnerror_errno("cannot stat '%s'", ce->name);1705return0;1706}else{1707if(submodule_from_ce(ce))1708returncheck_submodule_move_head(ce,oid_to_hex(&ce->oid),1709 NULL, o);17101711returncheck_ok_to_remove(ce->name,ce_namelen(ce),1712ce_to_dtype(ce), ce, &st,1713 error_type, o);1714}1715}17161717static intverify_absent(const struct cache_entry *ce,1718enum unpack_trees_error_types error_type,1719struct unpack_trees_options *o)1720{1721if(!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))1722return0;1723returnverify_absent_1(ce, error_type, o);1724}17251726static intverify_absent_sparse(const struct cache_entry *ce,1727enum unpack_trees_error_types error_type,1728struct unpack_trees_options *o)1729{1730enum unpack_trees_error_types orphaned_error = error_type;1731if(orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)1732 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;17331734returnverify_absent_1(ce, orphaned_error, o);1735}17361737static intmerged_entry(const struct cache_entry *ce,1738const struct cache_entry *old,1739struct unpack_trees_options *o)1740{1741int update = CE_UPDATE;1742struct cache_entry *merge =dup_entry(ce);17431744if(!old) {1745/*1746 * New index entries. In sparse checkout, the following1747 * verify_absent() will be delayed until after1748 * traverse_trees() finishes in unpack_trees(), then:1749 *1750 * - CE_NEW_SKIP_WORKTREE will be computed correctly1751 * - verify_absent() be called again, this time with1752 * correct CE_NEW_SKIP_WORKTREE1753 *1754 * verify_absent() call here does nothing in sparse1755 * checkout (i.e. o->skip_sparse_checkout == 0)1756 */1757 update |= CE_ADDED;1758 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;17591760if(verify_absent(merge,1761 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {1762free(merge);1763return-1;1764}1765invalidate_ce_path(merge, o);17661767if(submodule_from_ce(ce)) {1768int ret =check_submodule_move_head(ce, NULL,1769oid_to_hex(&ce->oid),1770 o);1771if(ret)1772return ret;1773}17741775}else if(!(old->ce_flags & CE_CONFLICTED)) {1776/*1777 * See if we can re-use the old CE directly?1778 * That way we get the uptodate stat info.1779 *1780 * This also removes the UPDATE flag on a match; otherwise1781 * we will end up overwriting local changes in the work tree.1782 */1783if(same(old, merge)) {1784copy_cache_entry(merge, old);1785 update =0;1786}else{1787if(verify_uptodate(old, o)) {1788free(merge);1789return-1;1790}1791/* Migrate old flags over */1792 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1793invalidate_ce_path(old, o);1794}17951796if(submodule_from_ce(ce)) {1797int ret =check_submodule_move_head(ce,oid_to_hex(&old->oid),1798oid_to_hex(&ce->oid),1799 o);1800if(ret)1801return ret;1802}1803}else{1804/*1805 * Previously unmerged entry left as an existence1806 * marker by read_index_unmerged();1807 */1808invalidate_ce_path(old, o);1809}18101811do_add_entry(o, merge, update, CE_STAGEMASK);1812return1;1813}18141815static intdeleted_entry(const struct cache_entry *ce,1816const struct cache_entry *old,1817struct unpack_trees_options *o)1818{1819/* Did it exist in the index? */1820if(!old) {1821if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1822return-1;1823return0;1824}1825if(!(old->ce_flags & CE_CONFLICTED) &&verify_uptodate(old, o))1826return-1;1827add_entry(o, ce, CE_REMOVE,0);1828invalidate_ce_path(ce, o);1829return1;1830}18311832static intkeep_entry(const struct cache_entry *ce,1833struct unpack_trees_options *o)1834{1835add_entry(o, ce,0,0);1836return1;1837}18381839#if DBRT_DEBUG1840static voidshow_stage_entry(FILE*o,1841const char*label,const struct cache_entry *ce)1842{1843if(!ce)1844fprintf(o,"%s(missing)\n", label);1845else1846fprintf(o,"%s%06o%s %d\t%s\n",1847 label,1848 ce->ce_mode,1849oid_to_hex(&ce->oid),1850ce_stage(ce),1851 ce->name);1852}1853#endif18541855intthreeway_merge(const struct cache_entry *const*stages,1856struct unpack_trees_options *o)1857{1858const struct cache_entry *index;1859const struct cache_entry *head;1860const struct cache_entry *remote = stages[o->head_idx +1];1861int count;1862int head_match =0;1863int remote_match =0;18641865int df_conflict_head =0;1866int df_conflict_remote =0;18671868int any_anc_missing =0;1869int no_anc_exists =1;1870int i;18711872for(i =1; i < o->head_idx; i++) {1873if(!stages[i] || stages[i] == o->df_conflict_entry)1874 any_anc_missing =1;1875else1876 no_anc_exists =0;1877}18781879 index = stages[0];1880 head = stages[o->head_idx];18811882if(head == o->df_conflict_entry) {1883 df_conflict_head =1;1884 head = NULL;1885}18861887if(remote == o->df_conflict_entry) {1888 df_conflict_remote =1;1889 remote = NULL;1890}18911892/*1893 * First, if there's a #16 situation, note that to prevent #131894 * and #14.1895 */1896if(!same(remote, head)) {1897for(i =1; i < o->head_idx; i++) {1898if(same(stages[i], head)) {1899 head_match = i;1900}1901if(same(stages[i], remote)) {1902 remote_match = i;1903}1904}1905}19061907/*1908 * We start with cases where the index is allowed to match1909 * something other than the head: #14(ALT) and #2ALT, where it1910 * is permitted to match the result instead.1911 */1912/* #14, #14ALT, #2ALT */1913if(remote && !df_conflict_head && head_match && !remote_match) {1914if(index && !same(index, remote) && !same(index, head))1915returnreject_merge(index, o);1916returnmerged_entry(remote, index, o);1917}1918/*1919 * If we have an entry in the index cache, then we want to1920 * make sure that it matches head.1921 */1922if(index && !same(index, head))1923returnreject_merge(index, o);19241925if(head) {1926/* #5ALT, #15 */1927if(same(head, remote))1928returnmerged_entry(head, index, o);1929/* #13, #3ALT */1930if(!df_conflict_remote && remote_match && !head_match)1931returnmerged_entry(head, index, o);1932}19331934/* #1 */1935if(!head && !remote && any_anc_missing)1936return0;19371938/*1939 * Under the "aggressive" rule, we resolve mostly trivial1940 * cases that we historically had git-merge-one-file resolve.1941 */1942if(o->aggressive) {1943int head_deleted = !head;1944int remote_deleted = !remote;1945const struct cache_entry *ce = NULL;19461947if(index)1948 ce = index;1949else if(head)1950 ce = head;1951else if(remote)1952 ce = remote;1953else{1954for(i =1; i < o->head_idx; i++) {1955if(stages[i] && stages[i] != o->df_conflict_entry) {1956 ce = stages[i];1957break;1958}1959}1960}19611962/*1963 * Deleted in both.1964 * Deleted in one and unchanged in the other.1965 */1966if((head_deleted && remote_deleted) ||1967(head_deleted && remote && remote_match) ||1968(remote_deleted && head && head_match)) {1969if(index)1970returndeleted_entry(index, index, o);1971if(ce && !head_deleted) {1972if(verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))1973return-1;1974}1975return0;1976}1977/*1978 * Added in both, identically.1979 */1980if(no_anc_exists && head && remote &&same(head, remote))1981returnmerged_entry(head, index, o);19821983}19841985/* Below are "no merge" cases, which require that the index be1986 * up-to-date to avoid the files getting overwritten with1987 * conflict resolution files.1988 */1989if(index) {1990if(verify_uptodate(index, o))1991return-1;1992}19931994 o->nontrivial_merge =1;19951996/* #2, #3, #4, #6, #7, #9, #10, #11. */1997 count =0;1998if(!head_match || !remote_match) {1999for(i =1; i < o->head_idx; i++) {2000if(stages[i] && stages[i] != o->df_conflict_entry) {2001keep_entry(stages[i], o);2002 count++;2003break;2004}2005}2006}2007#if DBRT_DEBUG2008else{2009fprintf(stderr,"read-tree: warning #16 detected\n");2010show_stage_entry(stderr,"head ", stages[head_match]);2011show_stage_entry(stderr,"remote ", stages[remote_match]);2012}2013#endif2014if(head) { count +=keep_entry(head, o); }2015if(remote) { count +=keep_entry(remote, o); }2016return count;2017}20182019/*2020 * Two-way merge.2021 *2022 * The rule is to "carry forward" what is in the index without losing2023 * information across a "fast-forward", favoring a successful merge2024 * over a merge failure when it makes sense. For details of the2025 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.2026 *2027 */2028inttwoway_merge(const struct cache_entry *const*src,2029struct unpack_trees_options *o)2030{2031const struct cache_entry *current = src[0];2032const struct cache_entry *oldtree = src[1];2033const struct cache_entry *newtree = src[2];20342035if(o->merge_size !=2)2036returnerror("Cannot do a twoway merge of%dtrees",2037 o->merge_size);20382039if(oldtree == o->df_conflict_entry)2040 oldtree = NULL;2041if(newtree == o->df_conflict_entry)2042 newtree = NULL;20432044if(current) {2045if(current->ce_flags & CE_CONFLICTED) {2046if(same(oldtree, newtree) || o->reset) {2047if(!newtree)2048returndeleted_entry(current, current, o);2049else2050returnmerged_entry(newtree, current, o);2051}2052returnreject_merge(current, o);2053}else if((!oldtree && !newtree) ||/* 4 and 5 */2054(!oldtree && newtree &&2055same(current, newtree)) ||/* 6 and 7 */2056(oldtree && newtree &&2057same(oldtree, newtree)) ||/* 14 and 15 */2058(oldtree && newtree &&2059!same(oldtree, newtree) &&/* 18 and 19 */2060same(current, newtree))) {2061returnkeep_entry(current, o);2062}else if(oldtree && !newtree &&same(current, oldtree)) {2063/* 10 or 11 */2064returndeleted_entry(oldtree, current, o);2065}else if(oldtree && newtree &&2066same(current, oldtree) && !same(current, newtree)) {2067/* 20 or 21 */2068returnmerged_entry(newtree, current, o);2069}else2070returnreject_merge(current, o);2071}2072else if(newtree) {2073if(oldtree && !o->initial_checkout) {2074/*2075 * deletion of the path was staged;2076 */2077if(same(oldtree, newtree))2078return1;2079returnreject_merge(oldtree, o);2080}2081returnmerged_entry(newtree, current, o);2082}2083returndeleted_entry(oldtree, current, o);2084}20852086/*2087 * Bind merge.2088 *2089 * Keep the index entries at stage0, collapse stage1 but make sure2090 * stage0 does not have anything there.2091 */2092intbind_merge(const struct cache_entry *const*src,2093struct unpack_trees_options *o)2094{2095const struct cache_entry *old = src[0];2096const struct cache_entry *a = src[1];20972098if(o->merge_size !=1)2099returnerror("Cannot do a bind merge of%dtrees",2100 o->merge_size);2101if(a && old)2102return o->gently ? -1:2103error(ERRORMSG(o, ERROR_BIND_OVERLAP),2104super_prefixed(a->name),2105super_prefixed(old->name));2106if(!a)2107returnkeep_entry(old, o);2108else2109returnmerged_entry(a, NULL, o);2110}21112112/*2113 * One-way merge.2114 *2115 * The rule is:2116 * - take the stat information from stage0, take the data from stage12117 */2118intoneway_merge(const struct cache_entry *const*src,2119struct unpack_trees_options *o)2120{2121const struct cache_entry *old = src[0];2122const struct cache_entry *a = src[1];21232124if(o->merge_size !=1)2125returnerror("Cannot do a oneway merge of%dtrees",2126 o->merge_size);21272128if(!a || a == o->df_conflict_entry)2129returndeleted_entry(old, old, o);21302131if(old &&same(old, a)) {2132int update =0;2133if(o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {2134struct stat st;2135if(lstat(old->name, &st) ||2136ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))2137 update |= CE_UPDATE;2138}2139add_entry(o, old, update,0);2140return0;2141}2142returnmerged_entry(a, old, o);2143}