Skip to content

Commit c01aec5

Browse files
committed
refactor code
1 parent c0009f3 commit c01aec5

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 { getReferenceLabel, isRevisionReference } from '../../git/models/reference';
56
import { Repository } from '../../git/models/repository';
@@ -404,7 +405,7 @@ export class BranchGitCommand extends QuickCommand {
404405
} catch (ex) {
405406
Logger.error(ex);
406407
// TODO likely need some better error handling here
407-
return showGenericErrorMessage('Unable to create branch');
408+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
408409
}
409410
}
410411
}
@@ -529,7 +530,9 @@ export class BranchGitCommand extends QuickCommand {
529530
} catch (ex) {
530531
Logger.error(ex);
531532
// TODO likely need some better error handling here
532-
return showGenericErrorMessage('Unable to delete branch');
533+
return showGenericErrorMessage(
534+
new BranchError(ex.reason, ex, state.references.map(r => r.name).join(', ')).message,
535+
);
533536
}
534537
}
535538
}
@@ -638,7 +641,7 @@ export class BranchGitCommand extends QuickCommand {
638641
} catch (ex) {
639642
Logger.error(ex);
640643
// TODO likely need some better error handling here
641-
return showGenericErrorMessage('Unable to rename branch');
644+
return showGenericErrorMessage(new BranchError(ex.reason, ex, state.name).message);
642645
}
643646
}
644647
}

src/env/node/git/git.ts

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

168+
const branchErrorAndReason = [
169+
[GitErrors.noRemoteReference, BranchErrorReason.NoRemoteReference],
170+
[GitErrors.invalidBranchName, BranchErrorReason.InvalidBranchName],
171+
[GitErrors.branchAlreadyExists, BranchErrorReason.BranchAlreadyExists],
172+
[GitErrors.branchNotFullyMerged, BranchErrorReason.BranchNotFullyMerged],
173+
[GitErrors.branchNotYetBorn, BranchErrorReason.BranchNotYetBorn],
174+
[GitErrors.branchFastForwardRejected, BranchErrorReason.BranchFastForwardRejected],
175+
];
176+
168177
export class Git {
169178
/** Map of running git commands -- avoids running duplicate overlaping commands */
170179
private readonly pendingCommands = new Map<string, Promise<string | Buffer>>();
@@ -517,29 +526,12 @@ export class Git {
517526
await this.git<string>({ cwd: repoPath }, 'branch', ...args);
518527
} catch (ex) {
519528
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;
529+
for (const [error, reason] of branchErrorAndReason) {
530+
if (error.test(msg) || error.test(ex.stderr ?? '')) {
531+
throw new BranchError(reason, ex);
532+
}
541533
}
542-
throw new BranchError(reason, ex);
534+
throw new BranchError(BranchErrorReason.Other, ex);
543535
}
544536
}
545537

@@ -1024,9 +1016,9 @@ export class Git {
10241016
} else {
10251017
params.push(options.remote, options.branch);
10261018
}
1027-
} else if (options.remote != null) {
1019+
} else if (options.remote) {
10281020
params.push(options.remote);
1029-
} else if (options.delete != null) {
1021+
} else if (options.delete) {
10301022
params.push('-d', options.delete.remote, ...options.delete.branches);
10311023
}
10321024

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)