I need to validate the commit messages that are pushed to the remote in order to prevent developers from not putting enough detail in (string length) or only putting a ticket number.
I thought an update hook would work for this but it doesn't- it seemed to, but it only works for a reference that's been pushed previously. When I tried to push a new branch it rejected because it couldn't find the ref. I suspect it may also only be running against the latest commit in a pushed series.
What would be the right choice of hook to perform this task?
Snippet:
#!/usr/bin/env php
<?php
define('MINIMUM_MESSAGE_LENGTH', 10);
$exit = 0; // default exit code -> success
$ref = $argv[1];
$commitMessage = exec('git log -1 ' . $ref . ' --pretty=format:%s');
$commitMessage = trim($commitMessage);
// validations & exit($exit) follow;
yep this is PHP but the question is language agnostic
Policy enforcement means server-side hook.
pre-commit
can be bypassed, and is not easy to deploy.The Git Pro book has an example ("Customizing Git - An Example Git-Enforced Policy") which should work for new branches as well as legacy branches pushes.
You will find other examples at "How do we verify commit messages for a push?".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With