-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.sh
executable file
·110 lines (92 loc) · 2.56 KB
/
lint.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
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
#!/usr/bin/env bash
set -eo pipefail
postTagSystemRoot=$(cd "$(dirname "$0")" && pwd)
cd "$postTagSystemRoot"
lsfilesOptions=(
--cached
--others # untracked files
--exclude-standard # exclude .gitignore
'*'
':(exclude)*.png'
':(exclude)Dependencies/*'
':(exclude)libPostTagSystem/WolframHeaders/*'
':(exclude)*.xcodeproj/*' # Xcode manages these automatically
# data files
':(exclude)*.postcrib'
':(exclude)*.postinit'
':(exclude)*.postresult'
)
mapfile -t filesToLint < <(LC_ALL=C comm -13 <(git ls-files --deleted) <(git ls-files "${lsfilesOptions[@]}"))
formatInPlace=0
for arg in "$@"; do
case $arg in
-i)
formatInPlace=1
shift
;;
*)
echo "Argument $arg is not recognized."
echo
echo "Usage: ./lint.sh [-i]"
echo "Analyze the C++ code with clang-format and cpplint."
echo
echo "Options:"
echo " -i Inplace edit files with clang-format."
exit 1
;;
esac
done
exitStatus=0
for file in "${filesToLint[@]}"; do
if [[ "$file" == *.cpp || "$file" == *.hpp || "$file" == *.h ]]; then
cppFiles+=("$file")
elif grep -rIl '^#![[:blank:]]*/usr/bin/env bash' "$file" >/dev/null; then
# Some bash files don't use .sh extension, so find by shebang
bashFiles+=("$file")
elif [[ "$file" == *.md ]]; then
markdownFiles+=("$file")
else
remainingFiles+=("$file")
fi
done
# Formatting
red="\\\033[0;31m"
green="\\\033[0;32m"
endColor="\\\033[0m"
function formatWithCommand() {
local command="$1"
local file="$2"
diff=$(diff -U0 --label "$file" "$file" --label formatted <("$command" "$file") || :)
if [ $formatInPlace -eq 1 ]; then
"$command" -i "$file"
fi
if [[ -n "$diff" ]]; then
echo -e "$(echo -e "$diff\n\n" | sed "s|^-|$red-|g" | sed "s|^+|$green+|g" | sed "s|$|$endColor|g")"
exitStatus=1
fi
}
for file in "${cppFiles[@]}"; do
formatWithCommand clang-format "$file"
done
for file in "${remainingFiles[@]}"; do
formatWithCommand ./scripts/whitespaceFormat.sh "$file"
done
if [ $exitStatus -eq 1 ]; then
echo "Found formatting errors. Run ./lint.sh -i to automatically fix by applying the printed patch."
fi
# Linting
for file in "${cppFiles[@]}"; do
cpplint --quiet --extensions=hpp,cpp "$file" || exitStatus=1
done
widthLimit=120
checkLineWidthOutput=$(
for file in "${remainingFiles[@]}" "${bashFiles[@]}"; do
./scripts/checkLineWidth.sh "$file" "$widthLimit"
done
)
if [ -n "$checkLineWidthOutput" ]; then
exitStatus=1
echo "Found lines exceeding the maximum allowed length of $widthLimit:"
echo "$checkLineWidthOutput"
fi
exit $exitStatus