Skip to content

Instantly share code, notes, and snippets.

@schnell18
Last active December 31, 2015 12:09
Show Gist options
  • Save schnell18/7983974 to your computer and use it in GitHub Desktop.
Save schnell18/7983974 to your computer and use it in GitHub Desktop.
Git update hook to validate comment starts with ticket number. It also serves as VREF so that it can work with gitolite without overwrite gitolite's fine-grained access control.
#!/usr/bin/perl
use strict;
my $refname = $ARGV[0];
my $oldrev = $ARGV[1];
my $newrev = $ARGV[2];
my $rc = 0;
my $cmd = "git rev-list ";
if ($oldrev =~ /^0+$/) {
$cmd .= "${newrev}";
}
elsif ($newrev =~ /^0+$/) {
$cmd .= "${oldrev}";
}
else {
$cmd .= "${oldrev}..${newrev}";
}
open(OUT1, "$cmd |") or do {
warn "Can't run git rev-list: $!\n";
exit 2;
};
while(<OUT1>) {
chomp;
my $sha1 = $_;
my ($good, $cmt) = good_commit_message($sha1);
if (!$good) {
print "VREF/InvlidTicket\n";
print "Bad commit: $sha1\n";
print "Comment: [$cmt] does not start with ticket\n";
$rc = 1;
last;
}
}
close(OUT1);
exit($rc);
sub good_commit_message {
my ($rev) = @_;
my $comment;
my $cmd = "git cat-file commit $rev";
open(OUT2, "$cmd |") or do {
warn "Can't run git cat-file $!\n";
exit 3;
};
while(<OUT2>) {
chomp;
next unless /^$/;
$comment = <OUT2>;
chomp($comment);
last;
}
close(OUT2);
return (1, undef) if $comment =~ /^[A-Z]+-[0-9]+: /;
return (0, $comment);
}
# vim: set ai nu expandtab nobk sw=4 ts=4 syntax=perl:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment