One of my colleagues recently blogged about how the Confluence team avoids creating feature branches from bad commits. This blog post describes how to take the same idea one step further.

The Problem

I hate it when I make a trivial change, something like:

1
2
3
4
5
6
7
8
9
$ git checkout master
Switch to branch 'master'
 
$ git checkout -b STASHDEV-1234-fix-capitalisation-of-Stash
Switched to a new branch 'STASHDEV-1234-fix-capitalisation-of-Stash'
 
.. change capitalization of one word in a template ..
 
$ git commit -m "STASHDEV-1234: Totally trivial change" && git push --set-upstream origin STASHDEV-1234-fix-capitalisation-of-Stash

Then a few minutes later.. *BOOM*

broken builds

What the heck? How did changing the capitalization of a letter break the database migration tests?

Hmm.. maybe they’re flakey. Let’s re-run the build. *BOOM*

more broken builds

Arggh!

Oh wait. Of course. I know what heinous crime I’ve committed!

1
2
$ git checkout master
$ git checkout -b STASHDEV-1234-fix-capitalisation-of-Stash

Little did I know, one of my co-workers had broken master at some point in the past. My crime: branching from their broken commit. A simple merge from master at this point (providing it’s green) will remedy the situation, even if it does make the history a bit uglier.

Wouldn’t it be nice if we could avoid this whole situation altogether?

The Solution

What if git warned you when you switched to a dodgy ref? Something like:

1
2
3
4
5
$ git checkout master
master is lookin' good! c4f3b4b has 4 green builds.
 
$ git checkout stable-2.3
DANGER! stable-2.3 is busted. e1324fa has 2 red builds.

Then you could easily switch back to a better ref for your branch point, or slap the build breaker until they fix it.

I’ve built a little client-side git hook that does exactly that. It will work work for any git repository built by Bamboo or hosted by Stash, and is super easy to install.

Just run this command from the root of your local clone:

1
sh <(curl -s https://bitbucket.org/tpettersen/post-checkout-build-status/raw/master/install.sh)

You’ll be prompted for the url and credentials of either:

  • the Bamboo server that builds your repository; or
  • the Stash server that hosts your repository (if you’ve set up your CI server to notify Stash of build results).
1
2
3
4
5
6
7
$ sh <(curl -s https://bitbucket.org/tpettersen/post-checkout-build-status/raw/master/install.sh)
Retrieve build status from (S)tash or (B)amboo? B
Bamboo URL: bamboo.atlassian.com
Username: tpettersen
Password:
Installing hook...
post-update hook installed. Config written to .git/hooks/bamboo-config.yml

It works by registering a post-checkout hook that hits a little known REST end-point in Bamboo that provides all build results for a particular SHA.

The project is up on Bitbucket and I’m very open to pull requests.