aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/patch.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-08-18 17:29:55 +0200
committerHampusM <hampus@hampusmat.com>2021-08-18 17:29:55 +0200
commitd1a1b7dc947063aef5f8375a6a1e03246b272c84 (patch)
treef5cb9bd6d4b5463d9d022026ac6fea87cb6ebe02 /packages/server/src/git/patch.ts
parent6ed078de30a7bf35deace728857d1d293d59eb15 (diff)
Implemented caching for certain API endpoints, Added documentation & made backend-fixes
Diffstat (limited to 'packages/server/src/git/patch.ts')
-rw-r--r--packages/server/src/git/patch.ts18
1 files changed, 13 insertions, 5 deletions
diff --git a/packages/server/src/git/patch.ts b/packages/server/src/git/patch.ts
index 4239ce4..4527d03 100644
--- a/packages/server/src/git/patch.ts
+++ b/packages/server/src/git/patch.ts
@@ -1,5 +1,6 @@
import { Diff } from "./diff";
import { ConvenientPatch as NodeGitPatch } from "nodegit";
+import { createError, ErrorWhere, PatchTooLargeError } from "./error";
type Hunk = {
new_start: number,
@@ -87,10 +88,9 @@ export class Patch {
*
* These bounds are in the context of it's whole diff
*
- * @returns A patch bounds instance which contains a start & an end property
+ * @returns The patch's bounds
*/
- private async _bounds(): Promise<PatchBounds> {
- const raw_patches = (await this._diff.rawPatches()).split("\n");
+ private async _bounds(raw_patches: string[]): Promise<PatchBounds> {
const patch_header_data = await this._diff.patchHeaderData();
return {
@@ -104,7 +104,7 @@ export class Patch {
*/
private async _content(): Promise<string> {
const raw_patches = (await this._diff.rawPatches()).split("\n");
- const bounds = await this._bounds();
+ const bounds = await this._bounds(raw_patches);
return raw_patches.slice(bounds.start, bounds.end).join("\n");
}
@@ -115,10 +115,14 @@ export class Patch {
* @returns Whether or not the patch is too large
*/
public async isTooLarge(): Promise<boolean> {
+ if(this.additions > 2000 || this.deletions > 2000) {
+ return true;
+ }
+
const content = (await this._content()).split("\n");
const line_lengths = content.map(line => line.length).reduce((result, length) => result + length);
- if(content.length > 5000 || line_lengths > 5000) {
+ if(content.length > 10000 || line_lengths > 10000) {
return true;
}
@@ -131,6 +135,10 @@ export class Patch {
* @returns An array of hunk instances
*/
public async getHunks(): Promise<Hunk[]> {
+ if(await this.isTooLarge()) {
+ throw(createError(ErrorWhere.Patch, PatchTooLargeError));
+ }
+
const content = (await this._content()).split("\n");
const hunks = await this._ng_patch.hunks();