@@ -1003,37 +1003,9 @@ export const make = Effect.gen(function* () {
10031003 ...( remoteName . length > 0 ? { remoteName } : { } ) ,
10041004 } ;
10051005 return Effect . gen ( function * ( ) {
1006- const headContext = yield * resolveBranchHeadContext ( cwd , details ) ;
1007- const upstreamHeadIsDefault =
1008- headContext . headBranch === details . defaultBranch ||
1009- ( details . defaultBranch === null &&
1010- ( headContext . headBranch === "main" || headContext . headBranch === "master" ) ) ;
1011- // `git worktree add -b feature origin/main` makes the new local branch
1012- // track origin/main. That upstream is the branch's base, not its
1013- // published PR head. Looking up PRs for it can attach an old reverse
1014- // merge from main and auto-settle an unrelated feature thread.
1015- //
1016- // The branch may still have been pushed under its own name by a plain
1017- // `git push origin feature` that never moved the upstream. When a
1018- // remote-tracking ref for the local name exists, look the PR up by
1019- // that name instead of giving up. Without the ref there is nothing to
1020- // ask the host about, so no API call is spent.
1021- if (
1022- headContext . headBranch !== details . branch &&
1023- upstreamHeadIsDefault &&
1024- ! headContext . isCrossRepository
1025- ) {
1026- const publishedUnderOwnName = yield * hasRemoteTrackingRef ( cwd , details . branch ) ;
1027- if ( ! publishedUnderOwnName ) {
1028- return { latest : null , headContext } ;
1029- }
1030- const ownNameContext = yield * resolveBranchHeadContext ( cwd , {
1031- branch : details . branch ,
1032- upstreamRef : null ,
1033- remoteName : headContext . remoteName ?? "origin" ,
1034- } ) ;
1035- const latest = yield * findLatestPrForHeadContext ( cwd , ownNameContext ) ;
1036- return { latest, headContext : ownNameContext } ;
1006+ const { headContext, lookup } = yield * resolveLookupHeadContext ( cwd , details ) ;
1007+ if ( ! lookup ) {
1008+ return { latest : null , headContext } ;
10371009 }
10381010 // Only skip when the branch is untracked as well: anything carrying an
10391011 // upstream keeps the old behaviour.
@@ -1169,8 +1141,8 @@ export const make = Effect.gen(function* () {
11691141 }
11701142 : { } ) ,
11711143 } ) ,
1172- Effect . andThen ( resolveBranchHeadContext ( cwd , details ) ) ,
1173- Effect . map ( ( headContext ) =>
1144+ Effect . andThen ( resolveLookupHeadContext ( cwd , details ) ) ,
1145+ Effect . map ( ( { headContext } ) =>
11741146 resolveLastKnownPr ( branchKey , {
11751147 upstreamRef : details . upstreamRef ,
11761148 headBranch : headContext . headBranch ,
@@ -1356,6 +1328,96 @@ export const make = Effect.gen(function* () {
13561328 } satisfies BranchHeadContext ;
13571329 } ) ;
13581330
1331+ // The remote that holds a ref named after the local branch, or null when
1332+ // none does. Remote names may contain slashes, so refs are matched literally
1333+ // per remote instead of with a glob. When several remotes hold the name, the
1334+ // preferred remote wins, then origin, then the first configured remote.
1335+ const findRemoteTrackingRemote = Effect . fn ( "findRemoteTrackingRemote" ) ( function * (
1336+ cwd : string ,
1337+ branch : string ,
1338+ preferredRemoteName : string | null ,
1339+ ) {
1340+ if ( branch . length === 0 ) return null ;
1341+ return yield * Effect . gen ( function * ( ) {
1342+ const remoteNames = ( yield * gitCore . execute ( {
1343+ operation : "GitManager.findRemoteTrackingRemote.remotes" ,
1344+ cwd,
1345+ args : [ "remote" ] ,
1346+ timeoutMs : 5_000 ,
1347+ } ) ) . stdout
1348+ . split ( "\n" )
1349+ . map ( ( name ) => name . trim ( ) )
1350+ . filter ( ( name ) => name . length > 0 ) ;
1351+ if ( remoteNames . length === 0 ) return null ;
1352+ const refs = new Set (
1353+ ( yield * gitCore . execute ( {
1354+ operation : "GitManager.findRemoteTrackingRemote.refs" ,
1355+ cwd,
1356+ args : [
1357+ "for-each-ref" ,
1358+ "--format=%(refname)" ,
1359+ ...remoteNames . map ( ( name ) => `refs/remotes/${ name } /${ branch } ` ) ,
1360+ ] ,
1361+ timeoutMs : 5_000 ,
1362+ } ) ) . stdout
1363+ . split ( "\n" )
1364+ . map ( ( ref ) => ref . trim ( ) )
1365+ . filter ( ( ref ) => ref . length > 0 ) ,
1366+ ) ;
1367+ const matching = remoteNames . filter ( ( name ) => refs . has ( `refs/remotes/${ name } /${ branch } ` ) ) ;
1368+ if ( preferredRemoteName !== null && matching . includes ( preferredRemoteName ) ) {
1369+ return preferredRemoteName ;
1370+ }
1371+ if ( matching . includes ( "origin" ) ) return "origin" ;
1372+ return matching [ 0 ] ?? null ;
1373+ } ) . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ;
1374+ } ) ;
1375+
1376+ // `git worktree add -b feature origin/main` makes the new local branch track
1377+ // origin/main. That upstream is the branch's base, not its published PR
1378+ // head. Looking up PRs for it can attach an old reverse merge from main and
1379+ // auto-settle an unrelated feature thread.
1380+ //
1381+ // The branch may still have been pushed under its own name by a plain
1382+ // `git push <remote> feature` that never moved the upstream. When a remote
1383+ // holds a ref for the local name, look the PR up by that name on that
1384+ // remote. Without such a ref there is nothing to ask the host about, so
1385+ // `lookup` is false and no API call is spent. Both the cached lookup and the
1386+ // failure fallback resolve through here so the last-known PR compares
1387+ // against the same head branch.
1388+ const resolveLookupHeadContext = Effect . fn ( "resolveLookupHeadContext" ) ( function * (
1389+ cwd : string ,
1390+ details : {
1391+ branch : string ;
1392+ upstreamRef : string | null ;
1393+ defaultBranch : string | null ;
1394+ remoteName ?: string ;
1395+ } ,
1396+ ) {
1397+ const headContext = yield * resolveBranchHeadContext ( cwd , details ) ;
1398+ const upstreamHeadIsDefault =
1399+ headContext . headBranch === details . defaultBranch ||
1400+ ( details . defaultBranch === null &&
1401+ ( headContext . headBranch === "main" || headContext . headBranch === "master" ) ) ;
1402+ if (
1403+ headContext . headBranch === details . branch ||
1404+ ! upstreamHeadIsDefault ||
1405+ headContext . isCrossRepository
1406+ ) {
1407+ return { headContext, lookup : true } ;
1408+ }
1409+ const remoteName = yield * findRemoteTrackingRemote ( cwd , details . branch , headContext . remoteName ) ;
1410+ if ( remoteName === null ) {
1411+ return { headContext, lookup : false } ;
1412+ }
1413+ const ownNameContext = yield * resolveBranchHeadContext ( cwd , {
1414+ branch : details . branch ,
1415+ upstreamRef : null ,
1416+ remoteName,
1417+ } ) ;
1418+ return { headContext : ownNameContext , lookup : true } ;
1419+ } ) ;
1420+
13591421 /**
13601422 * Whether git has no record of this branch on any remote, so a change request
13611423 * cannot exist for it and asking the provider is a guaranteed-empty API call.
@@ -1369,24 +1431,6 @@ export const make = Effect.gen(function* () {
13691431 * because then every branch looks unpublished; it, and any failed probe,
13701432 * keeps the lookup.
13711433 */
1372- // True when any remote holds a ref named after the local branch.
1373- const hasRemoteTrackingRef = Effect . fn ( "hasRemoteTrackingRef" ) ( function * (
1374- cwd : string ,
1375- branch : string ,
1376- ) {
1377- if ( branch . length === 0 ) return false ;
1378- return yield * gitCore
1379- . execute ( {
1380- operation : "GitManager.hasRemoteTrackingRef" ,
1381- cwd,
1382- args : [ "for-each-ref" , "--count=1" , "--format=%(refname)" , `refs/remotes/*/${ branch } ` ] ,
1383- timeoutMs : 5_000 ,
1384- } )
1385- . pipe (
1386- Effect . map ( ( result ) => result . stdout . trim ( ) . length > 0 ) ,
1387- Effect . orElseSucceed ( ( ) => false ) ,
1388- ) ;
1389- } ) ;
13901434 const isUnpublishedBranch = Effect . fn ( "isUnpublishedBranch" ) ( function * (
13911435 cwd : string ,
13921436 headContext : Pick < BranchHeadContext , "headBranch" | "localBranch" > ,
0 commit comments