Skip to content

Instantly share code, notes, and snippets.

@egel
Last active March 20, 2017 09:48
Show Gist options
  • Save egel/ca14ecaf73f2f1370942e650676c8368 to your computer and use it in GitHub Desktop.
Save egel/ca14ecaf73f2f1370942e650676c8368 to your computer and use it in GitHub Desktop.
Block to commit if user's email not match
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
WHITE_LIST=''
accept_email() {
CURRENT_USER_EMAIL=$(git config --list | grep user.email | awk -F"=" '{print $2}')
substring="$1"
if [ "${CURRENT_USER_EMAIL}" != "${substring}" ]; then
printf "Not match" > /dev/null
WHITE_LIST="${WHITE_LIST}\n> ${substring}"
else
exit 0
fi
}
# Change for your accepted email addresses
accept_email "johndoe@example.com"
accept_email "john.doe@example.com"
printf "Your current git user.email <%s> NOT MATCH any approved emails\n" "${CURRENT_USER_EMAIL}"
printf "%b\n\n" "${WHITE_LIST}"
printf "Check ~/.gitconfig for more\n"
exit 1
@egel
Copy link
Author

egel commented Mar 20, 2017

Usage download and save into .git/hooks/pre-commit:

Change example email addresses for your own.

cd <your project git root directory>
wget https://gist.githubusercontent.com/egel/ca14ecaf73f2f1370942e650676c8368/raw/39059e596b44b38b1df5c5d4f973ef22a0e27d72/pre-commit -P ".git/hooks"
chmod +x .git/hooks/pre-commit

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