Githook pre-commit script to prevent accidental commits using the wrong email address, for example from git global configuration.
Last active
December 27, 2023 03:33
-
-
Save robvanoostenrijk/9af9a6f69752cc9c545023a5f19d7e7f to your computer and use it in GitHub Desktop.
Email validation git pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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