git completion: Handle aliases referencing other aliases (#9992)

This commit is contained in:
Christian Fersch 2023-11-24 17:03:02 +01:00 committed by GitHub
parent 3c814bf53d
commit 1980a22522
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View file

@ -60,6 +60,7 @@ Completions
- ``blender`` (:issue:`9905`).
- ``gimp`` (:issue:`9904`).
- ``horcrux`` (:issue:`9922`).
- ``git`` completions now support aliases that reference other aliases (:issue:`9992`).
- ``java_home`` (:issue:`9998`).
- ``crc`` (:issue:`10034`).
- ``oc`` (:issue:`10034`).

View file

@ -637,7 +637,7 @@ end
# but a command can be aliased multiple times)
# Approximately duplicates the logic from https://github.com/git/git/blob/d486ca60a51c9cb1fe068803c3f540724e95e83a/contrib/completion/git-completion.bash#L1130
# The Git script also finds aliases that reference other aliases via a loop but this is fine for a PoC
# The bash script also finds aliases that reference other aliases via a loop but we handle that separately
function __fish_git_aliased_command
for word in (string split ' ' -- $argv)
switch $word
@ -660,6 +660,31 @@ git config -z --get-regexp 'alias\..*' | while read -lz alias cmdline
# Git aliases can contain chars that variable names can't - escape them.
set -l alias (string replace 'alias.' '' -- $alias | string escape --style=var)
set -g __fish_git_alias_$alias $command $cmdline
set --append -g __fish_git_aliases $alias
end
# Resolve aliases that call another alias
for alias in $__fish_git_aliases
set -l handled $alias
while true
set -l alias_varname __fish_git_alias_$alias
set -l aliased_command $$alias_varname[1][1]
set -l aliased_escaped (string escape --style=var -- $aliased_command)
set -l aliased_varname __fish_git_alias_$aliased_escaped
set -q $aliased_varname
or break
# stop infinite recursion
contains $aliased_escaped $handled
and break
# expand alias in cmdline
set -l aliased_cmdline $$alias_varname[1][2]
set -l aliased_cmdline (string replace " $aliased_command " " $$aliased_varname[1][2..-1] " -- " $aliased_cmdline ")
set -g $alias_varname $$aliased_varname[1][1] (string trim "$aliased_cmdline")
set --append handled $aliased_escaped
end
end
function __fish_git_using_command