host info updates
[distro-setup] / .git_template / hooks / pre-commit
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23 # Copyright (C) 2016 Ian Kelling
24
25 # Licensed under the Apache License, Version 2.0 (the "License");
26 # you may not use this file except in compliance with the License.
27 # You may obtain a copy of the License at
28
29 # http://www.apache.org/licenses/LICENSE-2.0
30
31 # Unless required by applicable law or agreed to in writing, software
32 # distributed under the License is distributed on an "AS IS" BASIS,
33 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 # See the License for the specific language governing permissions and
35 # limitations under the License.
36
37
38 # ian: This section is the git upstream example hook, but it's actually useful.
39
40 # An example hook script to verify what is about to be committed.
41 # Called by "git commit" with no arguments. The hook should
42 # exit with non-zero status after issuing an appropriate message if
43 # it wants to stop the commit.
44 #
45 # To enable this hook, rename this file to "pre-commit".
46
47 if git rev-parse --verify HEAD >/dev/null 2>&1
48 then
49 against=HEAD
50 else
51 # Initial commit: diff against an empty tree object
52 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
53 fi
54
55 # If you want to allow non-ASCII filenames set this variable to true.
56 allownonascii=$(git config --bool hooks.allownonascii)
57
58 # Redirect output to stderr.
59 exec 1>&2
60
61 # Cross platform projects tend to avoid non-ASCII filenames; prevent
62 # them from being added to the repository. We exploit the fact that the
63 # printable range starts at the space character and ends with tilde.
64 if [[ $allownonascii != true ]] &&
65 # Note that the use of brackets around a tr range is ok here, (it's
66 # even required, for portability to Solaris 10's /usr/bin/tr), since
67 # the square bracket bytes happen to fall in the designated range.
68 test "$(git diff --cached --name-only --diff-filter=A -z $against |
69 LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0
70 then
71 cat <<EOF
72 Error: Attempt to add a non-ASCII file name.
73 This can cause problems if you want to work with people on other platforms.
74 To be portable it is advisable to rename the file.
75 If you know what you are doing you can disable this check using:
76 git config hooks.allownonascii true
77 EOF
78 exit 1
79 fi
80
81
82
83 # ian: inspired from:
84 # https://github.com/ntc2/conf/blob/master/dot.gitconfig
85 # last checked for any changes there on 7/2016
86 # duplicated at,
87 # http://stackoverflow.com/a/15398512/14456
88
89
90 set -eE -o pipefail
91 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
92
93 if [[ $(git config --bool hooks.allowwhitespace) == true ]] || \
94 [[ $GIT_ALLOWWHITESPACE == true ]]; then
95 exit 0
96 fi
97
98 # bail out when we call ourselves or there are no whitespace issues.
99 if [[ $GIT_ALLOWWHITESPACE == ignore ]] ||
100 ws_issues=$(git diff-index --check --cached $against --); then
101 echo "$ws_issues"
102 exit 0
103 fi
104 if [[ -d "$(git rev-parse --git-dir)/rebase-merge" ]] ; then
105 cat <<'EOF'
106 warning: whitespace issues, but rebase in progress, so I can't/won't fix them.
107 If you want to fix them, do for example:
108 git rebase --whitespace=fix HEAD~2
109 EOF
110 echo "$ws_issues"
111 exit 0
112 fi
113 # can't do it on the first commit
114 if ! git rev-list HEAD --count &>/dev/null; then
115 cat <<'EOF'
116 Whitespace issues found. We can't fix in a pre-commit hook for the first commit.
117 Either fix on your own. I suggest https://github.com/dlenski/wtf, from git root:
118
119 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
120
121 or allow whitespace with:
122 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true
123 EOF
124 echo "$ws_issues"
125 exit 1
126 fi
127 echo "Fixing whitespace lines. To avoid this, do:
128 git config hooks.allowwhitespace true, or export GIT_ALLOWWHITESPACE=true"
129 export GIT_ALLOWWHITESPACE=ignore
130 if ! git diff-files --quiet . && \
131 ! git diff-index --quiet --cached HEAD; then
132 # - dirty tree and dirty index
133 git commit -m "temporary commit of index while fixing whitespace"
134 git add -u :/
135 git commit -m "temporary commit of non-index while fixing whitespace"
136 git rebase --whitespace=fix HEAD~2
137 git reset HEAD~ &&
138 git reset --soft HEAD~
139 elif ! git diff-files --quiet .; then
140 # - dirty tree and clean index
141 git add -u :/
142 git commit -m "temporary commit of non-index while fixing whitespace"
143 git rebase --whitespace=fix HEAD~
144 git reset HEAD~
145 elif ! git diff-index --quiet --cached HEAD; then
146 # - clean tree and dirty index
147 git commit -m "temporary commit of index while fixing whitespace"
148 git rebase --whitespace=fix HEAD~
149 git reset --soft HEAD~
150 fi