Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through every commit in Git repository with LibGit2Sharp?

I tried looping through new LibGit2Sharp.Repository(path).Commits, but it seems it only goes through the commits in the master (or I guess wherever the HEAD points to?).

How would I loop through every commit in the repository, regardless of what branch it is in and loop through them in order of when the commits occurred (date)?

I'm doing this for a purpose like git log, to list commits regardless of the branches, in order of date.

Bonus points if I could also loop through the commits that are not referenced (i.e. if you git reset away from some commits and the commits are no longer referenced and about to get gc'd).

Thoughts?

like image 918
Tower Avatar asked Dec 06 '25 15:12

Tower


1 Answers

Support for git log --all should be achievable with the following syntax

using (var repo = new Repository(yourRepoPath))
{
      var commits = repo.Commits.QueryBy(new Filter { Since = repo.Refs });
}

The Filter also exposes a SortBy property to control the ordering of the results. Default sorting (GitSortOptions.Time) will output the most recent commits first.

Bonus points if I could also loop through the commits that are not referenced

There's no way to access the commits in the reflog nor the dangling one (yet?). Then, no bonus points, I guess :-)

like image 173
nulltoken Avatar answered Dec 08 '25 04:12

nulltoken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!