@@ -6,38 +6,37 @@ give_comps() {
66 echo $' # Modifying it manually is not recommended'
77 echo $' '
88 echo $' _mygit_completions_filter() {'
9- echo $' local words="$1" '
9+ echo $' local words=("$@") '
1010 echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1111 echo $' local result=()'
12+ echo $' local want_options=0'
1213 echo $' '
1314 echo $' # words the user already typed (excluding the command itself)'
1415 echo $' local used=()'
1516 echo $' if ((COMP_CWORD > 1)); then'
1617 echo $' used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
1718 echo $' fi'
1819 echo $' '
19- echo $' if [[ "${cur:0:1}" == "-" ]]; then'
20- echo $' # Completing an option: offer everything (including options)'
21- echo $' echo "$words"'
22- echo $' '
23- echo $' else'
24- echo $' # Completing a non-option: offer only non-options,'
25- echo $' # and don\' t re-offer ones already used earlier in the line.'
26- echo $' for word in $words; do'
20+ echo $' # Completing an option: offer everything.'
21+ echo $' # Completing a non-option: drop options and already-used words.'
22+ echo $' [[ "${cur:0:1}" == "-" ]] && want_options=1'
23+ echo $' for word in "${words[@]}"; do'
24+ echo $' if ((!want_options)); then'
2725 echo $' [[ "${word:0:1}" == "-" ]] && continue'
2826 echo $' '
29- echo $' local seen=0'
3027 echo $' for u in "${used[@]}"; do'
3128 echo $' if [[ "$u" == "$word" ]]; then'
32- echo $' seen=1'
33- echo $' break'
29+ echo $' continue 2'
3430 echo $' fi'
3531 echo $' done'
36- echo $' ((!seen)) && result+=("$word")'
37- echo $' done'
32+ echo $' fi'
3833 echo $' '
39- echo $' echo "${result[*]}"'
40- echo $' fi'
34+ echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
35+ echo $' printf -v word \' %q\' "$word"'
36+ echo $' result+=("$word")'
37+ echo $' done'
38+ echo $' '
39+ echo $' echo "${result[*]}"'
4140 echo $' }'
4241 echo $' '
4342 echo $' _mygit_completions() {'
@@ -60,15 +59,15 @@ give_comps() {
6059 echo $' ;;'
6160 echo $' '
6261 echo $' \' status\' *)'
63- echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help --verbose --branch -b")" -- "$cur")'
62+ echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help" " --verbose" " --branch" " -b")" -- "$cur")'
6463 echo $' ;;'
6564 echo $' '
6665 echo $' \' init\' *)'
6766 echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")'
6867 echo $' ;;'
6968 echo $' '
7069 echo $' *)'
71- echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h -v --help --version init status")" -- "$cur")'
70+ echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h" "-v" " --help" " --version" " init" " status")" -- "$cur")'
7271 echo $' ;;'
7372 echo $' '
7473 echo $' esac'
0 commit comments