October 17, 2014

Cleaning Up Your Build Workspaces with “p4 clean”

p4 clean
Image: PhylB via Flickr

Build engineers need a pristine state of their source material. This particularly means not having any artifacts such as object files or class files leftover from a previous build in the build workspace.

There are several ways to achieve this in Perforce:

  • Create a new workspace for each build
  • Reuse the workspace by removing all files and forcing a sync, optionally skipping the have-list update for more speed (using the –p option)

Neither is as fast as placing build artifacts outside the workspace in the first place, of course, but not every build environment is set up this way.

Perforce Server as of release 2014.1 made this simpler and faster by introducing a new “p4 clean” command, which restores any workspace to its original state. It removes build artifacts and other new files, resets any modified files, and restores deleted files at the same time.

The new clean command restores a workspace much faster than previous methods. Users should also note that:

  1. It restores the workspace only to its proper, current state. If the build needs to fetch new work or use a particular label, it should execute the corresponding sync after the initial clean.
  2. It compares the workspace file hashes with the server’s expected values to guarantee file integrity, which can take time for a huge code base.
  3. As such, the server’s hashes should be verified as part of routine maintenance (using “p4 verify”) to avoid unnecessary file copies.

p4 clean is not limited to build servers, of course. The next time you are tempted to write “p4 sync -f”, try this command instead and see how much faster it is.

There are many other new features and improvements in the latest release of the Perforce Server, the full details of which may be found in the release notes.

Happy hacking!