diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 77971ea06..874e7a612 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -7,7 +7,7 @@ LibGit2Sharp contributors Copyright © LibGit2Sharp contributors libgit2 git - https://github.com/libgit2/libgit2sharp/ + https://github.com/Hdom/libgit2sharp/ LibGit2Sharp contributors true true @@ -16,8 +16,18 @@ snupkg true ..\libgit2sharp.snk + CM.LibGit2Sharp square-logo.png App_Readme/LICENSE.md + AnyCPU;x64 + + + + DEBUG;TRACE + + + + DEBUG;TRACE @@ -32,7 +42,7 @@ - + diff --git a/LibGit2Sharp/SshExtensions.cs b/LibGit2Sharp/SshExtensions.cs new file mode 100644 index 000000000..80bb21de8 --- /dev/null +++ b/LibGit2Sharp/SshExtensions.cs @@ -0,0 +1,141 @@ +using LibGit2Sharp.Core; +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace LibGit2Sharp.Ssh +{ + internal static class NativeMethods + { + private const string libgit2 = NativeDllName.Name; + + [DllImport(libgit2)] + internal static extern int git_cred_ssh_key_new( + out IntPtr cred, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string publickey, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string privatekey, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string passphrase); + + [DllImport(libgit2)] + internal static extern int git_cred_ssh_key_memory_new( + out IntPtr cred, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string publickey, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string privatekey, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string passphrase); + } + + /// + /// Class that holds SSH username with key credentials for remote repository access. + /// + public sealed class SshUserKeyCredentials : Credentials + { + /// + /// Callback to acquire a credential object. + /// + /// The newly created credential object. + /// 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired. + protected internal override int GitCredentialHandler(out IntPtr cred) + { + if (Username == null) + { + throw new InvalidOperationException("SshUserKeyCredentials contains a null Username."); + } + + if (Passphrase == null) + { + throw new InvalidOperationException("SshUserKeyCredentials contains a null Passphrase."); + } + + if (PublicKey == null) + { + throw new InvalidOperationException("SshUserKeyCredentials contains a null PublicKey."); + } + + if (PrivateKey == null) + { + throw new InvalidOperationException("SshUserKeyCredentials contains a null PrivateKey."); + } + + return NativeMethods.git_cred_ssh_key_new(out cred, Username, PublicKey, PrivateKey, Passphrase); + } + + /// + /// Username for SSH authentication. + /// + public string Username { get; set; } + + /// + /// Public key file location for SSH authentication. + /// + public string PublicKey { get; set; } + + /// + /// Private key file location for SSH authentication. + /// + public string PrivateKey { get; set; } + + /// + /// Passphrase for SSH authentication. + /// + public string Passphrase { get; set; } + } + + /// + /// Class that holds SSH username with in-memory key credentials for remote repository access. + /// + public sealed class SshUserKeyMemoryCredentials : Credentials + { + /// + /// Callback to acquire a credential object. + /// + /// The newly created credential object. + /// 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired. + protected internal override int GitCredentialHandler(out IntPtr cred) + { + if (Username == null) + { + throw new InvalidOperationException("SshUserKeyMemoryCredentials contains a null Username."); + } + + if (Passphrase == null) + { + throw new InvalidOperationException("SshUserKeyMemoryCredentials contains a null Passphrase."); + } + + if (PublicKey == null) + { + //throw new InvalidOperationException("SshUserKeyMemoryCredentials contains a null PublicKey."); + } + + if (PrivateKey == null) + { + throw new InvalidOperationException("SshUserKeyMemoryCredentials contains a null PrivateKey."); + } + + return NativeMethods.git_cred_ssh_key_memory_new(out cred, Username, PublicKey, PrivateKey, Passphrase); + } + + /// + /// Username for SSH authentication. + /// + public string Username { get; set; } + + /// + /// Public key for SSH authentication. + /// + public string PublicKey { get; set; } + + /// + /// Private key for SSH authentication. + /// + public string PrivateKey { get; set; } + + /// + /// Passphrase for SSH authentication. + /// + public string Passphrase { get; set; } + } +} diff --git a/README.md b/README.md index c67e6ec8e..97e9e710d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ +# LibGit2Sharp-SSH-standard + +This is a fork of LibGit2Sharp, modified to support SSH on .NET Standard + + +## Building the Nuget Package +Preparation: +1. first build the LibGit2Sharp-SSH-standard.NativeBinaries Nuget package: https://github.com/alex-weaver/libgit2sharp.nativebinaries +1. edit nuget dependencies to include the correct version of LibGit2Sharp-SSH-standard.NativeBinaries +2. set verion in version.json +Run the following in powershell +``` +dotnet restore +dotnet pack -c Release +``` + +Note that on alpine linux the native binaries have a dependency on libcurl + +--- +The original readme for this package is included below: # LibGit2Sharp [![master azurepipelines][master-azurepipelines-badge]][master-azurepipelines]