From 5903b4a7423be6af701adfca1c0956490fcd52a7 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Tue, 16 Jun 2026 20:13:46 -0700 Subject: [PATCH] fix: address pull reconciliation policy errors (#5562) --- src/main/git/remote.ts | 5 +- .../git-handler-pull-reconciliation.test.ts | 123 +++++++++++++++++ src/relay/git-handler.ts | 6 +- .../right-sidebar/CommitArea.test.tsx | 16 +++ .../right-sidebar/SourceControl.tsx | 10 +- ...ource-control-pull-policy-error-notice.tsx | 128 ++++++++++++++++++ src/shared/git-remote-error.test.ts | 14 ++ src/shared/git-remote-error.ts | 10 ++ 8 files changed, 304 insertions(+), 8 deletions(-) create mode 100644 src/relay/git-handler-pull-reconciliation.test.ts create mode 100644 src/renderer/src/components/right-sidebar/source-control-pull-policy-error-notice.tsx diff --git a/src/main/git/remote.ts b/src/main/git/remote.ts index 95f76ee96..848751995 100644 --- a/src/main/git/remote.ts +++ b/src/main/git/remote.ts @@ -213,9 +213,8 @@ async function gitPullWithArgs( } export async function gitPull(worktreePath: string, pushTarget?: GitPushTarget): Promise { - // 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. + // Why: plain `git pull` honors the user's configured merge/rebase/ff policy. + // If no policy exists, Git's policy error is normalized with setup guidance. await gitPullWithArgs(worktreePath, [], pushTarget) } diff --git a/src/relay/git-handler-pull-reconciliation.test.ts b/src/relay/git-handler-pull-reconciliation.test.ts new file mode 100644 index 000000000..2803301d5 --- /dev/null +++ b/src/relay/git-handler-pull-reconciliation.test.ts @@ -0,0 +1,123 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { execFileSync } from 'child_process' +import { existsSync, mkdtempSync, writeFileSync } from 'fs' +import * as fs from 'fs/promises' +import * as path from 'path' +import { tmpdir } from 'os' +import { RelayContext } from './context' +import { GitHandler } from './git-handler' +import { + createMockDispatcher, + type MockDispatcher, + type RelayDispatcher +} from './git-handler-test-setup' + +describe('GitHandler pull reconciliation', () => { + let dispatcher: MockDispatcher + let tmpDir: string + let gitEnv: NodeJS.ProcessEnv + let previousGitConfigGlobal: string | undefined + let previousGitConfigNosystem: string | undefined + + beforeEach(() => { + tmpDir = mkdtempSync(path.join(tmpdir(), 'relay-git-pull-reconciliation-')) + const globalGitConfigPath = path.join(tmpDir, 'global-gitconfig') + writeFileSync(globalGitConfigPath, '') + previousGitConfigGlobal = process.env.GIT_CONFIG_GLOBAL + previousGitConfigNosystem = process.env.GIT_CONFIG_NOSYSTEM + process.env.GIT_CONFIG_GLOBAL = globalGitConfigPath + process.env.GIT_CONFIG_NOSYSTEM = '1' + gitEnv = { + ...process.env, + GIT_CONFIG_GLOBAL: globalGitConfigPath, + GIT_CONFIG_NOSYSTEM: '1' + } + dispatcher = createMockDispatcher() + const ctx = new RelayContext() + new GitHandler(dispatcher as unknown as RelayDispatcher, ctx) + }) + + afterEach(async () => { + restoreGitEnv('GIT_CONFIG_GLOBAL', previousGitConfigGlobal) + restoreGitEnv('GIT_CONFIG_NOSYSTEM', previousGitConfigNosystem) + await fs.rm(tmpDir, { recursive: true, force: true }) + }) + + function execGit(cwd: string, args: string[]): string { + return execFileSync('git', args, { + cwd, + encoding: 'utf-8', + env: gitEnv, + stdio: 'pipe' + }) + } + + function configureIdentity(cwd: string): void { + execGit(cwd, ['config', 'user.email', 'test@test.com']) + execGit(cwd, ['config', 'user.name', 'Test']) + } + + function commitAll(cwd: string, message: string): void { + execGit(cwd, ['add', '.']) + execGit(cwd, ['commit', '-m', message]) + } + + function createDivergentFixture(): string { + const bareDir = path.join(tmpDir, 'origin.git') + const consumerDir = path.join(tmpDir, 'consumer') + const producerDir = path.join(tmpDir, 'producer') + + execGit(tmpDir, ['init', '--bare', bareDir]) + execGit(tmpDir, ['clone', bareDir, consumerDir]) + configureIdentity(consumerDir) + writeFileSync(path.join(consumerDir, 'base.txt'), 'base\n') + commitAll(consumerDir, 'initial') + execGit(consumerDir, ['push', '--set-upstream', 'origin', 'HEAD']) + + execGit(tmpDir, ['clone', bareDir, producerDir]) + configureIdentity(producerDir) + writeFileSync(path.join(producerDir, 'remote.txt'), 'remote\n') + commitAll(producerDir, 'remote') + execGit(producerDir, ['push']) + + writeFileSync(path.join(consumerDir, 'local.txt'), 'local\n') + commitAll(consumerDir, 'local') + + return consumerDir + } + + it('explains how to configure a pull policy when divergent branches have no strategy', async () => { + const consumerDir = createDivergentFixture() + + await expect(dispatcher.callRequest('git.pull', { worktreePath: consumerDir })).rejects.toThrow( + 'Pull needs a Git pull policy for divergent branches. Configure one for this repository' + ) + + expect(execGit(consumerDir, ['status', '--short'])).toBe('') + }) + + it('preserves configured rebase pull semantics', async () => { + const consumerDir = createDivergentFixture() + execGit(consumerDir, ['config', 'pull.rebase', 'true']) + + await expect( + dispatcher.callRequest('git.pull', { worktreePath: consumerDir }) + ).resolves.not.toThrow() + + const parentRefs = execGit(consumerDir, ['log', '-1', '--pretty=%P']).trim().split(/\s+/) + expect(parentRefs).toHaveLength(1) + expect(existsSync(path.join(consumerDir, 'remote.txt'))).toBe(true) + expect(execGit(consumerDir, ['status', '--short'])).toBe('') + }) +}) + +function restoreGitEnv( + name: 'GIT_CONFIG_GLOBAL' | 'GIT_CONFIG_NOSYSTEM', + value: string | undefined +): void { + if (value === undefined) { + delete process.env[name] + return + } + process.env[name] = value +} diff --git a/src/relay/git-handler.ts b/src/relay/git-handler.ts index 75dfa806e..cbe91ce3b 100644 --- a/src/relay/git-handler.ts +++ b/src/relay/git-handler.ts @@ -28,7 +28,7 @@ import { import { refreshLocalBaseRefForWorktreeCreateOp } from './git-handler-local-base-ref-refresh' import { checkIgnoredPathsOp, detectConflictOperation, getStatusOp } from './git-handler-status-ops' import { resolveRelayPushTarget } from './git-handler-push-target' -import { normalizeGitErrorMessage, isNoUpstreamError } from '../shared/git-remote-error' +import { isNoUpstreamError, normalizeGitErrorMessage } from '../shared/git-remote-error' import { upstreamOnlyCommitsArePatchEquivalent } from '../shared/git-upstream-status' import { assertGitPushTargetShape } from '../shared/git-push-target-validation' import { getPublishTargetStatus, type GitCommandRunner } from '../shared/git-publish-target-status' @@ -658,8 +658,8 @@ export class GitHandler { } private async pull(params: Record) { - // Why: plain `git pull` uses the user's configured pull strategy (merge by - // default) so diverged branches reconcile instead of erroring out. + // Why: plain `git pull` honors the user's configured merge/rebase/ff policy. + // If no policy exists, Git's policy error is normalized with setup guidance. await this.pullWithArgs(params, []) } diff --git a/src/renderer/src/components/right-sidebar/CommitArea.test.tsx b/src/renderer/src/components/right-sidebar/CommitArea.test.tsx index 816543f56..779ba9520 100644 --- a/src/renderer/src/components/right-sidebar/CommitArea.test.tsx +++ b/src/renderer/src/components/right-sidebar/CommitArea.test.tsx @@ -228,6 +228,22 @@ describe('CommitArea', () => { expect(markup).toContain('commit-area-remote-error') }) + it('formats pull policy errors with command options', () => { + const markup = renderCommitArea({ + ...baseProps(), + remoteActionError: + 'Pull needs a Git pull policy for divergent branches. Configure one for this repository or host, then try again: git config pull.rebase false (merge), git config pull.rebase true (rebase), or git config pull.ff only (fast-forward only).' + }) + + expect(markup).toContain('Pull needs a policy') + expect(markup).toContain('Diverged') + expect(markup).toContain('git config pull.rebase false') + expect(markup).toContain('git config pull.rebase true') + expect(markup).toContain('git config pull.ff only') + expect(markup).toContain('aria-label="Copy merge pull policy command"') + expect(markup).toContain('commit-area-remote-error') + }) + it('keeps generation errors separate from commit and remote errors', () => { const markup = renderCommitArea({ ...baseProps(), diff --git a/src/renderer/src/components/right-sidebar/SourceControl.tsx b/src/renderer/src/components/right-sidebar/SourceControl.tsx index f66f3df6e..b52cda33e 100644 --- a/src/renderer/src/components/right-sidebar/SourceControl.tsx +++ b/src/renderer/src/components/right-sidebar/SourceControl.tsx @@ -197,6 +197,10 @@ import { type SourceControlRowOpenEvent } from './source-control-split-open' import { SourceControlAgentActionDialog } from './SourceControlAgentActionDialog' +import { + isPullPolicyRemoteActionError, + PullPolicyRemoteActionNotice +} from './source-control-pull-policy-error-notice' import { SourceControlTextGenerationDialog } from './SourceControlTextGenerationDialog' import { CreateHostedReviewComposer } from './CreateHostedReviewComposer' import { @@ -6356,7 +6360,9 @@ export function CommitArea({ )} - {remoteActionError && ( + {remoteActionError && isPullPolicyRemoteActionError(remoteActionError) ? ( + + ) : remoteActionError ? ( - )} + ) : null} {createPrIntentNotice && (
(null) + + useEffect(() => { + if (!copiedCommand) { + return + } + const timeout = window.setTimeout(() => setCopiedCommand(null), 1400) + return () => window.clearTimeout(timeout) + }, [copiedCommand]) + + const handleCopyCommand = useCallback((command: string) => { + void window.api.ui.writeClipboardText(command) + setCopiedCommand(command) + }, []) + + return ( +