Skip to content

Instantly share code, notes, and snippets.

@fengerzh
Created February 13, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengerzh/ae6031960cff0a3b8d73ee8b849aacf6 to your computer and use it in GitHub Desktop.
Save fengerzh/ae6031960cff0a3b8d73ee8b849aacf6 to your computer and use it in GitHub Desktop.
Git hook to update local username and email.
#!/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