Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pre receive hook to check commit message

I am trying to write a pre-receive hook to check the pattern of the commit messages using bash/shell.

I want to reject the entire push if any commit has issues. How to retrieve the commit messages?

like image 627
l a s Avatar asked Jun 28 '26 00:06

l a s


1 Answers

There is an entire example, with explanations, in the git docs, that covers this. Link to the example.

Roughly translating the ruby example, we have:

#!/bin/bash

set -eo pipefail

refname="$0"
oldrev="$1"
newrev="$2"

echo "Enforcing Policies..."

# Iterate over all the commits
for commit in $(git rev-list 538c33..d14fc7); do
  git cat-file commit "${commit}" | sed '1,/^$/d' | your-validator
done
like image 80
ffledgling Avatar answered Jun 30 '26 14:06

ffledgling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!