Help us shape the future of Go! Take the 2026 Go Developer Survey

workcopy

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

deepen.go 浅克隆工作副本的按需历史补全(收编自 argus internal/gitdeepen, v0.10.39)。

背景:依赖 merge-base 的 range 审查在 workcopy 的"默认分支浅克隆 + head checkout"下必挂("cannot find merge-base")。全量 `git fetch --unshallow` 把完整历史拉进关键路径,慢且重。本文件先本地探测 merge-base 可达性, 不可达再 `--deepen` 指数加深(64 起步逐轮 ×2):多数 PR 一两轮即收敛, 代价从"全仓历史"降到"base↔head 的实际距离"。

refspec 保持显式全量:clone --depth 1 隐含 --single-branch,裸 fetch 只补 默认分支的远端引用,PR base 非默认分支时 origin/<base> 不存在(真件实测 复现)。deepen 不移除 .git/shallow 标记——守卫因此从"标记存在"改为 "merge-base 可达探测",已补全的工作副本(标记可能仍在)探测通过即零开销。

Package workcopy Git 工作副本沙箱服务。 浅克隆 base 默认分支 + fetch PR head + checkout,供 cli 插件在真实工作树上执行。 凭证嵌在 clone URL 中,绝不落盘。singleflight 防并发重复 clone。 引用归零后目录**保留**(供同 PR 下次审查增量复用,省 base 分支重克隆), TTL 由 Sweep 兜底回收。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureMergeBase

func EnsureMergeBase(ctx context.Context, dir, baseBranch string, timeout time.Duration) error

EnsureMergeBase 确保 origin/<baseBranch> 与 HEAD 的 merge-base 可达。

  • 探测通过(完整克隆、已补全、或 base 与 head 重合等价场景)零 fetch 返回;
  • 探测失败且 .git/shallow 存在 → deepen 指数加深循环(每轮后重探);
  • 探测失败但非浅克隆 → 静默返回:merge-base 失败另有原因(base 分支缺失、 非 git 目录等),交由后续 OCR 执行报真实错误——与旧"无 shallow 标记即 跳过"语义一致;
  • 加深轮失败/预算耗尽仍不可达 → 报错降级。整条消息过 logredact(git 传输 失败回显带凭证 clone URL——ocrshim 通道此错误经 MCP 原样回传 argus)。

func EnsureMergeBaseTLS

func EnsureMergeBaseTLS(ctx context.Context, dir, baseBranch string, timeout time.Duration, insecureTLS bool) error

EnsureMergeBaseTLS EnsureMergeBase 的 TLS 形态:insecureTLS=true 时对 deepen fetch 注入 GIT_SSL_NO_VERIFY(与 Pool.runGit 同款纪律——自签 https 平台的 clone/PR fetch 均走该环境,deepen fetch 漏注会让补全在此类平台确定性失败 且静默降级丢失 range 审查;第八轮审计 C 级修复)。

Types

type Pool

type Pool struct {
	Root string
	Log  logx.Logger

	// CredentialOf 返回 (cloneBaseURL, token):由调用方按平台注入。
	CredentialOf func(platform string) (baseURL, token string, ok bool)
	// InsecureTLSOf 平台是否免 TLS 校验。
	InsecureTLSOf func(platform string) bool
	// contains filtered or unexported fields
}

Pool 工作副本沙箱池。

func NewPool

func NewPool(root string, log logx.Logger) *Pool

NewPool 创建工作副本沙箱池。

func (*Pool) Ensure

func (p *Pool) Ensure(ctx context.Context, key WorktreeKey) (string, error)

Ensure 返回就绪的工作副本目录(浅克隆 base + checkout PR head)。 同 key 并发调用共享同一目录(引用计数)。

func (*Pool) Release

func (p *Pool) Release(key WorktreeKey)

Release 任务结束后释放。引用归零**不再立即删目录**:保留供同 PR 下次审查 增量复用(换 head 只 fetch 新 refspec),TTL 由 Sweep 兜底回收。

func (*Pool) Sweep

func (p *Pool) Sweep(ttl time.Duration)

Sweep 清理泄漏的沙箱目录(兜底 TTL 扫描)。 只回收引用归零且超 TTL 的条目与孤儿目录——rc>0 的在用目录一律保留 (长任务超 TTL 时被删即是生产事故),因此 TTL 应配置为大于最长任务时长; 进程崩溃导致的泄漏目录(条目随进程消失)由下方孤儿扫描兜底。

type WorktreeKey

type WorktreeKey struct {
	Platform      string
	Owner         string
	Repo          string
	Number        string
	HeadSHA       string
	DefaultBranch string
}

WorktreeKey 工作副本标识(平台/仓库/PR/SHA)。

func (WorktreeKey) String

func (k WorktreeKey) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL