]> source.dussan.org Git - gitea.git/commitdiff
Restore creation of git-daemon-export-ok files (#16508) (#16514)
authorzeripath <art27@cantab.net>
Thu, 22 Jul 2021 13:52:02 +0000 (14:52 +0100)
committerGitHub <noreply@github.com>
Thu, 22 Jul 2021 13:52:02 +0000 (15:52 +0200)
Backport #16508

Somewhere along the line the creation of git-daemon-export-ok
files disappeared but the updating of these files when
repo visibility changes remained. The problem is that the
current state will create files even when the org or user
is private.

This PR restores creation correctly.

Fix #15521

Signed-off-by: Andrew Thornton <art27@cantab.net>
models/repo.go
modules/doctor/misc.go

index d6abc1b5e3883c0c004a14238c3a57dea5c58367..de8a2825313ad9595899d80262cf63bef573386e 100644 (file)
@@ -1152,6 +1152,16 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
                return fmt.Errorf("recalculateAccesses: %v", err)
        }
 
+       if u.Visibility == api.VisibleTypePublic && !repo.IsPrivate {
+               // Create/Remove git-daemon-export-ok for git-daemon...
+               daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
+               if f, err := os.Create(daemonExportFile); err != nil {
+                       log.Error("Failed to create %s: %v", daemonExportFile, err)
+               } else {
+                       f.Close()
+               }
+       }
+
        if setting.Service.AutoWatchNewRepos {
                if err = watchRepo(ctx.e, doer.ID, repo.ID, true); err != nil {
                        return fmt.Errorf("watchRepo: %v", err)
@@ -1310,15 +1320,16 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
                // Create/Remove git-daemon-export-ok for git-daemon...
                daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
                isExist, err := util.IsExist(daemonExportFile)
+               isPublic := !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePublic
                if err != nil {
                        log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
                        return err
                }
-               if repo.IsPrivate && isExist {
+               if !isPublic && isExist {
                        if err = util.Remove(daemonExportFile); err != nil {
                                log.Error("Failed to remove %s: %v", daemonExportFile, err)
                        }
-               } else if !repo.IsPrivate && !isExist {
+               } else if isPublic && !isExist {
                        if f, err := os.Create(daemonExportFile); err != nil {
                                log.Error("Failed to create %s: %v", daemonExportFile, err)
                        } else {
index 0ca1e841eefd593baa5413099671c3a35150f8bc..47fee8f7fd9fb509dceaf58313d9853631b04fa5 100644 (file)
@@ -6,7 +6,9 @@ package doctor
 
 import (
        "fmt"
+       "os"
        "os/exec"
+       "path"
        "strings"
 
        "code.gitea.io/gitea/models"
@@ -14,6 +16,9 @@ import (
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/repository"
        "code.gitea.io/gitea/modules/setting"
+       "code.gitea.io/gitea/modules/structs"
+       "code.gitea.io/gitea/modules/util"
+       lru "github.com/hashicorp/golang-lru"
        "xorm.io/builder"
 )
 
@@ -75,6 +80,7 @@ func checkUserStarNum(logger log.Logger, autofix bool) error {
 func checkEnablePushOptions(logger log.Logger, autofix bool) error {
        numRepos := 0
        numNeedUpdate := 0
+
        if err := iterateRepositories(func(repo *models.Repository) error {
                numRepos++
                r, err := git.OpenRepository(repo.RepoPath())
@@ -114,6 +120,66 @@ func checkEnablePushOptions(logger log.Logger, autofix bool) error {
        return nil
 }
 
+func checkDaemonExport(logger log.Logger, autofix bool) error {
+       numRepos := 0
+       numNeedUpdate := 0
+       cache, err := lru.New(512)
+       if err != nil {
+               logger.Critical("Unable to create cache: %v", err)
+               return err
+       }
+       if err := iterateRepositories(func(repo *models.Repository) error {
+               numRepos++
+
+               if owner, has := cache.Get(repo.OwnerID); has {
+                       repo.Owner = owner.(*models.User)
+               } else {
+                       if err := repo.GetOwner(); err != nil {
+                               return err
+                       }
+                       cache.Add(repo.OwnerID, repo.Owner)
+               }
+
+               // Create/Remove git-daemon-export-ok for git-daemon...
+               daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
+               isExist, err := util.IsExist(daemonExportFile)
+               if err != nil {
+                       log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
+                       return err
+               }
+               isPublic := !repo.IsPrivate && repo.Owner.Visibility == structs.VisibleTypePublic
+
+               if isPublic != isExist {
+                       numNeedUpdate++
+                       if autofix {
+                               if !isPublic && isExist {
+                                       if err = util.Remove(daemonExportFile); err != nil {
+                                               log.Error("Failed to remove %s: %v", daemonExportFile, err)
+                                       }
+                               } else if isPublic && !isExist {
+                                       if f, err := os.Create(daemonExportFile); err != nil {
+                                               log.Error("Failed to create %s: %v", daemonExportFile, err)
+                                       } else {
+                                               f.Close()
+                                       }
+                               }
+                       }
+               }
+               return nil
+       }); err != nil {
+               logger.Critical("Unable to checkDaemonExport: %v", err)
+               return err
+       }
+
+       if autofix {
+               logger.Info("Updated git-daemon-export-ok files for %d of %d repositories.", numNeedUpdate, numRepos)
+       } else {
+               logger.Info("Checked %d repositories, %d need updates.", numRepos, numNeedUpdate)
+       }
+
+       return nil
+}
+
 func init() {
        Register(&Check{
                Title:     "Check if SCRIPT_TYPE is available",
@@ -143,4 +209,11 @@ func init() {
                Run:       checkEnablePushOptions,
                Priority:  7,
        })
+       Register(&Check{
+               Title:     "Check git-daemon-export-ok files",
+               Name:      "check-git-daemon-export-ok",
+               IsDefault: false,
+               Run:       checkDaemonExport,
+               Priority:  8,
+       })
 }