flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/apps/web/app/routes/repo/commit.tsx

199 lines8,610 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

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