diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-01 17:19:14 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-01 18:55:16 +0200 |
| commit | fe21f3ac307b9e6598999ee416362803f72bb869 (patch) | |
| tree | b67aa60cfccab141031f0fb634a5cbec4f1bf374 /.local/bin | |
| parent | caf53274f24377717ee0f9ac030f9159d9d336e6 (diff) | |
| download | dotfiles-fe21f3ac307b9e6598999ee416362803f72bb869.tar.gz dotfiles-fe21f3ac307b9e6598999ee416362803f72bb869.zip | |
refactor(dotfiles): group pacman local db queries
In dotfiles sync, `sync_packages()` queries the database only twice
(once for packages to install and once for packages to upgrade) instead
of twice per package (which was too slow). The function determines the
packages to install/update by looking at the disjunction of the
specified packages and the list of installed packages obtained with
`pacman --query --quiet`.
Diffstat (limited to '.local/bin')
| -rwxr-xr-x | .local/bin/dotfiles | 34 |
1 files changed, 14 insertions, 20 deletions
diff --git a/.local/bin/dotfiles b/.local/bin/dotfiles index e953cd6..e7a581a 100755 --- a/.local/bin/dotfiles +++ b/.local/bin/dotfiles @@ -15,19 +15,6 @@ clipboard_hint() { echo -e "Copied to clipboard:\n\n$cmd\n" } -# is_sync cmd pack -# e.g.: is_sync pacman python-pipx -# return 0 → package is installed and up-to-date -# return 1 → package is not installed or out-of-date -is_sync() { - local cmd="${1:?cmd argument missing}" - local pack="${2:?pack argument missing}" - if ! "$cmd" --query "$pack" >/dev/null 2>&1 || "$cmd" --query --upgrades "$pack" >/dev/null 2>&1; then - return 1 - fi - return 0 -} - # sync_packs cmd packages [sync-flag…] # cmd → pacman or yay # packages → nameref to an array containing a list of packages to sync @@ -37,13 +24,20 @@ sync_packages() { local -n packages="${2:?packages argument missing}" shift 2 local sync_args=("$@") - local out_of_sync_deps=() - for dep in "${packages[@]}"; do - if ! is_sync "$cmd" "$dep"; then out_of_sync_deps+=("$dep"); fi - done - if ((${#out_of_sync_deps[@]} == 0)); then return; fi - if prompt_for_install "Out-of-sync packages: ${out_of_sync_deps[*]}"; then - sudo "$cmd" --sync "${sync_args[@]}" "${out_of_sync_deps[@]}" + readarray -t packages_to_install < <( + comm -13 \ + <(sort <("$cmd" --query --quiet)) \ + <(sort <(printf '%s\n' "${packages[@]}")) + ) + readarray -t packages_to_upgrade < <( + comm -12 \ + <(sort <("$cmd" --query --upgrades --quiet)) \ + <(sort <(printf '%s\n' "${packages[@]}")) + ) + local packages_to_sync=("${packages_to_install[@]}" "${packages_to_upgrade[@]}") + if ((${#packages_to_sync[@]} == 0)); then return; fi + if prompt_for_install "Out-of-sync packages: ${packages_to_sync[*]}"; then + sudo "$cmd" --sync "${sync_args[@]}" "${packages_to_sync[@]}" fi } |
