Stop a git commit by a specific author using pre-commit hook
Until v1.7.10.1 (released 2012-05-01), Git did not make --author
information available to Git hooks via environment variables, command-line arguments, or stdin. However, instead of requiring the use of the --author
command line, you can instruct users to set the GIT_AUTHOR_NAME
and GIT_AUTHOR_EMAIL
environment variables:
#!/bin/sh
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')
[ "${NAME}" != root ] && [ "${EMAIL}" != "root@localhost" ] || {
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 1
}
Like the --author
argument, these environment variables control the commit's author. Because these environment variables are in Git's environment, they're also in the environment of the pre-commit
hook. And because they're in the environment of the pre-commit
hook, they're passed to git var GIT_AUTHOR_IDENT
which uses them like git commit
does.
Unfortunately, setting these variables is much less convenient than using --author
. If you can, upgrade to v1.7.10.1 or later.