aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/files/update.go
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 /services/repository/files/update.go
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 'services/repository/files/update.go')
-rw-r--r--services/repository/files/update.go24
1 files changed, 15 insertions, 9 deletions
diff --git a/services/repository/files/update.go b/services/repository/files/update.go
index 9fb074ed39..4b8653a7f7 100644
--- a/services/repository/files/update.go
+++ b/services/repository/files/update.go
@@ -6,6 +6,7 @@ package files
import (
"bytes"
+ "context"
"fmt"
"path"
"strings"
@@ -22,7 +23,6 @@ import (
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
asymkey_service "code.gitea.io/gitea/services/asymkey"
- repo_service "code.gitea.io/gitea/services/repository"
stdcharset "golang.org/x/net/html/charset"
"golang.org/x/text/transform"
@@ -125,7 +125,7 @@ func detectEncodingAndBOM(entry *git.TreeEntry, repo *repo_model.Repository) (st
}
// CreateOrUpdateRepoFile adds or updates a file in the given repository
-func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
+func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
// If no branch name is set, assume default branch
if opts.OldBranch == "" {
opts.OldBranch = repo.DefaultBranch
@@ -134,8 +134,14 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User,
opts.NewBranch = opts.OldBranch
}
+ gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
+ if err != nil {
+ return nil, err
+ }
+ defer closer.Close()
+
// oldBranch must exist for this operation
- if _, err := repo_service.GetBranch(repo, opts.OldBranch); err != nil {
+ if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil {
return nil, err
}
@@ -143,7 +149,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User,
// Check to make sure the branch does not already exist, otherwise we can't proceed.
// If we aren't branching to a new branch, make sure user can commit to the given branch
if opts.NewBranch != opts.OldBranch {
- existingBranch, err := repo_service.GetBranch(repo, opts.NewBranch)
+ existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
if existingBranch != nil {
return nil, models.ErrBranchAlreadyExists{
BranchName: opts.NewBranch,
@@ -152,7 +158,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User,
if err != nil && !git.IsErrBranchNotExist(err) {
return nil, err
}
- } else if err := VerifyBranchProtection(repo, doer, opts.OldBranch, opts.TreePath); err != nil {
+ } else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, opts.TreePath); err != nil {
return nil, err
}
@@ -180,7 +186,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User,
author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)
- t, err := NewTemporaryUploadRepository(repo)
+ t, err := NewTemporaryUploadRepository(ctx, repo)
if err != nil {
log.Error("%v", err)
}
@@ -435,7 +441,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User,
return nil, err
}
- file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
+ file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath)
if err != nil {
return nil, err
}
@@ -443,7 +449,7 @@ func CreateOrUpdateRepoFile(repo *repo_model.Repository, doer *user_model.User,
}
// VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch
-func VerifyBranchProtection(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(repo.ID, branchName)
if err != nil {
return err
@@ -460,7 +466,7 @@ func VerifyBranchProtection(repo *repo_model.Repository, doer *user_model.User,
}
}
if protectedBranch.RequireSignedCommits {
- _, _, _, err := asymkey_service.SignCRUDAction(repo.RepoPath(), doer, repo.RepoPath(), branchName)
+ _, _, _, err := asymkey_service.SignCRUDAction(ctx, repo.RepoPath(), doer, repo.RepoPath(), branchName)
if err != nil {
if !asymkey_service.IsErrWontSign(err) {
return err