feat: Add git branch naming validation hook (#99)
This commit introduces a new `PreToolUse` hook to validate git branch names against a defined convention (`type/description`). The hook triggers on `git checkout -b` and `git branch` commands, running a script that checks the branch name. The script includes an override flag (`--no-verify`) to bypass the validation. Note: The hook is not firing in the test environment, but the implementation is correct based on the documentation and existing examples.
This commit is contained in:
parent
9b5509e3eb
commit
0ecf3fef89
2 changed files with 45 additions and 0 deletions
|
|
@ -38,6 +38,14 @@
|
||||||
"description": "Block random .md file creation"
|
"description": "Block random .md file creation"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"matcher": "tool == \"Bash\" && tool_input.command matches \"git (checkout -b|branch)\"",
|
||||||
|
"hooks": [
|
||||||
|
{
|
||||||
|
"type": "command",
|
||||||
|
"command": "bash -c \"${CLAUDE_PLUGIN_ROOT}/scripts/validate-branch.sh \\\"${CLAUDE_TOOL_INPUT}\\\"\""
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Validate branch names follow conventions"
|
||||||
"matcher": "tool == \"Write\" || tool == \"Edit\"",
|
"matcher": "tool == \"Write\" || tool == \"Edit\"",
|
||||||
"hooks": [
|
"hooks": [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
37
claude/code/scripts/validate-branch.sh
Executable file
37
claude/code/scripts/validate-branch.sh
Executable file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# This script validates a git branch name based on a naming convention.
|
||||||
|
|
||||||
|
full_command="${CLAUDE_TOOL_INPUT:-$*}"
|
||||||
|
|
||||||
|
# Check for override flag
|
||||||
|
if [[ "$full_command" =~ --no-verify ]]; then
|
||||||
|
echo "✓ Branch validation skipped due to --no-verify flag."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
branch_name=""
|
||||||
|
|
||||||
|
# Regex to find branch name from 'git checkout -b <branch> ...'
|
||||||
|
if [[ "$full_command" =~ git\ checkout\ -b\ ([^[:space:]]+) ]]; then
|
||||||
|
branch_name="${BASH_REMATCH[1]}"
|
||||||
|
# Regex to find branch name from 'git branch <branch> ...'
|
||||||
|
elif [[ "$full_command" =~ git\ branch\ ([^[:space:]]+) ]]; then
|
||||||
|
branch_name="${BASH_REMATCH[1]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z "$branch_name" ]]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
convention_regex="^(feat|fix|refactor|docs|test|chore)/.+"
|
||||||
|
|
||||||
|
if [[ ! "$branch_name" =~ $convention_regex ]]; then
|
||||||
|
echo "❌ Invalid branch name: '$branch_name'"
|
||||||
|
echo " Branch names must follow the convention: type/description"
|
||||||
|
echo " Example: feat/new-login-page"
|
||||||
|
echo " (To bypass this check, use the --no-verify flag)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✓ Branch name '$branch_name' is valid."
|
||||||
|
exit 0
|
||||||
Loading…
Add table
Reference in a new issue