Repository navigation
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·97 lines (84 loc) · 3.14 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·97 lines (84 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
##
# Pre-commit Hook: Branch Name & Changelog Safety Validation
#
# Validates:
# 1. Branch names — ensure they follow LightSpeed branching strategy: {type}/{scope}-{short-title}
# 2. Changelog changes (Phase 2) — prevent commits with invalid CHANGELOG.md
#
# This hook automatically skips validation for:
# - Protected branches (main, develop)
# - Bot/automation branches (dependabot/*, renovate/*)
# - Detached HEAD state (during rebase, merge, bisect operations)
#
# If validation fails, the commit is blocked with a helpful error message.
# To bypass the hook for development/testing, use: git commit --no-verify
#
# Installation: npm run setup:hooks
#
##
set -e
# Colors for output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Get the current branch name
BRANCH_NAME=$(git branch --show-current 2>/dev/null || true)
# Skip validation if in detached HEAD state (rebase, merge, bisect, etc.)
if [ -z "$BRANCH_NAME" ]; then
exit 0
fi
# Skip validation for protected and bot branches
if [ "$BRANCH_NAME" = "main" ] || [ "$BRANCH_NAME" = "develop" ] || \
[[ "$BRANCH_NAME" =~ ^(dependabot|renovate)/ ]]; then
exit 0
fi
# Get the repository root
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
# Step 1: Branch Name Validation
BRANCH_VALIDATION_SCRIPT="$REPO_ROOT/scripts/validation/validate-branch-name.cjs"
if [ -f "$BRANCH_VALIDATION_SCRIPT" ]; then
if ! node "$BRANCH_VALIDATION_SCRIPT" "$BRANCH_NAME"; then
echo ""
echo "💡 Tip: If you need to bypass this check for testing, use:"
echo " git commit --no-verify"
echo ""
exit 1
fi
else
echo "⚠️ Branch validation script not found at $BRANCH_VALIDATION_SCRIPT"
fi
# Step 2: Changelog Safety Validation (Phase 2)
CHANGELOG_FILE="CHANGELOG.md"
CHANGELOG_VALIDATION_SCRIPT="$REPO_ROOT/scripts/validation/validate-changelog-safety.js"
# Check if changelog was modified in this commit
if git diff --cached --name-only | grep -q "$CHANGELOG_FILE"; then
if [ -f "$CHANGELOG_VALIDATION_SCRIPT" ]; then
echo -e "${YELLOW}🔐 Validating staged CHANGELOG.md...${NC}"
# Run validation on the entire repository (includes staged changelog)
# The validation script will read the working directory version which includes staged changes
if node "$CHANGELOG_VALIDATION_SCRIPT" > /dev/null 2>&1; then
echo -e "${GREEN}✅ Changelog validation passed${NC}"
else
echo ""
echo -e "${RED}❌ Changelog validation failed${NC}"
echo -e "${RED}The staged CHANGELOG.md failed validation checks.${NC}"
echo ""
echo -e "${YELLOW}Common issues to check:${NC}"
echo " 1. YAML frontmatter present (---...---)"
echo " 2. [Unreleased] section exists"
echo " 3. Version sections follow: ## [X.Y.Z] - YYYY-MM-DD"
echo " 4. Entries follow format with em-dash (—) separator"
echo " 5. No duplicate version tags"
echo " 6. Valid dates in version headers"
echo " 7. All links properly formatted"
echo ""
echo "To bypass this check, use: git commit --no-verify"
echo "(Not recommended — CI will still fail!)"
echo ""
exit 1
fi
fi
fi
exit 0