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