import { error } from "@sveltejs/kit"; import { createBobbinClient, type BobbinContext } from "$lib/api/client"; import { branches as knotMirrorBranches, blob as knotMirrorBlob, blobRawUrl as knotMirrorBlobRawUrl, languages as knotMirrorLanguages, getRef as knotMirrorGetRef, tags as knotMirrorTags, tree as knotMirrorTree } from "$lib/api/knotmirror"; import { listCommits } from "$lib/api/gitmirror"; import type { Did } from "@atcute/lexicons"; export interface GitServiceConfig { bobbinUrl: string; } export interface GitTarget { ctx: BobbinContext; repo: Did; } export const knotMirrorTarget = ( config: GitServiceConfig, repo: { repoDid: string }, fetch: typeof globalThis.fetch ): GitTarget => ({ ctx: createBobbinClient({ serviceUrl: config.bobbinUrl, fetch }), repo: repo.repoDid as Did }); export const gitTarget = ( config: GitServiceConfig, repo: { uri: string; repoDid?: string }, fetch: typeof globalThis.fetch ): GitTarget => { if (!repo.repoDid) error(404, "This repository has not been indexed yet"); return knotMirrorTarget(config, { repoDid: repo.repoDid }, fetch); }; export const resolveDefaultBranch = ( target: GitTarget, init?: { signal?: AbortSignal } ): Promise => knotMirrorBranches(target.ctx, { repo: target.repo, limit: 1 }, init) .then((response) => response.defaultBranch ?? null) .catch(() => null); export const tree = (target: GitTarget, params: { ref: string; path?: string }) => knotMirrorTree(target.ctx, { repo: target.repo, ...params }); export const log = (target: GitTarget, params: { ref: string; limit: number; skip?: number }) => listCommits(target.ctx, { repo: target.repo, ranges: [params.ref], limit: params.limit, skip: params.skip }); export const branch = (target: GitTarget, name: string) => knotMirrorGetRef(target.ctx, { repo: target.repo, ref: name }); export const branches = (target: GitTarget, limit: number, cursor?: string) => knotMirrorBranches(target.ctx, { repo: target.repo, limit, cursor }); export const tags = (target: GitTarget, limit: number, cursor?: string) => knotMirrorTags(target.ctx, { repo: target.repo, limit, cursor }); export const tag = (target: GitTarget, name: string) => knotMirrorGetRef(target.ctx, { repo: target.repo, ref: `refs/tags/${name}` }); export const languages = (target: GitTarget, ref: string) => knotMirrorLanguages(target.ctx, { repo: target.repo, ref }); export const blob = (target: GitTarget, params: { ref: string; path: string }, maxBytes: number) => knotMirrorBlob(target.ctx, { repo: target.repo, ...params }, maxBytes); export const blobRawUrl = (target: GitTarget, params: { ref: string; path: string }): string => knotMirrorBlobRawUrl(target.ctx.serviceUrl, { repo: target.repo, ...params });