0

So I have a git repository that is a bit unusual, in that multiple people work on the same repository, using sudo. (The goal in the long run is to get away from this, but for now, we're only taking small steps in improving our practices.)

Of course, that makes identifying who made each commit hard, but I have identified a sensible solution using environment variables (GIT_AUTHOR_NAME etc.).

To 'force' my colleagues into using this, I would like to add some automated verification in one of the hooks that runs before a commit is made:

  • pre-commit
  • prepare-commit-msg
  • commit-msg

However, I cannot seem to find any way to find this information during these hooks. I could of course try to reinvent the wheel and go looking for environment variables and such, but I would very much prefer to not do this.

So does anyone know if there's a way to ask git for the author/committer names and emails that it intends to use for a staged commit? Or is that information only added to the commit after the pre-commit hook?

Falc
  • 123
  • 2
  • 2
    Whose insane idea was that original setup? I can't imagine that any of your developers like it or want it to stick around any longer than necessary as it likely does not work well (or at all) with any of their tools. Consider getting rid of it ASAP. – Michael Hampton Feb 08 '21 at 13:15
  • It's actually for our bind zone files, not for any development. – Falc Feb 08 '21 at 15:23
  • 2
    Even more reason to forget these workarounds and scrap it ASAP. Your zone files are _important_. – Michael Hampton Feb 08 '21 at 15:25
  • 2
    I would also recommend of not doing it like you are trying to do. Better setup a repo and a post-receive/commit hook that pushes it to where it is supposed to go. Then you will have better resilience AND know how pushed the changes. – Fredrik Feb 08 '21 at 22:15

2 Answers2

0

Environment variables are actually the primary way to pass optional parameters and configuration to the hooks. So there is nothing wrong with just reading GIT_AUTHOR_NAME from the environment in those hooks.

When git is called with one of those environment variables set it will use them to set the author information. If not however git will read that from the repo or global configuration and set the environment variables appropriately before calling the hooks.

acran
  • 216
  • 1
  • 5
0

For committer details, it turns out the correct approach is to use git var GIT_COMMITTER_IDENT.

For author details, @acran's answer is correct and will thus get accepted.

Falc
  • 123
  • 2