Skip to content
886 linesCodeBlameRaw
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 settled = false,
325}: {
326 /** Set when the merge queue took it out, or for a run from before checks were workflows. */
327 run: CheckRun | null;
328 required: RequiredCheck[];
329 statuses: CommitStatus[];
330 /** Each workflow run's jobs, by run id, where they could be read. */
331 jobs: Record<string, Job[]>;
332 pull: Pull;
333 /** The repository's path, `/<owner>/<repo>`. */
334 base: string;
335 earlier: CheckRun[];
336 canRerunWorkflows: boolean;
337 /** Where the required checks are chosen, for those who may. */
338 settingsUrl?: string | null;
339 error?: string | null;
340 /** The workflow runs' jobs are still being read: their rows wait, busy. */
341 loading?: boolean;
342 /**
343 * The pull request is closed or merged: its head's checks as they ended,
344 * to read only. Nothing is said of holding a merge.
345 */
346 settled?: boolean;
347}) {
348 if (!run && statuses.length === 0 && required.length === 0) return null;
349
350 // Every workflow row, so they can be counted the same way they are shown.
351 const workflow: Standing[] = statuses.flatMap((status) => {
352 const id = runIdOf(status);
353 const theirs = id ? jobs[id] : undefined;
354 return theirs && theirs.length > 0 ? theirs.map(jobStanding) : [statusStanding(status)];
355 });
356 const failed = workflow.filter((standing) => standing === "failed").length;
357 const pending = workflow.filter((standing) => standing === "running" || standing === "queued").length;
358 const passed = workflow.filter((standing) => standing === "passed").length;
359 const skipped = workflow.filter((standing) => standing === "skipped").length;
360 const total = workflow.length;
361 const queueFailed = run?.status === "failed" && run.results.length === 0;
362
363 const summary = required.length > 0 ? requiredSummary(required) : null;
364 const headline = summary
365 ? summary.title
366 : failed > 0
367 ? `${failed} of ${total} ${total === 1 ? "check" : "checks"} failed`
368 : pending > 0
369 ? `${pending} of ${total} ${total === 1 ? "check is" : "checks are"} still running`
370 : total > 0
371 ? "All checks have passed"
372 : "Checks";
373 const others = [
374 failed > 0 && `${failed} failed`,
375 pending > 0 && `${pending} running`,
376 passed > 0 && `${passed} passed`,
377 skipped > 0 && `${skipped} skipped or cancelled`,
378 ]
379 .filter(Boolean)
380 .join(", ");
381 const sub = summary
382 ? [summary.sub, others && `all checks: ${others}`].filter(Boolean).join(" · ")
383 : settled
384 ? [others, pull.headCommit && `on ${pull.headCommit.slice(0, 7)}`].filter(Boolean).join(" · ")
385 : [others, "none are required, so none hold the merge"].filter(Boolean).join(" · ");
386 const standing: Standing = summary
387 ? summary.standing
388 : failed > 0
389 ? "failed"
390 : pending > 0
391 ? "running"
392 : "passed";
393
394 const commitPath = (sha: string) =>
395 pull.fork ? `/${pull.fork.namespace}/${pull.fork.name}/commit/${sha}` : `${base}/commit/${sha}`;
396 const older = run != null && pull.headCommit != null && run.headCommit !== pull.headCommit;
397 const failedRuns = statuses.filter((status) => statusStanding(status) === "failed" && runIdOf(status));
398
399 return (
400 <div aria-busy={loading || undefined}>
401 <div className="flex items-start gap-3 px-4 py-3">
402 <span className="mt-0.5 shrink-0">
403 {standing === "failed" || queueFailed ? (
404 <CircleX size={16} className="text-danger" />
405 ) : standing === "running" ? (
406 <LoaderCircle size={16} className="animate-spin text-warn" />
407 ) : (
408 <CircleCheck size={16} className="text-success" />
409 )}
410 </span>
411 <div className="min-w-0 grow">
412 <p className="text-sm font-medium">{headline}</p>
413 {sub && <p className="mt-0.5 text-xs text-muted">{sub}</p>}
414 {!summary && !settled && settingsUrl && (
415 <p className="mt-0.5 text-xs text-muted">
416 <Link to={settingsUrl} className="text-fg hover:underline">
417 Choose required checks
418 </Link>{" "}
419 to hold merges until they pass.
420 </p>
421 )}
422 </div>
423 <div className="flex shrink-0 flex-wrap items-center justify-end gap-2">
424 {canRerunWorkflows &&
425 failedRuns.map((status) => (
426 <Form method="post" key={status.context}>
427 <input type="hidden" name="action" value="rerun-workflow" />
428 <input type="hidden" name="run" value={runIdOf(status) ?? ""} />
429 <Hint label={`Re-run the failed jobs of ${status.context}`}>
430 <SubmitButton
431 variant="quiet"
432 match={{ action: "rerun-workflow", run: runIdOf(status) ?? "" }}
433 pending="Re-running…"
434 >
435 <RotateCw size={13} />
436 Re-run failed jobs{failedRuns.length > 1 ? ` of ${status.context}` : ""}
437 </SubmitButton>
438 </Hint>
439 </Form>
440 ))}
441 </div>
442 </div>
443 {error && (
444 <div className="px-4 pb-2">
445 <ErrorText>{error}</ErrorText>
446 </div>
447 )}
448
449 <div className="divide-y divide-line border-t border-line">
450 {required.length > 0 && (
451 <section aria-label="Required checks" className="py-1">
452 <p className="px-4 pt-1.5 pb-1 text-xs font-medium text-muted">Required</p>
453 {required.map((check) => (
454 <CheckLine
455 key={check.name}
456 standing={requiredStanding(check)}
457 name={
458 <>
459 {check.name}
460 <span className="ml-1.5 rounded-full border border-line px-1.5 py-px text-[0.625rem] text-faint">Required</span>
461 </>
462 }
463 detail={
464 check.state === "expected"
465 ? "Waiting for status to be reported"
466 : (check.description ?? (check.state === "pending" ? "running" : undefined))
467 }
468 to={onSite(check.targetUrl)}
469 />
470 ))}
471 </section>
472 )}
473
474 {statuses.length > 0 && (
475 <section aria-label="All checks" className="py-1">
476 <p className="px-4 pt-1.5 pb-1 text-xs font-medium text-muted">{required.length > 0 ? "All checks" : "Workflows"}</p>
477 {statuses.map((status) => {
478 const id = runIdOf(status);
479 const runPath = id ? `${base}/actions/runs/${id}` : onSite(status.targetUrl);
480 const theirs = id ? jobs[id] : undefined;
481 if (!theirs || theirs.length === 0) {
482 return (
483 <CheckLine
484 key={status.context}
485 standing={statusStanding(status)}
486 name={status.context}
487 detail={status.description ?? undefined}
488 timing={loading && id != null}
489 to={runPath}
490 />
491 );
492 }
493 return theirs.map((job) => (
494 <CheckLine
495 key={`${status.context}-${job.id}`}
496 standing={jobStanding(job)}
497 name={
498 <>
499 <span className="text-muted">{status.context} / </span>
500 {job.name}
501 </>
502 }
503 detail={job.reason ?? (jobStanding(job) === "failed" ? "failed" : undefined)}
504 time={duration(job.startedAt, job.finishedAt) || undefined}
505 to={`${runPath}?job=${job.id}`}
506 />
507 ));
508 })}
509 </section>
510 )}
511
512 {run && (
513 <section aria-label={queueFailed ? "Merge queue" : "Earlier commands"} className="py-1">
514 <p className="flex flex-wrap items-center gap-x-1.5 px-4 pt-1.5 pb-1 text-xs text-faint">
515 <span className="font-medium text-muted">{queueFailed ? "Merge queue" : "Commands from the issue"}</span>
516 <span>· on</span>
517 <Link to={commitPath(run.headCommit)} className="inline-flex items-center gap-1 font-mono hover:text-fg">
518 <GitCommitHorizontal size={12} />
519 {run.headCommit.slice(0, 7)}
520 </Link>
521 {run.finishedAt ? (
522 <span>
523 · <TimeAgo at={run.finishedAt} />
524 </span>
525 ) : (
526 <span>
527 · started <TimeAgo at={run.createdAt} />
528 </span>
529 )}
530 </p>
531 {older && !queueFailed && (
532 <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">
533 <TriangleAlert size={13} className="mt-0.5 shrink-0" />
534 These ran on an older commit, before the latest push,{" "}
535 <span className="font-mono">{pull.headCommit?.slice(0, 7)}</span>.
536 </p>
537 )}
538 {run.error && (
539 <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>
540 )}
541 {run.results.map((result, index) => (
542 <CommandResult key={`${result.command}-${index}`} result={result} />
543 ))}
544 </section>
545 )}
546
547 {earlier.length > 0 && (
548 <details className="group">
549 <summary className="flex cursor-pointer list-none items-center gap-2 px-4 py-2 text-xs text-muted hover:text-fg">
550 <History size={13} />
551 Earlier records ({earlier.length})
552 <ChevronRight size={13} className="transition-transform group-open:rotate-90" />
553 </summary>
554 <ul className="space-y-1 px-4 pb-3 text-xs">
555 {earlier.map((before) => (
556 <li key={before.id} className="flex items-center gap-2 text-muted">
557 <StandingIcon
558 size={13}
559 standing={
560 before.status === "passed"
561 ? "passed"
562 : before.status === "queued" || before.status === "running"
563 ? "queued"
564 : "failed"
565 }
566 />
567 <Link to={commitPath(before.headCommit)} className="font-mono hover:text-fg">
568 {before.headCommit.slice(0, 7)}
569 </Link>
570 <span className="min-w-0 truncate">
571 {before.status === "errored"
572 ? `could not run${before.error ? `: ${before.error}` : ""}`
573 : before.results.length > 0
574 ? `${before.results.filter((result) => result.passed).length} of ${before.results.length} passed`
575 : (before.error ?? before.status)}
576 </span>
577 <span className="ml-auto shrink-0 text-faint">
578 <TimeAgo at={before.createdAt} />
579 </span>
580 </li>
581 ))}
582 </ul>
583 </details>
584 )}
585 </div>
586 </div>
587 );
588}
589
590// --- Mergeability ---------------------------------------------------------
591
592/** How to resolve the conflicts by hand, step by step, for a branch or a fork. */
593export function commandLineSteps(
594 pull: Pull,
595 owner: string,
596 repo: string,
597 defaultBranch: string,
598 files: string[],
599 site = "https://g1t.sh",
600): string[] {
601 const upstream = `${site}/${owner}/${repo}.git`;
602 const add = files.length > 0 && files.length <= 6 ? `git add ${files.join(" ")}` : "git add -A";
603 if (pull.branch) {
604 return [
605 `git fetch origin`,
606 `git checkout ${pull.branch}`,
607 `git merge origin/${defaultBranch}`,
608 `${add} && git commit --no-edit`,
609 `git push origin ${pull.branch}`,
610 ];
611 }
612 const fork = pull.fork!;
613 return [
614 `git clone ${site}/${fork.namespace}/${fork.name}.git && cd ${fork.name}`,
615 `git pull --no-rebase ${upstream} ${defaultBranch}`,
616 `${add} && git commit --no-edit`,
617 `git push`,
618 ];
619}
620
621/**
622 * The branch conflicts with its target: which files, and three ways out —
623 * g1t, the browser (soon), or the command line.
624 */
625export function ConflictsSection({
626 conflicts,
627 pull,
628 owner,
629 repo,
630 defaultBranch,
631 changesUrl,
632 canResolve,
633 resolving,
634 error,
635}: {
636 conflicts: string[];
637 pull: Pull;
638 owner: string;
639 repo: string;
640 defaultBranch: string;
641 /** Where the pull request's changes are shown, to link each file. */
642 changesUrl: string;
643 /** Whether the viewer may have g1t resolve them. */
644 canResolve: boolean;
645 /** Whether asking for it is on its way. */
646 resolving: boolean;
647 error?: string | null;
648}) {
649 const { site } = useAddresses();
650 const steps = commandLineSteps(pull, owner, repo, defaultBranch, conflicts, site);
651 return (
652 <div className="bg-danger/5">
653 <div className="flex gap-3 px-4 py-3">
654 <CircleAlert size={16} className="mt-0.5 shrink-0 text-danger" />
655 <div className="min-w-0 grow">
656 <p className="text-sm font-medium">This branch has conflicts that must be resolved</p>
657 <p className="mt-0.5 text-sm text-muted">
658 Merging <span className="font-mono">{defaultBranch}</span> into it stops on{" "}
659 {conflicts.length === 1 ? "one file" : `${conflicts.length} files`}. It cannot merge until they are resolved.
660 </p>
661 {conflicts.length > 0 && (
662 <>
663 <p className="mt-3 text-xs font-medium text-muted">Conflicting files</p>
664 <ul className="mt-1.5 space-y-1">
665 {conflicts.map((path) => (
666 <li key={path}>
667 <Link
668 to={`${changesUrl}#file-${path}`}
669 className="font-mono text-xs text-fg hover:text-danger hover:underline"
670 >
671 {path}
672 </Link>
673 </li>
674 ))}
675 </ul>
676 </>
677 )}
678 <div className="mt-3 flex flex-wrap items-center gap-2">
679 {canResolve && (
680 <Form method="post">
681 <input type="hidden" name="action" value="update" />
682 <Button variant="primary" type="submit" disabled={resolving}>
683 {resolving ? <LoaderCircle size={14} className="animate-spin" /> : <Sparkles size={14} />}
684 {resolving ? "Starting g1t…" : "Resolve with g1t"}
685 </Button>
686 </Form>
687 )}
688 <Tooltip>
689 <TooltipTrigger asChild>
690 {/* A disabled button gets no pointer events; the span carries the tooltip. */}
691 <span tabIndex={0} className="inline-flex">
692 <Button variant="quiet" type="button" disabled aria-disabled="true">
693 Resolve in the browser
694 <span className="rounded-full border border-line px-1.5 text-[0.625rem] text-faint">Soon</span>
695 </Button>
696 </span>
697 </TooltipTrigger>
698 <TooltipContent>
699 An editor for conflicts in the browser is coming. Until then, have g1t resolve them, or use the
700 command line.
701 </TooltipContent>
702 </Tooltip>
703 </div>
704 {error && (
705 <div className="mt-2">
706 <ErrorText>{error}</ErrorText>
707 </div>
708 )}
709 <details className="group mt-3">
710 <summary className="flex cursor-pointer list-none items-center gap-1.5 text-xs text-muted hover:text-fg">
711 <Terminal size={13} />
712 Resolve on the command line
713 <ChevronRight size={13} className="transition-transform group-open:rotate-90" />
714 </summary>
715 <ol className="mt-2 space-y-2 text-xs text-muted">
716 {steps.map((step, index) => (
717 <li key={step}>
718 <p className="mb-1">
719 {index + 1}.{" "}
720 {step.startsWith("git add")
721 ? "Fix the conflicts in each file, removing the conflict markers, then commit the merge:"
722 : step.startsWith("git push")
723 ? "Push it. The pull request updates, and its checks run again:"
724 : step.includes("merge") || step.includes("pull")
725 ? `Merge ${defaultBranch} in. Git stops on the conflicting files:`
726 : step.startsWith("git clone")
727 ? "Clone the pull request's fork:"
728 : step.startsWith("git checkout")
729 ? "Check out the pull request's branch:"
730 : "Get the latest of both:"}
731 </p>
732 <CopyLine text={step} prompt />
733 </li>
734 ))}
735 </ol>
736 {pull.fork && (
737 <p className="mt-2 text-xs text-faint">
738 A pull request's fork takes pushes from whoever opened it.
739 </p>
740 )}
741 </details>
742 </div>
743 </div>
744 </div>
745 );
746}
747
748/** Whether the change merges cleanly, when that is not a conflict. */
749export function MergeabilityRow({
750 mergeable,
751 defaultBranch,
752}: {
753 mergeable: Mergeable;
754 defaultBranch: string;
755}) {
756 if (mergeable === "checking") {
757 return (
758 <div className="flex gap-3 px-4 py-3 text-sm">
759 <LoaderCircle size={16} className="mt-0.5 shrink-0 animate-spin text-faint" />
760 <div>
761 <p className="font-medium">Checking whether this merges cleanly…</p>
762 <p className="mt-0.5 text-muted">
763 It and <span className="font-mono">{defaultBranch}</span> changed some of the same files, so g1t is merging
764 the two in a sandbox to see.
765 </p>
766 </div>
767 </div>
768 );
769 }
770 if (mergeable === "clean") {
771 return (
772 <div className="flex gap-3 px-4 py-3 text-sm">
773 <CircleCheck size={16} className="mt-0.5 shrink-0 text-success" />
774 <p className="font-medium">
775 This branch has no conflicts with <span className="font-mono">{defaultBranch}</span>
776 </p>
777 </div>
778 );
779 }
780 return null;
781}
782
783/**
784 * A catch-up handed to a sandbox, while it lasts: who is doing what, the
785 * run's live step, and how long it has taken. It ends with the pull request
786 * up to date (and this disappears), or with a plain failure and a way to
787 * try again. It never spins on with no end.
788 */
789export function CatchUpProgress({
790 owner,
791 repo,
792 number,
793 defaultBranch,
794 behind,
795 update,
796 startedAt,
797 retrying,
798 runs: initial,
799}: {
800 owner: string;
801 repo: string;
802 number: number;
803 defaultBranch: string;
804 behind: boolean;
805 update: Extract<PullBranchUpdate, { outcome: "needs_agent" }>;
806 /** When it was asked for, in ms since the epoch. */
807 startedAt: number;
808 /** Whether a retry is on its way. */
809 retrying: boolean;
810 /** The pull request's latest runs, from the page's loader. */
811 runs?: Live | null;
812}) {
813 // The page revalidates while this is working, which reloads the runs too.
814 const data = useRuns(owner, repo, { number: String(number), limit: "5" }, initial);
815 const [now, setNow] = useState(() => Date.now());
816 useEffect(() => {
817 const timer = setInterval(() => setNow(Date.now()), 5000);
818 return () => clearInterval(timer);
819 }, []);
820 const run = catchUpRun(data?.runs ?? [], startedAt);
821 const phase = catchUpPhase({ behind, run, startedAt, now });
822 if (phase === "done") return null;
823 const session = `/${owner}/${repo}/sessions/${number}`;
824 if (phase !== "working") {
825 return (
826 <div className="flex gap-3 px-4 py-3 text-sm">
827 <TriangleAlert size={16} className={`mt-0.5 shrink-0 ${phase === "failed" ? "text-danger" : "text-warn"}`} />
828 <div className="min-w-0">
829 <p className="font-medium">
830 {phase === "failed"
831 ? `Catching up with ${defaultBranch} failed`
832 : `Catching up with ${defaultBranch} is taking longer than it should`}
833 </p>
834 <p className="mt-0.5 text-muted">
835 {phase === "failed"
836 ? `${run?.error ? `${run.error} ` : ""}Nothing was pushed, so the pull request is as it was.`
837 : "It usually takes about a minute. It may still finish; this page updates if it does."}{" "}
838 <Link to={session} className="text-fg hover:underline">
839 See the session
840 </Link>
841 .
842 </p>
843 <Form method="post" className="mt-2">
844 <input type="hidden" name="action" value="update" />
845 <Button variant="quiet" type="submit" disabled={retrying}>
846 {retrying ? <LoaderCircle size={14} className="animate-spin" /> : <RotateCw size={14} />}
847 {retrying ? "Trying again…" : "Try again"}
848 </Button>
849 </Form>
850 </div>
851 </div>
852 );
853 }
854 return (
855 <div className="flex gap-3 px-4 py-3 text-sm">
856 <LoaderCircle size={16} className="mt-0.5 shrink-0 animate-spin text-accent" />
857 <div className="min-w-0 grow">
858 <p className="font-medium">{catchUpTitle(update.reason, defaultBranch)}</p>
859 <p className="mt-0.5 text-muted">
860 {catchUpWhy(update, defaultBranch)} This usually takes about a minute; the pull request updates here when it
861 is pushed.
862 </p>
863 {update.paths.length > 0 && (
864 <Hint label={update.paths.join(", ")}>
865 <p className="mt-1 truncate font-mono text-xs text-faint">
866 {update.paths.slice(0, 5).join(", ")}
867 {update.paths.length > 5 && ` and ${update.paths.length - 5} more`}
868 </p>
869 </Hint>
870 )}
871 <Hint label={run?.step}>
872 <p className="mt-2 truncate rounded-lg bg-bg px-3 py-2 font-mono text-xs text-fg/85 ring-1 ring-line">
873 <span className="mr-2 inline-block size-1.5 animate-pulse rounded-full bg-accent align-middle" />
874 {run?.step ?? (run ? "Starting a sandbox…" : "Waiting for a sandbox…")}
875 </p>
876 </Hint>
877 <p className="mt-2 flex flex-wrap items-center gap-x-3 text-xs text-muted">
878 <Elapsed from={new Date(startedAt).toISOString()} />
879 <Link to={session} className="hover:text-fg">
880 Watch the session
881 </Link>
882 </p>
883 </div>
884 </div>
885 );
886}