Skip to content

Commit fd6445a

Browse files
committed
Merge branch 'fc/bash-completion-alias-of-alias'
The command line completion script (in contrib/) learned to expand commands that are alias of alias. * fc/bash-completion-alias-of-alias: completion: bash: improve alias loop detection completion: bash: check for alias loop completion: bash: support recursive aliases
2 parents b291b0a + e4c75ed commit fd6445a

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

contrib/completion/git-completion.bash

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,26 +1120,44 @@ __git_pretty_aliases ()
11201120
# __git_aliased_command requires 1 argument
11211121
__git_aliased_command ()
11221122
{
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"
1123+
local cur=$1 last list word cmdline
1124+
1125+
while [[ -n "$cur" ]]; do
1126+
if [[ "$list" == *" $cur "* ]]; then
1127+
# loop detected
11401128
return
1141-
esac
1129+
fi
1130+
1131+
cmdline=$(__git config --get "alias.$cur")
1132+
list=" $cur $list"
1133+
last=$cur
1134+
cur=
1135+
1136+
for word in $cmdline; do
1137+
case "$word" in
1138+
\!gitk|gitk)
1139+
cur="gitk"
1140+
break
1141+
;;
1142+
\!*) : shell command alias ;;
1143+
-*) : option ;;
1144+
*=*) : setting env ;;
1145+
git) : git itself ;;
1146+
\(\)) : skip parens of shell function definition ;;
1147+
{) : skip start of shell helper function ;;
1148+
:) : skip null command ;;
1149+
\'*) : skip opening quote after sh -c ;;
1150+
*)
1151+
cur="$word"
1152+
break
1153+
esac
1154+
done
11421155
done
1156+
1157+
cur=$last
1158+
if [[ "$cur" != "$1" ]]; then
1159+
echo "$cur"
1160+
fi
11431161
}
11441162

11451163
# Check whether one of the given words is present on the command line,

t/t9902-completion.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,25 @@ test_expect_success 'complete files' '
21952195
test_completion "git add mom" "momified"
21962196
'
21972197

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+
21982217
test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
21992218
test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
22002219
test_completion "git co m" <<-\EOF

0 commit comments

Comments
 (0)