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

Commit 396dc2a

Browse files
committed
Add -gopath flag to init
- Separate network mode and GOPATH mode of operations. - In GOPATH mode, fetch projectData by searching through the current GOPATH. Solve the dependency constraints based on the projects found on disk and use network to solve for projects not found on disk. - In Network mode, do not check GOPATH or any ondisk projects. Solve the dependency constraints completely over network.
1 parent b86ad16 commit 396dc2a

File tree

9 files changed

+148
-46
lines changed

9 files changed

+148
-46
lines changed

cmd/dep/init.go

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ Initialize the project at filepath root by parsing its dependencies, writing
2727
manifest and lock files, and vendoring the dependencies. If root isn't
2828
specified, use the current directory.
2929
30-
The version of each dependency will reflect the current state of the GOPATH. If
31-
a dependency doesn't exist in the GOPATH, a version will be selected from the
32-
versions available from the upstream source per the following algorithm:
30+
By default, the dependencies are resolved over the network. A version will be
31+
selected from the versions available from the upstream source per the following
32+
algorithm:
3333
3434
- Tags conforming to semver (sorted by semver rules)
3535
- Default branch(es) (sorted lexicographically)
3636
- Non-semver tags (sorted lexicographically)
3737
38+
An alternate mode can be activated by passing -gopath. In this mode, version of
39+
each dependency will reflect the current state of the GOPATH. If a dependency
40+
doesn't exist in the GOPATH, a version will be selected based on the above
41+
network version selection algorithm.
42+
3843
A Gopkg.toml file will be written with inferred version constraints for all
3944
direct dependencies. Gopkg.lock will be written with precise versions, and
4045
vendor/ will be populated with the precise versions written to Gopkg.lock.
@@ -48,10 +53,12 @@ func (cmd *initCommand) Hidden() bool { return false }
4853

4954
func (cmd *initCommand) Register(fs *flag.FlagSet) {
5055
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
56+
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
5157
}
5258

5359
type initCommand struct {
5460
noExamples bool
61+
gopath bool
5562
}
5663

5764
func trimPathPrefix(p1, p2 string) string {
@@ -115,44 +122,56 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
115122
sm.UseDefaultSignalHandling()
116123
defer sm.Release()
117124

118-
ctx.Loggers.Err.Println("Searching GOPATH for projects...")
119-
pd, err := getProjectData(ctx, pkgT, cpr, sm)
120-
if err != nil {
121-
return err
122-
}
125+
// Create empty manifest, lock and projectData, and fill them according to
126+
// init operation modes. If gopath flag is set, fetch projectData by searching
127+
// through GOPATH, else operate in network mode, solve all the dependencies
128+
// over network.
123129
m := &dep.Manifest{
124-
Dependencies: pd.constraints,
130+
Dependencies: make(gps.ProjectConstraints),
125131
}
132+
l := &dep.Lock{}
133+
var pd projectData
126134

127-
// Make an initial lock from what knowledge we've collected about the
128-
// versions on disk
129-
l := &dep.Lock{
130-
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
131-
}
135+
if cmd.gopath {
136+
ctx.Loggers.Err.Println("Searching GOPATH for projects...")
137+
pd, err = getProjectData(ctx, pkgT, cpr, sm)
138+
if err != nil {
139+
return err
140+
}
132141

133-
for pr, v := range pd.ondisk {
134-
// That we have to chop off these path prefixes is a symptom of
135-
// a problem in gps itself
136-
pkgs := make([]string, 0, len(pd.dependencies[pr]))
137-
prslash := string(pr) + "/"
138-
for _, pkg := range pd.dependencies[pr] {
139-
if pkg == string(pr) {
140-
pkgs = append(pkgs, ".")
141-
} else {
142-
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
142+
m.Dependencies = pd.constraints
143+
144+
// Make an initial lock from what knowledge we've collected about the
145+
// versions on disk
146+
l = &dep.Lock{
147+
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
148+
}
149+
150+
for pr, v := range pd.ondisk {
151+
// That we have to chop off these path prefixes is a symptom of
152+
// a problem in gps itself
153+
pkgs := make([]string, 0, len(pd.dependencies[pr]))
154+
prslash := string(pr) + "/"
155+
for _, pkg := range pd.dependencies[pr] {
156+
if pkg == string(pr) {
157+
pkgs = append(pkgs, ".")
158+
} else {
159+
pkgs = append(pkgs, trimPathPrefix(pkg, prslash))
160+
}
143161
}
162+
163+
l.P = append(l.P, gps.NewLockedProject(
164+
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
165+
)
144166
}
145167

146-
l.P = append(l.P, gps.NewLockedProject(
147-
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
148-
)
168+
ctx.Loggers.Err.Println("Using network for remaining projects...")
149169
}
150170

151-
ctx.Loggers.Err.Println("Using network for remaining projects...")
152171
// Copy lock before solving. Use this to separate new lock projects from soln
153172
copyLock := *l
154173

155-
// Run solver with project versions found on disk
174+
// Run solver with the available knowledge to solve the dependency constraints
156175
if ctx.Loggers.Verbose {
157176
ctx.Loggers.Err.Println("dep: Solving...")
158177
}
@@ -180,25 +199,43 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
180199
}
181200
l = dep.LockFromInterface(soln)
182201

183-
// Iterate through the new projects in solved lock and add them to manifest
184-
// if direct deps and log feedback for all the new projects.
185-
for _, x := range l.Projects() {
186-
pr := x.Ident().ProjectRoot
187-
newProject := true
188-
// Check if it's a new project, not in the old lock
189-
for _, y := range copyLock.Projects() {
190-
if pr == y.Ident().ProjectRoot {
191-
newProject = false
202+
// Populate manifest based on operation mode.
203+
if cmd.gopath {
204+
// Iterate through the new projects in solved lock and add them to manifest
205+
// if direct deps and log feedback for all the new projects.
206+
for _, x := range l.Projects() {
207+
pr := x.Ident().ProjectRoot
208+
newProject := true
209+
// Check if it's a new project, not in the old lock
210+
for _, y := range copyLock.Projects() {
211+
if pr == y.Ident().ProjectRoot {
212+
newProject = false
213+
}
214+
}
215+
if newProject {
216+
// Check if it's in notondisk project map. These are direct deps, should
217+
// be added to manifest.
218+
if _, ok := pd.notondisk[pr]; ok {
219+
m.Dependencies[pr] = getProjectPropertiesFromVersion(x.Version())
220+
feedback(x.Version(), pr, fb.DepTypeDirect, ctx)
221+
} else {
222+
// Log feedback of transitive project
223+
feedback(x.Version(), pr, fb.DepTypeTransitive, ctx)
224+
}
192225
}
193226
}
194-
if newProject {
195-
// Check if it's in notondisk project map. These are direct deps, should
196-
// be added to manifest.
197-
if _, ok := pd.notondisk[pr]; ok {
227+
} else {
228+
// Get direct dependencies
229+
rm, _ := pkgT.ToReachMap(true, true, false, nil)
230+
directDep := rm.Flatten(false)
231+
232+
// Pick the direct dependencies from the above solution and add to manifest
233+
for _, x := range l.Projects() {
234+
pr := x.Ident().ProjectRoot
235+
if contains(directDep, string(pr)) {
198236
m.Dependencies[pr] = getProjectPropertiesFromVersion(x.Version())
199237
feedback(x.Version(), pr, fb.DepTypeDirect, ctx)
200238
} else {
201-
// Log feedback of transitive project
202239
feedback(x.Version(), pr, fb.DepTypeTransitive, ctx)
203240
}
204241
}

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"]
3+
["init", "-no-examples", "-gopath"]
44
],
55
"gopath-initial": {
66
"github.com/sdboyer/deptest": "v0.8.0",

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"]
3+
["init", "-no-examples", "-gopath"]
44
],
55
"gopath-initial": {
66
"github.com/sdboyer/deptest": "v0.8.0"

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"]
3+
["init", "-no-examples", "-gopath"]
44
],
55
"gopath-initial": {
66
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea"

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

Lines changed: 13 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+
[[dependencies]]
3+
name = "github.com/sdboyer/deptest"
4+
version = "^1.0.0"
5+
6+
[[dependencies]]
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)