Skip to content

feat(git): 多仓库工作区的仓库身份、当前仓库选择与改动分组移入 Core - #65

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/2013-workspace-repositories
Nov 16, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/2013-workspace-repositories

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue / Linked issue

PR 类型 / Type

  • feat:新功能或用户可见的改进

改动说明 / What changes

背景 / Why

#2013 的场景:打开一个本身没有 .git 的父文件夹,里面有几个独立仓库。现在桌面端(Viva,Go)自己在 Go 里判断:

  • 视图当前作用于哪个仓库;
  • 各仓库的改动怎么分组、怎么暂存;
  • 父仓库是否还需要记录嵌套仓库的新提交。

而且它默认打开的文件夹本身一定是仓库。按“通用逻辑进 Rust Core”的规则,这些规则先迁入 Core,并补齐契约和 fixture。下一步桌面端只负责呈现和执行原生 Git 写入。

迁移时顺带修正了 Go 里 submoduleReferenceChanged 的一个缺陷:

  • 它把“相对打开文件夹的 ID”拿去匹配打开文件夹自己的 status。打开的文件夹没有自己的仓库时,所有嵌套仓库都会显示“父仓库引用未变”。
  • 它也不处理更深的嵌套。

Core 改为按主机路径找到列表中最近的祖先仓库,再用相对该祖先的路径去匹配祖先的 gitlink 改动。

改动 / Changes

workspace.repositories 条目新增 id、name、role(rust/crates/git/src/contracts.rs、repository.rs)

  • id:相对打开文件夹、用 / 分隔的身份:
    • 文件夹自身为 .;
    • 下层仓库如 service-a、libs/core;
    • 外层仓库每上一层写一个 ..(打开的是某个检出的 packages/app 时为 ../..)。
    • 远程工作区用主机路径计算,结果与本地文件夹一致。
  • role:
    • workspace:包含打开文件夹的仓库,即文件夹自身或外层检出。最多一个,总是排第一。
    • nested:文件夹下面的仓库。
    • 打开的文件夹不在任何仓库里时,没有 workspace 条目。
  • name:视图显示的标签。workspace 仓库显示根目录名,nested 仓库显示 id,这样同名的两个嵌套目录也能区分。
  • 和打开文件夹没有共同祖先的根,没有可用的身份,不再列出。规范化路径下正常不会出现。
  • 只增加字段,旧调用方不受影响。当前桌面端的 Go 解码不拒绝未知字段。

新命令 git.workspaceRepositorySelection(rust/crates/git/src/workspace_repositories/selection.rs)

  • 请求:{ repositories: <workspace.repositories 条目原样>, selected? }。
  • 响应:{ active, fellBack, others }。
  • active 的取值顺序:
    1. 之前选中的仓库仍在列表里,就保持它;
    2. 否则取 workspace 条目;
    3. 再否则取第一个条目;
    4. 列表为空时为 null。
  • 只有传了 selected 且它已不在列表里时,fellBack 才为 true。
  • others 按发现顺序列出其余 ID。
  • 对应 Go 里的 branchRoot 默认值、branchGroups、referenceGroups 和 logFallsBack。

新命令 git.workspaceChangeGroups(rust/crates/git/src/workspace_repositories/change_groups.rs)

  • 请求:{ repositories: [<workspace.repositories 条目> + changes: <该仓库 git.status 的 changes 原样>] }。
  • 每个有改动的仓库一组,按发现顺序排列。每组包含:
    • fileCount、stagedCount、allStaged、toggleable;
    • 组复选框要写的 staging: { operation, paths };
    • parentReference(moved 或 unchanged)。
  • 顶层给出所有仓库合计的 fileCount、stagedCount、allStaged,以及总复选框的 stageAll: { operation, repositories: [{ id, paths }] }。
  • 暂存规则与现在的 Go 一致:
    • 全部已暂存时,复选框执行取消暂存,否则执行暂存;
    • 不能切换暂存的改动不写,例如只有子仓库内容变化的 gitlink;
    • 重命名两侧的路径都写。
  • parentReference:
    • 取列表中最近的祖先仓库,看它的 status 里这个 gitlink 的 commitChanged;
    • 祖先没有把它记录为 submodule(独立仓库、被忽略或未跟踪)时不返回。
  • 对应 Go 里 nested_repos_view.go 的分组与 allStaged,以及 stageAllRepositories、submoduleReferenceChanged。

两条新命令共用的校验(rust/crates/git/src/workspace_repositories.rs)

拒绝发现结果不可能产生的列表,返回 invalid_request:

  • 超过 10000 个仓库;
  • ID 为空、超过 4096 字节、以 / 开头,或含 \ 或 NUL;
  • 路径为空或含 NUL;
  • name 为空;
  • ID 或路径重复;
  • 不止一个 workspace 条目;
  • 改动总数超过 100 万,或改动路径为空。

git.workspaceCommitPrepare 新增可选 workspaceRepositories(rust/crates/git/src/workspace_commit.rs)

  • 传入 workspace.repositories 条目原样,按上面的规则校验。
  • Core 按发现顺序,用发现的 id,绑定宿主没有绑定的每个 nested 仓库。workspace 条目始终由宿主自己绑定,Core 不会添加。
  • 发现之后已删除的仓库跳过。
  • 带 pathScope,或 previous 计划带 pathScope 时,不添加任何仓库。
  • 重试时只添加 previous 会话里已有的 ID,所以经过确认的计划不会在两次尝试之间多出仓库。
  • 对应 Go 里的 includeNestedRepositories。

协议接线与文档

  • rust/crates/core/src/protocol/command.rs、runtime/dispatcher/git.rs:注册两条新命令。
  • rust/contracts/rust-core-api.md:命令表新增两行,并更新以下几处说明:
    • workspace.repositories 的 id/role/name;
    • 两条新命令的完整请求、响应和校验;
    • git.workspaceCommitPrepare 的 workspaceRepositories。

Fixture 与测试

  • 新增 fixture:
    • rust/fixtures/git/workspace-repository-selection-v1.json:6 个正常用例、9 个非法用例;
    • rust/fixtures/git/workspace-change-groups-v1.json:6 个正常用例、8 个非法用例。
  • 扩展 fixture:rust/fixtures/workspace/repositories-v1.json,覆盖以下情况:
    • 本身是仓库的文件夹;
    • 只装嵌套仓库的父文件夹;
    • 检出里的子文件夹;
    • 远程主机路径。
  • lithe-git 单元测试覆盖以下内容:
    • 两份新 fixture;
    • 数量和 ID 长度上限;
    • 位于文件系统根的仓库;
    • 发现条目的投影;
    • 没有共同祖先的根。
  • lithe-core 新增 tests/git_workspace_repositories.rs:
    • 通过 execute_json 跑两份 fixture;
    • 真实 Git 测试:父仓库、作为 submodule 的 libs/B、被 .gitignore 忽略的独立仓库 extras。验证 discovery 和 status 原样传入后的选择、分组、stageAll,以及子仓库提交前后 parentReference 从 unchanged 变为 moved。
  • tests/git_workspace_commit.rs 新增真实 Git 测试,覆盖:
    • 绑定发现的嵌套仓库;
    • 跳过已删除的仓库;
    • 带 pathScope 时不添加;
    • 重试时不新增;
    • 非法条目返回 invalid_request。
  • tests/git.rs、git_repository_discovery.rs、remote_sessions.rs:按新的条目形状更新断言。

不在本 PR 范围内 / Out of scope

  • 桌面端接入,在合入后另开 Lithe-IDEA PR:
    • 升级 PINNED_CORE_REVISION,全部改用 Core 的 ID;
    • 调用这三条命令,去掉“打开的文件夹必须是仓库”的判断;
    • 删除对应的 Go 重复逻辑;
    • 嵌套仓库的“父仓库引用未变”提示改为只在 parentReference == "unchanged" 时显示;
    • 同一个 PR 更新功能矩阵。
  • 引用面板按 kind(local/tag)和小写子串过滤:这是逐键输入的视图过滤,单仓库引用列表也用同一套,不放进这些命令。
  • #2013 的聚合 Git Log(多仓库合并历史、按仓库筛选)不在本 PR,等桌面端接入第一阶段后再设计。

类型专属:feat

多平台对齐

  • 两端都具备:本 PR 只改 Rust Core,命令与平台无关。macOS 与 Windows 用的是同一个 Viva 桌面端,后续接入 PR 合入后两端同时具备。路径和 ID 规则覆盖 Windows 盘符与 UNC 形式,已交叉检查 Windows 目标的 clippy。

范围检查 / Scope checks

  • 功能矩阵:N/A。本 PR 没有用户可观察的变化,矩阵条目 git-multi-repository-references 随桌面端接入 PR 一起更新。
  • 插件归属:不涉及任何具体插件,测试只用临时目录里的合成仓库和共享 fixture。
  • UI 验证 / i18n:N/A。没有界面和文案。name 是文件名,不翻译。
  • 统一日志系统:N/A。三条命令都是纯投影或已有准备流程的扩展,没有新增生命周期、终态或失败路径日志。
  • 逻辑归属:全部在 Rust Core。共享契约和 fixture 先于平台接入,桌面端接入后删除 Go 里的重复逻辑。
  • 性能与内存:
    • 两条新命令都是纯计算,不启动进程,也不读文件。
    • 输入有上限:仓库 1 万、改动 100 万、ID 4096 字节。
    • 祖先查找每个仓库只走一次路径的祖先链,配合哈希表,没有 O(n²) 的列表扫描。
    • workspaceCommitPrepare 只对发现的嵌套仓库多做一次 validate_root。
  • 设置:N/A。
  • 插件契约:N/A。没有改插件契约、能力版本、插件函数协议或宿主方法,新命令是桌面端的 Core 命令。
  • 发行包与资源:N/A。没有新增运行时资源。
  • 无用代码与重构:Core 里没有因此变成无用的代码。Go 里的重复逻辑在桌面端接入 PR 中删除。

验证 / Verification

本机为 Apple M4,Rust 1.100.0。

命令或操作 平台 结果
cargo fmt --manifest-path rust/Cargo.toml --all -- --check macOS arm64 通过
scripts/rust-core/verify-rust-core-comments.sh macOS arm64 通过
node scripts/rust-core/verify-platform-tests.mjs macOS arm64 通过
node --test scripts/rust-core/*.test.mjs macOS arm64 通过:16 passed
node scripts/rust-core/affected-crates.mjs macOS arm64 Linux 跑 --workspace;macOS、Windows 只跑平台测试 crate
cargo test --manifest-path rust/Cargo.toml --workspace --features lithe-remote/server --no-fail-fast macOS arm64 通过:2127 passed,0 failed
cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets -- -D warnings macOS arm64 通过
cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings macOS 交叉检查 Windows 目标 通过
Lithe-IDEA write-stable-tests 的 verify-test-stability.mjs,以本仓库为根 macOS arm64 通过:0 violations

未能执行的检查和原因:

  • 没有在 Windows 上运行新测试。按 affected-crates,PR CI 只在 Linux 上跑 lithe-git、lithe-core 的测试,Windows 只编译检查。路径规则在 Windows 上的表现,等桌面端接入后在 Windows 虚拟机上验收。
  • 没有在 Linux 上本地运行,由 PR 的 Linux CI 运行。

提交前确认 / Final checks

  • 分支从最新 main(ad148ec)创建,目标分支 main。
  • 以 Draft 提交,本地验证已通过。
  • 没有无关改动、构建产物、凭据或个人路径。
  • 本次验证没有启动 Lithe 应用,没有进程残留。

…ps in Core

A folder without its own .git that holds several independent repositories
is opened as one project (1lck/Lithe-IDEA#2013). The desktop decided in Go
which repository a view acts on, how the repositories' changes group and
stage, and whether a parent still has to record a nested commit, and it
assumed the opened folder is always a repository. Core now owns those rules
so every host shares them.

- workspace.repositories entries carry id (relative to the opened folder,
  `/`-separated, `..` for an enclosing repository), name and role
  (workspace or nested). A folder outside every repository has no workspace
  entry. Remote workspaces compute IDs from host paths.
- git.workspaceRepositorySelection takes the discovery entries and the
  previous pick, and answers the repository to act on, whether it fell
  back, and the others in discovery order.
- git.workspaceChangeGroups takes the discovery entries with their
  git.status changes, and answers per-repository groups, counts, the
  checkbox states, the paths each checkbox stages or unstages, and whether
  the deepest listed ancestor's gitlink to a nested repository moved. An
  untracked or ignored independent repository gets no parent hint.
- git.workspaceCommitPrepare accepts workspaceRepositories and binds every
  nested repository the host did not bind, except for path-scoped commits
  and retries, so the reviewed plan never gains repositories between
  attempts.

Fixtures: rust/fixtures/git/workspace-repository-selection-v1.json,
rust/fixtures/git/workspace-change-groups-v1.json, and the extended
rust/fixtures/workspace/repositories-v1.json.

Refs 1lck/Lithe-IDEA#2013
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 16, 2026 09:23
@xiaoyumuxi
xiaoyumuxi merged commit db54ba0 into main Nov 16, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant