From 96ba61f7e69035fefd044caa7a674ddd06eaa043 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 5 Sep 2026 17:50:48 -0700 Subject: [PATCH] Preserve preparation ownership until Git removal starts --- ...rktree-create-preparation-real-git.test.ts | 33 +++++++++++++++++++ src/main/git/worktree-create-preparation.ts | 18 +++++----- src/shared/git-binary-compatibility.test.ts | 10 ++++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/main/git/worktree-create-preparation-real-git.test.ts b/src/main/git/worktree-create-preparation-real-git.test.ts index c281c6d637e..5dac51c5fcd 100644 --- a/src/main/git/worktree-create-preparation-real-git.test.ts +++ b/src/main/git/worktree-create-preparation-real-git.test.ts @@ -55,6 +55,39 @@ afterEach(async () => { }) describe('prepared worktree creation with real Git', () => { + it('retains preparation ownership when the removal command cannot start', async () => { + const fixture = await createRepo() + const repoPath = await realpath(fixture.repoPath) + const root = await realpath(fixture.root) + const preparedPath = join(root, WORKTREE_CREATE_PREPARATION_DIRECTORY, 'owned-removal') + await mkdir(join(root, WORKTREE_CREATE_PREPARATION_DIRECTORY), { recursive: true }) + const lockReason = createWorktreePreparationLockReason('removal-failure') + await prepareWorktreeCreateCheckout(repoPath, preparedPath, 'main', lockReason) + const original = gitRunner.gitExecFileAsync + const spy = vi.spyOn(gitRunner, 'gitExecFileAsync').mockImplementation((args, options) => { + if (args.includes('remove') && args.includes(preparedPath)) { + return Promise.reject(new Error('injected removal launch failure')) + } + return original(args, options) + }) + try { + await expect(discardPreparedWorktree(repoPath, preparedPath)).rejects.toThrow( + 'injected removal launch failure' + ) + const remaining = await listWorktrees(repoPath, { includeCreatePreparations: true }) + const prepared = remaining.find((worktree) => + areWorktreePathsEqual(worktree.path, preparedPath) + ) + expect(prepared).toBeDefined() + expect(prepared?.lockReason).toBe(lockReason) + expect(await readFile(join(preparedPath, 'version.txt'), 'utf8')).toBe('one\n') + } finally { + spy.mockRestore() + await discardPreparedWorktree(repoPath, preparedPath) + } + expect(existsSync(preparedPath)).toBe(false) + }) + it('creates and finalizes while dead-owner reclamation is stalled', async () => { const fixture = await createRepo() const repoPath = await realpath(fixture.repoPath) diff --git a/src/main/git/worktree-create-preparation.ts b/src/main/git/worktree-create-preparation.ts index 78713957690..0f27f045287 100644 --- a/src/main/git/worktree-create-preparation.ts +++ b/src/main/git/worktree-create-preparation.ts @@ -45,16 +45,16 @@ async function performDiscardPreparedWorktree( timeout: options.timeout ?? WORKTREE_REMOVAL_REGISTRATION_TIMEOUT_MS } try { + // Preserve the ownership lock if removal cannot start; Git 2.25 supports locked removal. await gitExecFileAsync( - [...windowsLongPathGitArgs(repoPath), 'worktree', 'unlock', worktreePath], - cleanupGitOptions - ) - } catch { - // It may be unlocked already or only partially registered. - } - try { - await gitExecFileAsync( - [...windowsLongPathGitArgs(repoPath), 'worktree', 'remove', '--force', worktreePath], + [ + ...windowsLongPathGitArgs(repoPath), + 'worktree', + 'remove', + '--force', + '--force', + worktreePath + ], cleanupGitOptions ) } finally { diff --git a/src/shared/git-binary-compatibility.test.ts b/src/shared/git-binary-compatibility.test.ts index fb8161b9f90..e11fa6ed2c4 100644 --- a/src/shared/git-binary-compatibility.test.ts +++ b/src/shared/git-binary-compatibility.test.ts @@ -204,6 +204,16 @@ describeBinaryCompatibility('real Git binary compatibility', () => { await rm(join(repoPath, 'deferred-trash'), { recursive: true, force: true }) }) + it('removes locked prepared worktrees without a separate unlock', async () => { + await runGit(['worktree', 'add', '--detach', '--no-checkout', 'compat-discard', 'HEAD']) + await runGit(['-C', 'compat-discard', 'reset', '--hard', 'HEAD']) + await runGit(['worktree', 'lock', '--reason', 'owned preparation', 'compat-discard']) + await runGit(['worktree', 'remove', '--force', '--force', 'compat-discard']) + expect((await runGit(['worktree', 'list', '--porcelain'])).stdout).not.toContain( + 'compat-discard' + ) + }) + it('supports prepared worktree creation and finalization', async () => { await runGit(['worktree', 'add', '--detach', '--no-checkout', 'compat-prepared', 'HEAD']) await runGit(['-C', 'compat-prepared', 'reset', '--hard', 'HEAD'])