@@ -23,6 +23,7 @@ import {
23
23
ApplyPatchCommitErrorReason ,
24
24
BlameIgnoreRevsFileBadRevisionError ,
25
25
BlameIgnoreRevsFileError ,
26
+ BranchError ,
26
27
CherryPickError ,
27
28
CherryPickErrorReason ,
28
29
FetchError ,
@@ -182,17 +183,7 @@ import { countStringLength, filterMap } from '../../../system/array';
182
183
import { gate } from '../../../system/decorators/gate' ;
183
184
import { debug , log } from '../../../system/decorators/log' ;
184
185
import { debounce } from '../../../system/function' ;
185
- import {
186
- filterMap as filterMapIterable ,
187
- find ,
188
- first ,
189
- groupByMap ,
190
- join ,
191
- last ,
192
- map ,
193
- skip ,
194
- some ,
195
- } from '../../../system/iterable' ;
186
+ import { filterMap as filterMapIterable , find , first , join , last , map , skip , some } from '../../../system/iterable' ;
196
187
import { Logger } from '../../../system/logger' ;
197
188
import type { LogScope } from '../../../system/logger.scope' ;
198
189
import { getLogScope , setLogScopeExit } from '../../../system/logger.scope' ;
@@ -1241,13 +1232,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
1241
1232
}
1242
1233
1243
1234
@log ( )
1244
- async createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1245
- await this . git . branch ( repoPath , name , ref ) ;
1235
+ createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1236
+ try {
1237
+ return void this . git . branch ( repoPath , name , ref ) ;
1238
+ } catch ( ex ) {
1239
+ if ( ex instanceof BranchError ) {
1240
+ throw ex . WithBranch ( branch . name ) ;
1241
+ }
1242
+
1243
+ throw ex ;
1244
+ }
1246
1245
}
1247
1246
1248
1247
@log ( )
1249
- async renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1250
- await this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1248
+ renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1249
+ try {
1250
+ return void this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1251
+ } catch ( ex ) {
1252
+ if ( ex instanceof BranchError ) {
1253
+ throw ex . WithBranch ( branch . name ) ;
1254
+ }
1255
+
1256
+ throw ex ;
1257
+ }
1251
1258
}
1252
1259
1253
1260
@log ( )
@@ -1256,46 +1263,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
1256
1263
branch : GitBranchReference ,
1257
1264
options : { force ?: boolean ; remote ?: boolean } ,
1258
1265
) : Promise < void > {
1259
- if ( branch . remote ) {
1260
- return this . git . push ( repoPath , {
1261
- delete : {
1262
- remote : getRemoteNameFromBranchName ( branch . name ) ,
1263
- branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1264
- } ,
1265
- } ) ;
1266
- }
1266
+ try {
1267
+ if ( branch . remote ) {
1268
+ await this . git . push ( repoPath , {
1269
+ delete : {
1270
+ remote : getRemoteNameFromBranchName ( branch . name ) ,
1271
+ branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1272
+ } ,
1273
+ } ) ;
1274
+ return ;
1275
+ }
1267
1276
1268
- const args = [ '--delete' ] ;
1269
- if ( options . force ) {
1270
- args . push ( '--force' ) ;
1271
- }
1277
+ const args = [ '--delete' ] ;
1278
+ if ( options . force ) {
1279
+ args . push ( '--force' ) ;
1280
+ }
1272
1281
1273
- if ( ! options . remote || ! branch . upstream ) {
1274
- return this . git . branch ( repoPath , ...args , branch . ref ) ;
1275
- }
1282
+ if ( ! options . remote || ! branch . upstream ) {
1283
+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1284
+ return ;
1285
+ }
1276
1286
1277
- const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1278
- remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1279
- maxResults : 1 ,
1280
- } ) ;
1287
+ const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1288
+ remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1289
+ maxResults : 1 ,
1290
+ } ) ;
1281
1291
1282
- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1292
+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1283
1293
1284
- try {
1285
- await this . git . branch ( repoPath , ...args , branch . ref ) ;
1294
+ try {
1295
+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1296
+ } catch ( ex ) {
1297
+ // If it fails, restore the remote branch
1298
+ await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1299
+ await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1300
+ throw ex ;
1301
+ }
1302
+
1303
+ await this . git . push ( repoPath , {
1304
+ delete : {
1305
+ remote : remote ,
1306
+ branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1307
+ } ,
1308
+ } ) ;
1286
1309
} catch ( ex ) {
1287
- // If it fails, restore the remote branch
1288
- await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1289
- await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1310
+ if ( ex instanceof BranchError ) {
1311
+ throw ex . WithBranch ( branch . name ) ;
1312
+ }
1313
+
1290
1314
throw ex ;
1291
1315
}
1292
-
1293
- await this . git . push ( repoPath , {
1294
- delete : {
1295
- remote : remote ,
1296
- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1297
- } ,
1298
- } ) ;
1299
1316
}
1300
1317
1301
1318
@log ( )
0 commit comments