Click here to Skip to main content
15,885,216 members
Articles / DevOps / TFS

Visual Studio and vsDiffMerge with Mercurial and TortoiseHg

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
2 Sep 2014CPOL3 min read 12.1K   2   1
Typically, when I have worked within Visual Studio on source-controlled projects I have used Team Foundation Server (TFS). With my current employer, we use Mercurial, and we use TortoiseHg to interface with it.

Typically, when I have worked within Visual Studio on source-controlled projects I have used Team Foundation Server (TFS). With my current employer, we use Mercurial, and we use TortoiseHg to interface with it. For me this seemed to be a disconnected experience and I have been looking for ways to deeply integrate it. To that end, I have been using VisualHg as my source code control provider in Visual Studio, and recently found a way to use Visual Studio as my Diff and Merge tool from TortoiseHg. I have detailed the process of setting this up below, and authored an installer, which automates these steps. I will not cover the steps for setting up, nor using VisualHg as its own site does a good job of that already.

What you will need

Verify a few things first

  • You should have a file named vsDiffMerge.exe in the Visual Studio IDE directory, typically “C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\vsDiffMerge.exe”.
  • You should have a file named Mercurial.ini in your user directory, typically “C:\Users\%USERNAME%” or “%USERPROFILE%”.
  • TortoiseHg is NOT currently running. If you cannot shut down TortoiseHg, that is fine but will require a restart at the end.

If we have everything we need, and have verified the few things mentioned above, we are ready to proceed. We will do most of the work in Mercurial.ini, and a new batch file that we will create. The batch file will handle calling the Visual Studio components and give us a simple path to configure in Mercurial.ini. Therefore, we will start there.

Wire it up

Open the text editor and create a new file. Copy the batch script code below into the new file and then save the new file as HgVsDiffMerge.bat in your user directory (%USERPROFILE%).

@ECHO OFF
IF "%1" == "/Diff" GOTO DIFF
IF "%1" == "/Merge" GOTO MERGE
EXIT

:DIFF
%COMSPEC% /C ""%VS120COMNTOOLS%VsDevCmd.bat" && devenv.exe /diff %2 %3"
EXIT

:MERGE
%COMSPEC% /C ""%VS120COMNTOOLS%VsDevCmd.bat" && VsDiffMerge.exe /t /m %2 %3 %4 %5
EXIT

If you are using Visual Studio 2012, then change the variable “VS120COMNTOOLS” to “VS110COMNTOOLS”, or if you are using Visual Studio 2010 then change the variable “VS120COMNTOOLS” to “VS100COMNTOOLS”.

Now, make a backup copy of your Mercurial.ini file. If you just read that and ignored it, please stop and go make that backup copy of Mercurial.ini now.

Now open Mercurial.ini in your text editor. We will focus on a couple of sections in here. The first section (which may not exist) is the “merge-tools” section. If you do not have a “merge-tools” section, you will create one. Here we tell Mercurial, and TortoiseHg, about the batch script we created earlier. The section should look like the snippet below.

[merge-tools]
vsDiffMerge.executable=%UserProfile%\HgVsDiffMerge.bat
vsDiffMerge.args=/Merge $local $other $base $output
vsDiffMerge.priority=1
vsDiffMerge.premerge=True
vsDiffMerge.gui=True
vsDiffMerge.diffargs=/Diff $parent $child
vsDiffMerge.dirdiff=True

Now Mercurial and TortoiseHg know that there is a merge tool called vsDiffMerge. They know where to find it, and how to invoke it for a merge or diff. Now we will tell them to do so. In the “ui” section, set the value of “merge” to “vsDiffMerge”. In the “tortoisehg” section, set the value of “vdiff” to “vsDiffMerge”.

Save the Mercurial.ini file.

If TortoiseHg has been running this whole time, shut it down. Now launch TortoiseHg and select a change-set, right click any non-binary file in the change-set and select “Diff to parent”. If all went well, you should be looking at Visual Studio comparing your two file versions. You will also notice that now you have some of the benefits like hovering to see variable types and other useful diff and merge tools. This will be similar for merges, but will have an additional pane below those two, showing the result of the merge that would be committed.

In a Diff, the file on the left will be the parent and the file on the right will be the local copy. In a merge, the order is; local on the left, remote on the right, merged on the bottom.

Here’s a screen shot of a simple Diff.

Puctire of vsDiffMerge in Diff to parent.

vsDiffMerge – Diff to parent.

You can also download the installer I authored which automates these tasks. It is available here as HgVsDiffMergeSetup.zip (zipped MSI).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PLAYSTUDIOS
United States United States
A seasoned developer, architect, and leader with more than 13 years of experience with organizations in finance, telecom, legal, construction and COTS software. Proven ability to build, lead and mentor development teams, analyze issues from multiple perspectives and to conceive, design and implement strategic solutions. Microsoft certified with strong understanding of object oriented design and development with extensive expertise in .Net technology. Passionate problem solver with infectious excitement for technical business solutions. Technical search expertise with Autonomy IDOL Products (IDOL, DiH, DAH, etc.) and Microsoft FAST products (ESP, F4SP). Well versed in solution architecture and design, systems integration, speech recognition, financial procedures, telephony, binary formats, memory management and performance. Special Expertise in .Net Framework versions 1 through 4.5 using primarily C# (Windows and Web) with focuses on Windows Communication Foundation (WCF), Model View Controllers (MVC), Workflow, REST Services, and GDI components. Additional technical knowledge including SQL Server, SharePoint, IIS, HTML, DHTML, XHTML, Java and JavaScript, Visual Basic and VBScript, InstallShield, and Windows Installer XML (WiX).

See more about me, my skills and experience on LinkedIn.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mateusz Stępniak5-Aug-16 1:51
Mateusz Stępniak5-Aug-16 1:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.