8000 Add Tag.PeeledTarget property · GiTechLab/libgit2sharp@dbf7fc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbf7fc8

Browse files
committed
Add Tag.PeeledTarget property
Per issue libgit2#551, a PeeledTarget property has been added to Tag. When the tag is pointing to a chained TagAnnotation, it will return the final GitObject that the chain points to. closes libgit2#551
1 parent 4d41aba commit dbf7fc8

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using LibGit2Sharp.Core;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7+
using Xunit.Extensions;
78

89
namespace LibGit2Sharp.Tests
910
{
@@ -740,6 +741,35 @@ public void LookupNullTagNameThrows()
740741
}
741742
}
742743

744+
[Fact]
745+
public void CanRetrieveThePeeledTargetOfATagPointingToATag()
746+
{
747+
string path = SandboxBareTestRepo();
748+
using (var repo = new Repository(path))
749+
{
750+
Tag tag = repo.Tags["test"];
751+
752+
Assert.True(tag.Target is TagAnnotation);
753+
Assert.True(tag.PeeledTarget is Commit);
754+
}
755+
}
756+
757+
[Theory]
758+
[InlineData("e90810b")]
759+
[InlineData("lw")]
760+
[InlineData("point_to_blob")]
761+
[InlineData("tag_without_tagger")]
762+
public void PeeledTargetAndTargetAreEqualWhenTagIsNotChained(string tagName)
763+
{
764+
string path = SandboxBareTestRepo();
765+
using (var repo = new Repository(path))
766+
{
767+
Tag tag = repo.Tags[tagName];
768+
769+
Assert.Equal<GitObject>(tag.Target, tag.PeeledTarget);
770+
}
771+
}
772+
743773
private static T[] SortedTags<T>(IEnumerable<Tag> tags, Func<Tag, T> selector)
744774
{
745775
return tags.OrderBy(t => t.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();

LibGit2Sharp/Tag.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ public virtual GitObject Target
4040
}
4141
}
4242

43+
/// <summary>
44+
/// Gets the peeled <see cref="GitObject"/> that this tag points to.
45+
/// </summary>
46+
public virtual GitObject PeeledTarget
47+
{
48+
get
49+
{
50+
GitObject target = TargetObject;
51+
52+
var annotation = target as TagAnnotation;
53+
54+
while (annotation != null)
55+
{
56+
target = annotation.Target;
57+
annotation = target as TagAnnotation;
58+
}
59+
60+
return target;
61+
}
62+
}
63+
4364
/// <summary>
4465
/// Indicates whether the tag holds any metadata.
4566
/// </summary>

0 commit comments
Comments
 (0)

0