Skip to content

Commit

Permalink
Teach Remote to transform references using its default fetchspec
Browse files Browse the repository at this point in the history
  • Loading branch information
jamill committed Feb 21, 2013
1 parent 7f723ca commit 7b11e92
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
24 changes: 24 additions & 0 deletions LibGit2Sharp.Tests/RemoteFixture.cs
Expand Up @@ -236,5 +236,29 @@ public void CanTellIfARemoteNameIsValid(string refname, bool expectedResult)
Assert.Equal(expectedResult, repo.Network.Remotes.IsValidName(refname));
}
}

[Fact]
public void CanFetchSpecTransformReferenceToTarget()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
using (var repo = new Repository(StandardTestRepoPath))
{
Remote remote = repo.Network.Remotes["origin"];
Assert.NotNull(remote);
Assert.Equal("refs/remotes/origin/test", remote.FetchSpecTransformToTarget("refs/heads/test"));
}
}

[Fact]
public void CanFetchSpecTransformReferenceToSource()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
using (var repo = new Repository(StandardTestRepoPath))
{
Remote remote = repo.Network.Remotes["origin"];
Assert.NotNull(remote);
Assert.Equal("refs/heads/test", remote.FetchSpecTransformToSource("refs/remotes/origin/test"));
}
}
}
}
9 changes: 8 additions & 1 deletion LibGit2Sharp/Core/NativeMethods.cs
Expand Up @@ -669,9 +669,16 @@ private static bool IsRunningOnLinux()
[DllImport(libgit2)]
internal static extern GitReferenceType git_reference_type(ReferenceSafeHandle reference);

[DllImport(libgit2)]
internal static extern int git_refspec_transform(
byte[] reference,
UIntPtr outlen,
GitFetchSpecHandle refSpec,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);

[DllImport(libgit2)]
internal static extern int git_refspec_rtransform(
byte[] target,
byte[] reference,
UIntPtr outlen,
GitFetchSpecHandle refSpec,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
Expand Down
15 changes: 15 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Expand Up @@ -1252,6 +1252,21 @@ public static GitReferenceType git_reference_type(ReferenceSafeHandle reference)

#region git_refspec

public static string git_fetchspec_transform(GitFetchSpecHandle refSpecPtr, string name)
{
using (ThreadAffinity())
{
// libgit2 API does not support querying for required buffer size.
// Use a sufficiently large buffer until it does.
var buffer = new byte[1024];

int res = NativeMethods.git_refspec_transform(buffer, (UIntPtr)buffer.Length, refSpecPtr, name);
Ensure.ZeroResult(res);

return Utf8Marshaler.Utf8FromBuffer(buffer) ?? string.Empty;
}
}

public static string git_fetchspec_rtransform(GitFetchSpecHandle refSpecPtr, string name)
{
using (ThreadAffinity())
Expand Down
28 changes: 28 additions & 0 deletions LibGit2Sharp/Remote.cs
Expand Up @@ -51,6 +51,34 @@ internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
/// </summary>
public virtual string Url { get; private set; }

/// <summary>
/// Transform a reference to its target reference using the <see cref = "Remote" />'s default fetchspec.
/// </summary>
/// <param name="reference">The reference to transform.</param>
/// <returns>The transformed reference.</returns>
public virtual string FetchSpecTransformToTarget(string reference)
{
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, Name, true))
{
GitFetchSpecHandle fetchSpecPtr = Proxy.git_remote_fetchspec(remoteHandle);
return Proxy.git_fetchspec_transform(fetchSpecPtr, reference);
}
}

/// <summary>
/// Transform a reference to its source reference using the <see cref = "Remote" />'s default fetchspec.
/// </summary>
/// <param name="reference">The reference to transform.</param>
/// <returns>The transformed reference.</returns>
public virtual string FetchSpecTransformToSource(string reference)
{
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, Name, true))
{
GitFetchSpecHandle fetchSpecPtr = Proxy.git_remote_fetchspec(remoteHandle);
return Proxy.git_fetchspec_rtransform(fetchSpecPtr, reference);
}
}

/// <summary>
/// Fetch from the <see cref = "Remote" />.
/// </summary>
Expand Down

0 comments on commit 7b11e92

Please sign in to comment.