git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
NKNeal Kreitzinger <nkreitzinger@gmail.com>
Date
Mar 10, 2012, 19:41 UTC
Message-ID
<4F5BAE5E.5030407@gmail.com>
In-Reply-To
<CAGc=MuCQoQ711uXnB8J80RqsY+pqbaeCOXHjy0-mjq7a_ESdyA@mail.gmail.com>
On 3/8/2012 7:15 PM, Adrian Cornish wrote:
Show 19 quoted lines
> 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

Previous: Adrian CornishNext: Junio C Hamano
Message 2 of 13 in “Stop a git commit by a specific author using pre-commit hook”
  1. Adrian CornishMar 9, 2012
  2. Neal KreitzingerMar 10, 2012
  3. Junio C HamanoMar 10, 2012
  4. Neal KreitzingerMar 10, 2012
  5. Junio C HamanoMar 11, 2012
  6. 3/4 test: does pre-commit-hook learn authorship?Junio C Hamano, Mar 11, 2012
  7. Johannes SixtMar 11, 2012
  8. Junio C HamanoMar 11, 2012
  9. 3/4 ident.c: add split_ident_line() to parse formatted ident lineJunio C Hamano, Mar 11, 2012
  10. 3/3 commit: pass author/committer info to hooksJunio C Hamano, Mar 11, 2012
  11. Jeff KingMar 12, 2012
  12. 3/4 run_hook(): enhance the interface to pass arbitrary environmentJunio C Hamano, Mar 11, 2012
  13. 4/4 commit: pass author/committer info to hooksJunio C Hamano, Mar 11, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.