diff --git a/src/main/ipc/worktree-push-target-setup.test.ts b/src/main/ipc/worktree-push-target-setup.test.ts index a93a22bb4be1..c98bfdfec603 100644 --- a/src/main/ipc/worktree-push-target-setup.test.ts +++ b/src/main/ipc/worktree-push-target-setup.test.ts @@ -32,6 +32,10 @@ function makeRepoExec(remotes: Record): ExecMock { remotes[args[2]!] = args[3]! return { stdout: '', stderr: '' } } + if (args[0] === 'remote' && args[1] === 'remove') { + delete remotes[args[2]!] + return { stdout: '', stderr: '' } + } return { stdout: '', stderr: '' } }) } @@ -184,3 +188,54 @@ describe('configureCreatedWorktreePushTargetWithExec', () => { expect(result).toBe(target) }) }) + +describe('prepareWorktreePushTargetWithExec rollback', () => { + it('removes the remote it just added when the fetch fails', async () => { + const remotes: Record = { origin: 'git@github.com:stablyai/orca.git' } + const exec = vi.fn(async (args: string[]) => { + if (args[0] === 'fetch') { + throw new Error('network unreachable') + } + return makeRepoExec(remotes)(args, REPO) + }) + + await expect( + prepareWorktreePushTargetWithExec( + exec, + REPO, + { remoteName: 'pr-contributor-orca', branchName: 'contributor/fix', remoteUrl: FORK_SSH }, + () => false + ) + ).rejects.toThrow('network unreachable') + + expect(callsMatching(exec, ['remote', 'remove'])).toEqual([ + ['remote', 'remove', 'pr-contributor-orca'] + ]) + expect(remotes).not.toHaveProperty('pr-contributor-orca') + }) + + it('keeps a reused remote Orca did not add when the fetch fails', async () => { + const remotes: Record = { + origin: 'git@github.com:stablyai/orca.git', + existing: FORK_SSH + } + const exec = vi.fn(async (args: string[]) => { + if (args[0] === 'fetch') { + throw new Error('network unreachable') + } + return makeRepoExec(remotes)(args, REPO) + }) + + await expect( + prepareWorktreePushTargetWithExec( + exec, + REPO, + { remoteName: 'pr-contributor-orca', branchName: 'contributor/fix', remoteUrl: FORK_SSH }, + () => false + ) + ).rejects.toThrow('network unreachable') + + expect(callsMatching(exec, ['remote', 'remove'])).toEqual([]) + expect(remotes.existing).toBe(FORK_SSH) + }) +}) diff --git a/src/main/ipc/worktree-push-target-setup.ts b/src/main/ipc/worktree-push-target-setup.ts index 4101055ba65d..71aa1b69779f 100644 --- a/src/main/ipc/worktree-push-target-setup.ts +++ b/src/main/ipc/worktree-push-target-setup.ts @@ -95,14 +95,24 @@ export async function prepareWorktreePushTargetWithExec( } } - await execGit( - [ - 'fetch', - remoteName, - `+refs/heads/${target.branchName}:refs/remotes/${remoteName}/${target.branchName}` - ], - repoPath - ) + try { + await execGit( + [ + 'fetch', + remoteName, + `+refs/heads/${target.branchName}:refs/remotes/${remoteName}/${target.branchName}` + ], + repoPath + ) + } catch (error) { + // Why: the create this remote was added for is failing; leaving it behind + // orphans a pr-* remote nothing will ever clean up (cleanup runs on worktree + // removal, and no worktree exists). + if (remoteCreated) { + await execGit(['remote', 'remove', remoteName], repoPath).catch(() => {}) + } + throw error + } return { ...sanitizedTarget, remoteName, diff --git a/src/main/ipc/worktree-remote.ts b/src/main/ipc/worktree-remote.ts index 4004c73ac100..72ef373758eb 100644 --- a/src/main/ipc/worktree-remote.ts +++ b/src/main/ipc/worktree-remote.ts @@ -1113,12 +1113,21 @@ async function prepareWorktreePushTargetSsh( remoteCreated = true } } - await provider.fetchRemoteTrackingRef( - repoPath, - remoteName, - target.branchName, - `refs/remotes/${remoteName}/${target.branchName}` - ) + try { + await provider.fetchRemoteTrackingRef( + repoPath, + remoteName, + target.branchName, + `refs/remotes/${remoteName}/${target.branchName}` + ) + } catch (error) { + // Why: mirrors the local path — a fetch failure aborts the create, so the + // remote we just added on the host would be orphaned with no owner to clean it. + if (remoteCreated) { + await provider.exec(['remote', 'remove', remoteName], repoPath).catch(() => {}) + } + throw error + } return { ...sanitizedTarget, remoteName, ...(remoteCreated ? { remoteCreated: true } : {}) } } diff --git a/src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts b/src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts index afd134939163..68651186f4d8 100644 --- a/src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts +++ b/src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts @@ -211,4 +211,59 @@ describe('registerWorktreeHandlers', () => { ).rejects.toThrow('Reconnect to deploy the latest relay') expect(provider.addWorktree).not.toHaveBeenCalled() }) + + it('drops the fork remote it just added when the SSH head fetch fails', async () => { + const repo = { + id: 'repo-ssh', + path: '/remote/repo', + displayName: 'ssh', + badgeColor: '#000', + addedAt: 0, + connectionId: 'conn-1', + worktreeBaseRef: 'origin/main' + } + const exec = vi.fn().mockImplementation(async (args: string[]) => { + validateGitExecArgs(args) + if (args[0] === 'remote' && args[1] === 'get-url') { + return { stdout: 'git@github.com:stablyai/orca.git\n', stderr: '' } + } + if (args[0] === 'remote' && args.length === 1) { + return { stdout: 'origin\n', stderr: '' } + } + return { stdout: '', stderr: '' } + }) + const provider = { + exec, + fetchRemoteTrackingRef: vi + .fn() + .mockImplementation(async (_repoPath: string, remote: string) => { + if (remote === 'pr-contributor-orca') { + throw new Error('network unreachable') + } + }), + addWorktree: vi.fn().mockResolvedValue(undefined), + listWorktrees: vi.fn().mockResolvedValue([]) + } + const mux = { request: vi.fn().mockResolvedValue(undefined), notify: vi.fn() } + store.getRepos.mockReturnValue([repo]) + store.getRepo.mockReturnValue(repo) + getSshGitProviderMock.mockReturnValue(provider) + getActiveMultiplexerMock.mockReturnValue(mux) + + await expect( + handlers['worktrees:create'](null, { + repoId: 'repo-ssh', + name: 'contributor-fix', + branchNameOverride: 'contributor/fix', + pushTarget: { + remoteName: 'pr-contributor-orca', + branchName: 'contributor/fix', + remoteUrl: 'https://github.com/contributor/orca.git' + } + }) + ).rejects.toThrow('network unreachable') + + expect(exec).toHaveBeenCalledWith(['remote', 'remove', 'pr-contributor-orca'], '/remote/repo') + expect(provider.addWorktree).not.toHaveBeenCalled() + }) })