"git checkout" can be executed without any arguments. What it does is
not exactly great: it switches from HEAD to HEAD and showing worktree
modification as a side effect.

Make switch-branch reject this case. You have to either

- really switch a branch
- (explicitly) detach from the current branch
- create a new branch

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 builtin/checkout.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 880030e929..c7ae068d2c 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -56,6 +56,7 @@ struct checkout_opts {
        int show_progress;
        int dwim_new_local_branch;
        int accept_pathspec;
+       int switch_branch_doing_nothing_not_ok;
 
        /*
         * If new checkout options are added, skip_merge_working_tree
@@ -1233,6 +1234,13 @@ static int checkout_branch(struct checkout_opts *opts,
                die(_("Cannot switch branch to a non-commit '%s'"),
                    new_branch_info->name);
 
+       if (opts->switch_branch_doing_nothing_not_ok &&
+           !new_branch_info->name &&
+           !opts->new_branch &&
+           !opts->new_branch_force &&
+           !opts->force_detach)
+               die(_("nothing to do"));
+
        if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
            !opts->ignore_other_worktrees) {
                int flag;
@@ -1475,6 +1483,7 @@ int cmd_checkout(int argc, const char **argv, const char 
*prefix)
 
        memset(&opts, 0, sizeof(opts));
        opts.dwim_new_local_branch = 1;
+       opts.switch_branch_doing_nothing_not_ok = 0;
        opts.accept_pathspec = 1;
 
        options = parse_options_dup(checkout_options);
@@ -1503,6 +1512,7 @@ int cmd_switch_branch(int argc, const char **argv, const 
char *prefix)
        memset(&opts, 0, sizeof(opts));
        opts.dwim_new_local_branch = 1;
        opts.accept_pathspec = 0;
+       opts.switch_branch_doing_nothing_not_ok = 1;
 
        options = parse_options_dup(switch_options);
        options = add_common_options(&opts, options);
-- 
2.20.0.rc1.380.g3eb999425c.dirty

Reply via email to