Stop a git commit by a specific author using pre-commit hook

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Stop a git commit by a specific author using pre-commit hook

From: Adrian Cornish <hidden>
Date: 2016-06-15 22:53:15

Hi All,

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.

TIA
Adrian

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

Re: Stop a git commit by a specific author using pre-commit hook

From: Neal Kreitzinger <hidden>
Date: 2016-06-15 22:53:16

On 3/8/2012 7:15 PM, Adrian Cornish wrote:
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
We use whoami in our pre-commit hook to see who the user is that is 
doing the commit.  I think you could also use GIT_COMMITTER_NAME or 
linux $USER environment variables.  Either way, the --author seems like 
an unnecessary and unreliable way to get the username.  You're relying 
on the user to remember or obey that step.  By doing --author, you also 
seem to be implying that the git commit author value may not be the same 
as the user doing the commit so that would also ruin the original commit 
author data audit trail.  I haven't used these particular git 
environment variables, but I suspect they are dependent upon the user 
having their gitconfig filled out properly, so the linux whoami and 
$USER are probably more reliable.  If people can su to root then $USER 
will not work because it will still be set to their original user name 
(before they did su to root).  Therefore, "whoami" seems like your best 
solution.

v/r,
neal
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help