aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-08-07 19:16:01 +0200
committerHampusM <hampus@hampusmat.com>2021-08-07 19:16:01 +0200
commit17c20d77600407209908de5541415f2c4a85f5e3 (patch)
treed318292028004251b14f9fb87fa1a85838da906d
parentb2152da8f4bea1a655f7c9c7c07b24c041173f0a (diff)
Implemented tree entry commit history
-rw-r--r--packages/server/src/git/tree_entry.ts24
-rw-r--r--test/unit/tree_entry.unit.test.ts28
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<Commit[]> {
+ 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);
+ }
+ });
});
});