summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-10-25 07:32:25 +0000
committerGitHub <noreply@github.com>2020-10-25 15:32:25 +0800
commite2740b32b5aa120b24e4e31119b515dd19dbd9c7 (patch)
tree749bdde5a3ce68d436f9f6da7f323e59191fa700 /services
parentd130cd147f24eb222a539a5b262d166f94aad5fa (diff)
downloadgitea-e2740b32b5aa120b24e4e31119b515dd19dbd9c7.tar.gz
gitea-e2740b32b5aa120b24e4e31119b515dd19dbd9c7.zip
Update Mirror IsEmpty status on synchronize (#13185)
Fix #9630 Fix #13183 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services')
-rw-r--r--services/mirror/mirror.go69
1 files changed, 67 insertions, 2 deletions
diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go
index 22aed2c3a4..fc494bfce2 100644
--- a/services/mirror/mirror.go
+++ b/services/mirror/mirror.go
@@ -303,8 +303,8 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
return nil, false
}
- for i := range branches {
- cache.Remove(m.Repo.GetCommitsCountCacheKey(branches[i].Name, true))
+ for _, branch := range branches {
+ cache.Remove(m.Repo.GetCommitsCountCacheKey(branch.Name, true))
}
m.UpdatedUnix = timeutil.TimeStampNow()
@@ -422,6 +422,10 @@ func syncMirror(repoID string) {
return
}
defer gitRepo.Close()
+
+ if ok := checkAndUpdateEmptyRepository(m, gitRepo, results); !ok {
+ return
+ }
}
for _, result := range results {
@@ -487,6 +491,67 @@ func syncMirror(repoID string) {
log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)
}
+func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, results []*mirrorSyncResult) bool {
+ if !m.Repo.IsEmpty {
+ return true
+ }
+
+ hasDefault := false
+ hasMaster := false
+ defaultBranchName := m.Repo.DefaultBranch
+ if len(defaultBranchName) == 0 {
+ defaultBranchName = setting.Repository.DefaultBranch
+ }
+ firstName := ""
+ for _, result := range results {
+ if strings.HasPrefix(result.refName, "refs/pull/") {
+ continue
+ }
+ tp, name := git.SplitRefName(result.refName)
+ if len(tp) > 0 && tp != git.BranchPrefix {
+ continue
+ }
+ if len(firstName) == 0 {
+ firstName = name
+ }
+
+ hasDefault = hasDefault || name == defaultBranchName
+ hasMaster = hasMaster || name == "master"
+ }
+
+ if len(firstName) > 0 {
+ if hasDefault {
+ m.Repo.DefaultBranch = defaultBranchName
+ } else if hasMaster {
+ m.Repo.DefaultBranch = "master"
+ } else {
+ m.Repo.DefaultBranch = firstName
+ }
+ // Update the git repository default branch
+ if err := gitRepo.SetDefaultBranch(m.Repo.DefaultBranch); err != nil {
+ if !git.IsErrUnsupportedVersion(err) {
+ log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
+ desc := fmt.Sprintf("Failed to uupdate default branch of underlying git repository '%s': %v", m.Repo.RepoPath(), err)
+ if err = models.CreateRepositoryNotice(desc); err != nil {
+ log.Error("CreateRepositoryNotice: %v", err)
+ }
+ return false
+ }
+ }
+ m.Repo.IsEmpty = false
+ // Update the is empty and default_branch columns
+ if err := models.UpdateRepositoryCols(m.Repo, "default_branch", "is_empty"); err != nil {
+ log.Error("Failed to update default branch of repository %-v. Error: %v", m.Repo, err)
+ desc := fmt.Sprintf("Failed to uupdate default branch of repository '%s': %v", m.Repo.RepoPath(), err)
+ if err = models.CreateRepositoryNotice(desc); err != nil {
+ log.Error("CreateRepositoryNotice: %v", err)
+ }
+ return false
+ }
+ }
+ return true
+}
+
// InitSyncMirrors initializes a go routine to sync the mirrors
func InitSyncMirrors() {
go graceful.GetManager().RunWithShutdownContext(SyncMirrors)