1#define USE_THE_INDEX_COMPATIBILITY_MACROS 2#include"builtin.h" 3#include"config.h" 4#include"checkout.h" 5#include"lockfile.h" 6#include"parse-options.h" 7#include"refs.h" 8#include"object-store.h" 9#include"commit.h" 10#include"tree.h" 11#include"tree-walk.h" 12#include"cache-tree.h" 13#include"unpack-trees.h" 14#include"dir.h" 15#include"run-command.h" 16#include"merge-recursive.h" 17#include"branch.h" 18#include"diff.h" 19#include"revision.h" 20#include"remote.h" 21#include"blob.h" 22#include"xdiff-interface.h" 23#include"ll-merge.h" 24#include"resolve-undo.h" 25#include"submodule-config.h" 26#include"submodule.h" 27#include"advice.h" 28 29static int checkout_optimize_new_branch; 30 31static const char*const checkout_usage[] = { 32N_("git checkout [<options>] <branch>"), 33N_("git checkout [<options>] [<branch>] -- <file>..."), 34 NULL, 35}; 36 37struct checkout_opts { 38int patch_mode; 39int quiet; 40int merge; 41int force; 42int force_detach; 43int writeout_stage; 44int overwrite_ignore; 45int ignore_skipworktree; 46int ignore_other_worktrees; 47int show_progress; 48int count_checkout_paths; 49int overlay_mode; 50/* 51 * If new checkout options are added, skip_merge_working_tree 52 * should be updated accordingly. 53 */ 54 55const char*new_branch; 56const char*new_branch_force; 57const char*new_orphan_branch; 58int new_branch_log; 59enum branch_track track; 60struct diff_options diff_options; 61 62int branch_exists; 63const char*prefix; 64struct pathspec pathspec; 65struct tree *source_tree; 66}; 67 68static intpost_checkout_hook(struct commit *old_commit,struct commit *new_commit, 69int changed) 70{ 71returnrun_hook_le(NULL,"post-checkout", 72oid_to_hex(old_commit ? &old_commit->object.oid : &null_oid), 73oid_to_hex(new_commit ? &new_commit->object.oid : &null_oid), 74 changed ?"1":"0", NULL); 75/* "new_commit" can be NULL when checking out from the index before 76 a commit exists. */ 77 78} 79 80static intupdate_some(const struct object_id *oid,struct strbuf *base, 81const char*pathname,unsigned mode,int stage,void*context) 82{ 83int len; 84struct cache_entry *ce; 85int pos; 86 87if(S_ISDIR(mode)) 88return READ_TREE_RECURSIVE; 89 90 len = base->len +strlen(pathname); 91 ce =make_empty_cache_entry(&the_index, len); 92oidcpy(&ce->oid, oid); 93memcpy(ce->name, base->buf, base->len); 94memcpy(ce->name + base->len, pathname, len - base->len); 95 ce->ce_flags =create_ce_flags(0) | CE_UPDATE; 96 ce->ce_namelen = len; 97 ce->ce_mode =create_ce_mode(mode); 98 99/* 100 * If the entry is the same as the current index, we can leave the old 101 * entry in place. Whether it is UPTODATE or not, checkout_entry will 102 * do the right thing. 103 */ 104 pos =cache_name_pos(ce->name, ce->ce_namelen); 105if(pos >=0) { 106struct cache_entry *old = active_cache[pos]; 107if(ce->ce_mode == old->ce_mode && 108oideq(&ce->oid, &old->oid)) { 109 old->ce_flags |= CE_UPDATE; 110discard_cache_entry(ce); 111return0; 112} 113} 114 115add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 116return0; 117} 118 119static intread_tree_some(struct tree *tree,const struct pathspec *pathspec) 120{ 121read_tree_recursive(the_repository, tree,"",0,0, 122 pathspec, update_some, NULL); 123 124/* update the index with the given tree's info 125 * for all args, expanding wildcards, and exit 126 * with any non-zero return code. 127 */ 128return0; 129} 130 131static intskip_same_name(const struct cache_entry *ce,int pos) 132{ 133while(++pos < active_nr && 134!strcmp(active_cache[pos]->name, ce->name)) 135;/* skip */ 136return pos; 137} 138 139static intcheck_stage(int stage,const struct cache_entry *ce,int pos, 140int overlay_mode) 141{ 142while(pos < active_nr && 143!strcmp(active_cache[pos]->name, ce->name)) { 144if(ce_stage(active_cache[pos]) == stage) 145return0; 146 pos++; 147} 148if(!overlay_mode) 149return0; 150if(stage ==2) 151returnerror(_("path '%s' does not have our version"), ce->name); 152else 153returnerror(_("path '%s' does not have their version"), ce->name); 154} 155 156static intcheck_stages(unsigned stages,const struct cache_entry *ce,int pos) 157{ 158unsigned seen =0; 159const char*name = ce->name; 160 161while(pos < active_nr) { 162 ce = active_cache[pos]; 163if(strcmp(name, ce->name)) 164break; 165 seen |= (1<<ce_stage(ce)); 166 pos++; 167} 168if((stages & seen) != stages) 169returnerror(_("path '%s' does not have all necessary versions"), 170 name); 171return0; 172} 173 174static intcheckout_stage(int stage,const struct cache_entry *ce,int pos, 175const struct checkout *state,int*nr_checkouts, 176int overlay_mode) 177{ 178while(pos < active_nr && 179!strcmp(active_cache[pos]->name, ce->name)) { 180if(ce_stage(active_cache[pos]) == stage) 181returncheckout_entry(active_cache[pos], state, 182 NULL, nr_checkouts); 183 pos++; 184} 185if(!overlay_mode) { 186unlink_entry(ce); 187return0; 188} 189if(stage ==2) 190returnerror(_("path '%s' does not have our version"), ce->name); 191else 192returnerror(_("path '%s' does not have their version"), ce->name); 193} 194 195static intcheckout_merged(int pos,const struct checkout *state,int*nr_checkouts) 196{ 197struct cache_entry *ce = active_cache[pos]; 198const char*path = ce->name; 199 mmfile_t ancestor, ours, theirs; 200int status; 201struct object_id oid; 202 mmbuffer_t result_buf; 203struct object_id threeway[3]; 204unsigned mode =0; 205 206memset(threeway,0,sizeof(threeway)); 207while(pos < active_nr) { 208int stage; 209 stage =ce_stage(ce); 210if(!stage ||strcmp(path, ce->name)) 211break; 212oidcpy(&threeway[stage -1], &ce->oid); 213if(stage ==2) 214 mode =create_ce_mode(ce->ce_mode); 215 pos++; 216 ce = active_cache[pos]; 217} 218if(is_null_oid(&threeway[1]) ||is_null_oid(&threeway[2])) 219returnerror(_("path '%s' does not have necessary versions"), path); 220 221read_mmblob(&ancestor, &threeway[0]); 222read_mmblob(&ours, &threeway[1]); 223read_mmblob(&theirs, &threeway[2]); 224 225/* 226 * NEEDSWORK: re-create conflicts from merges with 227 * merge.renormalize set, too 228 */ 229 status =ll_merge(&result_buf, path, &ancestor,"base", 230&ours,"ours", &theirs,"theirs", 231 state->istate, NULL); 232free(ancestor.ptr); 233free(ours.ptr); 234free(theirs.ptr); 235if(status <0|| !result_buf.ptr) { 236free(result_buf.ptr); 237returnerror(_("path '%s': cannot merge"), path); 238} 239 240/* 241 * NEEDSWORK: 242 * There is absolutely no reason to write this as a blob object 243 * and create a phony cache entry. This hack is primarily to get 244 * to the write_entry() machinery that massages the contents to 245 * work-tree format and writes out which only allows it for a 246 * cache entry. The code in write_entry() needs to be refactored 247 * to allow us to feed a <buffer, size, mode> instead of a cache 248 * entry. Such a refactoring would help merge_recursive as well 249 * (it also writes the merge result to the object database even 250 * when it may contain conflicts). 251 */ 252if(write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid)) 253die(_("Unable to add merge result for '%s'"), path); 254free(result_buf.ptr); 255 ce =make_transient_cache_entry(mode, &oid, path,2); 256if(!ce) 257die(_("make_cache_entry failed for path '%s'"), path); 258 status =checkout_entry(ce, state, NULL, nr_checkouts); 259discard_cache_entry(ce); 260return status; 261} 262 263static voidmark_ce_for_checkout_overlay(struct cache_entry *ce, 264char*ps_matched, 265const struct checkout_opts *opts) 266{ 267 ce->ce_flags &= ~CE_MATCHED; 268if(!opts->ignore_skipworktree &&ce_skip_worktree(ce)) 269return; 270if(opts->source_tree && !(ce->ce_flags & CE_UPDATE)) 271/* 272 * "git checkout tree-ish -- path", but this entry 273 * is in the original index but is not in tree-ish 274 * or does not match the pathspec; it will not be 275 * checked out to the working tree. We will not do 276 * anything to this entry at all. 277 */ 278return; 279/* 280 * Either this entry came from the tree-ish we are 281 * checking the paths out of, or we are checking out 282 * of the index. 283 * 284 * If it comes from the tree-ish, we already know it 285 * matches the pathspec and could just stamp 286 * CE_MATCHED to it from update_some(). But we still 287 * need ps_matched and read_tree_recursive (and 288 * eventually tree_entry_interesting) cannot fill 289 * ps_matched yet. Once it can, we can avoid calling 290 * match_pathspec() for _all_ entries when 291 * opts->source_tree != NULL. 292 */ 293if(ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) 294 ce->ce_flags |= CE_MATCHED; 295} 296 297static voidmark_ce_for_checkout_no_overlay(struct cache_entry *ce, 298char*ps_matched, 299const struct checkout_opts *opts) 300{ 301 ce->ce_flags &= ~CE_MATCHED; 302if(!opts->ignore_skipworktree &&ce_skip_worktree(ce)) 303return; 304if(ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) { 305 ce->ce_flags |= CE_MATCHED; 306if(opts->source_tree && !(ce->ce_flags & CE_UPDATE)) 307/* 308 * In overlay mode, but the path is not in 309 * tree-ish, which means we should remove it 310 * from the index and the working tree. 311 */ 312 ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE; 313} 314} 315 316static intcheckout_paths(const struct checkout_opts *opts, 317const char*revision) 318{ 319int pos; 320struct checkout state = CHECKOUT_INIT; 321static char*ps_matched; 322struct object_id rev; 323struct commit *head; 324int errs =0; 325struct lock_file lock_file = LOCK_INIT; 326int nr_checkouts =0, nr_unmerged =0; 327 328if(opts->track != BRANCH_TRACK_UNSPECIFIED) 329die(_("'%s' cannot be used with updating paths"),"--track"); 330 331if(opts->new_branch_log) 332die(_("'%s' cannot be used with updating paths"),"-l"); 333 334if(opts->force && opts->patch_mode) 335die(_("'%s' cannot be used with updating paths"),"-f"); 336 337if(opts->force_detach) 338die(_("'%s' cannot be used with updating paths"),"--detach"); 339 340if(opts->merge && opts->patch_mode) 341die(_("'%s' cannot be used with%s"),"--merge","--patch"); 342 343if(opts->force && opts->merge) 344die(_("'%s' cannot be used with%s"),"-f","-m"); 345 346if(opts->new_branch) 347die(_("Cannot update paths and switch to branch '%s' at the same time."), 348 opts->new_branch); 349 350if(opts->patch_mode) 351returnrun_add_interactive(revision,"--patch=checkout", 352&opts->pathspec); 353 354repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); 355if(read_cache_preload(&opts->pathspec) <0) 356returnerror(_("index file corrupt")); 357 358if(opts->source_tree) 359read_tree_some(opts->source_tree, &opts->pathspec); 360 361 ps_matched =xcalloc(opts->pathspec.nr,1); 362 363/* 364 * Make sure all pathspecs participated in locating the paths 365 * to be checked out. 366 */ 367for(pos =0; pos < active_nr; pos++) 368if(opts->overlay_mode) 369mark_ce_for_checkout_overlay(active_cache[pos], 370 ps_matched, 371 opts); 372else 373mark_ce_for_checkout_no_overlay(active_cache[pos], 374 ps_matched, 375 opts); 376 377if(report_path_error(ps_matched, &opts->pathspec, opts->prefix)) { 378free(ps_matched); 379return1; 380} 381free(ps_matched); 382 383/* "checkout -m path" to recreate conflicted state */ 384if(opts->merge) 385unmerge_marked_index(&the_index); 386 387/* Any unmerged paths? */ 388for(pos =0; pos < active_nr; pos++) { 389const struct cache_entry *ce = active_cache[pos]; 390if(ce->ce_flags & CE_MATCHED) { 391if(!ce_stage(ce)) 392continue; 393if(opts->force) { 394warning(_("path '%s' is unmerged"), ce->name); 395}else if(opts->writeout_stage) { 396 errs |=check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode); 397}else if(opts->merge) { 398 errs |=check_stages((1<<2) | (1<<3), ce, pos); 399}else{ 400 errs =1; 401error(_("path '%s' is unmerged"), ce->name); 402} 403 pos =skip_same_name(ce, pos) -1; 404} 405} 406if(errs) 407return1; 408 409/* Now we are committed to check them out */ 410 state.force =1; 411 state.refresh_cache =1; 412 state.istate = &the_index; 413 414enable_delayed_checkout(&state); 415for(pos =0; pos < active_nr; pos++) { 416struct cache_entry *ce = active_cache[pos]; 417if(ce->ce_flags & CE_MATCHED) { 418if(!ce_stage(ce)) { 419 errs |=checkout_entry(ce, &state, 420 NULL, &nr_checkouts); 421continue; 422} 423if(opts->writeout_stage) 424 errs |=checkout_stage(opts->writeout_stage, 425 ce, pos, 426&state, 427&nr_checkouts, opts->overlay_mode); 428else if(opts->merge) 429 errs |=checkout_merged(pos, &state, 430&nr_unmerged); 431 pos =skip_same_name(ce, pos) -1; 432} 433} 434remove_marked_cache_entries(&the_index,1); 435remove_scheduled_dirs(); 436 errs |=finish_delayed_checkout(&state, &nr_checkouts); 437 438if(opts->count_checkout_paths) { 439if(nr_unmerged) 440fprintf_ln(stderr,Q_("Recreated%dmerge conflict", 441"Recreated%dmerge conflicts", 442 nr_unmerged), 443 nr_unmerged); 444if(opts->source_tree) 445fprintf_ln(stderr,Q_("Updated%dpath from%s", 446"Updated%dpaths from%s", 447 nr_checkouts), 448 nr_checkouts, 449find_unique_abbrev(&opts->source_tree->object.oid, 450 DEFAULT_ABBREV)); 451else if(!nr_unmerged || nr_checkouts) 452fprintf_ln(stderr,Q_("Updated%dpath from the index", 453"Updated%dpaths from the index", 454 nr_checkouts), 455 nr_checkouts); 456} 457 458if(write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) 459die(_("unable to write new index file")); 460 461read_ref_full("HEAD",0, &rev, NULL); 462 head =lookup_commit_reference_gently(the_repository, &rev,1); 463 464 errs |=post_checkout_hook(head, head,0); 465return errs; 466} 467 468static voidshow_local_changes(struct object *head, 469const struct diff_options *opts) 470{ 471struct rev_info rev; 472/* I think we want full paths, even if we're in a subdirectory. */ 473repo_init_revisions(the_repository, &rev, NULL); 474 rev.diffopt.flags = opts->flags; 475 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; 476diff_setup_done(&rev.diffopt); 477add_pending_object(&rev, head, NULL); 478run_diff_index(&rev,0); 479} 480 481static voiddescribe_detached_head(const char*msg,struct commit *commit) 482{ 483struct strbuf sb = STRBUF_INIT; 484 485if(!parse_commit(commit)) 486pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); 487if(print_sha1_ellipsis()) { 488fprintf(stderr,"%s %s...%s\n", msg, 489find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); 490}else{ 491fprintf(stderr,"%s %s %s\n", msg, 492find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); 493} 494strbuf_release(&sb); 495} 496 497static intreset_tree(struct tree *tree,const struct checkout_opts *o, 498int worktree,int*writeout_error) 499{ 500struct unpack_trees_options opts; 501struct tree_desc tree_desc; 502 503memset(&opts,0,sizeof(opts)); 504 opts.head_idx = -1; 505 opts.update = worktree; 506 opts.skip_unmerged = !worktree; 507 opts.reset =1; 508 opts.merge =1; 509 opts.fn = oneway_merge; 510 opts.verbose_update = o->show_progress; 511 opts.src_index = &the_index; 512 opts.dst_index = &the_index; 513parse_tree(tree); 514init_tree_desc(&tree_desc, tree->buffer, tree->size); 515switch(unpack_trees(1, &tree_desc, &opts)) { 516case-2: 517*writeout_error =1; 518/* 519 * We return 0 nevertheless, as the index is all right 520 * and more importantly we have made best efforts to 521 * update paths in the work tree, and we cannot revert 522 * them. 523 */ 524/* fallthrough */ 525case0: 526return0; 527default: 528return128; 529} 530} 531 532struct branch_info { 533const char*name;/* The short name used */ 534const char*path;/* The full name of a real branch */ 535struct commit *commit;/* The named commit */ 536/* 537 * if not null the branch is detached because it's already 538 * checked out in this checkout 539 */ 540char*checkout; 541}; 542 543static voidsetup_branch_path(struct branch_info *branch) 544{ 545struct strbuf buf = STRBUF_INIT; 546 547strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL); 548if(strcmp(buf.buf, branch->name)) 549 branch->name =xstrdup(buf.buf); 550strbuf_splice(&buf,0,0,"refs/heads/",11); 551 branch->path =strbuf_detach(&buf, NULL); 552} 553 554/* 555 * Skip merging the trees, updating the index and working directory if and 556 * only if we are creating a new branch via "git checkout -b <new_branch>." 557 */ 558static intskip_merge_working_tree(const struct checkout_opts *opts, 559const struct branch_info *old_branch_info, 560const struct branch_info *new_branch_info) 561{ 562/* 563 * Do the merge if sparse checkout is on and the user has not opted in 564 * to the optimized behavior 565 */ 566if(core_apply_sparse_checkout && !checkout_optimize_new_branch) 567return0; 568 569/* 570 * We must do the merge if we are actually moving to a new commit. 571 */ 572if(!old_branch_info->commit || !new_branch_info->commit || 573!oideq(&old_branch_info->commit->object.oid, 574&new_branch_info->commit->object.oid)) 575return0; 576 577/* 578 * opts->patch_mode cannot be used with switching branches so is 579 * not tested here 580 */ 581 582/* 583 * opts->quiet only impacts output so doesn't require a merge 584 */ 585 586/* 587 * Honor the explicit request for a three-way merge or to throw away 588 * local changes 589 */ 590if(opts->merge || opts->force) 591return0; 592 593/* 594 * --detach is documented as "updating the index and the files in the 595 * working tree" but this optimization skips those steps so fall through 596 * to the regular code path. 597 */ 598if(opts->force_detach) 599return0; 600 601/* 602 * opts->writeout_stage cannot be used with switching branches so is 603 * not tested here 604 */ 605 606/* 607 * Honor the explicit ignore requests 608 */ 609if(!opts->overwrite_ignore || opts->ignore_skipworktree || 610 opts->ignore_other_worktrees) 611return0; 612 613/* 614 * opts->show_progress only impacts output so doesn't require a merge 615 */ 616 617/* 618 * opts->overlay_mode cannot be used with switching branches so is 619 * not tested here 620 */ 621 622/* 623 * If we aren't creating a new branch any changes or updates will 624 * happen in the existing branch. Since that could only be updating 625 * the index and working directory, we don't want to skip those steps 626 * or we've defeated any purpose in running the command. 627 */ 628if(!opts->new_branch) 629return0; 630 631/* 632 * new_branch_force is defined to "create/reset and checkout a branch" 633 * so needs to go through the merge to do the reset 634 */ 635if(opts->new_branch_force) 636return0; 637 638/* 639 * A new orphaned branch requrires the index and the working tree to be 640 * adjusted to <start_point> 641 */ 642if(opts->new_orphan_branch) 643return0; 644 645/* 646 * Remaining variables are not checkout options but used to track state 647 */ 648 649/* 650 * Do the merge if this is the initial checkout. We cannot use 651 * is_cache_unborn() here because the index hasn't been loaded yet 652 * so cache_nr and timestamp.sec are always zero. 653 */ 654if(!file_exists(get_index_file())) 655return0; 656 657return1; 658} 659 660static intmerge_working_tree(const struct checkout_opts *opts, 661struct branch_info *old_branch_info, 662struct branch_info *new_branch_info, 663int*writeout_error) 664{ 665int ret; 666struct lock_file lock_file = LOCK_INIT; 667 668hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); 669if(read_cache_preload(NULL) <0) 670returnerror(_("index file corrupt")); 671 672resolve_undo_clear(); 673if(opts->force) { 674 ret =reset_tree(get_commit_tree(new_branch_info->commit), 675 opts,1, writeout_error); 676if(ret) 677return ret; 678}else{ 679struct tree_desc trees[2]; 680struct tree *tree; 681struct unpack_trees_options topts; 682 683memset(&topts,0,sizeof(topts)); 684 topts.head_idx = -1; 685 topts.src_index = &the_index; 686 topts.dst_index = &the_index; 687 688setup_unpack_trees_porcelain(&topts,"checkout"); 689 690refresh_cache(REFRESH_QUIET); 691 692if(unmerged_cache()) { 693error(_("you need to resolve your current index first")); 694return1; 695} 696 697/* 2-way merge to the new branch */ 698 topts.initial_checkout =is_cache_unborn(); 699 topts.update =1; 700 topts.merge =1; 701 topts.gently = opts->merge && old_branch_info->commit; 702 topts.verbose_update = opts->show_progress; 703 topts.fn = twoway_merge; 704if(opts->overwrite_ignore) { 705 topts.dir =xcalloc(1,sizeof(*topts.dir)); 706 topts.dir->flags |= DIR_SHOW_IGNORED; 707setup_standard_excludes(topts.dir); 708} 709 tree =parse_tree_indirect(old_branch_info->commit ? 710&old_branch_info->commit->object.oid : 711 the_hash_algo->empty_tree); 712init_tree_desc(&trees[0], tree->buffer, tree->size); 713 tree =parse_tree_indirect(&new_branch_info->commit->object.oid); 714init_tree_desc(&trees[1], tree->buffer, tree->size); 715 716 ret =unpack_trees(2, trees, &topts); 717clear_unpack_trees_porcelain(&topts); 718if(ret == -1) { 719/* 720 * Unpack couldn't do a trivial merge; either 721 * give up or do a real merge, depending on 722 * whether the merge flag was used. 723 */ 724struct tree *result; 725struct tree *work; 726struct merge_options o; 727if(!opts->merge) 728return1; 729 730/* 731 * Without old_branch_info->commit, the below is the same as 732 * the two-tree unpack we already tried and failed. 733 */ 734if(!old_branch_info->commit) 735return1; 736 737/* Do more real merge */ 738 739/* 740 * We update the index fully, then write the 741 * tree from the index, then merge the new 742 * branch with the current tree, with the old 743 * branch as the base. Then we reset the index 744 * (but not the working tree) to the new 745 * branch, leaving the working tree as the 746 * merged version, but skipping unmerged 747 * entries in the index. 748 */ 749 750add_files_to_cache(NULL, NULL,0); 751/* 752 * NEEDSWORK: carrying over local changes 753 * when branches have different end-of-line 754 * normalization (or clean+smudge rules) is 755 * a pain; plumb in an option to set 756 * o.renormalize? 757 */ 758init_merge_options(&o, the_repository); 759 o.verbosity =0; 760 work =write_tree_from_memory(&o); 761 762 ret =reset_tree(get_commit_tree(new_branch_info->commit), 763 opts,1, 764 writeout_error); 765if(ret) 766return ret; 767 o.ancestor = old_branch_info->name; 768 o.branch1 = new_branch_info->name; 769 o.branch2 ="local"; 770 ret =merge_trees(&o, 771get_commit_tree(new_branch_info->commit), 772 work, 773get_commit_tree(old_branch_info->commit), 774&result); 775if(ret <0) 776exit(128); 777 ret =reset_tree(get_commit_tree(new_branch_info->commit), 778 opts,0, 779 writeout_error); 780strbuf_release(&o.obuf); 781if(ret) 782return ret; 783} 784} 785 786if(!active_cache_tree) 787 active_cache_tree =cache_tree(); 788 789if(!cache_tree_fully_valid(active_cache_tree)) 790cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); 791 792if(write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) 793die(_("unable to write new index file")); 794 795if(!opts->force && !opts->quiet) 796show_local_changes(&new_branch_info->commit->object, &opts->diff_options); 797 798return0; 799} 800 801static voidreport_tracking(struct branch_info *new_branch_info) 802{ 803struct strbuf sb = STRBUF_INIT; 804struct branch *branch =branch_get(new_branch_info->name); 805 806if(!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL)) 807return; 808fputs(sb.buf, stdout); 809strbuf_release(&sb); 810} 811 812static voidupdate_refs_for_switch(const struct checkout_opts *opts, 813struct branch_info *old_branch_info, 814struct branch_info *new_branch_info) 815{ 816struct strbuf msg = STRBUF_INIT; 817const char*old_desc, *reflog_msg; 818if(opts->new_branch) { 819if(opts->new_orphan_branch) { 820char*refname; 821 822 refname =mkpathdup("refs/heads/%s", opts->new_orphan_branch); 823if(opts->new_branch_log && 824!should_autocreate_reflog(refname)) { 825int ret; 826struct strbuf err = STRBUF_INIT; 827 828 ret =safe_create_reflog(refname,1, &err); 829if(ret) { 830fprintf(stderr,_("Can not do reflog for '%s':%s\n"), 831 opts->new_orphan_branch, err.buf); 832strbuf_release(&err); 833free(refname); 834return; 835} 836strbuf_release(&err); 837} 838free(refname); 839} 840else 841create_branch(the_repository, 842 opts->new_branch, new_branch_info->name, 843 opts->new_branch_force ?1:0, 844 opts->new_branch_force ?1:0, 845 opts->new_branch_log, 846 opts->quiet, 847 opts->track); 848 new_branch_info->name = opts->new_branch; 849setup_branch_path(new_branch_info); 850} 851 852 old_desc = old_branch_info->name; 853if(!old_desc && old_branch_info->commit) 854 old_desc =oid_to_hex(&old_branch_info->commit->object.oid); 855 856 reflog_msg =getenv("GIT_REFLOG_ACTION"); 857if(!reflog_msg) 858strbuf_addf(&msg,"checkout: moving from%sto%s", 859 old_desc ? old_desc :"(invalid)", new_branch_info->name); 860else 861strbuf_insert(&msg,0, reflog_msg,strlen(reflog_msg)); 862 863if(!strcmp(new_branch_info->name,"HEAD") && !new_branch_info->path && !opts->force_detach) { 864/* Nothing to do. */ 865}else if(opts->force_detach || !new_branch_info->path) {/* No longer on any branch. */ 866update_ref(msg.buf,"HEAD", &new_branch_info->commit->object.oid, NULL, 867 REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); 868if(!opts->quiet) { 869if(old_branch_info->path && 870 advice_detached_head && !opts->force_detach) 871detach_advice(new_branch_info->name); 872describe_detached_head(_("HEAD is now at"), new_branch_info->commit); 873} 874}else if(new_branch_info->path) {/* Switch branches. */ 875if(create_symref("HEAD", new_branch_info->path, msg.buf) <0) 876die(_("unable to update HEAD")); 877if(!opts->quiet) { 878if(old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { 879if(opts->new_branch_force) 880fprintf(stderr,_("Reset branch '%s'\n"), 881 new_branch_info->name); 882else 883fprintf(stderr,_("Already on '%s'\n"), 884 new_branch_info->name); 885}else if(opts->new_branch) { 886if(opts->branch_exists) 887fprintf(stderr,_("Switched to and reset branch '%s'\n"), new_branch_info->name); 888else 889fprintf(stderr,_("Switched to a new branch '%s'\n"), new_branch_info->name); 890}else{ 891fprintf(stderr,_("Switched to branch '%s'\n"), 892 new_branch_info->name); 893} 894} 895if(old_branch_info->path && old_branch_info->name) { 896if(!ref_exists(old_branch_info->path) &&reflog_exists(old_branch_info->path)) 897delete_reflog(old_branch_info->path); 898} 899} 900remove_branch_state(the_repository); 901strbuf_release(&msg); 902if(!opts->quiet && 903(new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name,"HEAD")))) 904report_tracking(new_branch_info); 905} 906 907static intadd_pending_uninteresting_ref(const char*refname, 908const struct object_id *oid, 909int flags,void*cb_data) 910{ 911add_pending_oid(cb_data, refname, oid, UNINTERESTING); 912return0; 913} 914 915static voiddescribe_one_orphan(struct strbuf *sb,struct commit *commit) 916{ 917strbuf_addstr(sb," "); 918strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV); 919strbuf_addch(sb,' '); 920if(!parse_commit(commit)) 921pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); 922strbuf_addch(sb,'\n'); 923} 924 925#define ORPHAN_CUTOFF 4 926static voidsuggest_reattach(struct commit *commit,struct rev_info *revs) 927{ 928struct commit *c, *last = NULL; 929struct strbuf sb = STRBUF_INIT; 930int lost =0; 931while((c =get_revision(revs)) != NULL) { 932if(lost < ORPHAN_CUTOFF) 933describe_one_orphan(&sb, c); 934 last = c; 935 lost++; 936} 937if(ORPHAN_CUTOFF < lost) { 938int more = lost - ORPHAN_CUTOFF; 939if(more ==1) 940describe_one_orphan(&sb, last); 941else 942strbuf_addf(&sb,_(" ... and%dmore.\n"), more); 943} 944 945fprintf(stderr, 946Q_( 947/* The singular version */ 948"Warning: you are leaving%dcommit behind, " 949"not connected to\n" 950"any of your branches:\n\n" 951"%s\n", 952/* The plural version */ 953"Warning: you are leaving%dcommits behind, " 954"not connected to\n" 955"any of your branches:\n\n" 956"%s\n", 957/* Give ngettext() the count */ 958 lost), 959 lost, 960 sb.buf); 961strbuf_release(&sb); 962 963if(advice_detached_head) 964fprintf(stderr, 965Q_( 966/* The singular version */ 967"If you want to keep it by creating a new branch, " 968"this may be a good time\nto do so with:\n\n" 969" git branch <new-branch-name>%s\n\n", 970/* The plural version */ 971"If you want to keep them by creating a new branch, " 972"this may be a good time\nto do so with:\n\n" 973" git branch <new-branch-name>%s\n\n", 974/* Give ngettext() the count */ 975 lost), 976find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); 977} 978 979/* 980 * We are about to leave commit that was at the tip of a detached 981 * HEAD. If it is not reachable from any ref, this is the last chance 982 * for the user to do so without resorting to reflog. 983 */ 984static voidorphaned_commit_warning(struct commit *old_commit,struct commit *new_commit) 985{ 986struct rev_info revs; 987struct object *object = &old_commit->object; 988 989repo_init_revisions(the_repository, &revs, NULL); 990setup_revisions(0, NULL, &revs, NULL); 991 992 object->flags &= ~UNINTERESTING; 993add_pending_object(&revs, object,oid_to_hex(&object->oid)); 994 995for_each_ref(add_pending_uninteresting_ref, &revs); 996add_pending_oid(&revs,"HEAD", &new_commit->object.oid, UNINTERESTING); 997 998if(prepare_revision_walk(&revs)) 999die(_("internal error in revision walk"));1000if(!(old_commit->object.flags & UNINTERESTING))1001suggest_reattach(old_commit, &revs);1002else1003describe_detached_head(_("Previous HEAD position was"), old_commit);10041005/* Clean up objects used, as they will be reused. */1006clear_commit_marks_all(ALL_REV_FLAGS);1007}10081009static intswitch_branches(const struct checkout_opts *opts,1010struct branch_info *new_branch_info)1011{1012int ret =0;1013struct branch_info old_branch_info;1014void*path_to_free;1015struct object_id rev;1016int flag, writeout_error =0;1017memset(&old_branch_info,0,sizeof(old_branch_info));1018 old_branch_info.path = path_to_free =resolve_refdup("HEAD",0, &rev, &flag);1019if(old_branch_info.path)1020 old_branch_info.commit =lookup_commit_reference_gently(the_repository, &rev,1);1021if(!(flag & REF_ISSYMREF))1022 old_branch_info.path = NULL;10231024if(old_branch_info.path)1025skip_prefix(old_branch_info.path,"refs/heads/", &old_branch_info.name);10261027if(!new_branch_info->name) {1028 new_branch_info->name ="HEAD";1029 new_branch_info->commit = old_branch_info.commit;1030if(!new_branch_info->commit)1031die(_("You are on a branch yet to be born"));1032parse_commit_or_die(new_branch_info->commit);1033}10341035/* optimize the "checkout -b <new_branch> path */1036if(skip_merge_working_tree(opts, &old_branch_info, new_branch_info)) {1037if(!checkout_optimize_new_branch && !opts->quiet) {1038if(read_cache_preload(NULL) <0)1039returnerror(_("index file corrupt"));1040show_local_changes(&new_branch_info->commit->object, &opts->diff_options);1041}1042}else{1043 ret =merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);1044if(ret) {1045free(path_to_free);1046return ret;1047}1048}10491050if(!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)1051orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit);10521053update_refs_for_switch(opts, &old_branch_info, new_branch_info);10541055 ret =post_checkout_hook(old_branch_info.commit, new_branch_info->commit,1);1056free(path_to_free);1057return ret || writeout_error;1058}10591060static intgit_checkout_config(const char*var,const char*value,void*cb)1061{1062if(!strcmp(var,"checkout.optimizenewbranch")) {1063 checkout_optimize_new_branch =git_config_bool(var, value);1064return0;1065}10661067if(!strcmp(var,"diff.ignoresubmodules")) {1068struct checkout_opts *opts = cb;1069handle_ignore_submodules_arg(&opts->diff_options, value);1070return0;1071}10721073if(starts_with(var,"submodule."))1074returngit_default_submodule_config(var, value, NULL);10751076returngit_xmerge_config(var, value, NULL);1077}10781079static intparse_branchname_arg(int argc,const char**argv,1080int dwim_new_local_branch_ok,1081struct branch_info *new_branch_info,1082struct checkout_opts *opts,1083struct object_id *rev,1084int*dwim_remotes_matched)1085{1086struct tree **source_tree = &opts->source_tree;1087const char**new_branch = &opts->new_branch;1088int argcount =0;1089struct object_id branch_rev;1090const char*arg;1091int dash_dash_pos;1092int has_dash_dash =0;1093int i;10941095/*1096 * case 1: git checkout <ref> -- [<paths>]1097 *1098 * <ref> must be a valid tree, everything after the '--' must be1099 * a path.1100 *1101 * case 2: git checkout -- [<paths>]1102 *1103 * everything after the '--' must be paths.1104 *1105 * case 3: git checkout <something> [--]1106 *1107 * (a) If <something> is a commit, that is to1108 * switch to the branch or detach HEAD at it. As a special case,1109 * if <something> is A...B (missing A or B means HEAD but you can1110 * omit at most one side), and if there is a unique merge base1111 * between A and B, A...B names that merge base.1112 *1113 * (b) If <something> is _not_ a commit, either "--" is present1114 * or <something> is not a path, no -t or -b was given, and1115 * and there is a tracking branch whose name is <something>1116 * in one and only one remote (or if the branch exists on the1117 * remote named in checkout.defaultRemote), then this is a1118 * short-hand to fork local <something> from that1119 * remote-tracking branch.1120 *1121 * (c) Otherwise, if "--" is present, treat it like case (1).1122 *1123 * (d) Otherwise :1124 * - if it's a reference, treat it like case (1)1125 * - else if it's a path, treat it like case (2)1126 * - else: fail.1127 *1128 * case 4: git checkout <something> <paths>1129 *1130 * The first argument must not be ambiguous.1131 * - If it's *only* a reference, treat it like case (1).1132 * - If it's only a path, treat it like case (2).1133 * - else: fail.1134 *1135 */1136if(!argc)1137return0;11381139 arg = argv[0];1140 dash_dash_pos = -1;1141for(i =0; i < argc; i++) {1142if(!strcmp(argv[i],"--")) {1143 dash_dash_pos = i;1144break;1145}1146}1147if(dash_dash_pos ==0)1148return1;/* case (2) */1149else if(dash_dash_pos ==1)1150 has_dash_dash =1;/* case (3) or (1) */1151else if(dash_dash_pos >=2)1152die(_("only one reference expected,%dgiven."), dash_dash_pos);1153 opts->count_checkout_paths = !opts->quiet && !has_dash_dash;11541155if(!strcmp(arg,"-"))1156 arg ="@{-1}";11571158if(get_oid_mb(arg, rev)) {1159/*1160 * Either case (3) or (4), with <something> not being1161 * a commit, or an attempt to use case (1) with an1162 * invalid ref.1163 *1164 * It's likely an error, but we need to find out if1165 * we should auto-create the branch, case (3).(b).1166 */1167int recover_with_dwim = dwim_new_local_branch_ok;11681169int could_be_checkout_paths = !has_dash_dash &&1170check_filename(opts->prefix, arg);11711172if(!has_dash_dash && !no_wildcard(arg))1173 recover_with_dwim =0;11741175/*1176 * Accept "git checkout foo" and "git checkout foo --"1177 * as candidates for dwim.1178 */1179if(!(argc ==1&& !has_dash_dash) &&1180!(argc ==2&& has_dash_dash))1181 recover_with_dwim =0;11821183if(recover_with_dwim) {1184const char*remote =unique_tracking_name(arg, rev,1185 dwim_remotes_matched);1186if(remote) {1187if(could_be_checkout_paths)1188die(_("'%s' could be both a local file and a tracking branch.\n"1189"Please use -- (and optionally --no-guess) to disambiguate"),1190 arg);1191*new_branch = arg;1192 arg = remote;1193/* DWIMmed to create local branch, case (3).(b) */1194}else{1195 recover_with_dwim =0;1196}1197}11981199if(!recover_with_dwim) {1200if(has_dash_dash)1201die(_("invalid reference:%s"), arg);1202return argcount;1203}1204}12051206/* we can't end up being in (2) anymore, eat the argument */1207 argcount++;1208 argv++;1209 argc--;12101211 new_branch_info->name = arg;1212setup_branch_path(new_branch_info);12131214if(!check_refname_format(new_branch_info->path,0) &&1215!read_ref(new_branch_info->path, &branch_rev))1216oidcpy(rev, &branch_rev);1217else1218 new_branch_info->path = NULL;/* not an existing branch */12191220 new_branch_info->commit =lookup_commit_reference_gently(the_repository, rev,1);1221if(!new_branch_info->commit) {1222/* not a commit */1223*source_tree =parse_tree_indirect(rev);1224}else{1225parse_commit_or_die(new_branch_info->commit);1226*source_tree =get_commit_tree(new_branch_info->commit);1227}12281229if(!*source_tree)/* case (1): want a tree */1230die(_("reference is not a tree:%s"), arg);1231if(!has_dash_dash) {/* case (3).(d) -> (1) */1232/*1233 * Do not complain the most common case1234 * git checkout branch1235 * even if there happen to be a file called 'branch';1236 * it would be extremely annoying.1237 */1238if(argc)1239verify_non_filename(opts->prefix, arg);1240}else{1241 argcount++;1242 argv++;1243 argc--;1244}12451246return argcount;1247}12481249static intswitch_unborn_to_new_branch(const struct checkout_opts *opts)1250{1251int status;1252struct strbuf branch_ref = STRBUF_INIT;12531254if(!opts->new_branch)1255die(_("You are on a branch yet to be born"));1256strbuf_addf(&branch_ref,"refs/heads/%s", opts->new_branch);1257 status =create_symref("HEAD", branch_ref.buf,"checkout -b");1258strbuf_release(&branch_ref);1259if(!opts->quiet)1260fprintf(stderr,_("Switched to a new branch '%s'\n"),1261 opts->new_branch);1262return status;1263}12641265static intcheckout_branch(struct checkout_opts *opts,1266struct branch_info *new_branch_info)1267{1268if(opts->pathspec.nr)1269die(_("paths cannot be used with switching branches"));12701271if(opts->patch_mode)1272die(_("'%s' cannot be used with switching branches"),1273"--patch");12741275if(!opts->overlay_mode)1276die(_("'%s' cannot be used with switching branches"),1277"--no-overlay");12781279if(opts->writeout_stage)1280die(_("'%s' cannot be used with switching branches"),1281"--ours/--theirs");12821283if(opts->force && opts->merge)1284die(_("'%s' cannot be used with '%s'"),"-f","-m");12851286if(opts->force_detach && opts->new_branch)1287die(_("'%s' cannot be used with '%s'"),1288"--detach","-b/-B/--orphan");12891290if(opts->new_orphan_branch) {1291if(opts->track != BRANCH_TRACK_UNSPECIFIED)1292die(_("'%s' cannot be used with '%s'"),"--orphan","-t");1293}else if(opts->force_detach) {1294if(opts->track != BRANCH_TRACK_UNSPECIFIED)1295die(_("'%s' cannot be used with '%s'"),"--detach","-t");1296}else if(opts->track == BRANCH_TRACK_UNSPECIFIED)1297 opts->track = git_branch_track;12981299if(new_branch_info->name && !new_branch_info->commit)1300die(_("Cannot switch branch to a non-commit '%s'"),1301 new_branch_info->name);13021303if(new_branch_info->path && !opts->force_detach && !opts->new_branch &&1304!opts->ignore_other_worktrees) {1305int flag;1306char*head_ref =resolve_refdup("HEAD",0, NULL, &flag);1307if(head_ref &&1308(!(flag & REF_ISSYMREF) ||strcmp(head_ref, new_branch_info->path)))1309die_if_checked_out(new_branch_info->path,1);1310free(head_ref);1311}13121313if(!new_branch_info->commit && opts->new_branch) {1314struct object_id rev;1315int flag;13161317if(!read_ref_full("HEAD",0, &rev, &flag) &&1318(flag & REF_ISSYMREF) &&is_null_oid(&rev))1319returnswitch_unborn_to_new_branch(opts);1320}1321returnswitch_branches(opts, new_branch_info);1322}13231324intcmd_checkout(int argc,const char**argv,const char*prefix)1325{1326struct checkout_opts opts;1327struct branch_info new_branch_info;1328char*conflict_style = NULL;1329int dwim_new_local_branch, no_dwim_new_local_branch =0;1330int dwim_remotes_matched =0;1331struct option options[] = {1332OPT__QUIET(&opts.quiet,N_("suppress progress reporting")),1333OPT_STRING('b', NULL, &opts.new_branch,N_("branch"),1334N_("create and checkout a new branch")),1335OPT_STRING('B', NULL, &opts.new_branch_force,N_("branch"),1336N_("create/reset and checkout a branch")),1337OPT_BOOL('l', NULL, &opts.new_branch_log,N_("create reflog for new branch")),1338OPT_BOOL(0,"detach", &opts.force_detach,N_("detach HEAD at named commit")),1339OPT_SET_INT('t',"track", &opts.track,N_("set upstream info for new branch"),1340 BRANCH_TRACK_EXPLICIT),1341OPT_STRING(0,"orphan", &opts.new_orphan_branch,N_("new-branch"),N_("new unparented branch")),1342OPT_SET_INT_F('2',"ours", &opts.writeout_stage,1343N_("checkout our version for unmerged files"),13442, PARSE_OPT_NONEG),1345OPT_SET_INT_F('3',"theirs", &opts.writeout_stage,1346N_("checkout their version for unmerged files"),13473, PARSE_OPT_NONEG),1348OPT__FORCE(&opts.force,N_("force checkout (throw away local modifications)"),1349 PARSE_OPT_NOCOMPLETE),1350OPT_BOOL('m',"merge", &opts.merge,N_("perform a 3-way merge with the new branch")),1351OPT_BOOL_F(0,"overwrite-ignore", &opts.overwrite_ignore,1352N_("update ignored files (default)"),1353 PARSE_OPT_NOCOMPLETE),1354OPT_STRING(0,"conflict", &conflict_style,N_("style"),1355N_("conflict style (merge or diff3)")),1356OPT_BOOL('p',"patch", &opts.patch_mode,N_("select hunks interactively")),1357OPT_BOOL(0,"ignore-skip-worktree-bits", &opts.ignore_skipworktree,1358N_("do not limit pathspecs to sparse entries only")),1359OPT_BOOL(0,"no-guess", &no_dwim_new_local_branch,1360N_("do not second guess 'git checkout <no-such-branch>'")),1361OPT_BOOL(0,"ignore-other-worktrees", &opts.ignore_other_worktrees,1362N_("do not check if another worktree is holding the given ref")),1363{ OPTION_CALLBACK,0,"recurse-submodules", NULL,1364"checkout","control recursive updating of submodules",1365 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },1366OPT_BOOL(0,"progress", &opts.show_progress,N_("force progress reporting")),1367OPT_BOOL(0,"overlay", &opts.overlay_mode,N_("use overlay mode (default)")),1368OPT_END(),1369};13701371memset(&opts,0,sizeof(opts));1372memset(&new_branch_info,0,sizeof(new_branch_info));1373 opts.overwrite_ignore =1;1374 opts.prefix = prefix;1375 opts.show_progress = -1;1376 opts.overlay_mode = -1;13771378git_config(git_checkout_config, &opts);13791380 opts.track = BRANCH_TRACK_UNSPECIFIED;13811382 argc =parse_options(argc, argv, prefix, options, checkout_usage,1383 PARSE_OPT_KEEP_DASHDASH);13841385 dwim_new_local_branch = !no_dwim_new_local_branch;1386if(opts.show_progress <0) {1387if(opts.quiet)1388 opts.show_progress =0;1389else1390 opts.show_progress =isatty(2);1391}13921393if(conflict_style) {1394 opts.merge =1;/* implied */1395git_xmerge_config("merge.conflictstyle", conflict_style, NULL);1396}13971398if((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) >1)1399die(_("-b, -B and --orphan are mutually exclusive"));14001401if(opts.overlay_mode ==1&& opts.patch_mode)1402die(_("-p and --overlay are mutually exclusive"));14031404/*1405 * From here on, new_branch will contain the branch to be checked out,1406 * and new_branch_force and new_orphan_branch will tell us which one of1407 * -b/-B/--orphan is being used.1408 */1409if(opts.new_branch_force)1410 opts.new_branch = opts.new_branch_force;14111412if(opts.new_orphan_branch)1413 opts.new_branch = opts.new_orphan_branch;14141415/* --track without -b/-B/--orphan should DWIM */1416if(opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) {1417const char*argv0 = argv[0];1418if(!argc || !strcmp(argv0,"--"))1419die(_("--track needs a branch name"));1420skip_prefix(argv0,"refs/", &argv0);1421skip_prefix(argv0,"remotes/", &argv0);1422 argv0 =strchr(argv0,'/');1423if(!argv0 || !argv0[1])1424die(_("missing branch name; try -b"));1425 opts.new_branch = argv0 +1;1426}14271428/*1429 * Extract branch name from command line arguments, so1430 * all that is left is pathspecs.1431 *1432 * Handle1433 *1434 * 1) git checkout <tree> -- [<paths>]1435 * 2) git checkout -- [<paths>]1436 * 3) git checkout <something> [<paths>]1437 *1438 * including "last branch" syntax and DWIM-ery for names of1439 * remote branches, erroring out for invalid or ambiguous cases.1440 */1441if(argc) {1442struct object_id rev;1443int dwim_ok =1444!opts.patch_mode &&1445 dwim_new_local_branch &&1446 opts.track == BRANCH_TRACK_UNSPECIFIED &&1447!opts.new_branch;1448int n =parse_branchname_arg(argc, argv, dwim_ok,1449&new_branch_info, &opts, &rev,1450&dwim_remotes_matched);1451 argv += n;1452 argc -= n;1453}14541455if(argc) {1456parse_pathspec(&opts.pathspec,0,1457 opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN :0,1458 prefix, argv);14591460if(!opts.pathspec.nr)1461die(_("invalid path specification"));14621463/*1464 * Try to give more helpful suggestion.1465 * new_branch && argc > 1 will be caught later.1466 */1467if(opts.new_branch && argc ==1)1468die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),1469 argv[0], opts.new_branch);14701471if(opts.force_detach)1472die(_("git checkout: --detach does not take a path argument '%s'"),1473 argv[0]);14741475if(1< !!opts.writeout_stage + !!opts.force + !!opts.merge)1476die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"1477"checking out of the index."));1478}14791480if(opts.new_branch) {1481struct strbuf buf = STRBUF_INIT;14821483if(opts.new_branch_force)1484 opts.branch_exists =validate_branchname(opts.new_branch, &buf);1485else1486 opts.branch_exists =1487validate_new_branchname(opts.new_branch, &buf,0);1488strbuf_release(&buf);1489}14901491UNLEAK(opts);1492if(opts.patch_mode || opts.pathspec.nr) {1493int ret =checkout_paths(&opts, new_branch_info.name);1494if(ret && dwim_remotes_matched >1&&1495 advice_checkout_ambiguous_remote_branch_name)1496advise(_("'%s' matched more than one remote tracking branch.\n"1497"We found%dremotes with a reference that matched. So we fell back\n"1498"on trying to resolve the argument as a path, but failed there too!\n"1499"\n"1500"If you meant to check out a remote tracking branch on, e.g. 'origin',\n"1501"you can do so by fully qualifying the name with the --track option:\n"1502"\n"1503" git checkout --track origin/<name>\n"1504"\n"1505"If you'd like to always have checkouts of an ambiguous <name> prefer\n"1506"one remote, e.g. the 'origin' remote, consider setting\n"1507"checkout.defaultRemote=origin in your config."),1508 argv[0],1509 dwim_remotes_matched);1510return ret;1511}else{1512returncheckout_branch(&opts, &new_branch_info);1513}1514}