Skip to content

Instantly share code, notes, and snippets.

@zchee
Forked from xorpaul/SSHwithgit2go.go
Last active March 10, 2023 18:29
Show Gist options
  • Select an option

  • Save zchee/444c8c20aa7756468d8e to your computer and use it in GitHub Desktop.

Select an option

Save zchee/444c8c20aa7756468d8e to your computer and use it in GitHub Desktop.

Revisions

  1. @xorpaul xorpaul created this gist Jul 27, 2015.
    34 changes: 34 additions & 0 deletions SSHwithgit2go.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    package main

    import (
    git "github.com/libgit2/git2go"
    "log"
    )

    func credentialsCallback(url string, username string, allowedTypes git.CredType) (git.ErrorCode, *git.Cred) {
    ret, cred := git.NewCredSshKey("git", "/home/vagrant/.ssh/id_rsa.pub", "/home/vagrant/.ssh/id_rsa", "")
    return git.ErrorCode(ret), &cred
    }

    // Made this one just return 0 during troubleshooting...
    func certificateCheckCallback(cert *git.Certificate, valid bool, hostname string) git.ErrorCode {
    return 0
    }

    func main() {

    cloneOptions := &git.CloneOptions{}
    // use FetchOptions instead of directly RemoteCallbacks
    // https://github.com/libgit2/git2go/commit/36e0a256fe79f87447bb730fda53e5cbc90eb47c
    cloneOptions.FetchOptions = &git.FetchOptions{
    RemoteCallbacks: git.RemoteCallbacks{
    CredentialsCallback: credentialsCallback,
    CertificateCheckCallback: certificateCheckCallback,
    },
    }
    repo, err := git.Clone("[email protected]:username/private-repository.git", "private-repo", cloneOptions)
    if err != nil {
    log.Panic(err)
    }
    log.Print(repo)
    }