At Agilize a common developer workflow involves the usage of Jira for issue tracking, Git + Git Flow for versioning code, Slack for communication and programmer’s tools like IDE, Text Editor and Terminal. Basically, a prioritized sprint is maintained in Jira full of cards that describe the features, bugs, spikes and chores following some conventions of SCRUM and the software engineer assign himself to a card. During the development there is a convention that commits must have the issue code the engineer is working on. After some commits the convention gets boring (you have to type the issue code all time). Hopefully there is a way to automate that.
Git allows us to run a script every time a particular event occurs in the repository. For instance, it is impossible to populate the text editor with a template message.This tutorial explains the power of git hooks and provides a snippet that will be customized in this post to address our necessities.
Inside your repository there is a folder .git/hooks that contains some sample scripts that can be used as a playground and they end with .sample in order to not be interpreted by Git. Create a file named prepare-commit-msg that will contain the code below to automate the commit message.
After typing git commit the file above is called to populate the commit message and, thus, enables us to adopt a template in our commits. The script is called with three arguments:
- File name
- The name of a temporary file that contains the message
- The type of commit
- The commit SHA1 hash
At Agilize the issue code follows the regex in the line 6. Once we use gitflow to manage our branches they will always follow some examples like feature/AGZ-1256, hotfix/AGZ-981, release/AGZ-453. Therefore, commits are not created in the master or dev branches. Due to that, the variable in the line 6 defines a regex that will use the branch name to create our commit message. If you want to apply that to your company it just takes to update the regex (pay attention to the regex group in line 17).
The lines 15 and 16 check if the current branch is not master/dev and contains the issue code. The final lines just create the message with the code.
You can make the cursor start right after the template message. Just add the snippet below to your .bashrc file.
Want more resources? Check out the links below: