Reputation: 2091
In my .bash_profile
, I have a lot of functional shortcuts for git. For example:
function gitpull() {
branch="$1"
if [[ -z $branch ]]; then
current_branch=`git symbolic-ref -q --short HEAD`
git pull origin $current_branch;
elif [[ -n $branch && $branch == "m" ]]; then
git pull origin master;
else
git pull origin $branch;
fi;
}
However, when I'm typing this in the terminal, I want it to autocomplete git branches. How do I do this? (I already am using .git-completion.bash
)
Upvotes: 13
Views: 1995
Reputation: 1328712
With Git 2.31 (Q1 2021), the bash completion (in contrib/
) is updated to make it easier for end-users to add completion for their custom "git
" subcommands.
Thank to FelipeC (who wrote a previous answer on that same page)
See commit 5a067ba, commit 0e02bdc, commit 810df0e, commit 7f94b78 (30 Dec 2020) by Felipe Contreras (felipec
).
(Merged by Junio C Hamano -- gitster
-- in commit f9fb906, 15 Jan 2021)
completion
: add proper public __git_completeSigned-off-by: Felipe Contreras
When
__git_complete
was introduced, it was meant to be temporarily, while a proper guideline for public shell functions was established (tentatively_GIT_complete
), but since that never happened, people in the wild started to use__git_complete
, even though it was marked as not public.Eight years is more than enough wait, let's mark this function as public, and make it a bit more user-friendly.
So that instead of doing:
__git_complete gk __gitk_main
The user can do:
__git_complete gk gitk
And instead of:
__git_complete gf _git_fetch
Do:
__git_complete gf git_fetch
Backwards compatibility is maintained.
Upvotes: 1
Reputation: 9498
The recommended method is by using __git_complete()
:
__git_complete gitpull _git_pull
Upvotes: 4
Reputation: 18697
Manually-crafted bash completion is as simple as this:
# our handler that returns choices by populating Bash array COMPREPLY
# (filtered by the currently entered word ($2) via compgen builtin)
_gitpull_complete() {
branches=$(git branch -l | cut -c3-)
COMPREPLY=($(compgen -W "$branches" -- "$2"))
}
# we now register our handler to provide completion hints for the "gitpull" command
complete -F _gitpull_complete gitpull
After sourcing the above commands:
$ gitpull <TAB>
asd master qwe zxc
$ gitpull m<TAB>
$ gitpull master
The ultimate reference on bash completion is (of course) the section on Programmable Completion in the bash manual, but a nice introduction is given on "Debian Administration" page (part 1 and a more important part 2).
Upvotes: 14