diff --git a/src/features/vcs/__tests__/logBranchMemory.test.ts b/src/features/vcs/__tests__/logBranchMemory.test.ts index 6bfca8a356..bbe5a5fecc 100644 --- a/src/features/vcs/__tests__/logBranchMemory.test.ts +++ b/src/features/vcs/__tests__/logBranchMemory.test.ts @@ -149,4 +149,51 @@ describe("renameBranchInLogFilters", () => { expect(useGitLog.getState().logTabs[0]!.target).toBe("main"); expect(rememberedLogBranches("/mem-rename-keep")).toBeUndefined(); }); + + it("keeps another repository's tab of the same branch", () => { + project("/B"); + useVcs.setState({ roots: ["/A", "/B"] }); + const tab = openLogTab("branch", "feature"); + selectGitTab("log"); + renameBranchInLogFilters("/A", "feature", "renamed"); + const record = useGitLog.getState().logTabs.find((t) => t.id === tab)!; + expect([record.repo, record.target, logState(tab).filters.branches]).toEqual(["/B", "feature", ["feature"]]); + }); + + it("renames the tab of a linked worktree sharing the repository's branches", () => { + project("/W", "/W-wt"); + useVcs.setState({ roots: ["/W", "/W-wt"], branchRoots: { "/W-wt": "/W" } }); + const tab = openLogTab("branch", "feature"); + selectGitTab("log"); + renameBranchInLogFilters("/W", "feature", "renamed"); + expect(useGitLog.getState().logTabs.find((t) => t.id === tab)!.target).toBe("renamed"); + expect(logState(tab).filters.branches).toEqual(["renamed"]); + useVcs.setState({ branchRoots: {} }); + }); + + it("follows the rename in a log of a repository the project does not list", () => { + project("/A"); + useVcs.setState({ roots: ["/A", "/B"] }); + // An external log of a linked worktree outside the project. + useGitLog.setState({ filters: { ...emptyFilters(), branches: ["feature"], repositories: ["/elsewhere"] } }); + renameBranchInLogFilters("/A", "feature", "renamed"); + expect(useGitLog.getState().filters.branches).toEqual(["renamed"]); + }); + + it("renames as before when the renamed repository is no longer listed", () => { + project("/B"); + useVcs.setState({ roots: ["/B"] }); + const tab = openLogTab("branch", "topic"); + selectGitTab("log"); + renameBranchInLogFilters("/gone", "topic", "renamed"); + expect(useGitLog.getState().logTabs.find((t) => t.id === tab)!.target).toBe("renamed"); + }); + + it("keeps a branch filter that queries only other repositories", () => { + project("/A"); + useVcs.setState({ roots: ["/A", "/B"] }); + useGitLog.setState({ filters: { ...emptyFilters(), branches: ["feature"], repositories: ["/B"] } }); + renameBranchInLogFilters("/A", "feature", "renamed"); + expect(useGitLog.getState().filters.branches).toEqual(["feature"]); + }); }); diff --git a/src/features/vcs/logStore.ts b/src/features/vcs/logStore.ts index 2c5667b72a..03e7f4edbd 100644 --- a/src/features/vcs/logStore.ts +++ b/src/features/vcs/logStore.ts @@ -311,25 +311,46 @@ export function setLogBranches(branches: readonly string[]) { } /** - * Follows a branch rename: the Branch filter of every open view, the choice + * Follows a branch rename in `repo`: 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. + * under its new name instead of filtering an unknown revision. A view or tab scoped to + * other repositories keeps its branch of the same name. */ 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)); + // Linked worktrees share their main repository's branches. A repository the project does not + // list (an external log) may be one of them, so only a listed one counts as another repository. + const { roots: listed, branchRoots } = useVcs.getState(); + const group = (root: string) => branchRoots[root] ?? root; + const owns = (root: string) => group(root) === group(repo); + const isListed = (root: string) => listed.includes(root) || root in branchRoots; + // A repository no longer listed (its mapping removed meanwhile) can't be told apart: as before. + const other = (root: string) => isListed(repo) && isListed(root) && !owns(root); + const tabRoots = (tab: LogTab | undefined) => + tab?.repositories?.length ? tab.repositories : tab?.repo ? [tab.repo] : []; + const renamed = (filters: LogFilters, tab: LogTab | undefined): LogFilters | null => { + if (!filters.branches.includes(from)) return null; + // The repositories the view queries, as logRepositoryScope picks them. + const roots = tabRoots(tab).length ? tabRoots(tab) : (filters.repositories ?? []); + const queried = filters.repositories?.length ? roots.filter((r) => filters.repositories!.includes(r)) : roots; + if (queried.length && queried.every(other)) return null; + return { ...filters, branches: rename(filters.branches) }; + }; 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 tabOf = (id: string) => s.logTabs.find((t) => t.id === id); + const shown = renamed(s.filters, tabOf(s.shownLog)); + if (shown) useGitLog.setState({ filters: shown }); + for (const [id, view] of parked) { + const next = renamed(view.filters, tabOf(id)); + if (next) setViewState(id, { filters: next }); + } + const follows = (t: LogTab) => + t.kind === "branch" && t.target === from && !(tabRoots(t).length && tabRoots(t).every(other)); + if (s.logTabs.some(follows)) + useGitLog.setState((l) => ({ logTabs: l.logTabs.map((t) => (follows(t) ? { ...t, target: to } : t)) })); + if (shown) void reloadLog(); } const loadPromises = new Map>();