diff options
author | Jason Song <i@wolfogre.com> | 2023-01-09 11:50:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-09 11:50:54 +0800 |
commit | 7adc2de46404e32ed33f999d308ed56232cdfea5 (patch) | |
tree | 2404a22b5fa8a941eb5bfd16fd717fab29d899e4 /models/git | |
parent | b878155b8741c2769b6aa50a80609c36822451c9 (diff) | |
download | gitea-7adc2de46404e32ed33f999d308ed56232cdfea5.tar.gz gitea-7adc2de46404e32ed33f999d308ed56232cdfea5.zip |
Use context parameter in models/git (#22367)
After #22362, we can feel free to use transactions without
`db.DefaultContext`.
And there are still lots of models using `db.DefaultContext`, I think we
should refactor them carefully and one by one.
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/git')
-rw-r--r-- | models/git/branches.go | 52 | ||||
-rw-r--r-- | models/git/branches_test.go | 20 | ||||
-rw-r--r-- | models/git/commit_status.go | 32 | ||||
-rw-r--r-- | models/git/commit_status_test.go | 12 | ||||
-rw-r--r-- | models/git/lfs.go | 34 | ||||
-rw-r--r-- | models/git/lfs_lock.go | 20 | ||||
-rw-r--r-- | models/git/protected_tag.go | 12 |
7 files changed, 91 insertions, 91 deletions
diff --git a/models/git/branches.go b/models/git/branches.go index c02ab0a888..2becbc3d13 100644 --- a/models/git/branches.go +++ b/models/git/branches.go @@ -67,19 +67,19 @@ func (protectBranch *ProtectedBranch) IsProtected() bool { } // CanUserPush returns if some user could push to this protected branch -func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool { +func (protectBranch *ProtectedBranch) CanUserPush(ctx context.Context, userID int64) bool { if !protectBranch.CanPush { return false } if !protectBranch.EnableWhitelist { - if user, err := user_model.GetUserByID(db.DefaultContext, userID); err != nil { + if user, err := user_model.GetUserByID(ctx, userID); err != nil { log.Error("GetUserByID: %v", err) return false - } else if repo, err := repo_model.GetRepositoryByID(db.DefaultContext, protectBranch.RepoID); err != nil { + } else if repo, err := repo_model.GetRepositoryByID(ctx, protectBranch.RepoID); err != nil { log.Error("repo_model.GetRepositoryByID: %v", err) return false - } else if writeAccess, err := access_model.HasAccessUnit(db.DefaultContext, user, repo, unit.TypeCode, perm.AccessModeWrite); err != nil { + } else if writeAccess, err := access_model.HasAccessUnit(ctx, user, repo, unit.TypeCode, perm.AccessModeWrite); err != nil { log.Error("HasAccessUnit: %v", err) return false } else { @@ -95,7 +95,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool { return false } - in, err := organization.IsUserInTeams(db.DefaultContext, userID, protectBranch.WhitelistTeamIDs) + in, err := organization.IsUserInTeams(ctx, userID, protectBranch.WhitelistTeamIDs) if err != nil { log.Error("IsUserInTeams: %v", err) return false @@ -320,19 +320,19 @@ func UpdateProtectBranch(ctx context.Context, repo *repo_model.Repository, prote } // GetProtectedBranches get all protected branches -func GetProtectedBranches(repoID int64) ([]*ProtectedBranch, error) { +func GetProtectedBranches(ctx context.Context, repoID int64) ([]*ProtectedBranch, error) { protectedBranches := make([]*ProtectedBranch, 0) - return protectedBranches, db.GetEngine(db.DefaultContext).Find(&protectedBranches, &ProtectedBranch{RepoID: repoID}) + return protectedBranches, db.GetEngine(ctx).Find(&protectedBranches, &ProtectedBranch{RepoID: repoID}) } // IsProtectedBranch checks if branch is protected -func IsProtectedBranch(repoID int64, branchName string) (bool, error) { +func IsProtectedBranch(ctx context.Context, repoID int64, branchName string) (bool, error) { protectedBranch := &ProtectedBranch{ RepoID: repoID, BranchName: branchName, } - has, err := db.GetEngine(db.DefaultContext).Exist(protectedBranch) + has, err := db.GetEngine(ctx).Exist(protectedBranch) if err != nil { return true, err } @@ -413,13 +413,13 @@ func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, curre } // DeleteProtectedBranch removes ProtectedBranch relation between the user and repository. -func DeleteProtectedBranch(repoID, id int64) (err error) { +func DeleteProtectedBranch(ctx context.Context, repoID, id int64) (err error) { protectedBranch := &ProtectedBranch{ RepoID: repoID, ID: id, } - if affected, err := db.GetEngine(db.DefaultContext).Delete(protectedBranch); err != nil { + if affected, err := db.GetEngine(ctx).Delete(protectedBranch); err != nil { return err } else if affected != 1 { return fmt.Errorf("delete protected branch ID(%v) failed", id) @@ -440,7 +440,7 @@ type DeletedBranch struct { } // AddDeletedBranch adds a deleted branch to the database -func AddDeletedBranch(repoID int64, branchName, commit string, deletedByID int64) error { +func AddDeletedBranch(ctx context.Context, repoID int64, branchName, commit string, deletedByID int64) error { deletedBranch := &DeletedBranch{ RepoID: repoID, Name: branchName, @@ -448,20 +448,20 @@ func AddDeletedBranch(repoID int64, branchName, commit string, deletedByID int64 DeletedByID: deletedByID, } - _, err := db.GetEngine(db.DefaultContext).Insert(deletedBranch) + _, err := db.GetEngine(ctx).Insert(deletedBranch) return err } // GetDeletedBranches returns all the deleted branches -func GetDeletedBranches(repoID int64) ([]*DeletedBranch, error) { +func GetDeletedBranches(ctx context.Context, repoID int64) ([]*DeletedBranch, error) { deletedBranches := make([]*DeletedBranch, 0) - return deletedBranches, db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).Desc("deleted_unix").Find(&deletedBranches) + return deletedBranches, db.GetEngine(ctx).Where("repo_id = ?", repoID).Desc("deleted_unix").Find(&deletedBranches) } // GetDeletedBranchByID get a deleted branch by its ID -func GetDeletedBranchByID(repoID, id int64) (*DeletedBranch, error) { +func GetDeletedBranchByID(ctx context.Context, repoID, id int64) (*DeletedBranch, error) { deletedBranch := &DeletedBranch{} - has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).And("id = ?", id).Get(deletedBranch) + has, err := db.GetEngine(ctx).Where("repo_id = ?", repoID).And("id = ?", id).Get(deletedBranch) if err != nil { return nil, err } @@ -472,13 +472,13 @@ func GetDeletedBranchByID(repoID, id int64) (*DeletedBranch, error) { } // RemoveDeletedBranchByID removes a deleted branch from the database -func RemoveDeletedBranchByID(repoID, id int64) (err error) { +func RemoveDeletedBranchByID(ctx context.Context, repoID, id int64) (err error) { deletedBranch := &DeletedBranch{ RepoID: repoID, ID: id, } - if affected, err := db.GetEngine(db.DefaultContext).Delete(deletedBranch); err != nil { + if affected, err := db.GetEngine(ctx).Delete(deletedBranch); err != nil { return err } else if affected != 1 { return fmt.Errorf("remove deleted branch ID(%v) failed", id) @@ -498,8 +498,8 @@ func (deletedBranch *DeletedBranch) LoadUser(ctx context.Context) { } // RemoveDeletedBranchByName removes all deleted branches -func RemoveDeletedBranchByName(repoID int64, branch string) error { - _, err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch)) +func RemoveDeletedBranchByName(ctx context.Context, repoID int64, branch string) error { + _, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch)) return err } @@ -509,7 +509,7 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) { log.Trace("Doing: DeletedBranchesCleanup") deleteBefore := time.Now().Add(-olderThan) - _, err := db.GetEngine(db.DefaultContext).Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch)) + _, err := db.GetEngine(ctx).Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch)) if err != nil { log.Error("DeletedBranchesCleanup: %v", err) } @@ -526,19 +526,19 @@ type RenamedBranch struct { } // FindRenamedBranch check if a branch was renamed -func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist bool, err error) { +func FindRenamedBranch(ctx context.Context, repoID int64, from string) (branch *RenamedBranch, exist bool, err error) { branch = &RenamedBranch{ RepoID: repoID, From: from, } - exist, err = db.GetEngine(db.DefaultContext).Get(branch) + exist, err = db.GetEngine(ctx).Get(branch) return branch, exist, err } // RenameBranch rename a branch -func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) { - ctx, committer, err := db.TxContext(db.DefaultContext) +func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) { + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } diff --git a/models/git/branches_test.go b/models/git/branches_test.go index 56f416622e..e26a16de03 100644 --- a/models/git/branches_test.go +++ b/models/git/branches_test.go @@ -20,15 +20,15 @@ func TestAddDeletedBranch(t *testing.T) { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1}) - assert.Error(t, git_model.AddDeletedBranch(repo.ID, firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) - assert.NoError(t, git_model.AddDeletedBranch(repo.ID, "test", "5655464564554545466464656", int64(1))) + assert.Error(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) + assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, "test", "5655464564554545466464656", int64(1))) } func TestGetDeletedBranches(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - branches, err := git_model.GetDeletedBranches(repo.ID) + branches, err := git_model.GetDeletedBranches(db.DefaultContext, repo.ID) assert.NoError(t, err) assert.Len(t, branches, 2) } @@ -65,7 +65,7 @@ func TestRemoveDeletedBranch(t *testing.T) { firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1}) - err := git_model.RemoveDeletedBranchByID(repo.ID, 1) + err := git_model.RemoveDeletedBranchByID(db.DefaultContext, repo.ID, 1) assert.NoError(t, err) unittest.AssertNotExistsBean(t, firstBranch) unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 2}) @@ -74,7 +74,7 @@ func TestRemoveDeletedBranch(t *testing.T) { func getDeletedBranch(t *testing.T, branch *git_model.DeletedBranch) *git_model.DeletedBranch { repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - deletedBranch, err := git_model.GetDeletedBranchByID(repo.ID, branch.ID) + deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo.ID, branch.ID) assert.NoError(t, err) assert.Equal(t, branch.ID, deletedBranch.ID) assert.Equal(t, branch.Name, deletedBranch.Name) @@ -86,12 +86,12 @@ func getDeletedBranch(t *testing.T, branch *git_model.DeletedBranch) *git_model. func TestFindRenamedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - branch, exist, err := git_model.FindRenamedBranch(1, "dev") + branch, exist, err := git_model.FindRenamedBranch(db.DefaultContext, 1, "dev") assert.NoError(t, err) assert.Equal(t, true, exist) assert.Equal(t, "master", branch.To) - _, exist, err = git_model.FindRenamedBranch(1, "unknow") + _, exist, err = git_model.FindRenamedBranch(db.DefaultContext, 1, "unknow") assert.NoError(t, err) assert.Equal(t, false, exist) } @@ -110,7 +110,7 @@ func TestRenameBranch(t *testing.T) { }, git_model.WhitelistOptions{})) assert.NoError(t, committer.Commit()) - assert.NoError(t, git_model.RenameBranch(repo1, "master", "main", func(isDefault bool) error { + assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(isDefault bool) error { _isDefault = isDefault return nil })) @@ -144,7 +144,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { // is actually on repo with ID 1. repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) - deletedBranch, err := git_model.GetDeletedBranchByID(repo2.ID, 1) + deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo2.ID, 1) // Expect no error, and the returned branch is nil. assert.NoError(t, err) @@ -154,7 +154,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { // This should return the deletedBranch. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - deletedBranch, err = git_model.GetDeletedBranchByID(repo1.ID, 1) + deletedBranch, err = git_model.GetDeletedBranchByID(db.DefaultContext, repo1.ID, 1) // Expect no error, and the returned branch to be not nil. assert.NoError(t, err) diff --git a/models/git/commit_status.go b/models/git/commit_status.go index 07e0b9fb73..7c40b6d214 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -129,8 +129,8 @@ func (status *CommitStatus) loadAttributes(ctx context.Context) (err error) { } // APIURL returns the absolute APIURL to this commit-status. -func (status *CommitStatus) APIURL() string { - _ = status.loadAttributes(db.DefaultContext) +func (status *CommitStatus) APIURL(ctx context.Context) string { + _ = status.loadAttributes(ctx) return status.Repo.APIURL() + "/statuses/" + url.PathEscape(status.SHA) } @@ -162,7 +162,7 @@ type CommitStatusOptions struct { } // GetCommitStatuses returns all statuses for a given commit. -func GetCommitStatuses(repo *repo_model.Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) { +func GetCommitStatuses(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) { if opts.Page <= 0 { opts.Page = 1 } @@ -170,7 +170,7 @@ func GetCommitStatuses(repo *repo_model.Repository, sha string, opts *CommitStat opts.Page = setting.ItemsPerPage } - countSession := listCommitStatusesStatement(repo, sha, opts) + countSession := listCommitStatusesStatement(ctx, repo, sha, opts) countSession = db.SetSessionPagination(countSession, opts) maxResults, err := countSession.Count(new(CommitStatus)) if err != nil { @@ -179,14 +179,14 @@ func GetCommitStatuses(repo *repo_model.Repository, sha string, opts *CommitStat } statuses := make([]*CommitStatus, 0, opts.PageSize) - findSession := listCommitStatusesStatement(repo, sha, opts) + findSession := listCommitStatusesStatement(ctx, repo, sha, opts) findSession = db.SetSessionPagination(findSession, opts) sortCommitStatusesSession(findSession, opts.SortType) return statuses, maxResults, findSession.Find(&statuses) } -func listCommitStatusesStatement(repo *repo_model.Repository, sha string, opts *CommitStatusOptions) *xorm.Session { - sess := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repo.ID).And("sha = ?", sha) +func listCommitStatusesStatement(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) *xorm.Session { + sess := db.GetEngine(ctx).Where("repo_id = ?", repo.ID).And("sha = ?", sha) switch opts.State { case "pending", "success", "error", "failure", "warning": sess.And("state = ?", opts.State) @@ -241,10 +241,10 @@ func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOp } // FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts -func FindRepoRecentCommitStatusContexts(repoID int64, before time.Duration) ([]string, error) { +func FindRepoRecentCommitStatusContexts(ctx context.Context, repoID int64, before time.Duration) ([]string, error) { start := timeutil.TimeStampNow().AddDuration(-before) ids := make([]int64, 0, 10) - if err := db.GetEngine(db.DefaultContext).Table("commit_status"). + if err := db.GetEngine(ctx).Table("commit_status"). Where("repo_id = ?", repoID). And("updated_unix >= ?", start). Select("max( id ) as id"). @@ -257,7 +257,7 @@ func FindRepoRecentCommitStatusContexts(repoID int64, before time.Duration) ([]s if len(ids) == 0 { return contexts, nil } - return contexts, db.GetEngine(db.DefaultContext).Select("context").Table("commit_status").In("id", ids).Find(&contexts) + return contexts, db.GetEngine(ctx).Select("context").Table("commit_status").In("id", ids).Find(&contexts) } // NewCommitStatusOptions holds options for creating a CommitStatus @@ -269,7 +269,7 @@ type NewCommitStatusOptions struct { } // NewCommitStatus save commit statuses into database -func NewCommitStatus(opts NewCommitStatusOptions) error { +func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error { if opts.Repo == nil { return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA) } @@ -283,7 +283,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error { return fmt.Errorf("NewCommitStatus[%s, %s]: invalid sha: %w", repoPath, opts.SHA, err) } - ctx, committer, err := db.TxContext(db.DefaultContext) + ctx, committer, err := db.TxContext(ctx) if err != nil { return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err) } @@ -322,14 +322,14 @@ type SignCommitWithStatuses struct { } // ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state -func ParseCommitsWithStatus(oldCommits []*asymkey_model.SignCommit, repo *repo_model.Repository) []*SignCommitWithStatuses { +func ParseCommitsWithStatus(ctx context.Context, oldCommits []*asymkey_model.SignCommit, repo *repo_model.Repository) []*SignCommitWithStatuses { newCommits := make([]*SignCommitWithStatuses, 0, len(oldCommits)) for _, c := range oldCommits { commit := &SignCommitWithStatuses{ SignCommit: c, } - statuses, _, err := GetLatestCommitStatus(db.DefaultContext, repo.ID, commit.ID.String(), db.ListOptions{}) + statuses, _, err := GetLatestCommitStatus(ctx, repo.ID, commit.ID.String(), db.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } else { @@ -348,8 +348,8 @@ func hashCommitStatusContext(context string) string { } // ConvertFromGitCommit converts git commits into SignCommitWithStatuses -func ConvertFromGitCommit(commits []*git.Commit, repo *repo_model.Repository) []*SignCommitWithStatuses { - return ParseCommitsWithStatus( +func ConvertFromGitCommit(ctx context.Context, commits []*git.Commit, repo *repo_model.Repository) []*SignCommitWithStatuses { + return ParseCommitsWithStatus(ctx, asymkey_model.ParseCommitsWithSignature( user_model.ValidateCommitsWithEmails(commits), repo.GetTrustModel(), diff --git a/models/git/commit_status_test.go b/models/git/commit_status_test.go index c829673014..2197433b3e 100644 --- a/models/git/commit_status_test.go +++ b/models/git/commit_status_test.go @@ -22,28 +22,28 @@ func TestGetCommitStatuses(t *testing.T) { sha1 := "1234123412341234123412341234123412341234" - statuses, maxResults, err := git_model.GetCommitStatuses(repo1, sha1, &git_model.CommitStatusOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 50}}) + statuses, maxResults, err := git_model.GetCommitStatuses(db.DefaultContext, repo1, sha1, &git_model.CommitStatusOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 50}}) assert.NoError(t, err) assert.Equal(t, int(maxResults), 5) assert.Len(t, statuses, 5) assert.Equal(t, "ci/awesomeness", statuses[0].Context) assert.Equal(t, structs.CommitStatusPending, statuses[0].State) - assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[0].APIURL()) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[0].APIURL(db.DefaultContext)) assert.Equal(t, "cov/awesomeness", statuses[1].Context) assert.Equal(t, structs.CommitStatusWarning, statuses[1].State) - assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[1].APIURL()) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[1].APIURL(db.DefaultContext)) assert.Equal(t, "cov/awesomeness", statuses[2].Context) assert.Equal(t, structs.CommitStatusSuccess, statuses[2].State) - assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[2].APIURL()) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[2].APIURL(db.DefaultContext)) assert.Equal(t, "ci/awesomeness", statuses[3].Context) assert.Equal(t, structs.CommitStatusFailure, statuses[3].State) - assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[3].APIURL()) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[3].APIURL(db.DefaultContext)) assert.Equal(t, "deploy/awesomeness", statuses[4].Context) assert.Equal(t, structs.CommitStatusError, statuses[4].State) - assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[4].APIURL()) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[4].APIURL(db.DefaultContext)) } diff --git a/models/git/lfs.go b/models/git/lfs.go index 8d418b928d..3494264688 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -135,10 +135,10 @@ var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"} // NewLFSMetaObject stores a given populated LFSMetaObject structure in the database // if it is not already present. -func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { +func NewLFSMetaObject(ctx context.Context, m *LFSMetaObject) (*LFSMetaObject, error) { var err error - ctx, committer, err := db.TxContext(db.DefaultContext) + ctx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -164,13 +164,13 @@ func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { // GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID. // It may return ErrLFSObjectNotExist or a database error. If the error is nil, // the returned pointer is a valid LFSMetaObject. -func GetLFSMetaObjectByOid(repoID int64, oid string) (*LFSMetaObject, error) { +func GetLFSMetaObjectByOid(ctx context.Context, repoID int64, oid string) (*LFSMetaObject, error) { if len(oid) == 0 { return nil, ErrLFSObjectNotExist } m := &LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}, RepositoryID: repoID} - has, err := db.GetEngine(db.DefaultContext).Get(m) + has, err := db.GetEngine(ctx).Get(m) if err != nil { return nil, err } else if !has { @@ -181,18 +181,18 @@ func GetLFSMetaObjectByOid(repoID int64, oid string) (*LFSMetaObject, error) { // RemoveLFSMetaObjectByOid removes a LFSMetaObject entry from database by its OID. // It may return ErrLFSObjectNotExist or a database error. -func RemoveLFSMetaObjectByOid(repoID int64, oid string) (int64, error) { - return RemoveLFSMetaObjectByOidFn(repoID, oid, nil) +func RemoveLFSMetaObjectByOid(ctx context.Context, repoID int64, oid string) (int64, error) { + return RemoveLFSMetaObjectByOidFn(ctx, repoID, oid, nil) } // RemoveLFSMetaObjectByOidFn removes a LFSMetaObject entry from database by its OID. // It may return ErrLFSObjectNotExist or a database error. It will run Fn with the current count within the transaction -func RemoveLFSMetaObjectByOidFn(repoID int64, oid string, fn func(count int64) error) (int64, error) { +func RemoveLFSMetaObjectByOidFn(ctx context.Context, repoID int64, oid string, fn func(count int64) error) (int64, error) { if len(oid) == 0 { return 0, ErrLFSObjectNotExist } - ctx, committer, err := db.TxContext(db.DefaultContext) + ctx, committer, err := db.TxContext(ctx) if err != nil { return 0, err } @@ -218,8 +218,8 @@ func RemoveLFSMetaObjectByOidFn(repoID int64, oid string, fn func(count int64) e } // GetLFSMetaObjects returns all LFSMetaObjects associated with a repository -func GetLFSMetaObjects(repoID int64, page, pageSize int) ([]*LFSMetaObject, error) { - sess := db.GetEngine(db.DefaultContext) +func GetLFSMetaObjects(ctx context.Context, repoID int64, page, pageSize int) ([]*LFSMetaObject, error) { + sess := db.GetEngine(ctx) if page >= 0 && pageSize > 0 { start := 0 @@ -233,18 +233,18 @@ func GetLFSMetaObjects(repoID int64, page, pageSize int) ([]*LFSMetaObject, erro } // CountLFSMetaObjects returns a count of all LFSMetaObjects associated with a repository -func CountLFSMetaObjects(repoID int64) (int64, error) { - return db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{RepositoryID: repoID}) +func CountLFSMetaObjects(ctx context.Context, repoID int64) (int64, error) { + return db.GetEngine(ctx).Count(&LFSMetaObject{RepositoryID: repoID}) } // LFSObjectAccessible checks if a provided Oid is accessible to the user -func LFSObjectAccessible(user *user_model.User, oid string) (bool, error) { +func LFSObjectAccessible(ctx context.Context, user *user_model.User, oid string) (bool, error) { if user.IsAdmin { - count, err := db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) + count, err := db.GetEngine(ctx).Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) return count > 0, err } cond := repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid) - count, err := db.GetEngine(db.DefaultContext).Where(cond).Join("INNER", "repository", "`lfs_meta_object`.repository_id = `repository`.id").Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) + count, err := db.GetEngine(ctx).Where(cond).Join("INNER", "repository", "`lfs_meta_object`.repository_id = `repository`.id").Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) return count > 0, err } @@ -254,8 +254,8 @@ func ExistsLFSObject(ctx context.Context, oid string) (bool, error) { } // LFSAutoAssociate auto associates accessible LFSMetaObjects -func LFSAutoAssociate(metas []*LFSMetaObject, user *user_model.User, repoID int64) error { - ctx, committer, err := db.TxContext(db.DefaultContext) +func LFSAutoAssociate(ctx context.Context, metas []*LFSMetaObject, user *user_model.User, repoID int64) error { + ctx, committer, err := db.TxContext(ctx) if err != nil { return err } diff --git a/models/git/lfs_lock.go b/models/git/lfs_lock.go index dc5b0a2ced..25480f3f96 100644 --- a/models/git/lfs_lock.go +++ b/models/git/lfs_lock.go @@ -42,8 +42,8 @@ func cleanPath(p string) string { } // CreateLFSLock creates a new lock. -func CreateLFSLock(repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) { - dbCtx, committer, err := db.TxContext(db.DefaultContext) +func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) { + dbCtx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } @@ -98,8 +98,8 @@ func GetLFSLockByID(ctx context.Context, id int64) (*LFSLock, error) { } // GetLFSLockByRepoID returns a list of locks of repository. -func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) { - e := db.GetEngine(db.DefaultContext) +func GetLFSLockByRepoID(ctx context.Context, repoID int64, page, pageSize int) ([]*LFSLock, error) { + e := db.GetEngine(ctx) if page >= 0 && pageSize > 0 { start := 0 if page > 0 { @@ -112,12 +112,12 @@ func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) { } // GetTreePathLock returns LSF lock for the treePath -func GetTreePathLock(repoID int64, treePath string) (*LFSLock, error) { +func GetTreePathLock(ctx context.Context, repoID int64, treePath string) (*LFSLock, error) { if !setting.LFS.StartServer { return nil, nil } - locks, err := GetLFSLockByRepoID(repoID, 0, 0) + locks, err := GetLFSLockByRepoID(ctx, repoID, 0, 0) if err != nil { return nil, err } @@ -130,13 +130,13 @@ func GetTreePathLock(repoID int64, treePath string) (*LFSLock, error) { } // CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository. -func CountLFSLockByRepoID(repoID int64) (int64, error) { - return db.GetEngine(db.DefaultContext).Count(&LFSLock{RepoID: repoID}) +func CountLFSLockByRepoID(ctx context.Context, repoID int64) (int64, error) { + return db.GetEngine(ctx).Count(&LFSLock{RepoID: repoID}) } // DeleteLFSLockByID deletes a lock by given ID. -func DeleteLFSLockByID(id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) { - dbCtx, committer, err := db.TxContext(db.DefaultContext) +func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) { + dbCtx, committer, err := db.TxContext(ctx) if err != nil { return nil, err } diff --git a/models/git/protected_tag.go b/models/git/protected_tag.go index 1d32b11366..12e53a3331 100644 --- a/models/git/protected_tag.go +++ b/models/git/protected_tag.go @@ -57,14 +57,14 @@ func (pt *ProtectedTag) matchString(name string) bool { } // InsertProtectedTag inserts a protected tag to database -func InsertProtectedTag(pt *ProtectedTag) error { - _, err := db.GetEngine(db.DefaultContext).Insert(pt) +func InsertProtectedTag(ctx context.Context, pt *ProtectedTag) error { + _, err := db.GetEngine(ctx).Insert(pt) return err } // UpdateProtectedTag updates the protected tag -func UpdateProtectedTag(pt *ProtectedTag) error { - _, err := db.GetEngine(db.DefaultContext).ID(pt.ID).AllCols().Update(pt) +func UpdateProtectedTag(ctx context.Context, pt *ProtectedTag) error { + _, err := db.GetEngine(ctx).ID(pt.ID).AllCols().Update(pt) return err } @@ -98,9 +98,9 @@ func GetProtectedTags(ctx context.Context, repoID int64) ([]*ProtectedTag, error } // GetProtectedTagByID gets the protected tag with the specific id -func GetProtectedTagByID(id int64) (*ProtectedTag, error) { +func GetProtectedTagByID(ctx context.Context, id int64) (*ProtectedTag, error) { tag := new(ProtectedTag) - has, err := db.GetEngine(db.DefaultContext).ID(id).Get(tag) + has, err := db.GetEngine(ctx).ID(id).Get(tag) if err != nil { return nil, err } |