@@ -7,6 +7,7 @@ package git
7
7
import (
8
8
"bufio"
9
9
"bytes"
10
+ "context"
10
11
"io"
11
12
"math"
12
13
"strconv"
@@ -15,31 +16,36 @@ import (
15
16
16
17
// CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
17
18
func CatFileBatch (repoPath string ) (* io.PipeWriter , * bufio.Reader , func ()) {
18
- // Next feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
19
+ // We often want to feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
19
20
// so let's create a batch stdin and stdout
20
21
batchStdinReader , batchStdinWriter := io .Pipe ()
21
22
batchStdoutReader , batchStdoutWriter := io .Pipe ()
23
+ ctx , ctxCancel := context .WithCancel (DefaultContext )
24
+ closed := make (chan struct {})
22
25
cancel := func () {
23
26
_ = batchStdinReader .Close ()
24
27
_ = batchStdinWriter .Close ()
25
28
_ = batchStdoutReader .Close ()
26
29
_ = batchStdoutWriter .Close ()
30
+ ctxCancel ()
31
+ <- closed
27
32
}
28
33
29
34
go func () {
30
35
stderr := strings.Builder {}
31
- err := NewCommand ( "cat-file" , "--batch" ).RunInDirFullPipeline (repoPath , batchStdoutWriter , & stderr , batchStdinReader )
36
+ err := NewCommandContext ( ctx , "cat-file" , "--batch" ).RunInDirFullPipeline (repoPath , batchStdoutWriter , & stderr , batchStdinReader )
32
37
if err != nil {
33
38
_ = batchStdoutWriter .CloseWithError (ConcatenateError (err , (& stderr ).String ()))
34
39
_ = batchStdinReader .CloseWithError (ConcatenateError (err , (& stderr ).String ()))
35
40
} else {
36
41
_ = batchStdoutWriter .Close ()
37
42
_ = batchStdinReader .Close ()
38
43
}
44
+ close (closed )
39
45
}()
40
46
41
47
// For simplicities sake we'll us a buffered reader to read from the cat-file --batch
42
- batchReader := bufio .NewReader (batchStdoutReader )
48
+ batchReader := bufio .NewReaderSize (batchStdoutReader , 32 * 1024 )
43
49
44
50
return batchStdinWriter , batchReader , cancel
45
51
}
0 commit comments