From ae9804bc8b0d9f9fdafeaa3981dfd2070fc4d8e4 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Sat, 5 Dec 2026 00:19:31 +0900 Subject: [PATCH] feat(vcs): add option to filter Git Log on single click in Branches pane (#8040) * feat(vcs): add option to filter Git Log on single click in Branches pane * fix(vcs): do not update branch filter on context-menu right click --------- Co-authored-by: LU XIANGRUI <26499420+wildcatDownstairs@users.noreply.github.com> Co-authored-by: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> --- src/features/vcs/LogSettingsPage.tsx | 5 ++++ .../vcs/__tests__/logBranches.test.tsx | 30 +++++++++++++++++++ src/features/vcs/__tests__/logPanes.test.tsx | 2 ++ src/features/vcs/log/BranchesPanel.tsx | 13 +++++++- src/features/vcs/logOptions.ts | 4 +++ src/features/vcs/settingsPages.ts | 2 +- src/platform/locales/text/en.json | 2 ++ src/platform/locales/text/zh.json | 2 ++ 8 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/features/vcs/LogSettingsPage.tsx b/src/features/vcs/LogSettingsPage.tsx index f70ccad693..ed97c4f36f 100644 --- a/src/features/vcs/LogSettingsPage.tsx +++ b/src/features/vcs/LogSettingsPage.tsx @@ -15,6 +15,10 @@ const OPTIONS: { key: Flag; label: string }[] = [ { key: "refsOnLeft", label: "Show references on the left" }, ]; +const BRANCHES: { key: Flag; label: string }[] = [ + { key: "branchClickFilters", label: "Update branch filter on single click" }, +]; + const COLUMNS: { key: Flag; label: string }[] = [ { key: "showAuthor", label: "Author" }, { key: "showHash", label: "Hash" }, @@ -43,6 +47,7 @@ export function LogSettingsPage() { return (
{OPTIONS.map(flag)} + {BRANCHES.map(flag)} {COLUMNS.map(flag)}
); diff --git a/src/features/vcs/__tests__/logBranches.test.tsx b/src/features/vcs/__tests__/logBranches.test.tsx index 1261a8bcb1..5c7d0a8168 100644 --- a/src/features/vcs/__tests__/logBranches.test.tsx +++ b/src/features/vcs/__tests__/logBranches.test.tsx @@ -46,6 +46,7 @@ import { ContextMenuHost, iconMarkup } from "@/ui"; import syncStyles from "../BranchSync.module.css"; import { BranchesPanel } from "../log/BranchesPanel"; import { FilterBar } from "../log/FilterBar"; +import { setLogOptions } from "../logOptions"; import { useBranchesPane } from "../logPaneActions"; import { emptyFilters, useGitLog } from "../logStore"; import { useVcs } from "../store"; @@ -113,6 +114,35 @@ describe("Git Log branches pane", () => { expect(screen.getByText("feature").closest("[data-row]")).toHaveAttribute("aria-selected", "true"); }); + it("one click filters the log when branchClickFilters is enabled, ignoring group rows", () => { + setLogOptions({ branchClickFilters: true }); + try { + render(); + fireEvent.mouseDown(screen.getByText("feature")); + expect(useGitLog.getState().filters.branches).toEqual(["feature"]); + fireEvent.mouseDown(screen.getByText("Local")); + expect(useGitLog.getState().filters.branches).toEqual(["feature"]); + fireEvent.mouseDown(screen.getByText("main")); + expect(useGitLog.getState().filters.branches).toEqual(["main"]); + } finally { + setLogOptions({ branchClickFilters: false }); + } + }); + + it("right click selects the branch without changing the log filter when branchClickFilters is enabled", () => { + setLogOptions({ branchClickFilters: true }); + try { + render(); + fireEvent.mouseDown(screen.getByText("feature")); + expect(useGitLog.getState().filters.branches).toEqual(["feature"]); + fireEvent.mouseDown(screen.getByText("main"), { button: 2 }); + expect(useGitLog.getState().filters.branches).toEqual(["feature"]); + expect(screen.getByText("main").closest("[data-row]")).toHaveAttribute("aria-selected", "true"); + } finally { + setLogOptions({ branchClickFilters: false }); + } + }); + it("collapses and expands every group from the toolbar", () => { render(); expect(screen.getByText("feature")).toBeInTheDocument(); diff --git a/src/features/vcs/__tests__/logPanes.test.tsx b/src/features/vcs/__tests__/logPanes.test.tsx index aabfee7cbc..89112b3af7 100644 --- a/src/features/vcs/__tests__/logPanes.test.tsx +++ b/src/features/vcs/__tests__/logPanes.test.tsx @@ -558,5 +558,7 @@ describe("Settings › Version Control › Log", () => { expect(useLogOptions.getState().tagNames).toBe(!DEFAULT_LOG_OPTIONS.tagNames); fireEvent.click(screen.getByLabelText("Hash")); expect(useLogOptions.getState().showHash).toBe(!DEFAULT_LOG_OPTIONS.showHash); + fireEvent.click(screen.getByLabelText("Update branch filter on single click")); + expect(useLogOptions.getState().branchClickFilters).toBe(!DEFAULT_LOG_OPTIONS.branchClickFilters); }); }); diff --git a/src/features/vcs/log/BranchesPanel.tsx b/src/features/vcs/log/BranchesPanel.tsx index ffda7adf15..a8f1c8af44 100644 --- a/src/features/vcs/log/BranchesPanel.tsx +++ b/src/features/vcs/log/BranchesPanel.tsx @@ -27,6 +27,7 @@ import { summarizeBranchSync, type BranchSyncSummary } from "../branchSyncState" import { FetchButton } from "../FetchButton"; import styles from "../GitLog.module.css"; import { LogIdContext } from "../logContext"; +import { useLogOptions } from "../logOptions"; import { canUpdateBranch, useBranchesPane, type BranchTarget } from "../logPaneActions"; import { reloadLog, scopedLogRepositories, setLogBranches, setLogFilters, useGitLog } from "../logStore"; import { branchesByRepo } from "../multiRoot"; @@ -87,6 +88,7 @@ function sharedHead(roots: readonly string[]): string | null { export function BranchesPanel() { const scope = useContext(LogIdContext); + const branchClickFilters = useLogOptions((s) => s.branchClickFilters); const shown = useGitLog((s) => s.shownLog); const active = !scope || scope === shown; const logGeneration = useGitLog((s) => s.generation); @@ -475,7 +477,16 @@ export function BranchesPanel() { }} getTooltip={(n) => mergedRefTooltip(n, (r) => repoName(branchRoots[r] ?? r))} selected={selected} - onSelectionChange={(sel) => setSelected(sel)} + onSelectionChange={(sel, _lead, reason) => { + setSelected(sel); + if (!branchClickFilters || reason === "contextMenu") return; + const branchTargets = refs.filter((r) => sel.has(r.key)).map(({ ref, repo }) => ({ ref, repo })); + if (!branchTargets.length) return; + setLogFilters({ + branches: [...new Set(branchTargets.map(({ ref }) => ref))], + branchTargets: branchTargets.some(({ repo }) => repo) ? branchTargets : undefined, + }); + }} onActivate={(n) => { if (!n.ref) return; const activated = selected.has(n.key) ? targets : expand(n, scopedRepo); diff --git a/src/features/vcs/logOptions.ts b/src/features/vcs/logOptions.ts index a56277d18b..f4b3e457f7 100644 --- a/src/features/vcs/logOptions.ts +++ b/src/features/vcs/logOptions.ts @@ -34,6 +34,8 @@ export interface LogOptions { highlightCurrentBranch: boolean; /** The Branches pane on the left of the log. */ showBranches: boolean; + /** Single-clicking a branch in the Branches pane updates the log's branch filter. */ + branchClickFilters: boolean; /** Changed files grouped by directory (else a flat list). */ groupByDirectory: boolean; /** Selecting a changed file updates the embedded preview or an open editor preview. */ @@ -74,6 +76,7 @@ export const DEFAULT_LOG_OPTIONS: LogOptions = { highlightMerges: true, highlightCurrentBranch: true, showBranches: true, + branchClickFilters: false, groupByDirectory: true, diffPreview: true, showLineStats: false, @@ -101,6 +104,7 @@ const APPLICATION_LOG_OPTION_KEYS = [ "showHash", "showSignature", "showGitHubChecks", + "branchClickFilters", "diffPreview", "changesFromParents", "diffLocation", diff --git a/src/features/vcs/settingsPages.ts b/src/features/vcs/settingsPages.ts index 4058a0fc60..32c1ffbbf0 100644 --- a/src/features/vcs/settingsPages.ts +++ b/src/features/vcs/settingsPages.ts @@ -16,7 +16,7 @@ export function activateSettingsPages(): () => void { id: "vcs.log", title: "Log", order: 6, - keywords: ["git log", "references", "tags", "columns", "commit time"], + keywords: ["git log", "references", "tags", "columns", "commit time", "branches", "branch filter", "single click"], component: lazyComponent(() => import("./LogSettingsPage").then((m) => m.LogSettingsPage)), }); return () => { diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index a9d509ae31..1108cf154c 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -4288,6 +4288,8 @@ "Cannot read the whole values": "Cannot read the whole values", "The result changed since this page was read. Run the query again.": "The result changed since this page was read. Run the query again.", "View Options": "View Options", + "Branches pane": "Branches pane", + "Update branch filter on single click": "Update branch filter on single click", "Visible columns": "Visible columns", "Install Ruff to optimize imports in Python files": "Install Ruff to optimize imports in Python files", "Optimize Imports (Ruff)": "Optimize Imports (Ruff)", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index fc561e9285..31ef838856 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -6847,6 +6847,8 @@ "{removed} lines deleted": "删除了 {removed} 行", "Show commit time rather than author time": "显示提交时间而非作者时间", "Show references on the left": "在左侧显示引用", + "Branches pane": "分支面板", + "Update branch filter on single click": "单击时更新分支过滤", "Visible columns": "可见列", "Open Repository Version": "打开仓库版本", "Open the selected files as they exist after the change": "打开所选文件在该更改之后的版本",