Skip to content

Commit 1eeaf0b

Browse files
committed
refactor code
1 parent 31db995 commit 1eeaf0b

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-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 { getNameWithoutRemote, getReferenceLabel, isRevisionReference } from '../../git/models/reference';
56
import { Repository } from '../../git/models/repository';
@@ -400,7 +401,7 @@ export class BranchGitCommand extends QuickCommand {
400401
} catch (ex) {
401402
Logger.error(ex);
402403
// TODO likely need some better error handling here
403-
return showGenericErrorMessage('Unable to create branch');
404+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
404405
}
405406
}
406407
}
@@ -525,7 +526,9 @@ export class BranchGitCommand extends QuickCommand {
525526
} catch (ex) {
526527
Logger.error(ex);
527528
// TODO likely need some better error handling here
528-
return showGenericErrorMessage('Unable to delete branch');
529+
return showGenericErrorMessage(
530+
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
531+
);
529532
}
530533
}
531534
}
@@ -634,7 +637,7 @@ export class BranchGitCommand extends QuickCommand {
634637
} catch (ex) {
635638
Logger.error(ex);
636639
// TODO likely need some better error handling here
637-
return showGenericErrorMessage('Unable to rename branch');
640+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
638641
}
639642
}
640643
}

src/env/node/git/git.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -517,29 +517,19 @@ export class Git {
517517
await this.git<string>({ cwd: repoPath }, 'branch', ...args);
518518
} catch (ex) {
519519
const msg: string = ex?.toString() ?? '';
520-
let reason: BranchErrorReason = BranchErrorReason.Other;
521-
switch (true) {
522-
case GitErrors.noRemoteReference.test(msg) || GitErrors.noRemoteReference.test(ex.stderr ?? ''):
523-
reason = BranchErrorReason.NoRemoteReference;
524-
break;
525-
case GitErrors.invalidBranchName.test(msg) || GitErrors.invalidBranchName.test(ex.stderr ?? ''):
526-
reason = BranchErrorReason.InvalidBranchName;
527-
break;
528-
case GitErrors.branchAlreadyExists.test(msg) || GitErrors.branchAlreadyExists.test(ex.stderr ?? ''):
529-
reason = BranchErrorReason.BranchAlreadyExists;
530-
break;
531-
case GitErrors.branchNotFullyMerged.test(msg) || GitErrors.branchNotFullyMerged.test(ex.stderr ?? ''):
532-
reason = BranchErrorReason.BranchNotFullyMerged;
533-
break;
534-
case GitErrors.branchNotYetBorn.test(msg) || GitErrors.branchNotYetBorn.test(ex.stderr ?? ''):
535-
reason = BranchErrorReason.BranchNotYetBorn;
536-
break;
537-
case GitErrors.branchFastForwardRejected.test(msg) ||
538-
GitErrors.branchFastForwardRejected.test(ex.stderr ?? ''):
539-
reason = BranchErrorReason.BranchFastForwardRejected;
540-
break;
520+
for (const [error, reason] of [
521+
[GitErrors.noRemoteReference, BranchErrorReason.NoRemoteReference],
522+
[GitErrors.invalidBranchName, BranchErrorReason.InvalidBranchName],
523+
[GitErrors.branchAlreadyExists, BranchErrorReason.BranchAlreadyExists],
524+
[GitErrors.branchNotFullyMerged, BranchErrorReason.BranchNotFullyMerged],
525+
[GitErrors.branchNotYetBorn, BranchErrorReason.BranchNotYetBorn],
526+
[GitErrors.branchFastForwardRejected, BranchErrorReason.BranchFastForwardRejected],
527+
]) {
528+
if (error.test(msg) || error.test(ex.stderr ?? '')) {
529+
throw new BranchError(reason, ex);
530+
}
541531
}
542-
throw new BranchError(reason, ex);
532+
throw new BranchError(BranchErrorReason.Other, ex);
543533
}
544534
}
545535

@@ -1024,9 +1014,9 @@ export class Git {
10241014
} else {
10251015
params.push(options.remote, options.branch);
10261016
}
1027-
} else if (options.remote != null) {
1017+
} else if (options.remote) {
10281018
params.push(options.remote);
1029-
} else if (options.delete != null) {
1019+
} else if (options.delete) {
10301020
params.push('-d', options.delete.remote, ...options.delete.branches);
10311021
}
10321022

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)