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
|
import { Hono } from "hono";
import { join } from "path";
import { Layout } from "../views/layout";
import { RepoHeader, DiffView } from "../views/components";
import { IssueNav } from "./issues";
import {
listBranches,
listCommits,
repoExists,
getRepoPath,
} from "../git/repository";
import { softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import type { GitDiffFile } from "../git/repository";
import {
EmptyState,
Flex,
Text,
} from "../views/ui";
const compare = new Hono<AuthEnv>();
compare.use("*", softAuth);
compare.get("/:owner/:repo/compare/:spec?", async (c) => {
const { owner, repo } = c.req.param();
const user = c.get("user");
const spec = c.req.param("spec");
if (!(await repoExists(owner, repo))) {
return c.html(
<Layout title="Not Found" user={user}>
<EmptyState title="Repository not found" />
</Layout>,
404
);
}
const branches = await listBranches(owner, repo);
if (!spec || !spec.includes("...")) {
const defaultBase = branches.includes("main") ? "main" : branches[0] || "";
return c.html(
<Layout title={`Compare — ${owner}/${repo}`} user={user}>
<RepoHeader owner={owner} repo={repo} />
<IssueNav owner={owner} repo={repo} active="code" />
<h2 style="margin-bottom: 16px">Compare changes</h2>
<form
method="get"
action={`/${owner}/${repo}/compare`}
>
<Flex gap={12} align="center" style="margin-bottom: 20px">
<select name="base" class="branch-selector" style="cursor: pointer">
{branches.map((b) => (
<option value={b} selected={b === defaultBase}>
{b}
</option>
))}
</select>
<Text muted>...</Text>
<select name="head" class="branch-selector" style="cursor: pointer">
{branches.map((b) => (
<option value={b} selected={b !== defaultBase}>
{b}
</option>
))}
</select>
<button
type="submit"
class="btn btn-primary"
onclick={`this.form.action='/${owner}/${repo}/compare/'+this.form.base.value+'...'+this.form.head.value; return true;`}
>
Compare
</button>
</Flex>
</form>
</Layout>
);
}
const [base, head] = spec.split("...");
const repoDir = getRepoPath(owner, repo);
const proc = Bun.spawn(
["git", "diff", `${base}...${head}`],
{ cwd: repoDir, stdout: "pipe", stderr: "pipe" }
);
const raw = await new Response(proc.stdout).text();
await proc.exited;
const statProc = Bun.spawn(
["git", "diff", "--numstat", `${base}...${head}`],
{ cwd: repoDir, stdout: "pipe", stderr: "pipe" }
);
const stat = await new Response(statProc.stdout).text();
await statProc.exited;
const files: GitDiffFile[] = 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: "",
};
});
const logProc = Bun.spawn(
[
"git",
"log",
"--format=%H%x00%s%x00%an%x00%aI",
`${base}...${head}`,
],
{ cwd: repoDir, stdout: "pipe", stderr: "pipe" }
);
const logOutput = await new Response(logProc.stdout).text();
await logProc.exited;
const commitsBetween = logOutput
.trim()
.split("\n")
.filter(Boolean)
.map((line) => {
const [sha, msg, author, date] = line.split("\0");
return { sha, message: msg, author, date };
});
return c.html(
<Layout title={`${base}...${head} — ${owner}/${repo}`} user={user}>
<RepoHeader owner={owner} repo={repo} />
<IssueNav owner={owner} repo={repo} active="code" />
<h2 style="margin-bottom: 8px">
Comparing {base}...{head}
</h2>
<Text size={14} muted style="display:block;margin-bottom:20px">
{commitsBetween.length} commit{commitsBetween.length !== 1 ? "s" : ""}
</Text>
{commitsBetween.length > 0 && (
<div class="commit-list" style="margin-bottom: 24px">
{commitsBetween.map((cm) => (
<div class="commit-item">
<div>
<div class="commit-message">
<a href={`/${owner}/${repo}/commit/${cm.sha}`}>
{cm.message}
</a>
</div>
<div class="commit-meta">{cm.author}</div>
</div>
<a
href={`/${owner}/${repo}/commit/${cm.sha}`}
class="commit-sha"
>
{cm.sha.slice(0, 7)}
</a>
</div>
))}
</div>
)}
<DiffView raw={raw} files={files} />
</Layout>
);
});
export default compare;
|