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

Diff of /eclass/versionator.eclass

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

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

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.18

  ViewVC Help
Powered by ViewVC 1.1.20