Skip to content
218 linesCodeBlameRaw
1import { Check, Copy, GitCommitHorizontal, GitPullRequest, MessagesSquare, Sparkles } from "lucide-react";
2import { Suspense, useState } from "react";
3import { Await, Link, data, redirect } from "react-router";
4
5import type { Comparison, Pull } from "@g1t/contracts";
6
7import { parseCommitMessage } from "../../lib/commit-message";
8import type { Route } from "./+types/commit";
9import { page } from "../../lib/meta";
10import { CommitChecksBadge } from "../../components/commit-checks";
11import { DiffView } from "../../components/diff-view";
12import { CommitAvatars, CommitNames } from "../../components/commit-person";
13import { TimeAgo } from "../../components/ui";
14import { Badge } from "../../components/ui/badge";
15import { Button } from "../../components/ui/button";
16import { Card } from "../../components/ui/card";
17import { Skeleton } from "../../components/ui/skeleton";
18import { commitChecksFor } from "../../lib/commit-checks.server";
19import { immutable } from "../../lib/immutable.server";
20import { pullForCommit } from "../../lib/provenance.server";
21import { showOneCommit } from "../../lib/commit-people.server";
22import { repos } from "../../lib/services.server";
23import { getViewer, unwrap } from "../../lib/session.server";
24
25export function headers({ loaderHeaders }: Route.HeadersArgs) {
26 return { "Server-Timing": loaderHeaders.get("Server-Timing") ?? "" };
27}
28
29export function meta({ loaderData, params, ...args }: Route.MetaArgs) {
30 const subject = loaderData?.commit.message.split("\n")[0];
31 return page(args, {
32 title: `${subject ? `${subject} · ` : ""}${params.hash.slice(0, 7)} · ${params.owner}/${params.repo} · g1t`,
33 });
34}
35
36/** How far back a short hash is looked for. */
37const SHORT_HASH_SEARCH = 300;
38
39export async function loader({ params, context }: Route.LoaderArgs) {
40 const viewer = getViewer(context);
41 const path = { namespace: params.owner, name: params.repo };
42 const hash = params.hash.toLowerCase();
43 if (!/^[0-9a-f]{4,40}$/.test(hash)) throw data(null, { status: 404 });
44 if (hash.length < 40) {
45 // A short hash: find the commit it names and go to its full address.
46 const log = await repos.log(path, viewer, null, SHORT_HASH_SEARCH);
47 const matches = log.ok ? log.value.filter((commit) => commit.hash.startsWith(hash)) : [];
48 if (matches.length === 1) throw redirect(`/${params.owner}/${params.repo}/commit/${matches[0]!.hash}`);
49 throw new Response("Commit not found.", { status: 404 });
50 }
51 const started = Date.now();
52 const found = unwrap(await repos.get(path, viewer));
53 // The viewer may read the repository, and a commit never changes: after
54 // the first visit it and its diff come from the cache.
55 let cached = true;
56 const loaded = await immutable(`commit:${found.id}:${hash}`, async () => {
57 cached = false;
58 // At once: a commit is compared with its first parent, so the diff
59 // does not wait for the commit.
60 const [log, compared] = await Promise.all([
61 repos.log(path, viewer, hash, 1),
62 repos.compare(found.id, viewer, null, hash),
63 ]);
64 const commit = log.ok ? log.value[0] : undefined;
65 if (!commit) return null;
66 const comparison: Comparison = compared.ok
67 ? compared.value
68 : { base: null, head: commit.hash, files: [], truncated: false };
69 return { commit, comparison };
70 });
71 if (!loaded) throw new Response("Commit not found.", { status: 404 });
72 // Its people, as their accounts where there are any; not kept with the
73 // commit, since an address can be confirmed or removed later.
74 const commit = (await showOneCommit(loaded.commit))!;
75 return data(
76 {
77 ...loaded,
78 commit,
79 // Streamed: the page shows the commit while this is worked out.
80 pull: pullForCommit(path, viewer, loaded.commit.hash),
81 // Never cached: checks report on a commit long after it was made.
82 checks: commitChecksFor(path, viewer, [loaded.commit.hash]),
83 },
84 { headers: { "Server-Timing": `load;dur=${Date.now() - started};desc="${cached ? "cached" : "read"}"` } },
85 );
86}
87
88function CopyHash({ hash }: { hash: string }) {
89 const [copied, setCopied] = useState(false);
90 return (
91 <Button
92 type="button"
93 onClick={() => {
94 void navigator.clipboard?.writeText(hash).then(() => {
95 setCopied(true);
96 setTimeout(() => setCopied(false), 1500);
97 });
98 }} variant="outline" size="inline" className="bg-bg px-2 py-1 font-mono text-xs text-muted" aria-label="Copy the full hash">
99 {hash.slice(0, 12)}
100 {copied ? <Check size={12} className="text-success" /> : <Copy size={12} />}
101 </Button>
102 );
103}
104
105/** The pull request a commit arrived in, once it has been worked out. */
106function MergedIn({ base, pull }: { base: string; pull: Pull }) {
107 const byAgent = pull.runtime === "hosted";
108 return (
109 <div className="flex flex-wrap items-center gap-x-4 gap-y-2 border-t border-line px-5 py-3 text-sm">
110 <span className="flex items-center gap-2">
111 <GitPullRequest size={15} className="text-merged" />
112 <span className="text-muted">Merged in</span>
113 <Link to={`${base}/pull/${pull.number}`} className="font-medium hover:underline">
114 {pull.title} <span className="font-normal text-faint">#{pull.number}</span>
115 </Link>
116 </span>
117 {byAgent && (
118 <Badge tone="accent" size="md" className="gap-1.5">
119 <Sparkles size={12} />
120 written by {pull.agent}
121 </Badge>
122 )}
123 <Link
124 to={`${base}/pull/${pull.number}?tab=session`}
125 className="ml-auto flex items-center gap-1.5 rounded-md border border-line px-2.5 py-1 text-xs text-muted transition-colors hover:border-line-strong hover:text-fg"
126 >
127 <MessagesSquare size={13} />
128 Why it was written
129 </Link>
130 </div>
131 );
132}
133
134export default function CommitPage({ loaderData, params }: Route.ComponentProps) {
135 const { commit, pull, comparison, checks } = loaderData;
136 const base = `/${params.owner}/${params.repo}`;
137 const { subject, body, trailers } = parseCommitMessage(commit.message);
138 return (
139 <div>
140 <Card asChild className="overflow-hidden">
141 <section>
142 <div className="p-5">
143 <p className="flex items-center gap-2 text-xs text-faint">
144 <GitCommitHorizontal size={14} />
145 <Link to={`${base}/commits`} className="hover:text-fg">
146 Commit
147 </Link>
148 </p>
149 <h2 className="mt-2 text-xl font-semibold tracking-tight text-balance">{subject}</h2>
150 {body && <p className="mt-3 max-w-3xl text-sm whitespace-pre-wrap text-muted">{body}</p>}
151 {trailers.length > 0 && (
152 <dl className="mt-3 space-y-0.5 font-mono text-xs text-faint">
153 {trailers.map((trailer) => (
154 <div key={`${trailer.key}:${trailer.value}`}>
155 <dt className="inline">{trailer.key}:</dt> <dd className="inline text-muted">{trailer.value}</dd>
156 </div>
157 ))}
158 </dl>
159 )}
160 </div>
161 <div className="flex flex-wrap items-center gap-x-5 gap-y-3 border-t border-line bg-bg/40 px-5 py-3 text-sm">
162 <span className="flex items-center gap-2">
163 <CommitAvatars commit={commit} size={20} />
164 <span className="font-medium">
165 <CommitNames commit={commit} all />
166 </span>
167 <span className="text-muted">
168 committed <TimeAgo at={commit.authoredAt} />
169 </span>
170 </span>
171 <span className="flex items-center gap-2 text-xs text-muted">
172 {commit.parents.length === 0 ? (
173 "The first commit"
174 ) : (
175 <>
176 {commit.parents.length === 1 ? "Parent" : "Parents"}
177 {commit.parents.map((parent) => (
178 <Link
179 key={parent}
180 to={`${base}/commit/${parent}`}
181 prefetch="intent"
182 className="font-mono text-fg/80 hover:text-accent hover:underline"
183 >
184 {parent.slice(0, 7)}
185 </Link>
186 ))}
187 </>
188 )}
189 </span>
190 <span className="ml-auto flex items-center gap-2">
191 <CommitChecksBadge checks={checks} sha={commit.hash} />
192 <Link
193 to={`${base}/tree/${commit.hash}/`}
194 className="rounded-md px-2 py-1 text-xs text-muted hover:bg-raised hover:text-fg"
195 >
196 Browse files
197 </Link>
198 <CopyHash hash={commit.hash} />
199 </span>
200 </div>
201 <Suspense
202 fallback={
203 <div aria-busy="true" className="flex h-12 items-center gap-3 border-t border-line px-4">
204 <Skeleton className="size-4 rounded-full" />
205 <Skeleton className="h-3 w-64 max-w-[60%]" />
206 </div>
207 }
208 >
209 <Await resolve={pull}>{(pull) => (pull ? <MergedIn base={base} pull={pull} /> : null)}</Await>
210 </Suspense>
211 </section>
212 </Card>
213 <div className="mt-6">
214 <DiffView comparison={comparison} empty="This commit changes no files." fileBase={`${base}/blob/${commit.hash}`} />
215 </div>
216 </div>
217 );
218}