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