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:
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# This script checks if a commit message follows the specified format.
|
# This script checks if a commit message follows the specified format.
|
||||||
# It's designed to be used as a 'commit-msg' git hook.
|
# It's designed to be used as a 'commit-msg' git hook.
|
||||||
@@ -25,32 +25,31 @@ COMMIT_MSG_FILE=$1
|
|||||||
|
|
||||||
# --- Automated Commit Detection ---
|
# --- Automated Commit Detection ---
|
||||||
|
|
||||||
# Git directory (.git/)
|
# Read the first line (header) for initial checks
|
||||||
GIT_DIR=$(git rev-parse --git-dir)
|
HEADER=$(head -n 1 "$COMMIT_MSG_FILE")
|
||||||
# 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
|
|
||||||
|
|
||||||
# Check for a squash commit (from git merge --squash)
|
# Check for Merge commits (covers 'git merge' and PR merges from GitHub/GitLab)
|
||||||
elif [ -f "$GIT_DIR/SQUASH_MSG" ]; then
|
# Examples: "Merge branch 'main' into ...", "Merge pull request #123 from ..."
|
||||||
echo "Hook: Detected a squash commit. Skipping validation."
|
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
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Check for a revert commit
|
# Check for Revert commits
|
||||||
elif [ -f "$GIT_DIR/REVERT_HEAD" ]; then
|
# Example: "Revert "feat: add new feature""
|
||||||
echo "Hook: Detected a revert commit. Skipping validation."
|
REVERT_PATTERN="^Revert \".*\""
|
||||||
|
if [[ "$HEADER" =~ $REVERT_PATTERN ]]; then
|
||||||
|
echo -e "${GREEN}Revert commit detected by message content. Skipping validation.${NC}"
|
||||||
exit 0
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Check for a cherry-pick commit
|
# Check for Cherry-pick commits (this pattern appears at the end of the message)
|
||||||
elif [ -f "$GIT_DIR/CHERRY_PICK_HEAD" ]; then
|
# Example: "(cherry picked from commit deadbeef...)"
|
||||||
echo "Hook: Detected a cherry-pick commit. Skipping validation."
|
# 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
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user