aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/tree.ts
blob: b9bd1426be79f962918783774b1c786ab7315168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
	}
}