Branch libreoffice-5-0-4
[LibreOffice.git] / .git-hooks / pre-commit
blob693319826b6657a3372e5ff26ae82e7b1d2b05f0
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|ui|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|ui|xcu|xml";
20 my $found_bad = 0;
21 my $filename;
22 my $reported_filename = "";
23 my $lineno;
24 sub bad_line
26 my ($why, $line, $file_filter) = @_;
27 if (!defined $file_filter || $filename =~ /\.($file_filter)$/)
29 if (!$found_bad)
31 print STDERR "*\n";
32 print STDERR "* You have some suspicious patch lines:\n";
33 print STDERR "*\n";
34 $found_bad = 1;
36 if ($reported_filename ne $filename)
38 print STDERR "* In $filename\n";
39 $reported_filename = $filename;
41 print STDERR "* $why (line $lineno)\n";
42 print STDERR "$filename:$lineno:$line\n";
45 open( FILES, "git-diff-index -p -M --cached $h |" ) || die "Cannot run git diff-index.";
46 while (<FILES>)
48 if (m|^diff --git a/(.*) b/\1$|)
50 $filename = $1;
51 next;
53 if (/^@@ -\S+ \+(\d+)/)
55 $lineno = $1 - 1;
56 next;
58 if (/^ /)
60 $lineno++;
61 next;
63 if (s/^\+//)
65 $lineno++;
66 chomp;
67 if (/\s$/)
69 bad_line("trailing whitespace", $_ , $src_limited);
71 if (/\s* /)
73 bad_line("indent with Tab", $_, $src_limited);
75 if (/^(?:[<>=]){7}$/)
77 bad_line("unresolved merge conflict", $src_full);
79 if (/SAL_DEBUG/)
81 bad_line("temporary debug in commit", $_, $src_limited);
83 if (/<property name="use_markup">True<\/property>/)
85 bad_line("use font attributes instead of use-markup", $_, $src_limited);
89 if ( $found_bad)
91 exit($found_bad);
95 # Do the work :-)
97 # Initial commit: diff against an empty tree object
98 my $against="4b825dc642cb6eb9a060e54bf8d69288fbee4904";
99 if ( system( "git rev-parse --verify HEAD >/dev/null 2>&1" ) == 0 )
101 $against="HEAD"
104 # If you want to allow non-ascii filenames set this variable to true.
105 my $allownonascii=`git config hooks.allownonascii`;
107 # Cross platform projects tend to avoid non-ascii filenames; prevent
108 # them from being added to the repository. We exploit the fact that the
109 # printable range starts at the space character and ends with tilde.
110 if ( $allownonascii ne "true" &&
111 # Note that the use of brackets around a tr range is ok here, (it's
112 # even required, for portability to Solaris 10's /usr/bin/tr), since
113 # the square bracket bytes happen to fall in the designated range.
114 `git diff --cached --name-only --diff-filter=A -z $against | \
115 LC_ALL=C tr -d '[ -~]\\0'` ne "" )
117 print <<EOM;
118 Error: Attempt to add a non-ascii file name.
120 This can cause problems if you want to work
121 with people on other platforms.
123 To be portable it is advisable to rename the file ...
125 If you know what you are doing you can disable this
126 check using:
128 git config hooks.allownonascii true
131 exit( 1 );
134 # Block large files.
135 open( FILES, "git diff --cached --name-only --diff-filter=A -z $against |" ) || die "Cannot run git diff-index.";
136 while (<FILES>)
138 my $size = `git cat-file -s :$_`;
139 # For now let's say large is 500KB
140 my $limit = 500;
141 if ($size > $limit * 1024)
143 print "Error: Attempt to add a large file: $_, pleasy try to fit into $limit KB.\n";
144 exit( 1 );
148 # fix whitespace in code
149 check_whitespaces( $against);
151 # all OK
152 exit( 0 );
153 # vi:set shiftwidth=4 expandtab: