Skip to content

Commit 4d9b028

Browse files
committed
cmd/go/internal/modfetch: allow branch name as rev identifier
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 <[email protected]>
1 parent 8a75bf4 commit 4d9b028

File tree

4 files changed

+1080
-28
lines changed

4 files changed

+1080
-28
lines changed

vendor/cmd/go/internal/modfetch/coderepo_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ var codeRepoTests = []struct {
298298
time: time.Date(2016, 12, 8, 18, 13, 25, 0, time.UTC),
299299
gomod: "//vgo 0.0.4\n\nmodule gopkg.in/check.v1\n",
300300
},
301+
{
302+
path: "gopkg.in/yaml.v2",
303+
rev: "v2",
304+
version: "v0.0.0-20180328195020-5420a8b6744d",
305+
name: "5420a8b6744d3b0345ab293f6fcba19c978f1183",
306+
short: "5420a8b6744d",
307+
time: time.Date(2018, 3, 28, 19, 50, 20, 0, time.UTC),
308+
gomod: "module \"gopkg.in/yaml.v2\"\n\nrequire (\n\t\"gopkg.in/check.v1\" v0.0.0-20161208181325-20d25e280405\n)\n",
309+
},
310+
{
311+
path: "github.com/gobuffalo/buffalo",
312+
rev: "development",
313+
version: "v0.0.0-20180406185414-59b4005674b6",
314+
name: "59b4005674b633728e2bfc3bb09cc204f7c2d6f5",
315+
short: "59b4005674b6",
316+
time: time.Date(2018, 4, 6, 18, 54, 14, 0, time.UTC),
317+
gomod: "//vgo 0.0.4\n\nmodule github.com/gobuffalo/buffalo\n",
318+
},
301319
}
302320

303321
func TestCodeRepo(t *testing.T) {

vendor/cmd/go/internal/modfetch/github/fetch.go

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ func (r *repo) LatestAt(t time.Time, branch string) (*codehost.RevInfo, error) {
117117
return info, nil
118118
}
119119

120+
var refKinds = []string{"tags", "heads"}
121+
120122
func (r *repo) Stat(rev string) (*codehost.RevInfo, error) {
121123
var tag string
122124
if !codehost.AllHex(rev) {
@@ -130,41 +132,52 @@ func (r *repo) Stat(rev string) (*codehost.RevInfo, error) {
130132
URL string
131133
}
132134
}
133-
err := web.Get(
134-
"https://api.github.com/repos/"+url.PathEscape(r.owner)+"/"+url.PathEscape(r.repo)+"/git/refs/tags/"+tag,
135-
web.DecodeJSON(&ref),
136-
)
137-
if err != nil {
138-
return nil, err
139-
}
140-
switch ref.Object.Type {
141-
default:
142-
return nil, fmt.Errorf("invalid tag %q: not a commit or tag (%q)", tag, ref.Object.Type)
143135

144-
case "commit":
145-
rev = ref.Object.SHA
146-
147-
case "tag":
148-
var info struct {
149-
Object struct {
150-
SHA string
151-
Type string
152-
}
153-
}
154-
err = web.Get(
155-
ref.Object.URL,
156-
web.DecodeJSON(&info),
136+
var firstErr error
137+
for _, kind := range refKinds {
138+
err := web.Get(
139+
"https://api.github.com/repos/"+url.PathEscape(r.owner)+"/"+url.PathEscape(r.repo)+"/git/refs/"+kind+"/"+tag,
140+
web.DecodeJSON(&ref),
157141
)
158142
if err != nil {
159-
return nil, err
143+
if firstErr == nil {
144+
firstErr = err
145+
}
146+
continue
160147
}
161-
if info.Object.Type != "commit" {
162-
return nil, fmt.Errorf("invalid annotated tag %q: not a commit (%q)", tag, info.Object.Type)
148+
switch ref.Object.Type {
149+
default:
150+
return nil, fmt.Errorf("invalid ref %q: not a commit or tag (%q)", tag, ref.Object.Type)
151+
152+
case "commit":
153+
rev = ref.Object.SHA
154+
155+
case "tag":
156+
var info struct {
157+
Object struct {
158+
SHA string
159+
Type string
160+
}
161+
}
162+
err = web.Get(
163+
ref.Object.URL,
164+
web.DecodeJSON(&info),
165+
)
166+
if err != nil {
167+
return nil, err
168+
}
169+
if info.Object.Type != "commit" {
170+
return nil, fmt.Errorf("invalid annotated tag %q: not a commit (%q)", tag, info.Object.Type)
171+
}
172+
rev = info.Object.SHA
163173
}
164-
rev = info.Object.SHA
174+
if rev == "" {
175+
return nil, fmt.Errorf("invalid ref %q: missing SHA in GitHub response", tag)
176+
}
177+
break
165178
}
166179
if rev == "" {
167-
return nil, fmt.Errorf("invalid tag %q: missing SHA in GitHub response", tag)
180+
return nil, fmt.Errorf("unknown ref %q (%v)", tag, firstErr)
168181
}
169182
}
170183

vendor/cmd/go/internal/modfetch/query.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func Query(path, vers string, allowed func(module.Version) bool) (*RevInfo, erro
3535
}
3636

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

0 commit comments

Comments
 (0)