Reputation: 34840
Given an instance of a LibGit2Sharp.Commit
how do I work out what Branch
that exists on
Upvotes: 1
Views: 261
Reputation: 67589
In fact this Commit may exist on many Branch
es. It could even be pointed at by Tag
s 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 Branch
es can be done this way:
IEnumerable<Branch> branches = localHeadRefs
.Select(reference => repo.Branches[reference.CanonicalName]);
Upvotes: 2