ulfgebhardt
ulfgebhardt

Reputation: 125

Programmatically delete a TFS branch

I want to programmatically delete a branch in TFS that was create automatically.

There is an existing method "ICommonStructureService.DeleteBranches" that should do the work. My problem is that the method requires a parameter "string[] nodeUris" that specifies the branch to delete using a "vstfs://... " URI and I just don't know how to get that for my branch.

What I need is something like:

var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri <myCollectionUrl>));
var cssService = projectCollection.GetService<ICommonStructureService3>();
var project = cssService.GetProjectFromName(<myProjectName>);

But how can I get the Branch Uri from there?

Upvotes: 1

Views: 1862

Answers (3)

ulfgebhardt
ulfgebhardt

Reputation: 125

Meanwhile I found a solution. For deleting the branches I am using

versionControl.Destroy(new ItemSpec(myBranchPath, RecursionType.Full), VersionSpec.Latest,  null, DestroyFlags.KeepHistory);

This does exactly what I needed. versionControl is of type VersionControlServer and must be initialized using the Team Collection

Upvotes: 1

G G
G G

Reputation: 1664

I agree to Edward Thomson about using Destroy command. So I followed on advice from him and came up with following,

public void DeleteBranch(string path)
{
    var vcs = GetVersionControlServer();
    var itemSpec = new ItemSpec(path, RecursionType.Full);
    var itemSpecs = new[] {itemSpec};
    var workSpace = GetOrCreateWorkSpace(vcs);
    try
    {
        workSpace.Map(path, @"c:\Temp\tfs");
        var request = new GetRequest(itemSpec, VersionSpec.Latest);
        workSpace.Get(request, GetOptions.GetAll | GetOptions.Overwrite);
        workSpace.PendDelete(path, RecursionType.Full);
        var pendingchanges = workSpace.GetPendingChanges(itemSpecs);
        workSpace.CheckIn(pendingchanges, "Deleting The Branch");
    }
    finally
    {
        if (workSpace != null)
        {
            workSpace.Delete();
        }
    }
}

If there is a neat way to do the same than I am looking forward to it. This is bit slow as it does too many things,

  • Creates Temp Workspace
  • Gets All changes to that
  • Performs Delete to whole change set
  • checks it in
  • Cleans up the workspace

Upvotes: 0

Edward Thomson
Edward Thomson

Reputation: 78873

Deleting a branch in version control is like deleting any other version control item. You will need to pend a delete with Workspace.PendDelete on the Item.

The method you reference is wholly unrelated to version control, it's part of the TFS common structure service, which controls the "areas and iterations" that TFS work items can be assigned to.

In short, there's no way to perform any sort of version control operations against the common structure service. You delete a branch by creating a Workspace against a VersionControlServer, pending a delete and then checking in your pending changes.

Upvotes: 0

Related Questions