Wednesday, October 23, 2013

Visual Studio as a Diffing Tool

I have recently learned how to make use of Visual Studio 2012's built-in diffing tool. I'm happy to report that it does a reasonably decent job at this. While diffing, you have access to Visual Studio's superior syntax highlighting and intellisense capabilities. Also, it reuses existing windows, which can be handy if your target file is already open in your IDE. I have always used external diffing tools in the past, but there is a certain appeal to having an integrated tool. Just being able to diff and edit in a consistent color scheme is nice.

Sadly, Visual Studio does not support three-way merging or directory comparison, so I won't be putting KDiff3 down just yet. Also, unless you have set up a Team Foundation source control server, this capability is rather obscure and difficult to invoke. It involves some command line incantations:

devenv.exe /Diff Source Target [SourceName] [TargetName]

That isn't too bad, as command line arguments go. Still, I do not want to specify the complete path for every source and target file that I want to compare. Fortunately, it is quite possible to set up Visual Studio as a merge tool option for TortoiseHg. To do so, edit your global mercurial.ini file, and add this to your [merge-tools] section (go ahead and create that section if it does not exist):

[merge-tools]
vs.executable = ${ProgramFiles(x86)}/Microsoft Visual Studio 11.0/Common7/IDE/devenv.exe
vs.gui = True
vs.diffargs = /diff $parent $child "$plabel1" "$clabel"
vs.priority = 1

Now go up to your [tortoisehg] section. Create or edit your vdiff setting to point to your newly defined merge tool. It should look like this (along with whatever other items you have in that section):

[tortoisehg]
vdiff = vs

Restart TortoiseHg. Now, when you ask TortoiseHg to diff a single file for you, it should open that file in Visual Studio. If you ask for a three-way merge or a directory diff, TortoiseHg is smart enough to know that Visual Studio can't handle it, and will pick a different tool.