From 8c6a59cd766fead4998c957d86a0e33fd58d1cde Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 5 Aug 2021 17:03:36 +0200 Subject: The tree API has a branch query param & made repository branch less hardcoded --- packages/server/src/git/commit.ts | 6 +++--- packages/server/src/git/repository.ts | 27 +++++++++++++-------------- packages/server/src/git/tree.ts | 10 +++------- packages/server/src/git/tree_entry.ts | 2 +- 4 files changed, 20 insertions(+), 25 deletions(-) (limited to 'packages/server/src/git') 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 { - return new Commit(owner, await owner.ng_repository.getMasterCommit()); + public static async branchCommit(owner: Repository): Promise { + 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 { - return Branch.lookup(this, this._branch); + return Branch.lookup(this, this.branch_name); } /** @@ -87,11 +86,20 @@ export class Repository { public async commits(): Promise { 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 { + return Commit.branchCommit(this); + } + /** * Returns the repository's tree * @@ -133,15 +141,6 @@ export class Repository { return references.filter(ref => ref.isTag()).map(ref => new Tag(this, ref)); } - /** - * Returns the repository's master commit - * - * @returns An instance of a commit - */ - public async masterCommit(): Promise { - return Commit.masterCommit(this); - } - /** * Connect to the Git HTTP backend * 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 { - 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 { 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); -- cgit v1.2.3-18-g5258