From ddc0881e24c746f04d85c5da3a6515a0aa2e386a Mon Sep 17 00:00:00 2001 From: Bart van der Braak Date: Thu, 10 Apr 2025 22:23:09 +0200 Subject: [PATCH] Git hook to reject commit messages with HTML tags in them --- gitea/hooks/deny-html-tags | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 gitea/hooks/deny-html-tags diff --git a/gitea/hooks/deny-html-tags b/gitea/hooks/deny-html-tags new file mode 100644 index 0000000..ce6a433 --- /dev/null +++ b/gitea/hooks/deny-html-tags @@ -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 ']*>'; 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