【问题标题】:How do you get the latest version of source code using the Team Foundation Server SDK?如何使用 Team Foundation Server SDK 获取最新版本的源代码?
【发布时间】:2010-12-22 02:37:08
【问题描述】:

我正在尝试使用 SDK 以编程方式从 TFS 中提取最新版本的源代码,但我所做的不知何故不起作用:

string workspaceName = "MyWorkspace";
string projectPath = "/TestApp";
string workingDirectory = "C:\Projects\Test\TestApp";

VersionControlServer sourceControl; // actually instantiated before this method...

Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name);
if (workspaces.Length > 0)
{
    sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser);
}
Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace");
try
{
    workspace.Map(projectPath, workingDirectory);
    GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest);
    GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors
}
finally
{
    if (workspace != null)
    {
        workspace.Delete();
    }
}

该方法基本上是创建一个临时工作区,使用Get() 方法抓取该项目的所有项目,然后删除该工作区。这是正确的方法吗?任何示例都会有所帮助。

【问题讨论】:

    标签: c# tfs tfs-sdk


    【解决方案1】:

    我最终使用了另一种似乎可行的方法,主要是利用Item.DownloadFile() 方法:

    VersionControlServer sourceControl; // actually instantiated...
    
    ItemSet items = sourceControl.GetItems(sourcePath, VersionSpec.Latest, RecursionType.Full);
    
    foreach (Item item in items.Items)
    {
        // build relative path
        string relativePath = BuildRelativePath(sourcePath, item.ServerItem);
    
        switch (item.ItemType)
        {
        case ItemType.Any:
            throw new ArgumentOutOfRangeException("ItemType returned was Any; expected File or Folder.");
        case ItemType.File:
            item.DownloadFile(Path.Combine(targetPath, relativePath));
            break;
        case ItemType.Folder:
            Directory.CreateDirectory(Path.Combine(targetPath, relativePath));
            break;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我完成并将代码实现为一个按钮作为 web asp.net 解决方案。

      要使项目在引用中工作,应添加 Microsoft.TeamFoundation.ClientMicrosoft.TeamFoundation.VersionControl.Client 引用,并在代码中添加语句 using Microsoft.TeamFoundation.Client;using Microsoft.TeamFoundation.VersionControl.Client;

          protected void Button1_Click(object sender, EventArgs e)
          {
              string workspaceName = "MyWorkspace";
              string projectPath = @"$/TeamProject"; // the container Project (like a tabel in sql/ or like a folder) containing the projects sources in a collection (like a database in sql/ or also like a folder) from TFS
      
              string workingDirectory = @"D:\New1";  // local folder where to save projects sources
      
              TeamFoundationServer tfs = new TeamFoundationServer("http://test-server:8080/tfs/CollectionName", System.Net.CredentialCache.DefaultCredentials);
                                                                  // tfs server url including the  Collection Name --  CollectionName as the existing name of the collection from the tfs server 
              tfs.EnsureAuthenticated(); 
      
              VersionControlServer sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
      
              Workspace[] workspaces = sourceControl.QueryWorkspaces(workspaceName, sourceControl.AuthenticatedUser, Workstation.Current.Name);
              if (workspaces.Length > 0)
              {
                  sourceControl.DeleteWorkspace(workspaceName, sourceControl.AuthenticatedUser);
              }
              Workspace workspace = sourceControl.CreateWorkspace(workspaceName, sourceControl.AuthenticatedUser, "Temporary Workspace");
              try
              {
                  workspace.Map(projectPath, workingDirectory);
                  GetRequest request = new GetRequest(new ItemSpec(projectPath, RecursionType.Full), VersionSpec.Latest);
                  GetStatus status = workspace.Get(request, GetOptions.GetAll | GetOptions.Overwrite); // this line doesn't do anything - no failures or errors
              }
              finally
              {
                  if (workspace != null)
                  {
                      workspace.Delete();
                      Label1.Text = "The Projects have been brought into the Folder  " + workingDirectory;
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:

        你的方法是有效的。

        您的错误在您的项目路径中。改用这样的东西:

        string projectPath = "$/PathToApp/TestApp";
        

        【讨论】:

          【解决方案4】:

          我同意 Joerage 的观点,即您的服务器路径可能是罪魁祸首。为了更深入地了解正在发生的事情,您需要在 VersionControlServer 对象上连接一些事件。至少您需要 Getting、NonFatalError 和 Conflict。

          完整列表:http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.versioncontrolserver_events(VS.80).aspx

          【讨论】:

          • 我为响应延迟道歉,因为我陷入了另一个问题,但更改路径似乎也不起作用。我尝试连接所有事件,但没有一个被触发。我最终完全使用了不同的策略(item.DownloadFile),这种方式似乎工作得很好——而且不需要工作区。
          • @JohnRasch,您能否分享代码 sn-p,因为我遇到了同样的问题?!​​! :(
          【解决方案5】:

          我有类似的情况,我需要从 tfs 将“a”文件夹的内容下载到现有工作区,而不创建新工作区。在上述答案的帮助下,我能够将一些对我来说很好的东西放在一起。但是有一个限制。这适用于仅包含文件而不是其中的另一个文件夹的“a”文件夹的内容-我还没有尝试过。也许这将涉及一些小的更新。共享代码,以防万一有人在搜索这个。我真的很喜欢这种方法不处理工作区 [-create and delete],因为这是不希望的。

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Configuration;
          using Microsoft.TeamFoundation.VersionControl.Client;
          using Microsoft.TeamFoundation.Client;
          using System.IO;
          
          namespace DownloadFolder
          {
              class Program
              {
                  static void Main(string[] args)
                  {
                      string teamProjectCollectionUrl = "http://<YourTFSUrl>:8080/tfs/DefaultCollection";  // Get the version control server
                      TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
                      VersionControlServer vcs = teamProjectCollection.GetService<VersionControlServer>();
                      String Sourcepath = args[0]; // The folder path in TFS - "$/<TeamProject>/<FirstLevelFolder>/<SecondLevelFolder>"
                      String DestinationPath = args[1]; //The folder in local machine - "C:\MyTempFolder"
                      ItemSet items = vcs.GetItems(Sourcepath, VersionSpec.Latest, RecursionType.Full);
                      String FolderName = null;
                      foreach (Item item in items.Items)
                      {
                          String ItemName = Path.GetFileName(item.ServerItem);
                          switch (item.ItemType)
                          {
                              case ItemType.File:                        
                                  item.DownloadFile(Path.Combine(DestinationPath, FolderName, ItemName));
                                  break;
                              case ItemType.Folder:
                                  FolderName = Path.GetFileName(item.ServerItem);
                                  Directory.CreateDirectory(Path.Combine(DestinationPath, ItemName));
                                  break;
                          }
                      }
                  }
              }
          }
          

          在命令提示符下运行时,复制所有支持的 dll 以及 exe cmd>>DownloadFolder.exe "$/&lt;TeamProject&gt;/&lt;FirstLevelFolder&gt;/&lt;SecondLevelFolder&gt;" "C:\MyTempFolder"

          【讨论】:

            猜你喜欢
            • 2014-10-28
            • 1970-01-01
            • 1970-01-01
            • 2020-05-01
            • 2014-11-13
            • 1970-01-01
            • 1970-01-01
            • 2012-03-06
            • 2010-11-08
            相关资源
            最近更新 更多