Skip to content

Commit e2740b3

Browse files
zeripathlunny
andauthored
Update Mirror IsEmpty status on synchronize (#13185)
Fix #9630 Fix #13183 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent d130cd1 commit e2740b3

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

services/mirror/mirror.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
303303
return nil, false
304304
}
305305

306-
for i := range branches {
307-
cache.Remove(m.Repo.GetCommitsCountCacheKey(branches[i].Name, true))
306+
for _, branch := range branches {
307+
cache.Remove(m.Repo.GetCommitsCountCacheKey(branch.Name, true))
308308
}
309309

310310
m.UpdatedUnix = timeutil.TimeStampNow()
@@ -422,6 +422,10 @@ func syncMirror(repoID string) {
422422
return
423423
}
424424
defer gitRepo.Close()
425+
426+
if ok := checkAndUpdateEmptyRepository(m, gitRepo, results); !ok {
427+
return
428+
}
425429
}
426430

427431
for _, result := range results {
@@ -487,6 +491,67 @@ func syncMirror(repoID string) {
487491
log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)
488492
}
489493

494+
func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, results []*mirrorSyncResult) bool {
495+
if !m.Repo.IsEmpty {
496+
return true
497+
}
498+
499+
hasDefault := false
500+
hasMaster := false
501+
defaultBranchName := m.Repo.DefaultBranch
502+
if len(defaultBranchName) == 0 {
503+
defaultBranchName = setting.Repository.DefaultBranch
504+
}
505+
firstName := ""
506+
for _, result := range results {
507+
if strings.HasPrefix(result.refName, "refs/pull/") {
508+
continue
509+
}
510+
tp, name := git.SplitRefName(result.refName)
511+
if len(tp) > 0 && tp != git.BranchPrefix {
512+
continue
513+
}
514+
if len(firstName) == 0 {
515+
firstName = name
516+
}
517+
518+
hasDefault = hasDefault || name == defaultBranchName
519+
hasMaster = hasMaster || name == "master"
520+
}
521+
522+
if len(firstName) > 0 {
523+
if hasDefault {
524+
m.Repo.DefaultBranch = defaultBranchName
525+
} else if hasMaster {
526+
m.Repo.DefaultBranch = "master"
527+
} else {
528+
m.Repo.DefaultBranch = firstName
529+
}
530+
// Update the git repository default branch
531+
if err := gitRepo.SetDefaultBranch(m.Repo.DefaultBranch); err != nil {
532+
if !git.IsErrUnsupportedVersion(err) {
533+
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
534+
desc := fmt.Sprintf("Failed to uupdate default branch of underlying git repository '%s': %v", m.Repo.RepoPath(), err)
535+
if err = models.CreateRepositoryNotice(desc); err != nil {
536+
log.Error("CreateRepositoryNotice: %v", err)
537+
}
538+
return false
539+
}
540+
}
541+
m.Repo.IsEmpty = false
542+
// Update the is empty and default_branch columns
543+
if err := models.UpdateRepositoryCols(m.Repo, "default_branch", "is_empty"); err != nil {
544+
log.Error("Failed to update default branch of repository %-v. Error: %v", m.Repo, err)
545+
desc := fmt.Sprintf("Failed to uupdate default branch of repository '%s': %v", m.Repo.RepoPath(), err)
546+
if err = models.CreateRepositoryNotice(desc); err != nil {
547+
log.Error("CreateRepositoryNotice: %v", err)
548+
}
549+
return false
550+
}
551+
}
552+
return true
553+
}
554+
490555
// InitSyncMirrors initializes a go routine to sync the mirrors
491556
func InitSyncMirrors() {
492557
go graceful.GetManager().RunWithShutdownContext(SyncMirrors)

0 commit comments

Comments
 (0)