2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
9 # Copyright 2024 Ian Kelling
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
23 # Copyright (C) 2016 Ian Kelling
25 # Licensed under the Apache License, Version 2.0 (the "License");
26 # you may not use this file except in compliance with the License.
27 # You may obtain a copy of the License at
29 # http://www.apache.org/licenses/LICENSE-2.0
31 # Unless required by applicable law or agreed to in writing, software
32 # distributed under the License is distributed on an "AS IS" BASIS,
33 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 # See the License for the specific language governing permissions and
35 # limitations under the License.
38 # ian: This section is the git upstream example hook, but it's actually useful.
40 # An example hook script to verify what is about to be committed.
41 # Called by "git commit" with no arguments. The hook should
42 # exit with non-zero status after issuing an appropriate message if
43 # it wants to stop the commit.
45 # To enable this hook, rename this file to "pre-commit".
47 if git rev-parse
--verify HEAD
>/dev
/null
2>&1
51 # Initial commit: diff against an empty tree object
52 against
=4b825dc642cb6eb9a060e54bf8d69288fbee4904
55 # If you want to allow non-ASCII filenames set this variable to true.
56 allownonascii
=$
(git config
--bool hooks.allownonascii
)
58 # Redirect output to stderr.
61 # Cross platform projects tend to avoid non-ASCII filenames; prevent
62 # them from being added to the repository. We exploit the fact that the
63 # printable range starts at the space character and ends with tilde.
64 if [[ $allownonascii != true
]] &&
65 # Note that the use of brackets around a tr range is ok here, (it's
66 # even required, for portability to Solaris 10's /usr/bin/tr), since
67 # the square bracket bytes happen to fall in the designated range.
68 test "$(git diff --cached --name-only --diff-filter=A -z $against |
69 LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0
72 Error: Attempt to add a non-ASCII file name.
73 This can cause problems if you want to work with people on other platforms.
74 To be portable it is advisable to rename the file.
75 If you know what you are doing you can disable this check using:
76 git config hooks.allownonascii true
84 # https://github.com/ntc2/conf/blob/master/dot.gitconfig
85 # last checked for any changes there on 7/2016
87 # http://stackoverflow.com/a/15398512/14456
91 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
93 if [[ $
(git config
--bool hooks.allowwhitespace
) == true
]] || \
94 [[ $GIT_ALLOWWHITESPACE == true
]]; then
98 # bail out when we call ourselves or there are no whitespace issues.
99 if [[ $GIT_ALLOWWHITESPACE == ignore
]] ||
100 ws_issues
=$
(git diff-index
--check --cached $against --); then
104 if [[ -d "$(git rev-parse --git-dir)/rebase-merge" ]] ; then
106 warning: whitespace issues, but rebase in progress, so I can't/won't fix them.
107 If you want to fix them, do for example:
108 git rebase --whitespace=fix HEAD~2
113 # can't do it on the first commit
114 if ! git rev-list HEAD
--count &>/dev
/null
; then
116 Whitespace issues found. We can't fix in a pre-commit hook for the first commit.
117 Either fix on your own. I suggest https://github.com/dlenski/wtf, from git root:
119 git ls-files --exclude-standard -cmo --no-empty-directory | while read -r f; do if [[ -L $f ]] || ! grep -Iq . "$f"; then continue; fi; wtf.py -i -E lf "$f"; done
121 or allow whitespace with:
122 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true
127 echo "Fixing whitespace lines. To avoid this, do:
128 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true"
129 export GIT_ALLOWWHITESPACE
=ignore
130 if ! git diff-files
--quiet .
&& \
131 ! git diff-index
--quiet --cached HEAD
; then
132 # - dirty tree and dirty index
133 git commit
-m "temporary commit of index while fixing whitespace"
135 git commit
-m "temporary commit of non-index while fixing whitespace"
136 git rebase
--whitespace=fix HEAD~
2
138 git
reset --soft HEAD~
139 elif ! git diff-files
--quiet .
; then
140 # - dirty tree and clean index
142 git commit
-m "temporary commit of non-index while fixing whitespace"
143 git rebase
--whitespace=fix HEAD~
145 elif ! git diff-index
--quiet --cached HEAD
; then
146 # - clean tree and dirty index
147 git commit
-m "temporary commit of index while fixing whitespace"
148 git rebase
--whitespace=fix HEAD~
149 git
reset --soft HEAD~