From e5ce1467e0e6a04fdf40b4a6f2dd738abbe5ff44 Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Fri, 19 Dec 2025 11:59:30 -0800 Subject: [PATCH] fix(polecat): handle existing branch gracefully in polecat add MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When adding a polecat, check if the branch already exists (e.g., from a previous polecat that was removed but whose branch wasn't cleaned up). If so, reuse the existing branch with WorktreeAddExisting instead of failing with 'a branch named polecat/X already exists'. Fixes: gt-bmjw 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- internal/polecat/manager.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/polecat/manager.go b/internal/polecat/manager.go index bfad5578..264a4ca1 100644 --- a/internal/polecat/manager.go +++ b/internal/polecat/manager.go @@ -74,10 +74,24 @@ func (m *Manager) Add(name string) (*Polecat, error) { return nil, fmt.Errorf("mayor clone not found at %s (run 'gt rig add' to set up rig structure)", mayorPath) } - // Create worktree with new branch - // git worktree add -b polecat/ - if err := mayorGit.WorktreeAdd(polecatPath, branchName); err != nil { - return nil, fmt.Errorf("creating worktree: %w", err) + // Check if branch already exists (e.g., from previous polecat that wasn't cleaned up) + branchExists, err := mayorGit.BranchExists(branchName) + if err != nil { + return nil, fmt.Errorf("checking branch existence: %w", err) + } + + // Create worktree - reuse existing branch if it exists + if branchExists { + // Branch exists, create worktree using existing branch + if err := mayorGit.WorktreeAddExisting(polecatPath, branchName); err != nil { + return nil, fmt.Errorf("creating worktree with existing branch: %w", err) + } + } else { + // Create new branch with worktree + // git worktree add -b polecat/ + if err := mayorGit.WorktreeAdd(polecatPath, branchName); err != nil { + return nil, fmt.Errorf("creating worktree: %w", err) + } } // Create polecat state - ephemeral polecats start in working state