What's the proper way to refactor a single file into multiple files and maintain version history in Perforce?

This can't be done in a single checking, but it can certainly be done without "breaking the build". Let's say you want to split bigFile.cs into smallFile1.cs and smallFile2.cs. First integrate the big file into the two little files and submit them.

p4 integrate bigFile.cs smallFile1.cs
p4 integrate bigFile.cs smallFile2.cs
p4 submit

You now have two extra files in your project directory, but they're doing no harm. Now check out smallFile1.cs and smallFile2.cs, and your project file(s). Add references to the smaller files, remove the reference to the big, edit the small files accordingly, etc. Finally, mark bigFile.cs for delete and submit your changes.

You have now split your big file into two smaller files and the smaller files' history will show you their big file origins.


You can use the integrate command.

When you've made changes to a file that need to be propagated to another file, start the process with p4 integrate.

The simplist form of the command would be

p4 integrate fromFile toFile

I would therefore perform the following tasks:

  1. run p4 integrate with toFile being the new file and fromFile being the large, original file
  2. p4 submit
  3. p4 edit both fromFile and toFile to be the smaller versions of the original.
  4. p4 submit

With this method, your file history information is kept in tact for all future revisions of the files.


This actually can be done with a single checkin. The steps are as follows:

  1. integrate from the source file to both destination files as both raven and Scott Saad suggest
  2. before submitting the new files, do a p4 edit on both files
  3. make changes
  4. p4 submit

The complete file history shows up in the revision graph and time-lapse views. The only disadvantage that I can see to skipping the intermediate submit is that the action type changes from 'integrate' to 'add'. Because of that, other people might not realize there is more to the file history.

I think I slightly prefer the two-checkin process.

Tags:

Perforce