Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / git-hooks / pre-commit
blob495dc169aea89d58607f609f7a26bf133f841a2e
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);
67 if (/SAL_DEBUG/) {
68 bad_line("temporary debug in commit", $_, $src_limited);
72 if ( $found_bad)
74 exit($found_bad);
78 # Do the work :-)
80 # Initial commit: diff against an empty tree object
81 my $against="4b825dc642cb6eb9a060e54bf8d69288fbee4904";
82 if ( system( "git rev-parse --verify HEAD >/dev/null 2>&1" ) == 0 ) {
83 $against="HEAD"
86 # If you want to allow non-ascii filenames set this variable to true.
87 my $allownonascii=`git config hooks.allownonascii`;
89 # Cross platform projects tend to avoid non-ascii filenames; prevent
90 # them from being added to the repository. We exploit the fact that the
91 # printable range starts at the space character and ends with tilde.
92 if ( $allownonascii ne "true" &&
93 # Note that the use of brackets around a tr range is ok here, (it's
94 # even required, for portability to Solaris 10's /usr/bin/tr), since
95 # the square bracket bytes happen to fall in the designated range.
96 `git diff --cached --name-only --diff-filter=A -z $against | \
97 LC_ALL=C tr -d '[ -~]\\0'` ne "" )
99 print <<EOM;
100 Error: Attempt to add a non-ascii file name.
102 This can cause problems if you want to work
103 with people on other platforms.
105 To be portable it is advisable to rename the file ...
107 If you know what you are doing you can disable this
108 check using:
110 git config hooks.allownonascii true
113 exit( 1 );
116 # fix whitespace in code
117 check_whitespaces( $against);
119 # all OK
120 exit( 0 );
121 # vi:set shiftwidth=4 expandtab: