Skip to content

Commit 503cdda

Browse files
avargitster
authored andcommitted
help: error if [-a|-g|-c] and [-i|-m|-w] are combined
Add more sanity checking to "git help" usage by erroring out if these man viewer options are combined with incompatible command-modes that will never use these documentation viewers. This continues the work started in d35d03c (help: simplify by moving to OPT_CMDMODE(), 2021-09-22) of adding more sanity checking to "git help". Doing this allows us to clarify the "SYNOPSIS" in the documentation, and the "git help -h" output. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e8068b commit 503cdda

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

builtin/help.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,40 @@ static const char *check_git_cmd(const char* cmd)
574574
return cmd;
575575
}
576576

577-
static void opt_mode_usage(int argc, const char *opt_mode)
577+
static void no_help_format(const char *opt_mode, enum help_format fmt)
578+
{
579+
const char *opt_fmt;
580+
581+
switch (fmt) {
582+
case HELP_FORMAT_NONE:
583+
return;
584+
case HELP_FORMAT_MAN:
585+
opt_fmt = "--man";
586+
break;
587+
case HELP_FORMAT_INFO:
588+
opt_fmt = "--info";
589+
break;
590+
case HELP_FORMAT_WEB:
591+
opt_fmt = "--web";
592+
break;
593+
default:
594+
BUG("unreachable");
595+
}
596+
597+
usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
598+
builtin_help_usage, builtin_help_options, opt_mode,
599+
opt_fmt);
600+
}
601+
602+
static void opt_mode_usage(int argc, const char *opt_mode,
603+
enum help_format fmt)
578604
{
579605
if (argc)
580606
usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
581607
builtin_help_usage, builtin_help_options,
582608
opt_mode);
609+
610+
no_help_format(opt_mode, fmt);
583611
}
584612

585613
int cmd_help(int argc, const char **argv, const char *prefix)
@@ -594,7 +622,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
594622

595623
switch (cmd_mode) {
596624
case HELP_ACTION_ALL:
597-
opt_mode_usage(argc, "--all");
625+
opt_mode_usage(argc, "--all", help_format);
598626
if (verbose) {
599627
setup_pager();
600628
list_all_cmds_help();
@@ -606,20 +634,21 @@ int cmd_help(int argc, const char **argv, const char *prefix)
606634
printf("%s\n", _(git_more_info_string));
607635
break;
608636
case HELP_ACTION_GUIDES:
609-
opt_mode_usage(argc, "--guides");
637+
opt_mode_usage(argc, "--guides", help_format);
610638
list_guides_help();
611639
printf("%s\n", _(git_more_info_string));
612640
return 0;
613641
case HELP_ACTION_CONFIG_FOR_COMPLETION:
614-
opt_mode_usage(argc, "--config-for-completion");
642+
opt_mode_usage(argc, "--config-for-completion", help_format);
615643
list_config_help(SHOW_CONFIG_VARS);
616644
return 0;
617645
case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
618-
opt_mode_usage(argc, "--config-sections-for-completion");
646+
opt_mode_usage(argc, "--config-sections-for-completion",
647+
help_format);
619648
list_config_help(SHOW_CONFIG_SECTIONS);
620649
return 0;
621650
case HELP_ACTION_CONFIG:
622-
opt_mode_usage(argc, "--config");
651+
opt_mode_usage(argc, "--config", help_format);
623652
setup_pager();
624653
list_config_help(SHOW_CONFIG_HUMAN);
625654
printf("\n%s\n", _("'git help config' for more information"));

t/t0012-help.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ test_expect_success 'invalid usage' '
4949
test_expect_code 129 git help --config-sections-for-completion add
5050
'
5151

52+
for opt in '-a' '-g' '-c' '--config-for-completion' '--config-sections-for-completion'
53+
do
54+
test_expect_success "invalid usage of '$opt' with [-i|-m|-w]" '
55+
git help $opt &&
56+
test_expect_code 129 git help $opt -i &&
57+
test_expect_code 129 git help $opt -m &&
58+
test_expect_code 129 git help $opt -w
59+
'
60+
done
61+
5262
test_expect_success "works for commands and guides by default" '
5363
configure_help &&
5464
git help status &&

0 commit comments

Comments
 (0)