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{1022struct checkout_opts *opts = cb;10231024if(!strcmp(var,"checkout.optimizenewbranch")) {1025 checkout_optimize_new_branch =git_config_bool(var, value);1026return0;1027}10281029if(!strcmp(var,"checkout.overlaymode")) {1030 opts->overlay_mode =git_config_bool(var, value);1031return0;1032}10331034if(!strcmp(var,"diff.ignoresubmodules")) {1035handle_ignore_submodules_arg(&opts->diff_options, value);1036return0;1037}10381039if(starts_with(var,"submodule."))1040returngit_default_submodule_config(var, value, NULL);10411042returngit_xmerge_config(var, value, NULL);1043}10441045static intparse_branchname_arg(int argc,const char**argv,1046int dwim_new_local_branch_ok,1047struct branch_info *new_branch_info,1048struct checkout_opts *opts,1049struct object_id *rev,1050int*dwim_remotes_matched)1051{1052struct tree **source_tree = &opts->source_tree;1053const char**new_branch = &opts->new_branch;1054int argcount =0;1055struct object_id branch_rev;1056const char*arg;1057int dash_dash_pos;1058int has_dash_dash =0;1059int i;10601061/*1062 * case 1: git checkout <ref> -- [<paths>]1063 *1064 * <ref> must be a valid tree, everything after the '--' must be1065 * a path.1066 *1067 * case 2: git checkout -- [<paths>]1068 *1069 * everything after the '--' must be paths.1070 *1071 * case 3: git checkout <something> [--]1072 *1073 * (a) If <something> is a commit, that is to1074 * switch to the branch or detach HEAD at it. As a special case,1075 * if <something> is A...B (missing A or B means HEAD but you can1076 * omit at most one side), and if there is a unique merge base1077 * between A and B, A...B names that merge base.1078 *1079 * (b) If <something> is _not_ a commit, either "--" is present1080 * or <something> is not a path, no -t or -b was given, and1081 * and there is a tracking branch whose name is <something>1082 * in one and only one remote (or if the branch exists on the1083 * remote named in checkout.defaultRemote), then this is a1084 * short-hand to fork local <something> from that1085 * remote-tracking branch.1086 *1087 * (c) Otherwise, if "--" is present, treat it like case (1).1088 *1089 * (d) Otherwise :1090 * - if it's a reference, treat it like case (1)1091 * - else if it's a path, treat it like case (2)1092 * - else: fail.1093 *1094 * case 4: git checkout <something> <paths>1095 *1096 * The first argument must not be ambiguous.1097 * - If it's *only* a reference, treat it like case (1).1098 * - If it's only a path, treat it like case (2).1099 * - else: fail.1100 *1101 */1102if(!argc)1103return0;11041105 arg = argv[0];1106 dash_dash_pos = -1;1107for(i =0; i < argc; i++) {1108if(!strcmp(argv[i],"--")) {1109 dash_dash_pos = i;1110break;1111}1112}1113if(dash_dash_pos ==0)1114return1;/* case (2) */1115else if(dash_dash_pos ==1)1116 has_dash_dash =1;/* case (3) or (1) */1117else if(dash_dash_pos >=2)1118die(_("only one reference expected,%dgiven."), dash_dash_pos);11191120if(!strcmp(arg,"-"))1121 arg ="@{-1}";11221123if(get_oid_mb(arg, rev)) {1124/*1125 * Either case (3) or (4), with <something> not being1126 * a commit, or an attempt to use case (1) with an1127 * invalid ref.1128 *1129 * It's likely an error, but we need to find out if1130 * we should auto-create the branch, case (3).(b).1131 */1132int recover_with_dwim = dwim_new_local_branch_ok;11331134if(!has_dash_dash &&1135(check_filename(opts->prefix, arg) || !no_wildcard(arg)))1136 recover_with_dwim =0;1137/*1138 * Accept "git checkout foo" and "git checkout foo --"1139 * as candidates for dwim.1140 */1141if(!(argc ==1&& !has_dash_dash) &&1142!(argc ==2&& has_dash_dash))1143 recover_with_dwim =0;11441145if(recover_with_dwim) {1146const char*remote =unique_tracking_name(arg, rev,1147 dwim_remotes_matched);1148if(remote) {1149*new_branch = arg;1150 arg = remote;1151/* DWIMmed to create local branch, case (3).(b) */1152}else{1153 recover_with_dwim =0;1154}1155}11561157if(!recover_with_dwim) {1158if(has_dash_dash)1159die(_("invalid reference:%s"), arg);1160return argcount;1161}1162}11631164/* we can't end up being in (2) anymore, eat the argument */1165 argcount++;1166 argv++;1167 argc--;11681169 new_branch_info->name = arg;1170setup_branch_path(new_branch_info);11711172if(!check_refname_format(new_branch_info->path,0) &&1173!read_ref(new_branch_info->path, &branch_rev))1174oidcpy(rev, &branch_rev);1175else1176 new_branch_info->path = NULL;/* not an existing branch */11771178 new_branch_info->commit =lookup_commit_reference_gently(the_repository, rev,1);1179if(!new_branch_info->commit) {1180/* not a commit */1181*source_tree =parse_tree_indirect(rev);1182}else{1183parse_commit_or_die(new_branch_info->commit);1184*source_tree =get_commit_tree(new_branch_info->commit);1185}11861187if(!*source_tree)/* case (1): want a tree */1188die(_("reference is not a tree:%s"), arg);1189if(!has_dash_dash) {/* case (3).(d) -> (1) */1190/*1191 * Do not complain the most common case1192 * git checkout branch1193 * even if there happen to be a file called 'branch';1194 * it would be extremely annoying.1195 */1196if(argc)1197verify_non_filename(opts->prefix, arg);1198}else{1199 argcount++;1200 argv++;1201 argc--;1202}12031204return argcount;1205}12061207static intswitch_unborn_to_new_branch(const struct checkout_opts *opts)1208{1209int status;1210struct strbuf branch_ref = STRBUF_INIT;12111212if(!opts->new_branch)1213die(_("You are on a branch yet to be born"));1214strbuf_addf(&branch_ref,"refs/heads/%s", opts->new_branch);1215 status =create_symref("HEAD", branch_ref.buf,"checkout -b");1216strbuf_release(&branch_ref);1217if(!opts->quiet)1218fprintf(stderr,_("Switched to a new branch '%s'\n"),1219 opts->new_branch);1220return status;1221}12221223static intcheckout_branch(struct checkout_opts *opts,1224struct branch_info *new_branch_info)1225{1226if(opts->pathspec.nr)1227die(_("paths cannot be used with switching branches"));12281229if(opts->patch_mode)1230die(_("'%s' cannot be used with switching branches"),1231"--patch");12321233if(!opts->overlay_mode)1234die(_("'%s' cannot be used with switching branches"),1235"--no-overlay");12361237if(opts->writeout_stage)1238die(_("'%s' cannot be used with switching branches"),1239"--ours/--theirs");12401241if(opts->force && opts->merge)1242die(_("'%s' cannot be used with '%s'"),"-f","-m");12431244if(opts->force_detach && opts->new_branch)1245die(_("'%s' cannot be used with '%s'"),1246"--detach","-b/-B/--orphan");12471248if(opts->new_orphan_branch) {1249if(opts->track != BRANCH_TRACK_UNSPECIFIED)1250die(_("'%s' cannot be used with '%s'"),"--orphan","-t");1251}else if(opts->force_detach) {1252if(opts->track != BRANCH_TRACK_UNSPECIFIED)1253die(_("'%s' cannot be used with '%s'"),"--detach","-t");1254}else if(opts->track == BRANCH_TRACK_UNSPECIFIED)1255 opts->track = git_branch_track;12561257if(new_branch_info->name && !new_branch_info->commit)1258die(_("Cannot switch branch to a non-commit '%s'"),1259 new_branch_info->name);12601261if(new_branch_info->path && !opts->force_detach && !opts->new_branch &&1262!opts->ignore_other_worktrees) {1263int flag;1264char*head_ref =resolve_refdup("HEAD",0, NULL, &flag);1265if(head_ref &&1266(!(flag & REF_ISSYMREF) ||strcmp(head_ref, new_branch_info->path)))1267die_if_checked_out(new_branch_info->path,1);1268free(head_ref);1269}12701271if(!new_branch_info->commit && opts->new_branch) {1272struct object_id rev;1273int flag;12741275if(!read_ref_full("HEAD",0, &rev, &flag) &&1276(flag & REF_ISSYMREF) &&is_null_oid(&rev))1277returnswitch_unborn_to_new_branch(opts);1278}1279returnswitch_branches(opts, new_branch_info);1280}12811282intcmd_checkout(int argc,const char**argv,const char*prefix)1283{1284struct checkout_opts opts;1285struct branch_info new_branch_info;1286char*conflict_style = NULL;1287int dwim_new_local_branch =1;1288int dwim_remotes_matched =0;1289struct option options[] = {1290OPT__QUIET(&opts.quiet,N_("suppress progress reporting")),1291OPT_STRING('b', NULL, &opts.new_branch,N_("branch"),1292N_("create and checkout a new branch")),1293OPT_STRING('B', NULL, &opts.new_branch_force,N_("branch"),1294N_("create/reset and checkout a branch")),1295OPT_BOOL('l', NULL, &opts.new_branch_log,N_("create reflog for new branch")),1296OPT_BOOL(0,"detach", &opts.force_detach,N_("detach HEAD at named commit")),1297OPT_SET_INT('t',"track", &opts.track,N_("set upstream info for new branch"),1298 BRANCH_TRACK_EXPLICIT),1299OPT_STRING(0,"orphan", &opts.new_orphan_branch,N_("new-branch"),N_("new unparented branch")),1300OPT_SET_INT_F('2',"ours", &opts.writeout_stage,1301N_("checkout our version for unmerged files"),13022, PARSE_OPT_NONEG),1303OPT_SET_INT_F('3',"theirs", &opts.writeout_stage,1304N_("checkout their version for unmerged files"),13053, PARSE_OPT_NONEG),1306OPT__FORCE(&opts.force,N_("force checkout (throw away local modifications)"),1307 PARSE_OPT_NOCOMPLETE),1308OPT_BOOL('m',"merge", &opts.merge,N_("perform a 3-way merge with the new branch")),1309OPT_BOOL_F(0,"overwrite-ignore", &opts.overwrite_ignore,1310N_("update ignored files (default)"),1311 PARSE_OPT_NOCOMPLETE),1312OPT_STRING(0,"conflict", &conflict_style,N_("style"),1313N_("conflict style (merge or diff3)")),1314OPT_BOOL('p',"patch", &opts.patch_mode,N_("select hunks interactively")),1315OPT_BOOL(0,"ignore-skip-worktree-bits", &opts.ignore_skipworktree,1316N_("do not limit pathspecs to sparse entries only")),1317OPT_HIDDEN_BOOL(0,"guess", &dwim_new_local_branch,1318N_("second guess 'git checkout <no-such-branch>'")),1319OPT_BOOL(0,"ignore-other-worktrees", &opts.ignore_other_worktrees,1320N_("do not check if another worktree is holding the given ref")),1321{ OPTION_CALLBACK,0,"recurse-submodules", NULL,1322"checkout","control recursive updating of submodules",1323 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },1324OPT_BOOL(0,"progress", &opts.show_progress,N_("force progress reporting")),1325OPT_BOOL(0,"overlay", &opts.overlay_mode,N_("use overlay mode (default)")),1326OPT_END(),1327};13281329memset(&opts,0,sizeof(opts));1330memset(&new_branch_info,0,sizeof(new_branch_info));1331 opts.overwrite_ignore =1;1332 opts.prefix = prefix;1333 opts.show_progress = -1;1334 opts.overlay_mode = -1;13351336git_config(git_checkout_config, &opts);13371338 opts.track = BRANCH_TRACK_UNSPECIFIED;13391340 argc =parse_options(argc, argv, prefix, options, checkout_usage,1341 PARSE_OPT_KEEP_DASHDASH);13421343if(opts.show_progress <0) {1344if(opts.quiet)1345 opts.show_progress =0;1346else1347 opts.show_progress =isatty(2);1348}13491350if(conflict_style) {1351 opts.merge =1;/* implied */1352git_xmerge_config("merge.conflictstyle", conflict_style, NULL);1353}13541355if((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) >1)1356die(_("-b, -B and --orphan are mutually exclusive"));13571358if(opts.overlay_mode ==1&& opts.patch_mode)1359die(_("-p and --overlay are mutually exclusive"));13601361/*1362 * From here on, new_branch will contain the branch to be checked out,1363 * and new_branch_force and new_orphan_branch will tell us which one of1364 * -b/-B/--orphan is being used.1365 */1366if(opts.new_branch_force)1367 opts.new_branch = opts.new_branch_force;13681369if(opts.new_orphan_branch)1370 opts.new_branch = opts.new_orphan_branch;13711372/* --track without -b/-B/--orphan should DWIM */1373if(opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) {1374const char*argv0 = argv[0];1375if(!argc || !strcmp(argv0,"--"))1376die(_("--track needs a branch name"));1377skip_prefix(argv0,"refs/", &argv0);1378skip_prefix(argv0,"remotes/", &argv0);1379 argv0 =strchr(argv0,'/');1380if(!argv0 || !argv0[1])1381die(_("missing branch name; try -b"));1382 opts.new_branch = argv0 +1;1383}13841385/*1386 * Extract branch name from command line arguments, so1387 * all that is left is pathspecs.1388 *1389 * Handle1390 *1391 * 1) git checkout <tree> -- [<paths>]1392 * 2) git checkout -- [<paths>]1393 * 3) git checkout <something> [<paths>]1394 *1395 * including "last branch" syntax and DWIM-ery for names of1396 * remote branches, erroring out for invalid or ambiguous cases.1397 */1398if(argc) {1399struct object_id rev;1400int dwim_ok =1401!opts.patch_mode &&1402 dwim_new_local_branch &&1403 opts.track == BRANCH_TRACK_UNSPECIFIED &&1404!opts.new_branch;1405int n =parse_branchname_arg(argc, argv, dwim_ok,1406&new_branch_info, &opts, &rev,1407&dwim_remotes_matched);1408 argv += n;1409 argc -= n;1410}14111412if(argc) {1413parse_pathspec(&opts.pathspec,0,1414 opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN :0,1415 prefix, argv);14161417if(!opts.pathspec.nr)1418die(_("invalid path specification"));14191420/*1421 * Try to give more helpful suggestion.1422 * new_branch && argc > 1 will be caught later.1423 */1424if(opts.new_branch && argc ==1)1425die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),1426 argv[0], opts.new_branch);14271428if(opts.force_detach)1429die(_("git checkout: --detach does not take a path argument '%s'"),1430 argv[0]);14311432if(1< !!opts.writeout_stage + !!opts.force + !!opts.merge)1433die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"1434"checking out of the index."));1435}14361437if(opts.new_branch) {1438struct strbuf buf = STRBUF_INIT;14391440if(opts.new_branch_force)1441 opts.branch_exists =validate_branchname(opts.new_branch, &buf);1442else1443 opts.branch_exists =1444validate_new_branchname(opts.new_branch, &buf,0);1445strbuf_release(&buf);1446}14471448UNLEAK(opts);1449if(opts.patch_mode || opts.pathspec.nr) {1450int ret =checkout_paths(&opts, new_branch_info.name);1451if(ret && dwim_remotes_matched >1&&1452 advice_checkout_ambiguous_remote_branch_name)1453advise(_("'%s' matched more than one remote tracking branch.\n"1454"We found%dremotes with a reference that matched. So we fell back\n"1455"on trying to resolve the argument as a path, but failed there too!\n"1456"\n"1457"If you meant to check out a remote tracking branch on, e.g. 'origin',\n"1458"you can do so by fully qualifying the name with the --track option:\n"1459"\n"1460" git checkout --track origin/<name>\n"1461"\n"1462"If you'd like to always have checkouts of an ambiguous <name> prefer\n"1463"one remote, e.g. the 'origin' remote, consider setting\n"1464"checkout.defaultRemote=origin in your config."),1465 argv[0],1466 dwim_remotes_matched);1467return ret;1468}else{1469returncheckout_branch(&opts, &new_branch_info);1470}1471}