aboutsummaryrefslogtreecommitdiff
path: root/packages
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-06-25 13:21:56 +0200
committerHampusM <hampus@hampusmat.com>2021-06-25 13:21:56 +0200
commitdf341dc9b874f7e7bf99d9c1de3a84c49c4a04a6 (patch)
treefda4bcd92b9119bf4b95d393849d2f643eae8444 /packages
parenta13786d6cc185822f5940582efde2349ef729145 (diff)
Cleaned up the backend
Diffstat (limited to 'packages')
-rw-r--r--packages/server/src/api/v1/repo/log.ts8
-rw-r--r--packages/server/src/git/blob.ts2
-rw-r--r--packages/server/src/git/branch.ts4
-rw-r--r--packages/server/src/git/commit.ts12
-rw-r--r--packages/server/src/git/diff.ts41
-rw-r--r--packages/server/src/git/patch.ts51
-rw-r--r--packages/server/src/git/reference.ts4
-rw-r--r--packages/server/src/git/repository.ts20
-rw-r--r--packages/server/src/git/tag.ts16
-rw-r--r--packages/server/src/git/tree.ts6
-rw-r--r--packages/server/src/git/tree_entry.ts6
11 files changed, 85 insertions, 85 deletions
diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts
index d21cfa8..aa6fa1a 100644
--- a/packages/server/src/api/v1/repo/log.ts
+++ b/packages/server/src/api/v1/repo/log.ts
@@ -20,14 +20,14 @@ async function commitMap(commit: Commit) {
};
}
-async function patchMap(patch: Patch) {
+async function patchMap(patch: Patch, index: number) {
return {
additions: patch.additions,
deletions: patch.deletions,
from: patch.from,
to: patch.to,
- too_large: patch.too_large,
- hunks: await patch.getHunks()
+ too_large: await patch.isTooLarge(index),
+ hunks: await patch.getHunks(index)
};
}
@@ -68,7 +68,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
insertions: stats.insertions,
deletions: stats.deletions,
files_changed: stats.files_changed,
- diff: await Promise.all((await (await commit.diff()).getPatches()).map(patchMap))
+ diff: await Promise.all((await (await commit.diff()).patches()).map(patchMap))
}
});
}
diff --git a/packages/server/src/git/blob.ts b/packages/server/src/git/blob.ts
index aa3f9ad..aeb1381 100644
--- a/packages/server/src/git/blob.ts
+++ b/packages/server/src/git/blob.ts
@@ -7,7 +7,7 @@ export class Blob {
this._ng_tree_entry = entry;
}
- async content(): Promise<string> {
+ public async content(): Promise<string> {
return this._ng_tree_entry.isBlob() ? (await this._ng_tree_entry.getBlob()).toString() : "";
}
} \ No newline at end of file
diff --git a/packages/server/src/git/branch.ts b/packages/server/src/git/branch.ts
index 2142724..90241a0 100644
--- a/packages/server/src/git/branch.ts
+++ b/packages/server/src/git/branch.ts
@@ -3,7 +3,7 @@ import { Reference } from "./reference";
import { Repository } from "./repository";
export class Branch extends Reference {
- async latestCommit(): Promise<CommitSummary> {
+ public async latestCommit(): Promise<CommitSummary> {
const latest_commit = this._owner.nodegitRepository.getBranchCommit(this._ng_reference);
return {
id: (await latest_commit).sha(),
@@ -12,7 +12,7 @@ export class Branch extends Reference {
};
}
- static async lookup(owner: Repository, branch: string): Promise<Branch | null> {
+ public static async lookup(owner: Repository, branch: string): Promise<Branch | null> {
const reference = await owner.nodegitRepository.getBranch(branch).catch(err => {
if(err.errno === -3) {
return null;
diff --git a/packages/server/src/git/commit.ts b/packages/server/src/git/commit.ts
index 64bae4d..5d86eb4 100644
--- a/packages/server/src/git/commit.ts
+++ b/packages/server/src/git/commit.ts
@@ -38,11 +38,11 @@ export class Commit {
this.message = commit.message();
}
- async diff(): Promise<Diff> {
- return Diff.get((await this._ng_commit.getDiff())[0]);
+ public async diff(): Promise<Diff> {
+ return new Diff((await this._ng_commit.getDiff())[0]);
}
- async stats(): Promise<DiffStats> {
+ public async stats(): Promise<DiffStats> {
const stats = await (await this._ng_commit.getDiff())[0].getStats();
return {
@@ -52,16 +52,16 @@ export class Commit {
};
}
- async tree(): Promise<Tree> {
+ public async tree(): Promise<Tree> {
return new Tree(this._owner, await this._ng_commit.getTree());
}
- static async lookup(repository: Repository, id: string | NodeGitOid): Promise<Commit> {
+ public static async lookup(repository: Repository, id: string | NodeGitOid): Promise<Commit> {
const commit = await NodeGitCommit.lookup(repository.nodegitRepository, id instanceof NodeGitOid ? id : NodeGitOid.fromString(id));
return new Commit(repository, commit);
}
- static lookupExists(repository: Repository, id: string): Promise<boolean> {
+ public static lookupExists(repository: Repository, id: string): Promise<boolean> {
return NodeGitCommit.lookup(repository.nodegitRepository, NodeGitOid.fromString(id))
.then(() => true)
.catch(() => false);
diff --git a/packages/server/src/git/diff.ts b/packages/server/src/git/diff.ts
index a3ea375..e0e112d 100644
--- a/packages/server/src/git/diff.ts
+++ b/packages/server/src/git/diff.ts
@@ -7,29 +7,25 @@ type PatchHeaderData = {
last: number | null
}
-type DiffConstructorData = {
- patch_buf: string,
- patch_header_buf: string
-}
-
export class Diff {
private _ng_diff: NodeGitDiff;
- public raw_patches: string;
- public patch_header_indexes: number[];
- public patch_header_lengths: number[];
-
- constructor(diff: NodeGitDiff, data: DiffConstructorData) {
+ constructor(diff: NodeGitDiff) {
this._ng_diff = diff;
- this.raw_patches = data.patch_buf;
+ }
+
+ public async rawPatches(): Promise<string> {
+ return String(await this._ng_diff.toBuf(1));
+ }
- const raw_patches_arr = this.raw_patches.split("\n");
- const patch_headers = data.patch_header_buf.split("\n");
+ public async patchHeaderData(): Promise<PatchHeaderData> {
+ const raw_patches = await this.rawPatches();
+ const patch_headers = String(await this._ng_diff.toBuf(2)).split("\n");
- const patch_header_data = patch_headers.reduce((result, line, index) => {
+ return patch_headers.reduce((result, line, index) => {
// The start of a patch header
if((/^diff --git/u).test(line)) {
- result.indexes.push(raw_patches_arr.indexOf(line));
+ result.indexes.push(raw_patches.indexOf(line));
if(result.last !== null) {
result.lengths.push(patch_headers.slice(result.last, index).length);
@@ -44,20 +40,9 @@ export class Diff {
return result;
}, <PatchHeaderData>{ indexes: [], lengths: [], last: null });
-
- this.patch_header_indexes = patch_header_data.indexes;
- this.patch_header_lengths = patch_header_data.lengths;
-
- }
-
- async getPatches(): Promise<Patch[]> {
- return (await this._ng_diff.patches()).map((patch, index) => new Patch(this, patch, index));
}
- static async get(diff: NodeGitDiff): Promise<Diff> {
- return new Diff(diff, {
- patch_buf: String((await diff.toBuf(1))),
- patch_header_buf: String((await diff.toBuf(2)))
- });
+ public async patches(): Promise<Patch[]> {
+ return (await this._ng_diff.patches()).map(patch => new Patch(this, patch));
}
} \ No newline at end of file
diff --git a/packages/server/src/git/patch.ts b/packages/server/src/git/patch.ts
index 71d9193..93ef6fa 100644
--- a/packages/server/src/git/patch.ts
+++ b/packages/server/src/git/patch.ts
@@ -16,6 +16,11 @@ type Hunks = {
hunks: Hunk[]
}
+type PatchBounds = {
+ start: number,
+ end: number
+}
+
function getHunkContent(hunk: string[]) {
interface Lines {
new_lines: number[],
@@ -39,45 +44,53 @@ function getHunkContent(hunk: string[]) {
export class Patch {
private _ng_patch: NodeGitPatch;
+ private _diff: Diff;
public from: string;
public to: string;
public additions: number;
public deletions: number;
- public too_large = false;
- public content: string | null = null;
- constructor(diff: Diff, patch: NodeGitPatch, index: number) {
+ constructor(diff: Diff, patch: NodeGitPatch) {
this._ng_patch = patch;
+ this._diff = diff;
this.from = patch.oldFile().path();
this.to = patch.newFile().path();
this.additions = patch.lineStats()["total_additions"];
this.deletions = patch.lineStats()["total_deletions"];
+ }
- const raw_patches_arr = diff.raw_patches.split("\n");
- const start = diff.patch_header_indexes[index] + diff.patch_header_lengths[index];
- const end = (typeof diff.patch_header_indexes[index + 1] === "undefined") ? raw_patches_arr.length - 1 : diff.patch_header_indexes[index + 1];
-
- const patch_content = raw_patches_arr.slice(start, end);
+ private async bounds(index: number): Promise<PatchBounds> {
+ const raw_patches = await (await this._diff.rawPatches()).split("\n");
+ const patch_header_data = await this._diff.patchHeaderData();
- if(patch_content.length !== 0) {
- this.content = patch_content.join("\n");
+ return {
+ start: patch_header_data.indexes[index] + patch_header_data.lengths[index],
+ end: (typeof patch_header_data.indexes[index + 1] === "undefined") ? raw_patches.length - 1 : patch_header_data.indexes[index + 1]
+ };
+ }
- const line_lengths = patch_content.map(line => line.length).reduce((result, length) => result + length);
+ private async content(index: number): Promise<string> {
+ const raw_patches = await (await this._diff.rawPatches()).split("\n");
+ const bounds = await this.bounds(index);
- if(patch_content.length > 5000 || line_lengths > 5000) {
- this.too_large = true;
- }
- }
+ return raw_patches.slice(bounds.start, bounds.end).join("\n");
}
- async getHunks(): Promise<Hunk[] | null> {
- if(!this.content) {
- return null;
+ public async isTooLarge(index: number): Promise<boolean> {
+ const content = (await this.content(index)).split("\n");
+ const line_lengths = content.map(line => line.length).reduce((result, length) => result + length);
+
+ if(content.length > 5000 || line_lengths > 5000) {
+ return true;
}
- const content = this.content.split("\n");
+ return false;
+ }
+
+ public async getHunks(index: number): Promise<Hunk[] | null> {
+ const content = (await this.content(index)).split("\n");
const hunks = await this._ng_patch.hunks();
const hunks_data = hunks.reduce((result: Hunks, hunk, hunk_index) => {
diff --git a/packages/server/src/git/reference.ts b/packages/server/src/git/reference.ts
index 910fa7d..36f1312 100644
--- a/packages/server/src/git/reference.ts
+++ b/packages/server/src/git/reference.ts
@@ -5,8 +5,8 @@ export abstract class Reference {
protected _ng_reference: NodeGitReference;
protected _owner: Repository;
- id: string;
- name: string;
+ public id: string;
+ public name: string;
constructor(owner: Repository, reference: NodeGitReference) {
this._ng_reference = reference;
diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts
index ac0927a..ff23761 100644
--- a/packages/server/src/git/repository.ts
+++ b/packages/server/src/git/repository.ts
@@ -41,48 +41,48 @@ export class Repository {
this.owner = data.owner;
}
- async commits(): Promise<Commit[]> {
+ public async commits(): Promise<Commit[]> {
const walker = NodeGitRevwalk.create(this._ng_repository);
walker.pushHead();
return Promise.all((await walker.getCommitsUntil(() => true)).map(commit => new Commit(this, commit)));
}
- async tree(): Promise<Tree> {
+ public async tree(): Promise<Tree> {
const master_commit = await this._ng_repository.getMasterCommit();
const tree = await master_commit.getTree();
return new Tree(this, tree);
}
- lookupExists(id: string): Promise<boolean> {
+ public lookupExists(id: string): Promise<boolean> {
return NodeGitObject.lookup(this._ng_repository, NodeGitOid.fromString(id), NodeGitObject.TYPE.ANY)
.then(() => true)
.catch(() => false);
}
- async branches(): Promise<Branch[]> {
+ public async branches(): Promise<Branch[]> {
const references = await this._ng_repository.getReferences();
return references.filter(ref => ref.isBranch()).map(branch => new Branch(this, branch));
}
- async tags(): Promise<Tag[]> {
+ public async tags(): Promise<Tag[]> {
const references = await this._ng_repository.getReferences();
return references.filter(ref => ref.isTag()).map(tag => new Tag(this, tag));
}
- async latestCommit(): Promise<Commit> {
+ public async latestCommit(): Promise<Commit> {
return new Commit(this, await this._ng_repository.getMasterCommit());
}
- HTTPconnect(req: Request, reply: FastifyReply): void {
+ public HTTPconnect(req: Request, reply: FastifyReply): void {
connect(this, req, reply);
}
- get nodegitRepository(): NodeGitRepository {
+ public get nodegitRepository(): NodeGitRepository {
return this._ng_repository;
}
- static async open(base_dir: string, repository: string): Promise<Repository> {
+ public static async open(base_dir: string, repository: string): Promise<Repository> {
const ng_repository = await NodeGitRepository.openBare(`${base_dir}/${getFullRepositoryName(repository)}`);
return new Repository(ng_repository, {
@@ -91,7 +91,7 @@ export class Repository {
});
}
- static async openAll(base_dir: string): Promise<Repository[] | null> {
+ public static async openAll(base_dir: string): Promise<Repository[] | null> {
const dir_content = await getDirectory(base_dir);
if(dir_content.length === 0) {
diff --git a/packages/server/src/git/tag.ts b/packages/server/src/git/tag.ts
index ffda9c4..64828e7 100644
--- a/packages/server/src/git/tag.ts
+++ b/packages/server/src/git/tag.ts
@@ -21,13 +21,13 @@ async function addArchiveEntries(entries: TreeEntry[], repository: string, archi
}
}
else if(peeled instanceof Tree) {
- addArchiveEntries(peeled.entries(), repository, archive);
+ await addArchiveEntries(peeled.entries(), repository, archive);
}
}
}
export class Tag extends Reference {
- async author(): Promise<Author> {
+ public async author(): Promise<Author> {
const tagger = (await NodeGitTag.lookup(this._owner.nodegitRepository, this._ng_reference.target())).tagger();
return {
name: tagger.name(),
@@ -35,12 +35,12 @@ export class Tag extends Reference {
};
}
- async date(): Promise<number> {
+ public async date(): Promise<number> {
return (await NodeGitTag.lookup(this._owner.nodegitRepository, this._ng_reference.target())).tagger().when()
.time();
}
- async downloadTarball(reply: FastifyReply): Promise<void> {
+ public async downloadTarball(reply: FastifyReply): Promise<void> {
const commit = await Commit.lookup(this._owner, (await this._ng_reference.peel(NodeGitObject.TYPE.COMMIT)).id());
const tree = await commit.tree();
@@ -59,15 +59,17 @@ export class Tag extends Reference {
gzip.on("error", () => reply.raw.end());
archive.on("error", () => reply.raw.end());
- addArchiveEntries(await tree.entries(), this._owner.name.short, archive)
- .then(() => archive.finalize())
+ addArchiveEntries(tree.entries(), this._owner.name.short, archive)
+ .then(() => {
+ archive.finalize();
+ })
.catch(() => {
archive.finalize();
reply.raw.end();
});
}
- static async lookup(owner: Repository, tag: string): Promise<Tag | null> {
+ public static async lookup(owner: Repository, tag: string): Promise<Tag | null> {
const reference = await owner.nodegitRepository.getReference(tag).catch(err => {
if(err.errno === -3) {
return null;
diff --git a/packages/server/src/git/tree.ts b/packages/server/src/git/tree.ts
index b9bd142..8697010 100644
--- a/packages/server/src/git/tree.ts
+++ b/packages/server/src/git/tree.ts
@@ -12,11 +12,11 @@ export class Tree {
this._owner = owner;
}
- entries(): TreeEntry[] {
+ public entries(): TreeEntry[] {
return this._ng_tree.entries().map(entry => new TreeEntry(this._owner, entry));
}
- async find(path: string): Promise<Blob | Tree | null> {
+ public async find(path: string): Promise<Blob | Tree | null> {
const entry = await this._ng_tree.getEntry(path).catch(err => {
if(err.errno === -3) {
return null;
@@ -31,7 +31,7 @@ export class Tree {
return entry.isBlob() ? new Blob(entry) : new Tree(this._owner, await entry.getTree());
}
- findExists(path: string): Promise<boolean> {
+ public findExists(path: string): Promise<boolean> {
return this._ng_tree.getEntry(path)
.then(() => true)
.catch(() => false);
diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts
index 3bcf10e..d4eac40 100644
--- a/packages/server/src/git/tree_entry.ts
+++ b/packages/server/src/git/tree_entry.ts
@@ -21,12 +21,12 @@ export class TreeEntry {
this.type = entry.isBlob() ? "blob" : "tree";
}
- async latestCommit(): Promise<Commit> {
+ public async latestCommit(): Promise<Commit> {
const commits = await this._owner.commits();
return findAsync(commits, async commit => {
const diff = await commit.diff();
- const patches = await diff.getPatches();
+ const patches = await diff.patches();
return Boolean(this.type === "blob"
? patches.find(patch => patch.to === this.path)
@@ -34,7 +34,7 @@ export class TreeEntry {
});
}
- async peel(): Promise<Blob | Tree> {
+ public async peel(): Promise<Blob | Tree> {
return this.type === "blob" ? new Blob(this._ng_tree_entry) : new Tree(this._owner, await this._ng_tree_entry.getTree());
}
} \ No newline at end of file