Skip to content

Commit 9414938

Browse files
felipecgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4d83ee commit 9414938

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

contrib/completion/git-completion.bash

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,26 +1120,38 @@ __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"
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
11421149
done
1150+
1151+
cur=$last
1152+
if [[ "$cur" != "$1" ]]; then
1153+
echo "$cur"
1154+
fi
11431155
}
11441156

11451157
# 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)