From 131b99abb1dd1edb6b221baf2e2fce52f4697def Mon Sep 17 00:00:00 2001 From: Beorn Facchini Date: Wed, 13 Mar 2019 17:36:46 +1100 Subject: [PATCH 1/5] Support for constraint operators caret (^) and tilde (~) --- constraint.go | 30 ++++++++++++++++++++++++++++++ constraint_test.go | 13 +++++++++++++ 2 files changed, 43 insertions(+) diff --git a/constraint.go b/constraint.go index d055759..c1b9c1f 100644 --- a/constraint.go +++ b/constraint.go @@ -35,6 +35,8 @@ func init() { ">=": constraintGreaterThanEqual, "<=": constraintLessThanEqual, "~>": constraintPessimistic, + "^": constraintCaret, + "~": constraintTilde, } ops := make([]string, 0, len(constraintOperators)) @@ -202,3 +204,31 @@ func constraintPessimistic(v, c *Version) bool { // If nothing has rejected the version by now, it's valid return true } + +func constraintCaret(v, c *Version) bool { + if !prereleaseCheck(v, c) || v.LessThan(c) { + return false + } + + if v.segments[0] != c.segments[0] { + return false + } + + return true +} + +func constraintTilde(v, c *Version) bool { + if !prereleaseCheck(v, c) || v.LessThan(c) { + return false + } + + if v.segments[0] != c.segments[0] { + return false + } + + if v.segments[1] != c.segments[1] && c.si > 1 { + return false + } + + return true +} diff --git a/constraint_test.go b/constraint_test.go index 9c5bee3..7076566 100644 --- a/constraint_test.go +++ b/constraint_test.go @@ -75,6 +75,19 @@ func TestConstraintCheck(t *testing.T) { {">= 2.1.0-a", "2.1.1-beta", false}, {">= 2.1.0-a", "2.1.0", true}, {"<= 2.1.0-a", "2.0.0", true}, + {"^1.1", "1.1.1", true}, + {"^1.1", "1.2.3", true}, + {"^1.1", "2.1.0", false}, + {"^1.1.2", "1.1.1", false}, + {"^1.1.2", "1.1.2", true}, + {"^1.1.2", "1.1.2.3", true}, + {"~1", "1.3.5", true}, + {"~1", "2.1.0", false}, + {"~1.1", "1.1.1", true}, + {"~1.1", "1.2.3", false}, + {"~1.1.2", "1.1.1", false}, + {"~1.1.2", "1.1.2", true}, + {"~1.1.2", "1.1.2.3", true}, } for _, tc := range cases { From df5297b48070668fb18468e265e53fc3182590ec Mon Sep 17 00:00:00 2001 From: Matthew Andrews Date: Sat, 18 Jun 2016 21:31:16 +0100 Subject: [PATCH 2/5] Implement constraint logic for caret (^) and tilde (~) --- constraint_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/constraint_test.go b/constraint_test.go index 6f62013..9ef16db 100644 --- a/constraint_test.go +++ b/constraint_test.go @@ -88,6 +88,28 @@ func TestConstraintCheck(t *testing.T) { {"~1.1.2", "1.1.1", false}, {"~1.1.2", "1.1.2", true}, {"~1.1.2", "1.1.2.3", true}, + {"^1.0.0", "1.0.0", true}, + {"^1.0.0", "1.0.1", true}, + {"^1.0.0", "1.9.1", true}, + {"^1.0.0", "2.0.0", false}, + {"^0.1.0", "0.1.0", true}, + {"^0.1.0", "0.2.0", false}, + {"^0.1.0", "0.1.4", true}, + {"^0.0.1", "0.0.0", false}, + {"^0.0.1", "0.0.1", true}, + {"^0.0.1", "0.0.2", false}, + {"^0.0.1", "0.2.0", false}, + {"~1.0.0", "1.0.0", true}, + {"~1.0.0", "1.0.1", true}, + {"~1.0.0", "1.9.1", false}, + {"~1.0.0", "2.0.0", false}, + {"~0.1.0", "0.1.0", true}, + {"~0.1.0", "0.2.0", false}, + {"~0.1.0", "0.1.4", true}, + {"~0.0.1", "0.0.0", false}, + {"~0.0.1", "0.0.1", true}, + {"~0.0.1", "0.0.2", false}, + {"~0.0.1", "0.2.0", false}, } for _, tc := range cases { From 58c48d32f3f6f99c91386461ece34ae3f5f1ec11 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 19 Nov 2020 14:30:17 +0100 Subject: [PATCH 3/5] fix & extend tests --- constraint_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/constraint_test.go b/constraint_test.go index 9ef16db..6d1479e 100644 --- a/constraint_test.go +++ b/constraint_test.go @@ -87,18 +87,22 @@ func TestConstraintCheck(t *testing.T) { {"~1.1", "1.2.3", false}, {"~1.1.2", "1.1.1", false}, {"~1.1.2", "1.1.2", true}, + {"~1.1.2", "1.1.2.5", true}, {"~1.1.2", "1.1.2.3", true}, {"^1.0.0", "1.0.0", true}, {"^1.0.0", "1.0.1", true}, {"^1.0.0", "1.9.1", true}, {"^1.0.0", "2.0.0", false}, {"^0.1.0", "0.1.0", true}, - {"^0.1.0", "0.2.0", false}, + {"^0.1.0", "0.2.0", true}, + {"^0.1.0", "0.3.0", true}, {"^0.1.0", "0.1.4", true}, {"^0.0.1", "0.0.0", false}, {"^0.0.1", "0.0.1", true}, - {"^0.0.1", "0.0.2", false}, - {"^0.0.1", "0.2.0", false}, + {"^0.0.1", "0.0.2", true}, + {"^0.0.1", "0.2.0", true}, + {"^1.2.3", "1.99.99", true}, + {"^1.2.3", "2.00.00", false}, {"~1.0.0", "1.0.0", true}, {"~1.0.0", "1.0.1", true}, {"~1.0.0", "1.9.1", false}, @@ -108,7 +112,7 @@ func TestConstraintCheck(t *testing.T) { {"~0.1.0", "0.1.4", true}, {"~0.0.1", "0.0.0", false}, {"~0.0.1", "0.0.1", true}, - {"~0.0.1", "0.0.2", false}, + {"~0.0.1", "0.0.2", true}, {"~0.0.1", "0.2.0", false}, } @@ -126,7 +130,7 @@ func TestConstraintCheck(t *testing.T) { actual := c.Check(v) expected := tc.check if actual != expected { - t.Fatalf("Version: %s\nConstraint: %s\nExpected: %#v", + t.Fatalf("\nVersion: %s\nConstraint: %s\nExpected: %#v", tc.version, tc.constraint, expected) } } From 4b08706a8612303118f42df98ce84e29c040f691 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 19 Nov 2020 14:34:18 +0100 Subject: [PATCH 4/5] update Readme --- README.md | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f67a27b..39693c4 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ versions, can increment versions, etc. Versions used with go-version must follow [SemVer](http://semver.org/). -## Installation and Usage +# Installation and Usage Package documentation can be found on [GoDoc](http://godoc.org/github.com/6543/go-version). @@ -22,7 +22,7 @@ Installation can be done with a normal `go get`: $ go get github.com/6543/go-version ``` -#### Version Parsing and Comparison +# Version Parsing and Comparison ```go v1, err := version.NewVersion("1.2") @@ -35,7 +35,7 @@ if v1.LessThan(v2) { } ``` -#### Version Constraints +# Version Constraints ```go v1, err := version.NewVersion("1.2") @@ -45,9 +45,38 @@ constraints, err := version.NewConstraint(">= 1.0, < 1.4") if constraints.Check(v1) { fmt.Printf("%s satisfies constraints %s", v1, constraints) } + +### Tilde Range Comparisons (Patch) + +The tilde (`~`) comparison operator is for patch level ranges when a minor +version is specified and major level changes when the minor number is missing. +For example, + +* `~1.2.3` is equivalent to `>= 1.2.3, < 1.3.0` +* `~1` is equivalent to `>= 1, < 2` +* `~2.3` is equivalent to `>= 2.3, < 2.4` +* `~1.2.x` is equivalent to `>= 1.2.0, < 1.3.0` +* `~1.x` is equivalent to `>= 1, < 2` + +### Caret Range Comparisons (Major) + +The caret (`^`) comparison operator is for major level changes once a stable +(1.0.0) release has occurred. Prior to a 1.0.0 release the minor versions acts +as the API stability level. This is useful when comparisons of API versions as a +major change is API breaking. For example, + +* `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0` +* `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0` +* `^2.3` is equivalent to `>= 2.3, < 3` +* `^2.x` is equivalent to `>= 2.0.0, < 3` +* `^0.2.3` is equivalent to `>=0.2.3 <0.3.0` +* `^0.2` is equivalent to `>=0.2.0 <0.3.0` +* `^0.0.3` is equivalent to `>=0.0.3 <0.0.4` +* `^0.0` is equivalent to `>=0.0.0 <0.1.0` +* `^0` is equivalent to `>=0.0.0 <1.0.0` ``` -#### Version Sorting +# Version Sorting ```go versionsRaw := []string{"1.1", "0.7.1", "1.4-beta", "1.4", "2"} @@ -61,7 +90,7 @@ for i, raw := range versionsRaw { sort.Sort(version.Collection(versions)) ``` -## Issues and Contributing +# Issues and Contributing If you find an issue with this library, please report an issue. If you'd like, we welcome any contributions. Fork this library and submit a pull From 02cc0bbe561e3a5f4db250970b5a7ecd3bd1c131 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 19 Nov 2020 14:37:22 +0100 Subject: [PATCH 5/5] Update constraint.go --- constraint.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/constraint.go b/constraint.go index 449c78f..6d7ad2e 100644 --- a/constraint.go +++ b/constraint.go @@ -221,7 +221,7 @@ func constraintTilde(v, c *Version) bool { return false } - if v.segments[1] != c.segments[1] && c.si > 1 { + if c.si > 1 && v.segments[1] != c.segments[1] { return false }