Simon
Simon

Reputation: 34840

How to resolve Branch from LibGit2Sharp.Commit?

Given an instance of a LibGit2Sharp.Commit how do I work out what Branch that exists on

Upvotes: 1

Views: 261

Answers (1)

nulltoken
nulltoken

Reputation: 67589

In fact this Commit may exist on many Branches. It could even be pointed at by Tags or by the Stash.

If one want to know all the references that lead to a specific commit, the .ReachableFrom() method may fit just that need:

IEnumerable<Reference> refs = repo.Refs.ReachableFrom(new[] { my_commit });

Should you want to limit the retrieved the references being searched to the local heads only, an overload of this methods accepts a subset of references to work with:

IEnumerable<Reference> localHeadRefs = repo.Refs.ReachableFrom(
    repo.Refs.Where(r => r.IsLocalBranch()),
    new[] { my_commit });

Then, retrieving a list of Branches can be done this way:

IEnumerable<Branch> branches = localHeadRefs
    .Select(reference => repo.Branches[reference.CanonicalName]);

Upvotes: 2

Related Questions