#!/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 -c core.autocrlf=false worktree add "$wt_root/$name" "$branch" else # core.autocrlf=false: with a global autocrlf=true (Windows Git) the checkout would turn the sealed PTX and the scripts into CRLF git -c core.autocrlf=false 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 /{n++; w=$2} n>1 && /^branch /{print w" "$2} n>1 && /^detached/{print w" DETACHED"}' | while read -r w b; do 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