switch: reject "do nothing" case

"git checkout" can be executed without any arguments. What it does is not exactly great: it switches from HEAD to HEAD and shows worktree modification as a side effect. Make switch reject this case. Just use "git status" if you want that side effect. For switch, 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 <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 e342c63a4e070bd1398b9aa86ac4ce917edbadc7
1 file changed +9
builtin/checkout.c
+9
@@ -55,6 +55,7 @@ struct checkout_opts {
55 int no_dwim_new_local_branch;
56 int discard_changes;
57 int accept_pathspec;
58 + int switch_branch_doing_nothing_is_ok;
59
60 /*
61 * If new checkout options are added, skip_merge_working_tree
@@ -1338,6 +1339,12 @@ static int checkout_branch(struct checkout_opts *opts,
1339 die(_("Cannot switch branch to a non-commit '%s'"),
1340 new_branch_info->name);
1341
1342 + if (!opts->switch_branch_doing_nothing_is_ok &&
1343 + !new_branch_info->name &&
1344 + !opts->new_branch &&
1345 + !opts->force_detach)
1346 + die(_("missing branch or commit argument"));
1347 +
1348 if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
1349 !opts->ignore_other_worktrees) {
1350 int flag;
@@ -1593,6 +1600,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1600
1601 memset(&opts, 0, sizeof(opts));
1602 opts.no_dwim_new_local_branch = 0;
1603 + opts.switch_branch_doing_nothing_is_ok = 1;
1604 opts.accept_pathspec = 1;
1605
1606 options = parse_options_dup(checkout_options);
@@ -1624,6 +1632,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
1632 memset(&opts, 0, sizeof(opts));
1633 opts.no_dwim_new_local_branch = 0;
1634 opts.accept_pathspec = 0;
1635 + opts.switch_branch_doing_nothing_is_ok = 0;
1636
1637 options = parse_options_dup(switch_options);
1638 options = add_common_options(&opts, options);