Search code examples
c#.nettfs-2015visual-studio-2019tfs-sdk

Querying TFS 2015 Source Code Repository DateTime Properties with .Net Framework v4.8 and Visual Studio 2019


I am trying to filter for source control files that were either created or modified within a specific time period on particular Team Foundation Server 2015 branches. I am thus far able to access file properties (e.g. url) with the Microsoft.VisualStudio.Services.WebAPI and Microsoft.TeamFoundation.SourceControl.WebApi libraries with a C# .Net Framework 4.8 Console Application using the GitHttpClient class.

The GetItemsAsync() method of this class returns a list of "GitItems" that contain a "path" property that can be passed as an argument into the System.IO class FileInfo to instantiate an object with the properties I need: CreationTime and LastWriteTime. However, the GitItem objects do not include the full file (blob) path that FileInfo (as well as the class File) needs to generate these properties accurately. The path property only includes the file name (e.g. '/.gitignore'). Therefore, in the code below, the variable lastWriteTime and the CreationTime property both return '12/31/1600 7:00:00 PM,' since the path isn't recognized.

    static void Main(string[] args)
    {
        VssCredentials creds = new VssClientCredentials();
        creds.Storage = new VssClientCredentialStorage();

        VssConnection connection = new VssConnection(new Uri(teamCollection), creds);

        // Get a GitHttpClient to talk to the Git endpoints
        GitHttpClient gitClient = connection.GetClient<GitHttpClient>();

        // Get data about a specific repository
        var repositories = gitClient.GetRepositoriesAsync(teamProject).Result;

        GitVersionDescriptor descriptor = new GitVersionDescriptor()
        {
            VersionType = GitVersionType.Branch,
            Version = "develop",
            VersionOptions = GitVersionOptions.None
        };

        foreach (var repository in repositories)
        {

            var branches = gitClient.GetBranchesAsync(repository.Id).Result;
            var items = gitClient.GetItemsAsync(repository.Id, recursionLevel: VersionControlRecursionType.Full, versionDescriptor: descriptor, includeContentMetadata: true).Result;

            foreach (var item in items)
            {
                var fullPath = Path.GetFullPath(item.Path);
                FileInfo file = new FileInfo(fullPath);
                DateTime lastWriteTime = file.LastWriteTime;
            }
            Console.WriteLine(repository.Name);
        }
    }
}

}


Solution

  • According to your code, you are using GitHttpClient.GetItemsAsync method.

    public Task<GitItemsCollection> GetItemsAsync(
        Guid repositoryId,
        string path,
        GitVersionDescriptor version,
        VersionControlRecursionType recursionLevel,
        bool includeContentMetadata,
        bool includeLatestChange,
        Object userState
    )
    

    This will return a server side git path. File info class with LastWriteTime properties Gets or sets the time when the current file or directory was last written to. This should be a local system path.

    That's why the path isn't recognized. Which may return a date kind of '12/31/1600 7:00:00 PM,'

    Your question is similar to this VSTS API - repository creation date

    Don't think it is possible to get the exact date of the moment the operation create repo was completed. However, logically the birthday of the repository is usually considered its first commit date.

    If that's what you're looking for, you can achieve your goal with a usual Git command:

    git log -1 --reverse --format="format:%ci"
    

    Besides, you could also get a git commit with detail info through Rest API. Also take a look at this blog, which maybe helpful.