fix: pattern matching instead of file existence

The previous method of detecting automated commits was error-prone,
specifically when using VSCode to commit changes. This new method uses a
simple Regex pattern match to see if the commit message matches any
known auto-generated commits.

ref: N25B-241
This commit is contained in:
2025-11-03 14:51:18 +01:00
parent 020bf55772
commit 3c8cee54eb
2 changed files with 22 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# This script checks if the current branch name follows the specified format.
# It's designed to be used as a 'pre-commit' git hook.

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# This script checks if a commit message follows the specified format.
# It's designed to be used as a 'commit-msg' git hook.
@@ -25,32 +25,31 @@ COMMIT_MSG_FILE=$1
# --- Automated Commit Detection ---
# Git directory (.git/)
GIT_DIR=$(git rev-parse --git-dir)
# Check for a merge commit
if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
echo "Hook: Detected a merge commit."
# Ensure the message follows a 'Merge branch...' pattern.
first_line=$(head -n1 "$COMMIT_MSG_FILE")
if [[ ! "$first_line" =~ ^Merge.* ]]; then
echo "Error: Merge commit message should start with 'Merge'." >&2
exit 1
fi
exit 0
# Read the first line (header) for initial checks
HEADER=$(head -n 1 "$COMMIT_MSG_FILE")
# Check for a squash commit (from git merge --squash)
elif [ -f "$GIT_DIR/SQUASH_MSG" ]; then
echo "Hook: Detected a squash commit. Skipping validation."
# Check for Merge commits (covers 'git merge' and PR merges from GitHub/GitLab)
# Examples: "Merge branch 'main' into ...", "Merge pull request #123 from ..."
MERGE_PATTERN="^Merge (branch|pull request|tag) .*"
if [[ "$HEADER" =~ $MERGE_PATTERN ]]; then
echo -e "${GREEN}Merge commit detected by message content. Skipping validation.${NC}"
exit 0
fi
# Check for a revert commit
elif [ -f "$GIT_DIR/REVERT_HEAD" ]; then
echo "Hook: Detected a revert commit. Skipping validation."
# Check for Revert commits
# Example: "Revert "feat: add new feature""
REVERT_PATTERN="^Revert \".*\""
if [[ "$HEADER" =~ $REVERT_PATTERN ]]; then
echo -e "${GREEN}Revert commit detected by message content. Skipping validation.${NC}"
exit 0
fi
# Check for a cherry-pick commit
elif [ -f "$GIT_DIR/CHERRY_PICK_HEAD" ]; then
echo "Hook: Detected a cherry-pick commit. Skipping validation."
# Check for Cherry-pick commits (this pattern appears at the end of the message)
# Example: "(cherry picked from commit deadbeef...)"
# We use grep -q to search the whole file quietly.
CHERRY_PICK_PATTERN="\(cherry picked from commit [a-f0-9]{7,40}\)"
if grep -qE "$CHERRY_PICK_PATTERN" "$COMMIT_MSG_FILE"; then
echo -e "${GREEN}Cherry-pick detected by message content. Skipping validation.${NC}"
exit 0
fi