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

Commit 52acb7e

Browse files
authored
Merge pull request #497 from darkowlzz/496-gopath-flag-init
Add -gopath flag to init
2 parents dfc77a0 + 3c34e8f commit 52acb7e

File tree

13 files changed

+128
-46
lines changed

13 files changed

+128
-46
lines changed

cmd/dep/gopath_scanner.go

-21
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,6 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
134134
}
135135
}
136136

137-
func (g *gopathScanner) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
138-
// Iterate through the new projects in solved lock and add them to manifest
139-
// if direct deps and log feedback for all the new projects.
140-
for _, x := range l.Projects() {
141-
pr := x.Ident().ProjectRoot
142-
newProject := true
143-
// Check if it's a new project, not in the old lock
144-
for _, y := range g.origL.Projects() {
145-
if pr == y.Ident().ProjectRoot {
146-
newProject = false
147-
}
148-
}
149-
if newProject {
150-
// If it's in notondisk, add to manifest, these are direct dependencies.
151-
if _, ok := g.pd.notondisk[pr]; ok {
152-
m.Constraints[pr] = getProjectPropertiesFromVersion(x.Version())
153-
}
154-
}
155-
}
156-
}
157-
158137
func trimPathPrefix(p1, p2 string) string {
159138
if fs.HasFilepathPrefix(p1, p2) {
160139
return p1[len(p2):]

cmd/dep/init.go

+19-9
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,13 +139,17 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
132139
if err != nil {
133140
return err
134141
}
135-
gs := newGopathScanner(ctx, directDeps, sm)
136-
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
137-
if err != nil {
138-
return err
142+
143+
if cmd.gopath {
144+
gs := newGopathScanner(ctx, directDeps, sm)
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
152+
copyLock := *p.Lock // Copy lock before solving. Use this to separate new lock projects from solved lock
142153

143154
params := gps.SolveParameters{
144155
RootDir: root,
@@ -164,8 +175,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
164175
}
165176
p.Lock = dep.LockFromSolution(soln)
166177

167-
rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
168-
gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
178+
rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock, copyLock)
169179

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

cmd/dep/root_analyzer.go

+31-11
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
)
@@ -125,18 +126,37 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp
125126
return gps.SimpleManifest{}, nil, nil
126127
}
127128

128-
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
129-
// Remove dependencies from the manifest that aren't used
130-
for pr := range m.Constraints {
131-
var used bool
132-
for _, y := range l.Projects() {
133-
if pr == y.Ident().ProjectRoot {
134-
used = true
135-
break
129+
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) {
130+
// Iterate through the new projects in solved lock and add them to manifest
131+
// if they are direct deps and log feedback for all the new projects.
132+
for _, y := range l.Projects() {
133+
var f *fb.ConstraintFeedback
134+
pr := y.Ident().ProjectRoot
135+
// New constraints: in new lock and dir dep but not in manifest
136+
if _, ok := a.directDeps[string(pr)]; ok {
137+
if _, ok := m.Constraints[pr]; !ok {
138+
pp := getProjectPropertiesFromVersion(y.Version())
139+
if pp.Constraint != nil {
140+
m.Constraints[pr] = pp
141+
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}
142+
f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect)
143+
f.LogFeedback(a.ctx.Err)
144+
}
145+
f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect)
146+
f.LogFeedback(a.ctx.Err)
147+
}
148+
} else {
149+
// New locked projects: in new lock but not in old lock
150+
newProject := true
151+
for _, opl := range ol.Projects() {
152+
if pr == opl.Ident().ProjectRoot {
153+
newProject = false
154+
}
155+
}
156+
if newProject {
157+
f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive)
158+
f.LogFeedback(a.ctx.Err)
136159
}
137-
}
138-
if !used {
139-
delete(m.Constraints, pr)
140160
}
141161
}
142162
}

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

+1-1
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

+1-1
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

+1-1
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

+21
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,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"
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+
}
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+
}
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+
}

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

+1-1
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
"error-expected": "",
66
"gopath-initial": {

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

+1-1
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": {

0 commit comments

Comments
 (0)