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 p4merge
p4diff*
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 p4diff
Configure 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!


17 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?
Awesome! Worked first time with those instructions. What a joy after using the default diff tool!
Thanks! My life will be easier from today on…
Hello Andy,
Please allow me to post here as a follow up to the first result when googling for Git + p4merge. I also thought my observations would spare hairy moments to people landing n your page.
Time has passed since you wrote your blog post. Now Git recognizes p4merge and will no more use the value of mergetool.p4merge.cmd.
The only configuration option Git is going to use is mergetool.p4merge.path eventually.
Also since you posted, Git has gained git-difftool and will do the right thing ™ when the diff tool (or the mergetool) is p4merge. As a consequence, you don’t need your p4diff script anymore. Just remove the
external = p4diffconfiguration section and delete your existing p4diff script.Finally, here is my p4merge script that takes care of converting relative paths to absolute paths in order to benefit from the launchp4merge shim.
#!/bin/bash for arg; do [[ $arg = /* ]] || arg=$PWD/$arg; absargs+=("$arg"); done; /Applications/p4merge.app/Contents/Resources/launchp4merge "${absargs[@]}"Put it in
/usr/local/binin place of the previous one.For Windows users who landed here, you’ll notice p4merge.exe doesn’t handle NUL device properly. Here is a p4merge script you have to put in your
C:\Program Files(x86)\Git\binfolder (MSYS Git).#!/bin/bash NULL=$TMP/$$.NULL for arg; do [[ $arg = /dev/null ]] && arg=$NULL && echo "" >"$arg"; args+=("$arg"); done; /c/Program\ Files/Perforce/p4merge "${args[@]}" [[ -f $NULL ]] && rm $NULLHope that helps.
Hum looks like the formatting broke the script display, anyway here are two links to those files:
p4merge script for mac: https://gist.github.com/722266
p4merge script for windows: https://gist.github.com/722271
Great article! And even better comments. Just a quick typo fix:
keepBackup = false;Should probably instead be:
keepBackup = falseThe semi-colon seems to be invalid.
Thanks for this – I keep referring back to this post.
As of 5/23/11 newest version of p4merge for OSX is 10.2, available here:
http://ftp.perforce.com/perforce/r10.2/bin.macosx105u/
As of August 2011, Gregory’s p4merge script works perfectly for me. The only entries in my .gitconfig file that have anything to do with diff or merge are
[merge]
tool = p4merge
[mergetool]
prompt = false
and so far, p4merge works vastly better for me than opendiff. The command ‘git diff’ uses the terminal window, but ‘git difftool’ uses P4Merge. A nice choice to have, both have their uses.
In particular, Unicode characters display without the hack required for opendiff. The files open one at a time without the prompt, and close with command-W, leaving P4Merge running. No temporary files are left behind.
I repeat, if you are tempted to use any diff or merge related lines in .gitconfig not shown here, prove to yourself that you need them, then please post a comment for all of us explaining why they were necessary.
Don’t play Simon Says with anything else found on the web, as Gregory notes it is probably out of date.
(Like most code-related apps, P4Merge looks much better using the Lucida Console font, at a larger font size.)
This was very helpful, cheers.
One trick that I do in my version of your diff script is to copy the files into /tmp and append the revision hash to their filenames, then call p4merge to diff the copies. It makes it slightly easier to work out which revision is which if you’re diffing between two arbitrary points in the history.
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 [...]