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 /models/lfs.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 'models/lfs.go')
-rw-r--r-- | models/lfs.go | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/models/lfs.go b/models/lfs.go index da5e61ed0b..56924ffcf2 100644 --- a/models/lfs.go +++ b/models/lfs.go @@ -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 +} |