forked from sysprog21/sehttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit.hook
More file actions
executable file
·69 lines (60 loc) · 1.83 KB
/
Copy pathpre-commit.hook
File metadata and controls
executable file
·69 lines (60 loc) · 1.83 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
#!/usr/bin/env bash
CPPCHECK_suppresses="--suppress=missingInclude \
--suppress=sprintfOverlappingData:src/http.c \
--suppress=unusedFunction:ebpf/http-parse-sample.c \
--inline-suppr"
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1 --force $CPPCHECK_suppresses ."
RETURN=0
CLANG_FORMAT=$(which clang-format)
if [ $? -ne 0 ]; then
echo "[!] clang-format not installed. Unable to check source file format policy." >&2
exit 1
fi
CPPCHECK=$(which cppcheck)
if [ $? -ne 0 ]; then
echo "[!] cppcheck not installed. Unable to perform static analysis." >&2
exit 1
fi
ASPELL=$(which aspell)
if [ $? -ne 0 ]; then
echo "[!] aspell not installed. Unable to do spelling check." >&2
exit 1
fi
DIFF=$(which colordiff)
if [ $? -ne 0 ]; then
DIFF=diff
fi
FILES=`git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$"`
for FILE in $FILES; do
nf=`git checkout-index --temp $FILE | cut -f 1`
tempdir=`mktemp -d` || exit 1
newfile=`mktemp ${tempdir}/${nf}.XXXXXX` || exit 1
basename=`basename $FILE`
source="${tempdir}/${basename}"
mv $nf $source
cp .clang-format $tempdir
$CLANG_FORMAT $source > $newfile 2>> /dev/null
$DIFF -u -p -B --label="modified $FILE" --label="expected coding style" \
"${source}" "${newfile}"
r=$?
rm -rf "${tempdir}"
if [ $r != 0 ] ; then
echo "[!] $FILE does not follow the consistent coding style." >&2
RETURN=1
fi
if [ $RETURN -eq 1 ]; then
echo "" >&2
echo "Make sure you indent as the following:" >&2
echo " clang-format -i $FILE" >&2
echo
fi
done
# static analysis
$CPPCHECK $CPPCHECK_OPTS >/dev/null
if [ $? -ne 0 ]; then
RETURN=1
echo "" >&2
echo "Fail to pass static analysis." >&2
echo
fi
exit $RETURN