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
+23 -1
View File
@@ -3,6 +3,8 @@ package git
import (
"errors"
"fmt"
"net/url"
"strings"
)
// GitError 是 git 子进程返回非零或超时时的统一错误形式。Stderr 已截断 ≤2000 字符;
@@ -15,7 +17,27 @@ type GitError struct {
}
func (e *GitError) Error() string {
return fmt.Sprintf("git %v: exit %d: %s", e.Args, e.ExitCode, e.Stderr)
// 对 Args 中形如 scheme://user:pass@host 的 URL 脱敏,避免 PAT 明文
// 写进 last_sync_error / audit。
redacted := make([]string, len(e.Args))
for i, a := range e.Args {
redacted[i] = redactURLUserinfo(a)
}
return fmt.Sprintf("git %v: exit %d: %s", redacted, e.ExitCode, e.Stderr)
}
// redactURLUserinfo 剥离形如 scheme://user[:pass]@host 中的 userinfo,
// 输出 scheme://host...。非 URL 字符串或解析失败时原样返回。
func redactURLUserinfo(s string) string {
if !strings.Contains(s, "://") {
return s
}
u, err := url.Parse(s)
if err != nil || u.User == nil {
return s
}
u.User = nil
return u.String()
}
func (e *GitError) Unwrap() error { return e.Cause }