Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 544722 - dev-vcs/git: bash-completion autoloading not work with dev-vcs/git-flow
Summary: dev-vcs/git: bash-completion autoloading not work with dev-vcs/git-flow
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Robin Johnson
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 557884
  Show dependency tree
 
Reported: 2015-03-28 08:08 UTC by Oleg Ageev
Modified: 2019-04-21 01:20 UTC (History)
5 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
emerge --info (file_544722.txt,5.27 KB, text/plain)
2015-03-29 13:26 UTC, Oleg Ageev
Details
Patch to load completion files of git subcommands (git-dynamical-completion.patch,765 bytes, patch)
2015-06-30 22:17 UTC, Florian Gamböck
Details | Diff
Patch to load completion files of git subcommands, v2 (git-dynamical-completion-v2.patch,892 bytes, patch)
2015-07-02 08:21 UTC, Florian Gamböck
Details | Diff
Patch to load completion files of git subcommands, v3 (git-dynamical-completion-v3.patch,928 bytes, patch)
2018-04-08 20:13 UTC, Florian Gamböck
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Oleg Ageev 2015-03-28 08:08:33 UTC
Bash completion of git-flow not works if try:
$ git flow ...
and not works if try;
$ git-flow ...
but if try this
$ git flow ...
completion will work

Reproducible: Always
Comment 1 Andrew Savchenko gentoo-dev 2015-03-29 07:43:51 UTC
Please always add emerge --info to your bug reports.
Comment 2 Oleg Ageev 2015-03-29 13:26:24 UTC
Created attachment 400022 [details]
emerge --info
Comment 3 Florian Gamböck 2015-06-13 09:26:30 UTC
This "problem" is still present as of dev-vcs/git-flow-1.8.0 and app-shells/bash-completion-2.1_p20141224.

Due to the way bash-completion works now (dynamically loading needed completions) and "git-flow" being a separate completion script, there is no way for the dynamic loader to recognize the need of the git-flow completions.

The phenomenon Oleg describes here is easily explained:

$ git flow <TAB>
Does not the correct completions, because "git-flow" hasn't been loaded yet.

$ git-flow <TAB> # note the - instead of a whitespace
Also does no correct completion, because this command doesn't define completions. BUT: The dynamic loader has finally loaded "git-flow" now, due to recognizing the command and loading the completion with the same name!

$ git flow <TAB>
Now the completions work as intended, since the correct script has been loaded!

A simple workaround, without digging deep into the bash-completion internals, would be to simply symlink the git-flow script into the compatibility folder, like the old folks did:

# ln -sv /usr/share/bash-completion/completions/git-flow /etc/bash_completion.d/
This way, the git-flow completions are always loaded regardless of being needed or not.

Perhaps someone with more knowledge about the way bash-completion works nowadays can come up with a strategy to load such subcommands dynamically, too.
Comment 4 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2015-06-13 13:54:46 UTC
As I see it, the 'git' completion would have to have some extended support for loading sub-completions, i.e. recognize unknown 'flow' subcommand and try to load 'git-flow'.
Comment 5 Florian Gamböck 2015-06-14 10:17:20 UTC
Actually the git completion is rather smart anyway. If you type:

    $ git foo <TAB>

Then git automatically uses the bash function _git_foo() if defined. The only problem is, how should git know whether _git_foo() is in another file which is not loaded yet? Is git even capable of finding that out?

Or can we just say: If "git-foo" completion file exists in the same folder as "git", then source it before using _git_foo().

That would be one or two additional lines in the git completions file. That can be done via a patch that is applied in the ebuild.
Comment 6 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2015-06-14 15:32:41 UTC
What bash-completion itself does is: it checks whether the function exists, and loads the file if it doesn't. So you'd like to do the same -- check if function exists, if it doesn't, check if file exists and source it.
Comment 7 Florian Gamböck 2015-06-15 09:25:09 UTC
Where should we look for these subcommand completion files? Hardcoded in /usr/share/bash-completion/completions? Or is there an automatic handling of multiple directories, like custom completions in $HOME/.bash_completion.d or something like that?

This approach may quickly escalate to re-implementing bash-completion into the git completion script.

Is there a way for the main bash-completion loader to test if there is a file $command-$subcommand, even if the $command completion function already was assigned to $command?
Comment 8 Florian Gamböck 2015-06-30 22:17:58 UTC
Created attachment 406004 [details, diff]
Patch to load completion files of git subcommands

So for starters, how about the attached patch to the git completion file? It looks for a file called git-$subcommand in the same directory as the main git completion script and sources it if it exists, before attempting to use the related completion function.
Comment 9 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2015-07-01 04:39:36 UTC
Yeah, something like this. Though it'd be nice if you checked for the completion function existence (via declare -f) before sourcing the file again and again, and again... :) It would be good to cache 'file not found' case too.
Comment 10 Florian Gamböck 2015-07-02 08:21:42 UTC
Created attachment 406054 [details, diff]
Patch to load completion files of git subcommands, v2

Okay, I re-arranged the lines a bit. The related files are sourced only if there is no function already present. That means, an additional completion file is sourced only once per terminal.

You mentioned "caching of non-existent files", but actually I can't think of a proper way to do that. We could pass environment variables around like GIT_COMPL_NOT_FOUND="file1 file2" or writing those files to a tempfile that we would have to read each time. But is this really necessary in favor of a simple "test -f" that gets called on every try anyway?

Furthermore I don't see bash_completion doing it either, so maybe they were caught by the exact same thoughts on how to pass those missing files around. I would consider this a "nice-to-have" but not exactly "vital" for the moment.
Comment 11 Justin Lecher (RETIRED) gentoo-dev 2015-08-11 08:38:16 UTC
(In reply to Florian Gamböck from comment #10)
> Created attachment 406054 [details, diff] [details, diff]
> Patch to load completion files of git subcommands, v2

That patch work for me.
Comment 12 Justin Lecher (RETIRED) gentoo-dev 2015-09-02 07:30:17 UTC
Why is this package stabilized when functionality is impaired?
Comment 13 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2017-07-02 20:12:04 UTC
The git completion is provided by dev-vcs/git, so there's nothing for bash-completion maintainers to do here. Reassigning.
Comment 14 Florian Gamböck 2018-04-08 20:13:07 UTC
Created attachment 526872 [details, diff]
Patch to load completion files of git subcommands, v3

I changed the loading process by using the __load_completion function of bash-completion. This function automatically searches the correct completion script in the standard directories of bash-completion or fails silently if not found.

Also, I finally posted the patch to the mailing list.

https://public-inbox.org/git/20180408182552.26289-1-mail@floga.de/
Comment 15 Florian Gamböck 2019-04-07 16:37:25 UTC
This problem does no longer exist with >=dev-vcs/git-2.18. The completion for git-flow (and in fact completion scripts for every other non-canonical git subcommand) will be sourced automatically if needed by the completion script of git.