help: add --no-[external-commands|aliases] for use with --all

Add the ability to only emit git's own usage information under --all. This also allows us to extend the "test_section_spacing" tests added in a preceding commit to test "git help --all" output. Previously we could not do that, as the tests might find a git-* command in the "$PATH", which would make the output differ from one setup to another. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 21, 2022 at 20:38 UTC 1ce590133b545aa735683f46003d12df624d67af
5 files changed +66 -7
Documentation/git-help.txt
+9 -1
@@ -8,7 +8,7 @@ git-help - Display help information about Git
8 SYNOPSIS
9 --------
10 [verse]
11 -'git help' [-a|--all] [--[no-]verbose]
11 +'git help' [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]
12 'git help' [[-i|--info] [-m|--man] [-w|--web]] [<command>|<guide>]
13 'git help' [-g|--guides]
14 'git help' [-c|--config]
@@ -48,6 +48,14 @@ OPTIONS
48 --all::
49 Prints all the available commands on the standard output.
50
51 +--no-external-commands::
52 + When used with `--all`, exclude the listing of external "git-*"
53 + commands found in the `$PATH`.
54 +
55 +--no-aliases::
56 + When used with `--all`, exclude the listing of configured
57 + aliases.
58 +
59 --verbose::
60 When used with `--all` print description for all recognized
61 commands. This is the default.
builtin/help.c
+14 -2
@@ -51,9 +51,14 @@ static const char *html_path;
51 static int verbose = 1;
52 static enum help_format help_format = HELP_FORMAT_NONE;
53 static int exclude_guides;
54 +static int show_external_commands = -1;
55 +static int show_aliases = -1;
56 static struct option builtin_help_options[] = {
57 OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"),
58 HELP_ACTION_ALL),
59 + OPT_BOOL(0, "external-commands", &show_external_commands,
60 + N_("show external commands in --all")),
61 + OPT_BOOL(0, "aliases", &show_aliases, N_("show aliases in --all")),
62 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
63 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
64 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
@@ -75,7 +80,7 @@ static struct option builtin_help_options[] = {
80 };
81
82 static const char * const builtin_help_usage[] = {
78 - N_("git help [-a|--all] [--[no-]verbose]]"),
83 + N_("git help [-a|--all] [--[no-]verbose]] [--[no-]external-commands] [--[no-]aliases]"),
84 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>]"),
85 N_("git help [-g|--guides]"),
86 N_("git help [-c|--config]"),
@@ -620,12 +625,19 @@ int cmd_help(int argc, const char **argv, const char *prefix)
625 builtin_help_usage, 0);
626 parsed_help_format = help_format;
627
628 + if (cmd_mode != HELP_ACTION_ALL &&
629 + (show_external_commands >= 0 ||
630 + show_aliases >= 0))
631 + usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
632 + builtin_help_usage, builtin_help_options);
633 +
634 switch (cmd_mode) {
635 case HELP_ACTION_ALL:
636 opt_mode_usage(argc, "--all", help_format);
637 if (verbose) {
638 setup_pager();
628 - list_all_cmds_help();
639 + list_all_cmds_help(show_external_commands,
640 + show_aliases);
641 return 0;
642 }
643 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
help.c
+5 -3
@@ -476,15 +476,17 @@ static void list_all_cmds_help_aliases(int longest)
476 string_list_clear(&alias_list, 1);
477 }
478
479 -void list_all_cmds_help(void)
479 +void list_all_cmds_help(int show_external_commands, int show_aliases)
480 {
481 int longest;
482
483 puts(_("See 'git help <command>' to read about a specific subcommand"));
484 print_cmd_by_category(main_categories, &longest);
485
486 - list_all_cmds_help_external_commands();
487 - list_all_cmds_help_aliases(longest);
486 + if (show_external_commands)
487 + list_all_cmds_help_external_commands();
488 + if (show_aliases)
489 + list_all_cmds_help_aliases(longest);
490 }
491
492 int is_in_cmdlist(struct cmdnames *c, const char *s)
help.h
+1 -1
@@ -20,7 +20,7 @@ static inline void mput_char(char c, unsigned int num)
20 }
21
22 void list_common_cmds_help(void);
23 -void list_all_cmds_help(void);
23 +void list_all_cmds_help(int show_external_commands, int show_aliases);
24 void list_guides_help(void);
25
26 void list_all_main_cmds(struct string_list *list);
t/t0012-help.sh
+37
@@ -57,6 +57,19 @@ do
57 test_expect_code 129 git help $opt -m &&
58 test_expect_code 129 git help $opt -w
59 '
60 +
61 + if test "$opt" = "-a"
62 + then
63 + continue
64 + fi
65 +
66 + test_expect_success "invalid usage of '$opt' with --no-external-commands" '
67 + test_expect_code 129 git help $opt --no-external-commands
68 + '
69 +
70 + test_expect_success "invalid usage of '$opt' with --no-aliases" '
71 + test_expect_code 129 git help $opt --no-external-commands
72 + '
73 done
74
75 test_expect_success "works for commands and guides by default" '
@@ -187,6 +200,30 @@ do
200 '
201 done
202
203 +test_expect_success "'git help -a' section spacing" '
204 + test_section_spacing \
205 + git help -a --no-external-commands --no-aliases <<-\EOF &&
206 + See '\''git help <command>'\'' to read about a specific subcommand
207 +
208 + Main Porcelain Commands
209 +
210 + Ancillary Commands / Manipulators
211 +
212 + Ancillary Commands / Interrogators
213 +
214 + Interacting with Others
215 +
216 + Low-level Commands / Manipulators
217 +
218 + Low-level Commands / Interrogators
219 +
220 + Low-level Commands / Syncing Repositories
221 +
222 + Low-level Commands / Internal Helpers
223 + EOF
224 + test_cmp expect actual
225 +'
226 +
227 test_expect_success "'git help -g' section spacing" '
228 test_section_spacing_trailer git help -g <<-\EOF &&
229