Looping through every commit in Git repository with LibGit2Sharp?
-
06-06-2021 - |
Question
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?
Solution
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 :-)