This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #688 from jmank88/version_in_workspace
Break up Ctx.VersionInWorkspace
- Loading branch information
Showing
5 changed files
with
141 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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 gps | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Masterminds/vcs" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// VCSVersion returns the current project version for an absolute path. | ||
func VCSVersion(path string) (Version, error) { | ||
repo, err := vcs.NewRepo("", path) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "creating new repo for root: %s", path) | ||
} | ||
|
||
ver, err := repo.Current() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "finding current branch/version for root: %s", path) | ||
} | ||
|
||
rev, err := repo.Version() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "getting repo version for root: %s", path) | ||
} | ||
|
||
// First look through tags. | ||
tags, err := repo.Tags() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "getting repo tags for root: %s", path) | ||
} | ||
// Try to match the current version to a tag. | ||
if contains(tags, ver) { | ||
// Assume semver if it starts with a v. | ||
if strings.HasPrefix(ver, "v") { | ||
return NewVersion(ver).Pair(Revision(rev)), nil | ||
} | ||
|
||
return nil, errors.Errorf("version for root %s does not start with a v: %q", path, ver) | ||
} | ||
|
||
// Look for the current branch. | ||
branches, err := repo.Branches() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "getting repo branch for root: %s") | ||
} | ||
// Try to match the current version to a branch. | ||
if contains(branches, ver) { | ||
return NewBranch(ver).Pair(Revision(rev)), nil | ||
} | ||
|
||
return Revision(rev), nil | ||
} | ||
|
||
// contains checks if a array of strings contains a value | ||
func contains(a []string, b string) bool { | ||
for _, v := range a { | ||
if b == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 gps | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/golang/dep/internal/test" | ||
) | ||
|
||
func TestVCSVersion(t *testing.T) { | ||
test.NeedsExternalNetwork(t) | ||
test.NeedsGit(t) | ||
|
||
h := test.NewHelper(t) | ||
defer h.Cleanup() | ||
|
||
h.TempDir("src") | ||
gopath := h.Path(".") | ||
h.Setenv("GOPATH", gopath) | ||
|
||
importPaths := map[string]struct { | ||
rev Version | ||
checkout bool | ||
}{ | ||
"github.com/pkg/errors": { | ||
rev: NewVersion("v0.8.0").Pair("645ef00459ed84a119197bfb8d8205042c6df63d"), // semver | ||
checkout: true, | ||
}, | ||
"github.com/sirupsen/logrus": { | ||
rev: Revision("42b84f9ec624953ecbf81a94feccb3f5935c5edf"), // random sha | ||
checkout: true, | ||
}, | ||
"github.com/rsc/go-get-default-branch": { | ||
rev: NewBranch("another-branch").Pair("8e6902fdd0361e8fa30226b350e62973e3625ed5"), | ||
}, | ||
} | ||
|
||
// checkout the specified revisions | ||
for ip, info := range importPaths { | ||
h.RunGo("get", ip) | ||
repoDir := h.Path("src/" + ip) | ||
if info.checkout { | ||
h.RunGit(repoDir, "checkout", info.rev.String()) | ||
} | ||
abs := filepath.FromSlash(filepath.Join(gopath, "src", ip)) | ||
got, err := VCSVersion(abs) | ||
h.Must(err) | ||
|
||
if got != info.rev { | ||
t.Fatalf("expected %q, got %q", got.String(), info.rev.String()) | ||
} | ||
} | ||
} |