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

Commit b674913

Browse files
committed
Add -gopath flag to init
1 parent a42708e commit b674913

File tree

12 files changed

+118
-12
lines changed

12 files changed

+118
-12
lines changed

cmd/dep/init.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ disable this behavior. The following external tools are supported: glide.
3030
Any dependencies that are not constrained by external configuration use the
3131
GOPATH analysis below.
3232
33-
The version of each dependency will reflect the current state of the GOPATH. If
34-
a dependency doesn't exist in the GOPATH, a version will be selected from the
35-
versions available from the upstream source per the following algorithm:
33+
By default, the dependencies are resolved over the network. A version will be
34+
selected from the versions available from the upstream source per the following
35+
algorithm:
3636
3737
- Tags conforming to semver (sorted by semver rules)
3838
- Default branch(es) (sorted lexicographically)
3939
- Non-semver tags (sorted lexicographically)
4040
41+
An alternate mode can be activated by passing -gopath. In this mode, the version
42+
of each dependency will reflect the current state of the GOPATH. If a dependency
43+
doesn't exist in the GOPATH, a version will be selected based on the above
44+
network version selection algorithm.
45+
4146
A Gopkg.toml file will be written with inferred version constraints for all
4247
direct dependencies. Gopkg.lock will be written with precise versions, and
4348
vendor/ will be populated with the precise versions written to Gopkg.lock.
@@ -52,11 +57,13 @@ func (cmd *initCommand) Hidden() bool { return false }
5257
func (cmd *initCommand) Register(fs *flag.FlagSet) {
5358
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
5459
fs.BoolVar(&cmd.skipTools, "skip-tools", false, "skip importing configuration from other dependency managers")
60+
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
5561
}
5662

5763
type initCommand struct {
5864
noExamples bool
5965
skipTools bool
66+
gopath bool
6067
}
6168

6269
func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
@@ -132,10 +139,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
132139
if err != nil {
133140
return err
134141
}
142+
135143
gs := newGopathScanner(ctx, directDeps, sm)
136-
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
137-
if err != nil {
138-
return err
144+
if cmd.gopath {
145+
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
146+
if err != nil {
147+
return err
148+
}
139149
}
140150

141151
rootAnalyzer.skipTools = true // Don't import external config during solve for now
@@ -165,7 +175,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
165175
p.Lock = dep.LockFromSolution(soln)
166176

167177
rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
168-
gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
178+
if cmd.gopath {
179+
gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
180+
}
169181

170182
// Run gps.Prepare with appropriate constraint solutions from solve run
171183
// to generate the final lock memo.

cmd/dep/root_analyzer.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010

1111
"github.com/golang/dep"
12+
fb "github.com/golang/dep/internal/feedback"
1213
"github.com/golang/dep/internal/gps"
1314
"github.com/pkg/errors"
1415
)
@@ -139,6 +140,26 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock)
139140
delete(m.Constraints, pr)
140141
}
141142
}
143+
// Pick the direct dependencies from the solution lock and add to manifest.
144+
// This is done to fill up the manifest constraints with the dependencies
145+
// solved over the network.
146+
for _, y := range l.Projects() {
147+
var f *fb.ConstraintFeedback
148+
pr := y.Ident().ProjectRoot
149+
if _, ok := a.directDeps[string(pr)]; ok {
150+
pp := getProjectPropertiesFromVersion(y.Version())
151+
if pp.Constraint != nil {
152+
m.Constraints[pr] = pp
153+
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}
154+
f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect)
155+
} else {
156+
f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect)
157+
}
158+
} else {
159+
f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive)
160+
}
161+
f.LogFeedback(a.ctx.Err)
162+
}
142163
}
143164

144165
func (a *rootAnalyzer) Info() gps.ProjectAnalyzerInfo {

cmd/dep/testdata/harness_tests/init/case1/testcase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

cmd/dep/testdata/harness_tests/init/case2/testcase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

cmd/dep/testdata/harness_tests/init/case3/testcase.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
[[constraint]]
3+
name = "github.com/sdboyer/deptest"
4+
version = "1.0.0"
5+
6+
[[constraint]]
7+
name = "github.com/sdboyer/deptestdos"
8+
version = "2.0.0"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 foo
6+
7+
import "github.com/sdboyer/deptest"
8+
9+
func Foo() deptest.Foo {
10+
var y deptest.Foo
11+
12+
return y
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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/golang/notexist/foo"
11+
"github.com/sdboyer/deptestdos"
12+
)
13+
14+
func main() {
15+
var x deptestdos.Bar
16+
y := foo.FooFunc()
17+
18+
fmt.Println(x, y)
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"commands": [
3+
["init", "-no-examples"]
4+
],
5+
"gopath-initial": {
6+
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea"
7+
},
8+
"vendor-final": [
9+
"github.com/sdboyer/deptest",
10+
"github.com/sdboyer/deptestdos"
11+
]
12+
}

0 commit comments

Comments
 (0)