Using Mercurial, VisualHG and Visual Studio 2008 together

There is a strong desire in our group to move towards distributed version control systems specifically mercurial. As I spend most of my time working on the Chemistry Add-in for Word 2007 (Chem4Word) I have been using Visual Studio 2008. This doesn’t come with source control support out of the box – but you can (and we have) added on team foundation server support but this isn’t distributed and quite frankly sucked.

I have spent the last couple of days investigating other options and having got a good solution up and running thought I should share this with the world. This definitely isn’t meant to be an introduction to mercurial in general and I won’t be explaining all the terms (good intros are available from the mercurial website and elsewhere). I will be giving both command line and GUI instructions.

Installing VisualHG

The instructions on the VisualHG site are pretty good – but you only need to go through the first three. Basically you need to download and install TortoiseHg and then VisualHG.

Next make VisualHG the current active Source Control Plugin in Visual Studio 2008. <Tools> -> <Options> -> <Source Control>. Set the value of the drop-down list (Current Source Control plug-in) to <VisualHG>.

How to create a new repository and add it to source control

Open Visual Studio 2008 and create a new solution (in my case I went for a Console Application called cs-walkthrough under C:\projects\c# and I didn’t want a new directory for the solution)

In a command window change directory so you are in cs-walkthrough.

>hg init

this will create the .hg directory

create a file called .hgignore as a sibling of the .hg directory the contents of the file should be:

syntax: glob

*.csproj.user
obj/
bin/
*.ncb
*.suo
_ReSharper.*
*.resharper.user

Create a project on bitbucket with the same name as the solution (doesn’t have to be but will make things easier). So in this case I now have a publicly viewable repository on bitbucket http://bitbucket.org/jat45/cs-walkthrough.

Now create a file called hgrc under the .hg directory. This file should contain the following text:

[paths]
default = http://bitbucket.org/jat45/cs-walkthrough

[ui]
username = Joe Townsend

where jat45 should be replaced with your bitbucket id and cs-walkthrough should be replaced by the name of the bitbucket repository you just created.

From here on in you can do everything either from the command line or from within Visual Studio. I will give both commands (to access the VisualHG commands right-click on any of the files in the solution). All the command line commands should be run from the [cs-walkthrough] directory.

>hg status

hg status from the command line

or <HG status> from VisualHG

hg status from VisualHG

We want all these files under source control so either use

>hg add
or select all the files from the GUI and click Add then close the window

To commit these additions either use
>hg commit -m "initial commit"
or commit using <HG Commit>

Now push these changes to bitbucket

>hg push
or <HG Synchronize>(this will allow you to push, pull and see the incoming and outgoing changes).

Warning

Before committing or pushing changes you must make sure you build the solution first. This ensures that the [project].csproj file is correctly updated.

Getting an existing project

This is much easier than creating a new repository from scratch. Once again though we are back to the command line.

Change directory to the parent of where you would like the new project repository to be checked out to. In my case this is c:\projects\c#.

I am now going to get a clone of the cs-walkthough code from bitbucket but put it in a new location.

c:\projects\c#>hg clone http://bitbucket.org/jat45/cs-walkthrough my-new-source-dir

You can use the same command because the project is public so you can access the source code but not commit.

So I think that is it. You can do all the usual, push, pull, diff and so on either from the command line or from the VisualHG add-in. The one only thing that must be done from the command line is the initial clone or init. I am also investigating if everything works in VS2010 – so far no problems but if I run into any I will update you.

4 Responses to Using Mercurial, VisualHG and Visual Studio 2008 together

  1. Jesper Noehr says:

    Actually, the preferred way to set up your ~/.hgrc’s file username section is as follows:

    [ui]
    username = Firstname Lastname

    The username alone *will* work, but we find your account via your email address on Bitbucket as well. This is a better way to do it, as you’re not tying yourself into your username alone.

    • jat45 says:

      Thanks, I have updated the post to reflect this preference. I think that the [ui] = jat45 was coming from the VisualHG. It wasn’t clear what I should have been putting in.

  2. […] te brengen een goed artikel gevonden over de manier waarop dat het beste gedaan kan worden: Using Mercurial, VisualHG and Visual Studio 2008 together Reageren […]

  3. jedhunsaker says:

    Works great in VS2010.

Leave a comment