Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a git commit message in commit-msg hook?

Tags:

git

bash

githooks

I wrote a script

#!/bin/bash
commit_msg=$1
echo "Your commit message: $commit_msg"

in hooks/commit-msg which validates the commit message in

git commit -m "fixed a bug"

But when I run the hook, I have:

Your commit message: .git/COMMIT_EDITMSG

instead of

Your commit message: fixed a bug

How can I capture the commit message into a variable?

I've read How to capture a git commit message and run an action but it didn't help me because that hook was for post-receive and I need it for commit-msg so I don't have my commit message in

git log -1 HEAD --pretty=format:%s

because my hook blocks from doing a commit.

like image 579
maro Avatar asked Jan 16 '18 15:01

maro


1 Answers

From the docs:

The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer

Therefore, you need to read the contents of the given file to provide the message:

commit_msg=$(cat "${1:?Missing commit message file}")
like image 75
bishop Avatar answered Sep 23 '22 17:09

bishop