I need to get a list of commits without the auto-merged commits done by Git. How can this be done using libgit2sharp package?
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With