diff options
author | zeripath <art27@cantab.net> | 2021-06-07 00:44:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-06 19:44:58 -0400 |
commit | 51775f65bc933843199320b040186703a2bb9f51 (patch) | |
tree | 103463ed6f5ab76245c3dcbaa46773b3bf358e6b /modules/git/commit_info_nogogit.go | |
parent | b6762e23060972f0ad35aeab48850064687ea400 (diff) | |
download | gitea-51775f65bc933843199320b040186703a2bb9f51.tar.gz gitea-51775f65bc933843199320b040186703a2bb9f51.zip |
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>
Diffstat (limited to 'modules/git/commit_info_nogogit.go')
-rw-r--r-- | modules/git/commit_info_nogogit.go | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/modules/git/commit_info_nogogit.go b/modules/git/commit_info_nogogit.go index 485271f145..f34bef9f01 100644 --- a/modules/git/commit_info_nogogit.go +++ b/modules/git/commit_info_nogogit.go @@ -9,6 +9,7 @@ package git import ( "bufio" "bytes" + "context" "fmt" "io" "math" @@ -18,7 +19,7 @@ import ( ) // GetCommitsInfo gets information of all commits that are corresponding to these entries -func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) { +func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath string, cache *LastCommitCache) ([]CommitInfo, *Commit, error) { entryPaths := make([]string, len(tes)+1) // Get the commit for the treePath itself entryPaths[0] = "" @@ -31,13 +32,13 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo var revs map[string]*Commit if cache != nil { var unHitPaths []string - revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, cache) + revs, unHitPaths, err = getLastCommitForPathsByCache(ctx, commit.ID.String(), treePath, entryPaths, cache) if err != nil { return nil, nil, err } if len(unHitPaths) > 0 { sort.Strings(unHitPaths) - commits, err := GetLastCommitForPaths(commit, treePath, unHitPaths) + commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths) if err != nil { return nil, nil, err } @@ -53,7 +54,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo sort.Strings(entryPaths) revs = map[string]*Commit{} var foundCommits []*Commit - foundCommits, err = GetLastCommitForPaths(commit, treePath, entryPaths) + foundCommits, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths) for i, found := range foundCommits { revs[entryPaths[i]] = found } @@ -101,7 +102,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo return commitsInfo, treeCommit, nil } -func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) { +func getLastCommitForPathsByCache(ctx context.Context, commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) { wr, rd, cancel := cache.repo.CatFileBatch() defer cancel() @@ -124,7 +125,7 @@ func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cac } // GetLastCommitForPaths returns last commit information -func GetLastCommitForPaths(commit *Commit, treePath string, paths []string) ([]*Commit, error) { +func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) ([]*Commit, error) { // We read backwards from the commit to obtain all of the commits // 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) ([]* go func() { stderr := strings.Builder{} - err := NewCommand("rev-list", "--format=%T", commit.ID.String()).RunInDirPipeline(commit.repo.Path, revListWriter, &stderr) + err := NewCommand("rev-list", "--format=%T", commit.ID.String()).SetParentContext(ctx).RunInDirPipeline(commit.repo.Path, revListWriter, &stderr) if err != nil { _ = revListWriter.CloseWithError(ConcatenateError(err, (&stderr).String())) } else { @@ -202,6 +203,11 @@ revListLoop: treeReadingLoop: for { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } _, _, size, err := ReadBatchLine(batchReader) if err != nil { return nil, err @@ -321,6 +327,9 @@ revListLoop: } } } + if scan.Err() != nil { + return nil, scan.Err() + } commitsMap := make(map[string]*Commit, len(commits)) commitsMap[commit.ID.String()] = commit |