Fixed typo in GMXRC.csh.cmakein
[gromacs.git] / admin / clang-format.sh
blobae8f61dfbde939d1e6441f8c0f9bdfd3d190ed4e
1 #!/bin/bash
3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2019, 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 clang format and copyright header checks on modified files and
37 # reports/applies the necessary changes.
39 # See `clang-format.sh -h` for a brief usage, and docs/dev-manual/code-formatting.rst
40 # for more details.
42 # Parse command-line arguments
43 function usage() {
44 echo "usage: clang-format.sh [-f|--force] [--rev=REV]"
45 echo " [--format=(off|check)]"
46 echo " [--warnings=<file>] [<action>]"
47 echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
50 action="check-workdir"
51 declare -a diffargs
52 baserev="HEAD"
53 force=
54 format_mode=check
55 warning_file=
56 for arg in "$@" ; do
57 if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
58 "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
59 "$arg" == "update-index" || "$arg" == "update-workdir" ]]
60 then
61 action=$arg
62 elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
63 action=$arg-workdir
64 elif [[ "$action" == diff-* ]] ; then
65 diffargs+=("$arg")
66 elif [[ "$arg" == --format=* ]] ; then
67 format_mode=${arg#--format=}
68 if [[ "$format_mode" != "off" && "$format_mode" != "check" ]] ; then
69 echo "Unknown option: $arg"
70 echo
71 usage
72 exit 2
74 elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
75 force=1
76 elif [[ "$arg" == --rev=* ]] ; then
77 baserev=${arg#--rev=}
78 elif [[ "$arg" == --warnings=* ]] ; then
79 warning_file=${arg#--warnings=}
80 elif [[ "$arg" == "-h" || "$arg" == "--help" ]] ; then
81 usage
82 exit 0
83 else
84 echo "Unknown option: $arg"
85 echo
86 usage
87 exit 2
89 done
91 # Check that format is present
92 if [[ "$format_mode" != "off" ]]
93 then
94 if [ -z "$CLANG_FORMAT" ]
95 then
96 CLANG_FORMAT=`git config hooks.clangformatpath`
98 if [ -z "$CLANG_FORMAT" ]
99 then
100 echo "Please set the path to clang-format using the git hook"
101 echo "git config hooks.clang_formatpath /path/to/clang-format"
102 echo "or by setting an environment variable, e.g."
103 echo "CLANG_FORMAT=/path/to/clang-format"
104 echo "See docs/dev-manual/code-formatting.rst for how to get clang-format."
105 exit 2
107 if ! which "$CLANG_FORMAT" 1>/dev/null
108 then
109 echo "clang-format not found: $CLANG_FORMAT"
110 exit 2
114 # Switch to the root of the source tree and check the config file
115 srcdir=`git rev-parse --show-toplevel`
116 pushd $srcdir >/dev/null
117 admin_dir=$srcdir/admin
119 # Actual processing starts: create a temporary directory
120 tmpdir=`mktemp -d -t gmxclangformat.XXXXXX`
122 # Produce a list of changed files
123 # Only include files that have proper filter set in .gitattributes
124 internal_diff_args=
125 if [[ $action == *-index ]]
126 then
127 internal_diff_args="--cached"
129 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
130 cut -f2 <$tmpdir/difflist | \
131 git check-attr --stdin filter | \
132 sed -e 's/.*: filter: //' | \
133 paste $tmpdir/difflist - | \
134 grep -E '(complete_formatting|clangformat|copyright|includesort)$' >$tmpdir/filtered
135 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
136 grep -E '(complete_formatting|clangformat)$' <$tmpdir/filtered | \
137 cut -f2 >$tmpdir/filelist_clangformat
138 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
140 # Extract changed files to a temporary directory
141 mkdir $tmpdir/org
142 if [[ $action == *-index ]] ; then
143 git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist_all
144 else
145 rsync --files-from=$tmpdir/filelist_all $srcdir $tmpdir/org
147 # Need to have .clang-format file available somewhere above where we are using it
148 rsync $srcdir/.clang-format $tmpdir/
149 # Duplicate the original files to a separate directory, where all changes will
150 # be made.
151 cp -r $tmpdir/org $tmpdir/new
153 # Create output file for what was done (in case no messages get written)
154 touch $tmpdir/messages
156 # Run clang-format on the temporary directory
157 # Can only perform clang format on a non-empty list of files
158 cd $tmpdir/new
159 if [[ $format_mode != "off" && -s $tmpdir/filelist_clangformat ]] ; then
160 $CLANG_FORMAT -i `cat $tmpdir/filelist_clangformat` >$tmpdir/clang-format.out 2>&1
161 if [ -s $tmpdir/clang-format.out ]; then
162 echo "Reformatting failed. Check format output below for errors:"
163 cat $tmpdir/clang-format.out
164 rm -rf $tmpdir
165 exit 2
167 # Find the changed files if necessary
168 if [[ $action != diff-* ]] ; then
169 msg="needs formatting"
170 if [[ $action == update-* ]] ; then
171 msg="clang-format performed"
173 git diff --no-index --name-only ../org/ . | \
174 awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
176 # TODO: Consider checking whether rerunning clang-format causes additional changes
179 cd $tmpdir
181 # If a diff was requested, show it and we are done
182 if [[ $action == diff-* ]] ; then
183 git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
184 rm -rf $tmpdir
185 exit 0
188 # Find the changed files
189 git diff --no-index --name-only --exit-code org/ new/ | \
190 sed -e 's#new/##' > $tmpdir/changed
191 changes=
192 if [[ -s $tmpdir/changed ]]
193 then
194 changes=1
197 # Check if changed files have changed outside the index
198 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
199 then
200 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
201 >> $tmpdir/messages
202 if [[ ! $force && $action == update-* ]] ; then
203 echo "Modified files found in work tree, skipping update. Use -f to override."
204 echo "The following would have been done:"
205 sort $tmpdir/messages
206 rm -rf $tmpdir
207 exit 2
211 # Update the index/work tree if requested
212 if [[ $action == update-index ]] ; then
213 grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
214 cd $tmpdir/new
215 IFS='
217 for change in `cut -f2 $tmpdir/tohash | \
218 git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
219 paste - $tmpdir/tohash`
221 # NOTE: the patterns below contain literal tabs
222 sha1=${change%% *}
223 rest=${change#* }
224 mode=${rest:8:6}
225 path=${rest#* }
226 path=${path%% *}
227 # Contains a literal tab
228 echo "$mode $sha1 $path" >> $tmpdir/toindex
229 done
230 unset IFS
231 git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
232 elif [[ $action == update-workdir ]] ; then
233 rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
236 # Get back to the original directory
237 popd >/dev/null
239 # Report what was done
240 sort $tmpdir/messages | tee $warning_file
242 rm -rf $tmpdir
243 exit $changes