Skip to content

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.sh
503 linesCodeBlameRaw
1import { BookOpen, Check, ChevronDown, Code2, File, FileArchive, Folder, FolderGit2, GitBranch, History, Search, SquareTerminal, Tag as TagIcon } from "lucide-react";
2import { type ReactNode, Suspense } from "react";
3import { Await, Form, Link } from "react-router";
4
5import type { BlobView as Blob, Branch, TreeView as RawTree } from "@g1t/contracts";
6
7import type { FileCommits, ShownBlame, ShownCommit } from "../lib/commit-people";
8import { CommitAvatars, CommitNames } from "./commit-person";
9
10import type { DeploymentEnvironments } from "@g1t/contracts";
11
12import { BlameView } from "./blame-view";
13import { DeploymentsPanel } from "./deployments-panel";
14import { CodeLines } from "./code-lines";
15
16import { useAddresses } from "../lib/addresses";
17import { isImagePath } from "../lib/usercontent";
18import { CloneBox } from "./clone-box";
19import { type ChecksSource, CommitChecksBadge } from "./commit-checks";
20import { type AboutData, RepoAboutPanel } from "./repo-about";
21import { Markdown } from "./markdown";
22import { CopyLine, TimeAgo, notACredential } from "./ui";
23import { Card } from "./ui/card";
24import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger } from "./ui/dropdown-menu";
25import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
26import { Hint } from "./ui/hint";
27import { SkeletonLine } from "./ui/skeleton";
28
29function encodePath(path: string): string {
30 return path.split("/").map(encodeURIComponent).join("/");
31}
32
33function Breadcrumbs({
34 base,
35 repo,
36 gitRef,
37 path,
38}: {
39 base: string;
40 repo: string;
41 gitRef: string;
42 path: string;
43}) {
44 const segments = path.split("/").filter(Boolean);
45 if (segments.length === 0) return null;
46 // On a phone, one line: the folders between the repository and the last
47 // two fold into an ellipsis that opens the deepest of them.
48 const folded = segments.length - 2;
49 const slash = <span className="mx-1.5 shrink-0 text-faint">/</span>;
50 return (
51 <nav aria-label="Path" className="flex min-w-0 basis-full items-center font-mono text-sm whitespace-nowrap text-muted sm:basis-auto sm:flex-wrap sm:whitespace-normal">
52 <Link to={`${base}/tree/${gitRef}`} className="shrink-0 py-2 text-accent hover:underline sm:py-0">
53 {repo}
54 </Link>
55 {folded > 0 && (
56 <span className="flex shrink-0 items-center sm:hidden">
57 {slash}
58 <Link
59 to={`${base}/tree/${gitRef}/${encodePath(segments.slice(0, folded).join("/"))}`}
60 aria-label={`Up to ${segments.slice(0, folded).join("/")}`}
61 className="rounded px-1 py-2 text-accent hover:underline"
62 >
63 …
64 </Link>
65 </span>
66 )}
67 {segments.map((segment, i) => {
68 const to = encodePath(segments.slice(0, i + 1).join("/"));
69 const last = i === segments.length - 1;
70 return (
71 <span
72 key={to}
73 className={`${i < folded ? "hidden sm:flex" : "flex"} items-center ${last ? "min-w-0" : i === segments.length - 2 ? "min-w-0 shrink-[2]" : "shrink-0"}`}
74 >
75 {slash}
76 {last ? (
77 <span className="truncate font-medium text-fg">{segment}</span>
78 ) : (
79 <Link
80 to={`${base}/tree/${gitRef}/${to}`}
81 className="truncate py-2 text-accent hover:underline sm:py-0"
82 >
83 {segment}
84 </Link>
85 )}
86 </span>
87 );
88 })}
89 </nav>
90 );
91}
92
93/** The bar above a file listing: the latest commit, its checks, and the way to the history. */
94/** A tree as the page gets it: its latest commit with its people. */
95export type Tree = Omit<RawTree, "head"> & { head: ShownCommit | null };
96
97function CommitBar({ commit, base, checks }: { commit: ShownCommit; base: string; checks?: ChecksSource }) {
98 return (
99 <div className="flex items-center gap-2.5 border-b border-line bg-surface px-3 py-2.5 text-sm sm:gap-3 sm:px-4">
100 <CommitAvatars commit={commit} />
101 <span className="shrink-0 font-medium">
102 <CommitNames commit={commit} />
103 </span>
104 <Link to={`${base}/commit/${commit.hash}`} className="truncate text-muted hover:text-fg hover:underline">
105 {commit.message.split("\n")[0]}
106 </Link>
107 <CommitChecksBadge checks={checks} sha={commit.hash} className="-ml-1.5" />
108 <span className="ml-auto flex shrink-0 items-center gap-3 text-xs text-faint sm:gap-4">
109 <Link to={`${base}/commit/${commit.hash}`} className="hidden font-mono hover:text-fg sm:inline">
110 {commit.hash.slice(0, 7)}
111 </Link>
112 <span className="hidden sm:inline">
113 <TimeAgo at={commit.authoredAt} />
114 </span>
115 <Link to={`${base}/commits`} aria-label="History" className="-my-2 inline-flex items-center gap-1.5 py-2 font-medium text-muted hover:text-fg">
116 <History size={14} />
117 <span className="hidden sm:inline">History</span>
118 </Link>
119 </span>
120 </div>
121 );
122}
123
124const BAR_BUTTON =
125 "inline-flex h-8 shrink-0 items-center gap-1.5 rounded-md border border-line bg-surface px-3 text-sm transition-colors hover:border-line-strong hover:bg-raised data-[state=open]:border-line-strong";
126
127/** Which branch is shown, and the others to switch to, at the same path. */
128function BranchMenu({
129 base,
130 gitRef,
131 path,
132 branches,
133 view = "tree",
134}: {
135 base: string;
136 gitRef: string;
137 path: string;
138 branches: Branch[];
139 /** Whether `path` is a folder or a file. */
140 view?: "tree" | "blob";
141}) {
142 const rest = path ? `/${encodePath(path)}` : "";
143 return (
144 <DropdownMenu>
145 <DropdownMenuTrigger className={`${BAR_BUTTON} max-w-56 font-medium`}>
146 <GitBranch size={14} className="text-faint" />
147 <span className="truncate font-mono text-[0.8125rem]">{gitRef}</span>
148 <ChevronDown size={13} className="text-faint" />
149 </DropdownMenuTrigger>
150 <DropdownMenuContent align="start" className="max-h-80 w-64 overflow-y-auto">
151 <DropdownMenuLabel>Switch branches</DropdownMenuLabel>
152 {branches.map((branch) => (
153 <DropdownMenuItem key={branch.name} asChild>
154 <Link to={`${base}/${view}/${encodePath(branch.name)}${rest}`}>
155 <Check className={branch.name === gitRef ? "" : "invisible"} />
156 <span className="truncate font-mono text-[0.8125rem]">{branch.name}</span>
157 </Link>
158 </DropdownMenuItem>
159 ))}
160 </DropdownMenuContent>
161 </DropdownMenu>
162 );
163}
164
165/** Searches this repository's code on the search page, where it can be widened to all of g1t. */
166function SearchCode({ repo }: { repo: string }) {
167 return (
168 <Form action="/search" role="search" className="relative ml-auto min-w-0 flex-1 basis-40 sm:w-64 sm:flex-none sm:basis-auto">
169 <Search size={14} className="pointer-events-none absolute top-1/2 left-2.5 -translate-y-1/2 text-faint" />
170 <input
171 name="q"
172 {...notACredential()}
173 placeholder="Search code"
174 aria-label={`Search ${repo}`}
175 className="h-9 w-full rounded-md border border-line bg-surface pr-3 pl-8 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim sm:h-8"
176 />
177 <input type="hidden" name="repo" value={repo} />
178 <input type="hidden" name="type" value="code" />
179 </Form>
180 );
181}
182
183/** The one button for getting the code: clone over HTTPS or SSH, hand it to an agent, or download it. */
184function CodeButton({ path, gitRef }: { path: string; gitRef: string }) {
185 return (
186 <Popover>
187 <PopoverTrigger className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded-md bg-accent px-3 text-sm font-medium text-bg transition-colors hover:bg-accent/90">
188 <Code2 size={15} />
189 Code
190 <ChevronDown size={13} />
191 </PopoverTrigger>
192 <PopoverContent align="end" className="w-[min(24rem,calc(100vw-2rem))] p-0">
193 <div className="p-4">
194 <h2 className="mb-3 flex items-center gap-2 text-sm font-medium">
195 <SquareTerminal size={15} className="text-faint" />
196 Clone
197 </h2>
198 <CloneBox path={path} />
199 </div>
200 <div className="border-t border-line p-1.5">
201 {/* A plain link: the browser downloads it, not the router. */}
202 <a
203 href={`/${path}/archive/${encodePath(gitRef)}.zip`}
204 download
205 className="flex items-center gap-2.5 rounded-md px-2.5 py-2 text-sm text-fg/90 transition-colors hover:bg-line hover:text-fg"
206 >
207 <FileArchive size={15} className="text-faint" />
208 Download ZIP
209 </a>
210 </div>
211 </PopoverContent>
212 </Popover>
213 );
214}
215
216/** Above the files: the branch and, at the root, how many there are, search and Code; below it, where you are. */
217function CodeBar({
218 base,
219 repo,
220 gitRef,
221 path,
222 branches,
223}: {
224 base: string;
225 repo: { namespace: string; name: string };
226 gitRef: string;
227 path: string;
228 branches: Branch[] | null;
229}) {
230 const full = `${repo.namespace}/${repo.name}`;
231 const list = branches && branches.length > 0 ? branches : [{ name: gitRef, hash: "" }];
232 return (
233 <div className="mb-4 flex flex-wrap items-center gap-x-3 gap-y-2">
234 <BranchMenu base={base} gitRef={gitRef} path={path} branches={list} />
235 {path ? (
236 <Breadcrumbs base={base} repo={repo.name} gitRef={gitRef} path={path} />
237 ) : (
238 <>
239 {branches && (
240 <Link to={`${base}/branches`} className="inline-flex items-center gap-1.5 text-sm text-muted hover:text-accent">
241 <GitBranch size={14} className="text-faint" />
242 <span className="font-medium text-fg">{branches.length}</span>
243 {branches.length === 1 ? "branch" : "branches"}
244 </Link>
245 )}
246 <Link to={`${base}/tags`} className="inline-flex items-center gap-1.5 text-sm text-muted hover:text-accent">
247 <TagIcon size={14} className="text-faint" />
248 Tags
249 </Link>
250 <SearchCode repo={full} />
251 <CodeButton path={full} gitRef={gitRef} />
252 </>
253 )}
254 </div>
255 );
256}
257
258/**
259 * The files of a directory, each with the commit that last changed it.
260 * `last` is undefined while those are still being read (the column holds
261 * its place), and null when they could not be.
262 */
263function FileRows({
264 base,
265 gitRef,
266 prefix,
267 entries,
268 last,
269}: {
270 base: string;
271 gitRef: string;
272 prefix: string;
273 entries: Tree["entries"];
274 last: FileCommits | null | undefined;
275}) {
276 const byName = new Map((last?.entries ?? []).map((entry) => [entry.name, entry.commit]));
277 return (
278 // Busy while each entry's last commit is on its way; its cells keep their size.
279 <ul aria-busy={last === undefined || undefined} className="divide-y divide-line text-sm">
280 {entries.map((entry) => {
281 const isTree = entry.kind === "tree";
282 const Icon = isTree ? Folder : entry.kind === "gitlink" ? FolderGit2 : File;
283 const commit = byName.get(entry.name);
284 return (
285 <li key={entry.name} className="grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 px-4 transition-colors hover:bg-surface sm:grid-cols-[minmax(0,14rem)_minmax(0,1fr)_auto] lg:grid-cols-[minmax(0,18rem)_minmax(0,1fr)_auto]">
286 {/* The whole row's height is the link's: a 44px target on a phone. */}
287 <Link
288 to={`${base}/${isTree ? "tree" : "blob"}/${gitRef}/${prefix}${encodeURIComponent(entry.name)}`}
289 className="flex min-h-11 min-w-0 items-center gap-3 py-2 hover:text-accent hover:underline sm:min-h-0"
290 >
291 <Icon size={15} className={`shrink-0 ${isTree ? "text-accent-dim" : "text-faint"}`} />
292 <span className="truncate font-mono text-[0.8125rem]">{entry.name}</span>
293 </Link>
294 <span className="hidden min-w-0 sm:block">
295 {commit ? (
296 <Hint label={commit.message.split("\n")[0]}>
297 <Link to={`${base}/commit/${commit.hash}`} className="block animate-fade-in truncate text-muted hover:text-fg hover:underline">
298 {commit.message.split("\n")[0]}
299 </Link>
300 </Hint>
301 ) : last === undefined ? (
302 <SkeletonLine width="10rem" barClassName="max-w-full" />
303 ) : null}
304 </span>
305 <span className="text-right text-xs whitespace-nowrap text-faint">
306 {commit ? <span className="animate-fade-in"><TimeAgo at={commit.authoredAt} /></span> : last === undefined ? <SkeletonLine className="justify-end" barClassName="w-12" /> : null}
307 </span>
308 </li>
309 );
310 })}
311 </ul>
312 );
313}
314
315export function TreeView({
316 tree,
317 branches = null,
318 lastCommits = null,
319 checks = null,
320 about = null,
321 canPush = false,
322 homepage = null,
323 deployments = null,
324}: {
325 tree: Tree;
326 branches?: Branch[] | null;
327 /** Each entry's last commit, streamed in after the list. */
328 lastCommits?: Promise<FileCommits | null> | null;
329 /** The latest commit's checks, streamed in. */
330 checks?: ChecksSource;
331 /** The About beside the files, at the root: streamed in after them. */
332 about?: AboutData | null;
333 /** Whether the viewer may push, for the About's "Create a new release". */
334 canPush?: boolean;
335 /** The project's homepage; the repository's website when null. */
336 homepage?: string | null;
337 /** The repository's environments, for the About's Deployments section. */
338 deployments?: DeploymentEnvironments | null;
339}) {
340 const { repo, ref, path, head, entries, readme } = tree;
341 const base = `/${repo.namespace}/${repo.name}`;
342 const prefix = path ? `${encodePath(path)}/` : "";
343 const cloneUrl = `${useAddresses().site}${base}.git`;
344
345 if (!head) {
346 return (
347 <Card className="mx-auto max-w-2xl p-8">
348 <h2 className="text-lg font-semibold tracking-tight">
349 This repository is empty
350 </h2>
351 <p className="mt-1 text-sm text-muted">
352 Push an existing project to it. Use an access token as the password.
353 </p>
354 <div className="mt-5 space-y-2">
355 <CopyLine prompt text={`git remote add g1t ${cloneUrl}`} />
356 <CopyLine prompt text={`git push -u g1t ${ref}`} />
357 </div>
358 </Card>
359 );
360 }
361
362 return (
363 <div className={path ? "" : "grid gap-8 lg:grid-cols-[1fr_17rem]"}>
364 <div className="min-w-0">
365 <CodeBar base={base} repo={repo} gitRef={ref} path={path} branches={branches} />
366 <Card tone="plain" className="overflow-hidden">
367 <CommitBar commit={head} base={base} checks={checks} />
368 {lastCommits ? (
369 <Suspense fallback={<FileRows base={base} gitRef={ref} prefix={prefix} entries={entries} last={undefined} />}>
370 <Await resolve={lastCommits} errorElement={<FileRows base={base} gitRef={ref} prefix={prefix} entries={entries} last={null} />}>
371 {(last) => <FileRows base={base} gitRef={ref} prefix={prefix} entries={entries} last={last} />}
372 </Await>
373 </Suspense>
374 ) : (
375 <FileRows base={base} gitRef={ref} prefix={prefix} entries={entries} last={null} />
376 )}
377 </Card>
378
379 {readme?.text != null && (
380 <Card asChild tone="plain" className="mt-6 scroll-mt-20 overflow-hidden">
381 <section id="readme">
382 <h2 className="flex items-center gap-2 border-b border-line bg-surface px-4 py-2.5 text-sm font-medium">
383 <BookOpen size={15} className="text-faint" />
384 {readme.name}
385 </h2>
386 <div className="p-4 sm:p-6">
387 {/\.(md|markdown)$/i.test(readme.name) ? (
388 <Markdown
389 source={readme.text}
390 repo={{ namespace: repo.namespace, name: repo.name }}
391 // Relative links in a README point into the repository,
392 // and its pictures at the files of the commit shown.
393 base={`/${repo.namespace}/${repo.name}/blob/${ref}${path ? `/${path}` : ""}`}
394 rawBase={`/${repo.namespace}/${repo.name}/raw/${head.hash}${path ? `/${encodePath(path)}` : ""}`}
395 />
396 ) : (
397 <pre className="whitespace-pre-wrap text-sm"><code>{readme.text}</code></pre>
398 )}
399 </div>
400 </section>
401 </Card>
402 )}
403 </div>
404
405 {!path && (
406 <RepoAboutPanel
407 repo={repo}
408 gitRef={ref}
409 readme={Boolean(readme)}
410 data={about ?? { about: null, watchers: null, packages: null, tags: null }}
411 canPush={canPush}
412 homepage={homepage}
413 deployments={<DeploymentsPanel base={base} summary={deployments} className="border-t border-line pt-4" />}
414 />
415 )}
416 </div>
417 );
418}
419
420export function BlobView({
421 blob,
422 html,
423 blame,
424 branches = null,
425 notice,
426 marked,
427}: {
428 /** For the branch menu; it shows the current branch alone without them. */
429 branches?: Branch[] | null;
430 /** Said above the file, such as what is wrong with it. */
431 notice?: ReactNode;
432 /** Lines to mark as having a problem, 1-based. */
433 marked?: readonly number[];
434 blob: Blob;
435 /** Syntax-highlighted HTML per line, when the language is known. */
436 html: string[] | null;
437 /** Shown instead of the plain file when asked for, with HTML per line. */
438 blame?: { blame: ShownBlame; lines: string[] | null } | null;
439}) {
440 const { repo, ref, path, size, text } = blob;
441 const lines = text?.replace(/\n$/, "").split("\n");
442 const base = `/${repo.namespace}/${repo.name}`;
443 // The file as it is, on the usercontent origin (routes/repo/raw.ts).
444 const raw = `${base}/raw/${encodeURIComponent(ref)}/${encodePath(path)}`;
445 const toggle = (label: string, on: boolean, search: string) => (
446 <Link
447 to={{ search }}
448 preventScrollReset
449 className={`rounded px-2.5 py-1.5 transition-colors sm:px-2 sm:py-0.5 ${on ? "bg-raised text-fg" : "hover:text-fg"}`}
450 >
451 {label}
452 </Link>
453 );
454 return (
455 <div>
456 <div className="mb-4 flex flex-wrap items-center gap-x-3 gap-y-2">
457 <BranchMenu
458 base={base}
459 gitRef={ref}
460 path={path}
461 branches={branches && branches.length > 0 ? branches : [{ name: ref, hash: "" }]}
462 view="blob"
463 />
464 <Breadcrumbs base={base} repo={repo.name} gitRef={ref} path={path} />
465 </div>
466 {notice}
467 <Card tone="plain" className="overflow-hidden">
468 <div className="flex items-center gap-3 border-b border-line bg-surface px-3 py-2 text-xs text-muted sm:px-4 sm:py-2.5">
469 {lines && <span>{lines.length.toLocaleString("en-US")} lines</span>}
470 <span className="hidden min-[400px]:inline">{size.toLocaleString("en-US")} bytes</span>
471 <span className="ml-auto flex items-center gap-2">
472 {lines && (
473 <span className="flex rounded-md border border-line p-0.5">
474 {toggle("Code", !blame, "")}
475 {toggle("Blame", Boolean(blame), "?blame=1")}
476 </span>
477 )}
478 <a href={raw} className="rounded-md border border-line px-2.5 py-2 transition-colors hover:text-fg sm:px-2 sm:py-1">
479 Raw
480 </a>
481 </span>
482 </div>
483 {blame && lines ? (
484 <BlameView base={base} path={path} lines={lines} html={blame.lines} blame={blame.blame} />
485 ) : lines ? (
486 <CodeLines lines={lines} html={html} marked={marked} />
487 ) : isImagePath(path) ? (
488 <div className="flex justify-center bg-[repeating-conic-gradient(var(--color-raised)_0_25%,transparent_0_50%)] bg-[length:16px_16px] p-6">
489 <img src={raw} alt={path.split("/").pop() ?? path} className="max-h-[70vh] max-w-full" />
490 </div>
491 ) : (
492 <p className="p-6 text-sm text-muted">
493 This file is binary or too large to show.{" "}
494 <a href={raw} className="text-accent hover:underline">
495 View it raw
496 </a>
497 .
498 </p>
499 )}
500 </Card>
501 </div>
502 );
503}