Created
February 13, 2018 12:36
-
-
Save fengerzh/ae6031960cff0a3b8d73ee8b849aacf6 to your computer and use it in GitHub Desktop.
Git hook to update local username and email.
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/bash | |
function warn { | |
echo -e "\n$1 Email and author not initialized in local config!" | |
} | |
email="$(git config --local user.email)" | |
name="$(git config --local user.name)" | |
if [[ $1 != "0000000000000000000000000000000000000000" || -n $email || -n $name ]]; then | |
exit 0 | |
fi | |
remote="$([[ $(git remote | wc -l) -eq 1 ]] && git remote || git remote | grep "^origin$")" | |
if [[ -z $remote ]]; then | |
warn "Failed to detect remote." | |
exit 0 | |
fi | |
url="$(git config --local remote.${remote}.url)" | |
if [[ ! -f ~/.git-clone-init ]]; then | |
cat << INPUT > ~/.git-clone-init | |
#!/bin/bash | |
case "\$url" in | |
*@github.com:* ) email=""; name="";; | |
*//github.com/* ) email=""; name="";; | |
esac | |
INPUT | |
warn "\nMissing file ~/.git-clone-init. Template created..." | |
exit 0 | |
fi | |
. ~/.git-clone-init | |
if [[ -z $name || -z $email ]]; then | |
warn "Failed to detect identity using ~/.git-clone-init." | |
exit 0 | |
fi | |
git config --local user.email "$email" | |
git config --local user.name "$name" | |
echo -e "\nIdentity set to $name <$email>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment