This repository has no description
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
110export const toCommitSummary = (commit: LogCommit): CommitSummary => {
111 const [subject, body] = splitMessage(commit.message ?? "");
112 const hash = commit.this ?? "";
113 return {
114 hash,
115 shortHash: hash.slice(0, 8),
116 subject,
117 body,
118 authorName: commit.author?.Name ?? "",
119 authorEmail: commit.author?.Email ?? "",
120 when: commit.committer?.When ?? commit.author?.When ?? "",
121 changeId: commit.change_id
122 };
123};
124
125export interface BranchSummary {
126 name: string;
127 hash: string;
128 when?: string;
129 isDefault: boolean;
130}
131
132export const toBranchSummary = (branch: BranchEntry): BranchSummary => ({
133 name: branch.reference.name,
134 hash: branch.reference.hash,
135 when: branch.commit?.Committer?.When ?? branch.commit?.Author?.When,
136 isDefault: branch.is_default === true
137});
138
139export interface TagSummary {
140 name: string;
141 hash: string;
142 /** an annotated tag has its own hash, this is the commit it points at */
143 commitHash: string;
144 when?: string;
145 message?: string;
146}
147
148const hexFromBytes = (bytes: number[]): string =>
149 bytes.map((byte) => byte.toString(16).padStart(2, "0")).join("");
150
151export const toTagSummary = (tag: TagEntry): TagSummary => {
152 const target = tag.tag?.Target;
153 return {
154 name: tag.name,
155 hash: tag.hash,
156 commitHash: target?.length ? hexFromBytes(target) : tag.hash,
157 when: tag.tag?.Tagger?.When,
158 message: tag.message ?? tag.tag?.Message
159 };
160};
161
162export type TreeEntryKind = "file" | "directory" | "symlink" | "submodule";
163
164// modes come back octal and zero padded
165export const treeEntryKind = (mode: string): TreeEntryKind => {
166 switch (mode.replace(/^0+/, "").padStart(6, "0")) {
167 case "040000":
168 return "directory";
169 case "120000":
170 return "symlink";
171 case "160000":
172 return "submodule";
173 default:
174 return "file";
175 }
176};
177
178export interface TreeEntrySummary {
179 name: string;
180 kind: TreeEntryKind;
181 size: number;
182 lastCommitHash?: string;
183 lastCommitWhen?: string;
184 lastCommitMessage?: string;
185}
186
187export const toTreeEntrySummary = (entry: Tree.TreeEntry): TreeEntrySummary => ({
188 name: entry.name,
189 kind: treeEntryKind(entry.mode),
190 size: entry.size,
191 lastCommitHash: entry.last_commit?.hash,
192 lastCommitWhen: entry.last_commit?.when,
193 lastCommitMessage: entry.last_commit?.message
194});
195
196export const tagsByCommitHash = (
197 commits: CommitSummary[],
198 tags: TagSummary[]
199): Record<string, string[]> => {
200 const shown = new Set(commits.map((commit) => commit.hash));
201 return tags.reduce<Record<string, string[]>>((acc, tag) => {
202 if (shown.has(tag.commitHash)) (acc[tag.commitHash] ??= []).push(tag.name);
203 return acc;
204 }, {});
205};
206
207export const sortTreeEntries = (entries: TreeEntrySummary[]): TreeEntrySummary[] =>
208 [...entries].sort((a, b) => {
209 const aDir = a.kind === "directory" || a.kind === "submodule";
210 const bDir = b.kind === "directory" || b.kind === "submodule";
211 if (aDir !== bDir) return aDir ? -1 : 1;
212 return a.name.localeCompare(b.name);
213 });
214
215export const logFor = (
216 ctx: BobbinContext,
217 repo: string,
218 ref: string,
219 limit: number,
220 init?: XrpcRequestInit
221) => knotLog<LogResponse>(ctx, { repo, ref, limit }, init);
222
223export const branchesFor = (
224 ctx: BobbinContext,
225 repo: string,
226 limit: number,
227 init?: XrpcRequestInit
228) => knotBranches<BranchesResponse>(ctx, { repo, limit }, init);
229
230export const tagsFor = (ctx: BobbinContext, repo: string, limit: number, init?: XrpcRequestInit) =>
231 knotTags<TagsResponse>(ctx, { repo, limit }, init);