Skip to content

Commit dd87dc6

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 fb12fdc commit dd87dc6

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
@@ -1109,6 +1109,12 @@ internal static extern IntPtr git_reflog_entry_committer(
11091109
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
11101110
internal static extern string git_reflog_entry_message(SafeHandle entry);
11111111

1112+
[DllImport(libgit2)]
1113+
internal static extern int git_refspec_transform(
1114+
GitBuf buf,
1115+
GitRefSpecHandle refspec,
1116+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
1117+
11121118
[DllImport(libgit2)]
11131119
internal static extern int git_refspec_rtransform(
11141120
GitBuf buf,
@@ -1136,6 +1142,16 @@ internal static extern string git_refspec_src(
11361142
[DllImport(libgit2)]
11371143
internal static extern bool git_refspec_force(GitRefSpecHandle refSpec);
11381144

1145+
[DllImport(libgit2)]
1146+
internal static extern bool git_refspec_src_matches(
1147+
GitRefSpecHandle resfpec,
1148+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string reference);
1149+
1150+
[DllImport(libgit2)]
1151+
internal static extern bool git_refspec_dst_matches(
1152+
GitRefSpecHandle resfpec,
1153+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string reference);
1154+
11391155
[DllImport(libgit2)]
11401156
internal static extern int git_remote_autotag(RemoteSafeHandle remote);
11411157

LibGit2Sharp/Core/Proxy.cs

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

19931993
#region git_refspec
19941994

1995+
public static string git_refspec_transform(GitRefSpecHandle refSpecPtr, string name)
1996+
{
1997+
using (var buf = new GitBuf())
1998+
{
1999+
int res = NativeMethods.git_refspec_transform(buf, refSpecPtr, name);
2000+
Ensure.ZeroResult(res);
2001+
2002+
return LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty;
2003+
}
2004+
}
2005+
19952006
public static string git_refspec_rtransform(GitRefSpecHandle refSpecPtr, string name)
19962007
{
19972008
using (var buf = new GitBuf())
@@ -2028,6 +2039,16 @@ public static bool git_refspec_force(GitRefSpecHandle refSpec)
20282039
return NativeMethods.git_refspec_force(refSpec);
20292040
}
20302041

2042+
public static bool git_refspec_src_matches(GitRefSpecHandle refspec, string reference)
2043+
{
2044+
return NativeMethods.git_refspec_src_matches(refspec, reference);
2045+
}
2046+
2047+
public static bool git_refspec_dst_matches(GitRefSpecHandle refspec, string reference)
2048+
{
2049+
return NativeMethods.git_refspec_dst_matches(refspec, reference);
2050+
}
2051+
20312052
#endregion
20322053

20332054
#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)