3 # This file is part of the GROMACS molecular simulation package.
5 # Copyright (c) 2013, by the GROMACS development team, led by
6 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
7 # others, as listed in the AUTHORS file in the top-level source
8 # 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. By default, the current HEAD commit is compared to the work tree,
39 # 1. are different between these two trees and
40 # 2. change under uncrustify
41 # are reported. This behavior can be changed by
42 # 1. Specifying an --rev=REV argument, which uses REV instead of HEAD as
43 # the base of the comparison.
44 # 2. Specifying an action:
45 # check-* reports the files that uncrustify changes
46 # diff-* prints the actual diff of what would change
47 # update-* applies the changes to the repository
48 # *-workdir operates on the working directory (files on disk)
49 # *-index operates on the index of the repository
50 # For convenience, if you omit the workdir/index suffix, workdir is assumed
51 # (i.e., diff equals diff-workdir).
52 # By default, update-* refuses to update "dirty" files (i.e., that differ
53 # between the disk and the index) to make it easy to revert the changes.
54 # This can be overridden by adding a -f/--force option.
56 # The location of the uncrustify executable must be specified using an
57 # environment variable UNCRUSTIFY. Note that to produce the indentation used
58 # in the source tree, you need a custom version of uncrustify.
59 # To get and configure the currently used version, you can do the following:
61 # git clone -b gromacs git://github.com/rolandschulz/uncrustify.git
65 # 2. Copy the binary src/uncrustify into a directory of your choice.
66 # 3. Set the UNCRUSTIFY environment variable to point to the copied binary.
68 # To identify which files to run through uncrustify, the script uses git
69 # filters, specified in .gitattributes files. Only files that have the filter
70 # set as "uncrustify" (or something ending in "uncrustify") are processed: if
71 # other files have been changed, they are ignored by the script.
73 # If you want to run uncrustify automatically for changes you make, there are
75 # 1. Copy the git-pre-commit script in this directory to .git/hooks/pre-commit
77 # git config hooks.uncrustifymode check
78 # git config hooks.uncrustifypath /path/to/uncrustify
79 # See comments in the hook script for more information.
80 # 2. Configure a git filter (doesn't require this script, only the
81 # .gitattributes files):
82 # git config filter.uncrustify.clean \
83 # "/path/to/uncrustify -c admin/uncrustify.cfg -q -l cpp"
84 # The pre-commit hook + manually running the script gives better/more intuitive
85 # control (with the filter, it is possible to have a work tree that is
86 # different from HEAD and still have an empty 'git diff') and provides better
87 # performance for changes that modify many files.
88 # The filter allows one to transparently merge branches that have not been run
89 # through uncrustify, and is applied more consistently (the pre-commit hook is
90 # not run for every commit, e.g., during a rebase).
92 # Parse command-line arguments
94 echo "usage: uncrustify.sh [-f|--force] [--rev=REV] [action]"
95 echo "actions: (check|diff|update)[-(index|workdir)] (default:check-workdir)"
98 action="check-workdir"
103 if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
104 "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
105 "$arg" == "update-index" || "$arg" == "update-workdir" ]]
108 elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
110 elif [[ "$action" == diff-* ]] ; then
112 elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
114 elif [[ "$arg" == --rev=* ]] ; then
115 baserev=${arg#--rev=}
116 elif [[ "$arg" == "-h" ]] ; then
120 echo "Unknown option: $arg"
127 # Check that uncrustify is present
128 if [ -z "$UNCRUSTIFY" ]
130 echo "Please set the path to uncrustify using UNCRUSTIFY."
131 echo "Note that you need a custom version of uncrustify."
132 echo "See comments in the script file for how to get one."
135 if ! which "$UNCRUSTIFY" 1>/dev/null
137 echo "Uncrustify not found: $UNCRUSTIFY"
141 # Switch to the root of the source tree and check the config file
142 srcdir=`git rev-parse --show-toplevel`
144 admin_dir=$srcdir/admin
145 cfg_file=$admin_dir/uncrustify.cfg
146 if [ ! -f "$cfg_file" ]
148 echo "Uncrustify configuration file not found: $cfg_file"
152 # Actual processing starts: create a temporary directory
153 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
155 # Produce a list of changed files
156 # Only include files that have uncrustify set as filter in .gitattributes
158 if [[ $action == *-index ]]
160 internal_diff_args="--cached"
162 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
163 cut -f2 <$tmpdir/difflist | \
164 git check-attr --stdin filter | \
165 sed -e 's/.*: filter: //' | \
166 paste $tmpdir/difflist - | \
167 grep 'uncrustify$' >$tmpdir/filtered
168 cut -f2 <$tmpdir/filtered >$tmpdir/filelist
169 git diff-files --name-only | grep -Ff $tmpdir/filelist >$tmpdir/localmods
171 # Extract changed files to a temporary directory
174 if [[ $action == *-index ]] ; then
175 git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist
177 rsync --files-from=$tmpdir/filelist $srcdir $tmpdir/org
180 # Run uncrustify on the temporary directory
183 if ! $UNCRUSTIFY -c $cfg_file -F $tmpdir/filelist --prefix=../new/ >$tmpdir/uncrustify.out 2>&1 ; then
184 echo "Reformatting failed. Check uncrustify output below for errors:"
185 cat $tmpdir/uncrustify.out
189 # TODO: Consider checking whether rerunning uncrustify causes additional changes
193 # If a diff was requested, show it and we are done
194 if [[ $action == diff-* ]] ; then
195 git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
200 # Find the changed files
201 touch $tmpdir/messages
204 if ! git diff --no-index --name-only --exit-code org/ new/ | \
205 sed -e 's#new/##' > $tmpdir/changed
208 awk '{print $0 ": needs uncrustify"}' $tmpdir/changed \
211 # Check if changed files have changed outside the index
212 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
214 awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
216 if [[ ! $force && $action == update-* ]] ; then
217 sort $tmpdir/messages
218 echo "Modified files found in work tree, skipping update."
224 # Update the index/work tree if requested
225 if [[ $action == update-index ]] ; then
226 grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
230 for change in `cut -f2 $tmpdir/tohash | \
231 git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
232 paste - $tmpdir/tohash`
234 # NOTE: the patterns below contain literal tabs
240 # Contains a literal tab
241 echo "$mode $sha1 $path" >> $tmpdir/toindex
244 git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
245 elif [[ $action == update-workdir ]] ; then
246 rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
249 # Report what was done
250 if [[ $action == update-* ]] ; then
251 sort $tmpdir/messages | sed -e 's/needs uncrustify/uncrustified/'
253 sort $tmpdir/messages