Skip to content

Commit 3c77776

Browse files
pcloudsgitster
authored andcommitted
git: support --list-cmds=list-<category>
This allows us to select any group of commands by a category defined in command-list.txt. This is an internal/hidden option so we don't have to be picky about the category name or worried about exposing too much. This will be used later by git-completion.bash to retrieve certain command groups. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6bb2dc0 commit 3c77776

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

Documentation/git.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
168168
option and may change or be removed in the future. Supported
169169
groups are: builtins, parseopt (builtin commands that use
170170
parse-options), main (all commands in libexec directory),
171-
others (all other commands in `$PATH` that have git- prefix).
171+
others (all other commands in `$PATH` that have git- prefix),
172+
list-<category> (see categories in command-list.txt)
172173

173174
GIT COMMANDS
174175
------------

generate-cmdlist.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ define_categories () {
4545
test "$bit" -gt 32 && die "Urgh.. too many categories?"
4646
}
4747

48+
define_category_names () {
49+
echo
50+
echo "/* Category names */"
51+
echo "static const char *category_names[] = {"
52+
bit=0
53+
category_list "$1" |
54+
while read cat
55+
do
56+
echo " \"$cat\", /* (1UL << $bit) */"
57+
bit=$(($bit+1))
58+
done
59+
echo " NULL"
60+
echo "};"
61+
}
62+
4863
print_command_list () {
4964
echo "static struct cmdname_help command_list[] = {"
5065

@@ -70,4 +85,6 @@ struct cmdname_help {
7085
"
7186
define_categories "$1"
7287
echo
88+
define_category_names "$1"
89+
echo
7390
print_command_list "$1"

git.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ static int list_cmds(const char *spec)
6060
list_all_main_cmds(&list);
6161
else if (match_token(spec, len, "others"))
6262
list_all_other_cmds(&list);
63+
else if (len > 5 && !strncmp(spec, "list-", 5)) {
64+
struct strbuf sb = STRBUF_INIT;
65+
66+
strbuf_add(&sb, spec + 5, len - 5);
67+
list_cmds_by_category(&list, sb.buf);
68+
strbuf_release(&sb);
69+
}
6370
else
6471
die(_("unsupported command listing type '%s'"), spec);
6572
spec += len;

help.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,29 @@ void list_all_other_cmds(struct string_list *list)
329329
clean_cmdnames(&other_cmds);
330330
}
331331

332+
void list_cmds_by_category(struct string_list *list,
333+
const char *cat)
334+
{
335+
int i, n = ARRAY_SIZE(command_list);
336+
uint32_t cat_id = 0;
337+
338+
for (i = 0; category_names[i]; i++) {
339+
if (!strcmp(cat, category_names[i])) {
340+
cat_id = 1UL << i;
341+
break;
342+
}
343+
}
344+
if (!cat_id)
345+
die(_("unsupported command listing type '%s'"), cat);
346+
347+
for (i = 0; i < n; i++) {
348+
struct cmdname_help *cmd = command_list + i;
349+
350+
if (cmd->category & cat_id)
351+
string_list_append(list, drop_prefix(cmd->name));
352+
}
353+
}
354+
332355
int is_in_cmdlist(struct cmdnames *c, const char *s)
333356
{
334357
int i;

help.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ static inline void mput_char(char c, unsigned int num)
2121
extern void list_common_cmds_help(void);
2222
extern void list_all_main_cmds(struct string_list *list);
2323
extern void list_all_other_cmds(struct string_list *list);
24+
extern void list_cmds_by_category(struct string_list *list,
25+
const char *category);
2426
extern const char *help_unknown_cmd(const char *cmd);
2527
extern void load_command_list(const char *prefix,
2628
struct cmdnames *main_cmds,

0 commit comments

Comments
 (0)