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

Commit

Permalink
Merge pull request #497 from darkowlzz/496-gopath-flag-init
Browse files Browse the repository at this point in the history
Add -gopath flag to init
  • Loading branch information
carolynvs committed Jun 17, 2017
2 parents dfc77a0 + 3c34e8f commit 52acb7e
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 46 deletions.
21 changes: 0 additions & 21 deletions cmd/dep/gopath_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,6 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
}
}

func (g *gopathScanner) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
// Iterate through the new projects in solved lock and add them to manifest
// if direct deps and log feedback for all the new projects.
for _, x := range l.Projects() {
pr := x.Ident().ProjectRoot
newProject := true
// Check if it's a new project, not in the old lock
for _, y := range g.origL.Projects() {
if pr == y.Ident().ProjectRoot {
newProject = false
}
}
if newProject {
// If it's in notondisk, add to manifest, these are direct dependencies.
if _, ok := g.pd.notondisk[pr]; ok {
m.Constraints[pr] = getProjectPropertiesFromVersion(x.Version())
}
}
}
}

func trimPathPrefix(p1, p2 string) string {
if fs.HasFilepathPrefix(p1, p2) {
return p1[len(p2):]
Expand Down
28 changes: 19 additions & 9 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ disable this behavior. The following external tools are supported: glide.
Any dependencies that are not constrained by external configuration use the
GOPATH analysis below.
The version of each dependency will reflect the current state of the GOPATH. If
a dependency doesn't exist in the GOPATH, a version will be selected from the
versions available from the upstream source per the following algorithm:
By default, the dependencies are resolved over the network. A version will be
selected from the versions available from the upstream source per the following
algorithm:
- Tags conforming to semver (sorted by semver rules)
- Default branch(es) (sorted lexicographically)
- Non-semver tags (sorted lexicographically)
An alternate mode can be activated by passing -gopath. In this mode, the version
of each dependency will reflect the current state of the GOPATH. If a dependency
doesn't exist in the GOPATH, a version will be selected based on the above
network version selection algorithm.
A Gopkg.toml file will be written with inferred version constraints for all
direct dependencies. Gopkg.lock will be written with precise versions, and
vendor/ will be populated with the precise versions written to Gopkg.lock.
Expand All @@ -52,11 +57,13 @@ func (cmd *initCommand) Hidden() bool { return false }
func (cmd *initCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
fs.BoolVar(&cmd.skipTools, "skip-tools", false, "skip importing configuration from other dependency managers")
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
}

type initCommand struct {
noExamples bool
skipTools bool
gopath bool
}

func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
Expand Down Expand Up @@ -132,13 +139,17 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}
gs := newGopathScanner(ctx, directDeps, sm)
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
if err != nil {
return err

if cmd.gopath {
gs := newGopathScanner(ctx, directDeps, sm)
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)
if err != nil {
return err
}
}

rootAnalyzer.skipTools = true // Don't import external config during solve for now
copyLock := *p.Lock // Copy lock before solving. Use this to separate new lock projects from solved lock

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

rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
gs.FinalizeRootManifestAndLock(p.Manifest, p.Lock)
rootAnalyzer.FinalizeRootManifestAndLock(p.Manifest, p.Lock, copyLock)

// Run gps.Prepare with appropriate constraint solutions from solve run
// to generate the final lock memo.
Expand Down
42 changes: 31 additions & 11 deletions cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"

"github.com/golang/dep"
fb "github.com/golang/dep/internal/feedback"
"github.com/golang/dep/internal/gps"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -125,18 +126,37 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp
return gps.SimpleManifest{}, nil, nil
}

func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock) {
// Remove dependencies from the manifest that aren't used
for pr := range m.Constraints {
var used bool
for _, y := range l.Projects() {
if pr == y.Ident().ProjectRoot {
used = true
break
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) {
// Iterate through the new projects in solved lock and add them to manifest
// if they are direct deps and log feedback for all the new projects.
for _, y := range l.Projects() {
var f *fb.ConstraintFeedback
pr := y.Ident().ProjectRoot
// New constraints: in new lock and dir dep but not in manifest
if _, ok := a.directDeps[string(pr)]; ok {
if _, ok := m.Constraints[pr]; !ok {
pp := getProjectPropertiesFromVersion(y.Version())
if pp.Constraint != nil {
m.Constraints[pr] = pp
pc := gps.ProjectConstraint{Ident: y.Ident(), Constraint: pp.Constraint}
f = fb.NewConstraintFeedback(pc, fb.DepTypeDirect)
f.LogFeedback(a.ctx.Err)
}
f = fb.NewLockedProjectFeedback(y, fb.DepTypeDirect)
f.LogFeedback(a.ctx.Err)
}
} else {
// New locked projects: in new lock but not in old lock
newProject := true
for _, opl := range ol.Projects() {
if pr == opl.Ident().ProjectRoot {
newProject = false
}
}
if newProject {
f = fb.NewLockedProjectFeedback(y, fb.DepTypeTransitive)
f.LogFeedback(a.ctx.Err)
}
}
if !used {
delete(m.Constraints, pr)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/testdata/harness_tests/init/case1/testcase.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"commands": [
["init", "-no-examples", "-skip-tools"]
["init", "-no-examples", "-skip-tools", "-gopath"]
],
"error-expected": "",
"gopath-initial": {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/testdata/harness_tests/init/case2/testcase.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"commands": [
["init", "-no-examples", "-skip-tools"]
["init", "-no-examples", "-skip-tools", "-gopath"]
],
"error-expected": "",
"gopath-initial": {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/testdata/harness_tests/init/case3/testcase.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"commands": [
["init", "-no-examples", "-skip-tools"]
["init", "-no-examples", "-skip-tools", "-gopath"]
],
"error-expected": "",
"gopath-initial": {
Expand Down
21 changes: 21 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
13 changes: 13 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case4/initial/foo/bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 foo

import "github.com/sdboyer/deptest"

func Foo() deptest.Foo {
var y deptest.Foo

return y
}
19 changes: 19 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case4/initial/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 main

import (
"fmt"

"github.com/golang/notexist/foo"
"github.com/sdboyer/deptestdos"
)

func main() {
var x deptestdos.Bar
y := foo.FooFunc()

fmt.Println(x, y)
}
12 changes: 12 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case4/testcase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"commands": [
["init", "-no-examples"]
],
"gopath-initial": {
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea"
},
"vendor-final": [
"github.com/sdboyer/deptest",
"github.com/sdboyer/deptestdos"
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"commands": [
["init", "-no-examples"]
["init", "-no-examples", "-gopath"]
],
"error-expected": "",
"gopath-initial": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"commands": [
["init", "-no-examples", "-skip-tools"]
["init", "-no-examples", "-skip-tools", "-gopath"]
],
"error-expected": "",
"gopath-initial": {
Expand Down

0 comments on commit 52acb7e

Please sign in to comment.