| 1 |
vapier |
1.14 |
# Copyright 1999-2008 Gentoo Foundation |
| 2 |
ciaranm |
1.1 |
# Distributed under the terms of the GNU General Public License v2 |
| 3 |
vapier |
1.16 |
# $Header: /var/cvsroot/gentoo-x86/eclass/versionator.eclass,v 1.15 2008/06/12 12:48:34 opfer Exp $ |
| 4 |
vapier |
1.14 |
|
| 5 |
|
|
# @ECLASS: versionator.eclass |
| 6 |
|
|
# @MAINTAINER: |
| 7 |
|
|
# base-system@gentoo.org |
| 8 |
|
|
# @BLURB: functions which simplify manipulation of ${PV} and similar version strings |
| 9 |
|
|
# @DESCRIPTION: |
| 10 |
ciaranm |
1.1 |
# This eclass provides functions which simplify manipulating $PV and similar |
| 11 |
|
|
# variables. Most functions default to working with $PV, although other |
| 12 |
|
|
# values can be used. |
| 13 |
vapier |
1.14 |
# @EXAMPLE: |
| 14 |
ciaranm |
1.1 |
# Simple Example 1: $PV is 1.2.3b, we want 1_2.3b: |
| 15 |
|
|
# MY_PV=$(replace_version_separator 1 '_' ) |
| 16 |
|
|
# |
| 17 |
|
|
# Simple Example 2: $PV is 1.4.5, we want 1: |
| 18 |
|
|
# MY_MAJORV=$(get_major_version ) |
| 19 |
|
|
# |
| 20 |
ciaranm |
1.3 |
# Rather than being a number, the index parameter can be a separator character |
| 21 |
|
|
# such as '-', '.' or '_'. In this case, the first separator of this kind is |
| 22 |
|
|
# selected. |
| 23 |
ciaranm |
1.2 |
# |
| 24 |
|
|
# There's also: |
| 25 |
|
|
# version_is_at_least want have |
| 26 |
vapier |
1.14 |
# which may be buggy, so use with caution. |
| 27 |
ciaranm |
1.1 |
|
| 28 |
antarus |
1.12 |
# Quick function to toggle the shopts required for some functions on and off |
| 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 |
ciaranm |
1.1 |
|
| 50 |
vapier |
1.14 |
# @FUNCTION: get_all_version_components |
| 51 |
|
|
# @USAGE: [version] |
| 52 |
|
|
# @DESCRIPTION: |
| 53 |
ciaranm |
1.1 |
# Split up a version string into its component parts. If no parameter is |
| 54 |
|
|
# supplied, defaults to $PV. |
| 55 |
|
|
# 0.8.3 -> 0 . 8 . 3 |
| 56 |
|
|
# 7c -> 7 c |
| 57 |
|
|
# 3.0_p2 -> 3 . 0 _ p2 |
| 58 |
|
|
# 20040905 -> 20040905 |
| 59 |
|
|
# 3.0c-r1 -> 3 . 0 c - r1 |
| 60 |
|
|
get_all_version_components() { |
| 61 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 62 |
ciaranm |
1.1 |
local ver_str=${1:-${PV}} result result_idx=0 |
| 63 |
|
|
result=( ) |
| 64 |
|
|
|
| 65 |
|
|
# sneaky cache trick cache to avoid having to parse the same thing several |
| 66 |
|
|
# times. |
| 67 |
|
|
if [[ "${VERSIONATOR_CACHE_VER_STR}" == "${ver_str}" ]] ; then |
| 68 |
|
|
echo ${VERSIONATOR_CACHE_RESULT} |
| 69 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 70 |
ciaranm |
1.1 |
return |
| 71 |
|
|
fi |
| 72 |
|
|
export VERSIONATOR_CACHE_VER_STR="${ver_str}" |
| 73 |
|
|
|
| 74 |
|
|
while [[ -n "$ver_str" ]] ; do |
| 75 |
|
|
case "${ver_str:0:1}" in |
| 76 |
|
|
# number: parse whilst we have a number |
| 77 |
|
|
[[:digit:]]) |
| 78 |
|
|
result[$result_idx]="${ver_str%%[^[:digit:]]*}" |
| 79 |
|
|
ver_str="${ver_str##+([[:digit:]])}" |
| 80 |
|
|
result_idx=$(($result_idx + 1)) |
| 81 |
|
|
;; |
| 82 |
|
|
|
| 83 |
|
|
# separator: single character |
| 84 |
|
|
[-_.]) |
| 85 |
|
|
result[$result_idx]="${ver_str:0:1}" |
| 86 |
|
|
ver_str="${ver_str:1}" |
| 87 |
|
|
result_idx=$(($result_idx + 1)) |
| 88 |
|
|
;; |
| 89 |
|
|
|
| 90 |
|
|
# letter: grab the letters plus any following numbers |
| 91 |
|
|
[[:alpha:]]) |
| 92 |
|
|
local not_match="${ver_str##+([[:alpha:]])*([[:digit:]])}" |
| 93 |
|
|
result[$result_idx]=${ver_str:0:$((${#ver_str} - ${#not_match}))} |
| 94 |
|
|
ver_str="${not_match}" |
| 95 |
|
|
result_idx=$(($result_idx + 1)) |
| 96 |
|
|
;; |
| 97 |
|
|
|
| 98 |
|
|
# huh? |
| 99 |
|
|
*) |
| 100 |
|
|
result[$result_idx]="${ver_str:0:1}" |
| 101 |
|
|
ver_str="${ver_str:1}" |
| 102 |
|
|
result_idx=$(($result_idx + 1)) |
| 103 |
|
|
;; |
| 104 |
|
|
esac |
| 105 |
|
|
done |
| 106 |
|
|
|
| 107 |
|
|
export VERSIONATOR_CACHE_RESULT="${result[@]}" |
| 108 |
|
|
echo ${result[@]} |
| 109 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 110 |
ciaranm |
1.1 |
} |
| 111 |
|
|
|
| 112 |
vapier |
1.14 |
# @FUNCTION: get_version_components |
| 113 |
|
|
# @USAGE: [version] |
| 114 |
|
|
# @DESCRIPTION: |
| 115 |
ciaranm |
1.1 |
# Get the important version components, excluding '.', '-' and '_'. Defaults to |
| 116 |
|
|
# $PV if no parameter is supplied. |
| 117 |
|
|
# 0.8.3 -> 0 8 3 |
| 118 |
|
|
# 7c -> 7 c |
| 119 |
|
|
# 3.0_p2 -> 3 0 p2 |
| 120 |
|
|
# 20040905 -> 20040905 |
| 121 |
|
|
# 3.0c-r1 -> 3 0 c r1 |
| 122 |
|
|
get_version_components() { |
| 123 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 124 |
ciaranm |
1.1 |
local c="$(get_all_version_components "${1:-${PV}}")" |
| 125 |
|
|
c=( ${c[@]//[-._]/ } ) |
| 126 |
|
|
echo ${c[@]} |
| 127 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 128 |
ciaranm |
1.1 |
} |
| 129 |
|
|
|
| 130 |
vapier |
1.14 |
# @FUNCTION: get_major_version |
| 131 |
|
|
# @USAGE: [version] |
| 132 |
|
|
# @DESCRIPTION: |
| 133 |
ciaranm |
1.1 |
# Get the major version of a value. Defaults to $PV if no parameter is supplied. |
| 134 |
|
|
# 0.8.3 -> 0 |
| 135 |
|
|
# 7c -> 7 |
| 136 |
|
|
# 3.0_p2 -> 3 |
| 137 |
|
|
# 20040905 -> 20040905 |
| 138 |
|
|
# 3.0c-r1 -> 3 |
| 139 |
|
|
get_major_version() { |
| 140 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 141 |
ciaranm |
1.1 |
local c |
| 142 |
|
|
c=( $(get_all_version_components "${1:-${PV}}" ) ) |
| 143 |
|
|
echo ${c[0]} |
| 144 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 145 |
ciaranm |
1.1 |
} |
| 146 |
|
|
|
| 147 |
vapier |
1.14 |
# @FUNCTION: get_version_component_range |
| 148 |
|
|
# @USAGE: [version] |
| 149 |
|
|
# @DESCRIPTION: |
| 150 |
ciaranm |
1.1 |
# Get a particular component or range of components from the version. If no |
| 151 |
|
|
# version parameter is supplied, defaults to $PV. |
| 152 |
|
|
# 1 1.2.3 -> 1 |
| 153 |
|
|
# 1-2 1.2.3 -> 1.2 |
| 154 |
|
|
# 2- 1.2.3 -> 2.3 |
| 155 |
|
|
get_version_component_range() { |
| 156 |
swegener |
1.13 |
__versionator_shopt_toggle on |
| 157 |
ciaranm |
1.1 |
local c v="${2:-${PV}}" range="${1}" range_start range_end i=-1 j=0 |
| 158 |
|
|
c=( $(get_all_version_components ${v} ) ) |
| 159 |
|
|
range_start="${range%-*}" ; range_start="${range_start:-1}" |
| 160 |
|
|
range_end="${range#*-}" ; range_end="${range_end:-${#c[@]}}" |
| 161 |
|
|
|
| 162 |
|
|
while (( j < ${range_start} )) ; do |
| 163 |
|
|
i=$(($i + 1)) |
| 164 |
antarus |
1.12 |
[[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return |
| 165 |
ciaranm |
1.1 |
[[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) |
| 166 |
|
|
done |
| 167 |
|
|
|
| 168 |
|
|
while (( j <= ${range_end} )) ; do |
| 169 |
|
|
echo -n ${c[$i]} |
| 170 |
antarus |
1.12 |
[[ $i -gt ${#c[@]} ]] && __versionator_shopt_toggle off && return |
| 171 |
ciaranm |
1.1 |
[[ -n "${c[${i}]//[-._]}" ]] && j=$(($j + 1)) |
| 172 |
|
|
i=$(($i + 1)) |
| 173 |
|
|
done |
| 174 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 175 |
ciaranm |
1.1 |
} |
| 176 |
|
|
|
| 177 |
vapier |
1.14 |
# @FUNCTION: get_after_major_version |
| 178 |
|
|
# @USAGE: [version] |
| 179 |
|
|
# @DESCRIPTION: |
| 180 |
ciaranm |
1.1 |
# Get everything after the major version and its separator (if present) of a |
| 181 |
|
|
# value. Defaults to $PV if no parameter is supplied. |
| 182 |
|
|
# 0.8.3 -> 8.3 |
| 183 |
|
|
# 7c -> c |
| 184 |
|
|
# 3.0_p2 -> 0_p2 |
| 185 |
|
|
# 20040905 -> (empty string) |
| 186 |
|
|
# 3.0c-r1 -> 0c-r1 |
| 187 |
|
|
get_after_major_version() { |
| 188 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 189 |
spb |
1.11 |
echo $(get_version_component_range 2- "${1:-${PV}}" ) |
| 190 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 191 |
ciaranm |
1.1 |
} |
| 192 |
|
|
|
| 193 |
opfer |
1.15 |
# @FUNCTION: replace_version_separator |
| 194 |
vapier |
1.14 |
# @USAGE: <search> <replacement> [subject] |
| 195 |
|
|
# @DESCRIPTION: |
| 196 |
ciaranm |
1.1 |
# Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not |
| 197 |
|
|
# supplied). If there are fewer than $1 separators, don't change anything. |
| 198 |
|
|
# 1 '_' 1.2.3 -> 1_2.3 |
| 199 |
|
|
# 2 '_' 1.2.3 -> 1.2_3 |
| 200 |
|
|
# 1 '_' 1b-2.3 -> 1b_2.3 |
| 201 |
ciaranm |
1.3 |
# 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. |
| 203 |
ciaranm |
1.1 |
replace_version_separator() { |
| 204 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 205 |
ciaranm |
1.3 |
local w i c found=0 v="${3:-${PV}}" |
| 206 |
|
|
w=${1:-1} |
| 207 |
ciaranm |
1.1 |
c=( $(get_all_version_components ${v} ) ) |
| 208 |
ciaranm |
1.3 |
if [[ "${w//[[:digit:]]/}" == "${w}" ]] ; then |
| 209 |
|
|
# it's a character, not an index |
| 210 |
|
|
for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do |
| 211 |
|
|
if [[ "${c[${i}]}" == "${w}" ]] ; then |
| 212 |
ciaranm |
1.1 |
c[${i}]="${2}" |
| 213 |
|
|
break |
| 214 |
|
|
fi |
| 215 |
ciaranm |
1.3 |
done |
| 216 |
|
|
else |
| 217 |
|
|
for (( i = 0 ; i < ${#c[@]} ; i = $i + 1 )) ; do |
| 218 |
|
|
if [[ -n "${c[${i}]//[^-._]}" ]] ; then |
| 219 |
|
|
found=$(($found + 1)) |
| 220 |
|
|
if [[ "$found" == "${w}" ]] ; then |
| 221 |
|
|
c[${i}]="${2}" |
| 222 |
|
|
break |
| 223 |
|
|
fi |
| 224 |
|
|
fi |
| 225 |
|
|
done |
| 226 |
|
|
fi |
| 227 |
ciaranm |
1.1 |
c=${c[@]} |
| 228 |
|
|
echo ${c// } |
| 229 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 230 |
ciaranm |
1.1 |
} |
| 231 |
|
|
|
| 232 |
vapier |
1.14 |
# @FUNCTION: replace_all_version_separators |
| 233 |
|
|
# @USAGE: <replacement> [subject] |
| 234 |
|
|
# @DESCRIPTION: |
| 235 |
ciaranm |
1.1 |
# Replace all version separators in $2 (defaults to $PV) with $1. |
| 236 |
|
|
# '_' 1b.2.3 -> 1b_2_3 |
| 237 |
|
|
replace_all_version_separators() { |
| 238 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 239 |
ciaranm |
1.1 |
local c |
| 240 |
|
|
c=( $(get_all_version_components "${2:-${PV}}" ) ) |
| 241 |
|
|
c="${c[@]//[-._]/$1}" |
| 242 |
|
|
echo ${c// } |
| 243 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 244 |
ciaranm |
1.1 |
} |
| 245 |
|
|
|
| 246 |
vapier |
1.14 |
# @FUNCTION: delete_version_separator |
| 247 |
|
|
# @USAGE: <search> [subject] |
| 248 |
|
|
# @DESCRIPTION: |
| 249 |
ciaranm |
1.4 |
# Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If |
| 250 |
ciaranm |
1.3 |
# there are fewer than $1 separators, don't change anything. |
| 251 |
|
|
# 1 1.2.3 -> 12.3 |
| 252 |
|
|
# 2 1.2.3 -> 1.23 |
| 253 |
|
|
# 1 1b-2.3 -> 1b2.3 |
| 254 |
|
|
# 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. |
| 256 |
|
|
delete_version_separator() { |
| 257 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 258 |
ciaranm |
1.3 |
replace_version_separator "${1}" "" "${2}" |
| 259 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 260 |
ciaranm |
1.3 |
} |
| 261 |
|
|
|
| 262 |
vapier |
1.14 |
# @FUNCTION: delete_all_version_separators |
| 263 |
|
|
# @USAGE: [subject] |
| 264 |
|
|
# @DESCRIPTION: |
| 265 |
ciaranm |
1.3 |
# Delete all version separators in $1 (defaults to $PV). |
| 266 |
ciaranm |
1.5 |
# 1b.2.3 -> 1b23 |
| 267 |
ciaranm |
1.3 |
delete_all_version_separators() { |
| 268 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 269 |
ciaranm |
1.5 |
replace_all_version_separators "" "${1}" |
| 270 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 271 |
ciaranm |
1.3 |
} |
| 272 |
|
|
|
| 273 |
vapier |
1.14 |
# @FUNCTION: get_version_component_count |
| 274 |
|
|
# @USAGE: [version] |
| 275 |
|
|
# @DESCRIPTION: |
| 276 |
kugelfang |
1.10 |
# How many version components are there in $1 (defaults to $PV)? |
| 277 |
|
|
# 1.0.1 -> 3 |
| 278 |
|
|
# 3.0c-r1 -> 4 |
| 279 |
|
|
get_version_component_count() { |
| 280 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 281 |
kugelfang |
1.10 |
local a |
| 282 |
|
|
a=( $(get_version_components "${1:-${PV}}" ) ) |
| 283 |
|
|
echo ${#a[@]} |
| 284 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 285 |
kugelfang |
1.10 |
} |
| 286 |
|
|
|
| 287 |
vapier |
1.14 |
# @FUNCTION: get_last_version_component_index |
| 288 |
|
|
# @USAGE: [version] |
| 289 |
|
|
# @DESCRIPTION: |
| 290 |
kugelfang |
1.10 |
# What is the index of the last version component in $1 (defaults to $PV)? |
| 291 |
|
|
# Equivalent to get_version_component_count - 1. |
| 292 |
|
|
# 1.0.1 -> 3 |
| 293 |
|
|
# 3.0c-r1 -> 4 |
| 294 |
|
|
get_last_version_component_index() { |
| 295 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 296 |
kugelfang |
1.10 |
echo $(( $(get_version_component_count "${1:-${PV}}" ) - 1 )) |
| 297 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 298 |
kugelfang |
1.10 |
} |
| 299 |
|
|
|
| 300 |
vapier |
1.14 |
# @FUNCTION: version_is_at_least |
| 301 |
|
|
# @USAGE: <want> [have] |
| 302 |
|
|
# @DESCRIPTION: |
| 303 |
ciaranm |
1.2 |
# Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses |
| 304 |
ciaranm |
1.7 |
# only. May not be reliable, be sure to do very careful testing before actually |
| 305 |
vapier |
1.14 |
# using this. |
| 306 |
ciaranm |
1.2 |
version_is_at_least() { |
| 307 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 308 |
ciaranm |
1.7 |
local want_s="$1" have_s="${2:-${PVR}}" r |
| 309 |
|
|
version_compare "${want_s}" "${have_s}" |
| 310 |
|
|
r=$? |
| 311 |
|
|
case $r in |
| 312 |
|
|
1|2) |
| 313 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 314 |
ciaranm |
1.7 |
return 0 |
| 315 |
|
|
;; |
| 316 |
|
|
3) |
| 317 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 318 |
ciaranm |
1.7 |
return 1 |
| 319 |
|
|
;; |
| 320 |
|
|
*) |
| 321 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 322 |
ciaranm |
1.7 |
die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]" |
| 323 |
|
|
;; |
| 324 |
|
|
esac |
| 325 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 326 |
ciaranm |
1.7 |
} |
| 327 |
|
|
|
| 328 |
vapier |
1.14 |
# @FUNCTION: version_compare |
| 329 |
|
|
# @USAGE: <A> <B> |
| 330 |
|
|
# @DESCRIPTION: |
| 331 |
|
|
# 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, |
| 333 |
ciaranm |
1.7 |
# return 3. You probably want version_is_at_least rather than this function. |
| 334 |
|
|
# May not be very reliable. Test carefully before using this. |
| 335 |
|
|
version_compare() { |
| 336 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 337 |
ciaranm |
1.7 |
local ver_a=${1} ver_b=${2} parts_a parts_b cur_idx_a=0 cur_idx_b=0 |
| 338 |
|
|
parts_a=( $(get_all_version_components "${ver_a}" ) ) |
| 339 |
|
|
parts_b=( $(get_all_version_components "${ver_b}" ) ) |
| 340 |
|
|
|
| 341 |
|
|
### compare number parts. |
| 342 |
|
|
local inf_loop=0 |
| 343 |
|
|
while true ; do |
| 344 |
|
|
inf_loop=$(( ${inf_loop} + 1 )) |
| 345 |
|
|
[[ ${inf_loop} -gt 20 ]] && \ |
| 346 |
|
|
die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]" |
| 347 |
|
|
|
| 348 |
|
|
# grab the current number components |
| 349 |
|
|
local cur_tok_a=${parts_a[${cur_idx_a}]} |
| 350 |
|
|
local cur_tok_b=${parts_b[${cur_idx_b}]} |
| 351 |
|
|
|
| 352 |
|
|
# number? |
| 353 |
|
|
if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then |
| 354 |
|
|
cur_idx_a=$(( ${cur_idx_a} + 1 )) |
| 355 |
|
|
[[ ${parts_a[${cur_idx_a}]} == "." ]] \ |
| 356 |
|
|
&& cur_idx_a=$(( ${cur_idx_a} + 1 )) |
| 357 |
|
|
else |
| 358 |
|
|
cur_tok_a="" |
| 359 |
|
|
fi |
| 360 |
|
|
|
| 361 |
|
|
if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then |
| 362 |
|
|
cur_idx_b=$(( ${cur_idx_b} + 1 )) |
| 363 |
|
|
[[ ${parts_b[${cur_idx_b}]} == "." ]] \ |
| 364 |
|
|
&& cur_idx_b=$(( ${cur_idx_b} + 1 )) |
| 365 |
|
|
else |
| 366 |
|
|
cur_tok_b="" |
| 367 |
|
|
fi |
| 368 |
|
|
|
| 369 |
|
|
# done with number components? |
| 370 |
|
|
[[ -z ${cur_tok_a} ]] && [[ -z ${cur_tok_b} ]] && break |
| 371 |
|
|
|
| 372 |
|
|
# to avoid going into octal mode, strip any leading zeros. otherwise |
| 373 |
|
|
# bash will throw a hissy fit on versions like 6.3.068. |
| 374 |
|
|
cur_tok_a=${cur_tok_a##+(0)} |
| 375 |
|
|
cur_tok_b=${cur_tok_b##+(0)} |
| 376 |
|
|
|
| 377 |
|
|
# if a component is blank, make it zero. |
| 378 |
|
|
[[ -z ${cur_tok_a} ]] && cur_tok_a=0 |
| 379 |
|
|
[[ -z ${cur_tok_b} ]] && cur_tok_b=0 |
| 380 |
|
|
|
| 381 |
|
|
# compare |
| 382 |
antarus |
1.12 |
[[ ${cur_tok_a} -lt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 1 |
| 383 |
|
|
[[ ${cur_tok_a} -gt ${cur_tok_b} ]] && __versionator_shopt_toggle off && return 3 |
| 384 |
ciaranm |
1.2 |
done |
| 385 |
|
|
|
| 386 |
ciaranm |
1.7 |
### number parts equal. compare letter parts. |
| 387 |
|
|
local letter_a= |
| 388 |
|
|
letter_a=${parts_a[${cur_idx_a}]} |
| 389 |
|
|
if [[ ${#letter_a} -eq 1 ]] && [[ -z ${letter_a/[a-z]} ]] ; then |
| 390 |
|
|
cur_idx_a=$(( ${cur_idx_a} + 1 )) |
| 391 |
|
|
else |
| 392 |
|
|
letter_a="@" |
| 393 |
|
|
fi |
| 394 |
|
|
|
| 395 |
|
|
local letter_b= |
| 396 |
|
|
letter_b=${parts_b[${cur_idx_b}]} |
| 397 |
|
|
if [[ ${#letter_b} -eq 1 ]] && [[ -z ${letter_b/[a-z]} ]] ; then |
| 398 |
|
|
cur_idx_b=$(( ${cur_idx_b} + 1 )) |
| 399 |
|
|
else |
| 400 |
|
|
letter_b="@" |
| 401 |
|
|
fi |
| 402 |
|
|
|
| 403 |
|
|
# compare |
| 404 |
antarus |
1.12 |
[[ ${letter_a} < ${letter_b} ]] && __versionator_shopt_toggle off && return 1 |
| 405 |
|
|
[[ ${letter_a} > ${letter_b} ]] && __versionator_shopt_toggle off && return 3 |
| 406 |
ciaranm |
1.7 |
|
| 407 |
|
|
### letter parts equal. compare suffixes in order. |
| 408 |
|
|
local suffix rule part r_lt r_gt |
| 409 |
|
|
for rule in "alpha=1" "beta=1" "pre=1" "rc=1" "p=3" "r=3" ; do |
| 410 |
|
|
suffix=${rule%%=*} |
| 411 |
|
|
r_lt=${rule##*=} |
| 412 |
|
|
[[ ${r_lt} -eq 1 ]] && r_gt=3 || r_gt=1 |
| 413 |
|
|
|
| 414 |
|
|
local suffix_a= |
| 415 |
|
|
for part in ${parts_a[@]} ; do |
| 416 |
|
|
[[ ${part#${suffix}} != ${part} ]] && \ |
| 417 |
|
|
[[ -z ${part##${suffix}*([[:digit:]])} ]] && \ |
| 418 |
|
|
suffix_a=${part#${suffix}}0 |
| 419 |
ciaranm |
1.2 |
done |
| 420 |
ciaranm |
1.7 |
|
| 421 |
|
|
local suffix_b= |
| 422 |
|
|
for part in ${parts_b[@]} ; do |
| 423 |
|
|
[[ ${part#${suffix}} != ${part} ]] && \ |
| 424 |
|
|
[[ -z ${part##${suffix}*([[:digit:]])} ]] && \ |
| 425 |
|
|
suffix_b=${part#${suffix}}0 |
| 426 |
ciaranm |
1.2 |
done |
| 427 |
|
|
|
| 428 |
ciaranm |
1.7 |
[[ -z ${suffix_a} ]] && [[ -z ${suffix_b} ]] && continue |
| 429 |
ciaranm |
1.2 |
|
| 430 |
antarus |
1.12 |
[[ -z ${suffix_a} ]] && __versionator_shopt_toggle off && return ${r_gt} |
| 431 |
|
|
[[ -z ${suffix_b} ]] && __versionator_shopt_toggle off && return ${r_lt} |
| 432 |
ciaranm |
1.2 |
|
| 433 |
ciaranm |
1.7 |
# avoid octal problems |
| 434 |
|
|
suffix_a=${suffix_a##+(0)} ; suffix_a=${suffix_a:-0} |
| 435 |
|
|
suffix_b=${suffix_b##+(0)} ; suffix_b=${suffix_b:-0} |
| 436 |
ciaranm |
1.2 |
|
| 437 |
antarus |
1.12 |
[[ ${suffix_a} -lt ${suffix_b} ]] && __versionator_shopt_toggle off && return 1 |
| 438 |
|
|
[[ ${suffix_a} -gt ${suffix_b} ]] && __versionator_shopt_toggle off && return 3 |
| 439 |
ciaranm |
1.7 |
done |
| 440 |
|
|
|
| 441 |
|
|
### no differences. |
| 442 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 443 |
ciaranm |
1.7 |
return 2 |
| 444 |
|
|
} |
| 445 |
|
|
|
| 446 |
vapier |
1.14 |
# @FUNCTION: version_sort |
| 447 |
|
|
# @USAGE: <version> [more versions...] |
| 448 |
|
|
# @DESCRIPTION: |
| 449 |
ciaranm |
1.7 |
# 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. |
| 451 |
|
|
# Uses version_compare, so be careful. |
| 452 |
|
|
version_sort() { |
| 453 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 454 |
ciaranm |
1.7 |
local items= left=0 |
| 455 |
|
|
items=( $@ ) |
| 456 |
|
|
while [[ ${left} -lt ${#items[@]} ]] ; do |
| 457 |
|
|
local lowest_idx=${left} |
| 458 |
|
|
local idx=$(( ${lowest_idx} + 1 )) |
| 459 |
|
|
while [[ ${idx} -lt ${#items[@]} ]] ; do |
| 460 |
|
|
version_compare "${items[${lowest_idx}]}" "${items[${idx}]}" |
| 461 |
|
|
[[ $? -eq 3 ]] && lowest_idx=${idx} |
| 462 |
|
|
idx=$(( ${idx} + 1 )) |
| 463 |
|
|
done |
| 464 |
|
|
local tmp=${items[${lowest_idx}]} |
| 465 |
|
|
items[${lowest_idx}]=${items[${left}]} |
| 466 |
|
|
items[${left}]=${tmp} |
| 467 |
|
|
left=$(( ${left} + 1 )) |
| 468 |
|
|
done |
| 469 |
|
|
echo ${items[@]} |
| 470 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 471 |
ciaranm |
1.7 |
} |
| 472 |
|
|
|
| 473 |
vapier |
1.16 |
# @FUNCTION: version_format_string |
| 474 |
|
|
# @USAGE: <format> [version] |
| 475 |
|
|
# @DESCRIPTION: |
| 476 |
|
|
# Reformat complicated version strings. The first argument is the string |
| 477 |
|
|
# to reformat with while the rest of the args are passed on to the |
| 478 |
|
|
# get_version_components function. You should make sure to single quote |
| 479 |
|
|
# the first argument since it'll have variables that get delayed expansion.s |
| 480 |
|
|
# @EXAMPLE: |
| 481 |
|
|
# P="cow-hat-1.2.3_p4" |
| 482 |
|
|
# MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4') |
| 483 |
|
|
# Now MY_P will be: cow-hat_source_1_2-3_p4 |
| 484 |
|
|
version_format_string() { |
| 485 |
|
|
local fstr=$1 |
| 486 |
|
|
shift |
| 487 |
|
|
set -- $(get_version_components "$@") |
| 488 |
|
|
eval echo "${fstr}" |
| 489 |
|
|
} |
| 490 |
|
|
|
| 491 |
ciaranm |
1.7 |
__versionator__test_version_compare() { |
| 492 |
antarus |
1.12 |
__versionator_shopt_toggle on |
| 493 |
ciaranm |
1.7 |
local lt=1 eq=2 gt=3 p q |
| 494 |
|
|
|
| 495 |
|
|
__versionator__test_version_compare_t() { |
| 496 |
|
|
version_compare "${1}" "${3}" |
| 497 |
|
|
local r=$? |
| 498 |
|
|
[[ ${r} -eq ${2} ]] || echo "FAIL: ${@} (got ${r} exp ${2})" |
| 499 |
|
|
} |
| 500 |
|
|
|
| 501 |
|
|
echo " |
| 502 |
|
|
0 $lt 1 |
| 503 |
|
|
1 $lt 2 |
| 504 |
|
|
2 $gt 1 |
| 505 |
|
|
2 $eq 2 |
| 506 |
|
|
0 $eq 0 |
| 507 |
|
|
10 $lt 20 |
| 508 |
|
|
68 $eq 068 |
| 509 |
|
|
068 $gt 67 |
| 510 |
|
|
068 $lt 69 |
| 511 |
|
|
|
| 512 |
|
|
1.0 $lt 2.0 |
| 513 |
|
|
2.0 $eq 2.0 |
| 514 |
|
|
2.0 $gt 1.0 |
| 515 |
|
|
|
| 516 |
|
|
1.0 $gt 0.0 |
| 517 |
|
|
0.0 $eq 0.0 |
| 518 |
|
|
0.0 $lt 1.0 |
| 519 |
|
|
|
| 520 |
|
|
0.1 $lt 0.2 |
| 521 |
|
|
0.2 $eq 0.2 |
| 522 |
|
|
0.3 $gt 0.2 |
| 523 |
|
|
|
| 524 |
|
|
1.2 $lt 2.1 |
| 525 |
|
|
2.1 $gt 1.2 |
| 526 |
|
|
|
| 527 |
|
|
1.2.3 $lt 1.2.4 |
| 528 |
|
|
1.2.4 $gt 1.2.3 |
| 529 |
|
|
|
| 530 |
|
|
1.2.0 $eq 1.2 |
| 531 |
|
|
1.2.1 $gt 1.2 |
| 532 |
|
|
1.2 $lt 1.2.1 |
| 533 |
|
|
|
| 534 |
|
|
1.2b $eq 1.2b |
| 535 |
|
|
1.2b $lt 1.2c |
| 536 |
|
|
1.2b $gt 1.2a |
| 537 |
|
|
1.2b $gt 1.2 |
| 538 |
|
|
1.2 $lt 1.2a |
| 539 |
|
|
|
| 540 |
|
|
1.3 $gt 1.2a |
| 541 |
|
|
1.3 $lt 1.3a |
| 542 |
|
|
|
| 543 |
|
|
1.0_alpha7 $lt 1.0_beta7 |
| 544 |
|
|
1.0_beta $lt 1.0_pre |
| 545 |
|
|
1.0_pre5 $lt 1.0_rc2 |
| 546 |
|
|
1.0_rc2 $lt 1.0 |
| 547 |
|
|
|
| 548 |
|
|
1.0_p1 $gt 1.0 |
| 549 |
|
|
1.0_p1-r1 $gt 1.0_p1 |
| 550 |
|
|
|
| 551 |
|
|
1.0_alpha6-r1 $gt 1.0_alpha6 |
| 552 |
|
|
1.0_beta6-r1 $gt 1.0_alpha6-r2 |
| 553 |
|
|
|
| 554 |
|
|
1.0_pre1 $lt 1.0-p1 |
| 555 |
|
|
|
| 556 |
|
|
1.0p $gt 1.0_p1 |
| 557 |
|
|
1.0r $gt 1.0-r1 |
| 558 |
|
|
1.6.15 $gt 1.6.10-r2 |
| 559 |
|
|
1.6.10-r2 $lt 1.6.15 |
| 560 |
|
|
|
| 561 |
|
|
" | while read a b c ; do |
| 562 |
|
|
[[ -z "${a}${b}${c}" ]] && continue; |
| 563 |
|
|
__versionator__test_version_compare_t "${a}" "${b}" "${c}" |
| 564 |
|
|
done |
| 565 |
ciaranm |
1.2 |
|
| 566 |
|
|
|
| 567 |
ciaranm |
1.7 |
for q in "alpha beta pre rc=${lt};${gt}" "p r=${gt};${lt}" ; do |
| 568 |
|
|
for p in ${q%%=*} ; do |
| 569 |
|
|
local c=${q##*=} |
| 570 |
|
|
local alt=${c%%;*} agt=${c##*;} |
| 571 |
|
|
__versionator__test_version_compare_t "1.0" $agt "1.0_${p}" |
| 572 |
|
|
__versionator__test_version_compare_t "1.0" $agt "1.0_${p}1" |
| 573 |
|
|
__versionator__test_version_compare_t "1.0" $agt "1.0_${p}068" |
| 574 |
|
|
|
| 575 |
|
|
__versionator__test_version_compare_t "2.0_${p}" $alt "2.0" |
| 576 |
|
|
__versionator__test_version_compare_t "2.0_${p}1" $alt "2.0" |
| 577 |
|
|
__versionator__test_version_compare_t "2.0_${p}068" $alt "2.0" |
| 578 |
|
|
|
| 579 |
|
|
__versionator__test_version_compare_t "1.0_${p}" $eq "1.0_${p}" |
| 580 |
|
|
__versionator__test_version_compare_t "0.0_${p}" $lt "0.0_${p}1" |
| 581 |
|
|
__versionator__test_version_compare_t "666_${p}3" $gt "666_${p}" |
| 582 |
|
|
|
| 583 |
|
|
__versionator__test_version_compare_t "1_${p}7" $lt "1_${p}8" |
| 584 |
|
|
__versionator__test_version_compare_t "1_${p}7" $eq "1_${p}7" |
| 585 |
|
|
__versionator__test_version_compare_t "1_${p}7" $gt "1_${p}6" |
| 586 |
|
|
__versionator__test_version_compare_t "1_${p}09" $eq "1_${p}9" |
| 587 |
|
|
done |
| 588 |
ciaranm |
1.2 |
done |
| 589 |
|
|
|
| 590 |
ciaranm |
1.7 |
for p in "-r" "_p" ; do |
| 591 |
|
|
__versionator__test_version_compare_t "7.2${p}1" $lt "7.2${p}2" |
| 592 |
|
|
__versionator__test_version_compare_t "7.2${p}2" $gt "7.2${p}1" |
| 593 |
|
|
__versionator__test_version_compare_t "7.2${p}3" $gt "7.2${p}2" |
| 594 |
|
|
__versionator__test_version_compare_t "7.2${p}2" $lt "7.2${p}3" |
| 595 |
|
|
done |
| 596 |
antarus |
1.12 |
__versionator_shopt_toggle off |
| 597 |
ciaranm |
1.2 |
} |