@@ -23,6 +23,7 @@ import {
2323 ApplyPatchCommitErrorReason ,
2424 BlameIgnoreRevsFileBadRevisionError ,
2525 BlameIgnoreRevsFileError ,
26+ BranchError ,
2627 CherryPickError ,
2728 CherryPickErrorReason ,
2829 FetchError ,
@@ -182,17 +183,7 @@ import { countStringLength, filterMap } from '../../../system/array';
182183import { gate } from '../../../system/decorators/gate' ;
183184import { debug , log } from '../../../system/decorators/log' ;
184185import { 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' ;
196187import { Logger } from '../../../system/logger' ;
197188import type { LogScope } from '../../../system/logger.scope' ;
198189import { getLogScope , setLogScopeExit } from '../../../system/logger.scope' ;
@@ -1241,13 +1232,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12411232 }
12421233
12431234 @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+ }
12461245 }
12471246
12481247 @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+ }
12511258 }
12521259
12531260 @log ( )
@@ -1256,46 +1263,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12561263 branch : GitBranchReference ,
12571264 options : { force ?: boolean ; remote ?: boolean } ,
12581265 ) : 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+ }
12671276
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+ }
12721281
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+ }
12761286
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+ } ) ;
12811291
1282- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1292+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
12831293
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+ } ) ;
12861309 } 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+
12901314 throw ex ;
12911315 }
1292-
1293- await this . git . push ( repoPath , {
1294- delete : {
1295- remote : remote ,
1296- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1297- } ,
1298- } ) ;
12991316 }
13001317
13011318 @log ( )
0 commit comments