当前项目已经具备一套比较清晰的分层和跨端通信骨架:
IServiceAccessor + RPC 服务链路访问业务能力IPlatformService 只承载必须经过宿主环境的动作,不承载常规业务逻辑Git 功能如果要长期可维护,应该复用现有服务架构,而不是走新的直连路径。
Types -> Config -> Repo -> Service -> Runtime -> UI 依赖方向内IPlatformService推荐新增 IGitService,走现有 RPC 服务链路。
原因:
这也符合当前 IPlatformService 的职责边界:它只放 native dialog、窗口控制、外链打开之类“必须穿越宿主边界”的操作。
GitService,本地 / 远程都走同一服务协议这是推荐方案。
flowchart LR
UI["UI / Hooks / Side Pane"] --> Runtime["useGitRepository / useGitActions"]
Runtime --> RPC["RPC Channel"]
RPC --> Service["IGitService"]
Service --> Repo["GitCliRepo"]
Repo --> Provider["GitCommandProvider"]
Provider --> Git["System Git CLI"]
建议新增 Git 领域 DTO,放到 packages/shared/src/git.ts,并在 packages/shared/src/index.ts 导出。
建议至少包含:
GitRepositorySummary
workspacePathrepoRootworkspaceInRepoPathbranchNameheadRefTypeaheadbehindisDirtyisGitAvailableisRepositoryGitFileChange
pathrepoRelativePathworkspaceRelativePathxykindisStagedisUntrackedisConflictedGitDiffRequestGitDiffResultGitIdentity
userNameuserEmailnameSourceemailSource建议新增 Git 领域配置层,负责:
建议文件:
packages/services/src/git/config.ts为了符合项目里“横切关注点通过 Providers 进入”的原则,Git 域内部建议显式引入 Provider 层。
建议至少拆成两个 provider:
GitCommandProvider
git 命令GitEnvironmentProvider
Provider 层只做“如何执行”和“如何适配环境”,不做 Git 业务语义。
Repo 层是对 Git CLI 的薄封装,不承载 UI 语义。
建议文件:
packages/services/src/git/repo/gitCliRepo.ts建议职责:
resolveRepository(workspacePath)getStatus(workspacePath)getDiff(workspacePath, path, staged)stage(workspacePath, paths)unstage(workspacePath, paths)discard(workspacePath, paths, stagedScope)commit(workspacePath, message)getIdentity(workspacePath)Service 层对 UI 暴露稳定语义,并隐藏 Git CLI 细节。
建议文件:
packages/services/src/git/git.tspackages/services/src/git/gitService.ts建议对外接口:
getRepositorySummarygetChangesgetDiffgetBranchComparisonstagePathsunstagePathsdiscardPathscommitgetIdentityrefresh如果后续需要订阅式刷新,再补动态事件:
onDynamicRepositoryEvent(workspacePath)UI 层只通过 hooks 消费 IGitService。
当前 hooks:
packages/ui/src/hooks/useGitRepository.tspackages/ui/src/hooks/useGitActions.tspackages/ui/src/hooks/useGitAutoRefresh.tsUI 组件建议:
GitPaneGitRepositoryHeaderGitChangeSourceSelectGitChangesTreeGitDiffPanelGitCommitPanelGit pane 左侧建议有一个变更来源下拉,不同模式切换不同的数据来源。
建议支持四种模式:
未暂存
getChanges / getDiff已暂存
HEAD 的改动getChanges / getDiff全部分支更改
getBranchComparison上一轮更改
IGitService这里建议把“Git pane”理解为“统一的改动审阅面板”,而不是把每一种视图都硬塞成 Git service 的职责。
其中:
IGitService上一轮更改 属于会话级改动审阅,只在 Git pane 里共用一套树和 diff 展示壳,保持只读这样做的好处是:
上一轮更改 不需要为了 UI 入口位置而伪装成 Git 数据createLocalServices() 注册本地 GitServicefile/system/terminal 一样,来自远端服务结论:
file/system/terminal 对齐,而不是和 setting 对齐推荐坚持使用系统 Git CLI,而不是引入 JS Git 实现。
原因:
includeIf、平台默认路径、Git for Windows 等真实环境建议命令:
git rev-parse --show-toplevelgit status --porcelain=v2 --branch -zgit config --get user.namegit config --get user.emailgit config --list --show-origin --nullgit diff --no-ext-diff --no-textconv -- <path>git diff --cached --no-ext-diff --no-textconv -- <path>git diff --no-ext-diff --no-textconv @{upstream}...HEAD --git add -- <path...>git restore --staged -- <path...>git restore --worktree -- <path...>git commit -m <message>产品构造的 git 子进程必须经过 GitCommandProvider.run(packages/services/src/git/providers/gitCommandProvider.ts)。
调用方只传子命令与参数,不再自行拼接安全参数;入口统一做以下加固(buildProductGitArgs):
-c core.fsmonitor=:自动查询不执行仓库配置的 fsmonitor 脚本。打开工作区不等于授权运行该脚本。diff、show、log 时,紧跟子命令追加 --no-ext-diff --no-textconv。
--no-ext-diff 只禁止 external diff driver(diff.external 与 diff.<driver>.command),不禁止 diff.<driver>.textconv。diff -p、--binary、--no-index、show、log -p。
--numstat、--stat、--name-only、show <ref>:<path> 不执行,追加参数对它们无副作用。--binary 不会关闭 textconv。GitCommandExecutionResult.args 保留调用方原始参数,只有实际 spawn 的 argv 带前缀。预览与统计使用原始内容,worktree 补丁必须可应用到原始文件,不接受转换后的展示文本。 此约束只针对产品构造的命令,不改写用户显式执行的终端/Bash 命令。
范围外:
apps/zcode-cli 的 workflow-git-world-read.ts)是独立进程,argv 在其构造模块内声明同样两个参数。
CLI 自动 git 快照(git-snapshot.ts)使用自己的固定环境与 -c 覆盖,不经过本入口。回归验收:pnpm test:git-invocation,在临时目录创建同时带 diff.external、diff.<driver>.textconv、core.fsmonitor
脚本的仓库;脚本只写调用标记并输出可区分的文本。先用原生 git 确认未加防护时标记会出现,再经 provider 调用
diff、show、log -p、--no-index、status,即使调用方自带 --no-ext-diff,断言无标记、patch 来自原文。
workspacePath 不一定等于 repoRoot。
例如:
因此 Git 域必须同时保留三类路径:
workspacePathrepoRootworkspaceInRepoPath推荐行为:
repoRoot 执行 Git这样能避免 workspace 打开的是子目录时,直接看到整仓库其它区域变更而产生困惑。
根据项目约束,涉及交互和复杂状态切换的能力需要补日志。
建议:
packages/ui/src/logger.tsGitCommandProvider 不默认打印每条 Git 命令;Git 探测和状态查询属于高频内部细节,
需要排查时优先在上层一次性生命周期、失败分支或专门调试入口补充有边界的日志。不要记录:
当前右侧共享 pane 已经统一承载 browser 和 code-viewer。
推荐扩展为:
browsercode-viewergit而不是在左侧 WorkspaceSidebar 再新增一个与 task 并列的大导航区。
原因:
IGitService上一轮更改 的数据复用方式,后续结合现有 task store 再细化GitService