aboutsummaryrefslogtreecommitdiff
path: root/packages
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 /packages
parent22bf491be5f25bccf779be53b2ad37836f0850b8 (diff)
The tree API has a branch query param & made repository branch less hardcoded
Diffstat (limited to 'packages')
-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
5 files changed, 21 insertions, 26 deletions
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
};
}))
});