p4reconcile

2009-10-30

If you are using Perforce (p4), sometimes you need a way to reconcile a folder. Both P4WIN and P4V have this feature to some degree. The command line p4 does not so here's a small program to do just that.

In p4 generally you are supposed to check things out. This tells p4 that you are editing the file and makes the file modifiable. When you go to submit your changes p4 knows what files you've edited, added or deleted since you already told it.

Sometimes though it's not easy to tell p4 what you've edited, added or deleted. Two common examples come to mind.

  1. You are working offline.

p4 generally requires you to be able to connect to the p4 server so you can tell it that you are editing a file. If you are on an airplane or just somewhere where you can't connect to the p4 server then you have to remember to tell it which files you edited when you get re−connected. P4WIN and P4V both have options to help with this but the command line does not.

  1. You have some automated process that creates files that need to get checked in to p4.

For example my current project has a documentation generation system. The build always generates the docs but for each release we actually want to check the built docs into perforce since our release system gets its files from perforce to make public.

After the docs are built it would suck if we had to manually tell p4 what changed. We could just delete the whole tree and then add all the files again but that would lose any history of what was edited.

So...

Introducing p4reconcile.

It's a python script so you'll need to have python installed. On OSX and Linux it's usually installed by default. On Windows you'll need to install it if you don't already have it.

Assuming python is in your path you can type

./p4reconcile <folder>

or

python p4reconcile.py <folder>

on windows

Where folder is one of your local folders that is being managed by p4. p4reconcile will create a p4 changelist that adds, edits or deletes files from p4 to make p4 match the state of that folder. Note that p4reconcile only creates a changelist. It is up to you to submit the changelist to make the changes permanent.

You can get p4reconcile here.

I hope you find this useful.

PS: One other thing you might find useful is looking at the source code. p4 provides a way for python to talk directly to p4 using the −G option. The −G option makes p4 communicate to python through python objects. This removes the need to parse p4 output which could lead to errors. You can see this being used by looking at p4manager.py which is a small python class to manage p4.

Comments
My $345 Quest for a Space Channel 5 part 2
Designing Better APIs