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

Commit bdd0625

Browse files
committed
Import glide config during dep init
Use -skip-tools to bypass this behavior
1 parent a98c8f0 commit bdd0625

20 files changed

+623
-84
lines changed

Gopkg.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/ensure.go

+1-45
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ package main
66

77
import (
88
"bytes"
9-
"encoding/hex"
109
"flag"
1110
"fmt"
1211
"go/build"
1312
"log"
1413
"os"
1514
"path/filepath"
16-
"strconv"
1715
"strings"
1816

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

330-
// deduceConstraint tries to puzzle out what kind of version is given in a string -
331-
// semver, a revision, or as a fallback, a plain tag
332-
func deduceConstraint(s string) gps.Constraint {
333-
// always semver if we can
334-
c, err := gps.NewSemverConstraint(s)
335-
if err == nil {
336-
return c
337-
}
338-
339-
slen := len(s)
340-
if slen == 40 {
341-
if _, err = hex.DecodeString(s); err == nil {
342-
// Whether or not it's intended to be a SHA1 digest, this is a
343-
// valid byte sequence for that, so go with Revision. This
344-
// covers git and hg
345-
return gps.Revision(s)
346-
}
347-
}
348-
// Next, try for bzr, which has a three-component GUID separated by
349-
// dashes. There should be two, but the email part could contain
350-
// internal dashes
351-
if strings.Count(s, "-") >= 2 {
352-
// Work from the back to avoid potential confusion from the email
353-
i3 := strings.LastIndex(s, "-")
354-
// Skip if - is last char, otherwise this would panic on bounds err
355-
if slen == i3+1 {
356-
return gps.NewVersion(s)
357-
}
358-
359-
i2 := strings.LastIndex(s[:i3], "-")
360-
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
361-
// Getting this far means it'd pretty much be nuts if it's not a
362-
// bzr rev, so don't bother parsing the email.
363-
return gps.Revision(s)
364-
}
365-
}
366-
367-
// If not a plain SHA1 or bzr custom GUID, assume a plain version.
368-
// TODO: if there is amgibuity here, then prompt the user?
369-
return gps.NewVersion(s)
370-
}
371-
372328
func checkErrors(m map[string]pkgtree.PackageOrErr) error {
373329
noGoErrors, pkgErrors := 0, 0
374330
for _, poe := range m {

cmd/dep/ensure_test.go

-38
This file was deleted.

cmd/dep/init.go

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/golang/dep/gps"
1717
"github.com/golang/dep/gps/pkgtree"
1818
"github.com/golang/dep/internal"
19+
"github.com/golang/dep/internal/importer"
1920
"github.com/pkg/errors"
2021
)
2122

@@ -33,6 +34,10 @@ versions available from the upstream source per the following algorithm:
3334
- Default branch(es) (sorted lexicographically)
3435
- Non-semver tags (sorted lexicographically)
3536
37+
If configuration files for other dependency management tools are found, they
38+
are used to pre-populate the manifest and lock. Specify -skip-tools to disable
39+
this behavior.
40+
3641
A Gopkg.toml file will be written with inferred version constraints for all
3742
direct dependencies. Gopkg.lock will be written with precise versions, and
3843
vendor/ will be populated with the precise versions written to Gopkg.lock.
@@ -46,10 +51,12 @@ func (cmd *initCommand) Hidden() bool { return false }
4651

4752
func (cmd *initCommand) Register(fs *flag.FlagSet) {
4853
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
54+
fs.BoolVar(&cmd.skipTools, "skip-tools", false, "skip importing configuration from other dependency manager tools")
4955
}
5056

5157
type initCommand struct {
5258
noExamples bool
59+
skipTools bool
5360
}
5461

5562
func trimPathPrefix(p1, p2 string) string {
@@ -116,10 +123,26 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
116123
if err != nil {
117124
return err
118125
}
126+
119127
m := &dep.Manifest{
120128
Dependencies: pd.constraints,
121129
}
122130

131+
if !cmd.skipTools {
132+
ipd, err := importer.Import(root, cpr)
133+
if err != nil {
134+
return errors.Wrap(err, "Error importing dependency management configuration")
135+
}
136+
137+
if ipd != nil {
138+
m.Ignored = ipd.Ignored
139+
for pr, c := range ipd.Dependencies {
140+
internal.Vlogf("Importing dependency on %s: %s", pr, c)
141+
m.Dependencies[pr] = c
142+
}
143+
}
144+
}
145+
123146
// Make an initial lock from what knowledge we've collected about the
124147
// versions on disk
125148
l := &dep.Lock{

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

+20
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,14 @@
1+
ignored = ["github.com/sdboyer/dep-test","github.com/golang/notexist/samples"]
2+
3+
[[dependencies]]
4+
branch = "master"
5+
name = "github.com/golang/lint"
6+
7+
[[dependencies]]
8+
branch = "master"
9+
name = "github.com/sdboyer/deptest"
10+
source = "https://github.com/carolynvs/deptest.git"
11+
12+
[[dependencies]]
13+
name = "github.com/sdboyer/deptestdos"
14+
version = "v2.0.0"

cmd/dep/testdata/harness_tests/init/glide1/initial/glide.lock

+14
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,20 @@
1+
package: github.com/golang/notexist
2+
homepage: http://example.com
3+
license: MIT
4+
owners:
5+
- name: Sam Boyer
6+
7+
homepage: http://sdboyer.io
8+
ignore:
9+
- github.com/sdboyer/dep-test
10+
excludeDirs:
11+
- samples
12+
import:
13+
- package: github.com/sdboyer/deptest
14+
repo: https://github.com/carolynvs/deptest.git
15+
vcs: git
16+
version: master
17+
- package: github.com/sdboyer/deptestdos
18+
version: v2.0.0
19+
testImport:
20+
- package: github.com/golang/lint
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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/sdboyer/dep-test"
11+
"github.com/sdboyer/deptestdos"
12+
)
13+
14+
func main() {
15+
var x deptestdos.Bar
16+
fmt.Println(x)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2016 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 samples
6+
7+
import dt "github.com/carolynvs/go-dep-test"
8+
9+
func Sample1() int {
10+
var x = dt.Thing
11+
return x
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"commands": [
3+
["init", "-no-examples"]
4+
],
5+
"gopath-initial": {
6+
"github.com/golang/lint": "cb00e5669539f047b2f4c53a421a01b0c8e172c6",
7+
"github.com/sdboyer/deptest": "3f4c3bea144e112a69bbe5d8d01c1b09a544253f"
8+
},
9+
"vendor-final": [
10+
"github.com/sdboyer/deptest",
11+
"github.com/sdboyer/deptestdos"
12+
]
13+
}

gps/constraint_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,30 @@ func TestTypedConstraintString(t *testing.T) {
905905
}
906906
}
907907
}
908+
909+
func TestDeduceConstraint(t *testing.T) {
910+
sv, err := NewSemverConstraint("v1.2.3")
911+
if err != nil {
912+
t.Fatal(err)
913+
}
914+
915+
constraints := map[string]Constraint{
916+
"v1.2.3": sv,
917+
"5b3352dc16517996fb951394bcbbe913a2a616e3": Revision("5b3352dc16517996fb951394bcbbe913a2a616e3"),
918+
919+
// valid bzr revs
920+
921+
922+
// invalid bzr revs
923+
924+
925+
"20120425195858-psty8c35ve2oej8t": NewVersion("20120425195858-psty8c35ve2oej8t"),
926+
}
927+
928+
for str, expected := range constraints {
929+
c := DeduceConstraint(str)
930+
if c != expected {
931+
t.Fatalf("expected: %#v, got %#v for %s", expected, c, str)
932+
}
933+
}
934+
}

gps/constraints.go

+46
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"fmt"
99
"sort"
1010

11+
"encoding/hex"
12+
"strconv"
13+
"strings"
14+
1115
"github.com/Masterminds/semver"
1216
)
1317

@@ -361,3 +365,45 @@ type sortedWC []workingConstraint
361365
func (s sortedWC) Len() int { return len(s) }
362366
func (s sortedWC) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
363367
func (s sortedWC) Less(i, j int) bool { return s[i].Ident.less(s[j].Ident) }
368+
369+
// DeduceConstraint tries to puzzle out what kind of version is given in a string -
370+
// semver, a revision, or as a fallback, a plain tag
371+
func DeduceConstraint(s string) Constraint {
372+
// always semver if we can
373+
c, err := NewSemverConstraint(s)
374+
if err == nil {
375+
return c
376+
}
377+
378+
slen := len(s)
379+
if slen == 40 {
380+
if _, err = hex.DecodeString(s); err == nil {
381+
// Whether or not it's intended to be a SHA1 digest, this is a
382+
// valid byte sequence for that, so go with Revision. This
383+
// covers git and hg
384+
return Revision(s)
385+
}
386+
}
387+
// Next, try for bzr, which has a three-component GUID separated by
388+
// dashes. There should be two, but the email part could contain
389+
// internal dashes
390+
if strings.Count(s, "-") >= 2 {
391+
// Work from the back to avoid potential confusion from the email
392+
i3 := strings.LastIndex(s, "-")
393+
// Skip if - is last char, otherwise this would panic on bounds err
394+
if slen == i3+1 {
395+
return NewVersion(s)
396+
}
397+
398+
i2 := strings.LastIndex(s[:i3], "-")
399+
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
400+
// Getting this far means it'd pretty much be nuts if it's not a
401+
// bzr rev, so don't bother parsing the email.
402+
return Revision(s)
403+
}
404+
}
405+
406+
// If not a plain SHA1 or bzr custom GUID, assume a plain version.
407+
// TODO: if there is amgibuity here, then prompt the user?
408+
return NewVersion(s)
409+
}

0 commit comments

Comments
 (0)