How to get all git commits with libgit2sharp, regardless the branch?

1k views Asked by At

AFAIK, Repository.Commits property return all commits reachable from current branch.

I would like to get all possible commits, regardless the branch. I am using the following command :

var commitsToRewrite = repository.Branches.SelectMany(x => x.Commits)
                .GroupBy(x => x.Sha)
                .Select(x => x.First())
                .ToArray();

It is slow but it seems to work (maybe I missed some special cases that are not covered). Is this the right way to do ? Is there a more efficient, faster way ?

1

There are 1 answers

0
Oscar Cero Uno On

Maybe not your case, but in some rare cases i've found that only traversing all branches could skip some commits (may be cause of branch deletion). This piece of code seems to do a better job (hope so), and as a plus is faster and less memory intensive.

var commitsToRewrite = repository.Commits.QueryBy(new CommitFilter {IncludeReachableFrom = repository.Refs.ToList()})
                        .Distinct<Commit>(EqualityComparer<GitObject>.Default)
                        .ToList();

I tested this with ReactOS git repo having more than 85000 commits and 500Mb.