@@ -1599,6 +1599,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15991599 return remoteName ;
16001600 } ) ;
16011601
1602+ const resolveRemoteBaseRef = Effect . fn ( "resolveRemoteBaseRef" ) ( function * (
1603+ cwd : string ,
1604+ baseRef : string ,
1605+ ) {
1606+ if ( ! baseRef . includes ( "/" ) || baseRef . startsWith ( "refs/" ) ) return null ;
1607+ const comparisonRef = `refs/remotes/${ baseRef } ` ;
1608+ const remoteBase = yield * executeGit (
1609+ "GitVcsDriver.resolveRemoteBaseRef" ,
1610+ cwd ,
1611+ [ "show-ref" , "--verify" , "--quiet" , comparisonRef ] ,
1612+ { allowNonZeroExit : true } ,
1613+ ) ;
1614+ return remoteBase . exitCode === 0 ? { baseRef, comparisonRef } : null ;
1615+ } ) ;
1616+
16021617 // `allowRemoteOfCurrent` lets the review diff compare the default branch with its remote copy.
16031618 const resolveBaseBranchForNoUpstream = Effect . fn ( "resolveBaseBranchForNoUpstream" ) ( function * (
16041619 cwd : string ,
@@ -1612,18 +1627,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16121627 true ,
16131628 ) . pipe ( Effect . map ( ( stdout ) => stdout . trim ( ) ) ) ;
16141629
1615- // A configured remote base must keep its remote instead of being resolved against the fork.
1616- if ( configuredBaseBranch . includes ( "/" ) ) {
1617- const configuredRemoteBase = yield * executeGit (
1618- "GitVcsDriver.resolveBaseBranchForNoUpstream.configuredRemote" ,
1619- cwd ,
1620- [ "show-ref" , "--verify" , "--quiet" , `refs/remotes/${ configuredBaseBranch } ` ] ,
1621- { allowNonZeroExit : true } ,
1622- ) ;
1623- if ( configuredRemoteBase . exitCode === 0 ) {
1624- return configuredBaseBranch ;
1625- }
1626- }
1630+ // Keep the configured remote and compare its exact ref even when a local ref shadows it.
1631+ const configuredRemoteBase = yield * resolveRemoteBaseRef ( cwd , configuredBaseBranch ) ;
1632+ if ( configuredRemoteBase !== null ) return configuredRemoteBase ;
16271633
16281634 const primaryRemoteName = yield * resolvePrimaryRemoteName ( cwd ) . pipe (
16291635 Effect . orElseSucceed ( ( ) => null ) ,
@@ -1661,7 +1667,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16611667 refName : normalizedCandidate ,
16621668 } ) )
16631669 ) {
1664- return `${ primaryRemoteName } /${ normalizedCandidate } ` ;
1670+ return {
1671+ baseRef : `${ primaryRemoteName } /${ normalizedCandidate } ` ,
1672+ comparisonRef : `refs/remotes/${ primaryRemoteName } /${ normalizedCandidate } ` ,
1673+ } ;
16651674 }
16661675 continue ;
16671676 }
@@ -1674,11 +1683,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16741683 refName : normalizedCandidate ,
16751684 } ) )
16761685 ) {
1677- return `${ primaryRemoteName } /${ normalizedCandidate } ` ;
1686+ return {
1687+ baseRef : `${ primaryRemoteName } /${ normalizedCandidate } ` ,
1688+ comparisonRef : `refs/remotes/${ primaryRemoteName } /${ normalizedCandidate } ` ,
1689+ } ;
16781690 }
16791691
16801692 if ( yield * branchExists ( cwd , normalizedCandidate ) ) {
1681- return normalizedCandidate ;
1693+ return { baseRef : normalizedCandidate , comparisonRef : `refs/heads/ ${ normalizedCandidate } ` } ;
16821694 }
16831695 }
16841696
@@ -1689,15 +1701,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16891701 cwd : string ,
16901702 refName : string ,
16911703 ) {
1692- const baseRef = yield * resolveBaseBranchForNoUpstream ( cwd , refName ) ;
1693- if ( ! baseRef ) {
1704+ const base = yield * resolveBaseBranchForNoUpstream ( cwd , refName ) ;
1705+ if ( ! base ) {
16941706 return 0 ;
16951707 }
16961708
16971709 const result = yield * executeGit (
16981710 "GitVcsDriver.computeAheadCountAgainstBase" ,
16991711 cwd ,
1700- [ "rev-list" , "--count" , `${ baseRef } ..HEAD` ] ,
1712+ [ "rev-list" , "--count" , `${ base . comparisonRef } ..HEAD` ] ,
17011713 { allowNonZeroExit : true } ,
17021714 ) ;
17031715 if ( result . exitCode !== 0 ) {
@@ -2677,15 +2689,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
26772689 branch : string | null ,
26782690 explicitBaseRef ?: string ,
26792691 ) {
2680- const baseRef =
2681- explicitBaseRef ??
2682- ( branch
2683- ? yield * resolveBaseBranchForNoUpstream ( cwd , branch , { allowRemoteOfCurrent : true } ) . pipe (
2684- Effect . orElseSucceed ( ( ) => null ) ,
2685- )
2686- : null ) ;
2687- if ( baseRef === null ) return { baseRef, mergeBase : "HEAD" } ;
2688- const args = [ "merge-base" , baseRef , "HEAD" ] ;
2692+ const base =
2693+ explicitBaseRef !== undefined
2694+ ? ( ( yield * resolveRemoteBaseRef ( cwd , explicitBaseRef ) ) ?? {
2695+ baseRef : explicitBaseRef ,
2696+ comparisonRef : explicitBaseRef ,
2697+ } )
2698+ : branch
2699+ ? yield * resolveBaseBranchForNoUpstream ( cwd , branch , { allowRemoteOfCurrent : true } ) . pipe (
2700+ Effect . orElseSucceed ( ( ) => null ) ,
2701+ )
2702+ : null ;
2703+ if ( base === null ) return { baseRef : null , mergeBase : "HEAD" } ;
2704+ const { baseRef, comparisonRef } = base ;
2705+ const args = [ "merge-base" , comparisonRef , "HEAD" ] ;
26892706 const result = yield * executeGit ( "GitVcsDriver.resolveReviewMergeBase" , cwd , args , {
26902707 allowNonZeroExit : true ,
26912708 } ) ;
0 commit comments