@@ -9,6 +9,7 @@ package git
99import (
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
0 commit comments