aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-27 13:18:42 +0200
committerHampusM <hampus@hampusmat.com>2021-07-27 13:18:42 +0200
commitf6a5fe05c887a658c20257d2ef6bf2780f792873 (patch)
tree9ee4b42ed04b3fe7decdac10b20ec96ca764d378 /packages/server/src
parent69c58c94bd8cc0d469ae301931d4571aca84851a (diff)
Refactored the backend a bit & added TSDoc/JSDoc comments to the diff class
Diffstat (limited to 'packages/server/src')
-rw-r--r--packages/server/src/git/blob.ts9
-rw-r--r--packages/server/src/git/branch.ts4
-rw-r--r--packages/server/src/git/commit.ts6
-rw-r--r--packages/server/src/git/diff.ts21
-rw-r--r--packages/server/src/git/patch.ts4
-rw-r--r--packages/server/src/git/reference.ts15
-rw-r--r--packages/server/src/git/repository.ts29
-rw-r--r--packages/server/src/git/tag.ts26
-rw-r--r--packages/server/src/git/tree_entry.ts15
9 files changed, 70 insertions, 59 deletions
diff --git a/packages/server/src/git/blob.ts b/packages/server/src/git/blob.ts
index eebd2d4..4dadc01 100644
--- a/packages/server/src/git/blob.ts
+++ b/packages/server/src/git/blob.ts
@@ -12,19 +12,18 @@ export class Blob {
/**
* @param entry - A tree entry that's a blob
- * @param path - A path
*/
- constructor(entry: BlobTreeEntry, path: string) {
+ constructor(entry: BlobTreeEntry) {
this._tree_entry = entry;
- this.path = path;
+ this.path = entry.path;
}
/**
* Returns the blob's content
*/
public async content(): Promise<string> {
- return this._tree_entry.content();
+ return (await this._tree_entry.ng_tree_entry.getBlob()).content.toString();
}
/**
@@ -41,6 +40,6 @@ export class Blob {
throw(createError(BlobError, 500, "Not a blob"));
}
- return new Blob(entry, path);
+ return new Blob(entry);
}
} \ No newline at end of file
diff --git a/packages/server/src/git/branch.ts b/packages/server/src/git/branch.ts
index 7ddf672..4737279 100644
--- a/packages/server/src/git/branch.ts
+++ b/packages/server/src/git/branch.ts
@@ -16,7 +16,7 @@ export class Branch extends Reference {
* @returns A commit summary instance
*/
public async latestCommit(): Promise<CommitSummary> {
- const latest_commit = this._owner.nodegitRepository.getBranchCommit(this._ng_reference).catch(() => {
+ const latest_commit = this._owner.ng_repository.getBranchCommit(this._ng_reference).catch(() => {
throw(createError(BranchError, 500, "Failed to get the latest commit"));
});
@@ -35,7 +35,7 @@ export class Branch extends Reference {
* @returns An instance of a branch
*/
public static async lookup(owner: Repository, branch: string): Promise<Branch> {
- const reference = await owner.nodegitRepository.getBranch(branch).catch(err => {
+ const reference = await owner.ng_repository.getBranch(branch).catch(err => {
if(err.errno === -3) {
throw(createError(BranchError, 404, "Branch not found!"));
}
diff --git a/packages/server/src/git/commit.ts b/packages/server/src/git/commit.ts
index c49e5ec..8cf4282 100644
--- a/packages/server/src/git/commit.ts
+++ b/packages/server/src/git/commit.ts
@@ -86,7 +86,7 @@ export class Commit {
* @returns An instance of a 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));
+ const commit = await NodeGitCommit.lookup(repository.ng_repository, id instanceof NodeGitOid ? id : NodeGitOid.fromString(id));
return new Commit(repository, commit);
}
@@ -98,7 +98,7 @@ export class Commit {
* @returns Whether or not the commit exists
*/
public static lookupExists(repository: Repository, id: string): Promise<boolean> {
- return NodeGitCommit.lookup(repository.nodegitRepository, NodeGitOid.fromString(id))
+ return NodeGitCommit.lookup(repository.ng_repository, NodeGitOid.fromString(id))
.then(() => true)
.catch(() => false);
}
@@ -110,6 +110,6 @@ export class Commit {
* @returns An instance of a commit
*/
public static async masterCommit(owner: Repository): Promise<Commit> {
- return new Commit(owner, await owner.nodegitRepository.getMasterCommit());
+ return new Commit(owner, await owner.ng_repository.getMasterCommit());
}
} \ No newline at end of file
diff --git a/packages/server/src/git/diff.ts b/packages/server/src/git/diff.ts
index 2e88609..d3b788c 100644
--- a/packages/server/src/git/diff.ts
+++ b/packages/server/src/git/diff.ts
@@ -7,18 +7,32 @@ type PatchHeaderData = {
last: number | null
}
+/**
+ * A representation of a diff
+ */
export class Diff {
private _ng_diff: NodeGitDiff;
+ /**
+ * @param diff A Nodegit diff
+ */
constructor(diff: NodeGitDiff) {
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));
}
- public async patchHeaderData(): Promise<PatchHeaderData> {
+ /**
+ * Returns information about patch headers
+ *
+ * @returns The indexes and lengths of patch headers
+ */
+ 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");
@@ -42,6 +56,11 @@ export class Diff {
}, <PatchHeaderData>{ indexes: [], lengths: [], last: null });
}
+ /**
+ * Returns the diff's patches
+ *
+ * @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));
}
diff --git a/packages/server/src/git/patch.ts b/packages/server/src/git/patch.ts
index 7b695ff..8eaaaf1 100644
--- a/packages/server/src/git/patch.ts
+++ b/packages/server/src/git/patch.ts
@@ -22,8 +22,8 @@ type PatchBounds = {
}
interface HunkLines {
- new_lines: number[],
- deleted_lines: number[]
+ new_lines: number[],
+ deleted_lines: number[]
}
interface ProcessedHunk extends HunkLines {
diff --git a/packages/server/src/git/reference.ts b/packages/server/src/git/reference.ts
index 4f4f5a0..4e28e05 100644
--- a/packages/server/src/git/reference.ts
+++ b/packages/server/src/git/reference.ts
@@ -1,16 +1,6 @@
import { Reference as NodeGitReference } from "nodegit";
import { Repository } from "./repository";
-type ReferenceClass<T> = new(owner: Repository, reference: NodeGitReference) => T;
-
-export function isNodeGitReferenceBranch(ref: NodeGitReference): boolean {
- return Boolean(ref.isBranch());
-}
-
-export function isNodeGitReferenceTag(ref: NodeGitReference): boolean {
- return Boolean(ref.isTag());
-}
-
/**
* A representation of a reference
*/
@@ -32,9 +22,4 @@ export abstract class Reference {
this.id = reference.target().tostrS();
this.name = reference.shorthand();
}
-
- public static async all<T>(owner: Repository, Target: ReferenceClass<T>, ref_fn: (ref: NodeGitReference) => boolean): Promise<T[]> {
- const references = await owner.nodegitRepository.getReferences();
- return references.filter(ref_fn).map(ref => new Target(owner, ref));
- }
} \ No newline at end of file
diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts
index a67d6f7..4250af0 100644
--- a/packages/server/src/git/repository.ts
+++ b/packages/server/src/git/repository.ts
@@ -8,7 +8,6 @@ import { FastifyReply } from "fastify";
import { Tag } from "./tag";
import { Tree } from "./tree";
import { BranchError, createError, RepositoryError } from "./error";
-import { isNodeGitReferenceBranch, isNodeGitReferenceTag, Reference } from "./reference";
/**
* Returns the full name of a git repository
@@ -25,8 +24,8 @@ type RepositoryName = {
full: string,
}
-// This fucking shit isn't in the Nodegit documentation.
-interface WeirdNodeGitError extends Error {
+// I have no idea what this sort of error is from
+interface WeirdError extends Error {
errno: number;
errorFunction: string;
}
@@ -35,10 +34,10 @@ interface WeirdNodeGitError extends Error {
* A representation of an bare git repository
*/
export class Repository {
- private _ng_repository: NodeGitRepository;
-
private _branch: string;
+ public ng_repository: NodeGitRepository;
+
public name: RepositoryName;
public base_dir: string;
@@ -47,7 +46,7 @@ export class Repository {
* @param branch - The branch to use
*/
constructor(repository: NodeGitRepository, branch: string) {
- this._ng_repository = repository;
+ this.ng_repository = repository;
this.name = {
short: basename(repository.path()).slice(0, -4),
full: basename(repository.path())
@@ -86,7 +85,7 @@ export class Repository {
* @returns An array of commit instances
*/
public async commits(): Promise<Commit[]> {
- const walker = NodeGitRevwalk.create(this._ng_repository);
+ const walker = NodeGitRevwalk.create(this.ng_repository);
walker.pushHead();
return Promise.all((await walker.getCommitsUntil(() => true)).map(commit => new Commit(this, commit)));
@@ -108,7 +107,7 @@ export class Repository {
* @returns Whether or not it exists
*/
public lookupExists(id: string): Promise<boolean> {
- return NodeGitObject.lookup(this._ng_repository, NodeGitOid.fromString(id), NodeGitObject.TYPE.ANY)
+ return NodeGitObject.lookup(this.ng_repository, NodeGitOid.fromString(id), NodeGitObject.TYPE.ANY)
.then(() => true)
.catch(() => false);
}
@@ -118,8 +117,9 @@ export class Repository {
*
* @returns An array of branch instances
*/
- public branches(): Promise<Branch[]> {
- return Reference.all(this, Branch, isNodeGitReferenceBranch);
+ public async branches(): Promise<Branch[]> {
+ const references = await this.ng_repository.getReferences();
+ return references.filter(ref => ref.isBranch()).map(ref => new Branch(this, ref));
}
/**
@@ -128,7 +128,8 @@ export class Repository {
* @returns An array of tag instances
*/
public async tags(): Promise<Tag[]> {
- return Reference.all(this, Tag, isNodeGitReferenceTag);
+ const references = await this.ng_repository.getReferences();
+ return references.filter(ref => ref.isTag()).map(ref => new Tag(this, ref));
}
/**
@@ -150,10 +151,6 @@ export class Repository {
connect(this, req, reply);
}
- public get nodegitRepository(): NodeGitRepository {
- return this._ng_repository;
- }
-
/**
* Opens a bare git repository
*
@@ -163,7 +160,7 @@ export class Repository {
* @returns An instance of a git repository
*/
public static async open(base_dir: string, repository: string, branch?: string): Promise<Repository> {
- let ng_repository = await NodeGitRepository.openBare(`${base_dir}/${getFullRepositoryName(repository)}`).catch((err: WeirdNodeGitError) => {
+ let ng_repository = await NodeGitRepository.openBare(`${base_dir}/${getFullRepositoryName(repository)}`).catch((err: WeirdError) => {
if(err.errno === -3) {
throw(createError(RepositoryError, 404, "Repository not found"));
}
diff --git a/packages/server/src/git/tag.ts b/packages/server/src/git/tag.ts
index 31dfb41..fadb47c 100644
--- a/packages/server/src/git/tag.ts
+++ b/packages/server/src/git/tag.ts
@@ -4,19 +4,27 @@ import { Commit } from "./commit";
import { FastifyReply } from "fastify";
import { Reference } from "./reference";
import { Repository } from "./repository";
-import { BaseTreeEntry, BlobTreeEntry, TreeEntry } from "./tree_entry";
+import { BlobTreeEntry, TreeEntry } from "./tree_entry";
import { createGzip } from "zlib";
import { pipeline } from "stream";
import { createError, TagError } from "./error";
import { Author } from "../../../shared_types/src";
+import { Tree } from "./tree";
-async function addArchiveEntries(entries: BaseTreeEntry[], repository: string, archive: Pack) {
- for(const tree_entry of entries) {
+/**
+ * Recursively go through a repository and add it's content to a archive
+ *
+ * @param tree - A tree to add archive entries from
+ * @param repository - The repository which the tree is in
+ * @param archive - A tar archive pack
+ */
+async function addArchiveEntries(tree: Tree, repository: string, archive: Pack) {
+ for(const tree_entry of tree.entries()) {
if(tree_entry instanceof BlobTreeEntry) {
- archive.entry({ name: `${repository}/${tree_entry.path}` }, await tree_entry.content());
+ archive.entry({ name: `${repository}/${tree_entry.path}` }, (await (await tree_entry.blob()).content()));
}
else if(tree_entry instanceof TreeEntry) {
- await addArchiveEntries((await tree_entry.tree()).entries(), repository, archive);
+ await addArchiveEntries((await tree_entry.tree()), repository, archive);
}
}
}
@@ -33,7 +41,7 @@ export class Tag extends Reference {
* @returns An instance of an author
*/
public async author(): Promise<Author> {
- const tagger = (await NodeGitTag.lookup(this._owner.nodegitRepository, this._ng_reference.target())).tagger();
+ const tagger = (await NodeGitTag.lookup(this._owner.ng_repository, this._ng_reference.target())).tagger();
return {
name: tagger.name(),
email: tagger.email()
@@ -46,7 +54,7 @@ export class Tag extends Reference {
* @returns A Unix Epoch timestamp for the tag's date
*/
public async date(): Promise<number> {
- return (await NodeGitTag.lookup(this._owner.nodegitRepository, this._ng_reference.target())).tagger().when()
+ return (await NodeGitTag.lookup(this._owner.ng_repository, this._ng_reference.target())).tagger().when()
.time();
}
@@ -74,7 +82,7 @@ export class Tag extends Reference {
gzip.on("error", () => reply.raw.end());
archive.on("error", () => reply.raw.end());
- addArchiveEntries(tree.entries(), this._owner.name.short, archive)
+ addArchiveEntries(tree, this._owner.name.short, archive)
.then(() => {
archive.finalize();
})
@@ -92,7 +100,7 @@ export class Tag extends Reference {
* @returns An instance of a tag
*/
public static async lookup(owner: Repository, tag: string): Promise<Tag> {
- const reference = await owner.nodegitRepository.getReference(tag).catch(err => {
+ const reference = await owner.ng_repository.getReference(tag).catch(err => {
if(err.errno === -3) {
throw(createError(TagError, 404, "Tag not found"));
}
diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts
index 52f9988..99941cb 100644
--- a/packages/server/src/git/tree_entry.ts
+++ b/packages/server/src/git/tree_entry.ts
@@ -4,14 +4,15 @@ import { Repository } from "./repository";
import { dirname } from "path";
import { findAsync } from "./misc";
import { Tree } from "./tree";
+import { Blob } from "./blob";
/**
* The core structure of a tree entry
*/
export abstract class BaseTreeEntry {
- protected _ng_tree_entry: NodeGitTreeEntry;
protected _owner: Repository;
+ public ng_tree_entry: NodeGitTreeEntry;
public path: string;
/**
@@ -19,7 +20,7 @@ export abstract class BaseTreeEntry {
* @param entry - An instance of a Nodegit tree entry
*/
constructor(owner: Repository, entry: NodeGitTreeEntry) {
- this._ng_tree_entry = entry;
+ this.ng_tree_entry = entry;
this._owner = owner;
this.path = entry.path();
@@ -54,7 +55,7 @@ export class TreeEntry extends BaseTreeEntry {
* @returns An instance of a tree
*/
public async tree(): Promise<Tree> {
- return new Tree(this._owner, await this._ng_tree_entry.getTree());
+ return new Tree(this._owner, await this.ng_tree_entry.getTree());
}
}
@@ -63,10 +64,12 @@ export class TreeEntry extends BaseTreeEntry {
*/
export class BlobTreeEntry extends BaseTreeEntry {
/**
- * Returns the blob's content
+ * Returns the blob of the blob tree entry
+ *
+ * @returns An instance of a blob
*/
- public async content(): Promise<string> {
- return (await this._ng_tree_entry.getBlob()).toString();
+ public async blob(): Promise<Blob> {
+ return new Blob(this);
}
}