Skip to content

Commit 928c0d4

Browse files
yp05327GiteaBot
andauthored
Fix mirror error when mirror repo is empty (#30432) (#30467)
Backport #30432 Fix #30424 ps: convert `gitrepo.OpenRepository` to `git.OpenRepository` remove `ctx` from `checkAndUpdateEmptyRepository` Co-authored-by: Giteabot <[email protected]>
1 parent 222d16e commit 928c0d4

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

services/mirror/mirror_pull.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -449,19 +449,17 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
449449
return false
450450
}
451451

452-
var gitRepo *git.Repository
453-
if len(results) == 0 {
454-
log.Trace("SyncMirrors [repo: %-v]: no branches updated", m.Repo)
455-
} else {
456-
log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results))
457-
gitRepo, err = git.OpenRepository(ctx, m.Repo.RepoPath())
458-
if err != nil {
459-
log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err)
460-
return false
461-
}
462-
defer gitRepo.Close()
452+
gitRepo, err := git.OpenRepository(ctx, m.Repo.RepoPath())
453+
if err != nil {
454+
log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err)
455+
return false
456+
}
457+
defer gitRepo.Close()
463458

459+
log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results))
460+
if len(results) > 0 {
464461
if ok := checkAndUpdateEmptyRepository(m, gitRepo, results); !ok {
462+
log.Error("SyncMirrors [repo: %-v]: checkAndUpdateEmptyRepository: %v", m.Repo, err)
465463
return false
466464
}
467465
}
@@ -533,16 +531,24 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
533531
}
534532
log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
535533

536-
// Get latest commit date and update to current repository updated time
537-
commitDate, err := git.GetLatestCommitTime(ctx, m.Repo.RepoPath())
534+
isEmpty, err := gitRepo.IsEmpty()
538535
if err != nil {
539-
log.Error("SyncMirrors [repo: %-v]: unable to GetLatestCommitDate: %v", m.Repo, err)
536+
log.Error("SyncMirrors [repo: %-v]: unable to check empty git repo: %v", m.Repo, err)
540537
return false
541538
}
539+
if !isEmpty {
540+
// Get latest commit date and update to current repository updated time
541+
commitDate, err := git.GetLatestCommitTime(ctx, m.Repo.RepoPath())
542+
if err != nil {
543+
log.Error("SyncMirrors [repo: %-v]: unable to GetLatestCommitDate: %v", m.Repo, err)
544+
return false
545+
}
546+
547+
if err = repo_model.UpdateRepositoryUpdatedTime(ctx, m.RepoID, commitDate); err != nil {
548+
log.Error("SyncMirrors [repo: %-v]: unable to update repository 'updated_unix': %v", m.Repo, err)
549+
return false
550+
}
542551

543-
if err = repo_model.UpdateRepositoryUpdatedTime(ctx, m.RepoID, commitDate); err != nil {
544-
log.Error("SyncMirrors [repo: %-v]: unable to update repository 'updated_unix': %v", m.Repo, err)
545-
return false
546552
}
547553

548554
log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)

0 commit comments

Comments
 (0)