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
  • Save zchee/444c8c20aa7756468d8e to your computer and use it in GitHub Desktop.
Save zchee/444c8c20aa7756468d8e to your computer and use it in GitHub Desktop.
Working example with SSH and libgit2/git2go
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("git@github.com:username/private-repository.git", "private-repo", cloneOptions)
if err != nil {
log.Panic(err)
}
log.Print(repo)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment