aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/tree.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-29 15:39:31 +0200
committerHampusM <hampus@hampusmat.com>2021-07-29 15:59:52 +0200
commit8d42b6f44ca5b0eeb54938e1cf9509d1f8294f00 (patch)
treeb3bae9c6160bb11436a7f2875f27191780156353 /packages/server/src/git/tree.ts
parent4f47dbfeb6edd984b2e912f00993db6e4eb2193d (diff)
Refactored & improved the backend yet again
Diffstat (limited to 'packages/server/src/git/tree.ts')
-rw-r--r--packages/server/src/git/tree.ts36
1 files changed, 32 insertions, 4 deletions
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<BaseTreeEntry> {
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"));
});
@@ -61,6 +62,33 @@ export class Tree {
}
/**
+ * Returns an archive made from the tree
+ *
+ * @returns An instance of a tar pack
+ */
+ async createArchive(): Promise<Pack> {
+ 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
*
* @remarks