-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathcommit_trees.go
220 lines (173 loc) · 4.56 KB
/
commit_trees.go
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
package gitbase
import (
"io"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-mysql-server.v0/sql"
)
type commitTreesTable struct{}
// CommitTreesSchema is the schema for the commit trees table.
var CommitTreesSchema = sql.Schema{
{Name: "repository_id", Type: sql.Text, Source: CommitTreesTableName},
{Name: "commit_hash", Type: sql.Text, Source: CommitTreesTableName},
{Name: "tree_hash", Type: sql.Text, Source: CommitTreesTableName},
}
var _ sql.PushdownProjectionAndFiltersTable = (*commitTreesTable)(nil)
func newCommitTreesTable() sql.Table {
return new(commitTreesTable)
}
func (commitTreesTable) isGitbaseTable() {}
func (commitTreesTable) String() string {
return printTable(CommitTreesTableName, CommitTreesSchema)
}
func (commitTreesTable) Resolved() bool { return true }
func (commitTreesTable) Name() string { return CommitTreesTableName }
func (commitTreesTable) Schema() sql.Schema { return CommitTreesSchema }
func (t *commitTreesTable) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
return f(t)
}
func (t *commitTreesTable) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) {
return t, nil
}
func (commitTreesTable) Children() []sql.Node { return nil }
func (commitTreesTable) RowIter(ctx *sql.Context) (sql.RowIter, error) {
span, ctx := ctx.Span("gitbase.CommitTreesTable")
iter, err := NewRowRepoIter(ctx, &commitTreesIter{ctx: ctx})
if err != nil {
span.Finish()
return nil, err
}
return sql.NewSpanIter(span, iter), nil
}
func (commitTreesTable) HandledFilters(filters []sql.Expression) []sql.Expression {
return handledFilters(CommitTreesTableName, CommitTreesSchema, filters)
}
func (commitTreesTable) WithProjectAndFilters(
ctx *sql.Context,
_, filters []sql.Expression,
) (sql.RowIter, error) {
span, ctx := ctx.Span("gitbase.CommitTreesTable")
iter, err := rowIterWithSelectors(
ctx, CommitTreesSchema, CommitTreesTableName, filters,
[]string{"commit_hash", "repository_id"},
func(selectors selectors) (RowRepoIter, error) {
repos, err := selectors.textValues("repository_id")
if err != nil {
return nil, err
}
hashes, err := selectors.textValues("commit_hash")
if err != nil {
return nil, err
}
return &commitTreesIter{
ctx: ctx,
commitHashes: hashes,
repos: repos,
}, nil
},
)
if err != nil {
span.Finish()
return nil, err
}
return sql.NewSpanIter(span, iter), nil
}
type commitTreesIter struct {
ctx *sql.Context
repo *Repository
commits object.CommitIter
commit *object.Commit
trees *object.TreeWalker
seen map[plumbing.Hash]bool
// selectors for faster filtering
repos []string
commitHashes []string
}
func (i *commitTreesIter) NewIterator(repo *Repository) (RowRepoIter, error) {
var commits object.CommitIter
if len(i.repos) == 0 || stringContains(i.repos, repo.ID) {
var err error
commits, err = repo.Repo.CommitObjects()
if err != nil {
return nil, err
}
}
return &commitTreesIter{
ctx: i.ctx,
repo: repo,
commits: commits,
repos: i.repos,
commitHashes: i.commitHashes,
}, nil
}
func (i *commitTreesIter) shouldVisitCommit(commit *object.Commit) bool {
if len(i.commitHashes) > 0 && !stringContains(i.commitHashes, commit.Hash.String()) {
return false
}
return true
}
func (i *commitTreesIter) Next() (sql.Row, error) {
s, ok := i.ctx.Session.(*Session)
if !ok {
return nil, ErrInvalidGitbaseSession.New(i.ctx.Session)
}
for {
if i.commits == nil {
return nil, io.EOF
}
if i.trees == nil {
commit, err := i.commits.Next()
if err != nil {
if err == io.EOF {
i.commits.Close()
return nil, io.EOF
}
if s.SkipGitErrors {
continue
}
return nil, err
}
if !i.shouldVisitCommit(commit) {
continue
}
tree, err := commit.Tree()
if err != nil {
if s.SkipGitErrors {
continue
}
return nil, err
}
i.seen = make(map[plumbing.Hash]bool)
i.trees = object.NewTreeWalker(tree, true, i.seen)
i.commit = commit
}
_, entry, err := i.trees.Next()
if err != nil {
i.trees.Close()
i.trees = nil
if err == io.EOF || s.SkipGitErrors {
continue
}
return nil, err
}
if entry.Mode != filemode.Dir {
continue
}
return sql.NewRow(
i.repo.ID,
i.commit.Hash.String(),
entry.Hash.String(),
), nil
}
return nil, nil
}
func (i *commitTreesIter) Close() error {
if i.commits != nil {
i.commits.Close()
}
if i.trees != nil {
i.trees.Close()
}
return nil
}