1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
| import { join } from "path";
import { mkdir } from "fs/promises";
import { config } from "../lib/config";
export interface GitCommit {
sha: string;
message: string;
author: string;
authorEmail: string;
date: string;
parentShas: string[];
}
export interface GitTreeEntry {
mode: string;
type: "blob" | "tree" | "commit";
sha: string;
name: string;
size?: number;
}
export interface GitBlob {
content: string;
size: number;
isBinary: boolean;
}
export interface GitDiffFile {
path: string;
oldPath?: string;
status: string;
additions: number;
deletions: number;
patch: string;
}
function repoPath(owner: string, name: string): string {
return join(config.gitReposPath, owner, `${name}.git`);
}
async function exec(
cmd: string[],
opts?: { cwd?: string; env?: Record<string, string> }
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
const proc = Bun.spawn(cmd, {
cwd: opts?.cwd,
env: { ...process.env, ...opts?.env },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
]);
const exitCode = await proc.exited;
return { stdout, stderr, exitCode };
}
export async function initBareRepo(
owner: string,
name: string
): Promise<string> {
const path = repoPath(owner, name);
await mkdir(join(config.gitReposPath, owner), { recursive: true });
await exec(["git", "init", "--bare", path]);
await exec(["git", "symbolic-ref", "HEAD", "refs/heads/main"], {
cwd: path,
});
return path;
}
export async function repoExists(
owner: string,
name: string
): Promise<boolean> {
const path = repoPath(owner, name);
const file = Bun.file(join(path, "HEAD"));
return file.exists();
}
export function getRepoPath(owner: string, name: string): string {
return repoPath(owner, name);
}
export async function listBranches(
owner: string,
name: string
): Promise<string[]> {
const path = repoPath(owner, name);
const { stdout, exitCode } = await exec(
["git", "for-each-ref", "--format=%(refname:short)", "refs/heads/"],
{ cwd: path }
);
if (exitCode !== 0) return [];
return stdout.trim().split("\n").filter(Boolean);
}
export async function getDefaultBranch(
owner: string,
name: string
): Promise<string | null> {
const path = repoPath(owner, name);
const { stdout, exitCode } = await exec(
["git", "symbolic-ref", "--short", "HEAD"],
{ cwd: path }
);
if (exitCode !== 0) return null;
return stdout.trim() || null;
}
export async function resolveRef(
owner: string,
name: string,
ref: string
): Promise<string | null> {
const path = repoPath(owner, name);
const { stdout, exitCode } = await exec(
["git", "rev-parse", "--verify", ref],
{ cwd: path }
);
if (exitCode !== 0) return null;
return stdout.trim();
}
export async function getCommit(
owner: string,
name: string,
sha: string
): Promise<GitCommit | null> {
const path = repoPath(owner, name);
const format = "%H%n%s%n%an%n%ae%n%aI%n%P";
const { stdout, exitCode } = await exec(
["git", "log", "-1", `--format=${format}`, sha],
{ cwd: path }
);
if (exitCode !== 0) return null;
const lines = stdout.trim().split("\n");
if (lines.length < 5) return null;
return {
sha: lines[0],
message: lines[1],
author: lines[2],
authorEmail: lines[3],
date: lines[4],
parentShas: lines[5] ? lines[5].split(" ").filter(Boolean) : [],
};
}
export async function getCommitFullMessage(
owner: string,
name: string,
sha: string
): Promise<string> {
const path = repoPath(owner, name);
const { stdout } = await exec(
["git", "log", "-1", "--format=%B", sha],
{ cwd: path }
);
return stdout.trim();
}
export async function listCommits(
owner: string,
name: string,
ref: string,
limit = 30,
offset = 0
): Promise<GitCommit[]> {
const path = repoPath(owner, name);
const format = "%H%x00%s%x00%an%x00%ae%x00%aI%x00%P";
const { stdout, exitCode } = await exec(
[
"git",
"log",
`--format=${format}`,
`--skip=${offset}`,
`-${limit}`,
ref,
],
{ cwd: path }
);
if (exitCode !== 0) return [];
return stdout
.trim()
.split("\n")
.filter(Boolean)
.map((line) => {
const [sha, message, author, authorEmail, date, parents] =
line.split("\0");
return {
sha,
message,
author,
authorEmail,
date,
parentShas: parents ? parents.split(" ").filter(Boolean) : [],
};
});
}
export async function getTree(
owner: string,
name: string,
ref: string,
treePath = ""
): Promise<GitTreeEntry[]> {
const path = repoPath(owner, name);
const treeish = treePath ? `${ref}:${treePath}` : `${ref}`;
const { stdout, exitCode } = await exec(
["git", "ls-tree", "-l", treeish],
{ cwd: path }
);
if (exitCode !== 0) return [];
return stdout
.trim()
.split("\n")
.filter(Boolean)
.map((line) => {
const match = line.match(
/^(\d+)\s+(blob|tree|commit)\s+([0-9a-f]+)\s+(-|\d+)\t(.+)$/
);
if (!match) return null;
return {
mode: match[1],
type: match[2] as "blob" | "tree" | "commit",
sha: match[3],
size: match[4] === "-" ? undefined : parseInt(match[4], 10),
name: match[5],
};
})
.filter((e): e is GitTreeEntry => e !== null)
.sort((a, b) => {
if (a.type === "tree" && b.type !== "tree") return -1;
if (a.type !== "tree" && b.type === "tree") return 1;
return a.name.localeCompare(b.name);
});
}
export async function getBlob(
owner: string,
name: string,
ref: string,
filePath: string
): Promise<GitBlob | null> {
const path = repoPath(owner, name);
const { stdout, exitCode } = await exec(
["git", "cat-file", "-s", `${ref}:${filePath}`],
{ cwd: path }
);
if (exitCode !== 0) return null;
const size = parseInt(stdout.trim(), 10);
const { stdout: content, exitCode: catCode } = await exec(
["git", "show", `${ref}:${filePath}`],
{ cwd: path }
);
if (catCode !== 0) return null;
const isBinary = content.includes("\0");
return {
content: isBinary ? "" : content,
size,
isBinary,
};
}
export async function getDiff(
owner: string,
name: string,
sha: string
): Promise<{ files: GitDiffFile[]; raw: string }> {
const path = repoPath(owner, name);
const commit = await getCommit(owner, name, sha);
if (!commit) return { files: [], raw: "" };
let diffCmd: string[];
if (commit.parentShas.length === 0) {
const emptyTree = "4b825dc642cb6eb9a060e54bf899d15363da7b23";
diffCmd = ["git", "diff", emptyTree, sha];
} else {
diffCmd = ["git", "diff", `${commit.parentShas[0]}..${sha}`];
}
const { stdout: raw } = await exec(diffCmd, { cwd: path });
const { stdout: stat } = await exec([...diffCmd, "--numstat"], {
cwd: path,
});
const files = stat
.trim()
.split("\n")
.filter(Boolean)
.map((line) => {
const [add, del, filePath] = line.split("\t");
return {
path: filePath,
status: "modified",
additions: add === "-" ? 0 : parseInt(add, 10),
deletions: del === "-" ? 0 : parseInt(del, 10),
patch: "",
};
});
return { files, raw };
}
export async function getReadme(
owner: string,
name: string,
ref: string
): Promise<string | null> {
const tree = await getTree(owner, name, ref);
const readme = tree.find((e) =>
/^readme(\.(md|txt|rst))?$/i.test(e.name)
);
if (!readme) return null;
const blob = await getBlob(owner, name, ref, readme.name);
return blob?.content || null;
}
|