completions/git: Cache subcommand v2

This is sort of slow because it's called hundreds of times.

We used to have a cache, introduced in ad9b4290e, but it was removed
in fee5a9125a because it had
false-positives.

So what we do, because the issue is that this is called hundreds of
times per-commandline, we cache it keyed on the commandline.

This speeds up `complete -C'git sta'` by a factor of 2.3x.
This commit is contained in:
Fabian Boehm 2022-06-27 17:06:44 +02:00
parent 0f5ce57ec7
commit 842af06c5d
2 changed files with 17 additions and 0 deletions

View file

@ -565,6 +565,19 @@ end
function __fish_git_needs_command
# Figure out if the current invocation already has a command.
#
# This is called hundreds of times and the argparse is kinda slow,
# so we cache it as long as the commandline doesn't change.
set -l cmdline "$(commandline -c)"
if set -q __fish_git_cmdline; and test "$cmdline" = "$__fish_git_cmdline"
if set -q __fish_git_cmd[1]
echo -- $__fish_git_cmd
return 1
end
return 0
end
set -g __fish_git_cmdline $cmdline
set -l cmd (commandline -opc)
set -e cmd[1]
argparse -s (__fish_git_global_optspecs) -- $cmd 2>/dev/null
@ -576,9 +589,11 @@ function __fish_git_needs_command
set -q _flag_info_path; and return 1
if set -q argv[1]
# Also print the command, so this can be used to figure out what it is.
set -g __fish_git_cmd $argv[1]
echo $argv[1]
return 1
end
set -g __fish_git_cmd
return 0
end

View file

@ -139,3 +139,5 @@ end
$fish -c 'complete -C "git -C ./.gi"'
# CHECK: ./.git/ Directory
$fish -c 'complete -C "git diff -c"'