@@ -24,6 +24,7 @@ import {
24
24
ApplyPatchCommitErrorReason ,
25
25
BlameIgnoreRevsFileBadRevisionError ,
26
26
BlameIgnoreRevsFileError ,
27
+ BranchError ,
27
28
CherryPickError ,
28
29
CherryPickErrorReason ,
29
30
FetchError ,
@@ -1276,13 +1277,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
1276
1277
}
1277
1278
1278
1279
@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
+ }
1281
1290
}
1282
1291
1283
1292
@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
+ }
1286
1303
}
1287
1304
1288
1305
@log ( )
@@ -1291,46 +1308,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
1291
1308
branch : GitBranchReference ,
1292
1309
options : { force ?: boolean ; remote ?: boolean } ,
1293
1310
) : 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
+ }
1302
1321
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
+ }
1307
1326
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
+ }
1311
1331
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
+ } ) ;
1316
1336
1317
- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1337
+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1318
1338
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
+ } ) ;
1321
1354
} 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
+
1325
1359
throw ex ;
1326
1360
}
1327
-
1328
- await this . git . push ( repoPath , {
1329
- delete : {
1330
- remote : remote ,
1331
- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1332
- } ,
1333
- } ) ;
1334
1361
}
1335
1362
1336
1363
@log ( )
0 commit comments