/[gentoo-x86]/eclass/versionator.eclass
Gentoo

Diff of /eclass/versionator.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1.1.1 Revision 1.24
1# Copyright 1999-2004 Gentoo Foundation 1# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.1.1.1 2005/11/30 09:59:28 chriswhite Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.24 2013/11/23 04:35:16 jcallen Exp $
4# 4
5# Original Author: Ciaran McCreesh <ciaranm@gentoo.org> 5# @ECLASS: versionator.eclass
6# 6# @MAINTAINER:
7# Jonathan Callen <jcallen@gentoo.org>
8# base-system@gentoo.org
9# @BLURB: functions which simplify manipulation of ${PV} and similar version strings
10# @DESCRIPTION:
7# This eclass provides functions which simplify manipulating $PV and similar 11# This eclass provides functions which simplify manipulating $PV and similar
8# variables. Most functions default to working with $PV, although other 12# variables. Most functions default to working with $PV, although other
9# values can be used. 13# values can be used.
10# 14# @EXAMPLE:
11# Simple Example 1: $PV is 1.2.3b, we want 1_2.3b: 15# Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
12# MY_PV=$(replace_version_separator 1 '_' ) 16# MY_PV=$(replace_version_separator 1 '_' )
13# 17#
14# Simple Example 2: $PV is 1.4.5, we want 1: 18# Simple Example 2: $PV is 1.4.5, we want 1:
15# MY_MAJORV=$(get_major_version ) 19# MY_MAJORV=$(get_major_version )
16#
17# Full list of user usable functions provided by this eclass (see the functions
18# themselves for documentation):
19# get_all_version_components ver_str
20# get_version_components ver_str
21# get_major_version ver_str
22# get_version_component_range range ver_str
23# get_after_major_version ver_str
24# replace_version_separator index newvalue ver_str
25# replace_all_version_separators newvalue ver_str
26# delete_version_separator index ver_str
27# delete_all_version_separators ver_str
28# 20#
29# Rather than being a number, the index parameter can be a separator character 21# Rather than being a number, the index parameter can be a separator character
30# such as '-', '.' or '_'. In this case, the first separator of this kind is 22# such as '-', '.' or '_'. In this case, the first separator of this kind is
31# selected. 23# selected.
32# 24#
33# There's also: 25# There's also:
34# version_is_at_least want have 26# version_is_at_least want have
35# which may be buggy, so use with caution. 27# which may be buggy, so use with caution.
36 28
29if [[ ${___ECLASS_ONCE_VERSIONATOR} != "recur -_+^+_- spank" ]] ; then
30___ECLASS_ONCE_VERSIONATOR="recur -_+^+_- spank"
37 31
38shopt -s extglob 32inherit eutils
39 33
34# @FUNCTION: get_all_version_components
35# @USAGE: [version]
36# @DESCRIPTION:
40# Split up a version string into its component parts. If no parameter is 37# Split up a version string into its component parts. If no parameter is
41# supplied, defaults to $PV. 38# supplied, defaults to $PV.
42# 0.8.3 -> 0 . 8 . 3 39# 0.8.3 -> 0 . 8 . 3
43# 7c -> 7 c 40# 7c -> 7 c
44# 3.0_p2 -> 3 . 0 _ p2 41# 3.0_p2 -> 3 . 0 _ p2
45# 20040905 -> 20040905 42# 20040905 -> 20040905
46# 3.0c-r1 -> 3 . 0 c - r1 43# 3.0c-r1 -> 3 . 0 c - r1
47get_all_version_components() { 44get_all_version_components() {
45 eshopts_push -s extglob
48 local ver_str=${1:-${PV}} result result_idx=0 46 local ver_str=${1:-${PV}} result
49 result=( ) 47 result=()
50 48
51 # sneaky cache trick cache to avoid having to parse the same thing several 49 # sneaky cache trick cache to avoid having to parse the same thing several
52 # times. 50 # times.
53 if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then 51 if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
54 echo ${VERSIONATOR_CACHE_RESULT} 52 echo ${VERSIONATOR_CACHE_RESULT}
53 eshopts_pop
55 return 54 return
56 fi 55 fi
57 export VERSIONATOR_CACHE_VER_STR="${ver_str}" 56 export VERSIONATOR_CACHE_VER_STR=${ver_str}
58 57
59 while [[ -n "$ver_str" ]] ; do 58 while [[ -n $ver_str ]] ; do
60 case "${ver_str:0:1}" in 59 case "${ver_str::1}" in
61 # number: parse whilst we have a number 60 # number: parse whilst we have a number
62 [[:digit:]]) 61 [[:digit:]])
63 result[$result_idx]="${ver_str%%[^[:digit:]]*}" 62 result+=("${ver_str%%[^[:digit:]]*}")
64 ver_str="${ver_str##+([[:digit:]])}" 63 ver_str=${ver_str##+([[:digit:]])}
65 result_idx=$(($result_idx + 1))
66 ;; 64 ;;
67 65
68 # separator: single character 66 # separator: single character
69 [-_.]) 67 [-_.])
70 result[$result_idx]="${ver_str:0:1}" 68 result+=("${ver_str::1}")
71 ver_str="${ver_str:1}" 69 ver_str=${ver_str:1}
72 result_idx=$(($result_idx + 1))
73 ;; 70 ;;
74 71
75 # letter: grab the letters plus any following numbers 72 # letter: grab the letters plus any following numbers
76 [[:alpha:]]) 73 [[:alpha:]])
77 local not_match="${ver_str##+([[:alpha:]])*([[:digit:]])}" 74 local not_match=${ver_str##+([[:alpha:]])*([[:digit:]])}
75 # Can't say "${ver_str::-${#not_match}}" in Bash 3.2
78 result[$result_idx]=${ver_str:0:$((${#ver_str} - ${#not_match}))} 76 result+=("${ver_str::${#ver_str} - ${#not_match}}")
79 ver_str="${not_match}" 77 ver_str=${not_match}
80 result_idx=$(($result_idx + 1))
81 ;; 78 ;;
82 79
83 # huh? 80 # huh?
84 *) 81 *)
85 result[$result_idx]="${ver_str:0:1}" 82 result+=("${ver_str::1}")
86 ver_str="${ver_str:1}" 83 ver_str=${ver_str:1}
87 result_idx=$(($result_idx + 1))
88 ;; 84 ;;
89 esac 85 esac
90 done 86 done
91 87
92 export VERSIONATOR_CACHE_RESULT="${result[@]}" 88 export VERSIONATOR_CACHE_RESULT=${result[*]}
93 echo ${result[@]} 89 echo ${result[@]}
90 eshopts_pop
94} 91}
95 92
93# @FUNCTION: get_version_components
94# @USAGE: [version]
95# @DESCRIPTION:
96# Get the important version components, excluding '.', '-' and '_'. Defaults to 96# Get the important version components, excluding '.', '-' and '_'. Defaults to
97# $PV if no parameter is supplied. 97# $PV if no parameter is supplied.
98# 0.8.3 -> 0 8 3 98# 0.8.3 -> 0 8 3
99# 7c -> 7 c 99# 7c -> 7 c
100# 3.0_p2 -> 3 0 p2 100# 3.0_p2 -> 3 0 p2
101# 20040905 -> 20040905 101# 20040905 -> 20040905
102# 3.0c-r1 -> 3 0 c r1 102# 3.0c-r1 -> 3 0 c r1
103get_version_components() { 103get_version_components() {
104 local c="$(get_all_version_components "${1:-${PV}}")" 104 local c=$(get_all_version_components "${1:-${PV}}")
105 c=( ${c[@]//[-._]/ } ) 105 echo ${c//[-._]/ }
106 echo ${c[@]}
107} 106}
108 107
108# @FUNCTION: get_major_version
109# @USAGE: [version]
110# @DESCRIPTION:
109# Get the major version of a value. Defaults to $PV if no parameter is supplied. 111# Get the major version of a value. Defaults to $PV if no parameter is supplied.
110# 0.8.3 -> 0 112# 0.8.3 -> 0
111# 7c -> 7 113# 7c -> 7
112# 3.0_p2 -> 3 114# 3.0_p2 -> 3
113# 20040905 -> 20040905 115# 20040905 -> 20040905
114# 3.0c-r1 -> 3 116# 3.0c-r1 -> 3
115get_major_version() { 117get_major_version() {
116 local c
117 c=( $(get_all_version_components "${1:-${PV}}" ) ) 118 local c=($(get_all_version_components "${1:-${PV}}"))
118 echo ${c[0]} 119 echo ${c[0]}
119} 120}
120 121
122# @FUNCTION: get_version_component_range
123# @USAGE: <range> [version]
124# @DESCRIPTION:
121# Get a particular component or range of components from the version. If no 125# Get a particular component or range of components from the version. If no
122# version parameter is supplied, defaults to $PV. 126# version parameter is supplied, defaults to $PV.
123# 1 1.2.3 -> 1 127# 1 1.2.3 -> 1
124# 1-2 1.2.3 -> 1.2 128# 1-2 1.2.3 -> 1.2
125# 2- 1.2.3 -> 2.3 129# 2- 1.2.3 -> 2.3
126get_version_component_range() { 130get_version_component_range() {
131 eshopts_push -s extglob
127 local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0 132 local c v="${2:-${PV}}" range="${1}" range_start range_end
133 local -i i=-1 j=0
128 c=( $(get_all_version_components ${v} ) ) 134 c=($(get_all_version_components "${v}"))
129 range_start="${range%-*}" ; range_start="${range_start:-1}" 135 range_start=${range%-*}; range_start=${range_start:-1}
130 range_end="${range#*-}" ; range_end="${range_end:-${#c[@]}}" 136 range_end=${range#*-} ; range_end=${range_end:-${#c[@]}}
131 137
132 while (( j < ${range_start} )) ; do 138 while ((j < range_start)); do
133 i=$(($i + 1)) 139 i+=1
134 [[ $i -gt ${#c[@]} ]] && return 140 ((i > ${#c[@]})) && eshopts_pop && return
135 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) 141 [[ -n "${c[i]//[-._]}" ]] && j+=1
136 done 142 done
137 143
138 while (( j <= ${range_end} )) ; do 144 while ((j <= range_end)); do
139 echo -n ${c[$i]} 145 echo -n ${c[i]}
140 [[ $i -gt ${#c[@]} ]] && return 146 ((i > ${#c[@]})) && eshopts_pop && return
141 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) 147 [[ -n "${c[i]//[-._]}" ]] && j+=1
142 i=$(($i + 1)) 148 i+=1
143 done 149 done
150 eshopts_pop
144} 151}
145 152
153# @FUNCTION: get_after_major_version
154# @USAGE: [version]
155# @DESCRIPTION:
146# Get everything after the major version and its separator (if present) of a 156# Get everything after the major version and its separator (if present) of a
147# value. Defaults to $PV if no parameter is supplied. 157# value. Defaults to $PV if no parameter is supplied.
148# 0.8.3 -> 8.3 158# 0.8.3 -> 8.3
149# 7c -> c 159# 7c -> c
150# 3.0_p2 -> 0_p2 160# 3.0_p2 -> 0_p2
151# 20040905 -> (empty string) 161# 20040905 -> (empty string)
152# 3.0c-r1 -> 0c-r1 162# 3.0c-r1 -> 0c-r1
153get_after_major_version() { 163get_after_major_version() {
154 echo $(get_version_component_range 2- "${1:-PV}" ) 164 echo $(get_version_component_range 2- "${1:-${PV}}")
155} 165}
156 166
167# @FUNCTION: replace_version_separator
168# @USAGE: <search> <replacement> [subject]
169# @DESCRIPTION:
157# Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not 170# Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
158# supplied). If there are fewer than $1 separators, don't change anything. 171# supplied). If there are fewer than $1 separators, don't change anything.
159# 1 '_' 1.2.3 -> 1_2.3 172# 1 '_' 1.2.3 -> 1_2.3
160# 2 '_' 1.2.3 -> 1.2_3 173# 2 '_' 1.2.3 -> 1.2_3
161# 1 '_' 1b-2.3 -> 1b_2.3 174# 1 '_' 1b-2.3 -> 1b_2.3
162# Rather than being a number, $1 can be a separator character such as '-', '.' 175# Rather than being a number, $1 can be a separator character such as '-', '.'
163# or '_'. In this case, the first separator of this kind is selected. 176# or '_'. In this case, the first separator of this kind is selected.
164replace_version_separator() { 177replace_version_separator() {
178 eshopts_push -s extglob
165 local w i c found=0 v="${3:-${PV}}" 179 local w c v="${3:-${PV}}"
180 declare -i i found=0
166 w=${1:-1} 181 w=${1:-1}
167 c=( $(get_all_version_components ${v} ) ) 182 c=($(get_all_version_components ${v}))
168 if [[ "${w//[[:digit:]]/}" == "${w}" ]] ; then 183 if [[ ${w} != *[[:digit:]]* ]] ; then
169 # it's a character, not an index 184 # it's a character, not an index
170 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do 185 for ((i = 0; i < ${#c[@]}; i++)); do
171 if [[ "${c[${i}]}" == "${w}" ]] ; then 186 if [[ ${c[i]} == ${w} ]]; then
172 c[${i}]="${2}" 187 c[i]=${2}
173 break 188 break
174 fi 189 fi
175 done 190 done
176 else 191 else
177 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do 192 for ((i = 0; i < ${#c[@]}; i++)); do
178 if [[ -n "${c[${i}]//[^-._]}" ]] ; then 193 if [[ -n "${c[i]//[^-._]}" ]]; then
179 found=$(($found + 1)) 194 found+=1
180 if [[ "$found" == "${w}" ]] ; then 195 if ((found == w)); then
181 c[${i}]="${2}" 196 c[i]=${2}
182 break 197 break
183 fi 198 fi
184 fi 199 fi
185 done 200 done
186 fi 201 fi
187 c=${c[@]} 202 c=${c[*]}
188 echo ${c// } 203 echo ${c// }
204 eshopts_pop
189} 205}
190 206
207# @FUNCTION: replace_all_version_separators
208# @USAGE: <replacement> [subject]
209# @DESCRIPTION:
191# Replace all version separators in $2 (defaults to $PV) with $1. 210# Replace all version separators in $2 (defaults to $PV) with $1.
192# '_' 1b.2.3 -> 1b_2_3 211# '_' 1b.2.3 -> 1b_2_3
193replace_all_version_separators() { 212replace_all_version_separators() {
194 local c
195 c=( $(get_all_version_components "${2:-${PV}}" ) ) 213 local c=($(get_all_version_components "${2:-${PV}}"))
196 c="${c[@]//[-._]/$1}" 214 c=${c[@]//[-._]/$1}
197 echo ${c// } 215 echo ${c// }
198} 216}
199 217
218# @FUNCTION: delete_version_separator
219# @USAGE: <search> [subject]
220# @DESCRIPTION:
200# Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If 221# Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If
201# there are fewer than $1 separators, don't change anything. 222# there are fewer than $1 separators, don't change anything.
202# 1 1.2.3 -> 12.3 223# 1 1.2.3 -> 12.3
203# 2 1.2.3 -> 1.23 224# 2 1.2.3 -> 1.23
204# 1 1b-2.3 -> 1b2.3 225# 1 1b-2.3 -> 1b2.3
206# or '_'. In this case, the first separator of this kind is deleted. 227# or '_'. In this case, the first separator of this kind is deleted.
207delete_version_separator() { 228delete_version_separator() {
208 replace_version_separator "${1}" "" "${2}" 229 replace_version_separator "${1}" "" "${2}"
209} 230}
210 231
232# @FUNCTION: delete_all_version_separators
233# @USAGE: [subject]
234# @DESCRIPTION:
211# Delete all version separators in $1 (defaults to $PV). 235# Delete all version separators in $1 (defaults to $PV).
212# 1b.2.3 -> 1b23 236# 1b.2.3 -> 1b23
213delete_all_version_separators() { 237delete_all_version_separators() {
214 replace_all_version_separators "" "${1}" 238 replace_all_version_separators "" "${1}"
215} 239}
216 240
241# @FUNCTION: get_version_component_count
242# @USAGE: [version]
243# @DESCRIPTION:
244# How many version components are there in $1 (defaults to $PV)?
245# 1.0.1 -> 3
246# 3.0c-r1 -> 4
247get_version_component_count() {
248 local a=($(get_version_components "${1:-${PV}}"))
249 echo ${#a[@]}
250}
251
252# @FUNCTION: get_last_version_component_index
253# @USAGE: [version]
254# @DESCRIPTION:
255# What is the index of the last version component in $1 (defaults to $PV)?
256# Equivalent to get_version_component_count - 1.
257# 1.0.1 -> 2
258# 3.0c-r1 -> 3
259get_last_version_component_index() {
260 echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
261}
262
263# @FUNCTION: version_is_at_least
264# @USAGE: <want> [have]
265# @DESCRIPTION:
217# Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses 266# Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
218# only. May not be reliable, be sure to do very careful testing before actually 267# only. May not be reliable, be sure to do very careful testing before actually
219# using this. Prod ciaranm if you find something it can't handle. 268# using this.
220version_is_at_least() { 269version_is_at_least() {
221 local want_s="$1" have_s="${2:-${PVR}}" r 270 local want_s="$1" have_s="${2:-${PVR}}" r
222 version_compare "${want_s}" "${have_s}" 271 version_compare "${want_s}" "${have_s}"
223 r=$? 272 r=$?
224 case $r in 273 case $r in
232 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]" 281 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
233 ;; 282 ;;
234 esac 283 esac
235} 284}
236 285
286# @FUNCTION: version_compare
287# @USAGE: <A> <B>
288# @DESCRIPTION:
237# Takes two parameters (a, b) which are versions. If a is an earlier version 289# Takes two parameters (A, B) which are versions. If A is an earlier version
238# than b, returns 1. If a is identical to b, return 2. If b is later than a, 290# than B, returns 1. If A is identical to B, return 2. If A is later than B,
239# return 3. You probably want version_is_at_least rather than this function. 291# return 3. You probably want version_is_at_least rather than this function.
240# May not be very reliable. Test carefully before using this. 292# May not be very reliable. Test carefully before using this.
241version_compare() { 293version_compare() {
294 eshopts_push -s extglob
242 local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0 295 local ver_a=${1} ver_b=${2} parts_a parts_b
296 local cur_tok_a cur_tok_b num_part_a num_part_b
297 local -i cur_idx_a=0 cur_idx_b=0 prev_idx_a prev_idx_b
243 parts_a=( $(get_all_version_components "${ver_a}" ) ) 298 parts_a=( $(get_all_version_components "${ver_a}" ) )
244 parts_b=( $(get_all_version_components "${ver_b}" ) ) 299 parts_b=( $(get_all_version_components "${ver_b}" ) )
245 300
246 ### compare number parts. 301 ### compare number parts.
247 local inf_loop=0 302 local -i inf_loop=0
248 while true ; do 303 while true; do
249 inf_loop=$(( ${inf_loop} + 1 )) 304 inf_loop+=1
250 [[ ${inf_loop} -gt 20 ]] && \ 305 ((inf_loop > 20)) && \
251 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]" 306 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
252 307
308 # Store the current index to test later
309 prev_idx_a=cur_idx_a
310 prev_idx_b=cur_idx_b
311
253 # grab the current number components 312 # grab the current number components
254 local cur_tok_a=${parts_a[${cur_idx_a}]} 313 cur_tok_a=${parts_a[cur_idx_a]}
255 local cur_tok_b=${parts_b[${cur_idx_b}]} 314 cur_tok_b=${parts_b[cur_idx_b]}
256 315
257 # number? 316 # number?
258 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then 317 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
259 cur_idx_a=$(( ${cur_idx_a} + 1 )) 318 cur_idx_a+=1
260 [[ ${parts_a[${cur_idx_a}]} == "." ]] \ 319 [[ ${parts_a[cur_idx_a]} == . ]] \
261 && cur_idx_a=$(( ${cur_idx_a} + 1 )) 320 && cur_idx_a+=1
262 else 321 else
263 cur_tok_a="" 322 cur_tok_a=
264 fi 323 fi
265 324
266 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then 325 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
267 cur_idx_b=$(( ${cur_idx_b} + 1 )) 326 cur_idx_b+=1
268 [[ ${parts_b[${cur_idx_b}]} == "." ]] \ 327 [[ ${parts_b[cur_idx_b]} == . ]] \
269 && cur_idx_b=$(( ${cur_idx_b} + 1 )) 328 && cur_idx_b+=1
270 else 329 else
271 cur_tok_b="" 330 cur_tok_b=
272 fi 331 fi
273 332
274 # done with number components? 333 # done with number components?
275 [[ -z ${cur_tok_a} ]] && [[ -z ${cur_tok_b} ]] && break 334 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
276 335
336 # if a component is blank, then it is the lesser value
337 [[ -z ${cur_tok_a} ]] && eshopts_pop && return 1
338 [[ -z ${cur_tok_b} ]] && eshopts_pop && return 3
339
340 # According to PMS, if we are *not* in the first number part, and either
341 # token begins with "0", then we use a different algorithm (that
342 # effectively does floating point comparison)
343 if (( prev_idx_a != 0 && prev_idx_b != 0 )) \
344 && [[ ${cur_tok_a} == 0* || ${cur_tok_b} == 0* ]] ; then
345
346 # strip trailing zeros
347 cur_tok_a=${cur_tok_a%%+(0)}
348 cur_tok_b=${cur_tok_b%%+(0)}
349
350 # do a *string* comparison of the resulting values: 2 > 11
351 [[ ${cur_tok_a} < ${cur_tok_b} ]] && eshopts_pop && return 1
352 [[ ${cur_tok_a} > ${cur_tok_b} ]] && eshopts_pop && return 3
353 else
277 # to avoid going into octal mode, strip any leading zeros. otherwise 354 # to avoid going into octal mode, strip any leading zeros. otherwise
278 # bash will throw a hissy fit on versions like 6.3.068. 355 # bash will throw a hissy fit on versions like 6.3.068.
279 cur_tok_a=${cur_tok_a##+(0)} 356 cur_tok_a=${cur_tok_a##+(0)}
280 cur_tok_b=${cur_tok_b##+(0)} 357 cur_tok_b=${cur_tok_b##+(0)}
281 358
282 # if a component is blank, make it zero. 359 # now if a component is blank, it was originally 0 -- make it so
283 [[ -z ${cur_tok_a} ]] && cur_tok_a=0 360 : ${cur_tok_a:=0}
284 [[ -z ${cur_tok_b} ]] && cur_tok_b=0 361 : ${cur_tok_b:=0}
285 362
286 # compare 363 # compare
287 [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && return 1 364 ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
288 [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && return 3 365 ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
366 fi
289 done 367 done
290 368
291 ### number parts equal. compare letter parts. 369 ### number parts equal. compare letter parts.
292 local letter_a= 370 local letter_a=
293 letter_a=${parts_a[${cur_idx_a}]} 371 letter_a=${parts_a[cur_idx_a]}
294 if [[ ${#letter_a} -eq 1 ]] && [[ -z ${letter_a/[a-z]} ]] ; then 372 if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
295 cur_idx_a=$(( ${cur_idx_a} + 1 )) 373 cur_idx_a+=1
296 else 374 else
297 letter_a="@" 375 letter_a=@
298 fi 376 fi
299 377
300 local letter_b= 378 local letter_b=
301 letter_b=${parts_b[${cur_idx_b}]} 379 letter_b=${parts_b[cur_idx_b]}
302 if [[ ${#letter_b} -eq 1 ]] && [[ -z ${letter_b/[a-z]} ]] ; then 380 if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
303 cur_idx_b=$(( ${cur_idx_b} + 1 )) 381 cur_idx_b+=1
304 else 382 else
305 letter_b="@" 383 letter_b=@
306 fi 384 fi
307 385
308 # compare 386 # compare
309 [[ ${letter_a} < ${letter_b} ]] && return 1 387 [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
310 [[ ${letter_a} > ${letter_b} ]] && return 3 388 [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
311 389
312 ### letter parts equal. compare suffixes in order. 390 ### letter parts equal. compare suffixes in order.
313 local suffix rule part r_lt r_gt 391 inf_loop=0
314 for rule in "alpha=1" "beta=1" "pre=1" "rc=1" "p=3" "r=3" ; do 392 while true ; do
315 suffix=${rule%%=*} 393 inf_loop+=1
316 r_lt=${rule##*=} 394 ((inf_loop > 20)) && \
317 [[ ${r_lt} -eq 1 ]] && r_gt=3 || r_gt=1 395 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
396 [[ ${parts_a[cur_idx_a]} == _ ]] && ((cur_idx_a++))
397 [[ ${parts_b[cur_idx_b]} == _ ]] && ((cur_idx_b++))
318 398
399 cur_tok_a=${parts_a[cur_idx_a]}
400 cur_tok_b=${parts_b[cur_idx_b]}
401 num_part_a=0
402 num_part_b=0
403
404 if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
405 cur_idx_a+=1
406 num_part_a=${cur_tok_a##+([a-z])}
407 # I don't like octal
408 num_part_a=${num_part_a##+(0)}
409 : ${num_part_a:=0}
410 cur_tok_a=${cur_tok_a%%+([0-9])}
411 else
412 cur_tok_a=
413 fi
414
415 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
416 cur_idx_b+=1
417 num_part_b=${cur_tok_b##+([a-z])}
418 # I still don't like octal
419 num_part_b=${num_part_b##+(0)}
420 : ${num_part_b:=0}
421 cur_tok_b=${cur_tok_b%%+([0-9])}
422 else
423 cur_tok_b=
424 fi
425
426 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
319 local suffix_a= 427 local suffix
320 for part in ${parts_a[@]} ; do 428 for suffix in alpha beta pre rc "" p; do
321 [[ ${part#${suffix}} != ${part} ]] && \ 429 [[ ${cur_tok_a} == ${suffix} ]] && eshopts_pop && return 1
322 [[ -z ${part##${suffix}*([[:digit:]])} ]] && \ 430 [[ ${cur_tok_b} == ${suffix} ]] && eshopts_pop && return 3
323 suffix_a=${part#${suffix}}0
324 done 431 done
325 432 elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
326 local suffix_b= 433 break
327 for part in ${parts_b[@]} ; do 434 else
328 [[ ${part#${suffix}} != ${part} ]] && \ 435 ((num_part_a < num_part_b)) && eshopts_pop && return 1
329 [[ -z ${part##${suffix}*([[:digit:]])} ]] && \ 436 ((num_part_a > num_part_b)) && eshopts_pop && return 3
330 suffix_b=${part#${suffix}}0 437 fi
331 done
332
333 [[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue
334
335 [[ -z ${suffix_a} ]] && return ${r_gt}
336 [[ -z ${suffix_b} ]] && return ${r_lt}
337
338 # avoid octal problems
339 suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0}
340 suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0}
341
342 [[ ${suffix_a} -lt ${suffix_b} ]] && return 1
343 [[ ${suffix_a} -gt ${suffix_b} ]] && return 3
344 done 438 done
345 439
440 # At this point, the only thing that should be left is the -r# part
441 [[ ${parts_a[cur_idx_a]} == - ]] && ((cur_idx_a++))
442 [[ ${parts_b[cur_idx_b]} == - ]] && ((cur_idx_b++))
443
444 # Sanity check
445 if [[ ${parts_a[cur_idx_a]/r+([0-9])} || ${parts_b[cur_idx_b]/r+([0-9])} ]]; then
446 die "versionator compare bug [revisions, ${ver_a}, ${ver_b}]"
447 fi
448
449 num_part_a=${parts_a[cur_idx_a]#r}
450 num_part_a=${num_part_a##+(0)}
451 : ${num_part_a:=0}
452 num_part_b=${parts_b[cur_idx_b]#r}
453 num_part_b=${num_part_b##+(0)}
454 : ${num_part_b:=0}
455
456 ((num_part_a < num_part_b)) && eshopts_pop && return 1
457 ((num_part_a > num_part_b)) && eshopts_pop && return 3
458
346 ### no differences. 459 ### no differences.
460 eshopts_pop
347 return 2 461 return 2
348} 462}
349 463
464# @FUNCTION: version_sort
465# @USAGE: <version> [more versions...]
466# @DESCRIPTION:
350# Returns its parameters sorted, highest version last. We're using a quadratic 467# Returns its parameters sorted, highest version last. We're using a quadratic
351# algorithm for simplicity, so don't call it with more than a few dozen items. 468# algorithm for simplicity, so don't call it with more than a few dozen items.
352# Uses version_compare, so be careful. 469# Uses version_compare, so be careful.
353version_sort() { 470version_sort() {
471 eshopts_push -s extglob
472 local items=
354 local items= left=0 473 local -i left=0
355 items=( $@ ) 474 items=("$@")
356 while [[ ${left} -lt ${#items[@]} ]] ; do 475 while ((left < ${#items[@]})); do
357 local lowest_idx=${left} 476 local -i lowest_idx=left
358 local idx=$(( ${lowest_idx} + 1 )) 477 local -i idx=lowest_idx+1
359 while [[ ${idx} -lt ${#items[@]} ]] ; do 478 while ((idx < ${#items[@]})); do
360 version_compare "${items[${lowest_idx}]}" "${items[${idx}]}" 479 version_compare "${items[lowest_idx]}" "${items[idx]}"
361 [[ $? -eq 3 ]] && lowest_idx=${idx} 480 [[ $? -eq 3 ]] && lowest_idx=idx
362 idx=$(( ${idx} + 1 )) 481 idx+=1
363 done 482 done
364 local tmp=${items[${lowest_idx}]} 483 local tmp=${items[lowest_idx]}
365 items[${lowest_idx}]=${items[${left}]} 484 items[lowest_idx]=${items[left]}
366 items[${left}]=${tmp} 485 items[left]=${tmp}
367 left=$(( ${left} + 1 )) 486 left+=1
368 done 487 done
369 echo ${items[@]} 488 echo ${items[@]}
489 eshopts_pop
370} 490}
371 491
372__versionator__test_version_compare() { 492# @FUNCTION: version_format_string
373 local lt=1 eq=2 gt=3 p q 493# @USAGE: <format> [version]
374 494# @DESCRIPTION:
375 __versionator__test_version_compare_t() { 495# Reformat complicated version strings. The first argument is the string
376 version_compare "${1}" "${3}" 496# to reformat with while the rest of the args are passed on to the
497# get_version_components function. You should make sure to single quote
498# the first argument since it'll have variables that get delayed expansions.
499# @EXAMPLE:
500# P="cow-hat-1.2.3_p4"
501# MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4')
502# Now MY_P will be: cow-hat_source_1_2-3_p4
503version_format_string() {
377 local r=$? 504 local fstr=$1
378 [[ ${r} -eq ${2} ]] || echo "FAIL: ${@} (got ${r} exp ${2})" 505 shift
379 } 506 set -- $(get_version_components "$@")
380 507 eval echo "${fstr}"
381 echo "
382 0 $lt 1
383 1 $lt 2
384 2 $gt 1
385 2 $eq 2
386 0 $eq 0
387 10 $lt 20
388 68 $eq 068
389 068 $gt 67
390 068 $lt 69
391
392 1.0 $lt 2.0
393 2.0 $eq 2.0
394 2.0 $gt 1.0
395
396 1.0 $gt 0.0
397 0.0 $eq 0.0
398 0.0 $lt 1.0
399
400 0.1 $lt 0.2
401 0.2 $eq 0.2
402 0.3 $gt 0.2
403
404 1.2 $lt 2.1
405 2.1 $gt 1.2
406
407 1.2.3 $lt 1.2.4
408 1.2.4 $gt 1.2.3
409
410 1.2.0 $eq 1.2
411 1.2.1 $gt 1.2
412 1.2 $lt 1.2.1
413
414 1.2b $eq 1.2b
415 1.2b $lt 1.2c
416 1.2b $gt 1.2a
417 1.2b $gt 1.2
418 1.2 $lt 1.2a
419
420 1.3 $gt 1.2a
421 1.3 $lt 1.3a
422
423 1.0_alpha7 $lt 1.0_beta7
424 1.0_beta $lt 1.0_pre
425 1.0_pre5 $lt 1.0_rc2
426 1.0_rc2 $lt 1.0
427
428 1.0_p1 $gt 1.0
429 1.0_p1-r1 $gt 1.0_p1
430
431 1.0_alpha6-r1 $gt 1.0_alpha6
432 1.0_beta6-r1 $gt 1.0_alpha6-r2
433
434 1.0_pre1 $lt 1.0-p1
435
436 1.0p $gt 1.0_p1
437 1.0r $gt 1.0-r1
438 1.6.15 $gt 1.6.10-r2
439 1.6.10-r2 $lt 1.6.15
440
441 " | while read a b c ; do
442 [[ -z "${a}${b}${c}" ]] && continue;
443 __versionator__test_version_compare_t "${a}" "${b}" "${c}"
444 done
445
446
447 for q in "alpha beta pre rc=${lt};${gt}" "p r=${gt};${lt}" ; do
448 for p in ${q%%=*} ; do
449 local c=${q##*=}
450 local alt=${c%%;*} agt=${c##*;}
451 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}"
452 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}1"
453 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}068"
454
455 __versionator__test_version_compare_t "2.0_${p}" $alt "2.0"
456 __versionator__test_version_compare_t "2.0_${p}1" $alt "2.0"
457 __versionator__test_version_compare_t "2.0_${p}068" $alt "2.0"
458
459 __versionator__test_version_compare_t "1.0_${p}" $eq "1.0_${p}"
460 __versionator__test_version_compare_t "0.0_${p}" $lt "0.0_${p}1"
461 __versionator__test_version_compare_t "666_${p}3" $gt "666_${p}"
462
463 __versionator__test_version_compare_t "1_${p}7" $lt "1_${p}8"
464 __versionator__test_version_compare_t "1_${p}7" $eq "1_${p}7"
465 __versionator__test_version_compare_t "1_${p}7" $gt "1_${p}6"
466 __versionator__test_version_compare_t "1_${p}09" $eq "1_${p}9"
467 done
468 done
469
470 for p in "-r" "_p" ; do
471 __versionator__test_version_compare_t "7.2${p}1" $lt "7.2${p}2"
472 __versionator__test_version_compare_t "7.2${p}2" $gt "7.2${p}1"
473 __versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2"
474 __versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3"
475 done
476
477} 508}
478 509
510fi

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.24

  ViewVC Help
Powered by ViewVC 1.1.20