@@ -4,8 +4,6 @@ import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpat
44import { tmpdir } from 'node:os' ;
55import { join , resolve } from 'node:path' ;
66
7- const DEFAULT_BASE = 'origin/master' ;
8-
97const git = ( { root, args, input, env } ) =>
108 execFileSync ( 'git' , args , {
119 cwd : root ,
@@ -34,23 +32,33 @@ export const forkPointOf = ({ root, base, head }) => gitText({ root, args: ['mer
3432
3533export const headOf = ( { root } ) => gitText ( { root, args : [ 'rev-parse' , 'HEAD' ] } ) ;
3634
37- // The closest remote branch this one left. origin/master wins a tie, and a branch that holds this one plus more
35+ // origin/HEAD as the clone recorded it, else origin/main or origin/master, whichever exists.
36+ const defaultBranchOf = ( { root, rows } ) => {
37+ try {
38+ return gitText ( { root, args : [ 'symbolic-ref' , '--short' , 'refs/remotes/origin/HEAD' ] } ) ;
39+ } catch {
40+ return [ 'origin/main' , 'origin/master' ] . find ( ( ref ) => rows . some ( ( row ) => row . startsWith ( `${ ref } ` ) ) ) ;
41+ }
42+ } ;
43+
44+ // The closest remote branch this one left. The default branch wins a tie, and a branch that holds this one plus more
3845// is its child, not its base. A ref at this very commit is this work pushed under another name, unless the commit is
39- // on origin/master anyway. Costs about 250 ms across 2000 refs, so the answer is kept per commit.
46+ // on the default branch anyway. Costs about 250 ms across 2000 refs, so the answer is kept per commit.
4047const findBase = ( { root, head } ) => {
4148 const branch = gitText ( { root, args : [ 'branch' , '--show-current' ] } ) ;
4249 const skipped = new Set ( [ 'origin/HEAD' , 'origin' , `origin/${ branch } ` ] ) ;
4350 const refs = gitText ( { root, args : [ 'for-each-ref' , '--format=%(refname:short) %(ahead-behind:HEAD)' , 'refs/remotes/origin' ] } ) ;
4451 const rows = refs . split ( '\n' ) . filter ( Boolean ) ;
45- const defaultRow = rows . find ( ( row ) => row . startsWith ( `${ DEFAULT_BASE } ` ) ) ;
52+ const defaultBase = defaultBranchOf ( { root, rows } ) ;
53+ const defaultRow = rows . find ( ( row ) => row . startsWith ( `${ defaultBase } ` ) ) ;
4654 const isAheadOfDefault = ! defaultRow || Number ( defaultRow . split ( ' ' ) [ 2 ] ) > 0 ;
4755 let best = null ;
4856 for ( const row of rows ) {
4957 const [ ref , ahead , behind ] = row . split ( ' ' ) ;
50- const isChild = Number ( ahead ) > 0 && Number ( behind ) === 0 && ref !== DEFAULT_BASE ;
51- const isThisCommit = Number ( ahead ) === 0 && Number ( behind ) === 0 && ref !== DEFAULT_BASE ;
58+ const isChild = Number ( ahead ) > 0 && Number ( behind ) === 0 && ref !== defaultBase ;
59+ const isThisCommit = Number ( ahead ) === 0 && Number ( behind ) === 0 && ref !== defaultBase ;
5260 if ( skipped . has ( ref ) || isChild || ( isThisCommit && isAheadOfDefault ) ) continue ;
53- const isBetter = ! best || Number ( behind ) < best . behind || ( Number ( behind ) === best . behind && ref === DEFAULT_BASE ) ;
61+ const isBetter = ! best || Number ( behind ) < best . behind || ( Number ( behind ) === best . behind && ref === defaultBase ) ;
5462 if ( isBetter ) best = { ref, behind : Number ( behind ) } ;
5563 }
5664 if ( ! best ) throw new Error ( `no remote branch to compare ${ head . slice ( 0 , 10 ) } with` ) ;
0 commit comments