Skip to content

Commit 6bb2dc0

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e5d7a61 commit 6bb2dc0

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

Documentation/git.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
167167
List commands by group. This is an internal/experimental
168168
option and may change or be removed in the future. Supported
169169
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).
171172

172173
GIT COMMANDS
173174
------------

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ __git_commands () {
839839
then
840840
printf "%s" "${GIT_TESTING_COMMAND_COMPLETION}"
841841
else
842-
git help -a|egrep '^ [a-zA-Z0-9]'
842+
git --list-cmds=main,others
843843
fi
844844
}
845845

git.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ static int list_cmds(const char *spec)
5656

5757
if (match_token(spec, len, "builtins"))
5858
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);
5963
else
6064
die(_("unsupported command listing type '%s'"), spec);
6165
spec += len;

help.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,38 @@ void list_common_cmds_help(void)
297297
print_cmd_by_category(common_categories);
298298
}
299299

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+
300332
int is_in_cmdlist(struct cmdnames *c, const char *s)
301333
{
302334
int i;

help.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef HELP_H
22
#define HELP_H
33

4+
struct string_list;
5+
46
struct cmdnames {
57
int alloc;
68
int cnt;
@@ -17,6 +19,8 @@ static inline void mput_char(char c, unsigned int num)
1719
}
1820

1921
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);
2024
extern const char *help_unknown_cmd(const char *cmd);
2125
extern void load_command_list(const char *prefix,
2226
struct cmdnames *main_cmds,

0 commit comments

Comments
 (0)