Skip to content

Commit bdb32e3

Browse files
authored
fix: better version sorting (#57)
* Better version sorting Have remote list sorted correctly by default for better readability latest version selection (in case `-rc*` versions are used). * Further simplify version sorting Take advantage of Debian's convention of using ~ for pre-release suffixes. * Remove perl dependency from improved version sort * Better version sorting review changes * Whoops
1 parent dbf87cf commit bdb32e3

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

ubuntu-mainline-kernel.sh

+27-16
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ action_data=()
102102
} || {
103103
OS=$(lsb_release -si 2>&-)
104104
[[ "$OS" == "Ubuntu" ]] || [[ "$OS" == "LinuxMint" ]] || [[ "$OS" == "neon" ]] || {
105-
echo "Abort, this script is only intended for Ubuntu-like distro's"
105+
echo "Abort, this script is only intended for Ubuntu-like distros"
106106
exit 2
107107
}
108108
}
@@ -342,7 +342,7 @@ load_local_versions() {
342342
local version
343343
if [ ${#LOCAL_VERSIONS[@]} -eq 0 ]; then
344344
IFS=$'\n'
345-
for pckg in $(dpkg -l linux-image-* | cut -d " " -f 3 | sort); do
345+
for pckg in $(dpkg -l linux-image-* | cut -d " " -f 3 | sort -V); do
346346
# only match kernels from ppa
347347
if [[ "$pckg" =~ linux-image-[0-9]+\.[0-9]+\.[0-9]+-[0-9]{6} ]]; then
348348
version="v"$(echo "$pckg" | cut -d"-" -f 3,4)
@@ -369,6 +369,23 @@ latest_local_version() {
369369
}
370370

371371
remote_html_cache=""
372+
parse_remote_versions() {
373+
local line
374+
while read -r line; do
375+
if [[ $line =~ DIR.*href=\"(v[[:digit:]]+\.[[:digit:]]+(\.[[:digit:]]+)?)(-(rc[[:digit:]]+))?/\" ]]; then
376+
line="${BASH_REMATCH[1]}"
377+
if [[ -z "${BASH_REMATCH[2]}" ]]; then
378+
line="$line.0"
379+
fi
380+
# temporarily substitute rc suffix join character for correct version sort
381+
if [[ -n "${BASH_REMATCH[3]}" ]]; then
382+
line="$line~${BASH_REMATCH[4]}"
383+
fi
384+
echo "$line"
385+
fi
386+
done <<<"$remote_html_cache"
387+
}
388+
372389
load_remote_versions () {
373390
local line
374391

@@ -384,28 +401,22 @@ load_remote_versions () {
384401
fi
385402

386403
IFS=$'\n'
387-
for line in $remote_html_cache; do
388-
[[ "$line" =~ "folder" ]] || continue
389-
[[ $use_rc -eq 0 ]] && [[ "$line" =~ -rc ]] && continue
390-
[[ "$line" =~ v[0-9]+\.[0-9]+(\.[0-9]+)?(-rc[0-9]+)?/ ]] || continue
404+
while read -r line; do
405+
# reinstate original rc suffix join character
406+
if [[ $line =~ ^([^~]+)~([^~]+)$ ]]; then
407+
[[ $use_rc -eq 0 ]] && continue
408+
line="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}"
409+
fi
391410
[[ -n "$2" ]] && [[ ! "$line" =~ $2 ]] && continue
392-
393-
line=${line##*href=\"}
394-
line=${line%%\/\">*}
395-
[[ ! "$line" =~ (v[0-9]+\.[0-9]+)\.[0-9]+ ]] && [[ "$line" =~ (v[0-9]+\.[0-9]+)(-rc[0-9]+)? ]] && line=${BASH_REMATCH[1]}".0"${BASH_REMATCH[2]}
396-
397411
REMOTE_VERSIONS+=("$line")
398-
done
412+
done < <(parse_remote_versions | sort -V)
399413
unset IFS
400414
fi
401415
}
402416

403417
latest_remote_version () {
404418
load_remote_versions 1 "$1"
405-
local sorted
406-
407-
mapfile -t sorted < <(echo "${REMOTE_VERSIONS[*]}" | tr ' ' '\n' | sort -V)
408-
echo "${sorted[${#sorted[@]}-1]}"
419+
echo "${REMOTE_VERSIONS[${#REMOTE_VERSIONS[@]}-1]}"
409420
}
410421

411422
check_environment () {

0 commit comments

Comments
 (0)