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:
97 find . -not -name .git -type f -exec bash -c \
98 'grep -Il "" "$1" &>/dev/null && wtf.py -i -E lf "$1"' _ {} \;
99 or allow whitespace with:
100 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true
105 echo "Fixing whitespace lines. To avoid this, do:
106 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true"
107 export GIT_ALLOWWHITESPACE
=ignore
108 if ! git diff-files
--quiet .
&& \
109 ! git diff-index
--quiet --cached HEAD
; then
110 # - dirty tree and dirty index
111 git commit
-m "temporary commit of index while fixing whitespace"
113 git commit
-m "temporary commit of non-index while fixing whitespace"
114 git rebase
--whitespace=fix HEAD~
2
116 git
reset --soft HEAD~
117 elif ! git diff-files
--quiet .
; then
118 # - dirty tree and clean index
120 git commit
-m "temporary commit of non-index while fixing whitespace"
121 git rebase
--whitespace=fix HEAD~
123 elif ! git diff-index
--quiet --cached HEAD
; then
124 # - clean tree and dirty index
125 git commit
-m "temporary commit of index while fixing whitespace"
126 git rebase
--whitespace=fix HEAD~
127 git
reset --soft HEAD~