From e80dcd11e3bb42ef39153a0ca9db7ea07a5ec67d Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Wed, 18 Nov 2026 02:48:38 -0800 Subject: [PATCH] fix(git): avoid querying root Git references on non-repository folders (#2125) --- desktop/internal/workbench/git.go | 5 ++ desktop/internal/workbench/git_log_repo.go | 7 ++ desktop/internal/workbench/gitlog.go | 5 ++ .../workbench/workspace_repositories_test.go | 82 +++++++++++++++++++ .../git-multi-repository-references.json | 8 +- 5 files changed, 104 insertions(+), 3 deletions(-) diff --git a/desktop/internal/workbench/git.go b/desktop/internal/workbench/git.go index fcc705d3a1..e93086e607 100644 --- a/desktop/internal/workbench/git.go +++ b/desktop/internal/workbench/git.go @@ -46,6 +46,7 @@ func (a *App) readGit(p *project, policy func(context.Context) context.Context) a.background(p, gitTimeout, func(ctx context.Context) func(*App) { status, err := kernel.GitStatus(policy(ctx), root) return func(a *App) { + wasRepo := p.git.isRepository() if !a.setGit(p, gitState{loaded: true, status: status, err: err}, generation) { return } @@ -55,6 +56,10 @@ func (a *App) readGit(p *project, policy func(context.Context) context.Context) return } a.gitStatusPublished(p) + // Load or clear the log once GitStatus determines whether p.root is a repository. + if p.git.isRepository() != wasRepo && p.log.started && !p.walksSeveral() && p.logRepo() == nil { + a.loadLog(p) + } } }) } diff --git a/desktop/internal/workbench/git_log_repo.go b/desktop/internal/workbench/git_log_repo.go index a24e53a78a..b21582fb07 100644 --- a/desktop/internal/workbench/git_log_repo.go +++ b/desktop/internal/workbench/git_log_repo.go @@ -17,6 +17,13 @@ import ( // it. The roots list chooses several repositories too, and the log then shows their commits // together (gitlog_multi.go). The other repositories' references are read in nested_refs.go. +// logHasRepository reports whether the log already has a repository to walk. Until the project's +// Git status confirms p.root is a repository or Core selects a nested one, querying references at +// p.root would fail on non-repository folders and pollute the Git console. +func (p *project) logHasRepository() bool { + return p.walksSeveral() || p.logRepo() != nil || p.git.isRepository() +} + // logRepo is the nested repository the log walks, nil while it walks the project's own. func (p *project) logRepo() *nestedRepo { repo, ok := p.nested.repo(p.nested.logRepo) diff --git a/desktop/internal/workbench/gitlog.go b/desktop/internal/workbench/gitlog.go index b91ed23720..740fc0cac2 100644 --- a/desktop/internal/workbench/gitlog.go +++ b/desktop/internal/workbench/gitlog.go @@ -244,6 +244,11 @@ func (a *App) loadLog(p *project) { // The upstreams the graph paints were read for the repository before this one. a.logView.upstream, a.logView.upstreamFor = nil, nil a.selectCommit(p, "") + if !p.logHasRepository() { + l.loading = false + l.refs, l.sections, l.current = core.GitReferences{}, [3]logSection{}, "" + return + } // The nested repositories' groups in the reference pane are read with the log (nested_refs.go). a.loadNestedReferences(p) if p.walksSeveral() { diff --git a/desktop/internal/workbench/workspace_repositories_test.go b/desktop/internal/workbench/workspace_repositories_test.go index e12e2cc468..8d975b5f23 100644 --- a/desktop/internal/workbench/workspace_repositories_test.go +++ b/desktop/internal/workbench/workspace_repositories_test.go @@ -169,6 +169,88 @@ func TestWorkspaceLogRootsWalkTheChosenRepository(t *testing.T) { } } +// Opening a folder that is not a repository itself—whether it holds nested repositories or is +// empty—must never query Git references at the non-repository folder root or leave failed commands +// in the Git console. +func TestNonRepositoryFolderDoesNotQueryRootReferencesInGitConsole(t *testing.T) { + assertNoFailedConsoleRecords := func(t *testing.T, h *harness) { + t.Helper() + for _, r := range h.app.gitConsole.Snapshot().Records { + if r.Error != "" || (r.Exited && r.ExitCode != 0 && !r.ExpectedExit) { + t.Fatalf("unexpected failed Git console record: root=%q args=%v exit=%d err=%q", r.Root, r.Arguments, r.ExitCode, r.Error) + } + } + } + + t.Run("nested repositories", func(t *testing.T) { + parent := newWorkspaceFolder(t, "alpha", "beta") + h := newHarness(t, &gitKernel{Client: core.NewClient()}) + p := h.openWorkspaceFolder(parent) + alphaID := repositoryID(t, p, "alpha") + h.app.layout.gitTab = gitTabLog + if !h.app.chrome.isOpen(catalogIndex[itemGit]) { + h.click(center(h.button(itemGit))) + } + h.settleUntil("alpha's history", func() bool { + return p.logRepoID() == alphaID && !p.log.loading && len(p.log.graph.Rows) == 1 + }) + assertNoFailedConsoleRecords(t, h) + }) + + t.Run("empty folder", func(t *testing.T) { + requireTestGit(t) + empty := t.TempDir() + h := newHarness(t, &gitKernel{Client: core.NewClient()}) + h.app.delays = logDelays{} + h.open(empty) + h.app.layout.gitTab = gitTabLog + if !h.app.chrome.isOpen(catalogIndex[itemGit]) { + h.click(center(h.button(itemGit))) + } + h.settle() + p := h.app.project + if !h.shows(h.app.text.Git.NotRepository) { + t.Fatalf("expected %q on empty folder log tab", h.app.text.Git.NotRepository) + } + // Refreshing the log on a non-repository folder must also stay silent. + h.app.loadLog(p) + h.settle() + assertNoFailedConsoleRecords(t, h) + }) + + t.Run("initialized later", func(t *testing.T) { + requireTestGit(t) + dir := t.TempDir() + h := newHarness(t, &gitKernel{Client: core.NewClient()}) + h.app.delays = logDelays{} + h.open(dir) + h.app.layout.gitTab = gitTabLog + if !h.app.chrome.isOpen(catalogIndex[itemGit]) { + h.click(center(h.button(itemGit))) + } + h.settle() + assertNoFailedConsoleRecords(t, h) + + gitRun(t, dir, "init") + gitRun(t, dir, "symbolic-ref", "HEAD", "refs/heads/main") + if err := os.WriteFile(filepath.Join(dir, "readme.txt"), []byte("hello\n"), 0o600); err != nil { + t.Fatal(err) + } + gitRun(t, dir, "add", "readme.txt") + gitRun(t, dir, "commit", "-m", "late init") + + p := h.app.project + h.app.loadGit(p) + h.settleUntil("late repository history", func() bool { + return p.git.isRepository() && !p.log.loading && len(p.log.graph.Rows) == 1 + }) + if !h.shows("late init") { + t.Fatal("the log did not load after the folder became a Git repository") + } + assertNoFailedConsoleRecords(t, h) + }) +} + // The Commit tool window of a folder holding repositories groups the changes by repository, and // its Commit button commits the staged changes of each. func TestWorkspaceCommitCommitsEveryNestedRepository(t *testing.T) { diff --git a/shared/platform-feature-matrix/features/git-multi-repository-references.json b/shared/platform-feature-matrix/features/git-multi-repository-references.json index b0a0c55e32..094ac857b9 100644 --- a/shared/platform-feature-matrix/features/git-multi-repository-references.json +++ b/shared/platform-feature-matrix/features/git-multi-repository-references.json @@ -27,7 +27,8 @@ "desktop/internal/core/git_workspace_branches.go", "desktop/internal/workbench/branch_common.go", "desktop/internal/workbench/branch_common_view.go", - "desktop/internal/workbench/branch_common_test.go" + "desktop/internal/workbench/branch_common_test.go", + "desktop/internal/workbench/git.go" ] }, "windows": { @@ -54,11 +55,12 @@ "desktop/internal/core/git_workspace_branches.go", "desktop/internal/workbench/branch_common.go", "desktop/internal/workbench/branch_common_view.go", - "desktop/internal/workbench/branch_common_test.go" + "desktop/internal/workbench/branch_common_test.go", + "desktop/internal/workbench/git.go" ] }, "owner": "Git", - "verification": "在多仓库工作区打开 Git Log:确认按仓库分组、仓库配色、非活动仓库分组只读,点其它仓库的引用会切换活动仓库且只加载一次,Pull 弹窗可选择远程分支与策略。 Windows 无已保存折叠设置时,单仓库与多仓库的 Tags 默认折叠、Local / Remote 默认展开;手动展开 Tags 后重新打开面板仍保留选择,已有折叠设置继续生效。 Windows 明暗主题中普通本地、远程分支均复用顶部分支按钮的空心 VcsIcon,当前分支勾选、收藏星标、标签与文件夹提示保持可辨认。 macOS 在两个分支名不同的仓库间切换时不得查询旧分支;目标历史读取失败也不能保留旧仓库的提交、分页或文件详情。 Windows Git Log 本地和远程分支右键菜单的拷贝分支名称在删除之前,删除是最后一个操作并单独分组;当前分支无删除入口,执行 Git 写操作时删除仍禁用。 Windows Git Log Tags 标签节点右键菜单最后一组提供 Delete,点击后直接删除活动仓库本地标签,不弹二次确认;失败保留引用和选择,成功后清除被删标签的历史筛选并刷新。非活动仓库及写操作期间禁用;旧工作区/仓库的动作回调不得发起删除,迟到结果不得刷新新仓库或释放新请求状态。 Windows Tags 的 Delete 与分支和提交节点删除复用相同 TrashIcon,明暗主题图标与禁用状态一致。 引用树的 `/` 分组与同级排序(引用先于纯分组,再按自然序,收藏分支及其所在分组优先)由 Core `git.referenceTree` 决定,两端只负责展开、缩进与虚拟化;同一组引用在 macOS 与 Windows 的树结构和顺序一致。 打开本身不是 Git 仓库、内含多个独立仓库的文件夹:标题栏分支入口按 IDEA 样式逐行列出仓库(颜色方块、仓库名、右侧当前分支、›),点一行在其旁边展开该仓库的分支面板,在面板里检出、新建、合并、删除只改变该仓库,其他仓库分支不变;Git Log 提交行左侧显示正在查看仓库的颜色条,路径筛选器下方“仓库根目录”按颜色列出仓库,点击后日志改看该仓库且写操作作用于该仓库。 多仓库 Git Log 引用面板每个仓库节点下各有本地 / 远程 / 标签,点其它仓库的分支即在该仓库查看该分支;筛选按钮后的仓库名可点击,列出全部仓库:点名称只看该仓库,勾选多个仓库时日志按提交时间混排(Core `git.workspaceHistoryMerge`,各仓库保持 Git 的原有顺序,加载更多只在末尾追加),每行颜色条是所属仓库的颜色,提交详情、文件比较和提交右键菜单都作用于该行所属的仓库;取消勾选到只剩一个仓库时回到单仓库日志。 勾选多个仓库混排时,读满多个仓库的上限后日志末尾提示超出可读取范围,不显示加载失败。 打开内含多个仓库的文件夹:标题栏分支入口在仓库列表下方列出“共同本地分支 / 共同远程分支”(所有仓库都有的同名分支,由 Core `git.workspaceCommonReferences` 计算,所有仓库都在该分支时显示为当前分支),搜索框同样筛选这两节;在共同分支上检出或从它新建分支,会按 Core `git.workspaceBranchPlan` 的顺序依次作用于每个仓库,新分支名与任一仓库已有分支重名时提示已存在;检出会覆盖本地改动时,冲突对话框按“仓库/文件”列出所有仓库的改动,选择对所有仓库生效。某个仓库失败时停止,若已有仓库完成,弹出“只在部分仓库中完成”,点“回滚”让已完成的仓库回到原分支并删除刚创建的分支,点“不回滚”保持现状。勾选多个仓库混排时,每个仓库当前检出分支上的提交都有当前分支底色(Core `git.graphProjection` 的 `checkedOutHeads`)。", + "verification": "在多仓库工作区打开 Git Log:确认按仓库分组、仓库配色、非活动仓库分组只读,点其它仓库的引用会切换活动仓库且只加载一次,Pull 弹窗可选择远程分支与策略。 Windows 无已保存折叠设置时,单仓库与多仓库的 Tags 默认折叠、Local / Remote 默认展开;手动展开 Tags 后重新打开面板仍保留选择,已有折叠设置继续生效。 Windows 明暗主题中普通本地、远程分支均复用顶部分支按钮的空心 VcsIcon,当前分支勾选、收藏星标、标签与文件夹提示保持可辨认。 macOS 在两个分支名不同的仓库间切换时不得查询旧分支;目标历史读取失败也不能保留旧仓库的提交、分页或文件详情。 Windows Git Log 本地和远程分支右键菜单的拷贝分支名称在删除之前,删除是最后一个操作并单独分组;当前分支无删除入口,执行 Git 写操作时删除仍禁用。 Windows Git Log Tags 标签节点右键菜单最后一组提供 Delete,点击后直接删除活动仓库本地标签,不弹二次确认;失败保留引用和选择,成功后清除被删标签的历史筛选并刷新。非活动仓库及写操作期间禁用;旧工作区/仓库的动作回调不得发起删除,迟到结果不得刷新新仓库或释放新请求状态。 Windows Tags 的 Delete 与分支和提交节点删除复用相同 TrashIcon,明暗主题图标与禁用状态一致。 引用树的 `/` 分组与同级排序(引用先于纯分组,再按自然序,收藏分支及其所在分组优先)由 Core `git.referenceTree` 决定,两端只负责展开、缩进与虚拟化;同一组引用在 macOS 与 Windows 的树结构和顺序一致。 打开本身不是 Git 仓库、内含多个独立仓库的文件夹:标题栏分支入口按 IDEA 样式逐行列出仓库(颜色方块、仓库名、右侧当前分支、›),点一行在其旁边展开该仓库的分支面板,在面板里检出、新建、合并、删除只改变该仓库,其他仓库分支不变;Git Log 提交行左侧显示正在查看仓库的颜色条,路径筛选器下方“仓库根目录”按颜色列出仓库,点击后日志改看该仓库且写操作作用于该仓库。 多仓库 Git Log 引用面板每个仓库节点下各有本地 / 远程 / 标签,点其它仓库的分支即在该仓库查看该分支;筛选按钮后的仓库名可点击,列出全部仓库:点名称只看该仓库,勾选多个仓库时日志按提交时间混排(Core `git.workspaceHistoryMerge`,各仓库保持 Git 的原有顺序,加载更多只在末尾追加),每行颜色条是所属仓库的颜色,提交详情、文件比较和提交右键菜单都作用于该行所属的仓库;取消勾选到只剩一个仓库时回到单仓库日志。 勾选多个仓库混排时,读满多个仓库的上限后日志末尾提示超出可读取范围,不显示加载失败。 打开内含多个仓库的文件夹:标题栏分支入口在仓库列表下方列出“共同本地分支 / 共同远程分支”(所有仓库都有的同名分支,由 Core `git.workspaceCommonReferences` 计算,所有仓库都在该分支时显示为当前分支),搜索框同样筛选这两节;在共同分支上检出或从它新建分支,会按 Core `git.workspaceBranchPlan` 的顺序依次作用于每个仓库,新分支名与任一仓库已有分支重名时提示已存在;检出会覆盖本地改动时,冲突对话框按“仓库/文件”列出所有仓库的改动,选择对所有仓库生效。某个仓库失败时停止,若已有仓库完成,弹出“只在部分仓库中完成”,点“回滚”让已完成的仓库回到原分支并删除刚创建的分支,点“不回滚”保持现状。勾选多个仓库混排时,每个仓库当前检出分支上的提交都有当前分支底色(Core `git.graphProjection` 的 `checkedOutHeads`)。 打开本身不是 Git 仓库的文件夹(空文件夹或含嵌套独立仓库,以及空文件夹之后初始化为仓库)时,Git Log 仅在已确认所属仓库或嵌套仓库后再读取引用与历史,不在非仓库根目录触发 `for-each-ref` 报错污染 Git 控制台;由无头回归 `TestNonRepositoryFolderDoesNotQueryRootReferencesInGitConsole` 覆盖,原生三平台交互验收仍 pending。", "notes": "Viva:项目文件夹含嵌套仓库时(含文件夹本身不是 Git 仓库),标题栏分支入口按 IDEA 样式逐行列出仓库,颜色来自 Core(repository_colors.go),右侧是 Core 读出的当前分支;点一行在旁边展开该仓库的面板(“<仓库> 中的最近分支 / 本地分支 / 远程分支 / 标签”),面板里的检出、新建、合并、删除等写操作都只作用于该仓库。Git Log 引用面板按仓库分组,提交行左侧有正在查看仓库的颜色条,路径筛选器下方的“仓库根目录”列出各仓库并切换日志遍历的仓库。引用面板每个仓库节点下各有本地 / 远程 / 标签;筛选按钮后的仓库名可点击,勾选多个仓库时日志按提交时间混排,混排顺序与提交键由 Core `git.workspaceHistoryMerge` 决定,仓库的选择由 Core `git.workspaceRepositorySelection` 的 `chosen` 决定。仓库列表下方是 IDEA 的 Common Local / Remote Branches:共同分支由 Core `git.workspaceCommonReferences` 计算,检出与新建分支的逐仓库步骤和回滚动作由 Core `git.workspaceBranchPlan` 给出,桌面端按顺序执行、首个失败即停,并像 IDEA 一样询问是否回滚。尚未实现:共同分支上的合并、变基、删除、推送等其他操作,以及跨仓库的 Update / Push。 混排时所有仓库合计最多读取“设置 › 版本控制 › Git 日志”中多个仓库的上限(默认 20000 条),读满后各仓库停在当前位置,日志末尾提示超出可读取范围并可打开该设置。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36",