15

I have a file which has gotten too large, and should be refactored into two smaller files. What's the best way to do this in Perforce such that the relationship to the original file is maintained?

I'm adding two new files, and deleting the original in this case, but I would expect there to be some general solution to this problem.

I think the simplest case would be to add one new file which contains a subset of the content of the original, and delete that content from the original file, but leave it in place (it's trivial to delete it later anyway).

It would be nicest if the operation could be done in a single changelist to avoid any checkins which would break the build.

2
  • How are you going to do it in the first place? Add one file and keep the current one with less code? Add two files and remove the current one? Have three files (current and two new)?
    – rene
    Commented Feb 16, 2011 at 23:03
  • @rene, I've updated my question with more details. Commented Feb 16, 2011 at 23:25

3 Answers 3

11

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.

6
  • 2
    You don't have to do the first submit. You could open the files for edit and do your refactoring in the same change. Commented Feb 19, 2011 at 1:21
  • @Jason: Opening a file that is pending integration, and then submitting it, turns it into an add operation and thus you lose the history.
    – raven
    Commented Feb 19, 2011 at 2:05
  • 2
    It may seem like that, but you will not lose the integration relationship. It will show up as a Merge w/ Edit. Commented Feb 25, 2011 at 18:46
  • Will perforce be able to automatically merge a change to bigFile.cs in another branch into smallFile1.cs or smallFile2.cs depending on where the changed class went? (If not then this is not a full solution) Commented Mar 14, 2011 at 15:43
  • @Ian Ringrose: It will, kind of. What you need to do is have two branch specs (also used as integration specs), one of which points bigFile.cs to smallFile1.cs, and the other bigFile.cs to smallFile2.cs. Use them both to integrate in either direction, and Perforce will track the changes across the split. Some manual resolving of differences will still likely be needed, unfortunately, as it doesn't deal well with a change in one file in code that was deleted in the other file. Commented Mar 25, 2011 at 15:17
6

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.

1
  • If doing a p4 submit in the second step would break the build, then it can be avoided. The only reason I like doing it is because the history is more complete, showing one the exact state of the new file prior to it being modified.
    – Scott Saad
    Commented Feb 17, 2011 at 0:27
5

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.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .