Reputation: 36443
So I am working on a system to determine the changes since the last tag.
Git offers the git describe --tag
command which will:
The command finds the most recent tag that is reachable from a commit.
https://git-scm.com/docs/git-describe
However, I am using Libgit2sharp to do some automation and I am looking for the equivalent functionality however I am not having much luck.
I tried:
var result = repo.Refs.ReachableFrom(
repo.Refs.Where(r => r.IsTag),
new[] { repo.Head.Tip });
But this didn't yield any results (I am pretty use I am doing it wrong)
Upvotes: 1
Views: 98
Reputation: 36443
This is my current naive approach. I am open to criticism or better solutions. The commit log is ordered and therefore I just find the nearest tag targeting a commit and use that.
In my case I always using HEAD as I am looking for the last tag at the tip of HEAD.
Tag? GetLastTag(Repository repo)
{
var commitsToHead = repo.Head.Commits;
var tagsOnCommits = repo.Tags.Where(t => t.Target.GetType() == typeof(Commit)).ToList();
foreach (var commit in commitsToHead)
{
var foundTags = tagsOnCommits.Where(x => x.Target.Peel<Commit>().Id == commit.Id).ToArray();
if (foundTags.Length <= 0)
{
continue;
}
if (foundTags.Length > 1)
{
Console.WriteLine($"Found more than one tag for the commit {commit}");
}
return foundTags[0];
}
return null;
}
Upvotes: 0