Using P4Merge with Visual Studio 2008 and TFS

The merge and diff utility included with Visual Studio 2008 is, let’s face it, not the best tool on the market.  Sure, it does the job, but it could be so much better.  Thankfully, Visual Studio gives you a way to customise the tool that is used for merging.

Now, in the past, I’ve usually gone for WinMerge as my tool of choice to replace the out of the box offering, but WinMerge is starting to show it’s age and whilst it does a great job at comapres it doesn’t do 3-way merges, so I’d rather use something else.  By the way, I keep WinMerge installed because of it’s great directory comparison feature.  Oh, for those who don’t know, a 3-way merge is a merge that shows both your current version of the file, the file you’re merging with, and the root file from which the two other files are derived (i.e. the original).

Many people talk about how great the BeyondCompare product is, but for the sake of my wallet I’d rather use a tool that was free.  Enter Perforce’s P4Merge.

The only hassle with using this tool is that it doesn’t integrate that easily into Visual Studio as a merrge tool – you can’t just call the executable and supply arguments, you have to work around it a little.  Here’s how:

As a Comparison Tool

Setting P4Merge up as your comparison tool is simple enough.  Go to Tools->Options and navigate to Source Control and Visual Studio Team Foundation Server.  Select the Configure User Tools… option.

image

Then choose Add and set the options as shown:

image

Hit OK, and that one’s done.  Note you may have to supply a different path to the P4Merge program if you installed it in a different location.

Merge Tool

For merging things get a little tricker because P4Merge wants the output file for the merge operation to exist before you actually do the merge.  This is where the VS integration presents a small problem, since VS doesn’t create the result file before calling the tool.  It expects the tool to create it instead.

To work around this we need to create a simple batch file that we will get Visual Studio to call.  For the sake of sharing across multiple users on my machine, I’m placing the file in C:\Users\Public\Documents\p4merge.bat.

The contents of the file should be:

@ECHO OFF
COPY /Y NUL %4
START /WAIT /D "C:\Program Files\Perforce" p4merge.exe %1 %2 %3 %4

Once you’ve created that file, go through the same steps as above for Compare, but this time select the Merge operation and point to the batch file instead of the P4Merge program.  The end result should be something like this:

image

Note that the arguments are passed in a non-standard order. It’s %3 %1 %2 %4

Once this is done, get out of the options and try it out.  Here’s the tool in action

Comparison:

image

Merging:

image

Enjoy!

 

Reader Comments

This is great I could never figure out how to get the merge working. Granted I only spent about 5 minutes trying but this is great.

Yup, I'm a big fan of p4merge. The only thing I do differently is I swap the parameter positions in the batch file so that when I help a coworker set it up, they can just put in %1 %2 %3 %4 and the batch file handles the rest.

Just stumbled on your post and got excited because I could finally replace the diffmerge.exe tool of Visual Studio.
After using p4merge for several merges I have uninstalled it. I have been merging SQL files and p4merge fails to recognize when you have blocks of sql with the same end line (for example:
');
).
When you try to merge two sql blocks, one written localy and on received from server it only offers to merge the same lines, it does not offer to leave them as is. So if I have on server the following:

29: execute immediate '
30: blah blah
31: ');


and I write:

29: execute immediate '
30: some other blah
31: ');

It always merges the last line, it does not offer to leave two copies of them. Very frustrating.

@Dalibor I'd actually expect the merge tool to act that way. Maybe have a look at using BeyondCompare or another 3-way merge tool as an alternative as see if they do what you want them to.

By the way, in case you hadn't noticed, you can always directly edit the merged results at the bottom of the screen to get the merge just as you want it.

Post a Comment



Welcome

As you might have guessed, this is a tech blog. I normally write about development and things that can help us do it better, specifically in the .NET space, and occasionally i'll throw in a post about whatever happens to be in my head at the time. Enjoy your visit!
And in case you were wondering, I'm a Principal Consultant for Readify, I run the Sydney Alt.Net group and I play a bit of LOTRO in my spare time (feel free to look me up)


 

My Open Source Projects

Labels

Blog Archive