go-git
go-git copied to clipboard
Authorization from existing configuration
Hello!
I'm trying to write some code to delete remote branches.
However, I'm getting authorization errors on HTTPS repos. SSH seems to work as it uses the SSH config for the user.
Is there a way of passing through the configuration from the gitconfig file?
My Github credentials are saved in my keychain on osx, and then my config has the helper configured to use it.
[credential]
helper = osxkeychain
I'll find my example code to post...
Code is here:
func deleteBranch(repo *git.Repository, remote string, branchShortName string) error {
gitAuth, _ := gitssh.DefaultAuthBuilder("git")
err := repo.Push(&git.PushOptions{
RemoteName: remote,
Auth: gitAuth,
})
return err
}
Is there a way to pass through the auth from the gitconfig? Or would I have to write the logic to fetch it from the keychain as well?
Not quite on topic, but I had similar problems with go-git's default ssh auth on windows.
error creating SSH agent: "SSH agent requested but Pageant not running"
https://github.com/Lercher/gotools/blob/master/gitcli/main.go#L36 solved my problems. Maybe you can adapt the approach, i.e. loading and presenting the private RSA key as Auth, for osx.
@petems any luck or interesting revelations on this? I also need to do something around Git's Credential Store. Can't see anything relating to it / .gitconfig in the go-git codebase 🤔
A working version with deploy key
package main
import (
_ "fmt"
"log"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
const uri = "https://wwwin-github.cisco.com/liantan/doggie.git"
func main() {
gitAuth, err := ssh.NewPublicKeysFromFile(ssh.DefaultUsername, "deploy-private-key", "")
if err != nil {
log.Fatalf("parseprivatekey: %v", err)
}
config, err := gitAuth.ClientConfig()
if err != nil {
log.Fatal(err)
}
customClient := ssh.NewClient(config)
client.InstallProtocol("https", customClient)
_, err = git.PlainClone("tmp", false, &git.CloneOptions{
URL: uri,
})
if err != nil {
log.Fatal(err)
}
log.Println("DONE")
}