Git bisect might save your day
The day maven ruined my day (again…)
Yesterday, I lost 2 hours because our Maven project at Algodeal wouldn’t build anymore. I couldn’t execute:
mvn eclipse:eclipse
nor run the full build.
With the help of a colleague, we found out that only a two steps build would do the trick:
mvn clean install -DskipTests;mvn eclipse:eclipse
Obviously maven is already not my best friend but yesterday I was just fed up.
How git is called to the rescue
Luckily I’ve got one true friend: Git and it’s little brother git bisect.
The question I asked git:
What did we do that broke the build?
The answer git bisect gave:
How stupid you are, you added a plugin in your pom.xml to enforce the version of maven being used. Looks like since you’ve done that, generating eclipse projects with maven is not possible anymore.
Let me replay that in slow motion.
Git bisect is a handy tool that let’s you find a broken commit by bisecting between a well known good commit and a well known bad commit. Instead of trying all the N commits backward from bad to good, you only need to test log(N) revisions being nearer and nearer of the first broken commit.
Let’s apply this to our broken build
My well known bad commit is current version. Git calls it HEAD. A well known good commit is version 1.1.3 we released two weeks ago. I can remember that for our latest public release, everything worked ok.
To prove it:
git reset --hard VERSION1.1.3
mvn eclipse:eclipse
Works ok!
So I can start git bisect with this command:
git bisect start HEAD VERSION1.1.3
No all I need to do is run maven and tell git if the build id broken or not using git bisect good or git bisect bad. This will help git find out in which direction to search for the broken build.
Handy isn’t it? Instead of testing 282 revisions, I could do it in about 8 steps, git told me.
Here comes the laziest part
But you known what? I’m super lazy and would like git to run these 8 steps himself.
Yes we can! says git.
Use git run <script>
and git bisect will run fully automatically and tell you after a few seconds which revision broke your build!
cat ../maveneclipse.sh
#!/bin/sh
mvn eclipse:eclipse > /dev/null
git bisect start HEAD VERSION1.1.3
git bisect run ../maveneclipse.sh
running ../maveneclipse.sh
Bisecting: 140 revisions left to test after this (roughly 7 steps)
[49be0b426f3469b154d66179ecdbaad2128b872e] Now formats Javadocs
running ../maveneclipse.sh
Bisecting: 70 revisions left to test after this (roughly 6 steps)
[18a0eddab7a745c9cec538b9184efb25499e06c1] More optimisations
running ../maveneclipse.sh
Bisecting: 37 revisions left to test after this (roughly 5 steps)
[051bccc6252b23be6c9074545986cd057bcc69d8] Merge branch 'master'
running ../maveneclipse.sh
Bisecting: 15 revisions left to test after this (roughly 4 steps)
[1183351cf1976571138c80dcf70e702dc3575177] Merge branch 'master'
running ../maveneclipse.sh
Bisecting: 7 revisions left to test after this (roughly 3 steps)
[a586dcbddca57d93e00ed7ebaeb02239bfdc515c] Merge branch 'master'
running ../maveneclipse.sh
Bisecting: 3 revisions left to test after this (roughly 2 steps)
[b4938d0a2aacabb4e71c2c08cab5e63ce12efbce] Merge branch 'master'
running ../maveneclipse.sh
Bisecting: 1 revision left to test after this (roughly 1 step)
[6536952979f24786db146217b81f7d612bede425] Before we fix the build
running ../maveneclipse.sh
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[56750bb15630dbf1af1976ad0e7161c7ed696e69] Force Maven3
running ../maveneclipse.sh
56750bb15630dbf1af1976ad0e7161c7ed696e69 is the first bad commit
commit 56750bb15630dbf1af1976ad0e7161c7ed696e69
Author: David Gageot <gageot@algodeal.com>
Date: Wed Mar 17 18:20:29 2010 +0100
Enforce Maven3
Remember that, this could save your day (at least your build).
By the way, I’ll be giving talks about git at Paris JUG in may, USI2010 in july and at ChtiJug in april if they find a sponsor. You want to be a sponsor right?