-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit
executable file
·68 lines (58 loc) · 1.27 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
# Introduce: https://github.com/wolfogre/git-text
# Version: v1.2.0
set -e
function find_wrong_files() {
for FILE in "$@"; do
local FILE_TYPE
FILE_TYPE=$(file --mime-type "$FILE" | awk '{print $2}')
if ! is_text "$FILE_TYPE"; then
echo "$FILE"
fi
done
}
function is_text() {
FILE_TYPE=$1
local MAIN_TYPE=${FILE_TYPE%%/*}
local SUB_TYPE=${FILE_TYPE##*/}
case "$MAIN_TYPE" in
"text")
return 0
;;
"inode")
case "$SUB_TYPE" in
"x-empty"|"directory"|"symlink")
return 0
;;
esac
;;
"application")
case "$SUB_TYPE" in
"json"|"csv")
return 0
;;
esac
;;
"image")
case "$SUB_TYPE" in
"svg+xml")
return 0
;;
esac
;;
esac
return 1
}
FILES=$(git status --short | grep -E "^(A|M)" | awk '{print $2}')
if [[ -z "$FILES" ]]; then
exit 0
fi
WRONG_FILES=$(echo "$FILES" | while read -r LINE; do find_wrong_files "$LINE"; done)
if [[ -n "${WRONG_FILES}" ]]; then
echo "DELETE NON-TEXT FILES OR USE 'git commit -n':"
echo "$WRONG_FILES" | xargs file --mime-type
echo -e "\nIf there are any mistakes, please report to https://github.com/wolfogre/git-text/issues/new"
exit 1
fi
echo "ALL FILES ARE TEXT:"
echo "$FILES" | xargs file --mime-type