forked from Gibies/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·86 lines (76 loc) · 2.38 KB
/
pre-commit
File metadata and controls
executable file
·86 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
#
# PRE-COMMIT, hook script to verify what is about to be committed
# Copyright (C) 2022-2023 Xavier Delaruelle
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##########################################################################
# redirect output to stderr.
exec 1>&2
# apply SGR code to message if output channel is attached to terminal
sgr() {
local code=$1
local msg=$2
if [ -t 2 ]; then
echo "\033[${code}m${msg}\033[0m"
else
echo "$msg"
fi
}
echo_warning() {
echo -e "$(sgr 43 WARNING): $1"
}
echo_error() {
echo -e "$(sgr 41 ERROR): $1"
}
# fail if there are misspellings (check all files)
command -v codespell >/dev/null
if [ $? -eq 0 ]; then
git diff --cached HEAD | grep '^[+][^+]' | codespell -
if [ $? -ne 0 ]; then
exit 1
fi
else
echo_warning "codespell command not found"
fi
# interactively check misspellings in doc files
command -v hunspell >/dev/null
if [ $? -eq 0 ]; then
misspell=0
HUNSPELL_OPTS=(-d en_US -p .hunspell.en.dic)
for docfile in $(git diff --cached --name-only --diff-filter=d | grep -E\
'(.rst|.md)$'); do
words=$(git diff --cached "$docfile" | grep '^[+][^+]' | hunspell\
"${HUNSPELL_OPTS[@]}" -l)
if [ -n "$words" ]; then
# interactively edit file to fix misspellings
hunspell "${HUNSPELL_OPTS[@]}" "$docfile" </dev/tty >/dev/tty;
misspell=1
fi
done
# abort if misspellings were found as commit content should be adapted
if [ $misspell -eq 1 ]; then
echo_error "misspelling found, commit should be updated"
exit 1
fi
else
echo_warning "hunspell command not found"
fi
# fail if there are whitespace errors
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
exit 1
fi
exit 0
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: