File size: 16,855 Bytes
5a600cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | #---------------------------------*- sh -*-------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration | Website: https://openfoam.org
# \\ / A nd | Copyright (C) 2011-2025 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 <http://www.gnu.org/licenses/>.
#
# Script
# HookFunctions
#
# Description
# Functions for automatically checking code pre commit and receive
#
#------------------------------------------------------------------------------
if [ -z "$sourceBanner" ]
then
sourceBanner="(\
/\*---------------------------------------------------------------------------\*\\\\|\
/\*--------------------------------\*- C\+\+ -\*----------------------------------\*\\\\|\
/\*---------------------------------\*- C -\*-----------------------------------\*\\\\)
========= \|
\\\\\\\\ / F ield \| OpenFOAM: The Open Source CFD Toolbox
\\\\\\\\ / O peration \| Website: https://openfoam\.org
\\\\\\\\ / 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 <http://www\.gnu\.org/licenses/>\.
(.|
)*?
\\\\\*---------------------------------------------------------------------------\*/"
fi
if [ -z "$scriptBanner" ]
then
scriptBanner="(\
#------------------------------------------------------------------------------|\
#---------------------------------\*- sh -\*-------------------------------------|\
#----------------------------------\*-sh-\*--------------------------------------|\
#----------------------------\*- makefile-gmake -\*------------------------------)
# ========= \|
# \\\\\\\\ / F ield \| OpenFOAM: The Open Source CFD Toolbox
# \\\\\\\\ / O peration \| Website: https://openfoam\.org
# \\\\\\\\ / 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 <http://www\.gnu\.org/licenses/>\.
(.|
)*?
#------------------------------------------------------------------------------"
fi
if [ -z "$dictBanner" ]
then
dictBanner="(\
/\*---------------------------------------------------------------------------\*\\\\|\
/\*--------------------------------\*- C\+\+ -\*----------------------------------\*\\\\)
========= \|
\\\\\\\\ / F ield \| OpenFOAM: The Open Source CFD Toolbox
\\\\\\\\ / O peration \| Website: https://openfoam\.org
\\\\\\\\ / A nd \| Version: (dev|[0-9\.x]+)
\\\\\\\\/ M anipulation \|(.|
)*?
\\\\\*---------------------------------------------------------------------------\*/"
fi
#-----------------------------------------------------------------------------
gitIsRevision() # commit
{
git rev-parse --verify "$1" > /dev/null 2>&1
return $?
}
scopeGit() # scope, cacheScope
{
if [ "$1" == "--cached" ]
then
echo "$2"
elif gitIsRevision "$1"
then
echo "$1:"
else
echo "$(basename "$0"): $1 is not a valid scope"
exit 1
fi
}
scopeGitGrep() # scope
{
scopeGit "$1" "--cached -- "
}
scopeGitShow() # scope
{
scopeGit "$1" ":"
}
reportStart() # startMessage
{
echo -n "$1 ... " 1>&2
}
reportEnd() # failMessage, errorCount, errorMessages...
{
local failMessage errorCount
failMessage=$1
shift
errorCount=$1
shift
if [ "$errorCount" -eq 0 ]
then
echo PASSED 1>&2
else
echo "FAILED with $errorCount errors:" 1>&2
echo "$failMessage" | sed "s/^/ /g" 1>&2
for errorMessage in "$@"
do
echo "$errorMessage" | sed "s/^/ /g" 1>&2
done
fi
}
checkPattern() # scope, pattern, include, exclude, files...
{
local scope pattern include exclude lines errorCount=0
scope=$(scopeGitGrep "$1")
shift
pattern="$1"
shift
include="$1"
shift
exclude="$1"
shift
shopt -s extglob
# Loop files, accumulating errors
for file in "$@"
do
case "$file" in
($exclude)
;;
($include)
if [ "$(file -b "$file")" != "data" ]
then
lines=$( \
eval "git grep -E -hn $pattern $scope\"$file\"" \
| sed -e "s/:.*//" \
| tr "\n" " " \
)
if [ -n "$lines" ]
then
echo "$file: Lines $lines"
((++errorCount))
fi
fi
;;
esac
done
return $errorCount
}
checkTabs() # scope, files...
{
reportStart "Check for tabs"
local scope pattern include exclude errorCount errorMessages
scope=$1
shift
pattern="-e $'\t'"
include="*"
exclude="@(*[Mm]akefile*|wmake/rules/*|bin/tools/gtagsrc|*.html|*.css|*.pdf|*.png|*.gz)"
errorMessages="$(checkPattern "$scope" "$pattern" "$include" "$exclude" "$@")"
errorCount=$?
reportEnd "Remove <TAB> characters from the following files:" $errorCount "$errorMessages"
return $errorCount
}
checkLineEnds() # scope, files...
{
reportStart "Check for bad line endings"
local scope pattern include exclude errorCount errorMessages
scope=$1
shift
pattern="-e \"[ ]+$\" --or -e $'\r'"
include="*"
exclude="@(*.md|*.pdf|*.png|*.gz)"
errorMessages="$(checkPattern "$scope" "$pattern" "$include" "$exclude" "$@")"
errorCount=$?
reportEnd "Remove trailing whitespace and <CR> characters in the following files:" $errorCount "$errorMessages"
return $errorCount
}
checkLineLength() # scope, files...
{
reportStart "Check line lengths"
local scope pattern include exclude errorCount errorMessages
scope=$1
shift
pattern="-e \"^.{81,}\" --and --not -e \"^ *#\""
include="*.[CH]"
exclude=""
errorMessages="$(checkPattern "$scope" "$pattern" "$include" "$exclude" "$@")"
errorCount=$?
reportEnd "Limit lines to 80 characters in the following files:" $errorCount "$errorMessages"
return $errorCount
}
checkNonStandardCode() # scope, files...
{
reportStart "Check for non-standard code"
local scope pattern include exclude errorCount errorMessages
scope=$1
shift
pattern="-e \"> >\" --or -e \"NULL\""
include="*.[CH]"
exclude=""
errorMessages="$(checkPattern "$scope" "$pattern" "$include" "$exclude" "$@")"
errorCount=$?
reportEnd "$(cat << EOF
The following is considered non-standard code:
1. Spaced ending of multi-level template parameters. For example,
List<List<scalar> >
should instead be written:
List<List<scalar>>
2. The 'NULL' macro. This should be replaced by 'nullptr'.
Remove these patterns from the following files:
EOF
)" $errorCount "$errorMessages"
return $errorCount
}
checkHeaderIfndefNames() # scope, fix, files...
{
reportStart "Check header file #ifndef/#define names"
local scopeGrep fix correctDefine currentDefine failMessage errorCount=0 errorMessages=()
scopeGrep=$(scopeGitGrep "$1")
shift
fix=$1
shift
for file in "$@"
do
case "$file" in
(*.H)
correctDefine=$(basename "${file%.H}" | sed 's/\./_/')
if git grep -q -e "#ifndef .*_H$" --and --not -e "#ifndef \(\|.\+_\)${correctDefine}.*_H" $scopeGrep"$file"
then
((++errorCount))
errorMessages+=("$file")
currentDefine=$(grep "#ifndef .*_H$" "$file" | sed 's/#ifndef\s*//')
if $fix
then
sed -i -e "s/$currentDefine/${correctDefine}_H/" "$file"
fi
fi
;;
esac
done
if $fix
then
failMessage="The following files have been automatically updated so that the
#ifndef/#define statements match the file name. Check and re-add them
before pushing:"
else
failMessage="Revise the following files so that the #ifndef/#define statements match
the file name:"
fi
reportEnd "$failMessage" $errorCount "${errorMessages[@]}"
return $errorCount
}
checkBanner() # scope, files...
{
reportStart "Check banners"
local scopeGrep scopeShow fileName fileExtension errorCount=0 errorMessages=()
scopeGrep=$(scopeGitGrep "$1")
scopeShow=$(scopeGitShow "$1")
shift
for file in "$@"
do
fileName=$(basename "$file")
fileExtension=${fileName##*.}
# Skip links
if [ -h "$file" ]
then
:
# Copyrighted source and script files
elif git grep -q -e "Copyright (C) [0-9-]\+ OpenFOAM Foundation" $scopeGrep"$file"
then
case "$fileExtension" in
(c|C|Cver|cxx|dox|H|h|l|L)
if ! git show $scopeShow"$file" | pcre2grep -q -M "$sourceBanner"
then
((++errorCount))
errorMessages+=("$file")
fi
;;
(''|awk|csh|gnuplot|sed|sh)
if ! git show $scopeShow"$file" | pcre2grep -q -M "$scriptBanner"
then
((++errorCount))
errorMessages+=("$file")
fi
;;
(*)
:
#((++errorCount))
#errorMessages+=("$file: Unknown extension")
;;
esac
# Versioned case files
elif git grep -q -e "Version: \(dev\|[0-9.]+\)" $scopeGrep"$file"
then
if ! git show $scopeShow"$file" | pcre2grep -q -M "$dictBanner"
then
((++errorCount))
errorMessages+=("$file")
fi
# Unknown files
elif git grep -q -e "OpenFOAM: The Open Source CFD Toolbox" $scopeGrep"$file"
then
:
#((++errorCount))
#errorMessages+=("$file: Missing copyright or version")
fi
done
reportEnd "Correct the banner formatting in the following files:" $errorCount "${errorMessages[@]}"
return $errorCount
}
checkMode() # scope, fix, files...
{
reportStart "Check for executables"
local scope scopeGrep fix gitLs failMessage errorCount=0 errorMessages=()
scope="$1"
scopeGrep=$(scopeGitGrep "$scope")
shift
fix=$1
shift
if [ "$scope" == "--cached" ]
then
gitLs="git ls-files --stage"
else
gitLs="git ls-tree"
fi
for file in "$@"
do
if git grep -qe "\(^#\!.*/bin/.*\)" $scopeGrep"$file"
then
if [[ ! $($gitLs "$scope" "$file") =~ ^100(755|775|777) ]]
then
((++errorCount))
errorMessages+=("$file")
if $fix
then
chmod a+x "$file"
fi
fi
fi
done
if $fix
then
failMessage="The following files have been made executable automatically. Check and
re-add them before pushing:"
else
failMessage="Make the following files executable:"
fi
reportEnd "$failMessage" $errorCount "${errorMessages[@]}"
return $errorCount
}
checkCopyright() # scope, fix, files...
{
reportStart "Check copyright"
local scopeGrep fix year startYear endYear errorCount=0 errorMessages=() failMessage
scopeGrep=$(scopeGitGrep "$1")
shift
fix=$1
shift
year=$(date +%Y)
for file in "$@"
do
startYear=$( \
git grep -h -e "Copyright.*OpenFOAM" $scopeGrep"$file" \
| head -n 1 \
| sed 's/[^0-9]*\([0-9]*\).*/\1/g'
)
endYear=$( \
git grep -h -e "Copyright.*-.*OpenFOAM" $scopeGrep"$file" \
| head -n 1 \
| sed 's/[^-]*-\([0-9]*\).*/\1/g'
)
if [ "$startYear" != "" ]
then
if [ "$endYear" != "" ]
then
# Date is of type 2011-2012 OpenFOAM Foundation
if [ "$year" != "$endYear" ]
then
((++errorCount))
errorMessages+=("$file")
if $fix
then
sed -i -e "s/$startYear-$endYear OpenFOAM/$startYear-$year OpenFOAM/g" "$file"
fi
fi
else
# Date is of type 2011 OpenFOAM Foundation
if [ "$year" != "$startYear" ]
then
((++errorCount))
errorMessages+=("$file")
if $fix
then
sed -i -e "s/$startYear OpenFOAM/$startYear-$year OpenFOAM/g" "$file"
fi
fi
fi
fi
done
if $fix
then
failMessage="The following files have had their copyright dates automatically
updated. Check and re-add them before pushing:"
else
failMessage="Update the copyright dates in the following files:"
fi
reportEnd "$failMessage" $errorCount "${errorMessages[@]}"
return $errorCount
}
checkAllNoCopyright() # scope, fix, files...
{
local scope fix returnCode=0
scope="$1"
shift
fix="$1"
shift
# Check for tabs
checkTabs "$scope" "$@" || returnCode=1
# Check line endings
checkLineEnds "$scope" "$@" || returnCode=1
# Ensure code line lengths do not exceed 80 columns
checkLineLength "$scope" "$@" || returnCode=1
# Check for non-standard code patterns
checkNonStandardCode "$scope" "$@" || returnCode=1
# Check if #ifndef/#define bounds are named correctly
checkHeaderIfndefNames "$scope" "$fix" "$@" || returnCode=1
# Check banner
checkBanner "$scope" "$@" || returnCode=1
# Check mode
checkMode "$scope" "$fix" "$@" || returnCode=1
return $returnCode
}
checkAll() # scope, fix, files...
{
local scope fix returnCode=0
scope="$1"
shift
fix="$1"
shift
# Check all except copyright
checkAllNoCopyright "$scope" "$fix" "$@" || returnCode=1
# Check copyright
checkCopyright "$scope" "$fix" "$@" || returnCode=1
return $returnCode
}
#------------------------------------------------------------------------------
|