Clean up gmx_fatal.*
[gromacs/AngularHB.git] / admin / uncrustify.sh
blobc683163076da1a59fff37254b7a853a7c283cbe8
1 #!/bin/bash
3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2013,2014, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
36 # This script runs uncrustify on modified files and reports/applies the
37 # results. It also checks for outdated copyright years or headers.
38 # By default, the current HEAD commit is compared to the work tree,
39 # and files that
40 # 1. are different between these two trees and
41 # 2. change under uncrustify and/or have outdated copyright header
42 # are reported. This behavior can be changed by
43 # 1. Specifying an --rev=REV argument, which uses REV instead of HEAD as
44 # the base of the comparison.
45 # 2. Specifying an action:
46 # check-*: reports the files that uncrustify changes
47 # diff-*: prints the actual diff of what would change
48 # update-*: applies the changes to the repository
49 # *-workdir: operates on the working directory (files on disk)
50 # *-index: operates on the index of the repository
51 # For convenience, if you omit the workdir/index suffix, workdir is assumed
52 # (i.e., diff equals diff-workdir).
53 # 3. Specifying --uncrustify=off, which does not run uncrustify.
54 # 4. Specifying --copyright=<mode>, which alters the level of copyright
55 # checking is done:
56 # off: does not check copyright headers at all
57 # year: only update copyright year in new-format copyright headers
58 # add: in addition to 'year', add copyright headers to files that
59 # don't have any
60 # update: in addition to 'year' and 'add', also update new-format
61 # copyright headers if they are broken or outdated.
62 # replace: replace any copyright header with a new-format copyright
63 # header
64 # full: do all of the above
65 # By default, update-* refuses to update "dirty" files (i.e., that differ
66 # between the disk and the index) to make it easy to revert the changes.
67 # This can be overridden by adding a -f/--force option.
69 # The location of the uncrustify executable must be specified using an
70 # environment variable UNCRUSTIFY. Note that to produce the indentation used
71 # in the source tree, you need a custom version of uncrustify.
72 # To get and configure the currently used version, you can do the following:
73 # 1. Run
74 # git clone -b gromacs git://github.com/rolandschulz/uncrustify.git
75 # cd uncrustify
76 # ./configure
77 # make
78 # 2. Copy the binary src/uncrustify into a directory of your choice.
79 # 3. Set the UNCRUSTIFY environment variable to point to the copied binary.
81 # To identify which files to run through uncrustify, the script uses git
82 # filters, specified in .gitattributes files. Only files that have the filter
83 # set as "uncrustify" (or something ending in "uncrustify") are processed: if
84 # other files have been changed, they are ignored by the script. Files passed
85 # to uncrustify, as well as files with filter "copyright", get their copyright
86 # header checked. To only run uncrustify for a file, set the filter to
87 # "uncrustify_only".
89 # If you want to run uncrustify automatically for changes you make, there are
90 # two options:
91 # 1. Copy the git-pre-commit script in this directory to .git/hooks/pre-commit
92 # and set
93 # git config hooks.uncrustifymode check
94 # git config hooks.uncrustifypath /path/to/uncrustify
95 # See comments in the hook script for more information.
96 # 2. Configure a git filter (doesn't require this script, only the
97 # .gitattributes files):
98 # git config filter.uncrustify.clean \
99 # "/path/to/uncrustify -c admin/uncrustify.cfg -q -l cpp"
100 # The pre-commit hook + manually running the script gives better/more intuitive
101 # control (with the filter, it is possible to have a work tree that is
102 # different from HEAD and still have an empty 'git diff') and provides better
103 # performance for changes that modify many files. It is the only way that
104 # currently also checks the copyright headers.
105 # The filter allows one to transparently merge branches that have not been run
106 # through uncrustify, and is applied more consistently (the pre-commit hook is
107 # not run for every commit, e.g., during a rebase).
109 # Parse command-line arguments
110 function usage() {
111 echo "usage: uncrustify.sh [-f|--force] [--rev=REV]"
112 echo " [--uncrustify=(off|check)] [--copyright=<cmode>] [<action>]"
113 echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
114 echo "<cmode>: off|add|update*|replace|full"
117 action="check-workdir"
118 declare -a diffargs
119 baserev="HEAD"
120 force=
121 uncrustify_mode=check
122 copyright_mode=update
123 for arg in "$@" ; do
124 if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
125 "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
126 "$arg" == "update-index" || "$arg" == "update-workdir" ]]
127 then
128 action=$arg
129 elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
130 action=$arg-workdir
131 elif [[ "$action" == diff-* ]] ; then
132 diffargs+=("$arg")
133 elif [[ "$arg" == --uncrustify=* ]] ; then
134 uncrustify_mode=${arg#--uncrustify=}
135 if [[ "$uncrustify_mode" != "off" && "$uncrustify_mode" != "check" ]] ; then
136 echo "Unknown option: $arg"
137 echo
138 usage
139 exit 2
141 elif [[ "$arg" == --copyright=* ]] ; then
142 copyright_mode=${arg#--copyright=}
143 elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
144 force=1
145 elif [[ "$arg" == --rev=* ]] ; then
146 baserev=${arg#--rev=}
147 elif [[ "$arg" == "-h" ]] ; then
148 usage
149 exit 0
150 else
151 echo "Unknown option: $arg"
152 echo
153 usage
154 exit 2
156 done
158 # Check that uncrustify is present
159 if [[ "$uncrustify_mode" != "off" ]]
160 then
161 if [ -z "$UNCRUSTIFY" ]
162 then
163 echo "Please set the path to uncrustify using UNCRUSTIFY."
164 echo "Note that you need a custom version of uncrustify."
165 echo "See comments in the script file for how to get one."
166 exit 2
168 if ! which "$UNCRUSTIFY" 1>/dev/null
169 then
170 echo "Uncrustify not found: $UNCRUSTIFY"
171 exit 2
175 # Switch to the root of the source tree and check the config file
176 srcdir=`git rev-parse --show-toplevel`
177 cd $srcdir
178 admin_dir=$srcdir/admin
179 cfg_file=$admin_dir/uncrustify.cfg
180 if [ ! -f "$cfg_file" ]
181 then
182 echo "Uncrustify configuration file not found: $cfg_file"
183 exit 2
186 # Actual processing starts: create a temporary directory
187 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
189 # Produce a list of changed files
190 # Only include files that have proper filter set in .gitattributes
191 internal_diff_args=
192 if [[ $action == *-index ]]
193 then
194 internal_diff_args="--cached"
196 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
197 cut -f2 <$tmpdir/difflist | \
198 git check-attr --stdin filter | \
199 sed -e 's/.*: filter: //' | \
200 paste $tmpdir/difflist - | \
201 grep -E '(uncrustify|uncrustify_only|copyright)$' >$tmpdir/filtered
202 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
203 grep -E '(uncrustify|uncrustify_only)$' <$tmpdir/filtered | \
204 cut -f2 >$tmpdir/filelist_uncrustify
205 grep -E '(uncrustify|copyright)$' <$tmpdir/filtered | \
206 cut -f2 >$tmpdir/filelist_copyright
207 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
209 # Extract changed files to a temporary directory
210 mkdir $tmpdir/org
211 if [[ $action == *-index ]] ; then
212 git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist_all
213 else
214 rsync --files-from=$tmpdir/filelist_all $srcdir $tmpdir/org
216 # Duplicate the original files to a separate directory, where all changes will
217 # be made.
218 cp -r $tmpdir/org $tmpdir/new
220 # Create output file for what was done (in case no messages get written)
221 touch $tmpdir/messages
223 # Run uncrustify on the temporary directory
224 cd $tmpdir/new
225 if [[ $uncrustify_mode != "off" ]] ; then
226 if ! $UNCRUSTIFY -c $cfg_file -F $tmpdir/filelist_uncrustify --no-backup >$tmpdir/uncrustify.out 2>&1 ; then
227 echo "Reformatting failed. Check uncrustify output below for errors:"
228 cat $tmpdir/uncrustify.out
229 rm -rf $tmpdir
230 exit 2
232 # Find the changed files if necessary
233 if [[ $action != diff-* ]] ; then
234 msg="needs uncrustify"
235 if [[ $action == update-* ]] ; then
236 msg="uncrustified"
238 git diff --no-index --name-only ../org/ . | \
239 awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
241 # TODO: Consider checking whether rerunning uncrustify causes additional changes
244 # Update the copyright headers using the requested mode
245 if [[ $copyright_mode != "off" ]] ; then
246 cpscript_args="--update-year"
247 case "$copyright_mode" in
248 year)
250 add)
251 cpscript_args+=" --add-missing"
253 update)
254 cpscript_args+=" --add-missing --update-header"
256 replace)
257 cpscript_args+=" --replace-header"
259 full)
260 cpscript_args+=" --add-missing --update-header --replace-header"
263 echo "Unknown copyright mode: $copyright_mode"
264 exit 2
265 esac
266 if [[ $action == check-* ]] ; then
267 cpscript_args+=" --check"
269 # TODO: Probably better to invoke python explicitly through a customizable
270 # variable.
271 if ! $admin_dir/copyright.py -F $tmpdir/filelist_copyright $cpscript_args >>$tmpdir/messages
272 then
273 echo "Copyright checking failed!"
274 rm -rf $tmpdir
275 exit 2
279 cd $tmpdir
281 # If a diff was requested, show it and we are done
282 if [[ $action == diff-* ]] ; then
283 git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
284 rm -rf $tmpdir
285 exit 0
288 # Find the changed files
289 changes=
290 set -o pipefail
291 if ! git diff --no-index --name-only --exit-code org/ new/ | \
292 sed -e 's#new/##' > $tmpdir/changed
293 then
294 changes=1
297 # Check if changed files have changed outside the index
298 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
299 then
300 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
301 >> $tmpdir/messages
302 if [[ ! $force && $action == update-* ]] ; then
303 echo "Modified files found in work tree, skipping update. Use -f to override."
304 echo "The following would have been done:"
305 sort $tmpdir/messages
306 rm -rf $tmpdir
307 exit 2
311 # Update the index/work tree if requested
312 if [[ $action == update-index ]] ; then
313 grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
314 cd $tmpdir/new
315 IFS='
317 for change in `cut -f2 $tmpdir/tohash | \
318 git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
319 paste - $tmpdir/tohash`
321 # NOTE: the patterns below contain literal tabs
322 sha1=${change%% *}
323 rest=${change#* }
324 mode=${rest:8:6}
325 path=${rest#* }
326 path=${path%% *}
327 # Contains a literal tab
328 echo "$mode $sha1 $path" >> $tmpdir/toindex
329 done
330 unset IFS
331 git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
332 elif [[ $action == update-workdir ]] ; then
333 rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
336 # Report what was done
337 sort $tmpdir/messages
339 rm -rf $tmpdir
340 exit $changes