Skip to content

g1t/apps/web/app/components/merge-box.tsx

878 lines34,079 bytesCodeBlame
1/**
2 * The parts of a pull request's merge box that say what stands between it
3 * and a merge: the checks the branch requires and every other check, job by
4 * job, and whether
5 * the change merges cleanly into its target, with what to do when not.
6 */
7import {
8 Check,
9 ChevronRight,
10 CircleAlert,
11 CircleCheck,
12 CircleX,
13 Copy,
14 GitCommitHorizontal,
15 History,
16 LoaderCircle,
17 RotateCw,
18 Sparkles,
19 Terminal,
20 TriangleAlert,
21} from "lucide-react";
22import { useEffect, useMemo, useState } from "react";
23import { Form, Link } from "react-router";
24
25import type { CheckResult, CheckRun, CommitStatus, Job, Mergeable, PullBranchUpdate, Pull, RequiredCheck } from "@g1t/contracts";
26
27import { useAddresses } from "../lib/addresses";
28import { catchUpPhase, catchUpRun, catchUpTitle, catchUpWhy } from "../lib/catch-up";
29import { duration } from "./actions";
30import { Elapsed, type Live, useRuns } from "./agents";
31import { Button, CopyLine, ErrorText, SubmitButton, TimeAgo } from "./ui";
32import { Hint } from "./ui/hint";
33import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
34import { SkeletonLine } from "./ui/skeleton";
35
36// --- Output ---------------------------------------------------------------
37
38/** What the sandbox puts first when it kept only the end of the output. */
39const CUT_SHORT = "… (earlier output not shown)";
40
41/** One run of text in one style, from a line with ANSI colour codes. */
42type Segment = { text: string; className: string };
43
44const ANSI_COLOURS: Record<number, string> = {
45 30: "text-faint",
46 31: "text-danger",
47 32: "text-success",
48 33: "text-warn",
49 34: "text-info",
50 35: "text-merged",
51 36: "text-info",
52 37: "text-fg",
53 90: "text-faint",
54 91: "text-danger",
55 92: "text-success",
56 93: "text-warn",
57 94: "text-info",
58 95: "text-merged",
59 96: "text-info",
60 97: "text-fg",
61};
62
63/**
64 * A line's text split where its colour changes. Colours and bold are kept;
65 * every other escape sequence (cursor movement, erasing, links) is dropped.
66 */
67export function ansiSegments(line: string): Segment[] {
68 const segments: Segment[] = [];
69 let colour = "";
70 let bold = false;
71 // SGR sequences are kept apart; anything else that starts with ESC goes.
72 const pattern = /\x1b\[([0-9;]*)m|\x1b\[[0-9;?]*[A-Za-z]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[@-_]/g;
73 let at = 0;
74 const push = (text: string) => {
75 if (text) segments.push({ text, className: [colour, bold ? "font-semibold" : ""].filter(Boolean).join(" ") });
76 };
77 for (const match of line.matchAll(pattern)) {
78 push(line.slice(at, match.index));
79 at = match.index + match[0].length;
80 if (match[1] === undefined) continue;
81 const codes = match[1] === "" ? [0] : match[1].split(";").map(Number);
82 for (const code of codes) {
83 if (code === 0) {
84 colour = "";
85 bold = false;
86 } else if (code === 1) bold = true;
87 else if (code === 22) bold = false;
88 else if (code === 39) colour = "";
89 else if (ANSI_COLOURS[code]) colour = ANSI_COLOURS[code];
90 }
91 }
92 push(line.slice(at));
93 return segments;
94}
95
96/** Text without any ANSI escape sequences, for copying. */
97export function stripAnsi(text: string): string {
98 return ansiSegments(text.replace(/\r\n/g, "\n"))
99 .map((segment) => segment.text)
100 .join("");
101}
102
103/** Lines as a terminal would show them: a carriage return starts the line over. */
104function terminalLines(text: string): string[] {
105 return text
106 .replace(/\r\n/g, "\n")
107 .split("\n")
108 .map((line) => {
109 const parts = line.split("\r");
110 return parts[parts.length - 1] || parts.filter(Boolean).pop() || "";
111 });
112}
113
114/**
115 * A command's output as a log: numbered lines, colours rendered, a copy
116 * button, and a note when only the end of it was kept.
117 */
118export function LogViewer({ text }: { text: string }) {
119 const cut = text.startsWith(CUT_SHORT);
120 const body = cut ? text.slice(CUT_SHORT.length).replace(/^\n/, "") : text;
121 const lines = useMemo(() => {
122 const all = terminalLines(body);
123 while (all.length > 0 && all[all.length - 1].trim() === "") all.pop();
124 return all;
125 }, [body]);
126 const [copied, setCopied] = useState(false);
127 if (lines.length === 0) {
128 return <p className="px-3 py-2 font-mono text-xs text-faint">No output.</p>;
129 }
130 return (
131 <div className="relative">
132 <Hint label="Copy the output">
133 <button
134 type="button"
135 aria-label="Copy the output"
136 className="absolute top-1.5 right-3 z-10 rounded-md border border-line bg-surface p-1.5 text-faint transition-colors hover:text-fg"
137 onClick={() => {
138 void navigator.clipboard.writeText(stripAnsi(body));
139 setCopied(true);
140 setTimeout(() => setCopied(false), 1500);
141 }}
142 >
143 {copied ? <Check size={13} className="text-success" /> : <Copy size={13} />}
144 </button>
145 </Hint>
146 {cut && (
147 <p className="border-b border-line px-3 py-1.5 text-xs text-faint">
148 Showing the end of the output. Earlier lines were not kept.
149 </p>
150 )}
151 <div className="max-h-96 overflow-auto py-1.5 font-mono text-xs leading-5">
152 {lines.map((line, index) => (
153 <div key={index} className="flex gap-3 pr-12 pl-2 hover:bg-raised/40">
154 <span className="w-9 shrink-0 text-right text-faint/70 select-none">{index + 1}</span>
155 <span className="min-w-0 break-all whitespace-pre-wrap text-fg/85">
156 {ansiSegments(line).map((segment, at) =>
157 segment.className ? (
158 <span key={at} className={segment.className}>
159 {segment.text}
160 </span>
161 ) : (
162 segment.text
163 ),
164 )}
165 </span>
166 </div>
167 ))}
168 </div>
169 </div>
170 );
171}
172
173// --- Checks ---------------------------------------------------------------
174
175function seconds(ms: number): string {
176 if (ms < 1000) return `${ms}ms`;
177 const s = Math.round(ms / 1000);
178 return s < 60 ? `${(ms / 1000).toFixed(ms < 10_000 ? 1 : 0)}s` : `${Math.floor(s / 60)}m ${s % 60}s`;
179}
180
181type Standing = "passed" | "failed" | "running" | "queued" | "skipped";
182
183function StandingIcon({ standing, size = 15 }: { standing: Standing; size?: number }) {
184 if (standing === "passed") return <CircleCheck size={size} className="shrink-0 text-success" aria-label="Passed" />;
185 if (standing === "failed") return <CircleX size={size} className="shrink-0 text-danger" aria-label="Failed" />;
186 if (standing === "running")
187 return <LoaderCircle size={size} className="shrink-0 animate-spin text-warn" aria-label="Running" />;
188 if (standing === "skipped") return <CircleAlert size={size} className="shrink-0 text-faint" aria-label="Skipped" />;
189 return <LoaderCircle size={size} className="shrink-0 text-faint" aria-label="Queued" />;
190}
191
192/** The id of the workflow run a status links to, if it links to one. */
193export function runIdOf(status: CommitStatus): string | null {
194 return /\/actions\/runs\/([^/?#]+)/.exec(status.targetUrl ?? "")?.[1] ?? null;
195}
196
197/** Where a job stands, in the box's terms. */
198function jobStanding(job: Job): Standing {
199 if (job.status !== "completed") return job.status === "in_progress" || job.status === "calling" ? "running" : "queued";
200 if (job.conclusion === "success") return "passed";
201 if (job.conclusion === "skipped" || job.conclusion === "cancelled") return "skipped";
202 return "failed";
203}
204
205function statusStanding(status: CommitStatus): Standing {
206 if (status.state === "pending") return "running";
207 return status.state === "success" ? "passed" : "failed";
208}
209
210/** One row of the checks list: an icon, a name, how long, and a way in. */
211function CheckLine({
212 standing,
213 name,
214 detail,
215 time,
216 timing = false,
217 to,
218}: {
219 standing: Standing;
220 name: React.ReactNode;
221 detail?: React.ReactNode;
222 time?: string;
223 /** Its jobs and their times are still being read. */
224 timing?: boolean;
225 to?: string;
226}) {
227 return (
228 <div className="flex items-center gap-2.5 px-4 py-2 text-sm">
229 <StandingIcon standing={standing} />
230 <span className="min-w-0 grow truncate">
231 {name}
232 {detail && <span className="text-muted"> — {detail}</span>}
233 </span>
234 {time && <span className="shrink-0 animate-fade-in font-mono text-xs text-faint">{time}</span>}
235 {timing && <SkeletonLine className="w-10 shrink-0 text-xs" />}
236 {to && (
237 <Link to={to} className="shrink-0 text-xs text-muted hover:text-fg hover:underline">
238 Details
239 </Link>
240 )}
241 </div>
242 );
243}
244
245/** A command from a run recorded before checks were workflows: what it printed, open when it failed. */
246function CommandResult({ result }: { result: CheckResult }) {
247 const timedOut = result.exitCode == null && !result.passed;
248 return (
249 <details className="group" open={!result.passed}>
250 <summary className="flex cursor-pointer list-none items-center gap-2.5 px-4 py-2 text-sm hover:bg-raised/40">
251 <StandingIcon standing={result.passed ? "passed" : "failed"} />
252 <span className="min-w-0 grow truncate">
253 <code className="font-mono text-xs">{result.command}</code>
254 {!result.passed && (
255 <span className="text-muted">
256 {" — "}
257 {timedOut ? "stopped for taking too long" : `exited with code ${result.exitCode}`}
258 </span>
259 )}
260 </span>
261 <span className="shrink-0 font-mono text-xs text-faint">{seconds(result.durationMs)}</span>
262 <ChevronRight size={14} className="shrink-0 text-faint transition-transform group-open:rotate-90" />
263 </summary>
264 <div className="mx-4 mb-3 overflow-hidden rounded-lg border border-line bg-bg">
265 <LogViewer text={result.output} />
266 </div>
267 </details>
268 );
269}
270
271/** Where a required check stands, in the box's terms. */
272function requiredStanding(check: RequiredCheck): Standing {
273 if (check.state === "success") return "passed";
274 if (check.state === "failure") return "failed";
275 return check.state === "pending" ? "running" : "queued";
276}
277
278/** How the required checks stand together: "2 of 3 passing", and what holds the rest. */
279export function requiredSummary(required: RequiredCheck[]): { title: string; sub: string; standing: Standing } {
280 const count = (state: RequiredCheck["state"]) => required.filter((check) => check.state === state).length;
281 const passing = count("success");
282 const failing = count("failure");
283 const running = count("pending");
284 const expected = count("expected");
285 const sub = [
286 failing > 0 && `${failing} failing`,
287 running > 0 && `${running} running`,
288 expected > 0 && `${expected} not reported yet`,
289 ]
290 .filter(Boolean)
291 .join(", ");
292 return {
293 title: `Required checks: ${passing} of ${required.length} passing`,
294 sub,
295 standing: failing > 0 ? "failed" : running + expected > 0 ? "running" : "passed",
296 };
297}
298
299/** The repository-relative address of a status's link, when it is on g1t. */
300function onSite(url: string | null): string | undefined {
301 if (!url) return undefined;
302 const path = url.replace(/^https:\/\/g1t\.sh(?=\/)/, "");
303 return path.startsWith("/") ? path : undefined;
304}
305
306/**
307 * Every check on a pull request, at a glance and in detail: first the
308 * checks the default branch requires, each as it stands on the head (one
309 * nothing has reported is waited for), then every workflow run on the
310 * head, job by job. A record of the merge queue taking it out comes last.
311 */
312export function ChecksSection({
313 run,
314 required,
315 statuses,
316 jobs,
317 pull,
318 base,
319 earlier,
320 canRerunWorkflows,
321 settingsUrl,
322 error,
323 loading = false,
324}: {
325 /** Set when the merge queue took it out, or for a run from before checks were workflows. */
326 run: CheckRun | null;
327 required: RequiredCheck[];
328 statuses: CommitStatus[];
329 /** Each workflow run's jobs, by run id, where they could be read. */
330 jobs: Record<string, Job[]>;
331 pull: Pull;
332 /** The repository's path, `/<owner>/<repo>`. */
333 base: string;
334 earlier: CheckRun[];
335 canRerunWorkflows: boolean;
336 /** Where the required checks are chosen, for those who may. */
337 settingsUrl?: string | null;
338 error?: string | null;
339 /** The workflow runs' jobs are still being read: their rows wait, busy. */
340 loading?: boolean;
341}) {
342 if (!run && statuses.length === 0 && required.length === 0) return null;
343
344 // Every workflow row, so they can be counted the same way they are shown.
345 const workflow: Standing[] = statuses.flatMap((status) => {
346 const id = runIdOf(status);
347 const theirs = id ? jobs[id] : undefined;
348 return theirs && theirs.length > 0 ? theirs.map(jobStanding) : [statusStanding(status)];
349 });
350 const failed = workflow.filter((standing) => standing === "failed").length;
351 const pending = workflow.filter((standing) => standing === "running" || standing === "queued").length;
352 const passed = workflow.filter((standing) => standing === "passed").length;
353 const skipped = workflow.filter((standing) => standing === "skipped").length;
354 const total = workflow.length;
355 const queueFailed = run?.status === "failed" && run.results.length === 0;
356
357 const summary = required.length > 0 ? requiredSummary(required) : null;
358 const headline = summary
359 ? summary.title
360 : failed > 0
361 ? `${failed} of ${total} ${total === 1 ? "check" : "checks"} failed`
362 : pending > 0
363 ? `${pending} of ${total} ${total === 1 ? "check is" : "checks are"} still running`
364 : total > 0
365 ? "All checks have passed"
366 : "Checks";
367 const others = [
368 failed > 0 && `${failed} failed`,
369 pending > 0 && `${pending} running`,
370 passed > 0 && `${passed} passed`,
371 skipped > 0 && `${skipped} skipped or cancelled`,
372 ]
373 .filter(Boolean)
374 .join(", ");
375 const sub = summary
376 ? [summary.sub, others && `all checks: ${others}`].filter(Boolean).join(" · ")
377 : [others, "none are required, so none hold the merge"].filter(Boolean).join(" · ");
378 const standing: Standing = summary
379 ? summary.standing
380 : failed > 0
381 ? "failed"
382 : pending > 0
383 ? "running"
384 : "passed";
385
386 const commitPath = (sha: string) =>
387 pull.fork ? `/${pull.fork.namespace}/${pull.fork.name}/commit/${sha}` : `${base}/commit/${sha}`;
388 const older = run != null && pull.headCommit != null && run.headCommit !== pull.headCommit;
389 const failedRuns = statuses.filter((status) => statusStanding(status) === "failed" && runIdOf(status));
390
391 return (
392 <div aria-busy={loading || undefined}>
393 <div className="flex items-start gap-3 px-4 py-3">
394 <span className="mt-0.5 shrink-0">
395 {standing === "failed" || queueFailed ? (
396 <CircleX size={16} className="text-danger" />
397 ) : standing === "running" ? (
398 <LoaderCircle size={16} className="animate-spin text-warn" />
399 ) : (
400 <CircleCheck size={16} className="text-success" />
401 )}
402 </span>
403 <div className="min-w-0 grow">
404 <p className="text-sm font-medium">{headline}</p>
405 {sub && <p className="mt-0.5 text-xs text-muted">{sub}</p>}
406 {!summary && settingsUrl && (
407 <p className="mt-0.5 text-xs text-muted">
408 <Link to={settingsUrl} className="text-fg hover:underline">
409 Choose required checks
410 </Link>{" "}
411 to hold merges until they pass.
412 </p>
413 )}
414 </div>
415 <div className="flex shrink-0 flex-wrap items-center justify-end gap-2">
416 {canRerunWorkflows &&
417 failedRuns.map((status) => (
418 <Form method="post" key={status.context}>
419 <input type="hidden" name="action" value="rerun-workflow" />
420 <input type="hidden" name="run" value={runIdOf(status) ?? ""} />
421 <Hint label={`Re-run the failed jobs of ${status.context}`}>
422 <SubmitButton
423 variant="quiet"
424 match={{ action: "rerun-workflow", run: runIdOf(status) ?? "" }}
425 pending="Re-running…"
426 >
427 <RotateCw size={13} />
428 Re-run failed jobs{failedRuns.length > 1 ? ` of ${status.context}` : ""}
429 </SubmitButton>
430 </Hint>
431 </Form>
432 ))}
433 </div>
434 </div>
435 {error && (
436 <div className="px-4 pb-2">
437 <ErrorText>{error}</ErrorText>
438 </div>
439 )}
440
441 <div className="divide-y divide-line border-t border-line">
442 {required.length > 0 && (
443 <section aria-label="Required checks" className="py-1">
444 <p className="px-4 pt-1.5 pb-1 text-xs font-medium text-muted">Required</p>
445 {required.map((check) => (
446 <CheckLine
447 key={check.name}
448 standing={requiredStanding(check)}
449 name={
450 <>
451 {check.name}
452 <span className="ml-1.5 rounded-full border border-line px-1.5 py-px text-[0.625rem] text-faint">Required</span>
453 </>
454 }
455 detail={
456 check.state === "expected"
457 ? "Waiting for status to be reported"
458 : (check.description ?? (check.state === "pending" ? "running" : undefined))
459 }
460 to={onSite(check.targetUrl)}
461 />
462 ))}
463 </section>
464 )}
465
466 {statuses.length > 0 && (
467 <section aria-label="All checks" className="py-1">
468 <p className="px-4 pt-1.5 pb-1 text-xs font-medium text-muted">{required.length > 0 ? "All checks" : "Workflows"}</p>
469 {statuses.map((status) => {
470 const id = runIdOf(status);
471 const runPath = id ? `${base}/actions/runs/${id}` : onSite(status.targetUrl);
472 const theirs = id ? jobs[id] : undefined;
473 if (!theirs || theirs.length === 0) {
474 return (
475 <CheckLine
476 key={status.context}
477 standing={statusStanding(status)}
478 name={status.context}
479 detail={status.description ?? undefined}
480 timing={loading && id != null}
481 to={runPath}
482 />
483 );
484 }
485 return theirs.map((job) => (
486 <CheckLine
487 key={`${status.context}-${job.id}`}
488 standing={jobStanding(job)}
489 name={
490 <>
491 <span className="text-muted">{status.context} / </span>
492 {job.name}
493 </>
494 }
495 detail={job.reason ?? (jobStanding(job) === "failed" ? "failed" : undefined)}
496 time={duration(job.startedAt, job.finishedAt) || undefined}
497 to={`${runPath}?job=${job.id}`}
498 />
499 ));
500 })}
501 </section>
502 )}
503
504 {run && (
505 <section aria-label={queueFailed ? "Merge queue" : "Earlier commands"} className="py-1">
506 <p className="flex flex-wrap items-center gap-x-1.5 px-4 pt-1.5 pb-1 text-xs text-faint">
507 <span className="font-medium text-muted">{queueFailed ? "Merge queue" : "Commands from the issue"}</span>
508 <span>· on</span>
509 <Link to={commitPath(run.headCommit)} className="inline-flex items-center gap-1 font-mono hover:text-fg">
510 <GitCommitHorizontal size={12} />
511 {run.headCommit.slice(0, 7)}
512 </Link>
513 {run.finishedAt ? (
514 <span>
515 · <TimeAgo at={run.finishedAt} />
516 </span>
517 ) : (
518 <span>
519 · started <TimeAgo at={run.createdAt} />
520 </span>
521 )}
522 </p>
523 {older && !queueFailed && (
524 <p className="mx-4 my-1.5 flex items-start gap-2 rounded-md bg-warn/10 px-2.5 py-1.5 text-xs text-warn">
525 <TriangleAlert size={13} className="mt-0.5 shrink-0" />
526 These ran on an older commit, before the latest push,{" "}
527 <span className="font-mono">{pull.headCommit?.slice(0, 7)}</span>.
528 </p>
529 )}
530 {run.error && (
531 <p className="mx-4 my-1.5 rounded-lg border border-line bg-bg px-3 py-2 text-sm text-muted">{run.error}</p>
532 )}
533 {run.results.map((result, index) => (
534 <CommandResult key={`${result.command}-${index}`} result={result} />
535 ))}
536 </section>
537 )}
538
539 {earlier.length > 0 && (
540 <details className="group">
541 <summary className="flex cursor-pointer list-none items-center gap-2 px-4 py-2 text-xs text-muted hover:text-fg">
542 <History size={13} />
543 Earlier records ({earlier.length})
544 <ChevronRight size={13} className="transition-transform group-open:rotate-90" />
545 </summary>
546 <ul className="space-y-1 px-4 pb-3 text-xs">
547 {earlier.map((before) => (
548 <li key={before.id} className="flex items-center gap-2 text-muted">
549 <StandingIcon
550 size={13}
551 standing={
552 before.status === "passed"
553 ? "passed"
554 : before.status === "queued" || before.status === "running"
555 ? "queued"
556 : "failed"
557 }
558 />
559 <Link to={commitPath(before.headCommit)} className="font-mono hover:text-fg">
560 {before.headCommit.slice(0, 7)}
561 </Link>
562 <span className="min-w-0 truncate">
563 {before.status === "errored"
564 ? `could not run${before.error ? `: ${before.error}` : ""}`
565 : before.results.length > 0
566 ? `${before.results.filter((result) => result.passed).length} of ${before.results.length} passed`
567 : (before.error ?? before.status)}
568 </span>
569 <span className="ml-auto shrink-0 text-faint">
570 <TimeAgo at={before.createdAt} />
571 </span>
572 </li>
573 ))}
574 </ul>
575 </details>
576 )}
577 </div>
578 </div>
579 );
580}
581
582// --- Mergeability ---------------------------------------------------------
583
584/** How to resolve the conflicts by hand, step by step, for a branch or a fork. */
585export function commandLineSteps(
586 pull: Pull,
587 owner: string,
588 repo: string,
589 defaultBranch: string,
590 files: string[],
591 site = "https://g1t.sh",
592): string[] {
593 const upstream = `${site}/${owner}/${repo}.git`;
594 const add = files.length > 0 && files.length <= 6 ? `git add ${files.join(" ")}` : "git add -A";
595 if (pull.branch) {
596 return [
597 `git fetch origin`,
598 `git checkout ${pull.branch}`,
599 `git merge origin/${defaultBranch}`,
600 `${add} && git commit --no-edit`,
601 `git push origin ${pull.branch}`,
602 ];
603 }
604 const fork = pull.fork!;
605 return [
606 `git clone ${site}/${fork.namespace}/${fork.name}.git && cd ${fork.name}`,
607 `git pull --no-rebase ${upstream} ${defaultBranch}`,
608 `${add} && git commit --no-edit`,
609 `git push`,
610 ];
611}
612
613/**
614 * The branch conflicts with its target: which files, and three ways out —
615 * g1t, the browser (soon), or the command line.
616 */
617export function ConflictsSection({
618 conflicts,
619 pull,
620 owner,
621 repo,
622 defaultBranch,
623 changesUrl,
624 canResolve,
625 resolving,
626 error,
627}: {
628 conflicts: string[];
629 pull: Pull;
630 owner: string;
631 repo: string;
632 defaultBranch: string;
633 /** Where the pull request's changes are shown, to link each file. */
634 changesUrl: string;
635 /** Whether the viewer may have g1t resolve them. */
636 canResolve: boolean;
637 /** Whether asking for it is on its way. */
638 resolving: boolean;
639 error?: string | null;
640}) {
641 const { site } = useAddresses();
642 const steps = commandLineSteps(pull, owner, repo, defaultBranch, conflicts, site);
643 return (
644 <div className="bg-danger/5">
645 <div className="flex gap-3 px-4 py-3">
646 <CircleAlert size={16} className="mt-0.5 shrink-0 text-danger" />
647 <div className="min-w-0 grow">
648 <p className="text-sm font-medium">This branch has conflicts that must be resolved</p>
649 <p className="mt-0.5 text-sm text-muted">
650 Merging <span className="font-mono">{defaultBranch}</span> into it stops on{" "}
651 {conflicts.length === 1 ? "one file" : `${conflicts.length} files`}. It cannot merge until they are resolved.
652 </p>
653 {conflicts.length > 0 && (
654 <>
655 <p className="mt-3 text-xs font-medium text-muted">Conflicting files</p>
656 <ul className="mt-1.5 space-y-1">
657 {conflicts.map((path) => (
658 <li key={path}>
659 <Link
660 to={`${changesUrl}#file-${path}`}
661 className="font-mono text-xs text-fg hover:text-danger hover:underline"
662 >
663 {path}
664 </Link>
665 </li>
666 ))}
667 </ul>
668 </>
669 )}
670 <div className="mt-3 flex flex-wrap items-center gap-2">
671 {canResolve && (
672 <Form method="post">
673 <input type="hidden" name="action" value="update" />
674 <Button variant="primary" type="submit" disabled={resolving}>
675 {resolving ? <LoaderCircle size={14} className="animate-spin" /> : <Sparkles size={14} />}
676 {resolving ? "Starting g1t…" : "Resolve with g1t"}
677 </Button>
678 </Form>
679 )}
680 <Tooltip>
681 <TooltipTrigger asChild>
682 {/* A disabled button gets no pointer events; the span carries the tooltip. */}
683 <span tabIndex={0} className="inline-flex">
684 <Button variant="quiet" type="button" disabled aria-disabled="true">
685 Resolve in the browser
686 <span className="rounded-full border border-line px-1.5 text-[0.625rem] text-faint">Soon</span>
687 </Button>
688 </span>
689 </TooltipTrigger>
690 <TooltipContent>
691 An editor for conflicts in the browser is coming. Until then, have g1t resolve them, or use the
692 command line.
693 </TooltipContent>
694 </Tooltip>
695 </div>
696 {error && (
697 <div className="mt-2">
698 <ErrorText>{error}</ErrorText>
699 </div>
700 )}
701 <details className="group mt-3">
702 <summary className="flex cursor-pointer list-none items-center gap-1.5 text-xs text-muted hover:text-fg">
703 <Terminal size={13} />
704 Resolve on the command line
705 <ChevronRight size={13} className="transition-transform group-open:rotate-90" />
706 </summary>
707 <ol className="mt-2 space-y-2 text-xs text-muted">
708 {steps.map((step, index) => (
709 <li key={step}>
710 <p className="mb-1">
711 {index + 1}.{" "}
712 {step.startsWith("git add")
713 ? "Fix the conflicts in each file, removing the conflict markers, then commit the merge:"
714 : step.startsWith("git push")
715 ? "Push it. The pull request updates, and its checks run again:"
716 : step.includes("merge") || step.includes("pull")
717 ? `Merge ${defaultBranch} in. Git stops on the conflicting files:`
718 : step.startsWith("git clone")
719 ? "Clone the pull request's fork:"
720 : step.startsWith("git checkout")
721 ? "Check out the pull request's branch:"
722 : "Get the latest of both:"}
723 </p>
724 <CopyLine text={step} prompt />
725 </li>
726 ))}
727 </ol>
728 {pull.fork && (
729 <p className="mt-2 text-xs text-faint">
730 A pull request's fork takes pushes from whoever opened it.
731 </p>
732 )}
733 </details>
734 </div>
735 </div>
736 </div>
737 );
738}
739
740/** Whether the change merges cleanly, when that is not a conflict. */
741export function MergeabilityRow({
742 mergeable,
743 defaultBranch,
744}: {
745 mergeable: Mergeable;
746 defaultBranch: string;
747}) {
748 if (mergeable === "checking") {
749 return (
750 <div className="flex gap-3 px-4 py-3 text-sm">
751 <LoaderCircle size={16} className="mt-0.5 shrink-0 animate-spin text-faint" />
752 <div>
753 <p className="font-medium">Checking whether this merges cleanly…</p>
754 <p className="mt-0.5 text-muted">
755 It and <span className="font-mono">{defaultBranch}</span> changed some of the same files, so g1t is merging
756 the two in a sandbox to see.
757 </p>
758 </div>
759 </div>
760 );
761 }
762 if (mergeable === "clean") {
763 return (
764 <div className="flex gap-3 px-4 py-3 text-sm">
765 <CircleCheck size={16} className="mt-0.5 shrink-0 text-success" />
766 <p className="font-medium">
767 This branch has no conflicts with <span className="font-mono">{defaultBranch}</span>
768 </p>
769 </div>
770 );
771 }
772 return null;
773}
774
775/**
776 * A catch-up handed to a sandbox, while it lasts: who is doing what, the
777 * run's live step, and how long it has taken. It ends with the pull request
778 * up to date (and this disappears), or with a plain failure and a way to
779 * try again. It never spins on with no end.
780 */
781export function CatchUpProgress({
782 owner,
783 repo,
784 number,
785 defaultBranch,
786 behind,
787 update,
788 startedAt,
789 retrying,
790 runs: initial,
791}: {
792 owner: string;
793 repo: string;
794 number: number;
795 defaultBranch: string;
796 behind: boolean;
797 update: Extract<PullBranchUpdate, { outcome: "needs_agent" }>;
798 /** When it was asked for, in ms since the epoch. */
799 startedAt: number;
800 /** Whether a retry is on its way. */
801 retrying: boolean;
802 /** The pull request's latest runs, from the page's loader. */
803 runs?: Live | null;
804}) {
805 // The page revalidates while this is working, which reloads the runs too.
806 const data = useRuns(owner, repo, { number: String(number), limit: "5" }, initial);
807 const [now, setNow] = useState(() => Date.now());
808 useEffect(() => {
809 const timer = setInterval(() => setNow(Date.now()), 5000);
810 return () => clearInterval(timer);
811 }, []);
812 const run = catchUpRun(data?.runs ?? [], startedAt);
813 const phase = catchUpPhase({ behind, run, startedAt, now });
814 if (phase === "done") return null;
815 const session = `/${owner}/${repo}/sessions/${number}`;
816 if (phase !== "working") {
817 return (
818 <div className="flex gap-3 px-4 py-3 text-sm">
819 <TriangleAlert size={16} className={`mt-0.5 shrink-0 ${phase === "failed" ? "text-danger" : "text-warn"}`} />
820 <div className="min-w-0">
821 <p className="font-medium">
822 {phase === "failed"
823 ? `Catching up with ${defaultBranch} failed`
824 : `Catching up with ${defaultBranch} is taking longer than it should`}
825 </p>
826 <p className="mt-0.5 text-muted">
827 {phase === "failed"
828 ? `${run?.error ? `${run.error} ` : ""}Nothing was pushed, so the pull request is as it was.`
829 : "It usually takes about a minute. It may still finish; this page updates if it does."}{" "}
830 <Link to={session} className="text-fg hover:underline">
831 See the session
832 </Link>
833 .
834 </p>
835 <Form method="post" className="mt-2">
836 <input type="hidden" name="action" value="update" />
837 <Button variant="quiet" type="submit" disabled={retrying}>
838 {retrying ? <LoaderCircle size={14} className="animate-spin" /> : <RotateCw size={14} />}
839 {retrying ? "Trying again…" : "Try again"}
840 </Button>
841 </Form>
842 </div>
843 </div>
844 );
845 }
846 return (
847 <div className="flex gap-3 px-4 py-3 text-sm">
848 <LoaderCircle size={16} className="mt-0.5 shrink-0 animate-spin text-accent" />
849 <div className="min-w-0 grow">
850 <p className="font-medium">{catchUpTitle(update.reason, defaultBranch)}</p>
851 <p className="mt-0.5 text-muted">
852 {catchUpWhy(update, defaultBranch)} This usually takes about a minute; the pull request updates here when it
853 is pushed.
854 </p>
855 {update.paths.length > 0 && (
856 <Hint label={update.paths.join(", ")}>
857 <p className="mt-1 truncate font-mono text-xs text-faint">
858 {update.paths.slice(0, 5).join(", ")}
859 {update.paths.length > 5 && ` and ${update.paths.length - 5} more`}
860 </p>
861 </Hint>
862 )}
863 <Hint label={run?.step}>
864 <p className="mt-2 truncate rounded-lg bg-bg px-3 py-2 font-mono text-xs text-fg/85 ring-1 ring-line">
865 <span className="mr-2 inline-block size-1.5 animate-pulse rounded-full bg-accent align-middle" />
866 {run?.step ?? (run ? "Starting a sandbox…" : "Waiting for a sandbox…")}
867 </p>
868 </Hint>
869 <p className="mt-2 flex flex-wrap items-center gap-x-3 text-xs text-muted">
870 <Elapsed from={new Date(startedAt).toISOString()} />
871 <Link to={session} className="hover:text-fg">
872 Watch the session
873 </Link>
874 </p>
875 </div>
876 </div>
877 );
878}