如何使用TFS API创建新的源代码分支?

TS.*_*TS. 2 api version-control tfs branch new-operator

我正在尝试使用API​​创建一个新分支,并使用了PendBranch()CreateBranch().问题CreateBranch()是它立即提交,我希望能够在签入分支时添加注释.所以,我所做的就是如下所示.

基本上我从我的Windows应用程序获取所有信息,如服务器项和本地项,以及分支的源和目标.

不知何故,当我看到Source Control Explorer时,它仍然会显示"Not mapped",即使我workspace.Get()在创建工作区后给出了:workspace.Map(serverItem,localItem)

任何人都可以阐明这一点吗?

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ard 5

在TFS中,变更集注释实际上是可编辑的.因此,您可以尝试使用VS2008/TFS2008 SP1中引入的CreateBranch方法,如下所示:

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();

}
Run Code Online (Sandbox Code Playgroud)