perf(init): read the OMZ revision from .git instead of forking git

`git rev-parse HEAD` was run on every startup only to stamp the
zcompdump metadata. Resolve HEAD by reading the git directory in zsh:
handles `.git` files (worktrees, submodules), worktree common dirs,
detached HEADs and packed refs, and falls back to `git rev-parse` when
anything looks unexpected.

Measured on macOS arm64, zsh 5.9: 7.4 ms -> 0.2 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:01:19 -07:00
co-authored by Claude Fable 5.1
parent 426650fbe8
commit 40323d0e37
+43 -1
View File
@@ -110,9 +110,51 @@ if [[ -z "$ZSH_COMPDUMP" ]]; then
ZSH_COMPDUMP="${ZDOTDIR:-$HOME}/.zcompdump-${SHORT_HOST}-${ZSH_VERSION}"
fi
# Resolve the commit $ZSH is checked out at into $REPLY by reading the git
# directory, so that no git process is forked on every startup.
# Handles .git files (worktrees, submodules), worktree common dirs, detached
# HEADs and packed refs. Returns 1 if anything is unexpected.
_omz_git_head() {
local gitdir="$ZSH/.git" common head ref
local -a lines
REPLY=
# .git may be a file pointing at the real git dir
if [[ -f "$gitdir" ]]; then
read -r head 2>/dev/null < "$gitdir" || return 1
gitdir="${head#gitdir: }"
[[ "$gitdir" = /* ]] || gitdir="$ZSH/$gitdir"
fi
# worktrees keep their refs in the common git dir
common="$gitdir"
if [[ -f "$gitdir/commondir" ]]; then
read -r common 2>/dev/null < "$gitdir/commondir" || return 1
[[ "$common" = /* ]] || common="$gitdir/$common"
fi
[[ -r "$gitdir/HEAD" ]] || return 1
read -r head 2>/dev/null < "$gitdir/HEAD" || return 1
# detached HEAD: the file holds the commit itself
[[ "$head" = ref:\ * ]] || { REPLY="$head"; return 0 }
ref="${head#ref: }"
if [[ -r "$common/$ref" ]]; then
read -r REPLY 2>/dev/null < "$common/$ref" && return 0
fi
[[ -r "$common/packed-refs" ]] || return 1
lines=("${(@f)$(<"$common/packed-refs")}")
REPLY="${lines[(r)* $ref]%% *}"
[[ -n "$REPLY" ]]
}
# Construct zcompdump OMZ metadata
zcompdump_revision="#omz revision: $(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
_omz_git_head || REPLY="$(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)"
zcompdump_revision="#omz revision: $REPLY"
zcompdump_fpath="#omz fpath: $fpath"
unset -f _omz_git_head
# Delete the zcompdump file if OMZ zcompdump metadata changed
if ! command grep -q -Fx "$zcompdump_revision" "$ZSH_COMPDUMP" 2>/dev/null \