@ -9,6 +9,7 @@ package git
import (
import (
"bufio"
"bufio"
"bytes"
"bytes"
"context"
"fmt"
"fmt"
"io"
"io"
"math"
"math"
@ -18,7 +19,7 @@ import (
)
)
// GetCommitsInfo gets information of all commits that are corresponding to these entries
// 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 , c ommit * Commit , treePath string , cache * LastCommitCache ) ( [ ] CommitInfo , * Commit , error ) {
entryPaths := make ( [ ] string , len ( tes ) + 1 )
entryPaths := make ( [ ] string , len ( tes ) + 1 )
// Get the commit for the treePath itself
// Get the commit for the treePath itself
entryPaths [ 0 ] = ""
entryPaths [ 0 ] = ""
@ -31,13 +32,13 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
var revs map [ string ] * Commit
var revs map [ string ] * Commit
if cache != nil {
if cache != nil {
var unHitPaths [ ] string
var unHitPaths [ ] string
revs , unHitPaths , err = getLastCommitForPathsByCache ( commit . ID . String ( ) , treePath , entryPaths , cache )
revs , unHitPaths , err = getLastCommitForPathsByCache ( ctx , c ommit . ID . String ( ) , treePath , entryPaths , cache )
if err != nil {
if err != nil {
return nil , nil , err
return nil , nil , err
}
}
if len ( unHitPaths ) > 0 {
if len ( unHitPaths ) > 0 {
sort . Strings ( unHitPaths )
sort . Strings ( unHitPaths )
commits , err := GetLastCommitForPaths ( commit , treePath , unHitPaths )
commits , err := GetLastCommitForPaths ( ctx , c ommit , treePath , unHitPaths )
if err != nil {
if err != nil {
return nil , nil , err
return nil , nil , err
}
}
@ -53,7 +54,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
sort . Strings ( entryPaths )
sort . Strings ( entryPaths )
revs = map [ string ] * Commit { }
revs = map [ string ] * Commit { }
var foundCommits [ ] * Commit
var foundCommits [ ] * Commit
foundCommits , err = GetLastCommitForPaths ( commit , treePath , entryPaths )
foundCommits , err = GetLastCommitForPaths ( ctx , c ommit , treePath , entryPaths )
for i , found := range foundCommits {
for i , found := range foundCommits {
revs [ entryPaths [ i ] ] = found
revs [ entryPaths [ i ] ] = found
}
}
@ -101,7 +102,7 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
return commitsInfo , treeCommit , nil
return commitsInfo , treeCommit , nil
}
}
func getLastCommitForPathsByCache ( commitID , treePath string , paths [ ] string , cache * LastCommitCache ) ( map [ string ] * Commit , [ ] string , error ) {
func getLastCommitForPathsByCache ( ctx context . Context , c ommitID , treePath string , paths [ ] string , cache * LastCommitCache ) ( map [ string ] * Commit , [ ] string , error ) {
wr , rd , cancel := cache . repo . CatFileBatch ( )
wr , rd , cancel := cache . repo . CatFileBatch ( )
defer cancel ( )
defer cancel ( )
@ -124,7 +125,7 @@ func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cac
}
}
// GetLastCommitForPaths returns last commit information
// GetLastCommitForPaths returns last commit information
func GetLastCommitForPaths ( commit * Commit , treePath string , paths [ ] string ) ( [ ] * Commit , error ) {
func GetLastCommitForPaths ( ctx context . Context , c ommit * Commit , treePath string , paths [ ] string ) ( [ ] * Commit , error ) {
// We read backwards from the commit to obtain all of the commits
// 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
// 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 ( ) {
go func ( ) {
stderr := strings . Builder { }
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 {
if err != nil {
_ = revListWriter . CloseWithError ( ConcatenateError ( err , ( & stderr ) . String ( ) ) )
_ = revListWriter . CloseWithError ( ConcatenateError ( err , ( & stderr ) . String ( ) ) )
} else {
} else {
@ -202,6 +203,11 @@ revListLoop:
treeReadingLoop :
treeReadingLoop :
for {
for {
select {
case <- ctx . Done ( ) :
return nil , ctx . Err ( )
default :
}
_ , _ , size , err := ReadBatchLine ( batchReader )
_ , _ , size , err := ReadBatchLine ( batchReader )
if err != nil {
if err != nil {
return nil , err
return nil , err
@ -321,6 +327,9 @@ revListLoop:
}
}
}
}
}
}
if scan . Err ( ) != nil {
return nil , scan . Err ( )
}
commitsMap := make ( map [ string ] * Commit , len ( commits ) )
commitsMap := make ( map [ string ] * Commit , len ( commits ) )
commitsMap [ commit . ID . String ( ) ] = commit
commitsMap [ commit . ID . String ( ) ] = commit