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

时间:2015-05-20 10:58:57

标签: tfs export changeset

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

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

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

tfpt getcs / changeset:changesetNo

提前致谢

2 个答案:

答案 0 :(得分:12)

如何提取变更集列表

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

要使用,语法如下:

GetTfsChangeSet.exe TfsServerUrl changsetIdList fileOutputPath [merge]

其中:

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

e.g。

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

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

添加这些程序集引用:

  • 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;
        }
    }
}

答案 1 :(得分:0)

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

tf get $/MyProject /version:Cnnnn /recursive

其中nnnn是所需的变更集编号。