From f6490426f36bf321f0313ea05c7659ec478c7e2b Mon Sep 17 00:00:00 2001 From: Mansur Akhmadi Date: Thu, 19 Nov 2026 03:33:33 +0700 Subject: [PATCH] feat(vcs): choose the repository in the Git Remotes dialog when the project has several (#4373) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Git › Manage Remotes… worked only on the repository selected in the branch widget. With two or more repositories the Git Remotes dialog now shows the Repository combo the Pull, Merge, Rebase, Tag and Stash dialogs use; picking another repository loads its remotes in a fresh list (late answers for the previous pick are dropped), and Add / Remove / Edit act on it. The choice stays local to the dialog. With a single repository the dialog is unchanged. Closes #4372 Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com> --- .../vcs/__tests__/remotesDialogRepo.test.tsx | 110 ++++++++++++++++++ src/features/vcs/gitMenuDialogs.tsx | 90 +++++++------- 2 files changed, 157 insertions(+), 43 deletions(-) create mode 100644 src/features/vcs/__tests__/remotesDialogRepo.test.tsx diff --git a/src/features/vcs/__tests__/remotesDialogRepo.test.tsx b/src/features/vcs/__tests__/remotesDialogRepo.test.tsx new file mode 100644 index 000000000..aad0f1e23 --- /dev/null +++ b/src/features/vcs/__tests__/remotesDialogRepo.test.tsx @@ -0,0 +1,110 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; +import type { Remote, RemoteEdit } from "@/bindings"; + +const mocks = vi.hoisted(() => ({ + edits: [] as Array<{ repo: string | null; edit: RemoteEdit }>, + remotes: {} as Record | Remote[]>, +})); + +vi.mock("@/platform/ipc", async (importOriginal) => { + const real = await importOriginal(); + const ok = (data: unknown) => Promise.resolve({ status: "ok" as const, data }); + return { + ...real, + commands: { + ...real.commands, + vcsRemotes: async (repo: string | null) => ok(await (mocks.remotes[repo ?? ""] ?? [])), + vcsEditRemote: (repo: string | null, edit: RemoteEdit) => { + mocks.edits.push({ repo, edit }); + return ok(null); + }, + vcsBranches: () => ok([]), + }, + }; +}); + +import { VcsDialogHost } from "../dialogs"; +import { showManageRemotes } from "../gitMenuDialogs"; +import { useVcs } from "../store"; + +const remote = (name: string, url: string): Remote => ({ name, url }); + +beforeEach(() => { + mocks.edits = []; + mocks.remotes = { + "/p/a": [remote("origin", "git@example.com:a.git")], + "/p/b": [remote("upstream", "git@example.com:b.git"), remote("fork", "git@example.com:me/b.git")], + }; + useVcs.setState({ root: "/p", roots: ["/p/a", "/p/b"], repo: "/p/a", available: false }); +}); + +// The dialog host outlives a test's render: close Git Remotes so the next test starts with none. +afterEach(() => { + for (const ok of screen.queryAllByRole("button", { name: "OK" })) fireEvent.click(ok); +}); + +const pick = async (combo: string, option: string) => { + fireEvent.click(screen.getByRole("button", { name: combo })); + fireEvent.click(await screen.findByRole("option", { name: option })); +}; + +const addRemote = async (name: string, url: string) => { + fireEvent.click(screen.getByRole("button", { name: "Add Remote…" })); + fireEvent.change(await screen.findByLabelText("Name"), { target: { value: name } }); + fireEvent.change(screen.getByLabelText("URL"), { target: { value: url } }); + // The Define Remote dialog opens over Git Remotes: its OK is the last one. + fireEvent.click(screen.getAllByRole("button", { name: "OK" }).at(-1)!); +}; + +const options = () => screen.queryAllByRole("option").map((o) => o.textContent); + +it("offers no repository choice when the project has one repository", async () => { + useVcs.setState({ roots: ["/p"], repo: null }); + mocks.remotes = { "": [remote("origin", "git@example.com:p.git")] }; + render(); + await act(() => showManageRemotes()); + expect(await screen.findByText("git@example.com:p.git")).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Repository" })).toBeNull(); + + await addRemote("mirror", "git@example.com:mirror.git"); + await waitFor(() => + expect(mocks.edits).toEqual([ + { repo: null, edit: { op: "add", name: "mirror", url: "git@example.com:mirror.git" } }, + ]), + ); +}); + +it("lists and edits the remotes of the repository picked in the dialog", async () => { + render(); + await act(() => showManageRemotes()); + expect(screen.getByRole("button", { name: "Repository" })).toHaveTextContent("a"); + await waitFor(() => expect(options()).toEqual(["origingit@example.com:a.git"])); + + await pick("Repository", "b"); + expect(screen.getByRole("button", { name: "Repository" })).toHaveTextContent("b"); + await waitFor(() => expect(options()).toEqual(["upstreamgit@example.com:b.git", "forkgit@example.com:me/b.git"])); + // The first remote of the picked repository is selected. + expect(screen.getByRole("option", { name: /upstream/ })).toHaveAttribute("aria-selected", "true"); + + // "origin" exists only in a, so the name is free in b. + await addRemote("origin", "git@example.com:b.git"); + await waitFor(() => + expect(mocks.edits).toEqual([{ repo: "/p/b", edit: { op: "add", name: "origin", url: "git@example.com:b.git" } }]), + ); + // Picking in the dialog doesn't change the selected repository elsewhere. + expect(useVcs.getState().repo).toBe("/p/a"); +}); + +it("ignores the remotes of a repository that is no longer picked", async () => { + let resolveA: (remotes: Remote[]) => void = () => undefined; + mocks.remotes["/p/a"] = new Promise((resolve) => (resolveA = resolve)); + render(); + await act(() => showManageRemotes()); + + await pick("Repository", "b"); + await waitFor(() => expect(options()).toHaveLength(2)); + // a's remotes arrive after b was picked: they must not replace b's. + await act(async () => resolveA([remote("origin", "git@example.com:a.git")])); + expect(options()).toEqual(["upstreamgit@example.com:b.git", "forkgit@example.com:me/b.git"]); +}); diff --git a/src/features/vcs/gitMenuDialogs.tsx b/src/features/vcs/gitMenuDialogs.tsx index 9a87aa230..96f366b0c 100644 --- a/src/features/vcs/gitMenuDialogs.tsx +++ b/src/features/vcs/gitMenuDialogs.tsx @@ -473,7 +473,25 @@ export function showManageRemotes() { } function RemotesDialog({ onClose }: { onClose: () => void }) { - const [repo] = useState(currentRepo); + const [repo, setRepo] = useState(currentRepo); + return ( + +
+ + {/* Keyed: picking another repository starts a fresh list, so answers for the previous one are dropped. */} + +
+
+ ); +} + +function RemotesList({ repo }: { repo: string | null }) { const [remotes, setRemotes] = useState([]); const [selected, setSelected] = useState(null); const [error, setError] = useState(null); @@ -527,49 +545,35 @@ function RemotesDialog({ onClose }: { onClose: () => void }) { const current = remotes.find((r) => r.name === selected); return ( - -
-
- edit()} /> - void remove()} - /> - edit(current)} /> -
-
- {!remotes.length && ( -
- No remotes -
- )} - {remotes.map((r) => ( - - ))} -
- {error &&
{error}
} + <> +
+ edit()} /> + void remove()} /> + edit(current)} />
-
+
+ {!remotes.length && ( +
+ No remotes +
+ )} + {remotes.map((r) => ( + + ))} +
+ {error &&
{error}
} + ); }