mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
feat(vcs): choose the repository in the Git Remotes dialog when the project has several (#4373)
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>
This commit is contained in:
co-authored by
mansur-akhmadi
parent
669ea49e3d
commit
f6490426f3
@@ -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<string, Promise<Remote[]> | Remote[]>,
|
||||
}));
|
||||
|
||||
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 });
|
||||
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(<VcsDialogHost />);
|
||||
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(<VcsDialogHost />);
|
||||
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<Remote[]>((resolve) => (resolveA = resolve));
|
||||
render(<VcsDialogHost />);
|
||||
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"]);
|
||||
});
|
||||
@@ -473,7 +473,25 @@ export function showManageRemotes() {
|
||||
}
|
||||
|
||||
function RemotesDialog({ onClose }: { onClose: () => void }) {
|
||||
const [repo] = useState(currentRepo);
|
||||
const [repo, setRepo] = useState(currentRepo);
|
||||
return (
|
||||
<Dialog
|
||||
open
|
||||
title="Git Remotes"
|
||||
onClose={onClose}
|
||||
width={560}
|
||||
buttons={[{ label: "OK", role: "default", autoFocus: true, onClick: onClose }]}
|
||||
>
|
||||
<div className={styles.form}>
|
||||
<RepoPicker repo={repo} onChange={setRepo} />
|
||||
{/* Keyed: picking another repository starts a fresh list, so answers for the previous one are dropped. */}
|
||||
<RemotesList key={repo} repo={repo} />
|
||||
</div>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
function RemotesList({ repo }: { repo: string | null }) {
|
||||
const [remotes, setRemotes] = useState<Remote[]>([]);
|
||||
const [selected, setSelected] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -527,49 +545,35 @@ function RemotesDialog({ onClose }: { onClose: () => void }) {
|
||||
const current = remotes.find((r) => r.name === selected);
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open
|
||||
title="Git Remotes"
|
||||
onClose={onClose}
|
||||
width={560}
|
||||
buttons={[{ label: "OK", role: "default", autoFocus: true, onClick: onClose }]}
|
||||
>
|
||||
<div className={styles.form}>
|
||||
<div className={styles.manageBar} role="toolbar" aria-label={tx("Remotes")}>
|
||||
<IconButton icon="plus" title="Add Remote…" size="small" onClick={() => edit()} />
|
||||
<IconButton
|
||||
icon="minus"
|
||||
title="Remove Remote"
|
||||
size="small"
|
||||
disabled={!current}
|
||||
onClick={() => void remove()}
|
||||
/>
|
||||
<IconButton icon="edit" title="Edit Remote…" size="small" disabled={!current} onClick={() => edit(current)} />
|
||||
</div>
|
||||
<div className={styles.choiceList} role="listbox" aria-label={tx("Remotes")}>
|
||||
{!remotes.length && (
|
||||
<div className={styles.empty}>
|
||||
<T>No remotes</T>
|
||||
</div>
|
||||
)}
|
||||
{remotes.map((r) => (
|
||||
<button
|
||||
key={r.name}
|
||||
type="button"
|
||||
role="option"
|
||||
aria-selected={r.name === selected}
|
||||
className={clsx(styles.manageRow, r.name === selected && styles.manageRowSelected)}
|
||||
onClick={() => setSelected(r.name)}
|
||||
onDoubleClick={() => edit(r)}
|
||||
>
|
||||
<span className={styles.fileName}>{r.name}</span>
|
||||
<span className={clsx(styles.fileDir, styles.mono)}>{r.url}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{error && <div className={styles.error}>{error}</div>}
|
||||
<>
|
||||
<div className={styles.manageBar} role="toolbar" aria-label={tx("Remotes")}>
|
||||
<IconButton icon="plus" title="Add Remote…" size="small" onClick={() => edit()} />
|
||||
<IconButton icon="minus" title="Remove Remote" size="small" disabled={!current} onClick={() => void remove()} />
|
||||
<IconButton icon="edit" title="Edit Remote…" size="small" disabled={!current} onClick={() => edit(current)} />
|
||||
</div>
|
||||
</Dialog>
|
||||
<div className={styles.choiceList} role="listbox" aria-label={tx("Remotes")}>
|
||||
{!remotes.length && (
|
||||
<div className={styles.empty}>
|
||||
<T>No remotes</T>
|
||||
</div>
|
||||
)}
|
||||
{remotes.map((r) => (
|
||||
<button
|
||||
key={r.name}
|
||||
type="button"
|
||||
role="option"
|
||||
aria-selected={r.name === selected}
|
||||
className={clsx(styles.manageRow, r.name === selected && styles.manageRowSelected)}
|
||||
onClick={() => setSelected(r.name)}
|
||||
onDoubleClick={() => edit(r)}
|
||||
>
|
||||
<span className={styles.fileName}>{r.name}</span>
|
||||
<span className={clsx(styles.fileDir, styles.mono)}>{r.url}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{error && <div className={styles.error}>{error}</div>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user