Separate management of GPU contexts from modules
[gromacs.git] / admin / uncrustify.sh
blob92517bf7d2bf6fc74b932b8f5e70dee516273a1a
1 #!/bin/bash
3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2013,2014,2015, 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 and copyright header checks on modified files and
37 # reports/applies the necessary changes.
39 # See `uncrustify.sh -h` for a brief usage, and docs/dev-manual/uncrustify.rst
40 # for more details.
42 # Parse command-line arguments
43 function usage() {
44 echo "usage: uncrustify.sh [-f|--force] [--rev=REV]"
45 echo " [--uncrustify=(off|check)] [--copyright=<cmode>]"
46 echo " [--warnings=<file>] [<action>]"
47 echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
48 echo "<cmode>: off|add|update*|replace|full"
51 action="check-workdir"
52 declare -a diffargs
53 baserev="HEAD"
54 force=
55 uncrustify_mode=check
56 copyright_mode=update
57 warning_file=
58 for arg in "$@" ; do
59 if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
60 "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
61 "$arg" == "update-index" || "$arg" == "update-workdir" ]]
62 then
63 action=$arg
64 elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
65 action=$arg-workdir
66 elif [[ "$action" == diff-* ]] ; then
67 diffargs+=("$arg")
68 elif [[ "$arg" == --uncrustify=* ]] ; then
69 uncrustify_mode=${arg#--uncrustify=}
70 if [[ "$uncrustify_mode" != "off" && "$uncrustify_mode" != "check" ]] ; then
71 echo "Unknown option: $arg"
72 echo
73 usage
74 exit 2
76 elif [[ "$arg" == --copyright=* ]] ; then
77 copyright_mode=${arg#--copyright=}
78 elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
79 force=1
80 elif [[ "$arg" == --rev=* ]] ; then
81 baserev=${arg#--rev=}
82 elif [[ "$arg" == --warnings=* ]] ; then
83 warning_file=${arg#--warnings=}
84 elif [[ "$arg" == "-h" || "$arg" == "--help" ]] ; then
85 usage
86 exit 0
87 else
88 echo "Unknown option: $arg"
89 echo
90 usage
91 exit 2
93 done
95 # Check that uncrustify is present
96 if [[ "$uncrustify_mode" != "off" ]]
97 then
98 if [ -z "$UNCRUSTIFY" ]
99 then
100 UNCRUSTIFY=`git config hooks.uncrustifypath`
102 if [ -z "$UNCRUSTIFY" ]
103 then
104 echo "Please set the path to uncrustify using UNCRUSTIFY or"
105 echo "git config hooks.uncrustifypath."
106 echo "Note that you need a custom version of uncrustify."
107 echo "See docs/dev-manual/uncrustify.rst for how to get one."
108 exit 2
110 if ! which "$UNCRUSTIFY" 1>/dev/null
111 then
112 echo "Uncrustify not found: $UNCRUSTIFY"
113 exit 2
117 # Switch to the root of the source tree and check the config file
118 srcdir=`git rev-parse --show-toplevel`
119 pushd $srcdir >/dev/null
120 admin_dir=$srcdir/admin
121 cfg_file=$admin_dir/uncrustify.cfg
122 if [ ! -f "$cfg_file" ]
123 then
124 echo "Uncrustify configuration file not found: $cfg_file"
125 exit 2
128 # Actual processing starts: create a temporary directory
129 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
131 # Produce a list of changed files
132 # Only include files that have proper filter set in .gitattributes
133 internal_diff_args=
134 if [[ $action == *-index ]]
135 then
136 internal_diff_args="--cached"
138 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
139 cut -f2 <$tmpdir/difflist | \
140 git check-attr --stdin filter | \
141 sed -e 's/.*: filter: //' | \
142 paste $tmpdir/difflist - | \
143 grep -E '(uncrustify|uncrustify_only|copyright|includesort)$' >$tmpdir/filtered
144 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
145 grep -E '(uncrustify|uncrustify_only)$' <$tmpdir/filtered | \
146 cut -f2 >$tmpdir/filelist_uncrustify
147 grep -E '(uncrustify|copyright|includesort)$' <$tmpdir/filtered | \
148 cut -f2 >$tmpdir/filelist_copyright
149 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
151 # Extract changed files to a temporary directory
152 mkdir $tmpdir/org
153 if [[ $action == *-index ]] ; then
154 git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist_all
155 else
156 rsync --files-from=$tmpdir/filelist_all $srcdir $tmpdir/org
158 # Duplicate the original files to a separate directory, where all changes will
159 # be made.
160 cp -r $tmpdir/org $tmpdir/new
162 # Create output file for what was done (in case no messages get written)
163 touch $tmpdir/messages
165 # Run uncrustify on the temporary directory
166 cd $tmpdir/new
167 if [[ $uncrustify_mode != "off" ]] ; then
168 if ! $UNCRUSTIFY -c $cfg_file -F $tmpdir/filelist_uncrustify --no-backup >$tmpdir/uncrustify.out 2>&1 ; then
169 echo "Reformatting failed. Check uncrustify output below for errors:"
170 cat $tmpdir/uncrustify.out
171 rm -rf $tmpdir
172 exit 2
174 # Find the changed files if necessary
175 if [[ $action != diff-* ]] ; then
176 msg="needs uncrustify"
177 if [[ $action == update-* ]] ; then
178 msg="uncrustified"
180 git diff --no-index --name-only ../org/ . | \
181 awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
183 # TODO: Consider checking whether rerunning uncrustify causes additional changes
186 # Update the copyright headers using the requested mode
187 if [[ $copyright_mode != "off" ]] ; then
188 cpscript_args="--update-year"
189 case "$copyright_mode" in
190 year)
192 add)
193 cpscript_args+=" --add-missing"
195 update)
196 cpscript_args+=" --add-missing --update-header"
198 replace)
199 cpscript_args+=" --replace-header"
201 full)
202 cpscript_args+=" --add-missing --update-header --replace-header"
205 echo "Unknown copyright mode: $copyright_mode"
206 exit 2
207 esac
208 if [[ $action == check-* ]] ; then
209 cpscript_args+=" --check"
211 # TODO: Probably better to invoke python explicitly through a customizable
212 # variable.
213 if ! $admin_dir/copyright.py -F $tmpdir/filelist_copyright $cpscript_args >>$tmpdir/messages
214 then
215 echo "Copyright checking failed!"
216 rm -rf $tmpdir
217 exit 2
221 cd $tmpdir
223 # If a diff was requested, show it and we are done
224 if [[ $action == diff-* ]] ; then
225 git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
226 rm -rf $tmpdir
227 exit 0
230 # Find the changed files
231 git diff --no-index --name-only --exit-code org/ new/ | \
232 sed -e 's#new/##' > $tmpdir/changed
233 changes=
234 if [[ -s $tmpdir/changed ]]
235 then
236 changes=1
239 # Check if changed files have changed outside the index
240 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
241 then
242 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
243 >> $tmpdir/messages
244 if [[ ! $force && $action == update-* ]] ; then
245 echo "Modified files found in work tree, skipping update. Use -f to override."
246 echo "The following would have been done:"
247 sort $tmpdir/messages
248 rm -rf $tmpdir
249 exit 2
253 # Update the index/work tree if requested
254 if [[ $action == update-index ]] ; then
255 grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
256 cd $tmpdir/new
257 IFS='
259 for change in `cut -f2 $tmpdir/tohash | \
260 git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
261 paste - $tmpdir/tohash`
263 # NOTE: the patterns below contain literal tabs
264 sha1=${change%% *}
265 rest=${change#* }
266 mode=${rest:8:6}
267 path=${rest#* }
268 path=${path%% *}
269 # Contains a literal tab
270 echo "$mode $sha1 $path" >> $tmpdir/toindex
271 done
272 unset IFS
273 git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
274 elif [[ $action == update-workdir ]] ; then
275 rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
278 # Get back to the original directory
279 popd >/dev/null
281 # Report what was done
282 sort $tmpdir/messages | tee $warning_file
284 rm -rf $tmpdir
285 exit $changes