aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-27 16:49:53 +0200
committerHampusM <hampus@hampusmat.com>2021-07-27 16:49:53 +0200
commitfa44d81b658024685e0496150d66a51f2f5fda8c (patch)
tree38ed498ee8d070245432fec7c8ca2483254a3893
parentf6a5fe05c887a658c20257d2ef6bf2780f792873 (diff)
Improved a bunch of small things in the backend
-rw-r--r--packages/server/src/git/branch.ts4
-rw-r--r--packages/server/src/git/diff.ts26
-rw-r--r--packages/server/src/git/error.ts6
-rw-r--r--packages/server/src/git/patch.ts21
-rw-r--r--packages/server/src/git/tag.ts2
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<Branch> {
@@ -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<boolean> {
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<string> {
- 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<Omit<PatchHeaderData, "last">> {
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<Patch[]> {
- 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<Patch> {
+ 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<T> = 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<Patch> {
+ 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<Patch[]> {
+ 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<Tag> {