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
This commit is contained in:
Junio C Hamano
2020-11-25 15:24:51 -08:00
2 changed files with 55 additions and 18 deletions

View File

@ -1120,12 +1120,24 @@ __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 list word cmdline
while [[ -n "$cur" ]]; do
if [[ "$list" == *" $cur "* ]]; then
# loop detected
return
fi
cmdline=$(__git config --get "alias.$cur")
list=" $cur $list"
last=$cur
cur=
for word in $cmdline; do for word in $cmdline; do
case "$word" in case "$word" in
\!gitk|gitk) \!gitk|gitk)
echo "gitk" cur="gitk"
return break
;; ;;
\!*) : shell command alias ;; \!*) : shell command alias ;;
-*) : option ;; -*) : option ;;
@ -1136,10 +1148,16 @@ __git_aliased_command ()
:) : skip null command ;; :) : skip null command ;;
\'*) : skip opening quote after sh -c ;; \'*) : skip opening quote after sh -c ;;
*) *)
echo "$word" cur="$word"
return break
esac 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