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>
This commit is contained in:
Felipe Contreras 2020-11-09 20:03:42 -06:00 committed by Junio C Hamano
parent e4d83eee92
commit 9414938c34
2 changed files with 50 additions and 19 deletions

View File

@ -1120,26 +1120,38 @@ __git_pretty_aliases ()
# __git_aliased_command requires 1 argument # __git_aliased_command requires 1 argument
__git_aliased_command () __git_aliased_command ()
{ {
local word cmdline=$(__git config --get "alias.$1") local cur=$1 last word cmdline
for word in $cmdline; do
case "$word" in while [[ -n "$cur" ]]; do
\!gitk|gitk) cmdline=$(__git config --get "alias.$cur")
echo "gitk" last=$cur
return cur=
;;
\!*) : shell command alias ;; for word in $cmdline; do
-*) : option ;; case "$word" in
*=*) : setting env ;; \!gitk|gitk)
git) : git itself ;; cur="gitk"
\(\)) : skip parens of shell function definition ;; break
{) : skip start of shell helper function ;; ;;
:) : skip null command ;; \!*) : shell command alias ;;
\'*) : skip opening quote after sh -c ;; -*) : option ;;
*) *=*) : setting env ;;
echo "$word" git) : git itself ;;
return \(\)) : skip parens of shell function definition ;;
esac {) : skip start of shell helper function ;;
:) : skip null command ;;
\'*) : skip opening quote after sh -c ;;
*)
cur="$word"
break
esac
done
done done
cur=$last
if [[ "$cur" != "$1" ]]; then
echo "$cur"
fi
} }
# Check whether one of the given words is present on the command line, # Check whether one of the given words is present on the command line,

View File

@ -2195,6 +2195,25 @@ test_expect_success 'complete files' '
test_completion "git add mom" "momified" test_completion "git add mom" "momified"
' '
test_expect_success "simple alias" '
test_config alias.co checkout &&
test_completion "git co m" <<-\EOF
master Z
mybranch Z
mytag Z
EOF
'
test_expect_success "recursive alias" '
test_config alias.co checkout &&
test_config alias.cod "co --detached" &&
test_completion "git cod m" <<-\EOF
master Z
mybranch Z
mytag Z
EOF
'
test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" ' test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" && test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
test_completion "git co m" <<-\EOF test_completion "git co m" <<-\EOF