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
42 # Parse command-line arguments
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"
57 if [[ "$arg" == "check-index" ||
"$arg" == "check-workdir" || \
58 "$arg" == "diff-index" ||
"$arg" == "diff-workdir" || \
59 "$arg" == "update-index" ||
"$arg" == "update-workdir" ]]
62 elif [[ "$arg" == "check" ||
"$arg" == "diff" ||
"$arg" == "update" ]] ; then
64 elif [[ "$action" == diff-
* ]] ; then
66 elif [[ "$arg" == --format=* ]] ; then
67 format_mode
=${arg#--format=}
68 if [[ "$format_mode" != "off" && "$format_mode" != "check" ]] ; then
69 echo "Unknown option: $arg"
74 elif [[ "$arg" == "-f" ||
"$arg" == "--force" ]] ; then
76 elif [[ "$arg" == --rev=* ]] ; then
78 elif [[ "$arg" == --warnings=* ]] ; then
79 warning_file
=${arg#--warnings=}
80 elif [[ "$arg" == "-h" ||
"$arg" == "--help" ]] ; then
84 echo "Unknown option: $arg"
91 # Check that format is present
92 if [[ "$format_mode" != "off" ]]
94 if [ -z "$CLANG_FORMAT" ]
96 CLANG_FORMAT
=`git config hooks.clangformatpath`
98 if [ -z "$CLANG_FORMAT" ]
100 echo "Please set the path to clang-format using the git hook"
101 echo "git config hooks.clangformatpath /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."
107 if ! which "$CLANG_FORMAT" 1>/dev
/null
109 echo "clang-format not found: $CLANG_FORMAT"
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
125 if [[ $action == *-index ]]
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
142 if [[ $action == *-index ]] ; then
143 git checkout-index
--prefix=$tmpdir/org
/ --stdin <$tmpdir/filelist_all
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
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
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
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
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
/
188 # Find the changed files
189 git
diff --no-index --name-only --exit-code org
/ new
/ | \
190 sed -e 's#new/##' > $tmpdir/changed
192 if [[ -s $tmpdir/changed
]]
197 # Check if changed files have changed outside the index
198 if grep -Ff $tmpdir/localmods
$tmpdir/changed
> $tmpdir/conflicts
200 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
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
211 # Update the index/work tree if requested
212 if [[ $action == update-index
]] ; then
213 grep -Ff $tmpdir/changed
$tmpdir/filtered
> $tmpdir/tohash
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
227 # Contains a literal tab
228 echo "$mode $sha1 $path" >> $tmpdir/toindex
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
239 # Report what was done
240 sort $tmpdir/messages |
tee $warning_file