Jezen Thomas

Jezen Thomas Jezen Thomas

I'm a Haskell/Elm programmer, the founder of NBM, co-founder of Comparestack, and a public speaker. Occasionally I write about technology, and currently I am perpetually travelling the world. Find me on Instagram, or on GitHub, or write me an email.

Automatic Quality Assurance with Git Hooks

For some months now I’ve been trying to make sure every commit in my Rails project passes all tests and style checks. This helps to guard against code quality taking a nose-dive that usually is inevitable. Git (and Mercurial) allows us to run scripts at selected points of our version-control workflow.

At a high level, this means we can write a script that makes a number of checks against our codebase, and have Git automatically run this script any time we try making a commit.

I have a bash script in my Rails app that does the following:

  • Stash unstaged changes so checks are only run against staged changes
  • Run style checks with Rubocop
  • Check for security vulnerabilities with Brakeman
  • Run RSpec tests
  • Run Cucumber tests
  • Update application version number

If at any point any of those checks should fail, we should pop the stash and abort the commit.

Stashing Unstaged Changes

In my script I check if there are any staged changes, and stash anything that isn’t staged. This is because I only want to run checks against changes I’m including in the commit. If there are no changes staged for the commit, the script exits early without stashing anything. Don’t worry about the highlight function for now; it’s just a wrapped version of printf with some colour, and you’ll see it later.

Quality Control

For a git-hook to succeed, the script should exit with an error code of 0. After each check I add the exit code of that check to a counter, and exit the commit hook with that counter as the error code. If none of the checks fail, the exit code will be 0 and Git will allow the commit. Otherwise, the commit is aborted, and I have to fix the mistakes in my codebase.

The call to trap is a bit like Kernel#at_exit that you find in Ruby; it listens for an EXIT event and runs some function before exiting. In my case I’ve asked it to run a function called pop_stash, which will revert my working directory to the state it was in before stashing unstaged changes.

My checks are specific to Ruby/Rails projects, but there are most likely equivalents for whichever tech stack you’re using. I’m using the brakeman gem to protect me from creating obvious security vulnerabilities, and I think every project ought to have something like this running regularly.

Automatic Application Versioning

I like the idea of seeing my application version printed somewhere in my app. If all of my quality control checks pass, I write a datestamp to a file in my application. I went with a datestamp instead of major/minor numbers or a commit hash because it’s the simplest thing that works, and also it makes the most sense. I use Git to then add the bumped version to the staging area.

If you want to use this version number in your Rails app, you can add the following in config/application.rb.

The Whole Script

I was jumping around the script before so I could explain its components in finer detail, but this is an imperative script so the order in which each line is executed is important. Here’s the complete script for you to copy and paste reference.