Skip to content

Commit 1279905

Browse files
committed
handle BranchError in localGitProvider
# Conflicts: # src/env/node/git/localGitProvider.ts
1 parent 33bfb9d commit 1279905

File tree

3 files changed

+68
-41
lines changed

3 files changed

+68
-41
lines changed

src/commands/git/branch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class BranchGitCommand extends QuickCommand {
428428
} catch (ex) {
429429
Logger.error(ex);
430430
// TODO likely need some better error handling here
431-
return showGenericErrorMessage(ex.WithBranch(state.name));
431+
return showGenericErrorMessage(ex);
432432
}
433433
}
434434
}
@@ -567,9 +567,9 @@ export class BranchGitCommand extends QuickCommand {
567567
remote: state.flags.includes('--remotes'),
568568
});
569569
} catch (ex) {
570-
Logger.error(ex);
571570
// TODO likely need some better error handling here
572-
return showGenericErrorMessage(ex.WithBranch(ref.name));
571+
Logger.error(ex);
572+
return showGenericErrorMessage(ex);
573573
}
574574
}
575575
}
@@ -679,7 +679,7 @@ export class BranchGitCommand extends QuickCommand {
679679
} catch (ex) {
680680
Logger.error(ex);
681681
// TODO likely need some better error handling here
682-
return showGenericErrorMessage(ex.WithBranch(state.name));
682+
return showGenericErrorMessage(ex);
683683
}
684684
}
685685
}

src/env/node/git/localGitProvider.ts

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
ApplyPatchCommitErrorReason,
2525
BlameIgnoreRevsFileBadRevisionError,
2626
BlameIgnoreRevsFileError,
27+
BranchError,
2728
CherryPickError,
2829
CherryPickErrorReason,
2930
FetchError,
@@ -1276,13 +1277,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
12761277
}
12771278

12781279
@log()
1279-
async createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1280-
await this.git.branch(repoPath, name, ref);
1280+
createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1281+
try {
1282+
return void this.git.branch(repoPath, name, ref);
1283+
} catch (ex) {
1284+
if (ex instanceof BranchError) {
1285+
throw ex.WithBranch(branch.name);
1286+
}
1287+
1288+
throw ex;
1289+
}
12811290
}
12821291

12831292
@log()
1284-
async renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1285-
await this.git.branch(repoPath, '-m', oldName, newName);
1293+
renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1294+
try {
1295+
return void this.git.branch(repoPath, '-m', oldName, newName);
1296+
} catch (ex) {
1297+
if (ex instanceof BranchError) {
1298+
throw ex.WithBranch(branch.name);
1299+
}
1300+
1301+
throw ex;
1302+
}
12861303
}
12871304

12881305
@log()
@@ -1291,46 +1308,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
12911308
branch: GitBranchReference,
12921309
options: { force?: boolean; remote?: boolean },
12931310
): Promise<void> {
1294-
if (branch.remote) {
1295-
return this.git.push(repoPath, {
1296-
delete: {
1297-
remote: getRemoteNameFromBranchName(branch.name),
1298-
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1299-
},
1300-
});
1301-
}
1311+
try {
1312+
if (branch.remote) {
1313+
await this.git.push(repoPath, {
1314+
delete: {
1315+
remote: getRemoteNameFromBranchName(branch.name),
1316+
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
1317+
},
1318+
});
1319+
return;
1320+
}
13021321

1303-
const args = ['--delete'];
1304-
if (options.force) {
1305-
args.push('--force');
1306-
}
1322+
const args = ['--delete'];
1323+
if (options.force) {
1324+
args.push('--force');
1325+
}
13071326

1308-
if (!options.remote || !branch.upstream) {
1309-
return this.git.branch(repoPath, ...args, branch.ref);
1310-
}
1327+
if (!options.remote || !branch.upstream) {
1328+
await this.git.branch(repoPath, ...args, branch.ref);
1329+
return;
1330+
}
13111331

1312-
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1313-
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1314-
maxResults: 1,
1315-
});
1332+
const remote = getRemoteNameFromBranchName(branch.upstream.name);
1333+
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
1334+
maxResults: 1,
1335+
});
13161336

1317-
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
1337+
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
13181338

1319-
try {
1320-
await this.git.branch(repoPath, ...args, branch.ref);
1339+
try {
1340+
await this.git.branch(repoPath, ...args, branch.ref);
1341+
} catch (ex) {
1342+
// If it fails, restore the remote branch
1343+
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1344+
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1345+
throw ex;
1346+
}
1347+
1348+
await this.git.push(repoPath, {
1349+
delete: {
1350+
remote: remote,
1351+
branch: getBranchNameWithoutRemote(branch.upstream.name),
1352+
},
1353+
});
13211354
} catch (ex) {
1322-
// If it fails, restore the remote branch
1323-
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
1324-
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
1355+
if (ex instanceof BranchError) {
1356+
throw ex.WithBranch(branch.name);
1357+
}
1358+
13251359
throw ex;
13261360
}
1327-
1328-
await this.git.push(repoPath, {
1329-
delete: {
1330-
remote: remote,
1331-
branch: getBranchNameWithoutRemote(branch.upstream.name),
1332-
},
1333-
});
13341361
}
13351362

13361363
@log()

src/git/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class BranchError extends Error {
252252
}
253253

254254
private static buildBranchErrorMessage(reason?: BranchErrorReason, branch?: string): string {
255-
const baseMessage = `Unable to perform action on branch${branch ? ` '${branch}'` : ''}`;
255+
const baseMessage = `Unable to perform action ${branch ? `with branch '${branch}'` : 'on branch'}`;
256256
switch (reason) {
257257
case BranchErrorReason.BranchAlreadyExists:
258258
return `${baseMessage} because it already exists`;

0 commit comments

Comments
 (0)