git-sync-web 自动提交 2026-09-08 18:21:40
This commit is contained in:
@@ -87,6 +87,42 @@ func branchExists(path, branch string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// remoteTip 返回远程分支当前指向的 SHA。
|
||||
// 优先读本地远程跟踪引用(refs/remotes/<remote>/<branch>,快);
|
||||
// 若本地跟踪引用不可用(个别环境下 ref 存储读写异常),回退用 ls-remote 直查远程。
|
||||
func remoteTip(path, remote, branch string) (sha string, exists bool) {
|
||||
if remoteRefExists(path, remote, branch) {
|
||||
out, err := gitRun(path, 15*time.Second, "rev-parse", remote+"/"+branch)
|
||||
if err == nil {
|
||||
return strings.TrimSpace(out), true
|
||||
}
|
||||
}
|
||||
out, err := gitRun(path, 30*time.Second, "ls-remote", remote, "refs/heads/"+branch)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
fields := strings.Fields(strings.TrimSpace(out))
|
||||
if len(fields) < 2 {
|
||||
return "", false
|
||||
}
|
||||
return fields[0], true
|
||||
}
|
||||
|
||||
// aheadBehindSha 基于远程分支的具体 SHA 计算 (领先, 落后),不依赖本地跟踪引用
|
||||
func aheadBehindSha(path, tip string) (int, int, error) {
|
||||
out, err := gitRun(path, 30*time.Second, "rev-list", "--left-right", "--count", "HEAD..."+tip)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
parts := strings.Fields(out)
|
||||
if len(parts) != 2 {
|
||||
return 0, 0, fmt.Errorf("无法解析 rev-list 输出: %q", out)
|
||||
}
|
||||
a, _ := strconv.Atoi(parts[0])
|
||||
b, _ := strconv.Atoi(parts[1])
|
||||
return a, b, nil
|
||||
}
|
||||
|
||||
func dirtyCount(path string) int {
|
||||
out, err := gitRun(path, 30*time.Second, "status", "--porcelain")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user