Created
June 19, 2012 11:59
-
-
Save LordGaav/2953751 to your computer and use it in GitHub Desktop.
Git hook that determines the author based on the email address in a SSH key. Doesn't work because Git doesn't check the config between pre-commit and commit.
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/sh | |
# Retrieve author information as Git sees it while commiting | |
AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1 | |
NAME=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p') | |
EMAIL=$(printf '%s\n' "${AUTHORINFO}" | sed -n 's/^.* <\(.*\)> .*$/\1/p') | |
printf "AUTHORINFO: %s\n" "${AUTHORINFO}" | |
printf "NAME: %s\n" "${NAME}" | |
printf "EMAIL: %s\n" "${EMAIL}" | |
# Retrieve author information of the Repo user | |
REPONAME=$(git config --get repouser.name) | |
REPOEMAIL=$(git config --get repouser.email) | |
printf "REPONAME: %s\n" "${REPONAME}" | |
printf "REPOEMAIL: %s\n" "${REPOEMAIL}" | |
# If the current configured user is not the Repo user | |
if [ "${NAME}" != "${REPONAME}" ] || [ "${EMAIL}" != "${REPOEMAIL}" ]; then | |
# Reset the Repo user | |
git config user.name "${REPONAME}" | |
git config user.email "${REPOEMAIL}" | |
echo "Config reset!" | |
fi |
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/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') | |
printf "AUTHORINFO: %s\n" "${AUTHORINFO}" | |
printf "NAME: %s\n" "${NAME}" | |
printf "EMAIL: %s\n" "${EMAIL}" | |
# Retrieve author information of the Repo user | |
REPONAME=$(git config --get repouser.name) | |
REPOEMAIL=$(git config --get repouser.email) | |
printf "REPONAME: %s\n" "${REPONAME}" | |
printf "REPOEMAIL: %s\n" "${REPOEMAIL}" | |
# If we're trying to commit as the Repo user | |
#if [ "${NAME}" = "${REPONAME}" ] || [ "${EMAIL}" = "${REPOEMAIL}" ]; then | |
while [ 1 ]; do | |
# Check if we can access the SSH key and derive a previous committer from that | |
SSHUSER=$(ssh-add -l 2>/dev/null| cut -d" " -f3 | cut -d"@" -f1 | cut -d"+" -f1) | |
GITCOMMIT=$(git rev-list --all -i --author=${SSHUSER} | head -n1) | |
printf "SSHUSER: %s\n" "${SSHUSER}" | |
printf "GITCOMMIT: %s\n" "${GITCOMMIT}" | |
if [ -z "$SSHUSER" ] || [ -z "$GITCOMMIT" ]; then | |
echo "Could not determine Git user from SSH key" | |
break; | |
fi | |
GITUSER=$(git show -s --pretty=format:%aN ${GITCOMMIT}) | |
GITEMAIL=$(git show -s --pretty=format:%aE ${GITCOMMIT}) | |
printf "GITUSER: %s\n" "${GITUSER}" | |
printf "GITEMAIL: %s\n" "${GITEMAIL}" | |
if [ -z "$GITUSER" ] || [ -z "$GITEMAIL" ]; then | |
echo "Could not read previous Git commit for SSH user ${SSHUSER}" | |
break; | |
fi | |
# Set the found values | |
# Note that the post-commit hook will reset these config options | |
# to the Repo user after committing | |
git config user.name "${GITUSER}" | |
git config user.email "${GITEMAIL}" | |
echo "Config set!" | |
exit 0 | |
done | |
echo "Fail!" | |
# If all fails, show error message with solution | |
cat <<EOF >&2 | |
Please commit under your own name and email instead of "${NAME} <${EMAIL}>": | |
GIT_AUTHOR_NAME="Your Name" GIT_AUTHOR_EMAIL="[email protected]" git commit | |
EOF | |
exit 2 | |
#fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment