ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / bin / tools / pre-receive-hook
blob4a5a480962e4ed8bc4aa9bfe03cb5c7f3cb1a761
1 #!/bin/bash
2 #---------------------------------*- sh -*-------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 # \\/ M anipulation |
8 #------------------------------------------------------------------------------
9 # License
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
20 # for more details.
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/>.
25 # Script
26 # pre-receive-hook
28 # Description
29 # pre-receive hook for git.
30 # Copy or link this file as ".git/hooks/pre-receive"
32 # Eg,
33 # (
34 # cd $WM_PROJECT_DIR/.git/hooks &&
35 # ln -sf ../../bin/tools/pre-receive-hook pre-receive
36 # )
38 # Hook receives: <old-sha1> <new-sha1> <ref-name>
40 # Checks for
41 # - illegal code, e.g. <TAB>
42 # - columns greater than 80 for *.[CH] files
44 #------------------------------------------------------------------------------
45 hookName="pre-receive"
46 die()
48 echo "$hookName hook failure" 1>&2
49 echo '-----------------------------------' 1>&2
50 echo '' 1>&2
51 echo "$@" 1>&2
52 echo '' 1>&2
53 exit 1
56 #-----------------------------------------------------------------------------
58 unset fileList
59 unset badFiles
60 # join list of files with this amount of space
61 Indent=" "
64 # report bad files and die if there are any
66 dieOnBadFiles()
68 if [ -n "$badFiles" ]
69 then
70 echo "$hookName hook failure" 1>&2
71 echo '-----------------------------------' 1>&2
72 echo "$@" 1>&2
73 echo '' 1>&2
74 echo "File(s):" 1>&2
75 echo "$badFiles" 1>&2
76 echo '' 1>&2
77 exit 1
83 # qualify 'git grep' to check cached value or from a specific commit
85 gitScope()
87 if [ "$#" -gt 0 ]
88 then
89 echo "$1:"
90 else
91 echo "--cached -- "
97 # check for bad strings, characters, etc
99 checkIllegalCode()
101 echo "$hookName: check bad strings/characters etc ..." 1>&2
103 reBad="("$'\t'"|"$'\r\n'")"
104 msgBad="<TAB> or DOS-line-endings"
106 scope=$(gitScope $@)
108 badFiles=$(
109 for f in $fileList
111 case "$f" in
112 # exclude potential makefiles
113 (*[Mm]akefile* | wmake/rules/*)
116 # parse line numbers from grep output:
117 # <lineNr>: contents
118 lines=$(git grep -E -hn -e "$reBad" $scope"$f" |
119 sed -e 's@:.*@@' |
120 tr '\n' ' '
122 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
124 esac
125 done
128 dieOnBadFiles "Remove/correct bad '$msgBad' references"
133 # limit line length to 80-columns
135 checkLineLength()
137 echo "$hookName: check line lengths ..." 1>&2
139 scope=$(gitScope $@)
141 badFiles=$(
142 for f in $fileList
144 # limit to *.[CH] files
145 case "$f" in
146 (*.[CH])
147 # parse line numbers from grep output:
148 # <lineNr>: contents
149 lines=$(git grep -hn -e '^.\{81,\}' $scope"$f" |
150 sed -e 's@:.*@@' |
151 tr '\n' ' '
153 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
155 esac
156 done
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
170 scope=$(gitScope $@)
172 badFiles=$(
173 for f in $fileList
175 # limit to *.[CH] files
176 case "$f" in
177 (*.[CH])
178 # parse line numbers from grep output:
179 # <lineNr>: contents
180 lines=$(git grep -hn -e '^.\{81,\}' \
181 --and --not -e '^ *//' \
182 $scope"$f" |
183 sed -e 's@:.*@@' |
184 tr '\n' ' '
186 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
188 esac
189 done
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
203 scope=$(gitScope $@)
205 badFiles=$(
206 for f in $fileList
208 # limit to *.[CH] files
209 case "$f" in
210 (*.[CH])
211 # parse line numbers from grep output:
212 # <lineNr>: contents
213 lines=$(git grep -hn -e '^.\{81,\}' \
214 --and --not -e '^ *#' \
215 $scope"$f" |
216 sed -e 's@:.*@@' |
217 tr '\n' ' '
219 [ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
221 esac
222 done
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 ]
238 then
239 # ref to be deleted
240 continue
241 elif [ "$oldSHA1" = 0 ]
242 then
243 # ref to be created
244 rawFileList=$(git diff-tree --root $newSHA1)
245 else
246 # normal changes
247 rawFileList=$(git diff --name-only $oldSHA1..$newSHA1)
251 # no files changed: can skip all the checks
253 [ -n "$rawFileList" ] || continue
255 fileList=$(
256 for f in $rawFileList
258 git cat-file -e $newSHA1:$f > /dev/null 2>&1 && echo "$f"
259 done
262 # check for illegal code, e.g. <TAB>, etc
263 checkIllegalCode $newSHA1
265 # ensure code conforms to 80 columns max
266 checkLineLengthNonDirective $newSHA1
268 done
271 exit 0
272 #------------------------------------------------------------------------------