Skip to content

Commit 26c7d06

Browse files
pcloudsgitster
authored andcommitted
help -a: improve and make --verbose default
When you type "git help" (or just "git") you are greeted with a list with commonly used commands and their short description and are suggested to use "git help -a" or "git help -g" for more details. "git help -av" would be more friendly and inline with what is shown with "git help" since it shows list of commands with description as well, and commands are properly grouped. "help -av" does not show everything "help -a" shows though. Add external command section in "help -av" for this. While at there, add a section for aliases as well (until now aliases have no UI, just "git config"). Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Reviewed-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1d4361b commit 26c7d06

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

Documentation/git-help.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-help - Display help information about Git
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git help' [-a|--all [--verbose]] [-g|--guide]
11+
'git help' [-a|--all [--[no-]verbose]] [-g|--guide]
1212
[-i|--info|-m|--man|-w|--web] [COMMAND|GUIDE]
1313

1414
DESCRIPTION
@@ -42,8 +42,10 @@ OPTIONS
4242
--all::
4343
Prints all the available commands on the standard output. This
4444
option overrides any given command or guide name.
45-
When used with `--verbose` print description for all recognized
46-
commands.
45+
46+
--verbose::
47+
When used with `--all` print description for all recognized
48+
commands. This is the default.
4749

4850
-c::
4951
--config::

builtin/help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static const char *html_path;
3838
static int show_all = 0;
3939
static int show_guides = 0;
4040
static int show_config;
41-
static int verbose;
41+
static int verbose = 1;
4242
static unsigned int colopts;
4343
static enum help_format help_format = HELP_FORMAT_NONE;
4444
static int exclude_guides;

help.c

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ static int cmd_name_cmp(const void *elem1, const void *elem2)
9898
return strcmp(e1->name, e2->name);
9999
}
100100

101-
static void print_cmd_by_category(const struct category_description *catdesc)
101+
static void print_cmd_by_category(const struct category_description *catdesc,
102+
int *longest_p)
102103
{
103104
struct cmdname_help *cmds;
104105
int longest = 0;
@@ -124,6 +125,8 @@ static void print_cmd_by_category(const struct category_description *catdesc)
124125
print_command_list(cmds, mask, longest);
125126
}
126127
free(cmds);
128+
if (longest_p)
129+
*longest_p = longest;
127130
}
128131

129132
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
@@ -307,7 +310,7 @@ void list_commands(unsigned int colopts,
307310
void list_common_cmds_help(void)
308311
{
309312
puts(_("These are common Git commands used in various situations:"));
310-
print_cmd_by_category(common_categories);
313+
print_cmd_by_category(common_categories, NULL);
311314
}
312315

313316
void list_all_main_cmds(struct string_list *list)
@@ -405,7 +408,7 @@ void list_common_guides_help(void)
405408
{ CAT_guide, N_("The common Git guides are:") },
406409
{ 0, NULL }
407410
};
408-
print_cmd_by_category(catdesc);
411+
print_cmd_by_category(catdesc, NULL);
409412
putchar('\n');
410413
}
411414

@@ -494,9 +497,48 @@ void list_config_help(int for_human)
494497
string_list_clear(&keys, 0);
495498
}
496499

500+
static int get_alias(const char *var, const char *value, void *data)
501+
{
502+
struct string_list *list = data;
503+
504+
if (skip_prefix(var, "alias.", &var))
505+
string_list_append(list, var)->util = xstrdup(value);
506+
507+
return 0;
508+
}
509+
497510
void list_all_cmds_help(void)
498511
{
499-
print_cmd_by_category(main_categories);
512+
struct string_list others = STRING_LIST_INIT_DUP;
513+
struct string_list alias_list = STRING_LIST_INIT_DUP;
514+
struct cmdname_help *aliases;
515+
int i, longest;
516+
517+
printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
518+
print_cmd_by_category(main_categories, &longest);
519+
520+
list_all_other_cmds(&others);
521+
if (others.nr)
522+
printf("\n%s\n", _("External commands"));
523+
for (i = 0; i < others.nr; i++)
524+
printf(" %s\n", others.items[i].string);
525+
string_list_clear(&others, 0);
526+
527+
git_config(get_alias, &alias_list);
528+
string_list_sort(&alias_list);
529+
if (alias_list.nr) {
530+
printf("\n%s\n", _("Command aliases"));
531+
ALLOC_ARRAY(aliases, alias_list.nr + 1);
532+
for (i = 0; i < alias_list.nr; i++) {
533+
aliases[i].name = alias_list.items[i].string;
534+
aliases[i].help = alias_list.items[i].util;
535+
aliases[i].category = 1;
536+
}
537+
aliases[alias_list.nr].name = NULL;
538+
print_command_list(aliases, 1, longest);
539+
free(aliases);
540+
}
541+
string_list_clear(&alias_list, 1);
500542
}
501543

502544
int is_in_cmdlist(struct cmdnames *c, const char *s)

t/t0012-help.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ test_expect_success "setup" '
2929
# to verify
3030
test_expect_success 'basic help commands' '
3131
git help >/dev/null &&
32-
git help -a >/dev/null &&
32+
git help -a --no-verbose >/dev/null &&
3333
git help -g >/dev/null &&
34-
git help -av >/dev/null
34+
git help -a >/dev/null
3535
'
3636

3737
test_expect_success "works for commands and guides by default" '

0 commit comments

Comments
 (0)