Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/main/ipc/worktree-push-target-setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ function makeRepoExec(remotes: Record<string, string>): 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: '' }
})
}
Expand Down Expand Up @@ -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<string, string> = { origin: 'git@github.com:stablyai/orca.git' }
const exec = vi.fn<GitRemoteExec>(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<string, string> = {
origin: 'git@github.com:stablyai/orca.git',
existing: FORK_SSH
}
const exec = vi.fn<GitRemoteExec>(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)
})
})
26 changes: 18 additions & 8 deletions src/main/ipc/worktree-push-target-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {})
}
Comment on lines +111 to +113

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Separate persistent remote ownership from invocation-local rollback state. Both setup paths use remoteCreated for two different meanings. A reused remote that another worktree owns can be removed when the new worktree fetch fails.

  • src/main/ipc/worktree-push-target-setup.ts#L111-L113: remove the remote only when this invocation added it.
  • src/main/ipc/worktree-remote.ts#L1126-L1128: apply the same rollback condition for SSH setup.
  • src/main/ipc/worktree-push-target-setup.test.ts#L217-L240: add a failed-fetch case where the reuse callback returns true, then assert no removal.
  • src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts#L215-L268: add the equivalent SSH case for a known owned remote, then assert no removal.
📍 Affects 4 files
  • src/main/ipc/worktree-push-target-setup.ts#L111-L113 (this comment)
  • src/main/ipc/worktree-remote.ts#L1126-L1128
  • src/main/ipc/worktree-push-target-setup.test.ts#L217-L240
  • src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts#L215-L268

throw error
}
return {
...sanitizedTarget,
remoteName,
Expand Down
21 changes: 15 additions & 6 deletions src/main/ipc/worktree-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}) }
}

Expand Down
55 changes: 55 additions & 0 deletions src/main/ipc/worktrees-ssh-fork-push-target-remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})