feat(vcs): choose the repository in the Pull dialog when the project has several (#3312)

With two or more Git repositories, the Pull dialog shows a Repository combo before the remote. Picking one reloads its remotes, branches and branch info, and Pull runs in that repository; the choice stays local to the dialog.

Closes #3311

Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
This commit is contained in:
Mansur Akhmadi
2026-11-14 16:21:49 +08:00
committed by GitHub
co-authored by mansur-akhmadi
parent ec862b3668
commit d3720c0e11
2 changed files with 169 additions and 7 deletions
@@ -0,0 +1,118 @@
import { beforeEach, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import type { RemoteOp } from "@/bindings";
const mocks = vi.hoisted(() => ({
ops: [] as Array<{ repo: string | null; op: RemoteOp }>,
}));
vi.mock("@tauri-apps/api/core", async (importOriginal) => ({
...(await importOriginal<typeof import("@tauri-apps/api/core")>()),
Channel: class {
onmessage: (line: string) => void = () => {};
},
}));
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
const ok = (data: unknown) => Promise.resolve({ status: "ok" as const, data });
const head = (repo: string | null) => (repo === "/p/b" ? "dev" : "main");
return {
...real,
commands: {
...real.commands,
vcsBranchInfo: (repo: string | null) =>
ok({
head: head(repo),
headCommit: "c1",
upstream: `origin/${head(repo)}`,
ahead: 0,
behind: 0,
operation: null,
}),
vcsBranches: (repo: string | null) =>
ok(
(repo === "/p/b" ? ["dev", "main"] : ["main"]).map((name) => ({
name: `${repo === "/p/b" ? "upstream" : "origin"}/${name}`,
remote: true,
current: false,
})),
),
vcsRemotes: (repo: string | null) =>
ok(repo === "/p/c" ? [] : [{ name: repo === "/p/b" ? "upstream" : "origin", fetchUrl: "/r", pushUrl: "/r" }]),
vcsRemote: (repo: string | null, _request: number, op: RemoteOp) => {
mocks.ops.push({ repo, op });
return ok(null);
},
cancelRequest: () => ok(null),
},
};
});
import { VcsDialogHost } from "../dialogs";
import { showPullDialog } from "../gitMenuDialogs";
import { useVcs } from "../store";
beforeEach(() => {
mocks.ops = [];
useVcs.setState({
root: "/p",
roots: ["/p/a", "/p/b"],
repo: "/p/a",
branch: { head: "main", headCommit: "c1", upstream: "origin/main", ahead: 0, behind: 0, operation: null },
remoteBusy: null,
available: false,
});
});
it("offers no repository choice when the project has one repository", async () => {
useVcs.setState({ roots: ["/p"], repo: null });
render(<VcsDialogHost />);
await act(() => showPullDialog());
await waitFor(() => expect(screen.getByRole("button", { name: "Remote" })).toHaveTextContent("origin"));
expect(screen.queryByRole("button", { name: "Repository" })).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Pull" }));
await waitFor(() => expect(mocks.ops).toHaveLength(1));
expect(mocks.ops[0]).toMatchObject({ repo: null, op: { op: "pullFrom", remote: "origin", branches: ["main"] } });
});
it("pulls into the repository picked in the dialog, with that repository's remote and branch", async () => {
render(<VcsDialogHost />);
await act(() => showPullDialog());
expect(await screen.findByText("Pull to main")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Repository" }));
fireEvent.click(await screen.findByRole("option", { name: "b" }));
expect(await screen.findByText("Pull to dev")).toBeInTheDocument();
await waitFor(() => expect(screen.getByRole("button", { name: "Remote" })).toHaveTextContent("upstream"));
await waitFor(() => expect(screen.getByRole("button", { name: "Remote branch" })).toHaveTextContent("dev"));
fireEvent.click(screen.getByRole("button", { name: "Pull" }));
await waitFor(() => expect(mocks.ops).toHaveLength(1));
expect(mocks.ops[0]).toMatchObject({
repo: "/p/b",
op: { op: "pullFrom", remote: "upstream", branches: ["dev"], rebase: false },
});
});
it("keeps the repository choice when the picked repository has no remotes", async () => {
useVcs.setState({ roots: ["/p/a", "/p/b", "/p/c"] });
render(<VcsDialogHost />);
await act(() => showPullDialog());
await waitFor(() => expect(screen.getByRole("button", { name: "Remote" })).toHaveTextContent("origin"));
fireEvent.click(screen.getByRole("button", { name: "Repository" }));
fireEvent.click(await screen.findByRole("option", { name: "c" }));
expect(await screen.findByText(/No remotes are configured/)).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Pull" })).toBeDisabled();
fireEvent.click(screen.getByRole("button", { name: "Repository" }));
fireEvent.click(await screen.findByRole("option", { name: "a" }));
await waitFor(() => expect(screen.getByRole("button", { name: "Remote" })).toHaveTextContent("origin"));
expect(await screen.findByText("Pull to main")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Pull" })).toBeEnabled();
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(mocks.ops).toEqual([]);
});
+51 -7
View File
@@ -1,7 +1,7 @@
/** Git menu dialogs: Pull…, Merge…, Rebase…, Manage Remotes… and Worktrees. */
import { useEffect, useMemo, useRef, useState } from "react";
import clsx from "clsx";
import type { Branch, MergeOptions, Remote, Worktree } from "@/bindings";
import type { Branch, BranchInfo, MergeOptions, Remote, Worktree } from "@/bindings";
import { executeAction } from "@/platform/actions";
import { call, commands } from "@/platform/ipc";
import { notifications } from "@/workbench/contributions";
@@ -133,9 +133,41 @@ export function showPullDialog() {
openDialog((close) => <PullDialog onClose={close} />);
}
/** IDEA's Git Root choice: shown only when the project holds several repositories. */
function RepoPicker({ repo, onChange }: { repo: string | null; onChange: (root: string) => void }) {
const [roots] = useState(() => useVcs.getState().roots);
if (roots.length < 2) return null;
return (
<Select
aria-label="Repository"
value={repo ?? roots[0] ?? null}
options={roots.map((root) => ({ 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. */
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({ onClose }: { onClose: () => void }) {
const [repo] = useState(currentRepo);
const [info] = useState(() => useVcs.getState().branch);
const [repo, setRepo] = useState(currentRepo);
const info = useBranchInfo(repo);
const branches = useBranches(repo);
const [remotes, setRemotes] = useState<Remote[] | null>(null);
const [remote, setRemote] = useState<string | null>(null);
@@ -143,6 +175,14 @@ function PullDialog({ onClose }: { onClose: () => void }) {
const [rebase, setRebase] = useState(false);
const [flags, setFlags] = useState<Set<MergeFlag>>(() => 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))
@@ -156,7 +196,7 @@ function PullDialog({ onClose }: { onClose: () => void }) {
return () => {
alive = false;
};
}, [info?.upstream]);
}, [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.
@@ -199,11 +239,15 @@ function PullDialog({ onClose }: { onClose: () => void }) {
>
<div className={styles.form}>
{remotes && !remotes.length ? (
<div className={styles.hint}>
<T>No remotes are configured. Add one with Git › Manage Remotes….</T>
</div>
<>
<RepoPicker repo={repo} onChange={chooseRepo} />
<div className={styles.hint}>
<T>No remotes are configured. Add one with Git › Manage Remotes….</T>
</div>
</>
) : (
<div className={styles.fieldRow}>
<RepoPicker repo={repo} onChange={chooseRepo} />
<Select
aria-label="Remote"
value={remote}