feat: add source control action to abort merge (#2092)
Co-authored-by: Orca <help@stably.ai> Co-authored-by: brennanb2025 <brennankbenson@gmail.com>
This commit is contained in:
co-authored by
Orca
brennanb2025
parent
a65f7b4216
commit
659fe79226
@@ -38,6 +38,7 @@ vi.mock('fs', () => ({
|
||||
}))
|
||||
|
||||
import {
|
||||
abortMerge,
|
||||
bulkStageFiles,
|
||||
bulkDiscardChanges,
|
||||
bulkUnstageFiles,
|
||||
@@ -647,6 +648,20 @@ describe('getStatus', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('abortMerge', () => {
|
||||
beforeEach(() => {
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
})
|
||||
|
||||
it('runs git merge --abort in the worktree', async () => {
|
||||
gitExecFileAsyncMock.mockResolvedValueOnce({ stdout: '' })
|
||||
|
||||
await abortMerge('/repo')
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['merge', '--abort'], { cwd: '/repo' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('getStagedCommitContext', () => {
|
||||
beforeEach(() => {
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
|
||||
@@ -459,6 +459,10 @@ export async function detectConflictOperation(worktreePath: string): Promise<Git
|
||||
return 'unknown'
|
||||
}
|
||||
|
||||
export async function abortMerge(worktreePath: string): Promise<void> {
|
||||
await gitExecFileAsync(['merge', '--abort'], { cwd: worktreePath })
|
||||
}
|
||||
|
||||
export async function resolveGitDir(worktreePath: string): Promise<string> {
|
||||
const dotGitPath = path.join(worktreePath, '.git')
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ const {
|
||||
lstatMock,
|
||||
commitChangesMock,
|
||||
getStatusMock,
|
||||
abortMergeMock,
|
||||
getDiffMock,
|
||||
getBranchCompareMock,
|
||||
getBranchDiffMock,
|
||||
@@ -46,6 +47,7 @@ const {
|
||||
lstatMock: vi.fn(),
|
||||
commitChangesMock: vi.fn(),
|
||||
getStatusMock: vi.fn(),
|
||||
abortMergeMock: vi.fn(),
|
||||
getDiffMock: vi.fn(),
|
||||
getBranchCompareMock: vi.fn(),
|
||||
getBranchDiffMock: vi.fn(),
|
||||
@@ -89,6 +91,7 @@ vi.mock('fs/promises', () => ({
|
||||
vi.mock('../git/status', () => ({
|
||||
commitChanges: commitChangesMock,
|
||||
getStatus: getStatusMock,
|
||||
abortMerge: abortMergeMock,
|
||||
getDiff: getDiffMock,
|
||||
getBranchCompare: getBranchCompareMock,
|
||||
getBranchDiff: getBranchDiffMock,
|
||||
@@ -579,6 +582,26 @@ describe('registerFilesystemHandlers', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it('routes abort merge through local and SSH git providers', async () => {
|
||||
registerWorktreeRootsForRepo(store as never, 'repo-1', [REPO_PATH, WORKTREE_FEATURE_PATH])
|
||||
abortMergeMock.mockResolvedValue(undefined)
|
||||
const sshProvider = {
|
||||
abortMerge: vi.fn().mockResolvedValue(undefined)
|
||||
}
|
||||
getSshGitProviderMock.mockReturnValue(sshProvider)
|
||||
|
||||
registerFilesystemHandlers(store as never)
|
||||
|
||||
await handlers.get('git:abortMerge')!(null, { worktreePath: WORKTREE_FEATURE_PATH })
|
||||
await handlers.get('git:abortMerge')!(null, {
|
||||
worktreePath: '/remote/repo',
|
||||
connectionId: 'ssh-1'
|
||||
})
|
||||
|
||||
expect(abortMergeMock).toHaveBeenCalledWith(WORKTREE_FEATURE_PATH)
|
||||
expect(sshProvider.abortMerge).toHaveBeenCalledWith('/remote/repo')
|
||||
})
|
||||
|
||||
it('rejects git file paths that escape the selected worktree', async () => {
|
||||
registerFilesystemHandlers(store as never)
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
} from '../../shared/text-search'
|
||||
import {
|
||||
getStatus,
|
||||
abortMerge,
|
||||
detectConflictOperation,
|
||||
getDiff,
|
||||
commitChanges,
|
||||
@@ -567,6 +568,21 @@ export function registerFilesystemHandlers(
|
||||
}
|
||||
)
|
||||
|
||||
ipcMain.handle(
|
||||
'git:abortMerge',
|
||||
async (_event, args: { worktreePath: string; connectionId?: string }): Promise<void> => {
|
||||
if (args.connectionId) {
|
||||
const provider = getSshGitProvider(args.connectionId)
|
||||
if (!provider) {
|
||||
throw new Error(`No git provider for connection "${args.connectionId}"`)
|
||||
}
|
||||
return provider.abortMerge(args.worktreePath)
|
||||
}
|
||||
const worktreePath = await resolveRegisteredWorktreePath(args.worktreePath, store)
|
||||
await abortMerge(worktreePath)
|
||||
}
|
||||
)
|
||||
|
||||
ipcMain.handle(
|
||||
'git:diff',
|
||||
async (
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { SshGitProvider } from './ssh-git-provider'
|
||||
|
||||
describe('SshGitProvider merge operations', () => {
|
||||
it('abortMerge sends git.abortMerge request', async () => {
|
||||
const mux = {
|
||||
request: vi.fn().mockResolvedValue(undefined),
|
||||
notify: vi.fn(),
|
||||
onNotification: vi.fn(),
|
||||
dispose: vi.fn(),
|
||||
isDisposed: vi.fn().mockReturnValue(false)
|
||||
}
|
||||
const provider = new SshGitProvider('conn-1', mux as never)
|
||||
|
||||
await provider.abortMerge('/home/user/repo')
|
||||
|
||||
expect(mux.request).toHaveBeenCalledWith('git.abortMerge', {
|
||||
worktreePath: '/home/user/repo'
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -281,6 +281,10 @@ export class SshGitProvider implements IGitProvider {
|
||||
})) as GitConflictOperation
|
||||
}
|
||||
|
||||
async abortMerge(worktreePath: string): Promise<void> {
|
||||
await this.mux.request('git.abortMerge', { worktreePath })
|
||||
}
|
||||
|
||||
async getBranchCompare(worktreePath: string, baseRef: string): Promise<GitBranchCompareResult> {
|
||||
return (await this.mux.request('git.branchCompare', {
|
||||
worktreePath,
|
||||
|
||||
@@ -172,6 +172,7 @@ export type IGitProvider = {
|
||||
discardChanges(worktreePath: string, filePath: string): Promise<void>
|
||||
bulkDiscardChanges(worktreePath: string, filePaths: string[]): Promise<void>
|
||||
detectConflictOperation(worktreePath: string): Promise<GitConflictOperation>
|
||||
abortMerge(worktreePath: string): Promise<void>
|
||||
getBranchCompare(worktreePath: string, baseRef: string): Promise<GitBranchCompareResult>
|
||||
getCommitCompare(worktreePath: string, commitId: string): Promise<GitCommitCompareResult>
|
||||
getUpstreamStatus(worktreePath: string, pushTarget?: GitPushTarget): Promise<GitUpstreamStatus>
|
||||
|
||||
@@ -8,6 +8,7 @@ import type * as CommitMessageTextGenerationModule from '../text-generation/comm
|
||||
import { RuntimeGitCommands, type ResolvedRuntimeGitWorktree } from './orca-runtime-git'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
abortMerge: vi.fn(),
|
||||
getStagedCommitContext: vi.fn(),
|
||||
generateCommitMessageFromContext: vi.fn(),
|
||||
resolveCommitMessageSettings: vi.fn(),
|
||||
@@ -16,6 +17,7 @@ const mocks = vi.hoisted(() => ({
|
||||
|
||||
vi.mock('../git/status', async () => ({
|
||||
...(await vi.importActual<typeof GitStatusModule>('../git/status')),
|
||||
abortMerge: mocks.abortMerge,
|
||||
getStagedCommitContext: mocks.getStagedCommitContext
|
||||
}))
|
||||
|
||||
@@ -57,6 +59,7 @@ function makeCommands(worktreePath: string): RuntimeGitCommands {
|
||||
|
||||
describe('RuntimeGitCommands', () => {
|
||||
beforeEach(() => {
|
||||
mocks.abortMerge.mockReset()
|
||||
mocks.getStagedCommitContext.mockReset()
|
||||
mocks.generateCommitMessageFromContext.mockReset()
|
||||
mocks.resolveCommitMessageSettings.mockReset()
|
||||
@@ -69,6 +72,34 @@ describe('RuntimeGitCommands', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('aborts a local merge through the resolved worktree', async () => {
|
||||
const worktreePath = mkdtempSync(join(tmpdir(), 'orca-runtime-git-'))
|
||||
tempDirs.push(worktreePath)
|
||||
const commands = makeCommands(worktreePath)
|
||||
mocks.abortMerge.mockResolvedValue(undefined)
|
||||
|
||||
await expect(commands.abortRuntimeGitMerge('id:wt-1')).resolves.toEqual({ ok: true })
|
||||
|
||||
expect(mocks.abortMerge).toHaveBeenCalledWith(worktreePath)
|
||||
})
|
||||
|
||||
it('aborts a remote merge through the SSH git provider', async () => {
|
||||
const provider = { abortMerge: vi.fn().mockResolvedValue(undefined) }
|
||||
mocks.getSshGitProvider.mockReturnValue(provider)
|
||||
const commands = new RuntimeGitCommands({
|
||||
resolveRuntimeGitTarget: async () => ({
|
||||
worktree: makeWorktree('/remote/repo'),
|
||||
connectionId: 'conn-1'
|
||||
}),
|
||||
getRuntimeSettings: () => ({}) as GlobalSettings
|
||||
})
|
||||
|
||||
await expect(commands.abortRuntimeGitMerge('id:wt-1')).resolves.toEqual({ ok: true })
|
||||
|
||||
expect(provider.abortMerge).toHaveBeenCalledWith('/remote/repo')
|
||||
expect(mocks.abortMerge).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects slash-only git mutation paths before they can target the worktree root', async () => {
|
||||
const worktreePath = mkdtempSync(join(tmpdir(), 'orca-runtime-git-'))
|
||||
tempDirs.push(worktreePath)
|
||||
|
||||
@@ -17,6 +17,7 @@ import { getCommitMessageModelDiscoveryHostKey } from '../../shared/commit-messa
|
||||
import type { GitHistoryOptions, GitHistoryResult } from '../../shared/git-history'
|
||||
import { getRemoteFileUrl } from '../git/repo'
|
||||
import {
|
||||
abortMerge,
|
||||
bulkDiscardChanges,
|
||||
bulkStageFiles,
|
||||
bulkUnstageFiles,
|
||||
@@ -148,6 +149,20 @@ export class RuntimeGitCommands {
|
||||
return detectConflictOperation(target.worktree.path)
|
||||
}
|
||||
|
||||
async abortRuntimeGitMerge(worktreeSelector: string): Promise<{ ok: true }> {
|
||||
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
|
||||
const provider = target.connectionId ? getSshGitProvider(target.connectionId) : null
|
||||
if (target.connectionId) {
|
||||
if (!provider) {
|
||||
throw new Error(SSH_GIT_PROVIDER_UNAVAILABLE_MESSAGE)
|
||||
}
|
||||
await provider.abortMerge(target.worktree.path)
|
||||
return { ok: true }
|
||||
}
|
||||
await abortMerge(target.worktree.path)
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
async getRuntimeGitDiff(
|
||||
worktreeSelector: string,
|
||||
filePath: string,
|
||||
|
||||
@@ -2047,6 +2047,8 @@ export class OrcaRuntimeService {
|
||||
this.gitCommands.getRuntimeGitHistory.bind(this.gitCommands)
|
||||
getRuntimeGitConflictOperation: RuntimeGitCommands['getRuntimeGitConflictOperation'] =
|
||||
this.gitCommands.getRuntimeGitConflictOperation.bind(this.gitCommands)
|
||||
abortRuntimeGitMerge: RuntimeGitCommands['abortRuntimeGitMerge'] =
|
||||
this.gitCommands.abortRuntimeGitMerge.bind(this.gitCommands)
|
||||
getRuntimeGitDiff: RuntimeGitCommands['getRuntimeGitDiff'] =
|
||||
this.gitCommands.getRuntimeGitDiff.bind(this.gitCommands)
|
||||
getRuntimeGitBranchCompare: RuntimeGitCommands['getRuntimeGitBranchCompare'] =
|
||||
|
||||
@@ -197,6 +197,7 @@ describe('git RPC methods', () => {
|
||||
defaultModelId: 'auto'
|
||||
}),
|
||||
cancelRuntimeGenerateCommitMessage: vi.fn().mockResolvedValue({ ok: true }),
|
||||
abortRuntimeGitMerge: vi.fn().mockResolvedValue({ ok: true }),
|
||||
pushRuntimeGit: vi.fn().mockResolvedValue({ ok: true }),
|
||||
getRuntimeGitRemoteFileUrl: vi.fn().mockResolvedValue('https://example.com/file#L3')
|
||||
} as unknown as OrcaRuntimeService
|
||||
@@ -216,6 +217,7 @@ describe('git RPC methods', () => {
|
||||
await dispatcher.dispatch(
|
||||
makeRequest('git.cancelGenerateCommitMessage', { worktree: 'id:wt-1' })
|
||||
)
|
||||
await dispatcher.dispatch(makeRequest('git.abortMerge', { worktree: 'id:wt-1' }))
|
||||
await dispatcher.dispatch(
|
||||
makeRequest('git.push', {
|
||||
worktree: 'id:wt-1',
|
||||
@@ -237,6 +239,7 @@ describe('git RPC methods', () => {
|
||||
agentCmdOverrides: { cursor: 'cursor-agent' }
|
||||
})
|
||||
expect(runtime.cancelRuntimeGenerateCommitMessage).toHaveBeenCalledWith('id:wt-1')
|
||||
expect(runtime.abortRuntimeGitMerge).toHaveBeenCalledWith('id:wt-1')
|
||||
expect(runtime.pushRuntimeGit).toHaveBeenCalledWith(
|
||||
'id:wt-1',
|
||||
true,
|
||||
|
||||
@@ -51,6 +51,11 @@ export const GIT_METHODS: RpcMethod[] = [
|
||||
params: WorktreeSelector,
|
||||
handler: async (params, { runtime }) => runtime.getRuntimeGitConflictOperation(params.worktree)
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'git.abortMerge',
|
||||
params: WorktreeSelector,
|
||||
handler: async (params, { runtime }) => runtime.abortRuntimeGitMerge(params.worktree)
|
||||
}),
|
||||
defineMethod({
|
||||
name: 'git.diff',
|
||||
params: GitDiff,
|
||||
|
||||
@@ -761,6 +761,7 @@ describe('OrcaRuntimeRpcServer', () => {
|
||||
.fn()
|
||||
.mockResolvedValue({ hasUpstream: true, ahead: 1, behind: 0 })
|
||||
const rebaseRuntimeGitFromBase = vi.fn().mockResolvedValue({ ok: true })
|
||||
const abortRuntimeGitMerge = vi.fn().mockResolvedValue({ ok: true })
|
||||
const bulkStageRuntimeGitPaths = vi.fn().mockResolvedValue({ ok: true })
|
||||
const bulkUnstageRuntimeGitPaths = vi.fn().mockResolvedValue({ ok: true })
|
||||
const getRuntimeGitDiff = vi.fn().mockResolvedValue({
|
||||
@@ -842,6 +843,7 @@ describe('OrcaRuntimeRpcServer', () => {
|
||||
getRuntimeGitStatus,
|
||||
getRuntimeGitUpstreamStatus,
|
||||
rebaseRuntimeGitFromBase,
|
||||
abortRuntimeGitMerge,
|
||||
bulkStageRuntimeGitPaths,
|
||||
bulkUnstageRuntimeGitPaths,
|
||||
getRuntimeGitDiff,
|
||||
@@ -1400,6 +1402,16 @@ describe('OrcaRuntimeRpcServer', () => {
|
||||
(response) => replies.push(JSON.parse(response) as Record<string, unknown>),
|
||||
() => {}
|
||||
)
|
||||
await server['handleWebSocketMessage'](
|
||||
JSON.stringify({
|
||||
id: 'req_git_abort_merge',
|
||||
method: 'git.abortMerge',
|
||||
deviceToken: mobile.token,
|
||||
params: { worktree: 'id:wt-1' }
|
||||
}),
|
||||
(response) => replies.push(JSON.parse(response) as Record<string, unknown>),
|
||||
() => {}
|
||||
)
|
||||
await server['handleWebSocketMessage'](
|
||||
JSON.stringify({
|
||||
id: 'req_git_bulk_unstage',
|
||||
@@ -1612,6 +1624,7 @@ describe('OrcaRuntimeRpcServer', () => {
|
||||
expect.objectContaining({ id: 'req_git_rebase_from_base', ok: true })
|
||||
)
|
||||
expect(replies).toContainEqual(expect.objectContaining({ id: 'req_git_bulk_stage', ok: true }))
|
||||
expect(replies).toContainEqual(expect.objectContaining({ id: 'req_git_abort_merge', ok: true }))
|
||||
expect(replies).toContainEqual(
|
||||
expect.objectContaining({ id: 'req_git_bulk_unstage', ok: true })
|
||||
)
|
||||
@@ -1646,6 +1659,7 @@ describe('OrcaRuntimeRpcServer', () => {
|
||||
expect(pushRuntimeGit).toHaveBeenCalledWith('id:wt-1', true, undefined, undefined)
|
||||
expect(getRuntimeGitUpstreamStatus).toHaveBeenCalledWith('id:wt-1')
|
||||
expect(bulkStageRuntimeGitPaths).toHaveBeenCalledWith('id:wt-1', ['a.ts', 'b.ts'])
|
||||
expect(abortRuntimeGitMerge).toHaveBeenCalledWith('id:wt-1')
|
||||
expect(bulkUnstageRuntimeGitPaths).toHaveBeenCalledWith('id:wt-1', ['c.ts'])
|
||||
expect(openMobileDiff).toHaveBeenCalledWith('id:wt-1', 'docs/readme.md', true)
|
||||
expect(getRuntimeGitDiff).toHaveBeenCalledWith('id:wt-1', 'docs/readme.md', false, undefined)
|
||||
|
||||
@@ -145,6 +145,7 @@ const MOBILE_RPC_METHOD_ALLOWLIST = new Set([
|
||||
'files.open',
|
||||
'files.openDiff',
|
||||
'files.read',
|
||||
'git.abortMerge',
|
||||
'git.bulkStage',
|
||||
'git.bulkUnstage',
|
||||
'git.commit',
|
||||
|
||||
Reference in New Issue
Block a user