Skip to content

Commit 343ba10

Browse files
committed
WIP: update pre-receive hook
1 parent a6a54e1 commit 343ba10

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

serverhooks/update

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ echo "Enforcing policies for revision ${refname}:"
2020
# Get the list of all the commits
2121
# -------------------------------
2222

23-
# Check if a zero sha
23+
# exit if a branch is about to be deleted
24+
if new == '0000000000000000000000000000000000000000':
25+
exit 0
26+
27+
# Check if a zero sha, then either a new branch is pushed or an empty repo is being pushed
2428
if [ "${oldrev}" = "0000000000000000000000000000000000000000" ]; then
2529
# List everything reachable from newrev but not any heads
2630
span=`git rev-list $(git for-each-ref --format='%(refname)' refs/heads/* | sed 's/^/\^/') ${newrev}`
@@ -36,10 +40,47 @@ for sha1 in ${span}; do
3640
# check for whitespace errors in commit
3741
whitespace=$(exec git diff --check ${sha1}^ ${sha1} *.c *.h *.cpp *.hpp *.sh)
3842
# check commit message
39-
message=$(git log --format=%B -n 1 ${sha1} | wc -v)
43+
commitmessage=$(git log --format=%B -n 1 ${sha1})
44+
commitfirstline=$(echo "$commitmessage" | head -n 1)
45+
46+
# don't check commits from before 2020
47+
if [ 2020 -le "$(git log "${sha1}" --pretty=%cd | cut -d ' ' -f 5)" ]; then
48+
continue
49+
fi
50+
51+
# don't check merge commits
52+
# only merge commits have more than one parent
53+
nparents=$(git cat-file -p HEAD|grep parent|wc -l)
54+
if [ "${nparents}" != 1 ]; then
55+
continue
56+
fi
57+
58+
# don't check revert commits
59+
if [[ "$(commitfirstline)" == Revert* ]]; then
60+
continue
61+
fi
62+
63+
# if commitmessage has 2 or more lines, make sure the second line is empty
64+
if [ $(echo "$commitmessage" | wc -l) -ge 2 ]; then
65+
if [ $(echo "$commitmessage" | head -n 2 | tail -n 2) != "" ]; then
66+
echo "commit: ${sha1}"
67+
echo "bad commit message: second line is not blank"
68+
refuse=true
69+
fi
70+
fi
71+
72+
73+
# check length of commit description lines
74+
for line in $(commitmessage); do
75+
if [ 75 -lt $(echo $line | wc -c) ]
76+
echo "commit: ${sha1}"
77+
echo "bad commit message: header or description lines are too long (max 75 chars)"
78+
refuse=true
79+
fi
80+
done
4081

4182
# Verify its not empty
42-
if [ "${whitespace}" != "" -o "${message}" -le "100" ]; then
83+
if [ "${whitespace}" != "" -o "${commitmessage}" -le "100" ]; then
4384
found=$((${found} + 1))
4485
fi
4586
done

0 commit comments

Comments
 (0)