-
Notifications
You must be signed in to change notification settings - Fork 265
/
bufmoduleref.go
481 lines (434 loc) · 14.3 KB
/
bufmoduleref.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright 2020-2021 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufmoduleref
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/bufbuild/buf/private/bufpkg/buflock"
modulev1alpha1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/module/v1alpha1"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/uuidutil"
)
const (
// MainBranch is the name of the branch created for every repository.
// This is the default branch used if no branch or commit is specified.
MainBranch = "main"
)
// FileInfo contains module file info.
type FileInfo interface {
// Path is the path of the file relative to the root it is contained within.
// This will be normalized, validated and never empty,
// This will be unique within a given Image.
Path() string
// ExternalPath returns the path that identifies this file externally.
//
// This will be unnormalized.
// Never empty. Falls back to Path if there is not an external path.
//
// Example:
// Assume we had the input path /foo/bar which is a local directory.
// Path: one/one.proto
// RootDirPath: proto
// ExternalPath: /foo/bar/proto/one/one.proto
ExternalPath() string
// IsImport returns true if this file is an import.
IsImport() bool
// ModuleIdentity is the module that this file came from.
//
// Note this *can* be nil if we did not build from a named module.
// All code must assume this can be nil.
// Note that nil checking should work since the backing type is always a pointer.
ModuleIdentity() ModuleIdentity
// Commit is the commit for the module that this file came from.
//
// This will only be set if ModuleIdentity is set. but may not be set
// even if ModuleIdentity is set, that is commit is optional information
// even if we know what module this file came from.
Commit() string
// WithIsImport returns this FileInfo with the given IsImport value.
WithIsImport(isImport bool) FileInfo
isFileInfo()
}
// NewFileInfo returns a new FileInfo.
//
// TODO: we should make moduleIdentity and commit options.
// TODO: we don't validate commit
func NewFileInfo(
path string,
externalPath string,
isImport bool,
moduleIdentity ModuleIdentity,
commit string,
) (FileInfo, error) {
return newFileInfo(
path,
externalPath,
isImport,
moduleIdentity,
commit,
)
}
// ModuleOwner is a module owner.
//
// It just contains remote, owner.
//
// This is shared by ModuleIdentity.
type ModuleOwner interface {
Remote() string
Owner() string
isModuleOwner()
}
// NewModuleOwner returns a new ModuleOwner.
func NewModuleOwner(
remote string,
owner string,
) (ModuleOwner, error) {
return newModuleOwner(remote, owner)
}
// ModuleOwnerForString returns a new ModuleOwner for the given string.
//
// This parses the path in the form remote/owner.
func ModuleOwnerForString(path string) (ModuleOwner, error) {
slashSplit := strings.Split(path, "/")
if len(slashSplit) != 2 {
return nil, newInvalidModuleOwnerStringError(path)
}
remote := strings.TrimSpace(slashSplit[0])
if remote == "" {
return nil, newInvalidModuleIdentityStringError(path)
}
owner := strings.TrimSpace(slashSplit[1])
if owner == "" {
return nil, newInvalidModuleIdentityStringError(path)
}
return NewModuleOwner(remote, owner)
}
// ModuleIdentity is a module identity.
//
// It just contains remote, owner, repository.
//
// This is shared by ModuleReference and ModulePin.
type ModuleIdentity interface {
ModuleOwner
Repository() string
// IdentityString is the string remote/owner/repository.
IdentityString() string
isModuleIdentity()
}
// NewModuleIdentity returns a new ModuleIdentity.
func NewModuleIdentity(
remote string,
owner string,
repository string,
) (ModuleIdentity, error) {
return newModuleIdentity(remote, owner, repository)
}
// ModuleIdentityForString returns a new ModuleIdentity for the given string.
//
// This parses the path in the form remote/owner/repository
//
// TODO: we may want to add a special error if we detect / or @ as this may be a common mistake.
func ModuleIdentityForString(path string) (ModuleIdentity, error) {
remote, owner, repository, err := parseModuleIdentityComponents(path)
if err != nil {
return nil, err
}
return NewModuleIdentity(remote, owner, repository)
}
// ModuleReference is a module reference.
//
// It references either a branch, tag, or a commit.
// Note that since commits belong to branches, we can deduce
// the branch from the commit when resolving.
type ModuleReference interface {
ModuleIdentity
// Prints either remote/owner/repository:{branch,commit}
// If the reference is equal to MainBranch, prints remote/owner/repository.
fmt.Stringer
// Either branch, tag, or commit
Reference() string
isModuleReference()
}
// NewModuleReference returns a new validated ModuleReference.
func NewModuleReference(
remote string,
owner string,
repository string,
reference string,
) (ModuleReference, error) {
return newModuleReference(remote, owner, repository, reference)
}
// NewModuleReferenceForProto returns a new ModuleReference for the given proto ModuleReference.
func NewModuleReferenceForProto(protoModuleReference *modulev1alpha1.ModuleReference) (ModuleReference, error) {
return newModuleReferenceForProto(protoModuleReference)
}
// NewModuleReferencesForProtos maps the Protobuf equivalent into the internal representation.
func NewModuleReferencesForProtos(protoModuleReferences ...*modulev1alpha1.ModuleReference) ([]ModuleReference, error) {
if len(protoModuleReferences) == 0 {
return nil, nil
}
moduleReferences := make([]ModuleReference, len(protoModuleReferences))
for i, protoModuleReference := range protoModuleReferences {
moduleReference, err := NewModuleReferenceForProto(protoModuleReference)
if err != nil {
return nil, err
}
moduleReferences[i] = moduleReference
}
return moduleReferences, nil
}
// NewProtoModuleReferenceForModuleReference returns a new proto ModuleReference for the given ModuleReference.
func NewProtoModuleReferenceForModuleReference(moduleReference ModuleReference) *modulev1alpha1.ModuleReference {
return newProtoModuleReferenceForModuleReference(moduleReference)
}
// NewProtoModuleReferencesForModuleReferences maps the given module references into the protobuf representation.
func NewProtoModuleReferencesForModuleReferences(moduleReferences ...ModuleReference) []*modulev1alpha1.ModuleReference {
if len(moduleReferences) == 0 {
return nil
}
protoModuleReferences := make([]*modulev1alpha1.ModuleReference, len(moduleReferences))
for i, moduleReference := range moduleReferences {
protoModuleReferences[i] = NewProtoModuleReferenceForModuleReference(moduleReference)
}
return protoModuleReferences
}
// ModuleReferenceForString returns a new ModuleReference for the given string.
// If a branch or commit is not provided, the "main" branch is used.
//
// This parses the path in the form remote/owner/repository{:branch,:commit}.
func ModuleReferenceForString(path string) (ModuleReference, error) {
remote, owner, repository, reference, err := parseModuleReferenceComponents(path)
if err != nil {
return nil, err
}
if reference == "" {
// Default to the main branch if a ':' separator was not specified.
reference = MainBranch
}
return NewModuleReference(remote, owner, repository, reference)
}
// IsCommitModuleReference returns true if the ModuleReference references a commit.
//
// If false, this means the ModuleReference references a branch or tag.
// Branch and tag disambiguation needs to be done server-side.
func IsCommitModuleReference(moduleReference ModuleReference) bool {
return IsCommitReference(moduleReference.Reference())
}
// IsCommitReference returns whether the provided reference is a commit.
func IsCommitReference(reference string) bool {
_, err := uuidutil.FromDashless(reference)
return err == nil
}
// ModulePin is a module pin.
//
// It references a specific point in time of a Module.
//
// Note that a commit does this itself, but we want all this information.
// This is what is stored in a buf.lock file.
type ModulePin interface {
ModuleIdentity
// Prints remote/owner/repository:commit, which matches ModuleReference
fmt.Stringer
// all of these will be set
Branch() string
Commit() string
Digest() string
CreateTime() time.Time
isModulePin()
}
// NewModulePin returns a new validated ModulePin.
func NewModulePin(
remote string,
owner string,
repository string,
branch string,
commit string,
digest string,
createTime time.Time,
) (ModulePin, error) {
return newModulePin(remote, owner, repository, branch, commit, digest, createTime)
}
// NewModulePinForProto returns a new ModulePin for the given proto ModulePin.
func NewModulePinForProto(protoModulePin *modulev1alpha1.ModulePin) (ModulePin, error) {
return newModulePinForProto(protoModulePin)
}
// NewModulePinsForProtos maps the Protobuf equivalent into the internal representation.
func NewModulePinsForProtos(protoModulePins ...*modulev1alpha1.ModulePin) ([]ModulePin, error) {
if len(protoModulePins) == 0 {
return nil, nil
}
modulePins := make([]ModulePin, len(protoModulePins))
for i, protoModulePin := range protoModulePins {
modulePin, err := NewModulePinForProto(protoModulePin)
if err != nil {
return nil, err
}
modulePins[i] = modulePin
}
return modulePins, nil
}
// NewProtoModulePinForModulePin returns a new proto ModulePin for the given ModulePin.
func NewProtoModulePinForModulePin(modulePin ModulePin) *modulev1alpha1.ModulePin {
return newProtoModulePinForModulePin(modulePin)
}
// NewProtoModulePinsForModulePins maps the given module pins into the protobuf representation.
func NewProtoModulePinsForModulePins(modulePins ...ModulePin) []*modulev1alpha1.ModulePin {
if len(modulePins) == 0 {
return nil
}
protoModulePins := make([]*modulev1alpha1.ModulePin, len(modulePins))
for i, modulePin := range modulePins {
protoModulePins[i] = NewProtoModulePinForModulePin(modulePin)
}
return protoModulePins
}
// ValidateModuleReferencesUniqueByIdentity returns an error if the module references contain any duplicates.
//
// This only checks remote, owner, repository.
func ValidateModuleReferencesUniqueByIdentity(moduleReferences []ModuleReference) error {
seenModuleReferences := make(map[string]struct{})
for _, moduleReference := range moduleReferences {
moduleIdentityString := moduleReference.IdentityString()
if _, ok := seenModuleReferences[moduleIdentityString]; ok {
return fmt.Errorf("module %s appeared twice", moduleIdentityString)
}
seenModuleReferences[moduleIdentityString] = struct{}{}
}
return nil
}
// ValidateModulePinsUniqueByIdentity returns an error if the module pins contain any duplicates.
//
// This only checks remote, owner, repository.
func ValidateModulePinsUniqueByIdentity(modulePins []ModulePin) error {
seenModulePins := make(map[string]struct{})
for _, modulePin := range modulePins {
moduleIdentityString := modulePin.IdentityString()
if _, ok := seenModulePins[moduleIdentityString]; ok {
return fmt.Errorf("module %s appeared twice", moduleIdentityString)
}
seenModulePins[moduleIdentityString] = struct{}{}
}
return nil
}
// ModuleReferenceEqual returns true if a equals b.
func ModuleReferenceEqual(a ModuleReference, b ModuleReference) bool {
if (a == nil) != (b == nil) {
return false
}
if a == nil {
return true
}
return a.Remote() == b.Remote() &&
a.Owner() == b.Owner() &&
a.Repository() == b.Repository() &&
a.Reference() == b.Reference()
}
// ModulePinEqual returns true if a equals b.
func ModulePinEqual(a ModulePin, b ModulePin) bool {
if (a == nil) != (b == nil) {
return false
}
if a == nil {
return true
}
return a.Remote() == b.Remote() &&
a.Owner() == b.Owner() &&
a.Repository() == b.Repository() &&
a.Branch() == b.Branch() &&
a.Commit() == b.Commit() &&
a.Digest() == b.Digest() &&
a.CreateTime().Equal(b.CreateTime())
}
// DependencyModulePinsForBucket reads the module dependencies from the lock file in the bucket.
func DependencyModulePinsForBucket(
ctx context.Context,
readBucket storage.ReadBucket,
) ([]ModulePin, error) {
lockFile, err := buflock.ReadConfig(ctx, readBucket)
if err != nil {
return nil, fmt.Errorf("failed to read lock file: %w", err)
}
modulePins := make([]ModulePin, 0, len(lockFile.Dependencies))
for _, dep := range lockFile.Dependencies {
modulePin, err := NewModulePin(
dep.Remote,
dep.Owner,
dep.Repository,
dep.Branch,
dep.Commit,
dep.Digest,
dep.CreateTime,
)
if err != nil {
return nil, err
}
modulePins = append(modulePins, modulePin)
}
// just to be safe
SortModulePins(modulePins)
if err := ValidateModulePinsUniqueByIdentity(modulePins); err != nil {
return nil, err
}
return modulePins, nil
}
// PutDependencyModulePinsToBucket writes the module dependencies to the write bucket in the form of a lock file.
func PutDependencyModulePinsToBucket(
ctx context.Context,
writeBucket storage.WriteBucket,
modulePins []ModulePin,
) error {
if err := ValidateModulePinsUniqueByIdentity(modulePins); err != nil {
return err
}
SortModulePins(modulePins)
lockFile := &buflock.Config{
Dependencies: make([]buflock.Dependency, 0, len(modulePins)),
}
for _, pin := range modulePins {
lockFile.Dependencies = append(
lockFile.Dependencies,
buflock.Dependency{
Remote: pin.Remote(),
Owner: pin.Owner(),
Repository: pin.Repository(),
Branch: pin.Branch(),
Commit: pin.Commit(),
Digest: pin.Digest(),
CreateTime: pin.CreateTime(),
},
)
}
return buflock.WriteConfig(ctx, writeBucket, lockFile)
}
// SortFileInfos sorts the FileInfos.
func SortFileInfos(fileInfos []FileInfo) {
if len(fileInfos) == 0 {
return
}
sort.Slice(
fileInfos,
func(i int, j int) bool {
return fileInfos[i].Path() < fileInfos[j].Path()
},
)
}
// SortModulePins sorts the ModulePins.
func SortModulePins(modulePins []ModulePin) {
sort.Slice(modulePins, func(i, j int) bool {
return modulePinLess(modulePins[i], modulePins[j])
})
}