From 32197c1b396e9349dec870d552e08879126284ab Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 31 May 2026 20:58:51 +0200 Subject: [PATCH] tooling: add agent-worktree.sh for per-agent worktree isolation Copied from sibling Hassio projects and referenced by AGENTS.md's standard flow. The script is generic (relative .worktree paths, master base, agent/ branches), so no path or project-name changes were needed. Co-authored-by: Cursor --- scripts/agent-worktree.sh | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 scripts/agent-worktree.sh diff --git a/scripts/agent-worktree.sh b/scripts/agent-worktree.sh new file mode 100755 index 0000000..ad58281 --- /dev/null +++ b/scripts/agent-worktree.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +set -euo pipefail + +usage() { + echo "Usage:" + echo " $0 create [base-branch]" + echo " $0 list" + echo " $0 remove " + exit 1 +} + +sanitize_topic() { + local topic="${1:-}" + if [[ -z "$topic" ]]; then + usage + fi + echo "$topic" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd 'a-z0-9._-' +} + +cmd="${1:-}" +case "$cmd" in + create) + topic="$(sanitize_topic "${2:-}")" + base="${3:-master}" + branch="agent/${topic}" + wt_root=".worktree" + wt_path="${wt_root}/${topic}" + + mkdir -p "$wt_root" + + if [[ -e "$wt_path" ]]; then + echo "Worktree path already exists: $wt_path" + exit 1 + fi + + git fetch --all --prune + git worktree add "$wt_path" -b "$branch" "$base" + { + echo "owner=${USER:-unknown}" + echo "task=${topic}" + echo "branch=${branch}" + date -u +"created_at=%Y-%m-%dT%H:%M:%SZ" + } > "${wt_path}/.agent-lock" + echo "Created worktree: $wt_path" + echo "Branch: $branch" + ;; + list) + git worktree list + ;; + remove) + topic="$(sanitize_topic "${2:-}")" + wt_path=".worktree/${topic}" + if [[ ! -d "$wt_path" ]]; then + echo "Worktree path not found: $wt_path" + exit 1 + fi + git worktree remove "$wt_path" + echo "Removed worktree: $wt_path" + ;; + *) + usage + ;; +esac