Skip to content

Commit

Permalink
cmd/go/internal/modfetch: allow branch name as rev identifier
Browse files Browse the repository at this point in the history
If someone runs "vgo get x@rev" or adds "require x rev" to go.mod
and rev is a commit hash or git tag, vgo automatically replaces it
with the pseudo-version for the underlying commit.
This CL makes vgo do the same for branch names:
the head commit of the branch at that moment is recorded.
This allows running

	vgo get github.com/gobuffalo/buffalo@development

to sync with the current buffalo development branch.

Fixes golang/go#24045.

Change-Id: I8a58ed3a574e5ee839d37ad452436f80ac8081dc
Reviewed-on: https://go-review.googlesource.com/105216
Reviewed-by: Bryan C. Mills <bcmills@google.com>
  • Loading branch information
rsc committed Apr 25, 2018
1 parent 8a75bf4 commit 4d9b028
Show file tree
Hide file tree
Showing 4 changed files with 1,080 additions and 28 deletions.
18 changes: 18 additions & 0 deletions vendor/cmd/go/internal/modfetch/coderepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,24 @@ var codeRepoTests = []struct {
time: time.Date(2016, 12, 8, 18, 13, 25, 0, time.UTC),
gomod: "//vgo 0.0.4\n\nmodule gopkg.in/check.v1\n",
},
{
path: "gopkg.in/yaml.v2",
rev: "v2",
version: "v0.0.0-20180328195020-5420a8b6744d",
name: "5420a8b6744d3b0345ab293f6fcba19c978f1183",
short: "5420a8b6744d",
time: time.Date(2018, 3, 28, 19, 50, 20, 0, time.UTC),
gomod: "module \"gopkg.in/yaml.v2\"\n\nrequire (\n\t\"gopkg.in/check.v1\" v0.0.0-20161208181325-20d25e280405\n)\n",
},
{
path: "github.com/gobuffalo/buffalo",
rev: "development",
version: "v0.0.0-20180406185414-59b4005674b6",
name: "59b4005674b633728e2bfc3bb09cc204f7c2d6f5",
short: "59b4005674b6",
time: time.Date(2018, 4, 6, 18, 54, 14, 0, time.UTC),
gomod: "//vgo 0.0.4\n\nmodule github.com/gobuffalo/buffalo\n",
},
}

func TestCodeRepo(t *testing.T) {
Expand Down
69 changes: 41 additions & 28 deletions vendor/cmd/go/internal/modfetch/github/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (r *repo) LatestAt(t time.Time, branch string) (*codehost.RevInfo, error) {
return info, nil
}

var refKinds = []string{"tags", "heads"}

func (r *repo) Stat(rev string) (*codehost.RevInfo, error) {
var tag string
if !codehost.AllHex(rev) {
Expand All @@ -130,41 +132,52 @@ func (r *repo) Stat(rev string) (*codehost.RevInfo, error) {
URL string
}
}
err := web.Get(
"https://api.github.com/repos/"+url.PathEscape(r.owner)+"/"+url.PathEscape(r.repo)+"/git/refs/tags/"+tag,
web.DecodeJSON(&ref),
)
if err != nil {
return nil, err
}
switch ref.Object.Type {
default:
return nil, fmt.Errorf("invalid tag %q: not a commit or tag (%q)", tag, ref.Object.Type)

case "commit":
rev = ref.Object.SHA

case "tag":
var info struct {
Object struct {
SHA string
Type string
}
}
err = web.Get(
ref.Object.URL,
web.DecodeJSON(&info),
var firstErr error
for _, kind := range refKinds {
err := web.Get(
"https://api.github.com/repos/"+url.PathEscape(r.owner)+"/"+url.PathEscape(r.repo)+"/git/refs/"+kind+"/"+tag,
web.DecodeJSON(&ref),
)
if err != nil {
return nil, err
if firstErr == nil {
firstErr = err
}
continue
}
if info.Object.Type != "commit" {
return nil, fmt.Errorf("invalid annotated tag %q: not a commit (%q)", tag, info.Object.Type)
switch ref.Object.Type {
default:
return nil, fmt.Errorf("invalid ref %q: not a commit or tag (%q)", tag, ref.Object.Type)

case "commit":
rev = ref.Object.SHA

case "tag":
var info struct {
Object struct {
SHA string
Type string
}
}
err = web.Get(
ref.Object.URL,
web.DecodeJSON(&info),
)
if err != nil {
return nil, err
}
if info.Object.Type != "commit" {
return nil, fmt.Errorf("invalid annotated tag %q: not a commit (%q)", tag, info.Object.Type)
}
rev = info.Object.SHA
}
rev = info.Object.SHA
if rev == "" {
return nil, fmt.Errorf("invalid ref %q: missing SHA in GitHub response", tag)
}
break
}
if rev == "" {
return nil, fmt.Errorf("invalid tag %q: missing SHA in GitHub response", tag)
return nil, fmt.Errorf("unknown ref %q (%v)", tag, firstErr)
}
}

Expand Down
2 changes: 2 additions & 0 deletions vendor/cmd/go/internal/modfetch/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func Query(path, vers string, allowed func(module.Version) bool) (*RevInfo, erro
}

if strings.HasPrefix(vers, "v") && semver.IsValid(vers) {
// TODO: This turns query for "v2" into Stat "v2.0.0",
// but probably it should allow checking for a branch named "v2".
return repo.Stat(semver.Canonical(vers))
}
if strings.HasPrefix(vers, ">") || strings.HasPrefix(vers, "<") || vers == "latest" {
Expand Down
Loading

0 comments on commit 4d9b028

Please sign in to comment.