completions/git: define function before use

This commit is contained in:
Johannes Altmanninger
2021-07-12 23:41:28 +02:00
parent e3d8b315ed
commit e50805646e
2 changed files with 14 additions and 9 deletions

View File

@@ -593,13 +593,6 @@ end
# So instead, we store the aliases in global variables, named after the alias, containing the command.
# This is because alias:command is an n:1 mapping (an alias can only have one corresponding command,
# but a command can be aliased multiple times)
git config -z --get-regexp 'alias\..*' | while read -lz alias cmdline
set -l command (__fish_git_aliased_command $cmdline)
string match -q --regex '\w+' -- $command; or continue
# 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
end
# 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
@@ -607,9 +600,9 @@ function __fish_git_aliased_command
for word in (string split ' ' -- $argv)
switch $word
case !gitk gitk
echo "gitk"
echo gitk
return
# Adding " to the list
# Adding " to the list
case '!*' '-*' '*=*' git '()' '{' : '\'*' '"*'
continue
case '*'
@@ -619,6 +612,14 @@ function __fish_git_aliased_command
end
end
git config -z --get-regexp 'alias\..*' | while read -lz alias cmdline
set -l command (__fish_git_aliased_command $cmdline)
string match -q --regex '\w+' -- $command; or continue
# 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
end
function __fish_git_using_command
set -l cmd (__fish_git_needs_command)
test -z "$cmd"

View File

@@ -36,6 +36,10 @@ touch foo
complete -C'git add '
#CHECK: foo Untracked file
git config alias.s status
complete 'git s --s'
# CHECK --short
# Note: We can't rely on the initial branch because that might be
# "master", or it could be changed to something else in future!
git checkout -b newbranch >/dev/null 2>&1