1

So if .git/hooks/pre-commit exists, then it will be run right after:

git commit -am "foobar"

is run. So then how do I access the desired git commit message in the pre-commit?

I do some sleuthing and put this in .git/hooks/pre-commit and made the file executable:

#!/usr/bin/env bash

echo "here is pre-commit hook"

env | sort

echo "args:"

echo $@

exit 1;

here are the env vars that are prefixed with "GIT"

GIT_AUTHOR_DATE=@1558245421 -0700
GIT_AUTHOR_EMAIL=alex@alexs-mac.local
GIT_AUTHOR_NAME=Alexander Mills
GIT_EDITOR=:
GIT_EXEC_PATH=/Library/Developer/CommandLineTools/usr/libexec/git-core
GIT_INDEX_FILE=/Users/alex/codes/interos/notifier-server/.git/index.lock
GIT_PREFIX=

and there seem to be no arguments to the script, because nothing gets logged after echo "args:"...

so how do I access the desired commit message in a pre-commit hook?

Specifically I am looking to bail from pre-commit if the commit-message does not have a good format. If there is another hook that can do that, then that would be a good answer to this question.

  • Not precisely a dupe because the other question is a bit vague? – Alexander Mills May 19 '19 at 06:40
  • As it's said in the dup pre-commit hook doesn't have access to the commit message, {prepare-}commit-msg hooks do. – phd May 19 '19 at 12:13
  • @AlexanderMills: the accepted answer points out the problem: in a pre-commit hook *there is no commit message yet!* The message is not created until after the pre-commit hook says that it's OK to proceed to next step. – torek May 19 '19 at 18:08

1 Answers1

0

Not the most ideal git hooks situation - it would be nice if it were an env set and not an argument (git is not very thread-safe)

so forget about .git/hooks/pre-commit and instead use .git/hooks/commit-msg, and in there you might have:

#!/usr/bin/env bash

cat $1
cat .git/COMMIT_EDITMSG

# the two above lines are the same

that is the git commit message that is still pending a commit.