1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-25 01:54:26 +00:00

Fix mirror error when mirror repo is empty (#30432) (#30467)

Backport #30432
Fix https://github.com/go-gitea/gitea/issues/30424

ps: convert `gitrepo.OpenRepository` to `git.OpenRepository`
remove `ctx` from `checkAndUpdateEmptyRepository`

Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
yp05327 2024-04-14 21:18:06 +09:00 committed by GitHub
parent 222d16e6ea
commit 928c0d4f46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -449,19 +449,17 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
return false return false
} }
var gitRepo *git.Repository gitRepo, err := git.OpenRepository(ctx, m.Repo.RepoPath())
if len(results) == 0 { if err != nil {
log.Trace("SyncMirrors [repo: %-v]: no branches updated", m.Repo) log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err)
} else { return false
log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results)) }
gitRepo, err = git.OpenRepository(ctx, m.Repo.RepoPath()) defer gitRepo.Close()
if err != nil {
log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err)
return false
}
defer gitRepo.Close()
log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results))
if len(results) > 0 {
if ok := checkAndUpdateEmptyRepository(m, gitRepo, results); !ok { if ok := checkAndUpdateEmptyRepository(m, gitRepo, results); !ok {
log.Error("SyncMirrors [repo: %-v]: checkAndUpdateEmptyRepository: %v", m.Repo, err)
return false return false
} }
} }
@ -533,16 +531,24 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
} }
log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo) log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
// Get latest commit date and update to current repository updated time isEmpty, err := gitRepo.IsEmpty()
commitDate, err := git.GetLatestCommitTime(ctx, m.Repo.RepoPath())
if err != nil { if err != nil {
log.Error("SyncMirrors [repo: %-v]: unable to GetLatestCommitDate: %v", m.Repo, err) log.Error("SyncMirrors [repo: %-v]: unable to check empty git repo: %v", m.Repo, err)
return false return false
} }
if !isEmpty {
// Get latest commit date and update to current repository updated time
commitDate, err := git.GetLatestCommitTime(ctx, m.Repo.RepoPath())
if err != nil {
log.Error("SyncMirrors [repo: %-v]: unable to GetLatestCommitDate: %v", m.Repo, err)
return false
}
if err = repo_model.UpdateRepositoryUpdatedTime(ctx, m.RepoID, commitDate); err != nil {
log.Error("SyncMirrors [repo: %-v]: unable to update repository 'updated_unix': %v", m.Repo, err)
return false
}
if err = repo_model.UpdateRepositoryUpdatedTime(ctx, m.RepoID, commitDate); err != nil {
log.Error("SyncMirrors [repo: %-v]: unable to update repository 'updated_unix': %v", m.Repo, err)
return false
} }
log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo) log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)