Skip to content

Commit 8753b96

Browse files
author
Erki Märks
committedOct 3, 2023
allow prerelease matching
1 parent 2f39fdc commit 8753b96

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed
 

‎constraints.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ var findConstraintRegex *regexp.Regexp
162162
var validConstraintRegex *regexp.Regexp
163163

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

168168
func init() {
@@ -221,9 +221,10 @@ type constraint struct {
221221
origfunc string
222222

223223
// When an x is used as part of the version (e.g., 1.x)
224-
minorDirty bool
225-
dirty bool
226-
patchDirty bool
224+
minorDirty bool
225+
dirty bool
226+
patchDirty bool
227+
prereleaseDirty bool
227228
}
228229

229230
// Check if a version meets the constraint
@@ -251,10 +252,16 @@ func parseConstraint(c string) (*constraint, error) {
251252
}
252253

253254
ver := m[2]
254-
minorDirty := false
255+
prereleaseDirty := false
255256
patchDirty := false
257+
minorDirty := false
256258
dirty := false
257-
if isX(m[3]) || m[3] == "" {
259+
260+
if (isX(m[3]) || m[3] == "") && len(m) > 8 && m[7] != "" && isX(strings.TrimPrefix(m[8], ".")) {
261+
ver = fmt.Sprintf("0.0.0-%s.0", strings.Split(m[7], ".")[0])
262+
dirty = true
263+
prereleaseDirty = true
264+
} else if isX(m[3]) || m[3] == "" {
258265
ver = fmt.Sprintf("0.0.0%s", m[6])
259266
dirty = true
260267
} else if isX(strings.TrimPrefix(m[4], ".")) || m[4] == "" {
@@ -278,6 +285,7 @@ func parseConstraint(c string) (*constraint, error) {
278285
cs.con = con
279286
cs.minorDirty = minorDirty
280287
cs.patchDirty = patchDirty
288+
cs.prereleaseDirty = prereleaseDirty
281289
cs.dirty = dirty
282290

283291
return cs, nil
@@ -471,6 +479,11 @@ func constraintTilde(v *Version, c *constraint) (bool, error) {
471479
// equivalent to >= 0.0.0.
472480
if c.con.Major() == 0 && c.con.Minor() == 0 && c.con.Patch() == 0 &&
473481
!c.minorDirty && !c.patchDirty {
482+
483+
if c.prereleaseDirty && strings.Split(v.pre, ".")[0] != strings.Split(c.con.pre, ".")[0] {
484+
return false, nil
485+
}
486+
474487
return true, nil
475488
}
476489

‎constraints_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func TestConstraintCheck(t *testing.T) {
125125
{"*", "4.5.6", true},
126126
{"*", "1.2.3-alpha.1", false},
127127
{"*-0", "1.2.3-alpha.1", true},
128+
{"*-alpha.*", "1.2.3-alpha.1", true},
129+
{"*-alpha.*", "1.2.3-beta.1", false},
128130
{"2.*", "1", false},
129131
{"2.*", "3.4.5", false},
130132
{"2.*", "2.1.1", true},

0 commit comments

Comments
 (0)