15

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
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • 1
    It's surprising there doesn't seem to be a way to get at this information. I can confirm that `git var GIT_AUTHOR_IDENT` shows the original author, not the one provided via `--author`... – Borealid Mar 09 '12 at 00:42
  • Tangentially also https://gist.github.com/tripleee/16767aa4137706fd896c – tripleee Jun 09 '14 at 12:32

2 Answers2

12

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="your@email.com" 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.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
  • Thanks this does exactly as I wanted. I tried git var on GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL and it was always blank - this explains why. – Adrian Cornish Mar 09 '12 at 14:49
  • 1
    This is not correct anylonger, seems to work with GIT 2.7.4 (mabye also earlier). Putting `echo $GIT_AUTHOR_NAME` in a `pre-commit` hook prints out the author name (either the one set in git config or the one passed via `--author`). Also `git var GIT_AUTHOR_IDENT` seems to work with `--author`. – lumbric Mar 10 '18 at 19:06
  • Looks like support was added with this commit, so would be present in 1.7.11 and later https://github.com/git/git/commit/7dfe8ad6006c105989e4e8cfb196aa4ecda3c21d – Jason Viers Apr 01 '20 at 18:33
  • So what's the new method? – theonlygusti Mar 26 '21 at 10:48
0

I used the following, adding this to the systems .bashrc.

It won't catch folk who actually su to root and live in that shell, (bad!) But it does keep my logs useful when folk just use sudo. I too am trying to keep a /etc changelog with git - so I can see what's been done month-by-month.

#I want everyone to check in changes to /etc files, but also want their names even when they use sudo.
export GIT_COMMITTER_EMAIL=${USER}@ourcompany.co.nz
export GIT_AUTHOR_EMAIL=${USER}@ourcompany.co.nz

https://serverfault.com/questions/256754/correct-user-names-when-tracking-etc-in-git-repository-and-committing-as-root

Community
  • 1
  • 1
dman
  • 1,089
  • 13
  • 9