【问题标题】:How to get Older content of a file using libgit2sharp, or Octokit?如何使用 libgit2sharp 或 Octokit 获取文件的旧内容?
【发布时间】:2016-06-22 02:44:25
【问题描述】:

以下 StackOverflow 条目说明了如何使用 libgit2sharp 从 GitHub.com 获取文本文件的最新内容: How to get file's contents on Git using LibGit2Sharp?

但我需要输入一个大概一个月前的日期时间,然后取回该日期时间的内容。我以为我有一个解决方案,但还没走多远就失败了:

// 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.
}

我也在尝试使用 Octokit for .NET,但我只能获得最新的内容。任何解决方案将不胜感激。我承认沉重的 GIT 术语会让我无法理解答案。

【问题讨论】:

    标签: git libgit2sharp octokit


    【解决方案1】:

    您可以尝试这样的事情:查找在相关日期/时间存在的最新提交(我将在下面将其称为 dt)。然后,在该提交中找到该文件并获取其内容(如果该文件存在于该提交中)。

    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'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 2013-10-17
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多