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

Diff of /eclass/versionator.eclass

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

Revision 1.15 Revision 1.20
1# Copyright 1999-2008 Gentoo Foundation 1# Copyright 1999-2008 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.15 2008/06/12 12:48:34 opfer Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.20 2011/11/22 18:42:10 vapier Exp $
4 4
5# @ECLASS: versionator.eclass 5# @ECLASS: versionator.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# Jonathan Callen <abcd@gentoo.org>
7# base-system@gentoo.org 8# base-system@gentoo.org
8# @BLURB: functions which simplify manipulation of ${PV} and similar version strings 9# @BLURB: functions which simplify manipulation of ${PV} and similar version strings
9# @DESCRIPTION: 10# @DESCRIPTION:
10# This eclass provides functions which simplify manipulating $PV and similar 11# This eclass provides functions which simplify manipulating $PV and similar
11# variables. Most functions default to working with $PV, although other 12# variables. Most functions default to working with $PV, although other
23# 24#
24# There's also: 25# There's also:
25# version_is_at_least want have 26# version_is_at_least want have
26# which may be buggy, so use with caution. 27# which may be buggy, so use with caution.
27 28
28# Quick function to toggle the shopts required for some functions on and off 29inherit eutils
29# Used because we can't set extglob in global scope anymore (QA Violation)
30__versionator_shopt_toggle() {
31 VERSIONATOR_RECURSION=${VERSIONATOR_RECURSION:-0}
32 case "$1" in
33 "on")
34 if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
35 VERSIONATOR_OLD_EXTGLOB=$(shopt -p extglob)
36 shopt -s extglob
37 fi
38 VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION + 1 ))
39 ;;
40 "off")
41 VERSIONATOR_RECURSION=$(( $VERSIONATOR_RECURSION - 1 ))
42 if [[ $VERSIONATOR_RECURSION -lt 1 ]] ; then
43 eval $VERSIONATOR_OLD_EXTGLOB
44 fi
45 ;;
46 esac
47 return 0
48}
49 30
50# @FUNCTION: get_all_version_components 31# @FUNCTION: get_all_version_components
51# @USAGE: [version] 32# @USAGE: [version]
52# @DESCRIPTION: 33# @DESCRIPTION:
53# Split up a version string into its component parts. If no parameter is 34# Split up a version string into its component parts. If no parameter is
56# 7c -> 7 c 37# 7c -> 7 c
57# 3.0_p2 -> 3 . 0 _ p2 38# 3.0_p2 -> 3 . 0 _ p2
58# 20040905 -> 20040905 39# 20040905 -> 20040905
59# 3.0c-r1 -> 3 . 0 c - r1 40# 3.0c-r1 -> 3 . 0 c - r1
60get_all_version_components() { 41get_all_version_components() {
61 __versionator_shopt_toggle on 42 eshopts_push -s extglob
62 local ver_str=${1:-${PV}} result result_idx=0 43 local ver_str=${1:-${PV}} result
63 result=( ) 44 result=()
64 45
65 # sneaky cache trick cache to avoid having to parse the same thing several 46 # sneaky cache trick cache to avoid having to parse the same thing several
66 # times. 47 # times.
67 if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then 48 if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
68 echo ${VERSIONATOR_CACHE_RESULT} 49 echo ${VERSIONATOR_CACHE_RESULT}
69 __versionator_shopt_toggle off 50 eshopts_pop
70 return 51 return
71 fi 52 fi
72 export VERSIONATOR_CACHE_VER_STR="${ver_str}" 53 export VERSIONATOR_CACHE_VER_STR=${ver_str}
73 54
74 while [[ -n "$ver_str" ]] ; do 55 while [[ -n $ver_str ]] ; do
75 case "${ver_str:0:1}" in 56 case "${ver_str::1}" in
76 # number: parse whilst we have a number 57 # number: parse whilst we have a number
77 [[:digit:]]) 58 [[:digit:]])
78 result[$result_idx]="${ver_str%%[^[:digit:]]*}" 59 result+=("${ver_str%%[^[:digit:]]*}")
79 ver_str="${ver_str##+([[:digit:]])}" 60 ver_str=${ver_str##+([[:digit:]])}
80 result_idx=$(($result_idx + 1))
81 ;; 61 ;;
82 62
83 # separator: single character 63 # separator: single character
84 [-_.]) 64 [-_.])
85 result[$result_idx]="${ver_str:0:1}" 65 result+=("${ver_str::1}")
86 ver_str="${ver_str:1}" 66 ver_str=${ver_str:1}
87 result_idx=$(($result_idx + 1))
88 ;; 67 ;;
89 68
90 # letter: grab the letters plus any following numbers 69 # letter: grab the letters plus any following numbers
91 [[:alpha:]]) 70 [[:alpha:]])
92 local not_match="${ver_str##+([[:alpha:]])*([[:digit:]])}" 71 local not_match=${ver_str##+([[:alpha:]])*([[:digit:]])}
72 # Can't say "${ver_str::-${#not_match}}" in Bash 3.2
93 result[$result_idx]=${ver_str:0:$((${#ver_str} - ${#not_match}))} 73 result+=("${ver_str::${#ver_str} - ${#not_match}}")
94 ver_str="${not_match}" 74 ver_str=${not_match}
95 result_idx=$(($result_idx + 1))
96 ;; 75 ;;
97 76
98 # huh? 77 # huh?
99 *) 78 *)
100 result[$result_idx]="${ver_str:0:1}" 79 result+=("${ver_str::1}")
101 ver_str="${ver_str:1}" 80 ver_str=${ver_str:1}
102 result_idx=$(($result_idx + 1))
103 ;; 81 ;;
104 esac 82 esac
105 done 83 done
106 84
107 export VERSIONATOR_CACHE_RESULT="${result[@]}" 85 export VERSIONATOR_CACHE_RESULT=${result[*]}
108 echo ${result[@]} 86 echo ${result[@]}
109 __versionator_shopt_toggle off 87 eshopts_pop
110} 88}
111 89
112# @FUNCTION: get_version_components 90# @FUNCTION: get_version_components
113# @USAGE: [version] 91# @USAGE: [version]
114# @DESCRIPTION: 92# @DESCRIPTION:
118# 7c -> 7 c 96# 7c -> 7 c
119# 3.0_p2 -> 3 0 p2 97# 3.0_p2 -> 3 0 p2
120# 20040905 -> 20040905 98# 20040905 -> 20040905
121# 3.0c-r1 -> 3 0 c r1 99# 3.0c-r1 -> 3 0 c r1
122get_version_components() { 100get_version_components() {
123 __versionator_shopt_toggle on
124 local c="$(get_all_version_components "${1:-${PV}}")" 101 local c=$(get_all_version_components "${1:-${PV}}")
125 c=( ${c[@]//[-._]/ } ) 102 echo ${c//[-._]/ }
126 echo ${c[@]}
127 __versionator_shopt_toggle off
128} 103}
129 104
130# @FUNCTION: get_major_version 105# @FUNCTION: get_major_version
131# @USAGE: [version] 106# @USAGE: [version]
132# @DESCRIPTION: 107# @DESCRIPTION:
135# 7c -> 7 110# 7c -> 7
136# 3.0_p2 -> 3 111# 3.0_p2 -> 3
137# 20040905 -> 20040905 112# 20040905 -> 20040905
138# 3.0c-r1 -> 3 113# 3.0c-r1 -> 3
139get_major_version() { 114get_major_version() {
140 __versionator_shopt_toggle on
141 local c
142 c=( $(get_all_version_components "${1:-${PV}}" ) ) 115 local c=($(get_all_version_components "${1:-${PV}}"))
143 echo ${c[0]} 116 echo ${c[0]}
144 __versionator_shopt_toggle off
145} 117}
146 118
147# @FUNCTION: get_version_component_range 119# @FUNCTION: get_version_component_range
148# @USAGE: [version] 120# @USAGE: <range> [version]
149# @DESCRIPTION: 121# @DESCRIPTION:
150# Get a particular component or range of components from the version. If no 122# Get a particular component or range of components from the version. If no
151# version parameter is supplied, defaults to $PV. 123# version parameter is supplied, defaults to $PV.
152# 1 1.2.3 -> 1 124# 1 1.2.3 -> 1
153# 1-2 1.2.3 -> 1.2 125# 1-2 1.2.3 -> 1.2
154# 2- 1.2.3 -> 2.3 126# 2- 1.2.3 -> 2.3
155get_version_component_range() { 127get_version_component_range() {
156 __versionator_shopt_toggle on 128 eshopts_push -s extglob
157 local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0 129 local c v="${2:-${PV}}" range="${1}" range_start range_end
130 local -i i=-1 j=0
158 c=( $(get_all_version_components ${v} ) ) 131 c=($(get_all_version_components "${v}"))
159 range_start="${range%-*}" ; range_start="${range_start:-1}" 132 range_start=${range%-*}; range_start=${range_start:-1}
160 range_end="${range#*-}" ; range_end="${range_end:-${#c[@]}}" 133 range_end=${range#*-} ; range_end=${range_end:-${#c[@]}}
161 134
162 while (( j < ${range_start} )) ; do 135 while ((j < range_start)); do
163 i=$(($i + 1)) 136 i+=1
164 [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return 137 ((i > ${#c[@]})) && eshopts_pop && return
165 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) 138 [[ -n "${c[i]//[-._]}" ]] && j+=1
166 done 139 done
167 140
168 while (( j <= ${range_end} )) ; do 141 while ((j <= range_end)); do
169 echo -n ${c[$i]} 142 echo -n ${c[i]}
170 [[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return 143 ((i > ${#c[@]})) && eshopts_pop && return
171 [[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) 144 [[ -n "${c[i]//[-._]}" ]] && j+=1
172 i=$(($i + 1)) 145 i+=1
173 done 146 done
174 __versionator_shopt_toggle off 147 eshopts_pop
175} 148}
176 149
177# @FUNCTION: get_after_major_version 150# @FUNCTION: get_after_major_version
178# @USAGE: [version] 151# @USAGE: [version]
179# @DESCRIPTION: 152# @DESCRIPTION:
183# 7c -> c 156# 7c -> c
184# 3.0_p2 -> 0_p2 157# 3.0_p2 -> 0_p2
185# 20040905 -> (empty string) 158# 20040905 -> (empty string)
186# 3.0c-r1 -> 0c-r1 159# 3.0c-r1 -> 0c-r1
187get_after_major_version() { 160get_after_major_version() {
188 __versionator_shopt_toggle on
189 echo $(get_version_component_range 2- "${1:-${PV}}" ) 161 echo $(get_version_component_range 2- "${1:-${PV}}")
190 __versionator_shopt_toggle off
191} 162}
192 163
193# @FUNCTION: replace_version_separator 164# @FUNCTION: replace_version_separator
194# @USAGE: <search> <replacement> [subject] 165# @USAGE: <search> <replacement> [subject]
195# @DESCRIPTION: 166# @DESCRIPTION:
199# 2 '_' 1.2.3 -> 1.2_3 170# 2 '_' 1.2.3 -> 1.2_3
200# 1 '_' 1b-2.3 -> 1b_2.3 171# 1 '_' 1b-2.3 -> 1b_2.3
201# Rather than being a number, $1 can be a separator character such as '-', '.' 172# Rather than being a number, $1 can be a separator character such as '-', '.'
202# or '_'. In this case, the first separator of this kind is selected. 173# or '_'. In this case, the first separator of this kind is selected.
203replace_version_separator() { 174replace_version_separator() {
204 __versionator_shopt_toggle on 175 eshopts_push -s extglob
205 local w i c found=0 v="${3:-${PV}}" 176 local w c v="${3:-${PV}}"
177 declare -i i found=0
206 w=${1:-1} 178 w=${1:-1}
207 c=( $(get_all_version_components ${v} ) ) 179 c=($(get_all_version_components ${v}))
208 if [[ "${w//[[:digit:]]/}" == "${w}" ]] ; then 180 if [[ ${w} != *[[:digit:]]* ]] ; then
209 # it's a character, not an index 181 # it's a character, not an index
210 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do 182 for ((i = 0; i < ${#c[@]}; i++)); do
211 if [[ "${c[${i}]}" == "${w}" ]] ; then 183 if [[ ${c[i]} == ${w} ]]; then
212 c[${i}]="${2}" 184 c[i]=${2}
213 break 185 break
214 fi 186 fi
215 done 187 done
216 else 188 else
217 for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do 189 for ((i = 0; i < ${#c[@]}; i++)); do
218 if [[ -n "${c[${i}]//[^-._]}" ]] ; then 190 if [[ -n "${c[i]//[^-._]}" ]]; then
219 found=$(($found + 1)) 191 found+=1
220 if [[ "$found" == "${w}" ]] ; then 192 if ((found == w)); then
221 c[${i}]="${2}" 193 c[i]=${2}
222 break 194 break
223 fi 195 fi
224 fi 196 fi
225 done 197 done
226 fi 198 fi
227 c=${c[@]} 199 c=${c[*]}
228 echo ${c// } 200 echo ${c// }
229 __versionator_shopt_toggle off 201 eshopts_pop
230} 202}
231 203
232# @FUNCTION: replace_all_version_separators 204# @FUNCTION: replace_all_version_separators
233# @USAGE: <replacement> [subject] 205# @USAGE: <replacement> [subject]
234# @DESCRIPTION: 206# @DESCRIPTION:
235# Replace all version separators in $2 (defaults to $PV) with $1. 207# Replace all version separators in $2 (defaults to $PV) with $1.
236# '_' 1b.2.3 -> 1b_2_3 208# '_' 1b.2.3 -> 1b_2_3
237replace_all_version_separators() { 209replace_all_version_separators() {
238 __versionator_shopt_toggle on
239 local c
240 c=( $(get_all_version_components "${2:-${PV}}" ) ) 210 local c=($(get_all_version_components "${2:-${PV}}"))
241 c="${c[@]//[-._]/$1}" 211 c=${c[@]//[-._]/$1}
242 echo ${c// } 212 echo ${c// }
243 __versionator_shopt_toggle off
244} 213}
245 214
246# @FUNCTION: delete_version_separator 215# @FUNCTION: delete_version_separator
247# @USAGE: <search> [subject] 216# @USAGE: <search> [subject]
248# @DESCRIPTION: 217# @DESCRIPTION:
252# 2 1.2.3 -> 1.23 221# 2 1.2.3 -> 1.23
253# 1 1b-2.3 -> 1b2.3 222# 1 1b-2.3 -> 1b2.3
254# Rather than being a number, $1 can be a separator character such as '-', '.' 223# Rather than being a number, $1 can be a separator character such as '-', '.'
255# or '_'. In this case, the first separator of this kind is deleted. 224# or '_'. In this case, the first separator of this kind is deleted.
256delete_version_separator() { 225delete_version_separator() {
257 __versionator_shopt_toggle on
258 replace_version_separator "${1}" "" "${2}" 226 replace_version_separator "${1}" "" "${2}"
259 __versionator_shopt_toggle off
260} 227}
261 228
262# @FUNCTION: delete_all_version_separators 229# @FUNCTION: delete_all_version_separators
263# @USAGE: [subject] 230# @USAGE: [subject]
264# @DESCRIPTION: 231# @DESCRIPTION:
265# Delete all version separators in $1 (defaults to $PV). 232# Delete all version separators in $1 (defaults to $PV).
266# 1b.2.3 -> 1b23 233# 1b.2.3 -> 1b23
267delete_all_version_separators() { 234delete_all_version_separators() {
268 __versionator_shopt_toggle on
269 replace_all_version_separators "" "${1}" 235 replace_all_version_separators "" "${1}"
270 __versionator_shopt_toggle off
271} 236}
272 237
273# @FUNCTION: get_version_component_count 238# @FUNCTION: get_version_component_count
274# @USAGE: [version] 239# @USAGE: [version]
275# @DESCRIPTION: 240# @DESCRIPTION:
276# How many version components are there in $1 (defaults to $PV)? 241# How many version components are there in $1 (defaults to $PV)?
277# 1.0.1 -> 3 242# 1.0.1 -> 3
278# 3.0c-r1 -> 4 243# 3.0c-r1 -> 4
279get_version_component_count() { 244get_version_component_count() {
280 __versionator_shopt_toggle on
281 local a
282 a=( $(get_version_components "${1:-${PV}}" ) ) 245 local a=($(get_version_components "${1:-${PV}}"))
283 echo ${#a[@]} 246 echo ${#a[@]}
284 __versionator_shopt_toggle off
285} 247}
286 248
287# @FUNCTION: get_last_version_component_index 249# @FUNCTION: get_last_version_component_index
288# @USAGE: [version] 250# @USAGE: [version]
289# @DESCRIPTION: 251# @DESCRIPTION:
290# What is the index of the last version component in $1 (defaults to $PV)? 252# What is the index of the last version component in $1 (defaults to $PV)?
291# Equivalent to get_version_component_count - 1. 253# Equivalent to get_version_component_count - 1.
292# 1.0.1 -> 3 254# 1.0.1 -> 2
293# 3.0c-r1 -> 4 255# 3.0c-r1 -> 3
294get_last_version_component_index() { 256get_last_version_component_index() {
295 __versionator_shopt_toggle on
296 echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 )) 257 echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
297 __versionator_shopt_toggle off
298} 258}
299 259
300# @FUNCTION: version_is_at_least 260# @FUNCTION: version_is_at_least
301# @USAGE: <want> [have] 261# @USAGE: <want> [have]
302# @DESCRIPTION: 262# @DESCRIPTION:
303# Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses 263# Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
304# only. May not be reliable, be sure to do very careful testing before actually 264# only. May not be reliable, be sure to do very careful testing before actually
305# using this. 265# using this.
306version_is_at_least() { 266version_is_at_least() {
307 __versionator_shopt_toggle on
308 local want_s="$1" have_s="${2:-${PVR}}" r 267 local want_s="$1" have_s="${2:-${PVR}}" r
309 version_compare "${want_s}" "${have_s}" 268 version_compare "${want_s}" "${have_s}"
310 r=$? 269 r=$?
311 case $r in 270 case $r in
312 1|2) 271 1|2)
313 __versionator_shopt_toggle off
314 return 0 272 return 0
315 ;; 273 ;;
316 3) 274 3)
317 __versionator_shopt_toggle off
318 return 1 275 return 1
319 ;; 276 ;;
320 *) 277 *)
321 __versionator_shopt_toggle off
322 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]" 278 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
323 ;; 279 ;;
324 esac 280 esac
325 __versionator_shopt_toggle off
326} 281}
327 282
328# @FUNCTION: version_compare 283# @FUNCTION: version_compare
329# @USAGE: <A> <B> 284# @USAGE: <A> <B>
330# @DESCRIPTION: 285# @DESCRIPTION:
331# Takes two parameters (A, B) which are versions. If A is an earlier version 286# Takes two parameters (A, B) which are versions. If A is an earlier version
332# than B, returns 1. If A is identical to B, return 2. If A is later than B, 287# than B, returns 1. If A is identical to B, return 2. If A is later than B,
333# return 3. You probably want version_is_at_least rather than this function. 288# return 3. You probably want version_is_at_least rather than this function.
334# May not be very reliable. Test carefully before using this. 289# May not be very reliable. Test carefully before using this.
335version_compare() { 290version_compare() {
336 __versionator_shopt_toggle on 291 eshopts_push -s extglob
337 local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0 292 local ver_a=${1} ver_b=${2} parts_a parts_b
293 local cur_tok_a cur_tok_b num_part_a num_part_b
294 local -i cur_idx_a=0 cur_idx_b=0 prev_idx_a prev_idx_b
338 parts_a=( $(get_all_version_components "${ver_a}" ) ) 295 parts_a=( $(get_all_version_components "${ver_a}" ) )
339 parts_b=( $(get_all_version_components "${ver_b}" ) ) 296 parts_b=( $(get_all_version_components "${ver_b}" ) )
340 297
341 ### compare number parts. 298 ### compare number parts.
342 local inf_loop=0 299 local -i inf_loop=0
343 while true ; do 300 while true; do
344 inf_loop=$(( ${inf_loop} + 1 )) 301 inf_loop+=1
345 [[ ${inf_loop} -gt 20 ]] && \ 302 ((inf_loop > 20)) && \
346 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]" 303 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
347 304
305 # Store the current index to test later
306 prev_idx_a=cur_idx_a
307 prev_idx_b=cur_idx_b
308
348 # grab the current number components 309 # grab the current number components
349 local cur_tok_a=${parts_a[${cur_idx_a}]} 310 cur_tok_a=${parts_a[cur_idx_a]}
350 local cur_tok_b=${parts_b[${cur_idx_b}]} 311 cur_tok_b=${parts_b[cur_idx_b]}
351 312
352 # number? 313 # number?
353 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then 314 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
354 cur_idx_a=$(( ${cur_idx_a} + 1 )) 315 cur_idx_a+=1
355 [[ ${parts_a[${cur_idx_a}]} == "." ]] \ 316 [[ ${parts_a[cur_idx_a]} == . ]] \
356 && cur_idx_a=$(( ${cur_idx_a} + 1 )) 317 && cur_idx_a+=1
357 else 318 else
358 cur_tok_a="" 319 cur_tok_a=
359 fi 320 fi
360 321
361 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then 322 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
362 cur_idx_b=$(( ${cur_idx_b} + 1 )) 323 cur_idx_b+=1
363 [[ ${parts_b[${cur_idx_b}]} == "." ]] \ 324 [[ ${parts_b[cur_idx_b]} == . ]] \
364 && cur_idx_b=$(( ${cur_idx_b} + 1 )) 325 && cur_idx_b+=1
365 else 326 else
366 cur_tok_b="" 327 cur_tok_b=
367 fi 328 fi
368 329
369 # done with number components? 330 # done with number components?
370 [[ -z ${cur_tok_a} ]] && [[ -z ${cur_tok_b} ]] && break 331 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
371 332
333 # if a component is blank, then it is the lesser value
334 [[ -z ${cur_tok_a} ]] && eshopts_pop && return 1
335 [[ -z ${cur_tok_b} ]] && eshopts_pop && return 3
336
337 # According to PMS, if we are *not* in the first number part, and either
338 # token begins with "0", then we use a different algorithm (that
339 # effectively does floating point comparison)
340 if (( prev_idx_a != 0 && prev_idx_b != 0 )) \
341 && [[ ${cur_tok_a} == 0* || ${cur_tok_b} == 0* ]] ; then
342
343 # strip trailing zeros
344 cur_tok_a=${cur_tok_a%%+(0)}
345 cur_tok_b=${cur_tok_b%%+(0)}
346
347 # do a *string* comparison of the resulting values: 2 > 11
348 [[ ${cur_tok_a} < ${cur_tok_b} ]] && eshopts_pop && return 1
349 [[ ${cur_tok_a} > ${cur_tok_b} ]] && eshopts_pop && return 3
350 else
372 # to avoid going into octal mode, strip any leading zeros. otherwise 351 # to avoid going into octal mode, strip any leading zeros. otherwise
373 # bash will throw a hissy fit on versions like 6.3.068. 352 # bash will throw a hissy fit on versions like 6.3.068.
374 cur_tok_a=${cur_tok_a##+(0)} 353 cur_tok_a=${cur_tok_a##+(0)}
375 cur_tok_b=${cur_tok_b##+(0)} 354 cur_tok_b=${cur_tok_b##+(0)}
376 355
377 # if a component is blank, make it zero. 356 # now if a component is blank, it was originally 0 -- make it so
378 [[ -z ${cur_tok_a} ]] && cur_tok_a=0 357 : ${cur_tok_a:=0}
379 [[ -z ${cur_tok_b} ]] && cur_tok_b=0 358 : ${cur_tok_b:=0}
380 359
381 # compare 360 # compare
382 [[ ${cur_tok_a} -lt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 1 361 ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
383 [[ ${cur_tok_a} -gt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 3 362 ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
363 fi
384 done 364 done
385 365
386 ### number parts equal. compare letter parts. 366 ### number parts equal. compare letter parts.
387 local letter_a= 367 local letter_a=
388 letter_a=${parts_a[${cur_idx_a}]} 368 letter_a=${parts_a[cur_idx_a]}
389 if [[ ${#letter_a} -eq 1 ]] && [[ -z ${letter_a/[a-z]} ]] ; then 369 if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
390 cur_idx_a=$(( ${cur_idx_a} + 1 )) 370 cur_idx_a+=1
391 else 371 else
392 letter_a="@" 372 letter_a=@
393 fi 373 fi
394 374
395 local letter_b= 375 local letter_b=
396 letter_b=${parts_b[${cur_idx_b}]} 376 letter_b=${parts_b[cur_idx_b]}
397 if [[ ${#letter_b} -eq 1 ]] && [[ -z ${letter_b/[a-z]} ]] ; then 377 if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
398 cur_idx_b=$(( ${cur_idx_b} + 1 )) 378 cur_idx_b+=1
399 else 379 else
400 letter_b="@" 380 letter_b=@
401 fi 381 fi
402 382
403 # compare 383 # compare
404 [[ ${letter_a} < ${letter_b} ]] && __versionator_shopt_toggle off && return 1 384 [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
405 [[ ${letter_a} > ${letter_b} ]] && __versionator_shopt_toggle off && return 3 385 [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
406 386
407 ### letter parts equal. compare suffixes in order. 387 ### letter parts equal. compare suffixes in order.
408 local suffix rule part r_lt r_gt 388 inf_loop=0
409 for rule in "alpha=1" "beta=1" "pre=1" "rc=1" "p=3" "r=3" ; do 389 while true ; do
410 suffix=${rule%%=*} 390 inf_loop+=1
411 r_lt=${rule##*=} 391 ((inf_loop > 20)) && \
412 [[ ${r_lt} -eq 1 ]] && r_gt=3 || r_gt=1 392 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
393 [[ ${parts_a[cur_idx_a]} == _ ]] && ((cur_idx_a++))
394 [[ ${parts_b[cur_idx_b]} == _ ]] && ((cur_idx_b++))
413 395
396 cur_tok_a=${parts_a[cur_idx_a]}
397 cur_tok_b=${parts_b[cur_idx_b]}
398 num_part_a=0
399 num_part_b=0
400
401 if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
402 cur_idx_a+=1
403 num_part_a=${cur_tok_a##+([a-z])}
404 # I don't like octal
405 num_part_a=${num_part_a##+(0)}
406 : ${num_part_a:=0}
407 cur_tok_a=${cur_tok_a%%+([0-9])}
408 else
409 cur_tok_a=
410 fi
411
412 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
413 cur_idx_b+=1
414 num_part_b=${cur_tok_b##+([a-z])}
415 # I still don't like octal
416 num_part_b=${num_part_b##+(0)}
417 : ${num_part_b:=0}
418 cur_tok_b=${cur_tok_b%%+([0-9])}
419 else
420 cur_tok_b=
421 fi
422
423 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
414 local suffix_a= 424 local suffix
415 for part in ${parts_a[@]} ; do 425 for suffix in alpha beta pre rc "" p; do
416 [[ ${part#${suffix}} != ${part} ]] && \ 426 [[ ${cur_tok_a} == ${suffix} ]] && eshopts_pop && return 1
417 [[ -z ${part##${suffix}*([[:digit:]])} ]] && \ 427 [[ ${cur_tok_b} == ${suffix} ]] && eshopts_pop && return 3
418 suffix_a=${part#${suffix}}0
419 done 428 done
420 429 elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
421 local suffix_b= 430 break
422 for part in ${parts_b[@]} ; do 431 else
423 [[ ${part#${suffix}} != ${part} ]] && \ 432 ((num_part_a < num_part_b)) && eshopts_pop && return 1
424 [[ -z ${part##${suffix}*([[:digit:]])} ]] && \ 433 ((num_part_a > num_part_b)) && eshopts_pop && return 3
425 suffix_b=${part#${suffix}}0 434 fi
426 done 435 done
427 436
428 [[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue 437 # At this point, the only thing that should be left is the -r# part
438 [[ ${parts_a[cur_idx_a]} == - ]] && ((cur_idx_a++))
439 [[ ${parts_b[cur_idx_b]} == - ]] && ((cur_idx_b++))
429 440
430 [[ -z ${suffix_a} ]] && __versionator_shopt_toggle off && return ${r_gt} 441 # Sanity check
431 [[ -z ${suffix_b} ]] && __versionator_shopt_toggle off && return ${r_lt} 442 if [[ ${parts_a[cur_idx_a]/r+([0-9])} || ${parts_b[cur_idx_b]/r+([0-9])} ]]; then
443 die "versionator compare bug [revisions, ${ver_a}, ${ver_b}]"
444 fi
432 445
433 # avoid octal problems 446 num_part_a=${parts_a[cur_idx_a]#r}
434 suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0} 447 num_part_a=${num_part_a##+(0)}
435 suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0} 448 : ${num_part_a:=0}
449 num_part_b=${parts_b[cur_idx_b]#r}
450 num_part_b=${num_part_b##+(0)}
451 : ${num_part_b:=0}
436 452
437 [[ ${suffix_a} -lt ${suffix_b} ]] && __versionator_shopt_toggle off && return 1 453 ((num_part_a < num_part_b)) && eshopts_pop && return 1
438 [[ ${suffix_a} -gt ${suffix_b} ]] && __versionator_shopt_toggle off && return 3 454 ((num_part_a > num_part_b)) && eshopts_pop && return 3
439 done
440 455
441 ### no differences. 456 ### no differences.
442 __versionator_shopt_toggle off 457 eshopts_pop
443 return 2 458 return 2
444} 459}
445 460
446# @FUNCTION: version_sort 461# @FUNCTION: version_sort
447# @USAGE: <version> [more versions...] 462# @USAGE: <version> [more versions...]
448# @DESCRIPTION: 463# @DESCRIPTION:
449# Returns its parameters sorted, highest version last. We're using a quadratic 464# Returns its parameters sorted, highest version last. We're using a quadratic
450# algorithm for simplicity, so don't call it with more than a few dozen items. 465# algorithm for simplicity, so don't call it with more than a few dozen items.
451# Uses version_compare, so be careful. 466# Uses version_compare, so be careful.
452version_sort() { 467version_sort() {
453 __versionator_shopt_toggle on 468 eshopts_push -s extglob
469 local items=
454 local items= left=0 470 local -i left=0
455 items=( $@ ) 471 items=("$@")
456 while [[ ${left} -lt ${#items[@]} ]] ; do 472 while ((left < ${#items[@]})); do
457 local lowest_idx=${left} 473 local -i lowest_idx=left
458 local idx=$(( ${lowest_idx} + 1 )) 474 local -i idx=lowest_idx+1
459 while [[ ${idx} -lt ${#items[@]} ]] ; do 475 while ((idx < ${#items[@]})); do
460 version_compare "${items[${lowest_idx}]}" "${items[${idx}]}" 476 version_compare "${items[lowest_idx]}" "${items[idx]}"
461 [[ $? -eq 3 ]] && lowest_idx=${idx} 477 [[ $? -eq 3 ]] && lowest_idx=idx
462 idx=$(( ${idx} + 1 )) 478 idx+=1
463 done 479 done
464 local tmp=${items[${lowest_idx}]} 480 local tmp=${items[lowest_idx]}
465 items[${lowest_idx}]=${items[${left}]} 481 items[lowest_idx]=${items[left]}
466 items[${left}]=${tmp} 482 items[left]=${tmp}
467 left=$(( ${left} + 1 )) 483 left+=1
468 done 484 done
469 echo ${items[@]} 485 echo ${items[@]}
470 __versionator_shopt_toggle off 486 eshopts_pop
487}
488
489# @FUNCTION: version_format_string
490# @USAGE: <format> [version]
491# @DESCRIPTION:
492# Reformat complicated version strings. The first argument is the string
493# to reformat with while the rest of the args are passed on to the
494# get_version_components function. You should make sure to single quote
495# the first argument since it'll have variables that get delayed expansions.
496# @EXAMPLE:
497# P="cow-hat-1.2.3_p4"
498# MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4')
499# Now MY_P will be: cow-hat_source_1_2-3_p4
500version_format_string() {
501 local fstr=$1
502 shift
503 set -- $(get_version_components "$@")
504 eval echo "${fstr}"
471} 505}
472 506
473__versionator__test_version_compare() { 507__versionator__test_version_compare() {
474 __versionator_shopt_toggle on 508 eshopts_push -s extglob
475 local lt=1 eq=2 gt=3 p q 509 local lt=1 eq=2 gt=3 p q
476 510
477 __versionator__test_version_compare_t() { 511 __versionator__test_version_compare_t() {
478 version_compare "${1}" "${3}" 512 version_compare "${1}" "${3}"
479 local r=$? 513 local r=$?
507 2.1 $gt 1.2 541 2.1 $gt 1.2
508 542
509 1.2.3 $lt 1.2.4 543 1.2.3 $lt 1.2.4
510 1.2.4 $gt 1.2.3 544 1.2.4 $gt 1.2.3
511 545
512 1.2.0 $eq 1.2 546 1.2.0 $gt 1.2
513 1.2.1 $gt 1.2 547 1.2.1 $gt 1.2
514 1.2 $lt 1.2.1 548 1.2 $lt 1.2.1
515 549
516 1.2b $eq 1.2b 550 1.2b $eq 1.2b
517 1.2b $lt 1.2c 551 1.2b $lt 1.2c
531 1.0_p1-r1 $gt 1.0_p1 565 1.0_p1-r1 $gt 1.0_p1
532 566
533 1.0_alpha6-r1 $gt 1.0_alpha6 567 1.0_alpha6-r1 $gt 1.0_alpha6
534 1.0_beta6-r1 $gt 1.0_alpha6-r2 568 1.0_beta6-r1 $gt 1.0_alpha6-r2
535 569
536 1.0_pre1 $lt 1.0-p1 570 1.0_pre1 $lt 1.0_p1
537 571
538 1.0p $gt 1.0_p1 572 1.0p $gt 1.0_p1
539 1.0r $gt 1.0-r1 573 1.0r $gt 1.0-r1
540 1.6.15 $gt 1.6.10-r2 574 1.6.15 $gt 1.6.10-r2
541 1.6.10-r2 $lt 1.6.15 575 1.6.10-r2 $lt 1.6.15
544 [[ -z "${a}${b}${c}" ]] && continue; 578 [[ -z "${a}${b}${c}" ]] && continue;
545 __versionator__test_version_compare_t "${a}" "${b}" "${c}" 579 __versionator__test_version_compare_t "${a}" "${b}" "${c}"
546 done 580 done
547 581
548 582
549 for q in "alpha beta pre rc=${lt};${gt}" "p r=${gt};${lt}" ; do 583 for q in "alpha beta pre rc=${lt};${gt}" "p=${gt};${lt}" ; do
550 for p in ${q%%=*} ; do 584 for p in ${q%%=*} ; do
551 local c=${q##*=} 585 local c=${q##*=}
552 local alt=${c%%;*} agt=${c##*;} 586 local alt=${c%%;*} agt=${c##*;}
553 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}" 587 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}"
554 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}1" 588 __versionator__test_version_compare_t "1.0" $agt "1.0_${p}1"
564 598
565 __versionator__test_version_compare_t "1_${p}7" $lt "1_${p}8" 599 __versionator__test_version_compare_t "1_${p}7" $lt "1_${p}8"
566 __versionator__test_version_compare_t "1_${p}7" $eq "1_${p}7" 600 __versionator__test_version_compare_t "1_${p}7" $eq "1_${p}7"
567 __versionator__test_version_compare_t "1_${p}7" $gt "1_${p}6" 601 __versionator__test_version_compare_t "1_${p}7" $gt "1_${p}6"
568 __versionator__test_version_compare_t "1_${p}09" $eq "1_${p}9" 602 __versionator__test_version_compare_t "1_${p}09" $eq "1_${p}9"
603
604 __versionator__test_version_compare_t "1_${p}7-r0" $eq "1_${p}7"
605 __versionator__test_version_compare_t "1_${p}7-r0" $lt "1_${p}7-r1"
606 __versionator__test_version_compare_t "1_${p}7-r0" $lt "1_${p}7-r01"
607 __versionator__test_version_compare_t "1_${p}7-r01" $eq "1_${p}7-r1"
608 __versionator__test_version_compare_t "1_${p}8-r1" $gt "1_${p}7-r100"
609
610 __versionator__test_version_compare_t "1_${p}_alpha" $lt "1_${p}_beta"
569 done 611 done
570 done 612 done
571 613
572 for p in "-r" "_p" ; do 614 for p in "-r" "_p" ; do
573 __versionator__test_version_compare_t "7.2${p}1" $lt "7.2${p}2" 615 __versionator__test_version_compare_t "7.2${p}1" $lt "7.2${p}2"
574 __versionator__test_version_compare_t "7.2${p}2" $gt "7.2${p}1" 616 __versionator__test_version_compare_t "7.2${p}2" $gt "7.2${p}1"
575 __versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2" 617 __versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2"
576 __versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3" 618 __versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3"
577 done 619 done
578 __versionator_shopt_toggle off 620
621 # The following tests all come from portage's test cases:
622 __versionator__test_version_compare_t "6.0" $gt "5.0"
623 __versionator__test_version_compare_t "5.0" $gt "5"
624 __versionator__test_version_compare_t "1.0-r1" $gt "1.0-r0"
625 __versionator__test_version_compare_t "1.0-r1" $gt "1.0"
626 __versionator__test_version_compare_t "999999999999999999999999999999" $gt "999999999999999999999999999998"
627 __versionator__test_version_compare_t "1.0.0" $gt "1.0"
628 __versionator__test_version_compare_t "1.0.0" $gt "1.0b"
629 __versionator__test_version_compare_t "1b" $gt "1"
630 __versionator__test_version_compare_t "1b_p1" $gt "1_p1"
631 __versionator__test_version_compare_t "1.1b" $gt "1.1"
632 __versionator__test_version_compare_t "12.2.5" $gt "12.2b"
633
634 __versionator__test_version_compare_t "4.0" $lt "5.0"
635 __versionator__test_version_compare_t "5" $lt "5.0"
636 __versionator__test_version_compare_t "1.0_pre2" $lt "1.0_p2"
637 __versionator__test_version_compare_t "1.0_alpha2" $lt "1.0_p2"
638 __versionator__test_version_compare_t "1.0_alpha1" $lt "1.0_beta1"
639 __versionator__test_version_compare_t "1.0_beta3" $lt "1.0_rc3"
640 __versionator__test_version_compare_t "1.001000000000000000001" $lt "1.001000000000000000002"
641 __versionator__test_version_compare_t "1.00100000000" $lt "1.0010000000000000001"
642 __versionator__test_version_compare_t "999999999999999999999999999998" $lt "999999999999999999999999999999"
643 __versionator__test_version_compare_t "1.01" $lt "1.1"
644 __versionator__test_version_compare_t "1.0-r0" $lt "1.0-r1"
645 __versionator__test_version_compare_t "1.0" $lt "1.0-r1"
646 __versionator__test_version_compare_t "1.0" $lt "1.0.0"
647 __versionator__test_version_compare_t "1.0b" $lt "1.0.0"
648 __versionator__test_version_compare_t "1_p1" $lt "1b_p1"
649 __versionator__test_version_compare_t "1" $lt "1b"
650 __versionator__test_version_compare_t "1.1" $lt "1.1b"
651 __versionator__test_version_compare_t "12.2b" $lt "12.2.5"
652
653 __versionator__test_version_compare_t "4.0" $eq "4.0"
654 __versionator__test_version_compare_t "1.0" $eq "1.0"
655 __versionator__test_version_compare_t "1.0-r0" $eq "1.0"
656 __versionator__test_version_compare_t "1.0" $eq "1.0-r0"
657 __versionator__test_version_compare_t "1.0-r0" $eq "1.0-r0"
658 __versionator__test_version_compare_t "1.0-r1" $eq "1.0-r1"
659
660 # The following were just tests for != in portage, we need something a bit
661 # more precise
662 __versionator__test_version_compare_t "1" $lt "2"
663 __versionator__test_version_compare_t "1.0_alpha" $lt "1.0_pre"
664 __versionator__test_version_compare_t "1.0_beta" $gt "1.0_alpha"
665 __versionator__test_version_compare_t "0" $lt "0.0"
666 __versionator__test_version_compare_t "1.0-r0" $lt "1.0-r1"
667 __versionator__test_version_compare_t "1.0-r1" $gt "1.0-r0"
668 __versionator__test_version_compare_t "1.0" $lt "1.0-r1"
669 __versionator__test_version_compare_t "1.0-r1" $gt "1.0"
670 __versionator__test_version_compare_t "1_p1" $lt "1b_p1"
671 __versionator__test_version_compare_t "1b" $gt "1"
672 __versionator__test_version_compare_t "1.1b" $gt "1.1"
673 __versionator__test_version_compare_t "12.2b" $gt "12.2"
674
675 # The following tests all come from paludis's test cases:
676 __versionator__test_version_compare_t "1.0" $gt "1"
677 __versionator__test_version_compare_t "1" $lt "1.0"
678 __versionator__test_version_compare_t "1.0_alpha" $gt "1_alpha"
679 __versionator__test_version_compare_t "1.0_alpha" $gt "1"
680 __versionator__test_version_compare_t "1.0_alpha" $lt "1.0"
681 __versionator__test_version_compare_t "1.2.0.0_alpha7-r4" $gt "1.2_alpha7-r4"
682
683 __versionator__test_version_compare_t "0001" $eq "1"
684 __versionator__test_version_compare_t "01" $eq "001"
685 __versionator__test_version_compare_t "0001.1" $eq "1.1"
686 __versionator__test_version_compare_t "01.01" $eq "1.01"
687 __versionator__test_version_compare_t "1.010" $eq "1.01"
688 __versionator__test_version_compare_t "1.00" $eq "1.0"
689 __versionator__test_version_compare_t "1.0100" $eq "1.010"
690 __versionator__test_version_compare_t "1" $eq "1-r0"
691 __versionator__test_version_compare_t "1-r00" $eq "1-r0"
692
693 eshopts_pop
579} 694}

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.20

  ViewVC Help
Powered by ViewVC 1.1.20