mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	Move some code into models/git (#19879)
* Move access and repo permission to models/perm/access * fix test * Move some git related files into sub package models/git * Fix build * fix git test * move lfs to sub package * move more git related functions to models/git * Move functions sequence * Some improvements per @KN4CK3R and @delvh
This commit is contained in:
		@@ -17,6 +17,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	access_model "code.gitea.io/gitea/models/perm/access"
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	unit_model "code.gitea.io/gitea/models/unit"
 | 
			
		||||
@@ -118,7 +119,7 @@ type CanCommitToBranchResults struct {
 | 
			
		||||
// CanCommitToBranch returns true if repository is editable and user has proper access level
 | 
			
		||||
//   and branch is not protected for push
 | 
			
		||||
func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.User) (CanCommitToBranchResults, error) {
 | 
			
		||||
	protectedBranch, err := models.GetProtectedBranchBy(ctx, r.Repository.ID, r.BranchName)
 | 
			
		||||
	protectedBranch, err := git_model.GetProtectedBranchBy(ctx, r.Repository.ID, r.BranchName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return CanCommitToBranchResults{}, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -825,7 +826,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
 | 
			
		||||
		if len(ref) == 0 {
 | 
			
		||||
			// maybe it's a renamed branch
 | 
			
		||||
			return getRefNameFromPath(ctx, path, func(s string) bool {
 | 
			
		||||
				b, exist, err := models.FindRenamedBranch(ctx.Repo.Repository.ID, s)
 | 
			
		||||
				b, exist, err := git_model.FindRenamedBranch(ctx.Repo.Repository.ID, s)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Error("FindRenamedBranch", err)
 | 
			
		||||
					return false
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ import (
 | 
			
		||||
	asymkey_model "code.gitea.io/gitea/models/asymkey"
 | 
			
		||||
	"code.gitea.io/gitea/models/auth"
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	"code.gitea.io/gitea/models/organization"
 | 
			
		||||
	"code.gitea.io/gitea/models/perm"
 | 
			
		||||
	access_model "code.gitea.io/gitea/models/perm/access"
 | 
			
		||||
@@ -39,7 +40,7 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ToBranch convert a git.Commit and git.Branch to an api.Branch
 | 
			
		||||
func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
 | 
			
		||||
func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
 | 
			
		||||
	if bp == nil {
 | 
			
		||||
		var hasPerm bool
 | 
			
		||||
		var canPush bool
 | 
			
		||||
@@ -88,14 +89,14 @@ func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *mod
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		branch.UserCanPush = bp.CanUserPush(user.ID)
 | 
			
		||||
		branch.UserCanMerge = models.IsUserMergeWhitelisted(db.DefaultContext, bp, user.ID, permission)
 | 
			
		||||
		branch.UserCanMerge = git_model.IsUserMergeWhitelisted(db.DefaultContext, bp, user.ID, permission)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return branch, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
 | 
			
		||||
func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
 | 
			
		||||
func ToBranchProtection(bp *git_model.ProtectedBranch) *api.BranchProtection {
 | 
			
		||||
	pushWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.WhitelistUserIDs)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err)
 | 
			
		||||
@@ -399,7 +400,7 @@ func ToOAuth2Application(app *auth.OAuth2Application) *api.OAuth2Application {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ToLFSLock convert a LFSLock to api.LFSLock
 | 
			
		||||
func ToLFSLock(l *models.LFSLock) *api.LFSLock {
 | 
			
		||||
func ToLFSLock(l *git_model.LFSLock) *api.LFSLock {
 | 
			
		||||
	u, err := user_model.GetUserByID(l.OwnerID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
 
 | 
			
		||||
@@ -5,13 +5,13 @@
 | 
			
		||||
package convert
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ToCommitStatus converts models.CommitStatus to api.CommitStatus
 | 
			
		||||
func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus {
 | 
			
		||||
// ToCommitStatus converts git_model.CommitStatus to api.CommitStatus
 | 
			
		||||
func ToCommitStatus(status *git_model.CommitStatus) *api.CommitStatus {
 | 
			
		||||
	apiStatus := &api.CommitStatus{
 | 
			
		||||
		Created:     status.CreatedUnix.AsTime(),
 | 
			
		||||
		Updated:     status.CreatedUnix.AsTime(),
 | 
			
		||||
@@ -32,7 +32,7 @@ func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ToCombinedStatus converts List of CommitStatus to a CombinedStatus
 | 
			
		||||
func ToCombinedStatus(statuses []*models.CommitStatus, repo *api.Repository) *api.CombinedStatus {
 | 
			
		||||
func ToCombinedStatus(statuses []*git_model.CommitStatus, repo *api.Repository) *api.CombinedStatus {
 | 
			
		||||
	if len(statuses) == 0 {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -9,9 +9,9 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	asymkey_model "code.gitea.io/gitea/models/asymkey"
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
@@ -117,14 +117,14 @@ func (graph *Graph) LoadAndProcessCommits(repository *repo_model.Repository, git
 | 
			
		||||
		c.Verification = asymkey_model.ParseCommitWithSignature(c.Commit)
 | 
			
		||||
 | 
			
		||||
		_ = asymkey_model.CalculateTrustStatus(c.Verification, repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
 | 
			
		||||
			return models.IsOwnerMemberCollaborator(repository, user.ID)
 | 
			
		||||
			return repo_model.IsOwnerMemberCollaborator(repository, user.ID)
 | 
			
		||||
		}, &keyMap)
 | 
			
		||||
 | 
			
		||||
		statuses, _, err := models.GetLatestCommitStatus(db.DefaultContext, repository.ID, c.Commit.ID.String(), db.ListOptions{})
 | 
			
		||||
		statuses, _, err := git_model.GetLatestCommitStatus(db.DefaultContext, repository.ID, c.Commit.ID.String(), db.ListOptions{})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("GetLatestCommitStatus: %v", err)
 | 
			
		||||
		} else {
 | 
			
		||||
			c.Status = models.CalcCommitStatus(statuses)
 | 
			
		||||
			c.Status = git_model.CalcCommitStatus(statuses)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
@@ -240,7 +240,7 @@ type Commit struct {
 | 
			
		||||
	Commit       *git.Commit
 | 
			
		||||
	User         *user_model.User
 | 
			
		||||
	Verification *asymkey_model.CommitVerification
 | 
			
		||||
	Status       *models.CommitStatus
 | 
			
		||||
	Status       *git_model.CommitStatus
 | 
			
		||||
	Flow         int64
 | 
			
		||||
	Row          int
 | 
			
		||||
	Column       int
 | 
			
		||||
 
 | 
			
		||||
@@ -113,7 +113,7 @@ func (s *ContentStore) Verify(pointer Pointer) (bool, error) {
 | 
			
		||||
	return true, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadMetaObject will read a models.LFSMetaObject and return a reader
 | 
			
		||||
// ReadMetaObject will read a git_model.LFSMetaObject and return a reader
 | 
			
		||||
func ReadMetaObject(pointer Pointer) (io.ReadCloser, error) {
 | 
			
		||||
	contentStore := NewContentStore()
 | 
			
		||||
	return contentStore.Get(pointer)
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	access_model "code.gitea.io/gitea/models/perm/access"
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
@@ -146,7 +147,7 @@ func UpdateRepoSize(ctx context.Context, repo *repo_model.Repository) error {
 | 
			
		||||
		return fmt.Errorf("updateSize: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	lfsSize, err := models.GetRepoLFSSize(ctx, repo.ID)
 | 
			
		||||
	lfsSize, err := git_model.GetRepoLFSSize(ctx, repo.ID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,7 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
@@ -278,7 +279,7 @@ func GenerateGitContent(ctx context.Context, templateRepo, generateRepo *repo_mo
 | 
			
		||||
		return fmt.Errorf("failed to update size for repository: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := models.CopyLFS(ctx, generateRepo, templateRepo); err != nil {
 | 
			
		||||
	if err := git_model.CopyLFS(ctx, generateRepo, templateRepo); err != nil {
 | 
			
		||||
		return fmt.Errorf("failed to copy LFS: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	git_model "code.gitea.io/gitea/models/git"
 | 
			
		||||
	"code.gitea.io/gitea/models/organization"
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
@@ -390,7 +391,7 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
 | 
			
		||||
 | 
			
		||||
			defer content.Close()
 | 
			
		||||
 | 
			
		||||
			_, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repo.ID})
 | 
			
		||||
			_, err := git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: p, RepositoryID: repo.ID})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, p, err)
 | 
			
		||||
				return err
 | 
			
		||||
@@ -398,7 +399,7 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
 | 
			
		||||
 | 
			
		||||
			if err := contentStore.Put(p, content); err != nil {
 | 
			
		||||
				log.Error("Repo[%-v]: Error storing content for LFS meta object %-v: %v", repo, p, err)
 | 
			
		||||
				if _, err2 := models.RemoveLFSMetaObjectByOid(repo.ID, p.Oid); err2 != nil {
 | 
			
		||||
				if _, err2 := git_model.RemoveLFSMetaObjectByOid(repo.ID, p.Oid); err2 != nil {
 | 
			
		||||
					log.Error("Repo[%-v]: Error removing LFS meta object %-v: %v", repo, p, err2)
 | 
			
		||||
				}
 | 
			
		||||
				return err
 | 
			
		||||
@@ -417,8 +418,8 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
 | 
			
		||||
 | 
			
		||||
	var batch []lfs.Pointer
 | 
			
		||||
	for pointerBlob := range pointerChan {
 | 
			
		||||
		meta, err := models.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid)
 | 
			
		||||
		if err != nil && err != models.ErrLFSObjectNotExist {
 | 
			
		||||
		meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid)
 | 
			
		||||
		if err != nil && err != git_model.ErrLFSObjectNotExist {
 | 
			
		||||
			log.Error("Repo[%-v]: Error querying LFS meta object %-v: %v", repo, pointerBlob.Pointer, err)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
@@ -437,7 +438,7 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
 | 
			
		||||
 | 
			
		||||
		if exist {
 | 
			
		||||
			log.Trace("Repo[%-v]: LFS object %-v already present; creating meta object", repo, pointerBlob.Pointer)
 | 
			
		||||
			_, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: pointerBlob.Pointer, RepositoryID: repo.ID})
 | 
			
		||||
			_, err := git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: pointerBlob.Pointer, RepositoryID: repo.ID})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, pointerBlob.Pointer, err)
 | 
			
		||||
				return err
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,9 @@ const (
 | 
			
		||||
	RepoCreatingPublic             = "public"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ItemsPerPage maximum items per page in forks, watchers and stars of a repo
 | 
			
		||||
const ItemsPerPage = 40
 | 
			
		||||
 | 
			
		||||
// Repository settings
 | 
			
		||||
var (
 | 
			
		||||
	Repository = struct {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user