Skip to content

Commit 43bb5c1

Browse files
committed
More intuitive version matching
Avoids many issues with regex and glob patterns in version arguments matching incorrectly. Prevents invalid versions being requested from remote source. Always provides opportunity to confirm version being installed. Signed-off-by: Roy-Orbison <Roy-Orbison@users.noreply.github.com>
1 parent bdb32e3 commit 43bb5c1

File tree

1 file changed

+52
-39
lines changed

1 file changed

+52
-39
lines changed

ubuntu-mainline-kernel.sh

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ latest_local_version() {
369369
}
370370

371371
remote_html_cache=""
372+
remote_versions_read=0
373+
374+
normalize_version_for_match() {
375+
local version="$1"
376+
if [[ -n "$version" && ! "$version" =~ [*] ]]; then
377+
# not already a glob pattern
378+
if [[ ! "$version" =~ ^v ]]; then
379+
version="v$version"
380+
fi
381+
version="${version%.}"
382+
fi
383+
echo "$version"
384+
}
385+
372386
parse_remote_versions() {
373387
local line
374388
while read -r line; do
@@ -389,34 +403,40 @@ parse_remote_versions() {
389403
load_remote_versions () {
390404
local line
391405

392-
[[ -n "$2" ]] && {
393-
REMOTE_VERSIONS=()
406+
[[ -n "$1" ]] && {
407+
remote_versions_read=0
394408
}
395409

396-
if [ ${#REMOTE_VERSIONS[@]} -eq 0 ]; then
410+
if [ $remote_versions_read -eq 0 ]; then
411+
REMOTE_VERSIONS=()
397412
if [ -z "$remote_html_cache" ]; then
398-
[ -z "$1" ] && logn "Downloading index from $ppa_host"
413+
[ -z "$2" ] && logn "Downloading index from $ppa_host"
399414
remote_html_cache=$(download $ppa_host $ppa_index)
400-
[ -z "$1" ] && log
415+
[ -z "$2" ] && log
401416
fi
402417

418+
version_for_match="$(normalize_version_for_match "$1")"
403419
IFS=$'\n'
404420
while read -r line; do
421+
(( remote_versions_read++ ))
405422
# reinstate original rc suffix join character
406423
if [[ $line =~ ^([^~]+)~([^~]+)$ ]]; then
407424
[[ $use_rc -eq 0 ]] && continue
408425
line="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}"
409426
fi
410-
[[ -n "$2" ]] && [[ ! "$line" =~ $2 ]] && continue
411-
REMOTE_VERSIONS+=("$line")
427+
if [[ -z "$version_for_match" ]] || \
428+
[[ "$line" == $version_for_match || "$line" == $version_for_match.* || "$line" == $version_for_match-* ]]
429+
then
430+
REMOTE_VERSIONS+=("$line")
431+
fi
412432
done < <(parse_remote_versions | sort -V)
413433
unset IFS
414434
fi
415435
}
416436

417437
latest_remote_version () {
418-
load_remote_versions 1 "$1"
419-
echo "${REMOTE_VERSIONS[${#REMOTE_VERSIONS[@]}-1]}"
438+
load_remote_versions "$1" 1
439+
[[ ${#REMOTE_VERSIONS[@]} -gt 0 ]] && echo "${REMOTE_VERSIONS[${#REMOTE_VERSIONS[@]}-1]}"
420440
}
421441

422442
check_environment () {
@@ -430,7 +450,7 @@ guard_run_as_root () {
430450
if [ "$(id -u)" -ne 0 ]; then
431451
echo "The '$run_action' command requires root privileges"
432452
exit 2
433-
fi
453+
fi
434454
}
435455

436456
# execute requested action
@@ -535,15 +555,13 @@ Optional:
535555
;;
536556
remote-list)
537557
check_environment
538-
load_remote_versions
558+
load_remote_versions "${action_data[0]}"
539559

540560
# shellcheck disable=SC2015
541561
[[ -n "$(command -v column)" ]] && { column="column -x"; } || { column="cat"; }
542562

543563
(for version in "${REMOTE_VERSIONS[@]}"; do
544-
if [ -z "${action_data[0]}" ] || [[ "$version" =~ ${action_data[0]} ]]; then
545-
echo "$version"
546-
fi
564+
echo "$version"
547565
done) | $column
548566
;;
549567
install)
@@ -558,42 +576,37 @@ Optional:
558576
version=$(latest_remote_version)
559577
log
560578

561-
if containsElement "$version" "${LOCAL_VERSIONS[@]}"; then
562-
logn "Latest version is $version but seems its already installed"
563-
else
564-
logn "Latest version is: $version"
565-
fi
566-
567-
if [ $do_install -gt 0 ] && [ $assume_yes -eq 0 ];then
568-
logn ", continue? (y/N) "
569-
[ $quiet -eq 0 ] && read -rsn1 continue
570-
log
579+
[[ -z "$version" ]] && {
580+
err "No readable versions found"
581+
exit 2
582+
}
571583

572-
[ "$continue" != "y" ] && [ "$continue" != "Y" ] && { exit 0; }
573-
else
574-
log
575-
fi
584+
logn "Latest version is $version"
576585
else
577-
load_remote_versions
586+
load_remote_versions "${action_data[0]}"
578587

579-
version=""
580-
if containsElement "v${action_data[0]#v}" "${REMOTE_VERSIONS[@]}"; then
581-
version="v"${action_data[0]#v}
582-
fi
588+
version=$(latest_remote_version)
583589

584590
[[ -z "$version" ]] && {
585591
err "Version '${action_data[0]}' not found"
586592
exit 2
587593
}
588594
shift
589595

590-
if [ $do_install -gt 0 ] && containsElement "$version" "${LOCAL_VERSIONS[@]}" && [ $assume_yes -eq 0 ]; then
591-
logn "It seems version $version is already installed, continue? (y/N) "
592-
[ $quiet -eq 0 ] && read -rsn1 continue
593-
log
596+
logn "Specified version matches $version"
597+
fi
594598

595-
[ "$continue" != "y" ] && [ "$continue" != "Y" ] && { exit 0; }
596-
fi
599+
if containsElement "$version" "${LOCAL_VERSIONS[@]}"; then
600+
logn " but it seems it's already installed"
601+
fi
602+
if [ $do_install -gt 0 ] && [ $assume_yes -eq 0 ];then
603+
logn ", continue? (y/N) "
604+
[ $quiet -eq 0 ] && read -rsn1 continue
605+
log
606+
607+
[ "$continue" != "y" ] && [ "$continue" != "Y" ] && { exit 0; }
608+
else
609+
log
597610
fi
598611

599612
[ ! -d "$workdir" ] && {

0 commit comments

Comments
 (0)