Skip to content

Commit

Permalink
fix(branch): disallow branch before subcommand
Browse files Browse the repository at this point in the history
The command line parsing for `stg branch` was such that specifying a
branch name before a subcommand was being allowed, **but** that early
branch name argument **was not** known or used by the subcommand; i.e.
any branch name argument before the subcommand was silently ignored.

This allowed certain valid command lines to lead to pathological
behavior. For example:

    stg branch foo --delete --force

would delete the current branch, not branch "foo".

The fix is to enable clap's args_conflicts_with_subcommands for `stg
branch`, which disallows non-subcommand arguments to precede  a
subcommand argument. This is consistent with the intended usage of
`stg branch`.

Fixes: #447
  • Loading branch information
jpgrayson committed May 5, 2024
1 parent d45886a commit 92c33b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cmd/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn make() -> clap::Command {
result in an error.",
)
.disable_help_subcommand(true)
.args_conflicts_with_subcommands(true)
.override_usage(super::make_usage(
"stg branch",
&[
Expand Down
2 changes: 1 addition & 1 deletion t/t1000-branch-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test_expect_success 'Attempt switching to current branch' '

test_expect_success 'Attempt no branch command' '
general_error stg branch foo bar 2>err &&
grep "unexpected argument .bar." err
grep "the subcommand .bar. cannot be used with ..branch.." err
'

test_expect_success 'Invalid num arguments to branch list' '
Expand Down
9 changes: 9 additions & 0 deletions t/t1005-branch-delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ test_expect_success 'Attempt to delete branch with patches' '
grep -e "delete not permitted: the series still contains patches" err
'

test_expect_success 'Delete subcommand ordering' '
general_error stg branch master --delete --force &&
stg branch >out &&
cat >expected <<-\EOF &&
foo
EOF
test_cmp expected out
'

test_expect_success 'Force delete branch with patches' '
stg branch --delete --force master
'
Expand Down

0 comments on commit 92c33b8

Please sign in to comment.