From f2d55a9f05f5df7960d7f6dd37d1ef96c762d65a Mon Sep 17 00:00:00 2001
From: HampusM <hampus@hampusmat.com>
Date: Sun, 8 Aug 2021 15:33:22 +0200
Subject: Added a tree commit history API endpoint

---
 packages/server/src/routes/api/v1/repo/index.ts | 34 ++++++++++++++++++++++++-
 packages/server/src/routes/api/v1/repo/log.ts   | 21 +++------------
 packages/server/src/routes/api/v1/repo/map.ts   | 18 +++++++++++++
 3 files changed, 54 insertions(+), 19 deletions(-)
 create mode 100644 packages/server/src/routes/api/v1/repo/map.ts

(limited to 'packages/server/src/routes/api')

diff --git a/packages/server/src/routes/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts
index f5f9030..3b115d4 100644
--- a/packages/server/src/routes/api/v1/repo/index.ts
+++ b/packages/server/src/routes/api/v1/repo/index.ts
@@ -9,6 +9,7 @@ import log from "./log";
 import { verifyRepoName } from "../../util";
 import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "api";
 import { BaseError } from "../../../../git/error";
+import { commitMap } from "./map";
 
 declare module "fastify" {
 	interface FastifyRequest {
@@ -99,6 +100,37 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
 		}
 	});
 
+	fastify.route<Route>({
+		method: "GET",
+		url: "/tree/history",
+		handler: async(req, reply) => {
+			const tree = await req.repository.tree().catch((err: BaseError) => err);
+
+			if(tree instanceof BaseError) {
+				reply.code(tree.code).send({ error: tree.message });
+				return;
+			}
+
+			if(Object.keys(req.query).length === 0) {
+				reply.code(400).send({ error: "Missing query parameter 'path'!" });
+				return;
+			}
+
+			const tree_path = req.query.path;
+
+			const tree_entry = await tree.find(tree_path).catch((err: BaseError) => err);
+
+			if(tree_entry instanceof BaseError) {
+				reply.code(tree_entry.code).send({ error: tree_entry.message });
+				return;
+			}
+
+			const history = await tree_entry.history();
+
+			reply.send({ data: await Promise.all(history.map(commitMap)) });
+		}
+	});
+
 	fastify.route<Route>({
 		method: "GET",
 		url: "/tags",
@@ -111,4 +143,4 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
 	});
 
 	done();
-}
\ No newline at end of file
+}
diff --git a/packages/server/src/routes/api/v1/repo/log.ts b/packages/server/src/routes/api/v1/repo/log.ts
index 76706b3..b2eea42 100644
--- a/packages/server/src/routes/api/v1/repo/log.ts
+++ b/packages/server/src/routes/api/v1/repo/log.ts
@@ -3,23 +3,8 @@ import { Commit } from "../../../../git/commit";
 import { Patch } from "../../../../git/patch";
 import { Route } from "../../../../types/fastify";
 import { verifySHA } from "../../util";
-import { LogCommit as APILogCommit, Patch as APIPatch, Commit as APICommit } from "api";
-
-async function commitMap(commit: Commit) {
-	const stats = await commit.stats();
-	return <APILogCommit>{
-		id: commit.id,
-		author: {
-			name: commit.author.name,
-			email: commit.author.email
-		},
-		message: commit.message,
-		date: commit.date,
-		insertions: stats.insertions,
-		deletions: stats.deletions,
-		files_changed: stats.files_changed
-	};
-}
+import { Patch as APIPatch, Commit as APICommit } from "api";
+import { commitMap } from "./map";
 
 async function patchMap(patch: Patch) {
 	return <APIPatch>{
@@ -78,4 +63,4 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
 	});
 
 	done();
-}
\ No newline at end of file
+}
diff --git a/packages/server/src/routes/api/v1/repo/map.ts b/packages/server/src/routes/api/v1/repo/map.ts
new file mode 100644
index 0000000..520161e
--- /dev/null
+++ b/packages/server/src/routes/api/v1/repo/map.ts
@@ -0,0 +1,18 @@
+import { Commit } from "../../../../git/commit";
+import { LogCommit } from "api";
+
+export async function commitMap(commit: Commit): Promise<LogCommit> {
+	const stats = await commit.stats();
+	return <LogCommit>{
+		id: commit.id,
+		author: {
+			name: commit.author.name,
+			email: commit.author.email
+		},
+		message: commit.message,
+		date: commit.date,
+		insertions: stats.insertions,
+		deletions: stats.deletions,
+		files_changed: stats.files_changed
+	};
+}
-- 
cgit v1.2.3-18-g5258