Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions LibGit2Sharp.Tests/SmartSubtransportFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public void CustomSmartSubtransportTest(string scheme, string url)
}
finally
{
GlobalSettings.UnregisterSmartSubtransport(registration);

registration.Dispose();
ServicePointManager.ServerCertificateValidationCallback -= certificateValidationCallback;
}
}
Expand All @@ -89,22 +88,10 @@ public void CannotReregisterScheme()
}
finally
{
GlobalSettings.UnregisterSmartSubtransport(httpRegistration);
httpRegistration.Dispose();
}
}

[Fact]
public void CannotUnregisterTwice()
{
SmartSubtransportRegistration<MockSmartSubtransport> httpRegistration =
GlobalSettings.RegisterSmartSubtransport<MockSmartSubtransport>("http");

GlobalSettings.UnregisterSmartSubtransport(httpRegistration);

Assert.Throws<NotFoundException>(() =>
GlobalSettings.UnregisterSmartSubtransport(httpRegistration));
}

private class MockSmartSubtransport : RpcSmartSubtransport
{
protected override SmartSubtransportStream Action(String url, GitSmartSubtransportAction action)
Expand Down
15 changes: 0 additions & 15 deletions LibGit2Sharp/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ public static SmartSubtransportRegistration<T> RegisterSmartSubtransport<T>(stri
return registration;
}

/// <summary>
/// Unregisters a previously registered <see cref="SmartSubtransport"/>
/// as a custom smart-protocol transport with libgit2.
/// </summary>
/// <typeparam name="T">The type of SmartSubtransport to register</typeparam>
/// <param name="registration">The previous registration</param>
public static void UnregisterSmartSubtransport<T>(SmartSubtransportRegistration<T> registration)
where T : SmartSubtransport, new()
{
Ensure.ArgumentNotNull(registration, "registration");

Proxy.git_transport_unregister(registration.Scheme);
registration.Free();
}

/// <summary>
/// Registers a new <see cref="LogConfiguration"/> to receive
/// information logging information from libgit2 and LibGit2Sharp.
Expand Down
36 changes: 35 additions & 1 deletion LibGit2Sharp/SmartSubtransportRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ namespace LibGit2Sharp
/// under a particular scheme (eg "http").
/// </summary>
/// <typeparam name="T">The type of SmartSubtransport to register</typeparam>
public sealed class SmartSubtransportRegistration<T>
public sealed class SmartSubtransportRegistration<T> : IDisposable
where T : SmartSubtransport, new()
{
private bool disposed = false;

/// <summary>
/// Creates a new native registration for a smart protocol transport
/// in libgit2.
Expand Down Expand Up @@ -57,6 +59,38 @@ internal void Free()
RegistrationPointer = IntPtr.Zero;
}

void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// dispose managed objects
}
// dispose unmanaged objects
Proxy.git_transport_unregister(Scheme);
Free();
disposed = true;
}
}

/// <summary>
/// Clean up resources
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Finalizer called to free unmanaged resources
/// </summary>
~SmartSubtransportRegistration()
{
Dispose(false);
}

private static class EntryPoints
{
// Because our GitSmartSubtransportRegistration structure exists on the managed heap only for a short time (to be marshaled
Expand Down