模块定位
Git 操作引擎是代码托管的核心——把原生 git 的几十种操作封装成一套 git.Interface,供 领域服务层和 API 层的 controller 透明调用。它解决的核心问题是:代码托管的本质是”在裸 git 之上叠加协作语义”——PR、分支保护、codeowner、hook 校验都要在 git 的 ref 更新前后插入逻辑,而 git 本身是个 CLI 工具不是库。Harness 的做法是用 api.Git 直接在本地文件系统执行 git CLI 命令,用 hook.RefUpdater 状态机保证 hook 调用顺序,用泛型 channel 做大 diff/blame 的流式返回。
重要澄清:本模块是单进程同库设计——
git.Service直接在本地执行原生 git CLI(git commit-tree、git update-ref、git cat-file等),不存在 gRPC 远程调用 git server。kuberesolver.go仅在init()里RegisterInCluster()注册 k8s DNS resolver,本模块未实际建立 gRPC 连接(go.mod 中 grpc 标记为 indirect)。
模块架构
git/
interface.go Interface 抽象(40+ 方法:repo/tree/branch/tag/commit/diff/merge/blame/ref/...)
service.go Service 实现:本地执行 git CLI(持有 *api.Git、hookClientFactory、store)
wire.go ProvideService 返回 Interface 接口类型(便于 mock)
api/api.go api.Git:底层轻量 CLI 执行器(traceGit、lastCommitCache、githookFactory)
command/ git CLI 命令构造与执行
hook/ githook 机制(RefUpdater 状态机 + ClientFactory + CLI 二进制)
sha/ sha.SHA 值类型(Nil/None/EmptyTree 常量)
stream.go StreamReader[T] 泛型流读取器
mapping.go api.* → git.* 类型映射
commit.go merge.go branch.go diff.go blame.go ref.go ... 各操作实现
sharedrepo/ 共享临时仓库(merge 等操作)
storage/ 仓库存储
调用链路
以 Merge 为例(最复杂的操作,git/merge.go,340 行):
Service.Merge(ctx, *MergeParams) in git/merge.go
├─ params.Validate() 校验 BaseSHA/BaseBranch 互斥参数
├─ s.git.ResolveRev(ctx, repoPath, branch) 解析 branch → SHA
├─ s.git.GetMergeBase(...) 找 merge base
├─ s.git.DiffShortStat(...) 统计变更
├─ hook.CreateRefUpdater(...) in git/hook/refupdate.go
│ 状态机:INIT → PRE → UPDATE → POST → DONE
├─ sharedrepo.Run(...) 在共享临时仓库执行 merge
│ 按 merge.Func 策略:Merge / Squash / Rebase / FastForward
├─ refUpdater.Init() 注册 ref 更新
└─ 返回 MergeOutput(含 conflictFiles 或 mergeSHA)
简单操作如 GetCommit in git/commit.go 模式清晰:params → repoPath → s.git.GetCommitFromRev() → mapCommit() → output。
方法速查表
| 方法 | 路径 | 职责 | 关键设计 |
|---|---|---|---|
Service.Merge |
git/merge.go |
merge 操作 | 4 策略 + 共享临时仓库 |
Service.GetCommit |
git/commit.go |
取 commit | api→git mapping |
hook.RefUpdater |
git/hook/refupdate.go |
ref 更新状态机 | INIT→PRE→UPDATE→POST |
Service.Diff |
git/diff.go |
diff 流式 | io.Pipe + 双 goroutine |
Service.Blame |
git/blame.go |
blame 流式 | channel 返回 *BlamePart |
Service.ServicePack |
git/service_pack.go |
Smart HTTP | git-upload-pack/receive-pack |
ProcessPreReceiveObjects |
git/pre_receive_pre_processor.go |
推送对象扫描 | 超大文件/邮箱/LFS |
核心实现
Interface 抽象与 Service 实现
Interface in git/interface.go 定义 40+ 方法,覆盖:Repository(CreateRepository/DeleteRepository/SyncRepository)、Tree/Blob/Submodule(GetTreeNode/ListTreeNodes/GetBlob)、Branch(CreateBranch/DeleteBranch/UpdateDefaultBranch)、Tag、Commit(GetCommit/CommitFiles/MergeBase/IsAncestor)、Diff(RawDiff/Diff/DiffCut)、Merge(Merge/Revert)、Blame、Ref、Smart HTTP(GetInfoRefs/ServicePack)、ScanSecrets/Archive/OptimizeRepository 等。
Service in git/service.go 持有 reposRoot(仓库存储根目录)、sharedRepoRoot(共享临时仓库)、git *api.Git(底层 CLI 封装)、hookClientFactory、store、gitHookPath、reposGraveyard(删除仓库回收站)。通过 s.git(*api.Git in git/api/api.go)直接执行原生 git CLI——api.Git 仅持有 traceGit、lastCommitCache、githookFactory 三个字段,是个轻量命令执行器。ProvideService in wire.go 返回 Interface 接口类型,便于 mock 测试。
双路径 githook 机制
Hook 是本模块最精妙的设计,分运行时回调和CLI 二进制两路径:
运行时路径(Service 调用 hook):hook.ClientFactory in git/hook/client.go 是工厂接口,NewClient(envVars) 按环境变量创建 client。ControllerClientFactory in app/api/controller/githook/client.go 是实际实现,直接同进程调用 Controller.PreReceive/Update/PostReceive。hook.RefUpdater in git/hook/refupdate.go 是状态机(INIT→PRE→UPDATE→POST→DONE),保证 hook 调用顺序,UpdateRef 通过 git update-ref --stdin -z 原子更新多个 ref。
CLI 路径(git 调用 hook 二进制):hook/cli.go 注册 kingpin 命令 pre-receive/update/post-receive,hook/cli_core.go 从 stdin 读 git 格式的 ref 更新(<old> SP <new> SP <ref> LF)。hook/env.go 通过 GIT_HOOK_PAYLOAD 环境变量传递 gob+base64 编码的 payload,SanitizeArgsForGit 把 git 的 hooks/pre-receive 命令名映射为 CLI 子命令。
ProcessPreReceiveObjects in git/pre_receive_pre_processor.go 在 pre-receive 阶段扫描推送对象,检测超大文件(findOversizeFiles)、committer 邮箱不匹配(findCommitterMismatch)、LFS 指针缺失(findLFSPointers),用 cat-file --batch-check 列对象逐个检查。ServicePack in service_pack.go 处理 Smart HTTP 的 git-upload-pack(fetch)和 git-receive-pack(push),push 时注入 CreateEnvironmentForPush 环境变量(含 hook payload)。
流式处理
StreamReader[T] in git/stream.go 是泛型流读取器,封装 <-chan T + <-chan error 双 channel——Next() select 两个 channel,任一关闭返回 EOF。Diff 流式 in git/diff.go:Diff() 用 io.Pipe() 连接两个 goroutine,一个执行 rawDiff 写 pipe,另一个用 diff.Parser 解析 pipe 输出经 channel 返回 *FileDiff,实现边生成边解析边返回。Blame 流式 in git/blame.go:启动 goroutine 调 s.git.Blame() 获取 reader,循环 reader.NextPart() 逐块发 channel,支持行号范围过滤。这对大仓库的大 diff/长 blame 至关重要——避免一次性把整个 diff 读进内存。
sha 值类型与错误模型
sha.SHA in git/sha/sha.go 是 git SHA 的值类型封装:New(value) 用正则 ^[0-9a-f]{4,64}$ 校验(支持 SHA-1 40 字符和未来 SHA-256 64 字符)。预定义 Nil(全零,表删除)、None(空字符串,表未设置)、EmptyTree 常量,实现 GobEncode/Decode、JSON Marshal/Unmarshal、driver.Value(SQL)、JSONSchema。IsNil()/IsEmpty() 区分语义。错误映射 in git/errors.go 极简(仅 ErrNoParamsProvided),实际用 errors 包的 InvalidArgument/Conflict/NotFound 语义化构造器,经 API 层 usererror.Translate 的 httpStatusCode() 映射到 HTTP 状态码。
设计模式
| 模式 | 位置 | 为什么用 |
|---|---|---|
| Interface + Impl 分离 | Interface/Service,ProvideService 返回接口 |
便于 mock 测试 |
| Params/Output 模式 | 每操作 XxxParams/XxxOutput(嵌入 ReadParams/WriteParams) |
参数显式、读写权限分离 |
| Strategy(merge) | merge.Func 按 enum.MergeMethod |
4 种合并策略可插拔 |
| 状态机 | hook.RefUpdater INIT→PRE→UPDATE→POST |
保证 hook 顺序 |
| 泛型流 | StreamReader[T] + io.Pipe |
大 diff/blame 边读边返回 |
| Mapping 层 | mapping.go api.↔git. |
隔离底层 CLI 返回与上层接口 |
模块间交互
被 app/services/webhook、app/services/importer、app/services/merge 等注入 git.Interface。Hook 互调形成双向依赖:git.hook.ClientFactory 由 app/api/controller/githook 实现,ControllerClient 持有 git.Interface(RestrictedGIT)——git 模块调用 hook controller,hook controller 回调 git 模块。底层 api.Git 通过 git/command 包直接执行 git CLI。
扩展方式
新增 git 操作(如 GetTag):① interface.go 添加方法签名;② 新建 tag.go 实现 func (s *Service) GetTag(...) 调 s.git.GetTag();③ mapping.go 加 mapTag();④ 若 api.Git 无对应方法还需在 git/api/ 加底层 CLI 实现。无需改 proto(不存在 gRPC 层)。
新增 hook 回调:① git/hook/types.go 加 Input/Output 类型;② client.go 的 Client interface 加方法;③ app/api/controller/githook/client.go 的 ControllerClient 实现;④ refupdate.go 状态机加状态和调用点;⑤ cli_core.go + cli.go 加 CLI 命令和 stdin 解析。