git log --first-parent #258

Closed
pszittyay opened this Issue Nov 27, 2012 · 5 comments

Projects

None yet

3 participants

@pszittyay

I need to get the same commits as the command git log --first-parent.
Thanks

@pszittyay

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)
  {
   shaCommits.Add(commit.Sha);
   nextSha = commit.Parents.First().Sha;
  }
}
@pszittyay pszittyay closed this Dec 6, 2012
@nulltoken
libgit2 member

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.

/cc @carlosmn

@nulltoken nulltoken reopened this Dec 6, 2012
@pszittyay

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.

@carlosmn
libgit2 member

The parents are a sorted set of commits and always have the same ordering. A different order would make it a different commit.

@nulltoken
libgit2 member

@carlosmn made this available in libgit2/libgit2#1838

@carlosmn carlosmn added a commit that referenced this issue Dec 10, 2013
@carlosmn carlosmn 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.
bbc0cee
@carlosmn carlosmn added a commit that referenced this issue Dec 11, 2013
@carlosmn carlosmn 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.
d001b2a
@carlosmn carlosmn added a commit that closed this issue Dec 11, 2013
@carlosmn carlosmn 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.
01d105c
@carlosmn carlosmn closed this in 01d105c Dec 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment