diff --git a/src/commands/new.ts b/src/commands/new.ts index 2cbee53..ce02bd3 100644 --- a/src/commands/new.ts +++ b/src/commands/new.ts @@ -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) } diff --git a/src/git.ts b/src/git.ts index 9a11c51..77b4a57 100644 --- a/src/git.ts +++ b/src/git.ts @@ -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 { +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. */