2 # Copyright (C) 2016 Ian Kelling
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 # ian: This section is the git upstream example hook, but it's actually useful.
19 # An example hook script to verify what is about to be committed.
20 # Called by "git commit" with no arguments. The hook should
21 # exit with non-zero status after issuing an appropriate message if
22 # it wants to stop the commit.
24 # To enable this hook, rename this file to "pre-commit".
26 if git rev-parse
--verify HEAD
>/dev
/null
2>&1
30 # Initial commit: diff against an empty tree object
31 against
=4b825dc642cb6eb9a060e54bf8d69288fbee4904
34 # If you want to allow non-ASCII filenames set this variable to true.
35 allownonascii
=$
(git config
--bool hooks.allownonascii
)
37 # Redirect output to stderr.
40 # Cross platform projects tend to avoid non-ASCII filenames; prevent
41 # them from being added to the repository. We exploit the fact that the
42 # printable range starts at the space character and ends with tilde.
43 if [[ $allownonascii != true
]] &&
44 # Note that the use of brackets around a tr range is ok here, (it's
45 # even required, for portability to Solaris 10's /usr/bin/tr), since
46 # the square bracket bytes happen to fall in the designated range.
47 test $
(git
diff --cached --name-only --diff-filter=A
-z $against |
48 LC_ALL
=C
tr -d '[ -~]\0' |
wc -c) != 0
51 Error: Attempt to add a non-ASCII file name.
52 This can cause problems if you want to work with people on other platforms.
53 To be portable it is advisable to rename the file.
54 If you know what you are doing you can disable this check using:
55 git config hooks.allownonascii true
63 # https://github.com/ntc2/conf/blob/master/dot.gitconfig
64 # last checked for any changes there on 7/2016
66 # http://stackoverflow.com/a/15398512/14456
70 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
72 if [[ $
(git config
--bool hooks.allowwhitespace
) == true
]] || \
73 [[ $GIT_ALLOWWHITESPACE == true
]]; then
77 # bail out when we call ourselves or there are no whitespace issues.
78 if [[ $GIT_ALLOWWHITESPACE == ignore
]] ||
79 ws_issues
=$
(git diff-index
--check --cached $against --); then
83 if [[ -d "$(git rev-parse --git-dir)/rebase-merge" ]] ; then
85 warning: whitespace issues, but rebase in progress, so I can't/won't fix them.
86 If you want to fix them, do for example:
87 git rebase --whitespace=fix HEAD~2
92 # can't do it on the first commit
93 if ! git rev-list HEAD
--count &>/dev
/null
; then
95 Whitespace issues found. We can't fix in a pre-commit hook for the first commit.
96 Either fix on your own. I suggest https://github.com/dlenski/wtf, from git root:
98 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
100 or allow whitespace with:
101 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true
106 echo "Fixing whitespace lines. To avoid this, do:
107 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true"
108 export GIT_ALLOWWHITESPACE
=ignore
109 if ! git diff-files
--quiet .
&& \
110 ! git diff-index
--quiet --cached HEAD
; then
111 # - dirty tree and dirty index
112 git commit
-m "temporary commit of index while fixing whitespace"
114 git commit
-m "temporary commit of non-index while fixing whitespace"
115 git rebase
--whitespace=fix HEAD~
2
117 git
reset --soft HEAD~
118 elif ! git diff-files
--quiet .
; then
119 # - dirty tree and clean index
121 git commit
-m "temporary commit of non-index while fixing whitespace"
122 git rebase
--whitespace=fix HEAD~
124 elif ! git diff-index
--quiet --cached HEAD
; then
125 # - clean tree and dirty index
126 git commit
-m "temporary commit of index while fixing whitespace"
127 git rebase
--whitespace=fix HEAD~
128 git
reset --soft HEAD~