fix: resolve slashed upstream remotes (#4114)
This commit is contained in:
@@ -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: '' })
|
||||
|
||||
@@ -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<string | null> {
|
||||
try {
|
||||
const { stdout } = await runGit(['symbolic-ref', '--quiet', '--short', 'HEAD'])
|
||||
@@ -90,9 +121,21 @@ export async function resolveEffectiveGitUpstream(
|
||||
runGit: GitCommandRunner
|
||||
): Promise<EffectiveGitUpstream | null> {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user