-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·49 lines (39 loc) · 1.44 KB
/
pre-commit
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
#!/bin/bash
# set -x
PROJECT_DIC="$HOME/.scspell/$(git rev-list --parents HEAD | tail -1)/dictionary.txt"
all_files=($(typos --files .))
all_changes=($(git diff --name-only --cached --diff-filter=d | while IFS= read -r n; do echo "./$n"; done))
files_to_check=($(echo ${all_files[@]} ${all_changes[@]} | tr ' ' '\n' | sort | uniq -d | while IFS= read -r n; do echo "$n"; done))
function prepare_scspell_dic
{
if [ ! -f "$PROJECT_DIC" ]; then
mkdir -p "$(dirname "$PROJECT_DIC")"
echo $(git config --get remote.origin.url) > $(dirname "$PROJECT_DIC")/repo.info
touch $PROJECT_DIC
fi
}
function spell_check_typos {
local errno=0
pushd $(git rev-parse --git-dir 2> /dev/null)/.. > /dev/null
typos $(echo ${files_to_check[@]} | while IFS= read -r n; do echo "$n"; done)
errno=$?
popd > /dev/null
if [ $errno -ne 0 ]; then
exit $errno
fi
}
function spell_check_scspell {
pushd $(git rev-parse --git-dir 2> /dev/null)/.. > /dev/null
scspell --report-only --use-builtin-base-dict --override-dictionary "$PROJECT_DIC" --relative-to $PWD $(echo ${files_to_check[@]})
if [ $? -ne 0 ]; then
exec < /dev/tty
scspell --use-builtin-base-dict --override-dictionary "$PROJECT_DIC" --relative-to $PWD $(echo ${files_to_check[@]})
exit
fi
popd > /dev/null
}
if [ ${#files_to_check[@]} -ne 0 ]; then
spell_check_typos
prepare_scspell_dic
spell_check_scspell
fi