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, 23:03 UTC
Message-ID
<4F5BDDAC.4050700@gmail.com>
In-Reply-To
<7vboo415rn.fsf@alter.siamese.dyndns.org>
On 3/10/2012 3:54 PM, Junio C Hamano wrote:
Show 32 quoted lines
> Neal Kreitzinger<nkreitzinger@gmail.com>  writes:
>
>> 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.
>> ...
>> 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.
>> ...  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.
> When people want to raise a red flag against a commit made by root,
> they are coming from two different schools.
>
> One is "do not run 'git' or any development tool for that matter
> while being root". It is a good discipline to follow in general to
> limit what you do with escalated privilege to the minimum.
>
> The other is "record who actually did the work, not 'root' that
> people cannot later track down who it actually was".  People from
> this school do not mind running development tools as root.
>
> And your advice is a good one for the former, but not very relevant
> for the latter.
>
> And I think Adrian is asking for the latter.
>

Now I see. In that case, at the point pre-commit is run git has not overriden GIT_AUTHOR_IDENT with your --author value, yet. I don't know if that is a bug or not. The prepare-commit-msg hook is the same way. However, by the time the commit-msg hook runs git has placed your --author override into GIT_AUTHOR_IDENT so if you check it there it will work and you can abort the commit. Of course, at that point the user has already typed their commit message and may lose it. You could create a git alias like "git root-commit" that prompts them to enter their authorname and then runs git-commit --author with that user provided value. Pre-commit hook could detect root user and error out telling them to run git-root-commit instead.

v/r, neal

Previous: Junio C HamanoNext: Junio C Hamano
Message 4 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.