8000 Add extension methods to determine reference kind · crashsplash/libgit2sharp@1465bf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1465bf3

Browse files
Saamannulltoken
authored andcommitted
Add extension methods to determine reference kind
1 parent af00576 commit 1465bf3

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,5 +739,21 @@ private static T[] SortedRefs<T>(IRepository repo, Func<Reference, T> selector)
739739
{
740740
return repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
741741
}
742+
743+
[Fact]
744+
public void CanIdentifyReferenceKind()
745+
{
746+
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
747+
{
748+
Assert.True(repo.Refs["refs/heads/master"].IsLocalBranch());
749+
Assert.True(repo.Refs["refs/remotes/origin/master"].IsRemoteTrackingBranch());
750+
Assert.True(repo.Refs["refs/tags/lw"].IsTag());
751+
}
752+
753+
using (var repo = new Repository(BareTestRepoPath))
754+
{
755+
Assert.True(repo.Refs["refs/notes/commits"].IsNote());
756+
}
757+
}
742758
}
743759
}

LibGit2Sharp/ReferenceExtensions.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,45 @@ private static bool IsPrefixedBy(this string input, string prefix)
3131
{
3232
return input.StartsWith(prefix, StringComparison.Ordinal);
3333
}
34+
35+
/// <summary>
36+
/// Determine if the current <see cref="Reference"/> is a local branch.
37+
/// </summary>
38+
/// <param name="reference">The <see cref="Reference"/> to test.</param>
39+
/// <returns>true if the current <see cref="Reference"/> is a local branch, false otherwise.</returns>
40+
public static bool IsLocalBranch(this Reference reference)
41+
{
42+
return reference.CanonicalName.LooksLikeLocalBranch();
43+
}
44+
45+
/// <summary>
46+
/// Determine if the current <see cref="Reference"/> is a remote tracking branch.
47+
/// </summary>
48+
/// <param name="reference">The <see cref="Reference"/> to test.</param>
49+
/// <returns>true if the current <see cref="Reference"/> is a remote tracking branch, false otherwise.</returns>
50+
public static bool IsRemoteTrackingBranch(this Reference reference)
51+
{
52+
return reference.CanonicalName.LooksLikeRemoteTrackingBranch();
53+
}
54+
55+
/// <summary>
56+
/// Determine if the current <see cref="Reference"/> is a tag.
57+
/// </summary>
58+
/// <param name="reference">The <see cref="Reference"/> to test.</param>
59+
/// <returns>true if the current <see cref="Reference"/> is a tag, false otherwise.</returns>
60+
public static bool IsTag(this Reference reference)
61+
{
62+
return reference.CanonicalName.LooksLikeTag();
63+
}
64+
65+
/// <summary>
66+
/// Determine if the current <see cref="Reference"/> is a note.
67+
/// </summary>
68+
/// <param name="reference">The <see cref="Reference"/> to test.</param>
69+
/// <returns>true if the current <see cref="Reference"/> is a note, false otherwise.</returns>
70+
public static bool IsNote(this Reference reference)
71+
{
72+
return reference.CanonicalName.LooksLikeNote();
73+
}
3474
}
3575
}

0 commit comments

Comments
 (0)
0