blob: a6e30931b5c809da06c751ba6ef43e47a5a3e0ea [file] [log] [blame]
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001#define USE_THE_INDEX_VARIABLE
Daniel Barkalow782c2d62008-02-07 11:40:23 -05002#include "builtin.h"
Nguyễn Thái Ngọc Duy0dabeff2019-03-29 17:39:00 +07003#include "advice.h"
Nguyễn Thái Ngọc Duy0dabeff2019-03-29 17:39:00 +07004#include "branch.h"
5#include "cache-tree.h"
Thomas Gummerer7c85a872017-11-26 19:43:51 +00006#include "checkout.h"
Nguyễn Thái Ngọc Duy0dabeff2019-03-29 17:39:00 +07007#include "commit.h"
8#include "config.h"
9#include "diff.h"
10#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000011#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000012#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000013#include "hex.h"
Emily Shaffer72ddf342021-12-22 04:59:35 +010014#include "hook.h"
Elijah Newren67238992023-05-16 06:34:04 +000015#include "merge-ll.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020016#include "lockfile.h"
Elijah Newren5bc07222023-04-11 00:41:47 -070017#include "mem-pool.h"
Nguyễn Thái Ngọc Duy0dabeff2019-03-29 17:39:00 +070018#include "merge-recursive.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070019#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000020#include "object-store-ll.h"
Daniel Barkalow782c2d62008-02-07 11:40:23 -050021#include "parse-options.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000022#include "path.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000023#include "preload-index.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000024#include "read-cache.h"
Daniel Barkalow782c2d62008-02-07 11:40:23 -050025#include "refs.h"
Nguyễn Thái Ngọc Duy0dabeff2019-03-29 17:39:00 +070026#include "remote.h"
27#include "resolve-undo.h"
28#include "revision.h"
Elijah Newrene38da482023-03-21 06:26:05 +000029#include "setup.h"
Nguyễn Thái Ngọc Duy0dabeff2019-03-29 17:39:00 +070030#include "submodule.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000031#include "symlinks.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000032#include "trace2.h"
Daniel Barkalow782c2d62008-02-07 11:40:23 -050033#include "tree.h"
34#include "tree-walk.h"
35#include "unpack-trees.h"
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +070036#include "wt-status.h"
Junio C Hamano0cf85812008-08-30 07:52:24 -070037#include "xdiff-interface.h"
Matheus Tavaresd052cc02021-03-23 11:19:32 -030038#include "entry.h"
Matheus Tavares60539502021-05-04 13:27:29 -030039#include "parallel-checkout.h"
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +010040#include "add-interactive.h"
Ben Peartfa655d82018-08-16 18:27:11 +000041
Daniel Barkalow782c2d62008-02-07 11:40:23 -050042static const char * const checkout_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070043 N_("git checkout [<options>] <branch>"),
44 N_("git checkout [<options>] [<branch>] -- <file>..."),
Daniel Barkalow782c2d62008-02-07 11:40:23 -050045 NULL,
46};
47
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +070048static const char * const switch_branch_usage[] = {
49 N_("git switch [<options>] [<branch>]"),
50 NULL,
51};
52
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +070053static const char * const restore_usage[] = {
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +070054 N_("git restore [<options>] [--source=<branch>] <file>..."),
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +070055 NULL,
56};
57
Junio C Hamanodb941092008-08-30 07:46:55 -070058struct checkout_opts {
Nguyễn Thái Ngọc Duye51e3052012-08-29 20:55:23 +070059 int patch_mode;
Junio C Hamanodb941092008-08-30 07:46:55 -070060 int quiet;
61 int merge;
62 int force;
Junio C Hamano32669672011-02-08 04:32:49 -060063 int force_detach;
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +070064 int implicit_detach;
Junio C Hamano38901a42008-08-30 07:48:18 -070065 int writeout_stage;
Nguyễn Thái Ngọc Duyc1d70362011-11-27 17:15:33 +070066 int overwrite_ignore;
Nguyễn Thái Ngọc Duy08d595d2013-04-13 09:12:08 +100067 int ignore_skipworktree;
Nguyễn Thái Ngọc Duy1d0fa892015-01-03 16:41:26 +070068 int ignore_other_worktrees;
Edmundo Carmona Antoranz870ebdb2015-11-01 15:19:05 -060069 int show_progress;
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +010070 int count_checkout_paths;
Thomas Gummerer091e04b2019-01-08 21:52:24 +000071 int overlay_mode;
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +070072 int dwim_new_local_branch;
Nguyễn Thái Ngọc Duy3ec37ad2019-03-29 17:39:07 +070073 int discard_changes;
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +070074 int accept_ref;
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +070075 int accept_pathspec;
Nguyễn Thái Ngọc Duye342c632019-03-29 17:39:10 +070076 int switch_branch_doing_nothing_is_ok;
Nguyễn Thái Ngọc Duy65f099b2019-03-29 17:39:14 +070077 int only_merge_on_switching_branches;
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +070078 int can_switch_when_in_progress;
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +070079 int orphan_from_empty_tree;
Nguyễn Thái Ngọc Duybe8ed502019-04-25 16:45:47 +070080 int empty_pathspec_ok;
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +070081 int checkout_index;
82 int checkout_worktree;
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +070083 const char *ignore_unmerged_opt;
84 int ignore_unmerged;
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +000085 int pathspec_file_nul;
Jeff King7ce40882023-03-04 05:31:22 -050086 char *pathspec_from_file;
Junio C Hamanodb941092008-08-30 07:46:55 -070087
88 const char *new_branch;
Tay Ray Chuan02ac9832010-06-24 03:29:00 +080089 const char *new_branch_force;
Erick Mattos9db5ebf2010-03-21 12:34:38 -030090 const char *new_orphan_branch;
Junio C Hamanodb941092008-08-30 07:46:55 -070091 int new_branch_log;
92 enum branch_track track;
Johannes Schindelin175f6e52010-08-25 12:34:57 +020093 struct diff_options diff_options;
Nguyễn Thái Ngọc Duy55cf7042019-03-29 17:39:03 +070094 char *conflict_style;
Nguyễn Thái Ngọc Duye51e3052012-08-29 20:55:23 +070095
96 int branch_exists;
97 const char *prefix;
Nguyễn Thái Ngọc Duy817b3452013-07-14 15:35:41 +070098 struct pathspec pathspec;
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +070099 const char *from_treeish;
Nguyễn Thái Ngọc Duye51e3052012-08-29 20:55:23 +0700100 struct tree *source_tree;
Junio C Hamanodb941092008-08-30 07:46:55 -0700101};
102
brian m. carlsona8604762020-03-10 18:20:39 +0000103struct branch_info {
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +0100104 char *name; /* The short name used */
105 char *path; /* The full name of a real branch */
brian m. carlsona8604762020-03-10 18:20:39 +0000106 struct commit *commit; /* The named commit */
brian m. carlsonc397aac2020-03-16 18:05:03 +0000107 char *refname; /* The full name of the ref being checked out. */
108 struct object_id oid; /* The object ID of the commit being checked out. */
brian m. carlsona8604762020-03-10 18:20:39 +0000109 /*
110 * if not null the branch is detached because it's already
111 * checked out in this checkout
112 */
113 char *checkout;
114};
115
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +0100116static void branch_info_release(struct branch_info *info)
117{
118 free(info->name);
119 free(info->path);
120 free(info->refname);
121 free(info->checkout);
122}
123
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800124static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500125 int changed)
126{
Emily Shaffer72ddf342021-12-22 04:59:35 +0100127 return run_hooks_l("post-checkout",
brian m. carlson14228442021-04-26 01:02:56 +0000128 oid_to_hex(old_commit ? &old_commit->object.oid : null_oid()),
129 oid_to_hex(new_commit ? &new_commit->object.oid : null_oid()),
Benoit Pierre15048f82014-03-18 11:00:53 +0100130 changed ? "1" : "0", NULL);
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800131 /* "new_commit" can be NULL when checking out from the index before
Stephan Beyer2292ce42009-01-16 20:09:58 +0100132 a commit exists. */
Stephan Beyerae98a002009-01-16 20:09:59 +0100133
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500134}
135
brian m. carlsondf46d772018-03-12 02:27:26 +0000136static int update_some(const struct object_id *oid, struct strbuf *base,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200137 const char *pathname, unsigned mode, void *context UNUSED)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500138{
139 int len;
140 struct cache_entry *ce;
Jeff Kingc5326bd2014-11-13 13:30:34 -0500141 int pos;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500142
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500143 if (S_ISDIR(mode))
144 return READ_TREE_RECURSIVE;
145
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700146 len = base->len + strlen(pathname);
Jameson Millera8497352018-07-02 19:49:31 +0000147 ce = make_empty_cache_entry(&the_index, len);
brian m. carlsondf46d772018-03-12 02:27:26 +0000148 oidcpy(&ce->oid, oid);
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700149 memcpy(ce->name, base->buf, base->len);
150 memcpy(ce->name + base->len, pathname, len - base->len);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200151 ce->ce_flags = create_ce_flags(0) | CE_UPDATE;
152 ce->ce_namelen = len;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500153 ce->ce_mode = create_ce_mode(mode);
Jeff Kingc5326bd2014-11-13 13:30:34 -0500154
155 /*
156 * If the entry is the same as the current index, we can leave the old
157 * entry in place. Whether it is UPTODATE or not, checkout_entry will
158 * do the right thing.
159 */
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100160 pos = index_name_pos(&the_index, ce->name, ce->ce_namelen);
Jeff Kingc5326bd2014-11-13 13:30:34 -0500161 if (pos >= 0) {
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100162 struct cache_entry *old = the_index.cache[pos];
Jeff Kingc5326bd2014-11-13 13:30:34 -0500163 if (ce->ce_mode == old->ce_mode &&
Varun Naikecd72042019-08-02 09:28:52 -0700164 !ce_intent_to_add(old) &&
Jeff King4a7e27e2018-08-28 17:22:40 -0400165 oideq(&ce->oid, &old->oid)) {
Jeff Kingc5326bd2014-11-13 13:30:34 -0500166 old->ce_flags |= CE_UPDATE;
Jameson Millera8497352018-07-02 19:49:31 +0000167 discard_cache_entry(ce);
Jeff Kingc5326bd2014-11-13 13:30:34 -0500168 return 0;
169 }
170 }
171
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100172 add_index_entry(&the_index, ce,
173 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500174 return 0;
175}
176
Nguyễn Thái Ngọc Duy18e4f402013-07-14 15:35:52 +0700177static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500178{
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100179 read_tree(the_repository, tree,
180 pathspec, update_some, NULL);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500181
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500182 /* update the index with the given tree's info
183 * for all args, expanding wildcards, and exit
184 * with any non-zero return code.
185 */
186 return 0;
187}
188
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700189static int skip_same_name(const struct cache_entry *ce, int pos)
Junio C Hamano8fdcf312008-08-29 13:40:36 -0700190{
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100191 while (++pos < the_index.cache_nr &&
192 !strcmp(the_index.cache[pos]->name, ce->name))
Junio C Hamano8fdcf312008-08-29 13:40:36 -0700193 ; /* skip */
194 return pos;
195}
196
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000197static int check_stage(int stage, const struct cache_entry *ce, int pos,
198 int overlay_mode)
Junio C Hamano38901a42008-08-30 07:48:18 -0700199{
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100200 while (pos < the_index.cache_nr &&
201 !strcmp(the_index.cache[pos]->name, ce->name)) {
202 if (ce_stage(the_index.cache[pos]) == stage)
Junio C Hamano38901a42008-08-30 07:48:18 -0700203 return 0;
204 pos++;
205 }
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000206 if (!overlay_mode)
207 return 0;
Ævar Arnfjörð Bjarmason9f97ab02011-02-22 23:41:40 +0000208 if (stage == 2)
209 return error(_("path '%s' does not have our version"), ce->name);
210 else
211 return error(_("path '%s' does not have their version"), ce->name);
Junio C Hamano38901a42008-08-30 07:48:18 -0700212}
Junio C Hamano8fdcf312008-08-29 13:40:36 -0700213
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700214static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
Junio C Hamano0cf85812008-08-30 07:52:24 -0700215{
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800216 unsigned seen = 0;
217 const char *name = ce->name;
218
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100219 while (pos < the_index.cache_nr) {
220 ce = the_index.cache[pos];
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800221 if (strcmp(name, ce->name))
222 break;
223 seen |= (1 << ce_stage(ce));
224 pos++;
225 }
226 if ((stages & seen) != stages)
227 return error(_("path '%s' does not have all necessary versions"),
228 name);
Junio C Hamano0cf85812008-08-30 07:52:24 -0700229 return 0;
230}
231
René Scharfece25e4c2016-09-13 19:11:52 +0200232static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
Junio C Hamano7d0c1f42019-03-07 09:59:51 +0900233 const struct checkout *state, int *nr_checkouts,
234 int overlay_mode)
Junio C Hamano38901a42008-08-30 07:48:18 -0700235{
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100236 while (pos < the_index.cache_nr &&
237 !strcmp(the_index.cache[pos]->name, ce->name)) {
238 if (ce_stage(the_index.cache[pos]) == stage)
239 return checkout_entry(the_index.cache[pos], state,
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100240 NULL, nr_checkouts);
Junio C Hamano38901a42008-08-30 07:48:18 -0700241 pos++;
242 }
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000243 if (!overlay_mode) {
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100244 unlink_entry(ce, NULL);
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000245 return 0;
246 }
Ævar Arnfjörð Bjarmason9f97ab02011-02-22 23:41:40 +0000247 if (stage == 2)
248 return error(_("path '%s' does not have our version"), ce->name);
249 else
250 return error(_("path '%s' does not have their version"), ce->name);
Junio C Hamano38901a42008-08-30 07:48:18 -0700251}
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500252
Matheus Tavares60539502021-05-04 13:27:29 -0300253static int checkout_merged(int pos, const struct checkout *state,
254 int *nr_checkouts, struct mem_pool *ce_mem_pool)
Junio C Hamano0cf85812008-08-30 07:52:24 -0700255{
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100256 struct cache_entry *ce = the_index.cache[pos];
Junio C Hamano0cf85812008-08-30 07:52:24 -0700257 const char *path = ce->name;
258 mmfile_t ancestor, ours, theirs;
Elijah Newren35f69672022-02-02 02:37:30 +0000259 enum ll_merge_result merge_status;
Junio C Hamano0cf85812008-08-30 07:52:24 -0700260 int status;
brian m. carlson60af7692016-09-05 20:08:00 +0000261 struct object_id oid;
Junio C Hamano0cf85812008-08-30 07:52:24 -0700262 mmbuffer_t result_buf;
brian m. carlson60af7692016-09-05 20:08:00 +0000263 struct object_id threeway[3];
Junio C Hamano335c6e42011-12-15 10:10:11 -0800264 unsigned mode = 0;
Elijah Newren00906d62020-08-03 18:41:20 +0000265 struct ll_merge_options ll_opts;
266 int renormalize = 0;
Junio C Hamano0cf85812008-08-30 07:52:24 -0700267
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800268 memset(threeway, 0, sizeof(threeway));
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100269 while (pos < the_index.cache_nr) {
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800270 int stage;
271 stage = ce_stage(ce);
272 if (!stage || strcmp(path, ce->name))
273 break;
brian m. carlson60af7692016-09-05 20:08:00 +0000274 oidcpy(&threeway[stage - 1], &ce->oid);
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800275 if (stage == 2)
276 mode = create_ce_mode(ce->ce_mode);
277 pos++;
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100278 ce = the_index.cache[pos];
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800279 }
brian m. carlson60af7692016-09-05 20:08:00 +0000280 if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2]))
Junio C Hamanofbbccd02011-12-05 10:58:23 -0800281 return error(_("path '%s' does not have necessary versions"), path);
Junio C Hamano0cf85812008-08-30 07:52:24 -0700282
brian m. carlsond4493472016-09-05 20:08:02 +0000283 read_mmblob(&ancestor, &threeway[0]);
284 read_mmblob(&ours, &threeway[1]);
285 read_mmblob(&theirs, &threeway[2]);
Junio C Hamano0cf85812008-08-30 07:52:24 -0700286
Elijah Newren00906d62020-08-03 18:41:20 +0000287 memset(&ll_opts, 0, sizeof(ll_opts));
288 git_config_get_bool("merge.renormalize", &renormalize);
289 ll_opts.renormalize = renormalize;
Elijah Newren35f69672022-02-02 02:37:30 +0000290 merge_status = ll_merge(&result_buf, path, &ancestor, "base",
291 &ours, "ours", &theirs, "theirs",
292 state->istate, &ll_opts);
Junio C Hamano0cf85812008-08-30 07:52:24 -0700293 free(ancestor.ptr);
294 free(ours.ptr);
295 free(theirs.ptr);
Elijah Newren35f69672022-02-02 02:37:30 +0000296 if (merge_status == LL_MERGE_BINARY_CONFLICT)
297 warning("Cannot merge binary files: %s (%s vs. %s)",
298 path, "ours", "theirs");
299 if (merge_status < 0 || !result_buf.ptr) {
Junio C Hamano0cf85812008-08-30 07:52:24 -0700300 free(result_buf.ptr);
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +0000301 return error(_("path '%s': cannot merge"), path);
Junio C Hamano0cf85812008-08-30 07:52:24 -0700302 }
303
304 /*
305 * NEEDSWORK:
306 * There is absolutely no reason to write this as a blob object
Johannes Schindelin514e8032017-05-04 15:56:40 +0200307 * and create a phony cache entry. This hack is primarily to get
308 * to the write_entry() machinery that massages the contents to
309 * work-tree format and writes out which only allows it for a
310 * cache entry. The code in write_entry() needs to be refactored
311 * to allow us to feed a <buffer, size, mode> instead of a cache
312 * entry. Such a refactoring would help merge_recursive as well
313 * (it also writes the merge result to the object database even
314 * when it may contain conflicts).
Junio C Hamano0cf85812008-08-30 07:52:24 -0700315 */
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100316 if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid))
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +0000317 die(_("Unable to add merge result for '%s'"), path);
Junio C Hamano443a12f2017-05-08 13:21:06 +0900318 free(result_buf.ptr);
Matheus Tavares60539502021-05-04 13:27:29 -0300319 ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool);
Dmitry Potapov048f2762008-10-05 06:14:40 +0400320 if (!ce)
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +0000321 die(_("make_cache_entry failed for path '%s'"), path);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100322 status = checkout_entry(ce, state, NULL, nr_checkouts);
Junio C Hamano0cf85812008-08-30 07:52:24 -0700323 return status;
324}
325
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000326static void mark_ce_for_checkout_overlay(struct cache_entry *ce,
327 char *ps_matched,
328 const struct checkout_opts *opts)
Thomas Gummererb7033e72018-12-20 13:48:18 +0000329{
330 ce->ce_flags &= ~CE_MATCHED;
331 if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
332 return;
333 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
334 /*
335 * "git checkout tree-ish -- path", but this entry
336 * is in the original index but is not in tree-ish
337 * or does not match the pathspec; it will not be
338 * checked out to the working tree. We will not do
339 * anything to this entry at all.
340 */
341 return;
342 /*
343 * Either this entry came from the tree-ish we are
344 * checking the paths out of, or we are checking out
345 * of the index.
346 *
347 * If it comes from the tree-ish, we already know it
348 * matches the pathspec and could just stamp
349 * CE_MATCHED to it from update_some(). But we still
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100350 * need ps_matched and read_tree (and
Thomas Gummererb7033e72018-12-20 13:48:18 +0000351 * eventually tree_entry_interesting) cannot fill
352 * ps_matched yet. Once it can, we can avoid calling
353 * match_pathspec() for _all_ entries when
354 * opts->source_tree != NULL.
355 */
356 if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
357 ce->ce_flags |= CE_MATCHED;
358}
359
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000360static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
361 char *ps_matched,
362 const struct checkout_opts *opts)
363{
364 ce->ce_flags &= ~CE_MATCHED;
365 if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
366 return;
367 if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) {
368 ce->ce_flags |= CE_MATCHED;
369 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
370 /*
371 * In overlay mode, but the path is not in
372 * tree-ish, which means we should remove it
373 * from the index and the working tree.
374 */
375 ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE;
376 }
377}
378
brian m. carlsona8604762020-03-10 18:20:39 +0000379static int checkout_worktree(const struct checkout_opts *opts,
380 const struct branch_info *info)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500381{
René Scharfe68e3d622016-09-22 18:11:33 +0200382 struct checkout state = CHECKOUT_INIT;
Nguyễn Thái Ngọc Duy1d1f6892019-02-06 09:51:15 +0700383 int nr_checkouts = 0, nr_unmerged = 0;
Nguyễn Thái Ngọc Duy40581992019-04-25 16:45:49 +0700384 int errs = 0;
385 int pos;
Matheus Tavares60539502021-05-04 13:27:29 -0300386 int pc_workers, pc_threshold;
387 struct mem_pool ce_mem_pool;
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +0700388
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500389 state.force = 1;
390 state.refresh_cache = 1;
Nguyễn Thái Ngọc Duyd4a20242014-06-13 19:19:34 +0700391 state.istate = &the_index;
Lars Schneider2841e8f2017-06-30 22:41:28 +0200392
Matheus Tavares60539502021-05-04 13:27:29 -0300393 mem_pool_init(&ce_mem_pool, 0);
394 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
brian m. carlsonc397aac2020-03-16 18:05:03 +0000395 init_checkout_metadata(&state.meta, info->refname,
396 info->commit ? &info->commit->object.oid : &info->oid,
397 NULL);
398
Lars Schneider2841e8f2017-06-30 22:41:28 +0200399 enable_delayed_checkout(&state);
Derrick Stolee0f6d3ba2021-04-01 01:49:44 +0000400
Matheus Tavares60539502021-05-04 13:27:29 -0300401 if (pc_workers > 1)
402 init_parallel_checkout();
403
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100404 for (pos = 0; pos < the_index.cache_nr; pos++) {
405 struct cache_entry *ce = the_index.cache[pos];
Nguyễn Thái Ngọc Duye721c152013-03-27 12:58:21 +0700406 if (ce->ce_flags & CE_MATCHED) {
Junio C Hamano8fdcf312008-08-29 13:40:36 -0700407 if (!ce_stage(ce)) {
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100408 errs |= checkout_entry(ce, &state,
409 NULL, &nr_checkouts);
Junio C Hamano8fdcf312008-08-29 13:40:36 -0700410 continue;
411 }
Stefan Bellerf9022072013-08-03 13:51:21 +0200412 if (opts->writeout_stage)
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100413 errs |= checkout_stage(opts->writeout_stage,
414 ce, pos,
Junio C Hamano7d0c1f42019-03-07 09:59:51 +0900415 &state,
416 &nr_checkouts, opts->overlay_mode);
Stefan Bellerf9022072013-08-03 13:51:21 +0200417 else if (opts->merge)
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100418 errs |= checkout_merged(pos, &state,
Matheus Tavares60539502021-05-04 13:27:29 -0300419 &nr_unmerged,
420 &ce_mem_pool);
Junio C Hamano8fdcf312008-08-29 13:40:36 -0700421 pos = skip_same_name(ce, pos) - 1;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500422 }
423 }
Matheus Tavares60539502021-05-04 13:27:29 -0300424 if (pc_workers > 1)
425 errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
426 NULL, NULL);
427 mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000428 remove_marked_cache_entries(&the_index, 1);
429 remove_scheduled_dirs();
Matheus Tavares611c7782022-07-14 08:49:12 -0300430 errs |= finish_delayed_checkout(&state, opts->show_progress);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100431
432 if (opts->count_checkout_paths) {
Nguyễn Thái Ngọc Duy1d1f6892019-02-06 09:51:15 +0700433 if (nr_unmerged)
434 fprintf_ln(stderr, Q_("Recreated %d merge conflict",
435 "Recreated %d merge conflicts",
436 nr_unmerged),
437 nr_unmerged);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100438 if (opts->source_tree)
Nguyễn Thái Ngọc Duy3c5883b2019-02-06 09:51:14 +0700439 fprintf_ln(stderr, Q_("Updated %d path from %s",
440 "Updated %d paths from %s",
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100441 nr_checkouts),
442 nr_checkouts,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200443 repo_find_unique_abbrev(the_repository, &opts->source_tree->object.oid,
444 DEFAULT_ABBREV));
Nguyễn Thái Ngọc Duy1d1f6892019-02-06 09:51:15 +0700445 else if (!nr_unmerged || nr_checkouts)
Nguyễn Thái Ngọc Duy3c5883b2019-02-06 09:51:14 +0700446 fprintf_ln(stderr, Q_("Updated %d path from the index",
447 "Updated %d paths from the index",
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100448 nr_checkouts),
449 nr_checkouts);
450 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500451
Nguyễn Thái Ngọc Duy40581992019-04-25 16:45:49 +0700452 return errs;
453}
454
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500455static int checkout_paths(const struct checkout_opts *opts,
brian m. carlsona8604762020-03-10 18:20:39 +0000456 const struct branch_info *new_branch_info)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500457{
458 int pos;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500459 static char *ps_matched;
460 struct object_id rev;
461 struct commit *head;
462 int errs = 0;
463 struct lock_file lock_file = LOCK_INIT;
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700464 int checkout_index;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500465
466 trace2_cmd_mode(opts->patch_mode ? "patch" : "path");
467
468 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
469 die(_("'%s' cannot be used with updating paths"), "--track");
470
471 if (opts->new_branch_log)
472 die(_("'%s' cannot be used with updating paths"), "-l");
473
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +0700474 if (opts->ignore_unmerged && opts->patch_mode)
475 die(_("'%s' cannot be used with updating paths"),
476 opts->ignore_unmerged_opt);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500477
478 if (opts->force_detach)
479 die(_("'%s' cannot be used with updating paths"), "--detach");
480
481 if (opts->merge && opts->patch_mode)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000482 die(_("options '%s' and '%s' cannot be used together"), "--merge", "--patch");
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500483
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +0700484 if (opts->ignore_unmerged && opts->merge)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000485 die(_("options '%s' and '%s' cannot be used together"),
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +0700486 opts->ignore_unmerged_opt, "-m");
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500487
488 if (opts->new_branch)
489 die(_("Cannot update paths and switch to branch '%s' at the same time."),
490 opts->new_branch);
491
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700492 if (!opts->checkout_worktree && !opts->checkout_index)
493 die(_("neither '%s' or '%s' is specified"),
494 "--staged", "--worktree");
495
496 if (!opts->checkout_worktree && !opts->from_treeish)
497 die(_("'%s' must be used when '%s' is not specified"),
498 "--worktree", "--source");
499
Andy Koppeee8a8882023-02-26 18:43:54 +0000500 /*
501 * Reject --staged option to the restore command when combined with
502 * merge-related options. Use the accept_ref flag to distinguish it
503 * from the checkout command, which does not accept --staged anyway.
504 *
505 * `restore --ours|--theirs --worktree --staged` could mean resolving
506 * conflicted paths to one side in both the worktree and the index,
507 * but does not currently.
508 *
509 * `restore --merge|--conflict=<style>` already recreates conflicts
510 * in both the worktree and the index, so adding --staged would be
511 * meaningless.
512 */
513 if (!opts->accept_ref && opts->checkout_index) {
514 if (opts->writeout_stage)
515 die(_("'%s' or '%s' cannot be used with %s"),
516 "--ours", "--theirs", "--staged");
Nguyễn Thái Ngọc Duye3ddd3b2019-04-25 16:45:51 +0700517
Andy Koppeee8a8882023-02-26 18:43:54 +0000518 if (opts->merge)
519 die(_("'%s' or '%s' cannot be used with %s"),
520 "--merge", "--conflict", "--staged");
521 }
Nguyễn Thái Ngọc Duye3ddd3b2019-04-25 16:45:51 +0700522
Junio C Hamano54f98fe2023-07-31 15:44:07 -0700523 /*
524 * recreating unmerged index entries and writing out data from
525 * unmerged index entries would make no sense when checking out
526 * of a tree-ish.
527 */
528 if ((opts->merge || opts->writeout_stage) && opts->source_tree)
529 die(_("'%s', '%s', or '%s' cannot be used when checking out of a tree"),
530 "--merge", "--ours", "--theirs");
531
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700532 if (opts->patch_mode) {
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +0100533 enum add_p_mode patch_mode;
Denton Liu5602b502020-10-07 00:56:15 -0700534 const char *rev = new_branch_info->name;
535 char rev_oid[GIT_MAX_HEXSZ + 1];
536
537 /*
538 * Since rev can be in the form of `<a>...<b>` (which is not
539 * recognized by diff-index), we will always replace the name
540 * with the hex of the commit (whether it's in `...` form or
541 * not) for the run_add_interactive() machinery to work
542 * properly. However, there is special logic for the HEAD case
Johannes Schindelin5c29f192020-12-19 14:55:59 +0000543 * so we mustn't replace that. Also, when we were given a
544 * tree-object, new_branch_info->commit would be NULL, but we
545 * do not have to do any replacement, either.
Denton Liu5602b502020-10-07 00:56:15 -0700546 */
Johannes Schindelin5c29f192020-12-19 14:55:59 +0000547 if (rev && new_branch_info->commit && strcmp(rev, "HEAD"))
Denton Liu5602b502020-10-07 00:56:15 -0700548 rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid);
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700549
550 if (opts->checkout_index && opts->checkout_worktree)
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +0100551 patch_mode = ADD_P_CHECKOUT;
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700552 else if (opts->checkout_index && !opts->checkout_worktree)
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +0100553 patch_mode = ADD_P_RESET;
Nguyễn Thái Ngọc Duy2f0896e2019-04-25 16:45:54 +0700554 else if (!opts->checkout_index && opts->checkout_worktree)
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +0100555 patch_mode = ADD_P_WORKTREE;
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700556 else
Nguyễn Thái Ngọc Duy2f0896e2019-04-25 16:45:54 +0700557 BUG("either flag must have been set, worktree=%d, index=%d",
558 opts->checkout_worktree, opts->checkout_index);
Ævar Arnfjörð Bjarmasond21878f2023-02-06 23:58:57 +0100559 return !!run_add_p(the_repository, patch_mode, rev,
560 &opts->pathspec);
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700561 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500562
563 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100564 if (repo_read_index_preload(the_repository, &opts->pathspec, 0) < 0)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500565 return error(_("index file corrupt"));
566
567 if (opts->source_tree)
568 read_tree_some(opts->source_tree, &opts->pathspec);
Junio C Hamano5bdedac2023-07-31 15:44:09 -0700569 if (opts->merge)
570 unmerge_index(&the_index, &opts->pathspec, CE_MATCHED);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500571
572 ps_matched = xcalloc(opts->pathspec.nr, 1);
573
574 /*
575 * Make sure all pathspecs participated in locating the paths
576 * to be checked out.
577 */
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100578 for (pos = 0; pos < the_index.cache_nr; pos++)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500579 if (opts->overlay_mode)
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100580 mark_ce_for_checkout_overlay(the_index.cache[pos],
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500581 ps_matched,
582 opts);
583 else
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100584 mark_ce_for_checkout_no_overlay(the_index.cache[pos],
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500585 ps_matched,
586 opts);
587
Junio C Hamanof496b062019-07-09 15:25:44 -0700588 if (report_path_error(ps_matched, &opts->pathspec)) {
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500589 free(ps_matched);
590 return 1;
591 }
592 free(ps_matched);
Daniel Barkalow75336872008-02-28 16:52:44 -0500593
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500594 /* Any unmerged paths? */
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100595 for (pos = 0; pos < the_index.cache_nr; pos++) {
596 const struct cache_entry *ce = the_index.cache[pos];
Junio C Hamanod2b36912008-05-28 14:48:57 -0700597 if (ce->ce_flags & CE_MATCHED) {
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500598 if (!ce_stage(ce))
599 continue;
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +0700600 if (opts->ignore_unmerged) {
601 if (!opts->quiet)
602 warning(_("path '%s' is unmerged"), ce->name);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500603 } else if (opts->writeout_stage) {
604 errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode);
605 } else if (opts->merge) {
606 errs |= check_stages((1<<2) | (1<<3), ce, pos);
607 } else {
608 errs = 1;
609 error(_("path '%s' is unmerged"), ce->name);
610 }
611 pos = skip_same_name(ce, pos) - 1;
612 }
613 }
614 if (errs)
615 return 1;
616
617 /* Now we are committed to check them out */
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700618 if (opts->checkout_worktree)
brian m. carlsona8604762020-03-10 18:20:39 +0000619 errs |= checkout_worktree(opts, new_branch_info);
Jeff Kinge701bab2020-01-08 06:43:44 -0500620 else
621 remove_marked_cache_entries(&the_index, 1);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500622
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +0700623 /*
624 * Allow updating the index when checking out from the index.
625 * This is to save new stat info.
626 */
627 if (opts->checkout_worktree && !opts->checkout_index && !opts->source_tree)
628 checkout_index = 1;
629 else
630 checkout_index = opts->checkout_index;
631
632 if (checkout_index) {
633 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
634 die(_("unable to write new index file"));
635 } else {
636 /*
637 * NEEDSWORK: if --worktree is not specified, we
638 * should save stat info of checked out files in the
639 * index to avoid the next (potentially costly)
640 * refresh. But it's a bit tricker to do...
641 */
642 rollback_lock_file(&lock_file);
643 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500644
brian m. carlson34c290a2017-10-15 22:06:56 +0000645 read_ref_full("HEAD", 0, &rev, NULL);
Stefan Beller21e1ee82018-06-28 18:21:57 -0700646 head = lookup_commit_reference_gently(the_repository, &rev, 1);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500647
648 errs |= post_checkout_hook(head, head, 0);
649 return errs;
650}
651
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +0700652static void show_local_changes(struct object *head,
653 const struct diff_options *opts)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500654{
655 struct rev_info rev;
656 /* I think we want full paths, even if we're in a subdirectory. */
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200657 repo_init_revisions(the_repository, &rev, NULL);
Johannes Schindelin175f6e52010-08-25 12:34:57 +0200658 rev.diffopt.flags = opts->flags;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500659 rev.diffopt.output_format |= DIFF_FORMAT_NAME_STATUS;
Victoria Dye49ff3cb2022-08-08 19:07:49 +0000660 rev.diffopt.flags.recursive = 1;
Thomas Rast28452652012-08-03 14:16:24 +0200661 diff_setup_done(&rev.diffopt);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500662 add_pending_object(&rev, head, NULL);
663 run_diff_index(&rev, 0);
Ævar Arnfjörð Bjarmason1878b5e2022-04-13 22:01:35 +0200664 release_revisions(&rev);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500665}
666
Jonathan Niederb3c04942011-02-22 22:43:22 +0000667static void describe_detached_head(const char *msg, struct commit *commit)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500668{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500669 struct strbuf sb = STRBUF_INIT;
Ann T Ropeaca69d4d2017-12-06 01:20:42 +0100670
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200671 if (!repo_parse_commit(the_repository, commit))
Jeff King3c621832013-10-24 04:54:53 -0400672 pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
Ann T Ropeaca69d4d2017-12-06 01:20:42 +0100673 if (print_sha1_ellipsis()) {
674 fprintf(stderr, "%s %s... %s\n", msg,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200675 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV),
676 sb.buf);
Ann T Ropeaca69d4d2017-12-06 01:20:42 +0100677 } else {
678 fprintf(stderr, "%s %s %s\n", msg,
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200679 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV),
680 sb.buf);
Ann T Ropeaca69d4d2017-12-06 01:20:42 +0100681 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500682 strbuf_release(&sb);
683}
684
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +0700685static int reset_tree(struct tree *tree, const struct checkout_opts *o,
brian m. carlson13e7ed62020-03-16 18:05:04 +0000686 int worktree, int *writeout_error,
687 struct branch_info *info)
Junio C Hamano6286a082008-05-28 14:59:40 -0700688{
689 struct unpack_trees_options opts;
690 struct tree_desc tree_desc;
691
692 memset(&opts, 0, sizeof(opts));
693 opts.head_idx = -1;
694 opts.update = worktree;
695 opts.skip_unmerged = !worktree;
Elijah Newren480d3d62021-09-27 16:33:44 +0000696 opts.reset = o->force ? UNPACK_RESET_OVERWRITE_UNTRACKED :
697 UNPACK_RESET_PROTECT_UNTRACKED;
698 opts.preserve_ignored = (!o->force && !o->overwrite_ignore);
Junio C Hamano6286a082008-05-28 14:59:40 -0700699 opts.merge = 1;
700 opts.fn = oneway_merge;
Edmundo Carmona Antoranz870ebdb2015-11-01 15:19:05 -0600701 opts.verbose_update = o->show_progress;
Junio C Hamano6286a082008-05-28 14:59:40 -0700702 opts.src_index = &the_index;
703 opts.dst_index = &the_index;
brian m. carlson13e7ed62020-03-16 18:05:04 +0000704 init_checkout_metadata(&opts.meta, info->refname,
brian m. carlson14228442021-04-26 01:02:56 +0000705 info->commit ? &info->commit->object.oid : null_oid(),
brian m. carlson13e7ed62020-03-16 18:05:04 +0000706 NULL);
Junio C Hamano6286a082008-05-28 14:59:40 -0700707 parse_tree(tree);
708 init_tree_desc(&tree_desc, tree->buffer, tree->size);
Junio C Hamano291d8232008-05-28 15:26:59 -0700709 switch (unpack_trees(1, &tree_desc, &opts)) {
710 case -2:
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +0700711 *writeout_error = 1;
Junio C Hamano291d8232008-05-28 15:26:59 -0700712 /*
713 * We return 0 nevertheless, as the index is all right
714 * and more importantly we have made best efforts to
715 * update paths in the work tree, and we cannot revert
716 * them.
717 */
Jeff King1cf01a32017-09-21 02:25:41 -0400718 /* fallthrough */
Junio C Hamano291d8232008-05-28 15:26:59 -0700719 case 0:
720 return 0;
721 default:
Junio C Hamano6286a082008-05-28 14:59:40 -0700722 return 128;
Junio C Hamano291d8232008-05-28 15:26:59 -0700723 }
Junio C Hamano6286a082008-05-28 14:59:40 -0700724}
725
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500726static void setup_branch_path(struct branch_info *branch)
727{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500728 struct strbuf buf = STRBUF_INIT;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100729
brian m. carlsonc397aac2020-03-16 18:05:03 +0000730 /*
731 * If this is a ref, resolve it; otherwise, look up the OID for our
732 * expression. Failure here is okay.
733 */
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +0200734 if (!repo_dwim_ref(the_repository, branch->name, strlen(branch->name),
735 &branch->oid, &branch->refname, 0))
brian m. carlsonc397aac2020-03-16 18:05:03 +0000736 repo_get_oid_committish(the_repository, branch->name, &branch->oid);
737
Jeff Kingfd4692f2017-03-02 03:23:18 -0500738 strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +0100739 if (strcmp(buf.buf, branch->name)) {
740 free(branch->name);
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100741 branch->name = xstrdup(buf.buf);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +0100742 }
Junio C Hamanoa552de72009-03-21 13:17:30 -0700743 strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +0100744 free(branch->path);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500745 branch->path = strbuf_detach(&buf, NULL);
746}
747
Ævar Arnfjörð Bjarmason33d0dda2022-07-01 12:42:57 +0200748static void init_topts(struct unpack_trees_options *topts, int merge,
749 int show_progress, int overwrite_ignore,
750 struct commit *old_commit)
751{
752 memset(topts, 0, sizeof(*topts));
753 topts->head_idx = -1;
754 topts->src_index = &the_index;
755 topts->dst_index = &the_index;
756
757 setup_unpack_trees_porcelain(topts, "checkout");
758
Ævar Arnfjörð Bjarmasonfbc1ed62022-11-19 14:07:30 +0100759 topts->initial_checkout = is_index_unborn(&the_index);
Ævar Arnfjörð Bjarmason33d0dda2022-07-01 12:42:57 +0200760 topts->update = 1;
761 topts->merge = 1;
762 topts->quiet = merge && old_commit;
763 topts->verbose_update = show_progress;
764 topts->fn = twoway_merge;
765 topts->preserve_ignored = !overwrite_ignore;
766}
767
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +0700768static int merge_working_tree(const struct checkout_opts *opts,
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800769 struct branch_info *old_branch_info,
770 struct branch_info *new_branch_info,
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +0700771 int *writeout_error)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500772{
773 int ret;
Martin Ågren837e34e2017-10-05 22:32:04 +0200774 struct lock_file lock_file = LOCK_INIT;
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700775 struct tree *new_tree;
Junio C Hamanob96524f2008-12-05 17:54:10 -0800776
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100777 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
778 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
Vasco Almeidae923a8a2016-06-17 20:21:20 +0000779 return error(_("index file corrupt"));
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500780
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100781 resolve_undo_clear_index(&the_index);
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700782 if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
783 if (new_branch_info->commit)
784 BUG("'switch --orphan' should never accept a commit as starting point");
785 new_tree = parse_tree_indirect(the_hash_algo->empty_tree);
786 } else
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200787 new_tree = repo_get_commit_tree(the_repository,
788 new_branch_info->commit);
Nguyễn Thái Ngọc Duy3ec37ad2019-03-29 17:39:07 +0700789 if (opts->discard_changes) {
brian m. carlson13e7ed62020-03-16 18:05:04 +0000790 ret = reset_tree(new_tree, opts, 1, writeout_error, new_branch_info);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500791 if (ret)
792 return ret;
793 } else {
794 struct tree_desc trees[2];
795 struct tree *tree;
796 struct unpack_trees_options topts;
Glen Choo8d2eaf62022-03-01 16:36:13 -0800797 const struct object_id *old_commit_oid;
Linus Torvaldsbc052d72008-03-06 12:26:14 -0800798
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100799 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500800
Ævar Arnfjörð Bjarmasonfbc1ed62022-11-19 14:07:30 +0100801 if (unmerged_index(&the_index)) {
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +0000802 error(_("you need to resolve your current index first"));
Junio C Hamano04c9e112008-02-23 15:45:19 -0800803 return 1;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500804 }
Junio C Hamano04c9e112008-02-23 15:45:19 -0800805
806 /* 2-way merge to the new branch */
Ævar Arnfjörð Bjarmason33d0dda2022-07-01 12:42:57 +0200807 init_topts(&topts, opts->merge, opts->show_progress,
808 opts->overwrite_ignore, old_branch_info->commit);
brian m. carlson13e7ed62020-03-16 18:05:04 +0000809 init_checkout_metadata(&topts.meta, new_branch_info->refname,
810 new_branch_info->commit ?
811 &new_branch_info->commit->object.oid :
812 &new_branch_info->oid, NULL);
Glen Choo8d2eaf62022-03-01 16:36:13 -0800813
814 old_commit_oid = old_branch_info->commit ?
815 &old_branch_info->commit->object.oid :
816 the_hash_algo->empty_tree;
817 tree = parse_tree_indirect(old_commit_oid);
818 if (!tree)
819 die(_("unable to parse commit %s"),
820 oid_to_hex(old_commit_oid));
821
Junio C Hamano04c9e112008-02-23 15:45:19 -0800822 init_tree_desc(&trees[0], tree->buffer, tree->size);
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700823 parse_tree(new_tree);
824 tree = new_tree;
Junio C Hamano04c9e112008-02-23 15:45:19 -0800825 init_tree_desc(&trees[1], tree->buffer, tree->size);
826
Junio C Hamano291d8232008-05-28 15:26:59 -0700827 ret = unpack_trees(2, trees, &topts);
Martin Ågren1c41d282018-05-21 16:54:28 +0200828 clear_unpack_trees_porcelain(&topts);
Junio C Hamano49d833d2009-05-12 09:41:28 -0700829 if (ret == -1) {
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500830 /*
831 * Unpack couldn't do a trivial merge; either
832 * give up or do a real merge, depending on
833 * whether the merge flag was used.
834 */
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500835 struct tree *work;
Nguyễn Thái Ngọc Duy6eff4092019-03-22 16:31:38 +0700836 struct tree *old_tree;
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200837 struct merge_options o;
Nguyễn Thái Ngọc Duya7256de2019-03-19 16:39:10 +0700838 struct strbuf sb = STRBUF_INIT;
Elijah Newren65c01c62019-08-15 14:40:31 -0700839 struct strbuf old_commit_shortname = STRBUF_INIT;
Nguyễn Thái Ngọc Duya7256de2019-03-19 16:39:10 +0700840
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500841 if (!opts->merge)
842 return 1;
Junio C Hamano64da3ae2010-01-06 23:51:47 -0800843
844 /*
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800845 * Without old_branch_info->commit, the below is the same as
Junio C Hamano64da3ae2010-01-06 23:51:47 -0800846 * the two-tree unpack we already tried and failed.
847 */
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800848 if (!old_branch_info->commit)
Junio C Hamano64da3ae2010-01-06 23:51:47 -0800849 return 1;
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200850 old_tree = repo_get_commit_tree(the_repository,
851 old_branch_info->commit);
Nguyễn Thái Ngọc Duy6eff4092019-03-22 16:31:38 +0700852
853 if (repo_index_has_changes(the_repository, old_tree, &sb))
854 die(_("cannot continue with staged changes in "
855 "the following files:\n%s"), sb.buf);
856 strbuf_release(&sb);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500857
858 /* Do more real merge */
859
860 /*
861 * We update the index fully, then write the
862 * tree from the index, then merge the new
863 * branch with the current tree, with the old
864 * branch as the base. Then we reset the index
865 * (but not the working tree) to the new
866 * branch, leaving the working tree as the
867 * merged version, but skipping unmerged
868 * entries in the index.
869 */
870
Elijah Newren50c37ee2023-05-16 06:33:46 +0000871 add_files_to_cache(the_repository, NULL, NULL, 0, 0);
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700872 init_merge_options(&o, the_repository);
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200873 o.verbosity = 0;
Elijah Newren724dd762019-08-17 11:41:32 -0700874 work = write_in_core_index_as_tree(the_repository);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500875
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700876 ret = reset_tree(new_tree,
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000877 opts, 1,
brian m. carlson13e7ed62020-03-16 18:05:04 +0000878 writeout_error, new_branch_info);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500879 if (ret)
880 return ret;
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800881 o.ancestor = old_branch_info->name;
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700882 if (!old_branch_info->name) {
Elijah Newren65c01c62019-08-15 14:40:31 -0700883 strbuf_add_unique_abbrev(&old_commit_shortname,
884 &old_branch_info->commit->object.oid,
885 DEFAULT_ABBREV);
886 o.ancestor = old_commit_shortname.buf;
887 }
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800888 o.branch1 = new_branch_info->name;
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200889 o.branch2 = "local";
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000890 ret = merge_trees(&o,
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700891 new_tree,
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000892 work,
Elijah Newrenb4db8a22019-08-17 11:41:30 -0700893 old_tree);
Johannes Schindelinf241ff02016-07-26 18:06:02 +0200894 if (ret < 0)
895 exit(128);
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700896 ret = reset_tree(new_tree,
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000897 opts, 0,
brian m. carlson13e7ed62020-03-16 18:05:04 +0000898 writeout_error, new_branch_info);
Johannes Schindelin548009c2016-08-01 13:44:53 +0200899 strbuf_release(&o.obuf);
Elijah Newren65c01c62019-08-15 14:40:31 -0700900 strbuf_release(&old_commit_shortname);
Junio C Hamano84a57502008-05-28 14:54:02 -0700901 if (ret)
902 return ret;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500903 }
904 }
905
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100906 if (!cache_tree_fully_valid(the_index.cache_tree))
Junio C Hamano3fd13cb2014-09-11 10:33:32 -0700907 cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
David Turneraecf5672014-07-05 21:06:56 -0700908
Martin Ågren837e34e2017-10-05 22:32:04 +0200909 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +0000910 die(_("unable to write new index file"));
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500911
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +0700912 if (!opts->discard_changes && !opts->quiet && new_branch_info->commit)
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800913 show_local_changes(&new_branch_info->commit->object, &opts->diff_options);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500914
915 return 0;
916}
917
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800918static void report_tracking(struct branch_info *new_branch_info)
Junio C Hamano79a1e6b2008-02-16 17:17:09 -0800919{
Junio C Hamano6d21bf92008-07-02 00:51:18 -0700920 struct strbuf sb = STRBUF_INIT;
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800921 struct branch *branch = branch_get(new_branch_info->name);
Junio C Hamano79a1e6b2008-02-16 17:17:09 -0800922
Alex Henrieb6f3da52023-07-12 22:41:13 -0600923 if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL, 1))
Junio C Hamano79a1e6b2008-02-16 17:17:09 -0800924 return;
Junio C Hamano6d21bf92008-07-02 00:51:18 -0700925 fputs(sb.buf, stdout);
926 strbuf_release(&sb);
Junio C Hamanob0030db2008-02-20 15:05:23 -0800927}
Junio C Hamano79a1e6b2008-02-16 17:17:09 -0800928
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +0700929static void update_refs_for_switch(const struct checkout_opts *opts,
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800930 struct branch_info *old_branch_info,
931 struct branch_info *new_branch_info)
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500932{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500933 struct strbuf msg = STRBUF_INIT;
Ramkumar Ramachandra3bed2912013-06-16 14:15:16 +0530934 const char *old_desc, *reflog_msg;
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500935 if (opts->new_branch) {
Erick Mattos3631bf72010-05-21 21:28:37 -0300936 if (opts->new_orphan_branch) {
Cornelius Weig341fb282017-01-27 11:09:47 +0100937 char *refname;
938
939 refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
940 if (opts->new_branch_log &&
941 !should_autocreate_reflog(refname)) {
Nguyễn Thái Ngọc Duy1a83c242014-11-30 15:24:28 +0700942 int ret;
David Turnera4c653d2015-07-21 17:04:50 -0400943 struct strbuf err = STRBUF_INIT;
Erick Mattos3631bf72010-05-21 21:28:37 -0300944
Han-Wen Nienhuys7b089122021-11-22 14:19:08 +0000945 ret = safe_create_reflog(refname, &err);
Nguyễn Thái Ngọc Duy1a83c242014-11-30 15:24:28 +0700946 if (ret) {
David Turnera4c653d2015-07-21 17:04:50 -0400947 fprintf(stderr, _("Can not do reflog for '%s': %s\n"),
948 opts->new_orphan_branch, err.buf);
949 strbuf_release(&err);
Cornelius Weig341fb282017-01-27 11:09:47 +0100950 free(refname);
Erick Mattos3631bf72010-05-21 21:28:37 -0300951 return;
952 }
David Turnerabd0cd32015-07-21 17:04:52 -0400953 strbuf_release(&err);
Erick Mattos3631bf72010-05-21 21:28:37 -0300954 }
Cornelius Weig341fb282017-01-27 11:09:47 +0100955 free(refname);
Erick Mattos3631bf72010-05-21 21:28:37 -0300956 }
957 else
Nguyễn Thái Ngọc Duy4edce172018-11-10 06:49:00 +0100958 create_branch(the_repository,
959 opts->new_branch, new_branch_info->name,
Tay Ray Chuan02ac9832010-06-24 03:29:00 +0800960 opts->new_branch_force ? 1 : 0,
Jonathan Nieder39bd6f72011-11-26 02:54:55 -0600961 opts->new_branch_force ? 1 : 0,
Kaartic Sivaraame2bbd0c2017-11-18 22:56:46 +0530962 opts->new_branch_log,
Jeff Kingf9a482e2012-03-26 19:51:01 -0400963 opts->quiet,
Glen Choo3f3e7602022-01-28 16:04:43 -0800964 opts->track,
965 0);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +0100966 free(new_branch_info->name);
967 free(new_branch_info->refname);
968 new_branch_info->name = xstrdup(opts->new_branch);
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800969 setup_branch_path(new_branch_info);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500970 }
971
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800972 old_desc = old_branch_info->name;
973 if (!old_desc && old_branch_info->commit)
974 old_desc = oid_to_hex(&old_branch_info->commit->object.oid);
Ramkumar Ramachandra3bed2912013-06-16 14:15:16 +0530975
976 reflog_msg = getenv("GIT_REFLOG_ACTION");
977 if (!reflog_msg)
978 strbuf_addf(&msg, "checkout: moving from %s to %s",
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800979 old_desc ? old_desc : "(invalid)", new_branch_info->name);
Ramkumar Ramachandra3bed2912013-06-16 14:15:16 +0530980 else
René Scharfea91cc7f2020-02-09 14:44:23 +0100981 strbuf_insertstr(&msg, 0, reflog_msg);
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500982
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800983 if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) {
Jonathan Niederf8bd36a2011-02-08 04:34:34 -0600984 /* Nothing to do. */
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800985 } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */
986 update_ref(msg.buf, "HEAD", &new_branch_info->commit->object.oid, NULL,
Michael Haggerty91774af2017-11-05 09:42:06 +0100987 REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR);
Jonathan Niederf8bd36a2011-02-08 04:34:34 -0600988 if (!opts->quiet) {
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800989 if (old_branch_info->path &&
Ben Boeckeled9bff02021-08-23 12:44:00 +0200990 advice_enabled(ADVICE_DETACHED_HEAD) && !opts->force_detach)
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800991 detach_advice(new_branch_info->name);
992 describe_detached_head(_("HEAD is now at"), new_branch_info->commit);
Jonathan Niederf8bd36a2011-02-08 04:34:34 -0600993 }
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800994 } else if (new_branch_info->path) { /* Switch branches. */
995 if (create_symref("HEAD", new_branch_info->path, msg.buf) < 0)
Nguyễn Thái Ngọc Duy4636f652016-02-27 13:41:54 +0700996 die(_("unable to update HEAD"));
Daniel Barkalow782c2d62008-02-07 11:40:23 -0500997 if (!opts->quiet) {
Brandon Williamsc8a3ea12018-02-14 10:59:31 -0800998 if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) {
Jonathan Nieder39bd6f72011-11-26 02:54:55 -0600999 if (opts->new_branch_force)
1000 fprintf(stderr, _("Reset branch '%s'\n"),
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001001 new_branch_info->name);
Jonathan Nieder39bd6f72011-11-26 02:54:55 -06001002 else
1003 fprintf(stderr, _("Already on '%s'\n"),
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001004 new_branch_info->name);
Ævar Arnfjörð Bjarmason08eaa4b2011-02-22 23:41:43 +00001005 } else if (opts->new_branch) {
1006 if (opts->branch_exists)
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001007 fprintf(stderr, _("Switched to and reset branch '%s'\n"), new_branch_info->name);
Ævar Arnfjörð Bjarmason08eaa4b2011-02-22 23:41:43 +00001008 else
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001009 fprintf(stderr, _("Switched to a new branch '%s'\n"), new_branch_info->name);
Ævar Arnfjörð Bjarmason08eaa4b2011-02-22 23:41:43 +00001010 } else {
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +00001011 fprintf(stderr, _("Switched to branch '%s'\n"),
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001012 new_branch_info->name);
Ævar Arnfjörð Bjarmason08eaa4b2011-02-22 23:41:43 +00001013 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001014 }
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001015 if (old_branch_info->path && old_branch_info->name) {
1016 if (!ref_exists(old_branch_info->path) && reflog_exists(old_branch_info->path))
1017 delete_reflog(old_branch_info->path);
Erick Mattos3631bf72010-05-21 21:28:37 -03001018 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001019 }
Nguyễn Thái Ngọc Duyf4a4b9a2019-03-29 17:38:59 +07001020 remove_branch_state(the_repository, !opts->quiet);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001021 strbuf_release(&msg);
Junio C Hamano32669672011-02-08 04:32:49 -06001022 if (!opts->quiet &&
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001023 (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD"))))
1024 report_tracking(new_branch_info);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001025}
1026
René Scharfe468224e2011-10-01 17:51:39 +02001027static int add_pending_uninteresting_ref(const char *refname,
Michael Haggertyfcb615f2015-05-25 18:38:33 +00001028 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001029 int flags UNUSED, void *cb_data)
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001030{
brian m. carlsona58a1b02017-05-06 22:10:26 +00001031 add_pending_oid(cb_data, refname, oid, UNINTERESTING);
Jeff King5c08dc42011-03-20 05:09:18 -04001032 return 0;
1033}
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001034
1035static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
1036{
Jeff King0be240c2011-03-20 05:19:01 -04001037 strbuf_addstr(sb, " ");
brian m. carlson30e677e2018-03-12 02:27:28 +00001038 strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV);
Jeff King0be240c2011-03-20 05:19:01 -04001039 strbuf_addch(sb, ' ');
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001040 if (!repo_parse_commit(the_repository, commit))
Jeff King3c621832013-10-24 04:54:53 -04001041 pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001042 strbuf_addch(sb, '\n');
1043}
1044
1045#define ORPHAN_CUTOFF 4
1046static void suggest_reattach(struct commit *commit, struct rev_info *revs)
1047{
1048 struct commit *c, *last = NULL;
1049 struct strbuf sb = STRBUF_INIT;
1050 int lost = 0;
1051 while ((c = get_revision(revs)) != NULL) {
1052 if (lost < ORPHAN_CUTOFF)
1053 describe_one_orphan(&sb, c);
1054 last = c;
1055 lost++;
1056 }
1057 if (ORPHAN_CUTOFF < lost) {
1058 int more = lost - ORPHAN_CUTOFF;
1059 if (more == 1)
1060 describe_one_orphan(&sb, last);
1061 else
Ævar Arnfjörð Bjarmasonf06f08b2011-04-10 19:34:08 +00001062 strbuf_addf(&sb, _(" ... and %d more.\n"), more);
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001063 }
1064
1065 fprintf(stderr,
Ævar Arnfjörð Bjarmasonf06f08b2011-04-10 19:34:08 +00001066 Q_(
1067 /* The singular version */
1068 "Warning: you are leaving %d commit behind, "
1069 "not connected to\n"
1070 "any of your branches:\n\n"
Junio C Hamanof807b3d2011-05-24 10:08:29 -07001071 "%s\n",
Ævar Arnfjörð Bjarmasonf06f08b2011-04-10 19:34:08 +00001072 /* The plural version */
1073 "Warning: you are leaving %d commits behind, "
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001074 "not connected to\n"
1075 "any of your branches:\n\n"
Junio C Hamano0faf2472011-06-29 17:03:10 -07001076 "%s\n",
Ævar Arnfjörð Bjarmasonf06f08b2011-04-10 19:34:08 +00001077 /* Give ngettext() the count */
1078 lost),
1079 lost,
Junio C Hamanof807b3d2011-05-24 10:08:29 -07001080 sb.buf);
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001081 strbuf_release(&sb);
Junio C Hamanof807b3d2011-05-24 10:08:29 -07001082
Ben Boeckeled9bff02021-08-23 12:44:00 +02001083 if (advice_enabled(ADVICE_DETACHED_HEAD))
Junio C Hamanof807b3d2011-05-24 10:08:29 -07001084 fprintf(stderr,
Thomas Schneiderfc792ca2015-04-01 19:38:00 +02001085 Q_(
1086 /* The singular version */
1087 "If you want to keep it by creating a new branch, "
1088 "this may be a good time\nto do so with:\n\n"
1089 " git branch <new-branch-name> %s\n\n",
1090 /* The plural version */
Junio C Hamanof807b3d2011-05-24 10:08:29 -07001091 "If you want to keep them by creating a new branch, "
1092 "this may be a good time\nto do so with:\n\n"
Thomas Schneiderfc792ca2015-04-01 19:38:00 +02001093 " git branch <new-branch-name> %s\n\n",
1094 /* Give ngettext() the count */
1095 lost),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001096 repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV));
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001097}
1098
1099/*
1100 * We are about to leave commit that was at the tip of a detached
1101 * HEAD. If it is not reachable from any ref, this is the last chance
1102 * for the user to do so without resorting to reflog.
1103 */
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001104static void orphaned_commit_warning(struct commit *old_commit, struct commit *new_commit)
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001105{
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001106 struct rev_info revs;
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001107 struct object *object = &old_commit->object;
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001108
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001109 repo_init_revisions(the_repository, &revs, NULL);
René Scharfe468224e2011-10-01 17:51:39 +02001110 setup_revisions(0, NULL, &revs, NULL);
1111
1112 object->flags &= ~UNINTERESTING;
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001113 add_pending_object(&revs, object, oid_to_hex(&object->oid));
René Scharfe468224e2011-10-01 17:51:39 +02001114
Michael Haggertyfcb615f2015-05-25 18:38:33 +00001115 for_each_ref(add_pending_uninteresting_ref, &revs);
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +07001116 if (new_commit)
1117 add_pending_oid(&revs, "HEAD",
1118 &new_commit->object.oid,
1119 UNINTERESTING);
René Scharfe468224e2011-10-01 17:51:39 +02001120
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001121 if (prepare_revision_walk(&revs))
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001122 die(_("internal error in revision walk"));
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001123 if (!(old_commit->object.flags & UNINTERESTING))
1124 suggest_reattach(old_commit, &revs);
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001125 else
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001126 describe_detached_head(_("Previous HEAD position was"), old_commit);
Jeff King5c08dc42011-03-20 05:09:18 -04001127
Martin Ågrenb2ccdf72017-09-23 01:34:51 +02001128 /* Clean up objects used, as they will be reused. */
René Scharfecd888842020-10-31 13:46:08 +01001129 repo_clear_commit_marks(the_repository, ALL_REV_FLAGS);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001130 release_revisions(&revs);
Junio C Hamano8e2dc6a2011-02-18 16:04:47 -08001131}
1132
Nguyễn Thái Ngọc Duye51e3052012-08-29 20:55:23 +07001133static int switch_branches(const struct checkout_opts *opts,
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001134 struct branch_info *new_branch_info)
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001135{
1136 int ret = 0;
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001137 struct branch_info old_branch_info = { 0 };
brian m. carlson60af7692016-09-05 20:08:00 +00001138 struct object_id rev;
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +07001139 int flag, writeout_error = 0;
Nguyễn Thái Ngọc Duy65f099b2019-03-29 17:39:14 +07001140 int do_merge = 1;
Jeff Hostetlere27dd8a2019-02-22 14:25:08 -08001141
1142 trace2_cmd_mode("branch");
1143
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001144 memset(&old_branch_info, 0, sizeof(old_branch_info));
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001145 old_branch_info.path = resolve_refdup("HEAD", 0, &rev, &flag);
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001146 if (old_branch_info.path)
Stefan Beller21e1ee82018-06-28 18:21:57 -07001147 old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +07001148 if (!(flag & REF_ISSYMREF))
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001149 FREE_AND_NULL(old_branch_info.path);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001150
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001151 if (old_branch_info.path) {
1152 const char *const prefix = "refs/heads/";
1153 const char *p;
1154 if (skip_prefix(old_branch_info.path, prefix, &p))
1155 old_branch_info.name = xstrdup(p);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001156 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001157
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +07001158 if (opts->new_orphan_branch && opts->orphan_from_empty_tree) {
1159 if (new_branch_info->name)
1160 BUG("'switch --orphan' should never accept a commit as starting point");
1161 new_branch_info->commit = NULL;
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001162 new_branch_info->name = xstrdup("(empty)");
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +07001163 do_merge = 1;
1164 }
1165
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001166 if (!new_branch_info->name) {
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001167 new_branch_info->name = xstrdup("HEAD");
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001168 new_branch_info->commit = old_branch_info.commit;
1169 if (!new_branch_info->commit)
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +00001170 die(_("You are on a branch yet to be born"));
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001171 parse_commit_or_die(new_branch_info->commit);
Nguyễn Thái Ngọc Duy65f099b2019-03-29 17:39:14 +07001172
1173 if (opts->only_merge_on_switching_branches)
1174 do_merge = 0;
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001175 }
1176
Nguyễn Thái Ngọc Duy65f099b2019-03-29 17:39:14 +07001177 if (do_merge) {
Ben Peartfa655d82018-08-16 18:27:11 +00001178 ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
1179 if (ret) {
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001180 branch_info_release(&old_branch_info);
Ben Peartfa655d82018-08-16 18:27:11 +00001181 return ret;
1182 }
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +07001183 }
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001184
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001185 if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)
1186 orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit);
Daniel Cordero77ebd562009-05-16 10:54:45 -07001187
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001188 update_refs_for_switch(opts, &old_branch_info, new_branch_info);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001189
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001190 ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001191 branch_info_release(&old_branch_info);
1192
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +07001193 return ret || writeout_error;
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001194}
1195
Glen Chooa4e7e312023-06-28 19:26:22 +00001196static int git_checkout_config(const char *var, const char *value,
1197 const struct config_context *ctx, void *cb)
Junio C Hamano0cf85812008-08-30 07:52:24 -07001198{
Denton Liu64f1f582020-10-07 22:48:15 -07001199 struct checkout_opts *opts = cb;
1200
Johannes Schindelin175f6e52010-08-25 12:34:57 +02001201 if (!strcmp(var, "diff.ignoresubmodules")) {
Jeff Kingba176db2023-12-07 02:11:14 -05001202 if (!value)
1203 return config_error_nonbool(var);
Johannes Schindelin175f6e52010-08-25 12:34:57 +02001204 handle_ignore_submodules_arg(&opts->diff_options, value);
1205 return 0;
1206 }
Denton Liu64f1f582020-10-07 22:48:15 -07001207 if (!strcmp(var, "checkout.guess")) {
1208 opts->dwim_new_local_branch = git_config_bool(var, value);
1209 return 0;
1210 }
Jens Lehmann23b4c7b2010-08-28 16:50:07 +02001211
Christian Couder59556542013-11-30 21:55:40 +01001212 if (starts_with(var, "submodule."))
Brandon Williams7463e2e2017-08-03 11:19:53 -07001213 return git_default_submodule_config(var, value, NULL);
Jens Lehmann23b4c7b2010-08-28 16:50:07 +02001214
Glen Chooa4e7e312023-06-28 19:26:22 +00001215 return git_xmerge_config(var, value, ctx, NULL);
Junio C Hamano0cf85812008-08-30 07:52:24 -07001216}
1217
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001218static void setup_new_branch_info_and_source_tree(
1219 struct branch_info *new_branch_info,
1220 struct checkout_opts *opts,
1221 struct object_id *rev,
1222 const char *arg)
1223{
1224 struct tree **source_tree = &opts->source_tree;
1225 struct object_id branch_rev;
1226
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001227 new_branch_info->name = xstrdup(arg);
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001228 setup_branch_path(new_branch_info);
1229
1230 if (!check_refname_format(new_branch_info->path, 0) &&
1231 !read_ref(new_branch_info->path, &branch_rev))
1232 oidcpy(rev, &branch_rev);
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001233 else
1234 /* not an existing branch */
1235 FREE_AND_NULL(new_branch_info->path);
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001236
1237 new_branch_info->commit = lookup_commit_reference_gently(the_repository, rev, 1);
1238 if (!new_branch_info->commit) {
1239 /* not a commit */
1240 *source_tree = parse_tree_indirect(rev);
1241 } else {
1242 parse_commit_or_die(new_branch_info->commit);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001243 *source_tree = repo_get_commit_tree(the_repository,
1244 new_branch_info->commit);
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001245 }
1246}
1247
Alexandr Miloslavskiy29577092019-12-30 18:38:12 +00001248static const char *parse_remote_branch(const char *arg,
1249 struct object_id *rev,
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001250 int could_be_checkout_paths)
Alexandr Miloslavskiy29577092019-12-30 18:38:12 +00001251{
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001252 int num_matches = 0;
1253 const char *remote = unique_tracking_name(arg, rev, &num_matches);
Alexandr Miloslavskiy29577092019-12-30 18:38:12 +00001254
1255 if (remote && could_be_checkout_paths) {
1256 die(_("'%s' could be both a local file and a tracking branch.\n"
1257 "Please use -- (and optionally --no-guess) to disambiguate"),
1258 arg);
1259 }
1260
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001261 if (!remote && num_matches > 1) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001262 if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) {
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001263 advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1264 "you can do so by fully qualifying the name with the --track option:\n"
1265 "\n"
1266 " git checkout --track origin/<name>\n"
1267 "\n"
1268 "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1269 "one remote, e.g. the 'origin' remote, consider setting\n"
1270 "checkout.defaultRemote=origin in your config."));
1271 }
1272
1273 die(_("'%s' matched multiple (%d) remote tracking branches"),
1274 arg, num_matches);
1275 }
1276
Alexandr Miloslavskiy29577092019-12-30 18:38:12 +00001277 return remote;
1278}
1279
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001280static int parse_branchname_arg(int argc, const char **argv,
1281 int dwim_new_local_branch_ok,
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001282 struct branch_info *new_branch_info,
Nguyễn Thái Ngọc Duy10f102b2015-01-03 16:41:25 +07001283 struct checkout_opts *opts,
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001284 struct object_id *rev)
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001285{
Nguyễn Thái Ngọc Duy10f102b2015-01-03 16:41:25 +07001286 const char **new_branch = &opts->new_branch;
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001287 int argcount = 0;
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001288 const char *arg;
Matthieu Moybca39692013-10-18 11:25:58 +02001289 int dash_dash_pos;
1290 int has_dash_dash = 0;
1291 int i;
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001292
1293 /*
1294 * case 1: git checkout <ref> -- [<paths>]
1295 *
1296 * <ref> must be a valid tree, everything after the '--' must be
1297 * a path.
1298 *
1299 * case 2: git checkout -- [<paths>]
1300 *
1301 * everything after the '--' must be paths.
1302 *
Matthieu Moya047faf2013-10-18 11:25:57 +02001303 * case 3: git checkout <something> [--]
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001304 *
Matthieu Moya047faf2013-10-18 11:25:57 +02001305 * (a) If <something> is a commit, that is to
1306 * switch to the branch or detach HEAD at it. As a special case,
1307 * if <something> is A...B (missing A or B means HEAD but you can
1308 * omit at most one side), and if there is a unique merge base
1309 * between A and B, A...B names that merge base.
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001310 *
Matthieu Moya047faf2013-10-18 11:25:57 +02001311 * (b) If <something> is _not_ a commit, either "--" is present
Andrei Rybakb39a8412023-01-07 14:56:55 +01001312 * or <something> is not a path, no -t or -b was given,
Matthieu Moya047faf2013-10-18 11:25:57 +02001313 * and there is a tracking branch whose name is <something>
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +00001314 * in one and only one remote (or if the branch exists on the
1315 * remote named in checkout.defaultRemote), then this is a
1316 * short-hand to fork local <something> from that
1317 * remote-tracking branch.
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001318 *
Matthieu Moya047faf2013-10-18 11:25:57 +02001319 * (c) Otherwise, if "--" is present, treat it like case (1).
1320 *
1321 * (d) Otherwise :
1322 * - if it's a reference, treat it like case (1)
1323 * - else if it's a path, treat it like case (2)
1324 * - else: fail.
1325 *
1326 * case 4: git checkout <something> <paths>
1327 *
1328 * The first argument must not be ambiguous.
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001329 * - If it's *only* a reference, treat it like case (1).
1330 * - If it's only a path, treat it like case (2).
1331 * - else: fail.
1332 *
1333 */
1334 if (!argc)
1335 return 0;
1336
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001337 if (!opts->accept_pathspec) {
1338 if (argc > 1)
1339 die(_("only one reference expected"));
1340 has_dash_dash = 1; /* helps disambiguate */
1341 }
1342
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001343 arg = argv[0];
Matthieu Moybca39692013-10-18 11:25:58 +02001344 dash_dash_pos = -1;
1345 for (i = 0; i < argc; i++) {
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001346 if (opts->accept_pathspec && !strcmp(argv[i], "--")) {
Matthieu Moybca39692013-10-18 11:25:58 +02001347 dash_dash_pos = i;
1348 break;
1349 }
1350 }
1351 if (dash_dash_pos == 0)
1352 return 1; /* case (2) */
1353 else if (dash_dash_pos == 1)
1354 has_dash_dash = 1; /* case (3) or (1) */
1355 else if (dash_dash_pos >= 2)
1356 die(_("only one reference expected, %d given."), dash_dash_pos);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +01001357 opts->count_checkout_paths = !opts->quiet && !has_dash_dash;
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001358
1359 if (!strcmp(arg, "-"))
1360 arg = "@{-1}";
1361
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001362 if (repo_get_oid_mb(the_repository, arg, rev)) {
Matthieu Moya047faf2013-10-18 11:25:57 +02001363 /*
1364 * Either case (3) or (4), with <something> not being
1365 * a commit, or an attempt to use case (1) with an
1366 * invalid ref.
1367 *
1368 * It's likely an error, but we need to find out if
1369 * we should auto-create the branch, case (3).(b).
1370 */
1371 int recover_with_dwim = dwim_new_local_branch_ok;
1372
Nguyễn Thái Ngọc Duybe4908f2018-11-13 18:52:26 +01001373 int could_be_checkout_paths = !has_dash_dash &&
1374 check_filename(opts->prefix, arg);
1375
1376 if (!has_dash_dash && !no_wildcard(arg))
Matthieu Moya047faf2013-10-18 11:25:57 +02001377 recover_with_dwim = 0;
Nguyễn Thái Ngọc Duybe4908f2018-11-13 18:52:26 +01001378
Matthieu Moya047faf2013-10-18 11:25:57 +02001379 /*
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001380 * Accept "git checkout foo", "git checkout foo --"
1381 * and "git switch foo" as candidates for dwim.
Matthieu Moya047faf2013-10-18 11:25:57 +02001382 */
1383 if (!(argc == 1 && !has_dash_dash) &&
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001384 !(argc == 2 && has_dash_dash) &&
1385 opts->accept_pathspec)
Matthieu Moya047faf2013-10-18 11:25:57 +02001386 recover_with_dwim = 0;
1387
1388 if (recover_with_dwim) {
Alexandr Miloslavskiy29577092019-12-30 18:38:12 +00001389 const char *remote = parse_remote_branch(arg, rev,
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001390 could_be_checkout_paths);
Matthieu Moya047faf2013-10-18 11:25:57 +02001391 if (remote) {
1392 *new_branch = arg;
1393 arg = remote;
1394 /* DWIMmed to create local branch, case (3).(b) */
1395 } else {
1396 recover_with_dwim = 0;
1397 }
1398 }
1399
1400 if (!recover_with_dwim) {
1401 if (has_dash_dash)
1402 die(_("invalid reference: %s"), arg);
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001403 return argcount;
1404 }
1405 }
1406
1407 /* we can't end up being in (2) anymore, eat the argument */
1408 argcount++;
1409 argv++;
1410 argc--;
1411
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001412 setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001413
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001414 if (!opts->source_tree) /* case (1): want a tree */
Junio C Hamano6c80cd22011-04-01 17:55:55 -07001415 die(_("reference is not a tree: %s"), arg);
Nguyễn Thái Ngọc Duy7ab4ad02019-03-29 17:39:01 +07001416
Nguyễn Thái Ngọc Duy7f82b242016-09-07 18:19:39 +07001417 if (!has_dash_dash) { /* case (3).(d) -> (1) */
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001418 /*
1419 * Do not complain the most common case
1420 * git checkout branch
1421 * even if there happen to be a file called 'branch';
1422 * it would be extremely annoying.
1423 */
1424 if (argc)
Nguyễn Thái Ngọc Duyb829b942016-09-07 18:19:41 +07001425 verify_non_filename(opts->prefix, arg);
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001426 } else if (opts->accept_pathspec) {
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001427 argcount++;
1428 argv++;
1429 argc--;
1430 }
1431
1432 return argcount;
1433}
1434
Nguyễn Thái Ngọc Duya2b49942012-08-29 20:55:22 +07001435static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
Junio C Hamanoabe19982012-01-30 12:10:08 -08001436{
1437 int status;
1438 struct strbuf branch_ref = STRBUF_INIT;
1439
Jeff Hostetlere27dd8a2019-02-22 14:25:08 -08001440 trace2_cmd_mode("unborn");
1441
Chris Webb8ced1aa2012-06-26 16:06:42 +01001442 if (!opts->new_branch)
1443 die(_("You are on a branch yet to be born"));
Junio C Hamanoabe19982012-01-30 12:10:08 -08001444 strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
1445 status = create_symref("HEAD", branch_ref.buf, "checkout -b");
1446 strbuf_release(&branch_ref);
Jeff Kingafa8c072012-11-15 09:12:33 -08001447 if (!opts->quiet)
1448 fprintf(stderr, _("Switched to a new branch '%s'\n"),
1449 opts->new_branch);
Junio C Hamanoabe19982012-01-30 12:10:08 -08001450 return status;
1451}
1452
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001453static void die_expecting_a_branch(const struct branch_info *branch_info)
1454{
1455 struct object_id oid;
1456 char *to_free;
Alex Henrie808213b2022-02-25 23:12:13 -07001457 int code;
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001458
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +02001459 if (repo_dwim_ref(the_repository, branch_info->name,
1460 strlen(branch_info->name), &oid, &to_free, 0) == 1) {
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001461 const char *ref = to_free;
1462
1463 if (skip_prefix(ref, "refs/tags/", &ref))
Alex Henrie808213b2022-02-25 23:12:13 -07001464 code = die_message(_("a branch is expected, got tag '%s'"), ref);
1465 else if (skip_prefix(ref, "refs/remotes/", &ref))
1466 code = die_message(_("a branch is expected, got remote branch '%s'"), ref);
1467 else
1468 code = die_message(_("a branch is expected, got '%s'"), ref);
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001469 }
Alex Henrie808213b2022-02-25 23:12:13 -07001470 else if (branch_info->commit)
1471 code = die_message(_("a branch is expected, got commit '%s'"), branch_info->name);
1472 else
1473 /*
1474 * This case should never happen because we already die() on
1475 * non-commit, but just in case.
1476 */
1477 code = die_message(_("a branch is expected, got '%s'"), branch_info->name);
1478
1479 if (advice_enabled(ADVICE_SUGGEST_DETACHING_HEAD))
1480 advise(_("If you want to detach HEAD at the commit, try again with the --detach option."));
1481
1482 exit(code);
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001483}
1484
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +07001485static void die_if_some_operation_in_progress(void)
1486{
1487 struct wt_status_state state;
1488
1489 memset(&state, 0, sizeof(state));
1490 wt_status_get_state(the_repository, &state, 0);
1491
1492 if (state.merge_in_progress)
1493 die(_("cannot switch branch while merging\n"
1494 "Consider \"git merge --quit\" "
1495 "or \"git worktree add\"."));
1496 if (state.am_in_progress)
1497 die(_("cannot switch branch in the middle of an am session\n"
1498 "Consider \"git am --quit\" "
1499 "or \"git worktree add\"."));
1500 if (state.rebase_interactive_in_progress || state.rebase_in_progress)
1501 die(_("cannot switch branch while rebasing\n"
1502 "Consider \"git rebase --quit\" "
1503 "or \"git worktree add\"."));
1504 if (state.cherry_pick_in_progress)
1505 die(_("cannot switch branch while cherry-picking\n"
1506 "Consider \"git cherry-pick --quit\" "
1507 "or \"git worktree add\"."));
1508 if (state.revert_in_progress)
1509 die(_("cannot switch branch while reverting\n"
1510 "Consider \"git revert --quit\" "
1511 "or \"git worktree add\"."));
1512 if (state.bisect_in_progress)
Nguyễn Thái Ngọc Duyd16dc422019-06-20 16:55:21 +07001513 warning(_("you are switching branch while bisecting"));
Ævar Arnfjörð Bjarmasonb6046ab2022-11-08 19:17:42 +01001514
1515 wt_status_state_free_buffers(&state);
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +07001516}
1517
Junio C Hamano9263c402023-11-23 14:11:41 +09001518/*
1519 * die if attempting to checkout an existing branch that is in use
1520 * in another worktree, unless ignore-other-wortrees option is given.
1521 * The check is bypassed when the branch is already the current one,
1522 * as it will not make things any worse.
1523 */
1524static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts,
1525 const char *full_ref)
1526{
1527 int flags;
1528 char *head_ref;
1529
1530 if (opts->ignore_other_worktrees)
1531 return;
1532 head_ref = resolve_refdup("HEAD", 0, NULL, &flags);
1533 if (head_ref && (!(flags & REF_ISSYMREF) || strcmp(head_ref, full_ref)))
1534 die_if_checked_out(full_ref, 1);
1535 free(head_ref);
1536}
1537
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001538static int checkout_branch(struct checkout_opts *opts,
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001539 struct branch_info *new_branch_info)
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001540{
Nguyễn Thái Ngọc Duy817b3452013-07-14 15:35:41 +07001541 if (opts->pathspec.nr)
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001542 die(_("paths cannot be used with switching branches"));
1543
1544 if (opts->patch_mode)
1545 die(_("'%s' cannot be used with switching branches"),
1546 "--patch");
1547
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07001548 if (opts->overlay_mode != -1)
Thomas Gummerer091e04b2019-01-08 21:52:24 +00001549 die(_("'%s' cannot be used with switching branches"),
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07001550 "--[no]-overlay");
Thomas Gummerer091e04b2019-01-08 21:52:24 +00001551
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001552 if (opts->writeout_stage)
1553 die(_("'%s' cannot be used with switching branches"),
1554 "--ours/--theirs");
1555
1556 if (opts->force && opts->merge)
1557 die(_("'%s' cannot be used with '%s'"), "-f", "-m");
1558
Nguyễn Thái Ngọc Duy3ec37ad2019-03-29 17:39:07 +07001559 if (opts->discard_changes && opts->merge)
1560 die(_("'%s' cannot be used with '%s'"), "--discard-changes", "--merge");
1561
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001562 if (opts->force_detach && opts->new_branch)
1563 die(_("'%s' cannot be used with '%s'"),
1564 "--detach", "-b/-B/--orphan");
1565
1566 if (opts->new_orphan_branch) {
1567 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
1568 die(_("'%s' cannot be used with '%s'"), "--orphan", "-t");
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +07001569 if (opts->orphan_from_empty_tree && new_branch_info->name)
1570 die(_("'%s' cannot take <start-point>"), "--orphan");
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001571 } else if (opts->force_detach) {
1572 if (opts->track != BRANCH_TRACK_UNSPECIFIED)
1573 die(_("'%s' cannot be used with '%s'"), "--detach", "-t");
1574 } else if (opts->track == BRANCH_TRACK_UNSPECIFIED)
1575 opts->track = git_branch_track;
1576
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001577 if (new_branch_info->name && !new_branch_info->commit)
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001578 die(_("Cannot switch branch to a non-commit '%s'"),
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001579 new_branch_info->name);
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001580
Nguyễn Thái Ngọc Duye342c632019-03-29 17:39:10 +07001581 if (!opts->switch_branch_doing_nothing_is_ok &&
1582 !new_branch_info->name &&
1583 !opts->new_branch &&
1584 !opts->force_detach)
1585 die(_("missing branch or commit argument"));
1586
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001587 if (!opts->implicit_detach &&
1588 !opts->force_detach &&
1589 !opts->new_branch &&
1590 !opts->new_branch_force &&
1591 new_branch_info->name &&
1592 !new_branch_info->path)
1593 die_expecting_a_branch(new_branch_info);
1594
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +07001595 if (!opts->can_switch_when_in_progress)
1596 die_if_some_operation_in_progress();
1597
Junio C Hamano9263c402023-11-23 14:11:41 +09001598 /* "git checkout <branch>" */
1599 if (new_branch_info->path && !opts->force_detach && !opts->new_branch)
1600 die_if_switching_to_a_branch_in_use(opts, new_branch_info->path);
Nguyễn Thái Ngọc Duye1c1ab92015-06-12 17:49:24 +07001601
Junio C Hamanob23285a2023-11-23 15:00:31 +09001602 /* "git checkout -B <branch>" */
1603 if (opts->new_branch_force) {
1604 char *full_ref = xstrfmt("refs/heads/%s", opts->new_branch);
1605 die_if_switching_to_a_branch_in_use(opts, full_ref);
1606 free(full_ref);
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001607 }
1608
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001609 if (!new_branch_info->commit && opts->new_branch) {
brian m. carlson60af7692016-09-05 20:08:00 +00001610 struct object_id rev;
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001611 int flag;
1612
brian m. carlson34c290a2017-10-15 22:06:56 +00001613 if (!read_ref_full("HEAD", 0, &rev, &flag) &&
brian m. carlson60af7692016-09-05 20:08:00 +00001614 (flag & REF_ISSYMREF) && is_null_oid(&rev))
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001615 return switch_unborn_to_new_branch(opts);
1616 }
Brandon Williamsc8a3ea12018-02-14 10:59:31 -08001617 return switch_branches(opts, new_branch_info);
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001618}
1619
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001620static struct option *add_common_options(struct checkout_opts *opts,
1621 struct option *prevopts)
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001622{
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001623 struct option options[] = {
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001624 OPT__QUIET(&opts->quiet, N_("suppress progress reporting")),
Denton Liu203c8532020-04-28 04:36:28 -04001625 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
Stefan Beller1fc458d2017-03-14 14:46:41 -07001626 "checkout", "control recursive updating of submodules",
Denton Liu203c8532020-04-28 04:36:28 -04001627 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001628 OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")),
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001629 OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")),
1630 OPT_STRING(0, "conflict", &opts->conflict_style, N_("style"),
Elijah Newrenddfc44a82021-12-01 00:05:07 +00001631 N_("conflict style (merge, diff3, or zdiff3)")),
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001632 OPT_END()
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001633 };
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001634 struct option *newopts = parse_options_concat(prevopts, options);
1635 free(prevopts);
1636 return newopts;
1637}
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001638
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001639static struct option *add_common_switch_branch_options(
1640 struct checkout_opts *opts, struct option *prevopts)
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001641{
1642 struct option options[] = {
Nguyễn Thái Ngọc Duy163e3b22019-03-29 17:39:12 +07001643 OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")),
René Scharfe6327f0e2022-01-20 13:35:54 +01001644 OPT_CALLBACK_F('t', "track", &opts->track, "(direct|inherit)",
Josh Steadmon15f00282022-01-18 12:49:46 -08001645 N_("set branch tracking configuration"),
1646 PARSE_OPT_OPTARG,
Josh Steadmond3115662021-12-20 19:30:23 -08001647 parse_opt_tracking_mode),
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +07001648 OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
1649 PARSE_OPT_NOCOMPLETE),
Junio C Hamanod44b5172023-11-24 12:10:42 +09001650 OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unborn branch")),
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001651 OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore,
1652 N_("update ignored files (default)"),
1653 PARSE_OPT_NOCOMPLETE),
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001654 OPT_BOOL(0, "ignore-other-worktrees", &opts->ignore_other_worktrees,
1655 N_("do not check if another worktree is holding the given ref")),
1656 OPT_END()
1657 };
1658 struct option *newopts = parse_options_concat(prevopts, options);
1659 free(prevopts);
1660 return newopts;
1661}
1662
1663static struct option *add_checkout_path_options(struct checkout_opts *opts,
1664 struct option *prevopts)
1665{
1666 struct option options[] = {
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001667 OPT_SET_INT_F('2', "ours", &opts->writeout_stage,
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001668 N_("checkout our version for unmerged files"),
1669 2, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001670 OPT_SET_INT_F('3', "theirs", &opts->writeout_stage,
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001671 N_("checkout their version for unmerged files"),
1672 3, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001673 OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")),
1674 OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree,
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001675 N_("do not limit pathspecs to sparse entries only")),
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001676 OPT_PATHSPEC_FROM_FILE(&opts->pathspec_from_file),
1677 OPT_PATHSPEC_FILE_NUL(&opts->pathspec_file_nul),
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001678 OPT_END()
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001679 };
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001680 struct option *newopts = parse_options_concat(prevopts, options);
1681 free(prevopts);
1682 return newopts;
1683}
1684
Denton Liu7c16ef72020-04-30 07:54:57 -04001685/* create-branch option (either b or c) */
1686static char cb_option = 'b';
1687
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001688static int checkout_main(int argc, const char **argv, const char *prefix,
1689 struct checkout_opts *opts, struct option *options,
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001690 const char * const usagestr[],
1691 struct branch_info *new_branch_info)
Nguyễn Thái Ngọc Duy20871822019-03-29 17:39:04 +07001692{
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001693 int parseopt_flags = 0;
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001694
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001695 opts->overwrite_ignore = 1;
1696 opts->prefix = prefix;
1697 opts->show_progress = -1;
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001698
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001699 git_config(git_checkout_config, opts);
Johannes Schindelin059fda12022-02-08 11:21:53 +00001700 if (the_repository->gitdir) {
1701 prepare_repo_settings(the_repository);
1702 the_repository->settings.command_requires_full_index = 0;
1703 }
Derrick Stolee1ba5f452021-06-29 02:13:06 +00001704
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001705 opts->track = BRANCH_TRACK_UNSPECIFIED;
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001706
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001707 if (!opts->accept_pathspec && !opts->accept_ref)
1708 BUG("make up your mind, you need to take _something_");
1709 if (opts->accept_pathspec && opts->accept_ref)
1710 parseopt_flags = PARSE_OPT_KEEP_DASHDASH;
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001711
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001712 argc = parse_options(argc, argv, prefix, options,
1713 usagestr, parseopt_flags);
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001714
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001715 if (opts->show_progress < 0) {
1716 if (opts->quiet)
1717 opts->show_progress = 0;
Edmundo Carmona Antoranz870ebdb2015-11-01 15:19:05 -06001718 else
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001719 opts->show_progress = isatty(2);
Edmundo Carmona Antoranz870ebdb2015-11-01 15:19:05 -06001720 }
1721
Nguyễn Thái Ngọc Duy55cf7042019-03-29 17:39:03 +07001722 if (opts->conflict_style) {
Glen Chooa4e7e312023-06-28 19:26:22 +00001723 struct key_value_info kvi = KVI_INIT;
1724 struct config_context ctx = {
1725 .kvi = &kvi,
1726 };
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001727 opts->merge = 1; /* implied */
Glen Chooa4e7e312023-06-28 19:26:22 +00001728 git_xmerge_config("merge.conflictstyle", opts->conflict_style,
1729 &ctx, NULL);
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001730 }
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +07001731 if (opts->force) {
Nguyễn Thái Ngọc Duy3ec37ad2019-03-29 17:39:07 +07001732 opts->discard_changes = 1;
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +07001733 opts->ignore_unmerged_opt = "--force";
1734 opts->ignore_unmerged = 1;
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +00001735 }
Tay Ray Chuan02ac9832010-06-24 03:29:00 +08001736
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001737 if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001738 die(_("options '-%c', '-%c', and '%s' cannot be used together"),
1739 cb_option, toupper(cb_option), "--orphan");
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001740
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001741 if (opts->overlay_mode == 1 && opts->patch_mode)
Jean-Noël Avila43ea6352022-01-05 20:02:14 +00001742 die(_("options '%s' and '%s' cannot be used together"), "-p", "--overlay");
Thomas Gummerer091e04b2019-01-08 21:52:24 +00001743
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +07001744 if (opts->checkout_index >= 0 || opts->checkout_worktree >= 0) {
1745 if (opts->checkout_index < 0)
1746 opts->checkout_index = 0;
1747 if (opts->checkout_worktree < 0)
1748 opts->checkout_worktree = 0;
1749 } else {
1750 if (opts->checkout_index < 0)
1751 opts->checkout_index = -opts->checkout_index - 1;
1752 if (opts->checkout_worktree < 0)
1753 opts->checkout_worktree = -opts->checkout_worktree - 1;
1754 }
1755 if (opts->checkout_index < 0 || opts->checkout_worktree < 0)
1756 BUG("these flags should be non-negative by now");
Nguyễn Thái Ngọc Duy3a733ce2019-04-25 16:45:52 +07001757 /*
Eric Sunshine088018e2020-05-05 03:17:16 -04001758 * convenient shortcut: "git restore --staged [--worktree]" equals
1759 * "git restore --staged [--worktree] --source HEAD"
Nguyễn Thái Ngọc Duy3a733ce2019-04-25 16:45:52 +07001760 */
Eric Sunshine088018e2020-05-05 03:17:16 -04001761 if (!opts->from_treeish && opts->checkout_index)
Nguyễn Thái Ngọc Duy3a733ce2019-04-25 16:45:52 +07001762 opts->from_treeish = "HEAD";
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +07001763
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001764 /*
1765 * From here on, new_branch will contain the branch to be checked out,
1766 * and new_branch_force and new_orphan_branch will tell us which one of
Denton Liu7c16ef72020-04-30 07:54:57 -04001767 * -b/-B/-c/-C/--orphan is being used.
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001768 */
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001769 if (opts->new_branch_force)
1770 opts->new_branch = opts->new_branch_force;
Tay Ray Chuan02ac9832010-06-24 03:29:00 +08001771
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001772 if (opts->new_orphan_branch)
1773 opts->new_branch = opts->new_orphan_branch;
Thomas Rast4f353652009-08-15 13:48:30 +02001774
Denton Liu7c16ef72020-04-30 07:54:57 -04001775 /* --track without -c/-C/-b/-B/--orphan should DWIM */
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001776 if (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {
Alex Riesen9188ed82008-08-21 19:23:20 +02001777 const char *argv0 = argv[0];
1778 if (!argc || !strcmp(argv0, "--"))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001779 die(_("--track needs a branch name"));
René Scharfee3f1da92014-10-04 20:54:50 +02001780 skip_prefix(argv0, "refs/", &argv0);
1781 skip_prefix(argv0, "remotes/", &argv0);
Alex Riesen9188ed82008-08-21 19:23:20 +02001782 argv0 = strchr(argv0, '/');
1783 if (!argv0 || !argv0[1])
Denton Liu7c16ef72020-04-30 07:54:57 -04001784 die(_("missing branch name; try -%c"), cb_option);
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001785 opts->new_branch = argv0 + 1;
Johannes Schindelinbb0ceb62008-08-09 16:00:12 +02001786 }
1787
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001788 /*
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001789 * Extract branch name from command line arguments, so
1790 * all that is left is pathspecs.
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001791 *
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001792 * Handle
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001793 *
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001794 * 1) git checkout <tree> -- [<paths>]
1795 * 2) git checkout -- [<paths>]
1796 * 3) git checkout <something> [<paths>]
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001797 *
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001798 * including "last branch" syntax and DWIM-ery for names of
1799 * remote branches, erroring out for invalid or ambiguous cases.
Pierre Habouzit859fdab2008-07-23 12:15:33 +02001800 */
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001801 if (argc && opts->accept_ref) {
brian m. carlson60af7692016-09-05 20:08:00 +00001802 struct object_id rev;
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001803 int dwim_ok =
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001804 !opts->patch_mode &&
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +07001805 opts->dwim_new_local_branch &&
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001806 opts->track == BRANCH_TRACK_UNSPECIFIED &&
1807 !opts->new_branch;
Jonathan Niederf8bd36a2011-02-08 04:34:34 -06001808 int n = parse_branchname_arg(argc, argv, dwim_ok,
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001809 new_branch_info, opts, &rev);
Jonathan Nieder09ebad62011-02-08 04:29:09 -06001810 argv += n;
1811 argc -= n;
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001812 } else if (!opts->accept_ref && opts->from_treeish) {
1813 struct object_id rev;
1814
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001815 if (repo_get_oid_mb(the_repository, opts->from_treeish, &rev))
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001816 die(_("could not resolve %s"), opts->from_treeish);
1817
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001818 setup_new_branch_info_and_source_tree(new_branch_info,
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001819 opts, &rev,
1820 opts->from_treeish);
1821
1822 if (!opts->source_tree)
1823 die(_("reference is not a tree: %s"), opts->from_treeish);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001824 }
1825
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001826 if (argc) {
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001827 parse_pathspec(&opts->pathspec, 0,
1828 opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
Nguyễn Thái Ngọc Duy480ca642013-07-14 15:35:50 +07001829 prefix, argv);
Alex Riesen301e42e2008-02-28 17:30:47 +01001830
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001831 if (!opts->pathspec.nr)
Ævar Arnfjörð Bjarmasone8a8a4d2011-02-22 23:41:39 +00001832 die(_("invalid path specification"));
Alex Riesen301e42e2008-02-28 17:30:47 +01001833
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001834 /*
1835 * Try to give more helpful suggestion.
1836 * new_branch && argc > 1 will be caught later.
1837 */
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001838 if (opts->new_branch && argc == 1 && !new_branch_info->commit)
Jean-Noel Avila6c486862017-05-11 14:06:32 +02001839 die(_("'%s' is not a commit and a branch '%s' cannot be created from it"),
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001840 argv[0], opts->new_branch);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001841
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001842 if (opts->force_detach)
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001843 die(_("git checkout: --detach does not take a path argument '%s'"),
1844 argv[0]);
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001845 }
Junio C Hamano32669672011-02-08 04:32:49 -06001846
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001847 if (opts->pathspec_from_file) {
1848 if (opts->pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +00001849 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001850
1851 if (opts->force_detach)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001852 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--detach");
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001853
1854 if (opts->patch_mode)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001855 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001856
1857 parse_pathspec_file(&opts->pathspec, 0,
1858 0,
1859 prefix, opts->pathspec_from_file, opts->pathspec_file_nul);
1860 } else if (opts->pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +00001861 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001862 }
1863
René Scharfebfda2042020-08-22 10:57:59 +02001864 opts->pathspec.recursive = 1;
1865
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001866 if (opts->pathspec.nr) {
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001867 if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
Nguyễn Thái Ngọc Duyb6312c22012-08-30 19:45:50 +07001868 die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
1869 "checking out of the index."));
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +00001870 } else {
1871 if (opts->accept_pathspec && !opts->empty_pathspec_ok &&
1872 !opts->patch_mode) /* patch mode is special */
1873 die(_("you must specify path(s) to restore"));
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001874 }
1875
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001876 if (opts->new_branch) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001877 struct strbuf buf = STRBUF_INIT;
Conrad Irwin55c4a672011-08-20 14:49:48 -07001878
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001879 if (opts->new_branch_force)
1880 opts->branch_exists = validate_branchname(opts->new_branch, &buf);
Junio C Hamanobc1c9c02017-10-13 13:45:40 +09001881 else
Nguyễn Thái Ngọc Duyb3edccb2019-03-29 17:39:02 +07001882 opts->branch_exists =
1883 validate_new_branchname(opts->new_branch, &buf, 0);
Daniel Barkalow352eadc2008-09-21 14:36:06 -04001884 strbuf_release(&buf);
1885 }
1886
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001887 if (opts->patch_mode || opts->pathspec.nr)
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001888 return checkout_paths(opts, new_branch_info);
Alexandr Miloslavskiyfa741802019-12-30 18:38:13 +00001889 else
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001890 return checkout_branch(opts, new_branch_info);
Daniel Barkalow782c2d62008-02-07 11:40:23 -05001891}
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001892
1893int cmd_checkout(int argc, const char **argv, const char *prefix)
1894{
1895 struct checkout_opts opts;
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001896 struct option *options;
1897 struct option checkout_options[] = {
1898 OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
1899 N_("create and checkout a new branch")),
1900 OPT_STRING('B', NULL, &opts.new_branch_force, N_("branch"),
1901 N_("create/reset and checkout a branch")),
Nguyễn Thái Ngọc Duy492eded2019-03-29 17:39:08 +07001902 OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +07001903 OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
1904 N_("second guess 'git checkout <no-such-branch>' (default)")),
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07001905 OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001906 OPT_END()
1907 };
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001908 int ret;
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001909 struct branch_info new_branch_info = { 0 };
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001910
1911 memset(&opts, 0, sizeof(opts));
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +07001912 opts.dwim_new_local_branch = 1;
Nguyễn Thái Ngọc Duye342c632019-03-29 17:39:10 +07001913 opts.switch_branch_doing_nothing_is_ok = 1;
Nguyễn Thái Ngọc Duy65f099b2019-03-29 17:39:14 +07001914 opts.only_merge_on_switching_branches = 0;
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001915 opts.accept_ref = 1;
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001916 opts.accept_pathspec = 1;
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001917 opts.implicit_detach = 1;
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +07001918 opts.can_switch_when_in_progress = 1;
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +07001919 opts.orphan_from_empty_tree = 0;
Nguyễn Thái Ngọc Duybe8ed502019-04-25 16:45:47 +07001920 opts.empty_pathspec_ok = 1;
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07001921 opts.overlay_mode = -1;
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +07001922 opts.checkout_index = -2; /* default on */
1923 opts.checkout_worktree = -2; /* default on */
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001924
Derrick Stolee31367762019-08-29 10:01:32 -07001925 if (argc == 3 && !strcmp(argv[1], "-b")) {
1926 /*
1927 * User ran 'git checkout -b <branch>' and expects
1928 * the same behavior as 'git switch -c <branch>'.
1929 */
1930 opts.switch_branch_doing_nothing_is_ok = 0;
1931 opts.only_merge_on_switching_branches = 1;
1932 }
1933
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001934 options = parse_options_dup(checkout_options);
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001935 options = add_common_options(&opts, options);
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001936 options = add_common_switch_branch_options(&opts, options);
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001937 options = add_checkout_path_options(&opts, options);
1938
1939 ret = checkout_main(argc, argv, prefix, &opts,
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001940 options, checkout_usage, &new_branch_info);
1941 branch_info_release(&new_branch_info);
1942 clear_pathspec(&opts.pathspec);
Jeff King7ce40882023-03-04 05:31:22 -05001943 free(opts.pathspec_from_file);
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001944 FREE_AND_NULL(options);
1945 return ret;
1946}
1947
1948int cmd_switch(int argc, const char **argv, const char *prefix)
1949{
1950 struct checkout_opts opts;
1951 struct option *options = NULL;
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001952 struct option switch_options[] = {
1953 OPT_STRING('c', "create", &opts.new_branch, N_("branch"),
1954 N_("create and switch to a new branch")),
1955 OPT_STRING('C', "force-create", &opts.new_branch_force, N_("branch"),
1956 N_("create/reset and switch to a branch")),
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +07001957 OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
1958 N_("second guess 'git switch <no-such-branch>'")),
Nguyễn Thái Ngọc Duy3ec37ad2019-03-29 17:39:07 +07001959 OPT_BOOL(0, "discard-changes", &opts.discard_changes,
1960 N_("throw away local modifications")),
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001961 OPT_END()
1962 };
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001963 int ret;
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001964 struct branch_info new_branch_info = { 0 };
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001965
1966 memset(&opts, 0, sizeof(opts));
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +07001967 opts.dwim_new_local_branch = 1;
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001968 opts.accept_ref = 1;
Nguyễn Thái Ngọc Duy5c06e262019-03-29 17:39:09 +07001969 opts.accept_pathspec = 0;
Nguyễn Thái Ngọc Duye342c632019-03-29 17:39:10 +07001970 opts.switch_branch_doing_nothing_is_ok = 0;
Nguyễn Thái Ngọc Duy65f099b2019-03-29 17:39:14 +07001971 opts.only_merge_on_switching_branches = 1;
Nguyễn Thái Ngọc Duy7968bef2019-03-29 17:39:11 +07001972 opts.implicit_detach = 0;
Nguyễn Thái Ngọc Duyc45f0f52019-03-29 17:39:15 +07001973 opts.can_switch_when_in_progress = 0;
Nguyễn Thái Ngọc Duy1806c292019-03-29 17:39:16 +07001974 opts.orphan_from_empty_tree = 1;
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07001975 opts.overlay_mode = -1;
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001976
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001977 options = parse_options_dup(switch_options);
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001978 options = add_common_options(&opts, options);
Nguyễn Thái Ngọc Duyb7b5fce2019-03-29 17:39:06 +07001979 options = add_common_switch_branch_options(&opts, options);
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001980
Denton Liu7c16ef72020-04-30 07:54:57 -04001981 cb_option = 'c';
1982
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001983 ret = checkout_main(argc, argv, prefix, &opts,
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01001984 options, switch_branch_usage, &new_branch_info);
1985 branch_info_release(&new_branch_info);
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +07001986 FREE_AND_NULL(options);
1987 return ret;
1988}
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07001989
1990int cmd_restore(int argc, const char **argv, const char *prefix)
1991{
1992 struct checkout_opts opts;
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07001993 struct option *options;
1994 struct option restore_options[] = {
1995 OPT_STRING('s', "source", &opts.from_treeish, "<tree-ish>",
Jean-Noël Avila182f59d2019-08-06 19:19:52 +02001996 N_("which tree-ish to checkout from")),
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +07001997 OPT_BOOL('S', "staged", &opts.checkout_index,
1998 N_("restore the index")),
1999 OPT_BOOL('W', "worktree", &opts.checkout_worktree,
2000 N_("restore the working tree (default)")),
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +07002001 OPT_BOOL(0, "ignore-unmerged", &opts.ignore_unmerged,
2002 N_("ignore unmerged entries")),
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07002003 OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode")),
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07002004 OPT_END()
2005 };
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07002006 int ret;
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01002007 struct branch_info new_branch_info = { 0 };
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07002008
2009 memset(&opts, 0, sizeof(opts));
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07002010 opts.accept_ref = 0;
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07002011 opts.accept_pathspec = 1;
Nguyễn Thái Ngọc Duybe8ed502019-04-25 16:45:47 +07002012 opts.empty_pathspec_ok = 0;
Nguyễn Thái Ngọc Duya6cfb9b2019-04-25 16:45:48 +07002013 opts.overlay_mode = 0;
Nguyễn Thái Ngọc Duy183fb442019-04-25 16:45:50 +07002014 opts.checkout_index = -1; /* default off */
2015 opts.checkout_worktree = -2; /* default on */
Nguyễn Thái Ngọc Duya5e5f392019-04-25 16:45:53 +07002016 opts.ignore_unmerged_opt = "--ignore-unmerged";
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07002017
Nguyễn Thái Ngọc Duyc9c935f2019-04-25 16:45:46 +07002018 options = parse_options_dup(restore_options);
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07002019 options = add_common_options(&opts, options);
2020 options = add_checkout_path_options(&opts, options);
2021
2022 ret = checkout_main(argc, argv, prefix, &opts,
Ævar Arnfjörð Bjarmason9081a422021-11-16 19:27:38 +01002023 options, restore_usage, &new_branch_info);
2024 branch_info_release(&new_branch_info);
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +07002025 FREE_AND_NULL(options);
2026 return ret;
2027}