diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /modules/context/repo.go | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'modules/context/repo.go')
-rw-r--r-- | modules/context/repo.go | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index b2844c04c4..694c483bd4 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -14,6 +14,8 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/cache" @@ -42,7 +44,7 @@ var IssueTemplateDirCandidates = []string{ // PullRequest contains information to make a pull request type PullRequest struct { - BaseRepo *models.Repository + BaseRepo *repo_model.Repository Allowed bool SameRepo bool HeadInfoSubURL string // [<user>:]<branch> url segment @@ -55,7 +57,7 @@ type Repository struct { IsViewBranch bool IsViewTag bool IsViewCommit bool - Repository *models.Repository + Repository *repo_model.Repository Owner *user_model.User Commit *git.Commit Tag *git.Tag @@ -66,9 +68,9 @@ type Repository struct { TreePath string CommitID string RepoLink string - CloneLink models.CloneLink + CloneLink repo_model.CloneLink CommitsCount int64 - Mirror *models.Mirror + Mirror *repo_model.Mirror PullRequest *PullRequest } @@ -118,7 +120,7 @@ func (r *Repository) CanCommitToBranch(doer *user_model.User) (CanCommitToBranch requireSigned = protectedBranch.RequireSignedCommits } - sign, keyID, _, err := r.Repository.SignCRUDAction(doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName) + sign, keyID, _, err := models.SignCRUDAction(r.Repository, doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName) canCommit := r.CanEnableEditor() && userCanPush if requireSigned { @@ -242,38 +244,39 @@ func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) { } // RetrieveBaseRepo retrieves base repository -func RetrieveBaseRepo(ctx *Context, repo *models.Repository) { +func RetrieveBaseRepo(ctx *Context, repo *repo_model.Repository) { // Non-fork repository will not return error in this method. if err := repo.GetBaseRepo(); err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { repo.IsFork = false repo.ForkID = 0 return } ctx.ServerError("GetBaseRepo", err) return - } else if err = repo.BaseRepo.GetOwner(); err != nil { + } else if err = repo.BaseRepo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("BaseRepo.GetOwner", err) return } } // RetrieveTemplateRepo retrieves template repository used to generate this repository -func RetrieveTemplateRepo(ctx *Context, repo *models.Repository) { +func RetrieveTemplateRepo(ctx *Context, repo *repo_model.Repository) { // Non-generated repository will not return error in this method. - if err := repo.GetTemplateRepo(); err != nil { - if models.IsErrRepoNotExist(err) { + templateRepo, err := repo_model.GetTemplateRepo(repo) + if err != nil { + if repo_model.IsErrRepoNotExist(err) { repo.TemplateID = 0 return } ctx.ServerError("GetTemplateRepo", err) return - } else if err = repo.TemplateRepo.GetOwner(); err != nil { + } else if err = templateRepo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("TemplateRepo.GetOwner", err) return } - perm, err := models.GetUserRepoPermission(repo.TemplateRepo, ctx.User) + perm, err := models.GetUserRepoPermission(templateRepo, ctx.User) if err != nil { ctx.ServerError("GetUserRepoPermission", err) return @@ -307,7 +310,7 @@ func EarlyResponseForGoGetMeta(ctx *Context) { ctx.PlainText(200, []byte(com.Expand(`<meta name="go-import" content="{GoGetImport} git {CloneLink}">`, map[string]string{ "GoGetImport": ComposeGoGetImport(username, reponame), - "CloneLink": models.ComposeHTTPSCloneURL(username, reponame), + "CloneLink": repo_model.ComposeHTTPSCloneURL(username, reponame), }))) } @@ -316,7 +319,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) { ownerName := ctx.Params(":username") previousRepoName := ctx.Params(":reponame") - repo, err := models.GetRepositoryByID(redirectRepoID) + repo, err := repo_model.GetRepositoryByID(redirectRepoID) if err != nil { ctx.ServerError("GetRepositoryByID", err) return @@ -334,9 +337,9 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) { ctx.Redirect(path.Join(setting.AppSubURL, redirectPath)) } -func repoAssignment(ctx *Context, repo *models.Repository) { +func repoAssignment(ctx *Context, repo *repo_model.Repository) { var err error - if err = repo.GetOwner(); err != nil { + if err = repo.GetOwner(db.DefaultContext); err != nil { ctx.ServerError("GetOwner", err) return } @@ -361,21 +364,24 @@ func repoAssignment(ctx *Context, repo *models.Repository) { if repo.IsMirror { var err error - ctx.Repo.Mirror, err = models.GetMirrorByRepoID(repo.ID) + mirror, err := repo_model.GetMirrorByRepoID(repo.ID) if err != nil { ctx.ServerError("GetMirrorByRepoID", err) return } - ctx.Data["MirrorEnablePrune"] = ctx.Repo.Mirror.EnablePrune - ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval - ctx.Data["Mirror"] = ctx.Repo.Mirror + ctx.Data["MirrorEnablePrune"] = mirror.EnablePrune + ctx.Data["MirrorInterval"] = mirror.Interval + ctx.Data["Mirror"] = mirror } - if err = repo.LoadPushMirrors(); err != nil { - ctx.ServerError("LoadPushMirrors", err) + + pushMirrors, err := repo_model.GetPushMirrorsByRepoID(repo.ID) + if err != nil { + ctx.ServerError("GetPushMirrorsByRepoID", err) return } ctx.Repo.Repository = repo + ctx.Data["PushMirrors"] = pushMirrors ctx.Data["RepoName"] = ctx.Repo.Repository.Name ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty } @@ -386,9 +392,9 @@ func RepoIDAssignment() func(ctx *Context) { repoID := ctx.ParamsInt64(":repoid") // Get repository. - repo, err := models.GetRepositoryByID(repoID) + repo, err := repo_model.GetRepositoryByID(repoID) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { ctx.NotFound("GetRepositoryByID", nil) } else { ctx.ServerError("GetRepositoryByID", err) @@ -433,9 +439,9 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { ctx.Data["Username"] = ctx.Repo.Owner.Name // Get repository. - repo, err := models.GetRepositoryByName(owner.ID, repoName) + repo, err := repo_model.GetRepositoryByName(owner.ID, repoName) if err != nil { - if models.IsErrRepoNotExist(err) { + if repo_model.IsErrRepoNotExist(err) { redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName) if err == nil { RedirectToRepo(ctx, redirectRepoID) @@ -534,11 +540,11 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { return } - gitRepo, err := git.OpenRepositoryCtx(ctx, models.RepoPath(userName, repoName)) + gitRepo, err := git.OpenRepositoryCtx(ctx, repo_model.RepoPath(userName, repoName)) if err != nil { if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") { log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err) - ctx.Repo.Repository.Status = models.RepositoryBroken + ctx.Repo.Repository.Status = repo_model.RepositoryBroken ctx.Repo.Repository.IsEmpty = true ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch // Only allow access to base of repo or settings @@ -547,7 +553,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { } return } - ctx.ServerError("RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) + ctx.ServerError("RepoAssignment Invalid repo "+repo_model.RepoPath(userName, repoName), err) return } ctx.Repo.GitRepo = gitRepo @@ -570,7 +576,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { if err != nil { if strings.Contains(err.Error(), "fatal: not a git repository ") { log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err) - ctx.Repo.Repository.Status = models.RepositoryBroken + ctx.Repo.Repository.Status = repo_model.RepositoryBroken ctx.Repo.Repository.IsEmpty = true ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch // Only allow access to base of repo or settings @@ -629,7 +635,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { ctx.Data["CanCompareOrPull"] = canCompare ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest - if ctx.Repo.Repository.Status == models.RepositoryPendingTransfer { + if ctx.Repo.Repository.Status == repo_model.RepositoryPendingTransfer { repoTransfer, err := models.GetPendingRepositoryTransfer(ctx.Repo.Repository) if err != nil { ctx.ServerError("GetPendingRepositoryTransfer", err) @@ -791,7 +797,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context ) if ctx.Repo.GitRepo == nil { - repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath) if err != nil { ctx.ServerError("RepoRef Invalid repo "+repoPath, err) |