#!/usr/bin/env perl # A hook script to verify what is about to be committed. # Called by "git commit" with no arguments. The hook should # exit with non-zero status after issuing an appropriate message # if it wants to stop the commit. use strict; use File::Temp qw/ :mktemp /; use File::Copy; use Cwd; $ENV{LC_ALL} = "C"; sub check_whitespaces($) { my ($h) = @_; my $found_bad = 0; my $filename; my $reported_filename = ""; my $lineno; sub bad_line { my ($why, $line) = @_; if (!$found_bad) { print STDERR "*\n"; print STDERR "* You have some suspicious patch lines:\n"; print STDERR "*\n"; $found_bad = 1; } if ($reported_filename ne $filename) { print STDERR "* In $filename\n"; $reported_filename = $filename; } print STDERR "* $why (line $lineno)\n"; print STDERR "$filename:$lineno:$line\n"; } open( FILES, "git-diff-index -p -M --cached $h |" ) || die "Cannot run git diff-index."; while () { if (m|^diff --git a/(.*) b/\1$|) { $filename = $1; next; } if (/^@@ -\S+ \+(\d+)/) { $lineno = $1 - 1; next; } if (/^ /) { $lineno++; next; } if (s/^\+//) { $lineno++; chomp; if (/^(?:[<>=]){7}/) { bad_line("unresolved merge conflict", $_); } } } if ( $found_bad) { exit($found_bad); } } # Do the work :-) # Initial commit: diff against an empty tree object my $against="4b825dc642cb6eb9a060e54bf8d69288fbee4904"; if ( system( "git rev-parse --verify HEAD >/dev/null 2>&1" ) == 0 ) { $against="HEAD" } # fix whitespace in code check_whitespaces( $against ); # all OK exit( 0 ); # vi:set shiftwidth=4 expandtab: