How to obtain number of commits behind/ahead with libgit2sharp?
Asked Answered
O

2

5

It's possible to obtain the number of commits behind/ahead using git rev-list command. I am trying to achieve the same thing using libgit2sharp library but the library is not fully documented so I couldn't find how to.

I'm looking for an example for obtaining behind/ahead commit numbers with libgit2sharp.

Orchestrate answered 3/10, 2017 at 11:37 Comment(1)
If you just want to get the commits behind/ahead between a local branch and its tracked branch, you could just use the Branch.TrackingDetails property, which behind the scene uses the HistoryDivergence class that Jason Haslam is refering to in his answer.Damnation
B
5

Completing the answer given by Jason Haslam... This is an example of how to use HistoryDivergence to get the number of commits ahead and behind of each branch:

using (var repo = new Repository("/path/to/repo"))
{
     foreach (Branch b in repo.Branches)
     {
               // if branch does not have a remote b.TrackingDetails.AheadBy and b.TrackingDetails.BehindBy will be both null
               var commitsAhead = b.TrackingDetails.AheadBy;
               var commitsBehind = b.TrackingDetails.BehindBy;
               Console.WriteLine($"Branch {b.FriendlyName} is {commitsAhead} ahead and {commitsBehind} behind");
      }
}
Buoyant answered 16/10, 2017 at 8:53 Comment(0)
P
2

Look at the HistoryDivergence class. It adapts the git_graph_ahead_behind function from libgit2.

Planogamete answered 4/10, 2017 at 4:31 Comment(2)
Constructor is declared internal, so how can it be used?Amoral
See the accepted answer. It is likely more correct.Planogamete

© 2022 - 2024 — McMap. All rights reserved.