Skip to content

Commit 082cb08

Browse files
committed
Enforce consistent style for Git hooks
All Git hook scripts now adhere to a consistent style guideline by using 2 spaces for indentation. Change-Id: Id4c5322bedf91b6df2b053f2d09b7b8693cc4068
1 parent 47465ba commit 082cb08

File tree

3 files changed

+86
-83
lines changed

3 files changed

+86
-83
lines changed

scripts/pre-commit.hook

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,45 @@ CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
6767
RETURN=0
6868
CLANG_FORMAT=$(which clang-format)
6969
if [ $? -ne 0 ]; then
70-
echo "[!] clang-format not installed. Unable to check source file format policy." >&2
71-
exit 1
70+
echo "[!] clang-format not installed. Unable to check source file format policy." >&2
71+
exit 1
7272
fi
7373

7474
CPPCHECK=$(which cppcheck)
7575
mkdir -p .out
7676
if [ $? -ne 0 ]; then
77-
echo "[!] cppcheck not installed. Unable to perform static analysis." >&2
78-
exit 1
77+
echo "[!] cppcheck not installed. Unable to perform static analysis." >&2
78+
exit 1
7979
fi
8080

81-
# Expected Cppcheck version is 1.90+
82-
# First, Cppcheck 2.x
83-
if [ -z "$($CPPCHECK --version | grep -E '^Cppcheck\s2')" ]; then
84-
# Second, Cppcheck 1.x
85-
CPPCHECK_VER=$($CPPCHECK --version | sed -Ee 's/Cppcheck 1.([0-9]+)/\1/;q')
86-
if [ $CPPCHECK_VER -lt 90 ]; then
87-
echo "[!] cppcheck version must be at least 1.90." >&2
88-
echo -e " Check 'Developer Info' for building Cppcheck from source:\n" \
89-
" https://cppcheck.sourceforge.net/devinfo/" >&2
90-
exit 1
91-
fi
81+
# Check that cppcheck's version is at least 1.90.
82+
cppcheck_ver=$("$CPPCHECK" --version)
83+
if echo "$cppcheck_ver" | grep -qE '^Cppcheck\s2'; then
84+
: # Version 2.x is acceptable.
85+
else
86+
# For version 1.x, extract the minor version and compare.
87+
minor_version=$(echo "$cppcheck_ver" | sed -Ee 's/Cppcheck 1\.([0-9]+)/\1/;q')
88+
if [ "$minor_version" -lt 90 ]; then
89+
echo "[!] cppcheck version must be at least 1.90." >&2
90+
echo -e " See Developer Info for building cppcheck from source:\n"
91+
echo -e " https://cppcheck.sourceforge.io/devinfo/" >&2
92+
exit 1
93+
fi
9294
fi
9395

9496
ASPELL=$(which aspell)
9597
if [ $? -ne 0 ]; then
96-
echo "[!] aspell not installed. Unable to do spelling check." >&2
97-
exit 1
98+
echo "[!] aspell not installed. Unable to do spelling check." >&2
99+
exit 1
98100
fi
99101
if [ -z "$(aspell dump dicts | grep -E '^en$')" ]; then
100-
echo "[!] aspell-en not installed. Unable to do spelling check." >&2
101-
exit 1
102+
echo "[!] aspell-en not installed. Unable to do spelling check." >&2
103+
exit 1
102104
fi
103105

104106
DIFF=$(which colordiff)
105107
if [ $? -ne 0 ]; then
106-
DIFF=diff
108+
DIFF=diff
107109
fi
108110

109111
if command -v sha1sum >/dev/null 2>&1; then
@@ -117,40 +119,41 @@ fi
117119

118120
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
119121
for FILE in $FILES; do
120-
nf=$(git checkout-index --temp $FILE | cut -f 1)
121-
tempdir=$(mktemp -d) || exit 1
122-
newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1
123-
basename=$(basename $FILE)
124-
125-
source="${tempdir}/${basename}"
126-
mv $nf $source
127-
cp .clang-format $tempdir
128-
$CLANG_FORMAT $source > $newfile 2>> /dev/null
129-
$DIFF -u -p -B --label="modified $FILE" --label="expected coding style" \
130-
"${source}" "${newfile}"
131-
r=$?
132-
rm -rf "${tempdir}"
133-
if [ $r != 0 ] ; then
134-
echo "[!] $FILE does not follow the consistent coding style." >&2
135-
RETURN=1
136-
fi
137-
if [ $RETURN -eq 1 ]; then
138-
echo "" >&2
139-
echo "Make sure you indent as the following:" >&2
140-
echo " clang-format -i $FILE" >&2
141-
echo
142-
fi
122+
nf=$(git checkout-index --temp $FILE | cut -f 1)
123+
tempdir=$(mktemp -d) || exit 1
124+
newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1
125+
basename=$(basename $FILE)
126+
127+
source="${tempdir}/${basename}"
128+
mv $nf $source
129+
cp .clang-format $tempdir
130+
$CLANG_FORMAT $source > $newfile 2>> /dev/null
131+
$DIFF -u -p -B \
132+
--label="modified $FILE" --label="expected coding style" \
133+
"${source}" "${newfile}"
134+
r=$?
135+
rm -rf "${tempdir}"
136+
if [ $r != 0 ] ; then
137+
echo "[!] $FILE does not follow the consistent coding style." >&2
138+
RETURN=1
139+
fi
140+
if [ $RETURN -eq 1 ]; then
141+
echo "" >&2
142+
echo "Make sure you indent as the following:" >&2
143+
echo " clang-format -i $FILE" >&2
144+
echo
145+
fi
143146
done
144147

145148
if [ ! -z "${FILES[*]}" ]; then
146-
echo "Following files need to be cleaned up:"
147-
echo "${FILES[*]}"
149+
echo "Following files need to be cleaned up:"
150+
echo "${FILES[*]}"
148151
fi
149152

150153
$SHA1SUM -c scripts/checksums 2>/dev/null >/dev/null
151154
if [ $? -ne 0 ]; then
152-
echo "[!] You are not allowed to change the header file queue.h or list.h" >&2
153-
exit 1
155+
echo "[!] You are not allowed to change the header file queue.h or list.h" >&2
156+
exit 1
154157
fi
155158

156159
# Prevent unsafe functions
@@ -159,26 +162,26 @@ banned="([^f]gets\()|(sprintf\()|(strcpy\()"
159162
status=0
160163
for file in $(git diff --staged --name-only | grep -E "\.(c|cc|cpp|h|hh|hpp)\$")
161164
do
162-
filepath="${root}/${file}"
163-
output=$(grep -nrE "${banned}" "${filepath}")
164-
if [ ! -z "${output}" ]; then
165-
echo "Dangerous function detected in ${filepath}"
166-
echo "${output}"
167-
echo
168-
echo "Read 'Common vulnerabilities guide for C programmers' carefully."
169-
echo " https://security.web.cern.ch/security/recommendations/en/codetools/c.shtml"
170-
RETURN=1
171-
fi
165+
filepath="${root}/${file}"
166+
output=$(grep -nrE "${banned}" "${filepath}")
167+
if [ ! -z "${output}" ]; then
168+
echo "Dangerous function detected in ${filepath}"
169+
echo "${output}"
170+
echo
171+
echo "Read 'Common vulnerabilities guide for C programmers' carefully."
172+
echo " https://security.web.cern.ch/security/recommendations/en/codetools/c.shtml"
173+
RETURN=1
174+
fi
172175
done
173176

174177
# static analysis
175178
echo "Running static analysis..."
176179
$CPPCHECK $CPPCHECK_OPTS >/dev/null
177180
if [ $? -ne 0 ]; then
178-
RETURN=1
179-
echo "" >&2
180-
echo "Fail to pass static analysis." >&2
181-
echo
181+
RETURN=1
182+
echo "" >&2
183+
echo "Fail to pass static analysis." >&2
184+
echo
182185
fi
183186

184187
# non-ASCII filenames are not allowed.
@@ -187,12 +190,12 @@ fi
187190
if test $(git diff --cached --name-only --diff-filter=A -z $against |
188191
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
189192
then
190-
cat <<\EOF
193+
cat <<\EOF
191194
ERROR: Attempt to add a non-ASCII file name.
192195
This can cause problems if you want to work with people on other platforms.
193196
To be portable it is advisable to rename the file.
194197
EOF
195-
RETURN=1
198+
RETURN=1
196199
fi
197200

198201
exit $RETURN

scripts/pre-push.hook

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ NC='\033[0m' # No Color
1414
# Bump copyright year
1515
commit=$(git rev-list --skip 1 --grep '^Bump copyright' 0b8be2c15160c216e8b6ec82c99a000e81c0e429...HEAD)
1616
if [ x"$commit" != x"50c5ac53d31adf6baac4f8d3db6b3ce2215fee40" ] ; then
17-
echo -e "${RED}ERROR${NC}: This repository is insane."
18-
echo -e "Make sure you did fork from https://github.com/sysprog21/lab0-c recently."
19-
echo ""
20-
exit 1
17+
echo -e "${RED}ERROR${NC}: This repository is insane."
18+
echo -e "Make sure you did fork from https://github.com/sysprog21/lab0-c recently."
19+
echo ""
20+
exit 1
2121
fi
2222

2323
# Show hints
@@ -27,24 +27,24 @@ echo ""
2727

2828
# only run this if you are pushing to master
2929
if [[ $current_branch = $protected_branch ]] ; then
30-
echo -e "${YELLOW}Running pre push to master check...${NC}"
31-
32-
echo -e "${YELLOW}Trying to build tests project...${NC}"
30+
echo -e "${YELLOW}Running pre push to master check...${NC}"
3331

34-
# build the project
35-
make
32+
echo -e "${YELLOW}Trying to build tests project...${NC}"
3633

37-
# $? is a shell variable which stores the return code from what we just ran
38-
rc=$?
39-
if [[ $rc != 0 ]] ; then
40-
echo -e "${RED}Failed to build the project, please fix this and push again${NC}"
41-
echo ""
42-
exit $rc
43-
fi
34+
# build the project
35+
make
4436

45-
# Everything went OK so we can exit with a zero
46-
echo -e "${GREEN}Pre-push check passed!${NC}"
37+
# $? is a shell variable which stores the return code from what we just ran
38+
rc=$?
39+
if [[ $rc != 0 ]] ; then
40+
echo -e "${RED}Failed to build the project, please fix this and push again${NC}"
4741
echo ""
42+
exit $rc
43+
fi
44+
45+
# Everything went OK so we can exit with a zero
46+
echo -e "${GREEN}Pre-push check passed!${NC}"
47+
echo ""
4848
fi
4949

5050
exit 0

scripts/prepare-commit-msg.hook

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ EOF
4949
# Prompt the user about aborting the commit.
5050
read -rp "Do you want to abort this commit? (y/N): " answer
5151
if [[ "$answer" =~ ^[Yy]$ ]]; then
52-
echo "Commit aborted by user." >&2
53-
exit 1
52+
echo "Commit aborted by user." >&2
53+
exit 1
5454
fi
5555

5656
exit 0

0 commit comments

Comments
 (0)