[zsh|git] update git global hook setup

use git config variables to run global hooks, and try to detect them
when entering a dir
This commit is contained in:
odrling 2024-03-15 22:59:28 +01:00
parent 0bcd5f42de
commit 9d379ba7e3
Signed by: odrling
GPG key ID: 2D8C2F151EDB7392
3 changed files with 47 additions and 10 deletions

View file

@ -15,7 +15,7 @@ ewarn() {
runif() {
if [ -n "$1" ]; then
if [ "$(git config "odrhooks.$1")" = 1 ]; then
shift
einfo "$*"
sh -c "$*" || die "$*: failed"
@ -23,9 +23,9 @@ runif() {
return 0
}
runif "$GIT_HOOK_ISORT" isort . --check
runif "$GIT_HOOK_BLACK" black . --check
runif "$GIT_HOOK_RUFF" ruff check .
runif "$GIT_HOOK_PYRIGHT" pyright .
runif "$GIT_HOOK_CLANG_FORMAT" 'fd --glob \*.c | xargs clang-format -Werror -n'
runif "$GIT_HOOK_GOFMT" '[ "$(gofmt -l . | wc -l)" -eq 0 ] || (gofmt -l -d .; exit 1)'
runif "isort" isort . --check
runif "black" black . --check
runif "ruff" ruff check .
runif "pyright" pyright .
runif "clang_format" 'fd --glob \*.c | xargs clang-format -Werror -n'
runif "gofmt" '[ "$(gofmt -l . | wc -l)" -eq 0 ] || (gofmt -l -d .; exit 1)'

View file

@ -129,21 +129,31 @@ odr-load-poetry() {
odr-load-python-venv() {
if [ -f poetry.lock ]; then
odr-load-poetry
return
fi
if [ -f pyproject.toml ]; then
elif [ -f pyproject.toml ]; then
VENVDIR="${PWD}/.direnv/python-$(odr-python-minor-version)"
if [ -d "$VENVDIR" ]; then
odr-load-venv "$VENVDIR"
else
ewarn "found pyproject.toml file but no local venv"
fi
else
return 1
fi
[ -f ~/.zsh/envs/python ] && source ~/.zsh/envs/python
}
odr-display-hooks() {
[ -n "${DETECTED_HOOKS}" ] &&
einfo "Detected hooks: ${DETECTED_HOOKS}. Enable them with enable-detected-hooks"
git config --get-regexp odrhooks\. 2>/dev/null
}
odr-defaultenv() {
export DETECTED_HOOKS=
odr-load-python-venv
odr-loadenvrc
odr-display-hooks
}
odr-envs() {
@ -158,6 +168,18 @@ odr-envs() {
esac
}
enable-hook() {
for hook in $@; do
einfo "Enabling $hook hook"
git config --local "odrhooks.$hook" 1
done
}
enable-detected-hooks() {
hooks=(${(s/ /)DETECTED_HOOKS})
enable-hook $hooks
}
source_up() {
ewarn "source_up was called and is a no-op, should be removed in .envrc"
}

15
.zsh/envs/python Normal file
View file

@ -0,0 +1,15 @@
# shellcheck shell=zsh
function () {
local possible_hooks=(ruff isort black pyright)
local hooks=()
for hook in ${possible_hooks[@]}; do
[ "$(git config odrhooks.$hook)" != 1 ] &&
grep $hook pyproject.toml &>/dev/null && hooks+=($hook)
done
export DETECTED_HOOKS="$hooks"
}