Skip to content

Commit 15db001

Browse files
authored
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.
1 parent 9fa93b0 commit 15db001

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

bin/git-forgit

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,12 +1123,13 @@ _forgit_git_checkout_file() {
11231123
# git checkout-file selector
11241124
_forgit_checkout_file() {
11251125
_forgit_inside_work_tree || return 1
1126-
local files opts
1126+
local files opts modified_files
11271127
_forgit_contains_non_flags "$@" && {
11281128
_forgit_git_checkout_file "$@"
11291129
return $?
11301130
}
1131-
[[ $(_forgit_list_modified_files | wc -l) -eq 0 ]] && echo 'Nothing to checkout.' && return 1
1131+
modified_files="$(_forgit_list_modified_files)"
1132+
[[ -z $modified_files ]] && echo 'Nothing to checkout.' && return 1
11321133
opts="
11331134
$FORGIT_FZF_DEFAULT_OPTS
11341135
-m -0
@@ -1138,7 +1139,7 @@ _forgit_checkout_file() {
11381139
files=()
11391140
while IFS='' read -r file; do
11401141
files+=("$file")
1141-
done < <(_forgit_list_modified_files |
1142+
done < <(printf '%s\n' "$modified_files" |
11421143
FZF_DEFAULT_OPTS="$opts" fzf)
11431144
[[ ${#files[@]} -gt 0 ]] && _forgit_git_checkout_file "$@" "${files[@]}"
11441145
}

0 commit comments

Comments
 (0)