tdf#125506: use rObjectToDevice if needed
[LibreOffice.git] / .git-hooks / pre-commit
blob1f3d21ebff987702539f2cdf4f2f4ebfe35eec94
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 lib "solenv/clang-format";
10 #use File::Copy;
11 #use Cwd;
13 $ENV{LC_ALL} = "C";
15 sub check_whitespaces($)
17 my ($h) = @_;
18 my $src_limited = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|swift|map|MK|pmk|pl|pm|sdi|sh|src|tab|ui|xcu|xml|xsl|py";
19 my $src_full = "c|cpp|cxx|h|hrc|hxx|idl|inl|java|swift|map|mk|MK|pmk|pl|pm|sdi|sh|src|tab|ui|xcu|xml|xsl|py";
21 my $found_bad = 0;
22 my $filename;
23 my $reported_filename = "";
24 my $lineno;
25 sub bad_line
27 my ($why, $line, $file_filter) = @_;
28 if (!defined $file_filter || $filename =~ /\.($file_filter)$/)
30 if (!$found_bad)
32 print STDERR "*\n";
33 print STDERR "* You have some suspicious patch lines:\n";
34 print STDERR "*\n";
35 $found_bad = 1;
37 if ($reported_filename ne $filename)
39 print STDERR "* In $filename\n";
40 $reported_filename = $filename;
42 print STDERR "* $why (line $lineno)\n";
43 print STDERR "$filename:$lineno:$line\n";
46 open( FILES, "git-diff-index -p -M --cached $h |" ) || die "Cannot run git diff-index.";
47 while (<FILES>)
49 if (m|^diff --git a/(.*) b/\1$|)
51 $filename = $1;
52 next;
54 if (/^@@ -\S+ \+(\d+)/)
56 $lineno = $1 - 1;
57 next;
59 if (/^ /)
61 $lineno++;
62 next;
64 if (s/^\+//)
66 $lineno++;
67 chomp;
68 if (/\s$/)
70 bad_line("trailing whitespace", $_ , $src_limited);
72 if (/\r$/)
74 bad_line("DOS lineends", $_ , $src_limited);
76 if (/\s* /)
78 bad_line("indent with Tab", $_, $src_limited);
80 if (/^(?:[<>=]){7}$/)
82 bad_line("unresolved merge conflict", $src_full);
84 if (/SAL_DEBUG/)
86 bad_line("temporary debug in commit", $_, $src_limited);
88 if (/<property name="use_markup">True<\/property>/)
90 bad_line("use font attributes instead of use-markup", $_, "ui");
92 if (/<property name="tooltip_markup"/ )
94 bad_line("use tooltip_text instead of tooltip_markup", $_, "ui");
96 if ((/translatable="yes"/) and not(/context=/))
98 bad_line("translatable .ui file line without context", $_, "ui");
100 if ((/<interface/) and not(/domain=/))
102 bad_line(".ui file without translation domain", $_, "ui");
106 if ( $found_bad)
108 exit($found_bad);
112 sub check_author()
114 my $author = `git var GIT_AUTHOR_IDENT`;
115 chomp $author;
116 if ($author =~ /^Your Name <you\@example.com>/)
118 print("ERROR: You have a suspicious author identity: '$author'\n");
119 exit(1);
123 sub check_style($)
125 if (! -e "solenv/clang-format/ClangFormat.pm")
127 # Commit happens in a submodule.
128 return;
131 require ClangFormat;
132 ClangFormat->import();
134 my ($h) = @_;
135 my $src = ClangFormat::get_extension_regex();
136 my @bad_names = ();
137 my $blacklist_names = ClangFormat::get_blacklist();
138 my $clang_format = ClangFormat::find();
140 # Get a list of non-deleted changed files.
141 open (FILES, "git diff-index --cached --diff-filter=AM --name-only $h |") || die "Cannot run git diff.";
142 while (my $filename = <FILES>)
144 chomp $filename;
145 if ($filename =~ /\.($src)$/ and !exists($blacklist_names->{$filename}))
147 if (!defined($clang_format))
149 my $version = ClangFormat::get_wanted_version();
150 my $opt_lo = ClangFormat::get_own_directory();
152 print("\nWARNING: Commit touches new (non-blacklisted) files, but no clang-format"
153 . " ${version}\n");
154 print(" found (via CLANG_FORMAT or PATH env vars, or in ${opt_lo}).\n\n");
156 my $platform = "linux64";
157 my $download = "wget";
158 if ($^O eq "cygwin")
160 $platform = "win.exe";
162 elsif ($^O eq "darwin")
164 $platform = "mac";
165 $download = "curl -O";
168 print("To get a suitable binary, please do:\n\n");
169 print("mkdir -p $opt_lo\n");
170 print("cd $opt_lo\n");
171 print("$download https://dev-www.libreoffice.org/bin/clang-format-$version-$platform\n");
172 print("cp clang-format-$version-$platform clang-format\n");
173 print("chmod +x clang-format\n\n");
175 print("(Or read solenv/clang-format/README on how to build it yourself.\n");
176 return;
178 if (!ClangFormat::check_style($clang_format, $filename))
180 push @bad_names, $filename;
185 # Enforce style.
186 if (scalar @bad_names)
188 my $autostyle = `git config libreoffice.autostyle`;
189 chomp $autostyle;
190 if ($autostyle ne "true")
192 print("\nThe above differences were found between the code to commit \n");
193 print("and the clang-format rules. You can apply these changes with:\n");
194 print("\n$clang_format -i " . join(" ", @bad_names) . "\n\n");
195 print("Aborting commit. Apply changes and commit again or skip checking\n");
196 print("with --no-verify (not recommended).\n");
197 exit(1);
199 else
201 # 'git config libreoffice.autostyle true' was invoked to run
202 # clang-format automatically.
203 print("\nThe above differences were found between the code to commit \n");
204 print("and the clang-format rules. Fixing these now automatically.\n");
205 print("Running '$clang_format -i " . join(" ", @bad_names) . "' for you...\n");
206 system("$clang_format -i " . join(" ", @bad_names));
207 # TODO this stages all local modifications, staging originally
208 # unstaged hunks.
209 system("git add " . join(" ", @bad_names));
210 print("Done.\n");
215 sub check_submodules($)
217 my ($h) = @_;
219 my $toplevel = `git rev-parse --show-toplevel`;
220 chomp $toplevel;
222 # trick to get a list of submodules - directly read from the .gitmodules
223 open(SUBMODULES, "git config --file '$toplevel'/.gitmodules --get-regexp path | awk '{ print \$2 }' |" ) || die "Cannot run git config on the .gitmodules.";
224 while (<SUBMODULES>)
226 chomp;
228 my $ignore = `git config submodule.$_.ignore`;
229 chomp $ignore;
230 if ($ignore eq 'all')
232 print <<EOM;
233 Error: Your git configuration has submodule.$_.ignore set to 'all'.
235 This is dangerous and can lead to accidentally pushing unwanted changes to
236 submodules.
238 To fix it, please do:
240 git config --unset submodule.$_.ignore
243 exit(1);
246 my $diff = `git diff --cached --name-only -z $h -- $_`;
247 chomp $diff;
248 if ($diff ne '')
250 print <<EOM;
251 Error: You are trying to commit changes to submodule $_ from the main repo.
253 Please do not do that, commit only to the submodule, the git hook on the
254 server will make sure the appropriate change is mirrored in the main repo.
256 To remove the change, you can do:
258 git submodule update $_
260 If it fails with 'error: Server does not allow request for unadvertised object',
261 run the following:
263 git submodule sync
264 git submodule update $_
267 exit(1);
272 # Do the work :-)
274 # Initial commit: diff against an empty tree object
275 my $against="4b825dc642cb6eb9a060e54bf8d69288fbee4904";
276 if ( system( "git rev-parse --verify HEAD >/dev/null 2>&1" ) == 0 )
278 $against="HEAD"
281 # If you want to allow non-ascii filenames set this variable to true.
282 my $allownonascii=`git config hooks.allownonascii`;
284 # Cross platform projects tend to avoid non-ascii filenames; prevent
285 # them from being added to the repository. We exploit the fact that the
286 # printable range starts at the space character and ends with tilde.
287 if ( $allownonascii ne "true" &&
288 # Note that the use of brackets around a tr range is ok here, (it's
289 # even required, for portability to Solaris 10's /usr/bin/tr), since
290 # the square bracket bytes happen to fall in the designated range.
291 `git diff --cached --name-only --diff-filter=A -z $against | \
292 LC_ALL=C tr -d '[ -~]\\0'` ne "" )
294 print <<EOM;
295 Error: Attempt to add a non-ascii file name.
297 This can cause problems if you want to work
298 with people on other platforms.
300 To be portable it is advisable to rename the file ...
302 If you know what you are doing you can disable this
303 check using:
305 git config hooks.allownonascii true
308 exit( 1 );
311 # Block large files.
312 open( FILES, "git diff --cached --name-only --diff-filter=A -z $against |" ) || die "Cannot run git diff-index.";
313 while (<FILES>)
315 if (/\.ui$/) # .ui files can get large
317 continue;
319 if (/\.xsl$/) # XSLT
321 continue;
323 my $size = `git cat-file -s :$_`;
324 # For now let's say large is 500KB
325 my $limit = 500;
326 if ($size > $limit * 1024)
328 print "Error: Attempt to add a large file: $_, pleasy try to fit into $limit KB.\n";
329 exit( 1 );
333 # fix whitespace in code
334 check_whitespaces( $against);
336 # fix style in code
337 check_style($against);
339 # catch missing author info
340 check_author();
342 # catch commits to the submodules
343 check_submodules($against);
345 # all OK
346 exit( 0 );
347 # vi:set shiftwidth=4 expandtab: