Script for running uncrustify for modified files.
[alexxy/gromacs.git] / admin / git-pre-commit
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 is intended as a pre-commit hook that optionally runs all
37 # changes through uncrustify.  By default, it does nothing.  To enable the
38 # script after copying it to .git/hooks/pre-commit, you need to set
39 #   git config hooks.uncrustifymode check
40 #   git config hooks.uncrustifypath /path/to/uncrustify
41 # With this configuration, all source files modified in the commit are run
42 # through uncrustify (see admin/uncrustify.sh for how this set of files is
43 # determined).  If any file is changed by uncrustify, the names of those files
44 # are reported and the commit is prevented.
45 # To disable the hook, you can set
46 #   git config hooks.uncrustifymode off
47 #
48 # The actual work is done by the admin/uncrustify.sh script, which gets
49 # run with the 'check-index' action.
50 # See the comments in that script for more information.
51
52 if git rev-parse --verify HEAD >/dev/null 2>&1
53 then
54         against=HEAD
55 else
56         # Initial commit: diff against an empty tree object
57         against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
58 fi
59
60 # Redirect output to stderr.
61 exec 1>&2
62
63 uncrustify_mode=`git config hooks.uncrustifymode`
64
65 if [ -f admin/uncrustify.sh ] && \
66    [ ! -z "$uncrustify_mode" ] && [ "$uncrustify_mode" != "off" ]
67 then
68     uncrustify_path=`git config hooks.uncrustifypath`
69     if [ -z "$uncrustify_path" ]
70     then
71         echo "Please set the path to uncrustify using 'git config hooks.uncrustifypath'."
72         echo "Note that you need a custom version of uncrustify."
73         exit 1
74     fi
75     export UNCRUSTIFY="$uncrustify_path"
76     case "$uncrustify_mode" in
77         check)
78             admin/uncrustify.sh check-index --rev=$against
79             stat=$?
80             if [ $stat -eq 1 ] ; then
81                 exit 1
82             elif [ $stat -ne 0 ] ; then
83                 echo "Source code formatting check failed"
84                 exit 1
85             fi
86             ;;
87         *)
88             echo "Unknown uncrustify mode: $uncrustify_mode"
89             exit 1
90             ;;
91     esac
92 fi