|
1 |
| -#!/usr/bin/env bash |
2 |
| - |
3 |
| -# Only operate on MacOS since there are no linux paths |
4 |
| -if [[ "$OSTYPE" != 'darwin'* ]] ; then |
5 |
| - _log_warning "unsupported operating system - only 'Darwin' is supported" |
6 |
| - return 0 |
7 |
| -fi |
| 1 | +# shellcheck shell=bash |
| 2 | +# |
| 3 | +# Locate and load completions for `git`. |
8 | 4 |
|
9 | 5 | # Make sure git is installed
|
10 |
| -_command_exists git || return 0 |
| 6 | +_command_exists git || return |
11 | 7 |
|
12 | 8 | # Don't handle completion if it's already managed
|
13 |
| -if complete -p git &>/dev/null ; then |
14 |
| - _log_warning "completion already loaded - this usually means it is safe to stop using this completion" |
15 |
| - return 0 |
| 9 | +if complete -p git &> /dev/null; then |
| 10 | + _log_warning "completion already loaded - this usually means it is safe to stop using this completion" |
| 11 | + return 0 |
16 | 12 | fi
|
17 | 13 |
|
18 |
| -_git_bash_completion_found=false |
| 14 | +_git_bash_completion_xcrun_git= |
| 15 | +if _command_exists xcrun; then |
| 16 | + _git_bash_completion_xcrun_git="$(xcrun --find git)" |
| 17 | +fi |
19 | 18 | _git_bash_completion_paths=(
|
20 |
| - # MacOS non-system locations |
21 |
| - '/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash' |
22 |
| - '/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash' |
| 19 | + # Standard locations |
| 20 | + "${GIT_EXE%/*}/../share/git-core/git-completion.bash" |
| 21 | + "${GIT_EXE%/*}/../share/git-core/contrib/completion/git-completion.bash" |
| 22 | + "${GIT_EXE%/*}/../etc/bash_completion.d/git-completion.bash" |
| 23 | + # MacOS non-system locations |
| 24 | + "${_git_bash_completion_xcrun_git%/bin/git}/share/git-core/git-completion.bash" |
23 | 25 | )
|
24 | 26 |
|
25 | 27 | # Load the first completion file found
|
26 |
| -for _comp_path in "${_git_bash_completion_paths[@]}" ; do |
27 |
| - if [[ -r "$_comp_path" ]] ; then |
28 |
| - _git_bash_completion_found=true |
29 |
| - source "$_comp_path" |
30 |
| - break |
31 |
| - fi |
| 28 | +_git_bash_completion_found=false |
| 29 | +for _comp_path in "${_git_bash_completion_paths[@]}"; do |
| 30 | + if [[ -r "$_comp_path" ]]; then |
| 31 | + _git_bash_completion_found=true |
| 32 | + # shellcheck disable=SC1090 # don't follow |
| 33 | + source "$_comp_path" |
| 34 | + break |
| 35 | + fi |
32 | 36 | done
|
33 | 37 |
|
34 | 38 | # Cleanup
|
35 | 39 | if [[ "${_git_bash_completion_found}" == false ]]; then
|
36 |
| - _log_warning "no completion files found - please try enabling the 'system' completion instead." |
| 40 | + _log_warning "no completion files found - please try enabling the 'system' completion instead." |
37 | 41 | fi
|
38 |
| -unset _git_bash_completion_paths |
39 |
| -unset _git_bash_completion_found |
| 42 | +unset "${!_git_bash_completion@}" |
0 commit comments