Skip to content

Commit 68a9306

Browse files
committed
add context to DataAsync
1 parent f4fb209 commit 68a9306

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+110
-102
lines changed

modules/actions/workflows.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"bytes"
8+
"context"
89
"io"
910
"strings"
1011

@@ -69,8 +70,8 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
6970
return ret, nil
7071
}
7172

72-
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
73-
f, err := entry.Blob().DataAsync()
73+
func GetContentFromEntry(ctx context.Context, entry *git.TreeEntry) ([]byte, error) {
74+
f, err := entry.Blob().DataAsync(ctx)
7475
if err != nil {
7576
return nil, err
7677
}
@@ -96,6 +97,7 @@ func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) {
9697
}
9798

9899
func DetectWorkflows(
100+
ctx context.Context,
99101
gitRepo *git.Repository,
100102
commit *git.Commit,
101103
triggedEvent webhook_module.HookEventType,
@@ -110,7 +112,7 @@ func DetectWorkflows(
110112
workflows := make([]*DetectedWorkflow, 0, len(entries))
111113
schedules := make([]*DetectedWorkflow, 0, len(entries))
112114
for _, entry := range entries {
113-
content, err := GetContentFromEntry(entry)
115+
content, err := GetContentFromEntry(ctx, entry)
114116
if err != nil {
115117
return nil, nil, err
116118
}
@@ -146,15 +148,15 @@ func DetectWorkflows(
146148
return workflows, schedules, nil
147149
}
148150

149-
func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
151+
func DetectScheduledWorkflows(ctx context.Context, gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
150152
entries, err := ListWorkflows(commit)
151153
if err != nil {
152154
return nil, err
153155
}
154156

155157
wfs := make([]*DetectedWorkflow, 0, len(entries))
156158
for _, entry := range entries {
157-
content, err := GetContentFromEntry(entry)
159+
content, err := GetContentFromEntry(ctx, entry)
158160
if err != nil {
159161
return nil, err
160162
}

modules/fileicon/basic.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
package fileicon
55

66
import (
7+
"context"
78
"html/template"
89

910
"code.gitea.io/gitea/modules/git"
1011
"code.gitea.io/gitea/modules/svg"
1112
)
1213

13-
func BasicThemeIcon(entry *git.TreeEntry) template.HTML {
14+
func BasicThemeIcon(ctx context.Context, entry *git.TreeEntry) template.HTML {
1415
svgName := "octicon-file"
1516
switch {
1617
case entry.IsLink():
1718
svgName = "octicon-file-symlink-file"
18-
if te, err := entry.FollowLink(); err == nil && te.IsDir() {
19+
if te, err := entry.FollowLink(ctx); err == nil && te.IsDir() {
1920
svgName = "octicon-file-directory-symlink"
2021
}
2122
case entry.IsDir():

modules/fileicon/material.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ func (m *MaterialIconProvider) renderFileIconSVG(ctx reqctx.RequestContext, name
8787

8888
func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.TreeEntry) template.HTML {
8989
if m.rules == nil {
90-
return BasicThemeIcon(entry)
90+
return BasicThemeIcon(ctx, entry)
9191
}
9292

9393
if entry.IsLink() {
94-
if te, err := entry.FollowLink(); err == nil && te.IsDir() {
94+
if te, err := entry.FollowLink(ctx); err == nil && te.IsDir() {
9595
// keep the old "octicon-xxx" class name to make some "theme plugin selector" could still work
9696
return svg.RenderHTML("material-folder-symlink", 16, "octicon-file-directory-symlink")
9797
}

modules/git/blame.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (r *BlameReader) Close() error {
132132
func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath string, commit *Commit, file string, bypassBlameIgnore bool) (*BlameReader, error) {
133133
var ignoreRevsFile *string
134134
if DefaultFeatures().CheckVersionAtLeast("2.23") && !bypassBlameIgnore {
135-
ignoreRevsFile = tryCreateBlameIgnoreRevsFile(commit)
135+
ignoreRevsFile = tryCreateBlameIgnoreRevsFile(ctx, commit)
136136
}
137137

138138
cmd := NewCommandNoGlobals("blame", "--porcelain")
@@ -180,13 +180,13 @@ func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath
180180
}, nil
181181
}
182182

183-
func tryCreateBlameIgnoreRevsFile(commit *Commit) *string {
183+
func tryCreateBlameIgnoreRevsFile(ctx context.Context, commit *Commit) *string {
184184
entry, err := commit.GetTreeEntryByPath(".git-blame-ignore-revs")
185185
if err != nil {
186186
return nil
187187
}
188188

189-
r, err := entry.Blob().DataAsync()
189+
r, err := entry.Blob().DataAsync(ctx)
190190
if err != nil {
191191
return nil
192192
}

modules/git/blob.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66

77
import (
88
"bytes"
9+
"context"
910
"encoding/base64"
1011
"errors"
1112
"io"
@@ -22,11 +23,11 @@ func (b *Blob) Name() string {
2223
}
2324

2425
// GetBlobContent Gets the limited content of the blob as raw text
25-
func (b *Blob) GetBlobContent(limit int64) (string, error) {
26+
func (b *Blob) GetBlobContent(ctx context.Context, limit int64) (string, error) {
2627
if limit <= 0 {
2728
return "", nil
2829
}
29-
dataRc, err := b.DataAsync()
30+
dataRc, err := b.DataAsync(ctx)
3031
if err != nil {
3132
return "", err
3233
}
@@ -37,8 +38,8 @@ func (b *Blob) GetBlobContent(limit int64) (string, error) {
3738

3839
// GetBlobLineCount gets line count of the blob.
3940
// It will also try to write the content to w if it's not nil, then we could pre-fetch the content without reading it again.
40-
func (b *Blob) GetBlobLineCount(w io.Writer) (int, error) {
41-
reader, err := b.DataAsync()
41+
func (b *Blob) GetBlobLineCount(ctx context.Context, w io.Writer) (int, error) {
42+
reader, err := b.DataAsync(ctx)
4243
if err != nil {
4344
return 0, err
4445
}
@@ -64,8 +65,8 @@ func (b *Blob) GetBlobLineCount(w io.Writer) (int, error) {
6465
}
6566

6667
// GetBlobContentBase64 Reads the content of the blob with a base64 encode and returns the encoded string
67-
func (b *Blob) GetBlobContentBase64() (string, error) {
68-
dataRc, err := b.DataAsync()
68+
func (b *Blob) GetBlobContentBase64(ctx context.Context) (string, error) {
69+
dataRc, err := b.DataAsync(ctx)
6970
if err != nil {
7071
return "", err
7172
}
@@ -93,8 +94,8 @@ func (b *Blob) GetBlobContentBase64() (string, error) {
9394
}
9495

9596
// GuessContentType guesses the content type of the blob.
96-
func (b *Blob) GuessContentType() (typesniffer.SniffedType, error) {
97-
r, err := b.DataAsync()
97+
func (b *Blob) GuessContentType(ctx context.Context) (typesniffer.SniffedType, error) {
98+
r, err := b.DataAsync(ctx)
9899
if err != nil {
99100
return typesniffer.SniffedType{}, err
100101
}

modules/git/blob_gogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Blob struct {
2323

2424
// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
2525
// Calling the Close function on the result will discard all unread output.
26-
func (b *Blob) DataAsync() (io.ReadCloser, error) {
26+
func (b *Blob) DataAsync(_ context.Context) (io.ReadCloser, error) {
2727
return b.gogitEncodedObj.Reader()
2828
}
2929

modules/git/blob_nogogit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type Blob struct {
2626

2727
// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
2828
// Calling the Close function on the result will discard all unread output.
29-
func (b *Blob) DataAsync() (io.ReadCloser, error) {
30-
wr, rd, cancel, err := b.repo.CatFileBatch(b.repo.Ctx)
29+
func (b *Blob) DataAsync(ctx context.Context) (io.ReadCloser, error) {
30+
wr, rd, cancel, err := b.repo.CatFileBatch(ctx)
3131
if err != nil {
3232
return nil, err
3333
}

modules/git/blob_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestBlob_Data(t *testing.T) {
2323
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
2424
assert.NoError(t, err)
2525

26-
r, err := testBlob.DataAsync()
26+
r, err := testBlob.DataAsync(t.Context())
2727
assert.NoError(t, err)
2828
require.NotNil(t, r)
2929

@@ -48,7 +48,7 @@ func Benchmark_Blob_Data(b *testing.B) {
4848
}
4949

5050
for b.Loop() {
51-
r, err := testBlob.DataAsync()
51+
r, err := testBlob.DataAsync(b.Context())
5252
if err != nil {
5353
b.Fatal(err)
5454
}

modules/git/commit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,13 @@ func (c *Commit) HasFile(filename string) (bool, error) {
328328
}
329329

330330
// GetFileContent reads a file content as a string or returns false if this was not possible
331-
func (c *Commit) GetFileContent(filename string, limit int) (string, error) {
331+
func (c *Commit) GetFileContent(ctx context.Context, filename string, limit int) (string, error) {
332332
entry, err := c.GetTreeEntryByPath(filename)
333333
if err != nil {
334334
return "", err
335335
}
336336

337-
r, err := entry.Blob().DataAsync()
337+
r, err := entry.Blob().DataAsync(ctx)
338338
if err != nil {
339339
return "", err
340340
}

modules/git/commit_info_gogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
8080
} else {
8181
fullPath = entry.Name()
8282
}
83-
if subModule, err := commit.GetSubModule(fullPath); err != nil {
83+
if subModule, err := commit.GetSubModule(ctx, fullPath); err != nil {
8484
return nil, nil, err
8585
} else if subModule != nil {
8686
subModuleURL = subModule.URL

modules/git/commit_info_nogogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
7474
} else {
7575
fullPath = entry.Name()
7676
}
77-
if subModule, err := commit.GetSubModule(fullPath); err != nil {
77+
if subModule, err := commit.GetSubModule(ctx, fullPath); err != nil {
7878
return nil, nil, err
7979
} else if subModule != nil {
8080
subModuleURL = subModule.URL

modules/git/commit_submodule.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
package git
55

6+
import "context"
7+
68
type SubmoduleWebLink struct {
79
RepoWebLink, CommitWebLink string
810
}
911

1012
// GetSubModules get all the submodules of current revision git tree
11-
func (c *Commit) GetSubModules() (*ObjectCache[*SubModule], error) {
13+
func (c *Commit) GetSubModules(ctx context.Context) (*ObjectCache[*SubModule], error) {
1214
if c.submoduleCache != nil {
1315
return c.submoduleCache, nil
1416
}
@@ -21,7 +23,7 @@ func (c *Commit) GetSubModules() (*ObjectCache[*SubModule], error) {
2123
return nil, err
2224
}
2325

24-
rd, err := entry.Blob().DataAsync()
26+
rd, err := entry.Blob().DataAsync(ctx)
2527
if err != nil {
2628
return nil, err
2729
}
@@ -36,8 +38,8 @@ func (c *Commit) GetSubModules() (*ObjectCache[*SubModule], error) {
3638
}
3739

3840
// GetSubModule get the submodule according entry name
39-
func (c *Commit) GetSubModule(entryName string) (*SubModule, error) {
40-
modules, err := c.GetSubModules()
41+
func (c *Commit) GetSubModule(ctx context.Context, entryName string) (*SubModule, error) {
42+
modules, err := c.GetSubModules(ctx)
4143
if err != nil {
4244
return nil, err
4345
}

modules/git/notes_nogogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
5454
}
5555

5656
blob := entry.Blob()
57-
dataRc, err := blob.DataAsync()
57+
dataRc, err := blob.DataAsync(ctx)
5858
if err != nil {
5959
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
6060
return err

modules/git/repo_blob_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestRepository_GetBlob_Found(t *testing.T) {
3030
blob, err := r.GetBlob(testCase.OID)
3131
assert.NoError(t, err)
3232

33-
dataReader, err := blob.DataAsync()
33+
dataReader, err := blob.DataAsync(t.Context())
3434
assert.NoError(t, err)
3535

3636
data, err := io.ReadAll(dataReader)

modules/git/tree_entry.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package git
66

77
import (
8+
"context"
89
"io"
910
"sort"
1011
"strings"
@@ -23,13 +24,13 @@ func (te *TreeEntry) Type() string {
2324
}
2425

2526
// FollowLink returns the entry pointed to by a symlink
26-
func (te *TreeEntry) FollowLink() (*TreeEntry, error) {
27+
func (te *TreeEntry) FollowLink(ctx context.Context) (*TreeEntry, error) {
2728
if !te.IsLink() {
2829
return nil, ErrBadLink{te.Name(), "not a symlink"}
2930
}
3031

3132
// read the link
32-
r, err := te.Blob().DataAsync()
33+
r, err := te.Blob().DataAsync(ctx)
3334
if err != nil {
3435
return nil, err
3536
}
@@ -70,14 +71,14 @@ func (te *TreeEntry) FollowLink() (*TreeEntry, error) {
7071
}
7172

7273
// FollowLinks returns the entry ultimately pointed to by a symlink
73-
func (te *TreeEntry) FollowLinks() (*TreeEntry, error) {
74+
func (te *TreeEntry) FollowLinks(ctx context.Context) (*TreeEntry, error) {
7475
if !te.IsLink() {
7576
return nil, ErrBadLink{te.Name(), "not a symlink"}
7677
}
7778
entry := te
7879
for i := 0; i < 999; i++ {
7980
if entry.IsLink() {
80-
next, err := entry.FollowLink()
81+
next, err := entry.FollowLink(ctx)
8182
if err != nil {
8283
return nil, err
8384
}

modules/issue/template/unmarshal.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func unmarshalFromEntry(ctx context.Context, entry *git.TreeEntry, filename stri
7171
return nil, fmt.Errorf("too large: %v > MaxDisplayFileSize", size)
7272
}
7373

74-
r, err := entry.Blob().DataAsync()
74+
r, err := entry.Blob().DataAsync(ctx)
7575
if err != nil {
7676
return nil, fmt.Errorf("data async: %w", err)
7777
}

modules/templates/util_render.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package templates
55

66
import (
7+
"context"
78
"encoding/hex"
89
"fmt"
910
"html/template"
@@ -181,11 +182,11 @@ func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
181182
textColor, itemColor, itemHTML)
182183
}
183184

184-
func (ut *RenderUtils) RenderFileIcon(entry *git.TreeEntry) template.HTML {
185+
func (ut *RenderUtils) RenderFileIcon(ctx context.Context,entry *git.TreeEntry) template.HTML {
185186
if setting.UI.FileIconTheme == "material" {
186187
return fileicon.DefaultMaterialIconProvider().FileIcon(ut.ctx, entry)
187188
}
188-
return fileicon.BasicThemeIcon(entry)
189+
return fileicon.BasicThemeIcon(ctx,entry)
189190
}
190191

191192
// RenderEmoji renders html text with emoji post processors

routers/api/v1/repo/file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) {
151151
}
152152

153153
// OK, now the blob is known to have at most 1024 bytes we can simply read this in one go (This saves reading it twice)
154-
dataRc, err := blob.DataAsync()
154+
dataRc, err := blob.DataAsync(ctx)
155155
if err != nil {
156156
ctx.APIErrorInternal(err)
157157
return

routers/api/v1/repo/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ func GetIssueConfig(ctx *context.APIContext) {
12301230
// "$ref": "#/responses/RepoIssueConfig"
12311231
// "404":
12321232
// "$ref": "#/responses/notFound"
1233-
issueConfig, _ := issue.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
1233+
issueConfig, _ := issue.GetTemplateConfigFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo)
12341234
ctx.JSON(http.StatusOK, issueConfig)
12351235
}
12361236

@@ -1257,7 +1257,7 @@ func ValidateIssueConfig(ctx *context.APIContext) {
12571257
// "$ref": "#/responses/RepoIssueConfigValidation"
12581258
// "404":
12591259
// "$ref": "#/responses/notFound"
1260-
_, err := issue.GetTemplateConfigFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
1260+
_, err := issue.GetTemplateConfigFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo)
12611261

12621262
if err == nil {
12631263
ctx.JSON(http.StatusOK, api.IssueConfigValidation{Valid: true, Message: ""})

0 commit comments

Comments
 (0)