Automated commit detection #20

Merged
k.marinus merged 6 commits from fix/skip-checking-auto-commits into dev 2025-11-03 14:35:41 +00:00
Showing only changes of commit 0d5e198cad - Show all commits

View File

@@ -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 # Check for Merge commits (covers 'git merge' and PR merges from GitHub/GitLab)
echo "Hook: Detected a merge commit." # Examples: "Merge branch 'main' into ...", "Merge pull request #123 from ..."
# Ensure the message follows a 'Merge branch...' pattern. MERGE_PATTERN="^Merge (branch|pull request|tag) .*"
first_line=$(head -n1 "$COMMIT_MSG_FILE") if [[ "$HEADER" =~ $MERGE_PATTERN ]]; then
if [[ ! "$first_line" =~ ^Merge.* ]]; then echo -e "${GREEN}Merge commit detected by message content. Skipping validation.${NC}"
echo "Error: Merge commit message should start with 'Merge'." >&2 exit 0
exit 1
fi fi
exit 0
# Check for a squash commit (from git merge --squash) # Check for Revert commits
elif [ -f "$GIT_DIR/SQUASH_MSG" ]; then # Example: "Revert "feat: add new feature""
echo "Hook: Detected a squash 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 revert commit # Check for Cherry-pick commits (this pattern appears at the end of the message)
elif [ -f "$GIT_DIR/REVERT_HEAD" ]; then # Example: "(cherry picked from commit deadbeef...)"
echo "Hook: Detected a revert commit. Skipping validation." # We use grep -q to search the whole file quietly.
exit 0 CHERRY_PICK_PATTERN="\(cherry picked from commit [a-f0-9]{7,40}\)"
if grep -qE "$CHERRY_PICK_PATTERN" "$COMMIT_MSG_FILE"; then
# Check for a cherry-pick commit echo -e "${GREEN}Cherry-pick detected by message content. Skipping validation.${NC}"
elif [ -f "$GIT_DIR/CHERRY_PICK_HEAD" ]; then
echo "Hook: Detected a cherry-pick commit. Skipping validation."
exit 0 exit 0
fi fi