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 count_checkout_paths; 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(the_repository, tree,"",0,0, 120 pathspec, update_some, NULL); 121 122/* update the index with the given tree's info 123 * for all args, expanding wildcards, and exit 124 * with any non-zero return code. 125 */ 126return0; 127} 128 129static intskip_same_name(const struct cache_entry *ce,int pos) 130{ 131while(++pos < active_nr && 132!strcmp(active_cache[pos]->name, ce->name)) 133;/* skip */ 134return pos; 135} 136 137static intcheck_stage(int stage,const struct cache_entry *ce,int pos) 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(stage ==2) 146returnerror(_("path '%s' does not have our version"), ce->name); 147else 148returnerror(_("path '%s' does not have their version"), ce->name); 149} 150 151static intcheck_stages(unsigned stages,const struct cache_entry *ce,int pos) 152{ 153unsigned seen =0; 154const char*name = ce->name; 155 156while(pos < active_nr) { 157 ce = active_cache[pos]; 158if(strcmp(name, ce->name)) 159break; 160 seen |= (1<<ce_stage(ce)); 161 pos++; 162} 163if((stages & seen) != stages) 164returnerror(_("path '%s' does not have all necessary versions"), 165 name); 166return0; 167} 168 169static intcheckout_stage(int stage,const struct cache_entry *ce,int pos, 170const struct checkout *state,int*nr_checkouts) 171{ 172while(pos < active_nr && 173!strcmp(active_cache[pos]->name, ce->name)) { 174if(ce_stage(active_cache[pos]) == stage) 175returncheckout_entry(active_cache[pos], state, 176 NULL, nr_checkouts); 177 pos++; 178} 179if(stage ==2) 180returnerror(_("path '%s' does not have our version"), ce->name); 181else 182returnerror(_("path '%s' does not have their version"), ce->name); 183} 184 185static intcheckout_merged(int pos,const struct checkout *state,int*nr_checkouts) 186{ 187struct cache_entry *ce = active_cache[pos]; 188const char*path = ce->name; 189 mmfile_t ancestor, ours, theirs; 190int status; 191struct object_id oid; 192 mmbuffer_t result_buf; 193struct object_id threeway[3]; 194unsigned mode =0; 195 196memset(threeway,0,sizeof(threeway)); 197while(pos < active_nr) { 198int stage; 199 stage =ce_stage(ce); 200if(!stage ||strcmp(path, ce->name)) 201break; 202oidcpy(&threeway[stage -1], &ce->oid); 203if(stage ==2) 204 mode =create_ce_mode(ce->ce_mode); 205 pos++; 206 ce = active_cache[pos]; 207} 208if(is_null_oid(&threeway[1]) ||is_null_oid(&threeway[2])) 209returnerror(_("path '%s' does not have necessary versions"), path); 210 211read_mmblob(&ancestor, &threeway[0]); 212read_mmblob(&ours, &threeway[1]); 213read_mmblob(&theirs, &threeway[2]); 214 215/* 216 * NEEDSWORK: re-create conflicts from merges with 217 * merge.renormalize set, too 218 */ 219 status =ll_merge(&result_buf, path, &ancestor,"base", 220&ours,"ours", &theirs,"theirs", 221 state->istate, NULL); 222free(ancestor.ptr); 223free(ours.ptr); 224free(theirs.ptr); 225if(status <0|| !result_buf.ptr) { 226free(result_buf.ptr); 227returnerror(_("path '%s': cannot merge"), path); 228} 229 230/* 231 * NEEDSWORK: 232 * There is absolutely no reason to write this as a blob object 233 * and create a phony cache entry. This hack is primarily to get 234 * to the write_entry() machinery that massages the contents to 235 * work-tree format and writes out which only allows it for a 236 * cache entry. The code in write_entry() needs to be refactored 237 * to allow us to feed a <buffer, size, mode> instead of a cache 238 * entry. Such a refactoring would help merge_recursive as well 239 * (it also writes the merge result to the object database even 240 * when it may contain conflicts). 241 */ 242if(write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid)) 243die(_("Unable to add merge result for '%s'"), path); 244free(result_buf.ptr); 245 ce =make_transient_cache_entry(mode, &oid, path,2); 246if(!ce) 247die(_("make_cache_entry failed for path '%s'"), path); 248 status =checkout_entry(ce, state, NULL, nr_checkouts); 249discard_cache_entry(ce); 250return status; 251} 252 253static intcheckout_paths(const struct checkout_opts *opts, 254const char*revision) 255{ 256int pos; 257struct checkout state = CHECKOUT_INIT; 258static char*ps_matched; 259struct object_id rev; 260struct commit *head; 261int errs =0; 262struct lock_file lock_file = LOCK_INIT; 263int nr_checkouts =0; 264 265if(opts->track != BRANCH_TRACK_UNSPECIFIED) 266die(_("'%s' cannot be used with updating paths"),"--track"); 267 268if(opts->new_branch_log) 269die(_("'%s' cannot be used with updating paths"),"-l"); 270 271if(opts->force && opts->patch_mode) 272die(_("'%s' cannot be used with updating paths"),"-f"); 273 274if(opts->force_detach) 275die(_("'%s' cannot be used with updating paths"),"--detach"); 276 277if(opts->merge && opts->patch_mode) 278die(_("'%s' cannot be used with%s"),"--merge","--patch"); 279 280if(opts->force && opts->merge) 281die(_("'%s' cannot be used with%s"),"-f","-m"); 282 283if(opts->new_branch) 284die(_("Cannot update paths and switch to branch '%s' at the same time."), 285 opts->new_branch); 286 287if(opts->patch_mode) 288returnrun_add_interactive(revision,"--patch=checkout", 289&opts->pathspec); 290 291hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); 292if(read_cache_preload(&opts->pathspec) <0) 293returnerror(_("index file corrupt")); 294 295if(opts->source_tree) 296read_tree_some(opts->source_tree, &opts->pathspec); 297 298 ps_matched =xcalloc(opts->pathspec.nr,1); 299 300/* 301 * Make sure all pathspecs participated in locating the paths 302 * to be checked out. 303 */ 304for(pos =0; pos < active_nr; pos++) { 305struct cache_entry *ce = active_cache[pos]; 306 ce->ce_flags &= ~CE_MATCHED; 307if(!opts->ignore_skipworktree &&ce_skip_worktree(ce)) 308continue; 309if(opts->source_tree && !(ce->ce_flags & CE_UPDATE)) 310/* 311 * "git checkout tree-ish -- path", but this entry 312 * is in the original index; it will not be checked 313 * out to the working tree and it does not matter 314 * if pathspec matched this entry. We will not do 315 * anything to this entry at all. 316 */ 317continue; 318/* 319 * Either this entry came from the tree-ish we are 320 * checking the paths out of, or we are checking out 321 * of the index. 322 * 323 * If it comes from the tree-ish, we already know it 324 * matches the pathspec and could just stamp 325 * CE_MATCHED to it from update_some(). But we still 326 * need ps_matched and read_tree_recursive (and 327 * eventually tree_entry_interesting) cannot fill 328 * ps_matched yet. Once it can, we can avoid calling 329 * match_pathspec() for _all_ entries when 330 * opts->source_tree != NULL. 331 */ 332if(ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) 333 ce->ce_flags |= CE_MATCHED; 334} 335 336if(report_path_error(ps_matched, &opts->pathspec, opts->prefix)) { 337free(ps_matched); 338return1; 339} 340free(ps_matched); 341 342/* "checkout -m path" to recreate conflicted state */ 343if(opts->merge) 344unmerge_marked_index(&the_index); 345 346/* Any unmerged paths? */ 347for(pos =0; pos < active_nr; pos++) { 348const struct cache_entry *ce = active_cache[pos]; 349if(ce->ce_flags & CE_MATCHED) { 350if(!ce_stage(ce)) 351continue; 352if(opts->force) { 353warning(_("path '%s' is unmerged"), ce->name); 354}else if(opts->writeout_stage) { 355 errs |=check_stage(opts->writeout_stage, ce, pos); 356}else if(opts->merge) { 357 errs |=check_stages((1<<2) | (1<<3), ce, pos); 358}else{ 359 errs =1; 360error(_("path '%s' is unmerged"), ce->name); 361} 362 pos =skip_same_name(ce, pos) -1; 363} 364} 365if(errs) 366return1; 367 368/* Now we are committed to check them out */ 369 state.force =1; 370 state.refresh_cache =1; 371 state.istate = &the_index; 372 373enable_delayed_checkout(&state); 374for(pos =0; pos < active_nr; pos++) { 375struct cache_entry *ce = active_cache[pos]; 376if(ce->ce_flags & CE_MATCHED) { 377if(!ce_stage(ce)) { 378 errs |=checkout_entry(ce, &state, 379 NULL, &nr_checkouts); 380continue; 381} 382if(opts->writeout_stage) 383 errs |=checkout_stage(opts->writeout_stage, 384 ce, pos, 385&state, &nr_checkouts); 386else if(opts->merge) 387 errs |=checkout_merged(pos, &state, 388&nr_checkouts); 389 pos =skip_same_name(ce, pos) -1; 390} 391} 392 errs |=finish_delayed_checkout(&state, &nr_checkouts); 393 394if(opts->count_checkout_paths) { 395if(opts->source_tree) 396fprintf_ln(stderr,Q_("Checked out%dpath out of%s", 397"Checked out%dpaths out of%s", 398 nr_checkouts), 399 nr_checkouts, 400find_unique_abbrev(&opts->source_tree->object.oid, 401 DEFAULT_ABBREV)); 402else 403fprintf_ln(stderr,Q_("Checked out%dpath out of the index", 404"Checked out%dpaths out of the index", 405 nr_checkouts), 406 nr_checkouts); 407} 408 409if(write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) 410die(_("unable to write new index file")); 411 412read_ref_full("HEAD",0, &rev, NULL); 413 head =lookup_commit_reference_gently(the_repository, &rev,1); 414 415 errs |=post_checkout_hook(head, head,0); 416return errs; 417} 418 419static voidshow_local_changes(struct object *head, 420const struct diff_options *opts) 421{ 422struct rev_info rev; 423/* I think we want full paths, even if we're in a subdirectory. */ 424repo_init_revisions(the_repository, &rev, NULL); 425 rev.diffopt.flags = opts->flags; 426 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; 427diff_setup_done(&rev.diffopt); 428add_pending_object(&rev, head, NULL); 429run_diff_index(&rev,0); 430} 431 432static voiddescribe_detached_head(const char*msg,struct commit *commit) 433{ 434struct strbuf sb = STRBUF_INIT; 435 436if(!parse_commit(commit)) 437pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); 438if(print_sha1_ellipsis()) { 439fprintf(stderr,"%s %s...%s\n", msg, 440find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); 441}else{ 442fprintf(stderr,"%s %s %s\n", msg, 443find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV), sb.buf); 444} 445strbuf_release(&sb); 446} 447 448static intreset_tree(struct tree *tree,const struct checkout_opts *o, 449int worktree,int*writeout_error) 450{ 451struct unpack_trees_options opts; 452struct tree_desc tree_desc; 453 454memset(&opts,0,sizeof(opts)); 455 opts.head_idx = -1; 456 opts.update = worktree; 457 opts.skip_unmerged = !worktree; 458 opts.reset =1; 459 opts.merge =1; 460 opts.fn = oneway_merge; 461 opts.verbose_update = o->show_progress; 462 opts.src_index = &the_index; 463 opts.dst_index = &the_index; 464parse_tree(tree); 465init_tree_desc(&tree_desc, tree->buffer, tree->size); 466switch(unpack_trees(1, &tree_desc, &opts)) { 467case-2: 468*writeout_error =1; 469/* 470 * We return 0 nevertheless, as the index is all right 471 * and more importantly we have made best efforts to 472 * update paths in the work tree, and we cannot revert 473 * them. 474 */ 475/* fallthrough */ 476case0: 477return0; 478default: 479return128; 480} 481} 482 483struct branch_info { 484const char*name;/* The short name used */ 485const char*path;/* The full name of a real branch */ 486struct commit *commit;/* The named commit */ 487/* 488 * if not null the branch is detached because it's already 489 * checked out in this checkout 490 */ 491char*checkout; 492}; 493 494static voidsetup_branch_path(struct branch_info *branch) 495{ 496struct strbuf buf = STRBUF_INIT; 497 498strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL); 499if(strcmp(buf.buf, branch->name)) 500 branch->name =xstrdup(buf.buf); 501strbuf_splice(&buf,0,0,"refs/heads/",11); 502 branch->path =strbuf_detach(&buf, NULL); 503} 504 505/* 506 * Skip merging the trees, updating the index and working directory if and 507 * only if we are creating a new branch via "git checkout -b <new_branch>." 508 */ 509static intskip_merge_working_tree(const struct checkout_opts *opts, 510const struct branch_info *old_branch_info, 511const struct branch_info *new_branch_info) 512{ 513/* 514 * Do the merge if sparse checkout is on and the user has not opted in 515 * to the optimized behavior 516 */ 517if(core_apply_sparse_checkout && !checkout_optimize_new_branch) 518return0; 519 520/* 521 * We must do the merge if we are actually moving to a new commit. 522 */ 523if(!old_branch_info->commit || !new_branch_info->commit || 524!oideq(&old_branch_info->commit->object.oid, 525&new_branch_info->commit->object.oid)) 526return0; 527 528/* 529 * opts->patch_mode cannot be used with switching branches so is 530 * not tested here 531 */ 532 533/* 534 * opts->quiet only impacts output so doesn't require a merge 535 */ 536 537/* 538 * Honor the explicit request for a three-way merge or to throw away 539 * local changes 540 */ 541if(opts->merge || opts->force) 542return0; 543 544/* 545 * --detach is documented as "updating the index and the files in the 546 * working tree" but this optimization skips those steps so fall through 547 * to the regular code path. 548 */ 549if(opts->force_detach) 550return0; 551 552/* 553 * opts->writeout_stage cannot be used with switching branches so is 554 * not tested here 555 */ 556 557/* 558 * Honor the explicit ignore requests 559 */ 560if(!opts->overwrite_ignore || opts->ignore_skipworktree || 561 opts->ignore_other_worktrees) 562return0; 563 564/* 565 * opts->show_progress only impacts output so doesn't require a merge 566 */ 567 568/* 569 * If we aren't creating a new branch any changes or updates will 570 * happen in the existing branch. Since that could only be updating 571 * the index and working directory, we don't want to skip those steps 572 * or we've defeated any purpose in running the command. 573 */ 574if(!opts->new_branch) 575return0; 576 577/* 578 * new_branch_force is defined to "create/reset and checkout a branch" 579 * so needs to go through the merge to do the reset 580 */ 581if(opts->new_branch_force) 582return0; 583 584/* 585 * A new orphaned branch requrires the index and the working tree to be 586 * adjusted to <start_point> 587 */ 588if(opts->new_orphan_branch) 589return0; 590 591/* 592 * Remaining variables are not checkout options but used to track state 593 */ 594 595return1; 596} 597 598static intmerge_working_tree(const struct checkout_opts *opts, 599struct branch_info *old_branch_info, 600struct branch_info *new_branch_info, 601int*writeout_error) 602{ 603int ret; 604struct lock_file lock_file = LOCK_INIT; 605 606hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); 607if(read_cache_preload(NULL) <0) 608returnerror(_("index file corrupt")); 609 610resolve_undo_clear(); 611if(opts->force) { 612 ret =reset_tree(get_commit_tree(new_branch_info->commit), 613 opts,1, writeout_error); 614if(ret) 615return ret; 616}else{ 617struct tree_desc trees[2]; 618struct tree *tree; 619struct unpack_trees_options topts; 620 621memset(&topts,0,sizeof(topts)); 622 topts.head_idx = -1; 623 topts.src_index = &the_index; 624 topts.dst_index = &the_index; 625 626setup_unpack_trees_porcelain(&topts,"checkout"); 627 628refresh_cache(REFRESH_QUIET); 629 630if(unmerged_cache()) { 631error(_("you need to resolve your current index first")); 632return1; 633} 634 635/* 2-way merge to the new branch */ 636 topts.initial_checkout =is_cache_unborn(); 637 topts.update =1; 638 topts.merge =1; 639 topts.gently = opts->merge && old_branch_info->commit; 640 topts.verbose_update = opts->show_progress; 641 topts.fn = twoway_merge; 642if(opts->overwrite_ignore) { 643 topts.dir =xcalloc(1,sizeof(*topts.dir)); 644 topts.dir->flags |= DIR_SHOW_IGNORED; 645setup_standard_excludes(topts.dir); 646} 647 tree =parse_tree_indirect(old_branch_info->commit ? 648&old_branch_info->commit->object.oid : 649 the_hash_algo->empty_tree); 650init_tree_desc(&trees[0], tree->buffer, tree->size); 651 tree =parse_tree_indirect(&new_branch_info->commit->object.oid); 652init_tree_desc(&trees[1], tree->buffer, tree->size); 653 654 ret =unpack_trees(2, trees, &topts); 655clear_unpack_trees_porcelain(&topts); 656if(ret == -1) { 657/* 658 * Unpack couldn't do a trivial merge; either 659 * give up or do a real merge, depending on 660 * whether the merge flag was used. 661 */ 662struct tree *result; 663struct tree *work; 664struct merge_options o; 665if(!opts->merge) 666return1; 667 668/* 669 * Without old_branch_info->commit, the below is the same as 670 * the two-tree unpack we already tried and failed. 671 */ 672if(!old_branch_info->commit) 673return1; 674 675/* Do more real merge */ 676 677/* 678 * We update the index fully, then write the 679 * tree from the index, then merge the new 680 * branch with the current tree, with the old 681 * branch as the base. Then we reset the index 682 * (but not the working tree) to the new 683 * branch, leaving the working tree as the 684 * merged version, but skipping unmerged 685 * entries in the index. 686 */ 687 688add_files_to_cache(NULL, NULL,0); 689/* 690 * NEEDSWORK: carrying over local changes 691 * when branches have different end-of-line 692 * normalization (or clean+smudge rules) is 693 * a pain; plumb in an option to set 694 * o.renormalize? 695 */ 696init_merge_options(&o); 697 o.verbosity =0; 698 work =write_tree_from_memory(&o); 699 700 ret =reset_tree(get_commit_tree(new_branch_info->commit), 701 opts,1, 702 writeout_error); 703if(ret) 704return ret; 705 o.ancestor = old_branch_info->name; 706 o.branch1 = new_branch_info->name; 707 o.branch2 ="local"; 708 ret =merge_trees(&o, 709get_commit_tree(new_branch_info->commit), 710 work, 711get_commit_tree(old_branch_info->commit), 712&result); 713if(ret <0) 714exit(128); 715 ret =reset_tree(get_commit_tree(new_branch_info->commit), 716 opts,0, 717 writeout_error); 718strbuf_release(&o.obuf); 719if(ret) 720return ret; 721} 722} 723 724if(!active_cache_tree) 725 active_cache_tree =cache_tree(); 726 727if(!cache_tree_fully_valid(active_cache_tree)) 728cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); 729 730if(write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) 731die(_("unable to write new index file")); 732 733if(!opts->force && !opts->quiet) 734show_local_changes(&new_branch_info->commit->object, &opts->diff_options); 735 736return0; 737} 738 739static voidreport_tracking(struct branch_info *new_branch_info) 740{ 741struct strbuf sb = STRBUF_INIT; 742struct branch *branch =branch_get(new_branch_info->name); 743 744if(!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL)) 745return; 746fputs(sb.buf, stdout); 747strbuf_release(&sb); 748} 749 750static voidupdate_refs_for_switch(const struct checkout_opts *opts, 751struct branch_info *old_branch_info, 752struct branch_info *new_branch_info) 753{ 754struct strbuf msg = STRBUF_INIT; 755const char*old_desc, *reflog_msg; 756if(opts->new_branch) { 757if(opts->new_orphan_branch) { 758char*refname; 759 760 refname =mkpathdup("refs/heads/%s", opts->new_orphan_branch); 761if(opts->new_branch_log && 762!should_autocreate_reflog(refname)) { 763int ret; 764struct strbuf err = STRBUF_INIT; 765 766 ret =safe_create_reflog(refname,1, &err); 767if(ret) { 768fprintf(stderr,_("Can not do reflog for '%s':%s\n"), 769 opts->new_orphan_branch, err.buf); 770strbuf_release(&err); 771free(refname); 772return; 773} 774strbuf_release(&err); 775} 776free(refname); 777} 778else 779create_branch(the_repository, 780 opts->new_branch, new_branch_info->name, 781 opts->new_branch_force ?1:0, 782 opts->new_branch_force ?1:0, 783 opts->new_branch_log, 784 opts->quiet, 785 opts->track); 786 new_branch_info->name = opts->new_branch; 787setup_branch_path(new_branch_info); 788} 789 790 old_desc = old_branch_info->name; 791if(!old_desc && old_branch_info->commit) 792 old_desc =oid_to_hex(&old_branch_info->commit->object.oid); 793 794 reflog_msg =getenv("GIT_REFLOG_ACTION"); 795if(!reflog_msg) 796strbuf_addf(&msg,"checkout: moving from%sto%s", 797 old_desc ? old_desc :"(invalid)", new_branch_info->name); 798else 799strbuf_insert(&msg,0, reflog_msg,strlen(reflog_msg)); 800 801if(!strcmp(new_branch_info->name,"HEAD") && !new_branch_info->path && !opts->force_detach) { 802/* Nothing to do. */ 803}else if(opts->force_detach || !new_branch_info->path) {/* No longer on any branch. */ 804update_ref(msg.buf,"HEAD", &new_branch_info->commit->object.oid, NULL, 805 REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); 806if(!opts->quiet) { 807if(old_branch_info->path && 808 advice_detached_head && !opts->force_detach) 809detach_advice(new_branch_info->name); 810describe_detached_head(_("HEAD is now at"), new_branch_info->commit); 811} 812}else if(new_branch_info->path) {/* Switch branches. */ 813if(create_symref("HEAD", new_branch_info->path, msg.buf) <0) 814die(_("unable to update HEAD")); 815if(!opts->quiet) { 816if(old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { 817if(opts->new_branch_force) 818fprintf(stderr,_("Reset branch '%s'\n"), 819 new_branch_info->name); 820else 821fprintf(stderr,_("Already on '%s'\n"), 822 new_branch_info->name); 823}else if(opts->new_branch) { 824if(opts->branch_exists) 825fprintf(stderr,_("Switched to and reset branch '%s'\n"), new_branch_info->name); 826else 827fprintf(stderr,_("Switched to a new branch '%s'\n"), new_branch_info->name); 828}else{ 829fprintf(stderr,_("Switched to branch '%s'\n"), 830 new_branch_info->name); 831} 832} 833if(old_branch_info->path && old_branch_info->name) { 834if(!ref_exists(old_branch_info->path) &&reflog_exists(old_branch_info->path)) 835delete_reflog(old_branch_info->path); 836} 837} 838remove_branch_state(the_repository); 839strbuf_release(&msg); 840if(!opts->quiet && 841(new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name,"HEAD")))) 842report_tracking(new_branch_info); 843} 844 845static intadd_pending_uninteresting_ref(const char*refname, 846const struct object_id *oid, 847int flags,void*cb_data) 848{ 849add_pending_oid(cb_data, refname, oid, UNINTERESTING); 850return0; 851} 852 853static voiddescribe_one_orphan(struct strbuf *sb,struct commit *commit) 854{ 855strbuf_addstr(sb," "); 856strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV); 857strbuf_addch(sb,' '); 858if(!parse_commit(commit)) 859pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); 860strbuf_addch(sb,'\n'); 861} 862 863#define ORPHAN_CUTOFF 4 864static voidsuggest_reattach(struct commit *commit,struct rev_info *revs) 865{ 866struct commit *c, *last = NULL; 867struct strbuf sb = STRBUF_INIT; 868int lost =0; 869while((c =get_revision(revs)) != NULL) { 870if(lost < ORPHAN_CUTOFF) 871describe_one_orphan(&sb, c); 872 last = c; 873 lost++; 874} 875if(ORPHAN_CUTOFF < lost) { 876int more = lost - ORPHAN_CUTOFF; 877if(more ==1) 878describe_one_orphan(&sb, last); 879else 880strbuf_addf(&sb,_(" ... and%dmore.\n"), more); 881} 882 883fprintf(stderr, 884Q_( 885/* The singular version */ 886"Warning: you are leaving%dcommit behind, " 887"not connected to\n" 888"any of your branches:\n\n" 889"%s\n", 890/* The plural version */ 891"Warning: you are leaving%dcommits behind, " 892"not connected to\n" 893"any of your branches:\n\n" 894"%s\n", 895/* Give ngettext() the count */ 896 lost), 897 lost, 898 sb.buf); 899strbuf_release(&sb); 900 901if(advice_detached_head) 902fprintf(stderr, 903Q_( 904/* The singular version */ 905"If you want to keep it by creating a new branch, " 906"this may be a good time\nto do so with:\n\n" 907" git branch <new-branch-name>%s\n\n", 908/* The plural version */ 909"If you want to keep them by creating a new branch, " 910"this may be a good time\nto do so with:\n\n" 911" git branch <new-branch-name>%s\n\n", 912/* Give ngettext() the count */ 913 lost), 914find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV)); 915} 916 917/* 918 * We are about to leave commit that was at the tip of a detached 919 * HEAD. If it is not reachable from any ref, this is the last chance 920 * for the user to do so without resorting to reflog. 921 */ 922static voidorphaned_commit_warning(struct commit *old_commit,struct commit *new_commit) 923{ 924struct rev_info revs; 925struct object *object = &old_commit->object; 926 927repo_init_revisions(the_repository, &revs, NULL); 928setup_revisions(0, NULL, &revs, NULL); 929 930 object->flags &= ~UNINTERESTING; 931add_pending_object(&revs, object,oid_to_hex(&object->oid)); 932 933for_each_ref(add_pending_uninteresting_ref, &revs); 934add_pending_oid(&revs,"HEAD", &new_commit->object.oid, UNINTERESTING); 935 936if(prepare_revision_walk(&revs)) 937die(_("internal error in revision walk")); 938if(!(old_commit->object.flags & UNINTERESTING)) 939suggest_reattach(old_commit, &revs); 940else 941describe_detached_head(_("Previous HEAD position was"), old_commit); 942 943/* Clean up objects used, as they will be reused. */ 944clear_commit_marks_all(ALL_REV_FLAGS); 945} 946 947static intswitch_branches(const struct checkout_opts *opts, 948struct branch_info *new_branch_info) 949{ 950int ret =0; 951struct branch_info old_branch_info; 952void*path_to_free; 953struct object_id rev; 954int flag, writeout_error =0; 955memset(&old_branch_info,0,sizeof(old_branch_info)); 956 old_branch_info.path = path_to_free =resolve_refdup("HEAD",0, &rev, &flag); 957if(old_branch_info.path) 958 old_branch_info.commit =lookup_commit_reference_gently(the_repository, &rev,1); 959if(!(flag & REF_ISSYMREF)) 960 old_branch_info.path = NULL; 961 962if(old_branch_info.path) 963skip_prefix(old_branch_info.path,"refs/heads/", &old_branch_info.name); 964 965if(!new_branch_info->name) { 966 new_branch_info->name ="HEAD"; 967 new_branch_info->commit = old_branch_info.commit; 968if(!new_branch_info->commit) 969die(_("You are on a branch yet to be born")); 970parse_commit_or_die(new_branch_info->commit); 971} 972 973/* optimize the "checkout -b <new_branch> path */ 974if(skip_merge_working_tree(opts, &old_branch_info, new_branch_info)) { 975if(!checkout_optimize_new_branch && !opts->quiet) { 976if(read_cache_preload(NULL) <0) 977returnerror(_("index file corrupt")); 978show_local_changes(&new_branch_info->commit->object, &opts->diff_options); 979} 980}else{ 981 ret =merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error); 982if(ret) { 983free(path_to_free); 984return ret; 985} 986} 987 988if(!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit) 989orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit); 990 991update_refs_for_switch(opts, &old_branch_info, new_branch_info); 992 993 ret =post_checkout_hook(old_branch_info.commit, new_branch_info->commit,1); 994free(path_to_free); 995return ret || writeout_error; 996} 997 998static intgit_checkout_config(const char*var,const char*value,void*cb) 999{1000if(!strcmp(var,"checkout.optimizenewbranch")) {1001 checkout_optimize_new_branch =git_config_bool(var, value);1002return0;1003}10041005if(!strcmp(var,"diff.ignoresubmodules")) {1006struct checkout_opts *opts = cb;1007handle_ignore_submodules_arg(&opts->diff_options, value);1008return0;1009}10101011if(starts_with(var,"submodule."))1012returngit_default_submodule_config(var, value, NULL);10131014returngit_xmerge_config(var, value, NULL);1015}10161017static intparse_branchname_arg(int argc,const char**argv,1018int dwim_new_local_branch_ok,1019struct branch_info *new_branch_info,1020struct checkout_opts *opts,1021struct object_id *rev,1022int*dwim_remotes_matched)1023{1024struct tree **source_tree = &opts->source_tree;1025const char**new_branch = &opts->new_branch;1026int argcount =0;1027struct object_id branch_rev;1028const char*arg;1029int dash_dash_pos;1030int has_dash_dash =0;1031int i;10321033/*1034 * case 1: git checkout <ref> -- [<paths>]1035 *1036 * <ref> must be a valid tree, everything after the '--' must be1037 * a path.1038 *1039 * case 2: git checkout -- [<paths>]1040 *1041 * everything after the '--' must be paths.1042 *1043 * case 3: git checkout <something> [--]1044 *1045 * (a) If <something> is a commit, that is to1046 * switch to the branch or detach HEAD at it. As a special case,1047 * if <something> is A...B (missing A or B means HEAD but you can1048 * omit at most one side), and if there is a unique merge base1049 * between A and B, A...B names that merge base.1050 *1051 * (b) If <something> is _not_ a commit, either "--" is present1052 * or <something> is not a path, no -t or -b was given, and1053 * and there is a tracking branch whose name is <something>1054 * in one and only one remote (or if the branch exists on the1055 * remote named in checkout.defaultRemote), then this is a1056 * short-hand to fork local <something> from that1057 * remote-tracking branch.1058 *1059 * (c) Otherwise, if "--" is present, treat it like case (1).1060 *1061 * (d) Otherwise :1062 * - if it's a reference, treat it like case (1)1063 * - else if it's a path, treat it like case (2)1064 * - else: fail.1065 *1066 * case 4: git checkout <something> <paths>1067 *1068 * The first argument must not be ambiguous.1069 * - If it's *only* a reference, treat it like case (1).1070 * - If it's only a path, treat it like case (2).1071 * - else: fail.1072 *1073 */1074if(!argc)1075return0;10761077 arg = argv[0];1078 dash_dash_pos = -1;1079for(i =0; i < argc; i++) {1080if(!strcmp(argv[i],"--")) {1081 dash_dash_pos = i;1082break;1083}1084}1085if(dash_dash_pos ==0)1086return1;/* case (2) */1087else if(dash_dash_pos ==1)1088 has_dash_dash =1;/* case (3) or (1) */1089else if(dash_dash_pos >=2)1090die(_("only one reference expected,%dgiven."), dash_dash_pos);1091 opts->count_checkout_paths = !opts->quiet && !has_dash_dash;10921093if(!strcmp(arg,"-"))1094 arg ="@{-1}";10951096if(get_oid_mb(arg, rev)) {1097/*1098 * Either case (3) or (4), with <something> not being1099 * a commit, or an attempt to use case (1) with an1100 * invalid ref.1101 *1102 * It's likely an error, but we need to find out if1103 * we should auto-create the branch, case (3).(b).1104 */1105int recover_with_dwim = dwim_new_local_branch_ok;11061107int could_be_checkout_paths = !has_dash_dash &&1108check_filename(opts->prefix, arg);11091110if(!has_dash_dash && !no_wildcard(arg))1111 recover_with_dwim =0;11121113/*1114 * Accept "git checkout foo" and "git checkout foo --"1115 * as candidates for dwim.1116 */1117if(!(argc ==1&& !has_dash_dash) &&1118!(argc ==2&& has_dash_dash))1119 recover_with_dwim =0;11201121if(recover_with_dwim) {1122const char*remote =unique_tracking_name(arg, rev,1123 dwim_remotes_matched);1124if(remote) {1125if(could_be_checkout_paths)1126die(_("'%s' could be both a local file and a tracking branch.\n"1127"Please use -- (and optionally --no-guess) to disambiguate"),1128 arg);1129*new_branch = arg;1130 arg = remote;1131/* DWIMmed to create local branch, case (3).(b) */1132}else{1133 recover_with_dwim =0;1134}1135}11361137if(!recover_with_dwim) {1138if(has_dash_dash)1139die(_("invalid reference:%s"), arg);1140return argcount;1141}1142}11431144/* we can't end up being in (2) anymore, eat the argument */1145 argcount++;1146 argv++;1147 argc--;11481149 new_branch_info->name = arg;1150setup_branch_path(new_branch_info);11511152if(!check_refname_format(new_branch_info->path,0) &&1153!read_ref(new_branch_info->path, &branch_rev))1154oidcpy(rev, &branch_rev);1155else1156 new_branch_info->path = NULL;/* not an existing branch */11571158 new_branch_info->commit =lookup_commit_reference_gently(the_repository, rev,1);1159if(!new_branch_info->commit) {1160/* not a commit */1161*source_tree =parse_tree_indirect(rev);1162}else{1163parse_commit_or_die(new_branch_info->commit);1164*source_tree =get_commit_tree(new_branch_info->commit);1165}11661167if(!*source_tree)/* case (1): want a tree */1168die(_("reference is not a tree:%s"), arg);1169if(!has_dash_dash) {/* case (3).(d) -> (1) */1170/*1171 * Do not complain the most common case1172 * git checkout branch1173 * even if there happen to be a file called 'branch';1174 * it would be extremely annoying.1175 */1176if(argc)1177verify_non_filename(opts->prefix, arg);1178}else{1179 argcount++;1180 argv++;1181 argc--;1182}11831184return argcount;1185}11861187static intswitch_unborn_to_new_branch(const struct checkout_opts *opts)1188{1189int status;1190struct strbuf branch_ref = STRBUF_INIT;11911192if(!opts->new_branch)1193die(_("You are on a branch yet to be born"));1194strbuf_addf(&branch_ref,"refs/heads/%s", opts->new_branch);1195 status =create_symref("HEAD", branch_ref.buf,"checkout -b");1196strbuf_release(&branch_ref);1197if(!opts->quiet)1198fprintf(stderr,_("Switched to a new branch '%s'\n"),1199 opts->new_branch);1200return status;1201}12021203static intcheckout_branch(struct checkout_opts *opts,1204struct branch_info *new_branch_info)1205{1206if(opts->pathspec.nr)1207die(_("paths cannot be used with switching branches"));12081209if(opts->patch_mode)1210die(_("'%s' cannot be used with switching branches"),1211"--patch");12121213if(opts->writeout_stage)1214die(_("'%s' cannot be used with switching branches"),1215"--ours/--theirs");12161217if(opts->force && opts->merge)1218die(_("'%s' cannot be used with '%s'"),"-f","-m");12191220if(opts->force_detach && opts->new_branch)1221die(_("'%s' cannot be used with '%s'"),1222"--detach","-b/-B/--orphan");12231224if(opts->new_orphan_branch) {1225if(opts->track != BRANCH_TRACK_UNSPECIFIED)1226die(_("'%s' cannot be used with '%s'"),"--orphan","-t");1227}else if(opts->force_detach) {1228if(opts->track != BRANCH_TRACK_UNSPECIFIED)1229die(_("'%s' cannot be used with '%s'"),"--detach","-t");1230}else if(opts->track == BRANCH_TRACK_UNSPECIFIED)1231 opts->track = git_branch_track;12321233if(new_branch_info->name && !new_branch_info->commit)1234die(_("Cannot switch branch to a non-commit '%s'"),1235 new_branch_info->name);12361237if(new_branch_info->path && !opts->force_detach && !opts->new_branch &&1238!opts->ignore_other_worktrees) {1239int flag;1240char*head_ref =resolve_refdup("HEAD",0, NULL, &flag);1241if(head_ref &&1242(!(flag & REF_ISSYMREF) ||strcmp(head_ref, new_branch_info->path)))1243die_if_checked_out(new_branch_info->path,1);1244free(head_ref);1245}12461247if(!new_branch_info->commit && opts->new_branch) {1248struct object_id rev;1249int flag;12501251if(!read_ref_full("HEAD",0, &rev, &flag) &&1252(flag & REF_ISSYMREF) &&is_null_oid(&rev))1253returnswitch_unborn_to_new_branch(opts);1254}1255returnswitch_branches(opts, new_branch_info);1256}12571258intcmd_checkout(int argc,const char**argv,const char*prefix)1259{1260struct checkout_opts opts;1261struct branch_info new_branch_info;1262char*conflict_style = NULL;1263int dwim_new_local_branch, no_dwim_new_local_branch =0;1264int dwim_remotes_matched =0;1265struct option options[] = {1266OPT__QUIET(&opts.quiet,N_("suppress progress reporting")),1267OPT_STRING('b', NULL, &opts.new_branch,N_("branch"),1268N_("create and checkout a new branch")),1269OPT_STRING('B', NULL, &opts.new_branch_force,N_("branch"),1270N_("create/reset and checkout a branch")),1271OPT_BOOL('l', NULL, &opts.new_branch_log,N_("create reflog for new branch")),1272OPT_BOOL(0,"detach", &opts.force_detach,N_("detach HEAD at named commit")),1273OPT_SET_INT('t',"track", &opts.track,N_("set upstream info for new branch"),1274 BRANCH_TRACK_EXPLICIT),1275OPT_STRING(0,"orphan", &opts.new_orphan_branch,N_("new-branch"),N_("new unparented branch")),1276OPT_SET_INT_F('2',"ours", &opts.writeout_stage,1277N_("checkout our version for unmerged files"),12782, PARSE_OPT_NONEG),1279OPT_SET_INT_F('3',"theirs", &opts.writeout_stage,1280N_("checkout their version for unmerged files"),12813, PARSE_OPT_NONEG),1282OPT__FORCE(&opts.force,N_("force checkout (throw away local modifications)"),1283 PARSE_OPT_NOCOMPLETE),1284OPT_BOOL('m',"merge", &opts.merge,N_("perform a 3-way merge with the new branch")),1285OPT_BOOL_F(0,"overwrite-ignore", &opts.overwrite_ignore,1286N_("update ignored files (default)"),1287 PARSE_OPT_NOCOMPLETE),1288OPT_STRING(0,"conflict", &conflict_style,N_("style"),1289N_("conflict style (merge or diff3)")),1290OPT_BOOL('p',"patch", &opts.patch_mode,N_("select hunks interactively")),1291OPT_BOOL(0,"ignore-skip-worktree-bits", &opts.ignore_skipworktree,1292N_("do not limit pathspecs to sparse entries only")),1293OPT_BOOL(0,"no-guess", &no_dwim_new_local_branch,1294N_("do not second guess 'git checkout <no-such-branch>'")),1295OPT_BOOL(0,"ignore-other-worktrees", &opts.ignore_other_worktrees,1296N_("do not check if another worktree is holding the given ref")),1297{ OPTION_CALLBACK,0,"recurse-submodules", NULL,1298"checkout","control recursive updating of submodules",1299 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },1300OPT_BOOL(0,"progress", &opts.show_progress,N_("force progress reporting")),1301OPT_END(),1302};13031304memset(&opts,0,sizeof(opts));1305memset(&new_branch_info,0,sizeof(new_branch_info));1306 opts.overwrite_ignore =1;1307 opts.prefix = prefix;1308 opts.show_progress = -1;13091310git_config(git_checkout_config, &opts);13111312 opts.track = BRANCH_TRACK_UNSPECIFIED;13131314 argc =parse_options(argc, argv, prefix, options, checkout_usage,1315 PARSE_OPT_KEEP_DASHDASH);13161317 dwim_new_local_branch = !no_dwim_new_local_branch;1318if(opts.show_progress <0) {1319if(opts.quiet)1320 opts.show_progress =0;1321else1322 opts.show_progress =isatty(2);1323}13241325if(conflict_style) {1326 opts.merge =1;/* implied */1327git_xmerge_config("merge.conflictstyle", conflict_style, NULL);1328}13291330if((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) >1)1331die(_("-b, -B and --orphan are mutually exclusive"));13321333/*1334 * From here on, new_branch will contain the branch to be checked out,1335 * and new_branch_force and new_orphan_branch will tell us which one of1336 * -b/-B/--orphan is being used.1337 */1338if(opts.new_branch_force)1339 opts.new_branch = opts.new_branch_force;13401341if(opts.new_orphan_branch)1342 opts.new_branch = opts.new_orphan_branch;13431344/* --track without -b/-B/--orphan should DWIM */1345if(opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) {1346const char*argv0 = argv[0];1347if(!argc || !strcmp(argv0,"--"))1348die(_("--track needs a branch name"));1349skip_prefix(argv0,"refs/", &argv0);1350skip_prefix(argv0,"remotes/", &argv0);1351 argv0 =strchr(argv0,'/');1352if(!argv0 || !argv0[1])1353die(_("missing branch name; try -b"));1354 opts.new_branch = argv0 +1;1355}13561357/*1358 * Extract branch name from command line arguments, so1359 * all that is left is pathspecs.1360 *1361 * Handle1362 *1363 * 1) git checkout <tree> -- [<paths>]1364 * 2) git checkout -- [<paths>]1365 * 3) git checkout <something> [<paths>]1366 *1367 * including "last branch" syntax and DWIM-ery for names of1368 * remote branches, erroring out for invalid or ambiguous cases.1369 */1370if(argc) {1371struct object_id rev;1372int dwim_ok =1373!opts.patch_mode &&1374 dwim_new_local_branch &&1375 opts.track == BRANCH_TRACK_UNSPECIFIED &&1376!opts.new_branch;1377int n =parse_branchname_arg(argc, argv, dwim_ok,1378&new_branch_info, &opts, &rev,1379&dwim_remotes_matched);1380 argv += n;1381 argc -= n;1382}13831384if(argc) {1385parse_pathspec(&opts.pathspec,0,1386 opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN :0,1387 prefix, argv);13881389if(!opts.pathspec.nr)1390die(_("invalid path specification"));13911392/*1393 * Try to give more helpful suggestion.1394 * new_branch && argc > 1 will be caught later.1395 */1396if(opts.new_branch && argc ==1)1397die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),1398 argv[0], opts.new_branch);13991400if(opts.force_detach)1401die(_("git checkout: --detach does not take a path argument '%s'"),1402 argv[0]);14031404if(1< !!opts.writeout_stage + !!opts.force + !!opts.merge)1405die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"1406"checking out of the index."));1407}14081409if(opts.new_branch) {1410struct strbuf buf = STRBUF_INIT;14111412if(opts.new_branch_force)1413 opts.branch_exists =validate_branchname(opts.new_branch, &buf);1414else1415 opts.branch_exists =1416validate_new_branchname(opts.new_branch, &buf,0);1417strbuf_release(&buf);1418}14191420UNLEAK(opts);1421if(opts.patch_mode || opts.pathspec.nr) {1422int ret =checkout_paths(&opts, new_branch_info.name);1423if(ret && dwim_remotes_matched >1&&1424 advice_checkout_ambiguous_remote_branch_name)1425advise(_("'%s' matched more than one remote tracking branch.\n"1426"We found%dremotes with a reference that matched. So we fell back\n"1427"on trying to resolve the argument as a path, but failed there too!\n"1428"\n"1429"If you meant to check out a remote tracking branch on, e.g. 'origin',\n"1430"you can do so by fully qualifying the name with the --track option:\n"1431"\n"1432" git checkout --track origin/<name>\n"1433"\n"1434"If you'd like to always have checkouts of an ambiguous <name> prefer\n"1435"one remote, e.g. the 'origin' remote, consider setting\n"1436"checkout.defaultRemote=origin in your config."),1437 argv[0],1438 dwim_remotes_matched);1439return ret;1440}else{1441returncheckout_branch(&opts, &new_branch_info);1442}1443}