65.9K
CodeProject 正在变化。 阅读更多。
Home

TFS:从日期/变更集跟踪源代码管理中的所有更改文件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (4投票s)

2014年12月12日

CPOL
viewsIcon

45566

downloadIcon

10

获取从某个日期或变更集修改后的所有文件列表

引言

此技巧可帮助您跟踪TFS中从给定日期(变更集)开始对代码库所做的所有更改。

背景

在某些情况下,开发团队可能希望跟踪从某个日期开始他们添加/修改的所有文件。我的代码片段将帮助他们通过简单的C#逻辑连接到TFS来实现这一目标。

Using the Code

在Visual Studio中创建一个控制台应用程序。

默认情况下,您将拥有Program.cs。现在让我们添加TfsHelper.cs并将以下代码放入其中

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;

namespace Maintenance
{
    public class TfsHelper
    {
        List<string> changedFiles = new List<string>();
        public void Init(params string[] args)
        {
            string localPath = args[0];
            string versionFromString = args[1];
            TfsTeamProjectCollection tfs = null;
            if (args.Length > 2)
                {
                tfs = new TfsTeamProjectCollection(new Uri(args[2]));
                 }
            else return;
            VersionControlServer vcs = tfs.GetService<versioncontrolserver>();
            try
            {
                var changeSetItems = vcs.QueryHistory(localPath, 
                                                      VersionSpec.ParseSingleSpec(
                                                                        versionFromString, 
                                                                        null),
                                                      0, RecursionType.Full, null, 
                                                      VersionSpec.ParseSingleSpec(
                                                                        versionFromString, 
                                                                        null), 
                                                                        null, Int32.MaxValue, true, false);
                foreach (Changeset item in changeSetItems)
                {
                    DateTime checkInDate = item.CreationDate;
                    string user = item.Committer;
                    foreach (Change changedItem in item.Changes)
                    {
                        string filename = changedItem.Item.ServerItem.Substring
                        (changedItem.Item.ServerItem.LastIndexOf('/') + 1);
// Your choice of filters. In this case I was not interested in the below files.
                        if (!filename.EndsWith(".dll")
                            && !filename.EndsWith(".pdb")
                            && !filename.EndsWith(".csproj")
                             && !filename.EndsWith(".pubxml")
                              && !filename.EndsWith(".sln")
                               && !filename.EndsWith(".config")
                               && !filename.EndsWith(".log")
                            && filename.IndexOf(".") > -1
                            && changedItem.ChangeType.Equals(ChangeType.Edit))
                        {
                            if (!Convert.ToBoolean(args[3]))
                            {
                                filename = string.Format("{0} - {1} - {2} by {3}",
                                    filename,
                                    checkInDate.ToString("dd-MM-yyyy"),
                                    changedItem.ChangeType.ToString(),
                                    user);
                            }
                            if (!changedFiles.Contains(filename))
                            {
                                if (Convert.ToBoolean(args[4]))
                                    changedFiles.Add(changedItem.Item.ServerItem);
                                else
                                    changedFiles.Add(filename);
                            }
                        }
                    }
                }
                foreach (string file in changedFiles)
                    Console.WriteLine(file);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            if (changedFiles != null && changedFiles.Count > 0)
                Console.WriteLine
                ("-----------------------------------------\nTotal File count: " + 
                changedFiles.Count);
            Console.WriteLine
            ("-----------------------------------------\nPress any key to close");
            Console.ReadKey();
        }
    }
}

现在从您的Program.cs中,按如下方式调用您的TfsHelper

 class Program
    {
        static void Main(string[] args)
        {
           TfsChangedFiles();
        }
        private static void TfsChangedFiles()
        {

       //Arguments more indetails
     // args[0] // local repository path
     // args[1] // Change sheet #(you may go with change sheet number between 1-24000+ of a date) 
     // args[2] // your remote TFS collections URL
     // args[3] // true or false - get list of concatenated "File Name+Date+change Type"
     // args[4] // true or false - absolute path of the file? true, else only file name

            TfsHelper TFS = new TfsHelper();
            TFS.Init(@"C:\TfsWorkSpace", "1",
                "https://mysite.visualstudio.com/DefaultCollection",
                "true", "false")
        }
    }
© . All rights reserved.