@@ -2513,16 +2513,33 @@ export class FolderRepositoryManager extends Disposable {
25132513 let tempBranchName : string | undefined ;
25142514 let originalBranchDeleted = false ;
25152515 try {
2516- // Fetch to ensure we have the latest remote state
2517- await this . _repository . fetch ( branch . upstream . remote , branch . name ) ;
2516+ // Fetch to ensure we have the latest remote state (using upstream name for correct refspec)
2517+ await this . _repository . fetch ( branch . upstream . remote , branch . upstream . name ) ;
25182518
25192519 // Get the remote branch reference
25202520 const remoteBranchRef = `refs/remotes/${ branch . upstream . remote } /${ branch . upstream . name } ` ;
25212521 const remoteBranch = await this . _repository . getBranch ( remoteBranchRef ) ;
25222522 const currentBranchName = branch . name ;
25232523
2524- // Create a temp branch at the remote commit with better uniqueness
2525- tempBranchName = `temp-pr-update-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 7 ) } ` ;
2524+ // Create a temp branch at the remote commit with uniqueness guarantee
2525+ let tempCounter = 0 ;
2526+ do {
2527+ tempBranchName = `temp-pr-update-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . substring ( 7 ) } ${ tempCounter > 0 ? `-${ tempCounter } ` : '' } ` ;
2528+ tempCounter ++ ;
2529+ try {
2530+ await this . _repository . getBranch ( tempBranchName ) ;
2531+ // Branch exists, try again with different name
2532+ tempBranchName = undefined ;
2533+ } catch {
2534+ // Branch doesn't exist, we can use this name
2535+ break ;
2536+ }
2537+ } while ( tempCounter < 10 ) ; // Safety limit
2538+
2539+ if ( ! tempBranchName ) {
2540+ throw new Error ( 'Could not generate unique temporary branch name' ) ;
2541+ }
2542+
25262543 await this . _repository . createBranch ( tempBranchName , false , remoteBranch . commit ) ;
25272544 await this . _repository . setBranchUpstream ( tempBranchName , remoteBranchRef ) ;
25282545
@@ -2551,11 +2568,23 @@ export class FolderRepositoryManager extends Disposable {
25512568 // Attempt cleanup of any created resources
25522569 if ( tempBranchName ) {
25532570 try {
2554- // If we're still on the temp branch, try to get back to a safe state
2555- if ( ! originalBranchDeleted ) {
2571+ // Check current HEAD to see where we are
2572+ const currentHead = this . _repository . state . HEAD ;
2573+
2574+ // If original branch wasn't deleted yet, we can safely checkout and cleanup
2575+ if ( ! originalBranchDeleted && currentHead ?. name !== branch . name ) {
25562576 await this . _repository . checkout ( branch . name ) ;
25572577 }
2558- await this . _repository . deleteBranch ( tempBranchName , true ) ;
2578+ // If original was deleted and we're on temp branch, we need to notify user
2579+ // The temp branch is now the only reference to their work
2580+ if ( originalBranchDeleted ) {
2581+ vscode . window . showWarningMessage (
2582+ vscode . l10n . t ( 'Branch reset partially completed. You are on temporary branch "{0}". Your original branch has been deleted but not recreated. Please manually resolve this state.' , tempBranchName )
2583+ ) ;
2584+ } else {
2585+ // Clean up temp branch if we successfully returned to original
2586+ await this . _repository . deleteBranch ( tempBranchName , true ) ;
2587+ }
25592588 } catch ( cleanupError ) {
25602589 Logger . error ( `Error during cleanup: ${ cleanupError } ` , this . id ) ;
25612590 }
0 commit comments