Bug Hunting with Revision Control using Mercurial

My impression through reading other software developers' blogs is that, when presented a choice of distributed revision control, there seem to be a majority preference for Git over Mercurial. This popularity does not come as surprising, since it is the brainchild of the most prominent icon of open source software today, the famed Linux kernel hacker Linus Torvalds.

It feels like the Mercurial camp is a little bit unrepresented, but today’s post isn’t about a blow-by-blow account between the two software revision control systems; you will probably be more informed by experts out there who have better qualifications in critiquing the pros and cons among the two. More importantly, I feel these differences are mostly nitpicking to general laypeople - when the demands of our projects do not scale to a level where the performance differences are visible, it really doesn’t matter.

People will have their biases, and the git-vs-mercurial battle will probably be not too different from the perennial ideological battles between the emacs-vs-vim crowds. And obviously, you should have detected my bias in this case already :)

Anyway, back to our original discussion.

I have only used git briefly, and I’ll not comment much on it given my limited impression. I’d agree with most people that it is a remarkable software revision control tool and leave it as that. It must strike you as curious to why I am using Mercurial then - it’s largely of based on pragmatism, given I’ve acquired much familiarity through constant use at work. Besides, if I haven’t been complaining enough to be tempted to try out git again, it must have not been that bad, right?

I’ll share with you the best thing I love about using these revision control tools, namely, for their amazing ability to find out where the sources of your bugs are. It’s not uncommon for software to break between the revisions you’ve check in; and sometimes these breakages fail to be noticed until you’ve checked it in after a bunch of some other code, making your original error even harder to find.

Well, not anymore.

Mercurial has this ability called ‘bisect’ which allows you to toggle between revisions until you’re able to zone down to the exact patch where your code first breaks. Given that most code check-ins tend to be of small increments, knowing which patch that causes the break helps you narrow down the error to a very small subset where you can focus your efforts on finding the bug.

How does this work? Let me show you a real life session of how bisect works in finding an erroneous commit:

[email protected]:~/replicode$hg bisect -g 1614 [email protected]:~/replicode$ hg bisect -b 1671
Testing changeset 1666:ee86f6717c42 (57 changesets remaining, ~5 tests)
36 files updated, 0 files merged, 8 files removed, 0 files unresolved
[email protected]:~/replicode$hg bisect -b 1666 Testing changeset 1630:6e221cda7176 (28 changesets remaining, ~4 tests) 28 files updated, 0 files merged, 2 files removed, 0 files unresolved [email protected]:~/replicode$ hg bisect -b 1630
Testing changeset 1623:f7b12d17a79b (14 changesets remaining, ~3 tests)
10 files updated, 0 files merged, 0 files removed, 0 files unresolved
[email protected]:~/replicode$hg bisect -b 1623 Testing changeset 1618:ac9135ec8e99 (9 changesets remaining, ~3 tests) 17 files updated, 0 files merged, 0 files removed, 0 files unresolved [email protected]:~/replicode$ hg bisect -g 1618
Testing changeset 1620:032f83fb6b8c (5 changesets remaining, ~2 tests)
26 files updated, 0 files merged, 1 files removed, 0 files unresolved
[email protected]:~/replicode$hg bisect -g 1620 Testing changeset 1621:1d8191199d0d (3 changesets remaining, ~1 tests) 5 files updated, 0 files merged, 0 files removed, 0 files unresolved [email protected]:~/replicode$ hg bisect -g 1621
Testing changeset 1622:65b4f19e8941 (2 changesets remaining, ~1 tests)
9 files updated, 0 files merged, 0 files removed, 0 files unresolved
[email protected]:~/replicode\$ hg bisect -g 1622
changeset:   1623:f7b12d17a79b
parent:      1618:ac9135ec8e99
parent:      1622:65b4f19e8941
user:        Anonymous Person <[email protected]>
date:        Fri Aug 07 14:13:56 2009 +0100
summary:     Automated merge with http://xxx


Some of the output has been slightly modified to protect the innocent[1].

The first two bisect commands, with the -g and -b arguments indicate the two changesets that you know for certain which are good and bad respectively. With the boundaries set, Mercurial goes on its way and starts checking out changesets that are committed in between.

For each checked out changeset, you’ll have the opportunity of testing it out; once you are done, you can indicate whether if the changeset is good or bad by using the same commands with the checked out changeset number. Mercurial then checks out the next changeset for you to test.

Here’s where there is some serious voodoo at work; notice that I had 57 changesets in between. It’s going to be a nightmare if I had to test out every single one of them - instead, Mercurial tries to subdivide through the changeset smartly to isolate the problem (hence the term ‘bisect’) and gives you a general estimate of how many more tests you have to do before Mercurial can definitively point out the problematic changeset. In my case, it took me 8 tries. Pretty impressive eh?

I certainly have my few criticisms about Mercurial, like my initial incredulity of how the developers came up with the command of hg when their software system is called ‘Mercurial’; but past those kinks and initial gag-reflexes, I have to admit that my experiences using has largely remained satisfactory. And if you’re one of those who are still at two minds about using a distributed revision control system, I urge you to give it a try. You just might like it!

$1$ It's not nice to put the names of the people I work with on my blog without their consent.