1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2025-06-04 08:33:57 +00:00

Fix handling of filenames with space in bash completion (#341)

By calling __gitcompappend with the same suffix as the completion for git
does (i.e. " ") we can remove the call to sort -u as it is no longer
needed (bash seems to remove duplicates) and then spaces are handled correctly.

Also make the completion available in testhost.
This commit is contained in:
Erik Flodin 2025-03-03 21:36:36 +01:00
parent bee1558a4e
commit 2ac90b004c
No known key found for this signature in database
GPG Key ID: 420A7C865EE3F85F
2 changed files with 29 additions and 39 deletions

View File

@ -123,6 +123,7 @@ testhost: require-docker .testyadm
--hostname testhost \
--rm -it \
-v "$(CURDIR)/.testyadm:/bin/yadm:ro" \
-v "$(CURDIR)/completion/bash/yadm:/usr/share/bash-completion/completions/yadm:ro" \
$(IMAGE) \
bash -l

View File

@ -12,16 +12,15 @@ if declare -F _git > /dev/null || declare -F __git_wrap__git_main > /dev/null; t
local current=${COMP_WORDS[COMP_CWORD]}
local penultimate
if [ "$((COMP_CWORD-1))" -ge "0" ]; then
if ((COMP_CWORD >= 1)); then
penultimate=${COMP_WORDS[COMP_CWORD - 1]}
fi
local antepenultimate
if [ "$((COMP_CWORD-2))" -ge "0" ]; then
if ((COMP_CWORD >= 2)); then
antepenultimate=${COMP_WORDS[COMP_CWORD - 2]}
fi
local -x GIT_DIR
# shellcheck disable=SC2034
GIT_DIR="$(yadm introspect repo 2>/dev/null)"
case "$penultimate" in
@ -75,9 +74,7 @@ if declare -F _git > /dev/null || declare -F __git_wrap__git_main > /dev/null; t
fi
fi
if [[ "$current" =~ ^- ]]; then
local matching
matching=$(compgen -W "${yadm_switches[*]}" -- "$current")
__gitcompappend "$matching"
__gitcompappend "${yadm_switches[*]}" "" "$current" " "
fi
# Find the index of where the sub-command argument should go.
@ -93,19 +90,11 @@ if declare -F _git > /dev/null || declare -F __git_wrap__git_main > /dev/null; t
fi
done
if [[ "$COMP_CWORD" = "$command_idx" ]]; then
local matching
matching=$(compgen -W "$(yadm introspect commands 2>/dev/null)" -- "$current")
__gitcompappend "$matching"
__gitcompappend "$(yadm introspect commands 2>/dev/null)" "" "$current" " "
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
complete -o bashdefault -o default -o nospace -F _yadm yadm 2>/dev/null ||
complete -o default -o nospace -F _yadm yadm
fi