completions/git: Remove a for-loop

This is an easy win for `git add ` completion time if we have multiple descriptions.

What happened was we did things once per description string, but the
things included a bunch of computation (including multiple `string`
calls and even a `realpath`!). Because these don't change, we can
simply do them once.

And it turns out we can just use a cartesian product:

for d in $desc
    printf '%s\t%s\n' $file $d
end

becomes

printf '%s\n' $file\t$desc
This commit is contained in:
Fabian Boehm 2023-02-06 21:22:43 +01:00
parent 96deaae7d8
commit f6b390dc61

View File

@ -324,7 +324,6 @@ function __fish_git_files
end end
# Only try printing if the file was selected. # Only try printing if the file was selected.
if set -q file[1] if set -q file[1]
for d in $desc
# Without "-z", git sometimes _quotes_ filenames. # Without "-z", git sometimes _quotes_ filenames.
# It adds quotes around it _and_ escapes the character. # It adds quotes around it _and_ escapes the character.
# e.g. `"a\\b"`. # e.g. `"a\\b"`.
@ -334,9 +333,9 @@ function __fish_git_files
set file (string trim -c \" -- $file) set file (string trim -c \" -- $file)
# The relative filename. # The relative filename.
if string match -q './*' -- (commandline -ct) if string match -q './*' -- (commandline -ct)
printf './%s\t%s\n' $file $d printf './%s\n' $file\t$desc
else else
printf '%s\t%s\n' "$file" $d printf '%s\n' "$file"\t$desc
end 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,
@ -349,8 +348,7 @@ function __fish_git_files
# If we didn't terminate it we'd have to escape any special chars # If we didn't terminate it we'd have to escape any special chars
# (non-alphanumeric, glob or regex special characters, in whatever dialect git uses) # (non-alphanumeric, glob or regex special characters, in whatever dialect git uses)
and set fromroot (string replace -- "$root/" ":/:" "$fromroot") and set fromroot (string replace -- "$root/" ":/:" "$fromroot")
and printf '%s\t%s\n' "$fromroot" $d and printf '%s\n' "$fromroot"\t$desc
end
end end
end end
end end