What's the fastest way to find Tags pointing to Commits?

后端 未结 2 602
北荒
北荒 2021-01-05 06:50

With libgit2sharp I would like to do the following:

foreach( Commit commit in repo.Commits )
{
    // How to implement assignedTags?
    foreach( Tag tag in          


        
2条回答
  •  悲&欢浪女
    2021-01-05 07:20

    Here is another version of nulltoken's answer but with using ILookupclass instead of dictionary. A bit nicer IMO:

    private static ILookup CreateCommitIdToTagLookup(Repository repo)
    {
        var commitIdToTagLookup =
            repo.Tags
            .Select(tag => new { Commit = tag.PeeledTarget as Commit, Tag = tag })
            .Where(x => x.Commit != null)
            .ToLookup(x => x.Commit.Id, x => x.Tag);
    
        return commitIdToTagLookup;
    }
    

    and simple usage example:

    using (var repo = new Repository("Path/to/your/repo"))
    {
        var commitIdToTagLookup = CreateCommitIdToTagLookup(repo);
    
        foreach (var commit in repo.Commits)
        {
            foreach (var tag in commitIdToTagLookup[commit.Id])
            {
                Console.WriteLine($"Tag {tag.FriendlyName} points at {commit.Id}");
            }
        }
    }
    

提交回复
热议问题