views:

668

answers:

1

Hi I am trying to create a new branch using the API, and have used both PendBranch() and CreateBranch(). The problem with CreateBranch() is it commits immediately and I want to be able to add comments as the branch is checked in. So, I what I did is shown below.

Basically I get all the information like server item and local item to be mapped as well as source and target of the branch from my windows application. Somehow, when I see Source Control Explorer it still says "Not mapped" even though I have given a : workspace.Get() after creating the workspace and workspace.Map(serverItem,localItem)

Can anyone shed light on this?

Thanks Tara.

public void CreateNewBranch(string server,string serverItem,string localItem,string sourceBranch, string targetBranch)
    {
        int changeSetNumber = 0;
        // Get a reference to Team Foundation Server and Source Control.
        tfs = GetTFS(server);
        // Create a new workspace for the currently authenticated user.             
      workspace = tfvc.CreateWorkspace("Example Workspace", tfvc.AuthenticatedUser);
        }
        // Create a mapping to the project.
        try
        {
           workspace.Map(serverItem, localItem);

            // Get the latest source files from the repository.
            //workspace.Get();

            // Perform a pending Branch operation. 
            workspace.PendBranch(sourceBranch, targetBranch, VersionSpec.Latest);
            // Get a list of all the Pending Changes.
            PendingChange[] pendingChanges = workspace.GetPendingChanges();
            if (pendingChanges.Length > 0)
            {
                changeSetNumber = workspace.CheckIn(pendingChanges, "Comment:Branch Created");
                MessageBox.Show("Checked in changeset # " + changeSetNumber);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            // Cleanup the workspace.
            workspace.Delete();
        }
    }
+1  A: 

Hi Tara,

In TFS changeset comments are actually editable. Therefore you could try something like the following that makes use of the CreateBranch method introduced in VS2008/TFS2008 SP1:

public void CreateBranchWithComment(
    string serverUrl, 
    string sourcePath, 
    string targetPath, 
    string comment)
{
    TeamFoundationServer tfs = new TeamFoundationServer(serverUrl);
    VersionControlServer vcServer = 
        (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

    int changesetId = vcServer.CreateBranch(
        sourcePath, 
        targetPath, 
        VersionSpec.Latest);

    Changeset changeset = vcServer.GetChangeset(changesetId);
    changeset.Comment = comment;
    changeset.Update();

}

Hope that helps,

Martin.

Martin Woodward
That is sooo simple :) thanks a lot! By the way, I was trying to remedy the problem of not being able to see the mappings in source control. It said "Not Mapped" and then I realised that workspace.Get() queries the current workspace, so I changed it and it worked beautifully!
TS
The "Latest" column in source control said "Not downloaded." I had to manually get the latest version in the IDE to see the working folder mappings..the new branch appeared grey.why is this??
TS
Richard Berg