Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGit2Sharp get all commits since {Hash}

Is it possible to get all commits since a specified commit using LibGit2Sharp?

I’ve tried the following.. but it didn't work:

using ( var repo = new Repository( repositoryDirectory ) )
{
    //Create commit filter.
    var filter = new CommitFilter
    {
        SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Reverse,
        Since = repo.Refs
    };

    /*Not Working
    if (shaHashOfCommit.IsNotEmpty())
        filter.Since = shaHashOfCommit;
    */

    var commits = repo.Commits.QueryBy( filter );
}
like image 885
musium Avatar asked Feb 12 '23 00:02

musium


1 Answers

The code below should meet your expectations.

using (var repo = new Repository(repositoryDirectory))
{
    var c = repo.Lookup<Commit>(shaHashOfCommit);

    // Let's only consider the refs that lead to this commit...
    var refs = repo.Refs.ReachableFrom(new []{c});

   //...and create a filter that will retrieve all the commits...
    var cf = new CommitFilter
    {
        Since = refs,       // ...reachable from all those refs...
        Until = c           // ...until this commit is met
    };

    var cs = repo.Commits.QueryBy(cf);

    foreach (var co in cs)
    {
        Console.WriteLine("{0}: {1}", co.Id.ToString(7), co.MessageShort);
    }       
}
like image 91
nulltoken Avatar answered Feb 13 '23 21:02

nulltoken