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

[PATCH] support bash completion for add-on commands

From
JHJoey Hess <joey@joeyh.name>
Date
Jul 16, 2015, 17:22 UTC
Message-ID
<20150716172234.GA3865@kitenet.net>

This makes it possible to implement bash completion for add-on commands, that will work even when the bash completion scripts are being loaded on-demand, as is done by the bash-completion package.

git's bash completion handles subcommands by running a _git_$command function. As well as the many such functions included in git-completion.bash, there can be other functions defined elsewhere to support third-party add-on git commands, and they'll happily be used.

But, bash completion scripts are often loaded on demand, as shown in the completion_loader example in bash's man page, and the bash-completion implementation that is commonly used on many Linux systems. The demand loading will load this very script from some place like /usr/share/bash-completion/completions/git, when the user complete a git command. But, completion scripts for git add-on commands don't get loaded.

For example, when I wrote a git-annex bash completion script, bash was unable to tab complete "git annex foo", until I tab completed a "git-annex" command. Which loaded the git-annex completion, and then that same completion worked to make "git annex foo" tab complete. An inconsistent UI..

So, if the git completion script is unable to find the wanted _git_$command function, have it fall-back to looking for a git-$command completion script, and loading it. The add-on script is looked for in the same directory as the git completion script, which we can find by looking at BASH_SOURCE.

Signed-off-by: Joey Hess <joeyh@joeyh.name>
---
 contrib/completion/git-completion.bash | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c97c648..ba91b2a 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2614,7 +2614,16 @@ __git_main ()
 	if [ -n "$expansion" ]; then
 		words[1]=$expansion
 		completion_func="_git_${expansion//-/_}"
-		declare -f $completion_func >/dev/null && $completion_func
+		declare -f $completion_func >/dev/null && $completion_func && return
+	fi
+
+	# As a fallback, if no completion function is defined for the
+	# command, look for add-on command completion script in same
+	# directory as this completion script, and if found, source it,
+	# and restart completion using it.
+	local compdir="${BASH_SOURCE%/*}"
+	if [ -e "$compdir/git-$command" ]; then
+		source "$compdir/git-$command" && __git_main "$@"
 	fi
 }
 
-- 
2.1.4
Message 1 of 1 in “support bash completion for add-on commands”
  1. support bash completion for add-on commandsJoey Hess, Jul 16, 2015

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.