This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / web / src / lib / api / repo.ts
6.8 kB 250 lines
1import { ClientResponseError, type BobbinContext, type XrpcRequestInit } from "./client"; 2import { getRepoByName, type RecordView, type RepoRecord } from "./records"; 3import { branches as knotBranches, log as knotLog, tags as knotTags } from "./knot"; 4import { httpStatusFor } from "./load"; 5import { rkeyFromUri } from "./uri"; 6import type * as Tree from "./lexicons/types/sh/tangled/repo/tree"; 7 8// log, branches and tags are `*/*` in the lexicons, so these shapes are copied 9// from core/types by hand. anything go-git touches keeps its go field names and 10// writes hashes as byte arrays, so the hex comes from a sibling field 11 12export interface GitSignature { 13 Name: string; 14 Email: string; 15 When: string; 16} 17 18export interface GitCommit { 19 Author?: GitSignature; 20 Committer?: GitSignature; 21 Message?: string; 22} 23 24// `this` and `parent` are the hex hashes, `hash` is the byte array 25export interface LogCommit { 26 this?: string; 27 parent?: string; 28 author?: GitSignature; 29 committer?: GitSignature; 30 message?: string; 31 tree?: string; 32 change_id?: string; 33} 34 35export interface LogResponse { 36 commits?: LogCommit[]; 37 ref?: string; 38 total?: number; 39 page?: number; 40} 41 42export interface GitReference { 43 name: string; 44 hash: string; 45} 46 47export interface BranchEntry { 48 reference: GitReference; 49 commit?: GitCommit; 50 is_default?: boolean; 51} 52 53export interface BranchesResponse { 54 branches?: BranchEntry[]; 55 total?: number; 56} 57 58export interface TagEntry { 59 name: string; 60 hash: string; 61 message?: string; 62 tag?: { 63 Tagger?: GitSignature; 64 Message?: string; 65 /** the commit an annotated tag points at, bytes like every go-git hash */ 66 Target?: number[]; 67 }; 68} 69 70export interface TagsResponse { 71 tags?: TagEntry[]; 72 total?: number; 73} 74 75// newer repos get tid rkeys and keep their display name in the record 76export const repoNameOf = (view: RecordView<RepoRecord>): string => 77 view.value.name ?? rkeyFromUri(view.uri); 78 79// a repo bobbin has never indexed is a miss, not an error 80export const resolveRepoByName = async ( 81 ctx: BobbinContext, 82 ownerDid: string, 83 name: string, 84 init?: XrpcRequestInit 85): Promise<RecordView<RepoRecord> | null> => { 86 try { 87 return await getRepoByName(ctx, ownerDid, name, init); 88 } catch (cause) { 89 if (cause instanceof ClientResponseError && httpStatusFor(cause) === 404) return null; 90 throw cause; 91 } 92}; 93 94export interface CommitSummary { 95 hash: string; 96 shortHash: string; 97 subject: string; 98 body: string; 99 authorName: string; 100 authorEmail: string; 101 when: string; 102 changeId?: string; 103} 104 105const splitMessage = (message: string): [string, string] => { 106 const separator = message.indexOf("\n\n"); 107 if (separator === -1) return [message.trim(), ""]; 108 return [message.slice(0, separator).trim(), message.slice(separator + 2).trim()]; 109}; 110 111/** the subject is the first paragraph, not the first line */ 112export const subjectOf = (message: string): string => splitMessage(message)[0]; 113 114export const toCommitSummary = (commit: LogCommit): CommitSummary => { 115 const [subject, body] = splitMessage(commit.message ?? ""); 116 const hash = commit.this ?? ""; 117 return { 118 hash, 119 shortHash: hash.slice(0, 8), 120 subject, 121 body, 122 authorName: commit.author?.Name ?? "", 123 authorEmail: commit.author?.Email ?? "", 124 when: commit.committer?.When ?? commit.author?.When ?? "", 125 changeId: commit.change_id 126 }; 127}; 128 129// the tree endpoint uses lexicon casing where the log ones pass through go field 130// names, so this cannot share `toCommitSummary` 131export const toTreeCommitSummary = (commit: Tree.LastCommit): CommitSummary => { 132 const [subject, body] = splitMessage(commit.message ?? ""); 133 return { 134 hash: commit.hash, 135 shortHash: commit.hash.slice(0, 8), 136 subject, 137 body, 138 authorName: commit.author?.name ?? "", 139 authorEmail: commit.author?.email ?? "", 140 when: commit.when ?? commit.author?.when ?? "" 141 }; 142}; 143 144export interface BranchSummary { 145 name: string; 146 hash: string; 147 when?: string; 148 isDefault: boolean; 149} 150 151export const toBranchSummary = (branch: BranchEntry): BranchSummary => ({ 152 name: branch.reference.name, 153 hash: branch.reference.hash, 154 when: branch.commit?.Committer?.When ?? branch.commit?.Author?.When, 155 isDefault: branch.is_default === true 156}); 157 158export interface TagSummary { 159 name: string; 160 hash: string; 161 /** an annotated tag has its own hash, this is the commit it points at */ 162 commitHash: string; 163 when?: string; 164 message?: string; 165} 166 167const hexFromBytes = (bytes: number[]): string => 168 bytes.map((byte) => byte.toString(16).padStart(2, "0")).join(""); 169 170export const toTagSummary = (tag: TagEntry): TagSummary => { 171 const target = tag.tag?.Target; 172 return { 173 name: tag.name, 174 hash: tag.hash, 175 commitHash: target?.length ? hexFromBytes(target) : tag.hash, 176 when: tag.tag?.Tagger?.When, 177 message: tag.message ?? tag.tag?.Message 178 }; 179}; 180 181export type TreeEntryKind = "file" | "directory" | "symlink" | "submodule"; 182 183// modes come back octal and zero padded 184export const treeEntryKind = (mode: string): TreeEntryKind => { 185 switch (mode.replace(/^0+/, "").padStart(6, "0")) { 186 case "040000": 187 return "directory"; 188 case "120000": 189 return "symlink"; 190 case "160000": 191 return "submodule"; 192 default: 193 return "file"; 194 } 195}; 196 197export interface TreeEntrySummary { 198 name: string; 199 kind: TreeEntryKind; 200 size: number; 201 lastCommitHash?: string; 202 lastCommitWhen?: string; 203 lastCommitMessage?: string; 204} 205 206export const toTreeEntrySummary = (entry: Tree.TreeEntry): TreeEntrySummary => ({ 207 name: entry.name, 208 kind: treeEntryKind(entry.mode), 209 size: entry.size, 210 lastCommitHash: entry.last_commit?.hash, 211 lastCommitWhen: entry.last_commit?.when, 212 lastCommitMessage: entry.last_commit?.message 213}); 214 215export const tagsByCommitHash = ( 216 commits: CommitSummary[], 217 tags: TagSummary[] 218): Record<string, string[]> => { 219 const shown = new Set(commits.map((commit) => commit.hash)); 220 return tags.reduce<Record<string, string[]>>((acc, tag) => { 221 if (shown.has(tag.commitHash)) (acc[tag.commitHash] ??= []).push(tag.name); 222 return acc; 223 }, {}); 224}; 225 226export const sortTreeEntries = (entries: TreeEntrySummary[]): TreeEntrySummary[] => 227 [...entries].sort((a, b) => { 228 const aDir = a.kind === "directory" || a.kind === "submodule"; 229 const bDir = b.kind === "directory" || b.kind === "submodule"; 230 if (aDir !== bDir) return aDir ? -1 : 1; 231 return a.name.localeCompare(b.name); 232 }); 233 234export const logFor = ( 235 ctx: BobbinContext, 236 repo: string, 237 ref: string, 238 limit: number, 239 init?: XrpcRequestInit 240) => knotLog<LogResponse>(ctx, { repo, ref, limit }, init); 241 242export const branchesFor = ( 243 ctx: BobbinContext, 244 repo: string, 245 limit: number, 246 init?: XrpcRequestInit 247) => knotBranches<BranchesResponse>(ctx, { repo, limit }, init); 248 249export const tagsFor = (ctx: BobbinContext, repo: string, limit: number, init?: XrpcRequestInit) => 250 knotTags<TagsResponse>(ctx, { repo, limit }, init);