put git hook on a diet. concentrate on safe, cheap and globally useful
This commit is contained in:
@@ -6,23 +6,24 @@
|
||||
# if it wants to stop the commit.
|
||||
|
||||
use strict;
|
||||
use File::Temp qw/ :mktemp /;
|
||||
use File::Copy;
|
||||
use Cwd;
|
||||
#use File::Copy;
|
||||
#use Cwd;
|
||||
|
||||
$ENV{LC_ALL} = "C";
|
||||
|
||||
sub check_whitespaces($$)
|
||||
sub check_whitespaces($)
|
||||
{
|
||||
my ($h, $src_exts) = @_;
|
||||
my ($h) = @_;
|
||||
my $src_limited = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|MK|pmk|pl|pm|sdi|sh|src|tab|xcu|xml";
|
||||
my $src_full = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|mk|MK|pmk|pl|pm|sdi|sh|src|tab|xcu|xml";
|
||||
|
||||
my $found_bad = 0;
|
||||
my $filename;
|
||||
my $reported_filename = "";
|
||||
my $lineno;
|
||||
sub bad_line {
|
||||
my ($why, $line) = @_;
|
||||
if ($filename =~ /\.($src_exts)$/) {
|
||||
my ($why, $line, $file_filter) = @_;
|
||||
if (!defined $file_filter || $filename =~ /\.($file_filter)$/) {
|
||||
if (!$found_bad) {
|
||||
print STDERR "*\n";
|
||||
print STDERR "* You have some suspicious patch lines:\n";
|
||||
@@ -55,13 +56,13 @@ sub check_whitespaces($$)
|
||||
$lineno++;
|
||||
chomp;
|
||||
if (/\s$/) {
|
||||
bad_line("trailing whitespace", $_);
|
||||
bad_line("trailing whitespace", $_ , $src_limited);
|
||||
}
|
||||
if (/^\s* /) {
|
||||
bad_line("indent SP followed by a TAB", $_);
|
||||
bad_line("indent SP followed by a TAB", $_, $src_limited);
|
||||
}
|
||||
if (/^(?:[<>=]){7}/) {
|
||||
bad_line("unresolved merge conflict", $_);
|
||||
if (/^(?:[<>=]){7}$/) {
|
||||
bad_line("unresolved merge conflict", $src_full);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,35 +94,35 @@ if ( $allownonascii ne "true" &&
|
||||
LC_ALL=C tr -d '[ -~]\\0'` ne "" )
|
||||
{
|
||||
print <<EOM;
|
||||
Error: Attempt to add a non-ascii file name."
|
||||
Error: Attempt to add a non-ascii file name.
|
||||
|
||||
This can cause problems if you want to work"
|
||||
with people on other platforms."
|
||||
This can cause problems if you want to work
|
||||
with people on other platforms.
|
||||
|
||||
To be portable it is advisable to rename the file ..."
|
||||
To be portable it is advisable to rename the file ...
|
||||
|
||||
If you know what you are doing you can disable this"
|
||||
check using:"
|
||||
If you know what you are doing you can disable this
|
||||
check using:
|
||||
|
||||
git config hooks.allownonascii true"
|
||||
git config hooks.allownonascii true
|
||||
|
||||
EOM
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
# check for missing doxygen comments in new files
|
||||
my $doxycheck = "../../bin/find-undocumented-classes";
|
||||
if (! -e $doxycheck) {
|
||||
# bootstrap repo
|
||||
$doxycheck =~ s|../../||;
|
||||
}
|
||||
open(FILES, "git diff-index --cached --name-only --diff-filter=A $against |") || die "Cannot run git diff-index.";
|
||||
while (my $file = <FILES>) {
|
||||
chomp($file);
|
||||
if ($file =~ /\.hxx$/) {
|
||||
system("$doxycheck -q $file | sed 's|".getcwd()."/||;'");
|
||||
}
|
||||
}
|
||||
#my $doxycheck = "../../bin/find-undocumented-classes";
|
||||
#if (! -e $doxycheck) {
|
||||
# # bootstrap repo
|
||||
# $doxycheck =~ s|../../||;
|
||||
#}
|
||||
#open(FILES, "git diff-index --cached --name-only --diff-filter=A $against |") || die "Cannot run git diff-index.";
|
||||
#while (my $file = <FILES>) {
|
||||
# chomp($file);
|
||||
# if ($file =~ /\.hxx$/) {
|
||||
# system("$doxycheck -q $file | sed 's|".getcwd()."/||;'");
|
||||
# }
|
||||
#}
|
||||
|
||||
# run 'msgcat --nowrap' when committing *.po files
|
||||
open(FILES, "git diff-index --cached --name-only $against |") || die "Cannot run git diff-index.";
|
||||
@@ -133,70 +134,8 @@ while (my $file = <FILES>) {
|
||||
}
|
||||
}
|
||||
|
||||
# be strict about tabs - we don't want them at all, setup your editor
|
||||
# correctly ;-)
|
||||
my $err_ext = "";
|
||||
my $src_exts = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|MK|pmk|pl|pm|sdi|sh|src|tab|xcu|xml";
|
||||
|
||||
open( FILES, "git diff-index --cached --name-only $against |" ) || die "Cannot run git diff-index.";
|
||||
while ( my $file = <FILES> ) {
|
||||
chomp( $file );
|
||||
if ( $file ne "GNUmakefile" &&
|
||||
$file =~ /\.($src_exts)$/) {
|
||||
open( F, "git diff-index -p --cached $against -- '$file' |" );
|
||||
while ( my $line = <F> ) {
|
||||
if ( $line =~ /^\+ *\t/ ) {
|
||||
$err_ext .= "$file\n";
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
close( F );
|
||||
}
|
||||
close( FILES );
|
||||
|
||||
if ( $err_ext ne "" ) {
|
||||
print <<EOM;
|
||||
Error: Your change in the following files introduces tabs in indentation:
|
||||
|
||||
$err_ext
|
||||
Please setup your editor not to use tabs, fix the files, and try again.
|
||||
We have had enough trouble with tabs in the past :-(
|
||||
|
||||
EOM
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
# check for old licenses
|
||||
my $check_licenses='^+.*\(Sun Industry Standards Source License Version\|GNU Lesser General Public License Version 2.1\)';
|
||||
my $err_licenses=`git diff-index --cached --name-only $against | while read FILE ; do \
|
||||
if git diff-index -p --cached $against -- "\$FILE" | grep -qs "$check_licenses" ; then \
|
||||
echo "\$FILE" \
|
||||
fi \
|
||||
done`;
|
||||
chomp( $err_licenses );
|
||||
if ( $err_licenses ne "" && $err_licenses ne "git-hooks/pre-commit" ) {
|
||||
print <<EOM;
|
||||
Error: Your change in the following files introduces old licenses:
|
||||
|
||||
$err_licenses
|
||||
|
||||
Please check with the author(s) that they agree with upgrading the
|
||||
license to LGPL3, and change the license accondingly.
|
||||
|
||||
EOM
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
# fix whitespace in code
|
||||
check_whitespaces( $against, $src_exts );
|
||||
|
||||
# check the rest of the files
|
||||
my $filter_patches=`git diff-index --check --cached $against -- | sed '/\.\(diff\|patch\):/,/.*/d'`;
|
||||
chomp( $filter_patches );
|
||||
if ( $filter_patches ne "" ) {
|
||||
print "WARNING:\n\n$filter_patches\n";
|
||||
}
|
||||
check_whitespaces( $against);
|
||||
|
||||
# all OK
|
||||
exit( 0 );
|
||||
|
Reference in New Issue
Block a user