2 #---------------------------------*- sh -*-------------------------------------
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
6 # \\ / A nd | Copyright (C) 2010-2011 OpenCFD Ltd.
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-commit hook for git.
30 # Copy or link this file as ".git/hooks/pre-commit"
34 # cd $WM_PROJECT_DIR/.git/hooks &&
35 # ln -sf ../../bin/tools/pre-commit-hook pre-commit
38 # Hook receives: empty
41 # - illegal code, e.g. <TAB>
42 # - columns greater than 80 for *.[CH] files
45 # Using "git commit --no-verify" it is possible to override the hook.
47 # By supplying arguments to the hook, it can also be used to manually
48 # test the specified files/directories for standards conformance.
50 #------------------------------------------------------------------------------
54 echo "$hookName hook failure" 1>&2
55 echo '-----------------------------------' 1>&2
62 #-----------------------------------------------------------------------------
63 # Check content that will be added by this commit.
65 if git rev-parse
--verify HEAD
> /dev
/null
2>&1
69 # Initial commit: diff against an empty tree object
70 against
=4b825dc642cb6eb9a060e54bf8d69288fbee4904
73 # called manually with arguments for the files/directories to be tested?
78 die
"interactive usage: supply list of files/directories to check"
82 # obtain list of all specified files/directories
83 fileList
=$
(git ls-files
-- $@
2>/dev
/null
)
85 # list of all files to be committed
86 fileList
=$
(git diff-index
--cached --name-only $against --)
90 # no files changed: can skip all the checks
91 # this usage can correspond to a 'git commit --amend'
93 [ -n "$fileList" ] ||
exit 0
97 # join list of files with this amount of space
101 # report bad files and die if there are any
105 if [ -n "$badFiles" ]
107 echo "$hookName hook failure" 1>&2
108 echo '-----------------------------------' 1>&2
112 echo "$badFiles" 1>&2
120 # qualify 'git grep' to check cached value or from a specific commit
134 # check for bad strings, characters, etc
138 echo "$hookName: check bad strings/characters etc ..." 1>&2
149 # exclude potential makefiles
150 (*[Mm
]akefile
* | wmake
/rules
/*)
153 # parse line numbers from grep output:
155 lines
=$
(git
grep -E -hn -e "$reBad" $scope"$f" |
159 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
165 dieOnBadFiles
"Remove/correct bad '$msgBad' references"
170 # limit line length to 80-columns
174 echo "$hookName: check line lengths ..." 1>&2
181 # limit to *.[CH] files
184 # parse line numbers from grep output:
186 lines
=$
(git
grep -hn -e '^.\{81,\}' $scope"$f" |
190 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
196 dieOnBadFiles
"Limit code to 80 columns before pushing"
201 # limit line length to 80-columns, except C++ comment lines
203 checkLineLengthNonComments
()
205 echo "$hookName: check line lengths ..." 1>&2
212 # limit to *.[CH] files
215 # parse line numbers from grep output:
217 lines
=$
(git
grep -hn -e '^.\{81,\}' \
218 --and --not -e '^ *//' \
223 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
229 dieOnBadFiles
"Limit code to 80 columns before pushing"
234 # limit line length to 80-columns, except #directive lines
236 checkLineLengthNonDirective
()
238 echo "$hookName: check line lengths ..." 1>&2
245 # limit to *.[CH] files
248 # parse line numbers from grep output:
250 lines
=$
(git
grep -hn -e '^.\{81,\}' \
251 --and --not -e '^ *#' \
256 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
262 dieOnBadFiles
"Limit code to 80 columns before pushing"
266 #------------------------------------------------------------------------------
267 # Main code : do all checks
270 # builtin whitespace check to avoid trailing space, including CR-LF endings
271 bad
=$
(git diff-index
--cached --check $against --) || die
"$bad"
273 # check for illegal code, e.g. <TAB>, etc
276 # ensure code conforms to 80 columns max
277 checkLineLengthNonDirective
281 #------------------------------------------------------------------------------