From 09dd1ee1d736bb35ad1e17fff38503ace7129def Mon Sep 17 00:00:00 2001 From: Amrit Subramanian Date: Fri, 24 Oct 2025 19:55:28 -0400 Subject: [PATCH] Simplify worktree path uniqueness handling Instead of forcefully removing existing worktrees and branches, append a short UUID to the worktree path when a conflict is detected. This approach is cleaner and avoids destructive operations. Changes: - Append short UUID to worktree path if directory already exists - Remove forced worktree removal logic - Remove forced branch deletion logic - Move shortUuid extraction earlier for reuse - Change worktreePath from const to let for modification Co-Authored-By: Claude --- main.ts | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/main.ts b/main.ts index 6dfcf97..734924b 100644 --- a/main.ts +++ b/main.ts @@ -322,15 +322,15 @@ async function createWorktree(projectDir: string, parentBranch: string, sessionN const worktreesBaseDir = getWorktreeBaseDir(); const projectWorktreeDir = path.join(worktreesBaseDir, projectDirName); const worktreeName = customBranchName || `session${sessionNumber}`; - const worktreePath = path.join(projectWorktreeDir, worktreeName); + let worktreePath = path.join(projectWorktreeDir, worktreeName); // Use custom branch name if provided, otherwise generate default let branchName: string; + const shortUuid = sessionUuid.split('-')[0]; if (customBranchName) { branchName = customBranchName; } else { // Include short UUID to ensure branch uniqueness across deletes/recreates - const shortUuid = sessionUuid.split('-')[0]; branchName = `fleetcode/${worktreeName}-${shortUuid}`; } @@ -343,20 +343,9 @@ async function createWorktree(projectDir: string, parentBranch: string, sessionN fs.writeFileSync(markerFile, projectDir, "utf-8"); } - // Check if worktree already exists and remove it + // Append short UUID to worktree path to ensure uniqueness if (fs.existsSync(worktreePath)) { - try { - await git.raw(["worktree", "remove", worktreePath, "--force"]); - } catch (error) { - console.error("Error removing existing worktree:", error); - } - } - - // Delete the branch if it exists - try { - await git.raw(["branch", "-D", branchName]); - } catch (error) { - // Branch doesn't exist, that's fine + worktreePath += `-${shortUuid}`; } // Create new worktree with a new branch from parent branch