Last active
July 15, 2025 14:33
-
-
Save yasn77/cc26c984fa31de21e69585c0185aba01 to your computer and use it in GitHub Desktop.
pre-push branch name check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - id: branch-name-validation | |
| name: Validate branch name | |
| entry: ./pre-push-branch-check.sh | |
| language: script | |
| stages: [pre-push] | |
| pass_filenames: false | |
| always_run: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Pre-push hook to validate branch names | |
| # This script validates that branch names follow the specified naming conventions | |
| set -e | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Get the current branch name | |
| current_branch=$(git rev-parse --abbrev-ref HEAD) | |
| # Skip validation for main/master branches | |
| if [[ "$current_branch" == "main" || "$current_branch" == "master" || "$current_branch" == "develop" ]]; then | |
| echo -e "${GREEN}✓ Skipping branch name validation for $current_branch${NC}" | |
| exit 0 | |
| fi | |
| echo -e "${YELLOW}Validating branch name: $current_branch${NC}" | |
| # Function to validate branch name | |
| validate_branch_name() { | |
| local branch_name="$1" | |
| local errors=() | |
| local branch_body="$branch_name" | |
| local has_prefix=false | |
| # Check if branch name is kebab-case and hyphen-separated | |
| if [[ ! "$branch_name" =~ ^[A-Za-z0-9/-]+$ ]]; then | |
| errors+=("Branch name must contain only letters (either case), numbers, hyphens, and forward slashes") | |
| fi | |
| # If a prefix is present, validate it | |
| if [[ "$branch_name" == *"/"* ]]; then | |
| has_prefix=true | |
| valid_prefixes=("feature/" "bugfix/" "hotfix/" "fix/" "chore/" "release/" "docs/") | |
| has_valid_prefix=false | |
| for prefix in "${valid_prefixes[@]}"; do | |
| if [[ "$branch_name" == "$prefix"* ]]; then | |
| has_valid_prefix=true | |
| branch_body=${branch_name#"$prefix"} # remove prefix | |
| break | |
| fi | |
| done | |
| if [[ "$has_valid_prefix" == false ]]; then | |
| # It has a slash, but not a valid prefix. | |
| errors+=("Branch name contains a '/' but does not have a valid prefix. Valid prefixes are: ${valid_prefixes[*]}") | |
| fi | |
| fi | |
| # Check for continuous hyphens | |
| if [[ "$branch_body" == *"--"* ]]; then | |
| errors+=("Branch name cannot contain continuous hyphens (--)") | |
| fi | |
| # Check for trailing hyphens | |
| if [[ "$branch_body" == *"-" ]]; then | |
| errors+=("Branch name cannot end with a hyphen") | |
| fi | |
| # Check for leading hyphens | |
| if [[ "$branch_body" == "-"* ]]; then | |
| if [ "$has_prefix" = true ]; then | |
| errors+=("Branch name cannot have a hyphen immediately after the prefix") | |
| else | |
| errors+=("Branch name cannot start with a hyphen") | |
| fi | |
| fi | |
| # Check minimum length | |
| if [[ ${#branch_body} -lt 3 ]]; then | |
| if [ "$has_prefix" = true ]; then | |
| errors+=("Branch name after prefix must be at least 3 characters long") | |
| else | |
| errors+=("Branch name must be at least 3 characters long") | |
| fi | |
| fi | |
| # Return errors | |
| if [[ ${#errors[@]} -gt 0 ]]; then | |
| echo -e "${RED}✗ Branch name validation failed:${NC}" | |
| for error in "${errors[@]}"; do | |
| echo -e "${RED} - $error${NC}" | |
| done | |
| echo "" | |
| echo -e "${YELLOW}Branch naming rules:${NC}" | |
| echo -e "${YELLOW} - Use letters(either case), numbers, and hyphens only${NC}" | |
| echo -e "${YELLOW} - If using a prefix, it must be one of: feature/, bugfix/, fix/, chore/, hotfix/, release/, docs/${NC}" | |
| echo -e "${YELLOW} - Use hyphens to separate words (no continuous hyphens)${NC}" | |
| echo -e "${YELLOW} - No leading or trailing hyphens${NC}" | |
| echo -e "${YELLOW} - Be descriptive and concise${NC}" | |
| echo "" | |
| echo -e "${YELLOW}Examples:${NC}" | |
| echo -e "${YELLOW} - feature/user-authentication${NC}" | |
| echo -e "${YELLOW} - my-new-feature${NC}" | |
| echo -e "${YELLOW} - bugfix/header-styling${NC}" | |
| echo -e "${YELLOW} - hotfix/critical-security-issue${NC}" | |
| echo -e "${YELLOW} - release/v1.0.1${NC}" | |
| echo -e "${YELLOW} - docs/api-endpoints${NC}" | |
| echo -e "${YELLOW} - feature/T-123-new-login-system${NC}" | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| # Validate the current branch name | |
| if validate_branch_name "$current_branch"; then | |
| echo -e "${GREEN}✓ Branch name '$current_branch' is valid${NC}" | |
| exit 0 | |
| else | |
| echo -e "${RED}✗ Push rejected due to invalid branch name${NC}" | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment