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

[PATCH v2 5/8] completion: add --option completion for most builtin commands

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 24, 2018, 20:35 UTC
Message-ID
<20180324203525.24159-6-pclouds@gmail.com>
In-Reply-To
<20180324203525.24159-1-pclouds@gmail.com>

Many builtin commands use parseopt which supports expose the option list via --git-completion-helper but do not have explicit support in git-completion.bash. This patch detects those commands and uses __gitcomp_builtin for option completion.

This does not pollute the command name completion though. "git <tab>" will show you the same set as before. This only kicks in when you type the correct command name.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 contrib/completion/git-completion.bash | 28 ++++++++++++++++++++++++++
 t/t9902-completion.sh                  |  6 ++++++
 2 files changed, 34 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b3a9ecfad0..a90b0e8db4 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3034,12 +3034,40 @@ _git_worktree ()
 	fi
 }
 
+__git_complete_common () {
+	local command="$1"
+
+	case "$cur" in
+	--*)
+		__gitcomp_builtin "$command"
+		;;
+	esac
+}
+
+__git_cmds_with_parseopt_helper=
+__git_support_parseopt_helper () {
+	test -n "$__git_cmds_with_parseopt_helper" ||
+		__git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
+
+	case " $__git_cmds_with_parseopt_helper " in
+	*" $1 "*)
+		return 0
+		;;
+	*)
+		return 1
+		;;
+	esac
+}
+
 __git_complete_command () {
 	local command="$1"
 	local completion_func="_git_${command//-/_}"
 	if declare -f $completion_func >/dev/null 2>/dev/null; then
 		$completion_func
 		return 0
+	elif __git_support_parseopt_helper "$command"; then
+		__git_complete_common "$command"
+		return 0
 	else
 		return 1
 	fi
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index e6485feb0a..d0a1e4c988 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -1454,6 +1454,12 @@ test_expect_success 'completion used <cmd> completion for alias: !f() { : git <c
 	EOF
 '
 
+test_expect_success 'completion without explicit _git_xxx function' '
+	test_completion "git version --" <<-\EOF
+	--build-options Z
+	EOF
+'
+
 test_expect_failure 'complete with tilde expansion' '
 	git init tmp && cd tmp &&
 	test_when_finished "cd .. && rm -rf tmp" &&
-- 
2.17.0.rc0.348.gd5a49e0b6f
Previous: Nguyễn Thái Ngọc DuyNext: Eric Sunshine
Message 17 of 18 in “completion: add option completion for most builtin commands”
  1. completion: add option completion for most builtin commandsNguyễn Thái Ngọc Duy, Mar 21, 2018
  2. Junio C HamanoMar 21, 2018
  3. Duy NguyenMar 22, 2018
  4. Junio C HamanoMar 22, 2018
  5. Duy NguyenMar 22, 2018
  6. Junio C HamanoMar 22, 2018
  7. Duy NguyenMar 22, 2018
  8. Duy NguyenMar 24, 2018
  9. 0/8 completion: add option completion for most builtin commandsNguyễn Thái Ngọc Duy, Mar 24, 2018
  10. 1/8 git.c: move cmd_struct declaration upNguyễn Thái Ngọc Duy, Mar 24, 2018
  11. 2/8 git.c: add hidden option --list-parseopt-builtinsNguyễn Thái Ngọc Duy, Mar 24, 2018
  12. 8/8 completion: use __gitcomp_builtin in _git_cherryNguyễn Thái Ngọc Duy, Mar 24, 2018
  13. 4/8 completion: factor out _git_xxx calling codeNguyễn Thái Ngọc Duy, Mar 24, 2018
  14. 7/8 completion: use __gitcomp_builtin in _git_ls_treeNguyễn Thái Ngọc Duy, Mar 24, 2018
  15. 3/8 completion: mention the oldest version we need to supportNguyễn Thái Ngọc Duy, Mar 24, 2018
  16. 6/8 completion: delete option-only completion commandsNguyễn Thái Ngọc Duy, Mar 24, 2018
  17. 5/8 completion: add --option completion for most builtin commandsNguyễn Thái Ngọc Duy, Mar 24, 2018
  18. Eric SunshineMar 25, 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.