4c2d0f4cf9ea2e5ff5f88b78423f13b2f36ea6b6
[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,2014,2015, 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.
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 and copyright header checks on modified files and
37 # reports/applies the necessary changes.
38 #
39 # See `uncrustify.sh -h` for a brief usage, and docs/dev-manual/uncrustify.md
40 # for more details.
41
42 # Parse command-line arguments
43 function usage() {
44     echo "usage: uncrustify.sh [-f|--force] [--rev=REV]"
45     echo "           [--uncrustify=(off|check)] [--copyright=<cmode>] [<action>]"
46     echo "<action>: (check*|diff|update)[-(index|workdir*)] (*=default)"
47     echo "<cmode>:  off|add|update*|replace|full"
48 }
49
50 action="check-workdir"
51 declare -a diffargs
52 baserev="HEAD"
53 force=
54 uncrustify_mode=check
55 copyright_mode=update
56 for arg in "$@" ; do
57     if [[ "$arg" == "check-index" || "$arg" == "check-workdir" || \
58           "$arg" == "diff-index" || "$arg" == "diff-workdir" || \
59           "$arg" == "update-index" || "$arg" == "update-workdir" ]]
60     then
61         action=$arg
62     elif [[ "$arg" == "check" || "$arg" == "diff" || "$arg" == "update" ]] ; then
63         action=$arg-workdir
64     elif [[ "$action" == diff-* ]] ; then
65         diffargs+=("$arg")
66     elif [[ "$arg" == --uncrustify=* ]] ; then
67         uncrustify_mode=${arg#--uncrustify=}
68         if [[ "$uncrustify_mode" != "off" && "$uncrustify_mode" != "check" ]] ; then
69             echo "Unknown option: $arg"
70             echo
71             usage
72             exit 2
73         fi
74     elif [[ "$arg" == --copyright=* ]] ; then
75         copyright_mode=${arg#--copyright=}
76     elif [[ "$arg" == "-f" || "$arg" == "--force" ]] ; then
77         force=1
78     elif [[ "$arg" == --rev=* ]] ; then
79         baserev=${arg#--rev=}
80     elif [[ "$arg" == "-h" || "$arg" == "--help" ]] ; then
81         usage
82         exit 0
83     else
84         echo "Unknown option: $arg"
85         echo
86         usage
87         exit 2
88     fi
89 done
90
91 # Check that uncrustify is present
92 if [[ "$uncrustify_mode" != "off" ]]
93 then
94     if [ -z "$UNCRUSTIFY" ]
95     then
96         UNCRUSTIFY=`git config hooks.uncrustifypath`
97     fi
98     if [ -z "$UNCRUSTIFY" ]
99     then
100         echo "Please set the path to uncrustify using UNCRUSTIFY or"
101         echo "git config hooks.uncrustifypath."
102         echo "Note that you need a custom version of uncrustify."
103         echo "See docs/dev-manual/uncrustify.md for how to get one."
104         exit 2
105     fi
106     if ! which "$UNCRUSTIFY" 1>/dev/null
107     then
108         echo "Uncrustify not found: $UNCRUSTIFY"
109         exit 2
110     fi
111 fi
112
113 # Switch to the root of the source tree and check the config file
114 srcdir=`git rev-parse --show-toplevel`
115 cd $srcdir
116 admin_dir=$srcdir/admin
117 cfg_file=$admin_dir/uncrustify.cfg
118 if [ ! -f "$cfg_file" ]
119 then
120     echo "Uncrustify configuration file not found: $cfg_file"
121     exit 2
122 fi
123
124 # Actual processing starts: create a temporary directory
125 tmpdir=`mktemp -d -t gmxuncrust.XXXXXX`
126
127 # Produce a list of changed files
128 # Only include files that have proper filter set in .gitattributes
129 internal_diff_args=
130 if [[ $action == *-index ]]
131 then
132     internal_diff_args="--cached"
133 fi
134 git diff-index $internal_diff_args --diff-filter=ACMR $baserev >$tmpdir/difflist
135 cut -f2 <$tmpdir/difflist | \
136     git check-attr --stdin filter | \
137     sed -e 's/.*: filter: //' | \
138     paste $tmpdir/difflist - | \
139     grep -E '(uncrustify|uncrustify_only|copyright|includesort)$' >$tmpdir/filtered
140 cut -f2 <$tmpdir/filtered >$tmpdir/filelist_all
141 grep -E '(uncrustify|uncrustify_only)$' <$tmpdir/filtered | \
142     cut -f2 >$tmpdir/filelist_uncrustify
143 grep -E '(uncrustify|copyright|includesort)$' <$tmpdir/filtered | \
144     cut -f2 >$tmpdir/filelist_copyright
145 git diff-files --name-only | grep -Ff $tmpdir/filelist_all >$tmpdir/localmods
146
147 # Extract changed files to a temporary directory
148 mkdir $tmpdir/org
149 if [[ $action == *-index ]] ; then
150     git checkout-index --prefix=$tmpdir/org/ --stdin <$tmpdir/filelist_all
151 else
152     rsync --files-from=$tmpdir/filelist_all $srcdir $tmpdir/org
153 fi
154 # Duplicate the original files to a separate directory, where all changes will
155 # be made.
156 cp -r $tmpdir/org $tmpdir/new
157
158 # Create output file for what was done (in case no messages get written)
159 touch $tmpdir/messages
160
161 # Run uncrustify on the temporary directory
162 cd $tmpdir/new
163 if [[ $uncrustify_mode != "off" ]] ; then
164     if ! $UNCRUSTIFY -c $cfg_file -F $tmpdir/filelist_uncrustify --no-backup >$tmpdir/uncrustify.out 2>&1 ; then
165         echo "Reformatting failed. Check uncrustify output below for errors:"
166         cat $tmpdir/uncrustify.out
167         rm -rf $tmpdir
168         exit 2
169     fi
170     # Find the changed files if necessary
171     if [[ $action != diff-* ]] ; then
172         msg="needs uncrustify"
173         if [[ $action == update-* ]] ; then
174             msg="uncrustified"
175         fi
176         git diff --no-index --name-only ../org/ . | \
177             awk -v msg="$msg" '{sub(/.\//,""); print $0 ": " msg}' >> $tmpdir/messages
178     fi
179     # TODO: Consider checking whether rerunning uncrustify causes additional changes
180 fi
181
182 # Update the copyright headers using the requested mode
183 if [[ $copyright_mode != "off" ]] ; then
184     cpscript_args="--update-year"
185     case "$copyright_mode" in
186         year)
187             ;;
188         add)
189             cpscript_args+=" --add-missing"
190             ;;
191         update)
192             cpscript_args+=" --add-missing --update-header"
193             ;;
194         replace)
195             cpscript_args+=" --replace-header"
196             ;;
197         full)
198             cpscript_args+=" --add-missing --update-header --replace-header"
199             ;;
200         *)
201             echo "Unknown copyright mode: $copyright_mode"
202             exit 2
203     esac
204     if [[ $action == check-* ]] ; then
205         cpscript_args+=" --check"
206     fi
207     # TODO: Probably better to invoke python explicitly through a customizable
208     # variable.
209     if ! $admin_dir/copyright.py -F $tmpdir/filelist_copyright $cpscript_args >>$tmpdir/messages
210     then
211         echo "Copyright checking failed!"
212         rm -rf $tmpdir
213         exit 2
214     fi
215 fi
216
217 cd $tmpdir
218
219 # If a diff was requested, show it and we are done
220 if [[ $action == diff-* ]] ; then
221     git diff --no-index --no-prefix "${diffargs[@]}" org/ new/
222     rm -rf $tmpdir
223     exit 0
224 fi
225
226 # Find the changed files
227 git diff --no-index --name-only --exit-code org/ new/ | \
228     sed -e 's#new/##' > $tmpdir/changed
229 changes=
230 if [[ -s $tmpdir/changed ]]
231 then
232     changes=1
233 fi
234
235 # Check if changed files have changed outside the index
236 if grep -Ff $tmpdir/localmods $tmpdir/changed > $tmpdir/conflicts
237 then
238     awk '{print $0 ": has changes in work tree"}' $tmpdir/conflicts \
239         >> $tmpdir/messages
240     if [[ ! $force && $action == update-* ]] ; then
241         echo "Modified files found in work tree, skipping update. Use -f to override."
242         echo "The following would have been done:"
243         sort $tmpdir/messages
244         rm -rf $tmpdir
245         exit 2
246     fi
247 fi
248
249 # Update the index/work tree if requested
250 if [[ $action == update-index ]] ; then
251     grep -Ff $tmpdir/changed $tmpdir/filtered > $tmpdir/tohash
252     cd $tmpdir/new
253     IFS='
254 '
255     for change in `cut -f2 $tmpdir/tohash | \
256                    git --git-dir=$srcdir/.git hash-object -w --stdin-paths --no-filters | \
257                    paste - $tmpdir/tohash`
258     do
259         # NOTE: the patterns below contain literal tabs
260         sha1=${change%% *}
261         rest=${change#* }
262         mode=${rest:8:6}
263         path=${rest#*   }
264         path=${path%%   *}
265         # Contains a literal tab
266         echo "$mode $sha1       $path" >> $tmpdir/toindex
267     done
268     unset IFS
269     git --git-dir=$srcdir/.git update-index --index-info < $tmpdir/toindex
270 elif [[ $action == update-workdir ]] ; then
271     rsync --files-from=$tmpdir/changed $tmpdir/new/ $srcdir/
272 fi
273
274 # Report what was done
275 sort $tmpdir/messages
276
277 rm -rf $tmpdir
278 exit $changes