Git hook to reject commit messages with HTML tags in them

This commit is contained in:
Bart van der Braak
2025-04-10 22:23:09 +02:00
parent 681099734f
commit ddc0881e24

View File

@@ -0,0 +1,48 @@
#!/bin/bash -eu
# Reject commits that contain HTML tags in their commit messages.
# Allow override with a special key phrase in the message.
set -o pipefail
nullsha="0000000000000000000000000000000000000000"
status=0
override_msg="override html check"
while read oldref newref refname; do
# Skip branch deletions
if [ "$newref" = "$nullsha" ]; then
continue
fi
# Handle new branches
if [ "$oldref" = "$nullsha" ]; then
oldref="HEAD"
fi
# Loop through each new commit
for commit in $(git rev-list ${oldref}..${newref}); do
# Get full commit message
msg=$(git log --format=%B -n 1 "$commit")
# Check for override
if echo "$msg" | grep -qi "$override_msg"; then
echo "Commit $commit allowed due to override message."
continue
fi
# Check for HTML tags
if echo "$msg" | grep -qE '</?[a-z][a-z0-9]*[^<>]*>'; then
echo "ERROR: Commit $commit contains HTML tags in the message."
echo "Message:"
echo "----------------------------------------"
echo "$msg"
echo "----------------------------------------"
echo "Please remove the HTML tags or use the override phrase:"
echo "\"$override_msg\""
status=1
fi
done
done
exit $status