11#! /bin/bash
22# Git pre-push hook for validating branch names.
33
4- # --- Configuration ---
54BRANCH_REGEX=" ^(task|bug|feature)/[0-9]+-[a-zA-Z0-9-]+$"
65
7- # --- Script Logic ---
6+ # ANSI Colors
7+ RED=' \033[0;31m'
8+ GREEN=' \033[0;32m'
9+ NC=' \033[0m' # No Color (reset)
810
911# Get the name of the current branch
1012CURRENT_BRANCH=$( git symbolic-ref --short HEAD)
@@ -15,20 +17,20 @@ EXCLUDED_BRANCHES=("main")
1517# Check if the current branch is one of the excluded branches
1618for EXCLUDED in " ${EXCLUDED_BRANCHES[@]} " ; do
1719 if [[ " $CURRENT_BRANCH " == " $EXCLUDED " ]]; then
18- echo " Skipping branch name validation for protected branch: $CURRENT_BRANCH "
20+ echo -e " ${GREEN} Skipping branch name validation for protected branch: $CURRENT_BRANCH ${NC} "
1921 exit 0
2022 fi
2123done
2224
2325# Validate the current branch name against the regex
2426if [[ " $CURRENT_BRANCH " =~ $BRANCH_REGEX ]]; then
25- echo " Branch name '$CURRENT_BRANCH ' is valid. Push allowed."
27+ echo -e " ${GREEN} Branch name '$CURRENT_BRANCH ' is valid. Push allowed.${NC} "
2628 exit 0
2729else
28- echo " --------------------------------------------------------"
29- echo " PUSH FAILED: Invalid branch name format."
30- echo " Branch: $CURRENT_BRANCH "
31- echo " --------------------------------------------------------"
30+ echo -e " ${RED} --------------------------------------------------------${NC} "
31+ echo -e " ${RED} ❌ PUSH FAILED: Invalid branch name format.${NC} "
32+ echo -e " ${RED} Branch: $CURRENT_BRANCH ${NC} "
33+ echo -e " ${RED} --------------------------------------------------------${NC} "
3234 echo " Branch naming rules are:"
3335 echo " 1. Must start with 'task/', 'bug/', or 'feature/'"
3436 echo " 2. Must include an issue number (digits) followed by a dash."
3840 echo " - feature/123-add-user-profile"
3941 echo " - bug/45-fix-login-redirect"
4042 echo " - task/789-update-composer-deps"
41- echo " --------------------------------------------------------"
43+ echo -e " ${RED} --------------------------------------------------------${NC} "
4244 # Exit with a non-zero status to abort the push
4345 exit 1
4446fi
0 commit comments