-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·194 lines (161 loc) · 4.47 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·194 lines (161 loc) · 4.47 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# This script is used by the pre-commit hook to check the commited
# files and auto or manual fixit it if needed.
if [[ ${BASH_SOURCE[0]} -ef "$0" ]]; then
# source config and common file
source "${0%/*}/components/config"
source "${0%/*}/components/common"
else
# source from unit test
source "./components/config"
source "./components/common"
fi
## EXEC_BIN is an array of executable binary format
EXEC_BIN=(
"COFF"
"ECOFF"
"XCOFF"
"XCOFF32"
"XCOFF64"
"ELF"
"Mach-O"
"PEF"
"CMD"
"MS-DOS"
"PE"
"PE32+"
)
## check_dnct is a function to check if a DNCT tag is spoted in the commit
check_dnct() {
# check if this flag is enabled
if [[ $DNCT_SUPPORT -eq $FALSE ]]; then
return $SUCCESS
fi
# run the git diff
local _spoted
if ! _spoted="$(git diff --cached --name-only --diff-filter=AM -p -G "$DNCT_REGEX")"; then
error "Unexpected error from git diff check your repository"
return $FAILURE
fi
# check if the commit message has the DNCT tag
if [[ -n $_spoted ]]; then
error "DNCT tag are spoted in commit, see files below to fix it:"
for _file in $_spoted; do
warning "-> $_file"
done
return $FAILURE
fi
return $SUCCESS
}
## check_dsstore is a function to check if a DSStore file is spoted in commit
check_dsstore() {
# check if this flag is enabled
if [[ $FILE_UNCACHE_DSSTORE -eq $FALSE ]]; then
return $SUCCESS
fi
# check if required arguments are provided
if [[ -z $1 ]]; then
return $FAILURE
fi
local _file="$1"
# check if the file is a DSStore file
if grep -i "\.ds_store$" <<<"$_file" >/dev/null; then
git_uncache "$_file" || return $FAILURE
fi
return $SUCCESS
}
## check_binary is a function to check if a binary file is spoted in commit
check_binary() {
# check if this flag is enabled
if [[ $FILE_UNCACHE_BINARY -eq $FALSE ]]; then
return $SUCCESS
fi
# check if required arguments are provided
if [[ -z $1 ]]; then
return $FAILURE
fi
local _file="$1"
# check if the file is a binary file
local _type
if ! _type="$(file -Nb "$_file" | grep -f <(printf "%s\n" "${EXEC_BIN[*]}"))"; then
return $SUCCESS
fi
case $FILE_UNCACHE_BINARY in
"$TRUE")
git_uncache "$_file"
;;
"$SUPERPOSITION")
interactive_uncache "Executable binary" "$_file"
;;
*)
return $SUCCESS
;;
esac
return $FAILURE
}
## check_size is a function to check if file size is greater than the limit
check_size() {
# check if this flag is disabled
if [[ $FILE_MAX_SIZE -lt 1 ]]; then
return $SUCCESS
fi
# check if required arguments are provided
if [[ -z $1 ]]; then
return $FAILURE
fi
local _file="$1"
# take the file size
local _size
if ! _size=$(stat -c%s "$_file" 2>/dev/null); then
return $FAILURE
fi
_size=$((_size / 1024))
# check the file size
if [[ $_size -lt $FILE_MAX_SIZE ]]; then
return $SUCCESS
fi
case $FILE_UNCACHE_OVERSIZE in
"$TRUE")
git_uncache "$_file" && return $SUCCESS
;;
"$SUPERPOSITION")
interactive_uncache \
"${_size}KB exceeds limit of ${FILE_MAX_SIZE}KB for a" "$_file" &&
return $SUCCESS
;;
*)
return $SUCCESS
;;
esac
return $FAILURE
}
## MAIN
# do not execute if this script is sourced required for unit testing
if [[ ${BASH_SOURCE[0]} -ef "$0" ]]; then
# check if a .gitignore exists
if [[ $GITIGNORE_REQUIRED -eq $TRUE ]] && [[ ! -f .gitignore ]]; then
fatal 'To prevent unwanted files from being commited, a .gitignore' \
'file is required.\n> For disabling this check in the current' \
'repository, you can run the following command:\n>' \
'git config mogh.gitignore 0'
fi
# check if a modified or added file is tagged with DNCT
if ! check_dnct; then
exit $FAILURE
fi
for _file in $(git diff --cached --name-only --diff-filter=ATM -p); do
# check .ds_store file
if ! check_dsstore "$_file"; then
continue
fi
# check binary file
if ! check_binary "$_file"; then
continue
fi
# check file size
if ! check_size "$_file"; then
continue
fi
done
exit $SUCCESS
fi