/** Git menu dialogs: Pull…, Merge…, Rebase…, Manage Remotes… and Worktrees. */ import { useEffect, useId, useMemo, useRef, useState, type KeyboardEvent } from "react"; import clsx from "clsx"; import type { Branch, BranchInfo, MergeOptions, Remote, Worktree } from "@/bindings"; import { call, commands } from "@/platform/ipc"; import { notifications } from "@/workbench/contributions"; import { openWorktree } from "./openWorktree"; import { Button, Checkbox, Dialog, HighlightedText, Icon, IconButton, Input, Menu, Select, T, Tree, listNavigate, useSanitizedField, type SelectOption, type SelectProps, type MenuItem, } from "@/ui"; import { useRowHeight } from "@/ui/density"; import { sortLocal, sortRemote } from "./branches"; import { branchRef, conflictActions, openConflictsInstead, validateBranchName } from "./branchOps"; import { joinPath } from "./clone"; import { cleanUpBranchNameOnTyping } from "@/platform/gitRefName"; import { confirmDialog, openDialog } from "./dialogs"; import { errorMessage, runOp, runRemote, saveAll } from "./ops"; import { baseName } from "./model"; import { currentRepo, loadBranches, refreshNow, repoName, useVcs } from "./store"; import { groupWorktrees, isCurrentWorktree, worktreeContainsProject, worktreeMoveTarget, type RepoWorktrees, } from "./worktrees"; import styles from "./common.module.css"; import remoteStyles from "./GitRemotes.module.css"; import commandStyles from "./GitCommandDialog.module.css"; import worktreeStyles from "./Worktrees.module.css"; import { tx } from "@/platform/i18n"; import { useProject } from "@/platform/project"; // --- shared ----------------------------------------------------------------- export type MergeFlag = "noFf" | "ffOnly" | "squash" | "noCommit"; /** Flags git refuses together with `flag`. */ const EXCLUSIVE: Record = { noFf: ["ffOnly", "squash"], ffOnly: ["noFf", "squash", "noCommit"], squash: ["noFf", "ffOnly"], noCommit: ["ffOnly"], }; /** Merge flags that cannot be turned on next to the `on` ones (and with `--rebase`). */ export function disabledMergeFlags(on: ReadonlySet, rebase = false): Set { if (rebase) return new Set(["noFf", "ffOnly", "squash", "noCommit"]); const out = new Set(); for (const f of on) for (const x of EXCLUSIVE[f]) out.add(x); return out; } export function mergeOptionsOf(on: ReadonlySet, message: string | null = null): MergeOptions { return { noFf: on.has("noFf"), ffOnly: on.has("ffOnly"), squash: on.has("squash"), noCommit: on.has("noCommit"), message: message?.trim() || null, }; } const FLAG_LABELS: Array<[MergeFlag, string]> = [ ["noFf", "--no-ff"], ["ffOnly", "--ff-only"], ["squash", "--squash"], ["noCommit", "--no-commit"], ]; function mergeFlagItems( flags: ReadonlySet, setFlags: (flags: Set) => void, rebase = false, ): MenuItem[] { const disabled = disabledMergeFlags(flags, rebase); return FLAG_LABELS.map(([flag, label]) => ({ label, verbatim: true, checked: flags.has(flag), disabled: disabled.has(flag) && !flags.has(flag), run: () => { const next = new Set(flags); if (next.has(flag)) next.delete(flag); else next.add(flag); setFlags(next); }, })); } function ModifyOptions({ items }: { items: MenuItem[] }) { const [open, setOpen] = useState(false); const anchor = useRef(null); return ( <> setOpen(false)} anchor={anchor} placement="top-start" aria-label="Modify options" items={items} /> ); } const emptyBranches: Branch[] = []; function useBranches(repo: string | null): Branch[] { const [loaded, setLoaded] = useState<{ repo: string | null; branches: Branch[] } | null>(null); useEffect(() => { let live = true; void call(commands.vcsBranches(repo)) .then((branches) => live && setLoaded({ repo, branches })) .catch(() => undefined); return () => { live = false; }; }, [repo]); return loaded?.repo === repo ? loaded.branches : emptyBranches; } const branchOptions = (list: readonly Branch[]): SelectOption[] => list.filter((b) => !b.name.endsWith("/HEAD")).map((b) => ({ value: b.name, label: b.name, icon: "branch" })); /** Every branch choice in these dialogs: repositories hold hundreds of branches, so type to filter, Enter picks. */ function BranchSelect(props: Omit) { return ({ value: root, label: repoName(root), verbatim: true }))} onChange={onChange} /> ); } /** The repository's branch info: the store's for the selected repository, else read from git. */ export function useBranchInfo(repo: string | null) { const [initial] = useState(() => ({ repo: currentRepo(), info: useVcs.getState().branch })); const [loaded, setLoaded] = useState<{ repo: string | null; info: BranchInfo | null } | null>(null); useEffect(() => { if (repo === initial.repo) return; let live = true; void call(commands.vcsBranchInfo(repo)) .then((info) => live && setLoaded({ repo, info })) .catch(() => live && setLoaded({ repo, info: null })); return () => { live = false; }; }, [repo, initial.repo]); if (repo === initial.repo) return initial.info; return loaded?.repo === repo ? loaded.info : null; } function PullDialog({ initialRepo, onClose }: { initialRepo?: string | null; onClose: () => void }) { const [repo, setRepo] = useState(() => (initialRepo === undefined ? currentRepo() : initialRepo)); const info = useBranchInfo(repo); const branches = useBranches(repo); const [remotes, setRemotes] = useState(null); const [remote, setRemote] = useState(null); const [chosenBranch, setBranch] = useState(null); const [rebase, setRebase] = useState(false); const [flags, setFlags] = useState>(() => new Set()); const chooseRepo = (root: string) => { if (root === repo) return; setRepo(root); setRemotes(null); setRemote(null); setBranch(null); }; useEffect(() => { let alive = true; call(commands.vcsRemotes(repo)) .then((list) => { if (!alive) return; setRemotes(list); const upstreamRemote = info?.upstream?.split("/")[0]; setRemote(list.find((r) => r.name === upstreamRemote)?.name ?? list[0]?.name ?? null); }) .catch(() => alive && setRemotes([])); return () => { alive = false; }; }, [repo, info?.upstream]); const remoteBranches = useMemo(() => (remote ? remoteBranchesOf(branches, remote) : []), [branches, remote]); // Default: the upstream's branch, else the same name as the current branch. const upstream = remote && info?.upstream?.startsWith(`${remote}/`) ? info.upstream.slice(remote.length + 1) : null; const preferred = upstream ?? info?.head ?? null; const branch = chosenBranch && remoteBranches.includes(chosenBranch) ? chosenBranch : preferred && remoteBranches.includes(preferred) ? preferred : (remoteBranches[0] ?? null); const selectedFlags = rebase ? ["--rebase"] : FLAG_LABELS.filter(([flag]) => flags.has(flag)).map(([, label]) => label); const options: MenuItem[] = [ { label: "--rebase", verbatim: true, checked: rebase, run: () => setRebase(!rebase) }, ...mergeFlagItems(flags, setFlags, rebase), ]; const run = async () => { if (!remote) return; onClose(); await saveAll(); await runRemote( { op: "pullFrom", remote, branches: branch ? [branch] : [], rebase, options: mergeOptionsOf(rebase ? new Set() : flags), }, `${info?.head ?? "HEAD"} ← ${remote}${branch ? `/${branch}` : ""}`, [repo], ); }; return ( } buttons={[ { label: "Pull", role: "default", disabled: !remote, onClick: () => void run() }, { label: "Cancel", role: "cancel" }, ]} >
{remotes && !remotes.length ? (
No remotes are configured. Add one with Git › Manage Remotes….
) : (
git pull