Created
July 1, 2022 08:12
-
-
Save aleksul/69abe1477c9db270d8055ee814928e81 to your computer and use it in GitHub Desktop.
Git pre-commit hook to verify your email in public/work repository
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/bash | |
# Place it in ~/.githooks/pre-commit & configure your emails and domains | |
# Then run: | |
# git config --global core.hooksPath ~/.githooks | |
public_email="[email protected]" | |
public_domain="github.com" | |
work_email="[email protected]" | |
work_domain="your.work.domain" | |
origin_url=$(git remote get-url origin) | |
origin_domain="" | |
exit_code=0 | |
get_domain() { | |
local protocol=$(echo "${origin_url}" | sed 's/\([a-z]*\):.*/\1/') | |
if [[ $protocol == "https" ]]; then | |
origin_domain=$(echo "${origin_url}" | sed 's/.*\/\/\(.*\)\/.*\/.*/\1/') | |
else | |
origin_domain=$(echo "${origin_url}" | sed 's/.*git@\(.*\):.*/\1/') | |
fi | |
} | |
verify_user(){ | |
local email=$(git config user.email) | |
case ${origin_domain} in | |
${public_domain}) | |
if [[ $email != $public_email ]]; then | |
echo "email not configured to personal email" | |
echo "run:" | |
echo " git config user.email ${public_email}" | |
echo "" | |
exit_code=1 | |
fi | |
;; | |
${work_domain}) | |
if [[ $email != $work_email ]]; then | |
echo "email not configured to work email" | |
echo "run:" | |
echo " git config user.email ${work_email}" | |
echo "" | |
exit_code=1 | |
fi | |
;; | |
*) | |
printf "%b" "\e[33m WARNING: no git user has been set for this domain '${origin_domain}' in the pre-commit hook \e[39m\n" | |
;; | |
esac | |
} | |
get_domain | |
verify_user | |
exit $exit_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment