git-sync-web 自动提交 2026-09-08 18:21:40

This commit is contained in:
tamakyi
2026-09-08 18:21:41 +08:00
parent f882668404
commit f5838fd8b6
4 changed files with 52 additions and 6 deletions
+8 -6
View File
@@ -531,6 +531,7 @@ func (e *Engine) syncOnce(s *repoState, trigger string) {
// 分支对齐:本地当前分支与配置不一致时,自动切换/创建(对齐原 bash 脚本行为)
// 否则 push 会因 src refspec 不存在而失败
if cur, cerr := currentBranch(cfg.RepoPath); cerr == nil && cur != cfg.Branch {
tip, remoteHasBranch := remoteTip(cfg.RepoPath, remote, cfg.Branch)
switch {
case branchExists(cfg.RepoPath, cfg.Branch):
if _, err := gitRun(cfg.RepoPath, 30*time.Second, "checkout", cfg.Branch); err != nil {
@@ -538,12 +539,12 @@ func (e *Engine) syncOnce(s *repoState, trigger string) {
} else {
e.logf(s, "info", "本地分支与配置不一致(%s → %s),已自动切换", cur, cfg.Branch)
}
case remoteRefExists(cfg.RepoPath, remote, cfg.Branch):
if _, err := gitRun(cfg.RepoPath, 30*time.Second, "checkout", "-B", cfg.Branch, remote+"/"+cfg.Branch); err != nil {
e.logf(s, "warn", "基于 %s/%s 创建分支失败: %v(继续在 %s 上同步)", remote, cfg.Branch, err, cur)
case remoteHasBranch:
if _, err := gitRun(cfg.RepoPath, 30*time.Second, "checkout", "-B", cfg.Branch, tip); err != nil {
e.logf(s, "warn", "基于远程 %s 创建分支失败: %v(继续在 %s 上同步)", cfg.Branch, err, cur)
} else {
_, _ = gitRun(cfg.RepoPath, 15*time.Second, "branch", "--set-upstream-to", remote+"/"+cfg.Branch, cfg.Branch)
e.logf(s, "info", "本地无分支 %s,已基于 %s/%s 创建并跟踪", cfg.Branch, remote, cfg.Branch)
e.logf(s, "info", "本地无分支 %s,已基于远程 %s(%s)创建并跟踪", cfg.Branch, cfg.Branch, tip[:8])
}
default:
if _, err := gitRun(cfg.RepoPath, 30*time.Second, "checkout", "-B", cfg.Branch); err != nil {
@@ -576,8 +577,9 @@ func (e *Engine) syncOnce(s *repoState, trigger string) {
var ahead, behind int
var err error
if remoteRefExists(cfg.RepoPath, remote, cfg.Branch) {
ahead, behind, err = aheadBehind(cfg.RepoPath, remote, cfg.Branch)
tip, remoteHasBranch := remoteTip(cfg.RepoPath, remote, cfg.Branch)
if remoteHasBranch {
ahead, behind, err = aheadBehindSha(cfg.RepoPath, tip)
if err != nil {
e.setFail(s, "获取领先/落后信息失败: "+err.Error())
return