Skip to content

Instantly share code, notes, and snippets.

@raido
Created May 10, 2017 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raido/afdef127f0396438093976a99491b0a5 to your computer and use it in GitHub Desktop.
Save raido/afdef127f0396438093976a99491b0a5 to your computer and use it in GitHub Desktop.
Git pre-commit hook to check if commit is allowed with current user.email
#!/bin/sh
# Retrieve author information as Git sees it while commiting
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1
NAME=$(printf '%s\n\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p')
EMAIL=$(printf '%s\n\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p')
REMOTE_ORIGIN_URL=$(git remote get-url origin)
# If we're trying to commit to repo with not allowed email
if [[ $REMOTE_ORIGIN_URL != *"<REPLACE_ME>"* ]] && [[ $EMAIL == *"<REPLACE_ME>"* ]]; then
printf "NAME: %s\n" "${NAME}"
printf "EMAIL: %s\n" "${EMAIL}"
printf "REMOTE URL: %s\n" "${REMOTE_ORIGIN_URL}"
echo "\nOh, please stop. I cannot allow you to commit with your current email: ${EMAIL}\n"
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment