Debian Codegraph @

bin/dune-git-whitespace-hook

raw
1 #!/bin/sh
2 # dune-git-whitespace-hook
3 # DO NOT TOUCH THE PRECEDING LINE
4 # It is used by dunecontrol to enable automatic updates of the whitespace hook
5 #
6 # SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
7 # SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
8 #
9 # DUNE pre-commit hook to enforce whitespace policy
10 # This hook prevents adding lines with trailing whitespace and or tab characters
11 # in line indentation for certain files (see the TRAILING_WHITESPACE_DEFAULT and
12 # TAB_IN_INDENT_DEFAULT variables below for the default sets of files that will
13 # be checked).
14 # You can tell the hook which files should be inspected by setting the Git
15 # configuration variables "hooks.whitespace.trailing" and "hooks.whitespace.tabinindent".
16 # Those variables should contain valid Perl regular expressions. The names of modified
17 # files will be matched against those regexes.
18
19 # git-diff-index needs a valid commit to compare to
20 if git rev-parse --verify HEAD >/dev/null 2>&1
21 then
22 against=HEAD
23 else
24 # Initial commit: diff against an empty tree object
25 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
26 fi
27
28
29 # By default, we disallow trailing whitespace for the following files, but the check for C/C++ and CMake sources
30 # happens in the tab-in-indent check to avoid confusing users with duplicate error messages
31 TRAILING_WHITESPACE_DEFAULT='^(dune\.module|README|README\.SVN|COPYING|INSTALL|TODO)$|^[^/]*(\.md|\.pc\.in)$|^doc/.*\.md$'
32
33 # By default, we disallow tabs in indents and trailing whitespace in C/C++ and CMake source files
34 TAB_IN_INDENT_DEFAULT='(^|/)CMakeLists\.txt$|(\.cpp|\.hpp|\.cc|\.hh|\.c|\.h|\.cmake|\.sh|\.py)$'
35
36 # Get user preferences
37 TRAILING_WHITESPACE_FILES=$(git config hooks.whitespace.trailing)
38
39 # Set default regex for disallowing trailing whitespace if the user did not set anything.
40 # We need to check the return value of git-config to distinguish the case
41 # when the user set an empty value
42 if [ $? -ne 0 ];
43 then
44 TRAILING_WHITESPACE_FILES="$TRAILING_WHITESPACE_DEFAULT"
45 fi
46
47
48 TAB_IN_INDENT_FILES=$(git config hooks.whitespace.tabinindent)
49
50 # Set default regex for disallowing tabs if the user did not set anything.
51 # We need to check the return value of git-config to distinguish the case
52 # when the user set an empty value
53 if [ $? -ne 0 ];
54 then
55 TAB_IN_INDENT_FILES="$TAB_IN_INDENT_DEFAULT"
56 fi
57
58
59 # Unfortunately, we have to mess directly with the repository config file,
60 # as git won't honor a custom config file specified by GIT_CONFIG
61
62 # backup repository-local user setting for core.whitespace
63 USER_WHITESPACE=$(git config --local --get core.whitespace)
64 if [ $? -ne 0 ];
65 then
66 USER_HAS_CUSTOM_WHITESPACE=0
67 else
68 USER_HAS_CUSTOM_WHITESPACE=1
69 fi
70
71 # Figure out how to call xargs to make sure it won't invoke its argument with
72 # an empty argument list. BSD xargs will not do that by default, while GNU xargs
73 # needs -r to do the same. So we start by checking whether xargs does the right
74 # thing without options. Now there could be other obscure versions of xargs out
75 # there (on clusters etc.) that behave in yet another way, so we try with -r as
76 # well. If that fails, we throw a big error message at the user.
77
78 # In the following line, xargs should not call false, so the return value should be 0.
79 echo "" | xargs false
80
81 if [ $? -ne 0 ]; then
82 # Let's try with -r
83 echo "" | xargs -r false
84 if [ $? -ne 0 ]; then
85 # Houston, we have a problem
86 if [ -z "$DUNE_WHITESPACE_IGNORE_XARGS" ]; then
87 echo "You seem to be lacking a version of xargs that is compatible to either BSD or GNU!" 1>&2
88 echo "Please file a bug report at http://dune-project.org about this issue with your exact operating system type and version!" 1>&2
89 echo "You can still use this hook by setting the environment variable DUNE_WHITESPACE_IGNORE_XARGS to 1, but please be aware" 1>&2
90 echo "that the hook might create false positives." 1>&2
91 echo "==============================================================" 1>&2
92 echo "Aborting the commit..." 1>&2
93 exit 99
94 else
95 SILENTXARGS=xargs
96 fi
97 else
98 SILENTXARGS="xargs -r"
99 fi
100 else
101 SILENTXARGS=xargs
102 fi
103
104
105 fail=0
106 done=0
107
108 do_cleanup()
109 {
110
111 if [ $done -ne 1 ];
112 then
113 echo "Error while executing whitespace checking pre-commit hook!" 1>&2
114 echo "There might still be whitespace errors in your commit!" 1>&2
115 fi
116
117 if [ $USER_HAS_CUSTOM_WHITESPACE -eq 1 ];
118 then
119 git config --replace-all core.whitespace "$USER_WHITESPACE"
120 else
121 git config --unset core.whitespace
122 fi
123
124 # be nice and let the commit go through if something went wrong along the
125 # way and we did not record a failure
126 exit $fail
127 }
128
129 trap do_cleanup EXIT
130
131 # set custom value
132 git config --replace-all core.whitespace trailing-space
133
134 if [ -z "$TRAILING_WHITESPACE_FILES" ];
135 then
136 git diff-index --check --cached $against --
137 result=$?
138 else
139 export TRAILING_WHITESPACE_FILES
140 git diff-index --cached --name-only $against \
141 | perl -ne 'print if /$ENV{TRAILING_WHITESPACE_FILES}/' \
142 | $SILENTXARGS git diff-index --check --cached $against --
143 result=$?
144 fi
145
146 if [ $result -ne 0 ];
147 then
148 fail=1
149 fi
150
151 git config --replace-all core.whitespace trailing-space,tab-in-indent
152
153 if [ -z "$TAB_IN_INDENT_FILES" ];
154 then
155 git diff-index --check --cached $against --
156 result=$?
157 else
158 export TAB_IN_INDENT_FILES
159 git diff-index --cached --name-only $against \
160 | perl -ne 'print if /$ENV{TAB_IN_INDENT_FILES}/' \
161 | $SILENTXARGS git diff-index --check --cached $against --
162 result=$?
163 fi
164
165 if [ $result -ne 0 ];
166 then
167 fail=1
168 fi
169
170 done=1
171
172 # trap will call the cleanup code