1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package catfile
import (
"bufio"
"context"
"fmt"
"io"
"sync"
"gitlab.com/gitlab-org/gitaly/internal/git"
)
// batchCheck encapsulates a 'git cat-file --batch-check' process
type batchCheck struct {
r *bufio.Reader
w io.WriteCloser
sync.Mutex
}
func newBatchCheck(ctx context.Context, repoPath string, env []string) (*batchCheck, error) {
bc := &batchCheck{}
var stdinReader io.Reader
stdinReader, bc.w = io.Pipe()
batchCmdArgs := []string{"--git-dir", repoPath, "cat-file", "--batch-check"}
batchCmd, err := git.BareCommand(ctx, stdinReader, nil, nil, env, batchCmdArgs...)
if err != nil {
return nil, err
}
bc.r = bufio.NewReader(batchCmd)
go func() {
<-ctx.Done()
// This is crucial to prevent leaking file descriptors.
bc.w.Close()
}()
if injectSpawnErrors {
// Testing only: intentionally leak process
return nil, &simulatedBatchSpawnError{}
}
return bc, nil
}
func (bc *batchCheck) info(spec string) (*ObjectInfo, error) {
bc.Lock()
defer bc.Unlock()
if _, err := fmt.Fprintln(bc.w, spec); err != nil {
return nil, err
}
return ParseObjectInfo(bc.r)
}
|