#!/usr/bin/env bash set -euo pipefail usage() { cat <<'EOF' usage: scripts/worktree-start [--dry-run] [--allow-unlinked] [start-ref] Create an issue-linked git worktree under branches/. Branch names must be traceable to an issue (or a PR, for review branches): implementation: (fix|feat|docs|chore)/issue-- review: review/pr-- Use --allow-unlinked to bypass the check (discouraged). Examples: scripts/worktree-start fix/issue-123-example scripts/worktree-start --dry-run review/pr-456-scope-check prgs/master EOF } dry_run=0 allow_unlinked=0 while [[ "${1:-}" == --* ]]; do case "$1" in --dry-run) dry_run=1 ;; --allow-unlinked) allow_unlinked=1 ;; --help) usage; exit 0 ;; *) usage >&2; exit 2 ;; esac shift done if [[ $# -lt 1 || $# -gt 2 ]]; then usage >&2 exit 2 fi branch="$1" start_ref="${2:-prgs/master}" # Enforce issue-linked, traceable branch names (issue → branch → worktree → PR). if [[ "$allow_unlinked" -eq 0 ]]; then if [[ "$branch" =~ ^(fix|feat|docs|chore)/issue-[0-9]+-.+ ]] \ || [[ "$branch" =~ ^review/pr-[0-9]+-.+ ]]; then : else cat >&2 <- Review branches: review/pr-- Fix the branch name, or pass --allow-unlinked to override (discouraged). EOF exit 2 fi fi script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "$script_dir/.." && pwd)" worktree_name="${branch//\//-}" worktree_path="$repo_root/branches/$worktree_name" if [[ -e "$worktree_path" ]]; then cat >&2 <