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