-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·77 lines (63 loc) · 2.32 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·77 lines (63 loc) · 2.32 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
#!/bin/bash
#
# Hook script to clean up code and build docs.
# Only .js files are checked.
# Called by "git commit" with no arguments.
# The hook should exit with non-zero status if it wants to stop the commit.
# Loop over staged files and clean them up.
while IFS= read -rd '' file
do
# Special checks for JavaScript sources:
if [[ $file == *.js && $file != *.min.js && $file != public/* && $file != docs/* && $file != build/* ]]
then
echo "Checking: '$file'"
len1=`wc -c < "$file"`
# Remove trailing whitespace on lines.
sed -i 's/\s\+$//' "$file"
len2=`wc -c < "$file"`
[ $len1 != $len2 ] && echo "Removed trailing whitespace in '$file'."
len1=$len2
# Remove leading and trailing blank lines, ensure that file ends in newline.
sed -i '/./,$!d;:a;/^\n*$/{$d;N;};/\n$/ba;$a\' "$file"
len2=`wc -c < "$file"`
[ $len1 != $len2 ] && echo "Removed surrounding empty lines in '$file'."
len1=$len2
# Remove trailing empty lines.
sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$file"
len2=`wc -c < "$file"`
[ $len1 != $len2 ] && echo "Removed trailing whitespace in '$file'."
len1=$len2
# Check for tabs.
if grep -q $'\t' "$file"
then
echo "ABORT: Found tab character in '$file'!"
exit 1
fi
# Remove debugger statements which are alone on a line.
sed -i '/^\s*debugger;\{0,1\}$/d' "$file"
len2=`wc -c < "$file"`
[ $len1 != $len2 ] && echo "Removed debugger statements in '$file'."
len1=$len2
# Check for debugger statements which are not alone on a line.
if grep -q 'debugger' "$file"
then
echo "ABORT: Found complicated debugger statement in '$file'!"
exit 1
fi
# Check for lines wider than 80 characters.
lines=$(grep -n '.\{81,\}' "$file" | cut -d: -f1 | paste -sd',')
if [ -n "$lines" ]
then
echo "ABORT: More than 80 characters in '$file' on line(s) $lines!"
exit 1
fi
fi
# Keep these changes.
git add "$file"
done < <(git diff --cached --name-only --diff-filter=ACM -z)
# Build documentation if possible.
if [ -x bin/make.js -a -d docs ]
then
node bin/make.js doc
git add -u docs/
fi