fix: detect branch conflicts for slashed remotes (#4080)
This commit is contained in:
@@ -216,6 +216,16 @@ describe('searchBaseRefs (widened glob)', () => {
|
||||
createRemoteRef(tmpDir, 'origin/feature/something', sha)
|
||||
createRemoteRef(tmpDir, 'upstream/feature/something', sha)
|
||||
|
||||
expect(
|
||||
await getBranchConflictKind(tmpDir, 'feature/something', 'origin/feature/something')
|
||||
).toBe('remote')
|
||||
})
|
||||
|
||||
it('reports remote conflicts when the remote name contains a slash', async () => {
|
||||
const sha = getHeadSha(tmpDir)
|
||||
git(tmpDir, ['remote', 'add', 'foo/bar', 'https://example.invalid/repo.git'])
|
||||
createRemoteRef(tmpDir, 'foo/bar/feature/something', sha)
|
||||
|
||||
const result = await getBranchConflictKind(
|
||||
tmpDir,
|
||||
'feature/something',
|
||||
|
||||
@@ -772,20 +772,21 @@ export async function getBranchConflictKind(
|
||||
}
|
||||
|
||||
try {
|
||||
const remoteNames = (await listRemoteNames(path)).sort((a, b) => b.length - a.length)
|
||||
const { stdout } = await gitExecFileAsync(
|
||||
['for-each-ref', '--format=%(refname)', 'refs/remotes'],
|
||||
{ cwd: path }
|
||||
)
|
||||
// Why: refs have the form refs/remotes/<remote>/<branch>. We strip the
|
||||
// first three segments so that e.g. "feature/dashboard" only matches
|
||||
// "refs/remotes/origin/feature/dashboard", not "refs/remotes/origin/other/feature/dashboard".
|
||||
const hasRemoteConflict = stdout.split('\n').some((ref) => {
|
||||
const trimmed = ref.trim()
|
||||
if (isAllowedRemoteBaseRef(trimmed, allowedBaseRef)) {
|
||||
return false
|
||||
}
|
||||
const parts = trimmed.split('/')
|
||||
return parts.slice(3).join('/') === branchName
|
||||
const shortRef = trimmed.replace(/^refs\/remotes\//, '')
|
||||
// Why: git allows slashes in remote names. Use the configured remote
|
||||
// list so foo/bar/feature resolves as branch "feature" for remote
|
||||
// "foo/bar", matching searchBaseRefDetails.
|
||||
return resolveLocalBranchName(trimmed, shortRef, remoteNames) === branchName
|
||||
})
|
||||
|
||||
return hasRemoteConflict ? 'remote' : null
|
||||
|
||||
Reference in New Issue
Block a user