Skip to content

Commit ad13921

Browse files
jservyy214123
authored andcommitted
Utilize common script for pre-commit hook
Change-Id: Id468016065eb19a24375c8cecb0ac6faf631f316
1 parent 8551e30 commit ad13921

File tree

1 file changed

+19
-107
lines changed

1 file changed

+19
-107
lines changed

scripts/pre-commit.hook

Lines changed: 19 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not d
1111
# Build unmatched suppressions for each *.c file.
1212
cppcheck_build_unmatched() {
1313
local file suppression=""
14-
for file in *.c tools/*.c; do
14+
for file in *.c; do
1515
suppression+=" --suppress=unmatchedSuppression:$file"
1616
done
1717
echo "$suppression"
@@ -46,12 +46,11 @@ cppcheck_suppressions() {
4646
"staticFunction:linenoise.c"
4747
"nullPointerOutOfMemory:web.c"
4848
"staticFunction:web.c"
49-
"constParameterCallback:tools/fmtscan.c"
5049
)
5150

5251
# Array for additional cppcheck options (non-suppressions)
5352
local -a other_flags=(
54-
"--inline-suppr harness.c --inline-suppr tools/fmtscan.c"
53+
"--inline-suppr harness.c"
5554
)
5655

5756
local out=""
@@ -69,50 +68,20 @@ cppcheck_suppressions() {
6968
printf "%s" "$out" | sed 's/[[:space:]]*$//'
7069
}
7170

72-
# Generation of standard compliance for GCC/Clang
73-
detect_cc_std() {
74-
local STDC_VERSION=""
75-
local EXTRA_DEFINES=""
76-
if command -v cc >/dev/null 2>&1; then
77-
if cc --version 2>/dev/null | grep -q "clang"; then
78-
STDC_VERSION=$(cc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
79-
EXTRA_DEFINES="-D__clang__=1"
80-
elif cc --version 2>/dev/null | grep -q "Free Software Foundation"; then
81-
STDC_VERSION=$(cc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
82-
EXTRA_DEFINES="-D__GNUC__=1"
83-
fi
84-
fi
85-
if [ -n "$STDC_VERSION" ]; then
86-
EXTRA_DEFINES+=" -D__STDC__=1 -D__STDC_VERSION__=${STDC_VERSION}"
87-
fi
88-
echo "$EXTRA_DEFINES"
89-
}
90-
9171
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
92-
CPPCHECK_OPTS+=" $(detect_cc_std)"
9372
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
94-
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out"
73+
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
9574

9675
set_colors
9776

9877
RETURN=0
9978

10079
# Disallow non-ASCII characters in workspace path
10180
workspace=$(git rev-parse --show-toplevel)
102-
if echo "$workspace" | LC_ALL=C grep -q "[一-龥]"; then
81+
if echo "$workspace" | grep -q "[一-龥]"; then
10382
throw "The workspace path '$workspace' contains non-ASCII characters."
10483
fi
10584

106-
# Check for merge conflict markers in staged changes.
107-
# Assemble the conflict marker regex without embedding it directly.
108-
CONFLICT_MARKERS=$(printf '%s|%s|%s' "<<<<<<<" "=======" ">>>>>>>")
109-
# Get staged files that contain conflict markers, but exclude hook files.
110-
CONFLICT_FILES=$(git diff --cached --name-only -G "${CONFLICT_MARKERS}" | \
111-
grep -vE '(^|/)\.git/hooks/|(^|/)(pre-commit|commit-msg|prepare-commit-msg|pre-push)\.hook$')
112-
if [ -n "${CONFLICT_FILES}" ]; then
113-
throw "Conflict markers are still present in the following files:\n%s" ${CONFLICT_FILES}
114-
fi
115-
11685
CLANG_FORMAT=$(which clang-format)
11786
if [ $? -ne 0 ]; then
11887
throw "clang-format not installed. Unable to check source file format policy."
@@ -179,8 +148,8 @@ if [ "${#binary_files[@]}" -gt 0 ]; then
179148
printf "${RED}[!]${NC} Binary data found.\n"
180149
fi
181150

182-
C_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h|hpp)$")
183-
for FILE in $C_FILES; do
151+
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
152+
for FILE in $FILES; do
184153
nf=$(git checkout-index --temp $FILE | cut -f 1)
185154
tempdir=$(mktemp -d) || exit 1
186155
newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1
@@ -207,47 +176,9 @@ for FILE in $C_FILES; do
207176
fi
208177
done
209178

210-
# Check syntax for changed shell scripts
211-
SHELL_FILES=()
212-
for file in "${FILES[@]}"; do
213-
if [[ "$file" =~ ^scripts/common\.sh$ || "$file" =~ ^scripts/.*\.hook$ ]]; then
214-
SHELL_FILES+=("$file")
215-
fi
216-
done
217-
if [ "${#SHELL_FILES[@]}" -gt 0 ]; then
218-
for file in "${SHELL_FILES[@]}"; do
219-
if ! bash -n "$file"; then
220-
throw "Syntax errors detected in $file." >&2
221-
fi
222-
done
223-
fi
224-
225-
ASPELL_DICT_FILE='scripts/aspell-pws'
226-
if ! LC_ALL=C tail -n +2 $ASPELL_DICT_FILE | sort -f -cdu; then
227-
throw '%s\n%s\n%s' \
228-
'Aspell dictionary is unsorted or contains duplicated entries.' \
229-
'Make sure that by using:' \
230-
" tail -n +2 $ASPELL_DICT_FILE | sort -f -du"
231-
fi
232-
233-
# Show insertion and deletion counts.
234-
if [ "${#FILES[@]}" -gt 0 ]; then
235-
echo "Following files were changed:"
236-
for file in "${FILES[@]}"; do
237-
summary=$(git diff --cached --numstat "$file" | awk '{
238-
if ($1 != "0" && $2 != "0")
239-
printf "%s insertions(+), %s deletions(-)", $1, $2;
240-
else if ($1 != "0")
241-
printf "%s insertions(+)", $1;
242-
else if ($2 != "0")
243-
printf "%s deletions(-)", $2;
244-
}')
245-
if [ -n "$summary" ]; then
246-
echo " - $file : $summary"
247-
else
248-
echo " - $file"
249-
fi
250-
done
179+
if [ ! -z "${FILES[*]}" ]; then
180+
echo "Following files need to be cleaned up:"
181+
echo "${FILES[*]}"
251182
fi
252183

253184
$SHA1SUM -c scripts/checksums 2>/dev/null >/dev/null
@@ -259,7 +190,8 @@ fi
259190
root=$(git rev-parse --show-toplevel)
260191
banned="([^f]gets\()|(sprintf\()|(strcpy\()"
261192
status=0
262-
for file in $C_FILES; do
193+
for file in $(git diff --staged --name-only | grep -E "\.(c|cc|cpp|h|hh|hpp)\$")
194+
do
263195
filepath="${root}/${file}"
264196
output=$(grep -nrE "${banned}" "${filepath}")
265197
if [ ! -z "${output}" ]; then
@@ -272,41 +204,21 @@ for file in $C_FILES; do
272204
fi
273205
done
274206

275-
# format string checks
276-
if [[ "$OSTYPE" != darwin* ]]; then
277-
# Format string checks
278-
if [ ! -f fmtscan ]; then
279-
make fmtscan
280-
if [ ! -f fmtscan ]; then
281-
throw "Fail to build 'fmtscan' tool"
282-
fi
283-
fi
284-
if [ -n "$C_FILES" ]; then
285-
echo "Running fmtscan..."
286-
./fmtscan
287-
if [ $? -ne 0 ]; then
288-
throw "Check format strings for spelling"
289-
fi
290-
fi
291-
fi
292-
293207
# static analysis
294-
if [ -n "$C_FILES" ]; then
295-
echo "Running static analysis..."
296-
$CPPCHECK $CPPCHECK_OPTS $C_FILES >/dev/null
297-
if [ $? -ne 0 ]; then
298-
RETURN=1
299-
echo "" >&2
300-
echo "Fail to pass static analysis." >&2
301-
echo
302-
fi
208+
echo "Running static analysis..."
209+
$CPPCHECK $CPPCHECK_OPTS >/dev/null
210+
if [ $? -ne 0 ]; then
211+
RETURN=1
212+
echo "" >&2
213+
echo "Fail to pass static analysis." >&2
214+
echo
303215
fi
304216

305217
# non-ASCII filenames are not allowed.
306218
# Cross platform projects tend to avoid non-ASCII filenames; prevent
307219
# them from being added to the repository.
308220
if test $(git diff --cached --name-only --diff-filter=A -z $against |
309-
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
221+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
310222
then
311223
cat <<\EOF
312224
ERROR: Attempt to add a non-ASCII file name.

0 commit comments

Comments
 (0)