Skip to content

Commit 53b66be

Browse files
committed
Bind the missing refspec methods
These are actions, so they need the handle which we previously made the code keep around.
1 parent 4bb5f5f commit 53b66be

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

LibGit2Sharp.Tests/RefSpecFixture.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,43 @@ public void SettingInvalidRefSpecsThrows(string refSpec)
190190
Assert.Equal(oldRefSpecs, newRemote.RefSpecs.Select(r => r.Specification).ToList());
191191
}
192192
}
193+
194+
[Theory]
195+
[InlineData("refs/heads/master", true, false)]
196+
[InlineData("refs/heads/some/master", true, false)]
197+
[InlineData("refs/remotes/foo/master", false, true)]
198+
[InlineData("refs/tags/foo", false, false)]
199+
public void CanCheckForMatches(string reference, bool shouldMatchSource, bool shouldMatchDest)
200+
{
201+
var path = SandboxStandardTestRepo();
202+
using (var repo = InitIsolatedRepository(path))
203+
{
204+
var remote = repo.Network.Remotes.Add("foo", "blahblah", "refs/heads/*:refs/remotes/foo/*");
205+
var refspec = remote.RefSpecs.Single();
206+
207+
Assert.Equal(shouldMatchSource, refspec.SourceMatches(reference));
208+
Assert.Equal(shouldMatchDest, refspec.DestinationMatches(reference));
209+
}
210+
}
211+
212+
[Theory]
213+
[InlineData("refs/heads/master", "refs/remotes/foo/master")]
214+
[InlineData("refs/heads/bar/master", "refs/remotes/foo/bar/master")]
215+
[InlineData("refs/heads/master", "refs/remotes/foo/master")]
216+
public void CanTransformRefspecs(string lhs, string rhs)
217+
{
218+
var path = SandboxStandardTestRepo();
219+
using (var repo = InitIsolatedRepository(path))
220+
{
221+
var remote = repo.Network.Remotes.Add("foo", "blahblah", "refs/heads/*:refs/remotes/foo/*");
222+
var refspec = remote.RefSpecs.Single();
223+
224+
var actualTransformed = refspec.Transform(lhs);
225+
var actualReverseTransformed = refspec.ReverseTransform(rhs);
226+
227+
Assert.Equal(rhs, actualTransformed);
228+
Assert.Equal(lhs, actualReverseTransformed);
229+
}
230+
}
193231
}
194232
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,12 @@ internal static extern IntPtr git_reflog_entry_committer(
11121112
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
11131113
internal static extern string git_reflog_entry_message(SafeHandle entry);
11141114

1115+
[DllImport(libgit2)]
1116+
internal static extern int git_refspec_transform(
1117+
GitBuf buf,
1118+
GitRefSpecHandle refspec,
1119+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
1120+
11151121
[DllImport(libgit2)]
11161122
internal static extern int git_refspec_rtransform(
11171123
GitBuf buf,
@@ -1139,6 +1145,16 @@ internal static extern string git_refspec_src(
11391145
[DllImport(libgit2)]
11401146
internal static extern bool git_refspec_force(GitRefSpecHandle refSpec);
11411147

1148+
[DllImport(libgit2)]
1149+
internal static extern bool git_refspec_src_matches(
1150+
GitRefSpecHandle resfpec,
1151+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string reference);
1152+
1153+
[DllImport(libgit2)]
1154+
internal static extern bool git_refspec_dst_matches(
1155+
GitRefSpecHandle resfpec,
1156+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string reference);
1157+
11421158
[DllImport(libgit2)]
11431159
internal static extern int git_remote_autotag(RemoteSafeHandle remote);
11441160

LibGit2Sharp/Core/Proxy.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,17 @@ public static string git_reflog_entry_message(SafeHandle entry)
20172017

20182018
#region git_refspec
20192019

2020+
public static string git_refspec_transform(GitRefSpecHandle refSpecPtr, string name)
2021+
{
2022+
using (var buf = new GitBuf())
2023+
{
2024+
int res = NativeMethods.git_refspec_transform(buf, refSpecPtr, name);
2025+
Ensure.ZeroResult(res);
2026+
2027+
return LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty;
2028+
}
2029+
}
2030+
20202031
public static string git_refspec_rtransform(GitRefSpecHandle refSpecPtr, string name)
20212032
{
20222033
using (var buf = new GitBuf())
@@ -2053,6 +2064,16 @@ public static bool git_refspec_force(GitRefSpecHandle refSpec)
20532064
return NativeMethods.git_refspec_force(refSpec);
20542065
}
20552066

2067+
public static bool git_refspec_src_matches(GitRefSpecHandle refspec, string reference)
2068+
{
2069+
return NativeMethods.git_refspec_src_matches(refspec, reference);
2070+
}
2071+
2072+
public static bool git_refspec_dst_matches(GitRefSpecHandle refspec, string reference)
2073+
{
2074+
return NativeMethods.git_refspec_dst_matches(refspec, reference);
2075+
}
2076+
20562077
#endregion
20572078

20582079
#region git_remote_

LibGit2Sharp/RefSpec.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ public virtual bool ForceUpdate
8282
}
8383
}
8484

85+
/// <summary>
86+
/// Check whether the given reference matches the source (lhs) part of
87+
/// this refspec.
88+
/// </summary>
89+
/// <param name="reference">The reference name to check</param>
90+
public virtual bool SourceMatches(string reference)
91+
{
92+
return Proxy.git_refspec_src_matches(handle, reference);
93+
}
94+
95+
/// <summary>
96+
/// Check whether the given reference matches the target (rhs) part of
97+
/// this refspec.
98+
/// </summary>
99+
/// <param name="reference">The reference name to check</param>
100+
public virtual bool DestinationMatches(string reference)
101+
{
102+
return Proxy.git_refspec_dst_matches(handle, reference);
103+
}
104+
105+
/// <summary>
106+
/// Perform the transformation described by this refspec on the given
107+
/// reference name (from source to destination).
108+
/// </summary>
109+
/// <param name="reference">The reference name to transform</param>
110+
public virtual string Transform(string reference)
111+
{
112+
return Proxy.git_refspec_transform(handle, reference);
113+
}
114+
115+
/// <summary>
116+
/// Perform the reverse of the transformation described by this refspec
117+
/// on the given reference name (from destination to source).
118+
/// </summary>
119+
/// <param name="reference">The reference name to transform</param>
120+
public virtual string ReverseTransform(string reference)
121+
{
122+
return Proxy.git_refspec_rtransform(handle, reference);
123+
}
124+
85125
private string DebuggerDisplay
86126
{
87127
get

0 commit comments

Comments
 (0)