From 17c20d77600407209908de5541415f2c4a85f5e3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 7 Aug 2021 19:16:01 +0200 Subject: Implemented tree entry commit history --- packages/server/src/git/tree_entry.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'packages/server/src/git') diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts index 22de716..182543e 100644 --- a/packages/server/src/git/tree_entry.ts +++ b/packages/server/src/git/tree_entry.ts @@ -1,11 +1,8 @@ import { Commit } from "./commit"; 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 @@ -43,6 +40,27 @@ export abstract class BaseTreeEntry { return new Commit(this._owner, file_hist[0].commit); } + + /** + * Returns the tree entry's commit history + * + * @returns An array of commit instances + */ + public async history(count?: number): Promise { + const rev_walk = NodeGitRevwalk.create(this._owner.ng_repository); + rev_walk.pushRef(`refs/heads/${this._owner.branch_name}`); + + const commit_cnt = (await rev_walk.getCommitsUntil(() => true)).length; + + rev_walk.pushRef(`refs/heads/${this._owner.branch_name}`); + const file_hist = await rev_walk.fileHistoryWalk(this.path, commit_cnt); + + const commit_history = file_hist.map(hist_entry => new Commit(this._owner, hist_entry.commit)); + + return count + ? commit_history.slice(0, count) + : commit_history; + } } /** -- cgit v1.2.3-18-g5258