Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatic package selection #217

Merged
merged 11 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Options:
--cached-only only use cached packages
--ignore <prompt|always|never>
whether to add packages to IgnorePkg
--latest pick latest matching version
--oldest pick oldest matching version
--prefer-cache do not query ala if a matching package was found in cache
--version show downgrade version
-h, --help show help script

Expand Down Expand Up @@ -78,6 +81,12 @@ Downgrade a package, looking in only the A.L.A.:
# downgrade --ala-only foo
```

Noninteractively downgrade foo to 1.0.0-1

```
# downgrade --latest --prefer-cache --ignore never 'foo=1.0.0-1'
```

## Configuration

Command-line options can be set persistently in
Expand Down
68 changes: 51 additions & 17 deletions bin/downgrade
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ $(gettext "Options"):
--cached-only $(gettext "only use cached packages")
--ignore <prompt|always|never>
$(gettext "whether to add packages to IgnorePkg")
--latest $(gettext "pick latest matching version")
--oldest $(gettext "pick oldest matching version")
--prefer-cache $(gettext "do not query ala if a matching package was found in cache")
--version $(gettext "show downgrade version")
-h, --help $(gettext "show help script")

Expand Down Expand Up @@ -180,31 +183,33 @@ matches_name_version_filter() {
esac
}

search_packages() {
search_ala() {
local name=$1 pkgfile_re index

pkgfile_re="$name-[^-]+-[0-9.]+-(any|$DOWNGRADE_ARCH)\\.pkg\\.tar\\.(gz|xz|zst)"
index="$DOWNGRADE_ALA_URL/packages/${name:0:1}/$name/"

if ((DOWNGRADE_FROM_ALA)); then
curl --fail --silent "$index" | sed_msg "to parse A.L.A." -E '
/.* href="('"$pkgfile_re"')".*/!d;
s||'"$index"'\1|g; s|\+| |g; s|%|\\x|g' | xargs -0 printf "%b"
fi
curl --fail --silent "$index" | sed_msg "to parse A.L.A." -E '
/.* href="('"$pkgfile_re"')".*/!d;
s||'"$index"'\1|g; s|\+| |g; s|%|\\x|g' | xargs -0 printf "%b"
}

if ((DOWNGRADE_FROM_CACHE)); then
# Delay this defaulting so #read_pacman_conf behavior is tested
if ((!${#PACMAN_CACHE[@]})); then
mapfile -t PACMAN_CACHE < <(read_pacman_conf CacheDir)
fi
search_cache() {
local name=$1 pkgfile_re index

if ((!${#PACMAN_CACHE[@]})); then
PACMAN_CACHE=(/var/cache/pacman/pkg/)
fi
pkgfile_re="$name-[^-]+-[0-9.]+-(any|$DOWNGRADE_ARCH)\\.pkg\\.tar\\.(gz|xz|zst)"

# Delay this defaulting so #read_pacman_conf behavior is tested
if ((!${#PACMAN_CACHE[@]})); then
mapfile -t PACMAN_CACHE < <(read_pacman_conf CacheDir)
fi

# shellcheck disable=SC2086
find -L "${PACMAN_CACHE[@]}" -maxdepth "$DOWNGRADE_MAXDEPTH" -regextype posix-extended -regex ".*/$pkgfile_re"
if ((!${#PACMAN_CACHE[@]})); then
PACMAN_CACHE=(/var/cache/pacman/pkg/)
fi

# shellcheck disable=SC2086
find -L "${PACMAN_CACHE[@]}" -maxdepth "$DOWNGRADE_MAXDEPTH" -regextype posix-extended -regex ".*/$pkgfile_re"
}

sort_packages() {
Expand Down Expand Up @@ -281,7 +286,16 @@ process_term() {

installed=($(previously_installed "$name"))
current=$(currently_installed "$name")
candidates=($(search_packages "$name" | filter_packages "$name" "$operator" "$version" | sort_packages))

candidates=()
if ((DOWNGRADE_FROM_CACHE)); then
candidates+=($(search_cache "$name" | filter_packages "$name" "$operator" "$version"))
fi
if ((DOWNGRADE_FROM_ALA)) && { ((! DOWNGRADE_PREFER_CACHE)) || ((${#candidates[@]} == 0)); }; then
candidates=($(search_ala "$name" | filter_packages "$name" "$operator" "$version") "${candidates[@]}")
fi

candidates=($(printf '%s\n' "${candidates[@]}" | sort_packages))

if (("${#candidates[@]}" == 0)); then
{
Expand All @@ -290,6 +304,12 @@ process_term() {
} >&2
elif (("${#candidates[@]}" == 1)); then
choice=${candidates[0]}
elif ((DOWNGRADE_TO_LATEST)); then
# Select the most up to date package
choice=${candidates[-1]}
elif ((DOWNGRADE_TO_OLDEST)); then
# Select the most out of date
choice=${candidates[0]}
else
choice=$(present_packages "$name" "${candidates[@]}" |
fzf --tac --border --header-lines 1 --tiebreak=begin |
Expand Down Expand Up @@ -431,6 +451,17 @@ parse_options() {
exit 1
fi
;;
--latest)
DOWNGRADE_TO_LATEST=1
DOWNGRADE_TO_OLDEST=0
;;
--oldest)
DOWNGRADE_TO_LATEST=0
DOWNGRADE_TO_OLDEST=1
;;
--prefer-cache)
DOWNGRADE_PREFER_CACHE=1
;;
--)
shift
pacman_args=("$@")
Expand Down Expand Up @@ -492,6 +523,9 @@ DOWNGRADE_MAXDEPTH=1
DOWNGRADE_CONF="/etc/xdg/downgrade/downgrade.conf"
DOWNGRADE_VERSION="11.3.0"
DOWNGRADE_IGNORE="prompt"
DOWNGRADE_TO_LATEST=0
DOWNGRADE_TO_OLDEST=0
krumelmonster marked this conversation as resolved.
Show resolved Hide resolved
DOWNGRADE_PREFER_CACHE=0

# Main code execution
if ((!LIB)); then
Expand Down
3 changes: 3 additions & 0 deletions completion/downgrade/fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ complete -c $cmd -l ala-url -x -d 'location of ALA server'
complete -c $cmd -l ala-only -d 'only use ALA server'
complete -c $cmd -l cached-only -d 'only use cached packages'
complete -c $cmd -l ignore -x -d 'whether to add packages to IgnorePkg' -a 'prompt always never'
complete -c $cmd -l latest -d 'pick latest matching version'
complete -c $cmd -l oldest -d 'pick oldest matching version'
complete -c $cmd -l prefer-cache -d 'skip ala if matched in cache'
complete -c $cmd -l version -d 'show downgrade version'
complete -c $cmd -s h -l help -d 'show help script'
3 changes: 3 additions & 0 deletions completion/downgrade/zsh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ _downgrade () {
'--ala-only[only use ALA server]' \
'--cached-only[only use cached packages]' \
'--ignore[whether to add packages to IgnorePkg]:action:(prompt always never)' \
'--latest[pick latest matching version]' \
'--oldest[pick oldest matching version]' \
'--prefer-cache[skip ala if matched in cache]' \
'--version[show downgrade version]' \
{-h,--help}'[show help script]' \
'*:packages:->pkg'
Expand Down
Loading
Loading