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/branch.ts | 4 ++-- packages/server/src/git/diff.ts | 26 +++++++++++++++++++++----- packages/server/src/git/error.ts | 6 ++++++ packages/server/src/git/patch.ts | 21 +++++++++++++++++++++ packages/server/src/git/tag.ts | 2 +- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/packages/server/src/git/branch.ts b/packages/server/src/git/branch.ts index 4737279..1dbb690 100644 --- a/packages/server/src/git/branch.ts +++ b/packages/server/src/git/branch.ts @@ -31,7 +31,7 @@ export class Branch extends Reference { * Lookup a branch * * @param owner - The repository which the branch is in - * @param branch - The SHA of a branch + * @param branch - The name of a branch * @returns An instance of a branch */ public static async lookup(owner: Repository, branch: string): Promise { @@ -48,7 +48,7 @@ export class Branch extends Reference { * Returns if a branch exists or not * * @param owner - The repository which the branch is in - * @param branch - The SHA of a branch + * @param branch - The name of a branch * @returns Whether or not the branch exists */ public static async lookupExists(owner: NodeGitRepository, branch: string): Promise { 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 diff --git a/packages/server/src/git/error.ts b/packages/server/src/git/error.ts index fff16a4..6715d8f 100644 --- a/packages/server/src/git/error.ts +++ b/packages/server/src/git/error.ts @@ -48,6 +48,12 @@ export class MiscError extends BaseError { } } +export class DiffError extends BaseError { + constructor(code: number, message: string) { + super(code, "A diff error has occured: " + message); + } +} + type ErrorConstructorType = new (code: number, message: string) => T; /** diff --git a/packages/server/src/git/patch.ts b/packages/server/src/git/patch.ts index 8eaaaf1..45ffe23 100644 --- a/packages/server/src/git/patch.ts +++ b/packages/server/src/git/patch.ts @@ -168,4 +168,25 @@ export class Patch { return hunks_data.hunks; } + + /** + * Returns a patch from a diff + * + * @param diff - The diff which the patch is in + * @param index - The index of a patch + * @returns An instance of a patch + */ + public static async fromDiff(diff: Diff, index: number): Promise { + return diff.patch(index); + } + + /** + * Returns all of the patches from a diff + * + * @param diff - A diff + * @returns An array of patch instances + */ + public static async allFromDiff(diff: Diff): Promise { + return diff.patches(); + } } \ No newline at end of file diff --git a/packages/server/src/git/tag.ts b/packages/server/src/git/tag.ts index fadb47c..b01ac98 100644 --- a/packages/server/src/git/tag.ts +++ b/packages/server/src/git/tag.ts @@ -96,7 +96,7 @@ export class Tag extends Reference { * Lookup a tag * * @param owner - The repository which the tag is in - * @param tag - The SHA of the tag to look for + * @param tag - The name of the tag to look for * @returns An instance of a tag */ public static async lookup(owner: Repository, tag: string): Promise { -- cgit v1.2.3-18-g5258