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.

172 linesCodeBlameRaw
1/**
2 * Above a fork's files: where it came from, a way to bring in what landed
3 * there since (Sync fork, posting to routes/repo/sync-fork.ts), and a way
4 * to offer the branch back as a pull request to the parent (Contribute).
5 */
6import { GitFork, GitPullRequest, RefreshCw } from "lucide-react";
7import { useState } from "react";
8import { Link, useFetcher } from "react-router";
9
10import type { SyncedFork } from "@g1t/contracts";
11
12import { cloneUrl, useAddresses } from "../lib/addresses";
13import type { SyncOutcome } from "../routes/repo/sync-fork";
14import { ButtonLink, CopyLine, ErrorText, SubmitButton } from "./ui";
15import { Alert } from "./ui/alert";
16import { Button } from "./ui/button";
17import { Card } from "./ui/card";
18import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
19
20/** Where a fork came from, as lib/forks.server.ts reads it. */
21export type ForkSource = {
22 namespace: string;
23 name: string;
24 /** The parent's default branch, which the fork's default branch follows. */
25 defaultBranch: string;
26 /** The fork's own default branch. */
27 forkDefault: string;
28};
29
30/** The parent's branch a fork's `branch` follows: its default for the fork's default, the same name otherwise. */
31export function upstreamBranch(source: ForkSource, branch: string): string {
32 return branch === source.forkDefault ? source.defaultBranch : branch;
33}
34
35function commits(n: number): string {
36 return `${n.toLocaleString("en-US")} ${n === 1 ? "commit" : "commits"}`;
37}
38
39export function ForkBar({
40 base,
41 branch,
42 parent,
43 canPush,
44}: {
45 /** The fork's address, `/<namespace>/<name>`. */
46 base: string;
47 /** The fork's branch shown. */
48 branch: string;
49 parent: ForkSource;
50 /** Whether the viewer may push to the fork, and so sync it and offer its branch back. */
51 canPush: boolean;
52}) {
53 const fetcher = useFetcher<SyncOutcome>({ key: `sync-fork:${base}:${branch}` });
54 const [open, setOpen] = useState(false);
55 const upstream = `${parent.namespace}/${parent.name}`;
56 const parentBranch = upstreamBranch(parent, branch);
57 const upstreamRef = `${upstream}:${parentBranch}`;
58 const head = `${base.slice(1)}:${branch}`;
59 const contribute = `/${upstream}/pulls/new?head=${encodeURIComponent(head)}&base=${encodeURIComponent(parentBranch)}`;
60 const done = fetcher.state === "idle" ? fetcher.data : undefined;
61 return (
62 <div className="mb-4 space-y-3">
63 <Card className="flex flex-wrap items-center gap-x-3 gap-y-2 px-4 py-2.5 text-sm">
64 <GitFork size={15} className="shrink-0 text-faint" />
65 <p className="min-w-0 grow text-muted">
66 <span className="font-mono text-fg/80">{branch}</span> follows{" "}
67 <Link to={`/${upstream}/tree/${encodeURIComponent(parentBranch)}`} className="font-mono text-fg/80 hover:text-accent hover:underline">
68 {upstreamRef}
69 </Link>
70 </p>
71 <div className="flex shrink-0 flex-wrap items-center gap-2">
72 {canPush && (
73 <ButtonLink variant="outline" size="sm" to={contribute}>
74 <GitPullRequest size={14} />
75 Contribute
76 </ButtonLink>
77 )}
78 {canPush && (
79 <Popover open={open} onOpenChange={setOpen}>
80 <PopoverTrigger asChild>
81 <Button variant="outline" size="sm">
82 <RefreshCw size={14} />
83 Sync fork
84 </Button>
85 </PopoverTrigger>
86 <PopoverContent align="end" className="w-80">
87 <fetcher.Form method="post" action={`${base}/sync-fork`} onSubmit={() => setOpen(false)} className="space-y-3">
88 <input type="hidden" name="branch" value={branch} />
89 <p className="font-medium">Bring in {upstreamRef}</p>
90 <p className="text-muted">
91 Commits on <span className="font-mono">{upstreamRef}</span> that <span className="font-mono">{branch}</span> lacks come in. When it has none
92 of its own, it moves up to match; when it has some, they are kept and a merge commit joins the two. If both changed the same lines, nothing
93 changes and you are told which files.
94 </p>
95 <SubmitButton size="sm" variant="accent" fetcher={fetcher} pending="Syncing…" className="w-full">
96 Update branch
97 </SubmitButton>
98 </fetcher.Form>
99 </PopoverContent>
100 </Popover>
101 )}
102 </div>
103 </Card>
104 {fetcher.state !== "idle" && (
105 <p role="status" className="text-sm text-muted">
106 Bringing in {upstreamRef}…
107 </p>
108 )}
109 {done?.error && <ErrorText>{done.error}</ErrorText>}
110 {done?.synced && <SyncResult synced={done.synced} upstream={upstream} upstreamRef={upstreamRef} contribute={contribute} />}
111 </div>
112 );
113}
114
115/** What syncing did, said in place. */
116function SyncResult({ synced, upstream, upstreamRef, contribute }: { synced: SyncedFork; upstream: string; upstreamRef: string; contribute: string }) {
117 const remote = cloneUrl(useAddresses(), upstream);
118 switch (synced.status) {
119 case "up_to_date":
120 return (
121 <Alert role="status" tone="info">
122 Already up to date with <span className="font-mono">{upstreamRef}</span>.
123 {synced.ahead > 0 && (
124 <>
125 {" "}
126 {synced.branch} has {commits(synced.ahead)} of its own.
127 </>
128 )}
129 </Alert>
130 );
131 case "fast_forwarded":
132 return (
133 <Alert role="status" tone="success">
134 Updated: {synced.branch} moved up {commits(synced.behind)} to match <span className="font-mono">{upstreamRef}</span>.
135 </Alert>
136 );
137 case "merged":
138 return (
139 <Alert role="status" tone="success">
140 Updated: {commits(synced.behind)} from <span className="font-mono">{upstreamRef}</span> merged into {synced.branch} with a merge commit. Its own{" "}
141 {commits(synced.ahead)} are kept.
142 </Alert>
143 );
144 case "conflicted":
145 return (
146 <Alert role="alert" tone="warn" className="space-y-2 py-3">
147 <p>
148 Not updated: {synced.branch} and <span className="font-mono">{upstreamRef}</span> both changed{" "}
149 {synced.conflicts.length === 1 ? "this file" : "these files"}, so nothing was changed.
150 </p>
151 {synced.conflicts.length > 0 && (
152 <ul className="list-disc space-y-0.5 pl-5 font-mono text-xs">
153 {synced.conflicts.map((file) => (
154 <li key={file}>{file}</li>
155 ))}
156 </ul>
157 )}
158 <p className="text-muted">
159 Merge it in your working copy and push, or{" "}
160 <Link to={contribute} className="text-accent hover:underline">
161 open a pull request
162 </Link>{" "}
163 and resolve it there.
164 </p>
165 <div className="space-y-1.5">
166 <CopyLine prompt text={`git remote add upstream ${remote}`} />
167 <CopyLine prompt text={`git pull upstream ${upstreamRef.slice(upstream.length + 1)}`} />
168 </div>
169 </Alert>
170 );
171 }
172}