1+ #! /bin/bash
2+ # Git pre-push hook for validating branch names.
3+
4+ # --- Configuration ---
5+ BRANCH_REGEX=" ^(task|bug|feature)/[0-9]+-[a-zA-Z0-9-]+$"
6+
7+ # --- Script Logic ---
8+
9+ # Get the name of the current branch
10+ CURRENT_BRANCH=$( git symbolic-ref --short HEAD)
11+
12+ # Exclude main branch from validation
13+ EXCLUDED_BRANCHES=(" main" )
14+
15+ # Check if the current branch is one of the excluded branches
16+ for EXCLUDED in " ${EXCLUDED_BRANCHES[@]} " ; do
17+ if [[ " $CURRENT_BRANCH " == " $EXCLUDED " ]]; then
18+ echo " Skipping branch name validation for protected branch: $CURRENT_BRANCH "
19+ exit 0
20+ fi
21+ done
22+
23+ # Validate the current branch name against the regex
24+ if [[ " $CURRENT_BRANCH " =~ $BRANCH_REGEX ]]; then
25+ echo " Branch name '$CURRENT_BRANCH ' is valid. Push allowed."
26+ exit 0
27+ else
28+ echo " --------------------------------------------------------"
29+ echo " PUSH FAILED: Invalid branch name format."
30+ echo " Branch: $CURRENT_BRANCH "
31+ echo " --------------------------------------------------------"
32+ echo " Branch naming rules are:"
33+ echo " 1. Must start with 'task/', 'bug/', or 'feature/'"
34+ echo " 2. Must include an issue number (digits) followed by a dash."
35+ echo " 3. Must end with a descriptive slug (letters, numbers, dashes)."
36+ echo " "
37+ echo " Examples of valid names:"
38+ echo " - feature/123-add-user-profile"
39+ echo " - bug/45-fix-login-redirect"
40+ echo " - task/789-update-composer-deps"
41+ echo " --------------------------------------------------------"
42+ # Exit with a non-zero status to abort the push
43+ exit 1
44+ fi
0 commit comments