Skip to content

Commit

Permalink
Move some helper files out of models (#19355)
Browse files Browse the repository at this point in the history
* Move some helper files out of models

* Some improvements

Co-authored-by: delvh <[email protected]>
  • Loading branch information
lunny and delvh authored May 8, 2022
1 parent d483407 commit 4ca1d75
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 107 deletions.
36 changes: 18 additions & 18 deletions cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"strings"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"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/util"

Expand Down Expand Up @@ -162,7 +162,7 @@ func (n *nilWriter) WriteString(s string) (int, 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
}
ctx, cancel := installSignals()
Expand All @@ -180,12 +180,12 @@ Gitea or set your environment appropriately.`, "")
}

// the environment is set by serv command
isWiki := os.Getenv(models.EnvRepoIsWiki) == "true"
username := os.Getenv(models.EnvRepoUsername)
reponame := os.Getenv(models.EnvRepoName)
userID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
prID, _ := strconv.ParseInt(os.Getenv(models.EnvPRID), 10, 64)
deployKeyID, _ := strconv.ParseInt(os.Getenv(models.EnvDeployKeyID), 10, 64)
isWiki, _ := strconv.ParseBool(os.Getenv(repo_module.EnvRepoIsWiki))
username := os.Getenv(repo_module.EnvRepoUsername)
reponame := os.Getenv(repo_module.EnvRepoName)
userID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
prID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPRID), 10, 64)
deployKeyID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvDeployKeyID), 10, 64)

hookOptions := private.HookOptions{
UserID: userID,
Expand Down Expand Up @@ -314,7 +314,7 @@ func runHookPostReceive(c *cli.Context) error {
}

// 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
}

Expand Down Expand Up @@ -343,11 +343,11 @@ Gitea or set your environment appropriately.`, "")
}

// the environment is set by serv command
repoUser := os.Getenv(models.EnvRepoUsername)
isWiki := os.Getenv(models.EnvRepoIsWiki) == "true"
repoName := os.Getenv(models.EnvRepoName)
pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
pusherName := os.Getenv(models.EnvPusherName)
repoUser := os.Getenv(repo_module.EnvRepoUsername)
isWiki, _ := strconv.ParseBool(os.Getenv(repo_module.EnvRepoIsWiki))
repoName := os.Getenv(repo_module.EnvRepoName)
pusherID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
pusherName := os.Getenv(repo_module.EnvPusherName)

hookOptions := private.HookOptions{
UserName: pusherName,
Expand Down Expand Up @@ -503,10 +503,10 @@ Gitea or set your environment appropriately.`, "")
}

reader := bufio.NewReader(os.Stdin)
repoUser := os.Getenv(models.EnvRepoUsername)
repoName := os.Getenv(models.EnvRepoName)
pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
pusherName := os.Getenv(models.EnvPusherName)
repoUser := os.Getenv(repo_module.EnvRepoUsername)
repoName := os.Getenv(repo_module.EnvRepoName)
pusherID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
pusherName := os.Getenv(repo_module.EnvPusherName)

// 1. Version and features negotiation.
// S: PKT-LINE(version=1\0push-options atomic...) / PKT-LINE(version=1\n)
Expand Down
23 changes: 12 additions & 11 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/pprof"
"code.gitea.io/gitea/modules/private"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/lfs"

Expand Down Expand Up @@ -235,17 +236,17 @@ func runServ(c *cli.Context) error {
}
return fail("Internal Server Error", "%s", err.Error())
}
os.Setenv(models.EnvRepoIsWiki, strconv.FormatBool(results.IsWiki))
os.Setenv(models.EnvRepoName, results.RepoName)
os.Setenv(models.EnvRepoUsername, results.OwnerName)
os.Setenv(models.EnvPusherName, results.UserName)
os.Setenv(models.EnvPusherEmail, results.UserEmail)
os.Setenv(models.EnvPusherID, strconv.FormatInt(results.UserID, 10))
os.Setenv(models.EnvRepoID, strconv.FormatInt(results.RepoID, 10))
os.Setenv(models.EnvPRID, fmt.Sprintf("%d", 0))
os.Setenv(models.EnvDeployKeyID, fmt.Sprintf("%d", results.DeployKeyID))
os.Setenv(models.EnvKeyID, fmt.Sprintf("%d", results.KeyID))
os.Setenv(models.EnvAppURL, setting.AppURL)
os.Setenv(repo_module.EnvRepoIsWiki, strconv.FormatBool(results.IsWiki))
os.Setenv(repo_module.EnvRepoName, results.RepoName)
os.Setenv(repo_module.EnvRepoUsername, results.OwnerName)
os.Setenv(repo_module.EnvPusherName, results.UserName)
os.Setenv(repo_module.EnvPusherEmail, results.UserEmail)
os.Setenv(repo_module.EnvPusherID, strconv.FormatInt(results.UserID, 10))
os.Setenv(repo_module.EnvRepoID, strconv.FormatInt(results.RepoID, 10))
os.Setenv(repo_module.EnvPRID, fmt.Sprintf("%d", 0))
os.Setenv(repo_module.EnvDeployKeyID, fmt.Sprintf("%d", results.DeployKeyID))
os.Setenv(repo_module.EnvKeyID, fmt.Sprintf("%d", results.KeyID))
os.Setenv(repo_module.EnvAppURL, setting.AppURL)

// LFS token authentication
if verb == lfsAuthenticateVerb {
Expand Down
4 changes: 2 additions & 2 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"strconv"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
gitea_git "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"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/util"
"code.gitea.io/gitea/routers"
Expand Down Expand Up @@ -111,7 +111,7 @@ func runPR() {
}
unittest.LoadFixtures()
util.RemoveAll(setting.RepoRootPath)
util.RemoveAll(models.LocalCopyPath())
util.RemoveAll(repo_module.LocalCopyPath())
unittest.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)

log.Printf("[PR] Setting up router\n")
Expand Down
4 changes: 2 additions & 2 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
"testing"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"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/storage"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -173,7 +173,7 @@ func initIntegrationTest() {
setting.SetCustomPathAndConf("", "", "")
setting.LoadForTest()
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()
setting.InitDBConfig()
if err := storage.Init(); err != nil {
Expand Down
17 changes: 0 additions & 17 deletions models/helper.go

This file was deleted.

15 changes: 4 additions & 11 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ var ItemsPerPage = 40
// NewRepoContext creates a new repository context
func NewRepoContext() {
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
Expand Down Expand Up @@ -527,7 +524,8 @@ func DecrementRepoForkNum(ctx context.Context, repoID int64) error {
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)

if utf8.RuneCountInString(repo.Description) > 255 {
Expand Down Expand Up @@ -579,7 +577,7 @@ func updateRepository(ctx context.Context, repo *repo_model.Repository, visibili
}
for i := range forkRepos {
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)
}
}
Expand All @@ -588,11 +586,6 @@ func updateRepository(ctx context.Context, repo *repo_model.Repository, visibili
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
func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err error) {
ctx, committer, err := db.TxContext()
Expand All @@ -601,7 +594,7 @@ func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err
}
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)
}

Expand Down
9 changes: 9 additions & 0 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func (repos RepositoryList) Swap(i, j int) {
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
func RepositoryListOfMap(repoMap map[int64]*repo_model.Repository) RepositoryList {
return RepositoryList(valuesRepository(repoMap))
Expand Down
2 changes: 1 addition & 1 deletion models/helper_environment.go → modules/repository/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models
package repository

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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)
return fmt.Errorf("git push: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion models/helper_directory.go → modules/repository/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models
package repository

import (
"fmt"
Expand Down
7 changes: 5 additions & 2 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
func InitGitServices() {
setting.NewServices()
mustInit(storage.Init)
mustInit(repo_service.NewContext)
mustInit(repo_service.Init)
}

func syncAppPathForGit(ctx context.Context) error {
Expand Down Expand Up @@ -116,7 +116,9 @@ func GlobalInitInstalled(ctx context.Context) {
// Setup i18n
translation.InitLocales()

InitGitServices()
setting.NewServices()
mustInit(storage.Init)

mailer.NewContext()
mustInit(cache.NewContext)
notification.NewContext()
Expand All @@ -138,6 +140,7 @@ func GlobalInitInstalled(ctx context.Context) {
mustInit(oauth2.Init)

models.NewRepoContext()
mustInit(repo_service.Init)

// Booting long running goroutines.
cron.NewContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func RestoreBranchPost(ctx *context.Context) {
if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{
Remote: ctx.Repo.Repository.RepoPath(),
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 {
if strings.Contains(err.Error(), "already exists") {
log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
Expand Down
19 changes: 10 additions & 9 deletions routers/web/repo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"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/structs"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -204,21 +205,21 @@ func httpBase(ctx *context.Context) (h *serviceHandler) {
}

environ = []string{
models.EnvRepoUsername + "=" + username,
models.EnvRepoName + "=" + reponame,
models.EnvPusherName + "=" + ctx.Doer.Name,
models.EnvPusherID + fmt.Sprintf("=%d", ctx.Doer.ID),
models.EnvAppURL + "=" + setting.AppURL,
repo_module.EnvRepoUsername + "=" + username,
repo_module.EnvRepoName + "=" + reponame,
repo_module.EnvPusherName + "=" + ctx.Doer.Name,
repo_module.EnvPusherID + fmt.Sprintf("=%d", ctx.Doer.ID),
repo_module.EnvAppURL + "=" + setting.AppURL,
}

if !ctx.Doer.KeepEmailPrivate {
environ = append(environ, models.EnvPusherEmail+"="+ctx.Doer.Email)
environ = append(environ, repo_module.EnvPusherEmail+"="+ctx.Doer.Email)
}

if isWiki {
environ = append(environ, models.EnvRepoIsWiki+"=true")
environ = append(environ, repo_module.EnvRepoIsWiki+"=true")
} else {
environ = append(environ, models.EnvRepoIsWiki+"=false")
environ = append(environ, repo_module.EnvRepoIsWiki+"=false")
}
}

Expand Down Expand Up @@ -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
r := ctx.Req
Expand Down
5 changes: 3 additions & 2 deletions routers/web/repo/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"code.gitea.io/gitea/modules/git/pipeline"
"code.gitea.io/gitea/modules/lfs"
"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/storage"
"code.gitea.io/gitea/modules/typesniffer"
Expand Down Expand Up @@ -103,14 +104,14 @@ func LFSLocks(ctx *context.Context) {
}

// Clone base repo.
tmpBasePath, err := models.CreateTemporaryPath("locks")
tmpBasePath, err := repo_module.CreateTemporaryPath("locks")
if err != nil {
log.Error("Failed to create temporary path: %v", err)
ctx.ServerError("LFSLocks", err)
return
}
defer func() {
if err := models.RemoveTemporaryPath(tmpBasePath); err != nil {
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("LFSLocks: RemoveTemporaryPath: %v", err)
}
}()
Expand Down
Loading

0 comments on commit 4ca1d75

Please sign in to comment.