-
Notifications
You must be signed in to change notification settings - Fork 9
/
semver2.sh
executable file
·356 lines (303 loc) · 9.66 KB
/
semver2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/sh
# POSIX SH portable semver 2.0 comparison tool.
# This bash script compares pre-releases alphabetically as well (number < lowerCaseLetter < upperCaseLetter)
#
# returns 1 when A greater than B
# returns 0 when A equals B
# returns -1 when A lower than B
#
# Usage
# chmod +x semver2.sh
# ./semver2.sh 1.0.0-rc.0.a+metadata v1.0.0-rc.0+metadata
# --> 1
#
# This software was built with the help of the following sources:
# https://stackoverflow.com/a/58067270
# https://www.unix.com/man-page/posix/1posix/cut/
# https://stackoverflow.com/questions/51052475/how-to-iterate-over-the-characters-of-a-string-in-a-posix-shell-script
set -eu
debug() {
if [ "$debug" = "debug" ]; then printf "DEBUG: %s$1 \n"; fi
}
# params char
# returns Integer
ord() {
printf '%d' "'$1"
}
isNumber() {
string=$1
char=""
while true; do
substract="${string#?}" # All but the first character of the string
char="${string%"$substract"}" # Remove $rest, and you're left with the first character
string="$substract"
# no more chars to compare then success
if [ -z "$char" ]; then
printf "true"
return 1
fi
# break if some of the chars is not a number
if [ "$(ord "$char")" -lt 48 ] || [ "$(ord "$char")" -gt 57 ]; then
printf "false"
return 0
fi
done
}
# params string {String}, Index {Number}
# returns char
getChar() {
string=$1
index=$2
cursor=-1
char=""
while [ "$cursor" != "$index" ]; do
substract="${string#?}" # All but the first character of the string
char="${string%"$substract"}" # Remove $rest, and you're left with the first character
string="$substract"
cursor=$((cursor + 1))
done
printf "%s$char"
}
outcome() {
result=$1
printf "%s$result\n"
}
compareNumber() {
if [ -z "$1" ] && [ -z "$2" ]; then
printf "%s" "0"
return
fi
[ $(($2 - $1)) -gt 0 ] && printf "%s" "-1"
[ $(($2 - $1)) -lt 0 ] && printf "1"
[ $(($2 - $1)) = 0 ] && printf "0"
}
compareString() {
result=false
index=0
while true
do
a=$(getChar "$1" $index)
b=$(getChar "$2" $index)
if [ -z "$a" ] && [ -z "$b" ]
then
printf "0"
return
fi
ord_a=$(ord "$a")
ord_b=$(ord "$b")
if [ "$(compareNumber "$ord_a" "$ord_b")" != "0" ]; then
printf "%s" "$(compareNumber "$ord_a" "$ord_b")"
return
fi
index=$((index + 1))
done
}
includesString() {
string="$1"
substring="$2"
if [ "${string#*"$substring"}" != "$string" ]
then
printf "1"
return 1 # $substring is in $string
fi
printf "0"
return 0 # $substring is not in $string
}
removeLeadingV() {
printf "%s${1#v}"
}
# https://github.com/Ariel-Rodriguez/sh-semversion-2/pull/2
# Spec #2 https://semver.org/#spec-item-2
# MUST NOT contain leading zeroes
normalizeZero() {
next=$(printf %s "${1#0}")
if [ -z "$next" ]; then
printf %s "$1"
fi
printf %s "$next"
}
semver_compare() {
firstParam=$1 #1.2.4-alpha.beta+METADATA
secondParam=$2 #1.2.4-alpha.beta.2+METADATA
debug=${3:-1}
verbose=${4:-1}
[ "$verbose" = "verbose" ] && set -x
version_a=$(printf %s "$firstParam" | cut -d'+' -f 1)
version_a=$(removeLeadingV "$version_a")
version_b=$(printf %s "$secondParam" | cut -d'+' -f 1)
version_b=$(removeLeadingV "$version_b")
a_major=$(printf %s "$version_a" | cut -d'.' -f 1)
a_minor=$(printf %s "$version_a" | cut -d'.' -f 2)
a_patch=$(printf %s "$version_a" | cut -d'.' -f 3 | cut -d'-' -f 1)
a_pre=""
if [ "$(includesString "$version_a" -)" = 1 ]; then
a_pre=$(printf %s"${version_a#"$a_major.$a_minor.$a_patch-"}")
fi
b_major=$(printf %s "$version_b" | cut -d'.' -f 1)
b_minor=$(printf %s "$version_b" | cut -d'.' -f 2)
b_patch=$(printf %s "$version_b" | cut -d'.' -f 3 | cut -d'-' -f 1)
b_pre=""
if [ "$(includesString "$version_b" -)" = 1 ]; then
b_pre=$(printf %s"${version_b#"$b_major.$b_minor.$b_patch-"}")
fi
a_major=$(normalizeZero "$a_major")
a_minor=$(normalizeZero "$a_minor")
a_patch=$(normalizeZero "$a_patch")
b_major=$(normalizeZero "$b_major")
b_minor=$(normalizeZero "$b_minor")
b_patch=$(normalizeZero "$b_patch")
unit_types="MAJOR MINOR PATCH"
a_normalized="$a_major $a_minor $a_patch"
b_normalized="$b_major $b_minor $b_patch"
debug "Detected: $a_major $a_minor $a_patch identifiers: $a_pre"
debug "Detected: $b_major $b_minor $b_patch identifiers: $b_pre"
#####
#
# Find difference between Major Minor or Patch
#
cursor=1
while [ "$cursor" -lt 4 ]
do
a=$(printf %s "$a_normalized" | cut -d' ' -f $cursor)
b=$(printf %s "$b_normalized" | cut -d' ' -f $cursor)
if [ "$a" != "$b" ]
then
debug "$(printf %s "$unit_types" | cut -d' ' -f $cursor) is different"
outcome "$(compareNumber "$a" "$b")"
return
fi;
debug "$(printf "%s" "$unit_types" | cut -d' ' -f $cursor) are equal"
cursor=$((cursor + 1))
done
#####
#
# Find difference between pre release identifiers
#
if [ -z "$a_pre" ] && [ -z "$b_pre" ]; then
debug "Because both are equals"
outcome "0"
return
fi
# Spec 11.3 a pre-release version has lower precedence than a normal version:
# 1.0.0 < 1.0.0-alpha
if [ -z "$a_pre" ]; then
debug "Because A is the stable release. Pre-release version has lower precedence than a released version"
outcome "1"
return
fi
# 1.0.0-alpha < 1.0.0
if [ -z "$b_pre" ]; then
debug "Because B is the stable release. Pre-release version has lower precedence than a released version"
outcome "-1"
return
fi
isSingleIdentifier() {
substract="${2#?}"
if [ "${1%"$2"}" = "" ]; then
printf "true"
return 1;
fi
return 0
}
cursor=1
while [ $cursor -lt 5 ]
do
a=$(printf %s "$a_pre" | cut -d'.' -f $cursor)
b=$(printf %s "$b_pre" | cut -d'.' -f $cursor)
debug "Comparing identifier $a with $b"
# Exit when there is nothing else to compare.
# Most likely because they are equals
if [ -z "$a" ] && [ -z "$b" ]
then
debug "are equals"
outcome "0"
return
fi;
# Spec #11 https://semver.org/#spec-item-11
# Precedence for two pre-release versions with the same major, minor, and patch version
# MUST be determined by comparing each dot separated identifier from left to right until a difference is found
# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
if [ -n "$a" ] && [ -z "$b" ]; then
# When A is larger than B and preidentifiers are 1+n
# 1.0.0-alpha.beta.1 1.0.0-alpha.beta
# 1.0.0-alpha.beta.1.2 1.0.0-alpha.beta.1
debug "Because A has larger set of pre-identifiers"
outcome "1"
return
fi
# When A is shorter than B and preidentifiers are 1+n
# 1.0.0-alpha.beta 1.0.0-alpha.beta.d
# 1.0.0-alpha.beta 1.0.0-alpha.beta.1.2
if [ -z "$a" ] && [ -n "$b" ]; then
debug "Because B has larger set of pre-identifiers"
outcome "-1"
return
fi
# Spec #11.4.1
# Identifiers consisting of only digits are compared numerically.
if [ "$(isNumber "$a")" = "true" ] || [ "$(isNumber "$b")" = "true" ]; then
# if both identifiers are numbers, then compare and proceed
# 1.0.0-beta.3 1.0.0-beta.2
if [ "$(isNumber "$a")" = "true" ] && [ "$(isNumber "$b")" = "true" ]; then
if [ "$(compareNumber "$a" "$b")" != "0" ]; then
debug "Number is not equal $(compareNumber "$a" "$b")"
outcome "$(compareNumber "$a" "$b")"
return
fi
fi
# Spec 11.4.3
# 1.0.0-alpha.1 1.0.0-alpha.beta.d
# 1.0.0-beta.3 1.0.0-1.2
if [ "$(isNumber "$a")" = "false" ]; then
debug "Because Numeric identifiers always have lower precedence than non-numeric identifiers."
outcome "1"
return
fi
# 1.0.0-alpha.d 1.0.0-alpha.beta.1
# 1.0.0-1.1 1.0.0-beta.1.2
if [ "$(isNumber "$b")" = "false" ]; then
debug "Because Numeric identifiers always have lower precedence than non-numeric identifiers."
outcome "-1"
return
fi
else
# Spec 11.4.2
# Identifiers with letters or hyphens are compared lexically in ASCII sort order.
# 1.0.0-alpha 1.0.0-beta.alpha
if [ "$(compareString "$a" "$b")" != "0" ]; then
debug "cardinal is not equal $(compareString a b)"
outcome "$(compareString "$a" "$b")"
return
fi
fi
# Edge case when there is single identifier exaple: x.y.z-beta
if [ "$cursor" = 1 ]; then
# When both versions are single return equals
# 1.0.0-alpha 1.0.0-alpha
if [ -n "$(isSingleIdentifier "$b_pre" "$b")" ] && [ -n "$(isSingleIdentifier "$a_pre" "$a")" ]; then
debug "Because both have single identifier"
outcome "0"
return
fi
# Return greater when has more identifiers
# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
# When A is larger than B
# 1.0.0-alpha.beta 1.0.0-alpha
if [ -n "$(isSingleIdentifier "$b_pre" "$b")" ] && [ -z "$(isSingleIdentifier "$a_pre" "$a")" ]; then
debug "Because of single identifier, A has more pre-identifiers"
outcome "1"
return
fi
# When A is shorter than B
# 1.0.0-alpha 1.0.0-alpha.beta
if [ -z "$(isSingleIdentifier "$b_pre" "$b")" ] && [ -n "$(isSingleIdentifier "$a_pre" "$a")" ]; then
debug "Because of single identifier, B has more pre-identifiers"
outcome "-1"
return
fi
fi
# Proceed to the next identifier because previous comparition was equal.
cursor=$((cursor + 1))
done
}
printf "%s$(semver_compare "$@")\n"