git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 13/13] completion: allow to customize the completable command list

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 10, 2018, 08:46 UTC
Message-ID
<20180510084652.25880-14-pclouds@gmail.com>
In-Reply-To
<20180510084652.25880-1-pclouds@gmail.com>

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. See the big comment block for details.

PS. perhaps I should make aliases a group too, which makes it possible to _not_ complete aliases by omitting this special group in $GIT_COMPLETION_CMD_GROUPS

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt               | 10 ++++++++
 contrib/completion/git-completion.bash | 28 +++++++++++++++++++++-
 git.c                                  |  2 ++
 help.c                                 | 33 ++++++++++++++++++++++++++
 help.h                                 |  1 +
 5 files changed, 73 insertions(+), 1 deletion(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 2659153cb3..91f7eaed7b 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1343,6 +1343,16 @@ 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 complete list. Normally only porcelain
+	commands and a few select others are in the complete list. You
+	can add more commands, separated by space, in this
+	variable. Prefixing the command with '-' will remove it from
+	the existing list.
++
+This variable should not be per-repository.
+
 include::diff-config.txt[]
 
 difftool.<tool>.path::
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 908692ea52..f7ca9a4d4f 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -38,6 +38,29 @@
 #
 #     When set to "1", do not include "DWIM" suggestions in git-checkout
 #     completion (e.g., completing "foo" when "origin/foo" exists).
+#
+#   GIT_COMPLETION_CMD_GROUPS
+#
+#     When set, "git --list-cmds=$GIT_COMPLETION_CMD_GROUPS" will be
+#     used to get the list of completable commands. The default is
+#     "mainporcelain,others,list-complete" (in English: all porcelain
+#     commands and external ones are included. Certain non-porcelain
+#     commands are also marked for completion in command-list.txt).
+#     You could for example complete all commands with
+#
+#         GIT_COMPLETION_CMD_GROUPS=main,others
+#
+#     Or you could go with main porcelain only and extra commands in
+#     the configuration variable completion.commands with
+#
+#         GIT_COMPLETION_CMD_GROUPS=mainporcelain,config
+#
+#     Or go completely custom group with
+#
+#         GIT_COMPLETION_CMD_GROUPS=config
+#
+#     Or you could even play with other command categories found in
+#     command-list.txt.
 
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
@@ -840,8 +863,11 @@ __git_commands () {
 		if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
 		then
 			printf "%s" "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
+		elif test -n "$GIT_COMPLETION_CMD_GROUPS"
+		then
+			git --list-cmds="$GIT_COMPLETION_CMD_GROUPS"
 		else
-			git --list-cmds=list-mainporcelain,others,list-complete
+			git --list-cmds=list-mainporcelain,others,list-complete,config
 		fi
 		;;
 	all)
diff --git a/git.c b/git.c
index 4d5b8a9931..ea4feedd0b 100644
--- a/git.c
+++ b/git.c
@@ -60,6 +60,8 @@ static int list_cmds(const char *spec)
 			list_all_main_cmds(&list);
 		else if (match_token(spec, len, "others"))
 			list_all_other_cmds(&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 23924dd300..abf87205b2 100644
--- 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 b2293e99be..3b38292a1b 100644
--- 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,
-- 
2.17.0.705.g3525833791
Previous: Duy NguyenNext: Nguyễn Thái Ngọc Duy
Message 126 of 137 in “Keep all info in command-list.txt in git binary”
  1. 0/5 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Mar 26, 2018
  2. 2/5 git.c: implement --list-cmds=all and use it in git-completion.bashNguyễn Thái Ngọc Duy, Mar 26, 2018
  3. Eric SunshineApr 9, 2018
  4. 1/5 git.c: convert --list-builtins to --list-cmds=builtinsNguyễn Thái Ngọc Duy, Mar 26, 2018
  5. 3/5 generate-cmdlist.sh: keep all information in common-cmds.hNguyễn Thái Ngọc Duy, Mar 26, 2018
  6. Eric SunshineApr 9, 2018
  7. Eric SunshineApr 9, 2018
  8. Duy NguyenApr 9, 2018
  9. Duy NguyenApr 15, 2018
  10. 5/5 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Mar 26, 2018
  11. Eric SunshineApr 9, 2018
  12. Junio C HamanoApr 9, 2018
  13. Eric SunshineApr 9, 2018
  14. Duy NguyenApr 9, 2018
  15. 4/5 git.c: implement --list-cmds=porcelainNguyễn Thái Ngọc Duy, Mar 26, 2018
  16. Eric SunshineApr 9, 2018
  17. Philip OakleyApr 11, 2018
  18. Duy NguyenApr 14, 2018
  19. Philip OakleyApr 15, 2018
  20. Duy NguyenApr 17, 2018
  21. Duy NguyenApr 17, 2018
  22. Philip OakleyApr 17, 2018
  23. Philip OakleyApr 18, 2018
  24. Duy NguyenApr 18, 2018
  25. Philip OakleyApr 19, 2018
  26. 0/6 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 15, 2018
  27. 1/6 git.c: convert --list-builtins to --list-cmds=builtinsNguyễn Thái Ngọc Duy, Apr 15, 2018
  28. 4/6 git.c: implement --list-cmds=porcelainNguyễn Thái Ngọc Duy, Apr 15, 2018
  29. 3/6 generate-cmdlist.sh: keep all information in common-cmds.hNguyễn Thái Ngọc Duy, Apr 15, 2018
  30. Junio C HamanoApr 16, 2018
  31. Øystein WalleApr 23, 2018
  32. SZEDER GáborApr 23, 2018
  33. Junio C HamanoApr 16, 2018
  34. Duy NguyenApr 16, 2018
  35. SZEDER GáborApr 16, 2018
  36. Ramsay JonesApr 16, 2018
  37. Junio C HamanoApr 16, 2018
  38. 6/6 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 15, 2018
  39. 2/6 git.c: implement --list-cmds=all and use it in git-completion.bashNguyễn Thái Ngọc Duy, Apr 15, 2018
  40. Junio C HamanoApr 16, 2018
  41. 5/6 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 15, 2018
  42. 0/6 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 21, 2018
  43. 1/6 git.c: convert --list-*builtins to --list-cmds=*Nguyễn Thái Ngọc Duy, Apr 21, 2018
  44. 2/6 git.c: implement --list-cmds=all and use it in git-completion.bashNguyễn Thái Ngọc Duy, Apr 21, 2018
  45. SZEDER GáborApr 23, 2018
  46. 3/6 generate-cmdlist.sh: keep all information in common-cmds.hNguyễn Thái Ngọc Duy, Apr 21, 2018
  47. 6/6 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 21, 2018
  48. Eric SunshineApr 23, 2018
  49. 5/6 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 21, 2018
  50. 4/6 git.c: implement --list-cmds=porcelainNguyễn Thái Ngọc Duy, Apr 21, 2018
  51. SZEDER GáborApr 23, 2018
  52. Duy NguyenApr 24, 2018
  53. Duy NguyenApr 24, 2018
  54. SZEDER GáborApr 25, 2018
  55. Duy NguyenApr 25, 2018
  56. SZEDER GáborApr 25, 2018
  57. Duy NguyenApr 25, 2018
  58. Duy NguyenApr 21, 2018
  59. Ramsay JonesApr 22, 2018
  60. Duy NguyenApr 22, 2018
  61. Ramsay JonesApr 22, 2018
  62. Duy NguyenApr 22, 2018
  63. Ramsay JonesApr 22, 2018
  64. 00/12 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 25, 2018
  65. 01/12 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, Apr 25, 2018
  66. Eric SunshineApr 25, 2018
  67. SZEDER GáborApr 25, 2018
  68. 02/12 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, Apr 25, 2018
  69. Eric SunshineApr 25, 2018
  70. Duy NguyenApr 29, 2018
  71. 03/12 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, Apr 25, 2018
  72. 08/12 git: support --list-cmds=<category>Nguyễn Thái Ngọc Duy, Apr 25, 2018
  73. Eric SunshineApr 25, 2018
  74. 07/12 completion: implement and use --list-cmds=allNguyễn Thái Ngọc Duy, Apr 25, 2018
  75. 06/12 git: accept multiple --list-cmds optionsNguyễn Thái Ngọc Duy, Apr 25, 2018
  76. Eric SunshineApr 25, 2018
  77. 11/12 command-list.txt: add new category "complete"Nguyễn Thái Ngọc Duy, Apr 25, 2018
  78. 09/12 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 25, 2018
  79. 10/12 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 25, 2018
  80. Eric SunshineApr 25, 2018
  81. 05/12 git.c: convert --list-*builtins to --list-cmds=*Nguyễn Thái Ngọc Duy, Apr 25, 2018
  82. 04/12 Remove common-cmds.hNguyễn Thái Ngọc Duy, Apr 25, 2018
  83. 12/12 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, Apr 25, 2018
  84. 00/10 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 29, 2018
  85. 01/10 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, Apr 29, 2018
  86. 02/10 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, Apr 29, 2018
  87. 03/10 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, Apr 29, 2018
  88. 06/10 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, Apr 29, 2018
  89. 05/10 git.c: convert --list-*builtins to --list-cmds=*Nguyễn Thái Ngọc Duy, Apr 29, 2018
  90. 10/10 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, Apr 29, 2018
  91. 04/10 Remove common-cmds.hNguyễn Thái Ngọc Duy, Apr 29, 2018
  92. 07/10 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, Apr 29, 2018
  93. 08/10 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 29, 2018
  94. 09/10 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 29, 2018
  95. Duy NguyenApr 29, 2018
  96. Duy NguyenApr 30, 2018
  97. 00/13 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, May 7, 2018
  98. 01/13 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, May 7, 2018
  99. 02/13 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, May 7, 2018
  100. Junio C HamanoMay 8, 2018
  101. 04/13 Remove common-cmds.hNguyễn Thái Ngọc Duy, May 7, 2018
  102. 06/13 git --list-cmds: collect command list in a string_listNguyễn Thái Ngọc Duy, May 7, 2018
  103. 07/13 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, May 7, 2018
  104. 05/13 git.c: convert --list-* to --list-cmds=*Nguyễn Thái Ngọc Duy, May 7, 2018
  105. Junio C HamanoMay 8, 2018
  106. 09/13 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, May 7, 2018
  107. 12/13 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, May 7, 2018
  108. 13/13 completion: allow to customize the completable command listNguyễn Thái Ngọc Duy, May 7, 2018
  109. 10/13 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, May 7, 2018
  110. Junio C HamanoMay 8, 2018
  111. 08/13 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, May 7, 2018
  112. Junio C HamanoMay 8, 2018
  113. 11/13 command-list.txt: documentation and guide lineNguyễn Thái Ngọc Duy, May 7, 2018
  114. Philip OakleyMay 12, 2018
  115. 03/13 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, May 7, 2018
  116. 00/13 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, May 10, 2018
  117. 01/13 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, May 10, 2018
  118. 03/13 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, May 10, 2018
  119. 02/13 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, May 10, 2018
  120. 04/13 Remove common-cmds.hNguyễn Thái Ngọc Duy, May 10, 2018
  121. 11/13 command-list.txt: documentation and guide lineNguyễn Thái Ngọc Duy, May 10, 2018
  122. 10/13 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, May 10, 2018
  123. 12/13 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, May 10, 2018
  124. SZEDER GáborMay 11, 2018
  125. Duy NguyenMay 13, 2018
  126. 13/13 completion: allow to customize the completable command listNguyễn Thái Ngọc Duy, May 10, 2018
  127. 05/13 git.c: convert --list-* to --list-cmds=*Nguyễn Thái Ngọc Duy, May 10, 2018
  128. 06/13 git --list-cmds: collect command list in a string_listNguyễn Thái Ngọc Duy, May 10, 2018
  129. 07/13 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, May 10, 2018
  130. SZEDER GáborMay 11, 2018
  131. SZEDER GáborMay 11, 2018
  132. Duy NguyenMay 11, 2018
  133. 08/13 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, May 10, 2018
  134. 09/13 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, May 10, 2018
  135. Simon RuderichApr 19, 2018
  136. SZEDER GáborApr 19, 2018
  137. Simon RuderichApr 20, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.