How to easily get all Refs for a given Commit?

左心房为你撑大大i 提交于 2019-12-12 06:38:23

问题


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);
    });


来源:https://stackoverflow.com/questions/22552138/how-to-easily-get-all-refs-for-a-given-commit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!