views:

78

answers:

2

Summary
I currently have a NAnt build script that performs a vssget on either the latest source code, or a specific branch (using a ${branch} parameter).

Whenever we do a production build/deployment the code-tree that was built has a branch created, (so that we can continue development and still know what codebase is on production, pretty standard stuff...)

Problem
The process of creation of that branch is still a manual one, performed by someone going into Visual Source Safe Explorer and performing the branching procedure. I was wondering if there is any way in NAnt of creating a VSS branch.

Current Plan
I already know about using <exec program="ss"> and am trying to avoid that, but in the absence of any better solutions, that is the most probable route I will take.

Does anyone know if there is a NAnt or NAntContrib target for this, or if anyone has a script task that they have used to do this in the past and could provide the code for that, that would be very much appreciated.

Disclaimer
I know about cvs, svn, git and all the other Source Control solutions, and to change the tool is not an option at present

+1  A: 

The vss tasks live in the NAntContrib project and no, currently there is no task that supports branching. Though, following the model of the existing vss tasks (add, checkout, checkin, etc) in NAntContrib, you could grab the source and extend it yourself. That is, if the VSS API supports branching.

Peter Lillevold
That's just what I had suspected, but thanks for the confirmation. I have actually downloaded the NAntContrib source and started fiddling with it, but I doubt I will get enough "official" time to write my own vssbranch task, so I will probably use exec with ss.exe.
funkymushroom
+1  A: 

We actually need this where I work. I whipped together a small task called 'vssbranch' (not particularly creative but here is the code...an example build file and the output of its execution:

CODE:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using SourceSafeTypeLib;

using NAnt.Core;
using NAnt.Core.Attributes;

namespace NAnt.Contrib.Tasks.SourceSafe
{
    [TaskName("vssbranch")]
    public sealed class BranchTask : BaseTask
    {
        /// <summary>
        /// The label comment.
        /// </summary>
        [TaskAttribute("comment")]
        public String Comment { get; set; }

        /// <summary>
        /// Determines whether to perform the branch recursively.
        /// The default is <see langword="true"/>
        /// </summary>
        [TaskAttribute("recursive"),
        BooleanValidator()]
        public Boolean Recursive { get; set; }

        [TaskAttribute("branchname", Required = true)]
        public String BranchName { get; set; }


        protected override void ExecuteTask()
        {
            this.Open();
            try
            {
                if (VSSItemType.VSSITEM_PROJECT != (VSSItemType)this.Item.Type)
                    throw new BuildException("Only vss projects can be branched", this.Location);

                IVSSItem newShare = null;
                this.Comment = String.IsNullOrEmpty(this.Comment) ? String.Empty : this.Comment;
                if (null != this.Item.Parent)
                    newShare = this.Item.Parent.NewSubproject(this.BranchName, this.Comment);

                if (null != newShare)
                {
                    newShare.Share(this.Item as VSSItem, this.Comment,
                        (this.Recursive) ?
                            (int)VSSFlags.VSSFLAG_RECURSYES : 0);
                    foreach (IVSSItem item in newShare.get_Items(false))
                        this.BranchItem(item, this.Recursive);
                }
            }
            catch (Exception ex)
            {
                throw new BuildException(String.Format("Failed to branch '{0}' to '{1}'", this.Item.Name, this.BranchName), this.Location, ex);
            }
        }

        private void BranchItem(IVSSItem itemToBranch, Boolean recursive)
        {
            if (null == itemToBranch) return;

            if (this.Verbose)
                this.Log(Level.Info, String.Format("Branching {0} path: {1}", itemToBranch.Name, itemToBranch.Spec));

            if (VSSItemType.VSSITEM_FILE == (VSSItemType)itemToBranch.Type)
                itemToBranch.Branch(this.Comment, 0);
            else if (recursive)
            {
                foreach (IVSSItem item in itemToBranch.get_Items(false))
                    this.BranchItem(item, recursive);
            }
        }
    }
}

BUILD FILE:

        <echo message="About to execute: VSS Branch" />

        <echo message="Source Safe Path: ${SourceSafeRootPath}/${CURRENT_FILE}" />

        <vssbranch
              username="my_user_name"
              password="my_password"
              recursive="true"
              comment="attempt to make a branch"
              branchname="test-branch"
              dbpath="${SourceSafeDBPath}"
              path="${SourceSafeRootPath}/${CURRENT_FILE}"
              verbose="true"
            />

    </foreach>
</target>

OUTPUT:

NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006) Copyright (C) 2001-2006 Gerry Shaw http://nant.sourceforge.net

Buildfile: file:///C:/scm/custom/src/VssBranch/bin/Debug/test.build Target framework: Microsoft .NET Framework 2.0 Target(s) specified: run

run:

[loadtasks] Scanning assembly "NAnt.Contrib.Tasks" for extensions. [loadtasks] Scanning assembly "VssBranch" for extensions. [echo] About to execute: VSS Branch

....

[vssbranch] Branching SecurityProto path: $/VSS/Endur's Source/C#/DailyLive/proto/test-branch/SecurityProto

....

BUILD SUCCEEDED

Total time: 12.9 seconds.

Obviously the output would vary, I was pulling in the items to branch from a text file named 'params.txt'. This task performs what is known in the VSS world as 'Share and Branch' (Branching immediately after Sharing)...other source control systems do not need to share before branching, eh...that's for another day

Mish Ochu
Oooh, thank you so much!
funkymushroom
Would I have to build this in a separate DLL, or should I put it in place in the original NAntContrib solution file?And if I build it into the original NAntContrib solution, should I use the built DLL to replace my current NAntContnrib dll?
funkymushroom
sorry...I didn't include instructions...I compiled the source in its own project simply referencing NAntContrib (and all the items NAntContrib referenced as well...in this case they were SourceSafe.Interop.dll and NAnt.Core.dll)
Mish Ochu
Thanks, I will try that. I grabbed the NAntContrib source code, and built the vssbranch task into that, and got it working.I did, however find a bug where it appeared to be truncating file names, and then throwing a file not found exception, which appears to be a bug in the API (not sure). I do want to add a `failonerror` attribute, which, if one file breaks, it skips and continues branching...
funkymushroom