Skip to content

Commit d64ab4b

Browse files
authored
Merge pull request #258 from sysprog21/precise-cppcheck
Run Cppcheck only when source files change
2 parents 71a0203 + b479ac9 commit d64ab4b

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

scripts/pre-commit.hook

+14-13
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ detect_cc_std() {
9191
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
9292
CPPCHECK_OPTS+=" $(detect_cc_std)"
9393
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
94-
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
94+
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out"
9595

9696
set_colors
9797

@@ -179,8 +179,8 @@ if [ "${#binary_files[@]}" -gt 0 ]; then
179179
printf "${RED}[!]${NC} Binary data found.\n"
180180
fi
181181

182-
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
183-
for FILE in $FILES; do
182+
C_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h|hpp)$")
183+
for FILE in $C_FILES; do
184184
nf=$(git checkout-index --temp $FILE | cut -f 1)
185185
tempdir=$(mktemp -d) || exit 1
186186
newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1
@@ -221,8 +221,7 @@ fi
221221
root=$(git rev-parse --show-toplevel)
222222
banned="([^f]gets\()|(sprintf\()|(strcpy\()"
223223
status=0
224-
for file in $(git diff --staged --name-only | grep -E "\.(c|cc|cpp|h|hh|hpp)\$")
225-
do
224+
for file in $C_FILES; do
226225
filepath="${root}/${file}"
227226
output=$(grep -nrE "${banned}" "${filepath}")
228227
if [ ! -z "${output}" ]; then
@@ -242,7 +241,7 @@ if [ ! -f fmtscan ]; then
242241
throw "Fail to build 'fmtscan' tools"
243242
fi
244243
fi
245-
if git diff --cached --name-only | grep -qiE "\.(c|h|cpp|hpp)$"; then
244+
if [ -n "$C_FILES" ]; then
246245
echo "Running fmtscan..."
247246
./fmtscan
248247
if [ $? -ne 0 ]; then
@@ -251,13 +250,15 @@ if git diff --cached --name-only | grep -qiE "\.(c|h|cpp|hpp)$"; then
251250
fi
252251

253252
# static analysis
254-
echo "Running static analysis..."
255-
$CPPCHECK $CPPCHECK_OPTS >/dev/null
256-
if [ $? -ne 0 ]; then
257-
RETURN=1
258-
echo "" >&2
259-
echo "Fail to pass static analysis." >&2
260-
echo
253+
if [ -n "$C_FILES" ]; then
254+
echo "Running static analysis..."
255+
$CPPCHECK $CPPCHECK_OPTS $C_FILES >/dev/null
256+
if [ $? -ne 0 ]; then
257+
RETURN=1
258+
echo "" >&2
259+
echo "Fail to pass static analysis." >&2
260+
echo
261+
fi
261262
fi
262263

263264
# non-ASCII filenames are not allowed.

0 commit comments

Comments
 (0)