be833e3312429ae111b8596aa19f904a9b2ad7c1
[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,2014,2015,2019,2020, 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 is intended as a pre-commit hook that optionally runs all
37 # changes through some formatting check.  Currently, it runs uncrustify and
38 # checks copyright headers.
39 #
40 # It needs to be copied as .git/hooks/pre-commit and configured with
41 #   git config hooks.uncrustifypath /path/to/uncrustify
42 #   git config hooks.uncrustifymode check
43 #   git config hooks.copyrightmode update
44 #
45 # To disable the hook temporarily for a commit, set NO_FORMAT_CHECK environment
46 # variable.  For example,
47 #   NO_FORMAT_CHECK=1 git commit -a
48 # You can also run git commit --no-verify, but that also disables other hooks.
49 #
50 # See docs/dev-manual/code-formatting.rst for more details.
51
52 if [ ! -z "$NO_FORMAT_CHECK" ]
53 then
54     exit 0
55 fi
56
57 if git rev-parse --verify HEAD >/dev/null 2>&1
58 then
59     against=HEAD
60 else
61     # Initial commit: diff against an empty tree object
62     against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
63 fi
64
65 # Redirect output to stderr.
66 exec 1>&2
67
68 clangtidy_mode=`git config hooks.clangtidymode`
69 clangformat_mode=`git config hooks.clangformatmode`
70 copyright_mode=`git config hooks.copyrightmode`
71 if [ -z "$uncrustify_mode" ]
72 then
73     clangtidy_mode=off
74 fi
75 if [ -z "$clangformat_mode" ]
76 then
77     clangformat_mode=off
78 fi
79 if [ -z "$copyright_mode" ]
80 then
81     copyright_mode=off
82 fi
83
84 if [[ -f admin/clang-tidy.sh && \
85       ( "$clangtidy_mode" != "off" ) ]]
86 then
87     runclangtidy_path=`git config hooks.runclangtidypath`
88     if [ -z "$runclangtidy_path" ]
89     then
90         echo "Please set the path to run-clang-tidy using 'git config hooks.runclangtidypath'."
91         echo "Note that you need at least clang-tidy-9."
92         exit 1
93     fi
94     export RUN_CLANG_TIDY="$runclangtidy_path"
95     admin/clang-tidy.sh check-index --rev=$against \
96         --tidy="$clangtidy_mode"
97     stat=$?
98     if [ $stat -eq 1 ] ; then
99         exit 1
100     elif [ $stat -ne 0 ] ; then
101         echo "Source code checking with clang-tidy failed"
102         exit 1
103     fi
104 fi
105
106 if [[ -f admin/clang-format.sh && \
107       ( "$clangformat_mode" != "off" ) ]]
108 then
109     clangformat_path=`git config hooks.clangformatpath`
110     if [ -z "$clangformat_path" ]
111     then
112         echo "Please set the path to clang-format using 'git config hooks.clangformatpath'."
113         exit 1
114     fi
115     export CLANG_FORMAT="$clangformat_path"
116     admin/clang-format.sh check-index --rev=$against \
117         --format="$clangformat_mode"
118     stat=$?
119     if [ $stat -eq 1 ] ; then
120         exit 1
121     elif [ $stat -ne 0 ] ; then
122         echo "Source code formatting check with clang-format failed"
123         exit 1
124     fi
125 fi
126
127 if [[ -f admin/copyright.sh && \
128       ( "$copyright_mode" != "off" ) ]]
129 then
130     admin/copyright.sh check-index --rev=$against \
131         --copyright="$copyright_mode"
132     stat=$?
133     if [ $stat -eq 1 ] ; then
134         exit 1
135     elif [ $stat -ne 0 ] ; then
136         echo "Copyright information check failed"
137         exit 1
138     fi
139 fi