Skip to content

Commit ffbf7c2

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 <[email protected]>
1 parent bdb32e3 commit ffbf7c2

File tree

1 file changed

+53
-39
lines changed

1 file changed

+53
-39
lines changed

ubuntu-mainline-kernel.sh

Lines changed: 53 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,41 @@ 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+
# shellcheck disable=SC2053
428+
if [[ -z "$version_for_match" ]] || \
429+
[[ "$line" == $version_for_match || "$line" == $version_for_match.* || "$line" == $version_for_match-* ]]
430+
then
431+
REMOTE_VERSIONS+=("$line")
432+
fi
412433
done < <(parse_remote_versions | sort -V)
413434
unset IFS
414435
fi
415436
}
416437

417438
latest_remote_version () {
418-
load_remote_versions 1 "$1"
419-
echo "${REMOTE_VERSIONS[${#REMOTE_VERSIONS[@]}-1]}"
439+
load_remote_versions "$1" 1
440+
[[ ${#REMOTE_VERSIONS[@]} -gt 0 ]] && echo "${REMOTE_VERSIONS[${#REMOTE_VERSIONS[@]}-1]}"
420441
}
421442

422443
check_environment () {
@@ -430,7 +451,7 @@ guard_run_as_root () {
430451
if [ "$(id -u)" -ne 0 ]; then
431452
echo "The '$run_action' command requires root privileges"
432453
exit 2
433-
fi
454+
fi
434455
}
435456

436457
# execute requested action
@@ -535,15 +556,13 @@ Optional:
535556
;;
536557
remote-list)
537558
check_environment
538-
load_remote_versions
559+
load_remote_versions "${action_data[0]}"
539560

540561
# shellcheck disable=SC2015
541562
[[ -n "$(command -v column)" ]] && { column="column -x"; } || { column="cat"; }
542563

543564
(for version in "${REMOTE_VERSIONS[@]}"; do
544-
if [ -z "${action_data[0]}" ] || [[ "$version" =~ ${action_data[0]} ]]; then
545-
echo "$version"
546-
fi
565+
echo "$version"
547566
done) | $column
548567
;;
549568
install)
@@ -558,42 +577,37 @@ Optional:
558577
version=$(latest_remote_version)
559578
log
560579

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
580+
[[ -z "$version" ]] && {
581+
err "No readable versions found"
582+
exit 2
583+
}
571584

572-
[ "$continue" != "y" ] && [ "$continue" != "Y" ] && { exit 0; }
573-
else
574-
log
575-
fi
585+
logn "Latest version is $version"
576586
else
577-
load_remote_versions
587+
load_remote_versions "${action_data[0]}"
578588

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

584591
[[ -z "$version" ]] && {
585592
err "Version '${action_data[0]}' not found"
586593
exit 2
587594
}
588595
shift
589596

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
597+
logn "Specified version matches $version"
598+
fi
594599

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

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

0 commit comments

Comments
 (0)