From a737bc4695f99b05a3c4807c88fc30b1a1eb89b5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 26 Jul 2021 17:33:54 +0200 Subject: Refactored the git tree, tree entry & blob classes --- packages/server/src/git/blob.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'packages/server/src/git/blob.ts') diff --git a/packages/server/src/git/blob.ts b/packages/server/src/git/blob.ts index e44a979..ef9f9ab 100644 --- a/packages/server/src/git/blob.ts +++ b/packages/server/src/git/blob.ts @@ -1,17 +1,29 @@ -import { TreeEntry as NodeGitTreeEntry } from "nodegit"; import { BlobError, createError } from "./error"; +import { Tree } from "./tree"; +import { BlobTreeEntry } from "./tree_entry"; export class Blob { - private _ng_tree_entry: NodeGitTreeEntry; + private _tree_entry: BlobTreeEntry; - constructor(entry: NodeGitTreeEntry) { - this._ng_tree_entry = entry; + public path; + + constructor(entry: BlobTreeEntry, path: string) { + this._tree_entry = entry; + + this.path = path; } public async content(): Promise { - if(!this._ng_tree_entry.isBlob()) { + return this._tree_entry.content(); + } + + public static async fromPath(tree: Tree, path: string): Promise { + const entry = await tree.find(path); + + if(!(entry instanceof BlobTreeEntry)) { throw(createError(BlobError, 500, "Not a blob")); } - return this._ng_tree_entry.isBlob() ? (await this._ng_tree_entry.getBlob()).toString() : ""; + + return new Blob(entry, path); } } \ No newline at end of file -- cgit v1.2.3-18-g5258