From 407d43efbb0884180af3e3be84b0f3a785c15e48 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Tue, 11 Mar 2025 23:13:49 +0800 Subject: [PATCH 1/2] Avoid repeated staging change detects In Git, staging changes serves as a critical step that prepares specific modifications for the next commit. There were unintended detects for staging changes in the pre-commit hook, which can be safely avoided. Change-Id: I361d721dd0d3127327f74f3d7110b45b8acec5f9 --- scripts/pre-commit.hook | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/pre-commit.hook b/scripts/pre-commit.hook index 2848c0318..8c9753cbc 100755 --- a/scripts/pre-commit.hook +++ b/scripts/pre-commit.hook @@ -179,8 +179,8 @@ if [ "${#binary_files[@]}" -gt 0 ]; then printf "${RED}[!]${NC} Binary data found.\n" fi -FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$") -for FILE in $FILES; do +C_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h|hpp)$") +for FILE in $C_FILES; do nf=$(git checkout-index --temp $FILE | cut -f 1) tempdir=$(mktemp -d) || exit 1 newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1 @@ -221,8 +221,7 @@ fi root=$(git rev-parse --show-toplevel) banned="([^f]gets\()|(sprintf\()|(strcpy\()" status=0 -for file in $(git diff --staged --name-only | grep -E "\.(c|cc|cpp|h|hh|hpp)\$") -do +for file in $C_FILES; do filepath="${root}/${file}" output=$(grep -nrE "${banned}" "${filepath}") if [ ! -z "${output}" ]; then @@ -242,7 +241,7 @@ if [ ! -f fmtscan ]; then throw "Fail to build 'fmtscan' tools" fi fi -if git diff --cached --name-only | grep -qiE "\.(c|h|cpp|hpp)$"; then +if [ -n "$C_FILES" ]; then echo "Running fmtscan..." ./fmtscan if [ $? -ne 0 ]; then From b479ac93076a237d5913cd92c9e560e49444af77 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Tue, 11 Mar 2025 23:24:19 +0800 Subject: [PATCH 2/2] Run Cppcheck only when source files change Static analysis is essential for C/C++ projects, but running 'fmtscan' can significantly slow down Cppcheck. The git pre-commit hook detects staged files and run Cppcheck only if C/C++ files have been modified, resulting in a noticeable reduction in analysis time. Change-Id: I872c0b6fc9008cc2b3c96283e458b25cf506eb16 --- scripts/pre-commit.hook | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scripts/pre-commit.hook b/scripts/pre-commit.hook index 8c9753cbc..2d301fdaf 100755 --- a/scripts/pre-commit.hook +++ b/scripts/pre-commit.hook @@ -91,7 +91,7 @@ detect_cc_std() { CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1" CPPCHECK_OPTS+=" $(detect_cc_std)" CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)" -CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ." +CPPCHECK_OPTS+=" --cppcheck-build-dir=.out" set_colors @@ -250,13 +250,15 @@ if [ -n "$C_FILES" ]; then fi # static analysis -echo "Running static analysis..." -$CPPCHECK $CPPCHECK_OPTS >/dev/null -if [ $? -ne 0 ]; then - RETURN=1 - echo "" >&2 - echo "Fail to pass static analysis." >&2 - echo +if [ -n "$C_FILES" ]; then + echo "Running static analysis..." + $CPPCHECK $CPPCHECK_OPTS $C_FILES >/dev/null + if [ $? -ne 0 ]; then + RETURN=1 + echo "" >&2 + echo "Fail to pass static analysis." >&2 + echo + fi fi # non-ASCII filenames are not allowed.