Skip to content

Commit a7276d9

Browse files
committed
more propagation
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 1d4cbbd commit a7276d9

File tree

17 files changed

+52
-48
lines changed

17 files changed

+52
-48
lines changed

modules/context/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler {
328328
// For API calls.
329329
if ctx.Repo.GitRepo == nil {
330330
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
331-
gitRepo, err := git.OpenRepository(repoPath)
331+
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
332332
if err != nil {
333333
ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
334334
return
@@ -386,7 +386,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
386386

387387
if ctx.Repo.GitRepo == nil {
388388
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
389-
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
389+
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath)
390390
if err != nil {
391391
ctx.InternalServerError(err)
392392
return

routers/api/v1/repo/blob.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func GetBlob(ctx *context.APIContext) {
4545
ctx.Error(http.StatusBadRequest, "", "sha not provided")
4646
return
4747
}
48-
if blob, err := files_service.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil {
48+
if blob, err := files_service.GetBlobBySHA(ctx, ctx.Repo.Repository, sha); err != nil {
4949
ctx.Error(http.StatusBadRequest, "", err)
5050
} else {
5151
ctx.JSON(http.StatusOK, blob)

routers/api/v1/repo/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func GetContents(ctx *context.APIContext) {
554554
treePath := ctx.Params("*")
555555
ref := ctx.FormTrim("ref")
556556

557-
if fileList, err := files_service.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
557+
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
558558
if git.IsErrNotExist(err) {
559559
ctx.NotFound("GetContentsOrList", err)
560560
return

routers/api/v1/repo/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewCommitStatus(ctx *context.APIContext) {
6262
Description: form.Description,
6363
Context: form.Context,
6464
}
65-
if err := files_service.CreateCommitStatus(ctx.Repo.Repository, ctx.User, sha, status); err != nil {
65+
if err := files_service.CreateCommitStatus(ctx, ctx.Repo.Repository, ctx.User, sha, status); err != nil {
6666
ctx.Error(http.StatusInternalServerError, "CreateCommitStatus", err)
6767
return
6868
}

routers/api/v1/repo/tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func GetTree(ctx *context.APIContext) {
6060
ctx.Error(http.StatusBadRequest, "", "sha not provided")
6161
return
6262
}
63-
if tree, err := files_service.GetTreeBySHA(ctx.Repo.Repository, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil {
63+
if tree, err := files_service.GetTreeBySHA(ctx, ctx.Repo.Repository, sha, ctx.FormInt("page"), ctx.FormInt("per_page"), ctx.FormBool("recursive")); err != nil {
6464
ctx.Error(http.StatusBadRequest, "", err.Error())
6565
} else {
6666
ctx.JSON(http.StatusOK, tree)

routers/api/v1/repo/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
456456
// findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error.
457457
// The caller is responsible for closing the returned repo again
458458
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
459-
wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath())
459+
wikiRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath())
460460
if err != nil {
461461

462462
if git.IsErrNotExist(err) || err.Error() == "no such file or directory" {

services/migrations/gitea_uploader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
132132
if err != nil {
133133
return err
134134
}
135-
g.gitRepo, err = git.OpenRepository(r.RepoPath())
135+
g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, r.RepoPath())
136136
return err
137137
}
138138

services/repository/adopt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
120120
}
121121

122122
repo.IsEmpty = false
123-
gitRepo, err := git.OpenRepository(repo.RepoPath())
123+
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
124124
if err != nil {
125125
return fmt.Errorf("openRepository: %v", err)
126126
}

services/repository/files/commit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package files
66

77
import (
8+
"context"
89
"fmt"
910

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

2223
// confirm that commit is exist
23-
gitRepo, err := git.OpenRepository(repoPath)
24+
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
2425
if err != nil {
2526
return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err)
2627
}

services/repository/files/content.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package files
66

77
import (
8+
"context"
89
"fmt"
910
"net/url"
1011
"path"
@@ -38,7 +39,7 @@ func (ct *ContentType) String() string {
3839

3940
// GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree
4041
// directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag
41-
func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface{}, error) {
42+
func GetContentsOrList(ctx context.Context, repo *models.Repository, treePath, ref string) (interface{}, error) {
4243
if repo.IsEmpty {
4344
return make([]interface{}, 0), nil
4445
}
@@ -56,7 +57,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
5657
}
5758
treePath = cleanTreePath
5859

59-
gitRepo, err := git.OpenRepository(repo.RepoPath())
60+
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
6061
if err != nil {
6162
return nil, err
6263
}
@@ -74,7 +75,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
7475
}
7576

7677
if entry.Type() != "tree" {
77-
return GetContents(repo, treePath, origRef, false)
78+
return GetContents(ctx, repo, treePath, origRef, false)
7879
}
7980

8081
// We are in a directory, so we return a list of FileContentResponse objects
@@ -90,7 +91,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
9091
}
9192
for _, e := range entries {
9293
subTreePath := path.Join(treePath, e.Name())
93-
fileContentResponse, err := GetContents(repo, subTreePath, origRef, true)
94+
fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true)
9495
if err != nil {
9596
return nil, err
9697
}
@@ -100,7 +101,7 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
100101
}
101102

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

118-
gitRepo, err := git.OpenRepository(repo.RepoPath())
119+
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
119120
if err != nil {
120121
return nil, err
121122
}
@@ -162,7 +163,7 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*
162163
// Now populate the rest of the ContentsResponse based on entry type
163164
if entry.IsRegular() || entry.IsExecutable() {
164165
contentsResponse.Type = string(ContentTypeRegular)
165-
if blobResponse, err := GetBlobBySHA(repo, entry.ID.String()); err != nil {
166+
if blobResponse, err := GetBlobBySHA(ctx, repo, entry.ID.String()); err != nil {
166167
return nil, err
167168
} else if !forList {
168169
// We don't show the content if we are getting a list of FileContentResponses
@@ -218,8 +219,8 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*
218219
}
219220

220221
// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
221-
func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
222-
gitRepo, err := git.OpenRepository(repo.RepoPath())
222+
func GetBlobBySHA(ctx context.Context, repo *models.Repository, sha string) (*api.GitBlobResponse, error) {
223+
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
223224
if err != nil {
224225
return nil, err
225226
}

services/repository/files/content_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ func TestGetContents(t *testing.T) {
6363

6464
expectedContentsResponse := getExpectedReadmeContentsResponse()
6565

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

72-
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) {
73-
fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false)
72+
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) {
73+
fileContentResponse, err := GetContents(ctx, ctx.Repo.Repository, treePath, "", false)
7474
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
7575
assert.NoError(t, err)
7676
})
@@ -98,14 +98,14 @@ func TestGetContentsOrListForDir(t *testing.T) {
9898
readmeContentsResponse,
9999
}
100100

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

107-
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) {
108-
fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
107+
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) {
108+
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
109109
assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
110110
assert.NoError(t, err)
111111
})
@@ -126,14 +126,14 @@ func TestGetContentsOrListForFile(t *testing.T) {
126126

127127
expectedContentsResponse := getExpectedReadmeContentsResponse()
128128

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

135-
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) {
136-
fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
135+
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) {
136+
fileContentResponse, err := GetContentsOrList(ctx, ctx.Repo.Repository, treePath, "")
137137
assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
138138
assert.NoError(t, err)
139139
})
@@ -155,15 +155,15 @@ func TestGetContentsErrors(t *testing.T) {
155155

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

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

187187
t.Run("bad treePath", func(t *testing.T) {
188188
badTreePath := "bad/tree.md"
189-
fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref)
189+
fileContentResponse, err := GetContentsOrList(ctx, repo, badTreePath, ref)
190190
assert.Error(t, err)
191191
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
192192
assert.Nil(t, fileContentResponse)
193193
})
194194

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

215215
t.Run("empty repo", func(t *testing.T) {
216-
contents, err := GetContentsOrList(repo, "", "")
216+
contents, err := GetContentsOrList(ctx, repo, "", "")
217217
assert.NoError(t, err)
218218
assert.Empty(t, contents)
219219
})
@@ -232,7 +232,7 @@ func TestGetBlobBySHA(t *testing.T) {
232232
ctx.SetParams(":id", "1")
233233
ctx.SetParams(":sha", sha)
234234

235-
gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Params(":sha"))
235+
gbr, err := GetBlobBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha"))
236236
expectedGBR := &api.GitBlobResponse{
237237
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK",
238238
Encoding: "base64",

services/repository/files/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func DeleteRepoFile(ctx context.Context, repo *models.Repository, doer *user_mod
191191
return nil, err
192192
}
193193

194-
file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
194+
file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath)
195195
if err != nil {
196196
return nil, err
197197
}

services/repository/files/file.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package files
66

77
import (
8+
"context"
89
"fmt"
910
"net/url"
1011
"path"
@@ -18,9 +19,9 @@ import (
1819
)
1920

2021
// GetFileResponseFromCommit Constructs a FileResponse from a Commit object
21-
func GetFileResponseFromCommit(repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
22-
fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil
23-
fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
22+
func GetFileResponseFromCommit(ctx context.Context, repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
23+
fileContents, _ := GetContents(ctx, repo, treeName, branch, false) // ok if fails, then will be nil
24+
fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
2425
verification := GetPayloadCommitVerification(commit)
2526
fileResponse := &api.FileResponse{
2627
Content: fileContents,

services/repository/files/file_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ func TestGetFileResponseFromCommit(t *testing.T) {
109109
repo := ctx.Repo.Repository
110110
branch := repo.DefaultBranch
111111
treePath := "README.md"
112-
gitRepo, _ := git.OpenRepository(repo.RepoPath())
112+
gitRepo, _ := git.OpenRepositoryCtx(ctx, repo.RepoPath())
113113
defer gitRepo.Close()
114114
commit, _ := gitRepo.GetBranchCommit(branch)
115115
expectedFileResponse := getExpectedFileResponse()
116116

117-
fileResponse, err := GetFileResponseFromCommit(repo, commit, branch, treePath)
117+
fileResponse, err := GetFileResponseFromCommit(ctx, repo, commit, branch, treePath)
118118
assert.NoError(t, err)
119119
assert.EqualValues(t, expectedFileResponse, fileResponse)
120120
}

services/repository/files/tree.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package files
66

77
import (
8+
"context"
89
"fmt"
910
"net/url"
1011

@@ -15,8 +16,8 @@ import (
1516
)
1617

1718
// GetTreeBySHA get the GitTreeResponse of a repository using a sha hash.
18-
func GetTreeBySHA(repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) {
19-
gitRepo, err := git.OpenRepository(repo.RepoPath())
19+
func GetTreeBySHA(ctx context.Context, repo *models.Repository, sha string, page, perPage int, recursive bool) (*api.GitTreeResponse, error) {
20+
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
2021
if err != nil {
2122
return nil, err
2223
}

services/repository/files/tree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestGetTreeBySHA(t *testing.T) {
2929
ctx.SetParams(":id", "1")
3030
ctx.SetParams(":sha", sha)
3131

32-
tree, err := GetTreeBySHA(ctx.Repo.Repository, ctx.Params(":sha"), page, perPage, true)
32+
tree, err := GetTreeBySHA(ctx, ctx.Repo.Repository, ctx.Params(":sha"), page, perPage, true)
3333
assert.NoError(t, err)
3434
expectedTree := &api.GitTreeResponse{
3535
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",

services/repository/files/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *models.Repository, doer *
433433
return nil, err
434434
}
435435

436-
file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
436+
file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath)
437437
if err != nil {
438438
return nil, err
439439
}

0 commit comments

Comments
 (0)