From 426d29bbd47e4b7214c7131e944004cc070561b7 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Sun, 22 Nov 2026 18:25:33 +0100 Subject: [PATCH] pinwheel: Add `wt` for git worktrees beside the primary checkout `wt ` goes to the worktree holding a branch, creating one in `.worktrees/` if needed; `wt -c` starts a new Graphite-tracked branch off main in its own worktree; `wt rm` removes one and keeps the branch. A new worktree gets `direnv allow` only when its `.envrc` matches the primary's. In backend it also gets a `.pants.rc` pinning `find_links` to the primary's `dist/`: `pants.toml` sets it to the checkout's own path, which put the path into every `requirements.pex` cache key and made each worktree rebuild them. No lockfile resolves from it. --- hosts/pinwheel/modules/work/default.nix | 2 + hosts/pinwheel/modules/work/wt.zsh | 77 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 hosts/pinwheel/modules/work/wt.zsh diff --git a/hosts/pinwheel/modules/work/default.nix b/hosts/pinwheel/modules/work/default.nix index 83be827..d179692 100644 --- a/hosts/pinwheel/modules/work/default.nix +++ b/hosts/pinwheel/modules/work/default.nix @@ -40,6 +40,8 @@ in autoload -Uz add-zsh-hook add-zsh-hook precmd _nix_direnv_bridge_hook + + source ${./wt.zsh} ''; # Configure IntelliJ to exclude .direnv from indexing diff --git a/hosts/pinwheel/modules/work/wt.zsh b/hosts/pinwheel/modules/work/wt.zsh new file mode 100644 index 0000000..3fdef9a --- /dev/null +++ b/hosts/pinwheel/modules/work/wt.zsh @@ -0,0 +1,77 @@ +# Git worktrees beside the primary checkout, one per branch: ~/code/work/backend's live in +# ~/code/work/backend.worktrees/. +# +# wt list worktrees +# wt cd to the worktree that has , adding one if none has it +# wt -c new branch off main, tracked by gt, in a new worktree; make its first +# commit with `gt modify -a`, since `gt create` would stack a second branch +# wt rm [] remove 's worktree, or the current one; the branch stays +wt() { + local primary dir + primary=$(git rev-parse --path-format=absolute --git-common-dir) || return + primary=${primary:h} + + case $1 in + '') + git worktree list + ;; + rm) + if [[ -n $2 ]]; then + dir=$(_wt_dir "$2") + [[ -n $dir ]] || { print -u2 "wt: no worktree has $2 checked out"; return 1 } + else + dir=$(git rev-parse --show-toplevel) || return + fi + if [[ $dir == "$primary" ]]; then + print -u2 "wt: $dir is the primary checkout" + return 1 + fi + git -C "$primary" worktree remove "$dir" || return + if [[ $PWD/ == "$dir"/* ]]; then + cd "$primary" + fi + ;; + -c) + [[ -n $2 ]] || { print -u2 'usage: wt -c '; return 2 } + dir=$primary.worktrees/${2//\//-} + git -C "$primary" worktree add -b "$2" "$dir" main && + gt --cwd "$dir" track --parent main "$2" && + _wt_setup "$primary" "$dir" && + cd "$dir" + ;; + *) + dir=$(_wt_dir "$1") + if [[ -z $dir ]]; then + dir=$primary.worktrees/${1//\//-} + git -C "$primary" worktree add "$dir" "$1" && _wt_setup "$primary" "$dir" || return + fi + cd "$dir" + ;; + esac +} + +_wt_dir() { + local line dir + for line in "${(@f)$(git worktree list --porcelain)}"; do + case $line in + 'worktree '*) dir=${line#worktree } ;; + "branch refs/heads/$1") print -r -- "$dir"; return ;; + esac + done +} + +_wt_setup() { + local primary=$1 dir=$2 + if [[ -f $dir/.envrc && -f $primary/.envrc && "$(<$dir/.envrc)" == "$(<$primary/.envrc)" ]]; then + direnv allow "$dir" + fi + if grep -qF 'file://%(buildroot)s/dist/' "$dir/pants.toml" 2>/dev/null; then + print -r -- "[python-repos] +find_links = [\"file://$primary/dist/\"]" >"$dir/.pants.rc" + fi +} + +_wt() { + compadd -- ${(f)"$(git for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)"} +} +compdef _wt wt