Reputation: 26805
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
Upvotes: 6
Views: 7051
Reputation: 1327634
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?".
Upvotes: 7
Reputation: 4061
You are right, a update hook wouldn't work. If you need to enforce a commit message standard, you should use a pre-commit hook. It will run before a commit is made and make sure it the commit is standard and then go ahead and create the commit. Here is a pre-commit hook sample
https://github.com/git/git/blob/master/templates/hooks--pre-commit.sample
For more on git hooks check this
Upvotes: 0