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

1

1

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 ?

Indefeasible answered 30/5, 2021 at 17:16 Comment(0)
P
2

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.

Peder answered 28/7, 2021 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.