@ -7,6 +7,7 @@ package git
import (
import (
"bufio"
"bufio"
"bytes"
"bytes"
"context"
"io"
"io"
"math"
"math"
"strconv"
"strconv"
@ -15,20 +16,24 @@ import (
// CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
// CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
func CatFileBatch ( repoPath string ) ( * io . PipeWriter , * bufio . Reader , func ( ) ) {
func CatFileBatch ( repoPath string ) ( * io . PipeWriter , * bufio . Reader , func ( ) ) {
// Next feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
// We often want to feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
// so let's create a batch stdin and stdout
// so let's create a batch stdin and stdout
batchStdinReader , batchStdinWriter := io . Pipe ( )
batchStdinReader , batchStdinWriter := io . Pipe ( )
batchStdoutReader , batchStdoutWriter := io . Pipe ( )
batchStdoutReader , batchStdoutWriter := io . Pipe ( )
ctx , ctxCancel := context . WithCancel ( DefaultContext )
closed := make ( chan struct { } )
cancel := func ( ) {
cancel := func ( ) {
_ = batchStdinReader . Close ( )
_ = batchStdinReader . Close ( )
_ = batchStdinWriter . Close ( )
_ = batchStdinWriter . Close ( )
_ = batchStdoutReader . Close ( )
_ = batchStdoutReader . Close ( )
_ = batchStdoutWriter . Close ( )
_ = batchStdoutWriter . Close ( )
ctxCancel ( )
<- closed
}
}
go func ( ) {
go func ( ) {
stderr := strings . Builder { }
stderr := strings . Builder { }
err := NewCommand ( "cat-file" , "--batch" ) . RunInDirFullPipeline ( repoPath , batchStdoutWriter , & stderr , batchStdinReader )
err := NewCommandContext ( ctx , "cat-file" , "--batch" ) . RunInDirFullPipeline ( repoPath , batchStdoutWriter , & stderr , batchStdinReader )
if err != nil {
if err != nil {
_ = batchStdoutWriter . CloseWithError ( ConcatenateError ( err , ( & stderr ) . String ( ) ) )
_ = batchStdoutWriter . CloseWithError ( ConcatenateError ( err , ( & stderr ) . String ( ) ) )
_ = batchStdinReader . CloseWithError ( ConcatenateError ( err , ( & stderr ) . String ( ) ) )
_ = batchStdinReader . CloseWithError ( ConcatenateError ( err , ( & stderr ) . String ( ) ) )
@ -36,10 +41,11 @@ func CatFileBatch(repoPath string) (*io.PipeWriter, *bufio.Reader, func()) {
_ = batchStdoutWriter . Close ( )
_ = batchStdoutWriter . Close ( )
_ = batchStdinReader . Close ( )
_ = batchStdinReader . Close ( )
}
}
close ( closed )
} ( )
} ( )
// For simplicities sake we'll us a buffered reader to read from the cat-file --batch
// For simplicities sake we'll us a buffered reader to read from the cat-file --batch
batchReader := bufio . NewReader ( batchStdoutReader )
batchReader := bufio . NewReaderSize ( batchStdoutReader , 32 * 1024 )
return batchStdinWriter , batchReader , cancel
return batchStdinWriter , batchReader , cancel
}
}