Skip to content

Instantly share code, notes, and snippets.

@Aminot1
Created April 25, 2022 23:53
Show Gist options
  • Save Aminot1/8466ef995e24171915fdd8b3e433eb30 to your computer and use it in GitHub Desktop.
Save Aminot1/8466ef995e24171915fdd8b3e433eb30 to your computer and use it in GitHub Desktop.
validate branch name
#!/usr/bin/env bash
# https://itnext.io/using-git-hooks-to-enforce-branch-naming-policy-ffd81fa01e5e
LC_ALL=C
local_branch="$(git rev-parse --abbrev-ref HEAD)"
#local_branch='testing here'
valid_branch_regex="^(develop|feature|bugfix|improvement|prerelease|release|hotfix)\/[a-z0-9.-]+$"
valid_main_branch_names=( "master" "develop" "dev" "qa" "staging")
error_message="There is something wrong with your branch name. Branch names in this project must adhere to this contract: $valid_branch_regex. Your commit will be rejected. You should rename your branch to a valid name and try again."
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
if ! printf '%s\0' "${valid_main_branch_names[@]}" | grep -Fxqz "$local_branch";
then
echo $error_message
exit 1
fi
fi
echo "branch name valid"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment