From 1980a225223bc8bd1ba65b5013a8b4a3e16cf096 Mon Sep 17 00:00:00 2001 From: Christian Fersch Date: Fri, 24 Nov 2023 17:03:02 +0100 Subject: [PATCH] git completion: Handle aliases referencing other aliases (#9992) --- CHANGELOG.rst | 1 + share/completions/git.fish | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5b02e42b1..a765ff55c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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`). diff --git a/share/completions/git.fish b/share/completions/git.fish index 90221ed52..9b850bc94 100644 --- a/share/completions/git.fish +++ b/share/completions/git.fish @@ -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