Skip to content

Commit e85ae38

Browse files
committed
Synchronize git hooks
This patch introduces the latest changes from lab0-c [1] for Git hooks, which prevent students from wring something unpleasant. This patch introduces [1] https://github.com/sysprog21/lab0-c
1 parent a5f03cc commit e85ae38

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

scripts/commit-msg.hook

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ read_commit_message() {
118118
[[ $REPLY =~ ^# ]]
119119
test $? -eq 0 || COMMIT_MSG_LINES+=("$REPLY")
120120

121+
[[ $REPLY =~ "# ------------------------ >8 ------------------------" ]]
122+
break
123+
121124
done < $COMMIT_MSG_FILE
122125
}
123126

@@ -154,7 +157,7 @@ validate_commit_message() {
154157
echo "Aspell not installed - unable to check spelling"
155158
else
156159
LINE_NUMBER=1
157-
MISSPELLED_WORDS=`echo "$COMMIT_MSG_LINES[LINE_NUMBER]" | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws`
160+
MISSPELLED_WORDS=$(echo "$COMMIT_MSG_LINES[LINE_NUMBER]" | $ASPELL --lang=en --list --home-dir=scripts --personal=aspell-pws)
158161
if [ -n "$MISSPELLED_WORDS" ]; then
159162
add_warning LINE_NUMBER "Possible misspelled word(s): $MISSPELLED_WORDS"
160163
fi
@@ -169,7 +172,7 @@ validate_commit_message() {
169172
# 2. Limit the subject line to configured number of characters
170173
# ------------------------------------------------------------------------------
171174

172-
subject_max_length=$(git config --get hooks.goodcommit.subjectmaxlength || echo '60')
175+
subject_max_length=$(git config --get hooks.goodcommit.subjectmaxlength || echo '50')
173176
test "${#COMMIT_SUBJECT}" -le $subject_max_length
174177
test $? -eq 0 || add_warning 1 "Limit the subject line to $subject_max_length characters (${#COMMIT_SUBJECT} chars)"
175178

@@ -188,7 +191,7 @@ validate_commit_message() {
188191
# 5. Use the imperative mood in the subject line
189192
# ------------------------------------------------------------------------------
190193

191-
IMPERATIVE_MOOD_BLACKLIST=(
194+
IMPERATIVE_MOOD_DENYLIST=(
192195
added adds adding
193196
adjusted adjusts adjusting
194197
amended amends amending
@@ -235,9 +238,9 @@ validate_commit_message() {
235238
# enable case insensitive match
236239
shopt -s nocasematch
237240

238-
for BLACKLISTED_WORD in "${IMPERATIVE_MOOD_BLACKLIST[@]}"; do
239-
[[ ${COMMIT_SUBJECT_TO_PROCESS} =~ ^[[:blank:]]*$BLACKLISTED_WORD ]]
240-
test $? -eq 0 && add_warning 1 "Use the imperative mood in the subject line, e.g 'fix' not 'fixes'" && break
241+
for DENYLISTED_WORD in "${IMPERATIVE_MOOD_DENYLIST[@]}"; do
242+
[[ ${COMMIT_SUBJECT_TO_PROCESS} =~ ^[[:blank:]]*$DENYLISTED_WORD ]]
243+
test $? -eq 0 && add_warning 1 "Use the imperative mood in the subject line, e.g., 'fix' not 'fixes'" && break
241244
done
242245

243246
# disable case insensitive match
@@ -266,6 +269,10 @@ validate_commit_message() {
266269
test "${#COMMIT_SUBJECT_WORDS[@]}" -gt 1
267270
test $? -eq 0 || add_warning 1 "Do no write single worded commits"
268271

272+
# 8a. Do not mention C source filenames
273+
[[ ${COMMIT_SUBJECT_TO_PROCESS} =~ [_a-zA-Z0-9]+\.[ch]$ ]]
274+
test $? -eq 1 || add_warning 1 "Avoid mentioning C source filenames"
275+
269276
# 9. Do not start the subject line with whitespace
270277
# ------------------------------------------------------------------------------
271278

scripts/pre-commit.hook

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
#!/usr/bin/env bash
22

3-
CPPCHECK_suppresses="--suppress=missingIncludeSystem"
4-
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1 --force $CPPCHECK_suppresses ."
3+
CPPCHECK_unmatched=
4+
for f in *.c; do
5+
CPPCHECK_unmatched="$CPPCHECK_unmatched --suppress=unmatchedSuppression:$f"
6+
done
7+
8+
CPPCHECK_suppresses="\
9+
--suppress=missingIncludeSystem \
10+
--suppress=noValidConfiguration \
11+
--suppress=unusedFunction\
12+
"
13+
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1 --force $CPPCHECK_suppresses $CPPCHECK_unmatched ."
514

615
RETURN=0
716
CLANG_FORMAT=$(which clang-format)
@@ -16,23 +25,45 @@ if [ $? -ne 0 ]; then
1625
exit 1
1726
fi
1827

28+
# Expected Cppcheck version is 1.90+
29+
# First, Cppcheck 2.x
30+
if [ -z "$($CPPCHECK --version | grep -E '^Cppcheck\s2')" ]; then
31+
# Second, Cppcheck 1.x
32+
CPPCHECK_VER=$($CPPCHECK --version | sed -Ee 's/Cppcheck 1.([0-9]+)/\1/;q')
33+
if [ $CPPCHECK_VER -lt 90 ]; then
34+
echo "[!] cppcheck version must be at least 1.90." >&2
35+
echo -e " Check 'Developer Info' for building Cppcheck from source:\n" \
36+
" http://cppcheck.sourceforge.net/devinfo/" >&2
37+
exit 1
38+
fi
39+
fi
40+
1941
ASPELL=$(which aspell)
2042
if [ $? -ne 0 ]; then
2143
echo "[!] aspell not installed. Unable to do spelling check." >&2
2244
exit 1
2345
fi
46+
if [ -z "$(aspell dump dicts | grep -E '^en$')" ]; then
47+
echo "[!] aspell-en not installed. Unable to do spelling check." >&2
48+
exit 1
49+
fi
2450

2551
DIFF=$(which colordiff)
2652
if [ $? -ne 0 ]; then
2753
DIFF=diff
2854
fi
2955

30-
FILES=`git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$"`
56+
SHA1SUM=$(which sha1sum)
57+
if [ $? -ne 0 ]; then
58+
SHA1SUM=shasum
59+
fi
60+
61+
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
3162
for FILE in $FILES; do
32-
nf=`git checkout-index --temp $FILE | cut -f 1`
33-
tempdir=`mktemp -d` || exit 1
34-
newfile=`mktemp ${tempdir}/${nf}.XXXXXX` || exit 1
35-
basename=`basename $FILE`
63+
nf=$(git checkout-index --temp $FILE | cut -f 1)
64+
tempdir=$(mktemp -d) || exit 1
65+
newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1
66+
basename=$(basename $FILE)
3667

3768
source="${tempdir}/${basename}"
3869
mv $nf $source
@@ -54,6 +85,17 @@ for FILE in $FILES; do
5485
fi
5586
done
5687

88+
if [ ! -z "${FILES[*]}" ]; then
89+
echo "Following files need to be cleaned up:"
90+
echo "${FILES[*]}"
91+
fi
92+
93+
$SHA1SUM -c scripts/checksums >/dev/null
94+
if [ $? -ne 0 ]; then
95+
echo "[!] You are not allowed to change the header file queue.h or list.h" >&2
96+
exit 1
97+
fi
98+
5799
# Prevent unsafe functions
58100
root=$(git rev-parse --show-toplevel)
59101
banned="([^f]gets\()|(sprintf\()|(strcpy\()"
@@ -81,4 +123,18 @@ if [ $? -ne 0 ]; then
81123
echo
82124
fi
83125

126+
# non-ASCII filenames are not allowed.
127+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
128+
# them from being added to the repository.
129+
if test $(git diff --cached --name-only --diff-filter=A -z $against |
130+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
131+
then
132+
cat <<\EOF
133+
ERROR: Attempt to add a non-ASCII file name.
134+
This can cause problems if you want to work with people on other platforms.
135+
To be portable it is advisable to rename the file.
136+
EOF
137+
RETURN=1
138+
fi
139+
84140
exit $RETURN

0 commit comments

Comments
 (0)