2017-04-06 08:30:28 -04:00
|
|
|
# test if git completion is missing, but loader exists, attempt to load
|
2021-02-07 17:30:27 -05:00
|
|
|
if ! declare -F _git > /dev/null && ! declare -F __git_wrap__git_main > /dev/null; then
|
|
|
|
if declare -F _completion_loader > /dev/null; then
|
|
|
|
_completion_loader git
|
|
|
|
fi
|
2017-04-06 08:30:28 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# only operate if git completion is present
|
2021-02-07 17:30:27 -05:00
|
|
|
if declare -F _git > /dev/null || declare -F __git_wrap__git_main > /dev/null; then
|
2017-04-06 08:30:28 -04:00
|
|
|
|
|
|
|
_yadm() {
|
|
|
|
|
|
|
|
local current=${COMP_WORDS[COMP_CWORD]}
|
2017-05-07 16:43:10 -04:00
|
|
|
local penultimate
|
2017-05-08 17:30:30 -04:00
|
|
|
if [ "$((COMP_CWORD-1))" -ge "0" ]; then
|
2017-05-07 16:43:10 -04:00
|
|
|
penultimate=${COMP_WORDS[COMP_CWORD-1]}
|
|
|
|
fi
|
|
|
|
local antepenultimate
|
2017-05-08 17:30:30 -04:00
|
|
|
if [ "$((COMP_CWORD-2))" -ge "0" ]; then
|
2017-05-07 16:43:10 -04:00
|
|
|
antepenultimate=${COMP_WORDS[COMP_CWORD-2]}
|
|
|
|
fi
|
2017-04-06 08:30:28 -04:00
|
|
|
|
2019-01-04 21:26:47 -05:00
|
|
|
local -x GIT_DIR
|
2017-04-06 08:30:28 -04:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
GIT_DIR="$(yadm introspect repo 2>/dev/null)"
|
|
|
|
|
|
|
|
case "$penultimate" in
|
|
|
|
bootstrap)
|
|
|
|
COMPREPLY=()
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
config)
|
|
|
|
COMPREPLY=( $(compgen -W "$(yadm introspect configs 2>/dev/null)") )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
decrypt)
|
|
|
|
COMPREPLY=( $(compgen -W "-l" -- "$current") )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
init)
|
|
|
|
COMPREPLY=( $(compgen -W "-f -w" -- "$current") )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
introspect)
|
|
|
|
COMPREPLY=( $(compgen -W "commands configs repo switches" -- "$current") )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
help)
|
|
|
|
COMPREPLY=() # no specific help yet
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
list)
|
|
|
|
COMPREPLY=( $(compgen -W "-a" -- "$current") )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case "$antepenultimate" in
|
|
|
|
clone)
|
2019-10-07 09:36:32 -04:00
|
|
|
COMPREPLY=( $(compgen -W "-f -w -b --bootstrap --no-bootstrap" -- "$current") )
|
2017-04-06 08:30:28 -04:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2019-01-04 21:17:34 -05:00
|
|
|
local yadm_switches=( $(yadm introspect switches 2>/dev/null) )
|
|
|
|
|
2017-04-06 08:30:28 -04:00
|
|
|
# this condition is so files are completed properly for --yadm-xxx options
|
2019-01-04 21:17:34 -05:00
|
|
|
if [[ " ${yadm_switches[*]} " != *" $penultimate "* ]]; then
|
2017-04-06 08:30:28 -04:00
|
|
|
# TODO: somehow solve the problem with [--yadm-xxx option] being
|
|
|
|
# incompatible with what git expects, namely [--arg=option]
|
2021-02-07 17:30:27 -05:00
|
|
|
if declare -F _git > /dev/null; then
|
|
|
|
_git
|
|
|
|
else
|
|
|
|
__git_wrap__git_main
|
|
|
|
fi
|
2017-04-06 08:30:28 -04:00
|
|
|
fi
|
|
|
|
if [[ "$current" =~ ^- ]]; then
|
|
|
|
local matching
|
2019-01-04 21:17:34 -05:00
|
|
|
matching=$(compgen -W "${yadm_switches[*]}" -- "$current")
|
2017-04-06 08:30:28 -04:00
|
|
|
__gitcompappend "$matching"
|
|
|
|
fi
|
|
|
|
|
2019-01-04 21:44:49 -05:00
|
|
|
# Find the index of where the sub-command argument should go.
|
|
|
|
local command_idx
|
|
|
|
for (( command_idx=1 ; command_idx < ${#COMP_WORDS[@]} ; command_idx++ )); do
|
|
|
|
local command_idx_arg="${COMP_WORDS[$command_idx]}"
|
|
|
|
if [[ " ${yadm_switches[*]} " = *" $command_idx_arg "* ]]; then
|
|
|
|
let command_idx++
|
|
|
|
elif [[ "$command_idx_arg" = -* ]]; then
|
|
|
|
:
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ "$COMP_CWORD" = "$command_idx" ]]; then
|
2017-04-06 08:30:28 -04:00
|
|
|
local matching
|
|
|
|
matching=$(compgen -W "$(yadm introspect commands 2>/dev/null)" -- "$current")
|
|
|
|
__gitcompappend "$matching"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# remove duplicates found in COMPREPLY (a native bash way could be better)
|
|
|
|
if [ -n "${COMPREPLY[*]}" ]; then
|
|
|
|
COMPREPLY=($(echo "${COMPREPLY[@]}" | sort -u))
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -o bashdefault -o default -F _yadm yadm 2>/dev/null \
|
|
|
|
|| complete -o default -F _yadm yadm
|
|
|
|
|
|
|
|
fi
|