aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/diff.ts
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 /packages/server/src/git/diff.ts
parentf6a5fe05c887a658c20257d2ef6bf2780f792873 (diff)
Improved a bunch of small things in the backend
Diffstat (limited to 'packages/server/src/git/diff.ts')
-rw-r--r--packages/server/src/git/diff.ts26
1 files changed, 21 insertions, 5 deletions
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