Skip to content

Instantly share code, notes, and snippets.

@swaldman3
Created October 7, 2012 15:19
Show Gist options
  • Save swaldman3/3848642 to your computer and use it in GitHub Desktop.
Save swaldman3/3848642 to your computer and use it in GitHub Desktop.
Git hooks - to go in .git/hooks/
#!/bin/sh
if diff "$1" "$1.tmp" > /dev/null; then
echo >&2 Aborting commit due to unchanged commit message.
rm "$1.tmp"
exit 1
else
rm "$1.tmp"
fi
#!/usr/bin/perl
#
# Pre-commit hook to look at the current branch, see if it starts with a
# number followed by - or _, and if so use that number to start the first
# line of the commit message with "Bug (number) : ".
use warnings;
use strict;
my ($filename, $source, $SHA1) = @ARGV;
# $source theoretically gives info on whether this is a merge commit, etc.
# I haven't done anything with this, but probably should. See "git help hooks" for
# full info.
unless ($filename) {
print "No filename";
exit 0;
}
my $branchname = `git symbolic-ref --short -q HEAD`;
exit 0 unless $branchname;
my $bugnumber = undef;
if ( $branchname =~ /^([0-9]+)[-|_]/ ) {
$bugnumber = $1;
}
exit 0 unless $bugnumber;
exit 0 unless open( FILE, "< $filename" );
my @lines = <FILE>;
close( FILE );
exit 0 unless open( FILE, "> $filename" );
print FILE "(Bug $bugnumber) : ";
print FILE @lines;
close( FILE );
#copy the original file to a temporary one so that we can compare the two
# in the commit-msg hook to see if unchanged - if unchanged, should
# abort commit.
my $tmpfilename = $filename . ".tmp";
system( "cp $filename $tmpfilename\n" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment