2 #---------------------------------*- sh -*-------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
8 #------------------------------------------------------------------------------
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
29 # pre-receive hook for git.
30 # Copy or link this file as ".git/hooks/pre-receive"
34 # cd $WM_PROJECT_DIR/.git/hooks &&
35 # ln -sf ../../bin/tools/pre-receive-hook pre-receive
38 # Hook receives: <old-sha1> <new-sha1> <ref-name>
41 # - illegal code, e.g. <TAB>
42 # - columns greater than 80 for *.[CH] files
44 #------------------------------------------------------------------------------
45 hookName
="pre-receive"
48 echo "$hookName hook failure" 1>&2
49 echo '-----------------------------------' 1>&2
56 #-----------------------------------------------------------------------------
60 # join list of files with this amount of space
64 # report bad files and die if there are any
70 echo "$hookName hook failure" 1>&2
71 echo '-----------------------------------' 1>&2
83 # qualify 'git grep' to check cached value or from a specific commit
97 # check for bad strings, characters, etc
101 echo "$hookName: check bad strings/characters etc ..." 1>&2
103 reBad
="("$
'\t'"|"$
'\r\n'")"
104 msgBad
="<TAB> or DOS-line-endings"
112 # exclude potential makefiles
113 (*[Mm
]akefile
* | wmake
/rules
/*)
116 # parse line numbers from grep output:
118 lines
=$
(git
grep -E -hn -e "$reBad" $scope"$f" |
122 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
128 dieOnBadFiles
"Remove/correct bad '$msgBad' references"
133 # limit line length to 80-columns
137 echo "$hookName: check line lengths ..." 1>&2
144 # limit to *.[CH] files
147 # parse line numbers from grep output:
149 lines
=$
(git
grep -hn -e '^.\{81,\}' $scope"$f" |
153 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
159 dieOnBadFiles
"Limit code to 80 columns before pushing"
164 # limit line length to 80-columns, except C++ comment lines
166 checkLineLengthNonComments
()
168 echo "$hookName: check line lengths ..." 1>&2
175 # limit to *.[CH] files
178 # parse line numbers from grep output:
180 lines
=$
(git
grep -hn -e '^.\{81,\}' \
181 --and --not -e '^ *//' \
186 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
192 dieOnBadFiles
"Limit code to 80 columns before pushing"
197 # limit line length to 80-columns, except #directive lines
199 checkLineLengthNonDirective
()
201 echo "$hookName: check line lengths ..." 1>&2
208 # limit to *.[CH] files
211 # parse line numbers from grep output:
213 lines
=$
(git
grep -hn -e '^.\{81,\}' \
214 --and --not -e '^ *#' \
219 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
225 dieOnBadFiles
"Limit code to 80 columns before pushing"
229 #------------------------------------------------------------------------------
230 # Main code : do all checks
233 while read oldSHA1 newSHA1 refName
235 unset fileList rawFileList
237 if [ "$newSHA1" = 0 ]
241 elif [ "$oldSHA1" = 0 ]
244 rawFileList
=$
(git diff-tree
--root $newSHA1)
247 rawFileList
=$
(git
diff --name-only $oldSHA1..
$newSHA1)
251 # no files changed: can skip all the checks
253 [ -n "$rawFileList" ] ||
continue
256 for f
in $rawFileList
258 git cat-file
-e $newSHA1:$f > /dev
/null
2>&1 && echo "$f"
262 # check for illegal code, e.g. <TAB>, etc
263 checkIllegalCode
$newSHA1
265 # ensure code conforms to 80 columns max
266 checkLineLengthNonDirective
$newSHA1
272 #------------------------------------------------------------------------------