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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package catfile
import (
"bufio"
"context"
"fmt"
"io"
"sync"
"github.com/opentracing/opentracing-go"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/alternates"
"gitlab.com/gitlab-org/gitaly/internal/git/repository"
"gitlab.com/gitlab-org/gitaly/internal/storage"
"gitlab.com/gitlab-org/labkit/correlation"
)
// 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, locator storage.Locator, repo repository.GitRepo) (*batchCheck, error) {
repoPath, err := locator.GetRepoPath(repo)
if err != nil {
return nil, err
}
env := alternates.Env(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories())
bc := &batchCheck{}
var stdinReader io.Reader
stdinReader, bc.w = io.Pipe()
// batch processes are long-lived and reused across RPCs,
// so we de-correlate the process from the RPC
ctx = correlation.ContextWithCorrelation(ctx, "")
ctx = opentracing.ContextWithSpan(ctx, nil)
batchCmd, err := git.SafeBareCmd(ctx, env,
[]git.GlobalOption{
git.ValueFlag{Name: "--git-dir", Value: repoPath},
},
git.SubCmd{
Name: "cat-file",
Flags: []git.Option{
git.Flag{Name: "--batch-check"},
},
},
git.WithStdin(stdinReader),
)
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)
}
|