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 +++++++++++++++++++++--- test/unit/tree_entry.unit.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) 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; + } } /** diff --git a/test/unit/tree_entry.unit.test.ts b/test/unit/tree_entry.unit.test.ts index 570ba0f..de270c5 100644 --- a/test/unit/tree_entry.unit.test.ts +++ b/test/unit/tree_entry.unit.test.ts @@ -26,6 +26,34 @@ describe("Tree entry", () => { expect(latest_commit).toBeDefined(); expect(latest_commit).toBeInstanceOf(Commit); }); + + it("Should get whole commit history", async() => { + expect.hasAssertions(); + + const history = await tree_entry.history(); + + expect(history).toBeDefined(); + expect(history.length).toBeGreaterThanOrEqual(1); + + for(const hist_entry of history) { + expect(hist_entry).toBeDefined(); + expect(hist_entry).toBeInstanceOf(Commit); + } + }); + + it("Should get 5 entries of the commit history", async() => { + expect.hasAssertions(); + + const history = await tree_entry.history(5); + + expect(history).toBeDefined(); + expect(history).toHaveLength(5); + + for(const hist_entry of history) { + expect(hist_entry).toBeDefined(); + expect(hist_entry).toBeInstanceOf(Commit); + } + }); }); }); -- cgit v1.2.3-18-g5258