Merge branch 'jc/checkout-detach-wo-tracking-report'
[alt-git.git] / builtin / checkout.c
blob947827de1d1abc7397ce4cb7190f7faf6e83d787
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "advice.h"
4 #include "branch.h"
5 #include "cache-tree.h"
6 #include "checkout.h"
7 #include "commit.h"
8 #include "config.h"
9 #include "diff.h"
10 #include "dir.h"
11 #include "environment.h"
12 #include "gettext.h"
13 #include "hex.h"
14 #include "hook.h"
15 #include "merge-ll.h"
16 #include "lockfile.h"
17 #include "mem-pool.h"
18 #include "merge-recursive.h"
19 #include "object-name.h"
20 #include "object-store-ll.h"
21 #include "parse-options.h"
22 #include "path.h"
23 #include "preload-index.h"
24 #include "read-cache.h"
25 #include "refs.h"
26 #include "remote.h"
27 #include "resolve-undo.h"
28 #include "revision.h"
29 #include "setup.h"
30 #include "submodule.h"
31 #include "symlinks.h"
32 #include "trace2.h"
33 #include "tree.h"
34 #include "tree-walk.h"
35 #include "unpack-trees.h"
36 #include "wt-status.h"
37 #include "xdiff-interface.h"
38 #include "entry.h"
39 #include "parallel-checkout.h"
40 #include "add-interactive.h"
42 static const char * const checkout_usage[] = {
43 N_("git checkout [<options>] <branch>"),
44 N_("git checkout [<options>] [<branch>] -- <file>..."),
45 NULL,
48 static const char * const switch_branch_usage[] = {
49 N_("git switch [<options>] [<branch>]"),
50 NULL,
53 static const char * const restore_usage[] = {
54 N_("git restore [<options>] [--source=<branch>] <file>..."),
55 NULL,
58 struct checkout_opts {
59 int patch_mode;
60 int quiet;
61 int merge;
62 int force;
63 int force_detach;
64 int implicit_detach;
65 int writeout_stage;
66 int overwrite_ignore;
67 int ignore_skipworktree;
68 int ignore_other_worktrees;
69 int show_progress;
70 int count_checkout_paths;
71 int overlay_mode;
72 int dwim_new_local_branch;
73 int discard_changes;
74 int accept_ref;
75 int accept_pathspec;
76 int switch_branch_doing_nothing_is_ok;
77 int only_merge_on_switching_branches;
78 int can_switch_when_in_progress;
79 int orphan_from_empty_tree;
80 int empty_pathspec_ok;
81 int checkout_index;
82 int checkout_worktree;
83 const char *ignore_unmerged_opt;
84 int ignore_unmerged;
85 int pathspec_file_nul;
86 char *pathspec_from_file;
88 const char *new_branch;
89 const char *new_branch_force;
90 const char *new_orphan_branch;
91 int new_branch_log;
92 enum branch_track track;
93 struct diff_options diff_options;
94 int conflict_style;
96 int branch_exists;
97 const char *prefix;
98 struct pathspec pathspec;
99 const char *from_treeish;
100 struct tree *source_tree;
103 #define CHECKOUT_OPTS_INIT { .conflict_style = -1, .merge = -1 }
105 struct branch_info {
106 char *name; /* The short name used */
107 char *path; /* The full name of a real branch */
108 struct commit *commit; /* The named commit */
109 char *refname; /* The full name of the ref being checked out. */
110 struct object_id oid; /* The object ID of the commit being checked out. */
112 * if not null the branch is detached because it's already
113 * checked out in this checkout
115 char *checkout;
118 static void branch_info_release(struct branch_info *info)
120 free(info->name);
121 free(info->path);
122 free(info->refname);
123 free(info->checkout);
126 static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
127 int changed)
129 return run_hooks_l("post-checkout",
130 oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()),
131 oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
132 changed ? "1" : "0", NULL);
133 /* "new_commit" can be NULL when checking out from the index before
134 a commit exists. */
138 static int update_some(const struct object_id *oid, struct strbuf *base,
139 const char *pathname, unsigned mode, void *context UNUSED)
141 int len;
142 struct cache_entry *ce;
143 int pos;
145 if (S_ISDIR(mode))
146 return READ_TREE_RECURSIVE;
148 len = base->len + strlen(pathname);
149 ce = make_empty_cache_entry(&the_index, len);
150 oidcpy(&ce->oid, oid);
151 memcpy(ce->name, base->buf, base->len);
152 memcpy(ce->name + base->len, pathname, len - base->len);
153 ce->ce_flags = create_ce_flags(0) | CE_UPDATE;
154 ce->ce_namelen = len;
155 ce->ce_mode = create_ce_mode(mode);
158 * If the entry is the same as the current index, we can leave the old
159 * entry in place. Whether it is UPTODATE or not, checkout_entry will
160 * do the right thing.
162 pos = index_name_pos(&the_index, ce->name, ce->ce_namelen);
163 if (pos >= 0) {
164 struct cache_entry *old = the_index.cache[pos];
165 if (ce->ce_mode == old->ce_mode &&
166 !ce_intent_to_add(old) &&
167 oideq(&ce->oid, &old->oid)) {
168 old->ce_flags |= CE_UPDATE;
169 discard_cache_entry(ce);
170 return 0;
174 add_index_entry(&the_index, ce,
175 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
176 return 0;
179 static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
181 read_tree(the_repository, tree,
182 pathspec, update_some, NULL);
184 /* update the index with the given tree's info
185 * for all args, expanding wildcards, and exit
186 * with any non-zero return code.
188 return 0;
191 static int skip_same_name(const struct cache_entry *ce, int pos)
193 while (++pos < the_index.cache_nr &&
194 !strcmp(the_index.cache[pos]->name, ce->name))
195 ; /* skip */
196 return pos;
199 static int check_stage(int stage, const struct cache_entry *ce, int pos,
200 int overlay_mode)
202 while (pos < the_index.cache_nr &&
203 !strcmp(the_index.cache[pos]->name, ce->name)) {
204 if (ce_stage(the_index.cache[pos]) == stage)
205 return 0;
206 pos++;
208 if (!overlay_mode)
209 return 0;
210 if (stage == 2)
211 return error(_("path '%s' does not have our version"), ce->name);
212 else
213 return error(_("path '%s' does not have their version"), ce->name);
216 static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
218 unsigned seen = 0;
219 const char *name = ce->name;
221 while (pos < the_index.cache_nr) {
222 ce = the_index.cache[pos];
223 if (strcmp(name, ce->name))
224 break;
225 seen |= (1 << ce_stage(ce));
226 pos++;
228 if ((stages & seen) != stages)
229 return error(_("path '%s' does not have all necessary versions"),
230 name);
231 return 0;
234 static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
235 const struct checkout *state, int *nr_checkouts,
236 int overlay_mode)
238 while (pos < the_index.cache_nr &&
239 !strcmp(the_index.cache[pos]->name, ce->name)) {
240 if (ce_stage(the_index.cache[pos]) == stage)
241 return checkout_entry(the_index.cache[pos], state,
242 NULL, nr_checkouts);
243 pos++;
245 if (!overlay_mode) {
246 unlink_entry(ce, NULL);
247 return 0;
249 if (stage == 2)
250 return error(_("path '%s' does not have our version"), ce->name);
251 else
252 return error(_("path '%s' does not have their version"), ce->name);
255 static int checkout_merged(int pos, const struct checkout *state,
256 int *nr_checkouts, struct mem_pool *ce_mem_pool,
257 int conflict_style)
259 struct cache_entry *ce = the_index.cache[pos];
260 const char *path = ce->name;
261 mmfile_t ancestor, ours, theirs;
262 enum ll_merge_result merge_status;
263 int status;
264 struct object_id oid;
265 mmbuffer_t result_buf;
266 struct object_id threeway[3];
267 unsigned mode = 0;
268 struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT;
269 int renormalize = 0;
271 memset(threeway, 0, sizeof(threeway));
272 while (pos < the_index.cache_nr) {
273 int stage;
274 stage = ce_stage(ce);
275 if (!stage || strcmp(path, ce->name))
276 break;
277 oidcpy(&threeway[stage - 1], &ce->oid);
278 if (stage == 2)
279 mode = create_ce_mode(ce->ce_mode);
280 pos++;
281 ce = the_index.cache[pos];
283 if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2]))
284 return error(_("path '%s' does not have necessary versions"), path);
286 read_mmblob(&ancestor, &threeway[0]);
287 read_mmblob(&ours, &threeway[1]);
288 read_mmblob(&theirs, &threeway[2]);
290 git_config_get_bool("merge.renormalize", &renormalize);
291 ll_opts.renormalize = renormalize;
292 ll_opts.conflict_style = conflict_style;
293 merge_status = ll_merge(&result_buf, path, &ancestor, "base",
294 &ours, "ours", &theirs, "theirs",
295 state->istate, &ll_opts);
296 free(ancestor.ptr);
297 free(ours.ptr);
298 free(theirs.ptr);
299 if (merge_status == LL_MERGE_BINARY_CONFLICT)
300 warning("Cannot merge binary files: %s (%s vs. %s)",
301 path, "ours", "theirs");
302 if (merge_status < 0 || !result_buf.ptr) {
303 free(result_buf.ptr);
304 return error(_("path '%s': cannot merge"), path);
308 * NEEDSWORK:
309 * There is absolutely no reason to write this as a blob object
310 * and create a phony cache entry. This hack is primarily to get
311 * to the write_entry() machinery that massages the contents to
312 * work-tree format and writes out which only allows it for a
313 * cache entry. The code in write_entry() needs to be refactored
314 * to allow us to feed a <buffer, size, mode> instead of a cache
315 * entry. Such a refactoring would help merge_recursive as well
316 * (it also writes the merge result to the object database even
317 * when it may contain conflicts).
319 if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid))
320 die(_("Unable to add merge result for '%s'"), path);
321 free(result_buf.ptr);
322 ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);
323 if (!ce)
324 die(_("make_cache_entry failed for path '%s'"), path);
325 status = checkout_entry(ce, state, NULL, nr_checkouts);
326 return status;
329 static void mark_ce_for_checkout_overlay(struct cache_entry *ce,
330 char *ps_matched,
331 const struct checkout_opts *opts)
333 ce->ce_flags &= ~CE_MATCHED;
334 if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
335 return;
336 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
338 * "git checkout tree-ish -- path", but this entry
339 * is in the original index but is not in tree-ish
340 * or does not match the pathspec; it will not be
341 * checked out to the working tree. We will not do
342 * anything to this entry at all.
344 return;
346 * Either this entry came from the tree-ish we are
347 * checking the paths out of, or we are checking out
348 * of the index.
350 * If it comes from the tree-ish, we already know it
351 * matches the pathspec and could just stamp
352 * CE_MATCHED to it from update_some(). But we still
353 * need ps_matched and read_tree (and
354 * eventually tree_entry_interesting) cannot fill
355 * ps_matched yet. Once it can, we can avoid calling
356 * match_pathspec() for _all_ entries when
357 * opts->source_tree != NULL.
359 if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
360 ce->ce_flags |= CE_MATCHED;
363 static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
364 char *ps_matched,
365 const struct checkout_opts *opts)
367 ce->ce_flags &= ~CE_MATCHED;
368 if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
369 return;
370 if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) {
371 ce->ce_flags |= CE_MATCHED;
372 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
374 * In overlay mode, but the path is not in
375 * tree-ish, which means we should remove it
376 * from the index and the working tree.
378 ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE;
382 static int checkout_worktree(const struct checkout_opts *opts,
383 const struct branch_info *info)
385 struct checkout state = CHECKOUT_INIT;
386 int nr_checkouts = 0, nr_unmerged = 0;
387 int errs = 0;
388 int pos;
389 int pc_workers, pc_threshold;
390 struct mem_pool ce_mem_pool;
392 state.force = 1;
393 state.refresh_cache = 1;
394 state.istate = &the_index;
396 mem_pool_init(&ce_mem_pool, 0);
397 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
398 init_checkout_metadata(&state.meta, info->refname,
399 info->commit ? &info->commit->object.oid : &info->oid,
400 NULL);
402 enable_delayed_checkout(&state);
404 if (pc_workers > 1)
405 init_parallel_checkout();
407 for (pos = 0; pos < the_index.cache_nr; pos++) {
408 struct cache_entry *ce = the_index.cache[pos];
409 if (ce->ce_flags & CE_MATCHED) {
410 if (!ce_stage(ce)) {
411 errs |= checkout_entry(ce, &state,
412 NULL, &nr_checkouts);
413 continue;
415 if (opts->writeout_stage)
416 errs |= checkout_stage(opts->writeout_stage,
417 ce, pos,
418 &state,
419 &nr_checkouts, opts->overlay_mode);
420 else if (opts->merge)
421 errs |= checkout_merged(pos, &state,
422 &nr_unmerged,
423 &ce_mem_pool,
424 opts->conflict_style);
425 pos = skip_same_name(ce, pos) - 1;
428 if (pc_workers > 1)
429 errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
430 NULL, NULL);
431 mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
432 remove_marked_cache_entries(&the_index, 1);
433 remove_scheduled_dirs();
434 errs |= finish_delayed_checkout(&state, opts->show_progress);
436 if (opts->count_checkout_paths) {
437 if (nr_unmerged)
438 fprintf_ln(stderr, Q_("Recreated %d merge conflict",
439 "Recreated %d merge conflicts",
440 nr_unmerged),
441 nr_unmerged);
442 if (opts->source_tree)
443 fprintf_ln(stderr, Q_("Updated %d path from %s",
444 "Updated %d paths from %s",
445 nr_checkouts),
446 nr_checkouts,
447 repo_find_unique_abbrev(the_repository, &opts->source_tree->object.oid,
448 DEFAULT_ABBREV));
449 else if (!nr_unmerged || nr_checkouts)
450 fprintf_ln(stderr, Q_("Updated %d path from the index",
451 "Updated %d paths from the index",
452 nr_checkouts),
453 nr_checkouts);
456 return errs;
459 static int checkout_paths(const struct checkout_opts *opts,
460 const struct branch_info *new_branch_info)
462 int pos;
463 static char *ps_matched;
464 struct object_id rev;
465 struct commit *head;
466 int errs = 0;
467 struct lock_file lock_file = LOCK_INIT;
468 int checkout_index;
470 trace2_cmd_mode(opts->patch_mode ? "patch" : "path");
472 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
473 die(_("'%s' cannot be used with updating paths"), "--track");
475 if (opts->new_branch_log)
476 die(_("'%s' cannot be used with updating paths"), "-l");
478 if (opts->ignore_unmerged && opts->patch_mode)
479 die(_("'%s' cannot be used with updating paths"),
480 opts->ignore_unmerged_opt);
482 if (opts->force_detach)
483 die(_("'%s' cannot be used with updating paths"), "--detach");
485 if (opts->merge && opts->patch_mode)
486 die(_("options '%s' and '%s' cannot be used together"), "--merge", "--patch");
488 if (opts->ignore_unmerged && opts->merge)
489 die(_("options '%s' and '%s' cannot be used together"),
490 opts->ignore_unmerged_opt, "-m");
492 if (opts->new_branch)
493 die(_("Cannot update paths and switch to branch '%s' at the same time."),
494 opts->new_branch);
496 if (!opts->checkout_worktree && !opts->checkout_index)
497 die(_("neither '%s' or '%s' is specified"),
498 "--staged", "--worktree");
500 if (!opts->checkout_worktree && !opts->from_treeish)
501 die(_("'%s' must be used when '%s' is not specified"),
502 "--worktree", "--source");
505 * Reject --staged option to the restore command when combined with
506 * merge-related options. Use the accept_ref flag to distinguish it
507 * from the checkout command, which does not accept --staged anyway.
509 * `restore --ours|--theirs --worktree --staged` could mean resolving
510 * conflicted paths to one side in both the worktree and the index,
511 * but does not currently.
513 * `restore --merge|--conflict=<style>` already recreates conflicts
514 * in both the worktree and the index, so adding --staged would be
515 * meaningless.
517 if (!opts->accept_ref && opts->checkout_index) {
518 if (opts->writeout_stage)
519 die(_("'%s' or '%s' cannot be used with %s"),
520 "--ours", "--theirs", "--staged");
522 if (opts->merge)
523 die(_("'%s' or '%s' cannot be used with %s"),
524 "--merge", "--conflict", "--staged");
528 * recreating unmerged index entries and writing out data from
529 * unmerged index entries would make no sense when checking out
530 * of a tree-ish.
532 if ((opts->merge || opts->writeout_stage) && opts->source_tree)
533 die(_("'%s', '%s', or '%s' cannot be used when checking out of a tree"),
534 "--merge", "--ours", "--theirs");
536 if (opts->patch_mode) {
537 enum add_p_mode patch_mode;
538 const char *rev = new_branch_info->name;
539 char rev_oid[GIT_MAX_HEXSZ + 1];
542 * Since rev can be in the form of `<a>...<b>` (which is not
543 * recognized by diff-index), we will always replace the name
544 * with the hex of the commit (whether it's in `...` form or
545 * not) for the run_add_interactive() machinery to work
546 * properly. However, there is special logic for the HEAD case
547 * so we mustn't replace that. Also, when we were given a
548 * tree-object, new_branch_info->commit would be NULL, but we
549 * do not have to do any replacement, either.
551 if (rev && new_branch_info->commit && strcmp(rev, "HEAD"))
552 rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid);
554 if (opts->checkout_index && opts->checkout_worktree)
555 patch_mode = ADD_P_CHECKOUT;
556 else if (opts->checkout_index && !opts->checkout_worktree)
557 patch_mode = ADD_P_RESET;
558 else if (!opts->checkout_index && opts->checkout_worktree)
559 patch_mode = ADD_P_WORKTREE;
560 else
561 BUG("either flag must have been set, worktree=%d, index=%d",
562 opts->checkout_worktree, opts->checkout_index);
563 return !!run_add_p(the_repository, patch_mode, rev,
564 &opts->pathspec);
567 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
568 if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0)
569 return error(_("index file corrupt"));
571 if (opts->source_tree)
572 read_tree_some(opts->source_tree, &opts->pathspec);
573 if (opts->merge)
574 unmerge_index(&the_index, &opts->pathspec, CE_MATCHED);
576 ps_matched = xcalloc(opts->pathspec.nr, 1);
579 * Make sure all pathspecs participated in locating the paths
580 * to be checked out.
582 for (pos = 0; pos < the_index.cache_nr; pos++)
583 if (opts->overlay_mode)
584 mark_ce_for_checkout_overlay(the_index.cache[pos],
585 ps_matched,
586 opts);
587 else
588 mark_ce_for_checkout_no_overlay(the_index.cache[pos],
589 ps_matched,
590 opts);
592 if (report_path_error(ps_matched, &opts->pathspec)) {
593 free(ps_matched);
594 return 1;
596 free(ps_matched);
598 /* Any unmerged paths? */
599 for (pos = 0; pos < the_index.cache_nr; pos++) {
600 const struct cache_entry *ce = the_index.cache[pos];
601 if (ce->ce_flags & CE_MATCHED) {
602 if (!ce_stage(ce))
603 continue;
604 if (opts->ignore_unmerged) {
605 if (!opts->quiet)
606 warning(_("path '%s' is unmerged"), ce->name);
607 } else if (opts->writeout_stage) {
608 errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode);
609 } else if (opts->merge) {
610 errs |= check_stages((1<<2) | (1<<3), ce, pos);
611 } else {
612 errs = 1;
613 error(_("path '%s' is unmerged"), ce->name);
615 pos = skip_same_name(ce, pos) - 1;
618 if (errs)
619 return 1;
621 /* Now we are committed to check them out */
622 if (opts->checkout_worktree)
623 errs |= checkout_worktree(opts, new_branch_info);
624 else
625 remove_marked_cache_entries(&the_index, 1);
628 * Allow updating the index when checking out from the index.
629 * This is to save new stat info.
631 if (opts->checkout_worktree && !opts->checkout_index && !opts->source_tree)
632 checkout_index = 1;
633 else
634 checkout_index = opts->checkout_index;
636 if (checkout_index) {
637 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
638 die(_("unable to write new index file"));
639 } else {
641 * NEEDSWORK: if --worktree is not specified, we
642 * should save stat info of checked out files in the
643 * index to avoid the next (potentially costly)
644 * refresh. But it's a bit tricker to do...
646 rollback_lock_file(&lock_file);
649 read_ref_full("HEAD", 0, &rev, NULL);
650 head = lookup_commit_reference_gently(the_repository, &rev, 1);
652 errs |= post_checkout_hook(head, head, 0);
653 return errs;
656 static void show_local_changes(struct object *head,
657 const struct diff_options *opts)
659 struct rev_info rev;
660 /* I think we want full paths, even if we're in a subdirectory. */
661 repo_init_revisions(the_repository, &rev, NULL);
662 rev.diffopt.flags = opts->flags;
663 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;
664 rev.diffopt.flags.recursive = 1;
665 diff_setup_done(&rev.diffopt);
666 add_pending_object(&rev, head, NULL);
667 run_diff_index(&rev, 0);
668 release_revisions(&rev);
671 static void describe_detached_head(const char *msg, struct commit *commit)
673 struct strbuf sb = STRBUF_INIT;
675 if (!repo_parse_commit(the_repository, commit))
676 pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
677 if (print_sha1_ellipsis()) {
678 fprintf(stderr, "%s %s... %s\n", msg,
679 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV),
680 sb.buf);
681 } else {
682 fprintf(stderr, "%s %s %s\n", msg,
683 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV),
684 sb.buf);
686 strbuf_release(&sb);
689 static int reset_tree(struct tree *tree, const struct checkout_opts *o,
690 int worktree, int *writeout_error,
691 struct branch_info *info)
693 struct unpack_trees_options opts;
694 struct tree_desc tree_desc;
696 memset(&opts, 0, sizeof(opts));
697 opts.head_idx = -1;
698 opts.update = worktree;
699 opts.skip_unmerged = !worktree;
700 opts.reset = o->force ? UNPACK_RESET_OVERWRITE_UNTRACKED :
701 UNPACK_RESET_PROTECT_UNTRACKED;
702 opts.preserve_ignored = (!o->force && !o->overwrite_ignore);
703 opts.merge = 1;
704 opts.fn = oneway_merge;
705 opts.verbose_update = o->show_progress;
706 opts.src_index = &the_index;
707 opts.dst_index = &the_index;
708 init_checkout_metadata(&opts.meta, info->refname,
709 info->commit ? &info->commit->object.oid : null_oid(),
710 NULL);
711 if (parse_tree(tree) < 0)
712 return 128;
713 init_tree_desc(&tree_desc, &tree->object.oid, tree->buffer, tree->size);
714 switch (unpack_trees(1, &tree_desc, &opts)) {
715 case -2:
716 *writeout_error = 1;
718 * We return 0 nevertheless, as the index is all right
719 * and more importantly we have made best efforts to
720 * update paths in the work tree, and we cannot revert
721 * them.
723 /* fallthrough */
724 case 0:
725 return 0;
726 default:
727 return 128;
731 static void setup_branch_path(struct branch_info *branch)
733 struct strbuf buf = STRBUF_INIT;
736 * If this is a ref, resolve it; otherwise, look up the OID for our
737 * expression. Failure here is okay.
739 if (!repo_dwim_ref(the_repository, branch->name, strlen(branch->name),
740 &branch->oid, &branch->refname, 0))
741 repo_get_oid_committish(the_repository, branch->name, &branch->oid);
743 strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
744 if (strcmp(buf.buf, branch->name)) {
745 free(branch->name);
746 branch->name = xstrdup(buf.buf);
748 strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
749 free(branch->path);
750 branch->path = strbuf_detach(&buf, NULL);
753 static void init_topts(struct unpack_trees_options *topts, int merge,
754 int show_progress, int overwrite_ignore,
755 struct commit *old_commit)
757 memset(topts, 0, sizeof(*topts));
758 topts->head_idx = -1;
759 topts->src_index = &the_index;
760 topts->dst_index = &the_index;
762 setup_unpack_trees_porcelain(topts, "checkout");
764 topts->initial_checkout = is_index_unborn(&the_index);
765 topts->update = 1;
766 topts->merge = 1;
767 topts->quiet = merge && old_commit;
768 topts->verbose_update = show_progress;
769 topts->fn = twoway_merge;
770 topts->preserve_ignored = !overwrite_ignore;
773 static int merge_working_tree(const struct checkout_opts *opts,
774 struct branch_info *old_branch_info,
775 struct branch_info *new_branch_info,
776 int *writeout_error)
778 int ret;
779 struct lock_file lock_file = LOCK_INIT;
780 struct tree *new_tree;
782 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
783 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
784 return error(_("index file corrupt"));
786 resolve_undo_clear_index(&the_index);
787 if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
788 if (new_branch_info->commit)
789 BUG("'switch --orphan' should never accept a commit as starting point");
790 new_tree = parse_tree_indirect(the_hash_algo->empty_tree);
791 if (!new_tree)
792 BUG("unable to read empty tree");
793 } else {
794 new_tree = repo_get_commit_tree(the_repository,
795 new_branch_info->commit);
796 if (!new_tree)
797 return error(_("unable to read tree (%s)"),
798 oid_to_hex(&new_branch_info->commit->object.oid));
800 if (opts->discard_changes) {
801 ret = reset_tree(new_tree, opts, 1, writeout_error, new_branch_info);
802 if (ret)
803 return ret;
804 } else {
805 struct tree_desc trees[2];
806 struct tree *tree;
807 struct unpack_trees_options topts;
808 const struct object_id *old_commit_oid;
810 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
812 if (unmerged_index(&the_index)) {
813 error(_("you need to resolve your current index first"));
814 return 1;
817 /* 2-way merge to the new branch */
818 init_topts(&topts, opts->merge, opts->show_progress,
819 opts->overwrite_ignore, old_branch_info->commit);
820 init_checkout_metadata(&topts.meta, new_branch_info->refname,
821 new_branch_info->commit ?
822 &new_branch_info->commit->object.oid :
823 &new_branch_info->oid, NULL);
825 old_commit_oid = old_branch_info->commit ?
826 &old_branch_info->commit->object.oid :
827 the_hash_algo->empty_tree;
828 tree = parse_tree_indirect(old_commit_oid);
829 if (!tree)
830 die(_("unable to parse commit %s"),
831 oid_to_hex(old_commit_oid));
833 init_tree_desc(&trees[0], &tree->object.oid,
834 tree->buffer, tree->size);
835 if (parse_tree(new_tree) < 0)
836 exit(128);
837 tree = new_tree;
838 init_tree_desc(&trees[1], &tree->object.oid,
839 tree->buffer, tree->size);
841 ret = unpack_trees(2, trees, &topts);
842 clear_unpack_trees_porcelain(&topts);
843 if (ret == -1) {
845 * Unpack couldn't do a trivial merge; either
846 * give up or do a real merge, depending on
847 * whether the merge flag was used.
849 struct tree *work;
850 struct tree *old_tree;
851 struct merge_options o;
852 struct strbuf sb = STRBUF_INIT;
853 struct strbuf old_commit_shortname = STRBUF_INIT;
855 if (!opts->merge)
856 return 1;
859 * Without old_branch_info->commit, the below is the same as
860 * the two-tree unpack we already tried and failed.
862 if (!old_branch_info->commit)
863 return 1;
864 old_tree = repo_get_commit_tree(the_repository,
865 old_branch_info->commit);
867 if (repo_index_has_changes(the_repository, old_tree, &sb))
868 die(_("cannot continue with staged changes in "
869 "the following files:\n%s"), sb.buf);
870 strbuf_release(&sb);
872 /* Do more real merge */
875 * We update the index fully, then write the
876 * tree from the index, then merge the new
877 * branch with the current tree, with the old
878 * branch as the base. Then we reset the index
879 * (but not the working tree) to the new
880 * branch, leaving the working tree as the
881 * merged version, but skipping unmerged
882 * entries in the index.
885 add_files_to_cache(the_repository, NULL, NULL, 0, 0);
886 init_merge_options(&o, the_repository);
887 o.verbosity = 0;
888 work = write_in_core_index_as_tree(the_repository);
890 ret = reset_tree(new_tree,
891 opts, 1,
892 writeout_error, new_branch_info);
893 if (ret)
894 return ret;
895 o.ancestor = old_branch_info->name;
896 if (!old_branch_info->name) {
897 strbuf_add_unique_abbrev(&old_commit_shortname,
898 &old_branch_info->commit->object.oid,
899 DEFAULT_ABBREV);
900 o.ancestor = old_commit_shortname.buf;
902 o.branch1 = new_branch_info->name;
903 o.branch2 = "local";
904 o.conflict_style = opts->conflict_style;
905 ret = merge_trees(&o,
906 new_tree,
907 work,
908 old_tree);
909 if (ret < 0)
910 exit(128);
911 ret = reset_tree(new_tree,
912 opts, 0,
913 writeout_error, new_branch_info);
914 strbuf_release(&o.obuf);
915 strbuf_release(&old_commit_shortname);
916 if (ret)
917 return ret;
921 if (!cache_tree_fully_valid(the_index.cache_tree))
922 cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
924 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
925 die(_("unable to write new index file"));
927 if (!opts->discard_changes && !opts->quiet && new_branch_info->commit)
928 show_local_changes(&new_branch_info->commit->object, &opts->diff_options);
930 return 0;
933 static void report_tracking(struct branch_info *new_branch_info)
935 struct strbuf sb = STRBUF_INIT;
936 struct branch *branch = branch_get(new_branch_info->name);
938 if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL, 1))
939 return;
940 fputs(sb.buf, stdout);
941 strbuf_release(&sb);
944 static void update_refs_for_switch(const struct checkout_opts *opts,
945 struct branch_info *old_branch_info,
946 struct branch_info *new_branch_info)
948 struct strbuf msg = STRBUF_INIT;
949 const char *old_desc, *reflog_msg;
950 if (opts->new_branch) {
951 if (opts->new_orphan_branch) {
952 char *refname;
954 refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
955 if (opts->new_branch_log &&
956 !should_autocreate_reflog(refname)) {
957 int ret;
958 struct strbuf err = STRBUF_INIT;
960 ret = safe_create_reflog(refname, &err);
961 if (ret) {
962 fprintf(stderr, _("Can not do reflog for '%s': %s\n"),
963 opts->new_orphan_branch, err.buf);
964 strbuf_release(&err);
965 free(refname);
966 return;
968 strbuf_release(&err);
970 free(refname);
972 else
973 create_branch(the_repository,
974 opts->new_branch, new_branch_info->name,
975 opts->new_branch_force ? 1 : 0,
976 opts->new_branch_force ? 1 : 0,
977 opts->new_branch_log,
978 opts->quiet,
979 opts->track,
981 free(new_branch_info->name);
982 free(new_branch_info->refname);
983 new_branch_info->name = xstrdup(opts->new_branch);
984 setup_branch_path(new_branch_info);
987 old_desc = old_branch_info->name;
988 if (!old_desc && old_branch_info->commit)
989 old_desc = oid_to_hex(&old_branch_info->commit->object.oid);
991 reflog_msg = getenv("GIT_REFLOG_ACTION");
992 if (!reflog_msg)
993 strbuf_addf(&msg, "checkout: moving from %s to %s",
994 old_desc ? old_desc : "(invalid)", new_branch_info->name);
995 else
996 strbuf_insertstr(&msg, 0, reflog_msg);
998 if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) {
999 /* Nothing to do. */
1000 } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */
1001 update_ref(msg.buf, "HEAD", &new_branch_info->commit->object.oid, NULL,
1002 REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);
1003 if (!opts->quiet) {
1004 if (old_branch_info->path &&
1005 advice_enabled(ADVICE_DETACHED_HEAD) && !opts->force_detach)
1006 detach_advice(new_branch_info->name);
1007 describe_detached_head(_("HEAD is now at"), new_branch_info->commit);
1009 } else if (new_branch_info->path) { /* Switch branches. */
1010 if (create_symref("HEAD", new_branch_info->path, msg.buf) < 0)
1011 die(_("unable to update HEAD"));
1012 if (!opts->quiet) {
1013 if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) {
1014 if (opts->new_branch_force)
1015 fprintf(stderr, _("Reset branch '%s'\n"),
1016 new_branch_info->name);
1017 else
1018 fprintf(stderr, _("Already on '%s'\n"),
1019 new_branch_info->name);
1020 } else if (opts->new_branch) {
1021 if (opts->branch_exists)
1022 fprintf(stderr, _("Switched to and reset branch '%s'\n"), new_branch_info->name);
1023 else
1024 fprintf(stderr, _("Switched to a new branch '%s'\n"), new_branch_info->name);
1025 } else {
1026 fprintf(stderr, _("Switched to branch '%s'\n"),
1027 new_branch_info->name);
1030 if (old_branch_info->path && old_branch_info->name) {
1031 if (!ref_exists(old_branch_info->path) && reflog_exists(old_branch_info->path))
1032 delete_reflog(old_branch_info->path);
1035 remove_branch_state(the_repository, !opts->quiet);
1036 strbuf_release(&msg);
1037 if (!opts->quiet &&
1038 !opts->force_detach &&
1039 (new_branch_info->path || !strcmp(new_branch_info->name, "HEAD")))
1040 report_tracking(new_branch_info);
1043 static int add_pending_uninteresting_ref(const char *refname,
1044 const struct object_id *oid,
1045 int flags UNUSED, void *cb_data)
1047 add_pending_oid(cb_data, refname, oid, UNINTERESTING);
1048 return 0;
1051 static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
1053 strbuf_addstr(sb, " ");
1054 strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV);
1055 strbuf_addch(sb, ' ');
1056 if (!repo_parse_commit(the_repository, commit))
1057 pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
1058 strbuf_addch(sb, '\n');
1061 #define ORPHAN_CUTOFF 4
1062 static void suggest_reattach(struct commit *commit, struct rev_info *revs)
1064 struct commit *c, *last = NULL;
1065 struct strbuf sb = STRBUF_INIT;
1066 int lost = 0;
1067 while ((c = get_revision(revs)) != NULL) {
1068 if (lost < ORPHAN_CUTOFF)
1069 describe_one_orphan(&sb, c);
1070 last = c;
1071 lost++;
1073 if (ORPHAN_CUTOFF < lost) {
1074 int more = lost - ORPHAN_CUTOFF;
1075 if (more == 1)
1076 describe_one_orphan(&sb, last);
1077 else
1078 strbuf_addf(&sb, _(" ... and %d more.\n"), more);
1081 fprintf(stderr,
1083 /* The singular version */
1084 "Warning: you are leaving %d commit behind, "
1085 "not connected to\n"
1086 "any of your branches:\n\n"
1087 "%s\n",
1088 /* The plural version */
1089 "Warning: you are leaving %d commits behind, "
1090 "not connected to\n"
1091 "any of your branches:\n\n"
1092 "%s\n",
1093 /* Give ngettext() the count */
1094 lost),
1095 lost,
1096 sb.buf);
1097 strbuf_release(&sb);
1099 if (advice_enabled(ADVICE_DETACHED_HEAD))
1100 fprintf(stderr,
1102 /* The singular version */
1103 "If you want to keep it by creating a new branch, "
1104 "this may be a good time\nto do so with:\n\n"
1105 " git branch <new-branch-name> %s\n\n",
1106 /* The plural version */
1107 "If you want to keep them by creating a new branch, "
1108 "this may be a good time\nto do so with:\n\n"
1109 " git branch <new-branch-name> %s\n\n",
1110 /* Give ngettext() the count */
1111 lost),
1112 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV));
1116 * We are about to leave commit that was at the tip of a detached
1117 * HEAD. If it is not reachable from any ref, this is the last chance
1118 * for the user to do so without resorting to reflog.
1120 static void orphaned_commit_warning(struct commit *old_commit, struct commit *new_commit)
1122 struct rev_info revs;
1123 struct object *object = &old_commit->object;
1125 repo_init_revisions(the_repository, &revs, NULL);
1126 setup_revisions(0, NULL, &revs, NULL);
1128 object->flags &= ~UNINTERESTING;
1129 add_pending_object(&revs, object, oid_to_hex(&object->oid));
1131 for_each_ref(add_pending_uninteresting_ref, &revs);
1132 if (new_commit)
1133 add_pending_oid(&revs, "HEAD",
1134 &new_commit->object.oid,
1135 UNINTERESTING);
1137 if (prepare_revision_walk(&revs))
1138 die(_("internal error in revision walk"));
1139 if (!(old_commit->object.flags & UNINTERESTING))
1140 suggest_reattach(old_commit, &revs);
1141 else
1142 describe_detached_head(_("Previous HEAD position was"), old_commit);
1144 /* Clean up objects used, as they will be reused. */
1145 repo_clear_commit_marks(the_repository, ALL_REV_FLAGS);
1146 release_revisions(&revs);
1149 static int switch_branches(const struct checkout_opts *opts,
1150 struct branch_info *new_branch_info)
1152 int ret = 0;
1153 struct branch_info old_branch_info = { 0 };
1154 struct object_id rev;
1155 int flag, writeout_error = 0;
1156 int do_merge = 1;
1158 trace2_cmd_mode("branch");
1160 memset(&old_branch_info, 0, sizeof(old_branch_info));
1161 old_branch_info.path = resolve_refdup("HEAD", 0, &rev, &flag);
1162 if (old_branch_info.path)
1163 old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1);
1164 if (!(flag & REF_ISSYMREF))
1165 FREE_AND_NULL(old_branch_info.path);
1167 if (old_branch_info.path) {
1168 const char *const prefix = "refs/heads/";
1169 const char *p;
1170 if (skip_prefix(old_branch_info.path, prefix, &p))
1171 old_branch_info.name = xstrdup(p);
1174 if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
1175 if (new_branch_info->name)
1176 BUG("'switch --orphan' should never accept a commit as starting point");
1177 new_branch_info->commit = NULL;
1178 new_branch_info->name = xstrdup("(empty)");
1179 do_merge = 1;
1182 if (!new_branch_info->name) {
1183 new_branch_info->name = xstrdup("HEAD");
1184 new_branch_info->commit = old_branch_info.commit;
1185 if (!new_branch_info->commit)
1186 die(_("You are on a branch yet to be born"));
1187 parse_commit_or_die(new_branch_info->commit);
1189 if (opts->only_merge_on_switching_branches)
1190 do_merge = 0;
1193 if (do_merge) {
1194 ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
1195 if (ret) {
1196 branch_info_release(&old_branch_info);
1197 return ret;
1201 if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)
1202 orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit);
1204 update_refs_for_switch(opts, &old_branch_info, new_branch_info);
1206 ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1);
1207 branch_info_release(&old_branch_info);
1209 return ret || writeout_error;
1212 static int git_checkout_config(const char *var, const char *value,
1213 const struct config_context *ctx, void *cb)
1215 struct checkout_opts *opts = cb;
1217 if (!strcmp(var, "diff.ignoresubmodules")) {
1218 if (!value)
1219 return config_error_nonbool(var);
1220 handle_ignore_submodules_arg(&opts->diff_options, value);
1221 return 0;
1223 if (!strcmp(var, "checkout.guess")) {
1224 opts->dwim_new_local_branch = git_config_bool(var, value);
1225 return 0;
1228 if (starts_with(var, "submodule."))
1229 return git_default_submodule_config(var, value, NULL);
1231 return git_xmerge_config(var, value, ctx, NULL);
1234 static void setup_new_branch_info_and_source_tree(
1235 struct branch_info *new_branch_info,
1236 struct checkout_opts *opts,
1237 struct object_id *rev,
1238 const char *arg)
1240 struct tree **source_tree = &opts->source_tree;
1241 struct object_id branch_rev;
1243 /* treat '@' as a shortcut for 'HEAD' */
1244 new_branch_info->name = !strcmp(arg, "@") ? xstrdup("HEAD") :
1245 xstrdup(arg);
1246 setup_branch_path(new_branch_info);
1248 if (!check_refname_format(new_branch_info->path, 0) &&
1249 !read_ref(new_branch_info->path, &branch_rev))
1250 oidcpy(rev, &branch_rev);
1251 else
1252 /* not an existing branch */
1253 FREE_AND_NULL(new_branch_info->path);
1255 new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1);
1256 if (!new_branch_info->commit) {
1257 /* not a commit */
1258 *source_tree = parse_tree_indirect(rev);
1259 if (!*source_tree)
1260 die(_("unable to read tree (%s)"), oid_to_hex(rev));
1261 } else {
1262 parse_commit_or_die(new_branch_info->commit);
1263 *source_tree = repo_get_commit_tree(the_repository,
1264 new_branch_info->commit);
1265 if (!*source_tree)
1266 die(_("unable to read tree (%s)"),
1267 oid_to_hex(&new_branch_info->commit->object.oid));
1271 static const char *parse_remote_branch(const char *arg,
1272 struct object_id *rev,
1273 int could_be_checkout_paths)
1275 int num_matches = 0;
1276 const char *remote = unique_tracking_name(arg, rev, &num_matches);
1278 if (remote && could_be_checkout_paths) {
1279 die(_("'%s' could be both a local file and a tracking branch.\n"
1280 "Please use -- (and optionally --no-guess) to disambiguate"),
1281 arg);
1284 if (!remote && num_matches > 1) {
1285 if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) {
1286 advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1287 "you can do so by fully qualifying the name with the --track option:\n"
1288 "\n"
1289 " git checkout --track origin/<name>\n"
1290 "\n"
1291 "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1292 "one remote, e.g. the 'origin' remote, consider setting\n"
1293 "checkout.defaultRemote=origin in your config."));
1296 die(_("'%s' matched multiple (%d) remote tracking branches"),
1297 arg, num_matches);
1300 return remote;
1303 static int parse_branchname_arg(int argc, const char **argv,
1304 int dwim_new_local_branch_ok,
1305 struct branch_info *new_branch_info,
1306 struct checkout_opts *opts,
1307 struct object_id *rev)
1309 const char **new_branch = &opts->new_branch;
1310 int argcount = 0;
1311 const char *arg;
1312 int dash_dash_pos;
1313 int has_dash_dash = 0;
1314 int i;
1317 * case 1: git checkout <ref> -- [<paths>]
1319 * <ref> must be a valid tree, everything after the '--' must be
1320 * a path.
1322 * case 2: git checkout -- [<paths>]
1324 * everything after the '--' must be paths.
1326 * case 3: git checkout <something> [--]
1328 * (a) If <something> is a commit, that is to
1329 * switch to the branch or detach HEAD at it. As a special case,
1330 * if <something> is A...B (missing A or B means HEAD but you can
1331 * omit at most one side), and if there is a unique merge base
1332 * between A and B, A...B names that merge base.
1334 * (b) If <something> is _not_ a commit, either "--" is present
1335 * or <something> is not a path, no -t or -b was given,
1336 * and there is a tracking branch whose name is <something>
1337 * in one and only one remote (or if the branch exists on the
1338 * remote named in checkout.defaultRemote), then this is a
1339 * short-hand to fork local <something> from that
1340 * remote-tracking branch.
1342 * (c) Otherwise, if "--" is present, treat it like case (1).
1344 * (d) Otherwise :
1345 * - if it's a reference, treat it like case (1)
1346 * - else if it's a path, treat it like case (2)
1347 * - else: fail.
1349 * case 4: git checkout <something> <paths>
1351 * The first argument must not be ambiguous.
1352 * - If it's *only* a reference, treat it like case (1).
1353 * - If it's only a path, treat it like case (2).
1354 * - else: fail.
1357 if (!argc)
1358 return 0;
1360 if (!opts->accept_pathspec) {
1361 if (argc > 1)
1362 die(_("only one reference expected"));
1363 has_dash_dash = 1; /* helps disambiguate */
1366 arg = argv[0];
1367 dash_dash_pos = -1;
1368 for (i = 0; i < argc; i++) {
1369 if (opts->accept_pathspec && !strcmp(argv[i], "--")) {
1370 dash_dash_pos = i;
1371 break;
1374 if (dash_dash_pos == 0)
1375 return 1; /* case (2) */
1376 else if (dash_dash_pos == 1)
1377 has_dash_dash = 1; /* case (3) or (1) */
1378 else if (dash_dash_pos >= 2)
1379 die(_("only one reference expected, %d given."), dash_dash_pos);
1380 opts->count_checkout_paths = !opts->quiet && !has_dash_dash;
1382 if (!strcmp(arg, "-"))
1383 arg = "@{-1}";
1385 if (repo_get_oid_mb(the_repository, arg, rev)) {
1387 * Either case (3) or (4), with <something> not being
1388 * a commit, or an attempt to use case (1) with an
1389 * invalid ref.
1391 * It's likely an error, but we need to find out if
1392 * we should auto-create the branch, case (3).(b).
1394 int recover_with_dwim = dwim_new_local_branch_ok;
1396 int could_be_checkout_paths = !has_dash_dash &&
1397 check_filename(opts->prefix, arg);
1399 if (!has_dash_dash && !no_wildcard(arg))
1400 recover_with_dwim = 0;
1403 * Accept "git checkout foo", "git checkout foo --"
1404 * and "git switch foo" as candidates for dwim.
1406 if (!(argc == 1 && !has_dash_dash) &&
1407 !(argc == 2 && has_dash_dash) &&
1408 opts->accept_pathspec)
1409 recover_with_dwim = 0;
1411 if (recover_with_dwim) {
1412 const char *remote = parse_remote_branch(arg, rev,
1413 could_be_checkout_paths);
1414 if (remote) {
1415 *new_branch = arg;
1416 arg = remote;
1417 /* DWIMmed to create local branch, case (3).(b) */
1418 } else {
1419 recover_with_dwim = 0;
1423 if (!recover_with_dwim) {
1424 if (has_dash_dash)
1425 die(_("invalid reference: %s"), arg);
1426 return argcount;
1430 /* we can't end up being in (2) anymore, eat the argument */
1431 argcount++;
1432 argv++;
1433 argc--;
1435 setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
1437 if (!opts->source_tree) /* case (1): want a tree */
1438 die(_("reference is not a tree: %s"), arg);
1440 if (!has_dash_dash) { /* case (3).(d) -> (1) */
1442 * Do not complain the most common case
1443 * git checkout branch
1444 * even if there happen to be a file called 'branch';
1445 * it would be extremely annoying.
1447 if (argc)
1448 verify_non_filename(opts->prefix, arg);
1449 } else if (opts->accept_pathspec) {
1450 argcount++;
1451 argv++;
1452 argc--;
1455 return argcount;
1458 static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
1460 int status;
1461 struct strbuf branch_ref = STRBUF_INIT;
1463 trace2_cmd_mode("unborn");
1465 if (!opts->new_branch)
1466 die(_("You are on a branch yet to be born"));
1467 strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
1468 status = create_symref("HEAD", branch_ref.buf, "checkout -b");
1469 strbuf_release(&branch_ref);
1470 if (!opts->quiet)
1471 fprintf(stderr, _("Switched to a new branch '%s'\n"),
1472 opts->new_branch);
1473 return status;
1476 static void die_expecting_a_branch(const struct branch_info *branch_info)
1478 struct object_id oid;
1479 char *to_free;
1480 int code;
1482 if (repo_dwim_ref(the_repository, branch_info->name,
1483 strlen(branch_info->name), &oid, &to_free, 0) == 1) {
1484 const char *ref = to_free;
1486 if (skip_prefix(ref, "refs/tags/", &ref))
1487 code = die_message(_("a branch is expected, got tag '%s'"), ref);
1488 else if (skip_prefix(ref, "refs/remotes/", &ref))
1489 code = die_message(_("a branch is expected, got remote branch '%s'"), ref);
1490 else
1491 code = die_message(_("a branch is expected, got '%s'"), ref);
1493 else if (branch_info->commit)
1494 code = die_message(_("a branch is expected, got commit '%s'"), branch_info->name);
1495 else
1497 * This case should never happen because we already die() on
1498 * non-commit, but just in case.
1500 code = die_message(_("a branch is expected, got '%s'"), branch_info->name);
1502 if (advice_enabled(ADVICE_SUGGEST_DETACHING_HEAD))
1503 advise(_("If you want to detach HEAD at the commit, try again with the --detach option."));
1505 exit(code);
1508 static void die_if_some_operation_in_progress(void)
1510 struct wt_status_state state;
1512 memset(&state, 0, sizeof(state));
1513 wt_status_get_state(the_repository, &state, 0);
1515 if (state.merge_in_progress)
1516 die(_("cannot switch branch while merging\n"
1517 "Consider \"git merge --quit\" "
1518 "or \"git worktree add\"."));
1519 if (state.am_in_progress)
1520 die(_("cannot switch branch in the middle of an am session\n"
1521 "Consider \"git am --quit\" "
1522 "or \"git worktree add\"."));
1523 if (state.rebase_interactive_in_progress || state.rebase_in_progress)
1524 die(_("cannot switch branch while rebasing\n"
1525 "Consider \"git rebase --quit\" "
1526 "or \"git worktree add\"."));
1527 if (state.cherry_pick_in_progress)
1528 die(_("cannot switch branch while cherry-picking\n"
1529 "Consider \"git cherry-pick --quit\" "
1530 "or \"git worktree add\"."));
1531 if (state.revert_in_progress)
1532 die(_("cannot switch branch while reverting\n"
1533 "Consider \"git revert --quit\" "
1534 "or \"git worktree add\"."));
1535 if (state.bisect_in_progress)
1536 warning(_("you are switching branch while bisecting"));
1538 wt_status_state_free_buffers(&state);
1542 * die if attempting to checkout an existing branch that is in use
1543 * in another worktree, unless ignore-other-wortrees option is given.
1544 * The check is bypassed when the branch is already the current one,
1545 * as it will not make things any worse.
1547 static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts,
1548 const char *full_ref)
1550 int flags;
1551 char *head_ref;
1553 if (opts->ignore_other_worktrees)
1554 return;
1555 head_ref = resolve_refdup("HEAD", 0, NULL, &flags);
1556 if (head_ref && (!(flags & REF_ISSYMREF) || strcmp(head_ref, full_ref)))
1557 die_if_checked_out(full_ref, 1);
1558 free(head_ref);
1561 static int checkout_branch(struct checkout_opts *opts,
1562 struct branch_info *new_branch_info)
1564 if (opts->pathspec.nr)
1565 die(_("paths cannot be used with switching branches"));
1567 if (opts->patch_mode)
1568 die(_("'%s' cannot be used with switching branches"),
1569 "--patch");
1571 if (opts->overlay_mode != -1)
1572 die(_("'%s' cannot be used with switching branches"),
1573 "--[no]-overlay");
1575 if (opts->writeout_stage)
1576 die(_("'%s' cannot be used with switching branches"),
1577 "--ours/--theirs");
1579 if (opts->force && opts->merge)
1580 die(_("'%s' cannot be used with '%s'"), "-f", "-m");
1582 if (opts->discard_changes && opts->merge)
1583 die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge");
1585 if (opts->force_detach && opts->new_branch)
1586 die(_("'%s' cannot be used with '%s'"),
1587 "--detach", "-b/-B/--orphan");
1589 if (opts->new_orphan_branch) {
1590 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
1591 die(_("'%s' cannot be used with '%s'"), "--orphan", "-t");
1592 if (opts->orphan_from_empty_tree && new_branch_info->name)
1593 die(_("'%s' cannot take <start-point>"), "--orphan");
1594 } else if (opts->force_detach) {
1595 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
1596 die(_("'%s' cannot be used with '%s'"), "--detach", "-t");
1597 } else if (opts->track == BRANCH_TRACK_UNSPECIFIED)
1598 opts->track = git_branch_track;
1600 if (new_branch_info->name && !new_branch_info->commit)
1601 die(_("Cannot switch branch to a non-commit '%s'"),
1602 new_branch_info->name);
1604 if (!opts->switch_branch_doing_nothing_is_ok &&
1605 !new_branch_info->name &&
1606 !opts->new_branch &&
1607 !opts->force_detach)
1608 die(_("missing branch or commit argument"));
1610 if (!opts->implicit_detach &&
1611 !opts->force_detach &&
1612 !opts->new_branch &&
1613 !opts->new_branch_force &&
1614 new_branch_info->name &&
1615 !new_branch_info->path)
1616 die_expecting_a_branch(new_branch_info);
1618 if (!opts->can_switch_when_in_progress)
1619 die_if_some_operation_in_progress();
1621 /* "git checkout <branch>" */
1622 if (new_branch_info->path && !opts->force_detach && !opts->new_branch)
1623 die_if_switching_to_a_branch_in_use(opts, new_branch_info->path);
1625 /* "git checkout -B <branch>" */
1626 if (opts->new_branch_force) {
1627 char *full_ref = xstrfmt("refs/heads/%s", opts->new_branch);
1628 die_if_switching_to_a_branch_in_use(opts, full_ref);
1629 free(full_ref);
1632 if (!new_branch_info->commit && opts->new_branch) {
1633 struct object_id rev;
1634 int flag;
1636 if (!read_ref_full("HEAD", 0, &rev, &flag) &&
1637 (flag & REF_ISSYMREF) && is_null_oid(&rev))
1638 return switch_unborn_to_new_branch(opts);
1640 return switch_branches(opts, new_branch_info);
1643 static int parse_opt_conflict(const struct option *o, const char *arg, int unset)
1645 struct checkout_opts *opts = o->value;
1647 if (unset) {
1648 opts->conflict_style = -1;
1649 return 0;
1651 opts->conflict_style = parse_conflict_style_name(arg);
1652 if (opts->conflict_style < 0)
1653 return error(_("unknown conflict style '%s'"), arg);
1654 /* --conflict overrides a previous --no-merge */
1655 if (!opts->merge)
1656 opts->merge = -1;
1658 return 0;
1661 static struct option *add_common_options(struct checkout_opts *opts,
1662 struct option *prevopts)
1664 struct option options[] = {
1665 OPT__QUIET(&opts->quiet, N_("suppress progress reporting")),
1666 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
1667 "checkout", "control recursive updating of submodules",
1668 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
1669 OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")),
1670 OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")),
1671 OPT_CALLBACK(0, "conflict", opts, N_("style"),
1672 N_("conflict style (merge, diff3, or zdiff3)"),
1673 parse_opt_conflict),
1674 OPT_END()
1676 struct option *newopts = parse_options_concat(prevopts, options);
1677 free(prevopts);
1678 return newopts;
1681 static struct option *add_common_switch_branch_options(
1682 struct checkout_opts *opts, struct option *prevopts)
1684 struct option options[] = {
1685 OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")),
1686 OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)",
1687 N_("set branch tracking configuration"),
1688 PARSE_OPT_OPTARG,
1689 parse_opt_tracking_mode),
1690 OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
1691 PARSE_OPT_NOCOMPLETE),
1692 OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")),
1693 OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore,
1694 N_("update ignored files (default)"),
1695 PARSE_OPT_NOCOMPLETE),
1696 OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees,
1697 N_("do not check if another worktree is holding the given ref")),
1698 OPT_END()
1700 struct option *newopts = parse_options_concat(prevopts, options);
1701 free(prevopts);
1702 return newopts;
1705 static struct option *add_checkout_path_options(struct checkout_opts *opts,
1706 struct option *prevopts)
1708 struct option options[] = {
1709 OPT_SET_INT_F('2', "ours", &opts->writeout_stage,
1710 N_("checkout our version for unmerged files"),
1711 2, PARSE_OPT_NONEG),
1712 OPT_SET_INT_F('3', "theirs", &opts->writeout_stage,
1713 N_("checkout their version for unmerged files"),
1714 3, PARSE_OPT_NONEG),
1715 OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")),
1716 OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree,
1717 N_("do not limit pathspecs to sparse entries only")),
1718 OPT_PATHSPEC_FROM_FILE(&opts->pathspec_from_file),
1719 OPT_PATHSPEC_FILE_NUL(&opts->pathspec_file_nul),
1720 OPT_END()
1722 struct option *newopts = parse_options_concat(prevopts, options);
1723 free(prevopts);
1724 return newopts;
1727 /* create-branch option (either b or c) */
1728 static char cb_option = 'b';
1730 static int checkout_main(int argc, const char **argv, const char *prefix,
1731 struct checkout_opts *opts, struct option *options,
1732 const char * const usagestr[])
1734 int parseopt_flags = 0;
1735 struct branch_info new_branch_info = { 0 };
1736 int ret;
1738 opts->overwrite_ignore = 1;
1739 opts->prefix = prefix;
1740 opts->show_progress = -1;
1742 git_config(git_checkout_config, opts);
1743 if (the_repository->gitdir) {
1744 prepare_repo_settings(the_repository);
1745 the_repository->settings.command_requires_full_index = 0;
1748 opts->track = BRANCH_TRACK_UNSPECIFIED;
1750 if (!opts->accept_pathspec && !opts->accept_ref)
1751 BUG("make up your mind, you need to take _something_");
1752 if (opts->accept_pathspec && opts->accept_ref)
1753 parseopt_flags = PARSE_OPT_KEEP_DASHDASH;
1755 argc = parse_options(argc, argv, prefix, options,
1756 usagestr, parseopt_flags);
1758 if (opts->show_progress < 0) {
1759 if (opts->quiet)
1760 opts->show_progress = 0;
1761 else
1762 opts->show_progress = isatty(2);
1765 /* --conflicts implies --merge */
1766 if (opts->merge == -1)
1767 opts->merge = opts->conflict_style >= 0;
1769 if (opts->force) {
1770 opts->discard_changes = 1;
1771 opts->ignore_unmerged_opt = "--force";
1772 opts->ignore_unmerged = 1;
1775 if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
1776 die(_("options '-%c', '-%c', and '%s' cannot be used together"),
1777 cb_option, toupper(cb_option), "--orphan");
1779 if (opts->overlay_mode == 1 && opts->patch_mode)
1780 die(_("options '%s' and '%s' cannot be used together"), "-p", "--overlay");
1782 if (opts->checkout_index >= 0 || opts->checkout_worktree >= 0) {
1783 if (opts->checkout_index < 0)
1784 opts->checkout_index = 0;
1785 if (opts->checkout_worktree < 0)
1786 opts->checkout_worktree = 0;
1787 } else {
1788 if (opts->checkout_index < 0)
1789 opts->checkout_index = -opts->checkout_index - 1;
1790 if (opts->checkout_worktree < 0)
1791 opts->checkout_worktree = -opts->checkout_worktree - 1;
1793 if (opts->checkout_index < 0 || opts->checkout_worktree < 0)
1794 BUG("these flags should be non-negative by now");
1796 * convenient shortcut: "git restore --staged [--worktree]" equals
1797 * "git restore --staged [--worktree] --source HEAD"
1799 if (!opts->from_treeish && opts->checkout_index)
1800 opts->from_treeish = "HEAD";
1803 * From here on, new_branch will contain the branch to be checked out,
1804 * and new_branch_force and new_orphan_branch will tell us which one of
1805 * -b/-B/-c/-C/--orphan is being used.
1807 if (opts->new_branch_force)
1808 opts->new_branch = opts->new_branch_force;
1810 if (opts->new_orphan_branch)
1811 opts->new_branch = opts->new_orphan_branch;
1813 /* --track without -c/-C/-b/-B/--orphan should DWIM */
1814 if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {
1815 const char *argv0 = argv[0];
1816 if (!argc || !strcmp(argv0, "--"))
1817 die(_("--track needs a branch name"));
1818 skip_prefix(argv0, "refs/", &argv0);
1819 skip_prefix(argv0, "remotes/", &argv0);
1820 argv0 = strchr(argv0, '/');
1821 if (!argv0 || !argv0[1])
1822 die(_("missing branch name; try -%c"), cb_option);
1823 opts->new_branch = argv0 + 1;
1827 * Extract branch name from command line arguments, so
1828 * all that is left is pathspecs.
1830 * Handle
1832 * 1) git checkout <tree> -- [<paths>]
1833 * 2) git checkout -- [<paths>]
1834 * 3) git checkout <something> [<paths>]
1836 * including "last branch" syntax and DWIM-ery for names of
1837 * remote branches, erroring out for invalid or ambiguous cases.
1839 if (argc && opts->accept_ref) {
1840 struct object_id rev;
1841 int dwim_ok =
1842 !opts->patch_mode &&
1843 opts->dwim_new_local_branch &&
1844 opts->track == BRANCH_TRACK_UNSPECIFIED &&
1845 !opts->new_branch;
1846 int n = parse_branchname_arg(argc, argv, dwim_ok,
1847 &new_branch_info, opts, &rev);
1848 argv += n;
1849 argc -= n;
1850 } else if (!opts->accept_ref && opts->from_treeish) {
1851 struct object_id rev;
1853 if (repo_get_oid_mb(the_repository, opts->from_treeish, &rev))
1854 die(_("could not resolve %s"), opts->from_treeish);
1856 setup_new_branch_info_and_source_tree(&new_branch_info,
1857 opts, &rev,
1858 opts->from_treeish);
1860 if (!opts->source_tree)
1861 die(_("reference is not a tree: %s"), opts->from_treeish);
1864 if (argc) {
1865 parse_pathspec(&opts->pathspec, 0,
1866 opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
1867 prefix, argv);
1869 if (!opts->pathspec.nr)
1870 die(_("invalid path specification"));
1873 * Try to give more helpful suggestion.
1874 * new_branch && argc > 1 will be caught later.
1876 if (opts->new_branch && argc == 1 && !new_branch_info.commit)
1877 die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),
1878 argv[0], opts->new_branch);
1880 if (opts->force_detach)
1881 die(_("git checkout: --detach does not take a path argument '%s'"),
1882 argv[0]);
1885 if (opts->pathspec_from_file) {
1886 if (opts->pathspec.nr)
1887 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
1889 if (opts->force_detach)
1890 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--detach");
1892 if (opts->patch_mode)
1893 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
1895 parse_pathspec_file(&opts->pathspec, 0,
1897 prefix, opts->pathspec_from_file, opts->pathspec_file_nul);
1898 } else if (opts->pathspec_file_nul) {
1899 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
1902 opts->pathspec.recursive = 1;
1904 if (opts->pathspec.nr) {
1905 if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
1906 die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
1907 "checking out of the index."));
1908 } else {
1909 if (opts->accept_pathspec && !opts->empty_pathspec_ok &&
1910 !opts->patch_mode) /* patch mode is special */
1911 die(_("you must specify path(s) to restore"));
1914 if (opts->new_branch) {
1915 struct strbuf buf = STRBUF_INIT;
1917 if (opts->new_branch_force)
1918 opts->branch_exists = validate_branchname(opts->new_branch, &buf);
1919 else
1920 opts->branch_exists =
1921 validate_new_branchname(opts->new_branch, &buf, 0);
1922 strbuf_release(&buf);
1925 if (opts->patch_mode || opts->pathspec.nr)
1926 ret = checkout_paths(opts, &new_branch_info);
1927 else
1928 ret = checkout_branch(opts, &new_branch_info);
1930 branch_info_release(&new_branch_info);
1931 clear_pathspec(&opts->pathspec);
1932 free(opts->pathspec_from_file);
1933 free(options);
1935 return ret;
1938 int cmd_checkout(int argc, const char **argv, const char *prefix)
1940 struct checkout_opts opts = CHECKOUT_OPTS_INIT;
1941 struct option *options;
1942 struct option checkout_options[] = {
1943 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
1944 N_("create and checkout a new branch")),
1945 OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
1946 N_("create/reset and checkout a branch")),
1947 OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
1948 OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
1949 N_("second guess 'git checkout <no-such-branch>' (default)")),
1950 OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
1951 OPT_END()
1954 opts.dwim_new_local_branch = 1;
1955 opts.switch_branch_doing_nothing_is_ok = 1;
1956 opts.only_merge_on_switching_branches = 0;
1957 opts.accept_ref = 1;
1958 opts.accept_pathspec = 1;
1959 opts.implicit_detach = 1;
1960 opts.can_switch_when_in_progress = 1;
1961 opts.orphan_from_empty_tree = 0;
1962 opts.empty_pathspec_ok = 1;
1963 opts.overlay_mode = -1;
1964 opts.checkout_index = -2; /* default on */
1965 opts.checkout_worktree = -2; /* default on */
1967 if (argc == 3 && !strcmp(argv[1], "-b")) {
1969 * User ran 'git checkout -b <branch>' and expects
1970 * the same behavior as 'git switch -c <branch>'.
1972 opts.switch_branch_doing_nothing_is_ok = 0;
1973 opts.only_merge_on_switching_branches = 1;
1976 options = parse_options_dup(checkout_options);
1977 options = add_common_options(&opts, options);
1978 options = add_common_switch_branch_options(&opts, options);
1979 options = add_checkout_path_options(&opts, options);
1981 return checkout_main(argc, argv, prefix, &opts, options,
1982 checkout_usage);
1985 int cmd_switch(int argc, const char **argv, const char *prefix)
1987 struct checkout_opts opts = CHECKOUT_OPTS_INIT;
1988 struct option *options = NULL;
1989 struct option switch_options[] = {
1990 OPT_STRING('c', "create", &opts.new_branch, N_("branch"),
1991 N_("create and switch to a new branch")),
1992 OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
1993 N_("create/reset and switch to a branch")),
1994 OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
1995 N_("second guess 'git switch <no-such-branch>'")),
1996 OPT_BOOL(0, "discard-changes", &opts.discard_changes,
1997 N_("throw away local modifications")),
1998 OPT_END()
2001 opts.dwim_new_local_branch = 1;
2002 opts.accept_ref = 1;
2003 opts.accept_pathspec = 0;
2004 opts.switch_branch_doing_nothing_is_ok = 0;
2005 opts.only_merge_on_switching_branches = 1;
2006 opts.implicit_detach = 0;
2007 opts.can_switch_when_in_progress = 0;
2008 opts.orphan_from_empty_tree = 1;
2009 opts.overlay_mode = -1;
2011 options = parse_options_dup(switch_options);
2012 options = add_common_options(&opts, options);
2013 options = add_common_switch_branch_options(&opts, options);
2015 cb_option = 'c';
2017 return checkout_main(argc, argv, prefix, &opts, options,
2018 switch_branch_usage);
2021 int cmd_restore(int argc, const char **argv, const char *prefix)
2023 struct checkout_opts opts = CHECKOUT_OPTS_INIT;
2024 struct option *options;
2025 struct option restore_options[] = {
2026 OPT_STRING('s', "source", &opts.from_treeish, "<tree-ish>",
2027 N_("which tree-ish to checkout from")),
2028 OPT_BOOL('S', "staged", &opts.checkout_index,
2029 N_("restore the index")),
2030 OPT_BOOL('W', "worktree", &opts.checkout_worktree,
2031 N_("restore the working tree (default)")),
2032 OPT_BOOL(0, "ignore-unmerged", &opts.ignore_unmerged,
2033 N_("ignore unmerged entries")),
2034 OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode")),
2035 OPT_END()
2038 opts.accept_ref = 0;
2039 opts.accept_pathspec = 1;
2040 opts.empty_pathspec_ok = 0;
2041 opts.overlay_mode = 0;
2042 opts.checkout_index = -1; /* default off */
2043 opts.checkout_worktree = -2; /* default on */
2044 opts.ignore_unmerged_opt = "--ignore-unmerged";
2046 options = parse_options_dup(restore_options);
2047 options = add_common_options(&opts, options);
2048 options = add_checkout_path_options(&opts, options);
2050 return checkout_main(argc, argv, prefix, &opts, options,
2051 restore_usage);