From e6c55d76e68e7ef7c2eb13cbd7804ec9569d5ba8 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 04:45:39 -0700 Subject: [PATCH] fix: resolve slashed upstream remotes (#4114) --- src/main/git/upstream.test.ts | 33 ++++++++++++++++++++ src/shared/git-effective-upstream.ts | 45 +++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/git/upstream.test.ts b/src/main/git/upstream.test.ts index a6026ed7c..906c7e3ab 100644 --- a/src/main/git/upstream.test.ts +++ b/src/main/git/upstream.test.ts @@ -141,6 +141,39 @@ describe('getUpstreamStatus', () => { }) }) + it('keeps a configured upstream whose remote name contains a slash', async () => { + gitExecFileAsyncMock.mockImplementation((args: string[]) => { + if (args[0] === 'symbolic-ref') { + return Promise.resolve({ stdout: 'feature\n' }) + } + if (args[0] === 'rev-parse' && args.includes('HEAD@{u}')) { + return Promise.resolve({ stdout: 'origin/team/feature\n' }) + } + if (args[0] === 'remote') { + return Promise.resolve({ stdout: 'origin\norigin/team\n' }) + } + if (args[0] === 'rev-parse' && args.includes('refs/remotes/origin/feature')) { + return Promise.resolve({ stdout: 'origin-feature-oid\n' }) + } + if (args[0] === 'rev-list' && args.includes('HEAD...origin/team/feature')) { + return Promise.resolve({ stdout: '2\t0\n' }) + } + if (args[0] === 'rev-list' && args.includes('HEAD...origin/feature')) { + return Promise.resolve({ stdout: '9\t9\n' }) + } + throw new Error(`unexpected git args: ${args.join(' ')}`) + }) + + const result = await getUpstreamStatus('/repo') + + expect(result).toEqual({ + hasUpstream: true, + upstreamName: 'origin/team/feature', + ahead: 2, + behind: 0 + }) + }) + it('uses an explicit publish target instead of the configured upstream', async () => { gitExecFileAsyncMock .mockResolvedValueOnce({ stdout: '', stderr: '' }) diff --git a/src/shared/git-effective-upstream.ts b/src/shared/git-effective-upstream.ts index ad3e09325..05203316a 100644 --- a/src/shared/git-effective-upstream.ts +++ b/src/shared/git-effective-upstream.ts @@ -31,6 +31,37 @@ export function splitRemoteBranchName(refName: string): { } } +function hasMultipleSlashSegments(refName: string): boolean { + return refName.includes('/') && refName.indexOf('/') !== refName.lastIndexOf('/') +} + +async function splitRemoteBranchNameByKnownRemote( + runGit: GitCommandRunner, + refName: string +): Promise<{ remoteName: string; branchName: string } | null> { + try { + const { stdout } = await runGit(['remote']) + const remotes = stdout + .split(/\r?\n/) + .map((line) => line.trim()) + .filter(Boolean) + .sort((a, b) => b.length - a.length) + return ( + remotes + .map((remoteName) => { + if (refName === remoteName || !refName.startsWith(`${remoteName}/`)) { + return null + } + const branchName = refName.slice(remoteName.length + 1) + return branchName ? { remoteName, branchName } : null + }) + .find((parsed) => parsed !== null) ?? null + ) + } catch { + return null + } +} + async function getCurrentBranchName(runGit: GitCommandRunner): Promise { try { const { stdout } = await runGit(['symbolic-ref', '--quiet', '--short', 'HEAD']) @@ -90,9 +121,21 @@ export async function resolveEffectiveGitUpstream( runGit: GitCommandRunner ): Promise { const currentBranchName = await getCurrentBranchName(runGit) - const configured = await getConfiguredUpstream(runGit) + let configured = await getConfiguredUpstream(runGit) if (configured) { + if ( + currentBranchName && + configured.remoteName === 'origin' && + configured.branchName !== currentBranchName && + hasMultipleSlashSegments(configured.upstreamName) + ) { + const parsed = await splitRemoteBranchNameByKnownRemote(runGit, configured.upstreamName) + if (parsed) { + configured = { ...configured, ...parsed } + } + } + if (!currentBranchName || configured.branchName === currentBranchName) { return configured }