Skip to content

Commit 1f3c5f3

Browse files
committed
Merge branch 'ab/help-fixes'
Updates to how command line options to "git help" are handled. * ab/help-fixes: help: don't print "\n" before single-section output help: add --no-[external-commands|aliases] for use with --all help: error if [-a|-g|-c] and [-i|-m|-w] are combined help: correct usage & behavior of "git help --all" help: note the option name on option incompatibility help.c: split up list_all_cmds_help() function help tests: test "git" and "git help [-a|-g] spacing help.c: use puts() instead of printf{,_ln}() for consistency help doc: add missing "]" to "[-a|--all]"
2 parents 69a3b75 + 93de1b6 commit 1f3c5f3

File tree

5 files changed

+187
-24
lines changed

5 files changed

+187
-24
lines changed

Documentation/git-help.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ git-help - Display help information about Git
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git help' [-a|--all [--[no-]verbose]]
12-
[[-i|--info] [-m|--man] [-w|--web]] [<command>|<guide>]
11+
'git help' [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]
12+
'git help' [[-i|--info] [-m|--man] [-w|--web]] [<command>|<guide>]
1313
'git help' [-g|--guides]
1414
'git help' [-c|--config]
1515

@@ -46,8 +46,15 @@ OPTIONS
4646
-------
4747
-a::
4848
--all::
49-
Prints all the available commands on the standard output. This
50-
option overrides any given command or guide name.
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.
5158

5259
--verbose::
5360
When used with `--all` print description for all recognized

builtin/help.c

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ static const char *html_path;
5151
static int verbose = 1;
5252
static enum help_format help_format = HELP_FORMAT_NONE;
5353
static int exclude_guides;
54+
static int show_external_commands = -1;
55+
static int show_aliases = -1;
5456
static struct option builtin_help_options[] = {
5557
OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"),
5658
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")),
5762
OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
5863
OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
5964
OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
@@ -75,8 +80,8 @@ static struct option builtin_help_options[] = {
7580
};
7681

7782
static const char * const builtin_help_usage[] = {
78-
N_("git help [-a|--all] [--[no-]verbose]]\n"
79-
" [[-i|--info] [-m|--man] [-w|--web]] [<command>]"),
83+
"git help [-a|--all] [--[no-]verbose]] [--[no-]external-commands] [--[no-]aliases]",
84+
N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>]"),
8085
"git help [-g|--guides]",
8186
"git help [-c|--config]",
8287
NULL
@@ -574,11 +579,40 @@ static const char *check_git_cmd(const char* cmd)
574579
return cmd;
575580
}
576581

577-
static void no_extra_argc(int argc)
582+
static void no_help_format(const char *opt_mode, enum help_format fmt)
583+
{
584+
const char *opt_fmt;
585+
586+
switch (fmt) {
587+
case HELP_FORMAT_NONE:
588+
return;
589+
case HELP_FORMAT_MAN:
590+
opt_fmt = "--man";
591+
break;
592+
case HELP_FORMAT_INFO:
593+
opt_fmt = "--info";
594+
break;
595+
case HELP_FORMAT_WEB:
596+
opt_fmt = "--web";
597+
break;
598+
default:
599+
BUG("unreachable");
600+
}
601+
602+
usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
603+
builtin_help_usage, builtin_help_options, opt_mode,
604+
opt_fmt);
605+
}
606+
607+
static void opt_mode_usage(int argc, const char *opt_mode,
608+
enum help_format fmt)
578609
{
579610
if (argc)
580-
usage_msg_opt(_("this option doesn't take any other arguments"),
581-
builtin_help_usage, builtin_help_options);
611+
usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
612+
builtin_help_usage, builtin_help_options,
613+
opt_mode);
614+
615+
no_help_format(opt_mode, fmt);
582616
}
583617

584618
int cmd_help(int argc, const char **argv, const char *prefix)
@@ -591,11 +625,19 @@ int cmd_help(int argc, const char **argv, const char *prefix)
591625
builtin_help_usage, 0);
592626
parsed_help_format = help_format;
593627

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+
594634
switch (cmd_mode) {
595635
case HELP_ACTION_ALL:
636+
opt_mode_usage(argc, "--all", help_format);
596637
if (verbose) {
597638
setup_pager();
598-
list_all_cmds_help();
639+
list_all_cmds_help(show_external_commands,
640+
show_aliases);
599641
return 0;
600642
}
601643
printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
@@ -604,20 +646,21 @@ int cmd_help(int argc, const char **argv, const char *prefix)
604646
printf("%s\n", _(git_more_info_string));
605647
break;
606648
case HELP_ACTION_GUIDES:
607-
no_extra_argc(argc);
649+
opt_mode_usage(argc, "--guides", help_format);
608650
list_guides_help();
609651
printf("%s\n", _(git_more_info_string));
610652
return 0;
611653
case HELP_ACTION_CONFIG_FOR_COMPLETION:
612-
no_extra_argc(argc);
654+
opt_mode_usage(argc, "--config-for-completion", help_format);
613655
list_config_help(SHOW_CONFIG_VARS);
614656
return 0;
615657
case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
616-
no_extra_argc(argc);
658+
opt_mode_usage(argc, "--config-sections-for-completion",
659+
help_format);
617660
list_config_help(SHOW_CONFIG_SECTIONS);
618661
return 0;
619662
case HELP_ACTION_CONFIG:
620-
no_extra_argc(argc);
663+
opt_mode_usage(argc, "--config", help_format);
621664
setup_pager();
622665
list_config_help(SHOW_CONFIG_HUMAN);
623666
printf("\n%s\n", _("'git help config' for more information"));

help.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ static void print_cmd_by_category(const struct category_description *catdesc,
124124
uint32_t mask = catdesc[i].category;
125125
const char *desc = catdesc[i].desc;
126126

127-
printf("\n%s\n", _(desc));
127+
if (i)
128+
putchar('\n');
129+
puts(_(desc));
128130
print_command_list(cmds, mask, longest);
129131
}
130132
free(cmds);
@@ -317,7 +319,7 @@ void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
317319
}
318320

319321
if (other_cmds->cnt) {
320-
printf_ln(_("git commands available from elsewhere on your $PATH"));
322+
puts(_("git commands available from elsewhere on your $PATH"));
321323
putchar('\n');
322324
pretty_print_cmdnames(other_cmds, colopts);
323325
putchar('\n');
@@ -327,6 +329,7 @@ void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
327329
void list_common_cmds_help(void)
328330
{
329331
puts(_("These are common Git commands used in various situations:"));
332+
putchar('\n');
330333
print_cmd_by_category(common_categories, NULL);
331334
}
332335

@@ -432,22 +435,24 @@ static int get_alias(const char *var, const char *value, void *data)
432435
return 0;
433436
}
434437

435-
void list_all_cmds_help(void)
438+
static void list_all_cmds_help_external_commands(void)
436439
{
437440
struct string_list others = STRING_LIST_INIT_DUP;
438-
struct string_list alias_list = STRING_LIST_INIT_DUP;
439-
struct cmdname_help *aliases;
440-
int i, longest;
441-
442-
printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
443-
print_cmd_by_category(main_categories, &longest);
441+
int i;
444442

445443
list_all_other_cmds(&others);
446444
if (others.nr)
447445
printf("\n%s\n", _("External commands"));
448446
for (i = 0; i < others.nr; i++)
449447
printf(" %s\n", others.items[i].string);
450448
string_list_clear(&others, 0);
449+
}
450+
451+
static void list_all_cmds_help_aliases(int longest)
452+
{
453+
struct string_list alias_list = STRING_LIST_INIT_DUP;
454+
struct cmdname_help *aliases;
455+
int i;
451456

452457
git_config(get_alias, &alias_list);
453458
string_list_sort(&alias_list);
@@ -473,6 +478,20 @@ void list_all_cmds_help(void)
473478
string_list_clear(&alias_list, 1);
474479
}
475480

481+
void list_all_cmds_help(int show_external_commands, int show_aliases)
482+
{
483+
int longest;
484+
485+
puts(_("See 'git help <command>' to read about a specific subcommand"));
486+
putchar('\n');
487+
print_cmd_by_category(main_categories, &longest);
488+
489+
if (show_external_commands)
490+
list_all_cmds_help_external_commands();
491+
if (show_aliases)
492+
list_all_cmds_help_aliases(longest);
493+
}
494+
476495
int is_in_cmdlist(struct cmdnames *c, const char *s)
477496
{
478497
int i;

help.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static inline void mput_char(char c, unsigned int num)
2020
}
2121

2222
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);
2424
void list_guides_help(void);
2525

2626
void list_all_main_cmds(struct string_list *list);

t/t0012-help.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ test_expect_success 'basic help commands' '
3535
'
3636

3737
test_expect_success 'invalid usage' '
38+
test_expect_code 129 git help -a add &&
39+
test_expect_code 129 git help --all add &&
40+
3841
test_expect_code 129 git help -g add &&
3942
test_expect_code 129 git help -a -c &&
4043
@@ -46,6 +49,29 @@ test_expect_success 'invalid usage' '
4649
test_expect_code 129 git help --config-sections-for-completion add
4750
'
4851

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+
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+
4975
test_expect_success "works for commands and guides by default" '
5076
configure_help &&
5177
git help status &&
@@ -138,6 +164,74 @@ test_expect_success 'git help --config-sections-for-completion' '
138164
test_cmp human.munged sections
139165
'
140166

167+
test_section_spacing () {
168+
cat >expect &&
169+
"$@" >out &&
170+
grep -E "(^[^ ]|^$)" out >actual
171+
}
172+
173+
test_section_spacing_trailer () {
174+
test_section_spacing "$@" &&
175+
test_expect_code 1 git >out &&
176+
sed -n '/list available subcommands/,$p' <out >>expect
177+
}
178+
179+
180+
for cmd in git "git help"
181+
do
182+
test_expect_success "'$cmd' section spacing" '
183+
test_section_spacing_trailer git help <<-\EOF &&
184+
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
185+
186+
These are common Git commands used in various situations:
187+
188+
start a working area (see also: git help tutorial)
189+
190+
work on the current change (see also: git help everyday)
191+
192+
examine the history and state (see also: git help revisions)
193+
194+
grow, mark and tweak your common history
195+
196+
collaborate (see also: git help workflows)
197+
198+
EOF
199+
test_cmp expect actual
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+
The Git concept guides are:
230+
231+
EOF
232+
test_cmp expect actual
233+
'
234+
141235
test_expect_success 'generate builtin list' '
142236
mkdir -p sub &&
143237
git --list-cmds=builtins >builtins

0 commit comments

Comments
 (0)