switch: only allow explicit detached HEAD
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Fri, 29 Mar 2019 10:39:11 +0000 (17:39 +0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 2 Apr 2019 04:57:00 +0000 (13:57 +0900)
"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>
builtin/checkout.c
index e9dfe38d69d46b11b271995a7d68cf18db4795a1..4416b82bfde3f629e227d098739fa1ff8ee261e5 100644 (file)
@@ -45,6 +45,7 @@ struct checkout_opts {
        int merge;
        int force;
        int force_detach;
+       int implicit_detach;
        int writeout_stage;
        int overwrite_ignore;
        int ignore_skipworktree;
@@ -1298,6 +1299,29 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
        return status;
 }
 
+static void die_expecting_a_branch(const struct branch_info *branch_info)
+{
+       struct object_id oid;
+       char *to_free;
+
+       if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free) == 1) {
+               const char *ref = to_free;
+
+               if (skip_prefix(ref, "refs/tags/", &ref))
+                       die(_("a branch is expected, got tag '%s'"), ref);
+               if (skip_prefix(ref, "refs/remotes/", &ref))
+                       die(_("a branch is expected, got remote branch '%s'"), ref);
+               die(_("a branch is expected, got '%s'"), ref);
+       }
+       if (branch_info->commit)
+               die(_("a branch is expected, got commit '%s'"), branch_info->name);
+       /*
+        * This case should never happen because we already die() on
+        * non-commit, but just in case.
+        */
+       die(_("a branch is expected, got '%s'"), branch_info->name);
+}
+
 static int checkout_branch(struct checkout_opts *opts,
                           struct branch_info *new_branch_info)
 {
@@ -1345,6 +1369,14 @@ static int checkout_branch(struct checkout_opts *opts,
            !opts->force_detach)
                die(_("missing branch or commit argument"));
 
+       if (!opts->implicit_detach &&
+           !opts->force_detach &&
+           !opts->new_branch &&
+           !opts->new_branch_force &&
+           new_branch_info->name &&
+           !new_branch_info->path)
+               die_expecting_a_branch(new_branch_info);
+
        if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
            !opts->ignore_other_worktrees) {
                int flag;
@@ -1602,6 +1634,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        opts.no_dwim_new_local_branch = 0;
        opts.switch_branch_doing_nothing_is_ok = 1;
        opts.accept_pathspec = 1;
+       opts.implicit_detach = 1;
 
        options = parse_options_dup(checkout_options);
        options = add_common_options(&opts, options);
@@ -1633,6 +1666,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
        opts.no_dwim_new_local_branch = 0;
        opts.accept_pathspec = 0;
        opts.switch_branch_doing_nothing_is_ok = 0;
+       opts.implicit_detach = 0;
 
        options = parse_options_dup(switch_options);
        options = add_common_options(&opts, options);