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

[RFC PATCH 1/1] completion: load completion file for external subcommand

From
Florian Gamböck <mail@floga.de>
Date
Apr 8, 2018, 18:26 UTC
Message-ID
<20180408182552.26289-3-mail@floga.de>
In-Reply-To
<20180408182552.26289-1-mail@floga.de>

Adding external subcommands to Git is as easy as to put an executable file git-foo into PATH. Packaging such subcommands for a Linux distribution can be achieved by unpacking the executable into /usr/bin of the user's system. Adding system-wide completion scripts for new subcommands, however, can be a bit tricky.

Since bash-completion started to use dynamical loading of completion scripts somewhere around v2.0, it is no longer sufficient to drop a completion script of a subcommand into the standard completions path, /usr/share/bash-completion/completions, since this script will not be loaded if called as a git subcommand.

For example, look at https://bugs.gentoo.org/544722. To give a short summary: The popular git-flow subcommand provides a completion script, which gets installed as /usr/share/bash-completion/completions/git-flow.

If you now type into a Bash shell:
    git flow <TAB>

You will not get any completions, because bash-completion only loads completions for git and git has no idea that git-flow is defined in another file. You have to load this script manually or trigger the dynamic loader with:

    git-flow <TAB> # Please notice the dash instead of whitespace

This will not complete anything either, because it only defines a Bash function, without generating completions. But now the correct completion script has been loaded and the first command can use the completions.

So, the goal is now to teach the git completion script to consider the possibility of external completion scripts for subcommands, but of course without breaking current workflows.

I think the easiest method is to use a function that is defined by bash-completion v2.0+, namely __load_completion. It will take care of loading the correct script if present. Afterwards, the git completion script behaves as usual.

This way we can leverage bash-completion's dynamic loading for git subcommands and make it easier for developers to distribute custom completion scripts.

Signed-off-by: Florian Gamböck <mail@floga.de>
---
 contrib/completion/git-completion.bash | 8 ++++++++
 1 file changed, 8 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b09c8a236..e6114822c 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3096,12 +3096,20 @@ __git_main ()
 	fi
 
 	local completion_func="_git_${command//-/_}"
+	if ! declare -f $completion_func >/dev/null 2>/dev/null; then
+		declare -f __load_completion >/dev/null 2>/dev/null &&
+			__load_completion "git-$command"
+	fi
 	declare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return
 
 	local expansion=$(__git_aliased_command "$command")
 	if [ -n "$expansion" ]; then
 		words[1]=$expansion
 		completion_func="_git_${expansion//-/_}"
+		if ! declare -f $completion_func >/dev/null 2>/dev/null; then
+			declare -f __load_completion >/dev/null 2>/dev/null &&
+				__load_completion "git-$expansion"
+		fi
 		declare -f $completion_func >/dev/null 2>/dev/null && $completion_func
 	fi
 }
-- 
2.16.1
Previous: SZEDER GáborNext: Junio C Hamano
Message 7 of 13 in “completion: Dynamic completion loading”
  1. 0/1 completion: Dynamic completion loadingFlorian Gamböck, Apr 8, 2018
  2. 1/1 completion: Load completion file for external subcommandFlorian Gamböck, Apr 8, 2018
  3. Florian GamböckApr 8, 2018
  4. Stefan BellerApr 9, 2018
  5. Florian GamböckApr 9, 2018
  6. SZEDER GáborApr 19, 2018
  7. 1/1 completion: load completion file for external subcommandFlorian Gamböck, Apr 8, 2018
  8. Junio C HamanoApr 8, 2018
  9. Florian GamböckApr 9, 2018
  10. Junio C HamanoApr 9, 2018
  11. Florian GamböckApr 9, 2018
  12. Junio C HamanoApr 9, 2018
  13. Florian GamböckApr 10, 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.