working snapshot
[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 # This is the git upstream example hook, but it's actually useful.
18
19 #
20 # An example hook script to verify what is about to be committed.
21 # Called by "git commit" with no arguments. The hook should
22 # exit with non-zero status after issuing an appropriate message if
23 # it wants to stop the commit.
24 #
25 # To enable this hook, rename this file to "pre-commit".
26
27 if git rev-parse --verify HEAD >/dev/null 2>&1
28 then
29 against=HEAD
30 else
31 # Initial commit: diff against an empty tree object
32 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
33 fi
34
35 # If you want to allow non-ASCII filenames set this variable to true.
36 allownonascii=$(git config --bool hooks.allownonascii)
37
38 # Redirect output to stderr.
39 exec 1>&2
40
41 # Cross platform projects tend to avoid non-ASCII filenames; prevent
42 # them from being added to the repository. We exploit the fact that the
43 # printable range starts at the space character and ends with tilde.
44 if [[ $allownonascii != true ]] &&
45 # Note that the use of brackets around a tr range is ok here, (it's
46 # even required, for portability to Solaris 10's /usr/bin/tr), since
47 # the square bracket bytes happen to fall in the designated range.
48 test $(git diff --cached --name-only --diff-filter=A -z $against |
49 LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
50 then
51 cat <<EOF
52 Error: Attempt to add a non-ASCII file name.
53 This can cause problems if you want to work with people on other platforms.
54 To be portable it is advisable to rename the file.
55 If you know what you are doing you can disable this check using:
56 git config hooks.allownonascii true
57 EOF
58 exit 1
59 fi
60
61 set -eE -o pipefail
62 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
63
64
65 # inspired from:
66 # https://github.com/ntc2/conf/blob/master/dot.gitconfig
67 # duplicated at,
68 # http://stackoverflow.com/a/15398512/14456
69 # last checked for any changes there on 7/2016
70
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