diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-22 15:30:01 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-22 15:30:01 +0200 |
commit | 01e5d215dbc152e34ecd005111171457f87c235d (patch) | |
tree | c6017eaa7a3240fe28adeee72c3ded549cff384f /packages | |
parent | b0fce1acd94a06f84c74c87d45c96dbaf0941729 (diff) |
Simplified the opening of repositories
Diffstat (limited to 'packages')
-rw-r--r-- | packages/server/src/api/git.ts | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/packages/server/src/api/git.ts b/packages/server/src/api/git.ts index e3e07c7..49ef906 100644 --- a/packages/server/src/api/git.ts +++ b/packages/server/src/api/git.ts @@ -20,7 +20,7 @@ import { pipeline } from "stream"; import { spawn } from "child_process"; import { verifyGitRequest } from "./util"; -function addRepoDirSuffix(repo_name: string) { +function getFullRepositoryName(repo_name: string) { return repo_name.endsWith(".git") ? repo_name : `${repo_name}.git`; } @@ -210,9 +210,12 @@ export class GitAPI { this.base_dir = base_dir; } + openRepository(repo_name: string): Promise<nodegit.Repository> { + return nodegit.Repository.openBare(`${this.base_dir}/${getFullRepositoryName(repo_name)}`); + } + async getLog(repo_name: string): Promise<LogCommit[]> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const walker: nodegit.Revwalk = nodegit.Revwalk.create(repo); walker.pushHead(); @@ -234,8 +237,7 @@ export class GitAPI { } async getRepositoryLastCommit(repo_name: string): Promise<number> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const master_commit = await repo.getMasterCommit(); @@ -244,7 +246,7 @@ export class GitAPI { getRepositoryFile(repo_name: string, file: string): Promise<string | null> { return new Promise(resolve => { - const full_repo_name = addRepoDirSuffix(repo_name); + const full_repo_name = getFullRepositoryName(repo_name); readFile(`${this.base_dir}/${full_repo_name}/${file}`, (err, content) => { if(err) { resolve(null); @@ -282,8 +284,7 @@ export class GitAPI { } async getCommit(repo_name: string, commit_oid: string): Promise<Commit> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const commit = await repo.getCommit(commit_oid); const diff = (await commit.getDiff())[0]; const all_patches = (await diff.toBuf(1)).toString().split("\n"); @@ -372,8 +373,7 @@ export class GitAPI { } async getTree(repo_name: string, tree_path: string | null): Promise<Tree | null> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const master_commit = await repo.getMasterCommit(); const tree = await master_commit.getTree(); @@ -398,8 +398,7 @@ export class GitAPI { } async doesObjectExist(repo_name: string, id: string): Promise<boolean> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); return nodegit.Object.lookup(repo, nodegit.Oid.fromString(id), nodegit.Object.TYPE.ANY) .then(() => true) @@ -407,8 +406,7 @@ export class GitAPI { } async doesReadmeExist(repo_name: string): Promise<boolean> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const master_commit = await repo.getMasterCommit(); const tree = await master_commit.getTree(); @@ -419,8 +417,7 @@ export class GitAPI { } async getBranches(repo_name: string): Promise<ShortBranch[]> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const references = await repo.getReferences(); @@ -433,8 +430,7 @@ export class GitAPI { } async getBranch(repo_name: string, branch_id: string): Promise<Branch | null> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const references = await repo.getReferences(); const branches = references.filter(ref => ref.isBranch()); @@ -458,8 +454,7 @@ export class GitAPI { } async getTags(repo_name: string): Promise<Tag[]> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const references = await repo.getReferences(); @@ -478,8 +473,7 @@ export class GitAPI { } async downloadTagArchive(repo_name: string, tag_name: string, reply: FastifyReply): Promise<void> { - const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await this.openRepository(repo_name); const reference = await repo.getReference(tag_name) .catch(() => { |