aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-08-05 17:03:36 +0200
committerHampusM <hampus@hampusmat.com>2021-08-05 17:03:36 +0200
commit8c6a59cd766fead4998c957d86a0e33fd58d1cde (patch)
treeff2cd43a4a5dafb27348fc320784c39f1fe9c941
parent22bf491be5f25bccf779be53b2ad37836f0850b8 (diff)
The tree API has a branch query param & made repository branch less hardcoded
-rw-r--r--docs_src/API/v1/repos.md9
-rw-r--r--packages/server/src/git/commit.ts6
-rw-r--r--packages/server/src/git/repository.ts27
-rw-r--r--packages/server/src/git/tree.ts10
-rw-r--r--packages/server/src/git/tree_entry.ts2
-rw-r--r--packages/server/src/routes/api/v1/index.ts2
-rw-r--r--test/unit/commit.unit.test.ts2
-rw-r--r--test/unit/diff.unit.test.ts2
-rw-r--r--test/unit/repository.unit.test.ts4
9 files changed, 30 insertions, 34 deletions
diff --git a/docs_src/API/v1/repos.md b/docs_src/API/v1/repos.md
index 2df611f..9ddfc12 100644
--- a/docs_src/API/v1/repos.md
+++ b/docs_src/API/v1/repos.md
@@ -45,10 +45,11 @@ Retrieves a repository tree.
**Parameters:**<br>
-| Name | Location | Description | Required | Schema |
-|------|----------|------------------|----------|--------|
-| repo | path | The repository | true | string |
-| path | query | Path in the tree | false | string |
+| Name | Location | Description | Required | Schema |
+|--------|----------|------------------|----------|--------|
+| repo | path | The repository | true | string |
+| path | query | Path in the tree | false | string |
+| branch | query | A branch | false | string |
**Response:**<br>
diff --git a/packages/server/src/git/commit.ts b/packages/server/src/git/commit.ts
index e0b3bbb..cf1ac5a 100644
--- a/packages/server/src/git/commit.ts
+++ b/packages/server/src/git/commit.ts
@@ -104,12 +104,12 @@ export class Commit {
}
/**
- * Returns the master commit of a repository
+ * Returns the most recent commit of the repository's branch
*
* @param owner - A repository
* @returns An instance of a commit
*/
- public static async masterCommit(owner: Repository): Promise<Commit> {
- return new Commit(owner, await owner.ng_repository.getMasterCommit());
+ public static async branchCommit(owner: Repository): Promise<Commit> {
+ return new Commit(owner, await owner.ng_repository.getBranchCommit(owner.branch_name));
}
} \ No newline at end of file
diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts
index 411c4fb..87c6d3a 100644
--- a/packages/server/src/git/repository.ts
+++ b/packages/server/src/git/repository.ts
@@ -34,12 +34,11 @@ interface WeirdError extends Error {
* A representation of an bare git repository
*/
export class Repository {
- private _branch: string;
-
public ng_repository: NodeGitRepository;
public name: RepositoryName;
public base_dir: string;
+ public branch_name: string;
/**
* @param repository - An instance of a Nodegit repository
@@ -53,7 +52,7 @@ export class Repository {
};
this.base_dir = dirname(repository.path());
- this._branch = branch;
+ this.branch_name = branch;
}
/**
@@ -76,7 +75,7 @@ export class Repository {
* @returns An instance of a branch
*/
public branch(): Promise<Branch> {
- return Branch.lookup(this, this._branch);
+ return Branch.lookup(this, this.branch_name);
}
/**
@@ -87,12 +86,21 @@ export class Repository {
public async commits(): Promise<Commit[]> {
const walker = NodeGitRevwalk.create(this.ng_repository);
- walker.pushRef(`refs/heads/${this._branch}`);
+ walker.pushRef(`refs/heads/${this.branch_name}`);
return Promise.all((await walker.getCommitsUntil(() => true)).map(commit => new Commit(this, commit)));
}
/**
+ * Returns the repository's head commit
+ *
+ * @returns An instance of a commit
+ */
+ public async head(): Promise<Commit> {
+ return Commit.branchCommit(this);
+ }
+
+ /**
* Returns the repository's tree
*
* @returns An instance of a tree
@@ -134,15 +142,6 @@ export class Repository {
}
/**
- * Returns the repository's master commit
- *
- * @returns An instance of a commit
- */
- public async masterCommit(): Promise<Commit> {
- return Commit.masterCommit(this);
- }
-
- /**
* Connect to the Git HTTP backend
*
* @param req - A Fastify request
diff --git a/packages/server/src/git/tree.ts b/packages/server/src/git/tree.ts
index 2d3888e..7936e5d 100644
--- a/packages/server/src/git/tree.ts
+++ b/packages/server/src/git/tree.ts
@@ -3,6 +3,7 @@ import { Repository } from "./repository";
import { BaseTreeEntry, BlobTreeEntry, createTreeEntry, TreeEntry } from "./tree_entry";
import { createError, TreeError } from "./error";
import { pack, Pack } from "tar-stream";
+import { Commit } from "./commit";
/**
* A representation of a git tree
@@ -91,16 +92,11 @@ export class Tree {
/**
* Returns the tree of a repository
*
- * @remarks
- *
- * Assumes that you want to use the master branch.
- * This will be fixed in the future.
- *
* @param owner The repository which the tree is in
* @returns An instance of a tree
*/
public static async ofRepository(owner: Repository): Promise<Tree> {
- const master_commit = await owner.masterCommit();
- return master_commit.tree();
+ const commit = await Commit.branchCommit(owner);
+ return commit.tree();
}
} \ No newline at end of file
diff --git a/packages/server/src/git/tree_entry.ts b/packages/server/src/git/tree_entry.ts
index 9d694c5..84f36dd 100644
--- a/packages/server/src/git/tree_entry.ts
+++ b/packages/server/src/git/tree_entry.ts
@@ -34,7 +34,7 @@ export abstract class BaseTreeEntry {
*/
public async latestCommit(): Promise<Commit> {
const rev_walk = NodeGitRevwalk.create(this._owner.ng_repository);
- rev_walk.pushRef(`refs/heads/${(await this._owner.branch()).name}`);
+ rev_walk.pushRef(`refs/heads/${this._owner.branch_name}`);
const commits = await rev_walk.getCommitsUntil(() => true);
diff --git a/packages/server/src/routes/api/v1/index.ts b/packages/server/src/routes/api/v1/index.ts
index 9cc20c2..9c35d53 100644
--- a/packages/server/src/routes/api/v1/index.ts
+++ b/packages/server/src/routes/api/v1/index.ts
@@ -33,7 +33,7 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do
return <APIRepositorySummary>{
name: repository.name.short,
description: await repository.description(),
- last_updated: (await repository.masterCommit()).date
+ last_updated: (await repository.head()).date
};
}))
});
diff --git a/test/unit/commit.unit.test.ts b/test/unit/commit.unit.test.ts
index abbdb9a..137c64c 100644
--- a/test/unit/commit.unit.test.ts
+++ b/test/unit/commit.unit.test.ts
@@ -48,7 +48,7 @@ describe("Commit", () => {
let commit: Commit;
beforeAll(async() => {
- commit = await repository.masterCommit();
+ commit = await repository.head();
});
it("Should get the stats", async() => {
diff --git a/test/unit/diff.unit.test.ts b/test/unit/diff.unit.test.ts
index 5d30ec5..ed4061b 100644
--- a/test/unit/diff.unit.test.ts
+++ b/test/unit/diff.unit.test.ts
@@ -11,7 +11,7 @@ describe("Diff", () => {
beforeAll(async() => {
const repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO);
- diff = await (await repository.masterCommit()).diff();
+ diff = await (await repository.head()).diff();
});
describe("Instance methods", () => {
diff --git a/test/unit/repository.unit.test.ts b/test/unit/repository.unit.test.ts
index dd0d09b..66e32d4 100644
--- a/test/unit/repository.unit.test.ts
+++ b/test/unit/repository.unit.test.ts
@@ -104,10 +104,10 @@ describe("Repository", () => {
await expect(repository.lookupExists(env.UNAVAIL_OBJECT)).resolves.toBeFalsy();
});
- it("Should get the master commit", async() => {
+ it("Should get the head commit", async() => {
expect.assertions(8);
- const master_commit = await repository.masterCommit();
+ const master_commit = await repository.head();
expect(master_commit).toBeDefined();
expect(master_commit).toBeInstanceOf(Commit);