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

Commit

Permalink
Import glide config during dep init
Browse files Browse the repository at this point in the history
Use -skip-tools to bypass this behavior
  • Loading branch information
carolynvs committed May 3, 2017
1 parent a98c8f0 commit bdd0625
Show file tree
Hide file tree
Showing 20 changed files with 623 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

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

46 changes: 1 addition & 45 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ package main

import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"go/build"
"log"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/golang/dep"
Expand Down Expand Up @@ -275,7 +273,7 @@ func getProjectConstraint(arg string, sm *gps.SourceMgr) (gps.ProjectConstraint,
parts := strings.SplitN(arg, "@", 2)
arg = parts[0]
versionStr = parts[1]
constraint.Constraint = deduceConstraint(parts[1])
constraint.Constraint = gps.DeduceConstraint(parts[1])
}
// TODO: What if there is no @, assume default branch (which may not be master) ?
// TODO: if we decide to keep equals.....
Expand Down Expand Up @@ -327,48 +325,6 @@ func getProjectConstraint(arg string, sm *gps.SourceMgr) (gps.ProjectConstraint,
return constraint, nil
}

// deduceConstraint tries to puzzle out what kind of version is given in a string -
// semver, a revision, or as a fallback, a plain tag
func deduceConstraint(s string) gps.Constraint {
// always semver if we can
c, err := gps.NewSemverConstraint(s)
if err == nil {
return c
}

slen := len(s)
if slen == 40 {
if _, err = hex.DecodeString(s); err == nil {
// Whether or not it's intended to be a SHA1 digest, this is a
// valid byte sequence for that, so go with Revision. This
// covers git and hg
return gps.Revision(s)
}
}
// Next, try for bzr, which has a three-component GUID separated by
// dashes. There should be two, but the email part could contain
// internal dashes
if strings.Count(s, "-") >= 2 {
// Work from the back to avoid potential confusion from the email
i3 := strings.LastIndex(s, "-")
// Skip if - is last char, otherwise this would panic on bounds err
if slen == i3+1 {
return gps.NewVersion(s)
}

i2 := strings.LastIndex(s[:i3], "-")
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
// Getting this far means it'd pretty much be nuts if it's not a
// bzr rev, so don't bother parsing the email.
return gps.Revision(s)
}
}

// If not a plain SHA1 or bzr custom GUID, assume a plain version.
// TODO: if there is amgibuity here, then prompt the user?
return gps.NewVersion(s)
}

func checkErrors(m map[string]pkgtree.PackageOrErr) error {
noGoErrors, pkgErrors := 0, 0
for _, poe := range m {
Expand Down
38 changes: 0 additions & 38 deletions cmd/dep/ensure_test.go

This file was deleted.

23 changes: 23 additions & 0 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/golang/dep/gps"
"github.com/golang/dep/gps/pkgtree"
"github.com/golang/dep/internal"
"github.com/golang/dep/internal/importer"
"github.com/pkg/errors"
)

Expand All @@ -33,6 +34,10 @@ versions available from the upstream source per the following algorithm:
- Default branch(es) (sorted lexicographically)
- Non-semver tags (sorted lexicographically)
If configuration files for other dependency management tools are found, they
are used to pre-populate the manifest and lock. Specify -skip-tools to disable
this behavior.
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 @@ -46,10 +51,12 @@ 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 manager tools")
}

type initCommand struct {
noExamples bool
skipTools bool
}

func trimPathPrefix(p1, p2 string) string {
Expand Down Expand Up @@ -116,10 +123,26 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}

m := &dep.Manifest{
Dependencies: pd.constraints,
}

if !cmd.skipTools {
ipd, err := importer.Import(root, cpr)
if err != nil {
return errors.Wrap(err, "Error importing dependency management configuration")
}

if ipd != nil {
m.Ignored = ipd.Ignored
for pr, c := range ipd.Dependencies {
internal.Vlogf("Importing dependency on %s: %s", pr, c)
m.Dependencies[pr] = c
}
}
}

// Make an initial lock from what knowledge we've collected about the
// versions on disk
l := &dep.Lock{
Expand Down
20 changes: 20 additions & 0 deletions cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.lock

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

14 changes: 14 additions & 0 deletions cmd/dep/testdata/harness_tests/init/glide1/final/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ignored = ["github.com/sdboyer/dep-test","github.com/golang/notexist/samples"]

[[dependencies]]
branch = "master"
name = "github.com/golang/lint"

[[dependencies]]
branch = "master"
name = "github.com/sdboyer/deptest"
source = "https://github.com/carolynvs/deptest.git"

[[dependencies]]
name = "github.com/sdboyer/deptestdos"
version = "v2.0.0"
14 changes: 14 additions & 0 deletions cmd/dep/testdata/harness_tests/init/glide1/initial/glide.lock

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

20 changes: 20 additions & 0 deletions cmd/dep/testdata/harness_tests/init/glide1/initial/glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package: github.com/golang/notexist
homepage: http://example.com
license: MIT
owners:
- name: Sam Boyer
email: sdboyer@example.com
homepage: http://sdboyer.io
ignore:
- github.com/sdboyer/dep-test
excludeDirs:
- samples
import:
- package: github.com/sdboyer/deptest
repo: https://github.com/carolynvs/deptest.git
vcs: git
version: master
- package: github.com/sdboyer/deptestdos
version: v2.0.0
testImport:
- package: github.com/golang/lint
17 changes: 17 additions & 0 deletions cmd/dep/testdata/harness_tests/init/glide1/initial/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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/sdboyer/dep-test"
"github.com/sdboyer/deptestdos"
)

func main() {
var x deptestdos.Bar
fmt.Println(x)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2016 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 samples

import dt "github.com/carolynvs/go-dep-test"

func Sample1() int {
var x = dt.Thing
return x
}
13 changes: 13 additions & 0 deletions cmd/dep/testdata/harness_tests/init/glide1/testcase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"commands": [
["init", "-no-examples"]
],
"gopath-initial": {
"github.com/golang/lint": "cb00e5669539f047b2f4c53a421a01b0c8e172c6",
"github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f"
},
"vendor-final": [
"github.com/sdboyer/deptest",
"github.com/sdboyer/deptestdos"
]
}
27 changes: 27 additions & 0 deletions gps/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,30 @@ func TestTypedConstraintString(t *testing.T) {
}
}
}

func TestDeduceConstraint(t *testing.T) {
sv, err := NewSemverConstraint("v1.2.3")
if err != nil {
t.Fatal(err)
}

constraints := map[string]Constraint{
"v1.2.3": sv,
"5b3352dc16517996fb951394bcbbe913a2a616e3": Revision("5b3352dc16517996fb951394bcbbe913a2a616e3"),

// valid bzr revs
"jess@linux.com-20161116211307-wiuilyamo9ian0m7": Revision("jess@linux.com-20161116211307-wiuilyamo9ian0m7"),

// invalid bzr revs
"go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn": NewVersion("go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn"),
"go4@golang.org-sadfasdf-": NewVersion("go4@golang.org-sadfasdf-"),
"20120425195858-psty8c35ve2oej8t": NewVersion("20120425195858-psty8c35ve2oej8t"),
}

for str, expected := range constraints {
c := DeduceConstraint(str)
if c != expected {
t.Fatalf("expected: %#v, got %#v for %s", expected, c, str)
}
}
}
46 changes: 46 additions & 0 deletions gps/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"fmt"
"sort"

"encoding/hex"
"strconv"
"strings"

"github.com/Masterminds/semver"
)

Expand Down Expand Up @@ -361,3 +365,45 @@ type sortedWC []workingConstraint
func (s sortedWC) Len() int { return len(s) }
func (s sortedWC) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sortedWC) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) }

// DeduceConstraint tries to puzzle out what kind of version is given in a string -
// semver, a revision, or as a fallback, a plain tag
func DeduceConstraint(s string) Constraint {
// always semver if we can
c, err := NewSemverConstraint(s)
if err == nil {
return c
}

slen := len(s)
if slen == 40 {
if _, err = hex.DecodeString(s); err == nil {
// Whether or not it's intended to be a SHA1 digest, this is a
// valid byte sequence for that, so go with Revision. This
// covers git and hg
return Revision(s)
}
}
// Next, try for bzr, which has a three-component GUID separated by
// dashes. There should be two, but the email part could contain
// internal dashes
if strings.Count(s, "-") >= 2 {
// Work from the back to avoid potential confusion from the email
i3 := strings.LastIndex(s, "-")
// Skip if - is last char, otherwise this would panic on bounds err
if slen == i3+1 {
return NewVersion(s)
}

i2 := strings.LastIndex(s[:i3], "-")
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
// Getting this far means it'd pretty much be nuts if it's not a
// bzr rev, so don't bother parsing the email.
return Revision(s)
}
}

// If not a plain SHA1 or bzr custom GUID, assume a plain version.
// TODO: if there is amgibuity here, then prompt the user?
return NewVersion(s)
}
Loading

0 comments on commit bdd0625

Please sign in to comment.