Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  120 Posts | 0 Stories | 3601 Comments | 0 Trackbacks

News

Archives

Post Categories


To simplify the branching process, allow automation and remove the errors likely from manually branching, I'd like the whole process to be scripted. The P4 command allows us to do this, but there are complications as it requires user input via the specification forms for most of its commands. You can direct it to read the input from a file, but that's not much help when you want to use variables to specify branch names etc.

Generating Specification Forms on the Fly

My solution was to create a simple console app to generate spefication forms based on input parameters. The variables in the script go into the generator which stores them as files, which P4 can use as input. It's not terribly elegant, but it's efficient and I haven't found any other solutions out there. The code is on CodePlex here: Generate Perforce Specification Form

Using the console app, it's easy to take the manual steps from Branching with Perforce and script them for automation. In this case, I'm expecting branching to be an occassional job so the purpose of scripting it is to reduce the possibility of error and simplify the branching process for anyone who needs to do it and doesn't want to trawl through these blogs.

With this in mind, the script is a Windows batch file (the full script is BranchWorkspace.bat in the Samples directory of the CodePlex source) rather than a build task. It should be straightforward to read, but I'll dissect it here to show how the commands fit in with the manual process.

Walkthrough

Firstly the batch file sets up parameters for all the variables we'll use - branch name, branch master workspace name, views etc. Then it follows the same order as the manual process, generating the specification forms where needed, then invoking the P4 command and telling it to read from the spec form.

1. Create the branch view

::generate spec form for the branch:
set branchSpecPath=C:\%branchName%.p4
GenerateP4SpecForm "WRITE" "%branchSpecPath%" "|" "Branch: %branchName%|Owner:    elton.stoneman|Description: Created by elton.stoneman.|Options:    unlocked|View: %mainView% %branchView%"

The console app takes parameters telling it where to create the spec form, how the form options are separated (I use a pipe as there are colons in the options), and then the pipe-separated option list.

What goes into the list depends on the type of spec form P4 wants; there are samples in the CodePlex source, or you can copy and paste from a real spec form, or many of the commands take a -o parameter so you can output an existing spec form and use that as the basis.

::create the branch view:
p4 branch -i <%branchSpecPath%
- the branch view is created and we pass the spec form path to P4 so there's no user intervention.

2. Create the master workspace

As before, generate the relevant spec form and pass it into P4:

::generate spec form for the branch master workspace:
set branchClientSpecPath=C:\%branchClient%.p4
GenerateP4SpecForm "WRITE" "%branchClientSpecPath%" "|" "Client: %branchClient%|Owner: elton.stoneman|Host: myHost|Description: Created by elton.stoneman.|Root: %branchClientRoot%|Options: noallwrite noclobber nocompress unlocked nomodtime normdir|SubmitOptions: submitunchanged|LineEnd: local|View: %branchView% %branchClientView%"

::create branch workspace:
p4 client -i <%branchClientSpecPath%

3. Flag files to be branched

No spec form needed here, but the batch file allows you to branch from the current revisions or from a specified label:

::integrate the branch back to main - branch to label if provided:
IF "%label%"==" " p4 -c%branchClient% integrate -b %branchName%
IF NOT "%label%"==" " p4 -c%branchClient% integrate -b %branchName% @%label%

4. Submit the change list

This is a bit different as the submit command launches a spec form listing the files to be submitted, and requires you to add a description. We can't generate the spec form here, as we'll lose the file list, so instead we output the file list first:

::get a list of the changes (required for Submit):
set changePath=c:\%branchClient%.chg
p4 -c%branchClient% change -o >%changePath%

- and then use the UPDATE mode of the console app to set the description (P4 always uses the same text for the default description):

IF "%label%"==" " GenerateP4SpecForm "UPDATE" "%changePath%" "<enter description here>" "MASTER created from branch: %branchName%"
IF NOT "%label%"==" " GenerateP4SpecForm "UPDATE" "%changePath%" "<enter description here>" "MASTER created from branch: %branchName%, label: %label%"

This way we get the full change list from Perforce and add in our description (in this case, specifying the branch view and label used to get the files). Now we submit using the updated change list:

::submit to create branch in depot:
p4 -c%branchClient% submit -i <%changePath%

And the branch is complete and populated.

Note, the sample batch file leaves the generated spec forms in place, in case of any issues. It's a simple change to make the script delete them at the end.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Friday, April 04, 2008 11:17 AM

Feedback

# re: Scripting Branch Creation in Perforce 8/3/2010 6:28 AM manoj varshney
Instead of all steps. Pls. make one full script and what are parameters (branch name, owner...) need to pass. It will be easy and help lot for me.

# re: Scripting Branch Creation in Perforce 9/24/2010 4:20 PM Runescape Autominer
Excellent post, I wish i had the coding skills as you !

# re: Scripting Branch Creation in Perforce 11/9/2010 8:34 AM disk backup
Perforce triggers are user-written scripts that are called by a Perforce server whenever certain operations are performed.Daemons run at predetermined times, looking for changes to the Perforce metadata.

# re: Scripting Branch Creation in Perforce 11/12/2010 10:09 AM Chicken Coops
The scripts are very useful in branching.

# re: Scripting Branch Creation in Perforce 5/12/2011 7:37 AM online engineering degree
I've ever try to make this. But, I made a mistake in third process. I was very confused when trying. But after reading your post again, I have a clearer mind.

# re: Scripting Branch Creation in Perforce 6/4/2011 11:39 PM London Escorts
Very impressive article, rarely do we encounter one that’s both educative and entertaining, check us out and one of us could be visiting you soon…
Pakistani escorts London

# re: Scripting Branch Creation in Perforce 6/9/2011 12:09 PM traitement rides
There are certainly a lot more details to take into consideration, but thanks for sharing this post.Keep it up


# re: Scripting Branch Creation in Perforce 7/16/2011 8:18 PM saga gold
I've ever I was very try to make this. But, I made a mistake in third process. confused when trying. But after reading your post again, I have a clearer mind.


# re: Scripting Branch Creation in Perforce 7/17/2011 5:12 AM saga hair
Instead of all steps. Pls. make one full script and what are parameters (branch name, owner...) need to pass. It will be easy and help lot for me.

# re: Scripting Branch Creation in Perforce 7/20/2011 3:25 PM saga remy
Perforce triggers Daemons run at predetermined times, looking for changes to the Perforce metadata. are user-written scripts that are called by a Perforce server whenever certain operations are performed.

# re: Scripting Branch Creation in Perforce 7/23/2011 11:45 PM small business credit cards
perfect analysis dear
I am also agree with your comments
Thanks for sharing it


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: