【问题标题】:How to export TFS changeset files to a destination Folder如何将 TFS 变更集文件导出到目标文件夹
【发布时间】:2021-06-25 04:41:21
【问题描述】:

我想导出特定变更集和/或系列中的多个变更集的 TFS 源文件。文件应该导出到 D:\myTFSExport 文件夹。这不是现有的映射文件夹。

目的:在将代码上传到 TFS 后,我想提取并查看包含这些变更集的构建代码。

TFS Power Tool 的以下命令没有提及目标文件夹的选项。

tfpt getcs /changeset:changesetNo

提前致谢

【问题讨论】:

    标签: tfs export changeset


    【解决方案1】:

    如何提取变更集列表

    我正是有这个要求才能为发布创建补丁。我在 tfs 或 tfs 电动工具中找不到任何东西可以做到这一点,所以我自己写了。

    要使用,语法如下:

    GetTfsChangeSet.exe TfsServerUrl changsetIdList fileOutputPath [merge]
    

    地点:

    • TfsServerUrl:TFS 服务器 URL
    • changsetIdList : 逗号分隔的变更集列表
    • fileOutputPath:输出路径(不需要映射)
    • merge:使用 Merge 参数,将所有变更集合并到一个文件夹中。如果没有参数,每个更改集都会输出到不同的文件夹。

    例如

    GetTfsChangeSet.exe http://asdpwiap017:8080/tfs 1233,4555,3332 c:\deploy merge
    

    创建控制台应用程序解决方案。

    添加这些程序集引用:

    • Microsoft.TeamFoundation.Client
    • Microsoft.TeamFoundation.Common
    • Microsoft.TeamFoundation.VersionControl.Client

    程序.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
    
    namespace GetTfsChangeSet
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                if (args.Length < 3)
                {
                    Console.WriteLine("Usage:");
                    Console.WriteLine("GetTfsChangeSet.exe TfsServerUrl changsetIds fileOutputPath [merge]");
                    Console.WriteLine();
                    Console.WriteLine("where:");
                    Console.WriteLine("- changsetIdList : comma separated list of changesets");
                    Console.WriteLine("- merge: With Merge param, combines all changesets into one folder. Without the param, each change set is output to a different folder.");
                    Console.WriteLine();
                    Console.WriteLine("e.g.");
                    Console.WriteLine(@"GetTfsChangeSet.exe http://asdpwiap017:8080/tfs 1233,4555,3332 c:\deploy merge");
    
                    //Console.ReadKey();
                    return;
                }
    
                string teamProjectCollectionUrl = args[0]; // "http://asdpwiap017:8080/tfs";
                var changesets = args[1].Split(',');
                string outputDir = args[2];
                bool mergeChangeSets = args.Length >= 4 && args[3].ToLower().Equals("merge");
    
                if (mergeChangeSets)
                {
                    Console.WriteLine("Merge changesets " + args[1]);
                }
    
                TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
                string downloadPath = "";
    
                if (mergeChangeSets)
                {
                    downloadPath = args[1].Replace(',', '-');
                    if (downloadPath.Length > 30)
                    {
                        downloadPath = downloadPath.Substring(0, 15) + "..." + downloadPath.Substring(downloadPath.Length-15);
                    }
                    downloadPath = Path.Combine(outputDir, downloadPath);
                }
    
    
                foreach (var changesetStr in changesets.OrderBy(c=>c))
                {
                    var changeset = Convert.ToInt32(changesetStr);
                    if (!mergeChangeSets)
                    {
                        downloadPath = Path.Combine(outputDir, changeset.ToString());
                    }
    
                    var files = GetFilesAssociatedWithBuild(teamProjectCollection, changeset, downloadPath);
    
                    Console.WriteLine(string.Format("ChangeSet {0}: {1} files extracted.", changeset, files.Count));
                }
    
                Console.WriteLine("Done.");
                //Console.ReadKey();
            }
    
            private static List<string> GetFilesAssociatedWithBuild(TfsTeamProjectCollection teamProjectCollection, int changesetId, string downloadPath)
            {
                List<string> files = new List<string>();
                VersionControlServer versionControlServer = teamProjectCollection.GetService(typeof(VersionControlServer)) as VersionControlServer;
                Changeset changeset = versionControlServer.GetChangeset(changesetId);
                if (changeset.Changes != null)
                {
                    foreach (var changedItem in changeset.Changes)
                    {
                        var item = changedItem.Item;
                        if (item.ItemType != ItemType.File || item.DeletionId != 0)
                            continue;
    
                        var outFilename = Path.Combine(downloadPath, item.ServerItem.Replace("$/", "").Replace("/", @"\"));
                        item.DownloadFile(outFilename);
    
                        files.Add(outFilename);
                    }
                }
                return files;
            }
        }
    }
    

    【讨论】:

    • 我希望我能对这个答案投票一百次。非常感谢安东!
    • 很高兴我能帮上忙。
    • 要在 VS2017 上使用此代码,您需要 NuGet:Microsoft.TeamFoundationServer.ExtendedClient NuGet 控制台命令示例为:Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1跨度>
    • 如果您收到错误“无法检索 ISCCProvider 的注册信息”,则必须将集合添加到 URL,例如http://asdpwiap017:8080/tfs/DefaultCollection
    • 哦,这个救了我的命
    【解决方案2】:

    您可以简单地添加另一个映射到D:\myTFSExport 文件夹的工作区并使用

    tf get $/MyProject /version:Cnnnn /recursive
    

    nnnn 是所需的变更集编号。

    【讨论】:

    • 一个提示:您可以使用临时工作区,立即删除。然后使用 tf get 或 tfpt getcs
    猜你喜欢
    • 2013-06-09
    • 2017-08-27
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 2017-03-31
    • 2021-08-18
    相关资源
    最近更新 更多