-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcheck-style.sh
executable file
·76 lines (69 loc) · 1.48 KB
/
check-style.sh
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
#!/bin/bash
set -eo pipefail
# check-style.sh
#
# SUMMARY
#
# Checks that all text files have correct line endings and no trailing spaces.
exit_code=0
if [ "$1" == "--fix" ]; then
mode="fix"
else
mode="check"
fi
function ised() {
# In-place `sed` that uses the form of `sed -i` which works
# on both GNU and macOS.
sed -i.bak "$1" "$2"
rm "$2.bak"
}
for i in $(git ls-files); do
# ignore binary files
case $i in
*png) continue;;
*svg) continue;;
*ico) continue;;
*sig) continue;;
test-data*) continue;;
tests/data*) continue;;
website/plugins/*) continue;;
website/sidebars.js) continue;;
esac
# check that the file contains trailing newline
if [ -n "$(tail -c1 $i | tr -d $'\n')" ]; then
case $mode in
check)
echo "File \"$i\" doesn't end with a newline"
exit_code=1
;;
fix)
echo >> $i
;;
esac
fi
# check that the file uses LF line breaks
if grep $'\r$' $i > /dev/null; then
case $mode in
check)
echo "File \"$i\" contains CRLF line breaks instead of LF line breaks"
exit_code=1
;;
fix)
ised 's/\r$//' $i
;;
esac
fi
# check that the lines don't contain trailing spaces
if grep ' $' $i > /dev/null; then
case $mode in
check)
echo "File \"$i\" contains trailing spaces in some of the lines"
exit_code=1
;;
fix)
ised 's/ *$//' $i
;;
esac
fi
done
exit $exit_code