diff options
author | HampusM <hampus@hampusmat.com> | 2021-08-05 15:18:39 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-08-05 15:18:39 +0200 |
commit | 22bf491be5f25bccf779be53b2ad37836f0850b8 (patch) | |
tree | 50cce93d9bb5d1f0cb8255335c8768aa36177104 /packages | |
parent | 9f7c9103ae6ca7c3127b44a966300a8ea62b3454 (diff) |
The log API endpoint has a branch parameter & fixed the tree entry latest commit method
Diffstat (limited to 'packages')
-rw-r--r-- | packages/server/src/git/repository.ts | 7 | ||||
-rw-r--r-- | packages/server/src/git/tree_entry.ts | 24 | ||||
-rw-r--r-- | packages/server/src/routes/api/v1/repo/index.ts | 18 |
3 files changed, 30 insertions, 19 deletions
diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts index ccc8bcc..411c4fb 100644 --- a/packages/server/src/git/repository.ts +++ b/packages/server/src/git/repository.ts @@ -7,7 +7,7 @@ import { Commit } from "./commit"; import { FastifyReply } from "fastify"; import { Tag } from "./tag"; import { Tree } from "./tree"; -import { BranchError, createError, RepositoryError } from "./error"; +import { createError, RepositoryError } from "./error"; /** * Returns the full name of a git repository @@ -86,7 +86,8 @@ export class Repository { */ public async commits(): Promise<Commit[]> { const walker = NodeGitRevwalk.create(this.ng_repository); - walker.pushHead(); + + walker.pushRef(`refs/heads/${this._branch}`); return Promise.all((await walker.getCommitsUntil(() => true)).map(commit => new Commit(this, commit))); } @@ -170,7 +171,7 @@ export class Repository { if(branch) { if(!await Branch.lookupExists(ng_repository, branch)) { - throw(createError(BranchError, 404, "Branch not found!")); + throw(createError(RepositoryError, 404, "Branch not found!")); } } diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts index 99941cb..9d694c5 100644 --- a/packages/server/src/git/tree_entry.ts +++ b/packages/server/src/git/tree_entry.ts @@ -1,10 +1,11 @@ import { Commit } from "./commit"; -import { TreeEntry as NodeGitTreeEntry } from "nodegit"; +import { Revwalk as NodeGitRevwalk, TreeEntry as NodeGitTreeEntry } from "nodegit"; import { Repository } from "./repository"; import { dirname } from "path"; import { findAsync } from "./misc"; import { Tree } from "./tree"; import { Blob } from "./blob"; +import { createError, TreeError } from "./error"; /** * The core structure of a tree entry @@ -32,16 +33,25 @@ export abstract class BaseTreeEntry { * @returns An instance of a commit */ public async latestCommit(): Promise<Commit> { - const commits = await this._owner.commits(); + const rev_walk = NodeGitRevwalk.create(this._owner.ng_repository); + rev_walk.pushRef(`refs/heads/${(await this._owner.branch()).name}`); - return findAsync(commits, async commit => { - const diff = await commit.diff(); - const patches = await diff.patches(); + const commits = await rev_walk.getCommitsUntil(() => true); + + const latest_commit = await findAsync(commits, async commit => { + const diff = await commit.getDiff(); + const patches = await diff[0].patches(); return Boolean(this instanceof TreeEntry - ? patches.find(patch => patch.to === this.path) - : patches.find(patch => dirname(patch.to).startsWith(this.path))); + ? patches.find(patch => dirname(patch.newFile().path()).startsWith(this.path)) + : patches.find(patch => patch.newFile().path() === this.path)); }); + + if(!latest_commit) { + throw(createError(TreeError, 500, `Failed to get the latest commit of tree entry '${this.path}'!`)); + } + + return new Commit(this._owner, latest_commit); } } diff --git a/packages/server/src/routes/api/v1/repo/index.ts b/packages/server/src/routes/api/v1/repo/index.ts index 1759a29..f5f9030 100644 --- a/packages/server/src/routes/api/v1/repo/index.ts +++ b/packages/server/src/routes/api/v1/repo/index.ts @@ -1,16 +1,14 @@ import { CoolFastifyRequest, Route } from "../../../../types/fastify"; import { FastifyInstance, FastifyPluginOptions } from "fastify"; -import { Blob } from "../../../../git/blob"; import { Repository } from "../../../../git/repository"; import { Tag } from "../../../../git/tag"; -import { BaseTreeEntry, TreeEntry } from "../../../../git/tree_entry"; +import { BaseTreeEntry, BlobTreeEntry, TreeEntry } from "../../../../git/tree_entry"; import { basename } from "path"; import branches from "./branches"; 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 { Tree } from "../../../../git/tree"; declare module "fastify" { interface FastifyRequest { @@ -68,28 +66,30 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do method: "GET", url: "/tree", handler: async(req, reply) => { - const tree: Tree | BaseError = await req.repository.tree().catch(err => err); + const tree = await req.repository.tree().catch((err: BaseError) => err); if(tree instanceof BaseError) { reply.code(tree.code).send({ error: tree.message }); return; } - const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null; + const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) + ? req.query.path + : null; let data: APITree; if(tree_path) { - const tree_path_entry: Tree | Blob | BaseError = await tree.find(tree_path).catch(err => err); + const tree_path_entry = await tree.find(tree_path).catch((err: BaseError) => err); if(tree_path_entry instanceof BaseError) { reply.code(tree_path_entry.code).send({ error: tree_path_entry.message }); return; } - data = tree_path_entry instanceof Blob - ? { type: "blob", content: await tree_path_entry.content() } - : { type: "tree", content: await Promise.all(tree_path_entry.entries().map(treeEntryMap)) }; + data = tree_path_entry instanceof BlobTreeEntry + ? { type: "blob", content: await (await tree_path_entry.blob()).content() } + : { type: "tree", content: await Promise.all((await (tree_path_entry as TreeEntry).tree()).entries().map(treeEntryMap)) }; } else { data = { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) }; |