From df341dc9b874f7e7bf99d9c1de3a84c49c4a04a6 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 25 Jun 2021 13:21:56 +0200 Subject: Cleaned up the backend --- packages/server/src/api/v1/repo/log.ts | 8 +++--- packages/server/src/git/blob.ts | 2 +- packages/server/src/git/branch.ts | 4 +-- packages/server/src/git/commit.ts | 12 ++++---- packages/server/src/git/diff.ts | 41 +++++++++------------------ packages/server/src/git/patch.ts | 51 +++++++++++++++++++++------------- packages/server/src/git/reference.ts | 4 +-- packages/server/src/git/repository.ts | 20 ++++++------- packages/server/src/git/tag.ts | 16 ++++++----- packages/server/src/git/tree.ts | 6 ++-- packages/server/src/git/tree_entry.ts | 6 ++-- 11 files changed, 85 insertions(+), 85 deletions(-) (limited to 'packages/server') 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 { + public async content(): Promise { 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 { + public async latestCommit(): Promise { 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 { + public static async lookup(owner: Repository, branch: string): Promise { 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 { - return Diff.get((await this._ng_commit.getDiff())[0]); + public async diff(): Promise { + return new Diff((await this._ng_commit.getDiff())[0]); } - async stats(): Promise { + public async stats(): Promise { const stats = await (await this._ng_commit.getDiff())[0].getStats(); return { @@ -52,16 +52,16 @@ export class Commit { }; } - async tree(): Promise { + public async tree(): Promise { return new Tree(this._owner, await this._ng_commit.getTree()); } - static async lookup(repository: Repository, id: string | NodeGitOid): Promise { + public static async lookup(repository: Repository, id: string | NodeGitOid): Promise { 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 { + public static lookupExists(repository: Repository, id: string): Promise { 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 { + 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 { + 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; }, { indexes: [], lengths: [], last: null }); - - this.patch_header_indexes = patch_header_data.indexes; - this.patch_header_lengths = patch_header_data.lengths; - - } - - async getPatches(): Promise { - return (await this._ng_diff.patches()).map((patch, index) => new Patch(this, patch, index)); } - static async get(diff: NodeGitDiff): Promise { - return new Diff(diff, { - patch_buf: String((await diff.toBuf(1))), - patch_header_buf: String((await diff.toBuf(2))) - }); + public async patches(): Promise { + 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 { + 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 { + 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 { - if(!this.content) { - return null; + public async isTooLarge(index: number): Promise { + 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 { + 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 { + public async commits(): Promise { 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 { + public async tree(): Promise { const master_commit = await this._ng_repository.getMasterCommit(); const tree = await master_commit.getTree(); return new Tree(this, tree); } - lookupExists(id: string): Promise { + public lookupExists(id: string): Promise { return NodeGitObject.lookup(this._ng_repository, NodeGitOid.fromString(id), NodeGitObject.TYPE.ANY) .then(() => true) .catch(() => false); } - async branches(): Promise { + public async branches(): Promise { const references = await this._ng_repository.getReferences(); return references.filter(ref => ref.isBranch()).map(branch => new Branch(this, branch)); } - async tags(): Promise { + public async tags(): Promise { const references = await this._ng_repository.getReferences(); return references.filter(ref => ref.isTag()).map(tag => new Tag(this, tag)); } - async latestCommit(): Promise { + public async latestCommit(): Promise { 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 { + public static async open(base_dir: string, repository: string): Promise { 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 { + public static async openAll(base_dir: string): Promise { 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 { + public async author(): Promise { 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 { + public async date(): Promise { return (await NodeGitTag.lookup(this._owner.nodegitRepository, this._ng_reference.target())).tagger().when() .time(); } - async downloadTarball(reply: FastifyReply): Promise { + public async downloadTarball(reply: FastifyReply): Promise { 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 { + public static async lookup(owner: Repository, tag: string): Promise { 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 { + public async find(path: string): Promise { 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 { + public findExists(path: string): Promise { 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 { + public async latestCommit(): Promise { 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 { + public async peel(): Promise { 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 -- cgit v1.2.3-18-g5258