2020-10-20 18:35:53 +03:00
|
|
|
#!/bin/sh
|
2017-03-31 19:33:38 +03:00
|
|
|
|
|
|
|
REF="HEAD"
|
|
|
|
|
2017-10-26 20:17:00 +03:00
|
|
|
# test commit body for length
|
2018-08-27 20:04:21 +03:00
|
|
|
# lines containing urls are exempt for the length limit.
|
2020-10-20 18:35:53 +03:00
|
|
|
test_commit_bodylength()
|
2017-10-26 20:17:00 +03:00
|
|
|
{
|
|
|
|
length="72"
|
2018-08-27 20:04:21 +03:00
|
|
|
body=$(git log -n 1 --pretty=%b "$REF" | grep -Ev "http(s)*://" | grep -E -m 1 ".{$((length + 1))}")
|
2017-10-26 20:17:00 +03:00
|
|
|
if [ -n "$body" ]; then
|
|
|
|
echo "error: commit message body contains line over ${length} characters"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-03-31 19:33:38 +03:00
|
|
|
# check for a tagged line
|
2020-10-20 18:35:53 +03:00
|
|
|
check_tagged_line()
|
2017-03-31 19:33:38 +03:00
|
|
|
{
|
2020-10-20 18:35:53 +03:00
|
|
|
regex='^[[:space:]]*'"$1"':[[:space:]][[:print:]]+[[:space:]]<[[:graph:]]+>$'
|
2018-01-17 21:17:16 +03:00
|
|
|
foundline=$(git log -n 1 "$REF" | grep -E -m 1 "$regex")
|
2017-03-31 19:33:38 +03:00
|
|
|
if [ -z "$foundline" ]; then
|
|
|
|
echo "error: missing \"$1\""
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# check commit message for a normal commit
|
2020-10-20 18:35:53 +03:00
|
|
|
new_change_commit()
|
2017-03-31 19:33:38 +03:00
|
|
|
{
|
|
|
|
error=0
|
|
|
|
|
2019-01-08 20:23:05 +03:00
|
|
|
# subject is not longer than 72 characters
|
|
|
|
long_subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '.{73}')
|
2017-03-31 19:33:38 +03:00
|
|
|
if [ -n "$long_subject" ]; then
|
2019-01-08 20:23:05 +03:00
|
|
|
echo "error: commit subject over 72 characters"
|
2017-03-31 19:33:38 +03:00
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# need a signed off by
|
|
|
|
if ! check_tagged_line "Signed-off-by" ; then
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ensure that no lines in the body of the commit are over 72 characters
|
2017-10-26 20:17:00 +03:00
|
|
|
if ! test_commit_bodylength ; then
|
2017-03-31 19:33:38 +03:00
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $error
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:35:53 +03:00
|
|
|
is_coverity_fix()
|
2017-10-26 20:17:00 +03:00
|
|
|
{
|
|
|
|
# subject starts with Fix coverity defects means it's a coverity fix
|
2018-01-17 21:17:16 +03:00
|
|
|
subject=$(git log -n 1 --pretty=%s "$REF" | grep -E -m 1 '^Fix coverity defects')
|
2017-10-26 20:17:00 +03:00
|
|
|
if [ -n "$subject" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:35:53 +03:00
|
|
|
coverity_fix_commit()
|
2017-10-26 20:17:00 +03:00
|
|
|
{
|
|
|
|
error=0
|
|
|
|
|
|
|
|
# subject starts with Fix coverity defects: CID dddd, dddd...
|
|
|
|
subject=$(git log -n 1 --pretty=%s "$REF" |
|
2018-01-17 21:17:16 +03:00
|
|
|
grep -E -m 1 'Fix coverity defects: CID [[:digit:]]+(, [[:digit:]]+)*')
|
2017-10-26 20:17:00 +03:00
|
|
|
if [ -z "$subject" ]; then
|
|
|
|
echo "error: Coverity defect fixes must have a subject line that starts with \"Fix coverity defects: CID dddd\""
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# need a signed off by
|
|
|
|
if ! check_tagged_line "Signed-off-by" ; then
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# test each summary line for the proper format
|
|
|
|
OLDIFS=$IFS
|
2020-10-20 18:35:53 +03:00
|
|
|
IFS='
|
|
|
|
'
|
2018-01-17 21:17:16 +03:00
|
|
|
for line in $(git log -n 1 --pretty=%b "$REF" | grep -E '^CID'); do
|
2021-05-14 12:55:17 +03:00
|
|
|
if ! echo "$line" | grep -qE '^CID [[:digit:]]+: ([[:graph:]]+|[[:space:]])+ \(([[:upper:]]|\_)+\)'; then
|
2017-10-26 20:17:00 +03:00
|
|
|
echo "error: commit message has an improperly formatted CID defect line"
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
IFS=$OLDIFS
|
|
|
|
|
|
|
|
# ensure that no lines in the body of the commit are over 72 characters
|
|
|
|
if ! test_commit_bodylength; then
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $error
|
|
|
|
}
|
|
|
|
|
2017-03-31 19:33:38 +03:00
|
|
|
if [ -n "$1" ]; then
|
|
|
|
REF="$1"
|
|
|
|
fi
|
|
|
|
|
2017-10-26 20:17:00 +03:00
|
|
|
# if coverity fix, test against that
|
|
|
|
if is_coverity_fix; then
|
|
|
|
if ! coverity_fix_commit; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-03-31 19:33:38 +03:00
|
|
|
# have a normal commit
|
|
|
|
if ! new_change_commit ; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|