Skip to content

Commit a521b60

Browse files
committed
completion/system: accomodate multiple versions
For Homebrew, switch between v1 and v2 of bash-completion based on whether the running Bash shell is new enough to use v2.
1 parent 5474fab commit a521b60

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

completion/available/system.completion.bash

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
if shopt -qo nounset
77
then # Bash-completion is too large and complex to expect to handle unbound variables throughout the whole codebase.
8-
BASH_IT_RESTORE_NOUNSET=true
8+
__bash_it_restore_nounset=true
99
shopt -uo nounset
1010
else
11-
BASH_IT_RESTORE_NOUNSET=false
11+
__bash_it_restore_nounset=false
1212
fi
1313

1414
if [[ -r "${BASH_COMPLETION:-}" ]] ; then
15+
# shellcheck disable=SC1091
1516
source "${BASH_COMPLETION}"
17+
1618
elif [[ -r /etc/bash_completion ]] ; then
1719
# shellcheck disable=SC1091
1820
source /etc/bash_completion
@@ -23,17 +25,37 @@ elif [[ -r /etc/profile.d/bash_completion.sh ]] ; then
2325
source /etc/profile.d/bash_completion.sh
2426

2527
elif [[ $OSTYPE == 'darwin'* ]] && _command_exists brew ; then
26-
BREW_PREFIX=${BREW_PREFIX:-$(brew --prefix)}
28+
: ${BREW_PREFIX:-$(brew --prefix)}
29+
: ${BASH_COMPLETION_COMPAT_DIR:=$BREW_PREFIX/etc/bash_completion.d}
2730

28-
# homebrew/versions/bash-completion2 (required for projects.completion.bash) is installed to this path
29-
if [[ -r "$BREW_PREFIX"/etc/bash_completion ]] ; then
30-
# shellcheck disable=SC1090
31-
source "$BREW_PREFIX"/etc/bash_completion
32-
fi
31+
case "${BASH_VERSION}" in
32+
1*|2*|3.0*|3.1*)
33+
_log_warning "Cannot load completion due to version of shell."
34+
;;
35+
3.2*|4.0*|4.1*)
36+
# Import version 1.x of bash-completion, if installed.
37+
BASH_COMPLETION="$BREW_PREFIX/opt/bash-completion@1/etc/bash_completion"
38+
if [[ -r "$BASH_COMPLETION" ]] ; then
39+
# shellcheck disable=SC1090
40+
source "$BASH_COMPLETION"
41+
else
42+
unset BASH_COMPLETION
43+
fi
44+
;;
45+
4.2*|5*|*)
46+
# homebrew/versions/bash-completion2 (required for projects.completion.bash) is installed to this path
47+
if [[ -r "$BREW_PREFIX/opt/bash-completion@2/etc/profile.d/bash_completion.sh" ]] ; then
48+
# shellcheck disable=SC1090
49+
source "$BREW_PREFIX/opt/bash-completion@2/etc/profile.d/bash_completion.sh"
50+
else
51+
unset BASH_COMPLETION
52+
fi
53+
;;
54+
esac
3355
fi
3456

35-
if $BASH_IT_RESTORE_NOUNSET
57+
if $__bash_it_restore_nounset
3658
then
3759
shopt -so nounset
3860
fi
39-
unset BASH_IT_RESTORE_NOUNSET
61+
unset __bash_it_restore_nounset

0 commit comments

Comments
 (0)