Dopo i fatti
Dove gli hook al momento del commit modellano i commit, gli hook in questa pagina reagiscono ad essi. post-commit gira localmente dopo ogni commit, pre-push gira prima del trasferimento al remoto, e post-receive gira sul server dopo che un push e arrivato.
post-commit
#!/usr/bin/env bash
ctags -R --languages=python -f .tags & disown
notify-send "Committed: $(git log -1 --format=%s)"
pre-push
#!/usr/bin/env bash
remote="$1"
url="$2"
while read local_ref local_sha remote_ref remote_sha; do
if [ "$remote_ref" = "refs/heads/main" ]; then
if ! cargo test --quiet; then
echo "Tests fail; refusing to push to main."
exit 1
fi
fi
done
post-receive (lato server)
#!/usr/bin/env bash
while read oldrev newrev refname; do
branch=${refname#refs/heads/}
if [ "$branch" = "main" ]; then
GIT_WORK_TREE=/var/www/site git checkout -f main
curl -X POST https://chat.example/notify \
-d "Deployed $newrev to production"
fi
done
Altri hook server
pre-receive gira una volta per push e puo rifiutare l'intero push. update gira una volta per ref.
Concatenamento di hook
#!/usr/bin/env bash
for hook in .githooks/post-commit.d/*; do
[ -x "$hook" ] && "$hook" "$@" || exit $?
done
Errori comuni
post-commit non puo prevenire un commit; non mettere validazioni li. pre-push gira solo quando push e invocato manualmente.
Debug
exec >> /tmp/git-hook.log 2>&1
set -x