From fa44d81b658024685e0496150d66a51f2f5fda8c Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 27 Jul 2021 16:49:53 +0200 Subject: Improved a bunch of small things in the backend --- packages/server/src/git/diff.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'packages/server/src/git/diff.ts') diff --git a/packages/server/src/git/diff.ts b/packages/server/src/git/diff.ts index d3b788c..3d69183 100644 --- a/packages/server/src/git/diff.ts +++ b/packages/server/src/git/diff.ts @@ -1,4 +1,5 @@ import { Diff as NodeGitDiff } from "nodegit"; +import { createError, DiffError } from "./error"; import { Patch } from "./patch"; type PatchHeaderData = { @@ -11,20 +12,20 @@ type PatchHeaderData = { * A representation of a diff */ export class Diff { - private _ng_diff: NodeGitDiff; + public ng_diff: NodeGitDiff; /** * @param diff A Nodegit diff */ constructor(diff: NodeGitDiff) { - this._ng_diff = diff; + this.ng_diff = diff; } /** * Returns all of the diff's patches in a raw format */ public async rawPatches(): Promise { - return String(await this._ng_diff.toBuf(1)); + return String(await this.ng_diff.toBuf(1)); } /** @@ -34,7 +35,7 @@ export class Diff { */ public async patchHeaderData(): Promise> { const raw_patches = (await this.rawPatches()).split("\n"); - const patch_headers = String(await this._ng_diff.toBuf(2)).split("\n"); + const patch_headers = String(await this.ng_diff.toBuf(2)).split("\n"); return patch_headers.reduce((result, line, index) => { // The start of a patch header @@ -62,6 +63,21 @@ export class Diff { * @returns An array of patch instances */ public async patches(): Promise { - return (await this._ng_diff.patches()).map((patch, index) => new Patch(this, patch, index)); + return (await this.ng_diff.patches()).map((patch, index) => new Patch(this, patch, index)); + } + + /** + * Returns a patch from the diff + * + * @returns An instance of a patch + */ + public async patch(index: number): Promise { + const patch = (await this.ng_diff.patches())[index]; + + if(!patch) { + throw(createError(DiffError, 500, "Patch not found")); + } + + return new Patch(this, (await this.ng_diff.patches())[index], index); } } \ No newline at end of file -- cgit v1.2.3-18-g5258