Skip to content

Commit 199970e

Browse files
pks-tgitster
authored andcommitted
builtin/show-ref: ensure mutual exclusiveness of subcommands
The git-show-ref(1) command has three different modes, of which one is implicit and the other two can be chosen explicitly by passing a flag. But while these modes are standalone and cause us to execute completely separate code paths, we gladly accept the case where a user asks for both `--exclude-existing` and `--verify` at the same time even though it is not obvious what will happen. Spoiler: we ignore `--verify` and execute the `--exclude-existing` mode. Let's explicitly detect this invalid usage and die in case both modes were requested. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ee26f1e commit 199970e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

builtin/show-ref.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
275275
argc = parse_options(argc, argv, prefix, show_ref_options,
276276
show_ref_usage, 0);
277277

278+
if ((!!exclude_existing_opts.enabled + !!verify) > 1)
279+
die(_("only one of '%s' or '%s' can be given"),
280+
"--exclude-existing", "--verify");
281+
278282
if (exclude_existing_opts.enabled)
279283
return cmd_show_ref__exclude_existing(&exclude_existing_opts);
280284
else if (verify)

t/t1403-show-ref.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,13 @@ test_expect_success 'show-ref --verify with dangling ref' '
196196
)
197197
'
198198

199+
test_expect_success 'show-ref sub-modes are mutually exclusive' '
200+
cat >expect <<-EOF &&
201+
fatal: only one of ${SQ}--exclude-existing${SQ} or ${SQ}--verify${SQ} can be given
202+
EOF
203+
204+
test_must_fail git show-ref --verify --exclude-existing 2>err &&
205+
test_cmp expect err
206+
'
207+
199208
test_done

0 commit comments

Comments
 (0)