From 02efe052e4fd186b36ffb130f6cb357f080513ce Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Mon, 26 Oct 2026 01:29:44 +0100 Subject: [PATCH] dev: worktrees live in .git/wt, with a helper to create and clean them Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 6 +++++ scripts/dev/worktree.sh | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 scripts/dev/worktree.sh diff --git a/AGENTS.md b/AGENTS.md index b3cf54ef..07087ccb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -31,4 +31,10 @@ reference (the "Original"); the product is the Rust/CUDA port of its Original mo `docs/deploy.md`, `docs/roofline.md`); the RTX 3080 laptop is a secondary, experimental target (`docs/laptop-profile.md`). * The port is complete for the 5090 serve path; open work is listed in `docs/port-remaining.md` and the ledger. Nothing is merged to `main` yet. +## Worktrees + +- Create worktrees inside the main repository's git directory, never as sibling folders of `~/work`: `scripts/dev/worktree.sh new []` makes `.git/wt/`. The files are ordinary; only the location is hidden from the file listing. +- Clean up when a branch has been merged: `scripts/dev/worktree.sh clean` lists the clean, merged worktrees and `--yes` removes them. Uncommitted changes block the removal; commit them to the branch first. Do this at the end of every task that created a worktree and before starting a new batch, so no more than a handful exist at a time. +- Build output (`target/`, `dist/`) lives in the worktree and goes with it; a removed worktree never deletes its branch. + Read `engine/strata/AGENTS.md` before modifying imported engine code. diff --git a/scripts/dev/worktree.sh b/scripts/dev/worktree.sh new file mode 100644 index 00000000..1c112f53 --- /dev/null +++ b/scripts/dev/worktree.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Git worktrees of this repository live inside the main repository's .git directory (.git/wt/), so ~/work stays one folder per project. +# +# scripts/dev/worktree.sh new [ []] create .git/wt/ on a new or existing branch +# scripts/dev/worktree.sh list every worktree with its state: dirty / merged into +# scripts/dev/worktree.sh clean [--base ] [--yes] remove the worktrees that are clean and merged into +# +# `clean` never deletes work: a worktree with uncommitted changes is skipped, and removing one only deletes its directory, never its branch. +# Build outputs (target/) inside a removed worktree go with it; they are regenerable. +set -euo pipefail + +main=$(cd "$(git rev-parse --git-common-dir)/.." && pwd) +wt_root="$main/.git/wt" +base=claude/port-gpu-bringup + +cmd=${1:-list} +shift || true + +case "$cmd" in + new) + name=${1:?usage: worktree.sh new [ []]} + branch=${2:-claude/$name} + start=${3:-HEAD} + mkdir -p "$wt_root" + if git show-ref --verify --quiet "refs/heads/$branch"; then + git worktree add "$wt_root/$name" "$branch" + else + git worktree add -b "$branch" "$wt_root/$name" "$start" + fi + echo "worktree: $wt_root/$name ($branch)" + ;; + list | clean) + yes=0 + while [ $# -gt 0 ]; do + case "$1" in + --base) base=$2; shift 2 ;; + --yes) yes=1; shift ;; + *) echo "unknown argument: $1" >&2; exit 2 ;; + esac + done + git worktree list --porcelain | awk '/^worktree /{w=$2} /^branch /{print w" "$2} /^detached/{print w" DETACHED"}' | while read -r w b; do + [ "$w" = "$main" ] && continue + dirty=$(git -C "$w" status --porcelain 2>/dev/null | grep -vc '^$' || true) + if [ "$b" = DETACHED ]; then merged=detached; else git merge-base --is-ancestor "${b#refs/heads/}" "$base" 2>/dev/null && merged=merged || merged=unmerged; fi + echo "$w ${b#refs/heads/} dirty=$dirty $merged" + if [ "$cmd" = clean ] && [ "$dirty" = 0 ] && { [ "$merged" = merged ] || [ "$merged" = detached ]; }; then + if [ "$yes" = 1 ]; then git worktree remove "$w" && echo " removed"; else echo " would remove (pass --yes)"; fi + fi + done + git worktree prune + ;; + *) + echo "usage: worktree.sh new|list|clean" >&2 + exit 2 + ;; +esac