March 26, 2011

How to Chart your Mercurial Commits with the Activity Extension in Windows

How to Chart your Mercurial Commits with the Activity Extension in Windows

The Mercurial Activity extension lets you measure the number of commits done to a Mercurial repository per day and chart it out using Matplotlib.

The installation process was a bit of a doozy on my Windows system, so I wrote this step-by-step guide. The extension uses the Matplotlib library for plotting the chart, so you will need to have that set up inside your Python distribution. The default Python installation I had on my system was v2.7 64-bit, and the default Mercurial client I was using was TortoiseHg (with Mercurial 1.7). However after some tinkering around, I found out that I couldn’t get the extension to work with this sort of setup – mostly because TortoiseHg comes with its own Python distribution in some DLL file inside a library.zip archive and I have no idea how to install a Matplotlib site-package in there.

So this is what worked for me:

  • Get Python v2.6 32-bit (I found this to be the lowest common denominator which would work with all of Numpy (a pre-requisite for Matplotlib), Matplotlib, Mercurial and the Activity extension). For all remaining steps, make sure you are using this distribution of Python and not your default one (if it is different than this one).
  • Download NumPy and install it inside Python 2.6. I ended up getting the latest stable release: NumPy v1.5.1, win32 for python2.6. The installer is an exe file and you can simply run it to correctly install inside your Python 2.6 distribution. Check that NumPy is installed by running the Python 2.6 interpretter and typing this:

import numpy
print numpy.__version__

  • Download Matplotlib and install it inside Python 2.6. I ended up getting v1.0.0, win32 for python2.6. After installation, check from the Python 2.6 interpretter that its correctly installed

import matplotlib
print matplotlib.__version__

  • Download Mercurial for your Python distribution (even if you already have TortoiseHg installed). This is because we need to install Mercurial inside our Python 2.6 distribution whereas TortoiseHg comes with its own custom Python distribution. I ended up getting Mercurial 1.8.1 for Python 2.6 and x86 Windows. After installation, make sure that you can import it from Python 2.6. Also, you should be able to run ‘hg’ from the ‘Scripts’ folder inside your Python2.6 install (default location: C:Python26Scriptshg)

from mercurial import __version__
help(mercurial.__version__)

  • Finally, download the Activity extension itself. I got v1.4. This will unzip to a bunch of python scripts and a README.txt file. As the README instructs, edit your global mercurial settings file and put the following line under the [extensions] heading:

[extensions]
activity = C:\Programs\hgactivity-1.4\activity.py

Of course, set the actual path to where ever you extracted activity.py. If you have TortoiseHg installed, you can gain access to the global mercurial settings file by right-clicking in Windows Explorer, selecting TortoiseHg->Global Settings, and press the ‘Edit File’ button.

  • Now you are ready to actually use the extension. You will have to use it from a command prompt. cd to the root of a mercurial repository and issue the following command:

C:\Python26\Scripts\hg activity

This will create an activity.png file in the current directory. To see all other options available, run ‘hg activity help’.

Finally, I’d like to thanks the authors of this extension for making it available open source. You can modify activity.py to extract different types of data or present it in different ways. In the version of the script I downloaded, the Y-axis labels were switched off by default. You can fix this by editing the line that says

ax.yaxis.set_visible(False)

For other type of edits you’ll probably want to brush up with Python’s datetime class. Have fun! :)

Leave a Reply