aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/tree.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-06-24 22:50:38 +0200
committerHampusM <hampus@hampusmat.com>2021-06-24 22:50:38 +0200
commita13786d6cc185822f5940582efde2349ef729145 (patch)
tree7d4f49b50fc30ced65c5661b22b027456b79948e /packages/server/src/git/tree.ts
parent01e5d215dbc152e34ecd005111171457f87c235d (diff)
Refactored the backend yet again
Diffstat (limited to 'packages/server/src/git/tree.ts')
-rw-r--r--packages/server/src/git/tree.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/packages/server/src/git/tree.ts b/packages/server/src/git/tree.ts
new file mode 100644
index 0000000..b9bd142
--- /dev/null
+++ b/packages/server/src/git/tree.ts
@@ -0,0 +1,39 @@
+import { Blob } from "./blob";
+import { Tree as NodeGitTree } from "nodegit";
+import { Repository } from "./repository";
+import { TreeEntry } from "./tree_entry";
+
+export class Tree {
+ private _ng_tree: NodeGitTree;
+ private _owner: Repository;
+
+ constructor(owner: Repository, tree: NodeGitTree) {
+ this._ng_tree = tree;
+ this._owner = owner;
+ }
+
+ entries(): TreeEntry[] {
+ return this._ng_tree.entries().map(entry => new TreeEntry(this._owner, entry));
+ }
+
+ async find(path: string): Promise<Blob | Tree | null> {
+ const entry = await this._ng_tree.getEntry(path).catch(err => {
+ if(err.errno === -3) {
+ return null;
+ }
+ throw(err);
+ });
+
+ if(!entry) {
+ return null;
+ }
+
+ return entry.isBlob() ? new Blob(entry) : new Tree(this._owner, await entry.getTree());
+ }
+
+ findExists(path: string): Promise<boolean> {
+ return this._ng_tree.getEntry(path)
+ .then(() => true)
+ .catch(() => false);
+ }
+} \ No newline at end of file