From 2a26c823577f4552ec95058e9b6e18ec8d884bf8 Mon Sep 17 00:00:00 2001 From: Muu Date: Fri, 27 Nov 2026 14:27:05 +0800 Subject: [PATCH] fix(vcs): the Git Log branch filter follows a branch rename (#6341) Renaming a branch left the Git Log filtering the old name: the Branch chip kept it and the query failed with unknown revision. renameBranchInLogFilters now rewrites the shown view's branch filter, parked log views, the per-repository remembered choice and Log: tab targets, and reloads the log when the shown view filtered by the old name. A batch rename follows only when every repository was renamed. Co-authored-by: muutot <92625353+muutot@users.noreply.github.com> --- e2e/logBranchRenameFollow.spec.ts | 148 ++++++++++++++++++ .../vcs/__tests__/logBranchMemory.test.ts | 39 +++++ src/features/vcs/branchOps.ts | 9 +- src/features/vcs/logStore.ts | 22 +++ 4 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 e2e/logBranchRenameFollow.spec.ts diff --git a/e2e/logBranchRenameFollow.spec.ts b/e2e/logBranchRenameFollow.spec.ts new file mode 100644 index 0000000000..f60232491b --- /dev/null +++ b/e2e/logBranchRenameFollow.spec.ts @@ -0,0 +1,148 @@ +/** + * Renaming a branch follows in the Git Log: the Branch filter chip keeps naming + * the branch under its new name (and the remembered choice follows), instead of + * filtering an unknown revision. Git is stubbed at the IPC layer. + */ +import type { Page } from "@playwright/test"; +import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; + +const ROOT = "/dev/acme-api"; + +interface StubBranch { + name: string; + current: boolean; + remote: boolean; + upstream: string | null; + ahead: number; + behind: number; + lastCommitMs: number | null; + commit: string; +} + +/** Installs a two-branch repository (`sd` checked out) and opens the Git Log on `HEAD`. */ +async function openLogOnSd(page: Page) { + await stubDesktop(page); + await page.evaluate(async (root) => { + const h = window.__ipc.handlers; + const commits = Array.from({ length: 6 }, (_, i) => ({ + id: i.toString(16).padStart(40, "0"), + shortId: i.toString(16).padStart(8, "0"), + parents: i + 1 < 6 ? [(i + 1).toString(16).padStart(40, "0")] : [], + refs: i === 0 ? ["HEAD", "sd"] : [], + author: "Author", + email: "author@example.com", + timeMs: 20 - i, + summary: `Commit ${i}`, + message: `Commit ${i}`, + })); + let head = "sd"; + let branches: StubBranch[] = [ + { + name: "sd", + current: true, + remote: false, + upstream: null, + ahead: 0, + behind: 0, + lastCommitMs: 20, + commit: commits[0]!.id, + }, + { + name: "main", + current: false, + remote: false, + upstream: null, + ahead: 0, + behind: 0, + lastCommitMs: 20, + commit: commits[5]!.id, + }, + ]; + h.vcs_log = ({ query }) => { + const { skip, limit } = query as { skip: number; limit: number }; + return commits.slice(skip, limit ? skip + limit : undefined); + }; + h.vcs_log_refs = () => ({}); + h.vcs_log_users = () => ["Author "]; + h.vcs_roots = () => [{ root, operation: null, head, headCommit: commits[0]!.id, hasRemotes: false }]; + h.vcs_branch_info = () => ({ + head, + headCommit: commits[0]!.id, + upstream: null, + ahead: 0, + behind: 0, + operation: null, + }); + h.vcs_status = () => []; + h.vcs_branches = () => branches; + h.vcs_stashes = () => []; + h.vcs_tags = () => []; + h.vcs_config_get = () => null; + h.vcs_rename_branch = (args) => { + const { from, to } = args as { from: string; to: string }; + if (!branches.some((b) => !b.remote && b.name === from)) + throw { kind: "failed", message: "no such branch", detail: null }; + head = to; + branches = branches.map((b) => (b.name === from ? { ...b, name: to } : b)); + return null; + }; + h.vcs_commit_files = () => []; + h.vcs_log_details_batch = ({ revisions }) => + (revisions as string[]).map(() => ({ + message: "", + containingBranches: [], + signature: "N", + signer: "", + key: "", + fingerprint: "", + })); + const storeModule = "/src/features/vcs/store.ts"; + const { useVcs, refreshNow, loadBranches } = await import(/* @vite-ignore */ storeModule); + useVcs.setState({ available: true, root, repo: root, roots: [root], branchesLoaded: false, stashesLoaded: false }); + await refreshNow(); + await loadBranches(); + const actionsModule = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ actionsModule); + await executeAction("vcs.showLog"); + }, ROOT); + await expect(page.getByRole("listbox", { name: "Commits" }).getByRole("option").first()).toBeVisible(); +} + +test("the log branch filter follows a branch rename from the log's own toolbar", async ({ workbench: page }) => { + await openLogOnSd(page); + const toolbar = page.getByRole("toolbar", { name: "Log filters", exact: true }); + // First open settles the filter on HEAD; the chip names it. + await expect(toolbar.getByRole("button", { name: "Branch: HEAD", exact: true })).toBeVisible(); + + // Choose the branch `sd` in the filter popup, as the user would. + await toolbar.getByRole("button", { name: "Branch: HEAD", exact: true }).click(); + const popup = page.getByRole("dialog", { name: "Filter by Branch" }); + await expect(popup).toBeVisible(); + await popup.getByRole("option", { name: "Local", exact: true }).click(); + await popup.getByRole("option", { name: "sd", exact: true }).click(); + await expect(toolbar.getByRole("button", { name: "Branch: sd", exact: true })).toBeVisible(); + + // Rename the branch from its Branches pane context menu, as the user would. + await page.getByRole("tree", { name: "Branches" }).getByRole("treeitem", { name: "sd current" }).click({ + button: "right", + }); + await page.getByRole("menuitem", { name: "Rename…", exact: true }).click(); + // The prompt dialog: type the new name and confirm. + const dialog = page.getByRole("dialog", { name: "Rename Branch sd" }); + await expect(dialog).toBeVisible(); + const field = dialog.getByRole("textbox"); + await field.fill("feature"); + await dialog.getByRole("button", { name: "Rename", exact: true }).click(); + + // No error balloon (the old filter would produce "unknown revision sd"). + await expect(page.getByText(/unknown revision/)).toHaveCount(0); + await expect + .poll(() => ipcCalls(page, "vcs_rename_branch")) + .toMatchObject([{ repo: ROOT, from: "sd", to: "feature" }]); + + // The chip follows the rename; the log reloads without an error row. + await expect(toolbar.getByRole("button", { name: "Branch: feature", exact: true })).toBeVisible(); + await expect(toolbar.getByRole("button", { name: "Branch: sd", exact: true })).toHaveCount(0); + await expect(page.getByText(/unknown revision/)).toHaveCount(0); + await expect(page.getByRole("listbox", { name: "Commits" }).getByRole("option").first()).toBeVisible(); +}); diff --git a/src/features/vcs/__tests__/logBranchMemory.test.ts b/src/features/vcs/__tests__/logBranchMemory.test.ts index a2da8bfd9c..6bfca8a356 100644 --- a/src/features/vcs/__tests__/logBranchMemory.test.ts +++ b/src/features/vcs/__tests__/logBranchMemory.test.ts @@ -16,8 +16,12 @@ vi.mock("@/platform/ipc", async (importOriginal) => { import { emptyFilters, initLogBranchFilter, + logState, + openLogTab, rememberedLogBranches, + renameBranchInLogFilters, resolveLogBranches, + selectGitTab, setLogBranches, useGitLog, } from "../logStore"; @@ -111,3 +115,38 @@ describe("Git Log branch filter memory", () => { expect(useGitLog.getState().branchScope).toBe("/mem-multi/backend"); }); }); + +describe("renameBranchInLogFilters", () => { + it("renames the branch in the shown filter and the repository's memory", async () => { + project("/mem-rename"); + initLogBranchFilter(); + setLogBranches(["feature"]); + await vi.waitFor(() => expect(useGitLog.getState().loading).toBe(false)); + renameBranchInLogFilters("/mem-rename", "feature", "renamed"); + expect(useGitLog.getState().filters.branches).toEqual(["renamed"]); + expect(rememberedLogBranches("/mem-rename")).toEqual(["renamed"]); + }); + + it("rewrites parked views and Log: branch tabs, leaving other branches alone", () => { + project("/mem-rename-tabs"); + openLogTab("branch", "feature"); + openLogTab("branch", "other"); + selectGitTab("log"); + useGitLog.setState({ filters: { ...emptyFilters(), branches: ["feature", "main"] } }); + renameBranchInLogFilters("/mem-rename-tabs", "feature", "renamed"); + expect(useGitLog.getState().filters.branches).toEqual(["renamed", "main"]); + expect(logState("branch:1").filters.branches).toEqual(["renamed"]); + expect(logState("branch:2").filters.branches).toEqual(["other"]); + expect(useGitLog.getState().logTabs.map((t) => t.target)).toEqual(["renamed", "other"]); + }); + + it("does not touch unrelated filters or tabs", () => { + project("/mem-rename-keep"); + useGitLog.setState({ filters: { ...emptyFilters(), branches: ["main"] } }); + useGitLog.setState({ logTabs: [{ id: "branch:1", kind: "branch", target: "main" }] }); + renameBranchInLogFilters("/mem-rename-keep", "feature", "renamed"); + expect(useGitLog.getState().filters.branches).toEqual(["main"]); + expect(useGitLog.getState().logTabs[0]!.target).toBe("main"); + expect(rememberedLogBranches("/mem-rename-keep")).toBeUndefined(); + }); +}); diff --git a/src/features/vcs/branchOps.ts b/src/features/vcs/branchOps.ts index 61a57e1f23..1a90aa9565 100644 --- a/src/features/vcs/branchOps.ts +++ b/src/features/vcs/branchOps.ts @@ -37,6 +37,7 @@ import { type RemoteTarget, } from "./ops"; import { beginGitWork, conflictedFiles, currentRepo, currentRepoRoot, refreshNow, repoName, useVcs } from "./store"; +import { renameBranchInLogFilters } from "./logStore"; import { tx, txTemplate } from "@/platform/i18n"; import { useProject } from "@/platform/project"; import { workflowSettings } from "@/platform/workflowSettings"; @@ -761,7 +762,11 @@ export async function renameBranch(branch: Branch) { }, }, ); - if (ok !== undefined && useProject.getState().project === project) renameRecent(name, to); + if (ok !== undefined && useProject.getState().project === project) { + renameRecent(name, to); + // The log follows: its Branch filter and "Log: branch" tabs keep naming the branch. + if (repo) renameBranchInLogFilters(repo, name, to); + } } async function promptBranchRename( @@ -1762,6 +1767,8 @@ export async function renameInRepos(name: string, roots: readonly string[], upst if (useProject.getState().project !== project) return; if (complete) renameRecent(name, to); else rememberBranch(to); + // The log follows the kept renames in every renamed repository. + if (complete) for (const root of roots) renameBranchInLogFilters(root, name, to); // Only change tracking after the user has decided which renames to retain. if (unsetUpstream) { for (const { root } of done) { diff --git a/src/features/vcs/logStore.ts b/src/features/vcs/logStore.ts index 2240a6e374..ceeb2dba0a 100644 --- a/src/features/vcs/logStore.ts +++ b/src/features/vcs/logStore.ts @@ -302,6 +302,28 @@ export function setLogBranches(branches: readonly string[]) { void reloadLog(); } +/** + * Follows a branch rename: the Branch filter of every open view, the choice + * remembered for the repository and "Log: branch" tabs keep tracking the branch + * under its new name instead of filtering an unknown revision. + */ +export function renameBranchInLogFilters(repo: string, from: string, to: string) { + const rename = (branches: readonly string[]) => branches.map((b) => (b === from ? to : b)); + const remembered = rememberedLogBranches(repo); + if (remembered?.includes(from)) rememberLogBranches(repo, rename(remembered)); + const s = useGitLog.getState(); + if (s.filters.branches.includes(from)) + useGitLog.setState({ filters: { ...s.filters, branches: rename(s.filters.branches) } }); + for (const [id, view] of parked) + if (view.filters.branches.includes(from)) + setViewState(id, { filters: { ...view.filters, branches: rename(view.filters.branches) } }); + if (s.logTabs.some((t) => t.kind === "branch" && t.target === from)) + useGitLog.setState((l) => ({ + logTabs: l.logTabs.map((t) => (t.kind === "branch" && t.target === from ? { ...t, target: to } : t)), + })); + if (s.filters.branches.includes(from)) void reloadLog(); +} + const loadPromises = new Map>(); const pageRequests = new Map>(); const cherryRequests = new Map();