@@ -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 ,
@@ -188,17 +189,7 @@ import { countStringLength, filterMap } from '../../../system/array';
188
189
import { gate } from '../../../system/decorators/gate' ;
189
190
import { debug , log } from '../../../system/decorators/log' ;
190
191
import { debounce } from '../../../system/function' ;
191
- import {
192
- filterMap as filterMapIterable ,
193
- find ,
194
- first ,
195
- groupByMap ,
196
- join ,
197
- last ,
198
- map ,
199
- skip ,
200
- some ,
201
- } from '../../../system/iterable' ;
192
+ import { filterMap as filterMapIterable , find , first , join , last , map , skip , some } from '../../../system/iterable' ;
202
193
import { Logger } from '../../../system/logger' ;
203
194
import type { LogScope } from '../../../system/logger.scope' ;
204
195
import { getLogScope , setLogScopeExit } from '../../../system/logger.scope' ;
@@ -1265,13 +1256,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
1265
1256
}
1266
1257
1267
1258
@log ( )
1268
- async createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1269
- await this . git . branch ( repoPath , name , ref ) ;
1259
+ createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1260
+ try {
1261
+ return void this . git . branch ( repoPath , name , ref ) ;
1262
+ } catch ( ex ) {
1263
+ if ( ex instanceof BranchError ) {
1264
+ throw ex . WithBranch ( branch . name ) ;
1265
+ }
1266
+
1267
+ throw ex ;
1268
+ }
1270
1269
}
1271
1270
1272
1271
@log ( )
1273
- async renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1274
- await this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1272
+ renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1273
+ try {
1274
+ return void this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1275
+ } catch ( ex ) {
1276
+ if ( ex instanceof BranchError ) {
1277
+ throw ex . WithBranch ( branch . name ) ;
1278
+ }
1279
+
1280
+ throw ex ;
1281
+ }
1275
1282
}
1276
1283
1277
1284
@log ( )
@@ -1280,46 +1287,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
1280
1287
branch : GitBranchReference ,
1281
1288
options : { force ?: boolean ; remote ?: boolean } ,
1282
1289
) : Promise < void > {
1283
- if ( branch . remote ) {
1284
- return this . git . push ( repoPath , {
1285
- delete : {
1286
- remote : getRemoteNameFromBranchName ( branch . name ) ,
1287
- branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1288
- } ,
1289
- } ) ;
1290
- }
1290
+ try {
1291
+ if ( branch . remote ) {
1292
+ await this . git . push ( repoPath , {
1293
+ delete : {
1294
+ remote : getRemoteNameFromBranchName ( branch . name ) ,
1295
+ branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1296
+ } ,
1297
+ } ) ;
1298
+ return ;
1299
+ }
1291
1300
1292
- const args = [ '--delete' ] ;
1293
- if ( options . force ) {
1294
- args . push ( '--force' ) ;
1295
- }
1301
+ const args = [ '--delete' ] ;
1302
+ if ( options . force ) {
1303
+ args . push ( '--force' ) ;
1304
+ }
1296
1305
1297
- if ( ! options . remote || ! branch . upstream ) {
1298
- return this . git . branch ( repoPath , ...args , branch . ref ) ;
1299
- }
1306
+ if ( ! options . remote || ! branch . upstream ) {
1307
+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1308
+ return ;
1309
+ }
1300
1310
1301
- const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1302
- remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1303
- maxResults : 1 ,
1304
- } ) ;
1311
+ const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1312
+ remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1313
+ maxResults : 1 ,
1314
+ } ) ;
1305
1315
1306
- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1316
+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1307
1317
1308
- try {
1309
- await this . git . branch ( repoPath , ...args , branch . ref ) ;
1318
+ try {
1319
+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1320
+ } catch ( ex ) {
1321
+ // If it fails, restore the remote branch
1322
+ await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1323
+ await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1324
+ throw ex ;
1325
+ }
1326
+
1327
+ await this . git . push ( repoPath , {
1328
+ delete : {
1329
+ remote : remote ,
1330
+ branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1331
+ } ,
1332
+ } ) ;
1310
1333
} catch ( ex ) {
1311
- // If it fails, restore the remote branch
1312
- await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1313
- await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1334
+ if ( ex instanceof BranchError ) {
1335
+ throw ex . WithBranch ( branch . name ) ;
1336
+ }
1337
+
1314
1338
throw ex ;
1315
1339
}
1316
-
1317
- await this . git . push ( repoPath , {
1318
- delete : {
1319
- remote : remote ,
1320
- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1321
- } ,
1322
- } ) ;
1323
1340
}
1324
1341
1325
1342
@log ( )
0 commit comments