Created
April 27, 2012 03:55
-
-
Save nheinric/2505622 to your computer and use it in GitHub Desktop.
My git pre-commit hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use MIME::Base64; | |
my $AGAINST = "HEAD"; | |
`git rev-parse --verify HEAD >/dev/null 2>&1`; | |
$? and $AGAINST = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; # Initial commit | |
my $filename_regex = qr{^\+\+\+ b(.+)}; | |
my $regex_string = MIME::Base64::decode('KD9pKShmdWNrfHNoaXR8YmFsbHN8Y3VudHxjb2NrfHNoZWl0fHNsdXR8d2hvcmV8c3RydW1wZXR8Ym9uZ3xib2xsb2Nrc3xib25lcnxza2VlemV8Z3V6emx8c2thbmt8aG9yc2V8amVzdXN8amV6dXN8Y2hyaXN0fHRpdHMp'); | |
my $four_letter_regex = qr{$regex_string}; | |
my %bad_words; | |
my %trailing_whitespace; | |
my $current_file; | |
open(my $diff, "-|", "git diff --cached --diff-filter=AM -z $AGAINST") | |
or die "Cant get diff output"; | |
while ( <$diff> ) { | |
chomp; | |
/$filename_regex/; | |
$1 and do { $current_file = $1; next }; | |
next unless /^\+/; | |
while ( my $match = $_ =~ /$four_letter_regex/g ) { | |
$bad_words{$current_file}++; | |
} | |
/\s+$/ and $trailing_whitespace{$current_file}++; | |
} | |
close $diff; | |
my $file_is_bad; | |
if ( my @keys = keys %bad_words ) { | |
printf STDERR "%s has %d naughty words!\n" | |
, $_ | |
, $bad_words{$_} | |
foreach @keys; | |
$file_is_bad++; | |
} | |
if ( my @keys = keys %trailing_whitespace ) { | |
printf STDERR "%s has %d lines with trailing whitepace!\n" | |
, $_ | |
, $trailing_whitespace{$_} | |
foreach @keys; | |
$file_is_bad++; | |
} | |
$file_is_bad and die "Blocking commit"; | |
`git diff-index --check --cached $AGAINST --` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment