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