Pergunta

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

Foi útil?

Solução

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]);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top