Skip to content
171 linesCodeBlameRaw

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.

Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request1import { ArrowLeft, GitCommitHorizontal, GitPullRequest } from "lucide-react";
2import { Form, Link } from "react-router";
3
4import type { Comparison } from "@g1t/contracts";
5
6import type { Route } from "./+types/compare";
7import { DiffView } from "../../components/diff-view";
Merge commit authors and hovercards: commits show their accounts, every name opens a card (repos 0016)8import { CommitAvatars, CommitNames } from "../../components/commit-person";
9import { ButtonLink, EmptyState, TimeAgo } from "../../components/ui";
Merge branch 'worktree-agent-ae1299e92e4462012'10import { Hint } from "../../components/ui/hint";
Chat controls, public profiles, shadcn selects, and no Docs tab in a project11import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../components/ui/select";
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request12import { page } from "../../lib/meta";
Merge commit authors and hovercards: commits show their accounts, every name opens a card (repos 0016)13import { showCommits } from "../../lib/commit-people.server";
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request14import { repos } from "../../lib/services.server";
15import { getViewer, unwrap } from "../../lib/session.server";
16
17/** How far back each side's history is read to find what head has that base does not. */
18const HEAD_DEPTH = 100;
19const BASE_DEPTH = 300;
20
21export function meta({ params, ...args }: Route.MetaArgs) {
22 const range = params["*"] ? ` ${params["*"]}` : "";
23 return page(args, { title: `Compare${range} · ${params.owner}/${params.repo} · g1t` });
24}
25
26/** `main...feature` into its two sides; either may be missing. */
27function sides(range: string): { base: string | null; head: string | null } {
28 const at = range.indexOf("...");
29 if (at === -1) return { base: null, head: range || null };
30 return { base: range.slice(0, at) || null, head: range.slice(at + 3) || null };
31}
32
33export async function loader({ params, context, request }: Route.LoaderArgs) {
34 const path = { namespace: params.owner, name: params.repo };
35 const viewer = getViewer(context);
36 const url = new URL(request.url);
37 // The pickers submit as ?base=&head=; the address keeps base...head.
38 if (url.searchParams.has("base") || url.searchParams.has("head")) {
39 const base = url.searchParams.get("base") ?? "";
40 const head = url.searchParams.get("head") ?? "";
41 return Response.redirect(new URL(`/${params.owner}/${params.repo}/compare/${base}...${head}`, url).toString(), 302);
42 }
43 const [repo, list] = await Promise.all([repos.get(path, viewer), repos.branches(path, viewer)]);
44 const found = unwrap(repo);
45 const branches = unwrap(list)
46 .map((branch) => branch.name)
47 .filter((name) => !name.startsWith("g1t-queue/"));
48 const asked = sides(params["*"] ?? "");
49 const base = asked.base ?? found.defaultBranch;
50 const head = asked.head;
51 if (!head || head === base) return { base, head, branches, comparison: null, commits: [], same: head === base };
Compare diffs from where the two branches last agreed, as a pull request would52 const [headLog, baseLog] = await Promise.all([repos.log(path, viewer, head, HEAD_DEPTH), repos.log(path, viewer, base, BASE_DEPTH)]);
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request53 if (!headLog.ok || headLog.value.length === 0) throw new Response(`There is no branch, tag or commit named ${head}.`, { status: 404 });
54 // What head has that base does not: its history down to the first commit base also has.
55 const onBase = new Set(baseLog.ok ? baseLog.value.map((commit) => commit.hash) : []);
56 const shared = headLog.value.findIndex((commit) => onBase.has(commit.hash));
Merge commit authors and hovercards: commits show their accounts, every name opens a card (repos 0016)57 // With their people, in one identity call.
58 const commits = (await showCommits(shared === -1 ? headLog.value : headLog.value.slice(0, shared))).map((commit) => ({
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request59 hash: commit.hash,
60 message: commit.message.split("\n")[0] ?? "",
Merge commit authors and hovercards: commits show their accounts, every name opens a card (repos 0016)61 author: commit.author,
62 coAuthors: commit.coAuthors,
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request63 at: commit.authoredAt,
64 }));
Compare diffs from where the two branches last agreed, as a pull request would65 // The changes from where the two last agreed, as a pull request would
66 // bring them: what base gained since is not head taking it away. When
67 // that point is past what was read, the two tips are compared.
68 const mergeBase = shared === -1 ? base : headLog.value[shared]!.hash;
69 const compared = await repos.compare(found.id, viewer, mergeBase, head);
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request70 const comparison: Comparison | null = compared.ok ? compared.value : null;
71 return { base, head, branches, comparison, commits, same: false };
72}
73
74function RefPicker({ name, label, value, branches }: { name: string; label: string; value: string | null; branches: string[] }) {
75 const options = value && !branches.includes(value) ? [value, ...branches] : branches;
76 return (
Chat controls, public profiles, shadcn selects, and no Docs tab in a project77 <div className="inline-flex items-center gap-2 text-sm">
78 <span className="text-muted" id={`${name}-label`}>
79 {label}
80 </span>
81 <Select key={value ?? ""} name={name} defaultValue={value ?? undefined}>
82 <SelectTrigger size="sm" aria-labelledby={`${name}-label`} className="w-auto max-w-56 font-mono">
83 <SelectValue placeholder="Choose a branch" />
84 </SelectTrigger>
85 <SelectContent>
86 {options.map((option) => (
87 <SelectItem key={option} value={option} className="font-mono text-[0.8125rem]">
88 {option}
89 </SelectItem>
90 ))}
91 </SelectContent>
92 </Select>
93 </div>
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request94 );
95}
96
97export default function Compare({ loaderData, params }: Route.ComponentProps) {
98 const { base, head, branches, comparison, commits, same } = loaderData;
99 const repoBase = `/${params.owner}/${params.repo}`;
100 return (
101 <div className="space-y-5">
102 <div>
103 <h2 className="text-lg font-semibold tracking-tight">Compare changes</h2>
104 <p className="mt-1 text-sm text-muted">Choose two branches to see what one has that the other does not, then open a pull request for it.</p>
105 </div>
106 <Form method="get" className="flex flex-wrap items-center gap-3 rounded-xl border border-line bg-surface px-4 py-3">
107 <RefPicker name="base" label="base" value={base} branches={branches} />
108 <ArrowLeft size={14} className="text-faint" aria-hidden="true" />
109 <RefPicker name="head" label="compare" value={head} branches={branches} />
110 <button
111 type="submit"
112 className="h-8 rounded-md border border-line px-3 text-sm text-fg/90 transition-colors hover:border-line-strong hover:bg-raised"
113 >
114 Compare
115 </button>
116 {head && !same && commits.length > 0 && (
Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar117 <ButtonLink
118 to={`${repoBase}/pulls/new?branch=${encodeURIComponent(head)}&base=${encodeURIComponent(base)}`}
119 variant="accent"
120 >
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request121 <GitPullRequest size={15} />
122 Open a pull request
123 </ButtonLink>
124 )}
125 </Form>
126
127 {!head ? (
128 <EmptyState title="Pick a branch to compare">Its commits and changes against {base} show here.</EmptyState>
129 ) : same ? (
130 <EmptyState title="Nothing to compare">Both sides are {base}. Choose another branch to compare with it.</EmptyState>
131 ) : commits.length === 0 ? (
132 <EmptyState title={`${base} has everything ${head} has`}>There is nothing on {head} to bring into {base}.</EmptyState>
133 ) : (
134 <>
135 <section>
136 <h3 className="mb-2 text-sm font-medium text-muted">
137 {commits.length === HEAD_DEPTH ? `${HEAD_DEPTH}+` : commits.length} {commits.length === 1 ? "commit" : "commits"}
138 </h3>
139 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface">
140 {commits.map((commit) => (
141 <li key={commit.hash} className="flex items-center gap-3 px-4 py-2.5 text-sm">
142 <GitCommitHorizontal size={15} className="shrink-0 text-faint" />
Merge commit authors and hovercards: commits show their accounts, every name opens a card (repos 0016)143 <CommitAvatars commit={commit} size={16} max={2} />
Merge branch 'worktree-agent-ae1299e92e4462012'144 <Hint label={commit.message}>
145 <Link to={`${repoBase}/commit/${commit.hash}`} className="min-w-0 grow truncate hover:text-accent">
146 {commit.message}
147 </Link>
148 </Hint>
Merge commit authors and hovercards: commits show their accounts, every name opens a card (repos 0016)149 <span className="hidden shrink-0 text-xs text-muted sm:inline">
150 <CommitNames commit={commit} className="hover:text-fg" />
151 </span>
Compare: any two branches, the commits one has that the other does not, and the diff, with Open a pull request152 <Link to={`${repoBase}/commit/${commit.hash}`} className="shrink-0 font-mono text-xs text-faint hover:text-fg">
153 {commit.hash.slice(0, 7)}
154 </Link>
155 <span className="shrink-0 text-xs text-faint">
156 <TimeAgo at={commit.at} />
157 </span>
158 </li>
159 ))}
160 </ul>
161 </section>
162 {comparison ? (
163 <DiffView comparison={comparison} fileBase={`${repoBase}/blob/${encodeURIComponent(head)}`} empty="The two sides have the same files." />
164 ) : (
165 <p className="text-sm text-muted">The changes could not be read just now. The commits above are what {head} adds.</p>
166 )}
167 </>
168 )}
169 </div>
170 );
171}

This file's history is long; its oldest lines are credited to the oldest commit read.