Automatic rule loading system for Claude Code. Detects git operations and slash commands, loads relevant coding rules, enforces pre-commit quality gates.
File: .claude/hooks/activate-rules.sh
Executes on every user prompt. Detects project context and loads relevant rules.
Detection mechanisms:
- Keywords - git, commit, test, security, refactor terms
- Slash commands -
/xgit,/xtest,/xsecurity,/xrefactor
Actions:
- Scan project for language markers (
package.json,pyproject.toml,go.mod) - Detect frameworks by parsing dependency files
- Identify cloud providers via configuration files
- Inject 2-3 most relevant rules from centralized repository
When git operations are detected (commit, push, PR), enforce quality checks:
- Run tests - 100% pass rate required
- Security scan - No vulnerabilities
- Code quality - Linting, complexity checks
- Refactoring check - Detect code smells
Triggers:
- Keywords: commit, push, pull request, pr, merge, branch, rebase
- Slash commands:
/xgit,/git,/xcommit,/commit,/push
Git Operations:
/xgit- External git skill (commit + push)/git- Git operations/xcommit- Commit with checks/commit- Commit changes/push- Push to remote
Testing:
/xtest- Run test suite/test- Execute tests/xtdd- TDD workflow
Security:
/xsecurity- Security scan/security- Security audit/xaudit- Comprehensive audit
Code Quality:
/xrefactor- Refactoring assistant/xquality- Quality checks/xoptimize- Performance optimization
Location: .claude/hooks/activate-rules.sh
# Git/commit keywords (including slash commands)
if echo "${prompt_lower}" | grep -qE '(commit|pull request|pr|merge|branch|push)'; then
matched_rules+=("base/git-workflow")
# Detect git-related slash commands (e.g., /xgit, /commit, /xcommit, /git)
elif echo "${prompt_lower}" | grep -qE '/(x?git|x?commit|push)(\s|$)'; then
matched_rules+=("base/git-workflow")
fiRegex patterns:
/(x?git|x?commit|push)- Matches/git,/xgit,/commit,/xcommit,/pushx?- Optional "x" prefix(\s|$)- Followed by space or end of string (prevents false matches)
When git operation detected:
-
Announce checks
Running pre-commit checks... -
Execute in order
✓ Tests: 45 passed, 0 failed ✓ Security: No vulnerabilities found ✓ Code Quality: All checks passed ✓ Refactoring: No code smells detected -
Report results
- Show summary of all checks
- Highlight failures
- Provide actionable feedback
-
Proceed or block
- ✅ All checks pass → proceed with commit/push
- ❌ Any check fails → block commit, show errors
Input: "commit these changes to fix the login bug"
Output:
🚦 PRE-COMMIT QUALITY GATES DETECTED
───────────────────────────────────
Running pre-commit checks...
1️⃣ Running tests...
✓ 45 tests passed
2️⃣ Security scan...
✓ No vulnerabilities found
3️⃣ Code quality check...
✓ ESLint: 0 errors
✓ TypeScript: No type errors
4️⃣ Refactoring check...
✓ No code smells detected
All checks passed! ✅
Proceeding with commit...
Input: "/xgit"
Behavior: Same as keyword trigger - loads git workflow rules and enforces quality gates.
Input: "commit my changes"
Output:
🚦 PRE-COMMIT QUALITY GATES DETECTED
───────────────────────────────────
Running pre-commit checks...
1️⃣ Running tests...
❌ 3 tests failed:
- test_authentication.py::test_login_validation
- test_authentication.py::test_logout_flow
- test_api.py::test_rate_limiting
⚠️ COMMIT BLOCKED
Cannot proceed with commit due to failing tests.
Fix the failing tests and try again.
Edit .claude/hooks/activate-rules.sh:
# Custom slash command detection
if echo "${prompt_lower}" | grep -qE '/(x?custom|my-command)(\s|$)'; then
matched_rules+=("custom/my-rules")
fiUpdate skill-rules.json keywords:
{
"keywordMappings": {
"base": {
"custom_category": {
"keywords": ["custom", "my-command"],
"slashCommands": ["/custom", "/my-command"],
"rules": ["base/custom-rules.md"]
}
}
}
}Temporarily disable:
# Rename hook
mv .claude/hooks/activate-rules.sh .claude/hooks/activate-rules.sh.disabledRun test suites:
# Slash command detection
./tests/test-slash-command-detection.sh
# Pre-commit quality gates
./tests/test-precommit-gates.shExpected: 16 slash command tests pass, 17 quality gate tests pass.
Test specific prompt:
echo '{"prompt":"/xgit"}' | .claude/hooks/activate-rules.sh | jq -r '.systemMessage'- ✅
/xgitskill (external) - ✅
/xtestskill (if available) - ✅
/xsecurityskill (if available) - ✅
/xqualityskill (if available) - ✅ Traditional git commands
- ✅ GitHub CLI (
gh pr create)
- ❌ Non-git operations (feature implementation, bug fixes, refactoring)
- ❌ Research/exploration tasks
- ❌ Documentation updates (unless committing)
Problem: Type "commit changes" but no quality gates banner appears.
Solutions:
- Verify hook exists:
ls -la .claude/hooks/activate-rules.sh - Check executable:
chmod +x .claude/hooks/activate-rules.sh - Verify configured in
.claude/settings.json - Test:
echo '{"prompt":"commit"}' | .claude/hooks/activate-rules.sh
Problem: Pre-commit quality gate tests fail.
Solutions:
- Check syntax:
bash -n .claude/hooks/activate-rules.sh - Verify regex patterns work
- Review test output for specific failures
- Check jq installed:
which jq
Problem: Quality gates trigger when they shouldn't.
Solutions:
- Review keyword list in
is_git_operation()function - Make regex patterns more specific
- Add negative patterns to exclude cases
- Adjust detection logic in
activate-rules.sh
- Hook implementation:
.claude/hooks/activate-rules.sh - Keyword mappings:
.claude/skills/skill-rules.json - Test suites:
tests/test-slash-command-detection.sh,tests/test-precommit-gates.sh