Debian Codegraph @

admin/git-pre-commit

raw
1 #!/usr/bin/env bash
2 #
3 # This file is part of the GROMACS molecular simulation package.
4 #
5 # Copyright 2013- The GROMACS Authors
6 # and the project initiators Erik Lindahl, Berk Hess and David van der Spoel.
7 # Consult the AUTHORS/COPYING files and https://www.gromacs.org for details.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # https://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at https://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out https://www.gromacs.org.
34
35 # This script is intended as a pre-commit hook that optionally runs all
36 # changes through some formatting check. Currently, it runs clang-format and
37 # checks copyright headers.
38 #
39 # It needs to be copied as .git/hooks/pre-commit and configured with
40 # git config hooks.clangformatpath /path/to/clang-format-18
41 # git config hooks.clangformatmode check
42 # git config hooks.copyrightmode update
43 #
44 # To disable the hook temporarily for a commit, set NO_FORMAT_CHECK environment
45 # variable. For example,
46 # NO_FORMAT_CHECK=1 git commit -a
47 # You can also run git commit --no-verify, but that also disables other hooks.
48 #
49 # See docs/dev-manual/code-formatting.rst for more details.
50
51 if [ ! -z "$NO_FORMAT_CHECK" ]
52 then
53 exit 0
54 fi
55
56 if git rev-parse --verify HEAD >/dev/null 2>&1
57 then
58 against=HEAD
59 else
60 # Initial commit: diff against an empty tree object
61 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
62 fi
63
64 # Redirect output to stderr.
65 exec 1>&2
66
67 clangtidy_mode=`git config hooks.clangtidymode`
68 clangformat_mode=`git config hooks.clangformatmode`
69 copyright_mode=`git config hooks.copyrightmode`
70 if [ -z "$clangtidy_mode" ]
71 then
72 clangtidy_mode=off
73 fi
74 if [ -z "$clangformat_mode" ]
75 then
76 clangformat_mode=off
77 fi
78 if [ -z "$copyright_mode" ]
79 then
80 copyright_mode=off
81 fi
82
83 if [[ -f admin/clang-tidy.sh && \
84 ( "$clangtidy_mode" != "off" ) ]]
85 then
86 runclangtidy_path=`git config hooks.runclangtidypath`
87 if [ -z "$runclangtidy_path" ]
88 then
89 echo "Please set the path to run-clang-tidy using 'git config hooks.runclangtidypath'."
90 echo "Note that you should use clang-tidy-18 to get the same checking as in CI."
91 exit 1
92 fi
93 export RUN_CLANG_TIDY="$runclangtidy_path"
94 admin/clang-tidy.sh check-index --rev=$against \
95 --tidy="$clangtidy_mode"
96 stat=$?
97 if [ $stat -eq 1 ] ; then
98 exit 1
99 elif [ $stat -ne 0 ] ; then
100 echo "Source code checking with clang-tidy failed"
101 exit 1
102 fi
103 fi
104
105 if [[ -f admin/clang-format.sh && \
106 ( "$clangformat_mode" != "off" ) ]]
107 then
108 clangformat_path=`git config hooks.clangformatpath`
109 if [ -z "$clangformat_path" ]
110 then
111 echo "Please set the path to clang-format using 'git config hooks.clangformatpath /path/to/clang-format'."
112 echo "See https://manual.gromacs.org/current/dev-manual/code-formatting.html#gmx-clang-format"
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