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,
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
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
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
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:
74 # git clone -b gromacs git://github.com/rolandschulz/uncrustify.git
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" or "includesort", get
86 # their copyright header checked. To only run uncrustify for a file, set the
87 # filter to "uncrustify_only".
89 # If you want to run uncrustify automatically for changes you make, there are
91 # 1. Copy the git-pre-commit script in this directory to .git/hooks/pre-commit
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
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"
121 uncrustify_mode
=check
122 copyright_mode
=update
124 if [[ "$arg" == "check-index" ||
"$arg" == "check-workdir" || \
125 "$arg" == "diff-index" ||
"$arg" == "diff-workdir" || \
126 "$arg" == "update-index" ||
"$arg" == "update-workdir" ]]
129 elif [[ "$arg" == "check" ||
"$arg" == "diff" ||
"$arg" == "update" ]] ; then
131 elif [[ "$action" == diff-
* ]] ; then
133 elif [[ "$arg" == --uncrustify=* ]] ; then
134 uncrustify_mode
=${arg#--uncrustify=}
135 if [[ "$uncrustify_mode" != "off" && "$uncrustify_mode" != "check" ]] ; then
136 echo "Unknown option: $arg"
141 elif [[ "$arg" == --copyright=* ]] ; then
142 copyright_mode
=${arg#--copyright=}
143 elif [[ "$arg" == "-f" ||
"$arg" == "--force" ]] ; then
145 elif [[ "$arg" == --rev=* ]] ; then
146 baserev
=${arg#--rev=}
147 elif [[ "$arg" == "-h" ]] ; then
151 echo "Unknown option: $arg"
158 # Check that uncrustify is present
159 if [[ "$uncrustify_mode" != "off" ]]
161 if [ -z "$UNCRUSTIFY" ]
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."
168 if ! which "$UNCRUSTIFY" 1>/dev
/null
170 echo "Uncrustify not found: $UNCRUSTIFY"
175 # Switch to the root of the source tree and check the config file
176 srcdir
=`git rev-parse --show-toplevel`
178 admin_dir
=$srcdir/admin
179 cfg_file
=$admin_dir/uncrustify.cfg
180 if [ ! -f "$cfg_file" ]
182 echo "Uncrustify configuration file not found: $cfg_file"
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
192 if [[ $action == *-index ]]
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|includesort)$' <$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
211 if [[ $action == *-index ]] ; then
212 git checkout-index
--prefix=$tmpdir/org
/ --stdin <$tmpdir/filelist_all
214 rsync
--files-from=$tmpdir/filelist_all
$srcdir $tmpdir/org
216 # Duplicate the original files to a separate directory, where all changes will
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
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
232 # Find the changed files if necessary
233 if [[ $action != diff-
* ]] ; then
234 msg
="needs uncrustify"
235 if [[ $action == update-
* ]] ; then
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
251 cpscript_args
+=" --add-missing"
254 cpscript_args
+=" --add-missing --update-header"
257 cpscript_args
+=" --replace-header"
260 cpscript_args
+=" --add-missing --update-header --replace-header"
263 echo "Unknown copyright mode: $copyright_mode"
266 if [[ $action == check-
* ]] ; then
267 cpscript_args
+=" --check"
269 # TODO: Probably better to invoke python explicitly through a customizable
271 if ! $admin_dir/copyright.py
-F $tmpdir/filelist_copyright
$cpscript_args >>$tmpdir/messages
273 echo "Copyright checking failed!"
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
/
288 # Find the changed files
289 git
diff --no-index --name-only --exit-code org
/ new
/ | \
290 sed -e 's#new/##' > $tmpdir/changed
292 if [[ -s $tmpdir/changed
]]
297 # Check if changed files have changed outside the index
298 if grep -Ff $tmpdir/localmods
$tmpdir/changed
> $tmpdir/conflicts
300 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
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
311 # Update the index/work tree if requested
312 if [[ $action == update-index
]] ; then
313 grep -Ff $tmpdir/changed
$tmpdir/filtered
> $tmpdir/tohash
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
327 # Contains a literal tab
328 echo "$mode $sha1 $path" >> $tmpdir/toindex
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