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

Commit 1f6e95f

Browse files
author
marwan-at-work
committed
move deduceConstraint to SourceManager
1 parent 24c37f5 commit 1f6e95f

File tree

6 files changed

+147
-73
lines changed

6 files changed

+147
-73
lines changed

cmd/dep/ensure.go

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ package main
66

77
import (
88
"bytes"
9-
"encoding/hex"
109
"flag"
1110
"fmt"
1211
"go/build"
1312
"log"
1413
"path/filepath"
15-
"strconv"
1614
"strings"
1715

1816
"github.com/golang/dep"
@@ -297,79 +295,13 @@ func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstrai
297295
}
298296

299297
pi := gps.ProjectIdentifier{ProjectRoot: pr, Source: source}
300-
c, err := deduceConstraint(versionStr, pi, sm)
298+
c, err := sm.DeduceConstraint(versionStr, pi)
301299
if err != nil {
302300
return emptyPC, err
303301
}
304302
return gps.ProjectConstraint{Ident: pi, Constraint: c}, nil
305303
}
306304

307-
// deduceConstraint tries to puzzle out what kind of version is given in a string -
308-
// semver, a revision, or as a fallback, a plain tag
309-
func deduceConstraint(s string, pi gps.ProjectIdentifier, sm gps.SourceManager) (gps.Constraint, error) {
310-
if s == "" {
311-
// Find the default branch
312-
versions, err := sm.ListVersions(pi)
313-
if err != nil {
314-
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
315-
}
316-
317-
gps.SortPairedForUpgrade(versions)
318-
for _, v := range versions {
319-
if v.Type() == gps.IsBranch {
320-
return v.Unpair(), nil
321-
}
322-
}
323-
}
324-
325-
// always semver if we can
326-
c, err := gps.NewSemverConstraintIC(s)
327-
if err == nil {
328-
return c, nil
329-
}
330-
331-
slen := len(s)
332-
if slen == 40 {
333-
if _, err = hex.DecodeString(s); err == nil {
334-
// Whether or not it's intended to be a SHA1 digest, this is a
335-
// valid byte sequence for that, so go with Revision. This
336-
// covers git and hg
337-
return gps.Revision(s), nil
338-
}
339-
}
340-
// Next, try for bzr, which has a three-component GUID separated by
341-
// dashes. There should be two, but the email part could contain
342-
// internal dashes
343-
if strings.Count(s, "-") >= 2 {
344-
// Work from the back to avoid potential confusion from the email
345-
i3 := strings.LastIndex(s, "-")
346-
// Skip if - is last char, otherwise this would panic on bounds err
347-
if slen == i3+1 {
348-
return gps.NewVersion(s), nil
349-
}
350-
351-
i2 := strings.LastIndex(s[:i3], "-")
352-
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
353-
// Getting this far means it'd pretty much be nuts if it's not a
354-
// bzr rev, so don't bother parsing the email.
355-
return gps.Revision(s), nil
356-
}
357-
}
358-
359-
// call out to network and get the package's versions
360-
versions, err := sm.ListVersions(pi)
361-
if err != nil {
362-
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
363-
}
364-
365-
for _, version := range versions {
366-
if s == version.String() {
367-
return version.Unpair(), nil
368-
}
369-
}
370-
return nil, errors.Errorf("%s is not a valid version for the package %s(%s)", s, pi.ProjectRoot, pi.Source)
371-
}
372-
373305
func checkErrors(m map[string]pkgtree.PackageOrErr) error {
374306
noGoErrors, pkgErrors := 0, 0
375307
for _, poe := range m {

cmd/dep/ensure_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestDeduceConstraint(t *testing.T) {
3838

3939
pi := gps.ProjectIdentifier{ProjectRoot: "github.com/sdboyer/deptest"}
4040
for str, want := range constraints {
41-
got, err := deduceConstraint(str, pi, sm)
41+
got, err := sm.DeduceConstraint(str, pi)
4242
h.Must(err)
4343

4444
wantT := reflect.TypeOf(want)
@@ -68,7 +68,7 @@ func TestDeduceConstraint_InvalidInput(t *testing.T) {
6868

6969
pi := gps.ProjectIdentifier{ProjectRoot: "github.com/sdboyer/deptest"}
7070
for _, str := range constraints {
71-
_, err := deduceConstraint(str, pi, sm)
71+
_, err := sm.DeduceConstraint(str, pi)
7272
if err == nil {
7373
t.Errorf("expected %s to produce an error", str)
7474
}

cmd/dep/glide_importer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (g *glideImporter) buildProjectConstraint(pkg glidePackage) (pc gps.Project
206206
}
207207

208208
pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Name), Source: pkg.Repository}
209-
pc.Constraint, err = deduceConstraint(pkg.Reference, pc.Ident, g.sm)
209+
pc.Constraint, err = g.sm.DeduceConstraint(pkg.Reference, pc.Ident)
210210

211211
f := fb.NewConstraintFeedback(pc, fb.DepTypeImported)
212212
f.LogFeedback(g.logger)

cmd/dep/godep_importer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (g *godepImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, e
158158
// create a project constraint
159159
func (g *godepImporter) buildProjectConstraint(pkg godepPackage) (pc gps.ProjectConstraint, err error) {
160160
pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.ImportPath)}
161-
pc.Constraint, err = deduceConstraint(pkg.Comment, pc.Ident, g.sm)
161+
pc.Constraint, err = g.sm.DeduceConstraint(pkg.Comment, pc.Ident)
162162

163163
f := fb.NewConstraintFeedback(pc, fb.DepTypeImported)
164164
f.LogFeedback(g.logger)

internal/gps/solve_basic_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
package gps
66

77
import (
8+
"encoding/hex"
89
"fmt"
910
"regexp"
11+
"strconv"
1012
"strings"
1113

1214
"github.com/Masterminds/semver"
1315
"github.com/golang/dep/internal/gps/pkgtree"
16+
"github.com/pkg/errors"
1417
)
1518

1619
var regfrom = regexp.MustCompile(`^(\w*) from (\w*) ([0-9\.\*]*)`)
@@ -1550,6 +1553,72 @@ func (sm *depspecSourceManager) ignore() map[string]bool {
15501553
return sm.ig
15511554
}
15521555

1556+
// DeduceConstraint tries to puzzle out what kind of version is given in a string -
1557+
// semver, a revision, or as a fallback, a plain tag
1558+
func (sm *depspecSourceManager) DeduceConstraint(s string, pi ProjectIdentifier) (Constraint, error) {
1559+
if s == "" {
1560+
// Find the default branch
1561+
versions, err := sm.ListVersions(pi)
1562+
if err != nil {
1563+
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
1564+
}
1565+
1566+
SortPairedForUpgrade(versions)
1567+
for _, v := range versions {
1568+
if v.Type() == IsBranch {
1569+
return v.Unpair(), nil
1570+
}
1571+
}
1572+
}
1573+
1574+
// always semver if we can
1575+
c, err := NewSemverConstraintIC(s)
1576+
if err == nil {
1577+
return c, nil
1578+
}
1579+
1580+
slen := len(s)
1581+
if slen == 40 {
1582+
if _, err = hex.DecodeString(s); err == nil {
1583+
// Whether or not it's intended to be a SHA1 digest, this is a
1584+
// valid byte sequence for that, so go with Revision. This
1585+
// covers git and hg
1586+
return Revision(s), nil
1587+
}
1588+
}
1589+
// Next, try for bzr, which has a three-component GUID separated by
1590+
// dashes. There should be two, but the email part could contain
1591+
// internal dashes
1592+
if strings.Count(s, "-") >= 2 {
1593+
// Work from the back to avoid potential confusion from the email
1594+
i3 := strings.LastIndex(s, "-")
1595+
// Skip if - is last char, otherwise this would panic on bounds err
1596+
if slen == i3+1 {
1597+
return NewVersion(s), nil
1598+
}
1599+
1600+
i2 := strings.LastIndex(s[:i3], "-")
1601+
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
1602+
// Getting this far means it'd pretty much be nuts if it's not a
1603+
// bzr rev, so don't bother parsing the email.
1604+
return Revision(s), nil
1605+
}
1606+
}
1607+
1608+
// call out to network and get the package's versions
1609+
versions, err := sm.ListVersions(pi)
1610+
if err != nil {
1611+
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
1612+
}
1613+
1614+
for _, version := range versions {
1615+
if s == version.String() {
1616+
return version.Unpair(), nil
1617+
}
1618+
}
1619+
return nil, errors.Errorf("%s is not a valid version for the package %s(%s)", s, pi.ProjectRoot, pi.Source)
1620+
}
1621+
15531622
type depspecBridge struct {
15541623
*bridge
15551624
}

internal/gps/source_manager.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ package gps
66

77
import (
88
"context"
9+
"encoding/hex"
910
"fmt"
1011
"os"
1112
"os/signal"
1213
"path/filepath"
1314
"runtime"
15+
"strconv"
1416
"strings"
1517
"sync"
1618
"sync/atomic"
1719
"time"
1820

1921
"github.com/golang/dep/internal/gps/pkgtree"
22+
"github.com/pkg/errors"
2023
"github.com/sdboyer/constext"
2124
)
2225

@@ -72,6 +75,10 @@ type SourceManager interface {
7275
// no longer safe to call methods against it; all method calls will
7376
// immediately result in errors.
7477
Release()
78+
79+
// DeduceConstraint tries to puzzle out what kind of version is given in a string -
80+
// semver, a revision, or as a fallback, a plain tag
81+
DeduceConstraint(s string, pi ProjectIdentifier) (Constraint, error)
7582
}
7683

7784
// A ProjectAnalyzer is responsible for analyzing a given path for Manifest and
@@ -455,6 +462,72 @@ func (sm *SourceMgr) DeduceProjectRoot(ip string) (ProjectRoot, error) {
455462
return ProjectRoot(pd.root), err
456463
}
457464

465+
// DeduceConstraint tries to puzzle out what kind of version is given in a string -
466+
// semver, a revision, or as a fallback, a plain tag
467+
func (sm *SourceMgr) DeduceConstraint(s string, pi ProjectIdentifier) (Constraint, error) {
468+
if s == "" {
469+
// Find the default branch
470+
versions, err := sm.ListVersions(pi)
471+
if err != nil {
472+
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
473+
}
474+
475+
SortPairedForUpgrade(versions)
476+
for _, v := range versions {
477+
if v.Type() == IsBranch {
478+
return v.Unpair(), nil
479+
}
480+
}
481+
}
482+
483+
// always semver if we can
484+
c, err := NewSemverConstraintIC(s)
485+
if err == nil {
486+
return c, nil
487+
}
488+
489+
slen := len(s)
490+
if slen == 40 {
491+
if _, err = hex.DecodeString(s); err == nil {
492+
// Whether or not it's intended to be a SHA1 digest, this is a
493+
// valid byte sequence for that, so go with Revision. This
494+
// covers git and hg
495+
return Revision(s), nil
496+
}
497+
}
498+
// Next, try for bzr, which has a three-component GUID separated by
499+
// dashes. There should be two, but the email part could contain
500+
// internal dashes
501+
if strings.Count(s, "-") >= 2 {
502+
// Work from the back to avoid potential confusion from the email
503+
i3 := strings.LastIndex(s, "-")
504+
// Skip if - is last char, otherwise this would panic on bounds err
505+
if slen == i3+1 {
506+
return NewVersion(s), nil
507+
}
508+
509+
i2 := strings.LastIndex(s[:i3], "-")
510+
if _, err = strconv.ParseUint(s[i2+1:i3], 10, 64); err == nil {
511+
// Getting this far means it'd pretty much be nuts if it's not a
512+
// bzr rev, so don't bother parsing the email.
513+
return Revision(s), nil
514+
}
515+
}
516+
517+
// call out to network and get the package's versions
518+
versions, err := sm.ListVersions(pi)
519+
if err != nil {
520+
return nil, errors.Wrapf(err, "list versions for %s(%s)", pi.ProjectRoot, pi.Source) // means repo does not exist
521+
}
522+
523+
for _, version := range versions {
524+
if s == version.String() {
525+
return version.Unpair(), nil
526+
}
527+
}
528+
return nil, errors.Errorf("%s is not a valid version for the package %s(%s)", s, pi.ProjectRoot, pi.Source)
529+
}
530+
458531
type timeCount struct {
459532
count int
460533
start time.Time

0 commit comments

Comments
 (0)