dotfiles/.githooks/prepare-commit-msg
Marty Oehme 8cb3252cf1
sh: Switch yay to paru
Removed outdated `syu` symlink which just hooks into topgrade.
Replaced it with simple function that tries for topgrade, paru, yay,
pacman, in that order. Can still be invoked with simple `syu` command,
but *only* through interactive terminal use.

Switched git pre-commit hook to default to paru instead of yay when
compiling installed package lists for dotfile commits.
2020-12-22 19:54:38 +01:00

60 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env sh
COMMIT_MSG_FILE="$1"
COMMIT_SOURCE="$2"
BOOTSTRAPDIR="bootstrap"
pkgfileloc="$(git rev-parse --show-toplevel)/$BOOTSTRAPDIR/packages.txt"
pkgignoreloc="$(git rev-parse --show-toplevel)/$BOOTSTRAPDIR/packages_ignore.txt"
listgen="paru"
err() {
printf "\x1b[33mCAUTION:\x1b[0m %s\n" "$*"
exit 1
}
if [ ! -f "$pkgfileloc" ]; then
err "File not found - $pkgfileloc, can not determine package differences!"
fi
if ! type "$listgen" >/dev/null 2>&1; then
err "List generator not installed on machine, can not reliably determine package differences!"
fi
# get commited packages, remove empty lines
# and lines beginning with # to allow comments
pkgcommited=$(mktemp)
sed -e '/^[#$]/d' <"$pkgfileloc" | sort >"$pkgcommited"
# get packages on this machine
# q removes extraneous info
# e only lists explicitly installed
# tt removes those that are depended on, but NOT those optionally depended on
pkgcurrent=$("$listgen" -Qqett | sort)
# remove those listed in package_ignore.txt
if [ -f "$pkgignoreloc" ]; then
pkgcurrent=$(echo "$pkgcurrent" | comm -23 - "$pkgignoreloc")
fi
# compare the lists, list differences
result=$(echo "$pkgcurrent" | comm -3 - "$pkgcommited")
# get files only in repo (field1), and only on machine (field2)
added=$(echo "$result" | cut -f1 | sed -e '/^$/d')
removed=$(echo "$result" | cut -s -f2)
# if we have no changes, do nothing
if [ -n "$added" ] || [ -n "$removed" ]; then
text=$(printf "\-- PACKAGE DIFFERENCES TO COMMITED LIST FOUND --\nPackages NOT YET in repo:\n%s\n\nPackages ONLY in repo:%s\n" "$added" "$removed" | sed 's/^/# /gm')
else
exit 0
fi
# prepend package changes to message
case $COMMIT_SOURCE in
"" | message, | template,)
msg=$(echo "$text" | cat - "$COMMIT_MSG_FILE")
printf "%s" "$msg" >"$COMMIT_MSG_FILE"
;;
esac