commit 0f8fdbe1fda1579ec0debaba9d2f5f13e1ef9efa
parent a090c4e5483e37e7dadaaf3e561a7d1373cf329e
Author: Chris Bracken <chris@bracken.jp>
Date: Sun, 7 Jun 2026 09:03:36 +0900
zsh: optimise prompt vcs check some more
On further consideration, in the case of a non-repo prompt, we're always
paying the cost of both the jj and git checks so it's probably worth
optimising the git check too.
Diffstat:
1 file changed, 24 insertions(+), 6 deletions(-)
diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc
@@ -48,17 +48,35 @@ if [[ -s $zcompdump && (! -s ${zcompdump}.zwc || $zcompdump -nt ${zcompdump}.zwc
fi
unset zcompdump
-# Current VCS branch (for prompt).
+# Current VCS branch (for prompt). Pure-shell, no forks: walks up from $PWD
+# looking for .jj or .git, and reads .git/HEAD directly for the branch name.
vcs_branch() {
- # Walk up looking for .jj — avoids forking jj just to ask "are we in one?"
local d=$PWD
while [[ $d != / ]]; do
- [[ -d $d/.jj ]] && { echo -n " ◉ "; return }
+ if [[ -d $d/.jj ]]; then
+ echo -n " ◉ "
+ return
+ fi
+ if [[ -e $d/.git ]]; then
+ local gitdir=$d/.git
+ # Worktrees / submodules: .git is a file containing "gitdir: <path>".
+ if [[ -f $gitdir ]]; then
+ local content=$(<$gitdir)
+ gitdir=${content#gitdir: }
+ [[ $gitdir == /* ]] || gitdir=$d/$gitdir
+ fi
+ local head
+ [[ -r $gitdir/HEAD ]] && head=$(<$gitdir/HEAD)
+ if [[ $head == ref:* ]]; then
+ local ref=${head#ref: }
+ echo -n " (${ref#refs/heads/})"
+ elif [[ -n $head ]]; then
+ echo -n " (${head[1,7]})"
+ fi
+ return
+ fi
d=${d:h}
done
- local branch
- branch="$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)"
- [[ -n $branch ]] && echo -n " ($branch)"
}
# Prompt.