p4 switch: Switching It Up
The 2015.1 release adds a new command, "p4 switch," that is designed to make it easy to switch between different streams when you're in the middle of a pending change. In a previous blog post we looked at a simple example of using "p4 switch" to make a quick change in a dev branch; I thought I'd talk a bit more about what exactly is happening when you run this command.
The basic "p4 switch" command shelves your pending changes (if any), switches your client view to the new stream, and syncs the workspace, all in one step.
Changes that are automatically shelved in this way will be unshelved when you switch back, so your workspace will look exactly the way you left it. There is also an automatic "p4 reconcile" before the shelve, so even modified files that you haven't opened will be safely preserved and will still be there when you switch back.
Another timesaver in the "p4 switch" command is the ability to use a relative path (that is, relative to the depot your current stream is in) to specify the stream. If you're switching from //Ace/main to //Ace/dev, you only need to type "p4 switch dev".
The "-c" ("create") flag on "p4 switch" will create the new stream if it does not already exist. This is the equivalent of using "p4 stream" to create a new stream (using the current stream as the parent), followed by "p4 populate" to populate it, followed by a normal "p4 switch". Note that as of 2015.1 this feature is only enabled on local servers; on shared servers, new streams still need to be created and populated explicitly.
The "-r" ("reopen") flag on "p4 switch" offers an alternative to shelving your pending work and leaving it behind -- what if you decide that the change you were making in the mainline actually belongs in a dev stream? Previously you might have done this by shelving the change and then unshelving it through the stream view; now this can be done at the time you switch streams. When you use the "-r" flag, the pending changes stay in your workspace, but they are now associated with the stream that you have just switched to.
The ability to reopen your client's pending changes against a different set of depot files is also available via the new "p4 sync -r" command, which "p4 switch -r" is essentially a wrapper for. This works with "classic" client views as well as stream clients -- if you change the mapping for an open client file and sync it with "sync -r", we will attempt to reopen the client file with the same pending action against the new depot file.