Skip to content

Commit 764878f

Browse files
GiteaBotyp05327
andauthored
Fix mirror error when mirror repo is empty (#30432) (#30455)
Backport #30432 by @yp05327 Fix #30424 Co-authored-by: yp05327 <[email protected]>
1 parent fd2184e commit 764878f

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 = gitrepo.OpenRepository(ctx, m.Repo)
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 := gitrepo.OpenRepository(ctx, m.Repo)
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(ctx, m, gitRepo, results); !ok {
462+
log.Error("SyncMirrors [repo: %-v]: checkAndUpdateEmptyRepository: %v", m.Repo, err)
465463
return false
466464
}
467465
}
@@ -534,16 +532,24 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
534532
}
535533
log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
536534

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

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

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

0 commit comments

Comments
 (0)