]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/show-ref: ensure mutual exclusiveness of subcommands
authorPatrick Steinhardt <ps@pks.im>
Tue, 31 Oct 2023 08:16:46 +0000 (09:16 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Nov 2023 03:09:00 +0000 (12:09 +0900)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/show-ref.c
t/t1403-show-ref.sh

index 36ac10551da579d4e948ff146e1ec3dc217e44c1..6685495dd2c47331d00648736e18561691f988d8 100644 (file)
@@ -275,6 +275,10 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, show_ref_options,
                             show_ref_usage, 0);
 
+       if ((!!exclude_existing_opts.enabled + !!verify) > 1)
+               die(_("only one of '%s' or '%s' can be given"),
+                   "--exclude-existing", "--verify");
+
        if (exclude_existing_opts.enabled)
                return cmd_show_ref__exclude_existing(&exclude_existing_opts);
        else if (verify)
index 9252a581abf8a702fffb01bc7c388666207d5251..f1e0388324e6588b995dee7f44da9a9bf61e8c41 100755 (executable)
@@ -196,4 +196,13 @@ test_expect_success 'show-ref --verify with dangling ref' '
        )
 '
 
+test_expect_success 'show-ref sub-modes are mutually exclusive' '
+       cat >expect <<-EOF &&
+       fatal: only one of ${SQ}--exclude-existing${SQ} or ${SQ}--verify${SQ} can be given
+       EOF
+
+       test_must_fail git show-ref --verify --exclude-existing 2>err &&
+       test_cmp expect err
+'
+
 test_done