Skip to content

Commit 6532f37

Browse files
pcloudsgitster
authored andcommitted
completion: allow to customize the completable command list
By default we show porcelain, external commands and a couple others that are also popular. If you are not happy with this list, you can now customize it a new config variable. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3301d36 commit 6532f37

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

Documentation/config.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,14 @@ credential.<url>.*::
13431343
credentialCache.ignoreSIGHUP::
13441344
Tell git-credential-cache--daemon to ignore SIGHUP, instead of quitting.
13451345

1346+
completion.commands::
1347+
This is only used by git-completion.bash to add or remove
1348+
commands from the list of completed commands. Normally only
1349+
porcelain commands and a few select others are completed. You
1350+
can add more commands, separated by space, in this
1351+
variable. Prefixing the command with '-' will remove it from
1352+
the existing list.
1353+
13461354
include::diff-config.txt[]
13471355

13481356
difftool.<tool>.path::

Documentation/git.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
170170
parse-options), main (all commands in libexec directory),
171171
others (all other commands in `$PATH` that have git- prefix),
172172
list-<category> (see categories in command-list.txt),
173-
nohelpers (exclude helper commands) and alias.
173+
nohelpers (exclude helper commands), alias and config
174+
(retrieve command list from config variable completion.commands)
174175

175176
GIT COMMANDS
176177
------------

contrib/completion/git-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@ __git_main ()
30123012
then
30133013
__gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
30143014
else
3015-
__gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete)"
3015+
__gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
30163016
fi
30173017
;;
30183018
esac

git.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ static int list_cmds(const char *spec)
7777
exclude_helpers_from_list(&list);
7878
else if (match_token(spec, len, "alias"))
7979
list_aliases(&list);
80+
else if (match_token(spec, len, "config"))
81+
list_cmds_by_config(&list);
8082
else if (len > 5 && !strncmp(spec, "list-", 5)) {
8183
struct strbuf sb = STRBUF_INIT;
8284

help.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,39 @@ void list_cmds_by_category(struct string_list *list,
366366
}
367367
}
368368

369+
void list_cmds_by_config(struct string_list *list)
370+
{
371+
const char *cmd_list;
372+
373+
/*
374+
* There's no actual repository setup at this point (and even
375+
* if there is, we don't really care; only global config
376+
* matters). If we accidentally set up a repository, it's ok
377+
* too since the caller (git --list-cmds=) should exit shortly
378+
* anyway.
379+
*/
380+
if (git_config_get_string_const("completion.commands", &cmd_list))
381+
return;
382+
383+
string_list_sort(list);
384+
string_list_remove_duplicates(list, 0);
385+
386+
while (*cmd_list) {
387+
struct strbuf sb = STRBUF_INIT;
388+
const char *p = strchrnul(cmd_list, ' ');
389+
390+
strbuf_add(&sb, cmd_list, p - cmd_list);
391+
if (*cmd_list == '-')
392+
string_list_remove(list, cmd_list + 1, 0);
393+
else
394+
string_list_insert(list, sb.buf);
395+
strbuf_release(&sb);
396+
while (*p == ' ')
397+
p++;
398+
cmd_list = p;
399+
}
400+
}
401+
369402
void list_common_guides_help(void)
370403
{
371404
struct category_description catdesc[] = {

help.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern void list_all_main_cmds(struct string_list *list);
2626
extern void list_all_other_cmds(struct string_list *list);
2727
extern void list_cmds_by_category(struct string_list *list,
2828
const char *category);
29+
extern void list_cmds_by_config(struct string_list *list);
2930
extern const char *help_unknown_cmd(const char *cmd);
3031
extern void load_command_list(const char *prefix,
3132
struct cmdnames *main_cmds,

0 commit comments

Comments
 (0)