perf: avoid listing modified files twice in checkout-file (#546)

_forgit_checkout_file called _forgit_list_modified_files once to check for
an empty result and again to feed fzf, running git diff twice on every
invocation. Cache the result and reuse it for both checks.
This commit is contained in:
Chris Apple 2026-07-15 15:06:19 -07:00 committed by GitHub
parent 9fa93b0861
commit 15db001662
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1123,12 +1123,13 @@ _forgit_git_checkout_file() {
# git checkout-file selector
_forgit_checkout_file() {
_forgit_inside_work_tree || return 1
local files opts
local files opts modified_files
_forgit_contains_non_flags "$@" && {
_forgit_git_checkout_file "$@"
return $?
}
[[ $(_forgit_list_modified_files | wc -l) -eq 0 ]] && echo 'Nothing to checkout.' && return 1
modified_files="$(_forgit_list_modified_files)"
[[ -z $modified_files ]] && echo 'Nothing to checkout.' && return 1
opts="
$FORGIT_FZF_DEFAULT_OPTS
-m -0
@ -1138,7 +1139,7 @@ _forgit_checkout_file() {
files=()
while IFS='' read -r file; do
files+=("$file")
done < <(_forgit_list_modified_files |
done < <(printf '%s\n' "$modified_files" |
FZF_DEFAULT_OPTS="$opts" fzf)
[[ ${#files[@]} -gt 0 ]] && _forgit_git_checkout_file "$@" "${files[@]}"
}