tags:

views:

425

answers:

2

What's the equivalent of "git clean" with Perforce?

+3  A: 

There is no equivalent. Perforce has no command to remove files that are not under its control. You can see them in P4V, on the Workspace tab (they have plain white icons rather than the lined icons with the green dot) and delete them manually. If you want to automate the process, the easiest thing to do would be to remove the files from your workspace, delete everything in the directory, then sync it back up. A batch file to do it would look something like this:

p4 sync "//depot/someFolder/...#0"
erase C:\projects\someFolder\*.* /s /q /f
rd C:\projects\someFolder /s  /q
p4 sync -f "//depot/someFolder/..."

The first line is optional if you use the force switches on the erase and sync commands.


That solution has its drawbacks however. If you're currently working on any of the files, you obviously don't want to wipe them out. Also, a full sync can take quite a while if there is a huge amount of data in the directory tree you wish to clean.

A better way to do it would be to have your clean utility (I think we've grown beyond a batch file at this point) grab the list of files under version control using the p4 files command. Then iterate through all the files in the directory, deleting those that don't appear on the list.

raven
An extension to this would be to use `p4 fstat` to check if a file is under Perforce's version control. If it is not, Perforce will respond with `no such file(s)`, at which point the script would delete the file. Using this trick the script would be much faster as a complete resync of the depot would be unnecessary.
fbrereto
@fbrereton: You're right. My first suggestion was easy, but not the best. I added another idea to my answer along the lines of your suggestion, but using the files command instead of the fstat command.
raven
+4  A: 

Try (for Unix) from your top-level:

# Find all files and filter for those that are unknown by Perforce
find . -type f | p4 -x - fstat 2>&1 > /dev/null | sed 's/ -.*$//' > /tmp/list
### manually check /tmp/list for files you didn't mean to delete
# Go ahead and remove the unwanted files.
xargs rm < /tmp/list

Or, for a clean -f kind of approach just pipe directly to xargs rm instead of first staging the file list in /tmp/list.

Emil
Follow with "find -depth -type d -empty -exec rmdir {} \;" to remove empty directories.
Simon Perreault
this is more like git clean -xf; is there any way to honor the .p4ignore?
Ron