Skip to content

Commit 7b326c7

Browse files
committed
bash completion: add support for git 2.30 and on
The _git backwards compat wrapper was dropped upstream: git/git@441ecda Instead, we rely on __git_complete to detect loaded bash completions, since we will use it later on to set up completions -- it is now public API. There is a gap between git 2.30 and 2.31 where _git does not exist, but there is no public API to create completions. Starting 2.31, we are formally permitted to copy/imitate the upstream completions with: ``` __git_complete mycmd git_cmd ``` For 2.30 specifically, we have to pass the internal completion function used instead of "git_cmd", but it's difficult to detect this necessity in a forwards-compatible way. Try it that way first, in the assumption that if the internal completion function still exists it does the same thing.
1 parent 5c547ed commit 7b326c7

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

etc/hub.bash_completion.sh

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# This script complements the completion script that ships with git.
33

44
# If there is no git tab completion, but we have the _completion loader try to load it
5-
if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then
5+
if ! declare -F __git_complete > /dev/null && declare -F _completion_loader > /dev/null; then
66
_completion_loader git
77
fi
88

99
# Check that git tab completion is available and we haven't already set up completion
10-
if declare -F _git > /dev/null && ! declare -F __git_list_all_commands_without_hub > /dev/null; then
10+
if declare -F __git_complete > /dev/null && ! declare -F __git_list_all_commands_without_hub > /dev/null; then
1111
# Duplicate and rename the 'list_all_commands' function
1212
eval "$(declare -f __git_list_all_commands | \
1313
sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"
@@ -382,6 +382,15 @@ EOF
382382
}
383383

384384
# Enable completion for hub even when not using the alias
385-
complete -o bashdefault -o default -o nospace -F _git hub 2>/dev/null \
386-
|| complete -o default -o nospace -F _git hub
385+
if declare -F _git >/dev/null; then
386+
# git < 2.30
387+
complete -o bashdefault -o default -o nospace -F _git hub 2>/dev/null \
388+
|| complete -o default -o nospace -F _git hub
389+
elif declare -F __git_main; then
390+
# in git 2.30 both __git_complete and __git_main are private
391+
__git_complete hub __git_main
392+
else
393+
# git > 2.31 if __git_main is removed
394+
__git_complete hub git
395+
fi
387396
fi

0 commit comments

Comments
 (0)