perf(git): cache the git version used for alias selection

`git version` was forked on every startup so that four aliases could be
picked according to the installed git. Cache the version in
$ZSH_CACHE_DIR for a day, keyed on the git binary's path and mtime, so a
different or upgraded git is picked up right away and the fork otherwise
happens once a day.

Measured on macOS arm64, zsh 5.9: git.plugin.zsh 9.2 ms -> 2.0 ms per
interactive start.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Robby Russell
2026-09-06 08:06:51 -07:00
co-authored by Claude Fable 5.1
parent 426650fbe8
commit 984d07a53b
+19 -1
View File
@@ -1,6 +1,24 @@
# Git version checking # Git version checking
autoload -Uz is-at-least autoload -Uz is-at-least
git_version="${${(As: :)$(git version 2>/dev/null)}[3]}" # Running `git version` forks on every startup, so cache the result for a day
# in $ZSH_CACHE_DIR, keyed on the git binary's path and mtime.
() {
local cache="$ZSH_CACHE_DIR/git-version" key
local -a stat lines
zmodload -F zsh/stat b:zstat
zstat -A stat +mtime -- "$commands[git]" 2>/dev/null
key="$commands[git] $stat[1]"
lines=("$cache"(Nm-1))
(( $#lines )) && lines=("${(@f)$(<"$cache")}")
if [[ "$lines[1]" = "$key" ]]; then
git_version="$lines[2]"
return
fi
git_version="${${(As: :)$(git version 2>/dev/null)}[3]}"
[[ ! -w "$ZSH_CACHE_DIR" ]] || print -rl -- "$key" "$git_version" >| "$cache"
}
# #
# Functions Current # Functions Current