Skip to content

Instantly share code, notes, and snippets.

@mattbell87
Created February 16, 2016 04:51
Show Gist options
  • Save mattbell87/4dce109b3c7098ca8524 to your computer and use it in GitHub Desktop.
Save mattbell87/4dce109b3c7098ca8524 to your computer and use it in GitHub Desktop.
Pre-receive hook to validate user and email fields on git commits
#!/bin/bash
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1
NAME=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p')
EMAIL=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p')
HOSTNAME=$(hostname -f)
if [ "$NAME" != "$USER" ]
then
echo "YOUR COMMIT(S) HAVE NOT BEEN ACCEPTED"
echo ""
echo "Sorry! Please fix your username before you send changes to $HOSTNAME"
echo ""
echo "Use the following command on a terminal to update it:"
echo ' git config --global user.name "'$USER'"'
exit 1
fi
VALIDEMAIL='@valid.domain.com'
if [[ "$EMAIL" != *"$VALIDEMAIL"* ]]
then
echo "YOUR COMMIT(S) HAVE NOT BEEN ACCEPTED"
echo ""
echo "Sorry! Please use your $VALIDEMAIL email account to sent changes to $HOSTNAME"
echo ""
echo "Use the following command on a terminal to update it:"
echo ' git config --global user.email "youremailaddresshere@domain.com"'
exit 1
fi
@mattbell87
Copy link
Author

Only works if SSH/local commit because web is authenticated differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment