How to prevent creating branches with the prefixes that are not defined in the "Branching Model" using git hooks in Bitbucket Cloud?
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
Summary
Currently, Bitbucket Cloud does not have a feature to disable creating branch prefixes which are not defined in the "Branching Model" of the repository settings. If the user wishes to have only Bugfix/Feature/Hotfix/Release branch naming conventions pattern, but, the user can also create branches with different patterns which may cause inconsistencies within the repository.
Solution
In this scenario, git hooks can be used. Git hooks are regular scripts that automatically execute whenever a specific event occurs in a git repository. Git hooks can effectively limit users from creating branches, committing to branches outside of a pre-determined pattern, and pushing such changes to the remote repository.
Please note that these are client-side git hooks and need to be configured on each user's local machine.
How to configure git hook scripts on the local machine?
- Navigate to the local repository's folder to find pre-defined git hooks.
- Look for the "hooks" directory within the ".git" directory of the repository.
- Inside the "hooks" folder, various pre-defined scripts with a ".sample" extension are available.
- To make a script functional, remove the ".sample" extension and save the script with only the name.
For instance, before utilizing the pre-push script, rename the hook script from "pre-push.sample" to "pre-push."
Example steps are given below:
# cd .git
# ls
HEAD config description hooks index info logs objects packed-refs refs
# cd hooks
# ls
applypatch-msg.sample fsmonitor-watchman.sample pre-applypatch.sample pre-merge-commit.sample pre-rebase.sample prepare-commit-msg.sample update.sample
commit-msg.sample post-update.sample pre-commit.sample pre-push.sample pre-receive.sample push-to-checkout.sample
# mv pre-push.sample pre-push
# mv pre-commit.sample pre-commit
Once the script names are changed, the scripts are ready for use.
Example:
The below script will allow the creation of only the pre-defined type of branches with the prefix "bugfix, feature, hotfix, release".
By making changes to the regex query (valid_branch_regex value) in the below scripts, different results can be obtained depending on the use case and needs.
2 types of git hooks are required here:
- pre-push
- pre-commit
The pre-push/pre-commit hook must be configured on each user's local machine to execute a script. This script will compare the current branch against a set of conditions, determining whether to permit or deny the user from pushing the branch to the remote repository or committing to it.
- Pre-commit hook script
#!/bin/bash
LC_ALL=C
local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(bugfix|feature|hotfix|release)[a-z0-9/._-]+$"
message="This branch violates the branch naming rules. Please rename your branch."
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
echo "$message"
exit 1
fi
exit 0
- Pre-push hook script
#!/bin/bash
local_branch=`git rev-parse --abbrev-ref HEAD` valid_branch_regex="^(bugfix|feature|hotfix|release)[a-z0-9/._-]+$"
if [[ ! $local_branch =~ $valid_branch_regex ]]; then
echo " \"$local_branch\" is not allowed by pre-push hook, please use only allowed pattern"
exit 1
fi
exit 0
Please note that these scripts are for reference purposes only. Kindly modify it based on the use case.
After configuring the aforementioned two scripts, if a user attempts to commit changes to a branch that deviates from the specified regex pattern or tries to create a branch with a distinct naming convention, the user will receive the following error or response:
git push -u origin temp/test
git: 'credential-show' is not a git command. See 'git --help'.
The most similar command is
credential-store
"temp/test" is not allowed by pre-push hook, please use only allowed pattern
error: failed to push some refs to 'https://bitbucket.org/workspace/repo.git'