11import type { CancellationToken } from 'vscode' ;
2- import type { GitConfigKeys } from '../../../../constants' ;
32import type { Container } from '../../../../container' ;
43import { CancellationError , isCancellationError } from '../../../../errors' ;
54import type { GitCache } from '../../../../git/cache' ;
@@ -256,18 +255,13 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
256255 const scope = getLogScope ( ) ;
257256
258257 try {
259- let baseOrTargetBranch = await this . getBaseBranchName ( repoPath , ref , cancellation ) ;
260- // If the base looks like its remote branch, look for the target or default
261- if ( baseOrTargetBranch == null || baseOrTargetBranch . endsWith ( `/${ ref } ` ) ) {
262- baseOrTargetBranch = await this . getTargetBranchName ( repoPath , ref ) ;
263- baseOrTargetBranch ??= await this . getDefaultBranchName ( repoPath , undefined , cancellation ) ;
264- if ( baseOrTargetBranch == null ) return undefined ;
265- }
258+ const mergeTarget = await this . getBestMergeTargetBranchName ( repoPath , ref ) ;
259+ if ( mergeTarget == null ) return undefined ;
266260
267261 const mergeBase = await this . provider . refs . getMergeBase (
268262 repoPath ,
269263 ref ,
270- baseOrTargetBranch ,
264+ mergeTarget ,
271265 undefined ,
272266 cancellation ,
273267 ) ;
@@ -314,7 +308,7 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
314308 return {
315309 repoPath : repoPath ,
316310 branch : ref ,
317- baseOrTargetBranch : baseOrTargetBranch ,
311+ mergeTarget : mergeTarget ,
318312 mergeBase : mergeBase ,
319313
320314 commits : totalCommits ,
@@ -669,7 +663,7 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
669663 const branch = await this . provider . refs . getSymbolicReferenceName ( repoPath , mergeBase ) ;
670664 if ( branch != null ) {
671665 if ( update ) {
672- void this . setBaseBranchName ( repoPath , ref , branch ) ;
666+ void this . storeBaseBranchName ( repoPath , ref , branch ) ;
673667 }
674668 return branch ;
675669 }
@@ -679,48 +673,13 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
679673
680674 const branch = await this . getBaseBranchFromReflog ( repoPath , ref , { upstream : true } , cancellation ) ;
681675 if ( branch != null ) {
682- void this . setBaseBranchName ( repoPath , ref , branch ) ;
676+ void this . storeBaseBranchName ( repoPath , ref , branch ) ;
683677 return branch ;
684678 }
685679
686680 return undefined ;
687681 }
688682
689- @log ( )
690- async setBaseBranchName ( repoPath : string , ref : string , base : string ) : Promise < void > {
691- const mergeBaseConfigKey : GitConfigKeys = `branch.${ ref } .gk-merge-base` ;
692-
693- await this . provider . config . setConfig ( repoPath , mergeBaseConfigKey , base ) ;
694- }
695-
696- @log ( )
697- async getUserMergeTargetBranchName ( repoPath : string , ref : string ) : Promise < string | undefined > {
698- const mergeTargetConfigKey : GitConfigKeys = `branch.${ ref } .gk-user-merge-target` ;
699- const target = await this . provider . config . getConfig ( repoPath , mergeTargetConfigKey ) ;
700- return target ?. trim ( ) || undefined ;
701- }
702-
703- @log ( )
704- async setUserMergeTargetBranchName ( repoPath : string , ref : string , target : string | undefined ) : Promise < void > {
705- const mergeTargetConfigKey : GitConfigKeys = `branch.${ ref } .gk-user-merge-target` ;
706- await this . provider . config . setConfig ( repoPath , mergeTargetConfigKey , target ) ;
707- }
708-
709- async getMergeTargetBranchName ( repoPath : string , branch : GitBranch ) : Promise < string | undefined > {
710- const [ baseResult , defaultResult , targetResult , userTargetResult ] = await Promise . allSettled ( [
711- this . getBaseBranchName ?.( repoPath , branch . name ) ,
712- this . getDefaultBranchName ( repoPath , branch . getRemoteName ( ) ) ,
713- this . getTargetBranchName ?.( repoPath , branch . name ) ,
714- this . getUserMergeTargetBranchName ?.( repoPath , branch . name ) ,
715- ] ) ;
716-
717- const baseBranchName = getSettledValue ( baseResult ) ;
718- const defaultBranchName = getSettledValue ( defaultResult ) ;
719- const targetMaybeResult = getSettledValue ( targetResult ) ;
720- const userTargetBranchName = getSettledValue ( userTargetResult ) ;
721- return userTargetBranchName || targetMaybeResult || baseBranchName || defaultBranchName ;
722- }
723-
724683 private async getBaseBranchFromReflog (
725684 repoPath : string ,
726685 ref : string ,
@@ -782,26 +741,75 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
782741 return undefined ;
783742 }
784743
785- @log ( { exit : true } )
786- async getTargetBranchName ( repoPath : string , ref : string ) : Promise < string | undefined > {
787- const targetBaseConfigKey : GitConfigKeys = `branch.${ ref } .gk-target-base` ;
744+ @log ( )
745+ async getBestMergeTargetBranchName (
746+ repoPath : string ,
747+ ref : string ,
748+ remote ?: string ,
749+ options ?: { detectedOnly ?: boolean } ,
750+ cancellation ?: CancellationToken ,
751+ ) : Promise < string | undefined > {
752+ const [ userTargetResult , targetResult ] = await Promise . allSettled ( [
753+ options ?. detectedOnly ? undefined : this . getStoredUserMergeTargetBranchName ?.( repoPath , ref ) ,
754+ this . getStoredDetectedMergeTargetBranchName ?.( repoPath , ref ) ,
755+ ] ) ;
788756
789- let target = await this . provider . config . getConfig ( repoPath , targetBaseConfigKey ) ;
790- if ( target ) {
791- target = await this . provider . refs . getSymbolicReferenceName ( repoPath , target ) ;
757+ const targetBranchName = getSettledValue ( userTargetResult ) || getSettledValue ( targetResult ) ;
758+ if ( targetBranchName ) {
759+ const validated = await this . provider . refs . getSymbolicReferenceName (
760+ repoPath ,
761+ targetBranchName ,
762+ cancellation ,
763+ ) ;
764+ return validated || targetBranchName ;
792765 }
766+
767+ const [ baseResult , defaultResult ] = await Promise . allSettled ( [
768+ this . getBaseBranchName ?.( repoPath , ref , cancellation ) ,
769+ this . getDefaultBranchName ( repoPath , remote , cancellation ) ,
770+ ] ) ;
771+ return getSettledValue ( baseResult ) || getSettledValue ( defaultResult ) ;
772+ }
773+
774+ @log ( { exit : true } )
775+ async getStoredMergeTargetBranchName ( repoPath : string , ref : string ) : Promise < string | undefined > {
776+ const target =
777+ ( await this . getStoredUserMergeTargetBranchName ?.( repoPath , ref ) ) ??
778+ ( await this . getStoredDetectedMergeTargetBranchName ?.( repoPath , ref ) ) ;
793779 return target ?. trim ( ) || undefined ;
794780 }
795781
796- @log ( )
797- async setTargetBranchName ( repoPath : string , ref : string , target : string ) : Promise < void > {
798- const targetBaseConfigKey : GitConfigKeys = `branch.${ ref } .gk-target-base` ;
782+ @log ( { exit : true } )
783+ async getStoredDetectedMergeTargetBranchName ( repoPath : string , ref : string ) : Promise < string | undefined > {
784+ const target =
785+ ( await this . provider . config . getConfig ( repoPath , `branch.${ ref } .gk-merge-target` ) ) ??
786+ ( await this . provider . config . getConfig ( repoPath , `branch.${ ref } .gk-target-base` ) ) ;
787+ return target ?. trim ( ) || undefined ;
788+ }
799789
800- await this . provider . config . setConfig ( repoPath , targetBaseConfigKey , target ) ;
790+ @log ( )
791+ async getStoredUserMergeTargetBranchName ( repoPath : string , ref : string ) : Promise < string | undefined > {
792+ const target = await this . provider . config . getConfig ( repoPath , `branch.${ ref } .gk-merge-target-user` ) ;
793+ return target ?. trim ( ) || undefined ;
801794 }
802795
803796 @log ( )
804797 async renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
805798 await this . git . exec ( { cwd : repoPath } , 'branch' , '-m' , oldName , newName ) ;
806799 }
800+
801+ @log ( )
802+ async storeBaseBranchName ( repoPath : string , ref : string , base : string ) : Promise < void > {
803+ await this . provider . config . setConfig ( repoPath , `branch.${ ref } .gk-merge-base` , base ) ;
804+ }
805+
806+ @log ( )
807+ async storeMergeTargetBranchName ( repoPath : string , ref : string , target : string ) : Promise < void > {
808+ await this . provider . config . setConfig ( repoPath , `branch.${ ref } .gk-merge-target` , target ) ;
809+ }
810+
811+ @log ( )
812+ async storeUserMergeTargetBranchName ( repoPath : string , ref : string , target : string | undefined ) : Promise < void > {
813+ await this . provider . config . setConfig ( repoPath , `branch.${ ref } .gk-merge-target-user` , target ) ;
814+ }
807815}
0 commit comments