build: fix pre-commit
Moved the git hooks into shell scripts and placed them into the pre-commit configuration. Also extended robustness of the hooks. ref: N25B-241
This commit is contained in:
77
.githooks/check-branch-name.sh
Executable file
77
.githooks/check-branch-name.sh
Executable file
@@ -0,0 +1,77 @@
|
|||||||
|
#!/bin/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.
|
||||||
|
|
||||||
|
# Format: <type>/<short-description>
|
||||||
|
# Example: feat/add-user-login
|
||||||
|
|
||||||
|
# --- Configuration ---
|
||||||
|
# An array of allowed commit types
|
||||||
|
ALLOWED_TYPES=(feat fix refactor perf style test docs build chore revert)
|
||||||
|
# An array of branches to ignore
|
||||||
|
IGNORED_BRANCHES=(main dev)
|
||||||
|
|
||||||
|
# --- Colors for Output ---
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# --- Helper Functions ---
|
||||||
|
error_exit() {
|
||||||
|
echo -e "${RED}ERROR: $1${NC}" >&2
|
||||||
|
echo -e "${YELLOW}Branch name format is incorrect. Aborting commit.${NC}" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- Main Logic ---
|
||||||
|
|
||||||
|
# 1. Get the current branch name
|
||||||
|
BRANCH_NAME=$(git symbolic-ref --short HEAD)
|
||||||
|
|
||||||
|
# 2. Check if the current branch is in the ignored list
|
||||||
|
for ignored_branch in "${IGNORED_BRANCHES[@]}"; do
|
||||||
|
if [ "$BRANCH_NAME" == "$ignored_branch" ]; then
|
||||||
|
echo -e "${GREEN}Branch check skipped for default branch: $BRANCH_NAME${NC}"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# 3. Validate the overall structure: <type>/<description>
|
||||||
|
if ! [[ "$BRANCH_NAME" =~ ^[a-z]+/.+$ ]]; then
|
||||||
|
error_exit "Branch name must be in the format: <type>/<short-description>\nExample: feat/add-user-login"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 4. Extract the type and description
|
||||||
|
TYPE=$(echo "$BRANCH_NAME" | cut -d'/' -f1)
|
||||||
|
DESCRIPTION=$(echo "$BRANCH_NAME" | cut -d'/' -f2-)
|
||||||
|
|
||||||
|
# 5. Validate the <type>
|
||||||
|
type_valid=false
|
||||||
|
for allowed_type in "${ALLOWED_TYPES[@]}"; do
|
||||||
|
if [ "$TYPE" == "$allowed_type" ]; then
|
||||||
|
type_valid=true
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ "$type_valid" == false ]; then
|
||||||
|
error_exit "Invalid type '$TYPE'.\nAllowed types are: ${ALLOWED_TYPES[*]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 6. Validate the <short-description>
|
||||||
|
# Regex breakdown:
|
||||||
|
# ^[a-z0-9]+ - Starts with one or more lowercase letters/numbers (the first word).
|
||||||
|
# (-[a-z0-9]+){0,5} - Followed by a group of (dash + word) 0 to 5 times.
|
||||||
|
# $ - End of the string.
|
||||||
|
# This entire pattern enforces 1 to 6 words total, separated by dashes.
|
||||||
|
DESCRIPTION_REGEX="^[a-z0-9]+(-[a-z0-9]+){0,5}$"
|
||||||
|
|
||||||
|
if ! [[ "$DESCRIPTION" =~ $DESCRIPTION_REGEX ]]; then
|
||||||
|
error_exit "Invalid short description '$DESCRIPTION'.\nIt must be a maximum of 6 words, all lowercase, separated by dashes.\nExample: add-new-user-authentication-feature"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If all checks pass, exit successfully
|
||||||
|
echo -e "${GREEN}Branch name '$BRANCH_NAME' is valid.${NC}"
|
||||||
|
exit 0
|
||||||
93
.githooks/check-commit-msg.sh
Executable file
93
.githooks/check-commit-msg.sh
Executable file
@@ -0,0 +1,93 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# This script checks if a commit message follows the specified format.
|
||||||
|
# It's designed to be used as a 'commit-msg' git hook.
|
||||||
|
|
||||||
|
# Format:
|
||||||
|
# <type>: <short description>
|
||||||
|
#
|
||||||
|
# [optional]<body>
|
||||||
|
#
|
||||||
|
# [ref/close]: <issue identifier>
|
||||||
|
|
||||||
|
# --- Configuration ---
|
||||||
|
# An array of allowed commit types
|
||||||
|
ALLOWED_TYPES=(feat fix refactor perf style test docs build chore revert)
|
||||||
|
|
||||||
|
# --- Colors for Output ---
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# The first argument to the hook is the path to the file containing the commit message
|
||||||
|
COMMIT_MSG_FILE=$1
|
||||||
|
|
||||||
|
# --- Validation Functions ---
|
||||||
|
|
||||||
|
# Function to print an error message and exit
|
||||||
|
# Usage: error_exit "Your error message here"
|
||||||
|
error_exit() {
|
||||||
|
# >&2 redirects echo to stderr
|
||||||
|
echo -e "${RED}ERROR: $1${NC}" >&2
|
||||||
|
echo -e "${YELLOW}Commit message format is incorrect. Aborting commit.${NC}" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- Main Logic ---
|
||||||
|
|
||||||
|
# 1. Read the header (first line) of the commit message
|
||||||
|
HEADER=$(head -n 1 "$COMMIT_MSG_FILE")
|
||||||
|
|
||||||
|
# 2. Validate the header format: <type>: <description>
|
||||||
|
# Regex breakdown:
|
||||||
|
# ^(type1|type2|...) - Starts with one of the allowed types
|
||||||
|
# : - Followed by a literal colon
|
||||||
|
# \s - Followed by a single space
|
||||||
|
# .+ - Followed by one or more characters for the description
|
||||||
|
# $ - End of the line
|
||||||
|
TYPES_REGEX=$(
|
||||||
|
IFS="|"
|
||||||
|
echo "${ALLOWED_TYPES[*]}"
|
||||||
|
)
|
||||||
|
HEADER_REGEX="^($TYPES_REGEX): .+$"
|
||||||
|
|
||||||
|
if ! [[ "$HEADER" =~ $HEADER_REGEX ]]; then
|
||||||
|
error_exit "Invalid header format.\n\nHeader must be in the format: <type>: <short description>\nAllowed types: ${ALLOWED_TYPES[*]}\nExample: feat: add new user authentication feature"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 3. Validate the footer (last line) of the commit message
|
||||||
|
FOOTER=$(tail -n 1 "$COMMIT_MSG_FILE")
|
||||||
|
|
||||||
|
# Regex breakdown:
|
||||||
|
# ^(ref|close) - Starts with 'ref' or 'close'
|
||||||
|
# : - Followed by a literal colon
|
||||||
|
# \s - Followed by a single space
|
||||||
|
# N25B- - Followed by the literal string 'N25B-'
|
||||||
|
# [0-9]+ - Followed by one or more digits
|
||||||
|
# $ - End of the line
|
||||||
|
FOOTER_REGEX="^(ref|close): N25B-[0-9]+$"
|
||||||
|
|
||||||
|
if ! [[ "$FOOTER" =~ $FOOTER_REGEX ]]; then
|
||||||
|
error_exit "Invalid footer format.\n\nFooter must be in the format: [ref/close]: <issue identifier>\nExample: ref: N25B-123"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 4. If the message has more than 2 lines, validate the separator
|
||||||
|
# A blank line must exist between the header and the body.
|
||||||
|
LINE_COUNT=$(wc -l <"$COMMIT_MSG_FILE" | xargs) # xargs trims whitespace
|
||||||
|
|
||||||
|
# We only care if there is a body. Header + Footer = 2 lines.
|
||||||
|
# Header + Blank Line + Body... + Footer > 2 lines.
|
||||||
|
if [ "$LINE_COUNT" -gt 2 ]; then
|
||||||
|
# Get the second line
|
||||||
|
SECOND_LINE=$(sed -n '2p' "$COMMIT_MSG_FILE")
|
||||||
|
|
||||||
|
# Check if the second line is NOT empty. If it's not, it's an error.
|
||||||
|
if [ -n "$SECOND_LINE" ]; then
|
||||||
|
error_exit "Missing blank line between header and body.\n\nThe second line of your commit message must be empty if a body is present."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If all checks pass, exit with success
|
||||||
|
echo -e "${GREEN}Commit message is valid.${NC}"
|
||||||
|
exit 0
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
commit_msg_file=$1
|
|
||||||
commit_msg=$(cat "$commit_msg_file")
|
|
||||||
|
|
||||||
if echo "$commit_msg" | grep -Eq "^(feat|fix|refactor|perf|style|test|docs|build|chore|revert): .+"; then
|
|
||||||
if echo "$commit_msg" | grep -Eq "^(ref|close):\sN25B-.+"; then
|
|
||||||
exit 0
|
|
||||||
else
|
|
||||||
echo "❌ Commit message invalid! Must end with [ref/close]: N25B-000"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "❌ Commit message invalid! Must start with <type>: <description>"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# Get current branch
|
|
||||||
branch=$(git rev-parse --abbrev-ref HEAD)
|
|
||||||
|
|
||||||
if echo "$branch" | grep -Eq "(dev|main)"; then
|
|
||||||
echo 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# allowed pattern <type/>
|
|
||||||
if echo "$branch" | grep -Eq "^(feat|fix|refactor|perf|style|test|docs|build|chore|revert)\/\w+(-\w+){0,5}$"; then
|
|
||||||
exit 0
|
|
||||||
else
|
|
||||||
echo "❌ Invalid branch name: $branch"
|
|
||||||
echo "Branch must be named <type>/<description-of-branch> (must have one to six words separated by a dash)"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
echo "#<type>: <description>
|
|
||||||
|
|
||||||
#[optional body]
|
|
||||||
|
|
||||||
#[optional footer(s)]
|
|
||||||
|
|
||||||
#[ref/close]: <issue identifier>" > $1
|
|
||||||
@@ -1,10 +1,24 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
# Ruff version.
|
# Ruff version.
|
||||||
rev: v0.14.2
|
rev: v0.14.2
|
||||||
hooks:
|
hooks:
|
||||||
# Run the linter.
|
# Run the linter.
|
||||||
- id: ruff-check
|
- id: ruff-check
|
||||||
args: [ --fix ]
|
# Run the formatter.
|
||||||
# Run the formatter.
|
- id: ruff-format
|
||||||
- id: ruff-format
|
# Configure local hooks
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: check-commit-msg
|
||||||
|
name: Check commit message format
|
||||||
|
entry: .githooks/check-commit-msg.sh
|
||||||
|
language: script
|
||||||
|
stages: [commit-msg]
|
||||||
|
- id: check-branch-name
|
||||||
|
name: Check branch name format
|
||||||
|
entry: .githooks/check-branch-name.sh
|
||||||
|
language: script
|
||||||
|
stages: [pre-commit]
|
||||||
|
always_run: true
|
||||||
|
pass_filenames: false
|
||||||
|
|||||||
Reference in New Issue
Block a user