Git commit hooks

I recently wrote/extended a couple of git commit hooks which have turned out to be rather useful. Once is a fairly standard commit-msg hook to check the format of the first line of a commit message is either "Release version x.y.z", "[tag] Message" or "Bug 123456 — Title":

tags="core|atom|gd|media|build|docs|tests|calendar|contacts|documents|picasaweb|youtube"
test "" != "$(grep -P '^(Release version [0-9]+.[0-9]+.[0-9]+|\[('$tags')$$ [\S].+[\S]|Bug [0-9]+ — [\S].+[\S])$' "$1")" || {
 echo -e >&2 "First line of commit message should be in one of the following formats:\n * 'Bug 123456 — Title'\n * '[tag] Message'\
 \n * 'Release version x.y.z'\nValid tags are:\n * ${tags//|/\n * }"
 exit 1
}

Just define the tags your project allows in the tags variable, separated only by pipes.

The other hook is a pre-commit hook to ensure any new API has a Since: tag if it's got a gtk-doc comment. It's implemented as a modification of the sample pre-commit hook, so here's the diff:

--- /home/philip/Downloads/4m8QcjYs.txt    2010-03-28 19:14:50.432440248 +0100
+++ pre-commit    2010-03-22 20:37:47.612794352 +0000
 my $filename;
 my $reported_filename = "";
 my $lineno;
+    my $in_gtk_doc_comment = 0;
+    my $used_since_tag = 0;
 sub bad_line {
 my ($why, $line) = @_;
 if (!$found_bad) {
@@ -64,6 +68,19 @@
 if (/^([<>])\1{6} |^={7}$/) {
 bad_line("unresolved merge conflict", $_);
 }
+        if (/^\s*\/\*\*$/) {
+        $in_gtk_doc_comment = 1;
+        $used_since_tag = 0;
+        }
+        if ($in_gtk_doc_comment && /^\s*\*\*\/$/) {
+        $in_gtk_doc_comment = 0;
+        if (!$used_since_tag) {
+            bad_line("new gtk-doc commented API without a Since: tag", $_);
+        }
+        }
+        if ($in_gtk_doc_comment && /\*\s+Since:\s+[0-9]+.[0-9]+.[0-9]+$/) {
+        $used_since_tag = 1;
+        }
 }
 }
 exit($found_bad);

It looks for the start of a gtk-doc comment block in any added code, and errors if it doesn't find a Since: tag before the end.

Hopefully these'll be useful to somebody. They've both turned out to be more useful to me than I'd hoped, so hopefully my commit messages make a little more sense now. The code for both of them is in the public domain.

3 thoughts on “Git commit hooks

  1. Pingback: Tweets that mention Git commit hooks — drboblog -- Topsy.com

Comments are closed.