Search code examples
libgit2sharp

Retrieve firstparentonly on master using libgit2sharp


I want to retrieve commits from master first parent only using libgit2sharp, the equivalent command line statement is

git log --first-parent master --oneline

I know of the CommitFilter that I can use with QueryBy, like this

repo.Commits.QueryBy(new CommitFilter() { FirstParentOnly = true })

I'm getting commits on the branch that I'm currently on if I'm not on master. Can I limit the commit results to master, even when I'm on a different branch ?


Solution

  • I believe you are looking for the IncludeReachableFrom in the CommitFilter.

    You might want to also use ExcludeReachableFrom at the same time to remove the commits from the branch that you are currently on...

    using (var repo = new Repository("/Users/sushi/code/redux/playscript"))
    {
        var commitLog = repo.Commits.QueryBy(new CommitFilter() { FirstParentOnly = true, IncludeReachableFrom = "master" });
        foreach (var commit in commitLog)
        {
            Console.WriteLine($"{commit.Sha}");
            Console.ReadKey();
        }
    }