Before git was prevalent, my dayjob company used to use Perforce as the preferred source control system. Although many projects have moved to git, there are a number of systems still relies on Perforce, one of which is a packaging system that I needed to use.
So the system takes in source file from a Perforce repository then compiles and package this into an installable bundle. However our project maintains its revision control in git. Therefore we needed something that copies a subset of these source files into the Perforce repository and commits them.
Initially I did this manually for the first few rounds – running
rsync to copy the source files and committing from my machine. Unlike git, Perforce is a centralized source control system – pretty similar to Subversion, hence there needs to be a checkout/checkin steps for each file.
So here’s what you need to do if you want to automate Perforce checkins that takes in source files from another source control system. I actually have a bash script written for this but chances are your environment will be different enough from mine and render the script useless. Instead I’ll just outline the sequence of commands that you need to automate this replication.
Before you start, you’ll need to download Perforce command line client. There’s a version for Linux (Intel), Windows, and Mac OS X. Try downloading it and run it on the command line to make sure that it works.
To use Perforce’s command line client you’ll need to setup a couple of environment variables:
P4PORT– the fully qualified host name and port number of the Perforce server; something like
P4CLIENT– the client repository name, more on that later.
Then you’ll need to configure a client repository. With the
P4PORT environment variable initialized, create a folder somewhere and run “
p4 workspace” in there. In Linux, it’ll open
vi with a workspace configuration file. You’ll probably already have one configured in your development machine, you’ll can model it after that – consult your organization’s knowledge bases on how to configure this. One important detail is to note down (or customize) the client name in the configuration file – you’ll need this value to configure the
P4CLIENT environment variable later on.
Now that you have a client repository, setup the
P4CLIENT environment variable with that name. Then type “
p4 login” inside your workspace folder. You’ll be prompted for your Perforce password. When it says you’re logged in, type in “
p4 sync” to download the latest version of your source tree into your workspace.
With a perforce workspace at hand, you now can start copying files off your git repository into your Perforce repository – maybe even automate this in a shell script. These are the steps that you’ll need to do and what eventually you’ll need to program in your script.
Always start with an updated local Perforce workspace. You’ll avoid any potential subsequent update issues that may cause conflicts that require human intervention – as in debugging your automation script as it fails mid-way or manual merging/conflict resolution of files in the Perforce repository. Run the following command to sync your perforce repository:
p4 sync /local/perforce/dir/project/subdir/*
Notice that the command takes in a file-spec parameter. If this is not given, then Perforce will sync the entire workspace, not just the files that you want to update. Syncing the entire workspace sometimes this can take a very long time – more so if the server is half a world away.
Then you’ll need to change the relevant files to read/write since Perforce will set files as read-only by default if they’re not checked-out.
chmod -R u+w /local/perforce/dir/project/subdir/
Now you can copy files off the git repository into the Perforce workspace. We use rsync so that it’ll copy off only changed files and makes the process a tad faster:
rsync -ay --delete-after /local/git/dir/project/subdir/ /local/perforce/dir/project/subdir/
When the copy completes, we’ll ask Perforce to reconcile offline changes and overwrite any conflicts with the files that we got from git. This makes sense since the master copy is in git and Perforce only functions as a shadow copy of it. Run these commands in sequence for that:
p4 reconcile p4 resolve -ay
Finally submit the changes to the Perforce server.
p4 submit -d "…commit message…"
So that’s it. Like what I’ve said earlier, I have a bash script to automate this and you can also write a similar script matching your environment and schedule it using cron or as part as your Jenkins continuous integration build.
That’s all folks. I’ve spent probably a couple of days in total for this – both writing the script, looking up what Perforce commands to use (which includes learning how to use Perforce from the command line) and solving various environment issues that comes with it. Hopefully this can save some of your time.