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 <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 6532f3740b1c228c0a2a03a4126f4f7e4f2d73e7
6 files changed +47 -2
Documentation/config.txt
+8
@@ -1343,6 +1343,14 @@ credential.<url>.*::
1343 credentialCache.ignoreSIGHUP::
1344 Tell git-credential-cache--daemon to ignore SIGHUP, instead of quitting.
1345
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 +
1354 include::diff-config.txt[]
1355
1356 difftool.<tool>.path::
Documentation/git.txt
+2 -1
@@ -170,7 +170,8 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
170 parse-options), main (all commands in libexec directory),
171 others (all other commands in `$PATH` that have git- prefix),
172 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)
175
176 GIT COMMANDS
177 ------------
contrib/completion/git-completion.bash
+1 -1
@@ -3012,7 +3012,7 @@ __git_main ()
3012 then
3013 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3014 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)"
3016 fi
3017 ;;
3018 esac
git.c
+2
@@ -77,6 +77,8 @@ static int list_cmds(const char *spec)
77 exclude_helpers_from_list(&list);
78 else if (match_token(spec, len, "alias"))
79 list_aliases(&list);
80 + else if (match_token(spec, len, "config"))
81 + list_cmds_by_config(&list);
82 else if (len > 5 && !strncmp(spec, "list-", 5)) {
83 struct strbuf sb = STRBUF_INIT;
84
help.c
+33
@@ -366,6 +366,39 @@ void list_cmds_by_category(struct string_list *list,
366 }
367 }
368
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 +
402 void list_common_guides_help(void)
403 {
404 struct category_description catdesc[] = {
help.h
+1
@@ -26,6 +26,7 @@ extern void list_all_main_cmds(struct string_list *list);
26 extern void list_all_other_cmds(struct string_list *list);
27 extern void list_cmds_by_category(struct string_list *list,
28 const char *category);
29 +extern void list_cmds_by_config(struct string_list *list);
30 extern const char *help_unknown_cmd(const char *cmd);
31 extern void load_command_list(const char *prefix,
32 struct cmdnames *main_cmds,