disable sd filters test for now
[LibreOffice.git] / git-hooks / pre-commit
blob2e1ebf8212f22eddbe7b948c9d5747b1787c4053
1 #!/usr/bin/env perl
3 # A hook script to verify what is about to be committed.
4 # Called by "git commit" with no arguments. The hook should
5 # exit with non-zero status after issuing an appropriate message
6 # if it wants to stop the commit.
8 use strict;
9 #use File::Copy;
10 #use Cwd;
12 $ENV{LC_ALL} = "C";
14 sub check_whitespaces($)
16 my ($h) = @_;
17 my $src_limited = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|MK|pmk|pl|pm|sdi|sh|src|tab|xcu|xml";
18 my $src_full = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|map|mk|MK|pmk|pl|pm|sdi|sh|src|tab|xcu|xml";
20 my $found_bad = 0;
21 my $filename;
22 my $reported_filename = "";
23 my $lineno;
24 sub bad_line {
25 my ($why, $line, $file_filter) = @_;
26 if (!defined $file_filter || $filename =~ /\.($file_filter)$/) {
27 if (!$found_bad) {
28 print STDERR "*\n";
29 print STDERR "* You have some suspicious patch lines:\n";
30 print STDERR "*\n";
31 $found_bad = 1;
33 if ($reported_filename ne $filename) {
34 print STDERR "* In $filename\n";
35 $reported_filename = $filename;
37 print STDERR "* $why (line $lineno)\n";
38 print STDERR "$filename:$lineno:$line\n";
41 open( FILES, "git-diff-index -p -M --cached $h |" ) || die "Cannot run git diff-index.";
42 while (<FILES>) {
43 if (m|^diff --git a/(.*) b/\1$|) {
44 $filename = $1;
45 next;
47 if (/^@@ -\S+ \+(\d+)/) {
48 $lineno = $1 - 1;
49 next;
51 if (/^ /) {
52 $lineno++;
53 next;
55 if (s/^\+//) {
56 $lineno++;
57 chomp;
58 if (/\s$/) {
59 bad_line("trailing whitespace", $_ , $src_limited);
61 if (/^\s* /) {
62 bad_line("indent SP followed by a TAB", $_, $src_limited);
64 if (/^(?:[<>=]){7}$/) {
65 bad_line("unresolved merge conflict", $src_full);
69 if ( $found_bad)
71 exit($found_bad);
75 # Do the work :-)
77 # Initial commit: diff against an empty tree object
78 my $against="4b825dc642cb6eb9a060e54bf8d69288fbee4904";
79 if ( system( "git rev-parse --verify HEAD >/dev/null 2>&1" ) == 0 ) {
80 $against="HEAD"
83 # If you want to allow non-ascii filenames set this variable to true.
84 my $allownonascii=`git config hooks.allownonascii`;
86 # Cross platform projects tend to avoid non-ascii filenames; prevent
87 # them from being added to the repository. We exploit the fact that the
88 # printable range starts at the space character and ends with tilde.
89 if ( $allownonascii ne "true" &&
90 # Note that the use of brackets around a tr range is ok here, (it's
91 # even required, for portability to Solaris 10's /usr/bin/tr), since
92 # the square bracket bytes happen to fall in the designated range.
93 `git diff --cached --name-only --diff-filter=A -z $against | \
94 LC_ALL=C tr -d '[ -~]\\0'` ne "" )
96 print <<EOM;
97 Error: Attempt to add a non-ascii file name.
99 This can cause problems if you want to work
100 with people on other platforms.
102 To be portable it is advisable to rename the file ...
104 If you know what you are doing you can disable this
105 check using:
107 git config hooks.allownonascii true
110 exit( 1 );
113 # fix whitespace in code
114 check_whitespaces( $against);
116 # all OK
117 exit( 0 );
118 # vi:set shiftwidth=4 expandtab: