1#include"builtin.h" 2#include"lockfile.h" 3#include"parse-options.h" 4#include"refs.h" 5#include"commit.h" 6#include"tree.h" 7#include"tree-walk.h" 8#include"cache-tree.h" 9#include"unpack-trees.h" 10#include"dir.h" 11#include"run-command.h" 12#include"merge-recursive.h" 13#include"branch.h" 14#include"diff.h" 15#include"revision.h" 16#include"remote.h" 17#include"blob.h" 18#include"xdiff-interface.h" 19#include"ll-merge.h" 20#include"resolve-undo.h" 21#include"submodule-config.h" 22#include"submodule.h" 23#include"argv-array.h" 24#include"sigchain.h" 25 26static const char*const checkout_usage[] = { 27N_("git checkout [<options>] <branch>"), 28N_("git checkout [<options>] [<branch>] -- <file>..."), 29 NULL, 30}; 31 32struct checkout_opts { 33int patch_mode; 34int quiet; 35int merge; 36int force; 37int force_detach; 38int writeout_stage; 39int overwrite_ignore; 40int ignore_skipworktree; 41int ignore_other_worktrees; 42 43const char*new_branch; 44const char*new_branch_force; 45const char*new_orphan_branch; 46int new_branch_log; 47enum branch_track track; 48struct diff_options diff_options; 49 50int branch_exists; 51const char*prefix; 52struct pathspec pathspec; 53struct tree *source_tree; 54 55const char*new_worktree; 56const char**saved_argv; 57int new_worktree_mode; 58}; 59 60static intpost_checkout_hook(struct commit *old,struct commit *new, 61int changed) 62{ 63returnrun_hook_le(NULL,"post-checkout", 64sha1_to_hex(old ? old->object.sha1 : null_sha1), 65sha1_to_hex(new?new->object.sha1 : null_sha1), 66 changed ?"1":"0", NULL); 67/* "new" can be NULL when checking out from the index before 68 a commit exists. */ 69 70} 71 72static intupdate_some(const unsigned char*sha1,struct strbuf *base, 73const char*pathname,unsigned mode,int stage,void*context) 74{ 75int len; 76struct cache_entry *ce; 77int pos; 78 79if(S_ISDIR(mode)) 80return READ_TREE_RECURSIVE; 81 82 len = base->len +strlen(pathname); 83 ce =xcalloc(1,cache_entry_size(len)); 84hashcpy(ce->sha1, sha1); 85memcpy(ce->name, base->buf, base->len); 86memcpy(ce->name + base->len, pathname, len - base->len); 87 ce->ce_flags =create_ce_flags(0) | CE_UPDATE; 88 ce->ce_namelen = len; 89 ce->ce_mode =create_ce_mode(mode); 90 91/* 92 * If the entry is the same as the current index, we can leave the old 93 * entry in place. Whether it is UPTODATE or not, checkout_entry will 94 * do the right thing. 95 */ 96 pos =cache_name_pos(ce->name, ce->ce_namelen); 97if(pos >=0) { 98struct cache_entry *old = active_cache[pos]; 99if(ce->ce_mode == old->ce_mode && 100!hashcmp(ce->sha1, old->sha1)) { 101 old->ce_flags |= CE_UPDATE; 102free(ce); 103return0; 104} 105} 106 107add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 108return0; 109} 110 111static intread_tree_some(struct tree *tree,const struct pathspec *pathspec) 112{ 113read_tree_recursive(tree,"",0,0, pathspec, update_some, NULL); 114 115/* update the index with the given tree's info 116 * for all args, expanding wildcards, and exit 117 * with any non-zero return code. 118 */ 119return0; 120} 121 122static intskip_same_name(const struct cache_entry *ce,int pos) 123{ 124while(++pos < active_nr && 125!strcmp(active_cache[pos]->name, ce->name)) 126;/* skip */ 127return pos; 128} 129 130static intcheck_stage(int stage,const struct cache_entry *ce,int pos) 131{ 132while(pos < active_nr && 133!strcmp(active_cache[pos]->name, ce->name)) { 134if(ce_stage(active_cache[pos]) == stage) 135return0; 136 pos++; 137} 138if(stage ==2) 139returnerror(_("path '%s' does not have our version"), ce->name); 140else 141returnerror(_("path '%s' does not have their version"), ce->name); 142} 143 144static intcheck_stages(unsigned stages,const struct cache_entry *ce,int pos) 145{ 146unsigned seen =0; 147const char*name = ce->name; 148 149while(pos < active_nr) { 150 ce = active_cache[pos]; 151if(strcmp(name, ce->name)) 152break; 153 seen |= (1<<ce_stage(ce)); 154 pos++; 155} 156if((stages & seen) != stages) 157returnerror(_("path '%s' does not have all necessary versions"), 158 name); 159return0; 160} 161 162static intcheckout_stage(int stage,struct cache_entry *ce,int pos, 163struct checkout *state) 164{ 165while(pos < active_nr && 166!strcmp(active_cache[pos]->name, ce->name)) { 167if(ce_stage(active_cache[pos]) == stage) 168returncheckout_entry(active_cache[pos], state, NULL); 169 pos++; 170} 171if(stage ==2) 172returnerror(_("path '%s' does not have our version"), ce->name); 173else 174returnerror(_("path '%s' does not have their version"), ce->name); 175} 176 177static intcheckout_merged(int pos,struct checkout *state) 178{ 179struct cache_entry *ce = active_cache[pos]; 180const char*path = ce->name; 181 mmfile_t ancestor, ours, theirs; 182int status; 183unsigned char sha1[20]; 184 mmbuffer_t result_buf; 185unsigned char threeway[3][20]; 186unsigned mode =0; 187 188memset(threeway,0,sizeof(threeway)); 189while(pos < active_nr) { 190int stage; 191 stage =ce_stage(ce); 192if(!stage ||strcmp(path, ce->name)) 193break; 194hashcpy(threeway[stage -1], ce->sha1); 195if(stage ==2) 196 mode =create_ce_mode(ce->ce_mode); 197 pos++; 198 ce = active_cache[pos]; 199} 200if(is_null_sha1(threeway[1]) ||is_null_sha1(threeway[2])) 201returnerror(_("path '%s' does not have necessary versions"), path); 202 203read_mmblob(&ancestor, threeway[0]); 204read_mmblob(&ours, threeway[1]); 205read_mmblob(&theirs, threeway[2]); 206 207/* 208 * NEEDSWORK: re-create conflicts from merges with 209 * merge.renormalize set, too 210 */ 211 status =ll_merge(&result_buf, path, &ancestor,"base", 212&ours,"ours", &theirs,"theirs", NULL); 213free(ancestor.ptr); 214free(ours.ptr); 215free(theirs.ptr); 216if(status <0|| !result_buf.ptr) { 217free(result_buf.ptr); 218returnerror(_("path '%s': cannot merge"), path); 219} 220 221/* 222 * NEEDSWORK: 223 * There is absolutely no reason to write this as a blob object 224 * and create a phony cache entry just to leak. This hack is 225 * primarily to get to the write_entry() machinery that massages 226 * the contents to work-tree format and writes out which only 227 * allows it for a cache entry. The code in write_entry() needs 228 * to be refactored to allow us to feed a <buffer, size, mode> 229 * instead of a cache entry. Such a refactoring would help 230 * merge_recursive as well (it also writes the merge result to the 231 * object database even when it may contain conflicts). 232 */ 233if(write_sha1_file(result_buf.ptr, result_buf.size, 234 blob_type, sha1)) 235die(_("Unable to add merge result for '%s'"), path); 236 ce =make_cache_entry(mode, sha1, path,2,0); 237if(!ce) 238die(_("make_cache_entry failed for path '%s'"), path); 239 status =checkout_entry(ce, state, NULL); 240return status; 241} 242 243static intcheckout_paths(const struct checkout_opts *opts, 244const char*revision) 245{ 246int pos; 247struct checkout state; 248static char*ps_matched; 249unsigned char rev[20]; 250int flag; 251struct commit *head; 252int errs =0; 253struct lock_file *lock_file; 254 255if(opts->track != BRANCH_TRACK_UNSPECIFIED) 256die(_("'%s' cannot be used with updating paths"),"--track"); 257 258if(opts->new_branch_log) 259die(_("'%s' cannot be used with updating paths"),"-l"); 260 261if(opts->force && opts->patch_mode) 262die(_("'%s' cannot be used with updating paths"),"-f"); 263 264if(opts->force_detach) 265die(_("'%s' cannot be used with updating paths"),"--detach"); 266 267if(opts->merge && opts->patch_mode) 268die(_("'%s' cannot be used with%s"),"--merge","--patch"); 269 270if(opts->force && opts->merge) 271die(_("'%s' cannot be used with%s"),"-f","-m"); 272 273if(opts->new_branch) 274die(_("Cannot update paths and switch to branch '%s' at the same time."), 275 opts->new_branch); 276 277if(opts->new_worktree) 278die(_("'%s' cannot be used with updating paths"),"--to"); 279 280if(opts->patch_mode) 281returnrun_add_interactive(revision,"--patch=checkout", 282&opts->pathspec); 283 284 lock_file =xcalloc(1,sizeof(struct lock_file)); 285 286hold_locked_index(lock_file,1); 287if(read_cache_preload(&opts->pathspec) <0) 288returnerror(_("corrupt index file")); 289 290if(opts->source_tree) 291read_tree_some(opts->source_tree, &opts->pathspec); 292 293 ps_matched =xcalloc(1, opts->pathspec.nr); 294 295/* 296 * Make sure all pathspecs participated in locating the paths 297 * to be checked out. 298 */ 299for(pos =0; pos < active_nr; pos++) { 300struct cache_entry *ce = active_cache[pos]; 301 ce->ce_flags &= ~CE_MATCHED; 302if(!opts->ignore_skipworktree &&ce_skip_worktree(ce)) 303continue; 304if(opts->source_tree && !(ce->ce_flags & CE_UPDATE)) 305/* 306 * "git checkout tree-ish -- path", but this entry 307 * is in the original index; it will not be checked 308 * out to the working tree and it does not matter 309 * if pathspec matched this entry. We will not do 310 * anything to this entry at all. 311 */ 312continue; 313/* 314 * Either this entry came from the tree-ish we are 315 * checking the paths out of, or we are checking out 316 * of the index. 317 * 318 * If it comes from the tree-ish, we already know it 319 * matches the pathspec and could just stamp 320 * CE_MATCHED to it from update_some(). But we still 321 * need ps_matched and read_tree_recursive (and 322 * eventually tree_entry_interesting) cannot fill 323 * ps_matched yet. Once it can, we can avoid calling 324 * match_pathspec() for _all_ entries when 325 * opts->source_tree != NULL. 326 */ 327if(ce_path_match(ce, &opts->pathspec, ps_matched)) 328 ce->ce_flags |= CE_MATCHED; 329} 330 331if(report_path_error(ps_matched, &opts->pathspec, opts->prefix)) { 332free(ps_matched); 333return1; 334} 335free(ps_matched); 336 337/* "checkout -m path" to recreate conflicted state */ 338if(opts->merge) 339unmerge_marked_index(&the_index); 340 341/* Any unmerged paths? */ 342for(pos =0; pos < active_nr; pos++) { 343const struct cache_entry *ce = active_cache[pos]; 344if(ce->ce_flags & CE_MATCHED) { 345if(!ce_stage(ce)) 346continue; 347if(opts->force) { 348warning(_("path '%s' is unmerged"), ce->name); 349}else if(opts->writeout_stage) { 350 errs |=check_stage(opts->writeout_stage, ce, pos); 351}else if(opts->merge) { 352 errs |=check_stages((1<<2) | (1<<3), ce, pos); 353}else{ 354 errs =1; 355error(_("path '%s' is unmerged"), ce->name); 356} 357 pos =skip_same_name(ce, pos) -1; 358} 359} 360if(errs) 361return1; 362 363/* Now we are committed to check them out */ 364memset(&state,0,sizeof(state)); 365 state.force =1; 366 state.refresh_cache =1; 367 state.istate = &the_index; 368for(pos =0; pos < active_nr; pos++) { 369struct cache_entry *ce = active_cache[pos]; 370if(ce->ce_flags & CE_MATCHED) { 371if(!ce_stage(ce)) { 372 errs |=checkout_entry(ce, &state, NULL); 373continue; 374} 375if(opts->writeout_stage) 376 errs |=checkout_stage(opts->writeout_stage, ce, pos, &state); 377else if(opts->merge) 378 errs |=checkout_merged(pos, &state); 379 pos =skip_same_name(ce, pos) -1; 380} 381} 382 383if(write_locked_index(&the_index, lock_file, COMMIT_LOCK)) 384die(_("unable to write new index file")); 385 386read_ref_full("HEAD",0, rev, &flag); 387 head =lookup_commit_reference_gently(rev,1); 388 389 errs |=post_checkout_hook(head, head,0); 390return errs; 391} 392 393static voidshow_local_changes(struct object *head, 394const struct diff_options *opts) 395{ 396struct rev_info rev; 397/* I think we want full paths, even if we're in a subdirectory. */ 398init_revisions(&rev, NULL); 399 rev.diffopt.flags = opts->flags; 400 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS; 401diff_setup_done(&rev.diffopt); 402add_pending_object(&rev, head, NULL); 403run_diff_index(&rev,0); 404} 405 406static voiddescribe_detached_head(const char*msg,struct commit *commit) 407{ 408struct strbuf sb = STRBUF_INIT; 409if(!parse_commit(commit)) 410pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb); 411fprintf(stderr,"%s %s...%s\n", msg, 412find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf); 413strbuf_release(&sb); 414} 415 416static intreset_tree(struct tree *tree,const struct checkout_opts *o, 417int worktree,int*writeout_error) 418{ 419struct unpack_trees_options opts; 420struct tree_desc tree_desc; 421 422memset(&opts,0,sizeof(opts)); 423 opts.head_idx = -1; 424 opts.update = worktree; 425 opts.skip_unmerged = !worktree; 426 opts.reset =1; 427 opts.merge =1; 428 opts.fn = oneway_merge; 429 opts.verbose_update = !o->quiet &&isatty(2); 430 opts.src_index = &the_index; 431 opts.dst_index = &the_index; 432parse_tree(tree); 433init_tree_desc(&tree_desc, tree->buffer, tree->size); 434switch(unpack_trees(1, &tree_desc, &opts)) { 435case-2: 436*writeout_error =1; 437/* 438 * We return 0 nevertheless, as the index is all right 439 * and more importantly we have made best efforts to 440 * update paths in the work tree, and we cannot revert 441 * them. 442 */ 443case0: 444return0; 445default: 446return128; 447} 448} 449 450struct branch_info { 451const char*name;/* The short name used */ 452const char*path;/* The full name of a real branch */ 453struct commit *commit;/* The named commit */ 454/* 455 * if not null the branch is detached because it's already 456 * checked out in this checkout 457 */ 458char*checkout; 459}; 460 461static voidsetup_branch_path(struct branch_info *branch) 462{ 463struct strbuf buf = STRBUF_INIT; 464 465strbuf_branchname(&buf, branch->name); 466if(strcmp(buf.buf, branch->name)) 467 branch->name =xstrdup(buf.buf); 468strbuf_splice(&buf,0,0,"refs/heads/",11); 469 branch->path =strbuf_detach(&buf, NULL); 470} 471 472static intmerge_working_tree(const struct checkout_opts *opts, 473struct branch_info *old, 474struct branch_info *new, 475int*writeout_error) 476{ 477int ret; 478struct lock_file *lock_file =xcalloc(1,sizeof(struct lock_file)); 479 480hold_locked_index(lock_file,1); 481if(read_cache_preload(NULL) <0) 482returnerror(_("corrupt index file")); 483 484resolve_undo_clear(); 485if(opts->force) { 486 ret =reset_tree(new->commit->tree, opts,1, writeout_error); 487if(ret) 488return ret; 489}else{ 490struct tree_desc trees[2]; 491struct tree *tree; 492struct unpack_trees_options topts; 493 494memset(&topts,0,sizeof(topts)); 495 topts.head_idx = -1; 496 topts.src_index = &the_index; 497 topts.dst_index = &the_index; 498 499setup_unpack_trees_porcelain(&topts,"checkout"); 500 501refresh_cache(REFRESH_QUIET); 502 503if(unmerged_cache()) { 504error(_("you need to resolve your current index first")); 505return1; 506} 507 508/* 2-way merge to the new branch */ 509 topts.initial_checkout =is_cache_unborn(); 510 topts.update =1; 511 topts.merge =1; 512 topts.gently = opts->merge && old->commit; 513 topts.verbose_update = !opts->quiet &&isatty(2); 514 topts.fn = twoway_merge; 515if(opts->overwrite_ignore) { 516 topts.dir =xcalloc(1,sizeof(*topts.dir)); 517 topts.dir->flags |= DIR_SHOW_IGNORED; 518setup_standard_excludes(topts.dir); 519} 520 tree =parse_tree_indirect(old->commit && !opts->new_worktree_mode ? 521 old->commit->object.sha1 : 522 EMPTY_TREE_SHA1_BIN); 523init_tree_desc(&trees[0], tree->buffer, tree->size); 524 tree =parse_tree_indirect(new->commit->object.sha1); 525init_tree_desc(&trees[1], tree->buffer, tree->size); 526 527 ret =unpack_trees(2, trees, &topts); 528if(ret == -1) { 529/* 530 * Unpack couldn't do a trivial merge; either 531 * give up or do a real merge, depending on 532 * whether the merge flag was used. 533 */ 534struct tree *result; 535struct tree *work; 536struct merge_options o; 537if(!opts->merge) 538return1; 539 540/* 541 * Without old->commit, the below is the same as 542 * the two-tree unpack we already tried and failed. 543 */ 544if(!old->commit) 545return1; 546 547/* Do more real merge */ 548 549/* 550 * We update the index fully, then write the 551 * tree from the index, then merge the new 552 * branch with the current tree, with the old 553 * branch as the base. Then we reset the index 554 * (but not the working tree) to the new 555 * branch, leaving the working tree as the 556 * merged version, but skipping unmerged 557 * entries in the index. 558 */ 559 560add_files_to_cache(NULL, NULL,0); 561/* 562 * NEEDSWORK: carrying over local changes 563 * when branches have different end-of-line 564 * normalization (or clean+smudge rules) is 565 * a pain; plumb in an option to set 566 * o.renormalize? 567 */ 568init_merge_options(&o); 569 o.verbosity =0; 570 work =write_tree_from_memory(&o); 571 572 ret =reset_tree(new->commit->tree, opts,1, 573 writeout_error); 574if(ret) 575return ret; 576 o.ancestor = old->name; 577 o.branch1 =new->name; 578 o.branch2 ="local"; 579merge_trees(&o,new->commit->tree, work, 580 old->commit->tree, &result); 581 ret =reset_tree(new->commit->tree, opts,0, 582 writeout_error); 583if(ret) 584return ret; 585} 586} 587 588if(!active_cache_tree) 589 active_cache_tree =cache_tree(); 590 591if(!cache_tree_fully_valid(active_cache_tree)) 592cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); 593 594if(write_locked_index(&the_index, lock_file, COMMIT_LOCK)) 595die(_("unable to write new index file")); 596 597if(!opts->force && !opts->quiet) 598show_local_changes(&new->commit->object, &opts->diff_options); 599 600return0; 601} 602 603static voidreport_tracking(struct branch_info *new) 604{ 605struct strbuf sb = STRBUF_INIT; 606struct branch *branch =branch_get(new->name); 607 608if(!format_tracking_info(branch, &sb)) 609return; 610fputs(sb.buf, stdout); 611strbuf_release(&sb); 612} 613 614static voidupdate_refs_for_switch(const struct checkout_opts *opts, 615struct branch_info *old, 616struct branch_info *new) 617{ 618struct strbuf msg = STRBUF_INIT; 619const char*old_desc, *reflog_msg; 620if(opts->new_branch) { 621if(opts->new_orphan_branch) { 622if(opts->new_branch_log && !log_all_ref_updates) { 623int temp; 624struct strbuf log_file = STRBUF_INIT; 625int ret; 626const char*ref_name; 627 628 ref_name =mkpath("refs/heads/%s", opts->new_orphan_branch); 629 temp = log_all_ref_updates; 630 log_all_ref_updates =1; 631 ret =log_ref_setup(ref_name, &log_file); 632 log_all_ref_updates = temp; 633strbuf_release(&log_file); 634if(ret) { 635fprintf(stderr,_("Can not do reflog for '%s'\n"), 636 opts->new_orphan_branch); 637return; 638} 639} 640} 641else 642create_branch(old->name, opts->new_branch,new->name, 643 opts->new_branch_force ?1:0, 644 opts->new_branch_log, 645 opts->new_branch_force ?1:0, 646 opts->quiet, 647 opts->track); 648new->name = opts->new_branch; 649setup_branch_path(new); 650} 651 652 old_desc = old->name; 653if(!old_desc && old->commit) 654 old_desc =sha1_to_hex(old->commit->object.sha1); 655 656 reflog_msg =getenv("GIT_REFLOG_ACTION"); 657if(!reflog_msg) 658strbuf_addf(&msg,"checkout: moving from%sto%s", 659 old_desc ? old_desc :"(invalid)",new->name); 660else 661strbuf_insert(&msg,0, reflog_msg,strlen(reflog_msg)); 662 663if(!strcmp(new->name,"HEAD") && !new->path && !opts->force_detach) { 664/* Nothing to do. */ 665}else if(opts->force_detach || !new->path) {/* No longer on any branch. */ 666update_ref(msg.buf,"HEAD",new->commit->object.sha1, NULL, 667 REF_NODEREF, UPDATE_REFS_DIE_ON_ERR); 668if(!opts->quiet) { 669if(old->path && advice_detached_head) 670detach_advice(new->name); 671describe_detached_head(_("HEAD is now at"),new->commit); 672} 673}else if(new->path) {/* Switch branches. */ 674create_symref("HEAD",new->path, msg.buf); 675if(!opts->quiet) { 676if(old->path && !strcmp(new->path, old->path)) { 677if(opts->new_branch_force) 678fprintf(stderr,_("Reset branch '%s'\n"), 679new->name); 680else 681fprintf(stderr,_("Already on '%s'\n"), 682new->name); 683}else if(opts->new_branch) { 684if(opts->branch_exists) 685fprintf(stderr,_("Switched to and reset branch '%s'\n"),new->name); 686else 687fprintf(stderr,_("Switched to a new branch '%s'\n"),new->name); 688}else{ 689fprintf(stderr,_("Switched to branch '%s'\n"), 690new->name); 691} 692} 693if(old->path && old->name) { 694if(!ref_exists(old->path) &&reflog_exists(old->path)) 695delete_reflog(old->path); 696} 697} 698remove_branch_state(); 699strbuf_release(&msg); 700if(!opts->quiet && 701(new->path || (!opts->force_detach && !strcmp(new->name,"HEAD")))) 702report_tracking(new); 703} 704 705static intadd_pending_uninteresting_ref(const char*refname, 706const unsigned char*sha1, 707int flags,void*cb_data) 708{ 709add_pending_sha1(cb_data, refname, sha1, UNINTERESTING); 710return0; 711} 712 713static voiddescribe_one_orphan(struct strbuf *sb,struct commit *commit) 714{ 715strbuf_addstr(sb," "); 716strbuf_addstr(sb, 717find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); 718strbuf_addch(sb,' '); 719if(!parse_commit(commit)) 720pp_commit_easy(CMIT_FMT_ONELINE, commit, sb); 721strbuf_addch(sb,'\n'); 722} 723 724#define ORPHAN_CUTOFF 4 725static voidsuggest_reattach(struct commit *commit,struct rev_info *revs) 726{ 727struct commit *c, *last = NULL; 728struct strbuf sb = STRBUF_INIT; 729int lost =0; 730while((c =get_revision(revs)) != NULL) { 731if(lost < ORPHAN_CUTOFF) 732describe_one_orphan(&sb, c); 733 last = c; 734 lost++; 735} 736if(ORPHAN_CUTOFF < lost) { 737int more = lost - ORPHAN_CUTOFF; 738if(more ==1) 739describe_one_orphan(&sb, last); 740else 741strbuf_addf(&sb,_(" ... and%dmore.\n"), more); 742} 743 744fprintf(stderr, 745Q_( 746/* The singular version */ 747"Warning: you are leaving%dcommit behind, " 748"not connected to\n" 749"any of your branches:\n\n" 750"%s\n", 751/* The plural version */ 752"Warning: you are leaving%dcommits behind, " 753"not connected to\n" 754"any of your branches:\n\n" 755"%s\n", 756/* Give ngettext() the count */ 757 lost), 758 lost, 759 sb.buf); 760strbuf_release(&sb); 761 762if(advice_detached_head) 763fprintf(stderr, 764Q_( 765/* The singular version */ 766"If you want to keep it by creating a new branch, " 767"this may be a good time\nto do so with:\n\n" 768" git branch <new-branch-name>%s\n\n", 769/* The plural version */ 770"If you want to keep them by creating a new branch, " 771"this may be a good time\nto do so with:\n\n" 772" git branch <new-branch-name>%s\n\n", 773/* Give ngettext() the count */ 774 lost), 775find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); 776} 777 778/* 779 * We are about to leave commit that was at the tip of a detached 780 * HEAD. If it is not reachable from any ref, this is the last chance 781 * for the user to do so without resorting to reflog. 782 */ 783static voidorphaned_commit_warning(struct commit *old,struct commit *new) 784{ 785struct rev_info revs; 786struct object *object = &old->object; 787struct object_array refs; 788 789init_revisions(&revs, NULL); 790setup_revisions(0, NULL, &revs, NULL); 791 792 object->flags &= ~UNINTERESTING; 793add_pending_object(&revs, object,sha1_to_hex(object->sha1)); 794 795for_each_ref(add_pending_uninteresting_ref, &revs); 796add_pending_sha1(&revs,"HEAD",new->object.sha1, UNINTERESTING); 797 798 refs = revs.pending; 799 revs.leak_pending =1; 800 801if(prepare_revision_walk(&revs)) 802die(_("internal error in revision walk")); 803if(!(old->object.flags & UNINTERESTING)) 804suggest_reattach(old, &revs); 805else 806describe_detached_head(_("Previous HEAD position was"), old); 807 808clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS); 809free(refs.objects); 810} 811 812static intswitch_branches(const struct checkout_opts *opts, 813struct branch_info *new) 814{ 815int ret =0; 816struct branch_info old; 817void*path_to_free; 818unsigned char rev[20]; 819int flag, writeout_error =0; 820memset(&old,0,sizeof(old)); 821 old.path = path_to_free =resolve_refdup("HEAD",0, rev, &flag); 822 old.commit =lookup_commit_reference_gently(rev,1); 823if(!(flag & REF_ISSYMREF)) 824 old.path = NULL; 825 826if(old.path) 827skip_prefix(old.path,"refs/heads/", &old.name); 828 829if(!new->name) { 830new->name ="HEAD"; 831new->commit = old.commit; 832if(!new->commit) 833die(_("You are on a branch yet to be born")); 834parse_commit_or_die(new->commit); 835} 836 837 ret =merge_working_tree(opts, &old,new, &writeout_error); 838if(ret) { 839free(path_to_free); 840return ret; 841} 842 843if(!opts->quiet && !old.path && old.commit && 844new->commit != old.commit && !opts->new_worktree_mode) 845orphaned_commit_warning(old.commit,new->commit); 846 847update_refs_for_switch(opts, &old,new); 848 849 ret =post_checkout_hook(old.commit,new->commit,1); 850free(path_to_free); 851return ret || writeout_error; 852} 853 854static char*junk_work_tree; 855static char*junk_git_dir; 856static int is_junk; 857static pid_t junk_pid; 858 859static voidremove_junk(void) 860{ 861struct strbuf sb = STRBUF_INIT; 862if(!is_junk ||getpid() != junk_pid) 863return; 864if(junk_git_dir) { 865strbuf_addstr(&sb, junk_git_dir); 866remove_dir_recursively(&sb,0); 867strbuf_reset(&sb); 868} 869if(junk_work_tree) { 870strbuf_addstr(&sb, junk_work_tree); 871remove_dir_recursively(&sb,0); 872} 873strbuf_release(&sb); 874} 875 876static voidremove_junk_on_signal(int signo) 877{ 878remove_junk(); 879sigchain_pop(signo); 880raise(signo); 881} 882 883static intprepare_linked_checkout(const struct checkout_opts *opts, 884struct branch_info *new) 885{ 886struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; 887struct strbuf sb = STRBUF_INIT; 888const char*path = opts->new_worktree, *name; 889struct stat st; 890struct child_process cp; 891int counter =0, len, ret; 892 893if(!new->commit) 894die(_("no branch specified")); 895if(file_exists(path) && !is_empty_dir(path)) 896die(_("'%s' already exists"), path); 897 898 len =strlen(path); 899while(len &&is_dir_sep(path[len -1])) 900 len--; 901 902for(name = path + len -1; name > path; name--) 903if(is_dir_sep(*name)) { 904 name++; 905break; 906} 907strbuf_addstr(&sb_repo, 908git_path("worktrees/%.*s", (int)(path + len - name), name)); 909 len = sb_repo.len; 910if(safe_create_leading_directories_const(sb_repo.buf)) 911die_errno(_("could not create leading directories of '%s'"), 912 sb_repo.buf); 913while(!stat(sb_repo.buf, &st)) { 914 counter++; 915strbuf_setlen(&sb_repo, len); 916strbuf_addf(&sb_repo,"%d", counter); 917} 918 name =strrchr(sb_repo.buf,'/') +1; 919 920 junk_pid =getpid(); 921atexit(remove_junk); 922sigchain_push_common(remove_junk_on_signal); 923 924if(mkdir(sb_repo.buf,0777)) 925die_errno(_("could not create directory of '%s'"), sb_repo.buf); 926 junk_git_dir =xstrdup(sb_repo.buf); 927 is_junk =1; 928 929/* 930 * lock the incomplete repo so prune won't delete it, unlock 931 * after the preparation is over. 932 */ 933strbuf_addf(&sb,"%s/locked", sb_repo.buf); 934write_file(sb.buf,1,"initializing\n"); 935 936strbuf_addf(&sb_git,"%s/.git", path); 937if(safe_create_leading_directories_const(sb_git.buf)) 938die_errno(_("could not create leading directories of '%s'"), 939 sb_git.buf); 940 junk_work_tree =xstrdup(path); 941 942strbuf_reset(&sb); 943strbuf_addf(&sb,"%s/gitdir", sb_repo.buf); 944write_file(sb.buf,1,"%s\n",real_path(sb_git.buf)); 945write_file(sb_git.buf,1,"gitdir:%s/worktrees/%s\n", 946real_path(get_git_common_dir()), name); 947/* 948 * This is to keep resolve_ref() happy. We need a valid HEAD 949 * or is_git_directory() will reject the directory. Any valid 950 * value would do because this value will be ignored and 951 * replaced at the next (real) checkout. 952 */ 953strbuf_reset(&sb); 954strbuf_addf(&sb,"%s/HEAD", sb_repo.buf); 955write_file(sb.buf,1,"%s\n",sha1_to_hex(new->commit->object.sha1)); 956strbuf_reset(&sb); 957strbuf_addf(&sb,"%s/commondir", sb_repo.buf); 958write_file(sb.buf,1,"../..\n"); 959 960if(!opts->quiet) 961fprintf_ln(stderr,_("Enter%s(identifier%s)"), path, name); 962 963setenv("GIT_CHECKOUT_NEW_WORKTREE","1",1); 964setenv(GIT_DIR_ENVIRONMENT, sb_git.buf,1); 965setenv(GIT_WORK_TREE_ENVIRONMENT, path,1); 966memset(&cp,0,sizeof(cp)); 967 cp.git_cmd =1; 968 cp.argv = opts->saved_argv; 969 ret =run_command(&cp); 970if(!ret) { 971 is_junk =0; 972free(junk_work_tree); 973free(junk_git_dir); 974 junk_work_tree = NULL; 975 junk_git_dir = NULL; 976} 977strbuf_reset(&sb); 978strbuf_addf(&sb,"%s/locked", sb_repo.buf); 979unlink_or_warn(sb.buf); 980strbuf_release(&sb); 981strbuf_release(&sb_repo); 982strbuf_release(&sb_git); 983return ret; 984} 985 986static intgit_checkout_config(const char*var,const char*value,void*cb) 987{ 988if(!strcmp(var,"diff.ignoresubmodules")) { 989struct checkout_opts *opts = cb; 990handle_ignore_submodules_arg(&opts->diff_options, value); 991return0; 992} 993 994if(starts_with(var,"submodule.")) 995returnparse_submodule_config_option(var, value); 996 997returngit_xmerge_config(var, value, NULL); 998} 9991000struct tracking_name_data {1001/* const */char*src_ref;1002char*dst_ref;1003unsigned char*dst_sha1;1004int unique;1005};10061007static intcheck_tracking_name(struct remote *remote,void*cb_data)1008{1009struct tracking_name_data *cb = cb_data;1010struct refspec query;1011memset(&query,0,sizeof(struct refspec));1012 query.src = cb->src_ref;1013if(remote_find_tracking(remote, &query) ||1014get_sha1(query.dst, cb->dst_sha1)) {1015free(query.dst);1016return0;1017}1018if(cb->dst_ref) {1019free(query.dst);1020 cb->unique =0;1021return0;1022}1023 cb->dst_ref = query.dst;1024return0;1025}10261027static const char*unique_tracking_name(const char*name,unsigned char*sha1)1028{1029struct tracking_name_data cb_data = { NULL, NULL, NULL,1};1030char src_ref[PATH_MAX];1031snprintf(src_ref, PATH_MAX,"refs/heads/%s", name);1032 cb_data.src_ref = src_ref;1033 cb_data.dst_sha1 = sha1;1034for_each_remote(check_tracking_name, &cb_data);1035if(cb_data.unique)1036return cb_data.dst_ref;1037free(cb_data.dst_ref);1038return NULL;1039}10401041static voidcheck_linked_checkout(struct branch_info *new,const char*id)1042{1043struct strbuf sb = STRBUF_INIT;1044struct strbuf path = STRBUF_INIT;1045struct strbuf gitdir = STRBUF_INIT;1046const char*start, *end;10471048if(id)1049strbuf_addf(&path,"%s/worktrees/%s/HEAD",get_git_common_dir(), id);1050else1051strbuf_addf(&path,"%s/HEAD",get_git_common_dir());10521053if(strbuf_read_file(&sb, path.buf,0) <0||1054!skip_prefix(sb.buf,"ref:", &start))1055goto done;1056while(isspace(*start))1057 start++;1058 end = start;1059while(*end && !isspace(*end))1060 end++;1061if(strncmp(start,new->path, end - start) ||new->path[end - start] !='\0')1062goto done;1063if(id) {1064strbuf_reset(&path);1065strbuf_addf(&path,"%s/worktrees/%s/gitdir",get_git_common_dir(), id);1066if(strbuf_read_file(&gitdir, path.buf,0) <=0)1067goto done;1068strbuf_rtrim(&gitdir);1069}else1070strbuf_addstr(&gitdir,get_git_common_dir());1071die(_("'%s' is already checked out at '%s'"),new->name, gitdir.buf);1072done:1073strbuf_release(&path);1074strbuf_release(&sb);1075strbuf_release(&gitdir);1076}10771078static voidcheck_linked_checkouts(struct branch_info *new)1079{1080struct strbuf path = STRBUF_INIT;1081DIR*dir;1082struct dirent *d;10831084strbuf_addf(&path,"%s/worktrees",get_git_common_dir());1085if((dir =opendir(path.buf)) == NULL) {1086strbuf_release(&path);1087return;1088}10891090/*1091 * $GIT_COMMON_DIR/HEAD is practically outside1092 * $GIT_DIR so resolve_ref_unsafe() won't work (it1093 * uses git_path). Parse the ref ourselves.1094 */1095check_linked_checkout(new, NULL);10961097while((d =readdir(dir)) != NULL) {1098if(!strcmp(d->d_name,".") || !strcmp(d->d_name,".."))1099continue;1100check_linked_checkout(new, d->d_name);1101}1102strbuf_release(&path);1103closedir(dir);1104}11051106static intparse_branchname_arg(int argc,const char**argv,1107int dwim_new_local_branch_ok,1108struct branch_info *new,1109struct checkout_opts *opts,1110unsigned char rev[20])1111{1112struct tree **source_tree = &opts->source_tree;1113const char**new_branch = &opts->new_branch;1114int force_detach = opts->force_detach;1115int argcount =0;1116unsigned char branch_rev[20];1117const char*arg;1118int dash_dash_pos;1119int has_dash_dash =0;1120int i;11211122/*1123 * case 1: git checkout <ref> -- [<paths>]1124 *1125 * <ref> must be a valid tree, everything after the '--' must be1126 * a path.1127 *1128 * case 2: git checkout -- [<paths>]1129 *1130 * everything after the '--' must be paths.1131 *1132 * case 3: git checkout <something> [--]1133 *1134 * (a) If <something> is a commit, that is to1135 * switch to the branch or detach HEAD at it. As a special case,1136 * if <something> is A...B (missing A or B means HEAD but you can1137 * omit at most one side), and if there is a unique merge base1138 * between A and B, A...B names that merge base.1139 *1140 * (b) If <something> is _not_ a commit, either "--" is present1141 * or <something> is not a path, no -t or -b was given, and1142 * and there is a tracking branch whose name is <something>1143 * in one and only one remote, then this is a short-hand to1144 * fork local <something> from that remote-tracking branch.1145 *1146 * (c) Otherwise, if "--" is present, treat it like case (1).1147 *1148 * (d) Otherwise :1149 * - if it's a reference, treat it like case (1)1150 * - else if it's a path, treat it like case (2)1151 * - else: fail.1152 *1153 * case 4: git checkout <something> <paths>1154 *1155 * The first argument must not be ambiguous.1156 * - If it's *only* a reference, treat it like case (1).1157 * - If it's only a path, treat it like case (2).1158 * - else: fail.1159 *1160 */1161if(!argc)1162return0;11631164 arg = argv[0];1165 dash_dash_pos = -1;1166for(i =0; i < argc; i++) {1167if(!strcmp(argv[i],"--")) {1168 dash_dash_pos = i;1169break;1170}1171}1172if(dash_dash_pos ==0)1173return1;/* case (2) */1174else if(dash_dash_pos ==1)1175 has_dash_dash =1;/* case (3) or (1) */1176else if(dash_dash_pos >=2)1177die(_("only one reference expected,%dgiven."), dash_dash_pos);11781179if(!strcmp(arg,"-"))1180 arg ="@{-1}";11811182if(get_sha1_mb(arg, rev)) {1183/*1184 * Either case (3) or (4), with <something> not being1185 * a commit, or an attempt to use case (1) with an1186 * invalid ref.1187 *1188 * It's likely an error, but we need to find out if1189 * we should auto-create the branch, case (3).(b).1190 */1191int recover_with_dwim = dwim_new_local_branch_ok;11921193if(check_filename(NULL, arg) && !has_dash_dash)1194 recover_with_dwim =0;1195/*1196 * Accept "git checkout foo" and "git checkout foo --"1197 * as candidates for dwim.1198 */1199if(!(argc ==1&& !has_dash_dash) &&1200!(argc ==2&& has_dash_dash))1201 recover_with_dwim =0;12021203if(recover_with_dwim) {1204const char*remote =unique_tracking_name(arg, rev);1205if(remote) {1206*new_branch = arg;1207 arg = remote;1208/* DWIMmed to create local branch, case (3).(b) */1209}else{1210 recover_with_dwim =0;1211}1212}12131214if(!recover_with_dwim) {1215if(has_dash_dash)1216die(_("invalid reference:%s"), arg);1217return argcount;1218}1219}12201221/* we can't end up being in (2) anymore, eat the argument */1222 argcount++;1223 argv++;1224 argc--;12251226new->name = arg;1227setup_branch_path(new);12281229if(!check_refname_format(new->path,0) &&1230!read_ref(new->path, branch_rev))1231hashcpy(rev, branch_rev);1232else1233new->path = NULL;/* not an existing branch */12341235if(new->path && !force_detach && !*new_branch) {1236unsigned char sha1[20];1237int flag;1238char*head_ref =resolve_refdup("HEAD",0, sha1, &flag);1239if(head_ref &&1240(!(flag & REF_ISSYMREF) ||strcmp(head_ref,new->path)) &&1241!opts->ignore_other_worktrees)1242check_linked_checkouts(new);1243free(head_ref);1244}12451246new->commit =lookup_commit_reference_gently(rev,1);1247if(!new->commit) {1248/* not a commit */1249*source_tree =parse_tree_indirect(rev);1250}else{1251parse_commit_or_die(new->commit);1252*source_tree =new->commit->tree;1253}12541255if(!*source_tree)/* case (1): want a tree */1256die(_("reference is not a tree:%s"), arg);1257if(!has_dash_dash) {/* case (3).(d) -> (1) */1258/*1259 * Do not complain the most common case1260 * git checkout branch1261 * even if there happen to be a file called 'branch';1262 * it would be extremely annoying.1263 */1264if(argc)1265verify_non_filename(NULL, arg);1266}else{1267 argcount++;1268 argv++;1269 argc--;1270}12711272return argcount;1273}12741275static intswitch_unborn_to_new_branch(const struct checkout_opts *opts)1276{1277int status;1278struct strbuf branch_ref = STRBUF_INIT;12791280if(!opts->new_branch)1281die(_("You are on a branch yet to be born"));1282strbuf_addf(&branch_ref,"refs/heads/%s", opts->new_branch);1283 status =create_symref("HEAD", branch_ref.buf,"checkout -b");1284strbuf_release(&branch_ref);1285if(!opts->quiet)1286fprintf(stderr,_("Switched to a new branch '%s'\n"),1287 opts->new_branch);1288return status;1289}12901291static intcheckout_branch(struct checkout_opts *opts,1292struct branch_info *new)1293{1294if(opts->pathspec.nr)1295die(_("paths cannot be used with switching branches"));12961297if(opts->patch_mode)1298die(_("'%s' cannot be used with switching branches"),1299"--patch");13001301if(opts->writeout_stage)1302die(_("'%s' cannot be used with switching branches"),1303"--ours/--theirs");13041305if(opts->force && opts->merge)1306die(_("'%s' cannot be used with '%s'"),"-f","-m");13071308if(opts->force_detach && opts->new_branch)1309die(_("'%s' cannot be used with '%s'"),1310"--detach","-b/-B/--orphan");13111312if(opts->new_orphan_branch) {1313if(opts->track != BRANCH_TRACK_UNSPECIFIED)1314die(_("'%s' cannot be used with '%s'"),"--orphan","-t");1315}else if(opts->force_detach) {1316if(opts->track != BRANCH_TRACK_UNSPECIFIED)1317die(_("'%s' cannot be used with '%s'"),"--detach","-t");1318}else if(opts->track == BRANCH_TRACK_UNSPECIFIED)1319 opts->track = git_branch_track;13201321if(new->name && !new->commit)1322die(_("Cannot switch branch to a non-commit '%s'"),1323new->name);13241325if(opts->new_worktree)1326returnprepare_linked_checkout(opts,new);13271328if(!new->commit && opts->new_branch) {1329unsigned char rev[20];1330int flag;13311332if(!read_ref_full("HEAD",0, rev, &flag) &&1333(flag & REF_ISSYMREF) &&is_null_sha1(rev))1334returnswitch_unborn_to_new_branch(opts);1335}1336returnswitch_branches(opts,new);1337}13381339intcmd_checkout(int argc,const char**argv,const char*prefix)1340{1341struct checkout_opts opts;1342struct branch_info new;1343char*conflict_style = NULL;1344int dwim_new_local_branch =1;1345struct option options[] = {1346OPT__QUIET(&opts.quiet,N_("suppress progress reporting")),1347OPT_STRING('b', NULL, &opts.new_branch,N_("branch"),1348N_("create and checkout a new branch")),1349OPT_STRING('B', NULL, &opts.new_branch_force,N_("branch"),1350N_("create/reset and checkout a branch")),1351OPT_BOOL('l', NULL, &opts.new_branch_log,N_("create reflog for new branch")),1352OPT_BOOL(0,"detach", &opts.force_detach,N_("detach the HEAD at named commit")),1353OPT_SET_INT('t',"track", &opts.track,N_("set upstream info for new branch"),1354 BRANCH_TRACK_EXPLICIT),1355OPT_STRING(0,"orphan", &opts.new_orphan_branch,N_("new-branch"),N_("new unparented branch")),1356OPT_SET_INT('2',"ours", &opts.writeout_stage,N_("checkout our version for unmerged files"),13572),1358OPT_SET_INT('3',"theirs", &opts.writeout_stage,N_("checkout their version for unmerged files"),13593),1360OPT__FORCE(&opts.force,N_("force checkout (throw away local modifications)")),1361OPT_BOOL('m',"merge", &opts.merge,N_("perform a 3-way merge with the new branch")),1362OPT_BOOL(0,"overwrite-ignore", &opts.overwrite_ignore,N_("update ignored files (default)")),1363OPT_STRING(0,"conflict", &conflict_style,N_("style"),1364N_("conflict style (merge or diff3)")),1365OPT_BOOL('p',"patch", &opts.patch_mode,N_("select hunks interactively")),1366OPT_BOOL(0,"ignore-skip-worktree-bits", &opts.ignore_skipworktree,1367N_("do not limit pathspecs to sparse entries only")),1368OPT_HIDDEN_BOOL(0,"guess", &dwim_new_local_branch,1369N_("second guess 'git checkout <no-such-branch>'")),1370OPT_FILENAME(0,"to", &opts.new_worktree,1371N_("check a branch out in a separate working directory")),1372OPT_BOOL(0,"ignore-other-worktrees", &opts.ignore_other_worktrees,1373N_("do not check if another worktree is holding the given ref")),1374OPT_END(),1375};13761377memset(&opts,0,sizeof(opts));1378memset(&new,0,sizeof(new));1379 opts.overwrite_ignore =1;1380 opts.prefix = prefix;13811382 opts.saved_argv =xmalloc(sizeof(const char*) * (argc +2));1383memcpy(opts.saved_argv, argv,sizeof(const char*) * (argc +1));13841385gitmodules_config();1386git_config(git_checkout_config, &opts);13871388 opts.track = BRANCH_TRACK_UNSPECIFIED;13891390 argc =parse_options(argc, argv, prefix, options, checkout_usage,1391 PARSE_OPT_KEEP_DASHDASH);13921393/* recursive execution from checkout_new_worktree() */1394 opts.new_worktree_mode =getenv("GIT_CHECKOUT_NEW_WORKTREE") != NULL;1395if(opts.new_worktree_mode)1396 opts.new_worktree = NULL;13971398if(!opts.new_worktree)1399setup_work_tree();14001401if(conflict_style) {1402 opts.merge =1;/* implied */1403git_xmerge_config("merge.conflictstyle", conflict_style, NULL);1404}14051406if((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) >1)1407die(_("-b, -B and --orphan are mutually exclusive"));14081409/*1410 * From here on, new_branch will contain the branch to be checked out,1411 * and new_branch_force and new_orphan_branch will tell us which one of1412 * -b/-B/--orphan is being used.1413 */1414if(opts.new_branch_force)1415 opts.new_branch = opts.new_branch_force;14161417if(opts.new_orphan_branch)1418 opts.new_branch = opts.new_orphan_branch;14191420/* --track without -b/-B/--orphan should DWIM */1421if(opts.track != BRANCH_TRACK_UNSPECIFIED && !opts.new_branch) {1422const char*argv0 = argv[0];1423if(!argc || !strcmp(argv0,"--"))1424die(_("--track needs a branch name"));1425skip_prefix(argv0,"refs/", &argv0);1426skip_prefix(argv0,"remotes/", &argv0);1427 argv0 =strchr(argv0,'/');1428if(!argv0 || !argv0[1])1429die(_("Missing branch name; try -b"));1430 opts.new_branch = argv0 +1;1431}14321433/*1434 * Extract branch name from command line arguments, so1435 * all that is left is pathspecs.1436 *1437 * Handle1438 *1439 * 1) git checkout <tree> -- [<paths>]1440 * 2) git checkout -- [<paths>]1441 * 3) git checkout <something> [<paths>]1442 *1443 * including "last branch" syntax and DWIM-ery for names of1444 * remote branches, erroring out for invalid or ambiguous cases.1445 */1446if(argc) {1447unsigned char rev[20];1448int dwim_ok =1449!opts.patch_mode &&1450 dwim_new_local_branch &&1451 opts.track == BRANCH_TRACK_UNSPECIFIED &&1452!opts.new_branch;1453int n =parse_branchname_arg(argc, argv, dwim_ok,1454&new, &opts, rev);1455 argv += n;1456 argc -= n;1457}14581459if(argc) {1460parse_pathspec(&opts.pathspec,0,1461 opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN :0,1462 prefix, argv);14631464if(!opts.pathspec.nr)1465die(_("invalid path specification"));14661467/*1468 * Try to give more helpful suggestion.1469 * new_branch && argc > 1 will be caught later.1470 */1471if(opts.new_branch && argc ==1)1472die(_("Cannot update paths and switch to branch '%s' at the same time.\n"1473"Did you intend to checkout '%s' which can not be resolved as commit?"),1474 opts.new_branch, argv[0]);14751476if(opts.force_detach)1477die(_("git checkout: --detach does not take a path argument '%s'"),1478 argv[0]);14791480if(1< !!opts.writeout_stage + !!opts.force + !!opts.merge)1481die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"1482"checking out of the index."));1483}14841485if(opts.new_branch) {1486struct strbuf buf = STRBUF_INIT;14871488 opts.branch_exists =1489validate_new_branchname(opts.new_branch, &buf,1490!!opts.new_branch_force,1491!!opts.new_branch_force);14921493strbuf_release(&buf);1494}14951496if(opts.patch_mode || opts.pathspec.nr)1497returncheckout_paths(&opts,new.name);1498else1499returncheckout_branch(&opts, &new);1500}