From c68e75ac5a772f43c3f149b49f1f6598f46c0aef Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Tue, 19 Jun 2018 11:20:54 +0100 Subject: [PATCH] bin/tools/pre-commit-hook: Added banner check for source and scripts --- bin/tools/HookFunctions | 134 +++++++++++++++++++++++++++++++++++--- bin/tools/pre-commit-hook | 3 + 2 files changed, 128 insertions(+), 9 deletions(-) diff --git a/bin/tools/HookFunctions b/bin/tools/HookFunctions index 592194fc1..f71b2df15 100755 --- a/bin/tools/HookFunctions +++ b/bin/tools/HookFunctions @@ -30,10 +30,69 @@ #------------------------------------------------------------------------------ headerSeparator="-----------------------------------" + echoIndent=" " -#----------------------------------------------------------------------------- +sourceBanner="(\ +/\*---------------------------------------------------------------------------\*\\\\|\ +/\*--------------------------------\*- C\+\+ -\*----------------------------------\*\\\\) + ========= \| + \\\\\\\\ / F ield \| OpenFOAM: The Open Source CFD Toolbox + \\\\\\\\ / O peration \| + \\\\\\\\ / A nd \| Copyright \(C\) [0-9-]+ OpenFOAM Foundation + \\\\\\\\/ M anipulation \| +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM\. + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + \(at your option\) any later version\. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE\. See the GNU General Public License + for more details\. + + You should have received a copy of the GNU General Public License + along with OpenFOAM\. If not, see \. +(.| +)*? +\\\\\*---------------------------------------------------------------------------\*/" + +scriptBanner="(\ +#------------------------------------------------------------------------------|\ +#---------------------------------\*- sh -\*-------------------------------------|\ +#----------------------------------\*-sh-\*--------------------------------------|\ +#----------------------------\*- makefile-gmake -\*------------------------------) +# ========= \| +# \\\\\\\\ / F ield \| OpenFOAM: The Open Source CFD Toolbox +# \\\\\\\\ / O peration \| +# \\\\\\\\ / A nd \| Copyright \(C\) [0-9-]+ OpenFOAM Foundation +# \\\\\\\\/ M anipulation \| +#------------------------------------------------------------------------------ +# License +# This file is part of OpenFOAM\. +# +# OpenFOAM is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# \(at your option\) any later version\. +# +# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE\. See the GNU General Public License +# for more details\. +# +# You should have received a copy of the GNU General Public License +# along with OpenFOAM\. If not, see \. +(.| +)*? +#------------------------------------------------------------------------------" + + +#----------------------------------------------------------------------------- # # report failure and exit @@ -71,7 +130,7 @@ dieOnBadFiles() # # qualify 'git grep' to check cached value or from a specific commit # -gitScope() +gitScopeGrep() { if [ "$#" -gt 0 ] then @@ -82,6 +141,20 @@ gitScope() } +# +# qualify 'git show' to check cached value or from a specific commit +# +gitScopeShow() +{ + if [ "$#" -gt 0 ] + then + echo "$1:" + else + echo ":" + fi +} + + # # check for bad strings, characters, etc # @@ -92,7 +165,7 @@ checkIllegalCode() reBad="("$'\t'")" msgBad="" - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) badFiles=$( for f in $fileList @@ -129,7 +202,7 @@ checkLineLength() { echo "$hookName: check line lengths ..." 1>&2 - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) badFiles=$( for f in $fileList @@ -160,7 +233,7 @@ checkLineLengthNonComments() { echo "$hookName: check line lengths ..." 1>&2 - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) badFiles=$( for f in $fileList @@ -193,7 +266,7 @@ checkLineLengthNonDirective() { echo "$hookName: check line lengths ..." 1>&2 - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) badFiles=$( for f in $fileList @@ -226,7 +299,7 @@ checkNonStandardCodePatterns() { echo "$hookName: checking for non-standard code ..." 1>&2 - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) badFiles=$( for f in $fileList @@ -270,7 +343,7 @@ checkHeaderIfndefNames() { echo "$hookName: check header files #ifndef/#define names ..." 1>&2 - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) badFiles=$( for f in $fileList @@ -299,6 +372,49 @@ checkHeaderIfndefNames() } +# +# check that the banners are correctly formatted +# +checkBanner() +{ + echo "$hookName: check banner ..." 1>&2 + + scopeGrep=$(gitScopeGrep $@) + scopeShow=$(gitScopeShow $@) + + badFiles=$( + for f in $fileList + do + if git grep -q -e "Copyright (C) [0-9-]\+ OpenFOAM Foundation" $scopeGrep"$f" + then + case "$f" in + (*.c|*.C|*.Cver|*.cxx|*.dox|*.H|*.h) + # C++ Source + if ! git show $scopeShow"$f" | pcregrep -q -M "$sourceBanner" + then + echo $f + fi + ;; + (*.*) + # No check for other extensions yet + ;; + (*) + # Assume that everything with a copyright statement but without + # an extension is a script with '#' comments + if ! git show $scopeShow"$f" | pcregrep -q -M "$scriptBanner" + then + echo $f + fi + ;; + esac + fi + done + ) + + dieOnBadFiles "Fix banner formating before pushing" +} + + # # check that OpenFOAM Foundation copyright is current # @@ -306,7 +422,7 @@ checkCopyright() { echo "$hookName: check copyright ..." 1>&2 - scope=$(gitScope $@) + scope=$(gitScopeGrep $@) year=$(date +%Y) badFiles=$( diff --git a/bin/tools/pre-commit-hook b/bin/tools/pre-commit-hook index 055c913e5..d16938e4c 100755 --- a/bin/tools/pre-commit-hook +++ b/bin/tools/pre-commit-hook @@ -115,6 +115,9 @@ checkNonStandardCodePatterns # check if #ifndef/#define bounds are named correctly checkHeaderIfndefNames +# check banner +checkBanner + # check copyright checkCopyright