semver_dialects 3.0.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/semver_dialects/boundary.rb +13 -81
- data/lib/semver_dialects/rpm.rb +156 -0
- data/lib/semver_dialects/semantic_version.rb +43 -102
- data/lib/semver_dialects/version.rb +1 -1
- data/lib/semver_dialects.rb +13 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1f8032a4c9d878a2d87171d58bcd60d34d23797e7a190bd4f71983ece8631d4
|
4
|
+
data.tar.gz: f14c6e7ffc8782d8c4c3c8a439cb4cf807470157e6cd86b420cf2b8aa32b9322
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e36e7b55574b064ac14c2a1bbe0e8d2bd9fcf5a22a155b42a1623029f939e54fdac6ac76ef19a6d391de0a0a4fa0801383b72627a583a2520b6cc94de72f55cb
|
7
|
+
data.tar.gz: af976bec11fc7bc8eba97c7afa17a207713ed22d386a8ae9701826cfd53c23ad86fb246fbebb84e77dca9c693422939ca639a5859ecabb3e5825fd9137bd8af9
|
@@ -5,6 +5,8 @@
|
|
5
5
|
# below all versions (negative infinity), or any version.
|
6
6
|
module SemverDialects
|
7
7
|
class Boundary
|
8
|
+
include Comparable
|
9
|
+
|
8
10
|
attr_accessor :semver
|
9
11
|
|
10
12
|
def initialize(semver)
|
@@ -15,46 +17,12 @@ module SemverDialects
|
|
15
17
|
@semver.to_s
|
16
18
|
end
|
17
19
|
|
18
|
-
def
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
other.instance_of?(AboveAll) ? true : @semver < other.semver
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def >(other)
|
27
|
-
if other.instance_of?(BelowAll)
|
28
|
-
true
|
29
|
-
else
|
30
|
-
other.instance_of?(AboveAll) ? false : @semver > other.semver
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def <=(other)
|
35
|
-
self < other || self == other
|
36
|
-
end
|
37
|
-
|
38
|
-
def >=(other)
|
39
|
-
self > other || self == other
|
40
|
-
end
|
20
|
+
def <=>(other)
|
21
|
+
return nil unless other.is_a?(Boundary)
|
22
|
+
return -1 if other.instance_of?(AboveAll)
|
23
|
+
return 1 if other.instance_of?(BelowAll)
|
41
24
|
|
42
|
-
|
43
|
-
# self cannot be BelowAll or AboveAll
|
44
|
-
if other.instance_of?(BelowAll) || other.instance_of?(AboveAll)
|
45
|
-
false
|
46
|
-
else
|
47
|
-
@semver == other.semver
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def !=(other)
|
52
|
-
# self cannot be BelowAll or AboveAll
|
53
|
-
if other.instance_of?(BelowAll) || other.instance_of?(AboveAll)
|
54
|
-
false
|
55
|
-
else
|
56
|
-
@semver != other.semver
|
57
|
-
end
|
25
|
+
semver <=> other.semver
|
58
26
|
end
|
59
27
|
|
60
28
|
def is_initial_version?
|
@@ -76,28 +44,10 @@ module SemverDialects
|
|
76
44
|
false
|
77
45
|
end
|
78
46
|
|
79
|
-
def
|
80
|
-
other.instance_of?(BelowAll)
|
81
|
-
end
|
82
|
-
|
83
|
-
def >(_other)
|
84
|
-
false
|
85
|
-
end
|
86
|
-
|
87
|
-
def <=(other)
|
88
|
-
self < other || self == other
|
89
|
-
end
|
90
|
-
|
91
|
-
def >=(other)
|
92
|
-
self > other || self == other
|
93
|
-
end
|
94
|
-
|
95
|
-
def ==(other)
|
96
|
-
(other.instance_of? BelowAll) ? true : false
|
97
|
-
end
|
47
|
+
def <=>(other)
|
48
|
+
return 0 if other.instance_of?(BelowAll)
|
98
49
|
|
99
|
-
|
100
|
-
!(self == other)
|
50
|
+
-1 if other.is_a?(Boundary)
|
101
51
|
end
|
102
52
|
end
|
103
53
|
|
@@ -115,28 +65,10 @@ module SemverDialects
|
|
115
65
|
false
|
116
66
|
end
|
117
67
|
|
118
|
-
def
|
119
|
-
|
120
|
-
end
|
121
|
-
|
122
|
-
def >(other)
|
123
|
-
other.instance_of?(AboveAll) ? false : true
|
124
|
-
end
|
125
|
-
|
126
|
-
def <=(other)
|
127
|
-
self < other || self == other
|
128
|
-
end
|
129
|
-
|
130
|
-
def >=(other)
|
131
|
-
self > other || self == other
|
132
|
-
end
|
133
|
-
|
134
|
-
def ==(other)
|
135
|
-
(other.instance_of? AboveAll) ? true : false
|
136
|
-
end
|
68
|
+
def <=>(other)
|
69
|
+
return 0 if other.instance_of?(AboveAll)
|
137
70
|
|
138
|
-
|
139
|
-
!(self == other)
|
71
|
+
1 if other.is_a?(Boundary)
|
140
72
|
end
|
141
73
|
end
|
142
74
|
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'strscan'
|
4
|
+
|
5
|
+
module SemverDialects
|
6
|
+
module Rpm
|
7
|
+
module TokenPairComparison
|
8
|
+
# Token can be either alphabets, integers or tilde.
|
9
|
+
# Caret is currently not supported. More details here https://gitlab.com/gitlab-org/gitlab/-/issues/428941#note_1882343489
|
10
|
+
# Precedence: numeric token > string token > no token > tilda (~)
|
11
|
+
def compare_token_pair(a, b)
|
12
|
+
return 1 if a != '~' && b == '~'
|
13
|
+
return -1 if a == '~' && b != '~'
|
14
|
+
|
15
|
+
return 1 if !a.nil? && b.nil?
|
16
|
+
return -1 if a.nil? && !b.nil?
|
17
|
+
|
18
|
+
return 1 if a.is_a?(Integer) && b.is_a?(String)
|
19
|
+
return -1 if a.is_a?(String) && b.is_a?(Integer)
|
20
|
+
|
21
|
+
# Remaining scenario are tokens of the same type ie Integer or String. Use <=> to compare
|
22
|
+
a <=> b
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# This implementation references `go-rpm-version` https://github.com/knqyf263/go-rpm-version
|
27
|
+
# Which is based on the official `rpmvercmp` https://github.com/rpm-software-management/rpm/blob/master/rpmio/rpmvercmp.c implementation
|
28
|
+
# rpm versioning schema can be found here https://github.com/rpm-software-management/rpm/blob/master/docs/manual/dependencies.md#versioning
|
29
|
+
# Details on how the caret and tilde symbols are handled can be found here https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_handling_non_sorting_versions_with_tilde_dot_and_caret
|
30
|
+
class Version < BaseVersion
|
31
|
+
include TokenPairComparison
|
32
|
+
|
33
|
+
attr_reader :tokens, :addition, :epoch
|
34
|
+
|
35
|
+
def initialize(tokens, epoch: nil, release_tag: nil)
|
36
|
+
@tokens = tokens
|
37
|
+
@addition = release_tag
|
38
|
+
@epoch = epoch
|
39
|
+
end
|
40
|
+
|
41
|
+
def <=>(other)
|
42
|
+
# Compare epoch first
|
43
|
+
epoch_cmp = compare_epochs(epoch, other.epoch)
|
44
|
+
return epoch_cmp unless epoch_cmp.zero?
|
45
|
+
|
46
|
+
# Then compare version
|
47
|
+
cmp = compare_tokens(tokens, other.tokens)
|
48
|
+
return cmp unless cmp.zero?
|
49
|
+
|
50
|
+
# And finally compare release tags
|
51
|
+
compare_additions(addition, other.addition)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Note that to_s does not accurately recreate the version string.
|
55
|
+
# More details here https://gitlab.com/gitlab-org/gitlab/-/issues/428941#note_1882343489
|
56
|
+
def to_s
|
57
|
+
main = if !epoch.nil?
|
58
|
+
"#{epoch}:" + tokens.join('.')
|
59
|
+
else
|
60
|
+
tokens.join('.')
|
61
|
+
end
|
62
|
+
main += "-#{addition.tokens.join('.')}" unless addition.nil?
|
63
|
+
|
64
|
+
# Remove . around ~
|
65
|
+
main.gsub(/\.~\./, '~')
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def compare_epochs(a, b)
|
71
|
+
(a || 0) <=> (b || 0)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class ReleaseTag < BaseVersion
|
76
|
+
include TokenPairComparison
|
77
|
+
|
78
|
+
def initialize(tokens)
|
79
|
+
@tokens = tokens
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class VersionParser
|
84
|
+
DASH = /-/
|
85
|
+
ALPHABET = /([a-zA-Z]+)/
|
86
|
+
TILDE = /~/
|
87
|
+
DIGIT = /([0-9]+)/
|
88
|
+
COLON = /:/
|
89
|
+
NON_ALPHANUMERIC_DASH_AND_TILDE = /[^a-zA-Z0-9~]+/
|
90
|
+
|
91
|
+
def self.parse(input)
|
92
|
+
new(input).parse
|
93
|
+
end
|
94
|
+
|
95
|
+
def initialize(input)
|
96
|
+
@scanner = StringScanner.new(input)
|
97
|
+
end
|
98
|
+
|
99
|
+
# parse splits the input string into epoch, version and release tag Eg: <epoch>:<version>-<release_tag>
|
100
|
+
# The version and release tag are split at the first `-` character if present
|
101
|
+
# With the segment before the first `-` being version while the other being release tag
|
102
|
+
# Subsequent `-` are disregarded
|
103
|
+
def parse
|
104
|
+
epoch = nil
|
105
|
+
if s = scanner.scan(/\d+:/)
|
106
|
+
epoch = s[..-2].to_i
|
107
|
+
end
|
108
|
+
|
109
|
+
# parse tokens until we reach the release tag, if any
|
110
|
+
tokens = parse_tokens(false)
|
111
|
+
|
112
|
+
# parse release tag
|
113
|
+
release_tag = nil
|
114
|
+
if scanner.rest?
|
115
|
+
release_tag = ReleaseTag.new(parse_tokens(true))
|
116
|
+
end
|
117
|
+
|
118
|
+
raise IncompleteScanError, scanner.rest if scanner.rest?
|
119
|
+
|
120
|
+
Version.new(tokens, epoch: epoch, release_tag: release_tag)
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
attr_reader :scanner
|
126
|
+
|
127
|
+
def parse_tokens(stop_at_release_tag)
|
128
|
+
tokens = []
|
129
|
+
|
130
|
+
until scanner.eos?
|
131
|
+
case
|
132
|
+
when (s = scanner.scan(DASH))
|
133
|
+
if !stop_at_release_tag
|
134
|
+
return tokens
|
135
|
+
end
|
136
|
+
# If release tag has been encountered, ignore subsequent dashes
|
137
|
+
when (s = scanner.scan(ALPHABET))
|
138
|
+
tokens << s
|
139
|
+
when (s = scanner.scan(TILDE))
|
140
|
+
tokens << s
|
141
|
+
when (s = scanner.scan(DIGIT))
|
142
|
+
tokens << s.to_i
|
143
|
+
when (s = scanner.scan(NON_ALPHANUMERIC_DASH_AND_TILDE))
|
144
|
+
# Non-ascii characters are considered equal
|
145
|
+
# so they are ignored when parsing versions
|
146
|
+
# https://github.com/rpm-software-management/rpm/blob/rpm-4.19.1.1-release/tests/rpmvercmp.at#L143
|
147
|
+
else
|
148
|
+
raise SemverDialects::IncompleteScanError, scanner.rest
|
149
|
+
end
|
150
|
+
end
|
151
|
+
tokens
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
@@ -2,10 +2,17 @@
|
|
2
2
|
|
3
3
|
require_relative '../utils'
|
4
4
|
|
5
|
-
# SemanticVersion represents a Semver 2 version.
|
6
|
-
# Comparison rules are the ones of Semver 2.
|
7
5
|
module SemverDialects
|
6
|
+
# SemanticVersion is a generic version class.
|
7
|
+
# It parses and compares versions of any syntax.
|
8
|
+
# It can't always be accurate because a single comparison logic
|
9
|
+
# can't possibly handle all the supported syntaxes.
|
10
|
+
# Since it's generic, it doesn't validate versions.
|
8
11
|
class SemanticVersion
|
12
|
+
include Comparable
|
13
|
+
|
14
|
+
ANY_NUMBER = 'x'
|
15
|
+
|
9
16
|
attr_reader :version_string, :prefix_segments, :suffix_segments, :segments
|
10
17
|
|
11
18
|
# String to build a regexp that matches a version.
|
@@ -89,25 +96,6 @@ module SemverDialects
|
|
89
96
|
[first_array_prefix.concat(first_array_suffix), second_array_prefix.concat(second_array_suffix)]
|
90
97
|
end
|
91
98
|
|
92
|
-
def get_equalized_prefix_arrays_for(semver_a, semver_b)
|
93
|
-
first_array_prefix = semver_a.prefix_segments.clone
|
94
|
-
second_array_prefix = semver_b.prefix_segments.clone
|
95
|
-
first_array_prefix, second_array_prefix = _get_equalized_arrays_for(first_array_prefix, second_array_prefix)
|
96
|
-
[first_array_prefix, second_array_prefix]
|
97
|
-
end
|
98
|
-
|
99
|
-
def <(other)
|
100
|
-
self_array, other_array = get_equalized_arrays_for(self, other)
|
101
|
-
(0..self_array.size - 1).each do |i|
|
102
|
-
if self_array[i] < other_array[i]
|
103
|
-
return true
|
104
|
-
elsif self_array[i] > other_array[i]
|
105
|
-
return false
|
106
|
-
end
|
107
|
-
end
|
108
|
-
false
|
109
|
-
end
|
110
|
-
|
111
99
|
def is_zero?
|
112
100
|
@prefix_segments.empty? || @prefix_segments.all?(&:is_zero?)
|
113
101
|
end
|
@@ -120,47 +108,18 @@ module SemverDialects
|
|
120
108
|
@suffix_segments.any?(&:is_post_release)
|
121
109
|
end
|
122
110
|
|
123
|
-
def
|
124
|
-
|
125
|
-
(0..self_array.size - 1).each do |i|
|
126
|
-
if self_array[i] > other_array[i]
|
127
|
-
return true
|
128
|
-
elsif self_array[i] < other_array[i]
|
129
|
-
return false
|
130
|
-
end
|
131
|
-
end
|
132
|
-
false
|
133
|
-
end
|
134
|
-
|
135
|
-
def >=(other)
|
136
|
-
self == other || self > other || self == other
|
137
|
-
end
|
138
|
-
|
139
|
-
def <=(other)
|
140
|
-
self == other || self < other
|
141
|
-
end
|
142
|
-
|
143
|
-
def ==(other)
|
144
|
-
segments_a = []
|
145
|
-
segments_b = []
|
146
|
-
|
147
|
-
segments_a += other.segments
|
148
|
-
segments_b += @segments
|
111
|
+
def <=>(other)
|
112
|
+
return nil unless other.is_a?(SemanticVersion)
|
149
113
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
end
|
114
|
+
self_array, other_array = get_equalized_arrays_for(self, other)
|
115
|
+
zipped_arrays = self_array.zip(other_array)
|
116
|
+
zipped_arrays.each do |(a, b)|
|
117
|
+
return 0 if a.wildcard? || b.wildcard?
|
155
118
|
|
156
|
-
|
157
|
-
return
|
119
|
+
cmp = a <=> b
|
120
|
+
return cmp if cmp != 0
|
158
121
|
end
|
159
|
-
|
160
|
-
end
|
161
|
-
|
162
|
-
def !=(other)
|
163
|
-
!(self == other)
|
122
|
+
0
|
164
123
|
end
|
165
124
|
|
166
125
|
def to_normalized_s
|
@@ -185,6 +144,8 @@ module SemverDialects
|
|
185
144
|
end
|
186
145
|
|
187
146
|
class SemanticVersionSegment
|
147
|
+
include Comparable
|
148
|
+
|
188
149
|
attr_accessor :normalized_group_string, :original_group_string, :is_post_release, :is_pre_release
|
189
150
|
|
190
151
|
@@group_suffixes = {
|
@@ -227,52 +188,28 @@ module SemverDialects
|
|
227
188
|
end
|
228
189
|
end
|
229
190
|
|
230
|
-
def
|
231
|
-
|
232
|
-
semver_a.to_i.send(comparator, semver_b.to_i)
|
233
|
-
elsif semver_a.number? && !semver_b.number?
|
234
|
-
if semver_b == 'X'
|
235
|
-
true
|
236
|
-
else
|
237
|
-
ret_anr_bnonr
|
238
|
-
end
|
239
|
-
elsif !semver_a.number? && semver_b.number?
|
240
|
-
if semver_a == 'X'
|
241
|
-
true
|
242
|
-
else
|
243
|
-
ret_anonr_bnr
|
244
|
-
end
|
245
|
-
elsif semver_a == 'X' || semver_b == 'X' # !semantic_version_b.group_string.is_number? && !semantic_version_agrous_string.is_number?
|
246
|
-
true
|
247
|
-
else
|
248
|
-
semver_a.send(comparator, semver_b)
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
def <(other)
|
253
|
-
compare(normalized_group_string, other.normalized_group_string, true, false, :<)
|
254
|
-
end
|
255
|
-
|
256
|
-
def >(other)
|
257
|
-
compare(normalized_group_string, other.normalized_group_string, false, true, :>)
|
258
|
-
end
|
191
|
+
def <=>(other)
|
192
|
+
return nil unless other.is_a?(SemanticVersionSegment)
|
259
193
|
|
260
|
-
|
261
|
-
|
262
|
-
:>)
|
263
|
-
end
|
194
|
+
self_semver = normalized_group_string
|
195
|
+
other_semver = other.normalized_group_string
|
264
196
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
197
|
+
both_are_numbers = self_semver.number? && other_semver.number?
|
198
|
+
at_least_one_is_x = self_semver == 'X' || other_semver == 'X'
|
199
|
+
a_numeric_b_non_numeric = self_semver.number? && !other_semver.number?
|
200
|
+
b_numeric_a_non_numeric = other_semver.number? && !self_semver.number?
|
269
201
|
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
202
|
+
if both_are_numbers
|
203
|
+
self_semver.to_i <=> other_semver.to_i
|
204
|
+
elsif at_least_one_is_x
|
205
|
+
0
|
206
|
+
elsif a_numeric_b_non_numeric
|
207
|
+
-1
|
208
|
+
elsif b_numeric_a_non_numeric
|
209
|
+
1
|
210
|
+
else
|
211
|
+
self_semver <=> other_semver
|
212
|
+
end
|
276
213
|
end
|
277
214
|
|
278
215
|
def to_normalized_s
|
@@ -283,6 +220,10 @@ module SemverDialects
|
|
283
220
|
@version_string
|
284
221
|
end
|
285
222
|
|
223
|
+
def wildcard?
|
224
|
+
normalized_group_string == 'X'
|
225
|
+
end
|
226
|
+
|
286
227
|
def is_number?
|
287
228
|
normalized_group_string.number?
|
288
229
|
end
|
data/lib/semver_dialects.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'semver_dialects/version'
|
4
4
|
require 'semver_dialects/base_version'
|
5
5
|
require 'semver_dialects/maven'
|
6
|
+
require 'semver_dialects/rpm'
|
6
7
|
require 'semver_dialects/semver2'
|
7
8
|
require 'semver_dialects/semantic_version'
|
8
9
|
require 'semver_dialects/boundary'
|
@@ -85,15 +86,23 @@ module SemverDialects
|
|
85
86
|
end
|
86
87
|
|
87
88
|
def self.os_pkg_version_satisfies?(typ, raw_ver, raw_constraint)
|
88
|
-
return unless typ
|
89
|
+
return unless %w[deb rpm].include?(typ)
|
89
90
|
# we only support the less than operator, because that's the only one currently output
|
90
91
|
# by the advisory exporter for operating system packages.
|
91
92
|
raise SemverDialects::InvalidConstraintError, raw_constraint unless raw_constraint[0] == '<'
|
92
93
|
|
93
|
-
|
94
|
-
|
94
|
+
case typ
|
95
|
+
when 'deb'
|
96
|
+
v1 = DebVersion.new(raw_ver)
|
97
|
+
v2 = DebVersion.new(raw_constraint[1..])
|
98
|
+
|
99
|
+
v1 < v2
|
100
|
+
when 'rpm'
|
101
|
+
v1 = Rpm::VersionParser.parse(raw_ver)
|
102
|
+
v2 = Rpm::VersionParser.parse(raw_constraint[1..])
|
95
103
|
|
96
|
-
|
104
|
+
v1 < v2
|
105
|
+
end
|
97
106
|
end
|
98
107
|
|
99
108
|
# Parse a version according to the syntax type.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semver_dialects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Thome
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2024-06-
|
13
|
+
date: 2024-06-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: pastel
|
@@ -187,6 +187,7 @@ files:
|
|
187
187
|
- lib/semver_dialects/interval_set.rb
|
188
188
|
- lib/semver_dialects/interval_set_parser.rb
|
189
189
|
- lib/semver_dialects/maven.rb
|
190
|
+
- lib/semver_dialects/rpm.rb
|
190
191
|
- lib/semver_dialects/semantic_version.rb
|
191
192
|
- lib/semver_dialects/semver2.rb
|
192
193
|
- lib/semver_dialects/version.rb
|