various improvements
[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
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
99
100 or allow whitespace with:
101 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true
102 EOF
103 echo "$ws_issues"
104 exit 1
105 fi
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"
113 git add -u :/
114 git commit -m "temporary commit of non-index while fixing whitespace"
115 git rebase --whitespace=fix HEAD~2
116 git reset HEAD~ &&
117 git reset --soft HEAD~
118 elif ! git diff-files --quiet .; then
119 # - dirty tree and clean index
120 git add -u :/
121 git commit -m "temporary commit of non-index while fixing whitespace"
122 git rebase --whitespace=fix HEAD~
123 git reset 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~
129 fi