So, don’t get me wrong, I hate Perforce. It actually makes my head hurt a little remembering my experience with it. The only saving grace was the Perforce merge tool, p4merge. There are number of different diff/merge tools that will work with git on OS X, but I’m not all that impressed with them — even the rather expensive Changes left me feeling underwhelmed.
After figuring out how to configure git to use Changes as its merging and diff tool, I set out on a mission (read: Google search) to figure out how to use p4merge instead. It turns out it’s pretty simple, but that all the pages with instructions on how to do it no longer exist.
Here are the steps to get it working:
Download and Install P4V
Download the free Perforce Visual Client dmg from here. Once it’s downloaded, copy p4merge from the disk image to your /Applications directory.
Write some simple shell scripts
p4merge*
Create a new text file in /usr/local/bin called p4merge and add the following lines:
#!/bin/sh /Applications/p4merge.app/Contents/MacOS/p4merge $*
Make the script executable by entering this command:
chmod +x p4mergep4diff*
Create a new text file in /usr/local/bin called p4diff and add the following lines:
#!/bin/sh [ $# -eq 7 ] && /usr/local/bin/p4merge "$2" "$5"
Make the script executable by entering this command:
chmod +x p4diffConfigure Git to use the scripts
Open your git configuration file (probably ~/.gitconig) and add these lines:
[merge] keepBackup = false; tool = p4merge [mergetool "p4merge"] cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED" keepTemporaries = false trustExitCode = false keepBackup = false [diff] external = p4diff
Use it!
That’s it. Now when you run git-mergetool or git-diff the visual Perforce merge tool will launch with the files you want to merge or diff. Hope this helps!


10 Comments
I am definitely going to use this. Thanks for the steps!
You’re my hero, Mr. McIntosh
make sure that you change the && to an actual &&
and by && I mean & &
This doesn’t seem to work when there is only a LOCAL and REMOTE, but no BASE… p4merge gets hung up looking for the BASE, which does not exist.
Thanks a lot for the post, but
1)
#!/bin/sh
[ $# -eq 7 ] && /usr/local/bin/p4merge “$2″ “$5″
2)
cmd = p4merge “$BASE” “$LOCAL” “$REMOTE” “$MERGED”
But what does the above line do? Can i use the same thing for GIT on UNIX?
Someone please help
You can save yourself from having to create the script if you use the launchp4merge that comes bundled:
[mergetool "p4merge"]
cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge “$PWD/$BASE” “$PWD/$REMOTE” “$PWD/$LOCAL” “$PWD/$MERGED”
keepTemporaries = false
trustExitCode = false
keepBackup = false
With this, all you have to do is install the app.
when I use this, it will open a different window for each file in to diff. how do I open it all in 1 p4merge process? I figured this out for merging by googling around, but i’m not sure how to do this for diffing.
Short and sweet, thanks!
I have 3 questions regarding this if you don’t mind?
1) After I “Hit return to start merge resolution tool (p4merge)” it pops straight up with this:
2010-01-08 15:50:36.269 p4merge.real[58490:80b] Can’t open input server /Library/InputManagers/Inquisitor
Any idea what this is and whats wrong? the editor still starts and all seems well but things like this bug me :p
2) despite having trustExitCode set to false, it doesn’t ask me if the merge is complete like it would with say vimdiff?
3) i have used both sets of keepBackup settings (both set to false, in fact, everything is as above) but if still leaves pesky .orig files in my working directory… any ideas?
I have a feeling these are linked, could anyone shed some light? Also quite interested to see the authors answer to W. Andrew Loe III’s post?
One Trackback
[...] for merging. I am not a big Perforce fan but the free p4merge tool is really great. Check out the blog post by Andy McIntosh for a detailed explanation how to set it [...]