completion: implement and use --list-cmds=main,others

This is part of the effort to break down and provide commands by category in machine-readable form. This could be helpful later on when completion script switches to use --list-cmds for selecting completable commands. It would be much easier for the user to choose to complete _all_ commands instead of the default selection by passing different values to --list-cmds in git-completino.bash. While at there, replace "git help -a" in git-completion.bash with --list-cmds since it's better suited for this task. 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:39 UTC 6bb2dc0b9472a84c7d17ee93bda28a7c1c97d415
5 files changed +43 -2
Documentation/git.txt
+2 -1
@@ -167,7 +167,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
167 List commands by group. This is an internal/experimental
168 option and may change or be removed in the future. Supported
169 groups are: builtins, parseopt (builtin commands that use
170 - parse-options).
170 + parse-options), main (all commands in libexec directory),
171 + others (all other commands in `$PATH` that have git- prefix).
172
173 GIT COMMANDS
174 ------------
contrib/completion/git-completion.bash
+1 -1
@@ -839,7 +839,7 @@ __git_commands () {
839 then
840 printf "%s" "${GIT_TESTING_COMMAND_COMPLETION}"
841 else
842 - git help -a|egrep '^ [a-zA-Z0-9]'
842 + git --list-cmds=main,others
843 fi
844 }
845
git.c
+4
@@ -56,6 +56,10 @@ static int list_cmds(const char *spec)
56
57 if (match_token(spec, len, "builtins"))
58 list_builtins(&list, 0);
59 + else if (match_token(spec, len, "main"))
60 + list_all_main_cmds(&list);
61 + else if (match_token(spec, len, "others"))
62 + list_all_other_cmds(&list);
63 else
64 die(_("unsupported command listing type '%s'"), spec);
65 spec += len;
help.c
+32
@@ -297,6 +297,38 @@ void list_common_cmds_help(void)
297 print_cmd_by_category(common_categories);
298 }
299
300 +void list_all_main_cmds(struct string_list *list)
301 +{
302 + struct cmdnames main_cmds, other_cmds;
303 + int i;
304 +
305 + memset(&main_cmds, 0, sizeof(main_cmds));
306 + memset(&other_cmds, 0, sizeof(other_cmds));
307 + load_command_list("git-", &main_cmds, &other_cmds);
308 +
309 + for (i = 0; i < main_cmds.cnt; i++)
310 + string_list_append(list, main_cmds.names[i]->name);
311 +
312 + clean_cmdnames(&main_cmds);
313 + clean_cmdnames(&other_cmds);
314 +}
315 +
316 +void list_all_other_cmds(struct string_list *list)
317 +{
318 + struct cmdnames main_cmds, other_cmds;
319 + int i;
320 +
321 + memset(&main_cmds, 0, sizeof(main_cmds));
322 + memset(&other_cmds, 0, sizeof(other_cmds));
323 + load_command_list("git-", &main_cmds, &other_cmds);
324 +
325 + for (i = 0; i < other_cmds.cnt; i++)
326 + string_list_append(list, other_cmds.names[i]->name);
327 +
328 + clean_cmdnames(&main_cmds);
329 + clean_cmdnames(&other_cmds);
330 +}
331 +
332 int is_in_cmdlist(struct cmdnames *c, const char *s)
333 {
334 int i;
help.h
+4
@@ -1,6 +1,8 @@
1 #ifndef HELP_H
2 #define HELP_H
3
4 +struct string_list;
5 +
6 struct cmdnames {
7 int alloc;
8 int cnt;
@@ -17,6 +19,8 @@ static inline void mput_char(char c, unsigned int num)
19 }
20
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 const char *help_unknown_cmd(const char *cmd);
25 extern void load_command_list(const char *prefix,
26 struct cmdnames *main_cmds,