Search code examples
c#linqlibgit2sharp

Libgit2sharp Iterate commits from newest to oldest


I want to iterate through all changesets of a repository. I want to avoid to first read all commits in the ram and then iterate through then

Currently, I've got this method

   foreach (LibGit2Sharp.Commit commit in repo.Commits)
   {
        return Transform(commit);
   }

I know that I could do this:

foreach (LibGit2Sharp.Commit commit in repo.Commits.OrderByDescending(i=>i.Committer.When))

but then it reads all commits and then linq reorders them afterward.

Isn't there any way to let libgit2sharp iterate in a reversed order?


Solution

  • You can create a commit Filter and use it to sort the commits by:

    • Reverse
    • Time
    • Topological

    Example:

    var repo = new LibGit2Sharp.Repository("/Users/sushi/code/sushi/Xamarin.PlayScript.Starling");
    var filter = new CommitFilter()
    {
        SortBy = CommitSortStrategies.Reverse 
    };
    IEnumerable<Commit> commits = repo.Commits.QueryBy(filter);
    foreach (var commit in commits)
    {
        Console.WriteLine(commit.Committer.When);
    }