Skip to content

Instantly share code, notes, and snippets.

@albertored11
Last active March 8, 2025 19:37
Show Gist options
  • Select an option

  • Save albertored11/bfc0068f4e43ca0d7ce0af968f7314db to your computer and use it in GitHub Desktop.

Select an option

Save albertored11/bfc0068f4e43ca0d7ce0af968f7314db to your computer and use it in GitHub Desktop.

Revisions

  1. albertored11 revised this gist Mar 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion yaycache
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ for pkgdir in "$cachedir"/*/; do
    pkgver="$(pacman -Q $pkgname | cut -d ' ' -f2 | cut -d '-' -f1 | cut -d ':' -f2)"

    cd "$pkgdir"
    rm -f "$(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir/%s\n")"
    rm -f $(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir/%s\n")

    fi

  2. albertored11 revised this gist Mar 14, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion yaycache
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ for pkgdir in "$cachedir"/*/; do

    done

    # Keep latest version for uninstalled native packages, keep two latest versions for installed packages
    # Remove everything for uninstalled foreign packages, keep latest version for uninstalled native packages, keep two latest versions for installed packages
    /usr/bin/paccache -qruk0 $pkgcache
    /usr/bin/paccache -qruk1
    /usr/bin/paccache -qrk2 -c /var/cache/pacman/pkg $pkgcache
  3. albertored11 revised this gist Mar 14, 2021. 2 changed files with 21 additions and 13 deletions.
    32 changes: 20 additions & 12 deletions yaycache
    Original file line number Diff line number Diff line change
    @@ -3,23 +3,31 @@
    # Assuming yay is run by user with UID 1000
    admin="$(id -nu 1000)"
    cachedir="/home/$admin/.cache/yay"
    removed="$(comm -23 <(find $cachedir -mindepth 1 -maxdepth 1 -type d | cut -d / -f6 | sort) <(pacman -Qqm) | xargs -r printf "$cachedir/%s\n")"
    removed="$(comm -23 <(basename -a $(find $cachedir -mindepth 1 -maxdepth 1 -type d) | sort) <(pacman -Qqm) | xargs -r printf "$cachedir/%s\n")"

    # Remove yay cache for foreign packages that are not installed anymore
    # Remove yay cache for foreign packages that are not installed anymore
    rm -rf $removed

    pkgcache="$(find $cachedir -mindepth 1 -maxdepth 1 -type d | xargs -r printf "-c %s\n")"

    for pkgdir in $cachedir/*/; do
    pkgname="$(echo $pkgdir | cut -d '/' -f6)"
    pkgver="$(pacman -Q $pkgname | cut -d ' ' -f2 | cut -d '-' -f1)"
    cd $pkgdir
    # Remove untracked files (e. g. source/build files) excepting package files and main source files for installed version
    rm -f $(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir%s\n")
    rm -rf src/
    for pkgdir in "$cachedir"/*/; do

    pkgname=$(basename "$pkgdir")

    # Remove untracked files (e. g. source/build files) excepting package files and main source files for installed version if non-git package
    if [[ ! "$pkgname" =~ ^.*-git$ ]]; then

    pkgver="$(pacman -Q $pkgname | cut -d ' ' -f2 | cut -d '-' -f1 | cut -d ':' -f2)"

    cd "$pkgdir"
    rm -f "$(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir/%s\n")"

    fi

    rm -rf "$pkgdir"/src/

    done

    # Remove everything for uninstalled foreign packages, keep latest version for uninstalled native packages, keep two latest versions for installed packages
    /usr/bin/paccache -qruk0 $pkgcache
    # Keep latest version for uninstalled native packages, keep two latest versions for installed packages
    /usr/bin/paccache -qruk1
    /usr/bin/paccache -qrk2 -c /var/cache/pacman/pkg $pkgcache
    /usr/bin/paccache -qrk2 -c /var/cache/pacman/pkg $pkgcache
    2 changes: 1 addition & 1 deletion yaycache.hook
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,4 @@ Target = *
    Description = Cleaning pacman and yay cache...
    When = PostTransaction
    Exec = /home/herbort/.local/bin/yaycache
    Depends = pacman-contrib
    Depends = pacman-contrib
  4. albertored11 revised this gist Dec 23, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion yaycache.hook
    Original file line number Diff line number Diff line change
    @@ -7,5 +7,5 @@ Target = *
    [Action]
    Description = Cleaning pacman and yay cache...
    When = PostTransaction
    Exec = /home/herbort/.local/bin/yaycache.sh
    Exec = /home/herbort/.local/bin/yaycache
    Depends = pacman-contrib
  5. albertored11 renamed this gist Dec 23, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. albertored11 revised this gist Dec 22, 2020. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions yaycache.hook
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    [Trigger]
    Operation = Upgrade
    Operation = Remove
    Type = Package
    Target = *

    [Action]
    Description = Cleaning pacman and yay cache...
    When = PostTransaction
    Exec = /home/herbort/.local/bin/yaycache.sh
    Depends = pacman-contrib
  7. albertored11 created this gist Dec 22, 2020.
    25 changes: 25 additions & 0 deletions yaycache.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #!/usr/bin/env bash

    # Assuming yay is run by user with UID 1000
    admin="$(id -nu 1000)"
    cachedir="/home/$admin/.cache/yay"
    removed="$(comm -23 <(find $cachedir -mindepth 1 -maxdepth 1 -type d | cut -d / -f6 | sort) <(pacman -Qqm) | xargs -r printf "$cachedir/%s\n")"

    # Remove yay cache for foreign packages that are not installed anymore
    rm -rf $removed

    pkgcache="$(find $cachedir -mindepth 1 -maxdepth 1 -type d | xargs -r printf "-c %s\n")"

    for pkgdir in $cachedir/*/; do
    pkgname="$(echo $pkgdir | cut -d '/' -f6)"
    pkgver="$(pacman -Q $pkgname | cut -d ' ' -f2 | cut -d '-' -f1)"
    cd $pkgdir
    # Remove untracked files (e. g. source/build files) excepting package files and main source files for installed version
    rm -f $(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir%s\n")
    rm -rf src/
    done

    # Remove everything for uninstalled foreign packages, keep latest version for uninstalled native packages, keep two latest versions for installed packages
    /usr/bin/paccache -qruk0 $pkgcache
    /usr/bin/paccache -qruk1
    /usr/bin/paccache -qrk2 -c /var/cache/pacman/pkg $pkgcache