Geeks With Blogs

News
Elton Stoneman (@EltonStoneman) IT Consultant, integration specialist, @Microsoft MVP and @Pluralsight author.

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. Posted on Friday, April 4, 2008 11:17 AM Perforce , Scripting & Automation , CodePlex Project | Back to top


Comments on this post: Scripting Branch Creation in Perforce

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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.
Left by manoj varshney on Aug 03, 2010 6:28 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
Excellent post, I wish i had the coding skills as you !
Left by Runescape Autominer on Sep 24, 2010 4:20 PM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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.
Left by disk backup on Nov 09, 2010 8:34 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
The scripts are very useful in branching.
Left by Chicken Coops on Nov 12, 2010 10:09 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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.
Left by online engineering degree on May 12, 2011 7:37 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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
Left by London Escorts on Jun 04, 2011 11:39 PM

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

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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.
Left by saga gold on Jul 16, 2011 8:18 PM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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.
Left by saga hair on Jul 17, 2011 5:12 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
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.
Left by saga remy on Jul 20, 2011 3:25 PM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
perfect analysis dear
I am also agree with your comments
Thanks for sharing it
Left by small business credit cards on Jul 23, 2011 11:45 PM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
There are certainly a lot more details to take into consideration, but thanks for providing this post.
Left by trendsandgadgets on May 22, 2012 11:54 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
perfect analysis dear
Left by sharksteammopreviews on Jun 19, 2012 1:54 PM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
i like your site you are a great site...
Left by Shirts custom on Sep 29, 2012 6:03 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
a Really good article , would it be possible to provide a readme or steps guide on how to use this.
Left by Bhanu on Oct 04, 2013 10:17 AM

# re: Scripting Branch Creation in Perforce
Requesting Gravatar...
a Really good article , would it be possible to provide a readme or steps guide on how to use this.
Left by Bhanu on Oct 04, 2013 10:17 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net | Join free