feat(vcs): choose the repository in the Tag dialog when the project has several (#3488)

With several repositories the Tag dialog shows the repository combo (as Pull, Merge and Rebase do); tags are checked and created in the picked repository, ignoring late answers for the previous pick.

Co-authored-by: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
This commit is contained in:
Mansur Akhmadi
2026-11-15 16:42:12 +08:00
committed by GitHub
co-authored by mansur-akhmadi
parent 832c397e0b
commit f80dd0aa49
3 changed files with 129 additions and 4 deletions
@@ -0,0 +1,111 @@
import { beforeEach, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
const mocks = vi.hoisted(() => ({
created: [] as Array<{ repo: string | null; name: string; commit: string; message: string | null; force: boolean }>,
tags: {} as Record<string, Promise<string[]> | string[]>,
}));
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,
vcsTags: async (repo: string | null) => ({ status: "ok" as const, data: await (mocks.tags[repo ?? ""] ?? []) }),
vcsCreateTag: (repo: string | null, name: string, commit: string, message: string | null, force: boolean) => {
mocks.created.push({ repo, name, commit, message, force });
return ok(null);
},
},
};
});
import { VcsDialogHost } from "../dialogs";
import { showTagDialog } from "../operations";
import { useVcs } from "../store";
beforeEach(() => {
mocks.created = [];
mocks.tags = { "/p/a": ["v1.0"], "/p/b": ["v2.0"], "/p": ["v1.0"] };
useVcs.setState({
root: "/p",
roots: ["/p/a", "/p/b"],
repo: "/p/a",
branch: { head: "main", headCommit: "c1", upstream: null, ahead: 0, behind: 0, operation: null },
available: false,
});
});
const pick = async (combo: string, option: string) => {
fireEvent.click(screen.getByRole("button", { name: combo }));
fireEvent.click(await screen.findByRole("option", { name: option }));
};
const typeName = (value: string) => fireEvent.change(screen.getByLabelText("Tag name:"), { target: { value } });
it("offers no repository choice when the project has one repository", async () => {
useVcs.setState({ roots: ["/p"], repo: null });
mocks.tags = { "": ["v1.0"] };
render(<VcsDialogHost />);
await act(() => showTagDialog());
expect(await screen.findByText("Tag")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Repository" })).toBeNull();
typeName("v1.0");
expect(await screen.findByText("Tag 'v1.0' already exists")).toBeInTheDocument();
typeName("v1.1");
fireEvent.click(screen.getByRole("button", { name: "Create Tag" }));
await waitFor(() => expect(mocks.created).toHaveLength(1));
expect(mocks.created[0]).toMatchObject({ repo: null, name: "v1.1", commit: "HEAD" });
});
it("creates the tag in the repository picked in the dialog", async () => {
render(<VcsDialogHost />);
await act(() => showTagDialog());
expect(screen.getByRole("button", { name: "Repository" })).toHaveTextContent("a");
await pick("Repository", "b");
expect(screen.getByRole("button", { name: "Repository" })).toHaveTextContent("b");
typeName("v3.0");
fireEvent.change(screen.getByLabelText("Message:"), { target: { value: "Release" } });
fireEvent.click(screen.getByRole("button", { name: "Create Tag" }));
await waitFor(() => expect(mocks.created).toHaveLength(1));
expect(mocks.created[0]).toEqual({ repo: "/p/b", name: "v3.0", commit: "HEAD", message: "Release", force: false });
});
it("checks the name against the tags of the picked repository", async () => {
render(<VcsDialogHost />);
await act(() => showTagDialog());
typeName("v1.0");
expect(await screen.findByText("Tag 'v1.0' already exists")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Create Tag" })).toBeDisabled();
// v1.0 exists only in a: the name is free in b, and b's own v2.0 is taken.
await pick("Repository", "b");
await waitFor(() => expect(screen.queryByText("Tag 'v1.0' already exists")).toBeNull());
expect(screen.getByRole("button", { name: "Create Tag" })).toBeEnabled();
typeName("v2.0");
expect(await screen.findByText("Tag 'v2.0' already exists")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(mocks.created).toEqual([]);
});
it("ignores the tags of a repository that is no longer picked", async () => {
let resolveA: (tags: string[]) => void = () => undefined;
mocks.tags["/p/a"] = new Promise<string[]>((resolve) => (resolveA = resolve));
render(<VcsDialogHost />);
await act(() => showTagDialog());
await pick("Repository", "b");
typeName("v1.0");
// a's tags arrive after b was picked: they must not mark v1.0 as taken in b.
await act(async () => resolveA(["v1.0"]));
expect(screen.queryByText("Tag 'v1.0' already exists")).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Create Tag" }));
await waitFor(() => expect(mocks.created).toHaveLength(1));
expect(mocks.created[0]).toMatchObject({ repo: "/p/b", name: "v1.0" });
});
+1 -1
View File
@@ -135,7 +135,7 @@ export function showPullDialog(repo?: string | null) {
}
/** IDEA's Git Root choice: shown only when the project holds several repositories. */
function RepoPicker({ repo, onChange }: { repo: string | null; onChange: (root: string) => void }) {
export function RepoPicker({ repo, onChange }: { repo: string | null; onChange: (root: string) => void }) {
const [roots] = useState(() => useVcs.getState().roots);
if (roots.length < 2) return null;
return (
+17 -3
View File
@@ -27,6 +27,7 @@ import { buildFileTree, type FileTreeNode } from "./fileTree";
import { tx } from "@/platform/i18n";
import { displayPath } from "./compareData";
import { repositoryPath } from "./repositoryPaths";
import { RepoPicker } from "./gitMenuDialogs";
// ---------------------------------------------------------------------------
// Operation in progress
@@ -437,15 +438,27 @@ export function showTagDialog() {
}
function TagDialog({ onClose }: { onClose: () => void }) {
const [repo] = useState(currentRepo);
const [repo, setRepo] = useState(currentRepo);
const [name, setName] = useState("");
const [commit, setCommit] = useState("HEAD");
const [message, setMessage] = useState("");
const [force, setForce] = useState(false);
const [existing, setExisting] = useState<string[]>([]);
useEffect(() => {
void call(commands.vcsTags(repo)).then(setExisting, () => undefined);
}, []);
let live = true;
void call(commands.vcsTags(repo)).then(
(tags) => live && setExisting(tags),
() => undefined,
);
return () => {
live = false;
};
}, [repo]);
const chooseRepo = (root: string) => {
if (root === repo) return;
setRepo(root);
setExisting([]);
};
const error = validateTagName(name, force ? [] : existing);
const run = async () => {
if (error) return;
@@ -470,6 +483,7 @@ function TagDialog({ onClose }: { onClose: () => void }) {
]}
>
<div className={styles.form}>
<RepoPicker repo={repo} onChange={chooseRepo} />
<label className={styles.fieldLabel} htmlFor="openidea-tag-name">
<T>Tag name:</T>
</label>