completions/git: Don't check commandline so much

This just caches some checks, speeding up `git add ` completions by
~33% with 4000 matching files.
This commit is contained in:
Fabian Boehm 2023-07-13 16:31:33 +02:00
parent a6c36a014c
commit 1f1975689e

View file

@ -183,6 +183,13 @@ function __fish_git_files
# We explicitly enable globs so we can use that to match the current token. # We explicitly enable globs so we can use that to match the current token.
set -l git_opt -c status.relativePaths -c core.quotePath= set -l git_opt -c status.relativePaths -c core.quotePath=
string match -q './*' -- (commandline -ct)
and set -l rel ./
or set -l rel
string match -q ':*' -- (commandline -ct)
and set -l colon 1
or set -l colon
# We pick the v2 format if we can, because it shows relative filenames (if used without "-z"). # We pick the v2 format if we can, because it shows relative filenames (if used without "-z").
# We fall back on the v1 format by reading git's _version_, because trying v2 first is too slow. # We fall back on the v1 format by reading git's _version_, because trying v2 first is too slow.
set -l ver (__fish_git --version | string replace -rf 'git version (\d+)\.(\d+)\.?.*' '$1\n$2') set -l ver (__fish_git --version | string replace -rf 'git version (\d+)\.(\d+)\.?.*' '$1\n$2')
@ -332,16 +339,11 @@ function __fish_git_files
# there is nothing we can do, but that's a general issue with scripted completions. # there is nothing we can do, but that's a general issue with scripted completions.
set file (string trim -c \" -- $file) set file (string trim -c \" -- $file)
# The relative filename. # The relative filename.
if string match -q './*' -- (commandline -ct) printf "$rel%s\n" "$file"\t$desc
printf './%s\n' $file\t$desc
else
printf '%s\n' "$file"\t$desc
end
# Now from repo root. # Now from repo root.
# Only do this if the filename isn't a simple child, # Only do this if the filename isn't a simple child,
# or the current token starts with ":" # or the current token starts with ":"
if string match -q '../*' -- $file if set -ql colon[1]; or string match -q '../*' -- $file
or string match -q ':*' -- (commandline -ct)
set -l fromroot (builtin realpath -- $file 2>/dev/null) set -l fromroot (builtin realpath -- $file 2>/dev/null)
# `:` starts pathspec "magic", and the second `:` terminates it. # `:` starts pathspec "magic", and the second `:` terminates it.
# `/` is the magic letter for "from repo root". # `/` is the magic letter for "from repo root".
@ -481,13 +483,12 @@ function __fish_git_files
set -a file (string join / -- $previous) set -a file (string join / -- $previous)
# The filename with ":/:" prepended. # The filename with ":/:" prepended.
if string match -q '../*' -- $file if set -ql colon[1]; or string match -q '../*' -- $file
or string match -q ':*' -- (commandline -ct)
set file (string replace -- "$root/" ":/:" "$root/$relfile") set file (string replace -- "$root/" ":/:" "$root/$relfile")
end end
if test "$root/$relfile" -ef "$relfile" if test "$root/$relfile" -ef "$relfile"
and not string match -q ':*' -- (commandline -ct) and not set -ql colon[1]
set file $relfile set file $relfile
end end