diff options
author | HampusM <hampus@hampusmat.com> | 2021-07-26 17:33:54 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-07-26 17:33:54 +0200 |
commit | a737bc4695f99b05a3c4807c88fc30b1a1eb89b5 (patch) | |
tree | 4ca8f9955a80a09a508c55f86daeb2aba2ae64ac /packages/server/src/git/tree_entry.ts | |
parent | 35151a0283141ddac96f8cc71b59cd1030e31f3d (diff) |
Refactored the git tree, tree entry & blob classes
Diffstat (limited to 'packages/server/src/git/tree_entry.ts')
-rw-r--r-- | packages/server/src/git/tree_entry.ts | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts index d4eac40..e4780c8 100644 --- a/packages/server/src/git/tree_entry.ts +++ b/packages/server/src/git/tree_entry.ts @@ -1,24 +1,21 @@ -import { Blob } from "./blob"; import { Commit } from "./commit"; import { TreeEntry as NodeGitTreeEntry } from "nodegit"; import { Repository } from "./repository"; -import { Tree } from "./tree"; import { dirname } from "path"; import { findAsync } from "./misc"; +import { Tree } from "./tree"; -export class TreeEntry { - private _ng_tree_entry: NodeGitTreeEntry; - private _owner: Repository; +export abstract class BaseTreeEntry { + protected _ng_tree_entry: NodeGitTreeEntry; + protected _owner: Repository; public path: string; - public type: "blob" | "tree"; constructor(owner: Repository, entry: NodeGitTreeEntry) { this._ng_tree_entry = entry; this._owner = owner; this.path = entry.path(); - this.type = entry.isBlob() ? "blob" : "tree"; } public async latestCommit(): Promise<Commit> { @@ -28,13 +25,27 @@ export class TreeEntry { const diff = await commit.diff(); const patches = await diff.patches(); - return Boolean(this.type === "blob" + return Boolean(this instanceof TreeEntry ? patches.find(patch => patch.to === this.path) : patches.find(patch => dirname(patch.to).startsWith(this.path))); }); } +} - public async peel(): Promise<Blob | Tree> { - return this.type === "blob" ? new Blob(this._ng_tree_entry) : new Tree(this._owner, await this._ng_tree_entry.getTree()); +export class TreeEntry extends BaseTreeEntry { + public async tree(): Promise<Tree> { + return new Tree(this._owner, await this._ng_tree_entry.getTree()); } +} + +export class BlobTreeEntry extends BaseTreeEntry { + public async content(): Promise<string> { + return (await this._ng_tree_entry.getBlob()).toString(); + } +} + +export function createTreeEntry(owner: Repository, entry: NodeGitTreeEntry): BaseTreeEntry { + return entry.isBlob() + ? new BlobTreeEntry(owner, entry) + : new TreeEntry(owner, entry); }
\ No newline at end of file |