Skip to content

Commit 8966441

Browse files
committed
refactor code
1 parent 080f265 commit 8966441

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,
@@ -427,7 +428,7 @@ export class BranchGitCommand extends QuickCommand {
427428
} catch (ex) {
428429
Logger.error(ex);
429430
// TODO likely need some better error handling here
430-
return showGenericErrorMessage('Unable to create branch');
431+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
431432
}
432433
}
433434
}
@@ -552,7 +553,9 @@ export class BranchGitCommand extends QuickCommand {
552553
} catch (ex) {
553554
Logger.error(ex);
554555
// TODO likely need some better error handling here
555-
return showGenericErrorMessage('Unable to delete branch');
556+
return showGenericErrorMessage(
557+
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
558+
);
556559
}
557560
}
558561
}
@@ -661,7 +664,7 @@ export class BranchGitCommand extends QuickCommand {
661664
} catch (ex) {
662665
Logger.error(ex);
663666
// TODO likely need some better error handling here
664-
return showGenericErrorMessage('Unable to rename branch');
667+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
665668
}
666669
}
667670
}

src/env/node/git/git.ts

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

174+
const branchErrorAndReason = [
175+
[GitErrors.noRemoteReference, BranchErrorReason.NoRemoteReference],
176+
[GitErrors.invalidBranchName, BranchErrorReason.InvalidBranchName],
177+
[GitErrors.branchAlreadyExists, BranchErrorReason.BranchAlreadyExists],
178+
[GitErrors.branchNotFullyMerged, BranchErrorReason.BranchNotFullyMerged],
179+
[GitErrors.branchNotYetBorn, BranchErrorReason.BranchNotYetBorn],
180+
[GitErrors.branchFastForwardRejected, BranchErrorReason.BranchFastForwardRejected],
181+
];
182+
174183
const tagErrorAndReason: [RegExp, TagErrorReason][] = [
175184
[GitErrors.tagAlreadyExists, TagErrorReason.TagAlreadyExists],
176185
[GitErrors.tagNotFound, TagErrorReason.TagNotFound],
@@ -531,29 +540,12 @@ export class Git {
531540
await this.git<string>({ cwd: repoPath }, 'branch', ...args);
532541
} catch (ex) {
533542
const msg: string = ex?.toString() ?? '';
534-
let reason: BranchErrorReason = BranchErrorReason.Other;
535-
switch (true) {
536-
case GitErrors.noRemoteReference.test(msg) || GitErrors.noRemoteReference.test(ex.stderr ?? ''):
537-
reason = BranchErrorReason.NoRemoteReference;
538-
break;
539-
case GitErrors.invalidBranchName.test(msg) || GitErrors.invalidBranchName.test(ex.stderr ?? ''):
540-
reason = BranchErrorReason.InvalidBranchName;
541-
break;
542-
case GitErrors.branchAlreadyExists.test(msg) || GitErrors.branchAlreadyExists.test(ex.stderr ?? ''):
543-
reason = BranchErrorReason.BranchAlreadyExists;
544-
break;
545-
case GitErrors.branchNotFullyMerged.test(msg) || GitErrors.branchNotFullyMerged.test(ex.stderr ?? ''):
546-
reason = BranchErrorReason.BranchNotFullyMerged;
547-
break;
548-
case GitErrors.branchNotYetBorn.test(msg) || GitErrors.branchNotYetBorn.test(ex.stderr ?? ''):
549-
reason = BranchErrorReason.BranchNotYetBorn;
550-
break;
551-
case GitErrors.branchFastForwardRejected.test(msg) ||
552-
GitErrors.branchFastForwardRejected.test(ex.stderr ?? ''):
553-
reason = BranchErrorReason.BranchFastForwardRejected;
554-
break;
543+
for (const [error, reason] of branchErrorAndReason) {
544+
if (error.test(msg) || error.test(ex.stderr ?? '')) {
545+
throw new BranchError(reason, ex);
546+
}
555547
}
556-
throw new BranchError(reason, ex);
548+
throw new BranchError(BranchErrorReason.Other, ex);
557549
}
558550
}
559551

@@ -1038,9 +1030,9 @@ export class Git {
10381030
} else {
10391031
params.push(options.remote, options.branch);
10401032
}
1041-
} else if (options.remote != null) {
1033+
} else if (options.remote) {
10421034
params.push(options.remote);
1043-
} else if (options.delete != null) {
1035+
} else if (options.delete) {
10441036
params.push('-d', options.delete.remote, ...options.delete.branches);
10451037
}
10461038

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)