Integrating Perforce with Xcode 4

I prefer to work with first rate tools. You might call it snobbery, but I’m just too old for spending my precious time on fiddling with inferior products. That’s why I’m using Mac OS X and even more the reason I chose Perforce as our source control system. It simply blows the competition away.

When Apple introduced Xcode 4, I had realized with horror that they removed Perforce support. This was simply unacceptable for me. I would give up on Xcode much easier than giving up one of the cornerstones of our business. So I waited patiently, filed bug reports with Apple, until the buggy-to-the-level-of-unusability Xcode 4 was released. I decided to stick with Xcode 3 for the foreseeable future (until of course Apple dropped the tools and I had to migrate).

I did not want much from the Xcode side – just let me check out the files when I start working on them. I usually handle all the other stuff (like diffing, code review, etc) from P4V. Then while walking through all the Xcode 4 options and menu items (to familiarize myself with the inevitable new tool) I found something on the Behaviors preference pane.

You can execute a shell script every time Xcode unlocks a file. Fortunately Perforce locks all the files that are under source control and not in the checked out state – so you can be sure that all your files will be locked when not checked out. This fits perfectly with the Unlock file behavior. When you start working on a file Xcode wants to unlock it and executes your script. The script could then check out the given file from Perforce. The checkout will unlock the file, so everybody will be happy.

My xcode4edit.sh script is pretty straightforward:

#!/bin/sh
`cat ~/.profile | grep P4PORT`
`cat ~/.profile | grep P4CHARSET`
`cat ~/.profile | grep P4USER`
`cat ~/.profile | grep P4CLIENTPATH`
/usr/local/bin/p4 edit ${1#file://localhost}

This is checked in into the root of the depot so every team member have access to it. My .profile (as well as other members of the group) contains the actual configuration parameters:

export P4PORT=<server>:1666
export P4USER=<user>
export P4CHARSET=utf8
export P4CLIENTPATH=/Volumes/Data/Workspace

Of course you can add additional configuration variables to the script and into .profile if you want.

Output from the p4 edit command will go into the Console, so you can use that for debugging any problems you face.