user6499401
user6499401

Reputation:

A VB.NET equivalent of C# code for libgit2sharp Credentialsprovider

I'm trying to code against LibGit2Sharp, and I'm falling at the first hurdle

I'm looking at the sample code at https://github.com/libgit2/libgit2sharp/wiki/git-clone

As I need to use credentials I'm looking at the bottom bit of code:

var co = new CloneOptions();
co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = "Username", Password = "Password" };
Repository.Clone("https://github.com/libgit2/libgit2sharp.git", "path/to/repo", co);

what has me baffled is

co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = "Username", Password = "Password" };

What is the VB.NET equivalent of that code? And ideally I'd like to understand what this does. What does the

= (_url, _user, _cred) => 

syntax do in C#?

Many thanks

Pino

Upvotes: 1

Views: 278

Answers (2)

user6499401
user6499401

Reputation:

Along the same lines as Nathan's and Craig's suggestions (thanks all!) I implemented this as follows:

    Dim oOpt As LibGit2Sharp.CloneOptions = Nothing
    Dim oCred As LibGit2Sharp.UsernamePasswordCredentials = Nothing

    Try
        oOpt = New LibGit2Sharp.CloneOptions
        oCred = New LibGit2Sharp.UsernamePasswordCredentials
        oCred.Username = "xxxxxxx"
        oCred.Password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        oOpt.CredentialsProvider = New LibGit2Sharp.Handlers.CredentialsHandler(Function(_url, _user, _cred) oCred)

        Try
            LibGit2Sharp.Repository.Clone("<your repository>", "<your local folder>", oOpt)
        Catch ex As Exception

        End Try
    Catch
    Finally
        If Not oCred Is Nothing Then
            oCred = Nothing
        End If
        If Not oOpt Is Nothing Then
            oOpt = Nothing
        End If
    End Try

Job's a good'un

Upvotes: 0

Nathan
Nathan

Reputation: 889

There are too many online converters to mention all of them.
I prefer the converter from Telerik but use the other as a backup.

https://converter.telerik.com/

https://www.carlosag.net/tools/codetranslator/

The code below was converted by Telerik's converter.

Dim co = New CloneOptions()
co.CredentialsProvider = Function(_url, _user, _cred) New UsernamePasswordCredentials With {
    .Username = "Username",
    .Password = "Password"
}
Repository.Clone("https://github.com/libgit2/libgit2sharp.git", "path/to/repo", co)

If I don't understand a C# code, I will convert it with a converter and that usually helps me understand the C# code. Sometimes I have to use multiple converters as each one may give a different result.

Upvotes: 1

Related Questions