-
-
Save hardenchant/4ead865f2513b38a335b4a14482759f8 to your computer and use it in GitHub Desktop.
#git pre-commit #hook to set commit email address based on the repo's remote domain(s)
This file contains hidden or 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/zsh | |
# source: https://gist.github.com/dreness/2de62e01d2053f9440eb | |
# Set repo's user.email to a default or alternate value based on remote domain. | |
# Because the in-flight commit info has already been set, if an email address | |
# change is needed, this commit is aborted. Retry the commit to use the new address. | |
# | |
# To automatically install custom hooks to new repos: | |
# 1) Add something like the following to ~/.gitconfig: | |
# [init] | |
# templatedir = ~/.git/templates | |
# 2) Create a directory at ~/.git/templates/hooks and install this file there | |
# Use 'git init' to add to existing repos | |
# If this repo's remote domain contains the string WORK_DOMAIN, set user.email | |
# to WORK_EMAIL, otherwise use DEFAULT_EMAIL. | |
# it sets at bashrc | |
# WORK_DOMAIN='' | |
# WORK_EMAIL='' | |
# WORK_SIGN_FINGERPRINT='' | |
DEFAULT_EMAIL=$(git config --global user.email) | |
# Current repo's configured email address and remotes | |
EMAIL=$(git config --local user.email) | |
REMOTES="$(git remote -v)" | |
# If this repo has a local user.email setting, do nothing. This allows for | |
# manually setting user.email instead of using this script. | |
if [[ -n ${EMAIL} ]]; then | |
exit 0 | |
fi | |
if [[ "${REMOTES}" == *${WORK_DOMAIN}* ]]; then | |
# This repo has a remote in WORK_DOMAIN | |
echo "Setting user.email to ${WORK_EMAIL}. Retry." | |
git config --local user.email "${WORK_EMAIL}" | |
git config --local user.signingkey $WORK_SIGN_FINGERPRINT | |
git config --local commit.gpgsign true | |
echo "GPG signing activated." | |
exit 1 | |
else | |
# This repo does NOT have a remote in WORK_DOMAIN | |
echo "Setting user.email to ${DEFAULT_EMAIL}. Retry" | |
git config --local user.email "${DEFAULT_EMAIL}" | |
exit 1 | |
fi | |
SCRIPT_PATH=$0:A | |
printf "ERROR in pre-commit hook: %s\nCan't configure email address?! \n" ${SCRIPT_PATH} | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment