feat(source-control): commit, push, pull, and sync actions in panel (#1211)
Co-authored-by: Orca <help@stably.ai> Co-authored-by: Alexander Saavedra <mralexsaavedra@gmail.com> Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com> Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: brennanb2025 <brennankbenson@gmail.com>
This commit is contained in:
co-authored by
Orca
Neil
Brennan Benson
Jinjing
github-actions[bot]
brennanb2025
parent
5c7e5f2f1d
commit
9f8bf81c38
@@ -0,0 +1,134 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { gitExecFileAsyncMock } = vi.hoisted(() => ({
|
||||
gitExecFileAsyncMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('./runner', () => ({
|
||||
gitExecFileAsync: gitExecFileAsyncMock
|
||||
}))
|
||||
|
||||
import { gitFetch, gitPull, gitPush } from './remote'
|
||||
|
||||
describe('git remote operations', () => {
|
||||
beforeEach(() => {
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
})
|
||||
|
||||
it('pushes with --set-upstream regardless of publish flag', async () => {
|
||||
// Why: every push uses --set-upstream so worktrees that were created
|
||||
// tracking the BASE ref (origin/main) get their upstream repointed to
|
||||
// origin/<branch> on first push. Without that the local branch keeps
|
||||
// tracking origin/main forever and the UI's ahead/behind read via
|
||||
// @{u} measures "ahead of base" rather than "ahead of remote branch".
|
||||
// Both publish=true and publish=false take the same path now; the
|
||||
// parameter is preserved in the signature for IPC compatibility but
|
||||
// is no longer load-bearing.
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: '', stderr: '' })
|
||||
|
||||
await gitPush('/repo', true)
|
||||
await gitPush('/repo', false)
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
['push', '--set-upstream', 'origin', 'HEAD'],
|
||||
{ cwd: '/repo' }
|
||||
)
|
||||
expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
['push', '--set-upstream', 'origin', 'HEAD'],
|
||||
{ cwd: '/repo' }
|
||||
)
|
||||
})
|
||||
|
||||
it('maps non-fast-forward push failures to an actionable message', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(new Error('remote rejected: non-fast-forward'))
|
||||
|
||||
await expect(gitPush('/repo', false)).rejects.toThrow(
|
||||
'Push rejected: remote has newer commits (non-fast-forward). Please pull or sync first.'
|
||||
)
|
||||
})
|
||||
|
||||
it('passes through clean tail line when push error does not match known patterns', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(
|
||||
new Error('Command failed: git push\nfatal: something obscure happened')
|
||||
)
|
||||
|
||||
await expect(gitPush('/repo', false)).rejects.toThrow('fatal: something obscure happened')
|
||||
})
|
||||
|
||||
it('strips embedded credentials from push error messages', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(
|
||||
new Error(
|
||||
'Command failed: git push\nhttps://x-access-token:ghp_abc@github.com/foo/bar.git\nfatal: remote error'
|
||||
)
|
||||
)
|
||||
|
||||
let caught: Error | undefined
|
||||
try {
|
||||
await gitPush('/repo', false)
|
||||
} catch (error) {
|
||||
caught = error as Error
|
||||
}
|
||||
|
||||
expect(caught).toBeInstanceOf(Error)
|
||||
expect(caught?.message).not.toContain('ghp_abc')
|
||||
expect(caught?.message).not.toContain('x-access-token')
|
||||
})
|
||||
|
||||
it('strips token-only credentials (https://TOKEN@host) from push error messages', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(
|
||||
new Error(
|
||||
'Command failed: git push\nhttps://ghp_onlyToken@github.com/foo/bar.git\nfatal: remote error'
|
||||
)
|
||||
)
|
||||
|
||||
let caught: Error | undefined
|
||||
try {
|
||||
await gitPush('/repo', false)
|
||||
} catch (error) {
|
||||
caught = error as Error
|
||||
}
|
||||
|
||||
expect(caught).toBeInstanceOf(Error)
|
||||
expect(caught?.message).not.toContain('ghp_onlyToken')
|
||||
})
|
||||
|
||||
it('falls back to a generic message for non-Error rejections', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce('string')
|
||||
|
||||
await expect(gitPush('/repo', false)).rejects.toThrow('Git remote operation failed.')
|
||||
})
|
||||
|
||||
it("runs pull with the user's configured strategy", async () => {
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: '', stderr: '' })
|
||||
|
||||
await gitPull('/repo')
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['pull'], { cwd: '/repo' })
|
||||
})
|
||||
|
||||
it('normalizes pull authentication errors to a friendly message', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(new Error('Authentication failed'))
|
||||
|
||||
await expect(gitPull('/repo')).rejects.toThrow(
|
||||
'Authentication failed. Check your remote credentials.'
|
||||
)
|
||||
})
|
||||
|
||||
it('runs fetch with prune', async () => {
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: '', stderr: '' })
|
||||
|
||||
await gitFetch('/repo')
|
||||
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(['fetch', '--prune'], { cwd: '/repo' })
|
||||
})
|
||||
|
||||
it('normalizes fetch authentication errors to a friendly message', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(new Error('Authentication failed'))
|
||||
|
||||
await expect(gitFetch('/repo')).rejects.toThrow(
|
||||
'Authentication failed. Check your remote credentials.'
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,46 @@
|
||||
import { normalizeGitErrorMessage } from '../../shared/git-remote-error'
|
||||
import { gitExecFileAsync } from './runner'
|
||||
|
||||
export async function gitPush(worktreePath: string, _publish = false): Promise<void> {
|
||||
try {
|
||||
// Why: always pass --set-upstream so that worktrees Orca creates with
|
||||
// `git worktree add --track -b <name> <dir> <baseRef>` (which initially
|
||||
// track the BASE — e.g. origin/main) get their upstream repointed to
|
||||
// origin/<branch> on first push. Without this the local branch keeps
|
||||
// tracking origin/main forever, so ahead/behind reads via @{u} measure
|
||||
// "ahead of base" rather than "ahead of remote branch", and the primary
|
||||
// button never rotates from "Push" to "Commit" after a successful push.
|
||||
//
|
||||
// The `publish` flag becomes redundant under this strategy — every push
|
||||
// sets upstream, including the first. We keep the parameter in the
|
||||
// signature so callers don't need to change, but it's no longer
|
||||
// load-bearing. On an already-published branch --set-upstream is a
|
||||
// no-op for the tracking config and a regular push otherwise.
|
||||
//
|
||||
// Branch-vs-base reporting (the "Committed on Branch" section) is
|
||||
// unaffected because it uses branchCompare against an explicit baseRef
|
||||
// from worktree config, not the upstream relationship.
|
||||
await gitExecFileAsync(['push', '--set-upstream', 'origin', 'HEAD'], { cwd: worktreePath })
|
||||
} catch (error) {
|
||||
throw new Error(normalizeGitErrorMessage(error, 'push'))
|
||||
}
|
||||
}
|
||||
|
||||
export async function gitPull(worktreePath: string): Promise<void> {
|
||||
// Why: plain `git pull` uses the user's configured pull strategy (merge by
|
||||
// default) so diverged branches reconcile instead of erroring out. Conflicts
|
||||
// surface through the existing conflict-resolution flow.
|
||||
try {
|
||||
await gitExecFileAsync(['pull'], { cwd: worktreePath })
|
||||
} catch (error) {
|
||||
throw new Error(normalizeGitErrorMessage(error, 'pull'))
|
||||
}
|
||||
}
|
||||
|
||||
export async function gitFetch(worktreePath: string): Promise<void> {
|
||||
try {
|
||||
await gitExecFileAsync(['fetch', '--prune'], { cwd: worktreePath })
|
||||
} catch (error) {
|
||||
throw new Error(normalizeGitErrorMessage(error, 'fetch'))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { gitExecFileAsyncMock } = vi.hoisted(() => ({
|
||||
gitExecFileAsyncMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('./runner', () => ({
|
||||
gitExecFileAsync: gitExecFileAsyncMock
|
||||
}))
|
||||
|
||||
import { getUpstreamStatus } from './upstream'
|
||||
|
||||
const missingTrackingRefError = new Error(
|
||||
"fatal: ambiguous argument 'HEAD@{u}': unknown revision or path not in the working tree.\n" +
|
||||
"Use '--' to separate paths from revisions, like this:\n" +
|
||||
"'git <command> [<revision>...] -- [<file>...]'"
|
||||
)
|
||||
|
||||
describe('getUpstreamStatus', () => {
|
||||
beforeEach(() => {
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
})
|
||||
|
||||
it('returns upstream and ahead/behind counts when tracking is configured', async () => {
|
||||
gitExecFileAsyncMock
|
||||
.mockResolvedValueOnce({ stdout: 'origin/main\n' })
|
||||
.mockResolvedValueOnce({ stdout: '2\t3\n' })
|
||||
|
||||
const result = await getUpstreamStatus('/repo')
|
||||
|
||||
expect(result).toEqual({
|
||||
hasUpstream: true,
|
||||
upstreamName: 'origin/main',
|
||||
ahead: 2,
|
||||
behind: 3
|
||||
})
|
||||
})
|
||||
|
||||
it('returns hasUpstream=false when upstream is missing', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(new Error('fatal: no upstream configured'))
|
||||
|
||||
const result = await getUpstreamStatus('/repo')
|
||||
|
||||
expect(result).toEqual({
|
||||
hasUpstream: false,
|
||||
ahead: 0,
|
||||
behind: 0
|
||||
})
|
||||
})
|
||||
|
||||
it('returns hasUpstream=false when the configured tracking ref is missing', async () => {
|
||||
gitExecFileAsyncMock.mockRejectedValueOnce(missingTrackingRefError)
|
||||
|
||||
const result = await getUpstreamStatus('/repo')
|
||||
|
||||
expect(result).toEqual({
|
||||
hasUpstream: false,
|
||||
ahead: 0,
|
||||
behind: 0
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,61 @@
|
||||
import type { GitUpstreamStatus } from '../../shared/types'
|
||||
import { isNoUpstreamError, normalizeGitErrorMessage } from '../../shared/git-remote-error'
|
||||
import { gitExecFileAsync } from './runner'
|
||||
|
||||
export async function getUpstreamStatus(worktreePath: string): Promise<GitUpstreamStatus> {
|
||||
try {
|
||||
const { stdout: upstreamStdout } = await gitExecFileAsync(
|
||||
['rev-parse', '--abbrev-ref', 'HEAD@{u}'],
|
||||
{
|
||||
cwd: worktreePath
|
||||
}
|
||||
)
|
||||
const upstreamName = upstreamStdout.trim()
|
||||
if (!upstreamName) {
|
||||
return { hasUpstream: false, ahead: 0, behind: 0 }
|
||||
}
|
||||
|
||||
const { stdout: countsStdout } = await gitExecFileAsync(
|
||||
['rev-list', '--left-right', '--count', 'HEAD...@{u}'],
|
||||
{
|
||||
cwd: worktreePath
|
||||
}
|
||||
)
|
||||
|
||||
const tokens = countsStdout.trim().split(/\s+/)
|
||||
if (tokens.length !== 2) {
|
||||
// Why: 'rev-list --left-right --count HEAD...@{u}' must emit exactly two
|
||||
// tokens; anything else (empty stdout, truncation, unexpected locale) is a
|
||||
// real failure and must not be silently reported as "in sync" 0/0.
|
||||
throw new Error(`Unexpected git rev-list output: ${JSON.stringify(countsStdout)}`)
|
||||
}
|
||||
const ahead = Number.parseInt(tokens[0]!, 10)
|
||||
const behind = Number.parseInt(tokens[1]!, 10)
|
||||
if (!Number.isFinite(ahead) || !Number.isFinite(behind) || ahead < 0 || behind < 0) {
|
||||
throw new Error(`Unparseable git rev-list counts: ${JSON.stringify(countsStdout)}`)
|
||||
}
|
||||
|
||||
return {
|
||||
hasUpstream: true,
|
||||
upstreamName,
|
||||
ahead,
|
||||
behind
|
||||
}
|
||||
} catch (error) {
|
||||
// Why: we only swallow clearly-no-upstream signals — that's an expected
|
||||
// state, not a failure. Other errors (auth, corruption, "not a git
|
||||
// repository", sparse-checkout) should surface to the user so they can
|
||||
// act on them. The shared isNoUpstreamError helper intentionally omits
|
||||
// broad phrases like "no such branch" to avoid masking real errors.
|
||||
if (isNoUpstreamError(error)) {
|
||||
return {
|
||||
hasUpstream: false,
|
||||
ahead: 0,
|
||||
behind: 0
|
||||
}
|
||||
}
|
||||
// Why: parity with gitPush/gitPull/gitFetch — normalize before crossing
|
||||
// the IPC boundary so renderers don't see execFile stderr preambles or local paths.
|
||||
throw new Error(normalizeGitErrorMessage(error, 'upstream'))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user