Files
nixos-configs/hosts/pinwheel/modules/work/wt.zsh
T
Alexander Heldt bebeaeb2e1 pinwheel: Skip worktrees that conflict in wt sync
gt restack stops at a merge conflict mid-rebase, so wt sync left that
worktree half rebased. It now aborts the restack with `gt abort --force`
(without --force it waits for a confirmation prompt), leaving the
worktree as it was, carries on with the rest, and lists the conflicted
worktrees apart from those that failed for other reasons. Restacks run
with --no-interactive so a prompt cannot stall the loop.
2026-11-25 15:30:28 +01:00

148 lines
5.3 KiB
Bash

# Git worktrees beside the primary checkout, one per branch: ~/code/work/backend's live in
# ~/code/work/backend.worktrees/<branch>.
wt() {
if [[ $1 == -h || $1 == --help ]]; then
print -rl -- \
'usage: wt list worktrees' \
' wt <branch> cd to the worktree that has <branch>, adding one if none has it' \
' wt -c <branch> 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 [<branch>] remove <branch>'s worktree, or the current one; the branch stays" \
' wt sync [<args>] gt sync <args> in the primary checkout, then gt restack in every' \
' worktree not on main whose PR is not merged; one that conflicts' \
' is left as it was and listed at the end'
return
fi
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
;;
sync)
gt --cwd "$primary" sync "${@:2}" || return
git -C "$primary" worktree prune
local line branch merged failed=() conflicts=()
merged=(${(f)"$(jq -r '.prInfos[] | select(.state == "MERGED") | .headRefName' \
"$primary/.git/.graphite_pr_info" 2>/dev/null)"})
for line in "${(@f)$(git worktree list --porcelain)}"; do
case $line in
'worktree '*) dir=${line#worktree } ;;
'branch refs/heads/main') ;;
'branch '*)
branch=${line#branch refs/heads/}
if (( ${merged[(Ie)$branch]} )); then
print -r -- "${dir:t}: merged, not restacked"
else
print -r -- "${dir:t}:"
if ! gt --cwd "$dir" restack --no-interactive; then
if [[ -d $(git -C "$dir" rev-parse --path-format=absolute --git-path rebase-merge) ]]; then
gt --cwd "$dir" abort --force --no-interactive >/dev/null
print -r -- "wt: aborted, ${dir:t} is left as it was"
conflicts+=("$dir")
else
failed+=("$dir")
fi
fi
fi
;;
esac
done
if (( $#conflicts )); then
print -u2 -l "wt: merge conflicts, not restacked; run gt restack in:" $conflicts
fi
if (( $#failed )); then
print -u2 -l "wt: gt restack failed in:" $failed
fi
(( $#conflicts + $#failed == 0 ))
;;
-c)
[[ -n $2 ]] || { print -u2 'usage: wt -c <branch>'; 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"
;;
-*)
print -u2 "wt: unknown option $1, see wt --help"
return 2
;;
*)
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 reply
if [[ -f $dir/.envrc ]]; then
if git -C "$dir" diff --quiet main -- .envrc; then
direnv allow "$dir"
else
print -r -- "wt: $dir/.envrc differs from main's"
if ! git -C "$dir" diff --quiet main...HEAD -- .envrc; then
print -r -- "wt: this branch changes it:"
git --no-pager -C "$dir" diff main...HEAD -- .envrc
fi
if [[ -n $(git -C "$dir" log --oneline HEAD..main -- .envrc) ]]; then
print -r -- "wt: main has changed it since this branch was made:"
git --no-pager -C "$dir" log --oneline HEAD..main -- .envrc
fi
read -q "reply?wt: allow it? [y/N] "
print
[[ $reply == y ]] && direnv allow "$dir"
fi
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
if [[ -f $primary/.bsp/pants.json && -f $dir/pants.toml ]]; then
print -r -- "wt: writing .bsp/pants.json for Metals"
(cd "$dir" && direnv exec . pants --no-pantsd experimental-bsp) ||
print -u2 "wt: no .bsp/pants.json, so Metals will set up scala-cli here; run" \
"pants --no-pantsd experimental-bsp in the devshell before opening a Scala file"
fi
}
_wt() {
compadd -- ${(f)"$(git for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null)"}
}
compdef _wt wt