3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2013, 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", get their copyright
88 # If you want to run uncrustify automatically for changes you make, there are
90 # 1. Copy the git-pre-commit script in this directory to .git/hooks/pre-commit
92 # git config hooks.uncrustifymode check
93 # git config hooks.uncrustifypath /path/to/uncrustify
94 # See comments in the hook script for more information.
95 # 2. Configure a git filter (doesn't require this script, only the
96 # .gitattributes files):
97 # git config filter.uncrustify.clean \
98 # "/path/to/uncrustify -c admin/uncrustify.cfg -q -l cpp"
99 # The pre-commit hook + manually running the script gives better/more intuitive
100 # control (with the filter, it is possible to have a work tree that is
101 # different from HEAD and still have an empty 'git diff') and provides better
102 # performance for changes that modify many files. It is the only way that
103 # currently also checks the copyright headers.
104 # The filter allows one to transparently merge branches that have not been run
105 # through uncrustify, and is applied more consistently (the pre-commit hook is
106 # not run for every commit, e.g., during a rebase).
108 # Parse command-line arguments
110 echo "usage: uncrustify.sh [-f|--force] [--rev=REV]"
111 echo " [--uncrustify=(off|check)] [--copyright=<cmode>] [<action>]"
112 echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
113 echo "<cmode>: off|add|update*|replace|full"
116 action="check-workdir"
120 uncrustify_mode=check
121 copyright_mode=update
123 if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
124 "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
125 "$arg" == "update-index" || "$arg" == "update-workdir" ]]
128 elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
130 elif [[ "$action" == diff-* ]] ; then
132 elif [[ "$arg" == --uncrustify=* ]] ; then
133 uncrustify_mode=${arg#--uncrustify=}
134 if [[ "$uncrustify_mode" != "off" && "$uncrustify_mode" != "check" ]] ; then
135 echo "Unknown option: $arg"
140 elif [[ "$arg" == --copyright=* ]] ; then
141 copyright_mode=${arg#--copyright=}
142 elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
144 elif [[ "$arg" == --rev=* ]] ; then
145 baserev=${arg#--rev=}
146 elif [[ "$arg" == "-h" ]] ; then
150 echo "Unknown option: $arg"
157 # Check that uncrustify is present
158 if [[ "$uncrustify_mode" != "off" ]]
160 if [ -z "$UNCRUSTIFY" ]
162 echo "Please set the path to uncrustify using UNCRUSTIFY."
163 echo "Note that you need a custom version of uncrustify."
164 echo "See comments in the script file for how to get one."
167 if ! which "$UNCRUSTIFY" 1>/dev/null
169 echo "Uncrustify not found: $UNCRUSTIFY"
174 # Switch to the root of the source tree and check the config file
175 srcdir=`git rev-parse --show-toplevel`
177 admin_dir=$srcdir/admin
178 cfg_file=$admin_dir/uncrustify.cfg
179 if [ ! -f "$cfg_file" ]
181 echo "Uncrustify configuration file not found: $cfg_file"
185 # Actual processing starts: create a temporary directory
186 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
188 # Produce a list of changed files
189 # Only include files that have proper filter set in .gitattributes
191 if [[ $action == *-index ]]
193 internal_diff_args="--cached"
195 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
196 cut -f2 <$tmpdir/difflist | \
197 git check-attr --stdin filter | \
198 sed -e 's/.*: filter: //' | \
199 paste $tmpdir/difflist - | \
200 grep -E '(uncrustify|copyright)$' >$tmpdir/filtered
201 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
202 grep 'uncrustify$' <$tmpdir/filtered | cut -f2 >$tmpdir/filelist_uncrustify
203 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
205 # Extract changed files to a temporary directory
207 if [[ $action == *-index ]] ; then
208 git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist_all
210 rsync --files-from=$tmpdir/filelist_all $srcdir $tmpdir/org
212 # Duplicate the original files to a separate directory, where all changes will
214 cp -r $tmpdir/org $tmpdir/new
216 # Create output file for what was done (in case no messages get written)
217 touch $tmpdir/messages
219 # Run uncrustify on the temporary directory
221 if [[ $uncrustify_mode != "off" ]] ; then
222 if ! $UNCRUSTIFY -c $cfg_file -F $tmpdir/filelist_uncrustify --no-backup >$tmpdir/uncrustify.out 2>&1 ; then
223 echo "Reformatting failed. Check uncrustify output below for errors:"
224 cat $tmpdir/uncrustify.out
228 # Find the changed files if necessary
229 if [[ $action != diff-* ]] ; then
230 msg="needs uncrustify"
231 if [[ $action == update-* ]] ; then
234 git diff --no-index --name-only ../org/ . | \
235 awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
237 # TODO: Consider checking whether rerunning uncrustify causes additional changes
240 # Update the copyright headers using the requested mode
241 if [[ $copyright_mode != "off" ]] ; then
242 cpscript_args="--update-year"
243 case "$copyright_mode" in
247 cpscript_args+=" --add-missing"
250 cpscript_args+=" --add-missing --update-header"
253 cpscript_args+=" --replace-header"
256 cpscript_args+=" --add-missing --update-header --replace-header"
259 echo "Unknown copyright mode: $copyright_mode"
261 if [[ $copyright -eq 2 ]] ; then
262 cpscript_args+=" --update-header"
265 if [[ $action == check-* ]] ; then
266 cpscript_args+=" --check"
268 # TODO: Probably better to invoke python explicitly through a customizable
270 if ! $admin_dir/copyright.py -F $tmpdir/filelist_all $cpscript_args >>$tmpdir/messages
272 echo "Copyright checking failed!"
280 # If a diff was requested, show it and we are done
281 if [[ $action == diff-* ]] ; then
282 git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
287 # Find the changed files
290 if ! git diff --no-index --name-only --exit-code org/ new/ | \
291 sed -e 's#new/##' > $tmpdir/changed
296 # Check if changed files have changed outside the index
297 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
299 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
301 if [[ ! $force && $action == update-* ]] ; then
302 echo "Modified files found in work tree, skipping update. Use -f to override."
303 echo "The following would have been done:"
304 sort $tmpdir/messages
310 # Update the index/work tree if requested
311 if [[ $action == update-index ]] ; then
312 grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
316 for change in `cut -f2 $tmpdir/tohash | \
317 git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
318 paste - $tmpdir/tohash`
320 # NOTE: the patterns below contain literal tabs
326 # Contains a literal tab
327 echo "$mode $sha1 $path" >> $tmpdir/toindex
330 git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
331 elif [[ $action == update-workdir ]] ; then
332 rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
335 # Report what was done
336 sort $tmpdir/messages