Skip to content

Commit 5cdef33

Browse files
built-by-asclaude
andauthored
Simplify worktree path uniqueness handling (#19)
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 <[email protected]>
1 parent 67b61a2 commit 5cdef33

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

main.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,15 @@ async function createWorktree(projectDir: string, parentBranch: string, sessionN
322322
const worktreesBaseDir = getWorktreeBaseDir();
323323
const projectWorktreeDir = path.join(worktreesBaseDir, projectDirName);
324324
const worktreeName = customBranchName || `session${sessionNumber}`;
325-
const worktreePath = path.join(projectWorktreeDir, worktreeName);
325+
let worktreePath = path.join(projectWorktreeDir, worktreeName);
326326

327327
// Use custom branch name if provided, otherwise generate default
328328
let branchName: string;
329+
const shortUuid = sessionUuid.split('-')[0];
329330
if (customBranchName) {
330331
branchName = customBranchName;
331332
} else {
332333
// Include short UUID to ensure branch uniqueness across deletes/recreates
333-
const shortUuid = sessionUuid.split('-')[0];
334334
branchName = `fleetcode/${worktreeName}-${shortUuid}`;
335335
}
336336

@@ -343,20 +343,9 @@ async function createWorktree(projectDir: string, parentBranch: string, sessionN
343343
fs.writeFileSync(markerFile, projectDir, "utf-8");
344344
}
345345

346-
// Check if worktree already exists and remove it
346+
// Append short UUID to worktree path to ensure uniqueness
347347
if (fs.existsSync(worktreePath)) {
348-
try {
349-
await git.raw(["worktree", "remove", worktreePath, "--force"]);
350-
} catch (error) {
351-
console.error("Error removing existing worktree:", error);
352-
}
353-
}
354-
355-
// Delete the branch if it exists
356-
try {
357-
await git.raw(["branch", "-D", branchName]);
358-
} catch (error) {
359-
// Branch doesn't exist, that's fine
348+
worktreePath += `-${shortUuid}`;
360349
}
361350

362351
// Create new worktree with a new branch from parent branch

0 commit comments

Comments
 (0)