182dcb9af9ea5fcac76786e4bf293c677afbd5ed
[distro-setup] / .git_template / hooks / pre-commit
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
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
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16
17 # ian: This section is the git upstream example hook, but it's actually useful.
18
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.
23 #
24 # To enable this hook, rename this file to "pre-commit".
25
26 if git rev-parse --verify HEAD >/dev/null 2>&1
27 then
28 against=HEAD
29 else
30 # Initial commit: diff against an empty tree object
31 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
32 fi
33
34 # If you want to allow non-ASCII filenames set this variable to true.
35 allownonascii=$(git config --bool hooks.allownonascii)
36
37 # Redirect output to stderr.
38 exec 1>&2
39
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
49 then
50 cat <<EOF
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
56 EOF
57 exit 1
58 fi
59
60
61
62 # ian: inspired from:
63 # https://github.com/ntc2/conf/blob/master/dot.gitconfig
64 # last checked for any changes there on 7/2016
65 # duplicated at,
66 # http://stackoverflow.com/a/15398512/14456
67
68
69 set -eE -o pipefail
70 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
71
72 if [[ $(git config --bool hooks.allowwhitespace) == true ]] || \
73 [[ $GIT_ALLOWWHITESPACE == true ]]; then
74 exit 0
75 fi
76
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
80 echo "$ws_issues"
81 exit 0
82 fi
83 if [[ -d "$(git rev-parse --git-dir)/rebase-merge" ]] ; then
84 cat <<'EOF'
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
88 EOF
89 echo "$ws_issues"
90 exit 0
91 fi
92 # can't do it on the first commit
93 if ! git rev-list HEAD --count &>/dev/null; then
94 cat <<'EOF'
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
101 EOF
102 echo "$ws_issues"
103 exit 1
104 fi
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"
112 git add -u :/
113 git commit -m "temporary commit of non-index while fixing whitespace"
114 git rebase --whitespace=fix HEAD~2
115 git reset HEAD~ &&
116 git reset --soft HEAD~
117 elif ! git diff-files --quiet .; then
118 # - dirty tree and clean index
119 git add -u :/
120 git commit -m "temporary commit of non-index while fixing whitespace"
121 git rebase --whitespace=fix HEAD~
122 git reset 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~
128 fi