Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow prerelease matching #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var findConstraintRegex *regexp.Regexp
var validConstraintRegex *regexp.Regexp

const cvRegex string = `v?([0-9|x|X|\*]+)(\.[0-9|x|X|\*]+)?(\.[0-9|x|X|\*]+)?` +
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-\*]+)*))?` +
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`

func init() {
Expand Down Expand Up @@ -221,9 +221,10 @@ type constraint struct {
origfunc string

// When an x is used as part of the version (e.g., 1.x)
minorDirty bool
dirty bool
patchDirty bool
minorDirty bool
dirty bool
patchDirty bool
prereleaseDirty bool
}

// Check if a version meets the constraint
Expand Down Expand Up @@ -251,20 +252,38 @@ func parseConstraint(c string) (*constraint, error) {
}

ver := m[2]
minorDirty := false
prereleaseDirty := false
patchDirty := false
minorDirty := false
dirty := false

if len(m) > 8 && m[7] != "" && isX(strings.TrimPrefix(m[8], ".")) {
prereleaseDirty = true
}

if isX(m[3]) || m[3] == "" {
ver = fmt.Sprintf("0.0.0%s", m[6])
if prereleaseDirty {
ver = fmt.Sprintf("0.0.0-%s.0", strings.Split(m[7], ".")[0])
} else {
ver = fmt.Sprintf("0.0.0%s", m[6])
}
dirty = true
} else if isX(strings.TrimPrefix(m[4], ".")) || m[4] == "" {
if prereleaseDirty {
ver = fmt.Sprintf("%s.0.0-%s.0", m[3], strings.Split(m[7], ".")[0])
} else {
ver = fmt.Sprintf("%s.0.0%s", m[3], m[6])
}
minorDirty = true
dirty = true
ver = fmt.Sprintf("%s.0.0%s", m[3], m[6])
} else if isX(strings.TrimPrefix(m[5], ".")) || m[5] == "" {
if prereleaseDirty {
ver = fmt.Sprintf("%s%s.0-%s.0", m[3], m[4], strings.Split(m[7], ".")[0])
} else {
ver = fmt.Sprintf("%s%s.0%s", m[3], m[4], m[6])
}
dirty = true
patchDirty = true
ver = fmt.Sprintf("%s%s.0%s", m[3], m[4], m[6])
}

con, err := NewVersion(ver)
Expand All @@ -278,6 +297,7 @@ func parseConstraint(c string) (*constraint, error) {
cs.con = con
cs.minorDirty = minorDirty
cs.patchDirty = patchDirty
cs.prereleaseDirty = prereleaseDirty
cs.dirty = dirty

return cs, nil
Expand Down Expand Up @@ -467,6 +487,10 @@ func constraintTilde(v *Version, c *constraint) (bool, error) {
return false, fmt.Errorf("%s is less than %s", v, c.orig)
}

if c.prereleaseDirty && strings.Split(v.pre, ".")[0] != strings.Split(c.con.pre, ".")[0] {
return false, nil
}

// ~0.0.0 is a special case where all constraints are accepted. It's
// equivalent to >= 0.0.0.
if c.con.Major() == 0 && c.con.Minor() == 0 && c.con.Patch() == 0 &&
Expand Down
6 changes: 6 additions & 0 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func TestConstraintCheck(t *testing.T) {
{"*", "4.5.6", true},
{"*", "1.2.3-alpha.1", false},
{"*-0", "1.2.3-alpha.1", true},
{"*-alpha.*", "1.2.3-alpha.1", true},
{"*-alpha.*", "1.2.3-beta.1", false},
{"1.*-alpha.*", "1.2.3-alpha.1", true},
{"2.*-alpha.*", "1.2.3-alpha.1", false},
{"1.2.*-alpha.*", "1.2.3-alpha.1", true},
{"1.3.*-alpha.*", "1.2.3-alpha.1", false},
{"2.*", "1", false},
{"2.*", "3.4.5", false},
{"2.*", "2.1.1", true},
Expand Down