@@ -24,6 +24,7 @@ import {
2424 ApplyPatchCommitErrorReason ,
2525 BlameIgnoreRevsFileBadRevisionError ,
2626 BlameIgnoreRevsFileError ,
27+ BranchError ,
2728 CherryPickError ,
2829 CherryPickErrorReason ,
2930 FetchError ,
@@ -188,17 +189,7 @@ import { countStringLength, filterMap } from '../../../system/array';
188189import { gate } from '../../../system/decorators/gate' ;
189190import { debug , log } from '../../../system/decorators/log' ;
190191import { debounce } from '../../../system/function' ;
191- import {
192- filterMap as filterMapIterable ,
193- find ,
194- first ,
195- groupByMap ,
196- join ,
197- last ,
198- map ,
199- skip ,
200- some ,
201- } from '../../../system/iterable' ;
192+ import { filterMap as filterMapIterable , find , first , join , last , map , skip , some } from '../../../system/iterable' ;
202193import { Logger } from '../../../system/logger' ;
203194import type { LogScope } from '../../../system/logger.scope' ;
204195import { getLogScope , setLogScopeExit } from '../../../system/logger.scope' ;
@@ -1265,13 +1256,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12651256 }
12661257
12671258 @log ( )
1268- async createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1269- await this . git . branch ( repoPath , name , ref ) ;
1259+ createBranch ( repoPath : string , name : string , ref : string ) : Promise < void > {
1260+ try {
1261+ return void this . git . branch ( repoPath , name , ref ) ;
1262+ } catch ( ex ) {
1263+ if ( ex instanceof BranchError ) {
1264+ throw ex . WithBranch ( branch . name ) ;
1265+ }
1266+
1267+ throw ex ;
1268+ }
12701269 }
12711270
12721271 @log ( )
1273- async renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1274- await this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1272+ renameBranch ( repoPath : string , oldName : string , newName : string ) : Promise < void > {
1273+ try {
1274+ return void this . git . branch ( repoPath , '-m' , oldName , newName ) ;
1275+ } catch ( ex ) {
1276+ if ( ex instanceof BranchError ) {
1277+ throw ex . WithBranch ( branch . name ) ;
1278+ }
1279+
1280+ throw ex ;
1281+ }
12751282 }
12761283
12771284 @log ( )
@@ -1280,46 +1287,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12801287 branch : GitBranchReference ,
12811288 options : { force ?: boolean ; remote ?: boolean } ,
12821289 ) : Promise < void > {
1283- if ( branch . remote ) {
1284- return this . git . push ( repoPath , {
1285- delete : {
1286- remote : getRemoteNameFromBranchName ( branch . name ) ,
1287- branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1288- } ,
1289- } ) ;
1290- }
1290+ try {
1291+ if ( branch . remote ) {
1292+ await this . git . push ( repoPath , {
1293+ delete : {
1294+ remote : getRemoteNameFromBranchName ( branch . name ) ,
1295+ branch : branch . remote ? getBranchNameWithoutRemote ( branch . name ) : branch . name ,
1296+ } ,
1297+ } ) ;
1298+ return ;
1299+ }
12911300
1292- const args = [ '--delete' ] ;
1293- if ( options . force ) {
1294- args . push ( '--force' ) ;
1295- }
1301+ const args = [ '--delete' ] ;
1302+ if ( options . force ) {
1303+ args . push ( '--force' ) ;
1304+ }
12961305
1297- if ( ! options . remote || ! branch . upstream ) {
1298- return this . git . branch ( repoPath , ...args , branch . ref ) ;
1299- }
1306+ if ( ! options . remote || ! branch . upstream ) {
1307+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1308+ return ;
1309+ }
13001310
1301- const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1302- remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1303- maxResults : 1 ,
1304- } ) ;
1311+ const remote = getRemoteNameFromBranchName ( branch . upstream . name ) ;
1312+ remoteCommit = await this . git . rev_list ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , {
1313+ maxResults : 1 ,
1314+ } ) ;
13051315
1306- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1316+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
13071317
1308- try {
1309- await this . git . branch ( repoPath , ...args , branch . ref ) ;
1318+ try {
1319+ await this . git . branch ( repoPath , ...args , branch . ref ) ;
1320+ } catch ( ex ) {
1321+ // If it fails, restore the remote branch
1322+ await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1323+ await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1324+ throw ex ;
1325+ }
1326+
1327+ await this . git . push ( repoPath , {
1328+ delete : {
1329+ remote : remote ,
1330+ branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1331+ } ,
1332+ } ) ;
13101333 } catch ( ex ) {
1311- // If it fails, restore the remote branch
1312- await this . git . update_ref ( repoPath , `refs/remotes/${ remote } /${ branch . ref } ` , commit ) ;
1313- await this . git . branch__set_upstream ( repoPath , branch , remote , branch ) ;
1334+ if ( ex instanceof BranchError ) {
1335+ throw ex . WithBranch ( branch . name ) ;
1336+ }
1337+
13141338 throw ex ;
13151339 }
1316-
1317- await this . git . push ( repoPath , {
1318- delete : {
1319- remote : remote ,
1320- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1321- } ,
1322- } ) ;
13231340 }
13241341
13251342 @log ( )
0 commit comments