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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 20, 2018 at 20:40 UTC 3c7777672bf9bc9ac2ddb422633b39af4faa1682
5 files changed +51 -1
Documentation/git.txt
+2 -1
@@ -168,7 +168,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
168 option and may change or be removed in the future. Supported
169 groups are: builtins, parseopt (builtin commands that use
170 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)
173
174 GIT COMMANDS
175 ------------
generate-cmdlist.sh
+17
@@ -45,6 +45,21 @@ define_categories () {
45 test "$bit" -gt 32 && die "Urgh.. too many categories?"
46 }
47
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 +
63 print_command_list () {
64 echo "static struct cmdname_help command_list[] = {"
65
@@ -70,4 +85,6 @@ struct cmdname_help {
85 "
86 define_categories "$1"
87 echo
88 +define_category_names "$1"
89 +echo
90 print_command_list "$1"
git.c
+7
@@ -60,6 +60,13 @@ static int list_cmds(const char *spec)
60 list_all_main_cmds(&list);
61 else if (match_token(spec, len, "others"))
62 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 + }
70 else
71 die(_("unsupported command listing type '%s'"), spec);
72 spec += len;
help.c
+23
@@ -329,6 +329,29 @@ void list_all_other_cmds(struct string_list *list)
329 clean_cmdnames(&other_cmds);
330 }
331
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 +
355 int is_in_cmdlist(struct cmdnames *c, const char *s)
356 {
357 int i;
help.h
+2
@@ -21,6 +21,8 @@ static inline void mput_char(char c, unsigned int num)
21 extern void list_common_cmds_help(void);
22 extern void list_all_main_cmds(struct string_list *list);
23 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);
26 extern const char *help_unknown_cmd(const char *cmd);
27 extern void load_command_list(const char *prefix,
28 struct cmdnames *main_cmds,