From 60e0fbbf427d78d102fadd83336b6af24e720dae Mon Sep 17 00:00:00 2001 From: David Mandelberg Date: Fri, 4 Jan 2019 21:17:34 -0500 Subject: [PATCH 1/3] Fix completion after a command-line flag. Before: yadm checkout -f # Completes filenames. yadm checkout --yadm-dir # Completes filenames. After: yadm checkout -f # Completes branch names. yadm checkout --yadm-dir # Completes filenames. --- completion/yadm.bash_completion | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/completion/yadm.bash_completion b/completion/yadm.bash_completion index 1091e55..34f199f 100644 --- a/completion/yadm.bash_completion +++ b/completion/yadm.bash_completion @@ -60,15 +60,17 @@ if declare -F _git > /dev/null; then ;; esac + local yadm_switches=( $(yadm introspect switches 2>/dev/null) ) + # this condition is so files are completed properly for --yadm-xxx options - if [[ ! "$penultimate" =~ ^- ]]; then + if [[ " ${yadm_switches[*]} " != *" $penultimate "* ]]; then # TODO: somehow solve the problem with [--yadm-xxx option] being # incompatible with what git expects, namely [--arg=option] _git fi if [[ "$current" =~ ^- ]]; then local matching - matching=$(compgen -W "$(yadm introspect switches 2>/dev/null)" -- "$current") + matching=$(compgen -W "${yadm_switches[*]}" -- "$current") __gitcompappend "$matching" fi From 5d9e0a7133647b6b7fadc2a5261fc0c4030dc4da Mon Sep 17 00:00:00 2001 From: David Mandelberg Date: Fri, 4 Jan 2019 21:26:47 -0500 Subject: [PATCH 2/3] Mark GIT_DIR for export. Before: yadm push # Completes filenames. After: yadm push # Completes names of git remotes. --- completion/yadm.bash_completion | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completion/yadm.bash_completion b/completion/yadm.bash_completion index 34f199f..60a71ff 100644 --- a/completion/yadm.bash_completion +++ b/completion/yadm.bash_completion @@ -18,7 +18,7 @@ if declare -F _git > /dev/null; then antepenultimate=${COMP_WORDS[COMP_CWORD-2]} fi - local GIT_DIR + local -x GIT_DIR # shellcheck disable=SC2034 GIT_DIR="$(yadm introspect repo 2>/dev/null)" From bcf6531da68dcc14c6f5716bd1af4e454cb91157 Mon Sep 17 00:00:00 2001 From: David Mandelberg Date: Fri, 4 Jan 2019 21:44:49 -0500 Subject: [PATCH 3/3] Only add yadm commands to the completion list when applicable. Before: yadm # Completes git and yadm commands. yadm -Y . # Completes yadm commands. yadm p -u origin foo # Completes yadm+git commands like p*. yadm push -u origin # Completes branch names and yadm commands. After: yadm # Completes git and yadm commands. yadm -Y . # Completes yadm commands. yadm p -u origin foo # Completes yadm+git commands like p*. yadm push -u origin # Completes branch names. --- completion/yadm.bash_completion | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/completion/yadm.bash_completion b/completion/yadm.bash_completion index 60a71ff..228bc34 100644 --- a/completion/yadm.bash_completion +++ b/completion/yadm.bash_completion @@ -74,7 +74,19 @@ if declare -F _git > /dev/null; then __gitcompappend "$matching" fi - if [ "$COMP_CWORD" == 1 ] || [[ "$antepenultimate" =~ ^- ]] ; then + # 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 local matching matching=$(compgen -W "$(yadm introspect commands 2>/dev/null)" -- "$current") __gitcompappend "$matching"