Wednesday, November 2, 2011

P4 detect new files and changes

If you are a Perforce user, you might occasionally find you have been creating or editing files without being correctly synchronized with the Perforce server. It's always a mundane task to manually correct this. Recently, I made a script for checking a list of files for being both added to P4 as well as being checked out for edit.

It's important for me to make very clear: USE THIS AT YOUR OWN RISK. TEST IT CAREFULLY.  I've been using this a while, but when it comes to this sort of thing you can never be too careful.

The script below goes into a batch file that will, for each file:

  • Attempt to add it to perforce. If it's already added, it won't affect anything.
  • Attempt to check out the file for editing. If it's already checked out, it won't affect anything.
  • Revert any unmodified changes on the file. If you already had the file checked out and modified, it won't revert it.
The end result is every file in the list that is either unknown to Perforce server or has been modified without checking it out are put in the default change list for review and possible committing to the server. The filelist.txt is a list of full file pathnames, created by whatever means you prefer. I sometimes use "where /r . *.cs" or similar, depending on context, but most of the time this file is generated by part of my build process.

Here's the script:

for /F %%A in (filelist.txt) do call :process %%A
exit /b

p4 add %A
p4 edit %1
p4 revert -a %1

I hope someone finds this useful. 

The code in this post is subject to the terms of the Creative Commons Attribution-ShareAlike 3.0 Unported License

Creative Commons License