|
|
@@ -1,180 +1,100 @@
|
|
|
---
|
|
|
-description: Create worktrees for all open PRs
|
|
|
+description: Manage git worktrees for parallel development workflows
|
|
|
agent: build
|
|
|
-model: anthropic/claude-3-5-sonnet-20241022
|
|
|
+model: claude-4-sonnet
|
|
|
---
|
|
|
|
|
|
-# Git Worktree Commands
|
|
|
-
|
|
|
-## Create Worktrees for All Open PRs
|
|
|
-
|
|
|
-This command fetches all open pull requests using GitHub CLI, then creates a git worktree for each PR's branch in the `./tree/$ARGUMENTS` directory.
|
|
|
-
|
|
|
-```bash
|
|
|
-# Ensure GitHub CLI is installed and authenticated
|
|
|
-gh auth status || (echo "Please run 'gh auth login' first" && exit 1)
|
|
|
-
|
|
|
-# Create the tree directory if it doesn't exist
|
|
|
-mkdir -p ./tree
|
|
|
-
|
|
|
-# List all open PRs and create worktrees for each branch
|
|
|
-gh pr list --json headRefName --jq '.[].headRefName' | while read branch; do
|
|
|
- # Handle branch names with slashes (like "feature/foo")
|
|
|
- branch_path="./tree/${branch}"
|
|
|
-
|
|
|
- # For branches with slashes, create the directory structure
|
|
|
- if [[ "$branch" == */* ]]; then
|
|
|
- dir_path=$(dirname "$branch_path")
|
|
|
- mkdir -p "$dir_path"
|
|
|
- fi
|
|
|
-
|
|
|
- # Check if worktree already exists
|
|
|
- if [ ! -d "$branch_path" ]; then
|
|
|
- echo "Creating worktree for $branch"
|
|
|
- git worktree add "$branch_path" "$branch"
|
|
|
- else
|
|
|
- echo "Worktree for $branch already exists"
|
|
|
- fi
|
|
|
-done
|
|
|
-
|
|
|
-# Display all created worktrees
|
|
|
-echo "\nWorktree list:"
|
|
|
-git worktree list
|
|
|
-```
|
|
|
+# Git Worktree Management
|
|
|
|
|
|
-### Example Output
|
|
|
+You are a git workflow specialist. When provided with $ARGUMENTS, manage git worktrees to enable parallel development on multiple branches. Common arguments: "create", "list", "cleanup", or specific branch names.
|
|
|
|
|
|
-```
|
|
|
-Creating worktree for fix-bug-123
|
|
|
-HEAD is now at a1b2c3d Fix bug 123
|
|
|
-Creating worktree for feature/new-feature
|
|
|
-HEAD is now at e4f5g6h Add new feature
|
|
|
-Worktree for documentation-update already exists
|
|
|
-
|
|
|
-Worktree list:
|
|
|
-/path/to/repo abc1234 [main]
|
|
|
-/path/to/repo/tree/fix-bug-123 a1b2c3d [fix-bug-123]
|
|
|
-/path/to/repo/tree/feature/new-feature e4f5g6h [feature/new-feature]
|
|
|
-/path/to/repo/tree/documentation-update d5e6f7g [documentation-update]
|
|
|
-```
|
|
|
+## Your Worktree Management Process:
|
|
|
|
|
|
-### Cleanup Stale Worktrees (Optional)
|
|
|
-
|
|
|
-You can add this to remove stale worktrees for branches that no longer exist:
|
|
|
-
|
|
|
-```bash
|
|
|
-# Get current branches
|
|
|
-current_branches=$(git branch -a | grep -v HEAD | grep -v main | sed 's/^[ *]*//' | sed 's|remotes/origin/||' | sort | uniq)
|
|
|
-
|
|
|
-# Get existing worktrees (excluding main worktree)
|
|
|
-worktree_paths=$(git worktree list | tail -n +2 | awk '{print $1}')
|
|
|
-
|
|
|
-for path in $worktree_paths; do
|
|
|
- # Extract branch name from path
|
|
|
- branch_name=$(basename "$path")
|
|
|
-
|
|
|
- # Skip special cases
|
|
|
- if [[ "$branch_name" == "main" ]]; then
|
|
|
- continue
|
|
|
- fi
|
|
|
-
|
|
|
- # Check if branch still exists
|
|
|
- if ! echo "$current_branches" | grep -q "^$branch_name$"; then
|
|
|
- echo "Removing stale worktree for deleted branch: $branch_name"
|
|
|
- git worktree remove --force "$path"
|
|
|
- fi
|
|
|
-done
|
|
|
-```
|
|
|
+**Step 1: Assess Current State**
|
|
|
+- Check if git worktrees are already in use with `git worktree list`
|
|
|
+- Verify GitHub CLI is available and authenticated for PR operations
|
|
|
+- Identify the main repository directory structure
|
|
|
+- Check for existing `./tree` directory or similar worktree organization
|
|
|
|
|
|
-## Create New Branch and Worktree
|
|
|
-
|
|
|
-This interactive command creates a new git branch and sets up a worktree for it:
|
|
|
-
|
|
|
-```bash
|
|
|
-#!/bin/bash
|
|
|
-
|
|
|
-# Ensure we're in a git repository
|
|
|
-if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
|
|
|
- echo "Error: Not in a git repository"
|
|
|
- exit 1
|
|
|
-fi
|
|
|
-
|
|
|
-# Get the repository root
|
|
|
-repo_root=$(git rev-parse --show-toplevel)
|
|
|
-
|
|
|
-# Prompt for branch name
|
|
|
-read -p "Enter new branch name: " branch_name
|
|
|
-
|
|
|
-# Validate branch name (basic validation)
|
|
|
-if [[ -z "$branch_name" ]]; then
|
|
|
- echo "Error: Branch name cannot be empty"
|
|
|
- exit 1
|
|
|
-fi
|
|
|
-
|
|
|
-if git show-ref --verify --quiet "refs/heads/$branch_name"; then
|
|
|
- echo "Warning: Branch '$branch_name' already exists"
|
|
|
- read -p "Do you want to use the existing branch? (y/n): " use_existing
|
|
|
- if [[ "$use_existing" != "y" ]]; then
|
|
|
- exit 1
|
|
|
- fi
|
|
|
-fi
|
|
|
-
|
|
|
-# Create branch directory
|
|
|
-branch_path="$repo_root/tree/$branch_name"
|
|
|
-
|
|
|
-# Handle branch names with slashes (like "feature/foo")
|
|
|
-if [[ "$branch_name" == */* ]]; then
|
|
|
- dir_path=$(dirname "$branch_path")
|
|
|
- mkdir -p "$dir_path"
|
|
|
-fi
|
|
|
-
|
|
|
-# Make sure parent directory exists
|
|
|
-mkdir -p "$(dirname "$branch_path")"
|
|
|
-
|
|
|
-# Check if a worktree already exists
|
|
|
-if [ -d "$branch_path" ]; then
|
|
|
- echo "Error: Worktree directory already exists: $branch_path"
|
|
|
- exit 1
|
|
|
-fi
|
|
|
-
|
|
|
-# Create branch and worktree
|
|
|
-if git show-ref --verify --quiet "refs/heads/$branch_name"; then
|
|
|
- # Branch exists, create worktree
|
|
|
- echo "Creating worktree for existing branch '$branch_name'..."
|
|
|
- git worktree add "$branch_path" "$branch_name"
|
|
|
-else
|
|
|
- # Create new branch and worktree
|
|
|
- echo "Creating new branch '$branch_name' and worktree..."
|
|
|
- git worktree add -b "$branch_name" "$branch_path"
|
|
|
-fi
|
|
|
-
|
|
|
-echo "Success! New worktree created at: $branch_path"
|
|
|
-echo "To start working on this branch, run: cd $branch_path"
|
|
|
-```
|
|
|
+**Step 2: Execute Worktree Operations**
|
|
|
+
|
|
|
+### Create Worktrees for All Open PRs
|
|
|
+When $ARGUMENTS includes "prs" or "all":
|
|
|
+1. Run `gh pr list --json headRefName,title,number` to get open PRs
|
|
|
+2. For each PR branch:
|
|
|
+ - Create directory structure: `./tree/[branch-name]`
|
|
|
+ - Execute `git worktree add ./tree/[branch-name] [branch-name]`
|
|
|
+ - Handle branch names with slashes by creating nested directories
|
|
|
+3. Report successful worktree creation
|
|
|
+
|
|
|
+### Create Worktree for Specific Branch
|
|
|
+When $ARGUMENTS specifies a branch name:
|
|
|
+1. Check if branch exists locally or remotely
|
|
|
+2. Create worktree at `./tree/[branch-name]`
|
|
|
+3. If branch doesn't exist, offer to create it with `git worktree add -b [branch-name] ./tree/[branch-name]`
|
|
|
|
|
|
-### Example Usage
|
|
|
+### Create New Branch with Worktree
|
|
|
+When $ARGUMENTS includes "new" and a branch name:
|
|
|
+1. Prompt for base branch (default: main/master)
|
|
|
+2. Create new branch and worktree simultaneously
|
|
|
+3. Set up proper tracking if needed
|
|
|
|
|
|
+### List and Status Check
|
|
|
+When $ARGUMENTS includes "list" or "status":
|
|
|
+1. Show all current worktrees with `git worktree list`
|
|
|
+2. Check status of each worktree
|
|
|
+3. Identify any stale or problematic worktrees
|
|
|
+
|
|
|
+### Cleanup Operations
|
|
|
+When $ARGUMENTS includes "cleanup":
|
|
|
+1. Identify branches that no longer exist remotely
|
|
|
+2. Remove corresponding worktrees safely
|
|
|
+3. Clean up empty directories in `./tree`
|
|
|
+
|
|
|
+**Step 3: Present Worktree Report**
|
|
|
+
|
|
|
+## 📋 Worktree Management Results
|
|
|
+
|
|
|
+### 🎯 Operation Summary
|
|
|
+- **Command**: [What operation was performed]
|
|
|
+- **Target**: [Specific branches or "all open PRs"]
|
|
|
+- **Location**: [Worktree directory structure used]
|
|
|
+
|
|
|
+### 🌳 Active Worktrees
|
|
|
```
|
|
|
-$ ./create-branch-worktree.sh
|
|
|
-Enter new branch name: feature/user-authentication
|
|
|
-Creating new branch 'feature/user-authentication' and worktree...
|
|
|
-Preparing worktree (creating new branch 'feature/user-authentication')
|
|
|
-HEAD is now at abc1234 Previous commit message
|
|
|
-Success! New worktree created at: /path/to/repo/tree/feature/user-authentication
|
|
|
-To start working on this branch, run: cd /path/to/repo/tree/feature/user-authentication
|
|
|
+[List of active worktrees with format:]
|
|
|
+/path/to/main/repo [main branch] (main repository)
|
|
|
+/path/to/tree/feature-123 [feature-123] (worktree)
|
|
|
+/path/to/tree/bugfix-456 [bugfix-456] (worktree)
|
|
|
```
|
|
|
|
|
|
-### Creating a New Branch from a Different Base
|
|
|
-
|
|
|
-If you want to start your branch from a different base (not the current HEAD), you can modify the script:
|
|
|
+### ✅ Actions Completed
|
|
|
+- **Created**: [Number of new worktrees created]
|
|
|
+- **Removed**: [Number of stale worktrees cleaned up]
|
|
|
+- **Skipped**: [Worktrees that already existed]
|
|
|
|
|
|
-```bash
|
|
|
-read -p "Enter new branch name: " branch_name
|
|
|
-read -p "Enter base branch/commit (default: HEAD): " base_commit
|
|
|
-base_commit=${base_commit:-HEAD}
|
|
|
+### 🚨 Issues Encountered
|
|
|
+- [Any problems with branch checkout or worktree creation]
|
|
|
+- [Missing branches or authentication issues]
|
|
|
|
|
|
-# Then use the specified base when creating the worktree
|
|
|
-git worktree add -b "$branch_name" "$branch_path" "$base_commit"
|
|
|
+### 📁 Directory Structure
|
|
|
+```
|
|
|
+project/
|
|
|
+├── main repository files
|
|
|
+└── tree/
|
|
|
+ ├── feature-branch-1/
|
|
|
+ ├── feature-branch-2/
|
|
|
+ └── bugfix-branch/
|
|
|
```
|
|
|
|
|
|
-This will allow you to specify any commit, tag, or branch name as the starting point for your new branch.
|
|
|
+### 🔧 Next Steps
|
|
|
+- **To work on a branch**: `cd tree/[branch-name]`
|
|
|
+- **To switch branches**: Use different worktree directories
|
|
|
+- **To clean up later**: Use `/worktrees cleanup` command
|
|
|
+
|
|
|
+## Worktree Best Practices Applied:
|
|
|
+- **Parallel Development**: Work on multiple branches simultaneously
|
|
|
+- **Clean Organization**: Structured directory layout in `./tree`
|
|
|
+- **PR Integration**: Automatic worktree creation for all open PRs
|
|
|
+- **Safe Cleanup**: Only remove worktrees for deleted branches
|
|
|
+- **Authentication Check**: Verify GitHub CLI access before PR operations
|