1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00: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:
Lunny Xiao
2022-06-12 23:51:54 +08:00
committed by GitHub
parent a9dc9b06e4
commit 110fc57cbc
67 changed files with 549 additions and 495 deletions

View File

@@ -9,7 +9,7 @@ import (
"strconv"
"strings"
"code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
@@ -20,9 +20,9 @@ import (
api "code.gitea.io/gitea/modules/structs"
)
func handleLockListOut(ctx *context.Context, repo *repo_model.Repository, lock *models.LFSLock, err error) {
func handleLockListOut(ctx *context.Context, repo *repo_model.Repository, lock *git_model.LFSLock, err error) {
if err != nil {
if models.IsErrLFSLockNotExist(err) {
if git_model.IsErrLFSLockNotExist(err) {
ctx.JSON(http.StatusOK, api.LFSLockList{
Locks: []*api.LFSLock{},
})
@@ -88,8 +88,8 @@ func GetListLockHandler(ctx *context.Context) {
})
return
}
lock, err := models.GetLFSLockByID(ctx, v)
if err != nil && !models.IsErrLFSLockNotExist(err) {
lock, err := git_model.GetLFSLockByID(ctx, v)
if err != nil && !git_model.IsErrLFSLockNotExist(err) {
log.Error("Unable to get lock with ID[%s]: Error: %v", v, err)
}
handleLockListOut(ctx, repository, lock, err)
@@ -98,8 +98,8 @@ func GetListLockHandler(ctx *context.Context) {
path := ctx.FormString("path")
if path != "" { // Case where we request a specific id
lock, err := models.GetLFSLock(ctx, repository, path)
if err != nil && !models.IsErrLFSLockNotExist(err) {
lock, err := git_model.GetLFSLock(ctx, repository, path)
if err != nil && !git_model.IsErrLFSLockNotExist(err) {
log.Error("Unable to get lock for repository %-v with path %s: Error: %v", repository, path, err)
}
handleLockListOut(ctx, repository, lock, err)
@@ -107,7 +107,7 @@ func GetListLockHandler(ctx *context.Context) {
}
// If no query params path or id
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
lockList, err := git_model.GetLFSLockByRepoID(repository.ID, cursor, limit)
if err != nil {
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
@@ -168,19 +168,19 @@ func PostLockHandler(ctx *context.Context) {
return
}
lock, err := models.CreateLFSLock(repository, &models.LFSLock{
lock, err := git_model.CreateLFSLock(repository, &git_model.LFSLock{
Path: req.Path,
OwnerID: ctx.Doer.ID,
})
if err != nil {
if models.IsErrLFSLockAlreadyExist(err) {
if git_model.IsErrLFSLockAlreadyExist(err) {
ctx.JSON(http.StatusConflict, api.LFSLockError{
Lock: convert.ToLFSLock(lock),
Message: "already created lock",
})
return
}
if models.IsErrLFSUnauthorizedAction(err) {
if git_model.IsErrLFSUnauthorizedAction(err) {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to create locks : " + err.Error(),
@@ -234,7 +234,7 @@ func VerifyLockHandler(ctx *context.Context) {
} else if limit < 0 {
limit = 0
}
lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
lockList, err := git_model.GetLFSLockByRepoID(repository.ID, cursor, limit)
if err != nil {
log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
@@ -301,9 +301,9 @@ func UnLockHandler(ctx *context.Context) {
return
}
lock, err := models.DeleteLFSLockByID(ctx.ParamsInt64("lid"), repository, ctx.Doer, req.Force)
lock, err := git_model.DeleteLFSLockByID(ctx.ParamsInt64("lid"), repository, ctx.Doer, req.Force)
if err != nil {
if models.IsErrLFSUnauthorizedAction(err) {
if git_model.IsErrLFSUnauthorizedAction(err) {
ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
Message: "You must have push access to delete locks : " + err.Error(),

View File

@@ -18,7 +18,7 @@ import (
"strconv"
"strings"
"code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
@@ -197,8 +197,8 @@ func BatchHandler(ctx *context.Context) {
return
}
meta, err := models.GetLFSMetaObjectByOid(repository.ID, p.Oid)
if err != nil && err != models.ErrLFSObjectNotExist {
meta, err := git_model.GetLFSMetaObjectByOid(repository.ID, p.Oid)
if err != nil && err != git_model.ErrLFSObjectNotExist {
log.Error("Unable to get LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
writeStatus(ctx, http.StatusInternalServerError)
return
@@ -223,14 +223,14 @@ func BatchHandler(ctx *context.Context) {
}
if exists && meta == nil {
accessible, err := models.LFSObjectAccessible(ctx.Doer, p.Oid)
accessible, err := git_model.LFSObjectAccessible(ctx.Doer, p.Oid)
if err != nil {
log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err)
writeStatus(ctx, http.StatusInternalServerError)
return
}
if accessible {
_, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
_, err := git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
if err != nil {
log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
writeStatus(ctx, http.StatusInternalServerError)
@@ -297,7 +297,7 @@ func UploadHandler(ctx *context.Context) {
uploadOrVerify := func() error {
if exists {
accessible, err := models.LFSObjectAccessible(ctx.Doer, p.Oid)
accessible, err := git_model.LFSObjectAccessible(ctx.Doer, p.Oid)
if err != nil {
log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err)
return err
@@ -323,7 +323,7 @@ func UploadHandler(ctx *context.Context) {
log.Error("Error putting LFS MetaObject [%s] into content store. Error: %v", p.Oid, err)
return err
}
_, err := models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
_, err := git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
return err
}
@@ -335,7 +335,7 @@ func UploadHandler(ctx *context.Context) {
} else {
writeStatus(ctx, http.StatusInternalServerError)
}
if _, err = models.RemoveLFSMetaObjectByOid(repository.ID, p.Oid); err != nil {
if _, err = git_model.RemoveLFSMetaObjectByOid(repository.ID, p.Oid); err != nil {
log.Error("Error whilst removing metaobject for LFS OID[%s]: %v", p.Oid, err)
}
return
@@ -386,7 +386,7 @@ func getRequestContext(ctx *context.Context) *requestContext {
}
}
func getAuthenticatedMeta(ctx *context.Context, rc *requestContext, p lfs_module.Pointer, requireWrite bool) *models.LFSMetaObject {
func getAuthenticatedMeta(ctx *context.Context, rc *requestContext, p lfs_module.Pointer, requireWrite bool) *git_model.LFSMetaObject {
if !p.IsValid() {
log.Info("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo)
writeStatusMessage(ctx, http.StatusUnprocessableEntity, "Oid or size are invalid")
@@ -398,7 +398,7 @@ func getAuthenticatedMeta(ctx *context.Context, rc *requestContext, p lfs_module
return nil
}
meta, err := models.GetLFSMetaObjectByOid(repository.ID, p.Oid)
meta, err := git_model.GetLFSMetaObjectByOid(repository.ID, p.Oid)
if err != nil {
log.Error("Unable to get LFS OID[%s] Error: %v", p.Oid, err)
writeStatus(ctx, http.StatusNotFound)