Merge branch release-4-6 into master
[alexxy/gromacs.git] / admin / uncrustify.sh
1 #!/bin/bash
2 #
3 # This file is part of the GROMACS molecular simulation package.
4 #
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.
9 #
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.
14 #
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.
19 #
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.
24 #
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.
32 #
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.
35
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,
38 # and files that
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.
55 #
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:
60 #  1. Run
61 #       git clone -b gromacs git://github.com/rolandschulz/uncrustify.git
62 #       cd uncrustify
63 #       ./configure
64 #       make
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.
67 #
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.
72 #
73 # If you want to run uncrustify automatically for changes you make, there are
74 # two options:
75 #  1. Copy the git-pre-commit script in this directory to .git/hooks/pre-commit
76 #     and set
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).
91
92 # Parse command-line arguments
93 function usage() {
94     echo "usage: uncrustify.sh [-f|--force] [--rev=REV] [action]"
95     echo "actions: (check|diff|update)[-(index|workdir)] (default:check-workdir)"
96 }
97
98 action="check-workdir"
99 declare -a diffargs
100 baserev="HEAD"
101 force=
102 for arg in "$@" ; do
103     if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
104           "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
105           "$arg" == "update-index" || "$arg" == "update-workdir" ]]
106     then
107         action=$arg
108     elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
109         action=$arg-workdir
110     elif [[ "$action" == diff-* ]] ; then
111         diffargs+=("$arg")
112     elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
113         force=1
114     elif [[ "$arg" == --rev=* ]] ; then
115         baserev=${arg#--rev=}
116     elif [[ "$arg" == "-h" ]] ; then
117         usage
118         exit 0
119     else
120         echo "Unknown option: $arg"
121         echo
122         usage
123         exit 2
124     fi
125 done
126
127 # Check that uncrustify is present
128 if [ -z "$UNCRUSTIFY" ]
129 then
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."
133     exit 2
134 fi
135 if ! which "$UNCRUSTIFY" 1>/dev/null
136 then
137     echo "Uncrustify not found: $UNCRUSTIFY"
138     exit 2
139 fi
140
141 # Switch to the root of the source tree and check the config file
142 srcdir=`git rev-parse --show-toplevel`
143 cd $srcdir
144 admin_dir=$srcdir/admin
145 cfg_file=$admin_dir/uncrustify.cfg
146 if [ ! -f "$cfg_file" ]
147 then
148     echo "Uncrustify configuration file not found: $cfg_file"
149     exit 2
150 fi
151
152 # Actual processing starts: create a temporary directory
153 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
154
155 # Produce a list of changed files
156 # Only include files that have uncrustify set as filter in .gitattributes
157 internal_diff_args=
158 if [[ $action == *-index ]]
159 then
160     internal_diff_args="--cached"
161 fi
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
170
171 # Extract changed files to a temporary directory
172 mkdir $tmpdir/org
173 mkdir $tmpdir/new
174 if [[ $action == *-index ]] ; then
175     git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist
176 else
177     rsync --files-from=$tmpdir/filelist $srcdir $tmpdir/org
178 fi
179
180 # Run uncrustify on the temporary directory
181 cd $tmpdir/org
182
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
186     rm -rf $tmpdir
187     exit 2
188 fi
189 # TODO: Consider checking whether rerunning uncrustify causes additional changes
190
191 cd $tmpdir
192
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/
196     rm -rf $tmpdir
197     exit 0
198 fi
199
200 # Find the changed files
201 touch $tmpdir/messages
202 changes=
203 set -o pipefail
204 if ! git diff --no-index --name-only --exit-code org/ new/ | \
205          sed -e 's#new/##' > $tmpdir/changed
206 then
207     changes=1
208     awk '{print $0 ": needs uncrustify"}' $tmpdir/changed \
209         >> $tmpdir/messages
210 fi
211 # Check if changed files have changed outside the index
212 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
213 then
214     awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
215         >> $tmpdir/messages
216     if [[ ! $force && $action == update-* ]] ; then
217         sort $tmpdir/messages
218         echo "Modified files found in work tree, skipping update."
219         rm -rf $tmpdir
220         exit 2
221     fi
222 fi
223
224 # Update the index/work tree if requested
225 if [[ $action == update-index ]] ; then
226     grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
227     cd $tmpdir/new
228     IFS='
229 '
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`
233     do
234         # NOTE: the patterns below contain literal tabs
235         sha1=${change%% *}
236         rest=${change#* }
237         mode=${rest:8:6}
238         path=${rest#*   }
239         path=${path%%   *}
240         # Contains a literal tab
241         echo "$mode $sha1       $path" >> $tmpdir/toindex
242     done
243     unset IFS
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/
247 fi
248
249 # Report what was done
250 if [[ $action == update-* ]] ; then
251     sort $tmpdir/messages | sed -e 's/needs uncrustify/uncrustified/'
252 else
253     sort $tmpdir/messages
254 fi
255
256 rm -rf $tmpdir
257 exit $changes