2

I'm trying to have my C# application go through all of the TFS git projects and search through the files for a string pattern. I start out with my connection this way.

 var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://tfs.domain.com/tfs"));
 tfs.EnsureAuthenticated();
 var gitSvc = tfs.GetService<GitRepositoryService>();

I'm not seeing a way to get all the git projects information to put it through a foreach loop. I saw some code from here that will get all projects from tfs.Getservice.

 var tfs = TfsTeamProjectCollectionFactory
         .GetTeamProjectCollection(new Uri("http://{tfsserver}:8080/tfs"));

        tfs.EnsureAuthenticated();

        var versionControl = tfs.GetService<VersionControlServer>();


        StreamWriter outputFile = new StreamWriter(@"C:\Find.txt");
        var allProjs = versionControl.GetAllTeamProjects(true);
        foreach (var teamProj in allProjs)
        {
            foreach (var filePattern in filePatterns)
            {
                var items = versionControl.GetItems(teamProj.ServerItem + "/" + filePattern, RecursionType.Full).Items
                            .Where(i => !i.ServerItem.Contains("_ReSharper"));  //skipping resharper stuff
                foreach (var item in items)
                {
                    List<string> lines = SearchInFile(item);
                    if (lines.Count > 0)
                    {
                        outputFile.WriteLine("FILE:" + item.ServerItem);
                        outputFile.WriteLine(lines.Count.ToString() + " occurence(s) found.");
                        outputFile.WriteLine();
                    }
                    foreach (string line in lines)
                    {
                        outputFile.WriteLine(line);
                    }
                    if (lines.Count > 0)
                    {
                        outputFile.WriteLine();
                    }
                }
            }
            outputFile.Flush();
        }
    }

How can I do something similar like the code above, but by using Team Foundations Git library?

Bryan Mcneil
  • 395
  • 2
  • 14
  • What version of TFS are you using? TFS 2017 supports code search. – Daniel Mann Aug 29 '17 at 22:18
  • 1
    It would likely be much more efficient to just clone the repository (using the command line or using the LibGit2Sharp library) and search through the files (either looking at the contents on disk or - again - using LibGit2Sharp). – Edward Thomson Aug 30 '17 at 11:40

1 Answers1

1

There is a PowerShell sample to get the Git Project, you can try converting it to C# code. Reference this thread: https://social.msdn.microsoft.com/Forums/office/en-US/ce12ede3-8b14-49de-962a-20e68e0a6d00/how-to-use-microsoftteamfoundationgitclient-in-powershell-script?forum=tfsgeneral

Recommend you to use the Code Search if your are using TFS 2017, see Set up and administer Microsoft Code Search/Work Item Search for more information.

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55