Skip to content

Instantly share code, notes, and snippets.

@robvanoostenrijk
Last active December 27, 2023 03:33
Show Gist options
  • Save robvanoostenrijk/9af9a6f69752cc9c545023a5f19d7e7f to your computer and use it in GitHub Desktop.
Save robvanoostenrijk/9af9a6f69752cc9c545023a5f19d7e7f to your computer and use it in GitHub Desktop.
Email validation git pre-commit hook

Githook pre-commit script to prevent accidental commits using the wrong email address, for example from git global configuration.

#!/bin/sh
EMAIL=$(git config user.email)
if [[ $EMAIL == *"@domain.com" ]]
then
while true; do
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
read -p "[pre-commit hook] Committing with @domain.com email, continue? (Y/n) " yn
if [ "$yn" = "" ]; then
yn='Y'
fi
# Release stdin
exec <&-
case $yn in
[Yy])
exit 0
;;
[Nn])
echo "[pre-commit hook] Aborting commit."
exit 1
;;
*)
echo "[pre-commit hook] Please answer y or n for yes or no."
;;
esac
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment