From 15db0016623b87472c0b6ff7488b123a74b82c7e Mon Sep 17 00:00:00 2001 From: Chris Apple Date: Wed, 15 Jul 2026 15:06:19 -0700 Subject: [PATCH] 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. --- bin/git-forgit | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/git-forgit b/bin/git-forgit index 9315a0c..58ce16e 100755 --- a/bin/git-forgit +++ b/bin/git-forgit @@ -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[@]}" }