Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 6fb987b

Browse files
michael-gocarolynvs
authored andcommitted
gvt (and gb-vendor) importer (#1149)
1 parent 0979f44 commit 6fb987b

File tree

14 files changed

+556
-3
lines changed

14 files changed

+556
-3
lines changed

cmd/dep/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ specified, use the current directory.
2929
When configuration for another dependency management tool is detected, it is
3030
imported into the initial manifest and lock. Use the -skip-tools flag to
3131
disable this behavior. The following external tools are supported:
32-
glide, godep, vndr, govend.
32+
glide, godep, vndr, govend, gb, gvt.
3333
3434
Any dependencies that are not constrained by external configuration use the
3535
GOPATH analysis below.

cmd/dep/testdata/harness_tests/init/gvt/case1/final/Gopkg.lock

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
[[constraint]]
3+
name = "github.com/sdboyer/deptest"
4+
source = "https://github.com/carolynvs/deptest"
5+
6+
[[constraint]]
7+
name = "github.com/sdboyer/deptestdos"
8+
9+
[[constraint]]
10+
branch = "v2"
11+
name = "gopkg.in/yaml.v2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/sdboyer/deptest"
11+
"github.com/sdboyer/deptestdos"
12+
"gopkg.in/yaml.v2"
13+
)
14+
15+
func main() {
16+
var a deptestdos.Bar
17+
var b yaml.MapItem
18+
var c deptest.Foo
19+
fmt.Println(a, b, c)
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "0",
3+
"dependencies": [
4+
{
5+
"importpath": "github.com/sdboyer/deptest",
6+
"repository": "https://github.com/carolynvs/deptest",
7+
"revision": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f",
8+
"branch": "HEAD"
9+
},
10+
{
11+
"importpath": "github.com/sdboyer/deptestdos",
12+
"repository": "https://github.com/sdboyer/deptestdos",
13+
"revision": "5c607206be5decd28e6263ffffdcee067266015eXXX",
14+
"branch": "master"
15+
},
16+
{
17+
"importpath": "gopkg.in/yaml.v2",
18+
"repository": "https://gopkg.in/yaml.v2",
19+
"revision": "f7716cbe52baa25d2e9b0d0da546fcf909fc16b4",
20+
"branch": "v2"
21+
}
22+
]
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"commands": [
3+
["init", "-no-examples"]
4+
],
5+
"error-expected": "",
6+
"gopath-initial": {
7+
"github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f"
8+
},
9+
"vendor-final": [
10+
"github.com/sdboyer/deptest",
11+
"github.com/sdboyer/deptestdos"
12+
]
13+
}

docs/FAQ.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ about what's going on.
210210
During `dep init` configuration from other dependency managers is detected
211211
and imported, unless `-skip-tools` is specified.
212212

213-
The following tools are supported: `glide`, `godep`, `vndr` and `govend`.
213+
The following tools are supported: `glide`, `godep`, `vndr`, `govend`, `gb` and `gvt`.
214214

215215
See [#186](https://github.com/golang/dep/issues/186#issuecomment-306363441) for
216216
how to add support for another tool.

internal/importers/base/importer.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,21 @@ func (i *Importer) ImportPackages(packages []ImportedPackage, defaultConstraintF
180180
}
181181

182182
for _, prj := range projects {
183+
source := prj.Source
184+
if len(source) > 0 {
185+
isDefault, err := i.isDefaultSource(prj.Root, source)
186+
if err != nil {
187+
i.Logger.Printf(" Ignoring imported source %s for %s: %s", source, prj.Root, err.Error())
188+
source = ""
189+
} else if isDefault {
190+
source = ""
191+
}
192+
}
193+
183194
pc := gps.ProjectConstraint{
184195
Ident: gps.ProjectIdentifier{
185196
ProjectRoot: prj.Root,
186-
Source: prj.Source,
197+
Source: source,
187198
},
188199
}
189200

@@ -291,3 +302,23 @@ func (i *Importer) convertToConstraint(v gps.Version) gps.Constraint {
291302
}
292303
return v
293304
}
305+
306+
func (i *Importer) isDefaultSource(projectRoot gps.ProjectRoot, sourceURL string) (bool, error) {
307+
// this condition is mainly for gopkg.in imports,
308+
// as some importers specify the repository url as https://gopkg.in/...,
309+
// but sm.SourceURLsForPath() returns https://github.com/... urls for gopkg.in
310+
if sourceURL == "https://"+string(projectRoot) {
311+
return true, nil
312+
}
313+
314+
sourceURLs, err := i.sm.SourceURLsForPath(string(projectRoot))
315+
if err != nil {
316+
return false, err
317+
}
318+
// The first url in the slice will be the default one (usually https://...)
319+
if len(sourceURLs) > 0 && sourceURL == sourceURLs[0].String() {
320+
return true, nil
321+
}
322+
323+
return false, nil
324+
}

internal/importers/base/importer_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,30 @@ func TestBaseImporter_ImportProjects(t *testing.T) {
370370
},
371371
},
372372
},
373+
"alternate source": {
374+
importertest.TestCase{
375+
WantConstraint: "*",
376+
WantSourceRepo: importertest.ProjectSrc,
377+
},
378+
[]ImportedPackage{
379+
{
380+
Name: importertest.Project,
381+
Source: importertest.ProjectSrc,
382+
},
383+
},
384+
},
385+
"ignoring default source": {
386+
importertest.TestCase{
387+
WantConstraint: "*",
388+
WantSourceRepo: "",
389+
},
390+
[]ImportedPackage{
391+
{
392+
Name: importertest.Project,
393+
Source: "https://" + importertest.Project,
394+
},
395+
},
396+
},
373397
}
374398

375399
for name, tc := range testcases {

internal/importers/gvt/importer.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gvt
6+
7+
import (
8+
"encoding/json"
9+
"io/ioutil"
10+
"log"
11+
"os"
12+
"path/filepath"
13+
14+
"github.com/golang/dep"
15+
"github.com/golang/dep/internal/gps"
16+
"github.com/golang/dep/internal/importers/base"
17+
"github.com/pkg/errors"
18+
)
19+
20+
const gvtPath = "vendor" + string(os.PathSeparator) + "manifest"
21+
22+
// Importer imports gvt configuration into the dep configuration format.
23+
type Importer struct {
24+
*base.Importer
25+
gvtConfig gvtManifest
26+
}
27+
28+
// NewImporter for gvt. It handles gb (gb-vendor) too as they share a common manifest file & format
29+
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
30+
return &Importer{Importer: base.NewImporter(logger, verbose, sm)}
31+
}
32+
33+
type gvtManifest struct {
34+
Deps []gvtPkg `json:"dependencies"`
35+
}
36+
37+
type gvtPkg struct {
38+
ImportPath string
39+
Repository string
40+
Revision string
41+
Branch string
42+
}
43+
44+
// Name of the importer.
45+
func (g *Importer) Name() string {
46+
return "gvt"
47+
}
48+
49+
// HasDepMetadata checks if a directory contains config that the importer can handle.
50+
func (g *Importer) HasDepMetadata(dir string) bool {
51+
y := filepath.Join(dir, gvtPath)
52+
if _, err := os.Stat(y); err != nil {
53+
return false
54+
}
55+
56+
return true
57+
}
58+
59+
// Import the config found in the directory.
60+
func (g *Importer) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
61+
err := g.load(dir)
62+
if err != nil {
63+
return nil, nil, err
64+
}
65+
66+
return g.convert(pr)
67+
}
68+
69+
func (g *Importer) load(projectDir string) error {
70+
g.Logger.Println("Detected gb/gvt configuration files...")
71+
j := filepath.Join(projectDir, gvtPath)
72+
if g.Verbose {
73+
g.Logger.Printf(" Loading %s", j)
74+
}
75+
jb, err := ioutil.ReadFile(j)
76+
if err != nil {
77+
return errors.Wrapf(err, "unable to read %s", j)
78+
}
79+
err = json.Unmarshal(jb, &g.gvtConfig)
80+
if err != nil {
81+
return errors.Wrapf(err, "unable to parse %s", j)
82+
}
83+
84+
return nil
85+
}
86+
87+
func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
88+
g.Logger.Println("Converting from vendor/manifest ...")
89+
90+
packages := make([]base.ImportedPackage, 0, len(g.gvtConfig.Deps))
91+
for _, pkg := range g.gvtConfig.Deps {
92+
// Validate
93+
if pkg.ImportPath == "" {
94+
err := errors.New("invalid gvt configuration, ImportPath is required")
95+
return nil, nil, err
96+
}
97+
98+
if pkg.Revision == "" {
99+
err := errors.New("invalid gvt configuration, Revision is required")
100+
return nil, nil, err
101+
}
102+
103+
var contstraintHint = ""
104+
if pkg.Branch == "HEAD" {
105+
// gb-vendor sets "branch" to "HEAD", if the package was feteched via -tag or -revision,
106+
// we pass the revision as the constraint hint
107+
contstraintHint = pkg.Revision
108+
} else if pkg.Branch != "master" {
109+
// both gvt & gb-vendor set "branch" to "master" unless a different branch was requested.
110+
// so it's not realy a constraint unless it's a different branch
111+
contstraintHint = pkg.Branch
112+
}
113+
114+
ip := base.ImportedPackage{
115+
Name: pkg.ImportPath,
116+
Source: pkg.Repository,
117+
LockHint: pkg.Revision,
118+
ConstraintHint: contstraintHint,
119+
}
120+
packages = append(packages, ip)
121+
}
122+
123+
err := g.ImportPackages(packages, true)
124+
if err != nil {
125+
return nil, nil, err
126+
}
127+
128+
return g.Manifest, g.Lock, nil
129+
}

0 commit comments

Comments
 (0)