I need to get the same commits as the command git log --first-parent.
I have managed to achieve this by iterating all the commits and obtaining the first parent. Here is the code block:
Repository repo = new Repository(RepositoryLocation);
var commits = repo.Head.Commits;
//List of sha that would be obtained by git log --first-parent
List<string> shaCommits = new List<string>();
string nextSha = commits.First().Sha;
foreach (var commit in commits)
if (nextSha == commit.Sha)
nextSha = commit.Parents.First().Sha;
Hey @pszittyay, I'm glad you found a way to fulfill your need. Thanks for sharing your solution, as well!
FWIW, some information on that topic can be found this post By Junio C. Hamano.
I'm going to keep this open as I'd like this to be handled by LibGit2Sharp. However, this might require some time as the libgit2 revwalk.c code would certainly require some tweaking.
Hey @carlosmn, my solution is based on the fact that the first element of the Parent collection should always be "the parent". I could not find any documentation saying that this behaviour is expected but it happens to be so. If this changes in future releases, this solution will not work anymore.
The parents are a sorted set of commits and always have the same ordering. A different order would make it a different commit.
@carlosmn made this available in libgit2/libgit2#1838
CommitFilter: add simplify-by-first-parent option
Now that this is doable directly inside the revision walker, let's
expose it as a possible filter option.
This fixes #258.