How to use Mac OS X FileMerge with Perforce

Install the corrent version of Xcode and the p4 command line tool, then save the following shell script as /usr/local/bin/p4diff :

#!/bin/sh
# This makes FileMerge work with Perforce (unlike opendiff, which forks and doesn't wait).
# Note: We save the merged file in the checked-out copy (same as -right)
/Applications/Xcode.app/Contents/Applications/FileMerge.app/Contents/MacOS/FileMerge -left $1 -right $2 -merge $2

Then save the following shell script as /usr/local/bin/p4merge :

#!/bin/sh
# This makes FileMerge work with Perforce for P4MERGE (unlike opendiff, which forks and doesn't wait).
# p4 docs say the call is P4MERGE base theirs yours merge
# We give that to FileMerge as -left theirs -right yours -ancestor base -merge merge
/Applications/Xcode.app/Contents/Applications/FileMerge.app/Contents/MacOS/FileMerge -left $2 -right $3 -ancestor $1 -merge $4

Make both scripts executable with chmod +x /usr/local/bin/p4* , then set the environment variables P4DIFF=/usr/local/bin/p4diff and P4MERGE=/usr/local/bin/p4merge to let Perforce see the scripts.


These instructions are for OSX 10.7 Lion with Xcode 4.3.2 or later. The scripts are exactly the same, except that the path to FileMerge may be different: