如何使用命令行将TFS代码下载到本地工作空间中

问题描述 投票:-1回答:2

任何人都可以通过命令行将TFS代码下载到本地工作区的步骤帮助我。

到目前为止,我尝试使用以下代码,但遇到了多个问题

 var rv = ProcessHelper.Run(
            LocalWorkspace,
            @"C:\Program Files (x86)\Microsoft Visual 

Studio \ 2019 \ Enterprise \ Common7 \ IDE \ CommonExtensions \ Microsoft \ TeamFoundation \ Team Explorer \ tf.exe“, $“ get \”https://xxxx/tfs/xyz/Project/_versionControl?path=%24%2FProject\“ \” {LocalWorkspace} \“”);

对于上面的代码,它说不支持或指定路径

command-line tfs
2个回答
0
投票

首先,对于该示例,您需要向项目中添加必要的程序集引用,然后像这样使用它们:

   using System.IO;
   using System.Net;
   using Microsoft.TeamFoundation.Client;
   using Microsoft.TeamFoundation.Framework.Common;
   using Microsoft.TeamFoundation.Framework.Client;
   using Microsoft.TeamFoundation.VersionControl.Client;

您可以将此软件包安装到您的项目:Microsoft.TeamFoundationServer.ExtendedClient

有一个样本:DownloadDemo

第二,对于TF命令,您可以在构建日志中检查详细命令(以TFVC存储库为源),例如(签出任务:]

##[command]tf vc workspace /new /location:local /permission:Public ws_2_4 /collection:http://xxx:8080/tfs/DefaultCollection/ /loginType:OAuth /login:.,*** /noprompt
##[command]tf vc workfold /unmap /workspace:ws_2_4 $/ /collection:http://xxx:8080/tfs/DefaultCollection/ /loginType:OAuth /login:.,*** /noprompt
##[command]tf vc workfold /map /workspace:ws_2_4 $/ScrumStarain2019TFVC D:\AgentTest\vsts-agent-win-x86-2.144.2\_work\2\s /collection:http://xxx:8080/tfs/DefaultCollection/ /loginType:OAuth /login:.,*** /noprompt
##[command]tf vc get /version:7 /recursive /overwrite D:\AgentTest\vsts-agent-win-x86-2.144.2\_work\2\s /loginType:OAuth /login:.,*** /noprompt

0
投票

我想出了一种将TFS目录下载到本地工作区的解决方案。希望此代码能为后代所用。

  1. 在某些临时文件夹中创建本地工作空间

     LocalWorkspace = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():n}");
    
        if (!Directory.Exists(LocalWorkspace))
        {
            Directory.CreateDirectory(LocalWorkspace);
        }
    
    ProcessHelper.Run(
          LocalWorkspace,
          @"C:\Program Files (x86)\Microsoft Visual 
     Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team 
     Explorer\tf.exe",
          $"workspace /new /noprompt /collection:<your project url> /location:local 
    <workspace name>")
    
  2. 将最新版本的TFS放入该本地工作空间

     ProcessHelper.Run(
            LocalWorkspace,
            @"C:\Program Files (x86)\Microsoft Visual 
     Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team 
    Explorer\tf.exe",
            $"get <TeamProject> /recursive"));
    

ProcessHelper.Run():

   public sealed class ProcessHelper
    {
    private static string outputData;

    public static string Run(string workingDirectory, string executable, string arguments)
    {
        outputData = string.Empty;

        try
        {
            using Process proc = new Process();

            proc.StartInfo.WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), workingDirectory);
            proc.StartInfo.FileName = executable;
            proc.StartInfo.Arguments = arguments;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.CreateNoWindow = true;
            proc.ErrorDataReceived += DataReceived;
            proc.OutputDataReceived += DataReceived;
            proc.EnableRaisingEvents = true;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.BeginErrorReadLine();

            proc.WaitForExit();
        }
        catch (Exception ex)
        {
            return $"{executable} | {workingDirectory} | {arguments}{Environment.NewLine}{Environment.NewLine}{ex}";
        }

        return $"{executable} | {workingDirectory} | {arguments}{Environment.NewLine}{Environment.NewLine}{outputData}";
    }

    static void DataReceived(object sender, DataReceivedEventArgs e)
    {
        string data = e.Data;
        if (!String.IsNullOrEmpty(data))
        {
            Console.WriteLine(data);
            outputData += data;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.