Skip to content

Commit e342c63

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent 5c06e26 commit e342c63

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

builtin/checkout.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct checkout_opts {
5555
int no_dwim_new_local_branch;
5656
int discard_changes;
5757
int accept_pathspec;
58+
int switch_branch_doing_nothing_is_ok;
5859

5960
/*
6061
* If new checkout options are added, skip_merge_working_tree
@@ -1338,6 +1339,12 @@ static int checkout_branch(struct checkout_opts *opts,
13381339
die(_("Cannot switch branch to a non-commit '%s'"),
13391340
new_branch_info->name);
13401341

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+
13411348
if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
13421349
!opts->ignore_other_worktrees) {
13431350
int flag;
@@ -1593,6 +1600,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
15931600

15941601
memset(&opts, 0, sizeof(opts));
15951602
opts.no_dwim_new_local_branch = 0;
1603+
opts.switch_branch_doing_nothing_is_ok = 1;
15961604
opts.accept_pathspec = 1;
15971605

15981606
options = parse_options_dup(checkout_options);
@@ -1624,6 +1632,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
16241632
memset(&opts, 0, sizeof(opts));
16251633
opts.no_dwim_new_local_branch = 0;
16261634
opts.accept_pathspec = 0;
1635+
opts.switch_branch_doing_nothing_is_ok = 0;
16271636

16281637
options = parse_options_dup(switch_options);
16291638
options = add_common_options(&opts, options);

0 commit comments

Comments
 (0)