fix: address review findings (#1770)

This commit is contained in:
Jinjing
2026-05-13 16:14:50 -07:00
committed by GitHub
parent 575b1d42ed
commit 5a6b2994ea
24 changed files with 793 additions and 94 deletions
+12
View File
@@ -0,0 +1,12 @@
import type { GitPushTarget } from '../../shared/types'
import { assertGitPushTargetShape } from '../../shared/git-push-target-validation'
import { gitExecFileAsync } from './runner'
export async function validateGitPushTarget(
repoPath: string,
target: unknown
): Promise<GitPushTarget> {
assertGitPushTargetShape(target)
await gitExecFileAsync(['check-ref-format', '--branch', target.branchName], { cwd: repoPath })
return target
}
+69 -30
View File
@@ -15,34 +15,65 @@ describe('git remote operations', () => {
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.
it('pushes to origin when no upstream is configured', async () => {
gitExecFileAsyncMock.mockResolvedValue({ stdout: '', stderr: '' })
gitExecFileAsyncMock.mockRejectedValueOnce(Object.assign(new Error('no branch'), { code: 1 }))
await gitPush('/repo', true)
await gitPush('/repo', false)
expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith(
1,
['push', '--set-upstream', 'origin', 'HEAD'],
{ cwd: '/repo' }
)
expect(gitExecFileAsyncMock).toHaveBeenNthCalledWith(
2,
expect(gitExecFileAsyncMock).toHaveBeenLastCalledWith(
['push', '--set-upstream', 'origin', 'HEAD'],
{ cwd: '/repo' }
)
})
it('pushes to the configured upstream remote and branch', async () => {
gitExecFileAsyncMock
.mockResolvedValueOnce({ stdout: 'review/pr-1738\n', stderr: '' })
.mockResolvedValueOnce({ stdout: 'pr-prateek-orca\n', stderr: '' })
.mockResolvedValueOnce({
stdout: 'refs/heads/prateek/fix-sidebar-agents-toggle\n',
stderr: ''
})
.mockResolvedValueOnce({ stdout: '', stderr: '' })
await gitPush('/repo', false)
expect(gitExecFileAsyncMock.mock.calls).toEqual([
[['symbolic-ref', '--quiet', '--short', 'HEAD'], { cwd: '/repo' }],
[['config', '--get', 'branch.review/pr-1738.remote'], { cwd: '/repo' }],
[['config', '--get', 'branch.review/pr-1738.merge'], { cwd: '/repo' }],
[
['push', '--set-upstream', 'pr-prateek-orca', 'HEAD:prateek/fix-sidebar-agents-toggle'],
{ cwd: '/repo' }
]
])
})
it('uses an explicit push target even when it differs from the local branch name', async () => {
gitExecFileAsyncMock
.mockResolvedValueOnce({ stdout: '', stderr: '' })
.mockResolvedValueOnce({ stdout: '', stderr: '' })
await gitPush('/repo', false, {
remoteName: 'origin',
branchName: 'contributor/fix-sidebar'
})
expect(gitExecFileAsyncMock).toHaveBeenCalledWith(
['push', '--set-upstream', 'origin', 'HEAD:contributor/fix-sidebar'],
{ cwd: '/repo' }
)
expect(gitExecFileAsyncMock.mock.calls).toEqual([
[['check-ref-format', '--branch', 'contributor/fix-sidebar'], { cwd: '/repo' }],
[['push', '--set-upstream', 'origin', 'HEAD:contributor/fix-sidebar'], { cwd: '/repo' }]
])
})
it('maps non-fast-forward push failures to an actionable message', async () => {
gitExecFileAsyncMock.mockRejectedValueOnce(new Error('remote rejected: non-fast-forward'))
gitExecFileAsyncMock
.mockRejectedValueOnce(new Error('no branch'))
.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.'
@@ -50,19 +81,23 @@ describe('git remote operations', () => {
})
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')
)
gitExecFileAsyncMock
.mockRejectedValueOnce(new Error('no branch'))
.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'
gitExecFileAsyncMock
.mockRejectedValueOnce(new Error('no branch'))
.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 {
@@ -77,11 +112,13 @@ describe('git remote operations', () => {
})
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'
gitExecFileAsyncMock
.mockRejectedValueOnce(new Error('no branch'))
.mockRejectedValueOnce(
new Error(
'Command failed: git push\nhttps://ghp_onlyToken@github.com/foo/bar.git\nfatal: remote error'
)
)
)
let caught: Error | undefined
try {
@@ -95,7 +132,9 @@ describe('git remote operations', () => {
})
it('falls back to a generic message for non-Error rejections', async () => {
gitExecFileAsyncMock.mockRejectedValueOnce('string')
gitExecFileAsyncMock
.mockRejectedValueOnce(new Error('no branch'))
.mockRejectedValueOnce('string')
await expect(gitPush('/repo', false)).rejects.toThrow('Git remote operation failed.')
})
+58 -14
View File
@@ -1,26 +1,70 @@
import { normalizeGitErrorMessage } from '../../shared/git-remote-error'
import type { GitPushTarget } from '../../shared/types'
import { validateGitPushTarget } from './push-target-validation'
import { gitExecFileAsync } from './runner'
export async function gitPush(worktreePath: string, _publish = false): Promise<void> {
async function getConfiguredPushTarget(
worktreePath: string
): Promise<{ remote: string; refspec: string } | null> {
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.
const { stdout: branchStdout } = await gitExecFileAsync(
['symbolic-ref', '--quiet', '--short', 'HEAD'],
{ cwd: worktreePath }
)
const branch = branchStdout.trim()
if (!branch) {
return null
}
const [{ stdout: remoteStdout }, { stdout: mergeStdout }] = await Promise.all([
gitExecFileAsync(['config', '--get', `branch.${branch}.remote`], { cwd: worktreePath }),
gitExecFileAsync(['config', '--get', `branch.${branch}.merge`], { cwd: worktreePath })
])
const remote = remoteStdout.trim()
const mergeRef = mergeStdout.trim()
const branchRef = mergeRef.replace(/^refs\/heads\//, '')
if (!remote || !branchRef || remote === '.' || branchRef === mergeRef) {
return null
}
if (remote === 'origin' && branchRef !== branch) {
return null
}
return { remote, refspec: `HEAD:${branchRef}` }
} catch {
return null
}
}
function explicitPushTarget(target: GitPushTarget): { remote: string; refspec: string } {
return { remote: target.remoteName, refspec: `HEAD:${target.branchName}` }
}
export async function gitPush(
worktreePath: string,
_publish = false,
pushTarget?: GitPushTarget
): Promise<void> {
try {
if (pushTarget) {
await validateGitPushTarget(worktreePath, pushTarget)
}
// Why: push to the branch's configured upstream when one exists. PR-created
// worktrees can track a contributor fork remote; hardcoding origin here
// would send review commits to the upstream repository instead.
//
// 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.
// When no upstream exists, keep the existing first-publish behavior:
// create/update origin/<current branch> and set it as upstream.
//
// 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 })
const target = pushTarget
? explicitPushTarget(pushTarget)
: await getConfiguredPushTarget(worktreePath)
const args = target
? ['push', '--set-upstream', target.remote, target.refspec]
: ['push', '--set-upstream', 'origin', 'HEAD']
await gitExecFileAsync(args, { cwd: worktreePath })
} catch (error) {
throw new Error(normalizeGitErrorMessage(error, 'push'))
}