#!/bin/bash # Create a git worktree for feature isolation # Usage: worktree-create.sh set -e FEATURE_NAME="$1" if [ -z "$FEATURE_NAME" ]; then echo "Error: Feature name required" echo "Usage: worktree-create.sh " exit 1 fi # Get project root from current working directory (where Claude Code is running) # Note: This script is called from ${CLAUDE_PLUGIN_ROOT}/scripts/ but operates on the project PROJECT_ROOT="$(pwd)" PROJECT_NAME=$(basename "$PROJECT_ROOT") # Define paths BRANCH_NAME="feature/ab-${FEATURE_NAME}" WORKTREES_DIR="$(dirname "$PROJECT_ROOT")/ab-worktrees" WORKTREE_PATH="${WORKTREES_DIR}/${PROJECT_NAME}-${FEATURE_NAME}" echo "========================================" echo " Creating Git Worktree" echo "========================================" echo "" echo "Feature: $FEATURE_NAME" echo "Branch: $BRANCH_NAME" echo "Path: $WORKTREE_PATH" echo "" # Check if we're in a git repository if ! git -C "$PROJECT_ROOT" rev-parse --git-dir > /dev/null 2>&1; then echo "Error: Not a git repository" exit 1 fi # Check for uncommitted changes if ! git -C "$PROJECT_ROOT" diff-index --quiet HEAD -- 2>/dev/null; then echo "Warning: You have uncommitted changes in the main repository" echo "" fi # Create worktrees directory if needed mkdir -p "$WORKTREES_DIR" # Create branch if it doesn't exist cd "$PROJECT_ROOT" if git show-ref --verify --quiet "refs/heads/${BRANCH_NAME}"; then echo "Branch already exists: ${BRANCH_NAME}" else echo "Creating branch: ${BRANCH_NAME}" git branch "${BRANCH_NAME}" fi # Check if worktree already exists if [ -d "$WORKTREE_PATH" ]; then echo "Error: Worktree directory already exists: $WORKTREE_PATH" echo "Use 'git worktree remove' to clean up first, or choose a different name" exit 1 fi # Create worktree echo "Creating worktree..." git worktree add "$WORKTREE_PATH" "$BRANCH_NAME" # Create symlink to .auto-build-data/ from main repo # This ensures specs, plans, and status are shared across worktrees if [ -d "$PROJECT_ROOT/.auto-build-data" ]; then echo "Creating symlink to .auto-build-data/..." ln -s "$PROJECT_ROOT/.auto-build-data" "$WORKTREE_PATH/.auto-build-data" fi # Also symlink .claude/rules/ for memory access if [ -d "$PROJECT_ROOT/.claude/rules" ]; then mkdir -p "$WORKTREE_PATH/.claude" ln -s "$PROJECT_ROOT/.claude/rules" "$WORKTREE_PATH/.claude/rules" fi # Update registry REGISTRY_FILE="$PROJECT_ROOT/.auto-build-data/worktrees/worktree-registry.json" TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") if [ -f "$REGISTRY_FILE" ]; then # Add to existing registry using jq if available, otherwise use simple append if command -v jq &> /dev/null; then TEMP_FILE=$(mktemp) jq --arg name "$FEATURE_NAME" \ --arg branch "$BRANCH_NAME" \ --arg path "$WORKTREE_PATH" \ --arg created "$TIMESTAMP" \ '.worktrees += [{ "name": $name, "branch": $branch, "path": $path, "created": $created, "status": "active" }]' "$REGISTRY_FILE" > "$TEMP_FILE" && mv "$TEMP_FILE" "$REGISTRY_FILE" else # Fallback: Just note the worktree was created echo "Note: Install jq for automatic registry updates" fi fi echo "" echo "========================================" echo " Worktree Created Successfully" echo "========================================" echo "" echo "To work in this worktree:" echo " cd $WORKTREE_PATH" echo "" echo "To return to main project:" echo " cd $PROJECT_ROOT" echo "" echo "When done with the feature:" echo " /ab:worktree cleanup $FEATURE_NAME" echo "" # Output for script parsing echo "WORKTREE_CREATED=$WORKTREE_PATH" echo "BRANCH=$BRANCH_NAME"