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 #500 from carolynvs/import-glide
Browse files Browse the repository at this point in the history
Import glide configuration during init
  • Loading branch information
sdboyer committed Jun 2, 2017
2 parents 3cd62fc + 1ef8181 commit 01294a8
Show file tree
Hide file tree
Showing 80 changed files with 12,931 additions and 499 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
name = "github.com/Masterminds/vcs"
version = "1.11.0"

[[constraint]]
branch = "v2"
name = "github.com/go-yaml/yaml"

[[constraint]]
branch = "master"
name = "github.com/pelletier/go-toml"
Expand Down
16 changes: 10 additions & 6 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import (

type Analyzer struct{}

func (a Analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
// TODO: If we decide to support other tools manifest, this is where we would need
// to add that support.
// HasDepMetadata determines if a dep manifest exists at the specified path.
func (a Analyzer) HasDepMetadata(path string) bool {
mf := filepath.Join(path, ManifestName)
if fileOK, err := fs.IsRegular(mf); err != nil || !fileOK {
// Do not return an error, when does not exist.
fileOK, err := fs.IsRegular(mf)
return err == nil && fileOK
}

func (a Analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
if !a.HasDepMetadata(path) {
return nil, nil, nil
}
f, err := os.Open(mf)

f, err := os.Open(filepath.Join(path, ManifestName))
if err != nil {
return nil, nil, err
}
Expand Down
85 changes: 44 additions & 41 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,76 +262,70 @@ func (s *stringSlice) Set(value string) error {
}

func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstraint, error) {
constraint := gps.ProjectConstraint{
emptyPC := gps.ProjectConstraint{
Constraint: gps.Any(), // default to any; avoids panics later
}

// try to split on '@'
var versionStr string
// When there is no `@`, use any version
versionStr := "*"
atIndex := strings.Index(arg, "@")
if atIndex > 0 {
parts := strings.SplitN(arg, "@", 2)
arg = parts[0]
versionStr = parts[1]
constraint.Constraint = deduceConstraint(parts[1])
}
// TODO: What if there is no @, assume default branch (which may not be master) ?

// TODO: if we decide to keep equals.....

// split on colon if there is a network location
var source string
colonIndex := strings.Index(arg, ":")
if colonIndex > 0 {
parts := strings.SplitN(arg, ":", 2)
arg = parts[0]
constraint.Ident.Source = parts[1]
source = parts[1]
}

pr, err := sm.DeduceProjectRoot(arg)
if err != nil {
return constraint, errors.Wrapf(err, "could not infer project root from dependency path: %s", arg) // this should go through to the user
return emptyPC, errors.Wrapf(err, "could not infer project root from dependency path: %s", arg) // this should go through to the user
}

if string(pr) != arg {
return constraint, errors.Errorf("dependency path %s is not a project root, try %s instead", arg, pr)
return emptyPC, errors.Errorf("dependency path %s is not a project root, try %s instead", arg, pr)
}

constraint.Ident.ProjectRoot = gps.ProjectRoot(arg)

// Below we are checking if the constraint we deduced was valid.
if v, ok := constraint.Constraint.(gps.Version); ok && versionStr != "" {
if v.Type() == gps.IsVersion {
// we hit the fall through case in deduce constraint, let's call out to network
// and get the package's versions
versions, err := sm.ListVersions(constraint.Ident)
if err != nil {
return constraint, errors.Wrapf(err, "list versions for %s", arg) // means repo does not exist
}
pi := gps.ProjectIdentifier{ProjectRoot: pr, Source: source}
c, err := deduceConstraint(versionStr, pi, sm)
if err != nil {
return emptyPC, err
}
return gps.ProjectConstraint{Ident: pi, Constraint: c}, nil
}

var found bool
for _, version := range versions {
if versionStr == version.String() {
found = true
constraint.Constraint = version.Unpair()
break
}
}
// 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, pi gps.ProjectIdentifier, sm gps.SourceManager) (gps.Constraint, error) {
if s == "" {
// Find the default branch
versions, err := sm.ListVersions(pi)
if err != nil {
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
}

if !found {
return constraint, errors.Errorf("%s is not a valid version for the package %s", versionStr, arg)
gps.SortPairedForUpgrade(versions)
for _, v := range versions {
if v.Type() == gps.IsBranch {
return v.Unpair(), nil
}
}
}

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.NewSemverConstraintIC(s)
if err == nil {
return c
return c, nil
}

slen := len(s)
Expand All @@ -340,7 +334,7 @@ func deduceConstraint(s string) gps.Constraint {
// 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)
return gps.Revision(s), nil
}
}
// Next, try for bzr, which has a three-component GUID separated by
Expand All @@ -351,20 +345,29 @@ func deduceConstraint(s string) gps.Constraint {
i3 := strings.LastIndex(s, "-")
// Skip if - is last char, otherwise this would panic on bounds err
if slen == i3+1 {
return gps.NewVersion(s)
return gps.NewVersion(s), nil
}

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)
return gps.Revision(s), nil
}
}

// 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)
// call out to network and get the package's versions
versions, err := sm.ListVersions(pi)
if err != nil {
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
}

for _, version := range versions {
if s == version.String() {
return version.Unpair(), nil
}
}
return nil, errors.Errorf("%s is not a valid version for the package %s(%s)", s, pi.ProjectRoot, pi.Source)
}

func checkErrors(m map[string]pkgtree.PackageOrErr) error {
Expand Down
47 changes: 38 additions & 9 deletions cmd/dep/ensure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,37 @@ import (
"testing"

"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/test"
)

func TestDeduceConstraint(t *testing.T) {
t.Parallel()
h := test.NewHelper(t)
cacheDir := "gps-repocache"
h.TempDir(cacheDir)
sm, err := gps.NewSourceManager(h.Path(cacheDir))
h.Must(err)

sv, err := gps.NewSemverConstraintIC("v1.2.3")
sv, err := gps.NewSemverConstraintIC("v0.8.1")
if err != nil {
t.Fatal(err)
}

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

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

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

pi := gps.ProjectIdentifier{ProjectRoot: "github.com/sdboyer/deptest"}
for str, want := range constraints {
got := deduceConstraint(str)
got, err := deduceConstraint(str, pi, sm)
h.Must(err)

wantT := reflect.TypeOf(want)
gotT := reflect.TypeOf(got)
Expand All @@ -45,3 +51,26 @@ func TestDeduceConstraint(t *testing.T) {
}
}
}

func TestDeduceConstraint_InvalidInput(t *testing.T) {
h := test.NewHelper(t)

cacheDir := "gps-repocache"
h.TempDir(cacheDir)
sm, err := gps.NewSourceManager(h.Path(cacheDir))
h.Must(err)

constraints := []string{
// invalid bzr revs
"go4@golang.org-lskjdfnkjsdnf-ksjdfnskjdfn",
"20120425195858-psty8c35ve2oej8t",
}

pi := gps.ProjectIdentifier{ProjectRoot: "github.com/sdboyer/deptest"}
for _, str := range constraints {
_, err := deduceConstraint(str, pi, sm)
if err == nil {
t.Errorf("expected %s to produce an error", str)
}
}
}
Loading

0 comments on commit 01294a8

Please sign in to comment.