Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which git hook to use for validating pushed commit messages?

Tags:

git

githooks

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

like image 200
bcmcfc Avatar asked Jul 04 '15 07:07

bcmcfc


1 Answers

Policy enforcement means server-side hook.

  • A client-side hook like a pre-commit can be bypassed, and is not easy to deploy.
  • A server-side is deployed once (on the Git repo hosting server), and you are done.

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?".

like image 167
VonC Avatar answered Oct 13 '22 11:10

VonC