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/<topic> branches), so no path or project-name changes were needed. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Executable
+63
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
usage() {
|
||||
echo "Usage:"
|
||||
echo " $0 create <topic> [base-branch]"
|
||||
echo " $0 list"
|
||||
echo " $0 remove <topic>"
|
||||
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
|
||||
Reference in New Issue
Block a user