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
tokarchuk/v1.17
Lunny Xiao 2 years ago committed by GitHub
parent a9dc9b06e4
commit 110fc57cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      cmd/migrate_storage.go
  2. 4
      cmd/serv.go
  3. 22
      integrations/api_repo_lfs_test.go
  4. 6
      integrations/lfs_getobject_test.go
  5. 9
      integrations/repo_tag_test.go
  6. 80
      models/branch.go
  7. 26
      models/commit.go
  8. 78
      models/error.go
  9. 80
      models/git/branches.go
  10. 50
      models/git/branches_test.go
  11. 19
      models/git/commit_status.go
  12. 5
      models/git/commit_status_test.go
  13. 75
      models/git/lfs.go
  14. 2
      models/git/lfs_lock.go
  15. 18
      models/git/main_test.go
  16. 2
      models/git/protected_tag.go
  17. 33
      models/git/protected_tag_test.go
  18. 13
      models/issue_comment.go
  19. 3
      models/org_team.go
  20. 9
      models/pull.go
  21. 20
      models/repo.go
  22. 21
      models/repo/collaboration.go
  23. 22
      models/repo_collaboration.go
  24. 3
      models/review.go
  25. 3
      models/user.go
  26. 5
      modules/context/repo.go
  27. 9
      modules/convert/convert.go
  28. 8
      modules/convert/status.go
  29. 10
      modules/gitgraph/graph_models.go
  30. 2
      modules/lfs/content_store.go
  31. 3
      modules/repository/create.go
  32. 3
      modules/repository/generate.go
  33. 11
      modules/repository/repo.go
  34. 3
      modules/setting/repository.go
  35. 29
      routers/api/v1/repo/branch.go
  36. 5
      routers/api/v1/repo/file.go
  37. 8
      routers/api/v1/repo/status.go
  38. 9
      routers/private/hook_pre_receive.go
  39. 11
      routers/web/repo/branch.go
  40. 15
      routers/web/repo/commit.go
  41. 3
      routers/web/repo/compare.go
  42. 4
      routers/web/repo/download.go
  43. 9
      routers/web/repo/editor.go
  44. 13
      routers/web/repo/issue.go
  45. 36
      routers/web/repo/lfs.go
  46. 15
      routers/web/repo/pull.go
  47. 18
      routers/web/repo/setting_protected_branch.go
  48. 16
      routers/web/repo/tag.go
  49. 25
      routers/web/repo/view.go
  50. 3
      routers/web/repo/wiki.go
  51. 5
      services/asymkey/sign.go
  52. 3
      services/cron/tasks_basic.go
  53. 3
      services/gitdiff/gitdiff.go
  54. 28
      services/lfs/locks.go
  55. 20
      services/lfs/server.go
  56. 13
      services/pull/commit_status.go
  57. 9
      services/pull/lfs.go
  58. 13
      services/pull/merge.go
  59. 15
      services/pull/pull.go
  60. 5
      services/release/release.go
  61. 7
      services/repository/branch.go
  62. 6
      services/repository/files/commit.go
  63. 3
      services/repository/files/patch.go
  64. 15
      services/repository/files/update.go
  65. 13
      services/repository/files/upload.go
  66. 3
      services/repository/fork.go
  67. 3
      services/repository/push.go

@ -9,8 +9,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/models/migrations"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -88,7 +88,7 @@ func migrateAttachments(dstStorage storage.ObjectStorage) error {
} }
func migrateLFS(dstStorage storage.ObjectStorage) error { func migrateLFS(dstStorage storage.ObjectStorage) error {
return models.IterateLFS(func(mo *models.LFSMetaObject) error { return git_model.IterateLFS(func(mo *git_model.LFSMetaObject) error {
_, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath()) _, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath())
return err return err
}) })

@ -17,8 +17,8 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
@ -276,7 +276,7 @@ func runServ(c *cli.Context) error {
return fail("Internal error", "Failed to sign JWT token: %v", err) return fail("Internal error", "Failed to sign JWT token: %v", err)
} }
tokenAuthentication := &models.LFSTokenResponse{ tokenAuthentication := &git_model.LFSTokenResponse{
Header: make(map[string]string), Header: make(map[string]string),
Href: url, Href: url,
} }

@ -12,7 +12,7 @@ import (
"strings" "strings"
"testing" "testing"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -76,7 +76,7 @@ func TestAPILFSBatch(t *testing.T) {
content := []byte("dummy1") content := []byte("dummy1")
oid := storeObjectInRepo(t, repo.ID, &content) oid := storeObjectInRepo(t, repo.ID, &content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid) defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)
session := loginUser(t, "user2") session := loginUser(t, "user2")
@ -260,9 +260,9 @@ func TestAPILFSBatch(t *testing.T) {
content := []byte("dummy0") content := []byte("dummy0")
storeObjectInRepo(t, repo2.ID, &content) storeObjectInRepo(t, repo2.ID, &content)
meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.Nil(t, meta) assert.Nil(t, meta)
assert.Equal(t, models.ErrLFSObjectNotExist, err) assert.Equal(t, git_model.ErrLFSObjectNotExist, err)
req := newRequest(t, &lfs.BatchRequest{ req := newRequest(t, &lfs.BatchRequest{
Operation: "upload", Operation: "upload",
@ -275,7 +275,7 @@ func TestAPILFSBatch(t *testing.T) {
assert.Nil(t, br.Objects[0].Error) assert.Nil(t, br.Objects[0].Error)
assert.Empty(t, br.Objects[0].Actions) assert.Empty(t, br.Objects[0].Actions)
meta, err = models.GetLFSMetaObjectByOid(repo.ID, p.Oid) meta, err = git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, meta) assert.NotNil(t, meta)
@ -336,7 +336,7 @@ func TestAPILFSUpload(t *testing.T) {
content := []byte("dummy3") content := []byte("dummy3")
oid := storeObjectInRepo(t, repo.ID, &content) oid := storeObjectInRepo(t, repo.ID, &content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid) defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)
session := loginUser(t, "user2") session := loginUser(t, "user2")
@ -365,9 +365,9 @@ func TestAPILFSUpload(t *testing.T) {
err = contentStore.Put(p, bytes.NewReader([]byte("dummy5"))) err = contentStore.Put(p, bytes.NewReader([]byte("dummy5")))
assert.NoError(t, err) assert.NoError(t, err)
meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.Nil(t, meta) assert.Nil(t, meta)
assert.Equal(t, models.ErrLFSObjectNotExist, err) assert.Equal(t, git_model.ErrLFSObjectNotExist, err)
t.Run("InvalidAccess", func(t *testing.T) { t.Run("InvalidAccess", func(t *testing.T) {
req := newRequest(t, p, "invalid") req := newRequest(t, p, "invalid")
@ -378,7 +378,7 @@ func TestAPILFSUpload(t *testing.T) {
req := newRequest(t, p, "dummy5") req := newRequest(t, p, "dummy5")
session.MakeRequest(t, req, http.StatusOK) session.MakeRequest(t, req, http.StatusOK)
meta, err = models.GetLFSMetaObjectByOid(repo.ID, p.Oid) meta, err = git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, meta) assert.NotNil(t, meta)
}) })
@ -426,7 +426,7 @@ func TestAPILFSUpload(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, exist) assert.True(t, exist)
meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, meta) assert.NotNil(t, meta)
}) })
@ -441,7 +441,7 @@ func TestAPILFSVerify(t *testing.T) {
content := []byte("dummy3") content := []byte("dummy3")
oid := storeObjectInRepo(t, repo.ID, &content) oid := storeObjectInRepo(t, repo.ID, &content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid) defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)
session := loginUser(t, "user2") session := loginUser(t, "user2")

@ -12,7 +12,7 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
@ -28,7 +28,7 @@ func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string
pointer, err := lfs.GeneratePointer(bytes.NewReader(*content)) pointer, err := lfs.GeneratePointer(bytes.NewReader(*content))
assert.NoError(t, err) assert.NoError(t, err)
_, err = models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID}) _, err = git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID})
assert.NoError(t, err) assert.NoError(t, err)
contentStore := lfs.NewContentStore() contentStore := lfs.NewContentStore()
exist, err := contentStore.Exists(pointer) exist, err := contentStore.Exists(pointer)
@ -44,7 +44,7 @@ func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, exp
repo, err := repo_model.GetRepositoryByOwnerAndName("user2", "repo1") repo, err := repo_model.GetRepositoryByOwnerAndName("user2", "repo1")
assert.NoError(t, err) assert.NoError(t, err)
oid := storeObjectInRepo(t, repo.ID, content) oid := storeObjectInRepo(t, repo.ID, content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid) defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)
session := loginUser(t, "user2") session := loginUser(t, "user2")

@ -10,6 +10,7 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -32,12 +33,12 @@ func TestCreateNewTagProtected(t *testing.T) {
err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1", "first tag") err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1", "first tag")
assert.NoError(t, err) assert.NoError(t, err)
err = models.InsertProtectedTag(&models.ProtectedTag{ err = git_model.InsertProtectedTag(&git_model.ProtectedTag{
RepoID: repo.ID, RepoID: repo.ID,
NamePattern: "v-*", NamePattern: "v-*",
}) })
assert.NoError(t, err) assert.NoError(t, err)
err = models.InsertProtectedTag(&models.ProtectedTag{ err = git_model.InsertProtectedTag(&git_model.ProtectedTag{
RepoID: repo.ID, RepoID: repo.ID,
NamePattern: "v-1.1", NamePattern: "v-1.1",
AllowlistUserIDs: []int64{repo.OwnerID}, AllowlistUserIDs: []int64{repo.OwnerID},
@ -87,11 +88,11 @@ func TestCreateNewTagProtected(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
protectedTags, err := models.GetProtectedTags(repo.ID) protectedTags, err := git_model.GetProtectedTags(repo.ID)
assert.NoError(t, err) assert.NoError(t, err)
for _, protectedTag := range protectedTags { for _, protectedTag := range protectedTags {
err = models.DeleteProtectedTag(protectedTag) err = git_model.DeleteProtectedTag(protectedTag)
assert.NoError(t, err) assert.NoError(t, err)
} }
} }

@ -0,0 +1,80 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"context"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/log"
)
// HasEnoughApprovals returns true if pr has enough granted approvals.
func HasEnoughApprovals(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
if protectBranch.RequiredApprovals == 0 {
return true
}
return GetGrantedApprovalsCount(ctx, protectBranch, pr) >= protectBranch.RequiredApprovals
}
// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
func GetGrantedApprovalsCount(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) int64 {
sess := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeApprove).
And("official = ?", true).
And("dismissed = ?", false)
if protectBranch.DismissStaleApprovals {
sess = sess.And("stale = ?", false)
}
approvals, err := sess.Count(new(Review))
if err != nil {
log.Error("GetGrantedApprovalsCount: %v", err)
return 0
}
return approvals
}
// MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
func MergeBlockedByRejectedReview(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
if !protectBranch.BlockOnRejectedReviews {
return false
}
rejectExist, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeReject).
And("official = ?", true).
And("dismissed = ?", false).
Exist(new(Review))
if err != nil {
log.Error("MergeBlockedByRejectedReview: %v", err)
return true
}
return rejectExist
}
// MergeBlockedByOfficialReviewRequests block merge because of some review request to official reviewer
// of from official review
func MergeBlockedByOfficialReviewRequests(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
if !protectBranch.BlockOnOfficialReviewRequests {
return false
}
has, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeRequest).
And("official = ?", true).
Exist(new(Review))
if err != nil {
log.Error("MergeBlockedByOfficialReviewRequests: %v", err)
return true
}
return has
}
// MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
func MergeBlockedByOutdatedBranch(protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0
}

@ -1,26 +0,0 @@
// Copyright 2021 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
asymkey_model "code.gitea.io/gitea/models/asymkey"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
)
// ConvertFromGitCommit converts git commits into SignCommitWithStatuses
func ConvertFromGitCommit(commits []*git.Commit, repo *repo_model.Repository) []*SignCommitWithStatuses {
return ParseCommitsWithStatus(
asymkey_model.ParseCommitsWithSignature(
user_model.ValidateCommitsWithEmails(commits),
repo.GetTrustModel(),
func(user *user_model.User) (bool, error) {
return IsOwnerMemberCollaborator(repo, user.ID)
},
),
repo,
)
}

@ -8,7 +8,6 @@ package models
import ( import (
"fmt" "fmt"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
) )
@ -145,83 +144,6 @@ func (err ErrAccessTokenEmpty) Error() string {
return "access token is empty" return "access token is empty"
} }
//.____ ____________________
//| | \_ _____/ _____/
//| | | __) \_____ \
//| |___| \ / \
//|_______ \___ / /_______ /
// \/ \/ \/
// ErrLFSLockNotExist represents a "LFSLockNotExist" kind of error.
type ErrLFSLockNotExist struct {
ID int64
RepoID int64
Path string
}
// IsErrLFSLockNotExist checks if an error is a ErrLFSLockNotExist.
func IsErrLFSLockNotExist(err error) bool {
_, ok := err.(ErrLFSLockNotExist)
return ok
}
func (err ErrLFSLockNotExist) Error() string {
return fmt.Sprintf("lfs lock does not exist [id: %d, rid: %d, path: %s]", err.ID, err.RepoID, err.Path)
}
// ErrLFSUnauthorizedAction represents a "LFSUnauthorizedAction" kind of error.
type ErrLFSUnauthorizedAction struct {
RepoID int64
UserName string
Mode perm.AccessMode
}
// IsErrLFSUnauthorizedAction checks if an error is a ErrLFSUnauthorizedAction.
func IsErrLFSUnauthorizedAction(err error) bool {
_, ok := err.(ErrLFSUnauthorizedAction)
return ok
}
func (err ErrLFSUnauthorizedAction) Error() string {
if err.Mode == perm.AccessModeWrite {
return fmt.Sprintf("User %s doesn't have write access for lfs lock [rid: %d]", err.UserName, err.RepoID)
}
return fmt.Sprintf("User %s doesn't have read access for lfs lock [rid: %d]", err.UserName, err.RepoID)
}
// ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error.
type ErrLFSLockAlreadyExist struct {
RepoID int64
Path string
}
// IsErrLFSLockAlreadyExist checks if an error is a ErrLFSLockAlreadyExist.
func IsErrLFSLockAlreadyExist(err error) bool {
_, ok := err.(ErrLFSLockAlreadyExist)
return ok
}
func (err ErrLFSLockAlreadyExist) Error() string {
return fmt.Sprintf("lfs lock already exists [rid: %d, path: %s]", err.RepoID, err.Path)
}
// ErrLFSFileLocked represents a "LFSFileLocked" kind of error.
type ErrLFSFileLocked struct {
RepoID int64
Path string
UserName string
}
// IsErrLFSFileLocked checks if an error is a ErrLFSFileLocked.
func IsErrLFSFileLocked(err error) bool {
_, ok := err.(ErrLFSFileLocked)
return ok
}
func (err ErrLFSFileLocked) Error() string {
return fmt.Sprintf("File is lfs locked [repo: %d, locked by: %s, path: %s]", err.RepoID, err.UserName, err.Path)
}
// ErrNoPendingRepoTransfer is an error type for repositories without a pending // ErrNoPendingRepoTransfer is an error type for repositories without a pending
// transfer request // transfer request
type ErrNoPendingRepoTransfer struct { type ErrNoPendingRepoTransfer struct {

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git
import ( import (
"context" "context"
@ -129,10 +129,11 @@ func IsUserMergeWhitelisted(ctx context.Context, protectBranch *ProtectedBranch,
// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals) // IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
func IsUserOfficialReviewer(protectBranch *ProtectedBranch, user *user_model.User) (bool, error) { func IsUserOfficialReviewer(protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
return isUserOfficialReviewer(db.DefaultContext, protectBranch, user) return IsUserOfficialReviewerCtx(db.DefaultContext, protectBranch, user)
} }
func isUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) { // IsUserOfficialReviewerCtx check if user is official reviewer for the branch (counts towards required approvals)
func IsUserOfficialReviewerCtx(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
repo, err := repo_model.GetRepositoryByIDCtx(ctx, protectBranch.RepoID) repo, err := repo_model.GetRepositoryByIDCtx(ctx, protectBranch.RepoID)
if err != nil { if err != nil {
return false, err return false, err
@ -159,73 +160,6 @@ func isUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch,
return inTeam, nil return inTeam, nil
} }
// HasEnoughApprovals returns true if pr has enough granted approvals.
func (protectBranch *ProtectedBranch) HasEnoughApprovals(ctx context.Context, pr *PullRequest) bool {
if protectBranch.RequiredApprovals == 0 {
return true
}
return protectBranch.GetGrantedApprovalsCount(ctx, pr) >= protectBranch.RequiredApprovals
}
// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(ctx context.Context, pr *PullRequest) int64 {
sess := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeApprove).
And("official = ?", true).
And("dismissed = ?", false)
if protectBranch.DismissStaleApprovals {
sess = sess.And("stale = ?", false)
}
approvals, err := sess.Count(new(Review))
if err != nil {
log.Error("GetGrantedApprovalsCount: %v", err)
return 0
}
return approvals
}
// MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(ctx context.Context, pr *PullRequest) bool {
if !protectBranch.BlockOnRejectedReviews {
return false
}
rejectExist, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeReject).
And("official = ?", true).
And("dismissed = ?", false).
Exist(new(Review))
if err != nil {
log.Error("MergeBlockedByRejectedReview: %v", err)
return true
}
return rejectExist
}
// MergeBlockedByOfficialReviewRequests block merge because of some review request to official reviewer
// of from official review
func (protectBranch *ProtectedBranch) MergeBlockedByOfficialReviewRequests(ctx context.Context, pr *PullRequest) bool {
if !protectBranch.BlockOnOfficialReviewRequests {
return false
}
has, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeRequest).
And("official = ?", true).
Exist(new(Review))
if err != nil {
log.Error("MergeBlockedByOfficialReviewRequests: %v", err)
return true
}
return has
}
// MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
func (protectBranch *ProtectedBranch) MergeBlockedByOutdatedBranch(pr *PullRequest) bool {
return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0
}
// GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice // GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob { func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
return getFilePatterns(protectBranch.ProtectedFilePatterns) return getFilePatterns(protectBranch.ProtectedFilePatterns)
@ -252,13 +186,13 @@ func getFilePatterns(filePatterns string) []glob.Glob {
} }
// MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change // MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change
func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(pr *PullRequest) bool { func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(changedProtectedFiles []string) bool {
glob := protectBranch.GetProtectedFilePatterns() glob := protectBranch.GetProtectedFilePatterns()
if len(glob) == 0 { if len(glob) == 0 {
return false return false
} }
return len(pr.ChangedProtectedFiles) > 0 return len(changedProtectedFiles) > 0
} }
// IsProtectedFile return if path is protected // IsProtectedFile return if path is protected
@ -642,7 +576,7 @@ func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(i
} }
// 3. Update all not merged pull request base branch name // 3. Update all not merged pull request base branch name
_, err = sess.Table(new(PullRequest)).Where("base_repo_id=? AND base_branch=? AND has_merged=?", _, err = sess.Table("pull_request").Where("base_repo_id=? AND base_branch=? AND has_merged=?",
repo.ID, from, false). repo.ID, from, false).
Update(map[string]interface{}{"base_branch": to}) Update(map[string]interface{}{"base_branch": to})
if err != nil { if err != nil {

@ -2,12 +2,14 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git_test
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
@ -17,24 +19,24 @@ import (
func TestAddDeletedBranch(t *testing.T) { func TestAddDeletedBranch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1}).(*git_model.DeletedBranch)
assert.Error(t, AddDeletedBranch(repo.ID, firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) assert.Error(t, git_model.AddDeletedBranch(repo.ID, firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID))
assert.NoError(t, AddDeletedBranch(repo.ID, "test", "5655464564554545466464656", int64(1))) assert.NoError(t, git_model.AddDeletedBranch(repo.ID, "test", "5655464564554545466464656", int64(1)))
} }
func TestGetDeletedBranches(t *testing.T) { func TestGetDeletedBranches(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
branches, err := GetDeletedBranches(repo.ID) branches, err := git_model.GetDeletedBranches(repo.ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, branches, 2) assert.Len(t, branches, 2)
} }
func TestGetDeletedBranch(t *testing.T) { func TestGetDeletedBranch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1}).(*git_model.DeletedBranch)
assert.NotNil(t, getDeletedBranch(t, firstBranch)) assert.NotNil(t, getDeletedBranch(t, firstBranch))
} }
@ -42,8 +44,8 @@ func TestGetDeletedBranch(t *testing.T) {
func TestDeletedBranchLoadUser(t *testing.T) { func TestDeletedBranchLoadUser(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1}).(*git_model.DeletedBranch)
secondBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch) secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 2}).(*git_model.DeletedBranch)
branch := getDeletedBranch(t, firstBranch) branch := getDeletedBranch(t, firstBranch)
assert.Nil(t, branch.DeletedBy) assert.Nil(t, branch.DeletedBy)
@ -62,18 +64,18 @@ func TestRemoveDeletedBranch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1}).(*git_model.DeletedBranch)
err := RemoveDeletedBranchByID(repo.ID, 1) err := git_model.RemoveDeletedBranchByID(repo.ID, 1)
assert.NoError(t, err) assert.NoError(t, err)
unittest.AssertNotExistsBean(t, firstBranch) unittest.AssertNotExistsBean(t, firstBranch)
unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}) unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 2})
} }
func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch { func getDeletedBranch(t *testing.T, branch *git_model.DeletedBranch) *git_model.DeletedBranch {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
deletedBranch, err := GetDeletedBranchByID(repo.ID, branch.ID) deletedBranch, err := git_model.GetDeletedBranchByID(repo.ID, branch.ID)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, branch.ID, deletedBranch.ID) assert.Equal(t, branch.ID, deletedBranch.ID)
assert.Equal(t, branch.Name, deletedBranch.Name) assert.Equal(t, branch.Name, deletedBranch.Name)
@ -85,12 +87,12 @@ func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {
func TestFindRenamedBranch(t *testing.T) { func TestFindRenamedBranch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
branch, exist, err := FindRenamedBranch(1, "dev") branch, exist, err := git_model.FindRenamedBranch(1, "dev")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, true, exist) assert.Equal(t, true, exist)
assert.Equal(t, "master", branch.To) assert.Equal(t, "master", branch.To)
_, exist, err = FindRenamedBranch(1, "unknow") _, exist, err = git_model.FindRenamedBranch(1, "unknow")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, false, exist) assert.Equal(t, false, exist)
} }
@ -103,13 +105,13 @@ func TestRenameBranch(t *testing.T) {
ctx, committer, err := db.TxContext() ctx, committer, err := db.TxContext()
defer committer.Close() defer committer.Close()
assert.NoError(t, err) assert.NoError(t, err)
assert.NoError(t, UpdateProtectBranch(ctx, repo1, &ProtectedBranch{ assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{
RepoID: repo1.ID, RepoID: repo1.ID,
BranchName: "master", BranchName: "master",
}, WhitelistOptions{})) }, git_model.WhitelistOptions{}))
assert.NoError(t, committer.Commit()) assert.NoError(t, committer.Commit())
assert.NoError(t, RenameBranch(repo1, "master", "main", func(isDefault bool) error { assert.NoError(t, git_model.RenameBranch(repo1, "master", "main", func(isDefault bool) error {
_isDefault = isDefault _isDefault = isDefault
return nil return nil
})) }))
@ -118,18 +120,18 @@ func TestRenameBranch(t *testing.T) {
repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
assert.Equal(t, "main", repo1.DefaultBranch) assert.Equal(t, "main", repo1.DefaultBranch)
pull := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged pull := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) // merged
assert.Equal(t, "master", pull.BaseBranch) assert.Equal(t, "master", pull.BaseBranch)
pull = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open pull = unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) // open
assert.Equal(t, "main", pull.BaseBranch) assert.Equal(t, "main", pull.BaseBranch)
renamedBranch := unittest.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch) renamedBranch := unittest.AssertExistsAndLoadBean(t, &git_model.RenamedBranch{ID: 2}).(*git_model.RenamedBranch)
assert.Equal(t, "master", renamedBranch.From) assert.Equal(t, "master", renamedBranch.From)
assert.Equal(t, "main", renamedBranch.To) assert.Equal(t, "main", renamedBranch.To)
assert.Equal(t, int64(1), renamedBranch.RepoID) assert.Equal(t, int64(1), renamedBranch.RepoID)
unittest.AssertExistsAndLoadBean(t, &ProtectedBranch{ unittest.AssertExistsAndLoadBean(t, &git_model.ProtectedBranch{
RepoID: repo1.ID, RepoID: repo1.ID,
BranchName: "main", BranchName: "main",
}) })
@ -143,7 +145,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
// is actually on repo with ID 1. // is actually on repo with ID 1.
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
deletedBranch, err := GetDeletedBranchByID(repo2.ID, 1) deletedBranch, err := git_model.GetDeletedBranchByID(repo2.ID, 1)
// Expect no error, and the returned branch is nil. // Expect no error, and the returned branch is nil.
assert.NoError(t, err) assert.NoError(t, err)
@ -153,7 +155,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
// This should return the deletedBranch. // This should return the deletedBranch.
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
deletedBranch, err = GetDeletedBranchByID(repo1.ID, 1) deletedBranch, err = git_model.GetDeletedBranchByID(repo1.ID, 1)
// Expect no error, and the returned branch to be not nil. // Expect no error, and the returned branch to be not nil.
assert.NoError(t, err) assert.NoError(t, err)

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git
import ( import (
"context" "context"
@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -178,7 +179,7 @@ func GetCommitStatuses(repo *repo_model.Repository, sha string, opts *CommitStat
opts.Page = 1 opts.Page = 1
} }
if opts.PageSize <= 0 { if opts.PageSize <= 0 {
opts.Page = ItemsPerPage opts.Page = setting.ItemsPerPage
} }
countSession := listCommitStatusesStatement(repo, sha, opts) countSession := listCommitStatusesStatement(repo, sha, opts)
@ -353,3 +354,17 @@ func ParseCommitsWithStatus(oldCommits []*asymkey_model.SignCommit, repo *repo_m
func hashCommitStatusContext(context string) string { func hashCommitStatusContext(context string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(context))) return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
} }
// ConvertFromGitCommit converts git commits into SignCommitWithStatuses
func ConvertFromGitCommit(commits []*git.Commit, repo *repo_model.Repository) []*SignCommitWithStatuses {
return ParseCommitsWithStatus(
asymkey_model.ParseCommitsWithSignature(
user_model.ValidateCommitsWithEmails(commits),
repo.GetTrustModel(),
func(user *user_model.User) (bool, error) {
return repo_model.IsOwnerMemberCollaborator(repo, user.ID)
},
),
repo,
)
}

@ -2,12 +2,13 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git_test
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
@ -22,7 +23,7 @@ func TestGetCommitStatuses(t *testing.T) {
sha1 := "1234123412341234123412341234123412341234" sha1 := "1234123412341234123412341234123412341234"
statuses, maxResults, err := GetCommitStatuses(repo1, sha1, &CommitStatusOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 50}}) statuses, maxResults, err := git_model.GetCommitStatuses(repo1, sha1, &git_model.CommitStatusOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 50}})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, int(maxResults), 5) assert.Equal(t, int(maxResults), 5)
assert.Len(t, statuses, 5) assert.Len(t, statuses, 5)

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git
import ( import (
"context" "context"
@ -10,6 +10,7 @@ import (
"fmt" "fmt"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/lfs"
@ -19,6 +20,76 @@ import (
"xorm.io/builder" "xorm.io/builder"
) )
// ErrLFSLockNotExist represents a "LFSLockNotExist" kind of error.
type ErrLFSLockNotExist struct {
ID int64
RepoID int64
Path string
}
// IsErrLFSLockNotExist checks if an error is a ErrLFSLockNotExist.
func IsErrLFSLockNotExist(err error) bool {
_, ok := err.(ErrLFSLockNotExist)
return ok
}
func (err ErrLFSLockNotExist) Error() string {
return fmt.Sprintf("lfs lock does not exist [id: %d, rid: %d, path: %s]", err.ID, err.RepoID, err.Path)
}
// ErrLFSUnauthorizedAction represents a "LFSUnauthorizedAction" kind of error.
type ErrLFSUnauthorizedAction struct {
RepoID int64
UserName string
Mode perm.AccessMode
}
// IsErrLFSUnauthorizedAction checks if an error is a ErrLFSUnauthorizedAction.
func IsErrLFSUnauthorizedAction(err error) bool {
_, ok := err.(ErrLFSUnauthorizedAction)
return ok
}
func (err ErrLFSUnauthorizedAction) Error() string {
if err.Mode == perm.AccessModeWrite {
return fmt.Sprintf("User %s doesn't have write access for lfs lock [rid: %d]", err.UserName, err.RepoID)
}
return fmt.Sprintf("User %s doesn't have read access for lfs lock [rid: %d]", err.UserName, err.RepoID)
}
// ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error.
type ErrLFSLockAlreadyExist struct {
RepoID int64
Path string
}
// IsErrLFSLockAlreadyExist checks if an error is a ErrLFSLockAlreadyExist.
func IsErrLFSLockAlreadyExist(err error) bool {
_, ok := err.(ErrLFSLockAlreadyExist)
return ok
}
func (err ErrLFSLockAlreadyExist) Error() string {
return fmt.Sprintf("lfs lock already exists [rid: %d, path: %s]", err.RepoID, err.Path)
}
// ErrLFSFileLocked represents a "LFSFileLocked" kind of error.
type ErrLFSFileLocked struct {
RepoID int64
Path string
UserName string
}
// IsErrLFSFileLocked checks if an error is a ErrLFSFileLocked.
func IsErrLFSFileLocked(err error) bool {
_, ok := err.(ErrLFSFileLocked)
return ok
}
func (err ErrLFSFileLocked) Error() string {
return fmt.Sprintf("File is lfs locked [repo: %d, locked by: %s, path: %s]", err.RepoID, err.UserName, err.Path)
}
// LFSMetaObject stores metadata for LFS tracked files. // LFSMetaObject stores metadata for LFS tracked files.
type LFSMetaObject struct { type LFSMetaObject struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
@ -239,7 +310,7 @@ func CopyLFS(ctx context.Context, newRepo, oldRepo *repo_model.Repository) error
for _, v := range lfsObjects { for _, v := range lfsObjects {
v.ID = 0 v.ID = 0
v.RepositoryID = newRepo.ID v.RepositoryID = newRepo.ID
if _, err := db.GetEngine(ctx).Insert(v); err != nil { if err := db.Insert(ctx, v); err != nil {
return err return err
} }
} }

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git
import ( import (
"context" "context"

@ -0,0 +1,18 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git_test
import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
}

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git
import ( import (
"regexp" "regexp"

@ -2,11 +2,12 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package git_test
import ( import (
"testing" "testing"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -15,42 +16,42 @@ import (
func TestIsUserAllowed(t *testing.T) { func TestIsUserAllowed(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
pt := &ProtectedTag{} pt := &git_model.ProtectedTag{}
allowed, err := IsUserAllowedModifyTag(pt, 1) allowed, err := git_model.IsUserAllowedModifyTag(pt, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, allowed) assert.False(t, allowed)
pt = &ProtectedTag{ pt = &git_model.ProtectedTag{
AllowlistUserIDs: []int64{1}, AllowlistUserIDs: []int64{1},
} }
allowed, err = IsUserAllowedModifyTag(pt, 1) allowed, err = git_model.IsUserAllowedModifyTag(pt, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, allowed) assert.True(t, allowed)
allowed, err = IsUserAllowedModifyTag(pt, 2) allowed, err = git_model.IsUserAllowedModifyTag(pt, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, allowed) assert.False(t, allowed)
pt = &ProtectedTag{ pt = &git_model.ProtectedTag{
AllowlistTeamIDs: []int64{1}, AllowlistTeamIDs: []int64{1},
} }
allowed, err = IsUserAllowedModifyTag(pt, 1) allowed, err = git_model.IsUserAllowedModifyTag(pt, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, allowed) assert.False(t, allowed)
allowed, err = IsUserAllowedModifyTag(pt, 2) allowed, err = git_model.IsUserAllowedModifyTag(pt, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, allowed) assert.True(t, allowed)
pt = &ProtectedTag{ pt = &git_model.ProtectedTag{
AllowlistUserIDs: []int64{1}, AllowlistUserIDs: []int64{1},
AllowlistTeamIDs: []int64{1}, AllowlistTeamIDs: []int64{1},
} }
allowed, err = IsUserAllowedModifyTag(pt, 1) allowed, err = git_model.IsUserAllowedModifyTag(pt, 1)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, allowed) assert.True(t, allowed)
allowed, err = IsUserAllowedModifyTag(pt, 2) allowed, err = git_model.IsUserAllowedModifyTag(pt, 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, allowed) assert.True(t, allowed)
} }
@ -119,7 +120,7 @@ func TestIsUserAllowedToControlTag(t *testing.T) {
} }
t.Run("Glob", func(t *testing.T) { t.Run("Glob", func(t *testing.T) {
protectedTags := []*ProtectedTag{ protectedTags := []*git_model.ProtectedTag{
{ {
NamePattern: `*gitea`, NamePattern: `*gitea`,
AllowlistUserIDs: []int64{1}, AllowlistUserIDs: []int64{1},
@ -134,14 +135,14 @@ func TestIsUserAllowedToControlTag(t *testing.T) {
} }
for n, c := range cases { for n, c := range cases {
isAllowed, err := IsUserAllowedToControlTag(protectedTags, c.name, c.userid) isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, c.name, c.userid)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n) assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n)
} }
}) })
t.Run("Regex", func(t *testing.T) { t.Run("Regex", func(t *testing.T) {
protectedTags := []*ProtectedTag{ protectedTags := []*git_model.ProtectedTag{
{ {
NamePattern: `/gitea\z/`, NamePattern: `/gitea\z/`,
AllowlistUserIDs: []int64{1}, AllowlistUserIDs: []int64{1},
@ -156,7 +157,7 @@ func TestIsUserAllowedToControlTag(t *testing.T) {
} }
for n, c := range cases { for n, c := range cases {
isAllowed, err := IsUserAllowedToControlTag(protectedTags, c.name, c.userid) isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, c.name, c.userid)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n) assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n)
} }

@ -15,6 +15,7 @@ import (
"unicode/utf8" "unicode/utf8"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
project_model "code.gitea.io/gitea/models/project" project_model "code.gitea.io/gitea/models/project"
@ -271,11 +272,11 @@ type Comment struct {
RefIssue *Issue `xorm:"-"` RefIssue *Issue `xorm:"-"`
RefComment *Comment `xorm:"-"` RefComment *Comment `xorm:"-"`
Commits []*SignCommitWithStatuses `xorm:"-"` Commits []*git_model.SignCommitWithStatuses `xorm:"-"`
OldCommit string `xorm:"-"` OldCommit string `xorm:"-"`
NewCommit string `xorm:"-"` NewCommit string `xorm:"-"`
CommitsNum int64 `xorm:"-"` CommitsNum int64 `xorm:"-"`
IsForcePush bool `xorm:"-"` IsForcePush bool `xorm:"-"`
} }
func init() { func init() {
@ -761,7 +762,7 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) {
} }
defer closer.Close() defer closer.Close()
c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo) c.Commits = git_model.ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo)
c.CommitsNum = int64(len(c.Commits)) c.CommitsNum = int64(len(c.Commits))
} }

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models/db" "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/organization"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
@ -412,7 +413,7 @@ func DeleteTeam(t *organization.Team) error {
// update branch protections // update branch protections
{ {
protections := make([]*ProtectedBranch, 0, 10) protections := make([]*git_model.ProtectedBranch, 0, 10)
err := sess.In("repo_id", err := sess.In("repo_id",
builder.Select("id").From("repository").Where(builder.Eq{"owner_id": t.OrgID})). builder.Select("id").From("repository").Where(builder.Eq{"owner_id": t.OrgID})).
Find(&protections) Find(&protections)

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
pull_model "code.gitea.io/gitea/models/pull" pull_model "code.gitea.io/gitea/models/pull"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -78,9 +79,9 @@ type PullRequest struct {
HeadBranch string HeadBranch string
HeadCommitID string `xorm:"-"` HeadCommitID string `xorm:"-"`
BaseBranch string BaseBranch string
ProtectedBranch *ProtectedBranch `xorm:"-"` ProtectedBranch *git_model.ProtectedBranch `xorm:"-"`
MergeBase string `xorm:"VARCHAR(40)"` MergeBase string `xorm:"VARCHAR(40)"`
AllowMaintainerEdit bool `xorm:"NOT NULL DEFAULT false"` AllowMaintainerEdit bool `xorm:"NOT NULL DEFAULT false"`
HasMerged bool `xorm:"INDEX"` HasMerged bool `xorm:"INDEX"`
MergedCommitID string `xorm:"VARCHAR(40)"` MergedCommitID string `xorm:"VARCHAR(40)"`
@ -242,7 +243,7 @@ func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) {
return return
} }
} }
pr.ProtectedBranch, err = GetProtectedBranchBy(ctx, pr.BaseRepo.ID, pr.BaseBranch) pr.ProtectedBranch, err = git_model.GetProtectedBranchBy(ctx, pr.BaseRepo.ID, pr.BaseBranch)
} }
return return
} }

@ -15,6 +15,7 @@ import (
admin_model "code.gitea.io/gitea/models/admin" admin_model "code.gitea.io/gitea/models/admin"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
@ -34,9 +35,6 @@ import (
"xorm.io/builder" "xorm.io/builder"
) )
// ItemsPerPage maximum items per page in forks, watchers and stars of a repo
var ItemsPerPage = 40
// NewRepoContext creates a new repository context // NewRepoContext creates a new repository context
func NewRepoContext() { func NewRepoContext() {
unit.LoadUnitConfig() unit.LoadUnitConfig()
@ -284,16 +282,16 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
&Action{RepoID: repo.ID}, &Action{RepoID: repo.ID},
&repo_model.Collaboration{RepoID: repoID}, &repo_model.Collaboration{RepoID: repoID},
&Comment{RefRepoID: repoID}, &Comment{RefRepoID: repoID},
&CommitStatus{RepoID: repoID}, &git_model.CommitStatus{RepoID: repoID},
&DeletedBranch{RepoID: repoID}, &git_model.DeletedBranch{RepoID: repoID},
&webhook.HookTask{RepoID: repoID}, &webhook.HookTask{RepoID: repoID},
&LFSLock{RepoID: repoID}, &git_model.LFSLock{RepoID: repoID},
&repo_model.LanguageStat{RepoID: repoID}, &repo_model.LanguageStat{RepoID: repoID},
&issues_model.Milestone{RepoID: repoID}, &issues_model.Milestone{RepoID: repoID},
&repo_model.Mirror{RepoID: repoID}, &repo_model.Mirror{RepoID: repoID},
&Notification{RepoID: repoID}, &Notification{RepoID: repoID},
&ProtectedBranch{RepoID: repoID}, &git_model.ProtectedBranch{RepoID: repoID},
&ProtectedTag{RepoID: repoID}, &git_model.ProtectedTag{RepoID: repoID},
&repo_model.PushMirror{RepoID: repoID}, &repo_model.PushMirror{RepoID: repoID},
&Release{RepoID: repoID}, &Release{RepoID: repoID},
&repo_model.RepoIndexerStatus{RepoID: repoID}, &repo_model.RepoIndexerStatus{RepoID: repoID},
@ -357,14 +355,14 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
} }
// Remove LFS objects // Remove LFS objects
var lfsObjects []*LFSMetaObject var lfsObjects []*git_model.LFSMetaObject
if err = sess.Where("repository_id=?", repoID).Find(&lfsObjects); err != nil { if err = sess.Where("repository_id=?", repoID).Find(&lfsObjects); err != nil {
return err return err
} }
lfsPaths := make([]string, 0, len(lfsObjects)) lfsPaths := make([]string, 0, len(lfsObjects))
for _, v := range lfsObjects { for _, v := range lfsObjects {
count, err := db.CountByBean(ctx, &LFSMetaObject{Pointer: lfs.Pointer{Oid: v.Oid}}) count, err := db.CountByBean(ctx, &git_model.LFSMetaObject{Pointer: lfs.Pointer{Oid: v.Oid}})
if err != nil { if err != nil {
return err return err
} }
@ -375,7 +373,7 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
lfsPaths = append(lfsPaths, v.RelativePath()) lfsPaths = append(lfsPaths, v.RelativePath())
} }
if _, err := db.DeleteByBean(ctx, &LFSMetaObject{RepositoryID: repoID}); err != nil { if _, err := db.DeleteByBean(ctx, &git_model.LFSMetaObject{RepositoryID: repoID}); err != nil {
return err return err
} }

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -149,3 +150,23 @@ func ChangeCollaborationAccessMode(repo *Repository, uid int64, mode perm.Access
return committer.Commit() return committer.Commit()
} }
// IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
func IsOwnerMemberCollaborator(repo *Repository, userID int64) (bool, error) {
if repo.OwnerID == userID {
return true, nil
}
teamMember, err := db.GetEngine(db.DefaultContext).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
Where("team_repo.repo_id = ?", repo.ID).
And("team_unit.`type` = ?", unit.TypeCode).
And("team_user.uid = ?", userID).Table("team_user").Exist()
if err != nil {
return false, err
}
if teamMember {
return true, nil
}
return db.GetEngine(db.DefaultContext).Get(&Collaboration{RepoID: repo.ID, UserID: userID})
}

@ -10,11 +10,9 @@ import (
"fmt" "fmt"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"xorm.io/builder" "xorm.io/builder"
@ -120,23 +118,3 @@ func reconsiderWatches(ctx context.Context, repo *repo_model.Repository, uid int
// Remove all IssueWatches a user has subscribed to in the repository // Remove all IssueWatches a user has subscribed to in the repository
return removeIssueWatchersByRepoID(ctx, uid, repo.ID) return removeIssueWatchersByRepoID(ctx, uid, repo.ID)
} }
// IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
func IsOwnerMemberCollaborator(repo *repo_model.Repository, userID int64) (bool, error) {
if repo.OwnerID == userID {
return true, nil
}
teamMember, err := db.GetEngine(db.DefaultContext).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
Where("team_repo.repo_id = ?", repo.ID).
And("team_unit.`type` = ?", unit.TypeCode).
And("team_user.uid = ?", userID).Table("team_user").Exist(&organization.TeamUser{})
if err != nil {
return false, err
}
if teamMember {
return true, nil
}
return db.GetEngine(db.DefaultContext).Get(&repo_model.Collaboration{RepoID: repo.ID, UserID: userID})
}

@ -10,6 +10,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models/db" "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/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
@ -234,7 +235,7 @@ func IsOfficialReviewer(ctx context.Context, issue *Issue, reviewers ...*user_mo
} }
for _, reviewer := range reviewers { for _, reviewer := range reviewers {
official, err := isUserOfficialReviewer(ctx, pr.ProtectedBranch, reviewer) official, err := git_model.IsUserOfficialReviewerCtx(ctx, pr.ProtectedBranch, reviewer)
if official || err != nil { if official || err != nil {
return official, err return official, err
} }

@ -15,6 +15,7 @@ import (
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
@ -125,7 +126,7 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
{ {
const batchSize = 50 const batchSize = 50
for start := 0; ; start += batchSize { for start := 0; ; start += batchSize {
protections := make([]*ProtectedBranch, 0, batchSize) protections := make([]*git_model.ProtectedBranch, 0, batchSize)
// @perf: We can't filter on DB side by u.ID, as those IDs are serialized as JSON strings. // @perf: We can't filter on DB side by u.ID, as those IDs are serialized as JSON strings.
// We could filter down with `WHERE repo_id IN (reposWithPushPermission(u))`, // We could filter down with `WHERE repo_id IN (reposWithPushPermission(u))`,
// though that query will be quite complex and tricky to maintain (compare `getRepoAssignees()`). // though that query will be quite complex and tricky to maintain (compare `getRepoAssignees()`).

@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit" 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 // CanCommitToBranch returns true if repository is editable and user has proper access level
// and branch is not protected for push // and branch is not protected for push
func (r *Repository) CanCommitToBranch(ctx context.Context, doer *user_model.User) (CanCommitToBranchResults, error) { 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 { if err != nil {
return CanCommitToBranchResults{}, err return CanCommitToBranchResults{}, err
} }
@ -825,7 +826,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
if len(ref) == 0 { if len(ref) == 0 {
// maybe it's a renamed branch // maybe it's a renamed branch
return getRefNameFromPath(ctx, path, func(s string) bool { 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 { if err != nil {
log.Error("FindRenamedBranch", err) log.Error("FindRenamedBranch", err)
return false return false

@ -15,6 +15,7 @@ import (
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db" "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/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" 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 // 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 { if bp == nil {
var hasPerm bool var hasPerm bool
var canPush 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 return nil, err
} }
branch.UserCanPush = bp.CanUserPush(user.ID) 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 return branch, nil
} }
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection // 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) pushWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.WhitelistUserIDs)
if err != nil { if err != nil {
log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err) log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err)
@ -399,7 +400,7 @@ func ToOAuth2Application(app *auth.OAuth2Application) *api.OAuth2Application {
} }
// ToLFSLock convert a LFSLock to api.LFSLock // 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) u, err := user_model.GetUserByID(l.OwnerID)
if err != nil { if err != nil {
return nil return nil

@ -5,13 +5,13 @@
package convert package convert
import ( import (
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
) )
// ToCommitStatus converts models.CommitStatus to api.CommitStatus // ToCommitStatus converts git_model.CommitStatus to api.CommitStatus
func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus { func ToCommitStatus(status *git_model.CommitStatus) *api.CommitStatus {
apiStatus := &api.CommitStatus{ apiStatus := &api.CommitStatus{
Created: status.CreatedUnix.AsTime(), Created: status.CreatedUnix.AsTime(),
Updated: 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 // 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 { if len(statuses) == 0 {
return nil return nil
} }

@ -9,9 +9,9 @@ import (
"fmt" "fmt"
"strings" "strings"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "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) c.Verification = asymkey_model.ParseCommitWithSignature(c.Commit)
_ = asymkey_model.CalculateTrustStatus(c.Verification, repository.GetTrustModel(), func(user *user_model.User) (bool, error) { _ = 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) }, &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 { if err != nil {
log.Error("GetLatestCommitStatus: %v", err) log.Error("GetLatestCommitStatus: %v", err)
} else { } else {
c.Status = models.CalcCommitStatus(statuses) c.Status = git_model.CalcCommitStatus(statuses)
} }
} }
return nil return nil
@ -240,7 +240,7 @@ type Commit struct {
Commit *git.Commit Commit *git.Commit
User *user_model.User User *user_model.User
Verification *asymkey_model.CommitVerification Verification *asymkey_model.CommitVerification
Status *models.CommitStatus Status *git_model.CommitStatus
Flow int64 Flow int64
Row int Row int
Column int Column int

@ -113,7 +113,7 @@ func (s *ContentStore) Verify(pointer Pointer) (bool, error) {
return true, nil 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) { func ReadMetaObject(pointer Pointer) (io.ReadCloser, error) {
contentStore := NewContentStore() contentStore := NewContentStore()
return contentStore.Get(pointer) return contentStore.Get(pointer)

@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" 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) return fmt.Errorf("updateSize: %v", err)
} }
lfsSize, err := models.GetRepoLFSSize(ctx, repo.ID) lfsSize, err := git_model.GetRepoLFSSize(ctx, repo.ID)
if err != nil { if err != nil {
return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err) return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
} }

@ -16,6 +16,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "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) 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 fmt.Errorf("failed to copy LFS: %v", err)
} }
return nil return nil

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "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/organization"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -390,7 +391,7 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
defer content.Close() 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 { if err != nil {
log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, p, err) log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, p, err)
return err return err
@ -398,7 +399,7 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
if err := contentStore.Put(p, content); err != nil { if err := contentStore.Put(p, content); err != nil {
log.Error("Repo[%-v]: Error storing content for LFS meta object %-v: %v", repo, p, err) 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) log.Error("Repo[%-v]: Error removing LFS meta object %-v: %v", repo, p, err2)
} }
return err return err
@ -417,8 +418,8 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
var batch []lfs.Pointer var batch []lfs.Pointer
for pointerBlob := range pointerChan { for pointerBlob := range pointerChan {
meta, err := models.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid)
if err != nil && err != models.ErrLFSObjectNotExist { if err != nil && err != git_model.ErrLFSObjectNotExist {
log.Error("Repo[%-v]: Error querying LFS meta object %-v: %v", repo, pointerBlob.Pointer, err) log.Error("Repo[%-v]: Error querying LFS meta object %-v: %v", repo, pointerBlob.Pointer, err)
return err return err
} }
@ -437,7 +438,7 @@ func StoreMissingLfsObjectsInRepository(ctx context.Context, repo *repo_model.Re
if exist { if exist {
log.Trace("Repo[%-v]: LFS object %-v already present; creating meta object", repo, pointerBlob.Pointer) 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 { if err != nil {
log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, pointerBlob.Pointer, err) log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, pointerBlob.Pointer, err)
return err return err

@ -20,6 +20,9 @@ const (
RepoCreatingPublic = "public" RepoCreatingPublic = "public"
) )
// ItemsPerPage maximum items per page in forks, watchers and stars of a repo
const ItemsPerPage = 40
// Repository settings // Repository settings
var ( var (
Repository = struct { Repository = struct {

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -70,7 +71,7 @@ func GetBranch(ctx *context.APIContext) {
return return
} }
branchProtection, err := models.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branchName) branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branchName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return return
@ -206,7 +207,7 @@ func CreateBranch(ctx *context.APIContext) {
return return
} }
branchProtection, err := models.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branch.Name) branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branch.Name)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return return
@ -271,7 +272,7 @@ func ListBranches(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "GetCommit", err) ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return return
} }
branchProtection, err := models.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name) branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return return
@ -320,7 +321,7 @@ func GetBranchProtection(ctx *context.APIContext) {
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
bpName := ctx.Params(":name") bpName := ctx.Params(":name")
bp, err := models.GetProtectedBranchBy(ctx, repo.ID, bpName) bp, err := git_model.GetProtectedBranchBy(ctx, repo.ID, bpName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
return return
@ -356,7 +357,7 @@ func ListBranchProtections(ctx *context.APIContext) {
// "$ref": "#/responses/BranchProtectionList" // "$ref": "#/responses/BranchProtectionList"
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
bps, err := models.GetProtectedBranches(repo.ID) bps, err := git_model.GetProtectedBranches(repo.ID)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err)
return return
@ -412,7 +413,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
return return
} }
protectBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, form.BranchName) protectBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, form.BranchName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectBranchOfRepoByName", err) ctx.Error(http.StatusInternalServerError, "GetProtectBranchOfRepoByName", err)
return return
@ -484,7 +485,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
} }
} }
protectBranch = &models.ProtectedBranch{ protectBranch = &git_model.ProtectedBranch{
RepoID: ctx.Repo.Repository.ID, RepoID: ctx.Repo.Repository.ID,
BranchName: form.BranchName, BranchName: form.BranchName,
CanPush: form.EnablePush, CanPush: form.EnablePush,
@ -504,7 +505,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
BlockOnOutdatedBranch: form.BlockOnOutdatedBranch, BlockOnOutdatedBranch: form.BlockOnOutdatedBranch,
} }
err = models.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, models.WhitelistOptions{ err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
UserIDs: whitelistUsers, UserIDs: whitelistUsers,
TeamIDs: whitelistTeams, TeamIDs: whitelistTeams,
MergeUserIDs: mergeWhitelistUsers, MergeUserIDs: mergeWhitelistUsers,
@ -523,7 +524,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
} }
// Reload from db to get all whitelists // Reload from db to get all whitelists
bp, err := models.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, form.BranchName) bp, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, form.BranchName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
return return
@ -575,7 +576,7 @@ func EditBranchProtection(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.EditBranchProtectionOption) form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
bpName := ctx.Params(":name") bpName := ctx.Params(":name")
protectBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, bpName) protectBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, bpName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
return return
@ -739,7 +740,7 @@ func EditBranchProtection(ctx *context.APIContext) {
} }
} }
err = models.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, models.WhitelistOptions{ err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
UserIDs: whitelistUsers, UserIDs: whitelistUsers,
TeamIDs: whitelistTeams, TeamIDs: whitelistTeams,
MergeUserIDs: mergeWhitelistUsers, MergeUserIDs: mergeWhitelistUsers,
@ -758,7 +759,7 @@ func EditBranchProtection(ctx *context.APIContext) {
} }
// Reload from db to ensure get all whitelists // Reload from db to ensure get all whitelists
bp, err := models.GetProtectedBranchBy(ctx, repo.ID, bpName) bp, err := git_model.GetProtectedBranchBy(ctx, repo.ID, bpName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
return return
@ -802,7 +803,7 @@ func DeleteBranchProtection(ctx *context.APIContext) {
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
bpName := ctx.Params(":name") bpName := ctx.Params(":name")
bp, err := models.GetProtectedBranchBy(ctx, repo.ID, bpName) bp, err := git_model.GetProtectedBranchBy(ctx, repo.ID, bpName)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err) ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
return return
@ -812,7 +813,7 @@ func DeleteBranchProtection(ctx *context.APIContext) {
return return
} }
if err := models.DeleteProtectedBranch(ctx.Repo.Repository.ID, bp.ID); err != nil { if err := git_model.DeleteProtectedBranch(ctx.Repo.Repository.ID, bp.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err) ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err)
return return
} }

@ -15,6 +15,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
@ -173,10 +174,10 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
} }
// Now check if there is a meta object for this pointer // Now check if there is a meta object for this pointer
meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) meta, err := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid)
// If there isn't one just serve the data directly // If there isn't one just serve the data directly
if err == models.ErrLFSObjectNotExist { if err == git_model.ErrLFSObjectNotExist {
// Handle caching for the blob SHA (not the LFS object OID) // Handle caching for the blob SHA (not the LFS object OID)
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) { if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
return return

@ -8,7 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -56,7 +56,7 @@ func NewCommitStatus(ctx *context.APIContext) {
ctx.Error(http.StatusBadRequest, "sha not given", nil) ctx.Error(http.StatusBadRequest, "sha not given", nil)
return return
} }
status := &models.CommitStatus{ status := &git_model.CommitStatus{
State: api.CommitStatusState(form.State), State: api.CommitStatusState(form.State),
TargetURL: form.TargetURL, TargetURL: form.TargetURL,
Description: form.Description, Description: form.Description,
@ -188,7 +188,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
listOptions := utils.GetListOptions(ctx) listOptions := utils.GetListOptions(ctx)
statuses, maxResults, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{ statuses, maxResults, err := git_model.GetCommitStatuses(repo, sha, &git_model.CommitStatusOptions{
ListOptions: listOptions, ListOptions: listOptions,
SortType: ctx.FormTrim("sort"), SortType: ctx.FormTrim("sort"),
State: ctx.FormTrim("state"), State: ctx.FormTrim("state"),
@ -253,7 +253,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) {
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
statuses, count, err := models.GetLatestCommitStatus(ctx, repo.ID, sha, utils.GetListOptions(ctx)) statuses, count, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, utils.GetListOptions(ctx))
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s]: %v", repo.FullName(), sha, err)) ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s]: %v", repo.FullName(), sha, err))
return return

@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
git_model "code.gitea.io/gitea/models/git"
perm_model "code.gitea.io/gitea/models/perm" perm_model "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
@ -40,7 +41,7 @@ type preReceiveContext struct {
canWriteCode bool canWriteCode bool
checkedCanWriteCode bool checkedCanWriteCode bool
protectedTags []*models.ProtectedTag protectedTags []*git_model.ProtectedTag
gotProtectedTags bool gotProtectedTags bool
env []string env []string
@ -155,7 +156,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN
return return
} }
protectBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, branchName) protectBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, branchName)
if err != nil { if err != nil {
log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err) log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
@ -370,7 +371,7 @@ func preReceiveTag(ctx *preReceiveContext, oldCommitID, newCommitID, refFullName
if !ctx.gotProtectedTags { if !ctx.gotProtectedTags {
var err error var err error
ctx.protectedTags, err = models.GetProtectedTags(ctx.Repo.Repository.ID) ctx.protectedTags, err = git_model.GetProtectedTags(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
log.Error("Unable to get protected tags for %-v Error: %v", ctx.Repo.Repository, err) log.Error("Unable to get protected tags for %-v Error: %v", ctx.Repo.Repository, err)
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
@ -381,7 +382,7 @@ func preReceiveTag(ctx *preReceiveContext, oldCommitID, newCommitID, refFullName
ctx.gotProtectedTags = true ctx.gotProtectedTags = true
} }
isAllowed, err := models.IsUserAllowedToControlTag(ctx.protectedTags, tagName, ctx.opts.UserID) isAllowed, err := git_model.IsUserAllowedToControlTag(ctx.protectedTags, tagName, ctx.opts.UserID)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{ ctx.JSON(http.StatusInternalServerError, private.Response{
Err: err.Error(), Err: err.Error(),

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
@ -40,7 +41,7 @@ type Branch struct {
IsProtected bool IsProtected bool
IsDeleted bool IsDeleted bool
IsIncluded bool IsIncluded bool
DeletedBranch *models.DeletedBranch DeletedBranch *git_model.DeletedBranch
CommitsAhead int CommitsAhead int
CommitsBehind int CommitsBehind int
LatestPullRequest *models.PullRequest LatestPullRequest *models.PullRequest
@ -119,7 +120,7 @@ func RestoreBranchPost(ctx *context.Context) {
branchID := ctx.FormInt64("branch_id") branchID := ctx.FormInt64("branch_id")
branchName := ctx.FormString("name") branchName := ctx.FormString("name")
deletedBranch, err := models.GetDeletedBranchByID(ctx.Repo.Repository.ID, branchID) deletedBranch, err := git_model.GetDeletedBranchByID(ctx.Repo.Repository.ID, branchID)
if err != nil { if err != nil {
log.Error("GetDeletedBranchByID: %v", err) log.Error("GetDeletedBranchByID: %v", err)
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName)) ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
@ -184,7 +185,7 @@ func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, in
return nil, nil, 0 return nil, nil, 0
} }
protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID) protectedBranches, err := git_model.GetProtectedBranches(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.ServerError("GetProtectedBranches", err) ctx.ServerError("GetProtectedBranches", err)
return nil, nil, 0 return nil, nil, 0
@ -231,7 +232,7 @@ func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, in
return defaultBranchBranch, branches, totalNumOfBranches return defaultBranchBranch, branches, totalNumOfBranches
} }
func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, protectedBranches []*models.ProtectedBranch, func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, protectedBranches []*git_model.ProtectedBranch,
repoIDToRepo map[int64]*repo_model.Repository, repoIDToRepo map[int64]*repo_model.Repository,
repoIDToGitRepo map[int64]*git.Repository, repoIDToGitRepo map[int64]*git.Repository,
) *Branch { ) *Branch {
@ -326,7 +327,7 @@ func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, p
func getDeletedBranches(ctx *context.Context) ([]*Branch, error) { func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
branches := []*Branch{} branches := []*Branch{}
deletedBranches, err := models.GetDeletedBranches(ctx.Repo.Repository.ID) deletedBranches, err := git_model.GetDeletedBranches(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
return branches, err return branches, err
} }

@ -11,9 +11,10 @@ import (
"net/http" "net/http"
"strings" "strings"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db" "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" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
@ -75,7 +76,7 @@ func Commits(ctx *context.Context) {
ctx.ServerError("CommitsByRange", err) ctx.ServerError("CommitsByRange", err)
return return
} }
ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository) ctx.Data["Commits"] = git_model.ConvertFromGitCommit(commits, ctx.Repo.Repository)
ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name
@ -194,7 +195,7 @@ func SearchCommits(ctx *context.Context) {
return return
} }
ctx.Data["CommitCount"] = len(commits) ctx.Data["CommitCount"] = len(commits)
ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository) ctx.Data["Commits"] = git_model.ConvertFromGitCommit(commits, ctx.Repo.Repository)
ctx.Data["Keyword"] = query ctx.Data["Keyword"] = query
if all { if all {
@ -235,7 +236,7 @@ func FileHistory(ctx *context.Context) {
ctx.ServerError("CommitsByFileAndRange", err) ctx.ServerError("CommitsByFileAndRange", err)
return return
} }
ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository) ctx.Data["Commits"] = git_model.ConvertFromGitCommit(commits, ctx.Repo.Repository)
ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name
@ -335,12 +336,12 @@ func Diff(ctx *context.Context) {
ctx.Data["Commit"] = commit ctx.Data["Commit"] = commit
ctx.Data["Diff"] = diff ctx.Data["Diff"] = diff
statuses, _, err := models.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, commitID, db.ListOptions{}) statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, commitID, db.ListOptions{})
if err != nil { if err != nil {
log.Error("GetLatestCommitStatus: %v", err) log.Error("GetLatestCommitStatus: %v", err)
} }
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses) ctx.Data["CommitStatus"] = git_model.CalcCommitStatus(statuses)
ctx.Data["CommitStatuses"] = statuses ctx.Data["CommitStatuses"] = statuses
verification := asymkey_model.ParseCommitWithSignature(commit) verification := asymkey_model.ParseCommitWithSignature(commit)
@ -350,7 +351,7 @@ func Diff(ctx *context.Context) {
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0 ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
return models.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID) return repo_model.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID)
}, nil); err != nil { }, nil); err != nil {
ctx.ServerError("CalculateTrustStatus", err) ctx.ServerError("CalculateTrustStatus", err)
return return

@ -18,6 +18,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
@ -636,7 +637,7 @@ func PrepareCompareDiff(
return false return false
} }
commits := models.ConvertFromGitCommit(ci.CompareInfo.Commits, ci.HeadRepo) commits := git_model.ConvertFromGitCommit(ci.CompareInfo.Commits, ci.HeadRepo)
ctx.Data["Commits"] = commits ctx.Data["Commits"] = commits
ctx.Data["CommitCount"] = len(commits) ctx.Data["CommitCount"] = len(commits)

@ -9,7 +9,7 @@ import (
"path" "path"
"time" "time"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -43,7 +43,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified time.Time
pointer, _ := lfs.ReadPointer(dataRc) pointer, _ := lfs.ReadPointer(dataRc)
if pointer.IsValid() { if pointer.IsValid() {
meta, _ := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) meta, _ := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid)
if meta == nil { if meta == nil {
if err = dataRc.Close(); err != nil { if err = dataRc.Close(); err != nil {
log.Error("ServeBlobOrLFS: Close: %v", err) log.Error("ServeBlobOrLFS: Close: %v", err)

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
@ -253,9 +254,9 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
// This is where we handle all the errors thrown by files_service.CreateOrUpdateRepoFile // This is where we handle all the errors thrown by files_service.CreateOrUpdateRepoFile
if git.IsErrNotExist(err) { if git.IsErrNotExist(err) {
ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", ctx.Repo.TreePath), tplEditFile, &form) ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", ctx.Repo.TreePath), tplEditFile, &form)
} else if models.IsErrLFSFileLocked(err) { } else if git_model.IsErrLFSFileLocked(err) {
ctx.Data["Err_TreePath"] = true ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.upload_file_is_locked", err.(models.ErrLFSFileLocked).Path, err.(models.ErrLFSFileLocked).UserName), tplEditFile, &form) ctx.RenderWithErr(ctx.Tr("repo.editor.upload_file_is_locked", err.(git_model.ErrLFSFileLocked).Path, err.(git_model.ErrLFSFileLocked).UserName), tplEditFile, &form)
} else if models.IsErrFilenameInvalid(err) { } else if models.IsErrFilenameInvalid(err) {
ctx.Data["Err_TreePath"] = true ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_invalid", form.TreePath), tplEditFile, &form) ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_invalid", form.TreePath), tplEditFile, &form)
@ -660,9 +661,9 @@ func UploadFilePost(ctx *context.Context) {
Files: form.Files, Files: form.Files,
Signoff: form.Signoff, Signoff: form.Signoff,
}); err != nil { }); err != nil {
if models.IsErrLFSFileLocked(err) { if git_model.IsErrLFSFileLocked(err) {
ctx.Data["Err_TreePath"] = true ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.upload_file_is_locked", err.(models.ErrLFSFileLocked).Path, err.(models.ErrLFSFileLocked).UserName), tplUploadFile, &form) ctx.RenderWithErr(ctx.Tr("repo.editor.upload_file_is_locked", err.(git_model.ErrLFSFileLocked).Path, err.(git_model.ErrLFSFileLocked).UserName), tplUploadFile, &form)
} else if models.IsErrFilenameInvalid(err) { } else if models.IsErrFilenameInvalid(err) {
ctx.Data["Err_TreePath"] = true ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_invalid", form.TreePath), tplUploadFile, &form) ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_invalid", form.TreePath), tplUploadFile, &form)

@ -21,6 +21,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
@ -1536,7 +1537,7 @@ func ViewIssue(ctx *context.Context) {
if perm.CanWrite(unit.TypeCode) { if perm.CanWrite(unit.TypeCode) {
// Check if branch is not protected // Check if branch is not protected
if pull.HeadBranch != pull.HeadRepo.DefaultBranch { if pull.HeadBranch != pull.HeadRepo.DefaultBranch {
if protected, err := models.IsProtectedBranch(pull.HeadRepo.ID, pull.HeadBranch); err != nil { if protected, err := git_model.IsProtectedBranch(pull.HeadRepo.ID, pull.HeadBranch); err != nil {
log.Error("IsProtectedBranch: %v", err) log.Error("IsProtectedBranch: %v", err)
} else if !protected { } else if !protected {
canDelete = true canDelete = true
@ -1620,11 +1621,11 @@ func ViewIssue(ctx *context.Context) {
if ctx.Doer != nil { if ctx.Doer != nil {
showMergeInstructions = pull.ProtectedBranch.CanUserPush(ctx.Doer.ID) showMergeInstructions = pull.ProtectedBranch.CanUserPush(ctx.Doer.ID)
} }
ctx.Data["IsBlockedByApprovals"] = !pull.ProtectedBranch.HasEnoughApprovals(ctx, pull) ctx.Data["IsBlockedByApprovals"] = !models.HasEnoughApprovals(ctx, pull.ProtectedBranch, pull)
ctx.Data["IsBlockedByRejection"] = pull.ProtectedBranch.MergeBlockedByRejectedReview(ctx, pull) ctx.Data["IsBlockedByRejection"] = models.MergeBlockedByRejectedReview(ctx, pull.ProtectedBranch, pull)
ctx.Data["IsBlockedByOfficialReviewRequests"] = pull.ProtectedBranch.MergeBlockedByOfficialReviewRequests(ctx, pull) ctx.Data["IsBlockedByOfficialReviewRequests"] = models.MergeBlockedByOfficialReviewRequests(ctx, pull.ProtectedBranch, pull)
ctx.Data["IsBlockedByOutdatedBranch"] = pull.ProtectedBranch.MergeBlockedByOutdatedBranch(pull) ctx.Data["IsBlockedByOutdatedBranch"] = models.MergeBlockedByOutdatedBranch(pull.ProtectedBranch, pull)
ctx.Data["GrantedApprovals"] = pull.ProtectedBranch.GetGrantedApprovalsCount(ctx, pull) ctx.Data["GrantedApprovals"] = models.GetGrantedApprovalsCount(ctx, pull.ProtectedBranch, pull)
ctx.Data["RequireSigned"] = pull.ProtectedBranch.RequireSignedCommits ctx.Data["RequireSigned"] = pull.ProtectedBranch.RequireSignedCommits
ctx.Data["ChangedProtectedFiles"] = pull.ChangedProtectedFiles ctx.Data["ChangedProtectedFiles"] = pull.ChangedProtectedFiles
ctx.Data["IsBlockedByChangedProtectedFiles"] = len(pull.ChangedProtectedFiles) != 0 ctx.Data["IsBlockedByChangedProtectedFiles"] = len(pull.ChangedProtectedFiles) != 0

@ -15,7 +15,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -48,7 +48,7 @@ func LFSFiles(ctx *context.Context) {
if page <= 1 { if page <= 1 {
page = 1 page = 1
} }
total, err := models.CountLFSMetaObjects(ctx.Repo.Repository.ID) total, err := git_model.CountLFSMetaObjects(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.ServerError("LFSFiles", err) ctx.ServerError("LFSFiles", err)
return return
@ -58,7 +58,7 @@ func LFSFiles(ctx *context.Context) {
pager := context.NewPagination(int(total), setting.UI.ExplorePagingNum, page, 5) pager := context.NewPagination(int(total), setting.UI.ExplorePagingNum, page, 5)
ctx.Data["Title"] = ctx.Tr("repo.settings.lfs") ctx.Data["Title"] = ctx.Tr("repo.settings.lfs")
ctx.Data["PageIsSettingsLFS"] = true ctx.Data["PageIsSettingsLFS"] = true
lfsMetaObjects, err := models.GetLFSMetaObjects(ctx.Repo.Repository.ID, pager.Paginater.Current(), setting.UI.ExplorePagingNum) lfsMetaObjects, err := git_model.GetLFSMetaObjects(ctx.Repo.Repository.ID, pager.Paginater.Current(), setting.UI.ExplorePagingNum)
if err != nil { if err != nil {
ctx.ServerError("LFSFiles", err) ctx.ServerError("LFSFiles", err)
return return
@ -80,7 +80,7 @@ func LFSLocks(ctx *context.Context) {
if page <= 1 { if page <= 1 {
page = 1 page = 1
} }
total, err := models.CountLFSLockByRepoID(ctx.Repo.Repository.ID) total, err := git_model.CountLFSLockByRepoID(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.ServerError("LFSLocks", err) ctx.ServerError("LFSLocks", err)
return return
@ -90,7 +90,7 @@ func LFSLocks(ctx *context.Context) {
pager := context.NewPagination(int(total), setting.UI.ExplorePagingNum, page, 5) pager := context.NewPagination(int(total), setting.UI.ExplorePagingNum, page, 5)
ctx.Data["Title"] = ctx.Tr("repo.settings.lfs_locks") ctx.Data["Title"] = ctx.Tr("repo.settings.lfs_locks")
ctx.Data["PageIsSettingsLFS"] = true ctx.Data["PageIsSettingsLFS"] = true
lfsLocks, err := models.GetLFSLockByRepoID(ctx.Repo.Repository.ID, pager.Paginater.Current(), setting.UI.ExplorePagingNum) lfsLocks, err := git_model.GetLFSLockByRepoID(ctx.Repo.Repository.ID, pager.Paginater.Current(), setting.UI.ExplorePagingNum)
if err != nil { if err != nil {
ctx.ServerError("LFSLocks", err) ctx.ServerError("LFSLocks", err)
return return
@ -216,12 +216,12 @@ func LFSLockFile(ctx *context.Context) {
return return
} }
_, err := models.CreateLFSLock(ctx.Repo.Repository, &models.LFSLock{ _, err := git_model.CreateLFSLock(ctx.Repo.Repository, &git_model.LFSLock{
Path: lockPath, Path: lockPath,
OwnerID: ctx.Doer.ID, OwnerID: ctx.Doer.ID,
}) })
if err != nil { if err != nil {
if models.IsErrLFSLockAlreadyExist(err) { if git_model.IsErrLFSLockAlreadyExist(err) {
ctx.Flash.Error(ctx.Tr("repo.settings.lfs_lock_already_exists", originalPath)) ctx.Flash.Error(ctx.Tr("repo.settings.lfs_lock_already_exists", originalPath))
ctx.Redirect(ctx.Repo.RepoLink + "/settings/lfs/locks") ctx.Redirect(ctx.Repo.RepoLink + "/settings/lfs/locks")
return return
@ -238,7 +238,7 @@ func LFSUnlock(ctx *context.Context) {
ctx.NotFound("LFSUnlock", nil) ctx.NotFound("LFSUnlock", nil)
return return
} }
_, err := models.DeleteLFSLockByID(ctx.ParamsInt64("lid"), ctx.Repo.Repository, ctx.Doer, true) _, err := git_model.DeleteLFSLockByID(ctx.ParamsInt64("lid"), ctx.Repo.Repository, ctx.Doer, true)
if err != nil { if err != nil {
ctx.ServerError("LFSUnlock", err) ctx.ServerError("LFSUnlock", err)
return return
@ -263,9 +263,9 @@ func LFSFileGet(ctx *context.Context) {
ctx.Data["Title"] = oid ctx.Data["Title"] = oid
ctx.Data["PageIsSettingsLFS"] = true ctx.Data["PageIsSettingsLFS"] = true
meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, oid) meta, err := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, oid)
if err != nil { if err != nil {
if err == models.ErrLFSObjectNotExist { if err == git_model.ErrLFSObjectNotExist {
ctx.NotFound("LFSFileGet", nil) ctx.NotFound("LFSFileGet", nil)
return return
} }
@ -357,7 +357,7 @@ func LFSDelete(ctx *context.Context) {
return return
} }
count, err := models.RemoveLFSMetaObjectByOid(ctx.Repo.Repository.ID, oid) count, err := git_model.RemoveLFSMetaObjectByOid(ctx.Repo.Repository.ID, oid)
if err != nil { if err != nil {
ctx.ServerError("LFSDelete", err) ctx.ServerError("LFSDelete", err)
return return
@ -456,8 +456,8 @@ func LFSPointerFiles(ctx *context.Context) {
Size: pointerBlob.Size, Size: pointerBlob.Size,
} }
if _, err := models.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid); err != nil { if _, err := git_model.GetLFSMetaObjectByOid(repo.ID, pointerBlob.Oid); err != nil {
if err != models.ErrLFSObjectNotExist { if err != git_model.ErrLFSObjectNotExist {
return err return err
} }
} else { } else {
@ -474,12 +474,12 @@ func LFSPointerFiles(ctx *context.Context) {
// Can we fix? // Can we fix?
// OK well that's "simple" // OK well that's "simple"
// - we need to check whether current user has access to a repo that has access to the file // - we need to check whether current user has access to a repo that has access to the file
result.Associatable, err = models.LFSObjectAccessible(ctx.Doer, pointerBlob.Oid) result.Associatable, err = git_model.LFSObjectAccessible(ctx.Doer, pointerBlob.Oid)
if err != nil { if err != nil {
return err return err
} }
if !result.Associatable { if !result.Associatable {
associated, err := models.LFSObjectIsAssociated(pointerBlob.Oid) associated, err := git_model.LFSObjectIsAssociated(pointerBlob.Oid)
if err != nil { if err != nil {
return err return err
} }
@ -532,7 +532,7 @@ func LFSAutoAssociate(ctx *context.Context) {
return return
} }
oids := ctx.FormStrings("oid") oids := ctx.FormStrings("oid")
metas := make([]*models.LFSMetaObject, len(oids)) metas := make([]*git_model.LFSMetaObject, len(oids))
for i, oid := range oids { for i, oid := range oids {
idx := strings.IndexRune(oid, ' ') idx := strings.IndexRune(oid, ' ')
if idx < 0 || idx+1 > len(oid) { if idx < 0 || idx+1 > len(oid) {
@ -540,7 +540,7 @@ func LFSAutoAssociate(ctx *context.Context) {
return return
} }
var err error var err error
metas[i] = &models.LFSMetaObject{} metas[i] = &git_model.LFSMetaObject{}
metas[i].Size, err = strconv.ParseInt(oid[idx+1:], 10, 64) metas[i].Size, err = strconv.ParseInt(oid[idx+1:], 10, 64)
if err != nil { if err != nil {
ctx.ServerError("LFSAutoAssociate", fmt.Errorf("illegal oid input: %s %v", oid, err)) ctx.ServerError("LFSAutoAssociate", fmt.Errorf("illegal oid input: %s %v", oid, err))
@ -549,7 +549,7 @@ func LFSAutoAssociate(ctx *context.Context) {
metas[i].Oid = oid[:idx] metas[i].Oid = oid[:idx]
// metas[i].RepositoryID = ctx.Repo.Repository.ID // metas[i].RepositoryID = ctx.Repo.Repository.ID
} }
if err := models.LFSAutoAssociate(metas, ctx.Doer, ctx.Repo.Repository.ID); err != nil { if err := git_model.LFSAutoAssociate(metas, ctx.Doer, ctx.Repo.Repository.ID); err != nil {
ctx.ServerError("LFSAutoAssociate", err) ctx.ServerError("LFSAutoAssociate", err)
return return
} }

@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "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/organization"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
pull_model "code.gitea.io/gitea/models/pull" pull_model "code.gitea.io/gitea/models/pull"
@ -379,14 +380,14 @@ func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.C
if len(compareInfo.Commits) != 0 { if len(compareInfo.Commits) != 0 {
sha := compareInfo.Commits[0].ID.String() sha := compareInfo.Commits[0].ID.String()
commitStatuses, _, err := models.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, sha, db.ListOptions{}) commitStatuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, sha, db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLatestCommitStatus", err) ctx.ServerError("GetLatestCommitStatus", err)
return nil return nil
} }
if len(commitStatuses) != 0 { if len(commitStatuses) != 0 {
ctx.Data["LatestCommitStatuses"] = commitStatuses ctx.Data["LatestCommitStatuses"] = commitStatuses
ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(commitStatuses) ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(commitStatuses)
} }
} }
@ -440,14 +441,14 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitRefName()), err) ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitRefName()), err)
return nil return nil
} }
commitStatuses, _, err := models.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}) commitStatuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLatestCommitStatus", err) ctx.ServerError("GetLatestCommitStatus", err)
return nil return nil
} }
if len(commitStatuses) > 0 { if len(commitStatuses) > 0 {
ctx.Data["LatestCommitStatuses"] = commitStatuses ctx.Data["LatestCommitStatuses"] = commitStatuses
ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(commitStatuses) ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(commitStatuses)
} }
compareInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(), compareInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(),
@ -530,14 +531,14 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
return nil return nil
} }
commitStatuses, _, err := models.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{}) commitStatuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLatestCommitStatus", err) ctx.ServerError("GetLatestCommitStatus", err)
return nil return nil
} }
if len(commitStatuses) > 0 { if len(commitStatuses) > 0 {
ctx.Data["LatestCommitStatuses"] = commitStatuses ctx.Data["LatestCommitStatuses"] = commitStatuses
ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(commitStatuses) ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(commitStatuses)
} }
if pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck { if pull.ProtectedBranch != nil && pull.ProtectedBranch.EnableStatusCheck {
@ -629,7 +630,7 @@ func ViewPullCommits(ctx *context.Context) {
ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name
commits := models.ConvertFromGitCommit(prInfo.Commits, ctx.Repo.Repository) commits := git_model.ConvertFromGitCommit(prInfo.Commits, ctx.Repo.Repository)
ctx.Data["Commits"] = commits ctx.Data["Commits"] = commits
ctx.Data["CommitCount"] = len(commits) ctx.Data["CommitCount"] = len(commits)

@ -10,7 +10,7 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
@ -32,7 +32,7 @@ func ProtectedBranch(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsBranches"] = true ctx.Data["PageIsSettingsBranches"] = true
protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID) protectedBranches, err := git_model.GetProtectedBranches(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.ServerError("GetProtectedBranches", err) ctx.ServerError("GetProtectedBranches", err)
return return
@ -111,7 +111,7 @@ func SettingsProtectedBranch(c *context.Context) {
c.Data["Title"] = c.Tr("repo.settings.protected_branch") + " - " + branch c.Data["Title"] = c.Tr("repo.settings.protected_branch") + " - " + branch
c.Data["PageIsSettingsBranches"] = true c.Data["PageIsSettingsBranches"] = true
protectBranch, err := models.GetProtectedBranchBy(c, c.Repo.Repository.ID, branch) protectBranch, err := git_model.GetProtectedBranchBy(c, c.Repo.Repository.ID, branch)
if err != nil { if err != nil {
if !git.IsErrBranchNotExist(err) { if !git.IsErrBranchNotExist(err) {
c.ServerError("GetProtectBranchOfRepoByName", err) c.ServerError("GetProtectBranchOfRepoByName", err)
@ -121,7 +121,7 @@ func SettingsProtectedBranch(c *context.Context) {
if protectBranch == nil { if protectBranch == nil {
// No options found, create defaults. // No options found, create defaults.
protectBranch = &models.ProtectedBranch{ protectBranch = &git_model.ProtectedBranch{
BranchName: branch, BranchName: branch,
} }
} }
@ -135,7 +135,7 @@ func SettingsProtectedBranch(c *context.Context) {
c.Data["whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.WhitelistUserIDs), ",") c.Data["whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.WhitelistUserIDs), ",")
c.Data["merge_whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.MergeWhitelistUserIDs), ",") c.Data["merge_whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.MergeWhitelistUserIDs), ",")
c.Data["approvals_whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.ApprovalsWhitelistUserIDs), ",") c.Data["approvals_whitelist_users"] = strings.Join(base.Int64sToStrings(protectBranch.ApprovalsWhitelistUserIDs), ",")
contexts, _ := models.FindRepoRecentCommitStatusContexts(c.Repo.Repository.ID, 7*24*time.Hour) // Find last week status check contexts contexts, _ := git_model.FindRepoRecentCommitStatusContexts(c.Repo.Repository.ID, 7*24*time.Hour) // Find last week status check contexts
for _, ctx := range protectBranch.StatusCheckContexts { for _, ctx := range protectBranch.StatusCheckContexts {
var found bool var found bool
for i := range contexts { for i := range contexts {
@ -184,7 +184,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
return return
} }
protectBranch, err := models.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branch) protectBranch, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branch)
if err != nil { if err != nil {
if !git.IsErrBranchNotExist(err) { if !git.IsErrBranchNotExist(err) {
ctx.ServerError("GetProtectBranchOfRepoByName", err) ctx.ServerError("GetProtectBranchOfRepoByName", err)
@ -195,7 +195,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
if f.Protected { if f.Protected {
if protectBranch == nil { if protectBranch == nil {
// No options found, create defaults. // No options found, create defaults.
protectBranch = &models.ProtectedBranch{ protectBranch = &git_model.ProtectedBranch{
RepoID: ctx.Repo.Repository.ID, RepoID: ctx.Repo.Repository.ID,
BranchName: branch, BranchName: branch,
} }
@ -262,7 +262,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
protectBranch.UnprotectedFilePatterns = f.UnprotectedFilePatterns protectBranch.UnprotectedFilePatterns = f.UnprotectedFilePatterns
protectBranch.BlockOnOutdatedBranch = f.BlockOnOutdatedBranch protectBranch.BlockOnOutdatedBranch = f.BlockOnOutdatedBranch
err = models.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, models.WhitelistOptions{ err = git_model.UpdateProtectBranch(ctx, ctx.Repo.Repository, protectBranch, git_model.WhitelistOptions{
UserIDs: whitelistUsers, UserIDs: whitelistUsers,
TeamIDs: whitelistTeams, TeamIDs: whitelistTeams,
MergeUserIDs: mergeWhitelistUsers, MergeUserIDs: mergeWhitelistUsers,
@ -282,7 +282,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, util.PathEscapeSegments(branch))) ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, util.PathEscapeSegments(branch)))
} else { } else {
if protectBranch != nil { if protectBranch != nil {
if err := models.DeleteProtectedBranch(ctx.Repo.Repository.ID, protectBranch.ID); err != nil { if err := git_model.DeleteProtectedBranch(ctx.Repo.Repository.ID, protectBranch.ID); err != nil {
ctx.ServerError("DeleteProtectedBranch", err) ctx.ServerError("DeleteProtectedBranch", err)
return return
} }

@ -9,7 +9,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
@ -43,7 +43,7 @@ func NewProtectedTagPost(ctx *context.Context) {
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
form := web.GetForm(ctx).(*forms.ProtectTagForm) form := web.GetForm(ctx).(*forms.ProtectTagForm)
pt := &models.ProtectedTag{ pt := &git_model.ProtectedTag{
RepoID: repo.ID, RepoID: repo.ID,
NamePattern: strings.TrimSpace(form.NamePattern), NamePattern: strings.TrimSpace(form.NamePattern),
} }
@ -55,7 +55,7 @@ func NewProtectedTagPost(ctx *context.Context) {
pt.AllowlistTeamIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistTeams, ",")) pt.AllowlistTeamIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistTeams, ","))
} }
if err := models.InsertProtectedTag(pt); err != nil { if err := git_model.InsertProtectedTag(pt); err != nil {
ctx.ServerError("InsertProtectedTag", err) ctx.ServerError("InsertProtectedTag", err)
return return
} }
@ -108,7 +108,7 @@ func EditProtectedTagPost(ctx *context.Context) {
pt.AllowlistUserIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistUsers, ",")) pt.AllowlistUserIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistUsers, ","))
pt.AllowlistTeamIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistTeams, ",")) pt.AllowlistTeamIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistTeams, ","))
if err := models.UpdateProtectedTag(pt); err != nil { if err := git_model.UpdateProtectedTag(pt); err != nil {
ctx.ServerError("UpdateProtectedTag", err) ctx.ServerError("UpdateProtectedTag", err)
return return
} }
@ -124,7 +124,7 @@ func DeleteProtectedTagPost(ctx *context.Context) {
return return
} }
if err := models.DeleteProtectedTag(pt); err != nil { if err := git_model.DeleteProtectedTag(pt); err != nil {
ctx.ServerError("DeleteProtectedTag", err) ctx.ServerError("DeleteProtectedTag", err)
return return
} }
@ -137,7 +137,7 @@ func setTagsContext(ctx *context.Context) error {
ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsTags"] = true ctx.Data["PageIsSettingsTags"] = true
protectedTags, err := models.GetProtectedTags(ctx.Repo.Repository.ID) protectedTags, err := git_model.GetProtectedTags(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.ServerError("GetProtectedTags", err) ctx.ServerError("GetProtectedTags", err)
return err return err
@ -163,13 +163,13 @@ func setTagsContext(ctx *context.Context) error {
return nil return nil
} }
func selectProtectedTagByContext(ctx *context.Context) *models.ProtectedTag { func selectProtectedTagByContext(ctx *context.Context) *git_model.ProtectedTag {
id := ctx.FormInt64("id") id := ctx.FormInt64("id")
if id == 0 { if id == 0 {
id = ctx.ParamsInt64(":id") id = ctx.ParamsInt64(":id")
} }
tag, err := models.GetProtectedTagByID(id) tag, err := git_model.GetProtectedTagByID(id)
if err != nil { if err != nil {
ctx.ServerError("GetProtectedTagByID", err) ctx.ServerError("GetProtectedTagByID", err)
return nil return nil

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit" unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
@ -294,8 +295,8 @@ func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelin
if isTextFile && setting.LFS.StartServer { if isTextFile && setting.LFS.StartServer {
pointer, _ := lfs.ReadPointerFromBuffer(buf) pointer, _ := lfs.ReadPointerFromBuffer(buf)
if pointer.IsValid() { if pointer.IsValid() {
meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) meta, err := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid)
if err != nil && err != models.ErrLFSObjectNotExist { if err != nil && err != git_model.ErrLFSObjectNotExist {
ctx.ServerError("GetLFSMetaObject", err) ctx.ServerError("GetLFSMetaObject", err)
return return
} }
@ -417,8 +418,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
if isTextFile && setting.LFS.StartServer { if isTextFile && setting.LFS.StartServer {
pointer, _ := lfs.ReadPointerFromBuffer(buf) pointer, _ := lfs.ReadPointerFromBuffer(buf)
if pointer.IsValid() { if pointer.IsValid() {
meta, err := models.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid) meta, err := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid)
if err != nil && err != models.ErrLFSObjectNotExist { if err != nil && err != git_model.ErrLFSObjectNotExist {
ctx.ServerError("GetLFSMetaObject", err) ctx.ServerError("GetLFSMetaObject", err)
return return
} }
@ -466,7 +467,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
ctx.Data["IsTextSource"] = isTextFile || isDisplayingSource ctx.Data["IsTextSource"] = isTextFile || isDisplayingSource
// Check LFS Lock // Check LFS Lock
lfsLock, err := models.GetTreePathLock(ctx.Repo.Repository.ID, ctx.Repo.TreePath) lfsLock, err := git_model.GetTreePathLock(ctx.Repo.Repository.ID, ctx.Repo.TreePath)
ctx.Data["LFSLock"] = lfsLock ctx.Data["LFSLock"] = lfsLock
if err != nil { if err != nil {
ctx.ServerError("GetTreePathLock", err) ctx.ServerError("GetTreePathLock", err)
@ -842,7 +843,7 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
verification := asymkey_model.ParseCommitWithSignature(latestCommit) verification := asymkey_model.ParseCommitWithSignature(latestCommit)
if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) { if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
return models.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID) return repo_model.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID)
}, nil); err != nil { }, nil); err != nil {
ctx.ServerError("CalculateTrustStatus", err) ctx.ServerError("CalculateTrustStatus", err)
return nil return nil
@ -851,12 +852,12 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(latestCommit) ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(latestCommit)
} }
statuses, _, err := models.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), db.ListOptions{}) statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), db.ListOptions{})
if err != nil { if err != nil {
log.Error("GetLatestCommitStatus: %v", err) log.Error("GetLatestCommitStatus: %v", err)
} }
ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses) ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses)
ctx.Data["LatestCommitStatuses"] = statuses ctx.Data["LatestCommitStatuses"] = statuses
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
@ -991,12 +992,12 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOp
if page <= 0 { if page <= 0 {
page = 1 page = 1
} }
pager := context.NewPagination(total, models.ItemsPerPage, page, 5) pager := context.NewPagination(total, setting.ItemsPerPage, page, 5)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
items, err := getter(db.ListOptions{ items, err := getter(db.ListOptions{
Page: pager.Paginater.Current(), Page: pager.Paginater.Current(),
PageSize: models.ItemsPerPage, PageSize: setting.ItemsPerPage,
}) })
if err != nil { if err != nil {
ctx.ServerError("getter", err) ctx.ServerError("getter", err)
@ -1037,12 +1038,12 @@ func Forks(ctx *context.Context) {
page = 1 page = 1
} }
pager := context.NewPagination(ctx.Repo.Repository.NumForks, models.ItemsPerPage, page, 5) pager := context.NewPagination(ctx.Repo.Repository.NumForks, setting.ItemsPerPage, page, 5)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
forks, err := repo_model.GetForks(ctx.Repo.Repository, db.ListOptions{ forks, err := repo_model.GetForks(ctx.Repo.Repository, db.ListOptions{
Page: pager.Paginater.Current(), Page: pager.Paginater.Current(),
PageSize: models.ItemsPerPage, PageSize: setting.ItemsPerPage,
}) })
if err != nil { if err != nil {
ctx.ServerError("GetForks", err) ctx.ServerError("GetForks", err)

@ -16,6 +16,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
@ -350,7 +351,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.ServerError("CommitsByFileAndRangeNoFollow", err) ctx.ServerError("CommitsByFileAndRangeNoFollow", err)
return nil, nil return nil, nil
} }
ctx.Data["Commits"] = models.ConvertFromGitCommit(commitsHistory, ctx.Repo.Repository) ctx.Data["Commits"] = git_model.ConvertFromGitCommit(commitsHistory, ctx.Repo.Repository)
pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5) pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)

@ -13,6 +13,7 @@ import (
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -310,14 +311,14 @@ Loop:
return false, "", nil, &ErrWontSign{twofa} return false, "", nil, &ErrWontSign{twofa}
} }
case approved: case approved:
protectedBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, pr.BaseBranch) protectedBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, pr.BaseBranch)
if err != nil { if err != nil {
return false, "", nil, err return false, "", nil, err
} }
if protectedBranch == nil { if protectedBranch == nil {
return false, "", nil, &ErrWontSign{approved} return false, "", nil, &ErrWontSign{approved}
} }
if protectedBranch.GetGrantedApprovalsCount(ctx, pr) < 1 { if models.GetGrantedApprovalsCount(ctx, protectedBranch, pr) < 1 {
return false, "", nil, &ErrWontSign{approved} return false, "", nil, &ErrWontSign{approved}
} }
case baseSigned: case baseSigned:

@ -9,6 +9,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -109,7 +110,7 @@ func registerDeletedBranchesCleanup() {
OlderThan: 24 * time.Hour, OlderThan: 24 * time.Hour,
}, func(ctx context.Context, _ *user_model.User, config Config) error { }, func(ctx context.Context, _ *user_model.User, config Config) error {
realConfig := config.(*OlderThanConfig) realConfig := config.(*OlderThanConfig)
models.RemoveOldDeletedBranches(ctx, realConfig.OlderThan) git_model.RemoveOldDeletedBranches(ctx, realConfig.OlderThan)
return nil return nil
}) })
} }

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
pull_model "code.gitea.io/gitea/models/pull" pull_model "code.gitea.io/gitea/models/pull"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/analyze" "code.gitea.io/gitea/modules/analyze"
@ -1219,7 +1220,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio
} else if curFileLFSPrefix && strings.HasPrefix(line[1:], lfs.MetaFileOidPrefix) { } else if curFileLFSPrefix && strings.HasPrefix(line[1:], lfs.MetaFileOidPrefix) {
oid := strings.TrimPrefix(line[1:], lfs.MetaFileOidPrefix) oid := strings.TrimPrefix(line[1:], lfs.MetaFileOidPrefix)
if len(oid) == 64 { if len(oid) == 64 {
m := &models.LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}} m := &git_model.LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}
count, err := db.CountByBean(db.DefaultContext, m) count, err := db.CountByBean(db.DefaultContext, m)
if err == nil && count > 0 { if err == nil && count > 0 {

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

@ -18,7 +18,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
@ -197,8 +197,8 @@ func BatchHandler(ctx *context.Context) {
return return
} }
meta, err := models.GetLFSMetaObjectByOid(repository.ID, p.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repository.ID, p.Oid)
if err != nil && err != models.ErrLFSObjectNotExist { 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) log.Error("Unable to get LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
writeStatus(ctx, http.StatusInternalServerError) writeStatus(ctx, http.StatusInternalServerError)
return return
@ -223,14 +223,14 @@ func BatchHandler(ctx *context.Context) {
} }
if exists && meta == nil { if exists && meta == nil {
accessible, err := models.LFSObjectAccessible(ctx.Doer, p.Oid) accessible, err := git_model.LFSObjectAccessible(ctx.Doer, p.Oid)
if err != nil { if err != nil {
log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err) log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err)
writeStatus(ctx, http.StatusInternalServerError) writeStatus(ctx, http.StatusInternalServerError)
return return
} }
if accessible { 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 { if err != nil {
log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err) log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
writeStatus(ctx, http.StatusInternalServerError) writeStatus(ctx, http.StatusInternalServerError)
@ -297,7 +297,7 @@ func UploadHandler(ctx *context.Context) {
uploadOrVerify := func() error { uploadOrVerify := func() error {
if exists { if exists {
accessible, err := models.LFSObjectAccessible(ctx.Doer, p.Oid) accessible, err := git_model.LFSObjectAccessible(ctx.Doer, p.Oid)
if err != nil { if err != nil {
log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err) log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err)
return 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) log.Error("Error putting LFS MetaObject [%s] into content store. Error: %v", p.Oid, err)
return 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 return err
} }
@ -335,7 +335,7 @@ func UploadHandler(ctx *context.Context) {
} else { } else {
writeStatus(ctx, http.StatusInternalServerError) 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) log.Error("Error whilst removing metaobject for LFS OID[%s]: %v", p.Oid, err)
} }
return 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() { if !p.IsValid() {
log.Info("Attempt to access invalid LFS OID[%s] in %s/%s", p.Oid, rc.User, rc.Repo) 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") 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 return nil
} }
meta, err := models.GetLFSMetaObjectByOid(repository.ID, p.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repository.ID, p.Oid)
if err != nil { if err != nil {
log.Error("Unable to get LFS OID[%s] Error: %v", p.Oid, err) log.Error("Unable to get LFS OID[%s] Error: %v", p.Oid, err)
writeStatus(ctx, http.StatusNotFound) writeStatus(ctx, http.StatusNotFound)

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
@ -17,9 +18,9 @@ import (
) )
// MergeRequiredContextsCommitStatus returns a commit status state for given required contexts // MergeRequiredContextsCommitStatus returns a commit status state for given required contexts
func MergeRequiredContextsCommitStatus(commitStatuses []*models.CommitStatus, requiredContexts []string) structs.CommitStatusState { func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus, requiredContexts []string) structs.CommitStatusState {
if len(requiredContexts) == 0 { if len(requiredContexts) == 0 {
status := models.CalcCommitStatus(commitStatuses) status := git_model.CalcCommitStatus(commitStatuses)
if status != nil { if status != nil {
return status.State return status.State
} }
@ -38,7 +39,7 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*models.CommitStatus, re
if targetStatus == "" { if targetStatus == "" {
targetStatus = structs.CommitStatusPending targetStatus = structs.CommitStatusPending
commitStatuses = append(commitStatuses, &models.CommitStatus{ commitStatuses = append(commitStatuses, &git_model.CommitStatus{
State: targetStatus, State: targetStatus,
Context: ctx, Context: ctx,
Description: "Pending", Description: "Pending",
@ -52,10 +53,10 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*models.CommitStatus, re
} }
// IsCommitStatusContextSuccess returns true if all required status check contexts succeed. // IsCommitStatusContextSuccess returns true if all required status check contexts succeed.
func IsCommitStatusContextSuccess(commitStatuses []*models.CommitStatus, requiredContexts []string) bool { func IsCommitStatusContextSuccess(commitStatuses []*git_model.CommitStatus, requiredContexts []string) bool {
// If no specific context is required, require that last commit status is a success // If no specific context is required, require that last commit status is a success
if len(requiredContexts) == 0 { if len(requiredContexts) == 0 {
status := models.CalcCommitStatus(commitStatuses) status := git_model.CalcCommitStatus(commitStatuses)
if status == nil || status.State != structs.CommitStatusSuccess { if status == nil || status.State != structs.CommitStatusSuccess {
return false return false
} }
@ -132,7 +133,7 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *models.PullRequest
return "", errors.Wrap(err, "LoadBaseRepo") return "", errors.Wrap(err, "LoadBaseRepo")
} }
commitStatuses, _, err := models.GetLatestCommitStatus(ctx, pr.BaseRepo.ID, sha, db.ListOptions{}) commitStatuses, _, err := git_model.GetLatestCommitStatus(ctx, pr.BaseRepo.ID, sha, db.ListOptions{})
if err != nil { if err != nil {
return "", errors.Wrap(err, "GetLatestCommitStatus") return "", errors.Wrap(err, "GetLatestCommitStatus")
} }

@ -13,6 +13,7 @@ import (
"sync" "sync"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/git/pipeline" "code.gitea.io/gitea/modules/git/pipeline"
"code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -115,8 +116,8 @@ func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg
} }
// Then we need to check that this pointer is in the db // Then we need to check that this pointer is in the db
if _, err := models.GetLFSMetaObjectByOid(pr.HeadRepo.ID, pointer.Oid); err != nil { if _, err := git_model.GetLFSMetaObjectByOid(pr.HeadRepo.ID, pointer.Oid); err != nil {
if err == models.ErrLFSObjectNotExist { if err == git_model.ErrLFSObjectNotExist {
log.Warn("During merge of: %d in %-v, there is a pointer to LFS Oid: %s which although present in the LFS store is not associated with the head repo %-v", pr.Index, pr.BaseRepo, pointer.Oid, pr.HeadRepo) log.Warn("During merge of: %d in %-v, there is a pointer to LFS Oid: %s which although present in the LFS store is not associated with the head repo %-v", pr.Index, pr.BaseRepo, pointer.Oid, pr.HeadRepo)
continue continue
} }
@ -126,9 +127,9 @@ func createLFSMetaObjectsFromCatFileBatch(catFileBatchReader *io.PipeReader, wg
// OK we have a pointer that is associated with the head repo // OK we have a pointer that is associated with the head repo
// and is actually a file in the LFS // and is actually a file in the LFS
// Therefore it should be associated with the base repo // Therefore it should be associated with the base repo
meta := &models.LFSMetaObject{Pointer: pointer} meta := &git_model.LFSMetaObject{Pointer: pointer}
meta.RepositoryID = pr.BaseRepoID meta.RepositoryID = pr.BaseRepoID
if _, err := models.NewLFSMetaObject(meta); err != nil { if _, err := git_model.NewLFSMetaObject(meta); err != nil {
_ = catFileBatchReader.CloseWithError(err) _ = catFileBatchReader.CloseWithError(err)
break break
} }

@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
pull_model "code.gitea.io/gitea/models/pull" pull_model "code.gitea.io/gitea/models/pull"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
@ -756,7 +757,7 @@ func IsUserAllowedToMerge(ctx context.Context, pr *models.PullRequest, p access_
return false, err return false, err
} }
if (p.CanWrite(unit.TypeCode) && pr.ProtectedBranch == nil) || (pr.ProtectedBranch != nil && models.IsUserMergeWhitelisted(ctx, pr.ProtectedBranch, user.ID, p)) { if (p.CanWrite(unit.TypeCode) && pr.ProtectedBranch == nil) || (pr.ProtectedBranch != nil && git_model.IsUserMergeWhitelisted(ctx, pr.ProtectedBranch, user.ID, p)) {
return true, nil return true, nil
} }
@ -786,23 +787,23 @@ func CheckPullBranchProtections(ctx context.Context, pr *models.PullRequest, ski
} }
} }
if !pr.ProtectedBranch.HasEnoughApprovals(ctx, pr) { if !models.HasEnoughApprovals(ctx, pr.ProtectedBranch, pr) {
return models.ErrDisallowedToMerge{ return models.ErrDisallowedToMerge{
Reason: "Does not have enough approvals", Reason: "Does not have enough approvals",
} }
} }
if pr.ProtectedBranch.MergeBlockedByRejectedReview(ctx, pr) { if models.MergeBlockedByRejectedReview(ctx, pr.ProtectedBranch, pr) {
return models.ErrDisallowedToMerge{ return models.ErrDisallowedToMerge{
Reason: "There are requested changes", Reason: "There are requested changes",
} }
} }
if pr.ProtectedBranch.MergeBlockedByOfficialReviewRequests(ctx, pr) { if models.MergeBlockedByOfficialReviewRequests(ctx, pr.ProtectedBranch, pr) {
return models.ErrDisallowedToMerge{ return models.ErrDisallowedToMerge{
Reason: "There are official review requests", Reason: "There are official review requests",
} }
} }
if pr.ProtectedBranch.MergeBlockedByOutdatedBranch(pr) { if models.MergeBlockedByOutdatedBranch(pr.ProtectedBranch, pr) {
return models.ErrDisallowedToMerge{ return models.ErrDisallowedToMerge{
Reason: "The head branch is behind the base branch", Reason: "The head branch is behind the base branch",
} }
@ -812,7 +813,7 @@ func CheckPullBranchProtections(ctx context.Context, pr *models.PullRequest, ski
return nil return nil
} }
if pr.ProtectedBranch.MergeBlockedByProtectedFiles(pr) { if pr.ProtectedBranch.MergeBlockedByProtectedFiles(pr.ChangedProtectedFiles) {
return models.ErrDisallowedToMerge{ return models.ErrDisallowedToMerge{
Reason: "Changed protected files", Reason: "Changed protected files",
} }

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -735,13 +736,13 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *models.PullRequest) s
} }
// GetIssuesLastCommitStatus returns a map of issue ID to the most recent commit's latest status // GetIssuesLastCommitStatus returns a map of issue ID to the most recent commit's latest status
func GetIssuesLastCommitStatus(ctx context.Context, issues models.IssueList) (map[int64]*models.CommitStatus, error) { func GetIssuesLastCommitStatus(ctx context.Context, issues models.IssueList) (map[int64]*git_model.CommitStatus, error) {
_, lastStatus, err := GetIssuesAllCommitStatus(ctx, issues) _, lastStatus, err := GetIssuesAllCommitStatus(ctx, issues)
return lastStatus, err return lastStatus, err
} }
// GetIssuesAllCommitStatus returns a map of issue ID to a list of all statuses for the most recent commit as well as a map of issue ID to only the commit's latest status // GetIssuesAllCommitStatus returns a map of issue ID to a list of all statuses for the most recent commit as well as a map of issue ID to only the commit's latest status
func GetIssuesAllCommitStatus(ctx context.Context, issues models.IssueList) (map[int64][]*models.CommitStatus, map[int64]*models.CommitStatus, error) { func GetIssuesAllCommitStatus(ctx context.Context, issues models.IssueList) (map[int64][]*git_model.CommitStatus, map[int64]*git_model.CommitStatus, error) {
if err := issues.LoadPullRequests(); err != nil { if err := issues.LoadPullRequests(); err != nil {
return nil, nil, err return nil, nil, err
} }
@ -751,8 +752,8 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues models.IssueList) (map
var ( var (
gitRepos = make(map[int64]*git.Repository) gitRepos = make(map[int64]*git.Repository)
res = make(map[int64][]*models.CommitStatus) res = make(map[int64][]*git_model.CommitStatus)
lastRes = make(map[int64]*models.CommitStatus) lastRes = make(map[int64]*git_model.CommitStatus)
err error err error
) )
defer func() { defer func() {
@ -787,14 +788,14 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues models.IssueList) (map
} }
// getAllCommitStatus get pr's commit statuses. // getAllCommitStatus get pr's commit statuses.
func getAllCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (statuses []*models.CommitStatus, lastStatus *models.CommitStatus, err error) { func getAllCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (statuses []*git_model.CommitStatus, lastStatus *git_model.CommitStatus, err error) {
sha, shaErr := gitRepo.GetRefCommitID(pr.GetGitRefName()) sha, shaErr := gitRepo.GetRefCommitID(pr.GetGitRefName())
if shaErr != nil { if shaErr != nil {
return nil, nil, shaErr return nil, nil, shaErr
} }
statuses, _, err = models.GetLatestCommitStatus(db.DefaultContext, pr.BaseRepo.ID, sha, db.ListOptions{}) statuses, _, err = git_model.GetLatestCommitStatus(db.DefaultContext, pr.BaseRepo.ID, sha, db.ListOptions{})
lastStatus = models.CalcCommitStatus(statuses) lastStatus = git_model.CalcCommitStatus(statuses)
return statuses, lastStatus, err return statuses, lastStatus, err
} }

@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -32,11 +33,11 @@ func createTag(gitRepo *git.Repository, rel *models.Release, msg string) (bool,
return false, err return false, err
} }
protectedTags, err := models.GetProtectedTags(rel.Repo.ID) protectedTags, err := git_model.GetProtectedTags(rel.Repo.ID)
if err != nil { if err != nil {
return false, fmt.Errorf("GetProtectedTags: %v", err) return false, fmt.Errorf("GetProtectedTags: %v", err)
} }
isAllowed, err := models.IsUserAllowedToControlTag(protectedTags, rel.TagName, rel.PublisherID) isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, rel.TagName, rel.PublisherID)
if err != nil { if err != nil {
return false, err return false, err
} }

@ -11,6 +11,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -118,7 +119,7 @@ func RenameBranch(repo *repo_model.Repository, doer *user_model.User, gitRepo *g
return "from_not_exist", nil return "from_not_exist", nil
} }
if err := models.RenameBranch(repo, from, to, func(isDefault bool) error { if err := git_model.RenameBranch(repo, from, to, func(isDefault bool) error {
err2 := gitRepo.RenameBranch(from, to) err2 := gitRepo.RenameBranch(from, to)
if err2 != nil { if err2 != nil {
return err2 return err2
@ -158,7 +159,7 @@ func DeleteBranch(doer *user_model.User, repo *repo_model.Repository, gitRepo *g
return ErrBranchIsDefault return ErrBranchIsDefault
} }
isProtected, err := models.IsProtectedBranch(repo.ID, branchName) isProtected, err := git_model.IsProtectedBranch(repo.ID, branchName)
if err != nil { if err != nil {
return err return err
} }
@ -196,7 +197,7 @@ func DeleteBranch(doer *user_model.User, repo *repo_model.Repository, gitRepo *g
log.Error("Update: %v", err) log.Error("Update: %v", err)
} }
if err := models.AddDeletedBranch(repo.ID, branchName, commit.ID.String(), doer.ID); err != nil { if err := git_model.AddDeletedBranch(repo.ID, branchName, commit.ID.String(), doer.ID); err != nil {
log.Warn("AddDeletedBranch: %v", err) log.Warn("AddDeletedBranch: %v", err)
} }

@ -8,8 +8,8 @@ import (
"context" "context"
"fmt" "fmt"
"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey" asymkey_model "code.gitea.io/gitea/models/asymkey"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -20,7 +20,7 @@ import (
// CreateCommitStatus creates a new CommitStatus given a bunch of parameters // CreateCommitStatus creates a new CommitStatus given a bunch of parameters
// NOTE: All text-values will be trimmed from whitespaces. // NOTE: All text-values will be trimmed from whitespaces.
// Requires: Repo, Creator, SHA // Requires: Repo, Creator, SHA
func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error { func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creator *user_model.User, sha string, status *git_model.CommitStatus) error {
repoPath := repo.RepoPath() repoPath := repo.RepoPath()
// confirm that commit is exist // confirm that commit is exist
@ -36,7 +36,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
} }
gitRepo.Close() gitRepo.Close()
if err := models.NewCommitStatus(models.NewCommitStatusOptions{ if err := git_model.NewCommitStatus(git_model.NewCommitStatusOptions{
Repo: repo, Repo: repo,
Creator: creator, Creator: creator,
SHA: sha, SHA: sha,

@ -10,6 +10,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -66,7 +67,7 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode
return err return err
} }
} else { } else {
protectedBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, opts.OldBranch) protectedBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, opts.OldBranch)
if err != nil { if err != nil {
return err return err
} }

@ -13,6 +13,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
@ -75,8 +76,8 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *repo_model.Repository) (st
if setting.LFS.StartServer { if setting.LFS.StartServer {
pointer, _ := lfs.ReadPointerFromBuffer(buf) pointer, _ := lfs.ReadPointerFromBuffer(buf)
if pointer.IsValid() { if pointer.IsValid() {
meta, err := models.GetLFSMetaObjectByOid(repo.ID, pointer.Oid) meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, pointer.Oid)
if err != nil && err != models.ErrLFSObjectNotExist { if err != nil && err != git_model.ErrLFSObjectNotExist {
// return default // return default
return "UTF-8", false return "UTF-8", false
} }
@ -364,7 +365,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
} }
// Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content // Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content
opts.Content = content opts.Content = content
var lfsMetaObject *models.LFSMetaObject var lfsMetaObject *git_model.LFSMetaObject
if setting.LFS.StartServer && hasOldBranch { if setting.LFS.StartServer && hasOldBranch {
// Check there is no way this can return multiple infos // Check there is no way this can return multiple infos
@ -383,7 +384,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
if err != nil { if err != nil {
return nil, err return nil, err
} }
lfsMetaObject = &models.LFSMetaObject{Pointer: pointer, RepositoryID: repo.ID} lfsMetaObject = &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repo.ID}
content = pointer.StringContent() content = pointer.StringContent()
} }
} }
@ -423,7 +424,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
if lfsMetaObject != nil { if lfsMetaObject != nil {
// We have an LFS object - create it // We have an LFS object - create it
lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject) lfsMetaObject, err = git_model.NewLFSMetaObject(lfsMetaObject)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -434,7 +435,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
} }
if !exist { if !exist {
if err := contentStore.Put(lfsMetaObject.Pointer, strings.NewReader(opts.Content)); err != nil { if err := contentStore.Put(lfsMetaObject.Pointer, strings.NewReader(opts.Content)); err != nil {
if _, err2 := models.RemoveLFSMetaObjectByOid(repo.ID, lfsMetaObject.Oid); err2 != nil { if _, err2 := git_model.RemoveLFSMetaObjectByOid(repo.ID, lfsMetaObject.Oid); err2 != nil {
return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %v)", lfsMetaObject.Oid, err2, err) return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %v)", lfsMetaObject.Oid, err2, err)
} }
return nil, err return nil, err
@ -462,7 +463,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
// VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch // VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch
func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error { func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error {
protectedBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, branchName) protectedBranch, err := git_model.GetProtectedBranchBy(ctx, repo.ID, branchName)
if err != nil { if err != nil {
return err return err
} }

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -32,7 +33,7 @@ type UploadRepoFileOptions struct {
type uploadInfo struct { type uploadInfo struct {
upload *models.Upload upload *models.Upload
lfsMetaObject *models.LFSMetaObject lfsMetaObject *git_model.LFSMetaObject
} }
func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, original error) error { func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, original error) error {
@ -41,7 +42,7 @@ func cleanUpAfterFailure(infos *[]uploadInfo, t *TemporaryUploadRepository, orig
continue continue
} }
if !info.lfsMetaObject.Existing { if !info.lfsMetaObject.Existing {
if _, err := models.RemoveLFSMetaObjectByOid(t.repo.ID, info.lfsMetaObject.Oid); err != nil { if _, err := git_model.RemoveLFSMetaObjectByOid(t.repo.ID, info.lfsMetaObject.Oid); err != nil {
original = fmt.Errorf("%v, %v", original, err) original = fmt.Errorf("%v, %v", original, err)
} }
} }
@ -65,7 +66,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
for i, upload := range uploads { for i, upload := range uploads {
// Check file is not lfs locked, will return nil if lock setting not enabled // Check file is not lfs locked, will return nil if lock setting not enabled
filepath := path.Join(opts.TreePath, upload.Name) filepath := path.Join(opts.TreePath, upload.Name)
lfsLock, err := models.GetTreePathLock(repo.ID, filepath) lfsLock, err := git_model.GetTreePathLock(repo.ID, filepath)
if err != nil { if err != nil {
return err return err
} }
@ -74,7 +75,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
if err != nil { if err != nil {
return err return err
} }
return models.ErrLFSFileLocked{RepoID: repo.ID, Path: filepath, UserName: u.Name} return git_model.ErrLFSFileLocked{RepoID: repo.ID, Path: filepath, UserName: u.Name}
} }
names[i] = upload.Name names[i] = upload.Name
@ -133,7 +134,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
if infos[i].lfsMetaObject == nil { if infos[i].lfsMetaObject == nil {
continue continue
} }
infos[i].lfsMetaObject, err = models.NewLFSMetaObject(infos[i].lfsMetaObject) infos[i].lfsMetaObject, err = git_model.NewLFSMetaObject(infos[i].lfsMetaObject)
if err != nil { if err != nil {
// OK Now we need to cleanup // OK Now we need to cleanup
return cleanUpAfterFailure(&infos, t, err) return cleanUpAfterFailure(&infos, t, err)
@ -175,7 +176,7 @@ func copyUploadedLFSFileIntoRepository(info *uploadInfo, filename2attribute2info
return err return err
} }
info.lfsMetaObject = &models.LFSMetaObject{Pointer: pointer, RepositoryID: t.repo.ID} info.lfsMetaObject = &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: t.repo.ID}
if objectHash, err = t.HashObject(strings.NewReader(pointer.StringContent())); err != nil { if objectHash, err = t.HashObject(strings.NewReader(pointer.StringContent())); err != nil {
return err return err

@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -101,7 +102,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
} }
// copy lfs files failure should not be ignored // copy lfs files failure should not be ignored
if err = models.CopyLFS(txCtx, repo, opts.BaseRepo); err != nil { if err = git_model.CopyLFS(txCtx, repo, opts.BaseRepo); err != nil {
return err return err
} }

@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
@ -252,7 +253,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
notification.NotifyPushCommits(pusher, repo, opts, commits) notification.NotifyPushCommits(pusher, repo, opts, commits)
if err = models.RemoveDeletedBranchByName(repo.ID, branch); err != nil { if err = git_model.RemoveDeletedBranchByName(repo.ID, branch); err != nil {
log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err) log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err)
} }

Loading…
Cancel
Save