How easy is it to get all Refs for a given Commit?

Is there an easy way to find all the links (e.g. tags) for a given Commit? For instance:

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

In the code below, all links that can reach the selected commit are retrieved.

 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 get only Tag s, the method provides 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 links that either directly indicate the selected commit or point to the ancestor of this commit. In this sense, it behaves similarly to the git rev-list command.

+1
source

As @ user1130329 noted, viewing all the links several times can be very expensive. As an alternative, this is what I did 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); }); 
0
source

Source: https://habr.com/ru/post/1273336/


All Articles