switch: only allow explicit detached HEAD
"git checkout <commit>" will checkout the commit in question and detach HEAD from the current branch. It is naturally a right thing to do once you get git references. But detached HEAD is a scary concept to new users because we show a lot of warnings and stuff, and it could be hard to get out of (until you know better). To keep switch a bit more friendly to new users, we only allow entering detached HEAD mode when --detach is given. "git switch" must take a branch (unless you create a new branch, then of course switch can take any commit-ish) Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Mar 29, 2019 at 17:39 UTC
7968bef06b750fe9c59c1e1d0573bffc5b3cd94f
1 file changed
+34
builtin/checkout.c
+34
@@ -45,6 +45,7 @@ struct checkout_opts {
45
int merge;
46
int force;
47
int force_detach;
48
+ int implicit_detach;
49
int writeout_stage;
50
int overwrite_ignore;
51
int ignore_skipworktree;
@@ -1298,6 +1299,29 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
1299
return status;
1300
}
1301
1302
+static void die_expecting_a_branch(const struct branch_info *branch_info)
1303
+{
1304
+ struct object_id oid;
1305
+ char *to_free;
1306
+
1307
+ if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free) == 1) {
1308
+ const char *ref = to_free;
1309
+
1310
+ if (skip_prefix(ref, "refs/tags/", &ref))
1311
+ die(_("a branch is expected, got tag '%s'"), ref);
1312
+ if (skip_prefix(ref, "refs/remotes/", &ref))
1313
+ die(_("a branch is expected, got remote branch '%s'"), ref);
1314
+ die(_("a branch is expected, got '%s'"), ref);
1315
+ }
1316
+ if (branch_info->commit)
1317
+ die(_("a branch is expected, got commit '%s'"), branch_info->name);
1318
+ /*
1319
+ * This case should never happen because we already die() on
1320
+ * non-commit, but just in case.
1321
+ */
1322
+ die(_("a branch is expected, got '%s'"), branch_info->name);
1323
+}
1324
+
1325
static int checkout_branch(struct checkout_opts *opts,
1326
struct branch_info *new_branch_info)
1327
{
@@ -1345,6 +1369,14 @@ static int checkout_branch(struct checkout_opts *opts,
1369
!opts->force_detach)
1370
die(_("missing branch or commit argument"));
1371
1372
+ if (!opts->implicit_detach &&
1373
+ !opts->force_detach &&
1374
+ !opts->new_branch &&
1375
+ !opts->new_branch_force &&
1376
+ new_branch_info->name &&
1377
+ !new_branch_info->path)
1378
+ die_expecting_a_branch(new_branch_info);
1379
+
1380
if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
1381
!opts->ignore_other_worktrees) {
1382
int flag;
@@ -1602,6 +1634,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1634
opts.no_dwim_new_local_branch = 0;
1635
opts.switch_branch_doing_nothing_is_ok = 1;
1636
opts.accept_pathspec = 1;
1637
+ opts.implicit_detach = 1;
1638
1639
options = parse_options_dup(checkout_options);
1640
options = add_common_options(&opts, options);
@@ -1633,6 +1666,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
1666
opts.no_dwim_new_local_branch = 0;
1667
opts.accept_pathspec = 0;
1668
opts.switch_branch_doing_nothing_is_ok = 0;
1669
+ opts.implicit_detach = 0;
1670
1671
options = parse_options_dup(switch_options);
1672
options = add_common_options(&opts, options);