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