@@ -24,6 +24,7 @@ import {
2424 ApplyPatchCommitErrorReason ,
2525 BlameIgnoreRevsFileBadRevisionError ,
2626 BlameIgnoreRevsFileError ,
27+ BranchError ,
2728 CherryPickError ,
2829 CherryPickErrorReason ,
2930 FetchError ,
@@ -1276,13 +1277,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12761277 }
12771278
12781279 @log ( )
1279- async createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1280- await this . git . branch ( repoPath , name , ref ) ;
1280+ createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1281+ try {
1282+ return void this . git . branch ( repoPath , name , ref ) ;
1283+ } catch ( ex ) {
1284+ if ( ex instanceof BranchError ) {
1285+ throw ex . WithBranch ( branch . name ) ;
1286+ }
1287+
1288+ throw ex ;
1289+ }
12811290 }
12821291
12831292 @log ( )
1284- async renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1285- await this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1293+ renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1294+ try {
1295+ return void this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1296+ } catch ( ex ) {
1297+ if ( ex instanceof BranchError ) {
1298+ throw ex . WithBranch ( branch . name ) ;
1299+ }
1300+
1301+ throw ex ;
1302+ }
12861303 }
12871304
12881305 @log ( )
@@ -1291,46 +1308,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12911308 branch : GitBranchReference ,
12921309 options : { force ?: boolean ; remote ?: boolean } ,
12931310 ) : Promise < void > {
1294- if ( branch . remote ) {
1295- return this . git . push ( repoPath , {
1296- delete : {
1297- remote : getRemoteNameFromBranchName ( branch . name ) ,
1298- branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1299- } ,
1300- } ) ;
1301- }
1311+ try {
1312+ if ( branch . remote ) {
1313+ await this . git . push ( repoPath , {
1314+ delete : {
1315+ remote : getRemoteNameFromBranchName ( branch . name ) ,
1316+ branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1317+ } ,
1318+ } ) ;
1319+ return ;
1320+ }
13021321
1303- const args = [ '--delete' ] ;
1304- if ( options . force ) {
1305- args . push ( '--force' ) ;
1306- }
1322+ const args = [ '--delete' ] ;
1323+ if ( options . force ) {
1324+ args . push ( '--force' ) ;
1325+ }
13071326
1308- if ( ! options . remote || ! branch . upstream ) {
1309- return this . git . branch ( repoPath , ...args , branch . ref ) ;
1310- }
1327+ if ( ! options . remote || ! branch . upstream ) {
1328+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1329+ return ;
1330+ }
13111331
1312- const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1313- remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1314- maxResults : 1 ,
1315- } ) ;
1332+ const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1333+ remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1334+ maxResults : 1 ,
1335+ } ) ;
13161336
1317- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1337+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
13181338
1319- try {
1320- await this . git . branch ( repoPath , ...args , branch . ref ) ;
1339+ try {
1340+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1341+ } catch ( ex ) {
1342+ // If it fails, restore the remote branch
1343+ await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1344+ await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1345+ throw ex ;
1346+ }
1347+
1348+ await this . git . push ( repoPath , {
1349+ delete : {
1350+ remote : remote ,
1351+ branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1352+ } ,
1353+ } ) ;
13211354 } catch ( ex ) {
1322- // If it fails, restore the remote branch
1323- await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1324- await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1355+ if ( ex instanceof BranchError ) {
1356+ throw ex . WithBranch ( branch . name ) ;
1357+ }
1358+
13251359 throw ex ;
13261360 }
1327-
1328- await this . git . push ( repoPath , {
1329- delete : {
1330- remote : remote ,
1331- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1332- } ,
1333- } ) ;
13341361 }
13351362
13361363 @log ( )
0 commit comments