| 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 | */ |
| 6 | import { GitFork, GitPullRequest, RefreshCw } from "lucide-react"; |
| 7 | import { useState } from "react"; |
| 8 | import { Link, useFetcher } from "react-router"; |
| 9 | |
| 10 | import type { SyncedFork } from "@g1t/contracts"; |
| 11 | |
| 12 | import { cloneUrl, useAddresses } from "../lib/addresses"; |
| 13 | import type { SyncOutcome } from "../routes/repo/sync-fork"; |
| 14 | import { ButtonLink, CopyLine, ErrorText, SubmitButton } from "./ui"; |
| 15 | import { Alert } from "./ui/alert"; |
| 16 | import { Button } from "./ui/button"; |
| 17 | import { Card } from "./ui/card"; |
| 18 | import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover"; |
| 19 | |
| 20 | /** Where a fork came from, as lib/forks.server.ts reads it. */ |
| 21 | export 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. */ |
| 31 | export function upstreamBranch(source: ForkSource, branch: string): string { |
| 32 | return branch === source.forkDefault ? source.defaultBranch : branch; |
| 33 | } |
| 34 | |
| 35 | function commits(n: number): string { |
| 36 | return `${n.toLocaleString("en-US")} ${n === 1 ? "commit" : "commits"}`; |
| 37 | } |
| 38 | |
| 39 | export 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{" "} |
| 92 | <span className="font-mono">{branch}</span> lacks come in. When it has none of its own, it moves up to |
| 93 | match; when it has some, they are kept and a merge commit joins the two. If both changed the same |
| 94 | lines, nothing changes and you are told which files. |
| 95 | </p> |
| 96 | <SubmitButton size="sm" variant="accent" fetcher={fetcher} pending="Syncing…" className="w-full"> |
| 97 | Update branch |
| 98 | </SubmitButton> |
| 99 | </fetcher.Form> |
| 100 | </PopoverContent> |
| 101 | </Popover> |
| 102 | )} |
| 103 | </div> |
| 104 | </Card> |
| 105 | {fetcher.state !== "idle" && ( |
| 106 | <p role="status" className="text-sm text-muted"> |
| 107 | Bringing in {upstreamRef}… |
| 108 | </p> |
| 109 | )} |
| 110 | {done?.error && <ErrorText>{done.error}</ErrorText>} |
| 111 | {done?.synced && <SyncResult synced={done.synced} upstream={upstream} upstreamRef={upstreamRef} contribute={contribute} />} |
| 112 | </div> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | /** What syncing did, said in place. */ |
| 117 | function SyncResult({ |
| 118 | synced, |
| 119 | upstream, |
| 120 | upstreamRef, |
| 121 | contribute, |
| 122 | }: { |
| 123 | synced: SyncedFork; |
| 124 | upstream: string; |
| 125 | upstreamRef: string; |
| 126 | contribute: string; |
| 127 | }) { |
| 128 | const remote = cloneUrl(useAddresses(), upstream); |
| 129 | switch (synced.status) { |
| 130 | case "up_to_date": |
| 131 | return ( |
| 132 | <Alert role="status" tone="info"> |
| 133 | Already up to date with <span className="font-mono">{upstreamRef}</span>. |
| 134 | {synced.ahead > 0 && <> {synced.branch} has {commits(synced.ahead)} of its own.</>} |
| 135 | </Alert> |
| 136 | ); |
| 137 | case "fast_forwarded": |
| 138 | return ( |
| 139 | <Alert role="status" tone="success"> |
| 140 | Updated: {synced.branch} moved up {commits(synced.behind)} to match{" "} |
| 141 | <span className="font-mono">{upstreamRef}</span>. |
| 142 | </Alert> |
| 143 | ); |
| 144 | case "merged": |
| 145 | return ( |
| 146 | <Alert role="status" tone="success"> |
| 147 | Updated: {commits(synced.behind)} from <span className="font-mono">{upstreamRef}</span> merged into{" "} |
| 148 | {synced.branch} with a merge commit. Its own {commits(synced.ahead)} are kept. |
| 149 | </Alert> |
| 150 | ); |
| 151 | case "conflicted": |
| 152 | return ( |
| 153 | <Alert role="alert" tone="warn" className="space-y-2 py-3"> |
| 154 | <p> |
| 155 | Not updated: {synced.branch} and <span className="font-mono">{upstreamRef}</span> both changed{" "} |
| 156 | {synced.conflicts.length === 1 ? "this file" : "these files"}, so nothing was changed. |
| 157 | </p> |
| 158 | {synced.conflicts.length > 0 && ( |
| 159 | <ul className="list-disc space-y-0.5 pl-5 font-mono text-xs"> |
| 160 | {synced.conflicts.map((file) => ( |
| 161 | <li key={file}>{file}</li> |
| 162 | ))} |
| 163 | </ul> |
| 164 | )} |
| 165 | <p className="text-muted"> |
| 166 | Merge it in your working copy and push, or{" "} |
| 167 | <Link to={contribute} className="text-accent hover:underline"> |
| 168 | open a pull request |
| 169 | </Link>{" "} |
| 170 | and resolve it there. |
| 171 | </p> |
| 172 | <div className="space-y-1.5"> |
| 173 | <CopyLine prompt text={`git remote add upstream ${remote}`} /> |
| 174 | <CopyLine prompt text={`git pull upstream ${upstreamRef.slice(upstream.length + 1)}`} /> |
| 175 | </div> |
| 176 | </Alert> |
| 177 | ); |
| 178 | } |
| 179 | } |