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.7 kB 249 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 75export const repoNameOf = (view: RecordView<RepoRecord>): string => 76 view.value.name ?? rkeyFromUri(view.uri); 77 78// a repo bobbin has never indexed is a miss, not an error 79export const resolveRepoByName = async ( 80 ctx: BobbinContext, 81 ownerDid: string, 82 name: string, 83 init?: XrpcRequestInit 84): Promise<RecordView<RepoRecord> | null> => { 85 try { 86 return await getRepoByName(ctx, ownerDid, name, init); 87 } catch (cause) { 88 if (cause instanceof ClientResponseError && httpStatusFor(cause) === 404) return null; 89 throw cause; 90 } 91}; 92 93export interface CommitSummary { 94 hash: string; 95 shortHash: string; 96 subject: string; 97 body: string; 98 authorName: string; 99 authorEmail: string; 100 when: string; 101 changeId?: string; 102} 103 104const splitMessage = (message: string): [string, string] => { 105 const separator = message.indexOf("\n\n"); 106 if (separator === -1) return [message.trim(), ""]; 107 return [message.slice(0, separator).trim(), message.slice(separator + 2).trim()]; 108}; 109 110/** the subject is the first paragraph, not the first line */ 111export const subjectOf = (message: string): string => splitMessage(message)[0]; 112 113export const toCommitSummary = (commit: LogCommit): CommitSummary => { 114 const [subject, body] = splitMessage(commit.message ?? ""); 115 const hash = commit.this ?? ""; 116 return { 117 hash, 118 shortHash: hash.slice(0, 8), 119 subject, 120 body, 121 authorName: commit.author?.Name ?? "", 122 authorEmail: commit.author?.Email ?? "", 123 when: commit.committer?.When ?? commit.author?.When ?? "", 124 changeId: commit.change_id 125 }; 126}; 127 128// the tree endpoint uses lexicon casing where the log ones pass through go field 129// names, so this cannot share `toCommitSummary` 130export const toTreeCommitSummary = (commit: Tree.LastCommit): CommitSummary => { 131 const [subject, body] = splitMessage(commit.message ?? ""); 132 return { 133 hash: commit.hash, 134 shortHash: commit.hash.slice(0, 8), 135 subject, 136 body, 137 authorName: commit.author?.name ?? "", 138 authorEmail: commit.author?.email ?? "", 139 when: commit.when ?? commit.author?.when ?? "" 140 }; 141}; 142 143export interface BranchSummary { 144 name: string; 145 hash: string; 146 when?: string; 147 isDefault: boolean; 148} 149 150export const toBranchSummary = (branch: BranchEntry): BranchSummary => ({ 151 name: branch.reference.name, 152 hash: branch.reference.hash, 153 when: branch.commit?.Committer?.When ?? branch.commit?.Author?.When, 154 isDefault: branch.is_default === true 155}); 156 157export interface TagSummary { 158 name: string; 159 hash: string; 160 /** an annotated tag has its own hash, this is the commit it points at */ 161 commitHash: string; 162 when?: string; 163 message?: string; 164} 165 166const hexFromBytes = (bytes: number[]): string => 167 bytes.map((byte) => byte.toString(16).padStart(2, "0")).join(""); 168 169export const toTagSummary = (tag: TagEntry): TagSummary => { 170 const target = tag.tag?.Target; 171 return { 172 name: tag.name, 173 hash: tag.hash, 174 commitHash: target?.length ? hexFromBytes(target) : tag.hash, 175 when: tag.tag?.Tagger?.When, 176 message: tag.message ?? tag.tag?.Message 177 }; 178}; 179 180export type TreeEntryKind = "file" | "directory" | "symlink" | "submodule"; 181 182// modes come back octal and zero padded 183export const treeEntryKind = (mode: string): TreeEntryKind => { 184 switch (mode.replace(/^0+/, "").padStart(6, "0")) { 185 case "040000": 186 return "directory"; 187 case "120000": 188 return "symlink"; 189 case "160000": 190 return "submodule"; 191 default: 192 return "file"; 193 } 194}; 195 196export interface TreeEntrySummary { 197 name: string; 198 kind: TreeEntryKind; 199 size: number; 200 lastCommitHash?: string; 201 lastCommitWhen?: string; 202 lastCommitMessage?: string; 203} 204 205export const toTreeEntrySummary = (entry: Tree.TreeEntry): TreeEntrySummary => ({ 206 name: entry.name, 207 kind: treeEntryKind(entry.mode), 208 size: entry.size, 209 lastCommitHash: entry.last_commit?.hash, 210 lastCommitWhen: entry.last_commit?.when, 211 lastCommitMessage: entry.last_commit?.message 212}); 213 214export const tagsByCommitHash = ( 215 commits: CommitSummary[], 216 tags: TagSummary[] 217): Record<string, string[]> => { 218 const shown = new Set(commits.map((commit) => commit.hash)); 219 return tags.reduce<Record<string, string[]>>((acc, tag) => { 220 if (shown.has(tag.commitHash)) (acc[tag.commitHash] ??= []).push(tag.name); 221 return acc; 222 }, {}); 223}; 224 225export const sortTreeEntries = (entries: TreeEntrySummary[]): TreeEntrySummary[] => 226 [...entries].sort((a, b) => { 227 const aDir = a.kind === "directory" || a.kind === "submodule"; 228 const bDir = b.kind === "directory" || b.kind === "submodule"; 229 if (aDir !== bDir) return aDir ? -1 : 1; 230 return a.name.localeCompare(b.name); 231 }); 232 233export const logFor = ( 234 ctx: BobbinContext, 235 repo: string, 236 ref: string, 237 limit: number, 238 init?: XrpcRequestInit 239) => knotLog<LogResponse>(ctx, { repo, ref, limit }, init); 240 241export const branchesFor = ( 242 ctx: BobbinContext, 243 repo: string, 244 limit: number, 245 init?: XrpcRequestInit 246) => knotBranches<BranchesResponse>(ctx, { repo, limit }, init); 247 248export const tagsFor = (ctx: BobbinContext, repo: string, limit: number, init?: XrpcRequestInit) => 249 knotTags<TagsResponse>(ctx, { repo, limit }, init);