How to easily get all Refs for a given Commit?

2019-09-19 06:57发布

问题:

Is there an easy way to find all References (e.g. Tags) for a given Commit? For example:

using( Repository repo = new Repository( path_to_repo ) )
{
    foreach( Commit commit in repo.Commits )
    {
        List<Reference> assignedRefs = commit.Refs; // <- how to do this?
    }
}

回答1:

The code below retrieves all the references that can reach the selected commit.

using( Repository repo = new Repository( path_to_repo ) )
{
    foreach( Commit commit in repo.Commits )
    {
        IEnumerable<Reference> refs =
               repo.Refs.ReachableFrom(new[] { commit });
    }
}

If you want to only retrieve Tags, the method exposes an overload to help you with this.

using( Repository repo = new Repository( path_to_repo ) )
{
    var allTags = repo.Refs.Where(r => r.IsTag()).ToList();

    foreach( Commit commit in repo.Commits )
    {
        IEnumerable<Reference> refs =
               repo.Refs.ReachableFrom(allTags, new[] { commit });
    }
}

Note: This code will retrieve all refs that either directly point to the chosen commit or point to an ancestor of this commit. In this sense, it behaves similarly to the git rev-list command.



回答2:

As @user1130329 pointed out, going through all refs multiple times can be very expensive. Just as an alternative, this is what I have done here for a similar case:

logs = _repo.Commits
    .QueryBy(filter)
    .Select(c => new LogEntry
    {
        Id = c.Id.Sha,
        // other fields
    })
    .ToList();

var logsDictionary = logs.ToDictionary(d => d.Id);

_repo.Refs
    .Where(r => r.IsTag || r.IsLocalBranch)
    .ForEach(r =>
    {
        if (r.IsTag) logsDictionary.GetValueOrDefault(r.TargetIdentifier)?.Tags.Add(r.CanonicalName);
        if (r.IsLocalBranch) logsDictionary.GetValueOrDefault(r.TargetIdentifier)?.Heads.Add(r.CanonicalName);
    });