Package: git
Version: 1:2.11.0-4
Severity: normal

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 loaded on demand. If I type "git annex -<tab>",
/usr/share/bash-completion/completions/git is loaded. But, nothing makes
/usr/share/bash-completion/completions/git-annex be loaded, so
the tab completion does not work.

If I first type "git-annex -<tab>", that loads the git-annex completion,
which includes the necessary _git_annex to make "git annex -<tab>" also work.
But if I'm in the habit of typing "git annex" rather than "git-annex",
I'll never get good tab completion for its options.

If the git-annex completion file was installed in
/etc/bash_completion.d/git-annex, tab completion of "git annex"
would work from the beginning, because those completion files
are loaded at shell startup, not demand loaded. But, /etc/bash_completion.d/
is depreacted and will be removed sometime.

So, if the git completion script is unable to find the wanted
_git_$command function, the attached patch makes 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. With this patch, git-annex
and other git addons can provide completion files that will just work.

I originally submitted this patch to the git mailing list in 2015
as <20150716172234.ga3...@kitenet.net>, unfortunately none of the git
developers noticed it, or perhaps the esoteric nature of bash completion
and the particulars of how it's implemented on Debian scared them off.
So, I'm submitting it to the debian BTS in hopes that the git package
will get patched in Debian and perhaps the patch can later migrate upstream.

-- System Information:
Debian Release: 9.0
  APT prefers unstable
  APT policy: (500, 'unstable'), (500, 'testing'), (1, 'experimental')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 4.9.0-3-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_US.utf8, LC_CTYPE=en_US.utf8 (charmap=UTF-8), 
LANGUAGE=en_US.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages git depends on:
ii  dpkg             1.18.24
ii  git-man          1:2.11.0-4
ii  libc6            2.24-11
ii  libcurl3-gnutls  7.52.1-5
ii  liberror-perl    0.17024-1
ii  libexpat1        2.2.0-2
ii  libpcre3         2:8.39-3
ii  perl             5.24.1-3
ii  zlib1g           1:1.2.8.dfsg-5

Versions of packages git recommends:
ii  less                         481-2.1
ii  openssh-client [ssh-client]  1:7.4p1-10
ii  patch                        2.7.5-1+b2

Versions of packages git suggests:
ii  gettext-base                          0.19.8.1-2
pn  git-arch                              <none>
pn  git-cvs                               <none>
pn  git-daemon-run | git-daemon-sysvinit  <none>
pn  git-doc                               <none>
pn  git-el                                <none>
ii  git-email                             1:2.11.0-4
pn  git-gui                               <none>
pn  git-mediawiki                         <none>
pn  git-svn                               <none>
ii  gitk                                  1:2.11.0-4
pn  gitweb                                <none>

-- no debconf information

-- 
see shy jo
From mairix@mairix Mon Jan  1 12:34:56 1970
X-source-folder: /home/joey/mail/.git/annex/objects/76/W1/SHA256E-s21652--864aa4c6fd7c39a62c93cac3288b37fc3a3bbcfe0745a14e058718fb0151c451.gz/SHA256E-s21652--864aa4c6fd7c39a62c93cac3288b37fc3a3bbcfe0745a14e058718fb0151c451.gz
Return-Path: <git-ow...@vger.kernel.org>
X-Original-To: j...@kitenet.net
Delivered-To: j...@kitenet.net
Received: from vger.kernel.org (vger.kernel.org [209.132.180.67])
	by kitenet.net (Postfix) with ESMTP id 1CB421C5BE
	for <j...@kitenet.net>; Thu, 16 Jul 2015 13:22:47 -0400 (EDT)
Authentication-Results: kitenet.net;
	dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=joeyh.name header.i=@joeyh.name header.b=kVB/G+GK;
	dkim-atps=neutral
Received: (majord...@vger.kernel.org) by vger.kernel.org via listexpand
	id S1755118AbbGPRWp (ORCPT <rfc822;j...@kitenet.net>);
	Thu, 16 Jul 2015 13:22:45 -0400
Received: from kitenet.net ([66.228.36.95]:53728 "EHLO kitenet.net"
	rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP
	id S1751986AbbGPRWo (ORCPT <rfc822;g...@vger.kernel.org>);
	Thu, 16 Jul 2015 13:22:44 -0400
X-Question: 42
DKIM-Signature:	v=1; a=rsa-sha256; c=simple/simple; d=joeyh.name; s=mail;
	t=1437067354; bh=S41M8xrs0gjLpTaf2Y78ysFHAKcge7Ulwdhy/OHE+N8=;
	h=Date:From:To:Subject:From;
	b=kVB/G+GKKLHrAS99NIkJVVzUp5fFhkDT+KmoUt4RAfoInQq+gOpPDIHOoT8uo/ikU
	 5KuhVw2HYK7SKgPUi+h5okRafG+i0Y65RlM49f4k9jFoJ43oU0F3p4k3npGYsM3Tef
	 OwtYrGNA0M4+15XcBl0yKMcBSpRIFfj9hI2uoLgg=
Date:	Thu, 16 Jul 2015 13:22:34 -0400
From:	Joey Hess <j...@joeyh.name>
To:	g...@vger.kernel.org
Subject: [PATCH] support bash completion for add-on commands
Message-ID: <20150716172234.ga3...@kitenet.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.5.23 (2014-03-12)
X-Spam-Status: No, score=-3.1 required=10.0 tests=BAYES_00,DKIM_SIGNED,
	HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,
	RP_MATCHES_RCVD,T_DKIM_INVALID,URIBL_BLOCKED autolearn=ham autolearn_force=no
	version=3.4.1
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on kite.kitenet.net
Sender:	git-ow...@vger.kernel.org
Precedence: bulk
List-ID: <git.vger.kernel.org>
X-Mailing-List:	g...@vger.kernel.org
Status: RO
Content-Length: 2689
Lines: 62

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 <jo...@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
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Attachment: signature.asc
Description: PGP signature

Reply via email to