Question

It seems there is no simple way to get authors and committer IDs in a Git pre-commit hook (Missing git hooks documentation).

I can only find examples for post-commit hooks which, for example, look for spelling mistakes and then suggest

If there are errors, you can immediately fix the commit message by running git commit --amend.

which seems kind of too late.

Is there really no way to get this information in a pre-commit hook?

Was it helpful?

Solution

You can't. The commit does not yet exist so there's nothing to get.

You can come pretty close: use the same rules that git commit uses to pick a default author and committer, i.e., look for GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME, and GIT_COMMITTER_EMAIL in the environment, and if not set, use git config --get user.name, etc., to get the settings. But this does not seem to capture any --author= argument to a git commit command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top