Make your Commit Messages Great Again!

Abhishek Bharadwaj
AndroidPub
Published in
4 min readFeb 19, 2020
Photo by Safar Safarov on Unsplash

The Problem πŸ›

We recently came across a production bug that was causing our android app to crash, so we started debugging πŸ₯.

After a few attempts, we figured out it was caused by a commit written by Mr. X. A big chunk of code(15+ files) was committed in a single commit and the code was lacking the proper documentation and tests. Unfortunately, Mr. X went on a vacation and took Mr. Y(The Reviewer) with him πŸ€·β€β™‚.

With the time constraint we had, it was hard to figure out what that code was doing. Like any normal human being, we looked at the commit message and which said β€œFixes”. Logically fix is supposed to fix something, but… 😒

A lazy commit message!

We had no choice but to revert that commit and push the app again as we already got a few negative PlayStore reviews.

The Meeting πŸ˜’

After small retrospective with stakeholders, we came on a conclusion that

  • All devs should write proper documentation.
  • Sufficient test coverage should be there for any feature.
  • And the simplest one is to write a proper commit message!

We usually have a decent code review cycle and we make sure that the first two points should be addressed before any code gets merged, but for the third we were ignorant. We decided #ItsTimeTo write proper commit messages!

The Solution πŸ’‘

The answer was Git Hooks βš“. Git hooks are basically git’s way to fire off custom scripts when certain important actions occur like committing, merging, etc.

We were using them in our backend codebase, so we decided to use them for our android repo as well! After going through the documentation, we figured out the Git provides a commit-msg hook which we can use to validate our commit messages. By validation, I mean writing checks like commit messages should be in some format, should contain certain keywords, etc.

As soon as we started to think about git hooks we came across a few more smaller problems πŸ€¦β€β™‚.

  • Git hooks need to be written inside .git/ folder and everyone can have their own copy of git hooks as its not part of VCS.
  • Setting up infra where everyone can contribute to git hooks, which can be shared across the team.

After some research, we figured out how to solve this, and the solution was to create a githook folder in our apps codebase where everyone will write git hooks, and during the build, we copy those hooks to ./git folder and make them executable. πŸ™‚

The Code πŸ’»

It’s quite possible to write very sophisticated hooks that do all kinds of regular expression matching and validate commit messages, but for now, we are going to write a simple hook that checks for commit message length.

commit-msg

This is the small python script which we wrote, which does that. Just remember one thing the name of the script should be commit-msg ⚠️. You do have the liberty to use any scripting language like Python, ShellScript or even Kotlin.

One problem solved. Now for the second problem - how do we update this to the ./git folder for everyone in the team? For that, we wrote a Gradle script using kotlin, because why not!

githooks.gradle.kts

This script can be named anything. What it does is, it copies commit-msg from githooks/ folder to ./git/hooks/ and make them executable. We also wrote a small String extension function that runs shell commands (tested on Mac and Linux), and we are done! πŸ™Œ.

In the end, we have proper infra to write git hooks AND from next time onwards, Mr. X will get an error if he wants to write lazy commit messages and commit will fail.

To proper commit messages and beyond...

With git hooks, possibilities are endless and we can write all sorts of pre-commit, pre-push, etc hooks to maintain code quality and readability.

The Quote ⭐️

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Thanks for reading if you have any doubts/suggestions please write down in the comment section. If you like the article do give me some πŸ‘ πŸ˜ƒ.

--

--