From 8d42b6f44ca5b0eeb54938e1cf9509d1f8294f00 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 29 Jul 2021 15:39:31 +0200 Subject: Refactored & improved the backend yet again --- packages/server/src/git/tree.ts | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'packages/server/src/git/tree.ts') diff --git a/packages/server/src/git/tree.ts b/packages/server/src/git/tree.ts index 6c08e1a..2d3888e 100644 --- a/packages/server/src/git/tree.ts +++ b/packages/server/src/git/tree.ts @@ -1,7 +1,8 @@ import { Tree as NodeGitTree } from "nodegit"; import { Repository } from "./repository"; -import { BaseTreeEntry, createTreeEntry, TreeEntry } from "./tree_entry"; +import { BaseTreeEntry, BlobTreeEntry, createTreeEntry, TreeEntry } from "./tree_entry"; import { createError, TreeError } from "./error"; +import { pack, Pack } from "tar-stream"; /** * A representation of a git tree @@ -27,8 +28,8 @@ export class Tree { * * @returns An array of tree entry instances */ - public entries(): TreeEntry[] { - return this._ng_tree.entries().map(entry => new TreeEntry(this._owner, entry)); + public entries(): BaseTreeEntry[] { + return this._ng_tree.entries().map(entry => createTreeEntry(this._owner, entry)); } /** @@ -40,7 +41,7 @@ export class Tree { public async find(path: string): Promise { const entry = await this._ng_tree.getEntry(path).catch(err => { if(err.errno === -3) { - throw(createError(TreeError, 404, "Path not found")); + throw(createError(TreeError, 404, `Path '${path}' not found`)); } throw(createError(TreeError, 500, "Failed to get tree path")); }); @@ -60,6 +61,33 @@ export class Tree { .catch(() => false); } + /** + * Returns an archive made from the tree + * + * @returns An instance of a tar pack + */ + async createArchive(): Promise { + const archive = pack(); + const repository = this._owner.name.short; + + async function addEntries(tree: Tree) { + for(const tree_entry of tree.entries()) { + if(tree_entry instanceof BlobTreeEntry) { + archive.entry({ name: `${repository}/${tree_entry.path}` }, (await (await tree_entry.blob()).content())); + } + else if(tree_entry instanceof TreeEntry) { + await addEntries(await tree_entry.tree()); + } + } + } + + await addEntries(this); + + archive.finalize(); + + return archive; + } + /** * Returns the tree of a repository * -- cgit v1.2.3-18-g5258