@@ -9,6 +9,7 @@ package git
9
9
import (
10
10
"bufio"
11
11
"bytes"
12
+ "context"
12
13
"fmt"
13
14
"io"
14
15
"math"
@@ -18,7 +19,7 @@ import (
18
19
)
19
20
20
21
// 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 ) {
22
23
entryPaths := make ([]string , len (tes )+ 1 )
23
24
// Get the commit for the treePath itself
24
25
entryPaths [0 ] = ""
@@ -31,13 +32,13 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
31
32
var revs map [string ]* Commit
32
33
if cache != nil {
33
34
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 )
35
36
if err != nil {
36
37
return nil , nil , err
37
38
}
38
39
if len (unHitPaths ) > 0 {
39
40
sort .Strings (unHitPaths )
40
- commits , err := GetLastCommitForPaths (commit , treePath , unHitPaths )
41
+ commits , err := GetLastCommitForPaths (ctx , commit , treePath , unHitPaths )
41
42
if err != nil {
42
43
return nil , nil , err
43
44
}
@@ -53,7 +54,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
53
54
sort .Strings (entryPaths )
54
55
revs = map [string ]* Commit {}
55
56
var foundCommits []* Commit
56
- foundCommits , err = GetLastCommitForPaths (commit , treePath , entryPaths )
57
+ foundCommits , err = GetLastCommitForPaths (ctx , commit , treePath , entryPaths )
57
58
for i , found := range foundCommits {
58
59
revs [entryPaths [i ]] = found
59
60
}
@@ -101,7 +102,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
101
102
return commitsInfo , treeCommit , nil
102
103
}
103
104
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 ) {
105
106
wr , rd , cancel := cache .repo .CatFileBatch ()
106
107
defer cancel ()
107
108
@@ -124,7 +125,7 @@ func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cac
124
125
}
125
126
126
127
// 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 ) {
128
129
// We read backwards from the commit to obtain all of the commits
129
130
130
131
// 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) ([]*
136
137
137
138
go func () {
138
139
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 )
140
141
if err != nil {
141
142
_ = revListWriter .CloseWithError (ConcatenateError (err , (& stderr ).String ()))
142
143
} else {
@@ -202,6 +203,11 @@ revListLoop:
202
203
203
204
treeReadingLoop:
204
205
for {
206
+ select {
207
+ case <- ctx .Done ():
208
+ return nil , ctx .Err ()
209
+ default :
210
+ }
205
211
_ , _ , size , err := ReadBatchLine (batchReader )
206
212
if err != nil {
207
213
return nil , err
@@ -321,6 +327,9 @@ revListLoop:
321
327
}
322
328
}
323
329
}
330
+ if scan .Err () != nil {
331
+ return nil , scan .Err ()
332
+ }
324
333
325
334
commitsMap := make (map [string ]* Commit , len (commits ))
326
335
commitsMap [commit .ID .String ()] = commit
0 commit comments