-
Notifications
You must be signed in to change notification settings - Fork 568
/
commit_provenance.go
286 lines (271 loc) · 9.5 KB
/
commit_provenance.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
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
package pfsdb
import (
"context"
"github.com/jmoiron/sqlx"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/pfs"
pfsserver "github.com/pachyderm/pachyderm/v2/src/server/pfs"
)
// returns the commit of a certain repo in a commit set.
func ResolveCommitProvenance(tx *pachsql.Tx, repo *pfs.Repo, commitSet string) (*pfs.Commit, error) {
cs, err := CommitSetProvenance(tx, commitSet)
if err != nil {
return nil, err
}
for _, c := range cs {
if RepoKey(c.Repo) == RepoKey(repo) {
return c, nil
}
}
return nil, pfsserver.ErrCommitNotFound{Commit: &pfs.Commit{Repo: repo, Id: commitSet}}
}
// CommitSetProvenance returns all the commit IDs that are in the provenance
// of all the commits in this commit set.
//
// TODO(provenance): is 'SELECT DISTINCT commit_id' a performance concern?
func CommitSetProvenance(tx *pachsql.Tx, id string) (_ []*pfs.Commit, retErr error) {
q := `
WITH RECURSIVE prov(from_id, to_id) AS (
SELECT from_id, to_id
FROM pfs.commit_provenance JOIN pfs.commits ON int_id = from_id
WHERE commit_set_id = $1
UNION ALL
SELECT cp.from_id, cp.to_id
FROM prov p, pfs.commit_provenance cp
WHERE cp.from_id = p.to_id
)
SELECT DISTINCT commit_id
FROM pfs.commits, prov
WHERE int_id = prov.to_id AND commit_set_id != $1;`
rows, err := tx.Queryx(q, id)
if err != nil {
return nil, errors.EnsureStack(err)
}
defer errors.Close(&retErr, rows, "close rows")
cs := make([]*pfs.Commit, 0)
for rows.Next() {
var commit string
if err := rows.Scan(&commit); err != nil {
return nil, errors.EnsureStack(err)
}
cs = append(cs, ParseCommit(commit))
}
if err = rows.Err(); err != nil {
return nil, errors.EnsureStack(err)
}
return cs, nil
}
func CommitDirectProvenance(ctx context.Context, extCtx sqlx.ExtContext, id CommitID) ([]*pfs.Commit, error) {
var commits []Commit
query := `
SELECT DISTINCT
commit.commit_id,
commit.commit_set_id,
repo.name as "repo.name",
repo.type as "repo.type",
project.name as "repo.project.name",
branch.name as branch_name
FROM pfs.commit_provenance prov
JOIN pfs.commits commit ON prov.to_id = commit.int_id
JOIN pfs.repos repo ON commit.repo_id = repo.id
JOIN core.projects project ON repo.project_id = project.id
LEFT JOIN pfs.branches branch ON commit.branch_id = branch.id
WHERE prov.from_id = $1
`
if err := sqlx.SelectContext(ctx, extCtx, &commits, query, id); err != nil {
return nil, errors.Wrapf(err, "getting direct commit provenance for commitID=%d", id)
}
var commitPbs []*pfs.Commit
for _, commit := range commits {
commitPbs = append(commitPbs, commit.Pb())
}
return commitPbs, nil
}
// CommitSetSubvenance returns all the commit IDs that contain commits in this commit set in their
// full (transitive) provenance
func CommitSetSubvenance(tx *pachsql.Tx, id string) (_ []*pfs.Commit, retErr error) {
q := `
WITH RECURSIVE subv(from_id, to_id) AS (
SELECT from_id, to_id
FROM pfs.commit_provenance JOIN pfs.commits ON int_id = to_id
WHERE commit_set_id = $1
UNION ALL
SELECT cp.from_id, cp.to_id
FROM subv s, pfs.commit_provenance cp
WHERE cp.to_id = s.from_id
)
SELECT DISTINCT commit_id
FROM pfs.commits, subv
WHERE int_id = subv.from_id AND commit_set_id != $1;`
rows, err := tx.Queryx(q, id)
if err != nil {
return nil, errors.EnsureStack(err)
}
defer errors.Close(&retErr, rows, "close rows")
cs := make([]*pfs.Commit, 0)
for rows.Next() {
var commit string
if err := rows.Scan(&commit); err != nil {
return nil, errors.EnsureStack(err)
}
cs = append(cs, ParseCommit(commit))
}
if err = rows.Err(); err != nil {
return nil, errors.EnsureStack(err)
}
return cs, nil
}
func AddCommit(tx *pachsql.Tx, commit *pfs.Commit) error {
stmt := `INSERT INTO pfs.commits(commit_id, commit_set_id) VALUES ($1, $2) ON CONFLICT DO NOTHING;`
_, err := tx.Exec(stmt, CommitKey(commit), commit.Id)
return errors.Wrapf(err, "insert commit %q into pfs.commits", CommitKey(commit))
}
func getCommitTableID(tx *pachsql.Tx, commit *pfs.Commit) (_ int, retErr error) {
commitKey := CommitKey(commit)
query := `SELECT int_id FROM pfs.commits WHERE commit_id = $1;`
rows, err := tx.Queryx(query, commitKey)
if err != nil {
return 0, errors.EnsureStack(err)
}
defer errors.Close(&retErr, rows, "close rows")
var id int
for rows.Next() {
if err := rows.Scan(&id); err != nil {
return 0, errors.EnsureStack(err)
}
}
if err = rows.Err(); err != nil {
return 0, errors.EnsureStack(err)
}
return id, nil
}
func AddCommitProvenance(tx *pachsql.Tx, from, to *pfs.Commit) error {
fromId, err := getCommitTableID(tx, from)
if err != nil {
return errors.Wrapf(err, "get int id for 'from' commit, %q", CommitKey(from))
}
toId, err := getCommitTableID(tx, to)
if err != nil {
return errors.Wrapf(err, "get int id for 'to' commit, %q", CommitKey(to))
}
return addCommitProvenance(tx, fromId, toId)
}
func addCommitProvenance(tx *pachsql.Tx, from, to int) error {
stmt := `INSERT INTO pfs.commit_provenance(from_id, to_id) VALUES ($1, $2) ON CONFLICT DO NOTHING;`
_, err := tx.Exec(stmt, from, to)
return errors.Wrapf(err, "add commit provenance")
}
// GetCommitWithIDProvenance returns the full provenance of a commit, i.e. all commits that it either directly or transitively depends on.
func GetCommitWithIDProvenance(ctx context.Context, ext sqlx.ExtContext, startId CommitID, opts ...GraphOption) ([]*CommitWithID, error) {
commits, err := getCommitProvenance(ctx, ext, startId, opts...)
if err != nil {
return nil, errors.Wrap(err, "get commit with id provenance")
}
var commitWithIDs []*CommitWithID
for _, commit := range commits {
commitInfo, err := getCommitInfoFromCommitRow(ctx, ext, commit)
if err != nil {
return nil, errors.Wrap(err, "get commit with id provenance")
}
commitWithIDs = append(commitWithIDs, &CommitWithID{
ID: commit.ID,
CommitInfo: commitInfo,
})
}
return commitWithIDs, nil
}
func getCommitProvenance(ctx context.Context, ext sqlx.ExtContext, commitId CommitID, opts ...GraphOption) ([]*Commit, error) {
graphOpts := defaultGraphOptions()
for _, opt := range opts {
opt(graphOpts)
}
var commits []*Commit
if err := sqlx.SelectContext(ctx, ext, &commits, `
WITH RECURSIVE prov(from_id, to_id) AS (
SELECT from_id, to_id, 1 as depth
FROM pfs.commit_provenance
WHERE from_id = $1
UNION ALL
SELECT DISTINCT cp.from_id, cp.to_id, depth+1
FROM prov p
JOIN pfs.commit_provenance cp ON cp.from_id = p.to_id
WHERE depth < $2
)
SELECT`+commitFields+`
FROM prov p
JOIN pfs.commits commit ON p.to_id = commit.int_id
JOIN pfs.repos repo ON commit.repo_id = repo.id
JOIN core.projects project ON repo.project_id = project.id
LEFT JOIN pfs.branches branch ON commit.branch_id = branch.id
GROUP BY `+commitFieldsGroupBy+`
ORDER BY MIN(depth) ASC
LIMIT $3`, commitId, graphOpts.maxDepth, graphOpts.limit); err != nil {
return nil, errors.Wrap(err, "could not get commit provenance")
}
return commits, nil
}
func GetCommitSubvenance(ctx context.Context, tx *pachsql.Tx, commit *pfs.Commit) ([]*pfs.Commit, error) {
id, err := GetCommitID(ctx, tx, commit)
if err != nil {
return nil, err
}
subvenantCommits, err := getCommitSubvenance(ctx, tx, id)
if err != nil {
return nil, errors.Wrap(err, "get commit subvenance")
}
var commits []*pfs.Commit
for _, commit := range subvenantCommits {
commits = append(commits, commit.Pb())
}
return commits, nil
}
// GetCommitWithIDSubvenance returns the full provenance of a commits, i.e. all commits that it either directly or transitively depends on.
func GetCommitWithIDSubvenance(ctx context.Context, ext sqlx.ExtContext, startId CommitID, opts ...GraphOption) ([]*CommitWithID, error) {
commits, err := getCommitSubvenance(ctx, ext, startId, opts...)
if err != nil {
return nil, errors.Wrap(err, "get commit with id subvenance")
}
var commitWithIDs []*CommitWithID
for _, commit := range commits {
commitInfo, err := getCommitInfoFromCommitRow(ctx, ext, commit)
if err != nil {
return nil, errors.Wrap(err, "get commit with id subvenance")
}
commitWithIDs = append(commitWithIDs, &CommitWithID{
ID: commit.ID,
CommitInfo: commitInfo,
})
}
return commitWithIDs, nil
}
func getCommitSubvenance(ctx context.Context, ext sqlx.ExtContext, commitId CommitID, opts ...GraphOption) ([]*Commit, error) {
graphOpts := defaultGraphOptions()
for _, opt := range opts {
opt(graphOpts)
}
var commits []*Commit
if err := sqlx.SelectContext(ctx, ext, &commits, `
WITH RECURSIVE subv(from_id, to_id) AS (
SELECT from_id, to_id, 1 as depth
FROM pfs.commit_provenance
WHERE to_id = $1
UNION ALL
SELECT DISTINCT cp.from_id, cp.to_id, depth+1
FROM subv s
JOIN pfs.commit_provenance cp ON s.from_id = cp.to_id
WHERE depth < $2
)
SELECT`+commitFields+`
FROM subv s
JOIN pfs.commits commit ON s.from_id = commit.int_id
JOIN pfs.repos repo ON commit.repo_id = repo.id
JOIN core.projects project ON repo.project_id = project.id
LEFT JOIN pfs.branches branch ON commit.branch_id = branch.id
GROUP BY `+commitFieldsGroupBy+`
ORDER BY MIN(depth) ASC
LIMIT $3`, commitId, graphOpts.maxDepth, graphOpts.limit); err != nil {
return nil, errors.Wrap(err, "could not get commit subvenance")
}
return commits, nil
}