Using Mercurial with Subversion

If you’re a user of distributed source control systems such as Git and Mercurial then you may find it painful having to work with subversion (either at work or for an open source project). Recently I started playing with the excellent hgsubversion extension which allows you to use mercurial as a subversion client.

Assuming you already have Mercurial installed (or TortoiseHg) then you can easily grab the hgsubversion extension by grabbing its source from bitbucket:

cd c:Projects
hg clone http://bitbucket.org/durin42/hgsubversion

Here I’ve simply cloned hgsubversion in to my C:Projects directory.

Next, edit your Mercurial.ini file (in your home directory) and enable both the rebase and svn extensions. Note that rebase is distributed with Mercurial so you don’t need to specify a path:

[extensions]
rebase=
svn=c:projectshgsubversionhgsubversion

Now you can clone a subversion repository locally:

hg clone svn+https://path/to/someproject/svn/trunk someproject-hg

Here I’m cloning a subversion repository to the local “someproject-hg” directory. Note the svn+https in the URL.

If you want to generate a .hgignore file based on the svn:ignore properties in the remote repository then you can issue the following command:

hg svn genignore

At this point I can interact with someproject-hg as with any other mercurial repository – I can edit files and make local commits. When I’m ready to push the changes back to the subversion server I can issue a “hg push” just as if I were pushing to a remote mercurial repository.

Likewise, if I want to retrieve new changesets from svn then I can issue a “hg pull” as normal. When you pull the changes, this will create a branch in the local repository’s history. At this point, rather than performing a “hg merge” (as you would with a normal mercurial repository) it is necessary to rebase your changes to ensure that you keep a linear history. To perform a rebase, you can issue the command:

hg rebase --svn

If you’re in a situation where you have an existing Subversion repository and you’re not yet willing to fully commit to using a DVCS, hgsubversion is a great way to let committers work with mercurial locally without needing to make any changes to your existing server.

For more information on hgsubversion, be sure to check out the documentation here and here.

Written on May 13, 2010