This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathimporter.go
334 lines (288 loc) · 10 KB
/
importer.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package base
import (
"log"
"strings"
"github.com/golang/dep"
"github.com/golang/dep/gps"
fb "github.com/golang/dep/internal/feedback"
"github.com/pkg/errors"
)
// Importer provides a common implementation for importing from other
// dependency managers.
type Importer struct {
SourceManager gps.SourceManager
Logger *log.Logger
Verbose bool
Manifest *dep.Manifest
Lock *dep.Lock
}
// NewImporter creates a new Importer for embedding in an importer.
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
return &Importer{
Logger: logger,
Verbose: verbose,
Manifest: dep.NewManifest(),
Lock: &dep.Lock{},
SourceManager: sm,
}
}
// isTag determines if the specified value is a tag (plain or semver).
func (i *Importer) isTag(pi gps.ProjectIdentifier, value string) (bool, gps.Version, error) {
versions, err := i.SourceManager.ListVersions(pi)
if err != nil {
return false, nil, errors.Wrapf(err, "unable to list versions for %s(%s)", pi.ProjectRoot, pi.Source)
}
for _, version := range versions {
if version.Type() != gps.IsVersion && version.Type() != gps.IsSemver {
continue
}
if value == version.String() {
return true, version, nil
}
}
return false, nil, nil
}
// lookupVersionForLockedProject figures out the appropriate version for a locked
// project based on the locked revision and the constraint from the manifest.
// First try matching the revision to a version, then try the constraint from the
// manifest, then finally the revision.
func (i *Importer) lookupVersionForLockedProject(pi gps.ProjectIdentifier, c gps.Constraint, rev gps.Revision) (gps.Version, error) {
// Find the version that goes with this revision, if any
versions, err := i.SourceManager.ListVersions(pi)
if err != nil {
return rev, errors.Wrapf(err, "Unable to lookup the version represented by %s in %s(%s). Falling back to locking the revision only.", rev, pi.ProjectRoot, pi.Source)
}
var branchConstraint gps.PairedVersion
gps.SortPairedForUpgrade(versions) // Sort versions in asc order
matches := []gps.Version{}
for _, v := range versions {
if v.Revision() == rev {
matches = append(matches, v)
}
if c != nil && v.Type() == gps.IsBranch && v.String() == c.String() {
branchConstraint = v
}
}
// Try to narrow down the matches with the constraint. Otherwise return the first match.
if len(matches) > 0 {
if c != nil {
for _, v := range matches {
if i.testConstraint(c, v) {
return v, nil
}
}
}
return matches[0], nil
}
// Use branch constraint from the manifest
if branchConstraint != nil {
return branchConstraint.Unpair().Pair(rev), nil
}
// Give up and lock only to a revision
return rev, nil
}
// ImportedPackage is a common intermediate representation of a package imported
// from an external tool's configuration.
type ImportedPackage struct {
// Required. The package path, not necessarily the project root.
Name string
// Required. Text representing a revision or tag.
LockHint string
// Optional. Alternative source, or fork, for the project.
Source string
// Optional. Text representing a branch or version.
ConstraintHint string
}
// importedProject is a consolidated representation of a set of imported packages
// for the same project root.
type importedProject struct {
Root gps.ProjectRoot
ImportedPackage
}
// loadPackages consolidates all package references into a set of project roots.
func (i *Importer) loadPackages(packages []ImportedPackage) ([]importedProject, error) {
// preserve the original order of the packages so that messages that
// are printed as they are processed are in a consistent order.
orderedProjects := make([]importedProject, 0, len(packages))
projects := make(map[gps.ProjectRoot]*importedProject, len(packages))
for _, pkg := range packages {
pr, err := i.SourceManager.DeduceProjectRoot(pkg.Name)
if err != nil {
return nil, errors.Wrapf(err, "Cannot determine the project root for %s", pkg.Name)
}
pkg.Name = string(pr)
prj, exists := projects[pr]
if !exists {
prj := importedProject{pr, pkg}
orderedProjects = append(orderedProjects, prj)
projects[pr] = &orderedProjects[len(orderedProjects)-1]
continue
}
// The config found first "wins", though we allow for incrementally
// setting each field because some importers have a config and lock file.
if prj.Source == "" && pkg.Source != "" {
prj.Source = pkg.Source
}
if prj.ConstraintHint == "" && pkg.ConstraintHint != "" {
prj.ConstraintHint = pkg.ConstraintHint
}
if prj.LockHint == "" && pkg.LockHint != "" {
prj.LockHint = pkg.LockHint
}
}
return orderedProjects, nil
}
// ImportPackages loads imported packages into the manifest and lock.
// - defaultConstraintFromLock specifies if a constraint should be defaulted
// based on the locked version when there wasn't a constraint hint.
//
// Rules:
// * When a constraint is ignored, default to *.
// * HEAD revisions default to the matching branch.
// * Semantic versions default to ^VERSION.
// * Revision constraints are ignored.
// * Versions that don't satisfy the constraint, drop the constraint.
// * Untagged revisions ignore non-branch constraint hints.
func (i *Importer) ImportPackages(packages []ImportedPackage, defaultConstraintFromLock bool) (err error) {
projects, err := i.loadPackages(packages)
if err != nil {
return err
}
for _, prj := range projects {
source := prj.Source
if len(source) > 0 {
isDefault, err := i.isDefaultSource(prj.Root, source)
if err != nil {
i.Logger.Printf(" Ignoring imported source %s for %s: %s", source, prj.Root, err.Error())
source = ""
} else if isDefault {
source = ""
} else if strings.Contains(source, "/vendor/") {
i.Logger.Printf(" Ignoring imported source %s for %s because vendored sources aren't supported", source, prj.Root)
source = ""
}
}
pc := gps.ProjectConstraint{
Ident: gps.ProjectIdentifier{
ProjectRoot: prj.Root,
Source: source,
},
}
pc.Constraint, err = i.SourceManager.InferConstraint(prj.ConstraintHint, pc.Ident)
if err != nil {
pc.Constraint = gps.Any()
}
var version gps.Version
if prj.LockHint != "" {
var isTag bool
// Determine if the lock hint is a revision or tag
isTag, version, err = i.isTag(pc.Ident, prj.LockHint)
if err != nil {
return err
}
// If the hint is a revision, check if it is tagged
if !isTag {
revision := gps.Revision(prj.LockHint)
version, err = i.lookupVersionForLockedProject(pc.Ident, pc.Constraint, revision)
if err != nil {
version = nil
i.Logger.Println(err)
}
}
// Default the constraint based on the locked version
if defaultConstraintFromLock && prj.ConstraintHint == "" && version != nil {
c := i.convertToConstraint(version)
if c != nil {
pc.Constraint = c
}
}
}
// Ignore pinned constraints
if i.isConstraintPinned(pc.Constraint) {
if i.Verbose {
i.Logger.Printf(" Ignoring pinned constraint %v for %v.\n", pc.Constraint, pc.Ident)
}
pc.Constraint = gps.Any()
}
// Ignore constraints which conflict with the locked revision, so that
// solve doesn't later change the revision to satisfy the constraint.
if !i.testConstraint(pc.Constraint, version) {
if i.Verbose {
i.Logger.Printf(" Ignoring constraint %v for %v because it would invalidate the locked version %v.\n", pc.Constraint, pc.Ident, version)
}
pc.Constraint = gps.Any()
}
// Add constraint to manifest that is not empty (has a branch, version or source)
if !gps.IsAny(pc.Constraint) || pc.Ident.Source != "" {
i.Manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{
Source: pc.Ident.Source,
Constraint: pc.Constraint,
}
fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(i.Logger)
} else {
if i.Verbose {
i.Logger.Printf(" Skipping import of %v because its constraint is empty.\n", pc.Ident)
}
}
if version != nil {
lp := gps.NewLockedProject(pc.Ident, version, nil)
i.Lock.P = append(i.Lock.P, lp)
fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(i.Logger)
}
}
return nil
}
// isConstraintPinned returns if a constraint is pinned to a specific revision.
func (i *Importer) isConstraintPinned(c gps.Constraint) bool {
if version, isVersion := c.(gps.Version); isVersion {
switch version.Type() {
case gps.IsRevision, gps.IsVersion:
return true
}
}
return false
}
// testConstraint verifies that the constraint won't invalidate the locked version.
func (i *Importer) testConstraint(c gps.Constraint, v gps.Version) bool {
// Assume branch constraints are satisfied
if version, isVersion := c.(gps.Version); isVersion {
if version.Type() == gps.IsBranch {
return true
}
}
return c.Matches(v)
}
// convertToConstraint turns a version into a constraint.
// Semver tags are converted to a range with the caret operator.
func (i *Importer) convertToConstraint(v gps.Version) gps.Constraint {
if v.Type() == gps.IsSemver {
c, err := gps.NewSemverConstraintIC(v.String())
if err != nil {
// This should never fail, because the type is semver.
// If it does fail somehow, don't let that impact the import.
return nil
}
return c
}
return v
}
func (i *Importer) isDefaultSource(projectRoot gps.ProjectRoot, sourceURL string) (bool, error) {
// this condition is mainly for gopkg.in imports,
// as some importers specify the repository url as https://gopkg.in/...,
// but SourceManager.SourceURLsForPath() returns https://github.com/... urls for gopkg.in
if sourceURL == "https://"+string(projectRoot) {
return true, nil
}
sourceURLs, err := i.SourceManager.SourceURLsForPath(string(projectRoot))
if err != nil {
return false, err
}
// The first url in the slice will be the default one (usually https://...)
if len(sourceURLs) > 0 && sourceURL == sourceURLs[0].String() {
return true, nil
}
return false, nil
}