@@ -1041,6 +1041,7 @@ export class FolderRepositoryManager extends Disposable {
10411041 const localBranches = ( await this . repository . getRefs ( { pattern : 'refs/heads/' } ) )
10421042 . filter ( r => r . name !== undefined )
10431043 . map ( r => r . name ! ) ;
1044+ const matchingMetadata = await PullRequestGitHelper . getMatchingPullRequestMetadataForBranches ( this . repository , localBranches ) ;
10441045
10451046 // Chunk localBranches into chunks of 100 to avoid hitting the GitHub API rate limit
10461047 const chunkedLocalBranches : string [ ] [ ] = [ ] ;
@@ -1053,10 +1054,7 @@ export class FolderRepositoryManager extends Disposable {
10531054 const models : ( PullRequestModel | undefined ) [ ] = [ ] ;
10541055 for ( const chunk of chunkedLocalBranches ) {
10551056 models . push ( ...await Promise . all ( chunk . map ( async localBranchName => {
1056- const matchingPRMetadata = await PullRequestGitHelper . getMatchingPullRequestMetadataForBranch (
1057- this . repository ,
1058- localBranchName ,
1059- ) ;
1057+ const matchingPRMetadata = matchingMetadata . get ( localBranchName ) ;
10601058
10611059 if ( matchingPRMetadata ) {
10621060 const { owner, prNumber } = matchingPRMetadata ;
@@ -1102,7 +1100,7 @@ export class FolderRepositoryManager extends Disposable {
11021100 }
11031101
11041102 try {
1105- // Only check the 3 most recently used branches to minimize API calls
1103+ // Only check the 10 most recently used branches to minimize API calls
11061104 const localBranches = ( await this . repository . getRefs ( {
11071105 pattern : 'refs/heads/' ,
11081106 sort : 'committerdate' ,
@@ -1113,18 +1111,14 @@ export class FolderRepositoryManager extends Disposable {
11131111
11141112 Logger . debug ( `Found ${ localBranches . length } local branches to check` , this . id ) ;
11151113
1114+ const matchingMetadata = await PullRequestGitHelper . getMatchingPullRequestMetadataForBranches ( this . repository , localBranches ) ;
11161115 const associationResults : boolean [ ] = [ ] ;
11171116
1118- // Process all branches (max 3) in parallel
1117+ // Process the selected branches in parallel
11191118 const chunkResults = await Promise . all ( localBranches . map ( async branchName => {
11201119 try {
11211120 // Check if this branch already has PR metadata
1122- const existingMetadata = await PullRequestGitHelper . getMatchingPullRequestMetadataForBranch (
1123- this . repository ,
1124- branchName ,
1125- ) ;
1126-
1127- if ( existingMetadata ) {
1121+ if ( matchingMetadata . has ( branchName ) ) {
11281122 // Branch already has PR metadata, skip
11291123 return false ;
11301124 }
@@ -2215,29 +2209,10 @@ export class FolderRepositoryManager extends Disposable {
22152209 progress . report ( { message : vscode . l10n . t ( 'Deleted {0} of {1} branches' , deletedBranches , totalBranches ) } ) ;
22162210 } ;
22172211
2218- const deleteConfig = async ( branch : string ) => {
2219- await PullRequestGitHelper . associateBaseBranchWithBranch ( this . repository , branch , undefined ) ;
2220- await PullRequestGitHelper . associateBranchWithPullRequest ( this . repository , undefined , branch ) ;
2221- if ( this . repository . unsetConfig ) {
2222- const prefix = `branch.${ branch } .` ;
2223- const remaining = ( await this . repository . getConfigs ( ) ) . filter ( config => config . key . startsWith ( prefix ) ) ;
2224- for ( const config of remaining ) {
2225- try {
2226- await this . repository . unsetConfig ( config . key ) ;
2227- } catch ( e ) {
2228- Logger . error ( `Failed to remove leftover git config ${ config . key } : ${ e } ` , this . id ) ;
2229- }
2230- }
2231- }
2232- } ;
2233-
22342212 // batch deleting the branches to avoid consuming all available resources
22352213 await batchPromiseAll ( picks , 5 , async ( pick ) => {
22362214 try {
22372215 await this . repository . deleteBranch ( pick . label , true ) ;
2238- if ( ( await PullRequestGitHelper . getMatchingPullRequestMetadataForBranch ( this . repository , pick . label ) ) ) {
2239- console . log ( `Branch ${ pick . label } was not deleted` ) ;
2240- }
22412216 reportProgress ( ) ;
22422217 } catch ( e ) {
22432218 if ( typeof e . stderr === 'string' && e . stderr . includes ( 'not found' ) ) {
@@ -2254,27 +2229,43 @@ export class FolderRepositoryManager extends Disposable {
22542229
22552230 // Git removes the whole config section when deleting a branch, including keys with multiple values.
22562231 // Recreate deleted branches when a provider leaves config behind so Git can perform that cleanup again.
2257- const configsAfterDeletion = await this . repository . getConfigs ( ) ;
2258- const branchesNeedingConfigCleanup = picks . filter ( pick => {
2259- const prefix = `branch.${ pick . label } .` ;
2260- return configsAfterDeletion . some ( config => config . key . startsWith ( prefix ) ) ;
2261- } ) ;
2232+ // The last dot separates the variable name from the section; a branch name can contain dots.
2233+ const configSection = ( key : string ) => key . slice ( 0 , key . lastIndexOf ( '.' ) ) ;
2234+ const sectionsAfterDeletion = new Set ( ( await this . repository . getConfigs ( ) ) . map ( ( { key } ) => configSection ( key ) ) ) ;
22622235 const branchesNeedingRetry = new Set ( needsRetry ?. map ( pick => pick . label ) ) ;
2263- for ( const pick of branchesNeedingConfigCleanup ) {
2264- if ( ! branchesNeedingRetry . has ( pick . label ) ) {
2265- try {
2266- await this . repository . createBranch ( pick . label , false , 'HEAD' ) ;
2267- await this . repository . deleteBranch ( pick . label , true ) ;
2268- } catch ( e ) {
2269- Logger . error ( `Failed to remove leftover git config section for ${ pick . label } : ${ e } ` , this . id ) ;
2270- }
2236+ const branchesToClean = picks . filter ( pick => ! branchesNeedingRetry . has ( pick . label ) ) ;
2237+ const sectionsNeedingCleanup = new Set < string > ( ) ;
2238+ for ( const { label } of branchesToClean ) {
2239+ const section = `branch.${ label } ` ;
2240+ if ( ! sectionsAfterDeletion . has ( section ) ) {
2241+ continue ;
2242+ }
2243+ sectionsNeedingCleanup . add ( section ) ;
2244+ try {
2245+ await this . repository . createBranch ( label , false , 'HEAD' ) ;
2246+ await this . repository . deleteBranch ( label , true ) ;
2247+ } catch ( e ) {
2248+ Logger . error ( `Failed to remove leftover git config section for ${ label } : ${ e } ` , this . id ) ;
22712249 }
22722250 }
22732251
2274- // Config operations can't be parallelized. Clean up any keys left by providers that don't remove sections.
2275- for ( const pick of picks ) {
2276- if ( ! branchesNeedingRetry . has ( pick . label ) ) {
2277- await deleteConfig ( pick . label ) ;
2252+ // Clear associations serially before refreshing the remaining config.
2253+ for ( const { label } of branchesToClean ) {
2254+ await PullRequestGitHelper . associateBaseBranchWithBranch ( this . repository , label , undefined ) ;
2255+ await PullRequestGitHelper . associateBranchWithPullRequest ( this . repository , undefined , label ) ;
2256+ }
2257+
2258+ // Refresh after the fallback deletions, then remove remaining keys serially.
2259+ if ( this . repository . unsetConfig && sectionsNeedingCleanup . size ) {
2260+ for ( const { key } of await this . repository . getConfigs ( ) ) {
2261+ if ( ! sectionsNeedingCleanup . has ( configSection ( key ) ) ) {
2262+ continue ;
2263+ }
2264+ try {
2265+ await this . repository . unsetConfig ( key ) ;
2266+ } catch ( e ) {
2267+ Logger . error ( `Failed to remove leftover git config ${ key } : ${ e } ` , this . id ) ;
2268+ }
22782269 }
22792270 }
22802271 if ( needsRetry && needsRetry . length ) {
@@ -2572,18 +2563,6 @@ export class FolderRepositoryManager extends Disposable {
25722563 return githubRepository ?. resolveUser ( login ) ;
25732564 }
25742565
2575- async getMatchingPullRequestMetadataForBranch ( ) {
2576- if ( ! this . repository || ! this . repository . state . HEAD || ! this . repository . state . HEAD . name ) {
2577- return null ;
2578- }
2579-
2580- const matchingPullRequestMetadata = await PullRequestGitHelper . getMatchingPullRequestMetadataForBranch (
2581- this . repository ,
2582- this . repository . state . HEAD . name ,
2583- ) ;
2584- return matchingPullRequestMetadata ;
2585- }
2586-
25872566 async getMatchingPullRequestMetadataFromGitHub ( branch : Branch , remoteName ?: string , remoteUrl ?: string , upstreamBranchName ?: string ) : Promise <
25882567 ( PullRequestMetadata & { model : PullRequestModel } ) | null
25892568 > {
0 commit comments