diff options
author | Miklos Vajna <vmiklos@collabora.co.uk> | 2017-11-20 09:04:51 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2017-11-20 13:41:40 +0100 |
commit | 13de75274b727428355eefd55176277a5f891c47 (patch) | |
tree | 739dd3c29c2cf9d76d601326a403b1b6618e8930 /.git-hooks | |
parent | cd0dd31086bb43fcfcc95beb11aa30bb68d6c485 (diff) |
clang-format: enforce coding style via Jenkins
- factor out common code to a shared module, and quote path to the
clang-format binary, just in case.
- add a new check-last-commit script that is the CI equivalent of the
exiting git pre-commit hook, but this one handles lack of clang-format
as an error, not as a warning.
- $LODE_HOME/opt/bin is supposed to be in PATH already, so not
mentioning LODE_HOME in ClangFormat::find() explicitly.
- if both COMPILER_PLUGINS and LODE_HOME is set, invoke
solenv/clang-format/check-last-commit as part of 'make check'
To test these changes as part of CI, fix a single style violation in an
already committed, non-blacklisted file.
This depends on the lode.git commit
496123bcae28e06c6d6aeda39a5afd1e1fb1fd98 (utils_Linux: install
clang-format in the Jenkins case, 2017-11-16), otherwise erroring out on
a not installed clang-format as part of the build would be a problem.
Change-Id: Ib3110826194ff78a7f1bed1c3796147e92ccb3ba
Reviewed-on: https://gerrit.libreoffice.org/44939
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Diffstat (limited to '.git-hooks')
-rwxr-xr-x | .git-hooks/pre-commit | 63 |
1 files changed, 10 insertions, 53 deletions
diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit index 298ebc4f1d9f..e83e3a36648a 100755 --- a/.git-hooks/pre-commit +++ b/.git-hooks/pre-commit @@ -6,6 +6,8 @@ # if it wants to stop the commit. use strict; +use lib "solenv/clang-format"; +use ClangFormat; #use File::Copy; #use Cwd; @@ -108,71 +110,26 @@ sub check_whitespaces($) } } -# Is this binary the version we standardize on? -sub is_matching_clang_format_version($$) -{ - my ($clang_format, $version) = @_; - if (! -x $clang_format) - { - return 0; - } - - return `$clang_format -version` =~ /^clang-format version $version \(tags/; -} - sub check_style($) { my ($h) = @_; - my $src = "c|cpp|cxx|h|hxx|inl"; + my $src = ClangFormat::get_extension_regex(); my @bad_names = (); - my %blacklist_names = (); - - # Use clang-format from CLANG_FORMAT, or from PATH unless it's available in our dedicated - # directory. - my $version = "5.0.0"; - my $opt_lo = "/opt/lo/bin"; - my $clang_format = $ENV{CLANG_FORMAT}; - if (!(defined($clang_format) && is_matching_clang_format_version($clang_format, $version))) - { - $clang_format = "$opt_lo/clang-format"; - if (!is_matching_clang_format_version($clang_format, $version)) - { - foreach my $dir (split /:/, $ENV{PATH}) - { - $clang_format = "$dir/clang-format"; - if (is_matching_clang_format_version($clang_format, $version)) - { - last; - } - } - } - } - - # Read the blacklist. - if (open(LINES, "solenv/clang-format/blacklist")) - { - while (my $line = <LINES>) - { - chomp $line; - $blacklist_names{$line} = 1; - } - } - - if ($^O eq "cygwin") - { - $clang_format = `cygpath -m $clang_format`; - chomp $clang_format; - } + my $blacklist_names = ClangFormat::get_blacklist(); + my $clang_format = ClangFormat::find(); # Get a list of non-deleted changed files. open (FILES, "git diff-index --cached --diff-filter=AM --name-only $h |") || die "Cannot run git diff."; while (my $filename = <FILES>) { chomp $filename; - if ($filename =~ /\.($src)$/ and !exists($blacklist_names{$filename})) + if ($filename =~ /\.($src)$/ and !exists($blacklist_names->{$filename})) { if (! -x $clang_format) { + my $version = ClangFormat::get_wanted_version(); + my $opt_lo = ClangFormat::get_own_directory(); + print("\nWARNING: Commit touches new (non-blacklisted) files, but no clang-format" . " ${version}\n"); print(" found (via CLANG_FORMAT or PATH env vars, or in ${opt_lo}).\n\n"); @@ -200,7 +157,7 @@ sub check_style($) print("<https://dev-www.libreoffice.org/bin/README.clang-format.txt>).\n\n"); return; } - if (system("$clang_format $filename | git --no-pager diff --no-index --exit-code $filename -") != 0) + if (!ClangFormat::check_style($clang_format, $filename)) { push @bad_names, $filename; } |