mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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:
		@@ -5,9 +5,11 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"errors"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	repo_model "code.gitea.io/gitea/models/repo"
 | 
			
		||||
	user_model "code.gitea.io/gitea/models/user"
 | 
			
		||||
	"code.gitea.io/gitea/modules/lfs"
 | 
			
		||||
	"code.gitea.io/gitea/modules/timeutil"
 | 
			
		||||
@@ -71,12 +73,12 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) {
 | 
			
		||||
// GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID.
 | 
			
		||||
// It may return ErrLFSObjectNotExist or a database error. If the error is nil,
 | 
			
		||||
// the returned pointer is a valid LFSMetaObject.
 | 
			
		||||
func (repo *Repository) GetLFSMetaObjectByOid(oid string) (*LFSMetaObject, error) {
 | 
			
		||||
func GetLFSMetaObjectByOid(repoID int64, oid string) (*LFSMetaObject, error) {
 | 
			
		||||
	if len(oid) == 0 {
 | 
			
		||||
		return nil, ErrLFSObjectNotExist
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repo.ID}
 | 
			
		||||
	m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repoID}
 | 
			
		||||
	has, err := db.GetEngine(db.DefaultContext).Get(m)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -88,7 +90,7 @@ func (repo *Repository) GetLFSMetaObjectByOid(oid string) (*LFSMetaObject, error
 | 
			
		||||
 | 
			
		||||
// RemoveLFSMetaObjectByOid removes a LFSMetaObject entry from database by its OID.
 | 
			
		||||
// It may return ErrLFSObjectNotExist or a database error.
 | 
			
		||||
func (repo *Repository) RemoveLFSMetaObjectByOid(oid string) (int64, error) {
 | 
			
		||||
func RemoveLFSMetaObjectByOid(repoID int64, oid string) (int64, error) {
 | 
			
		||||
	if len(oid) == 0 {
 | 
			
		||||
		return 0, ErrLFSObjectNotExist
 | 
			
		||||
	}
 | 
			
		||||
@@ -99,7 +101,7 @@ func (repo *Repository) RemoveLFSMetaObjectByOid(oid string) (int64, error) {
 | 
			
		||||
	}
 | 
			
		||||
	defer committer.Close()
 | 
			
		||||
 | 
			
		||||
	m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repo.ID}
 | 
			
		||||
	m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repoID}
 | 
			
		||||
	if _, err := db.DeleteByBean(ctx, m); err != nil {
 | 
			
		||||
		return -1, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -113,7 +115,7 @@ func (repo *Repository) RemoveLFSMetaObjectByOid(oid string) (int64, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetLFSMetaObjects returns all LFSMetaObjects associated with a repository
 | 
			
		||||
func (repo *Repository) GetLFSMetaObjects(page, pageSize int) ([]*LFSMetaObject, error) {
 | 
			
		||||
func GetLFSMetaObjects(repoID int64, page, pageSize int) ([]*LFSMetaObject, error) {
 | 
			
		||||
	sess := db.GetEngine(db.DefaultContext)
 | 
			
		||||
 | 
			
		||||
	if page >= 0 && pageSize > 0 {
 | 
			
		||||
@@ -124,12 +126,12 @@ func (repo *Repository) GetLFSMetaObjects(page, pageSize int) ([]*LFSMetaObject,
 | 
			
		||||
		sess.Limit(pageSize, start)
 | 
			
		||||
	}
 | 
			
		||||
	lfsObjects := make([]*LFSMetaObject, 0, pageSize)
 | 
			
		||||
	return lfsObjects, sess.Find(&lfsObjects, &LFSMetaObject{RepositoryID: repo.ID})
 | 
			
		||||
	return lfsObjects, sess.Find(&lfsObjects, &LFSMetaObject{RepositoryID: repoID})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountLFSMetaObjects returns a count of all LFSMetaObjects associated with a repository
 | 
			
		||||
func (repo *Repository) CountLFSMetaObjects() (int64, error) {
 | 
			
		||||
	return db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{RepositoryID: repo.ID})
 | 
			
		||||
func CountLFSMetaObjects(repoID int64) (int64, error) {
 | 
			
		||||
	return db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{RepositoryID: repoID})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LFSObjectAccessible checks if a provided Oid is accessible to the user
 | 
			
		||||
@@ -202,3 +204,21 @@ func IterateLFS(f func(mo *LFSMetaObject) error) error {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CopyLFS copies LFS data from one repo to another
 | 
			
		||||
func CopyLFS(ctx context.Context, newRepo, oldRepo *repo_model.Repository) error {
 | 
			
		||||
	var lfsObjects []*LFSMetaObject
 | 
			
		||||
	if err := db.GetEngine(ctx).Where("repository_id=?", oldRepo.ID).Find(&lfsObjects); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, v := range lfsObjects {
 | 
			
		||||
		v.ID = 0
 | 
			
		||||
		v.RepositoryID = newRepo.ID
 | 
			
		||||
		if _, err := db.GetEngine(ctx).Insert(v); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user