@@ -17,11 +17,7 @@ export async function getBranchMergeTargetInfo(
1717 } ,
1818) : Promise < BranchTargetInfo > {
1919 const [ targetResult , baseResult , defaultResult ] = await Promise . allSettled ( [
20- getMergeTargetBranchName ( container , branch , {
21- cancellation : options ?. cancellation ,
22- detectedOnly : options ?. detectedOnly ,
23- timeout : options ?. timeout ,
24- } ) ,
20+ getBranchMergeTargetNameWithoutFallback ( container , branch , options ) ,
2521 container . git . branches ( branch . repoPath ) . getBaseBranchName ?.( branch . name , options ?. cancellation ) ,
2622 getDefaultBranchName ( container , branch . repoPath , branch . getRemoteName ( ) , {
2723 cancellation : options ?. cancellation ,
@@ -37,30 +33,55 @@ export async function getBranchMergeTargetInfo(
3733 } ;
3834}
3935
40- export async function getDefaultBranchName (
36+ export async function getBranchMergeTargetName (
4137 container : Container ,
42- repoPath : string ,
43- remoteName ?: string ,
44- options ?: { cancellation ?: CancellationToken } ,
45- ) : Promise < string | undefined > {
46- const name = await container . git . branches ( repoPath ) . getDefaultBranchName ( remoteName , options ?. cancellation ) ;
47- return name ?? getDefaultBranchNameFromIntegration ( container , repoPath , options ) ;
48- }
38+ branch : GitBranch ,
39+ options ?: {
40+ associatedPullRequest ?: Promise < PullRequest | undefined > ;
41+ cancellation ?: CancellationToken ;
42+ detectedOnly ?: boolean ;
43+ timeout ?: number ;
44+ } ,
45+ ) : Promise < MaybePausedResult < string | undefined > > {
46+ async function getMergeTargetFallback ( ) {
47+ const [ baseResult , defaultResult ] = await Promise . allSettled ( [
48+ container . git . branches ( branch . repoPath ) . getBaseBranchName ?.( branch . name , options ?. cancellation ) ,
49+ getDefaultBranchName ( container , branch . repoPath , branch . getRemoteName ( ) , {
50+ cancellation : options ?. cancellation ,
51+ } ) ,
52+ ] ) ;
53+ return getSettledValue ( baseResult ) ?? getSettledValue ( defaultResult ) ;
54+ }
4955
50- export async function getDefaultBranchNameFromIntegration (
51- container : Container ,
52- repoPath : string ,
53- options ?: { cancellation ?: CancellationToken } ,
54- ) : Promise < string | undefined > {
55- const remote = await container . git . remotes ( repoPath ) . getBestRemoteWithIntegration ( undefined , options ?. cancellation ) ;
56- if ( remote == null ) return undefined ;
56+ const result = await getBranchMergeTargetNameWithoutFallback ( container , branch , options ) ;
57+ if ( ! result . paused ) {
58+ if ( result . value ) return { value : result . value , paused : false } ;
5759
58- const integration = await remote . getIntegration ( ) ;
59- const defaultBranch = await integration ?. getDefaultBranch ?.( remote . provider . repoDesc , options ) ;
60- return defaultBranch && `${ remote . name } /${ defaultBranch ?. name } ` ;
60+ if ( options ?. cancellation ?. isCancellationRequested ) {
61+ return { value : Promise . resolve ( undefined ) , paused : true , reason : 'cancelled' } ;
62+ }
63+
64+ const fallback = await getMergeTargetFallback ( ) ;
65+ if ( options ?. cancellation ?. isCancellationRequested ) {
66+ return { value : Promise . resolve ( undefined ) , paused : true , reason : 'cancelled' } ;
67+ }
68+
69+ return { value : fallback , paused : false } ;
70+ }
71+
72+ if ( options ?. cancellation ?. isCancellationRequested || result . reason === 'cancelled' ) {
73+ return { value : Promise . resolve ( undefined ) , paused : true , reason : 'cancelled' } ;
74+ }
75+
76+ return {
77+ value : result . value . then ( r => r ?? getMergeTargetFallback ( ) ) ,
78+ paused : true ,
79+ reason : 'timedout' ,
80+ } ;
6181}
6282
63- export async function getMergeTargetBranchName (
83+ /** This is an internal helper function for getting only the merge target from stored data or a PR, not falling back to base/default */
84+ async function getBranchMergeTargetNameWithoutFallback (
6485 container : Container ,
6586 branch : GitBranch ,
6687 options ?: {
@@ -95,3 +116,26 @@ export async function getMergeTargetBranchName(
95116 options ?. timeout ,
96117 ) ;
97118}
119+
120+ export async function getDefaultBranchName (
121+ container : Container ,
122+ repoPath : string ,
123+ remoteName ?: string ,
124+ options ?: { cancellation ?: CancellationToken } ,
125+ ) : Promise < string | undefined > {
126+ const name = await container . git . branches ( repoPath ) . getDefaultBranchName ( remoteName , options ?. cancellation ) ;
127+ return name ?? getDefaultBranchNameFromIntegration ( container , repoPath , options ) ;
128+ }
129+
130+ export async function getDefaultBranchNameFromIntegration (
131+ container : Container ,
132+ repoPath : string ,
133+ options ?: { cancellation ?: CancellationToken } ,
134+ ) : Promise < string | undefined > {
135+ const remote = await container . git . remotes ( repoPath ) . getBestRemoteWithIntegration ( undefined , options ?. cancellation ) ;
136+ if ( remote == null ) return undefined ;
137+
138+ const integration = await remote . getIntegration ( ) ;
139+ const defaultBranch = await integration ?. getDefaultBranch ?.( remote . provider . repoDesc , options ) ;
140+ return defaultBranch && `${ remote . name } /${ defaultBranch ?. name } ` ;
141+ }
0 commit comments