Skip to content

Commit 543ae49

Browse files
committed
refactor code
1 parent 3d229ee commit 543ae49

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

src/commands/git/branch.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { QuickInputButtons } from 'vscode';
22
import type { Container } from '../../container';
3+
import { BranchError } from '../../git/errors';
34
import type { GitBranchReference, GitReference } from '../../git/models/reference';
45
import {
56
getNameWithoutRemote,
@@ -413,7 +414,7 @@ export class BranchGitCommand extends QuickCommand {
413414
} catch (ex) {
414415
Logger.error(ex);
415416
// TODO likely need some better error handling here
416-
return showGenericErrorMessage('Unable to create branch');
417+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
417418
}
418419
}
419420
}
@@ -538,7 +539,9 @@ export class BranchGitCommand extends QuickCommand {
538539
} catch (ex) {
539540
Logger.error(ex);
540541
// TODO likely need some better error handling here
541-
return showGenericErrorMessage('Unable to delete branch');
542+
return showGenericErrorMessage(
543+
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
544+
);
542545
}
543546
}
544547
}
@@ -647,7 +650,7 @@ export class BranchGitCommand extends QuickCommand {
647650
} catch (ex) {
648651
Logger.error(ex);
649652
// TODO likely need some better error handling here
650-
return showGenericErrorMessage('Unable to rename branch');
653+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
651654
}
652655
}
653656
}

src/env/node/git/git.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ function getStdinUniqueKey(): number {
166166
type ExitCodeOnlyGitCommandOptions = GitCommandOptions & { exitCodeOnly: true };
167167
export type PushForceOptions = { withLease: true; ifIncludes?: boolean } | { withLease: false; ifIncludes?: never };
168168

169+
const branchErrorAndReason = [
170+
[GitErrors.noRemoteReference, BranchErrorReason.NoRemoteReference],
171+
[GitErrors.invalidBranchName, BranchErrorReason.InvalidBranchName],
172+
[GitErrors.branchAlreadyExists, BranchErrorReason.BranchAlreadyExists],
173+
[GitErrors.branchNotFullyMerged, BranchErrorReason.BranchNotFullyMerged],
174+
[GitErrors.branchNotYetBorn, BranchErrorReason.BranchNotYetBorn],
175+
[GitErrors.branchFastForwardRejected, BranchErrorReason.BranchFastForwardRejected],
176+
];
177+
169178
export class Git {
170179
/** Map of running git commands -- avoids running duplicate overlaping commands */
171180
private readonly pendingCommands = new Map<string, Promise<string | Buffer>>();
@@ -518,29 +527,12 @@ export class Git {
518527
await this.git<string>({ cwd: repoPath }, 'branch', ...args);
519528
} catch (ex) {
520529
const msg: string = ex?.toString() ?? '';
521-
let reason: BranchErrorReason = BranchErrorReason.Other;
522-
switch (true) {
523-
case GitErrors.noRemoteReference.test(msg) || GitErrors.noRemoteReference.test(ex.stderr ?? ''):
524-
reason = BranchErrorReason.NoRemoteReference;
525-
break;
526-
case GitErrors.invalidBranchName.test(msg) || GitErrors.invalidBranchName.test(ex.stderr ?? ''):
527-
reason = BranchErrorReason.InvalidBranchName;
528-
break;
529-
case GitErrors.branchAlreadyExists.test(msg) || GitErrors.branchAlreadyExists.test(ex.stderr ?? ''):
530-
reason = BranchErrorReason.BranchAlreadyExists;
531-
break;
532-
case GitErrors.branchNotFullyMerged.test(msg) || GitErrors.branchNotFullyMerged.test(ex.stderr ?? ''):
533-
reason = BranchErrorReason.BranchNotFullyMerged;
534-
break;
535-
case GitErrors.branchNotYetBorn.test(msg) || GitErrors.branchNotYetBorn.test(ex.stderr ?? ''):
536-
reason = BranchErrorReason.BranchNotYetBorn;
537-
break;
538-
case GitErrors.branchFastForwardRejected.test(msg) ||
539-
GitErrors.branchFastForwardRejected.test(ex.stderr ?? ''):
540-
reason = BranchErrorReason.BranchFastForwardRejected;
541-
break;
530+
for (const [error, reason] of branchErrorAndReason) {
531+
if (error.test(msg) || error.test(ex.stderr ?? '')) {
532+
throw new BranchError(reason, ex);
533+
}
542534
}
543-
throw new BranchError(reason, ex);
535+
throw new BranchError(BranchErrorReason.Other, ex);
544536
}
545537
}
546538

@@ -1025,9 +1017,9 @@ export class Git {
10251017
} else {
10261018
params.push(options.remote, options.branch);
10271019
}
1028-
} else if (options.remote != null) {
1020+
} else if (options.remote) {
10291021
params.push(options.remote);
1030-
} else if (options.delete != null) {
1022+
} else if (options.delete) {
10311023
params.push('-d', options.delete.remote, ...options.delete.branches);
10321024
}
10331025

src/git/errors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,16 @@ export class BranchError extends Error {
249249
reason = messageOrReason;
250250
switch (reason) {
251251
case BranchErrorReason.BranchAlreadyExists:
252-
message = `${baseMessage} because it already exists.`;
252+
message = `${baseMessage} because it already exists`;
253253
break;
254254
case BranchErrorReason.BranchNotFullyMerged:
255-
message = `${baseMessage} because it is not fully merged.`;
255+
message = `${baseMessage} because it is not fully merged`;
256256
break;
257257
case BranchErrorReason.NoRemoteReference:
258-
message = `${baseMessage} because the remote reference does not exist.`;
258+
message = `${baseMessage} because the remote reference does not exist`;
259259
break;
260260
case BranchErrorReason.InvalidBranchName:
261-
message = `${baseMessage} because the branch name is invalid.`;
261+
message = `${baseMessage} because the branch name is invalid`;
262262
break;
263263
default:
264264
message = baseMessage;

0 commit comments

Comments
 (0)