Files
Agent-System/scripts/agent-worktree.sh
T
weichenandCursor 43c972d0ad docs(agents): multi-agent rules, templates, and tool entrypoints
Rewrite AGENTS.md with taxonomy, worktree, and integration slot rules;
add FEATURE-MATRIX-driven CURRENT; tool stubs; session-close checklist.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 02:31:27 +00:00

77 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Agent worktree helper — parallel coding only; do NOT start docker from worktrees.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
WORKTREES="${ROOT}/.worktrees"
usage() {
cat <<EOF
用法:
$0 create <branch-name> # git worktree add .worktrees/<slug> -b <branch>
$0 list # git worktree list
$0 remove <branch-name> # remove worktree and optionally delete branch
示例:
$0 create feature/my-task
$0 list
$0 remove feature/my-task
EOF
exit 1
}
ensure_ignored() {
if git -C "${ROOT}" check-ignore -q .worktrees/. 2>/dev/null; then
:
else
echo "[错误] .worktrees/ 未在 .gitignore 中,请先添加。" >&2
exit 1
fi
mkdir -p "${WORKTREES}"
}
slug_from_branch() {
echo "$1" | tr '/' '-'
}
cmd="${1:-}"
case "${cmd}" in
create)
[[ $# -eq 2 ]] || usage
branch="$2"
ensure_ignored
slug="$(slug_from_branch "${branch}")"
path="${WORKTREES}/${slug}"
if [[ -d "${path}" ]]; then
echo "[错误] 已存在: ${path}" >&2
exit 1
fi
git -C "${ROOT}" worktree add "${path}" -b "${branch}"
echo "Worktree: ${path} (branch ${branch})"
echo "禁止在此目录运行 env-service.sh start/stop/restart"
;;
list)
git -C "${ROOT}" worktree list
;;
remove)
[[ $# -ge 2 && $# -le 3 ]] || usage
branch="$2"
delete_branch="${3:-}"
slug="$(slug_from_branch "${branch}")"
path="${WORKTREES}/${slug}"
if [[ ! -d "${path}" ]]; then
echo "[错误] 不存在: ${path}" >&2
exit 1
fi
git -C "${ROOT}" worktree remove "${path}" --force
if [[ "${delete_branch}" == "--delete-branch" ]]; then
git -C "${ROOT}" branch -D "${branch}" 2>/dev/null || true
fi
echo "已移除 worktree ${path}"
;;
*)
usage
;;
esac