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
|
package repository
import (
"bufio"
"bytes"
"errors"
"io"
"math"
"regexp"
"gitlab.com/gitlab-org/gitaly/v14/internal/command"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper/lines"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v14/streamio"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
surroundContext = "2"
// searchFilesFilterMaxLength controls the maximum length of the regular
// expression to thwart excessive resource usage when filtering
searchFilesFilterMaxLength = 1000
)
var contentDelimiter = []byte("--\n")
func (s *server) SearchFilesByContent(req *gitalypb.SearchFilesByContentRequest, stream gitalypb.RepositoryService_SearchFilesByContentServer) error {
if err := validateSearchFilesRequest(req); err != nil {
return helper.DecorateError(codes.InvalidArgument, err)
}
repo := req.GetRepository()
if repo == nil {
return status.Errorf(codes.InvalidArgument, "SearchFilesByContent: empty Repository")
}
ctx := stream.Context()
cmd, err := s.gitCmdFactory.New(ctx, repo,
git.SubCmd{Name: "grep", Flags: []git.Option{
git.Flag{Name: "--ignore-case"},
git.Flag{Name: "-I"},
git.Flag{Name: "--line-number"},
git.Flag{Name: "--null"},
git.ValueFlag{Name: "--before-context", Value: surroundContext},
git.ValueFlag{Name: "--after-context", Value: surroundContext},
git.Flag{Name: "--perl-regexp"},
git.Flag{Name: "-e"}}, Args: []string{req.GetQuery(), string(req.GetRef())}})
if err != nil {
return status.Errorf(codes.Internal, "SearchFilesByContent: cmd start failed: %v", err)
}
if err = sendSearchFilesResultChunked(cmd, stream); err != nil {
return status.Errorf(codes.Internal, "SearchFilesByContent: sending chunked response failed: %v", err)
}
return nil
}
func sendMatchInChunks(buf []byte, stream gitalypb.RepositoryService_SearchFilesByContentServer) error {
sw := streamio.NewWriter(func(p []byte) error {
return stream.Send(&gitalypb.SearchFilesByContentResponse{MatchData: p})
})
if _, err := io.Copy(sw, bytes.NewReader(buf)); err != nil {
return err
}
return stream.Send(&gitalypb.SearchFilesByContentResponse{EndOfMatch: true})
}
func sendSearchFilesResultChunked(cmd *command.Command, stream gitalypb.RepositoryService_SearchFilesByContentServer) error {
var buf []byte
scanner := bufio.NewScanner(cmd)
for scanner.Scan() {
// Intentionally avoid scanner.Bytes() because that returns a []byte that
// becomes invalid on the next loop iteration, and we want to hold on to
// the contents of the current line for a while. Scanner.Text() is a
// string and hence immutable.
line := scanner.Text() + "\n"
if line == string(contentDelimiter) {
if err := sendMatchInChunks(buf, stream); err != nil {
return err
}
buf = nil
continue
}
buf = append(buf, line...)
}
if len(buf) > 0 {
return sendMatchInChunks(buf, stream)
}
return nil
}
func (s *server) SearchFilesByName(req *gitalypb.SearchFilesByNameRequest, stream gitalypb.RepositoryService_SearchFilesByNameServer) error {
if err := validateSearchFilesRequest(req); err != nil {
return helper.DecorateError(codes.InvalidArgument, err)
}
var filter *regexp.Regexp
if req.GetFilter() != "" {
if len(req.GetFilter()) > searchFilesFilterMaxLength {
return helper.ErrInvalidArgumentf("SearchFilesByName: filter exceeds maximum length")
}
var err error
filter, err = regexp.Compile(req.GetFilter())
if err != nil {
return helper.ErrInvalidArgumentf("SearchFilesByName: filter did not compile: %v", err)
}
}
repo := req.GetRepository()
if repo == nil {
return status.Errorf(codes.InvalidArgument, "SearchFilesByName: empty Repository")
}
ctx := stream.Context()
cmd, err := s.gitCmdFactory.New(
ctx,
repo,
git.SubCmd{Name: "ls-tree", Flags: []git.Option{
git.Flag{Name: "--full-tree"},
git.Flag{Name: "--name-status"},
git.Flag{Name: "-r"}}, Args: []string{string(req.GetRef()), req.GetQuery()}})
if err != nil {
return status.Errorf(codes.Internal, "SearchFilesByName: cmd start failed: %v", err)
}
lr := func(objs [][]byte) error {
return stream.Send(&gitalypb.SearchFilesByNameResponse{Files: objs})
}
return lines.Send(cmd, lr, lines.SenderOpts{Delimiter: '\n', Limit: math.MaxInt32, Filter: filter})
}
type searchFilesRequest interface {
GetRef() []byte
GetQuery() string
}
func validateSearchFilesRequest(req searchFilesRequest) error {
if len(req.GetQuery()) == 0 {
return errors.New("no query given")
}
if len(req.GetRef()) == 0 {
return errors.New("no ref given")
}
if bytes.HasPrefix(req.GetRef(), []byte("-")) {
return errors.New("invalid ref argument")
}
return nil
}
|