completion: bash: improve alias loop detection

It is possible for the name of an alias to end with the name of another alias, in which case the code will incorrectly detect a loop. We can fix that by adding an extra space between words. Suggested-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Nov 12, 2020 at 16:34 UTC e4c75edb52acab8c90520ceedab4326b972cc0af
1 file changed +5 -4
contrib/completion/git-completion.bash
+5 -4
@@ -1120,16 +1120,17 @@ __git_pretty_aliases ()
1120 # __git_aliased_command requires 1 argument
1121 __git_aliased_command ()
1122 {
1123 - local cur=$1 list word cmdline
1123 + local cur=$1 last list word cmdline
1124
1125 while [[ -n "$cur" ]]; do
1126 - if [[ "$list" == *"$cur "* ]]; then
1126 + if [[ "$list" == *" $cur "* ]]; then
1127 # loop detected
1128 return
1129 fi
1130
1131 cmdline=$(__git config --get "alias.$cur")
1132 - list="$cur $list"
1132 + list=" $cur $list"
1133 + last=$cur
1134 cur=
1135
1136 for word in $cmdline; do
@@ -1153,7 +1154,7 @@ __git_aliased_command ()
1154 done
1155 done
1156
1156 - cur="${list%% *}"
1157 + cur=$last
1158 if [[ "$cur" != "$1" ]]; then
1159 echo "$cur"
1160 fi