77import * as vscode from 'vscode' ;
88import { FolderRepositoryManager } from './folderRepositoryManager' ;
99import { IAccount , isITeam , ITeam , MergeMethod , PullRequestMergeability , reviewerId , ReviewState } from './interface' ;
10+ import { BranchInfo } from './pullRequestGitHelper' ;
1011import { PullRequestModel } from './pullRequestModel' ;
1112import { PullRequest , ReadyForReviewReply , ReviewType , SubmitReviewReply } from './views' ;
1213import { DEFAULT_DELETION_METHOD , PR_SETTINGS_NAMESPACE , SELECT_LOCAL_BRANCH , SELECT_REMOTE } from '../common/settingKeys' ;
@@ -274,9 +275,13 @@ export namespace PullRequestReviewCommon {
274275 }
275276 }
276277
278+ interface SelectedAction {
279+ type : 'remoteHead' | 'local' | 'remote' | 'suspend'
280+ } ;
281+
277282 export async function deleteBranch ( folderRepositoryManager : FolderRepositoryManager , item : PullRequestModel ) : Promise < { isReply : boolean , message : any } > {
278283 const branchInfo = await folderRepositoryManager . getBranchNameForPullRequest ( item ) ;
279- const actions : ( vscode . QuickPickItem & { type : 'remoteHead' | 'local' | 'remote' | 'suspend' } ) [ ] = [ ] ;
284+ const actions : ( vscode . QuickPickItem & SelectedAction ) [ ] = [ ] ;
280285 const defaultBranch = await folderRepositoryManager . getPullRequestRepositoryDefaultBranch ( item ) ;
281286
282287 if ( item . isResolved ( ) ) {
@@ -341,51 +346,9 @@ export namespace PullRequestReviewCommon {
341346 ignoreFocusOut : true ,
342347 } ) ;
343348
344- const deletedBranchTypes : string [ ] = [ ] ;
345349
346350 if ( selectedActions ) {
347- const isBranchActive = item . equals ( folderRepositoryManager . activePullRequest ) || ( folderRepositoryManager . repository . state . HEAD ?. name && folderRepositoryManager . repository . state . HEAD . name === branchInfo ?. branch ) ;
348-
349- const promises = selectedActions . map ( async action => {
350- switch ( action . type ) {
351- case 'remoteHead' :
352- await folderRepositoryManager . deleteBranch ( item ) ;
353- deletedBranchTypes . push ( action . type ) ;
354- await folderRepositoryManager . repository . fetch ( { prune : true } ) ;
355- // If we're in a remote repository, then we should checkout the default branch.
356- if ( folderRepositoryManager . repository . rootUri . scheme === Schemes . VscodeVfs ) {
357- await folderRepositoryManager . repository . checkout ( defaultBranch ) ;
358- }
359- return ;
360- case 'local' :
361- if ( isBranchActive ) {
362- if ( folderRepositoryManager . repository . state . workingTreeChanges . length ) {
363- const yes = vscode . l10n . t ( 'Yes' ) ;
364- const response = await vscode . window . showWarningMessage (
365- vscode . l10n . t ( 'Your local changes will be lost, do you want to continue?' ) ,
366- { modal : true } ,
367- yes ,
368- ) ;
369- if ( response === yes ) {
370- await vscode . commands . executeCommand ( 'git.cleanAll' ) ;
371- } else {
372- return ;
373- }
374- }
375- await folderRepositoryManager . checkoutDefaultBranch ( defaultBranch ) ;
376- }
377- await folderRepositoryManager . repository . deleteBranch ( branchInfo ! . branch , true ) ;
378- return deletedBranchTypes . push ( action . type ) ;
379- case 'remote' :
380- deletedBranchTypes . push ( action . type ) ;
381- return folderRepositoryManager . repository . removeRemote ( branchInfo ! . remote ! ) ;
382- case 'suspend' :
383- deletedBranchTypes . push ( action . type ) ;
384- return vscode . commands . executeCommand ( 'github.codespaces.disconnectSuspend' ) ;
385- }
386- } ) ;
387-
388- await Promise . all ( promises ) ;
351+ const deletedBranchTypes : string [ ] = await performBranchDeletion ( folderRepositoryManager , item , defaultBranch , branchInfo ! , selectedActions ) ;
389352
390353 return {
391354 isReply : false ,
@@ -403,4 +366,92 @@ export namespace PullRequestReviewCommon {
403366 } ;
404367 }
405368 }
369+
370+ async function performBranchDeletion ( folderRepositoryManager : FolderRepositoryManager , item : PullRequestModel , defaultBranch : string , branchInfo : BranchInfo , selectedActions : SelectedAction [ ] ) : Promise < string [ ] > {
371+ const isBranchActive = item . equals ( folderRepositoryManager . activePullRequest ) || ( folderRepositoryManager . repository . state . HEAD ?. name && folderRepositoryManager . repository . state . HEAD . name === branchInfo ?. branch ) ;
372+ const deletedBranchTypes : string [ ] = [ ] ;
373+
374+ const promises = selectedActions . map ( async action => {
375+ switch ( action . type ) {
376+ case 'remoteHead' :
377+ await folderRepositoryManager . deleteBranch ( item ) ;
378+ deletedBranchTypes . push ( action . type ) ;
379+ await folderRepositoryManager . repository . fetch ( { prune : true } ) ;
380+ // If we're in a remote repository, then we should checkout the default branch.
381+ if ( folderRepositoryManager . repository . rootUri . scheme === Schemes . VscodeVfs ) {
382+ await folderRepositoryManager . repository . checkout ( defaultBranch ) ;
383+ }
384+ return ;
385+ case 'local' :
386+ if ( isBranchActive ) {
387+ if ( folderRepositoryManager . repository . state . workingTreeChanges . length ) {
388+ const yes = vscode . l10n . t ( 'Yes' ) ;
389+ const response = await vscode . window . showWarningMessage (
390+ vscode . l10n . t ( 'Your local changes will be lost, do you want to continue?' ) ,
391+ { modal : true } ,
392+ yes ,
393+ ) ;
394+ if ( response === yes ) {
395+ await vscode . commands . executeCommand ( 'git.cleanAll' ) ;
396+ } else {
397+ return ;
398+ }
399+ }
400+ await folderRepositoryManager . checkoutDefaultBranch ( defaultBranch ) ;
401+ }
402+ await folderRepositoryManager . repository . deleteBranch ( branchInfo ! . branch , true ) ;
403+ return deletedBranchTypes . push ( action . type ) ;
404+ case 'remote' :
405+ deletedBranchTypes . push ( action . type ) ;
406+ return folderRepositoryManager . repository . removeRemote ( branchInfo ! . remote ! ) ;
407+ case 'suspend' :
408+ deletedBranchTypes . push ( action . type ) ;
409+ return vscode . commands . executeCommand ( 'github.codespaces.disconnectSuspend' ) ;
410+ }
411+ } ) ;
412+
413+ await Promise . all ( promises ) ;
414+ return deletedBranchTypes ;
415+ }
416+
417+ /**
418+ * Automatically delete branches after merge based on user preferences.
419+ * This function does not show any prompts - it uses the default deletion method preferences.
420+ */
421+ export async function autoDeleteBranchesAfterMerge ( folderRepositoryManager : FolderRepositoryManager , item : PullRequestModel ) : Promise < void > {
422+ const branchInfo = await folderRepositoryManager . getBranchNameForPullRequest ( item ) ;
423+ const defaultBranch = await folderRepositoryManager . getPullRequestRepositoryDefaultBranch ( item ) ;
424+
425+ // Get user preferences for automatic deletion
426+ const deleteLocalBranch = vscode . workspace
427+ . getConfiguration ( PR_SETTINGS_NAMESPACE )
428+ . get < boolean > ( `${ DEFAULT_DELETION_METHOD } .${ SELECT_LOCAL_BRANCH } ` , true ) ;
429+
430+ const deleteRemote = vscode . workspace
431+ . getConfiguration ( PR_SETTINGS_NAMESPACE )
432+ . get < boolean > ( `${ DEFAULT_DELETION_METHOD } .${ SELECT_REMOTE } ` , true ) ;
433+
434+ const selectedActions : SelectedAction [ ] = [ ] ;
435+
436+ // Delete remote head branch if it's not the default branch
437+ if ( item . isResolved ( ) ) {
438+ const isDefaultBranch = defaultBranch === item . head . ref ;
439+ if ( ! isDefaultBranch && ! item . isRemoteHeadDeleted ) {
440+ selectedActions . push ( { type : 'remoteHead' } ) ;
441+ }
442+ }
443+
444+ // Delete local branch if preference is set
445+ if ( branchInfo && deleteLocalBranch ) {
446+ selectedActions . push ( { type : 'local' } ) ;
447+ }
448+
449+ // Delete remote if it's no longer used and preference is set
450+ if ( branchInfo && branchInfo . remote && branchInfo . createdForPullRequest && ! branchInfo . remoteInUse && deleteRemote ) {
451+ selectedActions . push ( { type : 'remote' } ) ;
452+ }
453+
454+ // Execute all deletions in parallel
455+ await performBranchDeletion ( folderRepositoryManager , item , defaultBranch , branchInfo ! , selectedActions ) ;
456+ }
406457}
0 commit comments