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

Commit 6c5ebb9

Browse files
authored
Merge pull request #688 from jmank88/version_in_workspace
Break up Ctx.VersionInWorkspace
2 parents 98f1d99 + 21d9f73 commit 6c5ebb9

File tree

5 files changed

+141
-113
lines changed

5 files changed

+141
-113
lines changed

cmd/dep/gopath_scanner.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ func (g *gopathScanner) scanGopathForDependencies() (projectData, error) {
219219
go syncDep(pr, g.sm)
220220

221221
dependencies[pr] = []string{ip}
222-
v, err := g.ctx.VersionInWorkspace(pr)
222+
abs, err := g.ctx.AbsForImport(string(pr))
223+
if err != nil {
224+
notondisk[pr] = true
225+
continue
226+
}
227+
v, err := gps.VCSVersion(abs)
223228
if err != nil {
224229
notondisk[pr] = true
225230
continue
@@ -283,7 +288,13 @@ func (g *gopathScanner) scanGopathForDependencies() (projectData, error) {
283288
// was found in the initial pass on direct imports. We know it's
284289
// the former if there's no entry for it in the ondisk map.
285290
if _, in := ondisk[pr]; !in {
286-
v, err := g.ctx.VersionInWorkspace(pr)
291+
abs, err := g.ctx.AbsForImport(string(pr))
292+
if err != nil {
293+
colors[pkg] = black
294+
notondisk[pr] = true
295+
return nil
296+
}
297+
v, err := gps.VCSVersion(abs)
287298
if err != nil {
288299
// Even if we know it's on disk, errors are still
289300
// possible when trying to deduce version. If we

context.go

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"runtime"
12-
"strings"
1312

14-
"github.com/Masterminds/vcs"
1513
"github.com/golang/dep/internal/fs"
1614
"github.com/golang/dep/internal/gps"
1715
"github.com/pkg/errors"
@@ -236,10 +234,10 @@ func (c *Ctx) ImportForAbs(path string) (string, error) {
236234
return "", errors.Errorf("%s not in GOPATH", path)
237235
}
238236

239-
// absoluteProjectRoot determines the absolute path to the project root
237+
// AbsForImport returns the absolute path for the project root
240238
// including the $GOPATH. This will not work with stdlib packages and the
241239
// package directory needs to exist.
242-
func (c *Ctx) absoluteProjectRoot(path string) (string, error) {
240+
func (c *Ctx) AbsForImport(path string) (string, error) {
243241
posspath := filepath.Join(c.GOPATH, "src", path)
244242
dirOK, err := fs.IsDir(posspath)
245243
if err != nil {
@@ -250,62 +248,3 @@ func (c *Ctx) absoluteProjectRoot(path string) (string, error) {
250248
}
251249
return posspath, nil
252250
}
253-
254-
func (c *Ctx) VersionInWorkspace(root gps.ProjectRoot) (gps.Version, error) {
255-
pr, err := c.absoluteProjectRoot(string(root))
256-
if err != nil {
257-
return nil, errors.Wrapf(err, "determine project root for %s", root)
258-
}
259-
260-
repo, err := vcs.NewRepo("", pr)
261-
if err != nil {
262-
return nil, errors.Wrapf(err, "creating new repo for root: %s", pr)
263-
}
264-
265-
ver, err := repo.Current()
266-
if err != nil {
267-
return nil, errors.Wrapf(err, "finding current branch/version for root: %s", pr)
268-
}
269-
270-
rev, err := repo.Version()
271-
if err != nil {
272-
return nil, errors.Wrapf(err, "getting repo version for root: %s", pr)
273-
}
274-
275-
// First look through tags.
276-
tags, err := repo.Tags()
277-
if err != nil {
278-
return nil, errors.Wrapf(err, "getting repo tags for root: %s", pr)
279-
}
280-
// Try to match the current version to a tag.
281-
if contains(tags, ver) {
282-
// Assume semver if it starts with a v.
283-
if strings.HasPrefix(ver, "v") {
284-
return gps.NewVersion(ver).Pair(gps.Revision(rev)), nil
285-
}
286-
287-
return nil, errors.Errorf("version for root %s does not start with a v: %q", pr, ver)
288-
}
289-
290-
// Look for the current branch.
291-
branches, err := repo.Branches()
292-
if err != nil {
293-
return nil, errors.Wrapf(err, "getting repo branch for root: %s")
294-
}
295-
// Try to match the current version to a branch.
296-
if contains(branches, ver) {
297-
return gps.NewBranch(ver).Pair(gps.Revision(rev)), nil
298-
}
299-
300-
return gps.Revision(rev), nil
301-
}
302-
303-
// contains checks if a array of strings contains a value
304-
func contains(a []string, b string) bool {
305-
for _, v := range a {
306-
if b == v {
307-
return true
308-
}
309-
}
310-
return false
311-
}

context_test.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"testing"
1515
"unicode"
1616

17-
"github.com/golang/dep/internal/gps"
1817
"github.com/golang/dep/internal/test"
1918
)
2019

@@ -81,7 +80,7 @@ func TestAbsoluteProjectRoot(t *testing.T) {
8180
}
8281

8382
for i, ok := range importPaths {
84-
got, err := depCtx.absoluteProjectRoot(i)
83+
got, err := depCtx.AbsForImport(i)
8584
if ok {
8685
h.Must(err)
8786
want := h.Path(filepath.Join("src", i))
@@ -98,57 +97,12 @@ func TestAbsoluteProjectRoot(t *testing.T) {
9897

9998
// test that a file fails
10099
h.TempFile("src/thing/thing.go", "hello world")
101-
_, err := depCtx.absoluteProjectRoot("thing/thing.go")
100+
_, err := depCtx.AbsForImport("thing/thing.go")
102101
if err == nil {
103102
t.Fatal("error should not be nil for a file found")
104103
}
105104
}
106105

107-
func TestVersionInWorkspace(t *testing.T) {
108-
test.NeedsExternalNetwork(t)
109-
test.NeedsGit(t)
110-
111-
h := test.NewHelper(t)
112-
defer h.Cleanup()
113-
114-
h.TempDir("src")
115-
h.Setenv("GOPATH", h.Path("."))
116-
depCtx := &Ctx{GOPATH: h.Path(".")}
117-
118-
importPaths := map[string]struct {
119-
rev gps.Version
120-
checkout bool
121-
}{
122-
"github.com/pkg/errors": {
123-
rev: gps.NewVersion("v0.8.0").Pair("645ef00459ed84a119197bfb8d8205042c6df63d"), // semver
124-
checkout: true,
125-
},
126-
"github.com/Sirupsen/logrus": {
127-
rev: gps.Revision("42b84f9ec624953ecbf81a94feccb3f5935c5edf"), // random sha
128-
checkout: true,
129-
},
130-
"github.com/rsc/go-get-default-branch": {
131-
rev: gps.NewBranch("another-branch").Pair("8e6902fdd0361e8fa30226b350e62973e3625ed5"),
132-
},
133-
}
134-
135-
// checkout the specified revisions
136-
for ip, info := range importPaths {
137-
h.RunGo("get", ip)
138-
repoDir := h.Path("src/" + ip)
139-
if info.checkout {
140-
h.RunGit(repoDir, "checkout", info.rev.String())
141-
}
142-
143-
got, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip))
144-
h.Must(err)
145-
146-
if got != info.rev {
147-
t.Fatalf("expected %q, got %q", got.String(), info.rev.String())
148-
}
149-
}
150-
}
151-
152106
func TestLoadProject(t *testing.T) {
153107
h := test.NewHelper(t)
154108
defer h.Cleanup()

internal/gps/vcs_version.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 gps
6+
7+
import (
8+
"strings"
9+
10+
"github.com/Masterminds/vcs"
11+
"github.com/pkg/errors"
12+
)
13+
14+
// VCSVersion returns the current project version for an absolute path.
15+
func VCSVersion(path string) (Version, error) {
16+
repo, err := vcs.NewRepo("", path)
17+
if err != nil {
18+
return nil, errors.Wrapf(err, "creating new repo for root: %s", path)
19+
}
20+
21+
ver, err := repo.Current()
22+
if err != nil {
23+
return nil, errors.Wrapf(err, "finding current branch/version for root: %s", path)
24+
}
25+
26+
rev, err := repo.Version()
27+
if err != nil {
28+
return nil, errors.Wrapf(err, "getting repo version for root: %s", path)
29+
}
30+
31+
// First look through tags.
32+
tags, err := repo.Tags()
33+
if err != nil {
34+
return nil, errors.Wrapf(err, "getting repo tags for root: %s", path)
35+
}
36+
// Try to match the current version to a tag.
37+
if contains(tags, ver) {
38+
// Assume semver if it starts with a v.
39+
if strings.HasPrefix(ver, "v") {
40+
return NewVersion(ver).Pair(Revision(rev)), nil
41+
}
42+
43+
return nil, errors.Errorf("version for root %s does not start with a v: %q", path, ver)
44+
}
45+
46+
// Look for the current branch.
47+
branches, err := repo.Branches()
48+
if err != nil {
49+
return nil, errors.Wrapf(err, "getting repo branch for root: %s")
50+
}
51+
// Try to match the current version to a branch.
52+
if contains(branches, ver) {
53+
return NewBranch(ver).Pair(Revision(rev)), nil
54+
}
55+
56+
return Revision(rev), nil
57+
}
58+
59+
// contains checks if a array of strings contains a value
60+
func contains(a []string, b string) bool {
61+
for _, v := range a {
62+
if b == v {
63+
return true
64+
}
65+
}
66+
return false
67+
}

internal/gps/vcs_version_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 gps
6+
7+
import (
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/golang/dep/internal/test"
12+
)
13+
14+
func TestVCSVersion(t *testing.T) {
15+
test.NeedsExternalNetwork(t)
16+
test.NeedsGit(t)
17+
18+
h := test.NewHelper(t)
19+
defer h.Cleanup()
20+
21+
h.TempDir("src")
22+
gopath := h.Path(".")
23+
h.Setenv("GOPATH", gopath)
24+
25+
importPaths := map[string]struct {
26+
rev Version
27+
checkout bool
28+
}{
29+
"github.com/pkg/errors": {
30+
rev: NewVersion("v0.8.0").Pair("645ef00459ed84a119197bfb8d8205042c6df63d"), // semver
31+
checkout: true,
32+
},
33+
"github.com/sirupsen/logrus": {
34+
rev: Revision("42b84f9ec624953ecbf81a94feccb3f5935c5edf"), // random sha
35+
checkout: true,
36+
},
37+
"github.com/rsc/go-get-default-branch": {
38+
rev: NewBranch("another-branch").Pair("8e6902fdd0361e8fa30226b350e62973e3625ed5"),
39+
},
40+
}
41+
42+
// checkout the specified revisions
43+
for ip, info := range importPaths {
44+
h.RunGo("get", ip)
45+
repoDir := h.Path("src/" + ip)
46+
if info.checkout {
47+
h.RunGit(repoDir, "checkout", info.rev.String())
48+
}
49+
abs := filepath.FromSlash(filepath.Join(gopath, "src", ip))
50+
got, err := VCSVersion(abs)
51+
h.Must(err)
52+
53+
if got != info.rev {
54+
t.Fatalf("expected %q, got %q", got.String(), info.rev.String())
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)