summaryrefslogtreecommitdiffstats
path: root/modules/doctor
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-01-19 23:26:57 +0000
committerGitHub <noreply@github.com>2022-01-19 23:26:57 +0000
commit5cb0c9aa0d7eed087055b1efca79628957207d36 (patch)
treed117a514e1f17e5f6bfcda1be273f6a971112663 /modules/doctor
parent4563148a61ba892e8f2bb66342f00a950bcd5315 (diff)
downloadgitea-5cb0c9aa0d7eed087055b1efca79628957207d36.tar.gz
gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.zip
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/doctor')
-rw-r--r--modules/doctor/authorizedkeys.go3
-rw-r--r--modules/doctor/checkOldArchives.go3
-rw-r--r--modules/doctor/dbconsistency.go8
-rw-r--r--modules/doctor/dbversion.go6
-rw-r--r--modules/doctor/doctor.go4
-rw-r--r--modules/doctor/fix16961.go3
-rw-r--r--modules/doctor/mergebase.go11
-rw-r--r--modules/doctor/misc.go17
-rw-r--r--modules/doctor/paths.go3
-rw-r--r--modules/doctor/storage.go4
-rw-r--r--modules/doctor/usertype.go4
11 files changed, 38 insertions, 28 deletions
diff --git a/modules/doctor/authorizedkeys.go b/modules/doctor/authorizedkeys.go
index 1a9b60e248..3eb931e6f6 100644
--- a/modules/doctor/authorizedkeys.go
+++ b/modules/doctor/authorizedkeys.go
@@ -7,6 +7,7 @@ package doctor
import (
"bufio"
"bytes"
+ "context"
"fmt"
"os"
"path/filepath"
@@ -19,7 +20,7 @@ import (
const tplCommentPrefix = `# gitea public key`
-func checkAuthorizedKeys(logger log.Logger, autofix bool) error {
+func checkAuthorizedKeys(ctx context.Context, logger log.Logger, autofix bool) error {
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
return nil
}
diff --git a/modules/doctor/checkOldArchives.go b/modules/doctor/checkOldArchives.go
index 0db8794080..6353eaddd2 100644
--- a/modules/doctor/checkOldArchives.go
+++ b/modules/doctor/checkOldArchives.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"os"
"path/filepath"
@@ -13,7 +14,7 @@ import (
"code.gitea.io/gitea/modules/util"
)
-func checkOldArchives(logger log.Logger, autofix bool) error {
+func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numReposUpdated := 0
err := iterateRepositories(func(repo *repo_model.Repository) error {
diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go
index cd34994e1a..953a05fcd1 100644
--- a/modules/doctor/dbconsistency.go
+++ b/modules/doctor/dbconsistency.go
@@ -22,7 +22,7 @@ type consistencyCheck struct {
FixedMessage string
}
-func (c *consistencyCheck) Run(logger log.Logger, autofix bool) error {
+func (c *consistencyCheck) Run(ctx context.Context, logger log.Logger, autofix bool) error {
count, err := c.Counter()
if err != nil {
logger.Critical("Error: %v whilst counting %s", err, c.Name)
@@ -73,9 +73,9 @@ func genericOrphanCheck(name, subject, refobject, joincond string) consistencyCh
}
}
-func checkDBConsistency(logger log.Logger, autofix bool) error {
+func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) error {
// make sure DB version is uptodate
- if err := db.InitEngineWithMigration(context.Background(), migrations.EnsureUpToDate); err != nil {
+ if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
logger.Critical("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
return err
}
@@ -180,7 +180,7 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
)
for _, c := range consistencyChecks {
- if err := c.Run(logger, autofix); err != nil {
+ if err := c.Run(ctx, logger, autofix); err != nil {
return err
}
}
diff --git a/modules/doctor/dbversion.go b/modules/doctor/dbversion.go
index 0fc6762e47..8afd9f44fd 100644
--- a/modules/doctor/dbversion.go
+++ b/modules/doctor/dbversion.go
@@ -12,8 +12,8 @@ import (
"code.gitea.io/gitea/modules/log"
)
-func checkDBVersion(logger log.Logger, autofix bool) error {
- if err := db.InitEngineWithMigration(context.Background(), migrations.EnsureUpToDate); err != nil {
+func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error {
+ if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
if !autofix {
logger.Critical("Error: %v during ensure up to date", err)
return err
@@ -21,7 +21,7 @@ func checkDBVersion(logger log.Logger, autofix bool) error {
logger.Warn("Got Error: %v during ensure up to date", err)
logger.Warn("Attempting to migrate to the latest DB version to fix this.")
- err = db.InitEngineWithMigration(context.Background(), migrations.Migrate)
+ err = db.InitEngineWithMigration(ctx, migrations.Migrate)
if err != nil {
logger.Critical("Error: %v during migration", err)
}
diff --git a/modules/doctor/doctor.go b/modules/doctor/doctor.go
index 20a32f1865..c8975a788e 100644
--- a/modules/doctor/doctor.go
+++ b/modules/doctor/doctor.go
@@ -20,7 +20,7 @@ type Check struct {
Title string
Name string
IsDefault bool
- Run func(logger log.Logger, autofix bool) error
+ Run func(ctx context.Context, logger log.Logger, autofix bool) error
AbortIfFailed bool
SkipDatabaseInitialization bool
Priority int
@@ -77,7 +77,7 @@ func RunChecks(ctx context.Context, logger log.Logger, autofix bool, checks []*C
}
logger.Info("[%d] %s", log.NewColoredIDValue(i+1), check.Title)
logger.Flush()
- if err := check.Run(&wrappedLogger, autofix); err != nil {
+ if err := check.Run(ctx, &wrappedLogger, autofix); err != nil {
if check.AbortIfFailed {
logger.Critical("FAIL")
return err
diff --git a/modules/doctor/fix16961.go b/modules/doctor/fix16961.go
index 56d02ae92e..b929616b38 100644
--- a/modules/doctor/fix16961.go
+++ b/modules/doctor/fix16961.go
@@ -6,6 +6,7 @@ package doctor
import (
"bytes"
+ "context"
"fmt"
"code.gitea.io/gitea/models/db"
@@ -254,7 +255,7 @@ func fixBrokenRepoUnit16961(repoUnit *repo_model.RepoUnit, bs []byte) (fixed boo
return true, nil
}
-func fixBrokenRepoUnits16961(logger log.Logger, autofix bool) error {
+func fixBrokenRepoUnits16961(ctx context.Context, logger log.Logger, autofix bool) error {
// RepoUnit describes all units of a repository
type RepoUnit struct {
ID int64
diff --git a/modules/doctor/mergebase.go b/modules/doctor/mergebase.go
index ef78cc49d1..e2ebc41aa5 100644
--- a/modules/doctor/mergebase.go
+++ b/modules/doctor/mergebase.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"fmt"
"strings"
@@ -28,7 +29,7 @@ func iteratePRs(repo *repo_model.Repository, each func(*repo_model.Repository, *
)
}
-func checkPRMergeBase(logger log.Logger, autofix bool) error {
+func checkPRMergeBase(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numPRs := 0
numPRsUpdated := 0
@@ -43,17 +44,17 @@ func checkPRMergeBase(logger log.Logger, autofix bool) error {
if !pr.HasMerged {
var err error
- pr.MergeBase, err = git.NewCommand("merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath)
+ pr.MergeBase, err = git.NewCommandContext(ctx, "merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath)
if err != nil {
var err2 error
- pr.MergeBase, err2 = git.NewCommand("rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
+ pr.MergeBase, err2 = git.NewCommandContext(ctx, "rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
if err2 != nil {
logger.Warn("Unable to get merge base for PR ID %d, #%d onto %s in %s/%s. Error: %v & %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err, err2)
return nil
}
}
} else {
- parentsString, err := git.NewCommand("rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
+ parentsString, err := git.NewCommandContext(ctx, "rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
if err != nil {
logger.Warn("Unable to get parents for merged PR ID %d, #%d onto %s in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
return nil
@@ -66,7 +67,7 @@ func checkPRMergeBase(logger log.Logger, autofix bool) error {
args := append([]string{"merge-base", "--"}, parents[1:]...)
args = append(args, pr.GetGitRefName())
- pr.MergeBase, err = git.NewCommand(args...).RunInDir(repoPath)
+ pr.MergeBase, err = git.NewCommandContext(ctx, args...).RunInDir(repoPath)
if err != nil {
logger.Warn("Unable to get merge base for merged PR ID %d, #%d onto %s in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
return nil
diff --git a/modules/doctor/misc.go b/modules/doctor/misc.go
index a788e5f6a9..cb302c9c5b 100644
--- a/modules/doctor/misc.go
+++ b/modules/doctor/misc.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"fmt"
"os"
"os/exec"
@@ -38,7 +39,7 @@ func iterateRepositories(each func(*repo_model.Repository) error) error {
return err
}
-func checkScriptType(logger log.Logger, autofix bool) error {
+func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error {
path, err := exec.LookPath(setting.ScriptType)
if err != nil {
logger.Critical("ScriptType \"%q\" is not on the current PATH. Error: %v", setting.ScriptType, err)
@@ -48,7 +49,7 @@ func checkScriptType(logger log.Logger, autofix bool) error {
return nil
}
-func checkHooks(logger log.Logger, autofix bool) error {
+func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
if err := iterateRepositories(func(repo *repo_model.Repository) error {
results, err := repository.CheckDelegateHooks(repo.RepoPath())
if err != nil {
@@ -73,7 +74,7 @@ func checkHooks(logger log.Logger, autofix bool) error {
return nil
}
-func checkUserStarNum(logger log.Logger, autofix bool) error {
+func checkUserStarNum(ctx context.Context, logger log.Logger, autofix bool) error {
if err := models.DoctorUserStarNum(); err != nil {
logger.Critical("Unable update User Stars numbers")
return err
@@ -81,24 +82,24 @@ func checkUserStarNum(logger log.Logger, autofix bool) error {
return nil
}
-func checkEnablePushOptions(logger log.Logger, autofix bool) error {
+func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numNeedUpdate := 0
if err := iterateRepositories(func(repo *repo_model.Repository) error {
numRepos++
- r, err := git.OpenRepository(repo.RepoPath())
+ r, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath())
if err != nil {
return err
}
defer r.Close()
if autofix {
- _, err := git.NewCommand("config", "receive.advertisePushOptions", "true").RunInDir(r.Path)
+ _, err := git.NewCommandContext(ctx, "config", "receive.advertisePushOptions", "true").RunInDir(r.Path)
return err
}
- value, err := git.NewCommand("config", "receive.advertisePushOptions").RunInDir(r.Path)
+ value, err := git.NewCommandContext(ctx, "config", "receive.advertisePushOptions").RunInDir(r.Path)
if err != nil {
return err
}
@@ -124,7 +125,7 @@ func checkEnablePushOptions(logger log.Logger, autofix bool) error {
return nil
}
-func checkDaemonExport(logger log.Logger, autofix bool) error {
+func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) error {
numRepos := 0
numNeedUpdate := 0
cache, err := lru.New(512)
diff --git a/modules/doctor/paths.go b/modules/doctor/paths.go
index b4eab631ba..623df863cb 100644
--- a/modules/doctor/paths.go
+++ b/modules/doctor/paths.go
@@ -5,6 +5,7 @@
package doctor
import (
+ "context"
"fmt"
"os"
@@ -58,7 +59,7 @@ func checkConfigurationFile(logger log.Logger, autofix bool, fileOpts configurat
return nil
}
-func checkConfigurationFiles(logger log.Logger, autofix bool) error {
+func checkConfigurationFiles(ctx context.Context, logger log.Logger, autofix bool) error {
if fi, err := os.Stat(setting.CustomConf); err != nil || !fi.Mode().IsRegular() {
logger.Error("Failed to find configuration file at '%s'.", setting.CustomConf)
logger.Error("If you've never ran Gitea yet, this is normal and '%s' will be created for you on first run.", setting.CustomConf)
diff --git a/modules/doctor/storage.go b/modules/doctor/storage.go
index d2b7f5ee13..dafd989cf0 100644
--- a/modules/doctor/storage.go
+++ b/modules/doctor/storage.go
@@ -5,6 +5,8 @@
package doctor
import (
+ "context"
+
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
@@ -55,7 +57,7 @@ func checkAttachmentStorageFiles(logger log.Logger, autofix bool) error {
return nil
}
-func checkStorageFiles(logger log.Logger, autofix bool) error {
+func checkStorageFiles(ctx context.Context, logger log.Logger, autofix bool) error {
if err := storage.Init(); err != nil {
logger.Error("storage.Init failed: %v", err)
return err
diff --git a/modules/doctor/usertype.go b/modules/doctor/usertype.go
index 26c0d34cda..34e12afe72 100644
--- a/modules/doctor/usertype.go
+++ b/modules/doctor/usertype.go
@@ -5,11 +5,13 @@
package doctor
import (
+ "context"
+
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
)
-func checkUserType(logger log.Logger, autofix bool) error {
+func checkUserType(ctx context.Context, logger log.Logger, autofix bool) error {
count, err := models.CountWrongUserType()
if err != nil {
logger.Critical("Error: %v whilst counting wrong user types")