aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/branch.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/server/src/git/branch.ts')
-rw-r--r--packages/server/src/git/branch.ts21
1 files changed, 16 insertions, 5 deletions
diff --git a/packages/server/src/git/branch.ts b/packages/server/src/git/branch.ts
index 90241a0..163eca7 100644
--- a/packages/server/src/git/branch.ts
+++ b/packages/server/src/git/branch.ts
@@ -1,10 +1,15 @@
import { CommitSummary } from "./commit";
import { Reference } from "./reference";
import { Repository } from "./repository";
+import { Repository as NodeGitRepository } from "nodegit";
+import { BranchError, createError } from "./error";
export class Branch extends Reference {
public async latestCommit(): Promise<CommitSummary> {
- const latest_commit = this._owner.nodegitRepository.getBranchCommit(this._ng_reference);
+ const latest_commit = this._owner.nodegitRepository.getBranchCommit(this._ng_reference).catch(() => {
+ throw(createError(BranchError, 500, "Failed to get the latest commit"));
+ });
+
return {
id: (await latest_commit).sha(),
message: (await latest_commit).message(),
@@ -12,13 +17,19 @@ export class Branch extends Reference {
};
}
- public static async lookup(owner: Repository, branch: string): Promise<Branch | null> {
+ public static async lookup(owner: Repository, branch: string): Promise<Branch> {
const reference = await owner.nodegitRepository.getBranch(branch).catch(err => {
if(err.errno === -3) {
- return null;
+ throw(createError(BranchError, 404, "Branch not found!"));
}
- throw(err);
+ throw(createError(BranchError, 500, "Something went wrong."));
});
- return reference ? new Branch(owner, reference) : null;
+ return new Branch(owner, reference);
+ }
+
+ public static async lookupExists(ng_repository: NodeGitRepository, branch: string): Promise<boolean> {
+ return ng_repository.getBranch(branch)
+ .then(() => true)
+ .catch(() => false);
}
} \ No newline at end of file