Files
orca/src/shared/git-remote-branch-name.ts
T
Jinjing 692e0b0ab9 Push existing fork review branches without republishing (#5531)
* Enable pushing to configured push targets for existing fork reviews

* Resolve push targets using branch.pushRemote, remote.pushDefault,
  and URL-valued remotes normalized to matching named remotes.
* Support this push target resolution on both local and relay/SSH
  git handlers to prevent drift in SSH worktrees.
* Offer "Push" instead of "Publish Branch" in the Source Control UI
  when a linked review exists and a valid push target is available.
* Prevent pushing a feature branch's base branch (e.g. main) directly
  to a fork via remote.pushDefault.

* Keep fork push target when contributor branch matches base branch name

Ensure that a fork push target is not incorrectly discarded when its
branch name matches the base branch name on another remote (for example,
targeting fork/main while the base is origin/main).

Previously, the matching logic only compared the branch leaf name, which
treated different remotes as identical and disabled the push target. We
now qualify the ref comparison with the remote name to differentiate them.
2026-06-16 18:10:01 -07:00

61 lines
1.8 KiB
TypeScript

export function splitRemoteBranchName(refName: string): {
remoteName: string
branchName: string
} | null {
const slashIndex = refName.indexOf('/')
if (slashIndex <= 0 || slashIndex === refName.length - 1) {
return null
}
return {
remoteName: refName.slice(0, slashIndex),
branchName: refName.slice(slashIndex + 1)
}
}
export function gitRefTargetsBranchName(
refName: string | null | undefined,
branchName: string
): boolean {
const trimmed = refName?.trim()
if (!trimmed || !branchName) {
return false
}
const headsPrefix = 'refs/heads/'
if (trimmed.startsWith(headsPrefix)) {
return trimmed.slice(headsPrefix.length) === branchName
}
const remotesPrefix = 'refs/remotes/'
if (trimmed.startsWith(remotesPrefix)) {
return splitRemoteBranchName(trimmed.slice(remotesPrefix.length))?.branchName === branchName
}
return trimmed === branchName || splitRemoteBranchName(trimmed)?.branchName === branchName
}
export function gitRefTargetsBranchOnRemote(
refName: string | null | undefined,
remoteName: string,
branchName: string
): boolean {
const trimmed = refName?.trim()
if (!trimmed || !remoteName || !branchName) {
return false
}
// Why: fork reviews can target fork/main while the saved base is origin/main.
// Remote-qualified refs must match both pieces, not only the branch leaf.
if (
trimmed === `${remoteName}/${branchName}` ||
trimmed === `remotes/${remoteName}/${branchName}` ||
trimmed === `refs/remotes/${remoteName}/${branchName}`
) {
return true
}
if (trimmed.startsWith('refs/remotes/') || trimmed.startsWith('remotes/')) {
return false
}
const headsPrefix = 'refs/heads/'
if (trimmed.startsWith(headsPrefix)) {
return trimmed.slice(headsPrefix.length) === branchName
}
return trimmed === branchName
}