Skip to content

Commit

Permalink
more propagation
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Dec 3, 2021
1 parent 1d4cbbd commit a7276d9
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 48 deletions.
4 changes: 2 additions & 2 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler {
// For API calls.
if ctx.Repo.GitRepo == nil {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
gitRepo, err := git.OpenRepository(repoPath)
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
return
Expand Down Expand Up @@ -386,7 +386,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {

if ctx.Repo.GitRepo == nil {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetBlob(ctx *context.APIContext) {
ctx.Error(http.StatusBadRequest, "", "sha not provided")
return
}
if blob, err := files_service.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil {
if blob, err := files_service.GetBlobBySHA(ctx, ctx.Repo.Repository, sha); err != nil {
ctx.Error(http.StatusBadRequest, "", err)
} else {
ctx.JSON(http.StatusOK, blob)
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func GetContents(ctx *context.APIContext) {
treePath := ctx.Params("*")
ref := ctx.FormTrim("ref")

if fileList, err := files_service.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewCommitStatus(ctx *context.APIContext) {
Description: form.Description,
Context: form.Context,
}
if err := files_service.CreateCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil {
if err := files_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.User, sha, status); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetTree(ctx *context.APIContext) {
ctx.Error(http.StatusBadRequest, "", "sha not provided")
return
}
if tree, err := files_service.GetTreeBySHA(ctx.Repo.Repository, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil {
if tree, err := files_service.GetTreeBySHA(ctx, ctx.Repo.Repository, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil {
ctx.Error(http.StatusBadRequest, "", err.Error())
} else {
ctx.JSON(http.StatusOK, tree)
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
// findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error.
// The caller is responsible for closing the returned repo again
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
wikiRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath())
if err != nil {

if git.IsErrNotExist(err) || err.Error() == "no such file or directory" {
Expand Down
2 changes: 1 addition & 1 deletion services/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
if err != nil {
return err
}
g.gitRepo, err = git.OpenRepository(r.RepoPath())
g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, r.RepoPath())
return err
}

Expand Down
2 changes: 1 addition & 1 deletion services/repository/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
}

repo.IsEmpty = false
gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("openRepository: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions services/repository/files/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package files

import (
"context"
"fmt"

"code.gitea.io/gitea/models"
Expand All @@ -16,11 +17,11 @@ import (
// CreateCommitStatus creates a new CommitStatus given a bunch of parameters
// NOTE: All text-values will be trimmed from whitespaces.
// Requires: Repo, Creator, SHA
func CreateCommitStatus(repo *models.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error {
func CreateCommitStatus(ctx context.Context, repo *models.Repository, creator *user_model.User, sha string, status *models.CommitStatus) error {
repoPath := repo.RepoPath()

// confirm that commit is exist
gitRepo, err := git.OpenRepository(repoPath)
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
if err != nil {
return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err)
}
Expand Down
19 changes: 10 additions & 9 deletions services/repository/files/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package files

import (
"context"
"fmt"
"net/url"
"path"
Expand Down Expand Up @@ -38,7 +39,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 *models.Repository, treePath, ref string) (interface{}, error) {
func GetContentsOrList(ctx context.Context, repo *models.Repository, treePath, ref string) (interface{}, error) {
if repo.IsEmpty {
return make([]interface{}, 0), nil
}
Expand All @@ -56,7 +57,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
}
treePath = cleanTreePath

gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
Expand All @@ -74,7 +75,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
}

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
Expand All @@ -90,7 +91,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
}
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
}
Expand All @@ -100,7 +101,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
}

// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
func GetContents(ctx context.Context, repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
if ref == "" {
ref = repo.DefaultBranch
}
Expand All @@ -115,7 +116,7 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*
}
treePath = cleanTreePath

gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -162,7 +163,7 @@ func GetContents(repo *models.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
Expand Down Expand Up @@ -218,8 +219,8 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*
}

// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
gitRepo, err := git.OpenRepository(repo.RepoPath())
func GetBlobBySHA(ctx context.Context, repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
Expand Down
36 changes: 18 additions & 18 deletions services/repository/files/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func TestGetContents(t *testing.T) {

expectedContentsResponse := getExpectedReadmeContentsResponse()

t.Run("Get README.md contents with GetContents()", func(t *testing.T) {
fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false)
t.Run("Get README.md contents with GetContents(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, ref, false)
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
assert.NoError(t, err)
})

t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents()", func(t *testing.T) {
fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false)
t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContents(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false)
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
assert.NoError(t, err)
})
Expand Down Expand Up @@ -98,14 +98,14 @@ func TestGetContentsOrListForDir(t *testing.T) {
readmeContentsResponse,
}

t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
t.Run("Get root dir contents with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
assert.NoError(t, err)
})

t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
assert.NoError(t, err)
})
Expand All @@ -126,14 +126,14 @@ func TestGetContentsOrListForFile(t *testing.T) {

expectedContentsResponse := getExpectedReadmeContentsResponse()

t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
t.Run("Get README.md contents with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
assert.NoError(t, err)
})

t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
t.Run("Get README.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList(ctx, )", func(t *testing.T) {
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
assert.NoError(t, err)
})
Expand All @@ -155,15 +155,15 @@ func TestGetContentsErrors(t *testing.T) {

t.Run("bad treePath", func(t *testing.T) {
badTreePath := "bad/tree.md"
fileContentResponse, err := GetContents(repo, badTreePath, ref, false)
fileContentResponse, err := GetContents(ctx, repo, badTreePath, ref, false)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
assert.Nil(t, fileContentResponse)
})

t.Run("bad ref", func(t *testing.T) {
badRef := "bad_ref"
fileContentResponse, err := GetContents(repo, treePath, badRef, false)
fileContentResponse, err := GetContents(ctx, repo, treePath, badRef, false)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
assert.Nil(t, fileContentResponse)
Expand All @@ -186,15 +186,15 @@ func TestGetContentsOrListErrors(t *testing.T) {

t.Run("bad treePath", func(t *testing.T) {
badTreePath := "bad/tree.md"
fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref)
fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
assert.Nil(t, fileContentResponse)
})

t.Run("bad ref", func(t *testing.T) {
badRef := "bad_ref"
fileContentResponse, err := GetContentsOrList(repo, treePath, badRef)
fileContentResponse, err := GetContentsOrList(ctx, repo, treePath, badRef)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
assert.Nil(t, fileContentResponse)
Expand All @@ -213,7 +213,7 @@ func TestGetContentsOrListOfEmptyRepos(t *testing.T) {
repo := ctx.Repo.Repository

t.Run("empty repo", func(t *testing.T) {
contents, err := GetContentsOrList(repo, "", "")
contents, err := GetContentsOrList(ctx, repo, "", "")
assert.NoError(t, err)
assert.Empty(t, contents)
})
Expand All @@ -232,7 +232,7 @@ func TestGetBlobBySHA(t *testing.T) {
ctx.SetParams(":id", "1")
ctx.SetParams(":sha", sha)

gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Params(":sha"))
gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha"))
expectedGBR := &api.GitBlobResponse{
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
Encoding: "base64",
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod
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
}
Expand Down
7 changes: 4 additions & 3 deletions services/repository/files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package files

import (
"context"
"fmt"
"net/url"
"path"
Expand All @@ -18,9 +19,9 @@ import (
)

// GetFileResponseFromCommit Constructs a FileResponse from a Commit object
func GetFileResponseFromCommit(repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil
fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
func GetFileResponseFromCommit(ctx context.Context, repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
fileContents, _ := GetContents(ctx, repo, treeName, branch, false) // ok if fails, then will be nil
fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
verification := GetPayloadCommitVerification(commit)
fileResponse := &api.FileResponse{
Content: fileContents,
Expand Down
4 changes: 2 additions & 2 deletions services/repository/files/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func TestGetFileResponseFromCommit(t *testing.T) {
repo := ctx.Repo.Repository
branch := repo.DefaultBranch
treePath := "README.md"
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepositoryCtx(ctx, repo.RepoPath())
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(branch)
expectedFileResponse := getExpectedFileResponse()

fileResponse, err := GetFileResponseFromCommit(repo, commit, branch, treePath)
fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath)
assert.NoError(t, err)
assert.EqualValues(t, expectedFileResponse, fileResponse)
}
5 changes: 3 additions & 2 deletions services/repository/files/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package files

import (
"context"
"fmt"
"net/url"

Expand All @@ -15,8 +16,8 @@ import (
)

// GetTreeBySHA get the GitTreeResponse of a repository using a sha hash.
func GetTreeBySHA(repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) {
gitRepo, err := git.OpenRepository(repo.RepoPath())
func GetTreeBySHA(ctx context.Context, repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) {
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetTreeBySHA(t *testing.T) {
ctx.SetParams(":id", "1")
ctx.SetParams(":sha", sha)

tree, err := GetTreeBySHA(ctx.Repo.Repository, ctx.Params(":sha"), page, perPage, true)
tree, err := GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha"), page, perPage, true)
assert.NoError(t, err)
expectedTree := &api.GitTreeResponse{
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer *
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
}
Expand Down

0 comments on commit a7276d9

Please sign in to comment.