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

Commit e0c7ec3

Browse files
authored
Merge pull request #587 from sectioneight/handle-gopkg-unstable
Add support for -unstable gopkg.in imports
2 parents 8bdd9e5 + c4e1427 commit e0c7ec3

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

internal/gps/deduce.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var (
2727
svnSchemes = []string{"https", "http", "svn", "svn+ssh"}
2828
)
2929

30+
const gopkgUnstableSuffix = "-unstable"
31+
3032
func validateVCSScheme(scheme, typ string) bool {
3133
// everything allows plain ssh
3234
if scheme == "ssh" {
@@ -276,10 +278,18 @@ func (m gopkginDeducer) deduceSource(p string, u *url.URL) (maybeSource, error)
276278
} else {
277279
u.Path = path.Join(v[2], v[3])
278280
}
279-
major, err := strconv.ParseUint(v[4][1:], 10, 64)
281+
282+
unstable := false
283+
majorStr := v[4]
284+
285+
if strings.HasSuffix(majorStr, gopkgUnstableSuffix) {
286+
unstable = true
287+
majorStr = strings.TrimSuffix(majorStr, gopkgUnstableSuffix)
288+
}
289+
major, err := strconv.ParseUint(majorStr[1:], 10, 64)
280290
if err != nil {
281291
// this should only be reachable if there's an error in the regex
282-
return nil, fmt.Errorf("could not parse %q as a gopkg.in major version", v[4][1:])
292+
return nil, fmt.Errorf("could not parse %q as a gopkg.in major version", majorStr[1:])
283293
}
284294

285295
mb := make(maybeSources, len(gitSchemes))
@@ -290,9 +300,10 @@ func (m gopkginDeducer) deduceSource(p string, u *url.URL) (maybeSource, error)
290300
}
291301
u2.Scheme = scheme
292302
mb[k] = maybeGopkginSource{
293-
opath: v[1],
294-
url: &u2,
295-
major: major,
303+
opath: v[1],
304+
url: &u2,
305+
major: major,
306+
unstable: unstable,
296307
}
297308
}
298309

internal/gps/maybe_source.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ type maybeGopkginSource struct {
131131
url *url.URL
132132
// the major version to apply for filtering
133133
major uint64
134+
// whether or not the source package is "unstable"
135+
unstable bool
134136
}
135137

136138
func (m maybeGopkginSource) try(ctx context.Context, cachedir string, c singleSourceCache, superv *supervisor) (source, sourceState, error) {
@@ -151,7 +153,8 @@ func (m maybeGopkginSource) try(ctx context.Context, cachedir string, c singleSo
151153
repo: &gitRepo{r},
152154
},
153155
},
154-
major: m.major,
156+
major: m.major,
157+
unstable: m.unstable,
155158
}
156159

157160
var vl []PairedVersion

internal/gps/vcs_source.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ func (s *gitSource) listVersions(ctx context.Context) (vlist []PairedVersion, er
278278
// according to the input URL.
279279
type gopkginSource struct {
280280
gitSource
281-
major uint64
281+
major uint64
282+
unstable bool
282283
}
283284

284285
func (s *gopkginSource) listVersions(ctx context.Context) ([]PairedVersion, error) {
@@ -297,23 +298,27 @@ func (s *gopkginSource) listVersions(ctx context.Context) ([]PairedVersion, erro
297298
pv := v.(versionPair)
298299
switch tv := pv.v.(type) {
299300
case semVersion:
300-
if tv.sv.Major() == s.major {
301+
if tv.sv.Major() == s.major && !s.unstable {
301302
vlist[k] = v
302303
k++
303304
}
304305
case branchVersion:
305306
// The semver lib isn't exactly the same as gopkg.in's logic, but
306307
// it's close enough that it's probably fine to use. We can be more
307-
// exact if real problems crop up. The most obvious vector for
308-
// problems is that we totally ignore the "unstable" designation
309-
// right now.
308+
// exact if real problems crop up.
310309
sv, err := semver.NewVersion(tv.name)
311310
if err != nil || sv.Major() != s.major {
312311
// not a semver-shaped branch name at all, or not the same major
313312
// version as specified in the import path constraint
314313
continue
315314
}
316315

316+
// Gopkg.in has a special "-unstable" suffix which we need to handle
317+
// separately.
318+
if s.unstable != strings.HasSuffix(tv.name, gopkgUnstableSuffix) {
319+
continue
320+
}
321+
317322
// Turn off the default branch marker unconditionally; we can't know
318323
// which one to mark as default until we've seen them all
319324
tv.isDefault = false

internal/gps/vcs_source_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/url"
1111
"os/exec"
1212
"reflect"
13+
"strings"
1314
"sync"
1415
"testing"
1516
)
@@ -152,10 +153,12 @@ func testGopkginSourceInteractions(t *testing.T) {
152153
t.Errorf("URL was bad, lolwut? errtext: %s", err)
153154
return
154155
}
156+
unstable := strings.HasSuffix(opath, gopkgUnstableSuffix)
155157
mb := maybeGopkginSource{
156-
opath: opath,
157-
url: u,
158-
major: major,
158+
opath: opath,
159+
url: u,
160+
major: major,
161+
unstable: unstable,
159162
}
160163

161164
ctx := context.Background()
@@ -240,7 +243,7 @@ func testGopkginSourceInteractions(t *testing.T) {
240243

241244
// simultaneously run for v1, v2, and v3 filters of the target repo
242245
wg := &sync.WaitGroup{}
243-
wg.Add(3)
246+
wg.Add(4)
244247
go func() {
245248
tfunc("gopkg.in/sdboyer/gpkt.v1", "github.com/sdboyer/gpkt", 1, []Version{
246249
NewVersion("v1.1.0").Is(Revision("b2cb48dda625f6640b34d9ffb664533359ac8b91")),
@@ -265,6 +268,13 @@ func testGopkginSourceInteractions(t *testing.T) {
265268
wg.Done()
266269
}()
267270

271+
go func() {
272+
tfunc("github.com/sdboyer/gpkt2.v1-unstable", "github.com/sdboyer/gpkt2", 1, []Version{
273+
newDefaultBranch("v1-unstable").Is(Revision("24de0be8f4a0b8a44321562117749b257bfcef69")),
274+
})
275+
wg.Done()
276+
}()
277+
268278
wg.Wait()
269279
}
270280

0 commit comments

Comments
 (0)