Skip to content

Commit 51775f6

Browse files
zeripath6543
andauthored
Make commit info cancelable (#16032)
* Make modules/context.Context a context.Context Signed-off-by: Andrew Thornton <art27@cantab.net> * Simplify context calls Signed-off-by: Andrew Thornton <art27@cantab.net> * Set the base context for requests to the HammerContext Signed-off-by: Andrew Thornton <art27@cantab.net> * pass context into get-last-commit Signed-off-by: Andrew Thornton <art27@cantab.net> * Make commit_info cancellable Signed-off-by: Andrew Thornton <art27@cantab.net> * use context as context Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent b6762e2 commit 51775f6

12 files changed

+57
-35
lines changed

modules/git/commit_info_gogit.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package git
88

99
import (
10+
"context"
1011
"path"
1112

1213
"github.com/emirpasic/gods/trees/binaryheap"
@@ -16,7 +17,7 @@ import (
1617
)
1718

1819
// GetCommitsInfo gets information of all commits that are corresponding to these entries
19-
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) {
20+
func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) {
2021
entryPaths := make([]string, len(tes)+1)
2122
// Get the commit for the treePath itself
2223
entryPaths[0] = ""
@@ -42,7 +43,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
4243
return nil, nil, err
4344
}
4445
if len(unHitPaths) > 0 {
45-
revs2, err := GetLastCommitForPaths(c, treePath, unHitPaths)
46+
revs2, err := GetLastCommitForPaths(ctx, c, treePath, unHitPaths)
4647
if err != nil {
4748
return nil, nil, err
4849
}
@@ -55,7 +56,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
5556
}
5657
}
5758
} else {
58-
revs, err = GetLastCommitForPaths(c, treePath, entryPaths)
59+
revs, err = GetLastCommitForPaths(ctx, c, treePath, entryPaths)
5960
}
6061
if err != nil {
6162
return nil, nil, err
@@ -173,7 +174,7 @@ func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cac
173174
}
174175

175176
// GetLastCommitForPaths returns last commit information
176-
func GetLastCommitForPaths(c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) {
177+
func GetLastCommitForPaths(ctx context.Context, c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) {
177178
// We do a tree traversal with nodes sorted by commit time
178179
heap := binaryheap.NewWith(func(a, b interface{}) int {
179180
if a.(*commitAndPaths).commit.CommitTime().Before(b.(*commitAndPaths).commit.CommitTime()) {
@@ -192,6 +193,11 @@ func GetLastCommitForPaths(c cgobject.CommitNode, treePath string, paths []strin
192193
heap.Push(&commitAndPaths{c, paths, initialHashes})
193194

194195
for {
196+
select {
197+
case <-ctx.Done():
198+
return nil, ctx.Err()
199+
default:
200+
}
195201
cIn, ok := heap.Pop()
196202
if !ok {
197203
break

modules/git/commit_info_nogogit.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package git
99
import (
1010
"bufio"
1111
"bytes"
12+
"context"
1213
"fmt"
1314
"io"
1415
"math"
@@ -18,7 +19,7 @@ import (
1819
)
1920

2021
// GetCommitsInfo gets information of all commits that are corresponding to these entries
21-
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) {
22+
func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) {
2223
entryPaths := make([]string, len(tes)+1)
2324
// Get the commit for the treePath itself
2425
entryPaths[0] = ""
@@ -31,13 +32,13 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
3132
var revs map[string]*Commit
3233
if cache != nil {
3334
var unHitPaths []string
34-
revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, cache)
35+
revs, unHitPaths, err = getLastCommitForPathsByCache(ctx, commit.ID.String(), treePath, entryPaths, cache)
3536
if err != nil {
3637
return nil, nil, err
3738
}
3839
if len(unHitPaths) > 0 {
3940
sort.Strings(unHitPaths)
40-
commits, err := GetLastCommitForPaths(commit, treePath, unHitPaths)
41+
commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths)
4142
if err != nil {
4243
return nil, nil, err
4344
}
@@ -53,7 +54,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
5354
sort.Strings(entryPaths)
5455
revs = map[string]*Commit{}
5556
var foundCommits []*Commit
56-
foundCommits, err = GetLastCommitForPaths(commit, treePath, entryPaths)
57+
foundCommits, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths)
5758
for i, found := range foundCommits {
5859
revs[entryPaths[i]] = found
5960
}
@@ -101,7 +102,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
101102
return commitsInfo, treeCommit, nil
102103
}
103104

104-
func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
105+
func getLastCommitForPathsByCache(ctx context.Context, commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
105106
wr, rd, cancel := cache.repo.CatFileBatch()
106107
defer cancel()
107108

@@ -124,7 +125,7 @@ func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cac
124125
}
125126

126127
// GetLastCommitForPaths returns last commit information
127-
func GetLastCommitForPaths(commit *Commit, treePath string, paths []string) ([]*Commit, error) {
128+
func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) ([]*Commit, error) {
128129
// We read backwards from the commit to obtain all of the commits
129130

130131
// We'll do this by using rev-list to provide us with parent commits in order
@@ -136,7 +137,7 @@ func GetLastCommitForPaths(commit *Commit, treePath string, paths []string) ([]*
136137

137138
go func() {
138139
stderr := strings.Builder{}
139-
err := NewCommand("rev-list", "--format=%T", commit.ID.String()).RunInDirPipeline(commit.repo.Path, revListWriter, &stderr)
140+
err := NewCommand("rev-list", "--format=%T", commit.ID.String()).SetParentContext(ctx).RunInDirPipeline(commit.repo.Path, revListWriter, &stderr)
140141
if err != nil {
141142
_ = revListWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
142143
} else {
@@ -202,6 +203,11 @@ revListLoop:
202203

203204
treeReadingLoop:
204205
for {
206+
select {
207+
case <-ctx.Done():
208+
return nil, ctx.Err()
209+
default:
210+
}
205211
_, _, size, err := ReadBatchLine(batchReader)
206212
if err != nil {
207213
return nil, err
@@ -321,6 +327,9 @@ revListLoop:
321327
}
322328
}
323329
}
330+
if scan.Err() != nil {
331+
return nil, scan.Err()
332+
}
324333

325334
commitsMap := make(map[string]*Commit, len(commits))
326335
commitsMap[commit.ID.String()] = commit

modules/git/commit_info_test.go

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

77
import (
8+
"context"
89
"os"
910
"path/filepath"
1011
"testing"
@@ -69,7 +70,7 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
6970
assert.NoError(t, err)
7071
entries, err := tree.ListEntries()
7172
assert.NoError(t, err)
72-
commitsInfo, treeCommit, err := entries.GetCommitsInfo(commit, testCase.Path, nil)
73+
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.Background(), commit, testCase.Path, nil)
7374
assert.NoError(t, err)
7475
if err != nil {
7576
t.FailNow()
@@ -136,7 +137,7 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
136137
b.ResetTimer()
137138
b.Run(benchmark.name, func(b *testing.B) {
138139
for i := 0; i < b.N; i++ {
139-
_, _, err := entries.GetCommitsInfo(commit, "", nil)
140+
_, _, err := entries.GetCommitsInfo(context.Background(), commit, "", nil)
140141
if err != nil {
141142
b.Fatal(err)
142143
}

modules/git/last_commit_cache_gogit.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package git
88

99
import (
10+
"context"
1011
"path"
1112

1213
"github.com/go-git/go-git/v5/plumbing/object"
@@ -60,7 +61,7 @@ func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) {
6061
}
6162

6263
// CacheCommit will cache the commit from the gitRepository
63-
func (c *LastCommitCache) CacheCommit(commit *Commit) error {
64+
func (c *LastCommitCache) CacheCommit(ctx context.Context, commit *Commit) error {
6465

6566
commitNodeIndex, _ := commit.repo.CommitNodeIndex()
6667

@@ -69,10 +70,10 @@ func (c *LastCommitCache) CacheCommit(commit *Commit) error {
6970
return err
7071
}
7172

72-
return c.recursiveCache(index, &commit.Tree, "", 1)
73+
return c.recursiveCache(ctx, index, &commit.Tree, "", 1)
7374
}
7475

75-
func (c *LastCommitCache) recursiveCache(index cgobject.CommitNode, tree *Tree, treePath string, level int) error {
76+
func (c *LastCommitCache) recursiveCache(ctx context.Context, index cgobject.CommitNode, tree *Tree, treePath string, level int) error {
7677
if level == 0 {
7778
return nil
7879
}
@@ -89,7 +90,7 @@ func (c *LastCommitCache) recursiveCache(index cgobject.CommitNode, tree *Tree,
8990
entryMap[entry.Name()] = entry
9091
}
9192

92-
commits, err := GetLastCommitForPaths(index, treePath, entryPaths)
93+
commits, err := GetLastCommitForPaths(ctx, index, treePath, entryPaths)
9394
if err != nil {
9495
return err
9596
}
@@ -103,7 +104,7 @@ func (c *LastCommitCache) recursiveCache(index cgobject.CommitNode, tree *Tree,
103104
if err != nil {
104105
return err
105106
}
106-
if err := c.recursiveCache(index, subTree, entry, level-1); err != nil {
107+
if err := c.recursiveCache(ctx, index, subTree, entry, level-1); err != nil {
107108
return err
108109
}
109110
}

modules/git/last_commit_cache_nogogit.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package git
88

99
import (
1010
"bufio"
11+
"context"
1112
"path"
1213
)
1314

@@ -61,11 +62,11 @@ func (c *LastCommitCache) Get(ref, entryPath string, wr WriteCloserError, rd *bu
6162
}
6263

6364
// CacheCommit will cache the commit from the gitRepository
64-
func (c *LastCommitCache) CacheCommit(commit *Commit) error {
65-
return c.recursiveCache(commit, &commit.Tree, "", 1)
65+
func (c *LastCommitCache) CacheCommit(ctx context.Context, commit *Commit) error {
66+
return c.recursiveCache(ctx, commit, &commit.Tree, "", 1)
6667
}
6768

68-
func (c *LastCommitCache) recursiveCache(commit *Commit, tree *Tree, treePath string, level int) error {
69+
func (c *LastCommitCache) recursiveCache(ctx context.Context, commit *Commit, tree *Tree, treePath string, level int) error {
6970
if level == 0 {
7071
return nil
7172
}
@@ -82,7 +83,7 @@ func (c *LastCommitCache) recursiveCache(commit *Commit, tree *Tree, treePath st
8283
entryMap[entry.Name()] = entry
8384
}
8485

85-
commits, err := GetLastCommitForPaths(commit, treePath, entryPaths)
86+
commits, err := GetLastCommitForPaths(ctx, commit, treePath, entryPaths)
8687
if err != nil {
8788
return err
8889
}
@@ -97,7 +98,7 @@ func (c *LastCommitCache) recursiveCache(commit *Commit, tree *Tree, treePath st
9798
if err != nil {
9899
return err
99100
}
100-
if err := c.recursiveCache(commit, subTree, entry, level-1); err != nil {
101+
if err := c.recursiveCache(ctx, commit, subTree, entry, level-1); err != nil {
101102
return err
102103
}
103104
}

modules/git/notes_gogit.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
package git
88

99
import (
10+
"context"
1011
"io/ioutil"
1112

1213
"github.com/go-git/go-git/v5/plumbing/object"
1314
)
1415

1516
// GetNote retrieves the git-notes data for a given commit.
16-
func GetNote(repo *Repository, commitID string, note *Note) error {
17+
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
1718
notes, err := repo.GetCommit(NotesRef)
1819
if err != nil {
1920
return err
@@ -62,7 +63,7 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
6263
return err
6364
}
6465

65-
lastCommits, err := GetLastCommitForPaths(commitNode, "", []string{path})
66+
lastCommits, err := GetLastCommitForPaths(ctx, commitNode, "", []string{path})
6667
if err != nil {
6768
return err
6869
}

modules/git/notes_nogogit.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
package git
88

99
import (
10+
"context"
1011
"io/ioutil"
1112
"strings"
1213
)
1314

1415
// GetNote retrieves the git-notes data for a given commit.
15-
func GetNote(repo *Repository, commitID string, note *Note) error {
16+
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
1617
notes, err := repo.GetCommit(NotesRef)
1718
if err != nil {
1819
return err
@@ -63,7 +64,7 @@ func GetNote(repo *Repository, commitID string, note *Note) error {
6364
path = path[idx+1:]
6465
}
6566

66-
lastCommits, err := GetLastCommitForPaths(notes, treePath, []string{path})
67+
lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path})
6768
if err != nil {
6869
return err
6970
}

modules/git/notes_test.go

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

77
import (
8+
"context"
89
"path/filepath"
910
"testing"
1011

@@ -18,7 +19,7 @@ func TestGetNotes(t *testing.T) {
1819
defer bareRepo1.Close()
1920

2021
note := Note{}
21-
err = GetNote(bareRepo1, "95bb4d39648ee7e325106df01a621c530863a653", &note)
22+
err = GetNote(context.Background(), bareRepo1, "95bb4d39648ee7e325106df01a621c530863a653", &note)
2223
assert.NoError(t, err)
2324
assert.Equal(t, []byte("Note contents\n"), note.Message)
2425
assert.Equal(t, "Vladimir Panteleev", note.Commit.Author.Name)
@@ -31,10 +32,10 @@ func TestGetNestedNotes(t *testing.T) {
3132
defer repo.Close()
3233

3334
note := Note{}
34-
err = GetNote(repo, "3e668dbfac39cbc80a9ff9c61eb565d944453ba4", &note)
35+
err = GetNote(context.Background(), repo, "3e668dbfac39cbc80a9ff9c61eb565d944453ba4", &note)
3536
assert.NoError(t, err)
3637
assert.Equal(t, []byte("Note 2"), note.Message)
37-
err = GetNote(repo, "ba0a96fa63532d6c5087ecef070b0250ed72fa47", &note)
38+
err = GetNote(context.Background(), repo, "ba0a96fa63532d6c5087ecef070b0250ed72fa47", &note)
3839
assert.NoError(t, err)
3940
assert.Equal(t, []byte("Note 1"), note.Message)
4041
}

modules/repository/cache.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package repository
66

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

1011
"code.gitea.io/gitea/models"
@@ -23,7 +24,7 @@ func getRefName(fullRefName string) string {
2324
}
2425

2526
// CacheRef cachhe last commit information of the branch or the tag
26-
func CacheRef(repo *models.Repository, gitRepo *git.Repository, fullRefName string) error {
27+
func CacheRef(ctx context.Context, repo *models.Repository, gitRepo *git.Repository, fullRefName string) error {
2728
if !setting.CacheService.LastCommit.Enabled {
2829
return nil
2930
}
@@ -43,5 +44,5 @@ func CacheRef(repo *models.Repository, gitRepo *git.Repository, fullRefName stri
4344

4445
commitCache := git.NewLastCommitCache(repo.FullName(), gitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache())
4546

46-
return commitCache.CacheCommit(commit)
47+
return commitCache.CacheCommit(ctx, commit)
4748
}

routers/repo/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func Diff(ctx *context.Context) {
355355
}
356356

357357
note := &git.Note{}
358-
err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
358+
err = git.GetNote(ctx, ctx.Repo.GitRepo, commitID, note)
359359
if err == nil {
360360
ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
361361
ctx.Data["NoteCommit"] = note.Commit

routers/repo/view.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
146146
}
147147

148148
var latestCommit *git.Commit
149-
ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath, c)
149+
ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(ctx, ctx.Repo.Commit, ctx.Repo.TreePath, c)
150150
if err != nil {
151151
ctx.ServerError("GetCommitsInfo", err)
152152
return

services/repository/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
208208
}
209209

210210
// Cache for big repository
211-
if err := repo_module.CacheRef(repo, gitRepo, opts.RefFullName); err != nil {
211+
if err := repo_module.CacheRef(graceful.GetManager().HammerContext(), repo, gitRepo, opts.RefFullName); err != nil {
212212
log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err)
213213
}
214214
} else {

0 commit comments

Comments
 (0)