by Wojciech Sura

Configuring perforce for use with Visual Studio

My favorite (and actually quite popular) diff/merge tool is Perforce’s p4merge. I like its clean interface and advanced comparison algorithms, which perform quite well even when faced with complicated modifications.

Perforce-compare Perforce-merge

There is a way to integrate Perforce with Visual Studio, but this operation is a little bit complicated due to specific Perforce requirements for merged files. Let’s do that step by step.

First of all, create two batch files in Perforce directory:


START /WAIT /D "C:\Program Files\Perforce\" p4merge.exe -nl ""%6"" -nr ""%7"" ""%1"" ""%2""


COPY /Y NUL ""%4""
START /WAIT /D "C:\Program Files\Perforce\" p4merge.exe -nb ""%8"" -nl ""%6"" -nr ""%7"" -nm ""%9"" ""%3"" ""%1"" ""%2"" ""%4""

Then open Visual Studio’s configuration window and navigate to source control user tools:


Now add two tools. First for comparing:

Extension: .*
Operation: Compare
Command: C:\Program Files\Perforce\p4diff.bat
Arguments: %1 %2 "0" "0" "0" %6 %7

And second for merging:

Extension: .*
Operation: Merge
Command: C:\Program Files\Perforce\p4merge.bat
Arguments: %1 %2 %3 %4 "0" %6 %7 %8 %9

The reason for additional batch files is that Perforce expects, that the result of merging already exists, but on the other hand Visual Studio expects the merge tool to create this file.

If you use Microsoft Git Provider, to configure Perforce as a diff tool, run git bash in your repository and execute:

$ git config –local difftool.perforce.cmd ‘"C:\Program Files\Perforce\p4merge.exe" "$LOCAL" "$REMOTE"’
$ git config –local diff.tool perforce