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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
package blob
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/golang/protobuf/proto"
gitaly_errors "gitlab.com/gitlab-org/gitaly/v14/internal/errors"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper/chunk"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"golang.org/x/text/transform"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
// lfsPointerMaxSize is the maximum size for an lfs pointer text blob. This limit is used
// as a heuristic to filter blobs which can't be LFS pointers. The format of these pointers
// is described in https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md#the-pointer.
lfsPointerMaxSize = 200
)
var (
errInvalidRevision = errors.New("invalid revision")
errLimitReached = errors.New("limit reached")
)
// ListLFSPointers finds all LFS pointers which are transitively reachable via a graph walk of the
// given set of revisions.
func (s *server) ListLFSPointers(in *gitalypb.ListLFSPointersRequest, stream gitalypb.BlobService_ListLFSPointersServer) error {
ctx := stream.Context()
if in.GetRepository() == nil {
return status.Error(codes.InvalidArgument, "empty repository")
}
if len(in.Revisions) == 0 {
return status.Error(codes.InvalidArgument, "missing revisions")
}
chunker := chunk.New(&lfsPointerSender{
send: func(pointers []*gitalypb.LFSPointer) error {
return stream.Send(&gitalypb.ListLFSPointersResponse{
LfsPointers: pointers,
})
},
})
repo := s.localrepo(in.GetRepository())
if err := findLFSPointersByRevisions(ctx, repo, s.gitCmdFactory, chunker, int(in.Limit), in.Revisions...); err != nil {
if errors.Is(err, errInvalidRevision) {
return status.Errorf(codes.InvalidArgument, err.Error())
}
if !errors.Is(err, errLimitReached) {
return err
}
}
return nil
}
// ListAllLFSPointers finds all LFS pointers which exist in the repository, including those which
// are not reachable via graph walks.
func (s *server) ListAllLFSPointers(in *gitalypb.ListAllLFSPointersRequest, stream gitalypb.BlobService_ListAllLFSPointersServer) error {
ctx := stream.Context()
if in.GetRepository() == nil {
return status.Error(codes.InvalidArgument, "empty repository")
}
repo := s.localrepo(in.GetRepository())
cmd, err := repo.Exec(ctx, git.SubCmd{
Name: "cat-file",
Flags: []git.Option{
git.Flag{Name: "--batch-all-objects"},
git.Flag{Name: "--batch-check=%(objecttype) %(objectsize) %(objectname)"},
git.Flag{Name: "--buffer"},
git.Flag{Name: "--unordered"},
},
})
if err != nil {
return status.Errorf(codes.Internal, "could not run batch-check: %v", err)
}
chunker := chunk.New(&lfsPointerSender{
send: func(pointers []*gitalypb.LFSPointer) error {
return stream.Send(&gitalypb.ListAllLFSPointersResponse{
LfsPointers: pointers,
})
},
})
filteredReader := transform.NewReader(cmd, blobFilter{
maxSize: lfsPointerMaxSize,
})
if err := readLFSPointers(ctx, repo, chunker, filteredReader, int(in.Limit)); err != nil {
if !errors.Is(err, errLimitReached) {
return status.Errorf(codes.Internal, "could not read LFS pointers: %v", err)
}
}
return nil
}
// GetLFSPointers takes the list of requested blob IDs and filters them down to blobs which are
// valid LFS pointers. It is fine to pass blob IDs which do not point to a valid LFS pointer, but
// passing blob IDs which do not exist results in an error.
func (s *server) GetLFSPointers(req *gitalypb.GetLFSPointersRequest, stream gitalypb.BlobService_GetLFSPointersServer) error {
ctx := stream.Context()
if err := validateGetLFSPointersRequest(req); err != nil {
return status.Errorf(codes.InvalidArgument, "GetLFSPointers: %v", err)
}
repo := s.localrepo(req.GetRepository())
objectIDs := strings.Join(req.BlobIds, "\n")
chunker := chunk.New(&lfsPointerSender{
send: func(pointers []*gitalypb.LFSPointer) error {
return stream.Send(&gitalypb.GetLFSPointersResponse{
LfsPointers: pointers,
})
},
})
if err := readLFSPointers(ctx, repo, chunker, strings.NewReader(objectIDs), 0); err != nil {
if !errors.Is(err, errLimitReached) {
return err
}
}
return nil
}
func validateGetLFSPointersRequest(req *gitalypb.GetLFSPointersRequest) error {
if req.GetRepository() == nil {
return gitaly_errors.ErrEmptyRepository
}
if len(req.GetBlobIds()) == 0 {
return fmt.Errorf("empty BlobIds")
}
return nil
}
// findLFSPointersByRevisions will return all LFS objects reachable via the given set of revisions.
// Revisions accept all syntax supported by git-rev-list(1).
func findLFSPointersByRevisions(
ctx context.Context,
repo *localrepo.Repo,
gitCmdFactory git.CommandFactory,
chunker *chunk.Chunker,
limit int,
revisions ...string,
) (returnErr error) {
for _, revision := range revisions {
if strings.HasPrefix(revision, "-") && revision != "--all" && revision != "--not" {
return fmt.Errorf("%w: %q", errInvalidRevision, revision)
}
}
// git-rev-list(1) currently does not have any way to list all reachable objects of a
// certain type.
var revListStderr bytes.Buffer
revlist, err := repo.Exec(ctx, git.SubCmd{
Name: "rev-list",
Flags: []git.Option{
git.Flag{Name: "--in-commit-order"},
git.Flag{Name: "--objects"},
git.Flag{Name: "--no-object-names"},
git.Flag{Name: fmt.Sprintf("--filter=blob:limit=%d", lfsPointerMaxSize)},
},
Args: revisions,
}, git.WithStderr(&revListStderr))
if err != nil {
return fmt.Errorf("could not execute rev-list: %w", err)
}
defer func() {
// There is no way to properly determine whether the process has exited because of
// us signalling the context or because of any other means. We can only approximate
// this by checking whether the process state is "signal: killed". Which again is
// awful, but given that `Signaled()` status is also not accessible to us,
// it's the best we could do.
//
// Let's not do any of this, it's awful. Instead, we can simply check whether we
// have reached the limit. If so, we found all LFS pointers which the user requested
// and needn't bother whether git-rev-list(1) may have failed. So let's instead just
// have the RPCcontext cancel the process.
if errors.Is(returnErr, errLimitReached) {
return
}
if err := revlist.Wait(); err != nil && returnErr == nil {
returnErr = fmt.Errorf("rev-list failed: %w, stderr: %q",
err, revListStderr.String())
}
}()
return readLFSPointers(ctx, repo, chunker, revlist, limit)
}
// readLFSPointers reads object IDs of potential LFS pointers from the given reader and for each of
// them, it will determine whether the referenced object is an LFS pointer. Objects which are not a
// valid LFS pointer will be ignored. Objects which do not exist result in an error.
func readLFSPointers(
ctx context.Context,
repo *localrepo.Repo,
chunker *chunk.Chunker,
objectIDReader io.Reader,
limit int,
) (returnErr error) {
defer func() {
if err := chunker.Flush(); err != nil && returnErr == nil {
returnErr = err
}
}()
catfileBatch, err := repo.Exec(ctx, git.SubCmd{
Name: "cat-file",
Flags: []git.Option{
git.Flag{Name: "--batch"},
git.Flag{Name: "--buffer"},
},
}, git.WithStdin(objectIDReader))
if err != nil {
return fmt.Errorf("could not execute cat-file: %w", err)
}
var pointersFound int
reader := bufio.NewReader(catfileBatch)
buf := &bytes.Buffer{}
for {
objectInfo, err := catfile.ParseObjectInfo(reader)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Errorf("could not get LFS pointer info: %w", err)
}
// Avoid allocating bytes for an LFS pointer until we know the current
// blob really is an LFS pointer.
buf.Reset()
if _, err := io.CopyN(buf, reader, objectInfo.Size+1); err != nil {
return fmt.Errorf("could not read LFS pointer candidate: %w", err)
}
tempData := buf.Bytes()[:buf.Len()-1]
if objectInfo.Type != "blob" || !git.IsLFSPointer(tempData) {
continue
}
// Now that we know this is an LFS pointer it is not a waste to allocate
// memory.
data := make([]byte, len(tempData))
copy(data, tempData)
if err := chunker.Send(&gitalypb.LFSPointer{
Data: data,
Size: int64(len(data)),
Oid: objectInfo.Oid.String(),
}); err != nil {
return fmt.Errorf("sending LFS pointer chunk: %w", err)
}
pointersFound++
// Exit early in case we've got all LFS pointers. We want to do this here instead of
// just terminating the loop because we need to check git-cat-file(1)'s exit code in
// case the loop finishes successfully via an EOF. We don't want to do so here
// though: we don't care for successful termination of the command, we only care
// that we've got all pointers. The command is then getting cancelled via the
// parent's context.
if limit > 0 && pointersFound >= limit {
return errLimitReached
}
}
if err := catfileBatch.Wait(); err != nil {
return nil
}
return nil
}
type lfsPointerSender struct {
pointers []*gitalypb.LFSPointer
send func([]*gitalypb.LFSPointer) error
}
func (t *lfsPointerSender) Reset() {
t.pointers = t.pointers[:0]
}
func (t *lfsPointerSender) Append(m proto.Message) {
t.pointers = append(t.pointers, m.(*gitalypb.LFSPointer))
}
func (t *lfsPointerSender) Send() error {
return t.send(t.pointers)
}
|