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
This commit is contained in:
Lunny Xiao 2021-12-10 09:27:50 +08:00 committed by GitHub
parent fb8166c6c6
commit 719bddcd76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
301 changed files with 3193 additions and 2919 deletions

View file

@ -11,6 +11,7 @@ import (
"strings"
"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
@ -20,7 +21,7 @@ import (
)
// CreateNewBranch creates a new repository branch
func CreateNewBranch(doer *user_model.User, repo *models.Repository, oldBranchName, branchName string) (err error) {
func CreateNewBranch(doer *user_model.User, repo *repo_model.Repository, oldBranchName, branchName string) (err error) {
// Check if branch name can be used
if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil {
return err
@ -47,7 +48,7 @@ func CreateNewBranch(doer *user_model.User, repo *models.Repository, oldBranchNa
}
// GetBranch returns a branch by its name
func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
func GetBranch(repo *repo_model.Repository, branch string) (*git.Branch, error) {
if len(branch) == 0 {
return nil, fmt.Errorf("GetBranch: empty string for branch")
}
@ -62,12 +63,12 @@ func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
// GetBranches returns branches from the repository, skipping skip initial branches and
// returning at most limit branches, or all branches if limit is 0.
func GetBranches(repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) {
func GetBranches(repo *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) {
return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
}
// checkBranchName validates branch name with existing repository branches
func checkBranchName(ctx context.Context, repo *models.Repository, name string) error {
func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
_, err := git.WalkReferences(ctx, repo.RepoPath(), func(refName string) error {
branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
switch {
@ -97,7 +98,7 @@ func checkBranchName(ctx context.Context, repo *models.Repository, name string)
}
// CreateNewBranchFromCommit creates a new repository branch
func CreateNewBranchFromCommit(doer *user_model.User, repo *models.Repository, commit, branchName string) (err error) {
func CreateNewBranchFromCommit(doer *user_model.User, repo *repo_model.Repository, commit, branchName string) (err error) {
// Check if branch name can be used
if err := checkBranchName(git.DefaultContext, repo, branchName); err != nil {
return err
@ -118,7 +119,7 @@ func CreateNewBranchFromCommit(doer *user_model.User, repo *models.Repository, c
}
// RenameBranch rename a branch
func RenameBranch(repo *models.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
func RenameBranch(repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
if from == to {
return "target_exist", nil
}
@ -131,7 +132,7 @@ func RenameBranch(repo *models.Repository, doer *user_model.User, gitRepo *git.R
return "from_not_exist", nil
}
if err := repo.RenameBranch(from, to, func(isDefault bool) error {
if err := models.RenameBranch(repo, from, to, func(isDefault bool) error {
err2 := gitRepo.RenameBranch(from, to)
if err2 != nil {
return err2
@ -162,12 +163,12 @@ var (
)
// DeleteBranch delete branch
func DeleteBranch(doer *user_model.User, repo *models.Repository, gitRepo *git.Repository, branchName string) error {
func DeleteBranch(doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
if branchName == repo.DefaultBranch {
return ErrBranchIsDefault
}
isProtected, err := repo.IsProtectedBranch(branchName)
isProtected, err := models.IsProtectedBranch(repo.ID, branchName)
if err != nil {
return err
}
@ -205,7 +206,7 @@ func DeleteBranch(doer *user_model.User, repo *models.Repository, gitRepo *git.R
log.Error("Update: %v", err)
}
if err := repo.AddDeletedBranch(branchName, commit.ID.String(), doer.ID); err != nil {
if err := models.AddDeletedBranch(repo.ID, branchName, commit.ID.String(), doer.ID); err != nil {
log.Warn("AddDeletedBranch: %v", err)
}