git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v3 0/1] completion: dynamic completion loading
@ 2018-04-29 16:42 Florian Gamböck
  2018-04-29 16:42 ` [PATCH v3 1/1] completion: load completion file for external subcommand Florian Gamböck
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Gamböck @ 2018-04-29 16:42 UTC (permalink / raw)
  To: git; +Cc: Szeder Gábor, Junio C Hamano, Stefan Beller

In this small patch I want to introduce a way to dynamically load completion 
scripts for external subcommands.

A few years ago, you would put a completion script (which defines a Bash 
function _git_foo for a custom git subcommand foo) into 
/etc/bash_completion.d, or save it somewhere in your $HOME and source it 
manually in your .bashrc.

Starting with bash-completion v2.0 (or, to be absolutely exact, the preview 
versions started at v1.90), completion scripts are not sourced at bash startup 
anymore. Rather, when typing in a command and trigger completion by pressing 
the TAB key, the new bash-completion's main script looks for a script with the 
same name as the typed command in the completion directory, sources it, and 
provides the completions defined in this script.

However, that only works for top level commands. After a completion script has 
been found, the logic is delegated to this script. This means, that the 
completion of subcommands must be handled by the corresponding completion 
script.

As it is now, git is perfectly able to call custom completion functions, iff 
they are already defined when calling the git completion. With my patch, git 
uses an already defined loader function of bash-completion (or continues 
silently if this function is not found), loads an external completion script, 
and provides its completions.

An example for an external completion script would be 
/usr/share/bash-completion/completions/git-foo for a git subcommand foo.

Changes since v2:

-   Replaced __load_completion with _completion_loader, which was introduced 
    way before the former and should therefor be available even in older 
    distributions

-   Updated commit message with explanations from Szeder Gábor

Changes since v1 (RFC):

-   Re-arranged if-fi statement to increase readability (suggested by Junio C 
    Hamano)

-   Re-worded commit message to include the exakt version of bashcomp that 
    introduced dynamic loading (suggested by Stefan Beller)

Florian Gamböck (1):
  completion: load completion file for external subcommand

 contrib/completion/git-completion.bash | 10 ++++++++++
 1 file changed, 10 insertions(+)

-- 
2.16.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v3 1/1] completion: load completion file for external subcommand
  2018-04-29 16:42 [PATCH v3 0/1] completion: dynamic completion loading Florian Gamböck
@ 2018-04-29 16:42 ` Florian Gamböck
  2018-05-07 10:39   ` SZEDER Gábor
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Gamböck @ 2018-04-29 16:42 UTC (permalink / raw)
  To: git; +Cc: Szeder Gábor, Junio C Hamano, Stefan Beller

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 since v1.90 (preview of 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 was defined by
bash-completion v1.90, namely _completion_loader. It will take care of
loading the correct script if present. Afterwards, the git completion
script behaves as usual.

_completion_loader was introduced in commit 20c05b43 of bash-completion
(https://github.com/scop/bash-completion.git) back in 2011, so it should
be available in even older LTS distributions. This function searches for
external completion scripts not only in the default path
/usr/share/bash-completion/completions, but also in the user's home
directory via $XDG_DATA_HOME and in a user specified directory via
$BASH_COMPLETION_USER_DIR.

The only "drawback" (if it even can be called as such) is, that if
_completion_loader does not find a completion script, it automatically
registers a minimal function for basic path completion. In practice,
however, this will not matter, because in this case the given command is
a git command in its dashed form, e.g. 'git-diff-index', and those have
been deprecated for a long time.

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 | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b09c8a236..604ba2b03 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3096,12 +3096,22 @@ __git_main ()
 	fi
 
 	local completion_func="_git_${command//-/_}"
+	if ! declare -f $completion_func >/dev/null 2>/dev/null &&
+		declare -f _completion_loader >/dev/null 2>/dev/null
+	then
+		_completion_loader "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 &&
+			declare -f _completion_loader >/dev/null 2>/dev/null
+		then
+			_completion_loader "git-$expansion"
+		fi
 		declare -f $completion_func >/dev/null 2>/dev/null && $completion_func
 	fi
 }
-- 
2.16.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3 1/1] completion: load completion file for external subcommand
  2018-04-29 16:42 ` [PATCH v3 1/1] completion: load completion file for external subcommand Florian Gamböck
@ 2018-05-07 10:39   ` SZEDER Gábor
  0 siblings, 0 replies; 3+ messages in thread
From: SZEDER Gábor @ 2018-05-07 10:39 UTC (permalink / raw)
  To: Git mailing list, Szeder Gábor, Junio C Hamano,
	Stefan Beller

On Sun, Apr 29, 2018 at 6:42 PM, Florian Gamböck <mail@floga.de> wrote:
> This way we can leverage bash-completion's dynamic loading for git
> subcommands and make it easier for developers to distribute custom
> completion scripts.

The patch and the updated commit message both look good to me.
Thanks for following through.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-05-07 10:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-29 16:42 [PATCH v3 0/1] completion: dynamic completion loading Florian Gamböck
2018-04-29 16:42 ` [PATCH v3 1/1] completion: load completion file for external subcommand Florian Gamböck
2018-05-07 10:39   ` SZEDER Gábor

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).