Skip to content

Commit 4f8d453

Browse files
supperthomasGuozhanxin
authored andcommitted
add cmd check
1 parent 8de0bef commit 4f8d453

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

.hooks/pre-commit

+34-23
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,57 @@ changed_files=$(git diff-index --cached $against | \
1414
grep -E '[MA] .*\.(c|cpp|cc|cxx)$' | cut -d' ' -f 2)
1515

1616

17-
if [ -n "$changed_files" ]; then
18-
cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --platform=win64 --force $changed_files
19-
err=$?
20-
if [ $err -ne 0 ]; then
21-
echo "[rt-thread][cppcheck] we found a obvious fault, please fix the error then commit again"
22-
exit $err
23-
else
24-
echo "[rt-thread][cppcheck] cppcheck ok."
17+
if which cppcheck > /dev/null; then
18+
if [ -n "$changed_files" ]; then
19+
cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --platform=win64 --force $changed_files
20+
err=$?
21+
if [ $err -ne 0 ]; then
22+
echo "[rt-thread][cppcheck] we found a obvious fault, please fix the error then commit again"
23+
exit $err
24+
else
25+
echo "[rt-thread][cppcheck] cppcheck ok."
26+
fi
2527
fi
28+
else
29+
echo "cppcheck does not exist"
2630
fi
2731

2832
# We only filter the file name with c or cpp or h file.
2933
# astyle only astyle the added file[because of code reivewer], if you want change modify file, please use [MA]
3034
changed_files=$(git diff-index --cached $against | \
3135
grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2)
3236

33-
34-
if [ -n "$changed_files" ]; then
35-
astyle --style=allman --indent=spaces=4 --indent=spaces=4 --indent=spaces=4 --pad-header --pad-header --pad-header --align-pointer=name --lineend=linux --convert-tabs --verbose $changed_files
36-
err=$?
37-
if [ $err -ne 0 ]; then
38-
echo "[rt-thread][astyle] we found a obvious fault, please fix the error then commit again"
39-
exit $err
40-
else
41-
echo "[rt-thread][astyle] astyle file ok"
37+
if which astyle > /dev/null; then
38+
if [ -n "$changed_files" ]; then
39+
astyle --style=allman --indent=spaces=4 --indent=spaces=4 --indent=spaces=4 --pad-header --pad-header --pad-header --align-pointer=name --lineend=linux --convert-tabs --verbose $changed_files
40+
err=$?
41+
if [ $err -ne 0 ]; then
42+
echo "[rt-thread][astyle] we found a obvious fault, please fix the error then commit again"
43+
exit $err
44+
else
45+
echo "[rt-thread][astyle] astyle file ok"
46+
fi
4247
fi
48+
else
49+
echo "astyle does not exist"
4350
fi
4451

45-
4652
# We only filter the file name with c or cpp file.
4753
changed_files=$(git diff-index --cached $against | \
4854
grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2)
4955
# formatting check
5056
# https://github.com/mysterywolf/formatting
5157
# formatting cmd ref https://github.com/supperthomas/git_auto_script
52-
if [ -n "$changed_files" ]; then
53-
formatting $changed_files
54-
echo "[rt-thread] formatting $changed_files is ok"
55-
git add $changed_files
56-
exit 0
58+
if which formatting > /dev/null; then
59+
if [ -n "$changed_files" ]; then
60+
formatting $changed_files
61+
echo "[rt-thread] formatting $changed_files is ok"
62+
git add $changed_files
63+
exit 0
64+
fi
65+
else
66+
echo "formatting does not exist"
5767
fi
68+
5869
exit 0
5970

0 commit comments

Comments
 (0)