@@ -24,6 +24,7 @@ import {
2424 ApplyPatchCommitErrorReason ,
2525 BlameIgnoreRevsFileBadRevisionError ,
2626 BlameIgnoreRevsFileError ,
27+ BranchError ,
2728 CherryPickError ,
2829 CherryPickErrorReason ,
2930 FetchError ,
@@ -187,17 +188,7 @@ import { countStringLength, filterMap } from '../../../system/array';
187188import { gate } from '../../../system/decorators/gate' ;
188189import { debug , log } from '../../../system/decorators/log' ;
189190import { 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' ;
201192import { Logger } from '../../../system/logger' ;
202193import type { LogScope } from '../../../system/logger.scope' ;
203194import { getLogScope , setLogScopeExit } from '../../../system/logger.scope' ;
@@ -1264,13 +1255,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12641255 }
12651256
12661257 @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+ }
12691268 }
12701269
12711270 @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+ }
12741281 }
12751282
12761283 @log ( )
@@ -1279,46 +1286,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12791286 branch : GitBranchReference ,
12801287 options : { force ?: boolean ; remote ?: boolean } ,
12811288 ) : 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+ }
12901299
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+ }
12951304
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+ }
12991309
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+ } ) ;
13041314
1305- await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
1315+ await this . git . branch ( repoPath , '--delete' , '--remotes' , `${ remote } /${ branch . ref } ` ) ;
13061316
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+ } ) ;
13091332 } 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+
13131337 throw ex ;
13141338 }
1315-
1316- await this . git . push ( repoPath , {
1317- delete : {
1318- remote : remote ,
1319- branch : getBranchNameWithoutRemote ( branch . upstream . name ) ,
1320- } ,
1321- } ) ;
13221339 }
13231340
13241341 @log ( )
0 commit comments