From b0fce1acd94a06f84c74c87d45c96dbaf0941729 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 22 Jun 2021 15:10:29 +0200 Subject: Organized the git-related types --- packages/server/src/api/git.ts | 62 +++++++++++++---------- packages/server/src/api/git_types.d.ts | 91 +++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 61 deletions(-) (limited to 'packages/server/src/api') diff --git a/packages/server/src/api/git.ts b/packages/server/src/api/git.ts index d32a299..e3e07c7 100644 --- a/packages/server/src/api/git.ts +++ b/packages/server/src/api/git.ts @@ -1,5 +1,12 @@ -import { Branch, Hunk, Hunks, LatestCommit, LogCommit, Patch, PatchHeaderData, RequestInfo, ShortBranch, ShortCommit, ShortRepository, ShortTag, ShortTreeEntry, Tree } from "./git_types"; -import { Commit, ConvenientHunk, ConvenientPatch, Object, Oid, Repository, Revwalk, Tag, TreeEntry } from "nodegit"; +import { + Branch, Commit, + Hunk, Hunks, + LatestCommit, LogCommit, + Patch, PatchHeaderData, + Repository, RequestInfo, + ShortBranch, Tag, + Tree, TreeEntry +} from "./git_types"; import { FastifyReply, FastifyRequest } from "fastify"; import { Pack, pack } from "tar-stream"; import { join, parse } from "path"; @@ -8,6 +15,7 @@ import { IncomingMessage } from "http"; import { Route } from "../fastify_types"; import { URL } from "whatwg-url"; import { createGzip } from "zlib"; +import nodegit from "nodegit"; import { pipeline } from "stream"; import { spawn } from "child_process"; import { verifyGitRequest } from "./util"; @@ -58,7 +66,7 @@ function getPatchHeaderData(patch_headers: string[], all_patches: string[]) { }, { indexes: [], lengths: [], last: null }); } -function getHunks(hunks: ConvenientHunk[], patch_content: string[]) { +function getHunks(hunks: nodegit.ConvenientHunk[], patch_content: string[]) { return hunks.reduce((hunks_data: Hunks, hunk, hunk_index) => { const hunk_header = hunk.header(); const hunk_header_index = patch_content.indexOf(hunk_header.replace(/\n/gu, "")); @@ -79,7 +87,7 @@ function getHunks(hunks: ConvenientHunk[], patch_content: string[]) { }, { prev: null, hunks: [] }); } -function getPatch(patch: ConvenientPatch, too_large: boolean, hunks?: Hunk[]): Patch { +function getPatch(patch: nodegit.ConvenientPatch, too_large: boolean, hunks?: Hunk[]): Patch { return { from: patch.oldFile().path(), to: patch.newFile().path(), @@ -118,8 +126,8 @@ function getRequestInfo(req: Request): RequestInfo { }; } -async function getTreeEntryLastCommit(repo: Repository, tree_entry: TreeEntry) { - const walker = Revwalk.create(repo); +async function getTreeEntryLastCommit(repo: nodegit.Repository, tree_entry: nodegit.TreeEntry) { + const walker = nodegit.Revwalk.create(repo); walker.pushHead(); const raw_commits = await walker.getCommitsUntil(() => true); @@ -162,7 +170,7 @@ function readDirectory(directory: string) { }); } -async function addArchiveEntries(entries: TreeEntry[], repo_name: string, archive: Pack) { +async function addArchiveEntries(entries: nodegit.TreeEntry[], repo_name: string, archive: Pack) { for(const tree_entry of entries) { if(tree_entry.isBlob()) { const blob = await tree_entry.getBlob(); @@ -175,7 +183,7 @@ async function addArchiveEntries(entries: TreeEntry[], repo_name: string, archiv } } -function getTreeEntries(repo: Repository, entries: TreeEntry[]) { +function getTreeEntries(repo: nodegit.Repository, entries: nodegit.TreeEntry[]) { return entries.reduce((acc, entry) => { return acc.then(result => { return getTreeEntryLastCommit(repo, entry).then(last_commit => { @@ -192,7 +200,7 @@ function getTreeEntries(repo: Repository, entries: TreeEntry[]) { return result; }); }); - }, Promise.resolve([])); + }, Promise.resolve([])); } export class GitAPI { @@ -204,9 +212,9 @@ export class GitAPI { async getLog(repo_name: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); - const walker: Revwalk = Revwalk.create(repo); + const walker: nodegit.Revwalk = nodegit.Revwalk.create(repo); walker.pushHead(); const raw_commits = await walker.getCommitsUntil(() => true); @@ -227,7 +235,7 @@ export class GitAPI { async getRepositoryLastCommit(repo_name: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const master_commit = await repo.getMasterCommit(); @@ -247,7 +255,7 @@ export class GitAPI { }); } - async getRepositories(): Promise { + async getRepositories(): Promise { const dir_content = await readDirectory(this.base_dir); if(dir_content.length === 0) { @@ -270,12 +278,12 @@ export class GitAPI { }); }); }); - }, Promise.resolve([])); + }, Promise.resolve([])); } - async getCommit(repo_name: string, commit_oid: string): Promise { + async getCommit(repo_name: string, commit_oid: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_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"); @@ -365,7 +373,7 @@ export class GitAPI { async getTree(repo_name: string, tree_path: string | null): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const master_commit = await repo.getMasterCommit(); const tree = await master_commit.getTree(); @@ -391,16 +399,16 @@ export class GitAPI { async doesObjectExist(repo_name: string, id: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); - return Object.lookup(repo, Oid.fromString(id), Object.TYPE.ANY) + return nodegit.Object.lookup(repo, nodegit.Oid.fromString(id), nodegit.Object.TYPE.ANY) .then(() => true) .catch(() => false); } async doesReadmeExist(repo_name: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const master_commit = await repo.getMasterCommit(); const tree = await master_commit.getTree(); @@ -412,7 +420,7 @@ export class GitAPI { async getBranches(repo_name: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const references = await repo.getReferences(); @@ -426,7 +434,7 @@ export class GitAPI { async getBranch(repo_name: string, branch_id: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const references = await repo.getReferences(); const branches = references.filter(ref => ref.isBranch()); @@ -449,14 +457,14 @@ export class GitAPI { }; } - async getTags(repo_name: string): Promise { + async getTags(repo_name: string): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const references = await repo.getReferences(); return Promise.all(references.filter(ref => ref.isTag()).map(async ref => { - const tagger = (await Tag.lookup(repo, ref.target())).tagger(); + const tagger = (await nodegit.Tag.lookup(repo, ref.target())).tagger(); return { name: ref.shorthand(), @@ -471,7 +479,7 @@ export class GitAPI { async downloadTagArchive(repo_name: string, tag_name: string, reply: FastifyReply): Promise { const full_repo_name = addRepoDirSuffix(repo_name); - const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); + const repo = await nodegit.Repository.openBare(`${this.base_dir}/${full_repo_name}`); const reference = await repo.getReference(tag_name) .catch(() => { @@ -482,7 +490,7 @@ export class GitAPI { return; } - const commit = await Commit.lookup(repo, (await reference.peel(Object.TYPE.COMMIT)).id()); + const commit = await nodegit.Commit.lookup(repo, (await reference.peel(nodegit.Object.TYPE.COMMIT)).id()); const tree = await commit.getTree(); const archive = pack(); diff --git a/packages/server/src/api/git_types.d.ts b/packages/server/src/api/git_types.d.ts index ae110ff..8aff698 100644 --- a/packages/server/src/api/git_types.d.ts +++ b/packages/server/src/api/git_types.d.ts @@ -1,4 +1,28 @@ -export interface Hunk { +/* + Types +*/ + +// General +export type Author = { + name: string, + email: string +} +export type LatestCommit = { + id: string | null, + message: string | null, + date: number | null +} + +// Repository +export type Repository = { + name: string, + description: string | null, + owner: string | null, + last_updated: number +} + +// Diff +export type Hunk = { new_start: number, new_lines_cnt: number, old_start: number, @@ -7,7 +31,7 @@ export interface Hunk { deleted_lines: number[], hunk: string } -export interface Patch { +export type Patch = { from: string, to: string, additions: number, @@ -15,7 +39,18 @@ export interface Patch { too_large: boolean, hunks: Hunk[] | null } -export interface RequestInfo { +export type Hunks = { + prev: null | number, + hunks: Hunk[] +} +export type PatchHeaderData = { + indexes: number[], + lengths: number[], + last: number | null +} + +// Request +export type RequestInfo = { repo: string, url_path: string, parsed_url: URL, @@ -24,12 +59,9 @@ export interface RequestInfo { service: string | null, content_type: string } -export interface LatestCommit { - id: string | null, - message: string | null, - date: number | null -} -export interface ShortTreeEntry { + +// Tree +export type TreeEntry = { name: string, id: string, type: "blob" | "tree", @@ -37,18 +69,10 @@ export interface ShortTreeEntry { } export type Tree = { type: "blob" | "tree", - content: string | ShortTreeEntry[] -} -export interface ShortRepository { - name: string, - description: string | null, - owner: string | null, - last_updated: number -} -export type Author = { - name: string, - email: string + content: string | TreeEntry[] } + +// Log export type LogCommit = { id: string, author: Author, @@ -58,31 +82,30 @@ export type LogCommit = { deletions: number, files_changed: number } -export type ShortCommit = { +export type Commit = { id: string, author: Author, message: string, date: number, patches: Patch[] } -export interface Hunks { - prev: null | number, - hunks: Hunk[] + +// Tag +export type Tag = { + name: string, + date: number, + author: Author } + +/* + Interfaces +*/ + +// Branch export interface ShortBranch { id: string, name: string } export interface Branch extends ShortBranch { latest_commit: LatestCommit -} -export type ShortTag = { - name: string, - date: number, - author: Author -} -export type PatchHeaderData = { - indexes: number[], - lengths: number[], - last: number | null } \ No newline at end of file -- cgit v1.2.3-18-g5258