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
|
package commit
import (
"fmt"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/internal/helper/chunker"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var maxTreeEntries = 1000
func validateGetTreeEntriesRequest(in *gitalypb.GetTreeEntriesRequest) error {
if len(in.GetRevision()) == 0 {
return fmt.Errorf("empty Revision")
}
if len(in.GetPath()) == 0 {
return fmt.Errorf("empty Path")
}
return nil
}
func populateFlatPath(c *catfile.Batch, entries []*gitalypb.TreeEntry) error {
for _, entry := range entries {
entry.FlatPath = entry.Path
if entry.Type != gitalypb.TreeEntry_TREE {
continue
}
for {
subentries, err := treeEntries(c, entry.CommitOid, string(entry.FlatPath), "", false)
if err != nil {
return err
}
if len(subentries) != 1 || subentries[0].Type != gitalypb.TreeEntry_TREE {
break
}
entry.FlatPath = subentries[0].Path
}
}
return nil
}
func sendTreeEntries(stream gitalypb.CommitService_GetTreeEntriesServer, c *catfile.Batch, revision, path string, recursive bool) error {
entries, err := treeEntries(c, revision, path, "", recursive)
if err != nil {
return err
}
if !recursive {
if err := populateFlatPath(c, entries); err != nil {
return err
}
}
sender := chunker.New(&treeEntriesSender{stream: stream})
for _, e := range entries {
sender.Send(e)
}
return sender.Flush()
}
type treeEntriesSender struct {
response *gitalypb.GetTreeEntriesResponse
stream gitalypb.CommitService_GetTreeEntriesServer
}
func (c *treeEntriesSender) Append(it chunker.Item) {
c.response.Entries = append(c.response.Entries, it.(*gitalypb.TreeEntry))
}
func (c *treeEntriesSender) Send() error { return c.stream.Send(c.response) }
func (c *treeEntriesSender) Reset() { c.response = &gitalypb.GetTreeEntriesResponse{} }
func (s *server) GetTreeEntries(in *gitalypb.GetTreeEntriesRequest, stream gitalypb.CommitService_GetTreeEntriesServer) error {
grpc_logrus.Extract(stream.Context()).WithFields(log.Fields{
"Revision": in.Revision,
"Path": in.Path,
}).Debug("GetTreeEntries")
if err := validateGetTreeEntriesRequest(in); err != nil {
return status.Errorf(codes.InvalidArgument, "TreeEntry: %v", err)
}
c, err := catfile.New(stream.Context(), in.Repository)
if err != nil {
return err
}
revision := string(in.GetRevision())
path := string(in.GetPath())
return sendTreeEntries(stream, c, revision, path, in.Recursive)
}
|