summaryrefslogtreecommitdiffstats
path: root/modules/convert
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-01-19 23:26:57 +0000
committerGitHub <noreply@github.com>2022-01-19 23:26:57 +0000
commit5cb0c9aa0d7eed087055b1efca79628957207d36 (patch)
treed117a514e1f17e5f6bfcda1be273f6a971112663 /modules/convert
parent4563148a61ba892e8f2bb66342f00a950bcd5315 (diff)
downloadgitea-5cb0c9aa0d7eed087055b1efca79628957207d36.tar.gz
gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.zip
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/convert')
-rw-r--r--modules/convert/git_commit.go5
-rw-r--r--modules/convert/pull.go11
-rw-r--r--modules/convert/pull_review.go13
-rw-r--r--modules/convert/pull_test.go5
4 files changed, 18 insertions, 16 deletions
diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go
index 55cac5fd58..a5c3112f13 100644
--- a/modules/convert/git_commit.go
+++ b/modules/convert/git_commit.go
@@ -72,8 +72,7 @@ func ToPayloadCommit(repo *repo_model.Repository, c *git.Commit) *api.PayloadCom
}
// ToCommit convert a git.Commit to api.Commit
-func ToCommit(repo *repo_model.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) {
-
+func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User) (*api.Commit, error) {
var apiAuthor, apiCommitter *api.User
// Retrieve author and committer information
@@ -134,7 +133,7 @@ func ToCommit(repo *repo_model.Repository, commit *git.Commit, userCache map[str
}
// Retrieve files affected by the commit
- fileStatus, err := git.GetCommitFileStatus(repo.RepoPath(), commit.ID.String())
+ fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String())
if err != nil {
return nil, err
}
diff --git a/modules/convert/pull.go b/modules/convert/pull.go
index 2e2c1b85bc..1551645a51 100644
--- a/modules/convert/pull.go
+++ b/modules/convert/pull.go
@@ -5,6 +5,7 @@
package convert
import (
+ "context"
"fmt"
"code.gitea.io/gitea/models"
@@ -18,7 +19,7 @@ import (
// ToAPIPullRequest assumes following fields have been assigned with valid values:
// Required - Issue
// Optional - Merger
-func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRequest {
+func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_model.User) *api.PullRequest {
var (
baseBranch *git.Branch
headBranch *git.Branch
@@ -84,7 +85,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
},
}
- gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
@@ -110,7 +111,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
}
if pr.Flow == models.PullRequestFlowAGit {
- gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err)
return nil
@@ -137,7 +138,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, p.AccessMode)
- headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
+ headGitRepo, err := git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
return nil
@@ -173,7 +174,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
}
if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
- baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
+ baseGitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go
index b99d7bead6..962aae58bb 100644
--- a/modules/convert/pull_review.go
+++ b/modules/convert/pull_review.go
@@ -5,6 +5,7 @@
package convert
import (
+ "context"
"strings"
"code.gitea.io/gitea/models"
@@ -13,8 +14,8 @@ import (
)
// ToPullReview convert a review to api format
-func ToPullReview(r *models.Review, doer *user_model.User) (*api.PullReview, error) {
- if err := r.LoadAttributes(); err != nil {
+func ToPullReview(ctx context.Context, r *models.Review, doer *user_model.User) (*api.PullReview, error) {
+ if err := r.LoadAttributes(ctx); err != nil {
if !user_model.IsErrUserNotExist(err) {
return nil, err
}
@@ -54,14 +55,14 @@ func ToPullReview(r *models.Review, doer *user_model.User) (*api.PullReview, err
}
// ToPullReviewList convert a list of review to it's api format
-func ToPullReviewList(rl []*models.Review, doer *user_model.User) ([]*api.PullReview, error) {
+func ToPullReviewList(ctx context.Context, rl []*models.Review, doer *user_model.User) ([]*api.PullReview, error) {
result := make([]*api.PullReview, 0, len(rl))
for i := range rl {
// show pending reviews only for the user who created them
if rl[i].Type == models.ReviewTypePending && !(doer.IsAdmin || doer.ID == rl[i].ReviewerID) {
continue
}
- r, err := ToPullReview(rl[i], doer)
+ r, err := ToPullReview(ctx, rl[i], doer)
if err != nil {
return nil, err
}
@@ -71,8 +72,8 @@ func ToPullReviewList(rl []*models.Review, doer *user_model.User) ([]*api.PullRe
}
// ToPullReviewCommentList convert the CodeComments of an review to it's api format
-func ToPullReviewCommentList(review *models.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
- if err := review.LoadAttributes(); err != nil {
+func ToPullReviewCommentList(ctx context.Context, review *models.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
+ if err := review.LoadAttributes(ctx); err != nil {
if !user_model.IsErrUserNotExist(err) {
return nil, err
}
diff --git a/modules/convert/pull_test.go b/modules/convert/pull_test.go
index 11789b1200..5351b5e172 100644
--- a/modules/convert/pull_test.go
+++ b/modules/convert/pull_test.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
+ "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
@@ -23,7 +24,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
assert.NoError(t, pr.LoadAttributes())
assert.NoError(t, pr.LoadIssue())
- apiPullRequest := ToAPIPullRequest(pr, nil)
+ apiPullRequest := ToAPIPullRequest(git.DefaultContext, pr, nil)
assert.NotNil(t, apiPullRequest)
assert.EqualValues(t, &structs.PRBranchInfo{
Name: "branch1",
@@ -40,7 +41,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
// simulate fork deletion
pr.HeadRepo = nil
pr.HeadRepoID = 100000
- apiPullRequest = ToAPIPullRequest(pr, nil)
+ apiPullRequest = ToAPIPullRequest(git.DefaultContext, pr, nil)
assert.NotNil(t, apiPullRequest)
assert.Nil(t, apiPullRequest.Head.Repository)
assert.EqualValues(t, -1, apiPullRequest.Head.RepoID)