diff --git a/src/features/vcs/PushTagOptions.tsx b/src/features/vcs/PushTagOptions.tsx index a2617d1af3..955fb3abde 100644 --- a/src/features/vcs/PushTagOptions.tsx +++ b/src/features/vcs/PushTagOptions.tsx @@ -6,11 +6,12 @@ import { Checkbox, Select } from "@/ui"; import { useVcs } from "./store"; import styles from "./common.module.css"; -function storageKey(): string | null { +/** `prefix` scoped to the open project, like IDEA's workspace-level push settings. */ +export function projectPushStorageKey(prefix: string): string | null { const root = useProject.getState().project?.root ?? useVcs.getState().root; if (!root) return null; const target = useRemote.getState().session?.target; - return `openidea.vcs.pushTags:${target ? remoteLocation(target, root) : root}`; + return `${prefix}:${target ? remoteLocation(target, root) : root}`; } function readMode(key: string | null): PushTags | null { @@ -25,7 +26,7 @@ function readMode(key: string | null): PushTags | null { export function usePushTagOptions() { const [{ key, initial }] = useState(() => { - const key = storageKey(); + const key = projectPushStorageKey("openidea.vcs.pushTags"); return { key, initial: readMode(key) }; }); const [enabled, setEnabled] = useState(initial !== null); diff --git a/src/features/vcs/__tests__/multiPushTarget.test.tsx b/src/features/vcs/__tests__/multiPushTarget.test.tsx index f6d6870bbb..53a5867ed1 100644 --- a/src/features/vcs/__tests__/multiPushTarget.test.tsx +++ b/src/features/vcs/__tests__/multiPushTarget.test.tsx @@ -16,6 +16,7 @@ const mocks = vi.hoisted(() => ({ log: vi.fn(), changes: vi.fn(), ask: vi.fn(), + secondRemote: "fork", })); vi.mock("@/workbench/messages", async (importOriginal) => ({ @@ -63,7 +64,11 @@ vi.mock("@/platform/ipc", async (importOriginal) => { }, vcsRemotes: (repo: string) => ok( - mocks.unconfigured.has(repo) ? [] : repo === "/p/b" ? [remote("origin"), remote("fork")] : [remote("origin")], + mocks.unconfigured.has(repo) + ? [] + : repo === "/p/b" + ? [remote("origin"), remote(mocks.secondRemote)] + : [remote("origin")], ), vcsEditRemote: (repo: string, operation: unknown) => { mocks.editRemote(repo, operation); @@ -111,6 +116,7 @@ vi.mock("@/platform/ipc", async (importOriginal) => { import { provideNotificationService } from "@/workbench/contributions"; import { getContext, setContext } from "@/platform/context"; +import { useProject } from "@/platform/project"; import { registerVcsActions } from "../actions"; import { commonBranchMenu } from "../branchMenu"; import { VcsDialogHost } from "../dialogs"; @@ -120,6 +126,8 @@ import { useVcs } from "../store"; type Note = { title: string; body?: string }; let notes: Note[] = []; +const initialProject = useProject.getState().project; + beforeEach(() => { localStorage.clear(); mocks.ops = []; @@ -136,8 +144,10 @@ beforeEach(() => { mocks.changes.mockReset(); mocks.ask.mockReset(); mocks.ask.mockResolvedValue({ button: "force", checked: false }); + mocks.secondRemote = "fork"; notes = []; useVcs.setState({ root: "/p", roots: ["/p/a", "/p/b", "/p/c"], repo: "/p/a", remoteBusy: null, available: false }); + useProject.setState({ project: initialProject }); provideNotificationService({ notify: (n) => void notes.push(n as Note), expire: () => {} }); }); @@ -459,6 +469,22 @@ it("asks to confirm each forced target and pushes nothing when one is declined", fireEvent.click(screen.getByRole("button", { name: "Cancel" })); }); +it("stops asking and pushes nothing when the project changes during a confirmation", async () => { + mocks.ask.mockImplementationOnce(async () => { + useProject.setState({ project: { root: "/q" } as never }); + return { button: "force", checked: false }; + }); + await openPushDialog(); + fireEvent.change(screen.getByRole("combobox", { name: "Target branch of a" }), { target: { value: "feat-a" } }); + fireEvent.click(screen.getByRole("checkbox", { name: "Force (with lease)" })); + fireEvent.click(screen.getByRole("button", { name: "Force Push" })); + + await waitFor(() => expect(mocks.ask).toHaveBeenCalledTimes(1)); + await act(() => Promise.resolve()); + expect(mocks.ask).toHaveBeenCalledTimes(1); + expect(mocks.ops).toEqual([]); +}); + it("force pushes with lease once every target is confirmed", async () => { await openPushDialog(); fireEvent.click(screen.getByRole("checkbox", { name: "Force (with lease)" })); @@ -497,6 +523,69 @@ it("does not ask without Force", async () => { expect(mocks.ask).not.toHaveBeenCalled(); }); +/** Force pushes `repo` alone, optionally to branch `target` of `remote`. */ +async function forcePushFrom(repo: string, remote?: string, target?: string) { + const view = render(); + await act(() => showPushDialog({ repo })); + const name = repo.slice(repo.lastIndexOf("/") + 1); + const branch = await screen.findByRole("combobox", { name: `Target branch of ${name}` }); + if (remote) { + fireEvent.click(screen.getByRole("button", { name: `Remote of ${name}` })); + fireEvent.click(await screen.findByRole("option", { name: remote })); + } + if (target) fireEvent.change(branch, { target: { value: target } }); + fireEvent.click(screen.getByRole("checkbox", { name: "Force (with lease)" })); + const before = mocks.ops.length; + fireEvent.click(screen.getByRole("button", { name: "Force Push" })); + await waitFor(() => expect(mocks.ops).toHaveLength(before + 1)); + view.unmount(); +} + +it("remembers Don't warn about this target for the project it was ticked in", async () => { + mocks.ask.mockResolvedValue({ button: "force", checked: true }); + await forcePushFrom("/p/a"); + await forcePushFrom("/p/a"); + expect(askedTargets()).toEqual(["origin/feat-x"]); + + // origin/feat-x of another project is another branch: it still asks. + useVcs.setState({ root: "/q", roots: ["/q/a", "/q/b"], repo: "/q/a" }); + await forcePushFrom("/q/a"); + expect(askedTargets()).toEqual(["origin/feat-x", "origin/feat-x"]); +}); + +it("keeps a silenced target apart from another remote and branch that read the same", async () => { + // `origin` + `team/feature` and `origin/team` + `feature` both read `origin/team/feature`. + mocks.secondRemote = "origin/team"; + mocks.ask.mockResolvedValue({ button: "force", checked: true }); + await forcePushFrom("/p/b", "origin", "team/feature"); + await forcePushFrom("/p/b", "origin", "team/feature"); + expect(mocks.ask).toHaveBeenCalledTimes(1); + + await forcePushFrom("/p/b", "origin/team", "feature"); + expect(askedTargets()).toEqual(["origin/team/feature", "origin/team/feature"]); + expect(mocks.ops.at(-1)?.op).toMatchObject({ selection: { remote: "origin/team", target: "feature" } }); +}); + +it("confirms two repositories' targets one by one when only their joined names are the same", async () => { + mocks.secondRemote = "origin/team"; + await openPushDialog(); + check("c"); + fireEvent.change(screen.getByRole("combobox", { name: "Target branch of a" }), { target: { value: "team/feature" } }); + fireEvent.click(screen.getByRole("button", { name: "Remote of b" })); + fireEvent.click(await screen.findByRole("option", { name: "origin/team" })); + fireEvent.change(screen.getByRole("combobox", { name: "Target branch of b" }), { target: { value: "feature" } }); + fireEvent.click(screen.getByRole("checkbox", { name: "Force (with lease)" })); + fireEvent.click(screen.getByRole("button", { name: "Force Push" })); + await waitFor(() => expect(mocks.ops).toHaveLength(2)); + expect(askedTargets()).toEqual(["origin/team/feature", "origin/team/feature"]); +}); + +it("does not take the earlier app-wide silenced targets for this project", async () => { + localStorage.setItem("openidea.vcs.silentForcePushTargets", JSON.stringify(["origin/feat-x"])); + await forcePushFrom("/p/a"); + expect(askedTargets()).toEqual(["origin/feat-x"]); +}); + it("preselects the repositories that were committed before opening Push", async () => { render(); await act(() => showPushDialog({ selected: ["/p/b"] })); diff --git a/src/features/vcs/__tests__/singlePush.test.tsx b/src/features/vcs/__tests__/singlePush.test.tsx index 14029dc7ff..d325d77382 100644 --- a/src/features/vcs/__tests__/singlePush.test.tsx +++ b/src/features/vcs/__tests__/singlePush.test.tsx @@ -13,6 +13,12 @@ const mocks = vi.hoisted(() => ({ editRemote: vi.fn(), branchInfo: vi.fn(), failure: vi.fn(), + ask: vi.fn(), +})); + +vi.mock("@/workbench/messages", async (importOriginal) => ({ + ...(await importOriginal()), + ask: mocks.ask, })); vi.mock("@tauri-apps/api/core", async (importOriginal) => ({ @@ -106,6 +112,7 @@ beforeEach(() => { operation: null, }); mocks.failure.mockReset(); + mocks.ask.mockReset(); useProject.setState({ settings: null }); provideNotificationService({ notify: () => {}, expire: () => {} }); useVcs.setState({ @@ -458,6 +465,27 @@ it("remembers the submitted tag option for the project without saving cancelled expect(await open()).not.toBeChecked(); }); +it("asks again before a force push to the same target in another project", async () => { + mocks.inspect.mockReturnValue({ state: "ready", localTip: "c2", remoteTip: "r1", remoteCommits: 0 }); + mocks.ask.mockResolvedValue({ button: "force", checked: true }); + render(); + const forcePush = async () => { + await act(() => showPushDialog()); + fireEvent.click(await screen.findByRole("checkbox", { name: "Force (with lease)" })); + const before = mocks.remote.mock.calls.length; + fireEvent.click(screen.getByRole("button", { name: "Force Push" })); + await waitFor(() => expect(mocks.remote).toHaveBeenCalledTimes(before + 1)); + await waitFor(() => expect(useVcs.getState().remoteBusy).toBeNull()); + }; + await forcePush(); + await forcePush(); + expect(mocks.ask).toHaveBeenCalledTimes(1); + + useVcs.setState({ root: "/other", repo: "/other", roots: ["/other"] }); + await forcePush(); + expect(mocks.ask).toHaveBeenCalledTimes(2); +}); + it("offers to define another remote without changing the destination being reviewed", async () => { const previous = getContext().vcsAvailable; setContext("vcsAvailable", true); diff --git a/src/features/vcs/remoteUi.tsx b/src/features/vcs/remoteUi.tsx index fd03a0ed22..b2e822ffe1 100644 --- a/src/features/vcs/remoteUi.tsx +++ b/src/features/vcs/remoteUi.tsx @@ -40,7 +40,7 @@ import { } from "./ops"; import { PushChanges, pushChangesKey } from "./PushChanges"; import { PushCheckStatus } from "./PushCheckStatus"; -import { PushTagOptions, usePushTagOptions } from "./PushTagOptions"; +import { PushTagOptions, projectPushStorageKey, usePushTagOptions } from "./PushTagOptions"; import { PushReview, usePushDiff } from "./PushDiff"; import { PushInspectionBanner, @@ -672,14 +672,15 @@ function MultiPushDialog({ if (inspection.needsFetch) return inspection.fetch(); // Like the single-repository dialog: each forced target is confirmed first (once, when several // repositories push to the same one), and declining one pushes nothing. - const forced = new Set(); + const forced = new Map(); for (const r of force && forceAvailable ? picked : []) { const target = targetNameOf(r); - const remote = shownRemoteOf(r); - if (!isProtectedBranch(target) && !specialTarget(r)) forced.add(remote ? `${remote}/${target}` : target); + const remote = shownRemoteOf(r) ?? ""; + if (!isProtectedBranch(target) && !specialTarget(r)) + forced.set(JSON.stringify([remote, target]), [remote, target]); } - for (const target of forced) if (!(await confirmForcePush(target))) return; - if (useProject.getState().project !== project) return; + for (const [remote, target] of forced.values()) + if (!(await confirmForcePush(remote, target)) || useProject.getState().project !== project) return; const tags = tagOptions.rememberForPush(); onClose(); await pushTargets( @@ -1210,7 +1211,7 @@ function PushDialog({ const push = async () => { if (!selection || inspection.fetching || useProject.getState().project !== project) return; if (inspection.needsFetch) return inspection.fetch(); - if (force && forceAvailable && !(await confirmForcePush(remote ? `${remote}/${target}` : (target ?? "")))) return; + if (force && forceAvailable && !(await confirmForcePush(remote ?? "", target ?? ""))) return; if (useProject.getState().project !== project) return; const options = { forceWithLease: force && forceAvailable, @@ -1387,31 +1388,39 @@ function PushDialog({ ); } -const SILENT_FORCE_PUSH_KEY = "openidea.vcs.silentForcePushTargets"; +type ForcePushTarget = [remote: string, branch: string]; -function silentForcePushTargets(): string[] { +/** The project's silenced (remote, branch) pairs: a joined `remote/branch` would be ambiguous. */ +function silentForcePushTargets(key: string | null): ForcePushTarget[] { try { - const v: unknown = JSON.parse(localStorage.getItem(SILENT_FORCE_PUSH_KEY) ?? "[]"); - return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : []; + const v: unknown = key ? JSON.parse(localStorage.getItem(key) ?? "[]") : []; + return Array.isArray(v) + ? v.filter( + (x): x is ForcePushTarget => Array.isArray(x) && x.length === 2 && x.every((s) => typeof s === "string"), + ) + : []; } catch { return []; } } -/** IDEA's force push confirmation, with "Don't warn about this target". */ -async function confirmForcePush(target: string): Promise { - if (target && silentForcePushTargets().includes(target)) return true; +/** IDEA's force push confirmation, with "Don't warn about this target" remembered per project. */ +async function confirmForcePush(remote: string, branch: string): Promise { + // Taken before asking: the project may change while the confirmation is open. + const key = branch ? projectPushStorageKey("openidea.vcs.silentForcePushTargets") : null; + if (silentForcePushTargets(key).some(([r, b]) => r === remote && b === branch)) return true; + const target = remote ? `${remote}/${branch}` : branch; const r = await ask({ title: "Force Push", body: tx`You're going to force push to '${target}'. It may overwrite commits at the remote. Are you sure you want to proceed?`, icon: "warning", - checkbox: target ? "Don't warn about this target" : undefined, + checkbox: key ? "Don't warn about this target" : undefined, buttons: [{ id: "force", label: "Force Push", primary: true }], }); if (r.button !== "force") return false; - if (r.checked && target) { + if (r.checked && key) { try { - localStorage.setItem(SILENT_FORCE_PUSH_KEY, JSON.stringify([...silentForcePushTargets(), target])); + localStorage.setItem(key, JSON.stringify([...silentForcePushTargets(key), [remote, branch]])); } catch { // Not remembered: the next force push asks again. }