如何将TFS更改集文件导出到目标文件夹

Sai*_*Sur 9 tfs export changeset

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

目的:我想在Code上传到TFS后提取并查看包含这些变更集的Build的CODE.

TFS Power Tool的以下命令没有提及Destination文件夹的任何选项.

tfpt getcs/changeset:changesetNo

提前致谢

Ant*_*onK 14

如何提取变更集列表

我有这个要求,以便为发布创建补丁.我在tfs或tfs电动工具中找不到任何东西来做这件事,所以我写了自己的.

要使用,语法如下:

GetTfsChangeSet.exe TfsServerUrl changsetIdList fileOutputPath [merge]
Run Code Online (Sandbox Code Playgroud)

哪里:

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

例如

GetTfsChangeSet.exe http://asdpwiap017:8080/tfs 1233,4555,3332 c:\deploy merge
Run Code Online (Sandbox Code Playgroud)

创建一个控制台应用程序

添加这些程序集引用:

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

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

  • 我希望我可以多次赞成这个答案.非常感谢安东! (5认同)
  • 要在**VS2017**上使用此代码,您需要NuGet:**Microsoft.TeamFoundationServer.ExtendedClient**示例NuGet控制台命令将是:`Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1` (4认同)
  • 很高兴我能帮助你. (2认同)

Giu*_*ian 0

您只需添加另一个映射到该D:\myTFSExport文件夹​​的工作区并使用

tf get $/MyProject /version:Cnnnn /recursive
Run Code Online (Sandbox Code Playgroud)

哪里nnnn是所需的变更集编号。