]> git.ipfire.org Git - thirdparty/git.git/commitdiff
completion: allow to customize the completable command list
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 20 May 2018 18:40:09 +0000 (20:40 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 21 May 2018 04:23:14 +0000 (13:23 +0900)
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 <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt
Documentation/git.txt
contrib/completion/git-completion.bash
git.c
help.c
help.h

index 2659153cb377554bc5cf6fc4199233b2bab498a2..9e81dcf867d8323d35a534859aa74bd7acc2eb83 100644 (file)
@@ -1343,6 +1343,14 @@ credential.<url>.*::
 credentialCache.ignoreSIGHUP::
        Tell git-credential-cache--daemon to ignore SIGHUP, instead of quitting.
 
+completion.commands::
+       This is only used by git-completion.bash to add or remove
+       commands from the list of completed commands. Normally only
+       porcelain commands and a few select others are completed. You
+       can add more commands, separated by space, in this
+       variable. Prefixing the command with '-' will remove it from
+       the existing list.
+
 include::diff-config.txt[]
 
 difftool.<tool>.path::
index 75f50d237994038e15a84c94d0d86294dd5f52d8..6f7eddf847bd1218ad62fad3563dc18a2448625d 100644 (file)
@@ -170,7 +170,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
        parse-options), main (all commands in libexec directory),
        others (all other commands in `$PATH` that have git- prefix),
        list-<category> (see categories in command-list.txt),
-       nohelpers (exclude helper commands) and alias.
+       nohelpers (exclude helper commands), alias and config
+       (retrieve command list from config variable completion.commands)
 
 GIT COMMANDS
 ------------
index 98f278fb9ad5052b636c9167f6db6bfb7e07e257..e5b2ccbdd2208069f25f2ce9505a2f88a8c8b5ff 100644 (file)
@@ -3012,7 +3012,7 @@ __git_main ()
                        then
                                __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
                        else
-                               __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete)"
+                               __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
                        fi
                        ;;
                esac
diff --git a/git.c b/git.c
index 63acd9ea81ea9d339f98ebb78a389fc632d89fe3..447dac0e71e6f3dc6680dcabc31210c6a967b814 100644 (file)
--- a/git.c
+++ b/git.c
@@ -77,6 +77,8 @@ static int list_cmds(const char *spec)
                        exclude_helpers_from_list(&list);
                else if (match_token(spec, len, "alias"))
                        list_aliases(&list);
+               else if (match_token(spec, len, "config"))
+                       list_cmds_by_config(&list);
                else if (len > 5 && !strncmp(spec, "list-", 5)) {
                        struct strbuf sb = STRBUF_INIT;
 
diff --git a/help.c b/help.c
index 23924dd30044b9fcf34839671efcf92d3e7d175b..abf87205b2ea2f13925348fa1a42ac0aebbb1ac4 100644 (file)
--- a/help.c
+++ b/help.c
@@ -366,6 +366,39 @@ void list_cmds_by_category(struct string_list *list,
        }
 }
 
+void list_cmds_by_config(struct string_list *list)
+{
+       const char *cmd_list;
+
+       /*
+        * There's no actual repository setup at this point (and even
+        * if there is, we don't really care; only global config
+        * matters). If we accidentally set up a repository, it's ok
+        * too since the caller (git --list-cmds=) should exit shortly
+        * anyway.
+        */
+       if (git_config_get_string_const("completion.commands", &cmd_list))
+               return;
+
+       string_list_sort(list);
+       string_list_remove_duplicates(list, 0);
+
+       while (*cmd_list) {
+               struct strbuf sb = STRBUF_INIT;
+               const char *p = strchrnul(cmd_list, ' ');
+
+               strbuf_add(&sb, cmd_list, p - cmd_list);
+               if (*cmd_list == '-')
+                       string_list_remove(list, cmd_list + 1, 0);
+               else
+                       string_list_insert(list, sb.buf);
+               strbuf_release(&sb);
+               while (*p == ' ')
+                       p++;
+               cmd_list = p;
+       }
+}
+
 void list_common_guides_help(void)
 {
        struct category_description catdesc[] = {
diff --git a/help.h b/help.h
index b2293e99bee97bef1563aef29a6c7ad958eb632f..3b38292a1b317ba32d934be6006cb86e0e7d0d94 100644 (file)
--- a/help.h
+++ b/help.h
@@ -26,6 +26,7 @@ extern void list_all_main_cmds(struct string_list *list);
 extern void list_all_other_cmds(struct string_list *list);
 extern void list_cmds_by_category(struct string_list *list,
                                  const char *category);
+extern void list_cmds_by_config(struct string_list *list);
 extern const char *help_unknown_cmd(const char *cmd);
 extern void load_command_list(const char *prefix,
                              struct cmdnames *main_cmds,