Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore merge commits using libgit2sharp?

I need to get a list of commits without the auto-merged commits done by Git. How can this be done using libgit2sharp package?

like image 271
juvchan Avatar asked Mar 18 '23 14:03

juvchan


1 Answers

A merge commit is a commit with more than one parent.

In order to do this with Git, one would issue, for instance, the following command which would list all the commits reachable by HEAD which are not merge commits.

git log --no-merges HEAD

Where --no-merges is documented as "Do not print commits with more than one parent. This is exactly the same as --max-parents=1.".


One can do the same thing with LibGit2Sharp with the following piece of code:

using (var repo = new Repository(path))
{
    var allCommitsReachableByHead = repo.Commits;

    const string RFC2822Format = "ddd dd MMM HH:mm:ss yyyy K";

    foreach (var c in allCommitsReachableByHead)
    {
        if (c.Parents.Count() > 1)
        {
            continue;
        }

        Console.WriteLine("Author: {0} <{1}>", c.Author.Name, c.Author.Email);
        Console.WriteLine("Date:   {0}", c.Author.When.ToString(RFC2822Format, CultureInfo.InvariantCulture));
        Console.WriteLine();
        Console.WriteLine(c.Message);
        Console.WriteLine();
    }
}
like image 141
nulltoken Avatar answered Apr 29 '23 03:04

nulltoken