1

The following StackOverflow entry explains how to get the latest content of a textual file from GitHub.com, by using libgit2sharp: How to get file's contents on Git using LibGit2Sharp?

But I need to input a date-time of perhaps a month ago, and get back the content as it was on that date-time. I thought I had a solution, but it fails before getting far enough:

// This C# fails after returning a few entries. After 10 minutes says out of memory.

IEnumerable<LogEntry> enumbLogEntries_LogEntry_qb = repo.Commits
  .QueryBy("articles/sql-database/sql-database-get-started.md");

foreach (LogEntry logEntry in enumbLogEntries_LogEntry_qb)
{
  Console.WriteLine(logEntry.Commit.Committer.When); // Date.

  // I hope to use logEntry.Target to get the blob of content, I think.
}

I am also trying with Octokit for .NET, but again I can only get the latest content. Any solution would be appreciated. I confess that heavy GIT terminology can make answer unintelligible to me.

Community
  • 1
  • 1
MightyPen
  • 131
  • 1
  • 5

1 Answers1

1

You could try something like this: Find the latest commit that exists at the relevant date/time (I'll call it dt below). Then, find the file within that commit and get its contents (if the file exists in that commit).

using LibGit2Sharp;
using System.IO;

DateTimeOffset dt = DateTimeOffset.Now;  // replace this with the desired D/T
var c = repo.Commits
    .Where(c_ => c_.Author.When < dt)
    .OrderByDescending(c_ => c_.Author.When)
    .FirstOrDefault()
    ;
if (c != null)
{
    Tree tree = c.Tree;
    Blob blob = (Blob)(tree["path/to/file"].Target);
    StreamReader reader = new StreamReader(blob.GetContentStream());
    // read the file with 'reader'
}
Jacob Robbins
  • 1,860
  • 14
  • 16