列出TFS中已更改的文件,但按应用的更改数量排序

时间:2012-07-25 08:46:05

标签: version-control tfs

是否有办法列出项目中已更改的所有文件,但是按每个文件所做的更改次数排序?

我想进行代码审核,但只能选择最活跃的文件。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Excel作为TFS报告工具,如此博文:

http://www.woodwardweb.com/vsts/getting_started.html

PS。我在this question找到了该链接。

答案 1 :(得分:0)

我搜索了不同的方式,最后我发现最好的方法是使用TFS API 这是代码:

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 VControl
{
    class Program
    {
        class SourceElement
        {
            public string filename;
            public int numberOfModification;

        }
        static void Main(string[] args)
        {
             TfsTeamProjectCollection projectCollection = new 
            TfsTeamProjectCollection(new Uri("http://server:8080/tfs/ProjectCollection/"),
                new System.Net.NetworkCredential("username", "password"));
              projectCollection.EnsureAuthenticated();
            Workspace workspace = null;
            Boolean createdWorkspace = false;
            String newFolder = String.Empty;
             VersionControlServer versionControl = projectCollection.GetService<VersionControlServer>();
             var teamProjects = new List<TeamProject>(versionControl.GetAllTeamProjects(false));
             String workspaceName = String.Format("{0}-{1}", Environment.MachineName, "TestWorkspace");
            try
            {
                workspace = versionControl.GetWorkspace(workspaceName, versionControl.AuthorizedUser);
            }
            catch (WorkspaceNotFoundException)
            {
                workspace = versionControl.CreateWorkspace(workspaceName, versionControl.AuthorizedUser);
                createdWorkspace = true;
            }
            var serverFolder = String.Format("$/{0}", teamProjects[0].Name) + "/solutionFolder/";
            var localFolder = Path.Combine(Path.GetTempPath(), "localFolder") + "/solutionFolder/";
            var workingFolder = new WorkingFolder(serverFolder, localFolder);
            // Create a workspace mapping.
            workspace.CreateMapping(workingFolder);
            if (!workspace.HasReadPermission)
            {
                throw new SecurityException(
                    String.Format("{0} does not have read permission for {1}",
                    versionControl.AuthorizedUser, serverFolder));
            }

            // Get the files from the repository.
            workspace.Get();
            string[] directories = Directory.GetDirectories(workspace.Folders[0].LocalItem);
            FileStream outputFile = new FileStream("result.txt", FileMode.Create);
            StreamWriter  writer = new StreamWriter(outputFile);     
            List<SourceElement> fileLiset = new List<SourceElement>();
            foreach (string dir in directories)
            {
                foreach (string file in Directory.GetFiles(dir))
                {
                    string filenamae = System.IO.Path.GetFileName(file);
                    Item source = versionControl.GetItem(file);
                    System.Collections.IEnumerable history = versionControl.QueryHistory(file,
                    VersionSpec.Latest, 0, RecursionType.Full, null, null, null, 300, true, true, false, false);
                    int numberOfModification = 0;
                    foreach (var item in history)
                        numberOfModification++;
                    SourceElement fileElement = new SourceElement();
                    fileElement.filename = filenamae;
                    fileElement.numberOfModification = numberOfModification;
                     fileLiset.Add(fileElement);
                }
                }
         var sortedList = fileLiset.OrderBy(x=> x.numberOfModification);
                    // Loop through keys.
            foreach (var key in sortedList)
            {
                writer.WriteLine("{0}: {1}", key.filename, key.numberOfModification);
            }
            writer.Close();
        }
    }
 }