git 逻辑

This commit is contained in:
2026-06-10 15:30:52 +08:00
parent 41f2a84979
commit 023ab2f30e
17 changed files with 221 additions and 64 deletions
+12 -3
View File
@@ -26,7 +26,8 @@ type Runner interface {
Push(ctx context.Context, dir, branch string, cred *Credential) error
Commit(ctx context.Context, dir, message string, addAll bool) (sha string, err error)
Status(ctx context.Context, dir string) ([]FileStatus, error)
WorktreeAdd(ctx context.Context, dir, branch, path string) error
MergeFFOnly(ctx context.Context, dir string) error
WorktreeAdd(ctx context.Context, dir, branch, path, startPoint string) error
WorktreeRemove(ctx context.Context, dir, path string) error
WorktreeList(ctx context.Context, dir string) ([]Worktree, error)
}
@@ -103,11 +104,19 @@ func NewDefaultRunner(cfg Config) *DefaultRunner {
//
// 返回 stdout/stderr 字节切片与错误。错误为 GitError 时携带 stderr 与 exit code。
func (r *DefaultRunner) exec(ctx context.Context, cwd string, env []string, args ...string) (stdout, stderr []byte, err error) {
cmd := exec.CommandContext(ctx, r.cfg.Binary, args...)
// 统一前置 `-c credential.helper=` 清空 helper 链,绕过 Git for Windows
// 默认 GCM 抢占 PAT 的问题。
fullArgs := append([]string{"-c", "credential.helper="}, args...)
cmd := exec.CommandContext(ctx, r.cfg.Binary, fullArgs...)
if cwd != "" {
cmd.Dir = cwd
}
cmd.Env = append(os.Environ(), env...)
// base env 总是禁交互;调用方 env 追加在后可覆盖。
baseEnv := append(os.Environ(), "GIT_TERMINAL_PROMPT=0", "GCM_INTERACTIVE=never")
cmd.Env = append(baseEnv, env...)
// WaitDelay:超时 kill git 后,孤儿 ssh/git-remote-https 子进程可能持有
// stderr pipe 写端导致 cmd.Run() 无限阻塞,给一个上限强制收尾。
cmd.WaitDelay = 10 * time.Second
var so, se bytes.Buffer
cmd.Stdout = &so
cmd.Stderr = &se