All posts tagged 's2008'

VisualHG does a great job of integrating Mercurial DVCS into Visual Studio.  One feature that is lacking is the ability to create an initial repo from within Visual Studio. Fortunately, this is an easy problem to solve with a command file and the External Tools menu.

Create The Initialization Command File

The first thing I did was create a windows command file that does the following:

  • Initializes a Mercurial Repo in the directory passed in as a parameter
  • Copies my standard .hgignore file into the repository directory
  • Adds all of the current files into the repository
  • Performs an initial commit of those files

The complete command file is shown in Listing 1.

@echo %1
hg init %1
copy D:\Projects\HGSetup\.hgignore %1
hg add -R %1
hg commit -R %1 -m "Initial Commit"

Listing 1

If you’re a bit rusty with command files (I know I was when I started this exercise), every parameter passed into the file gets assigned a number. %1 is the first, %2 the second, etc.  This file requires the solution directory to be passed in as an argument.  The @echo outputs the value of the parameter, and the pause statements halt execution until a key gets pressed in the console window.

I keep my standard .hgignore file (as well as these command files) in the “D:\Projects\HGSetup” directory, so you will want to update the “copy” to the path for your standard .hgignore file. 

Wire Up Visual Studio to Execute The Command File

The last thing that needs to be setup is the external command in Visual Studio.  Select “Tools” (from the main menu) then “External Tools…” and you will be presented with the configuration dialog as in Figure 1.  I have already filled mine out, but the process is quite simple.  Click “Add”, enter the Title, and then wire up the command file to the External Tool that you just created.

NOTE: List like programming in Windows Forms, by using an ampersand in the title, you enable the following letter to be a hot key in the menu

Figure 1

The “Command:” (see A in the Figure) is the path and file name of the command file that you created earlier.  In the the Arguments box (see B in the Figure), add “$(SolutionDir)” (without the quotes).  The items contained in the Arguments text box wil lbe passed into the command defined with “Command”.  $(SolutionDir) is a macro that refers to the physical location of the currently open solution in Visual Studio.

NOTE: There are a lot more macros available, and allow for some very creative ways to interact with external programs. To see the full list, click on the arrow button to the right of the Arguments text box.

Lastly, enter the initial directory. In this example, I set it to my storage area for my .hgignore file.  You can also use macros in the Initial Directory text box, but for this function, it wasn’t necessary.

Creating a Repository and Adding The Solution Files

To test it, create a new project in Visual Studio (or open on up that isn’t already under source code control), click on Tools, and the Title you selected (or press the hotkey if you wired one up).  If all goes well, your solution will now be in a Mercurial repository, and VisualHG should pick up that fact and show the visual indicators as part of VisualHG’s excellent integration with Visual Studio.

As you work with you code, VisualHG’s functionality is perfect for the day to day operations of working with Source Code Control.  It just needed a little nudge to get the initial repository created.  And yes, I could have used the command line or TortoiseHg workbench, but the point is that I didn’t want to leave Visual Studio.

Happy Coding!

Managed Windows Shared Hosting by OrcsWeb