Track branch creation to avoid deleting pre-existing branches

This commit is contained in:
Chris Wanstrath 2026-03-08 23:17:13 -07:00
parent 6b5135c96e
commit 1f1b3f2a0d
2 changed files with 15 additions and 2 deletions

View File

@ -96,13 +96,20 @@ export async function action(
die(`Session "${branch}" already exists. Use "sandlot open ${branch}" to re-enter it.`)
}
const branchExisted = await git.branchExists(branch, root)
const spin = spinner("Creating worktree", branch)
let branchCreated = false
try {
<<<<<<< HEAD
await git.createWorktree(branch, worktreeAbs, root)
const symlinkPath = join(root, '.sandlot', branch)
await mkdir(dirname(symlinkPath), { recursive: true })
await symlink(worktreeAbs, symlinkPath)
=======
const wt = await git.createWorktree(branch, worktreeAbs, root)
branchCreated = wt.branchCreated
await mkdir(join(root, '.sandlot'), { recursive: true })
await symlink(worktreeAbs, join(root, '.sandlot', branch))
>>>>>>> 597eb97 (Track branch creation to avoid deleting pre-existing branches)
spin.text = "Starting container"
await vm.ensure((msg) => { spin.text = msg })
@ -110,8 +117,13 @@ export async function action(
} catch (err) {
spin.fail(String((err as Error).message ?? err))
await git.removeWorktree(worktreeAbs, root).catch(() => {})
<<<<<<< HEAD
await git.deleteLocalBranch(branch, root).catch(() => {})
await unlinkSessionSymlink(root, branch)
=======
if (branchCreated) await git.deleteLocalBranch(branch, root).catch(() => {})
await unlink(join(root, '.sandlot', branch)).catch(() => {})
>>>>>>> 597eb97 (Track branch creation to avoid deleting pre-existing branches)
process.exit(1)
}

View File

@ -36,7 +36,7 @@ export async function branchExists(branch: string, cwd?: string, opts?: { fetch?
}
/** Create a worktree for the given branch. */
export async function createWorktree(branch: string, worktreePath: string, cwd: string): Promise<void> {
export async function createWorktree(branch: string, worktreePath: string, cwd: string): Promise<{ branchCreated: boolean }> {
// Clean up stale worktree path if it exists
if (existsSync(worktreePath)) {
await $`git worktree remove ${worktreePath} --force`.cwd(cwd).nothrow().quiet()
@ -70,6 +70,7 @@ export async function createWorktree(branch: string, worktreePath: string, cwd:
if (switchedFromBranch) await checkout(branch, cwd).catch(() => {})
throw new Error(`Failed to create worktree for "${branch}": ${result.stderr.toString().trim()}`)
}
return { branchCreated: exists !== "local" }
}
/** Remove a worktree. Silently succeeds if the worktree is already gone. */