-
Notifications
You must be signed in to change notification settings - Fork 0
/
commits.go
292 lines (256 loc) · 7.05 KB
/
commits.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
287
288
289
290
291
292
package db
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"github.com/lib/pq"
"github.com/mmcloughlin/goperf/app/db/internal/db"
"github.com/mmcloughlin/goperf/app/entity"
)
// StoreCommit writes commit to the database.
func (d *DB) StoreCommit(ctx context.Context, c *entity.Commit) error {
return d.txq(ctx, func(q *db.Queries) error {
return storeCommit(ctx, q, c)
})
}
func storeCommit(ctx context.Context, q *db.Queries, c *entity.Commit) error {
sha, err := hex.DecodeString(c.SHA)
if err != nil {
return fmt.Errorf("invalid sha: %w", err)
}
tree, err := hex.DecodeString(c.Tree)
if err != nil {
return fmt.Errorf("invalid tree: %w", err)
}
parents := make([][]byte, len(c.Parents))
for i, p := range c.Parents {
parents[i], err = hex.DecodeString(p)
if err != nil {
return fmt.Errorf("invalid parent: %w", err)
}
}
return q.InsertCommit(ctx, db.InsertCommitParams{
SHA: sha,
Tree: tree,
Parents: parents,
AuthorName: c.Author.Name,
AuthorEmail: c.Author.Email,
AuthorTime: c.AuthorTime,
CommitterName: c.Committer.Name,
CommitterEmail: c.Committer.Email,
CommitTime: c.CommitTime,
Message: c.Message,
})
}
// StoreCommits writes the given commits to the database in a single batch.
func (d *DB) StoreCommits(ctx context.Context, cs []*entity.Commit) error {
fields := []string{
"sha",
"tree",
"parents",
"author_name",
"author_email",
"author_time",
"committer_name",
"committer_email",
"commit_time",
"message",
}
values := []interface{}{}
for _, c := range cs {
sha, err := hex.DecodeString(c.SHA)
if err != nil {
return fmt.Errorf("invalid sha: %w", err)
}
tree, err := hex.DecodeString(c.Tree)
if err != nil {
return fmt.Errorf("invalid tree: %w", err)
}
parents := make([][]byte, len(c.Parents))
for i, p := range c.Parents {
parents[i], err = hex.DecodeString(p)
if err != nil {
return fmt.Errorf("invalid parent: %w", err)
}
}
values = append(values,
sha,
tree,
pq.ByteaArray(parents),
c.Author.Name,
c.Author.Email,
c.AuthorTime,
c.Committer.Name,
c.Committer.Email,
c.CommitTime,
c.Message,
)
}
return d.tx(ctx, func(tx *sql.Tx) error {
return d.insert(ctx, tx, "commits", fields, values)
})
}
// FindCommitBySHA looks up the given commit in the database.
func (d *DB) FindCommitBySHA(ctx context.Context, sha string) (*entity.Commit, error) {
shabytes, err := hex.DecodeString(sha)
if err != nil {
return nil, fmt.Errorf("invalid sha: %w", err)
}
var c *entity.Commit
err = d.txq(ctx, func(q *db.Queries) error {
var err error
c, err = findCommitBySHA(ctx, q, shabytes)
return err
})
return c, err
}
func findCommitBySHA(ctx context.Context, q *db.Queries, sha []byte) (*entity.Commit, error) {
c, err := q.Commit(ctx, sha)
if err != nil {
return nil, err
}
return mapCommit(c), nil
}
// MostRecentCommit returns the most recent commit by commit time.
func (d *DB) MostRecentCommit(ctx context.Context) (*entity.Commit, error) {
var c *entity.Commit
err := d.txq(ctx, func(q *db.Queries) error {
var err error
c, err = mostRecentCommit(ctx, q)
return err
})
return c, err
}
func mostRecentCommit(ctx context.Context, q *db.Queries) (*entity.Commit, error) {
c, err := q.MostRecentCommit(ctx)
if err != nil {
return nil, err
}
return mapCommit(c), nil
}
// MostRecentCommitWithRef returns the most recent commit by commit time having the supplied ref.
func (d *DB) MostRecentCommitWithRef(ctx context.Context, ref string) (*entity.Commit, error) {
var c *entity.Commit
err := d.txq(ctx, func(q *db.Queries) error {
var err error
c, err = mostRecentCommitWithRef(ctx, q, ref)
return err
})
return c, err
}
func mostRecentCommitWithRef(ctx context.Context, q *db.Queries, ref string) (*entity.Commit, error) {
c, err := q.MostRecentCommitWithRef(ctx, ref)
if err != nil {
return nil, err
}
return mapCommit(c), nil
}
func mapCommit(c db.Commit) *entity.Commit {
parents := make([]string, len(c.Parents))
for i, parent := range c.Parents {
parents[i] = hex.EncodeToString(parent)
}
return &entity.Commit{
SHA: hex.EncodeToString(c.SHA),
Tree: hex.EncodeToString(c.Tree),
Parents: parents,
Author: entity.Person{
Name: c.AuthorName,
Email: c.AuthorEmail,
},
AuthorTime: c.AuthorTime,
Committer: entity.Person{
Name: c.CommitterName,
Email: c.CommitterEmail,
},
CommitTime: c.CommitTime,
Message: c.Message,
}
}
// StoreCommitRef writes a commit ref pair to the database.
func (d *DB) StoreCommitRef(ctx context.Context, r *entity.CommitRef) error {
return d.txq(ctx, func(q *db.Queries) error {
return storeCommitRef(ctx, q, r)
})
}
// StoreCommitRefs writes the given commit refs to the database.
func (d *DB) StoreCommitRefs(ctx context.Context, rs []*entity.CommitRef) error {
fields := []string{
"sha",
"ref",
}
values := []interface{}{}
for _, r := range rs {
sha, err := hex.DecodeString(r.SHA)
if err != nil {
return fmt.Errorf("invalid sha: %w", err)
}
values = append(values,
sha,
r.Ref,
)
}
return d.tx(ctx, func(tx *sql.Tx) error {
return d.insert(ctx, tx, "commit_refs", fields, values)
})
}
func storeCommitRef(ctx context.Context, q *db.Queries, r *entity.CommitRef) error {
sha, err := hex.DecodeString(r.SHA)
if err != nil {
return fmt.Errorf("invalid sha: %w", err)
}
return q.InsertCommitRef(ctx, db.InsertCommitRefParams{
SHA: sha,
Ref: r.Ref,
})
}
// StoreCommitPosition writes a commit position to the database. This should be
// rarely needed outside of testing; prefer BuildCommitPositions.
func (d *DB) StoreCommitPosition(ctx context.Context, p *entity.CommitPosition) error {
return d.txq(ctx, func(q *db.Queries) error {
return storeCommitPosition(ctx, q, p)
})
}
func storeCommitPosition(ctx context.Context, q *db.Queries, p *entity.CommitPosition) error {
sha, err := hex.DecodeString(p.SHA)
if err != nil {
return fmt.Errorf("invalid sha: %w", err)
}
return q.InsertCommitPosition(ctx, db.InsertCommitPositionParams{
SHA: sha,
CommitTime: p.CommitTime,
Index: int32(p.Index),
})
}
// BuildCommitPositions creates the commit positions table. The table is
// completely rebuilt from the source tables.
func (d *DB) BuildCommitPositions(ctx context.Context) error {
return d.txq(ctx, func(q *db.Queries) error {
return q.BuildCommitPositions(ctx)
})
}
// MostRecentCommitIndex returns the most recent commit index.
func (d *DB) MostRecentCommitIndex(ctx context.Context) (int, error) {
var idx int
err := d.txq(ctx, func(q *db.Queries) error {
i, err := q.MostRecentCommitIndex(ctx)
idx = int(i)
return err
})
return idx, err
}
// FindCommitIndexBySHA looks up the commit index for a SHA.
func (d *DB) FindCommitIndexBySHA(ctx context.Context, sha string) (int, error) {
shabytes, err := hex.DecodeString(sha)
if err != nil {
return 0, fmt.Errorf("invalid sha: %w", err)
}
idx := 0
err = d.txq(ctx, func(q *db.Queries) error {
i, err := q.CommitIndexForSHA(ctx, shabytes)
idx = int(i)
return err
})
return idx, err
}