4

The first positional argument to git switch can be a branch, e.g. git switch <branch> and it tab completes nicely.

  1. How can I make my own command with an argument that can tab complete into a git branch?

    For git banana [--optional-option] <branch> to tab complete, it does not seem to be enough to bash builtin complete -F as that would not necessarily include "banana". And also I want to amend the completions for git, not overwrite it.

  2. Does git have some subcommand or shell functions to help with this?

    I found __gitcomp but not sure how to use it

    • together with an alias
    • specifically to trigger for the 1st positional argument

1 Answer 1

5

The standard bash-completion module shipped with Git automatically determines all available subcommands (see [1]). If you make an alias.banana, bash-completion will recognize it. If you create a git-banana script in your usual $PATH, Git itself will recognize it as a subcommand, and so will bash-completion.

For each subcommand, the bash-completion script (see [2]) checks whether a function named _git_<subcommand> is defined in the shell (not necessarily in the file but in the "live" environment). The same file has plenty of examples – you'll want to look at the definition of _git_switch, which uses __git_complete_refs to suggest completions.

~/.bashrc
_git_banana() { __git_complete_refs --mode=heads }

(Specifically for aliases, if a specific _git_<name> function does not exist, then bash-completion will actually look up the alias and will apply completion of whichever "real" Git subcommand the alias points to.)


[1] See __git_compute_all_commands in /usr/share/bash-completion/completions/git, which uses git --list-cmds=main,others,alias,nohelpers

[2] See __git_complete_command in the same file.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, how can I troubleshoot this if the autocompletion doesn't kick in? I got an executable git-review visible from my path (I can complete git revi<TAB>) but I just get regular file completion when typing git review <TAB> even though I have this function declare: _git_review() { __git_complete_refs --mode=heads; }
If you run (set -x; __git_complete_command review) do you see it calling _git_review?
I get __git_complete_command: command not found, so maybe I don't have git completions fully set up?
However declaring _git_review() { __gitcomp "a b"; } does make git review <TAB> to suggest a and b.
After doing source /usr/share/bash-completion/completions/git it works to autocomplete git review <TAB>, Thanks! :) Is it also possible to support git-review<TAB>?
They are supposed to be auto-loaded by the bash-completion framework when trying to tab-complete git something<TAB>, I don't know when exactly it triggers. (It has so many completion modules that it can't really pre-load all of them upfront, it uses a lazy-load system.) I suppose you could manually source a few individual ones that you know you'll be needing, like /usr/share/bash-completion/completions/git, from your ~/.bashrc.
(Setting PS4="+\\e[34m\${BASH_SOURCE:--}:\\e[1m\$LINENO\\e[m:\${FUNCNAME:+\\e[33m\$FUNCNAME\\e[m} " might make the -x trace output more readable.)
For standalone commands like git-review, I think you could just bind the completion function using Bash's regular complete, just pre-load (i.e. manually source from your .bashrc) the Git completion module so that all of the __git_complete* helpers will be available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.