mirror of
1
Fork 0

Move some helper files out of models (#19355)

* Move some helper files out of models

* Some improvements

Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
Lunny Xiao 2022-05-09 00:46:32 +08:00 committed by GitHub
parent d4834071da
commit 4ca1d7547a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 108 additions and 107 deletions

View File

@ -15,9 +15,9 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -162,7 +162,7 @@ func (n *nilWriter) WriteString(s string) (int, error) {
} }
func runHookPreReceive(c *cli.Context) error { func runHookPreReceive(c *cli.Context) error {
if os.Getenv(models.EnvIsInternal) == "true" { if isInternal, _ := strconv.ParseBool(os.Getenv(repo_module.EnvIsInternal)); isInternal {
return nil return nil
} }
ctx, cancel := installSignals() ctx, cancel := installSignals()
@ -180,12 +180,12 @@ Gitea or set your environment appropriately.`, "")
} }
// the environment is set by serv command // the environment is set by serv command
isWiki := os.Getenv(models.EnvRepoIsWiki) == "true" isWiki, _ := strconv.ParseBool(os.Getenv(repo_module.EnvRepoIsWiki))
username := os.Getenv(models.EnvRepoUsername) username := os.Getenv(repo_module.EnvRepoUsername)
reponame := os.Getenv(models.EnvRepoName) reponame := os.Getenv(repo_module.EnvRepoName)
userID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64) userID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
prID, _ := strconv.ParseInt(os.Getenv(models.EnvPRID), 10, 64) prID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPRID), 10, 64)
deployKeyID, _ := strconv.ParseInt(os.Getenv(models.EnvDeployKeyID), 10, 64) deployKeyID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvDeployKeyID), 10, 64)
hookOptions := private.HookOptions{ hookOptions := private.HookOptions{
UserID: userID, UserID: userID,
@ -314,7 +314,7 @@ func runHookPostReceive(c *cli.Context) error {
} }
// Now if we're an internal don't do anything else // Now if we're an internal don't do anything else
if os.Getenv(models.EnvIsInternal) == "true" { if isInternal, _ := strconv.ParseBool(os.Getenv(repo_module.EnvIsInternal)); isInternal {
return nil return nil
} }
@ -343,11 +343,11 @@ Gitea or set your environment appropriately.`, "")
} }
// the environment is set by serv command // the environment is set by serv command
repoUser := os.Getenv(models.EnvRepoUsername) repoUser := os.Getenv(repo_module.EnvRepoUsername)
isWiki := os.Getenv(models.EnvRepoIsWiki) == "true" isWiki, _ := strconv.ParseBool(os.Getenv(repo_module.EnvRepoIsWiki))
repoName := os.Getenv(models.EnvRepoName) repoName := os.Getenv(repo_module.EnvRepoName)
pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64) pusherID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
pusherName := os.Getenv(models.EnvPusherName) pusherName := os.Getenv(repo_module.EnvPusherName)
hookOptions := private.HookOptions{ hookOptions := private.HookOptions{
UserName: pusherName, UserName: pusherName,
@ -503,10 +503,10 @@ Gitea or set your environment appropriately.`, "")
} }
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
repoUser := os.Getenv(models.EnvRepoUsername) repoUser := os.Getenv(repo_module.EnvRepoUsername)
repoName := os.Getenv(models.EnvRepoName) repoName := os.Getenv(repo_module.EnvRepoName)
pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64) pusherID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
pusherName := os.Getenv(models.EnvPusherName) pusherName := os.Getenv(repo_module.EnvPusherName)
// 1. Version and features negotiation. // 1. Version and features negotiation.
// S: PKT-LINE(version=1\0push-options atomic...) / PKT-LINE(version=1\n) // S: PKT-LINE(version=1\0push-options atomic...) / PKT-LINE(version=1\n)

View File

@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/pprof" "code.gitea.io/gitea/modules/pprof"
"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/lfs" "code.gitea.io/gitea/services/lfs"
@ -235,17 +236,17 @@ func runServ(c *cli.Context) error {
} }
return fail("Internal Server Error", "%s", err.Error()) return fail("Internal Server Error", "%s", err.Error())
} }
os.Setenv(models.EnvRepoIsWiki, strconv.FormatBool(results.IsWiki)) os.Setenv(repo_module.EnvRepoIsWiki, strconv.FormatBool(results.IsWiki))
os.Setenv(models.EnvRepoName, results.RepoName) os.Setenv(repo_module.EnvRepoName, results.RepoName)
os.Setenv(models.EnvRepoUsername, results.OwnerName) os.Setenv(repo_module.EnvRepoUsername, results.OwnerName)
os.Setenv(models.EnvPusherName, results.UserName) os.Setenv(repo_module.EnvPusherName, results.UserName)
os.Setenv(models.EnvPusherEmail, results.UserEmail) os.Setenv(repo_module.EnvPusherEmail, results.UserEmail)
os.Setenv(models.EnvPusherID, strconv.FormatInt(results.UserID, 10)) os.Setenv(repo_module.EnvPusherID, strconv.FormatInt(results.UserID, 10))
os.Setenv(models.EnvRepoID, strconv.FormatInt(results.RepoID, 10)) os.Setenv(repo_module.EnvRepoID, strconv.FormatInt(results.RepoID, 10))
os.Setenv(models.EnvPRID, fmt.Sprintf("%d", 0)) os.Setenv(repo_module.EnvPRID, fmt.Sprintf("%d", 0))
os.Setenv(models.EnvDeployKeyID, fmt.Sprintf("%d", results.DeployKeyID)) os.Setenv(repo_module.EnvDeployKeyID, fmt.Sprintf("%d", results.DeployKeyID))
os.Setenv(models.EnvKeyID, fmt.Sprintf("%d", results.KeyID)) os.Setenv(repo_module.EnvKeyID, fmt.Sprintf("%d", results.KeyID))
os.Setenv(models.EnvAppURL, setting.AppURL) os.Setenv(repo_module.EnvAppURL, setting.AppURL)
// LFS token authentication // LFS token authentication
if verb == lfsAuthenticateVerb { if verb == lfsAuthenticateVerb {

View File

@ -24,12 +24,12 @@ import (
"strconv" "strconv"
"time" "time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
gitea_git "code.gitea.io/gitea/modules/git" gitea_git "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/external" "code.gitea.io/gitea/modules/markup/external"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers" "code.gitea.io/gitea/routers"
@ -111,7 +111,7 @@ func runPR() {
} }
unittest.LoadFixtures() unittest.LoadFixtures()
util.RemoveAll(setting.RepoRootPath) util.RemoveAll(setting.RepoRootPath)
util.RemoveAll(models.LocalCopyPath()) util.RemoveAll(repo_module.LocalCopyPath())
unittest.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath) unittest.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
log.Printf("[PR] Setting up router\n") log.Printf("[PR] Setting up router\n")

View File

@ -24,7 +24,6 @@ import (
"testing" "testing"
"time" "time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -32,6 +31,7 @@ import (
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/queue"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -173,7 +173,7 @@ func initIntegrationTest() {
setting.SetCustomPathAndConf("", "", "") setting.SetCustomPathAndConf("", "", "")
setting.LoadForTest() setting.LoadForTest()
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master" setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
_ = util.RemoveAll(models.LocalCopyPath()) _ = util.RemoveAll(repo_module.LocalCopyPath())
git.CheckLFSVersion() git.CheckLFSVersion()
setting.InitDBConfig() setting.InitDBConfig()
if err := storage.Init(); err != nil { if err := storage.Init(); err != nil {

View File

@ -1,17 +0,0 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
repo_model "code.gitea.io/gitea/models/repo"
)
func valuesRepository(m map[int64]*repo_model.Repository) []*repo_model.Repository {
values := make([]*repo_model.Repository, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}

View File

@ -43,9 +43,6 @@ var ItemsPerPage = 40
// NewRepoContext creates a new repository context // NewRepoContext creates a new repository context
func NewRepoContext() { func NewRepoContext() {
unit.LoadUnitConfig() unit.LoadUnitConfig()
admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repositories", LocalCopyPath())
} }
// CheckRepoUnitUser check whether user could visit the unit of this repository // CheckRepoUnitUser check whether user could visit the unit of this repository
@ -527,7 +524,8 @@ func DecrementRepoForkNum(ctx context.Context, repoID int64) error {
return err return err
} }
func updateRepository(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) (err error) { // UpdateRepositoryCtx updates a repository with db context
func UpdateRepositoryCtx(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) (err error) {
repo.LowerName = strings.ToLower(repo.Name) repo.LowerName = strings.ToLower(repo.Name)
if utf8.RuneCountInString(repo.Description) > 255 { if utf8.RuneCountInString(repo.Description) > 255 {
@ -579,7 +577,7 @@ func updateRepository(ctx context.Context, repo *repo_model.Repository, visibili
} }
for i := range forkRepos { for i := range forkRepos {
forkRepos[i].IsPrivate = repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate forkRepos[i].IsPrivate = repo.IsPrivate || repo.Owner.Visibility == api.VisibleTypePrivate
if err = updateRepository(ctx, forkRepos[i], true); err != nil { if err = UpdateRepositoryCtx(ctx, forkRepos[i], true); err != nil {
return fmt.Errorf("updateRepository[%d]: %v", forkRepos[i].ID, err) return fmt.Errorf("updateRepository[%d]: %v", forkRepos[i].ID, err)
} }
} }
@ -588,11 +586,6 @@ func updateRepository(ctx context.Context, repo *repo_model.Repository, visibili
return nil return nil
} }
// UpdateRepositoryCtx updates a repository with db context
func UpdateRepositoryCtx(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) error {
return updateRepository(ctx, repo, visibilityChanged)
}
// UpdateRepository updates a repository // UpdateRepository updates a repository
func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err error) { func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err error) {
ctx, committer, err := db.TxContext() ctx, committer, err := db.TxContext()
@ -601,7 +594,7 @@ func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err
} }
defer committer.Close() defer committer.Close()
if err = updateRepository(ctx, repo, visibilityChanged); err != nil { if err = UpdateRepositoryCtx(ctx, repo, visibilityChanged); err != nil {
return fmt.Errorf("updateRepository: %v", err) return fmt.Errorf("updateRepository: %v", err)
} }

View File

@ -42,6 +42,15 @@ func (repos RepositoryList) Swap(i, j int) {
repos[i], repos[j] = repos[j], repos[i] repos[i], repos[j] = repos[j], repos[i]
} }
// FIXME: Remove in favor of maps.values when MIN_GO_VERSION >= 1.18
func valuesRepository(m map[int64]*repo_model.Repository) []*repo_model.Repository {
values := make([]*repo_model.Repository, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}
// RepositoryListOfMap make list from values of map // RepositoryListOfMap make list from values of map
func RepositoryListOfMap(repoMap map[int64]*repo_model.Repository) RepositoryList { func RepositoryListOfMap(repoMap map[int64]*repo_model.Repository) RepositoryList {
return RepositoryList(valuesRepository(repoMap)) return RepositoryList(valuesRepository(repoMap))

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package repository
import ( import (
"fmt" "fmt"

View File

@ -360,7 +360,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
if stdout, _, err := git.NewCommand(ctx, "push", "origin", "HEAD:"+defaultBranch). if stdout, _, err := git.NewCommand(ctx, "push", "origin", "HEAD:"+defaultBranch).
SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)). SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)).
RunStdString(&git.RunOpts{Dir: tmpPath, Env: models.InternalPushingEnvironment(u, repo)}); err != nil { RunStdString(&git.RunOpts{Dir: tmpPath, Env: InternalPushingEnvironment(u, repo)}); err != nil {
log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err) log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err)
return fmt.Errorf("git push: %v", err) return fmt.Errorf("git push: %v", err)
} }

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package repository
import ( import (
"fmt" "fmt"

View File

@ -73,7 +73,7 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
func InitGitServices() { func InitGitServices() {
setting.NewServices() setting.NewServices()
mustInit(storage.Init) mustInit(storage.Init)
mustInit(repo_service.NewContext) mustInit(repo_service.Init)
} }
func syncAppPathForGit(ctx context.Context) error { func syncAppPathForGit(ctx context.Context) error {
@ -116,7 +116,9 @@ func GlobalInitInstalled(ctx context.Context) {
// Setup i18n // Setup i18n
translation.InitLocales() translation.InitLocales()
InitGitServices() setting.NewServices()
mustInit(storage.Init)
mailer.NewContext() mailer.NewContext()
mustInit(cache.NewContext) mustInit(cache.NewContext)
notification.NewContext() notification.NewContext()
@ -138,6 +140,7 @@ func GlobalInitInstalled(ctx context.Context) {
mustInit(oauth2.Init) mustInit(oauth2.Init)
models.NewRepoContext() models.NewRepoContext()
mustInit(repo_service.Init)
// Booting long running goroutines. // Booting long running goroutines.
cron.NewContext(ctx) cron.NewContext(ctx)

View File

@ -129,7 +129,7 @@ func RestoreBranchPost(ctx *context.Context) {
if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{ if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{
Remote: ctx.Repo.Repository.RepoPath(), Remote: ctx.Repo.Repository.RepoPath(),
Branch: fmt.Sprintf("%s:%s%s", deletedBranch.Commit, git.BranchPrefix, deletedBranch.Name), Branch: fmt.Sprintf("%s:%s%s", deletedBranch.Commit, git.BranchPrefix, deletedBranch.Name),
Env: models.PushingEnvironment(ctx.Doer, ctx.Repo.Repository), Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository),
}); err != nil { }); err != nil {
if strings.Contains(err.Error(), "already exists") { if strings.Contains(err.Error(), "already exists") {
log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name) log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)

View File

@ -27,6 +27,7 @@ import (
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -204,21 +205,21 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
} }
environ = []string{ environ = []string{
models.EnvRepoUsername + "=" + username, repo_module.EnvRepoUsername + "=" + username,
models.EnvRepoName + "=" + reponame, repo_module.EnvRepoName + "=" + reponame,
models.EnvPusherName + "=" + ctx.Doer.Name, repo_module.EnvPusherName + "=" + ctx.Doer.Name,
models.EnvPusherID + fmt.Sprintf("=%d", ctx.Doer.ID), repo_module.EnvPusherID + fmt.Sprintf("=%d", ctx.Doer.ID),
models.EnvAppURL + "=" + setting.AppURL, repo_module.EnvAppURL + "=" + setting.AppURL,
} }
if !ctx.Doer.KeepEmailPrivate { if !ctx.Doer.KeepEmailPrivate {
environ = append(environ, models.EnvPusherEmail+"="+ctx.Doer.Email) environ = append(environ, repo_module.EnvPusherEmail+"="+ctx.Doer.Email)
} }
if isWiki { if isWiki {
environ = append(environ, models.EnvRepoIsWiki+"=true") environ = append(environ, repo_module.EnvRepoIsWiki+"=true")
} else { } else {
environ = append(environ, models.EnvRepoIsWiki+"=false") environ = append(environ, repo_module.EnvRepoIsWiki+"=false")
} }
} }
@ -269,7 +270,7 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
} }
} }
environ = append(environ, models.EnvRepoID+fmt.Sprintf("=%d", repo.ID)) environ = append(environ, repo_module.EnvRepoID+fmt.Sprintf("=%d", repo.ID))
w := ctx.Resp w := ctx.Resp
r := ctx.Req r := ctx.Req

View File

@ -23,6 +23,7 @@ import (
"code.gitea.io/gitea/modules/git/pipeline" "code.gitea.io/gitea/modules/git/pipeline"
"code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/typesniffer" "code.gitea.io/gitea/modules/typesniffer"
@ -103,14 +104,14 @@ func LFSLocks(ctx *context.Context) {
} }
// Clone base repo. // Clone base repo.
tmpBasePath, err := models.CreateTemporaryPath("locks") tmpBasePath, err := repo_module.CreateTemporaryPath("locks")
if err != nil { if err != nil {
log.Error("Failed to create temporary path: %v", err) log.Error("Failed to create temporary path: %v", err)
ctx.ServerError("LFSLocks", err) ctx.ServerError("LFSLocks", err)
return return
} }
defer func() { defer func() {
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("LFSLocks: RemoveTemporaryPath: %v", err) log.Error("LFSLocks: RemoveTemporaryPath: %v", err)
} }
}() }()

View File

@ -28,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/references"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
asymkey_service "code.gitea.io/gitea/services/asymkey" asymkey_service "code.gitea.io/gitea/services/asymkey"
@ -234,7 +235,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
return "", err return "", err
} }
defer func() { defer func() {
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err) log.Error("Merge: RemoveTemporaryPath: %s", err)
} }
}() }()
@ -590,7 +591,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
headUser = pr.HeadRepo.Owner headUser = pr.HeadRepo.Owner
} }
env = models.FullPushingEnvironment( env = repo_module.FullPushingEnvironment(
headUser, headUser,
doer, doer,
pr.BaseRepo, pr.BaseRepo,

View File

@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/gobwas/glob" "github.com/gobwas/glob"
@ -64,7 +65,7 @@ func TestPatch(pr *models.PullRequest) error {
return err return err
} }
defer func() { defer func() {
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err) log.Error("Merge: RemoveTemporaryPath: %s", err)
} }
}() }()

View File

@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/sync" "code.gitea.io/gitea/modules/sync"
issue_service "code.gitea.io/gitea/services/issue" issue_service "code.gitea.io/gitea/services/issue"
@ -452,7 +453,7 @@ func pushToBaseRepoHelper(ctx context.Context, pr *models.PullRequest, prefixHea
Branch: prefixHeadBranch + pr.HeadBranch + ":" + gitRefName, Branch: prefixHeadBranch + pr.HeadBranch + ":" + gitRefName,
Force: true, Force: true,
// Use InternalPushingEnvironment here because we know that pre-receive and post-receive do not run on a refs/pulls/... // Use InternalPushingEnvironment here because we know that pre-receive and post-receive do not run on a refs/pulls/...
Env: models.InternalPushingEnvironment(pr.Issue.Poster, pr.BaseRepo), Env: repo_module.InternalPushingEnvironment(pr.Issue.Poster, pr.BaseRepo),
}); err != nil { }); err != nil {
if git.IsErrPushOutOfDate(err) { if git.IsErrPushOutOfDate(err) {
// This should not happen as we're using force! // This should not happen as we're using force!

View File

@ -16,6 +16,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
) )
// createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch // createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch
@ -46,7 +47,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
} }
// Clone base repo. // Clone base repo.
tmpBasePath, err := models.CreateTemporaryPath("pull") tmpBasePath, err := repo_module.CreateTemporaryPath("pull")
if err != nil { if err != nil {
log.Error("CreateTemporaryPath: %v", err) log.Error("CreateTemporaryPath: %v", err)
return "", err return "", err
@ -57,7 +58,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
if err := git.InitRepository(ctx, tmpBasePath, false); err != nil { if err := git.InitRepository(ctx, tmpBasePath, false); err != nil {
log.Error("git init tmpBasePath: %v", err) log.Error("git init tmpBasePath: %v", err)
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", err return "", err
@ -85,7 +86,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
if err := addCacheRepo(tmpBasePath, baseRepoPath); err != nil { if err := addCacheRepo(tmpBasePath, baseRepoPath); err != nil {
log.Error("Unable to add base repository to temporary repo [%s -> %s]: %v", pr.BaseRepo.FullName(), tmpBasePath, err) log.Error("Unable to add base repository to temporary repo [%s -> %s]: %v", pr.BaseRepo.FullName(), tmpBasePath, err)
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", fmt.Errorf("Unable to add base repository to temporary repo [%s -> tmpBasePath]: %v", pr.BaseRepo.FullName(), err) return "", fmt.Errorf("Unable to add base repository to temporary repo [%s -> tmpBasePath]: %v", pr.BaseRepo.FullName(), err)
@ -99,7 +100,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
Stderr: &errbuf, Stderr: &errbuf,
}); err != nil { }); err != nil {
log.Error("Unable to add base repository as origin [%s -> %s]: %v\n%s\n%s", pr.BaseRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String()) log.Error("Unable to add base repository as origin [%s -> %s]: %v\n%s\n%s", pr.BaseRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String())
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", fmt.Errorf("Unable to add base repository as origin [%s -> tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("Unable to add base repository as origin [%s -> tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), err, outbuf.String(), errbuf.String())
@ -114,7 +115,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
Stderr: &errbuf, Stderr: &errbuf,
}); err != nil { }); err != nil {
log.Error("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, tmpBasePath, err, outbuf.String(), errbuf.String()) log.Error("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, tmpBasePath, err, outbuf.String(), errbuf.String())
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", fmt.Errorf("Unable to fetch origin base branch [%s:%s -> base, original_base in tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("Unable to fetch origin base branch [%s:%s -> base, original_base in tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
@ -129,7 +130,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
Stderr: &errbuf, Stderr: &errbuf,
}); err != nil { }); err != nil {
log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String()) log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String())
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", fmt.Errorf("Unable to set HEAD as base branch [tmpBasePath]: %v\n%s\n%s", err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("Unable to set HEAD as base branch [tmpBasePath]: %v\n%s\n%s", err, outbuf.String(), errbuf.String())
@ -139,7 +140,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil { if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
log.Error("Unable to add head repository to temporary repo [%s -> %s]: %v", pr.HeadRepo.FullName(), tmpBasePath, err) log.Error("Unable to add head repository to temporary repo [%s -> %s]: %v", pr.HeadRepo.FullName(), tmpBasePath, err)
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err) return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err)
@ -152,7 +153,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
Stderr: &errbuf, Stderr: &errbuf,
}); err != nil { }); err != nil {
log.Error("Unable to add head repository as head_repo [%s -> %s]: %v\n%s\n%s", pr.HeadRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String()) log.Error("Unable to add head repository as head_repo [%s -> %s]: %v\n%s\n%s", pr.HeadRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String())
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
return "", fmt.Errorf("Unable to add head repository as head_repo [%s -> tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), err, outbuf.String(), errbuf.String()) return "", fmt.Errorf("Unable to add head repository as head_repo [%s -> tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), err, outbuf.String(), errbuf.String())
@ -176,7 +177,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
Stdout: &outbuf, Stdout: &outbuf,
Stderr: &errbuf, Stderr: &errbuf,
}); err != nil { }); err != nil {
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
} }
if !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) { if !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) {

View File

@ -14,6 +14,7 @@ import (
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
) )
// Update updates pull request with base branch. // Update updates pull request with base branch.
@ -154,7 +155,7 @@ func GetDiverging(ctx context.Context, pr *models.PullRequest) (*git.DivergeObje
return nil, err return nil, err
} }
defer func() { defer func() {
if err := models.RemoveTemporaryPath(tmpRepo); err != nil { if err := repo_module.RemoveTemporaryPath(tmpRepo); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err) log.Error("Merge: RemoveTemporaryPath: %s", err)
} }
}() }()

View File

@ -36,7 +36,7 @@ func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_mode
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
Remote: repo.RepoPath(), Remote: repo.RepoPath(),
Branch: fmt.Sprintf("%s%s:%s%s", git.BranchPrefix, oldBranchName, git.BranchPrefix, branchName), Branch: fmt.Sprintf("%s%s:%s%s", git.BranchPrefix, oldBranchName, git.BranchPrefix, branchName),
Env: models.PushingEnvironment(doer, repo), Env: repo_module.PushingEnvironment(doer, repo),
}); err != nil { }); err != nil {
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
return err return err
@ -93,7 +93,7 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
Remote: repo.RepoPath(), Remote: repo.RepoPath(),
Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName), Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
Env: models.PushingEnvironment(doer, repo), Env: repo_module.PushingEnvironment(doer, repo),
}); err != nil { }); err != nil {
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
return err return err

View File

@ -19,6 +19,7 @@ import (
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
asymkey_service "code.gitea.io/gitea/services/asymkey" asymkey_service "code.gitea.io/gitea/services/asymkey"
"code.gitea.io/gitea/services/gitdiff" "code.gitea.io/gitea/services/gitdiff"
@ -34,7 +35,7 @@ type TemporaryUploadRepository struct {
// NewTemporaryUploadRepository creates a new temporary upload repository // NewTemporaryUploadRepository creates a new temporary upload repository
func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) { func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) {
basePath, err := models.CreateTemporaryPath("upload") basePath, err := repo_module.CreateTemporaryPath("upload")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -45,7 +46,7 @@ func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Reposito
// Close the repository cleaning up all files // Close the repository cleaning up all files
func (t *TemporaryUploadRepository) Close() { func (t *TemporaryUploadRepository) Close() {
defer t.gitRepo.Close() defer t.gitRepo.Close()
if err := models.RemoveTemporaryPath(t.basePath); err != nil { if err := repo_module.RemoveTemporaryPath(t.basePath); err != nil {
log.Error("Failed to remove temporary path %s: %v", t.basePath, err) log.Error("Failed to remove temporary path %s: %v", t.basePath, err)
} }
} }
@ -315,7 +316,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(parent string, author, co
// Push the provided commitHash to the repository branch by the provided user // Push the provided commitHash to the repository branch by the provided user
func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash, branch string) error { func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash, branch string) error {
// Because calls hooks we need to pass in the environment // Because calls hooks we need to pass in the environment
env := models.PushingEnvironment(doer, t.repo) env := repo_module.PushingEnvironment(doer, t.repo)
if err := git.Push(t.ctx, t.basePath, git.PushOptions{ if err := git.Push(t.ctx, t.basePath, git.PushOptions{
Remote: t.repo.RepoPath(), Remote: t.repo.RepoPath(),
Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch), Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch),

View File

@ -9,6 +9,8 @@ import (
"fmt" "fmt"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
admin_model "code.gitea.io/gitea/models/admin"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
packages_model "code.gitea.io/gitea/models/packages" packages_model "code.gitea.io/gitea/models/packages"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
@ -16,7 +18,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
cfg "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
pull_service "code.gitea.io/gitea/services/pull" pull_service "code.gitea.io/gitea/services/pull"
) )
@ -67,7 +69,7 @@ func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_mo
repo, err := CreateRepository(authUser, owner, models.CreateRepoOptions{ repo, err := CreateRepository(authUser, owner, models.CreateRepoOptions{
Name: repoName, Name: repoName,
IsPrivate: cfg.Repository.DefaultPushCreatePrivate, IsPrivate: setting.Repository.DefaultPushCreatePrivate,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@ -76,8 +78,10 @@ func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_mo
return repo, nil return repo, nil
} }
// NewContext start repository service // Init start repository service
func NewContext() error { func Init() error {
repo_module.LoadRepoConfig() repo_module.LoadRepoConfig()
admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repositories", repo_module.LocalCopyPath())
return initPushQueue() return initPushQueue()
} }

View File

@ -133,12 +133,12 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), "master") hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), "master")
basePath, err := models.CreateTemporaryPath("update-wiki") basePath, err := repo_module.CreateTemporaryPath("update-wiki")
if err != nil { if err != nil {
return err return err
} }
defer func() { defer func() {
if err := models.RemoveTemporaryPath(basePath); err != nil { if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err) log.Error("Merge: RemoveTemporaryPath: %s", err)
} }
}() }()
@ -249,7 +249,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{ if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: "origin", Remote: "origin",
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"),
Env: models.FullPushingEnvironment( Env: repo_module.FullPushingEnvironment(
doer, doer,
doer, doer,
repo, repo,
@ -287,12 +287,12 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("InitWiki: %v", err) return fmt.Errorf("InitWiki: %v", err)
} }
basePath, err := models.CreateTemporaryPath("update-wiki") basePath, err := repo_module.CreateTemporaryPath("update-wiki")
if err != nil { if err != nil {
return err return err
} }
defer func() { defer func() {
if err := models.RemoveTemporaryPath(basePath); err != nil { if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err) log.Error("Merge: RemoveTemporaryPath: %s", err)
} }
}() }()
@ -363,7 +363,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{ if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{
Remote: "origin", Remote: "origin",
Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"),
Env: models.PushingEnvironment(doer, repo), Env: repo_module.PushingEnvironment(doer, repo),
}); err != nil { }); err != nil {
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
return err return err