aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-12-22 03:12:06 -0800
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2025-12-22 03:12:06 -0800
commit0c3d5a38c03c3ac22017c40aa08e38b3e3124275 (patch)
tree0beef05024d53158e41fbf888a75cd5876d8648e
parent224aa11c1854cf1742d30d2bd64a60ac78da3814 (diff)
downloadgbpm-0c3d5a38c03c3ac22017c40aa08e38b3e3124275.tar.gz
gbpm-0c3d5a38c03c3ac22017c40aa08e38b3e3124275.tar.xz
improved match prioritisation
-rw-r--r--README4
-rwxr-xr-xgbpm.sh38
2 files changed, 22 insertions, 20 deletions
diff --git a/README b/README
index 6239b75..36cc9fd 100644
--- a/README
+++ b/README
@@ -18,8 +18,8 @@ NOTES
DEPENDENCIES
- bash (>= 4.0)
- coreutils (sort)
+ bash (>= 4.1)
+ coreutils (>= 7.0) (sort)
INSTALLING
diff --git a/gbpm.sh b/gbpm.sh
index 4a6fdd6..ac46c33 100755
--- a/gbpm.sh
+++ b/gbpm.sh
@@ -113,33 +113,35 @@ cmd_get() {
cmd_set() {
[[ -n "${1}" ]] || _die 'set command requires an argument'
- local dir dirs match match_start
+ local dir dirs bn prefix pri match match_pri
_populate_dirs
for dir in "${dirs[@]}"; do
- [[ "${dir##*/}" == *"${1}"* ]] || continue
-
- match="${dir}"
-
- # exact match
- if [[ "${dir##*/}" == "${1}" ]]; then
- unset match_start
- break
+ bn="${dir##*/}"
+
+ [[ "${bn}" == *"${1}"* ]] || continue
+
+ prefix="${bn%%${1}*}"
+ pri="${prefix//[^-]}"
+ pri="$((${#pri} * 4))"
+ if [[ "${prefix}" == @(|*-) ]]; then
+ case "${bn#*${1}}" in
+ '') ;;
+ -*) ((pri++)) ;;
+ *) ((pri += 2)) ;;
+ esac
+ else
+ ((pri += 3))
fi
- # match from start of version is preferred. we assume the
- # version is at the beginning of the directory name or
- # immediately following a hyphen. this avoids unintuitive
- # behaviour like `2.1' matching `3.2.1' over `2.1.0'.
- if [[ "${dir##*/}" == "${1}"* ||
- "${dir##*/}" == *-"${1}"* ]]; then
- match_start="${dir}"
- fi
+ [[ "${pri}" -le "${match_pri:-${pri}}" ]] || continue
+
+ match="${dir}"
+ match_pri="${pri}"
done
[[ -n "${match}" ]] || _die 'no matching version found'
- [[ -n "${match_start}" ]] && match="${match_start}"
_clear
_add "${match}"