Skip to content

Commit 886da67

Browse files
committed
Utilize common script for pre-commit hook
Change-Id: Id468016065eb19a24375c8cecb0ac6faf631f316
1 parent a74d387 commit 886da67

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Some distros like Arch Linux won't install `aspell-en` with `aspell`, and you mu
2929
$ sudo pacman -S aspell-en
3030
```
3131

32-
Note: [Cppcheck](https://cppcheck.sourceforge.net/) version must be at least 1.90, otherwise
32+
Note: [Cppcheck](https://cppcheck.sourceforge.io/) version must be at least 1.90, otherwise
3333
it might report errors with false positives. You can get its version by executing `$ cppcheck --version`.
34-
Check [Developer Info](https://cppcheck.sourceforge.net/devinfo/) for building Cppcheck from source.
34+
Check [Developer Info](https://cppcheck.sourceforge.io/devinfo/) for building Cppcheck from source.
3535

3636
### Integrate `clang-format` to `vim`
3737
If you want to run `clang-format` automatically after saving with vim,

scripts/pre-commit.hook

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#!/usr/bin/env bash
22

3+
# Ensure that the common script exists and is readable, then verify it has no
4+
# syntax errors and defines the required function.
5+
common_script="$(dirname "$0")/../../scripts/common.sh"
6+
[ -r "$common_script" ] || { echo "[!] '$common_script' not found or not readable." >&2; exit 1; }
7+
bash -n "$common_script" >/dev/null 2>&1 || { echo "[!] '$common_script' contains syntax errors." >&2; exit 1; }
8+
source "$common_script"
9+
declare -F set_colors >/dev/null 2>&1 || { echo "[!] '$common_script' does not define the required function." >&2; exit 1; }
10+
311
# Build unmatched suppressions for each *.c file.
412
cppcheck_build_unmatched() {
513
local file suppression=""
@@ -64,26 +72,25 @@ CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
6472
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
6573
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
6674

75+
set_colors
76+
6777
RETURN=0
6878

6979
# Disallow non-ASCII characters in workspace path
7080
workspace=$(git rev-parse --show-toplevel)
7181
if echo "$workspace" | grep -q "[一-龥]"; then
72-
echo "[!] The workspace path '$workspace' contains non-ASCII characters." >&2
73-
exit 1
82+
throw "The workspace path '$workspace' contains non-ASCII characters."
7483
fi
7584

7685
CLANG_FORMAT=$(which clang-format)
7786
if [ $? -ne 0 ]; then
78-
echo "[!] clang-format not installed. Unable to check source file format policy." >&2
79-
exit 1
87+
throw "clang-format not installed. Unable to check source file format policy."
8088
fi
8189

8290
CPPCHECK=$(which cppcheck)
8391
mkdir -p .out
8492
if [ $? -ne 0 ]; then
85-
echo "[!] cppcheck not installed. Unable to perform static analysis." >&2
86-
exit 1
93+
throw "cppcheck not installed. Unable to perform static analysis."
8794
fi
8895

8996
# Check that cppcheck's version is at least 1.90.
@@ -94,21 +101,18 @@ else
94101
# For version 1.x, extract the minor version and compare.
95102
minor_version=$(echo "$cppcheck_ver" | sed -Ee 's/Cppcheck 1\.([0-9]+)/\1/;q')
96103
if [ "$minor_version" -lt 90 ]; then
97-
echo "[!] cppcheck version must be at least 1.90." >&2
98-
echo -e " See Developer Info for building cppcheck from source:\n"
99-
echo -e " https://cppcheck.sourceforge.io/devinfo/" >&2
100-
exit 1
104+
throw "cppcheck version must be at least 1.90.\n\
105+
See Developer Info for building cppcheck from source:\n\
106+
https://cppcheck.sourceforge.io/devinfo/"
101107
fi
102108
fi
103109

104110
ASPELL=$(which aspell)
105111
if [ $? -ne 0 ]; then
106-
echo "[!] aspell not installed. Unable to do spelling check." >&2
107-
exit 1
112+
throw "aspell not installed. Unable to do spelling check."
108113
fi
109114
if [ -z "$(aspell dump dicts | grep -E '^en$')" ]; then
110-
echo "[!] aspell-en not installed. Unable to do spelling check." >&2
111-
exit 1
115+
throw "aspell-en not installed. Unable to do spelling check."
112116
fi
113117

114118
DIFF=$(which colordiff)
@@ -121,8 +125,7 @@ if command -v sha1sum >/dev/null 2>&1; then
121125
elif command -v shasum >/dev/null 2>&1; then
122126
SHA1SUM="shasum"
123127
else
124-
echo "[!] sha1sum or shasum not installed." >&2
125-
exit 1
128+
throw "sha1sum or shasum not installed."
126129
fi
127130

128131
# Get staged filenames (added, copied, or modified) into an array.
@@ -137,12 +140,12 @@ for file in "${FILES[@]}"; do
137140

138141
if echo "$mime_info" | grep -qi binary; then
139142
binary_files+=("$name")
140-
echo "[!] '$name' appears to be a binary blob."
143+
printf "${RED}[!]${NC} '${YELLOW}$name${NC}' appears to be a binary blob.\n"
141144
fi
142145
done
143146

144147
if [ "${#binary_files[@]}" -gt 0 ]; then
145-
echo "WARNING: Binary data found"
148+
printf "${RED}[!]${NC} Binary data found.\n"
146149
fi
147150

148151
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
@@ -180,8 +183,7 @@ fi
180183

181184
$SHA1SUM -c scripts/checksums 2>/dev/null >/dev/null
182185
if [ $? -ne 0 ]; then
183-
echo "[!] You are not allowed to change the header file queue.h or list.h" >&2
184-
exit 1
186+
throw "You are not allowed to change the header file queue.h or list.h"
185187
fi
186188

187189
# Prevent unsafe functions

0 commit comments

Comments
 (0)