{"thread":{"id":"39861","subject":"[PATCH] support bash completion for add-on commands","startedAt":"2015-07-16T17:22:34Z","lastAt":"2015-07-16T17:22:34Z","messageCount":1,"participants":["Joey Hess"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"266199","messageId":"20150716172234.GA3865@kitenet.net","threadId":"39861","inReplyTo":null,"subject":"[PATCH] support bash completion for add-on commands","fromName":"Joey Hess","fromEmail":"joey@joeyh.name","sentAt":"2015-07-16T17:22:34Z","receivedAt":"2015-07-16T17:22:34Z","isPatch":true,"sender":{"key":"joey@joeyh.name","avatar":null},"body":"This makes it possible to implement bash completion for add-on commands,\nthat will work even when the bash completion scripts are being loaded\non-demand, as is done by the bash-completion package.\n\ngit's bash completion handles subcommands by running a _git_$command\nfunction. As well as the many such functions included in\ngit-completion.bash, there can be other functions defined elsewhere\nto support third-party add-on git commands, and they'll happily be used.\n\nBut, bash completion scripts are often loaded on demand, as shown in the\ncompletion_loader example in bash's man page, and the bash-completion\nimplementation that is commonly used on many Linux systems. The demand\nloading will load this very script from some place like\n/usr/share/bash-completion/completions/git, when the user complete a git\ncommand. But, completion scripts for git add-on commands don't get loaded.\n\nFor example, when I wrote a git-annex bash completion script,\nbash was unable to tab complete \"git annex foo\", until I tab completed a\n\"git-annex\" command. Which loaded the git-annex completion, and then\nthat same completion worked to make \"git annex foo\" tab complete. An\ninconsistent UI..\n\nSo, if the git completion script is unable to find the wanted\n_git_$command function, have it fall-back to looking for a git-$command\ncompletion script, and loading it. The add-on script is looked for in the\nsame directory as the git completion script, which we can find by looking\nat BASH_SOURCE.\n\nSigned-off-by: Joey Hess <joeyh@joeyh.name>\n---\n contrib/completion/git-completion.bash | 11 ++++++++++-\n 1 file changed, 10 insertions(+), 1 deletion(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c97c648..ba91b2a 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2614,7 +2614,16 @@ __git_main ()\n \tif [ -n \"$expansion\" ]; then\n \t\twords[1]=$expansion\n \t\tcompletion_func=\"_git_${expansion//-/_}\"\n-\t\tdeclare -f $completion_func >/dev/null && $completion_func\n+\t\tdeclare -f $completion_func >/dev/null && $completion_func && return\n+\tfi\n+\n+\t# As a fallback, if no completion function is defined for the\n+\t# command, look for add-on command completion script in same\n+\t# directory as this completion script, and if found, source it,\n+\t# and restart completion using it.\n+\tlocal compdir=\"${BASH_SOURCE%/*}\"\n+\tif [ -e \"$compdir/git-$command\" ]; then\n+\t\tsource \"$compdir/git-$command\" && __git_main \"$@\"\n \tfi\n }\n \n\n-- \n2.1.4\n"}]}