Using Git hooks to enforce branch naming policy
Using Git hooks to enforce branch naming policy - ITNEXT
Photo by Patrick Perkins on UnsplashWhen you have many people collaborating in a repository, the number and names of branches can quickly get out of control. Hierarchical branch folders is an effective way to tame the chaos. So, you can prepend the special feature/ or release/ string in every branch, like:feature/mapboxrelease/7.5.0Azure DevOps treats / as a folder separator and will automatically collapse folders by default. But even if you have some guidelines for naming, you can’t rely on everyone to get it right. With most repository managers, like github, gitlab or bitbucket, it is easy to enforce naming policies just from the repository manager UI. The problem is that this doesn’t prevent developers to mistakenly create wrong branch names in their local repositories. Luckily , there is a way to check branch names locally before commit, and it all starts with Git hooks.What are Git hooks?Git hooks are scripts that run automatically every time a particular event occurs in a Git repository such as: commit, push, and receive. They let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle. Git hooks are a built-in feature — no need to download anything. Git hooks are run locally.These hook scripts are only limited by a developer’s imagination. Some example hook scripts include:pre-commit: Check the commit message for spelling errors.pre-receive: Enforce project coding standards.post-commit: Email/SMS team members of a new commit.post-receive: Push the code to production.https://www.atlassian.com/git/tutorials/git-hooksEvery Git repository has a .git/hooks folder with a script for each hook you can bind to. You're free to change or update these scripts as necessary, and Git will execute them when those events occur.More on git hooks https://githooks.com/Creating the hook to check branch namesThere is not a dedicated hook that runs when a new branch is created, so we will use pre-commit hook. We should add a pre-commit file to the hooks directory. In the hook, we will perform the following tasks:Check whether the branch name is correct. If not, we will print an error message end exit with 1.If everything is ok, we exit with 0.To get current branch name we use thisgit rev-parse --abbrev-ref HEADThen to check whether the name is correct, we can use regular expression. In our case name should start with feature, bugfix, etc. and only contain lowercase letters, digits, and some special symbols.^(feature|bugfix|improvement|library|prerelease|release|hotfix)\/[a-z0-9._-]+$RegexperHere is the final hook code (simple bash script):After adding this hook and trying to commit to branch, that does not adher to naming policy, we get this error:Intellij / Android StudioShare Git hooks with your teamAs mentioned earlier, hooks are in the .git folder, which is where all the Git metadata is stored. That folder is not under version control, since most of its contents are device and user-specific. If you’re using Git version 2.9 or greater, there is as simple way to change hooks directory using core.hooksPath configuration variable. Just create a folder inside the repository and put it under version control and then change hooks directory.$ git config core.hooksPath .githooksIn our case we have created .githooks at the root where all these hooks live.ConclusionI hope that this will help some of you to get started with using Git hooks and align the workflow of your development team :-)Thanks for reading! Feel free to leave any feedback!Connect with me on:MediumLinkedInTwitterGithub