From a13786d6cc185822f5940582efde2349ef729145 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 24 Jun 2021 22:50:38 +0200 Subject: Refactored the backend yet again --- packages/server/src/git/commit.ts | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 packages/server/src/git/commit.ts (limited to 'packages/server/src/git/commit.ts') diff --git a/packages/server/src/git/commit.ts b/packages/server/src/git/commit.ts new file mode 100644 index 0000000..64bae4d --- /dev/null +++ b/packages/server/src/git/commit.ts @@ -0,0 +1,69 @@ +import { Commit as NodeGitCommit, Oid as NodeGitOid } from "nodegit"; +import { Author } from "./misc"; +import { Diff } from "./diff"; +import { Repository } from "./repository"; +import { Tree } from "./tree"; + +export type CommitSummary = { + id: string | null, + message: string | null, + date: number | null +} + +type DiffStats = { + insertions: number, + deletions: number, + files_changed: number +} + +export class Commit { + private _ng_commit: NodeGitCommit; + private _owner: Repository; + + public id: string; + public author: Author; + public date: number; + public message: string; + + constructor(owner: Repository, commit: NodeGitCommit) { + this._ng_commit = commit; + this._owner = owner; + + this.id = commit.sha(); + this.author = { + name: commit.author().name(), + email: commit.author().email() + }; + this.date = commit.time(); + this.message = commit.message(); + } + + async diff(): Promise { + return Diff.get((await this._ng_commit.getDiff())[0]); + } + + async stats(): Promise { + const stats = await (await this._ng_commit.getDiff())[0].getStats(); + + return { + insertions: stats.insertions(), + deletions: stats.deletions(), + files_changed: stats.filesChanged() + }; + } + + async tree(): Promise { + return new Tree(this._owner, await this._ng_commit.getTree()); + } + + static async lookup(repository: Repository, id: string | NodeGitOid): Promise { + const commit = await NodeGitCommit.lookup(repository.nodegitRepository, id instanceof NodeGitOid ? id : NodeGitOid.fromString(id)); + return new Commit(repository, commit); + } + + static lookupExists(repository: Repository, id: string): Promise { + return NodeGitCommit.lookup(repository.nodegitRepository, NodeGitOid.fromString(id)) + .then(() => true) + .catch(() => false); + } +} \ No newline at end of file -- cgit v1.2.3-18-g5258