diff options
| author | HampusM <hampus@hampusmat.com> | 2021-06-24 22:50:38 +0200 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2021-06-24 22:50:38 +0200 | 
| commit | a13786d6cc185822f5940582efde2349ef729145 (patch) | |
| tree | 7d4f49b50fc30ced65c5661b22b027456b79948e /packages/server/src/git/tree_entry.ts | |
| parent | 01e5d215dbc152e34ecd005111171457f87c235d (diff) | |
Refactored the backend yet again
Diffstat (limited to 'packages/server/src/git/tree_entry.ts')
| -rw-r--r-- | packages/server/src/git/tree_entry.ts | 40 | 
1 files changed, 40 insertions, 0 deletions
diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts new file mode 100644 index 0000000..3bcf10e --- /dev/null +++ b/packages/server/src/git/tree_entry.ts @@ -0,0 +1,40 @@ +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"; + +export class TreeEntry { +	private _ng_tree_entry: NodeGitTreeEntry; +	private _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"; +	} + +	async latestCommit(): Promise<Commit> { +		const commits = await this._owner.commits(); + +		return findAsync(commits, async commit => { +			const diff = await commit.diff(); +			const patches = await diff.getPatches(); + +			return Boolean(this.type === "blob" +				? patches.find(patch => patch.to === this.path) +				: patches.find(patch => dirname(patch.to).startsWith(this.path))); +		}); +	} + +	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()); +	} +}
\ No newline at end of file  | 
