How to clone public git repository with LibGit2 in Swift?

253 views Asked by At

I am using libgit2 library to clone repository in iOS with swift language. I am able to clone repository by passing username and password

internal func credentialsCallback(
    cred: UnsafeMutablePointer<UnsafeMutablePointer<git_cred>?>?,
    url: UnsafePointer<CChar>?,
    username: UnsafePointer<CChar>?,
    _: UInt32,
    payload: UnsafeMutableRawPointer? ) -> Int32 {

    let result: Int32

    // Find username_from_url
    let name = username.map(String.init(cString:))

    switch Credentials.fromPointer(payload!) ?? .default{
    case .default:
        result = git_cred_default_new(cred)
    case .sshAgent:
        result = git_cred_ssh_key_from_agent(cred, name!)
    case .plaintext(let username, let password):
        result = git_cred_userpass_plaintext_new(cred, username, password)
    case .sshMemory(let username, let publicKey, let privateKey, let passphrase):
        result = git_cred_ssh_key_memory_new(cred, username, publicKey, privateKey, passphrase)
    }

    return (result != GIT_OK.rawValue) ? -1 : 0
}

If I use plaintext and provide a username, password with a URL like https://[email protected]/name/repo.git. But If I try to fetch the same repo with ssh [email protected]:name/repo.git and with sshMemory where I provided username, publickey and privatekey then it is not working.

let creds = Credentials.sshMemory(username: userName, publicKey: pubkeyData, privateKey: privateKeyData, passphrase: "mypassphrase") is not working.

Anyone tried SSH with libgit2 ?

0

There are 0 answers