completion: bash: support recursive aliases

It is possible to have recursive aliases like: l = log --oneline lg = l --graph So the completion should detect such aliases as well. Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Nov 9, 2020 at 20:03 UTC 9414938c348f47c76dcea7826ea0b22adb585300
2 files changed +50 -19
contrib/completion/git-completion.bash
+31 -19
@@ -1120,26 +1120,38 @@ __git_pretty_aliases ()
1120 # __git_aliased_command requires 1 argument
1121 __git_aliased_command ()
1122 {
1123 - local word cmdline=$(__git config --get "alias.$1")
1124 - for word in $cmdline; do
1125 - case "$word" in
1126 - \!gitk|gitk)
1127 - echo "gitk"
1128 - return
1129 - ;;
1130 - \!*) : shell command alias ;;
1131 - -*) : option ;;
1132 - *=*) : setting env ;;
1133 - git) : git itself ;;
1134 - \(\)) : skip parens of shell function definition ;;
1135 - {) : skip start of shell helper function ;;
1136 - :) : skip null command ;;
1137 - \'*) : skip opening quote after sh -c ;;
1138 - *)
1139 - echo "$word"
1140 - return
1141 - esac
1123 + local cur=$1 last word cmdline
1124 +
1125 + while [[ -n "$cur" ]]; do
1126 + cmdline=$(__git config --get "alias.$cur")
1127 + last=$cur
1128 + cur=
1129 +
1130 + for word in $cmdline; do
1131 + case "$word" in
1132 + \!gitk|gitk)
1133 + cur="gitk"
1134 + break
1135 + ;;
1136 + \!*) : shell command alias ;;
1137 + -*) : option ;;
1138 + *=*) : setting env ;;
1139 + git) : git itself ;;
1140 + \(\)) : skip parens of shell function definition ;;
1141 + {) : skip start of shell helper function ;;
1142 + :) : skip null command ;;
1143 + \'*) : skip opening quote after sh -c ;;
1144 + *)
1145 + cur="$word"
1146 + break
1147 + esac
1148 + done
1149 done
1150 +
1151 + cur=$last
1152 + if [[ "$cur" != "$1" ]]; then
1153 + echo "$cur"
1154 + fi
1155 }
1156
1157 # Check whether one of the given words is present on the command line,
t/t9902-completion.sh
+19
@@ -2195,6 +2195,25 @@ test_expect_success 'complete files' '
2195 test_completion "git add mom" "momified"
2196 '
2197
2198 +test_expect_success "simple alias" '
2199 + test_config alias.co checkout &&
2200 + test_completion "git co m" <<-\EOF
2201 + master Z
2202 + mybranch Z
2203 + mytag Z
2204 + EOF
2205 +'
2206 +
2207 +test_expect_success "recursive alias" '
2208 + test_config alias.co checkout &&
2209 + test_config alias.cod "co --detached" &&
2210 + test_completion "git cod m" <<-\EOF
2211 + master Z
2212 + mybranch Z
2213 + mytag Z
2214 + EOF
2215 +'
2216 +
2217 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2218 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2219 test_completion "git co m" <<-\EOF