@@ -24,6 +24,7 @@ import {
2424 ApplyPatchCommitErrorReason ,
2525 BlameIgnoreRevsFileBadRevisionError ,
2626 BlameIgnoreRevsFileError ,
27+ BranchError ,
2728 CherryPickError ,
2829 CherryPickErrorReason ,
2930 FetchError ,
@@ -185,17 +186,7 @@ import { countStringLength, filterMap } from '../../../system/array';
185186import { gate } from '../../../system/decorators/gate' ;
186187import { debug , log } from '../../../system/decorators/log' ;
187188import { 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' ;
199190import { Logger } from '../../../system/logger' ;
200191import type { LogScope } from '../../../system/logger.scope' ;
201192import { getLogScope , setLogScopeExit } from '../../../system/logger.scope' ;
@@ -1262,13 +1253,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12621253 }
12631254
12641255 @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+ }
12671266 }
12681267
12691268 @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+ }
12721279 }
12731280
12741281 @log ( )
@@ -1277,46 +1284,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12771284 branch : GitBranchReference ,
12781285 options : { force ?: boolean ; remote ?: boolean } ,
12791286 ) : 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+ }
12881297
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+ }
12931302
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+ }
12971307
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+ } ) ;
13021312
1303- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1313+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
13041314
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+ } ) ;
13071330 } 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+
13111335 throw ex ;
13121336 }
1313-
1314- await this . git . push ( repoPath , {
1315- delete : {
1316- remote : remote ,
1317- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1318- } ,
1319- } ) ;
13201337 }
13211338
13221339 @log ( )
0 commit comments