使用LibGit2Sharp返回自特定日期以来的所有git日志

时间:2013-08-30 02:50:43

标签: libgit2sharp

我正在使用Lib2GitSharp将Git日志与来自其他系统的信息一起加入,我希望能够在特定日期(或日期之间)检索所有git日志。

我在querying the log here上找到了文档,但似乎没有办法按日期查询日志。

等同于什么
git log --since="2013-08-20"

在LibGit2Sharp?

修改 这似乎有用,但也许有更好和/或更优雅的方式?

using (var repo = new Repository(options.Repo))
{
    var since = new DateTime(2013, 8, 20);
    var commits = repo.Commits.Where(c => c.Committer.When.CompareTo(since) > 0);
    foreach (Commit commit in commits)
    {
        Console.WriteLine("A commit happened at " + commit.Committer.When.ToLocalTime());
    }
}

1 个答案:

答案 0 :(得分:2)

使用Linq我可以查询存储库中的每个提交对象的日期,并只返回那些数据大于指定数据的对象。我不确定是否有更干净,更快,更优雅的方式,但这确实有效。

using (var repo = new Repository(options.Repo))
{
    var since = new DateTimeOffset(new DateTime(2013, 8, 20));
    var filter = new CommitFilter { Since = repo.Branches };
    var commitLog = repo.Commits.QueryBy(filter);
    var commits = commitLog.Where(c => c.Committer.When > since);
    foreach (Commit commit in commits)
    {
        Console.WriteLine("A commit happened at " + commit.Committer.When.ToLocalTime());
    }
}

编辑:来自nulltoken

的建议
相关问题