Perforce: Branching a Stream From Work In Progress

I just noticed that the Perforce knowledge base article about Branching Work In Progress is a little out of date. It does not cover what to do when you use streams and it is sub-optimal in the way it reconciles the files in the new branch. It mentions the shelve operation as a workaround but, sometimes, you really need a new branch.

Here is my take on this operation, using modern Perforce commands and the modern Windows shell, that is, Powershell.

For this example, I’ll assume that you have a workspace named “myclient” set to the stream “main” of the depot “project” and that you want to move your pending changes to a new stream named “myfeature”. This procedure will also switch your workspace to the new stream.

$files = (p4 opened) -replace "#.+","" -replace "//project/main","."
$files | p4 -x - revert -k
p4 stream -P //project/main -t development //project/myfeature
p4 populate //project/main/...@myclient //project/myfeature/...
p4 client -s -S //project/myfeature
p4 flush -q ...
$files | p4 -x - reconcile

And here are the explanations line by line:

$files = (p4 opened) -replace "#.+","" -replace "//project/main","."
This gets a list of all opened files and remove all revision and changelist information. (All your changes will end up in the default change list. Preserving the change list information is left as an exercise to the reader…) We also replace the depot path of the stream with “.”. Since we will use $files in the other commands, make sure your current directory is the workspace root. The source stream may need to be escaped since the -replace function expects a regular expression.

$files | p4 -x - revert -k
This reverts the opened files on the server but not on the client. This is slightly dangerous since it leave your workspace in an inconsistent state. We’ll fix that later.

p4 stream -P //project/main -t development //project/myfeature
This creates the new stream spec. It will open the stream spec in your text editor. Just close it since we already provided the required parameters on the command line. In a script, you should use the -o and -i options like this:
p4 stream -o -P //project/main -t development //project/myfeature | p4 stream -i

p4 populate //project/main/...@myclient //project/myfeature/...
This creates the files in your new stream from the same revision you were synced in the main stream. Don’t forget to change “myclient” by the name of your own workspace. This operation is server-side only.

p4 client -s -S //project/myfeature
This switches your workspace to the new stream but it does not sync anything. Now the state of your workspace is even more inconsistent! Because of previous “p4 revert” operation, a normal “p4 sync” would not be correct. We will fix that in the next 2 steps.

p4 flush -q ...
This let the server know that the content of our local files are already up to date. The -q option is to prevent the listing of all the files in the universe.

$files | p4 -x - reconcile
And finally, we use “reconcile” to let the server know that we have local modified files.

That’s it!

Advertisements
Posted in Perforce | Leave a comment