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