Programmatically delete a TFS branch

亡梦爱人 提交于 2020-01-05 07:43:25

问题


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?


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/14833083/programmatically-delete-a-tfs-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!