1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

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>
This commit is contained in:
zeripath
2022-01-19 23:26:57 +00:00
committed by GitHub
parent 4563148a61
commit 5cb0c9aa0d
193 changed files with 1264 additions and 1154 deletions

View File

@@ -5,6 +5,7 @@
package files
import (
"context"
"fmt"
"net/url"
"path"
@@ -39,7 +40,7 @@ func (ct *ContentType) String() string {
// GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree
// directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag
func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (interface{}, error) {
func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (interface{}, error) {
if repo.IsEmpty {
return make([]interface{}, 0), nil
}
@@ -57,11 +58,11 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter
}
treePath = cleanTreePath
gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
defer gitRepo.Close()
defer closer.Close()
// Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref)
@@ -75,7 +76,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter
}
if entry.Type() != "tree" {
return GetContents(repo, treePath, origRef, false)
return GetContents(ctx, repo, treePath, origRef, false)
}
// We are in a directory, so we return a list of FileContentResponse objects
@@ -91,7 +92,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter
}
for _, e := range entries {
subTreePath := path.Join(treePath, e.Name())
fileContentResponse, err := GetContents(repo, subTreePath, origRef, true)
fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true)
if err != nil {
return nil, err
}
@@ -101,7 +102,7 @@ func GetContentsOrList(repo *repo_model.Repository, treePath, ref string) (inter
}
// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
if ref == "" {
ref = repo.DefaultBranch
}
@@ -116,11 +117,11 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool
}
treePath = cleanTreePath
gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
defer gitRepo.Close()
defer closer.Close()
// Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref)
@@ -163,7 +164,7 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool
// Now populate the rest of the ContentsResponse based on entry type
if entry.IsRegular() || entry.IsExecutable() {
contentsResponse.Type = string(ContentTypeRegular)
if blobResponse, err := GetBlobBySHA(repo, entry.ID.String()); err != nil {
if blobResponse, err := GetBlobBySHA(ctx, repo, entry.ID.String()); err != nil {
return nil, err
} else if !forList {
// We don't show the content if we are getting a list of FileContentResponses
@@ -219,12 +220,12 @@ func GetContents(repo *repo_model.Repository, treePath, ref string, forList bool
}
// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
func GetBlobBySHA(repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) {
gitRepo, err := git.OpenRepository(repo.RepoPath())
func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, sha string) (*api.GitBlobResponse, error) {
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
defer gitRepo.Close()
defer closer.Close()
gitBlob, err := gitRepo.GetBlob(sha)
if err != nil {
return nil, err