My plan is to use git to keep track of changes in /etc but when committing I want to have the person making the change specify themselves as author by adding the –author option on the commandline.
So I would like to stop accidental commits as root.
I tried creating this pre-commit hook but it is not working – git var is still returning root even if I specify the author on commit line.
AUTHOR=`git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/\1/p'`
if [ "$AUTHOR" == "root <root@localhost>" ];
then
echo "Please commit under your own user name instead of \"$AUTHOR\":"
echo 'git commit --author="Adrian"'
echo "or if your name is not already in logs use full ident"
echo 'git commit --author="Adrian Cornish <a@localhost>"'
exit 1
fi
exit 0
Until v1.7.10.1 (released 2012-05-01), Git did not make
--authorinformation available to Git hooks via environment variables, command-line arguments, or stdin. However, instead of requiring the use of the--authorcommand line, you can instruct users to set theGIT_AUTHOR_NAMEandGIT_AUTHOR_EMAILenvironment variables:Like the
--authorargument, these environment variables control the commit’s author. Because these environment variables are in Git’s environment, they’re also in the environment of thepre-commithook. And because they’re in the environment of thepre-commithook, they’re passed togit var GIT_AUTHOR_IDENTwhich uses them likegit commitdoes.Unfortunately, setting these variables is much less convenient than using
--author. If you can, upgrade to v1.7.10.1 or later.