From: Joey Hess <joey@joeyh.name>
To: git@vger.kernel.org
Subject: [PATCH] support bash completion for add-on commands
Date: Thu, 16 Jul 2015 13:22:34 -0400 [thread overview]
Message-ID: <20150716172234.GA3865@kitenet.net> (raw)
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
reply other threads:[~2015-07-16 17:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150716172234.GA3865@kitenet.net \
--to=joey@joeyh.name \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).