git2go中的SSH身份验证

huangapple go评论108阅读模式
英文:

SSH Authentication in git2go

问题

我正在学习Go作为我的第一种编译语言(之前使用的是php/python)。我的第一个项目是一个用于Bitbucket的小型POST钩子监听器,它通过os/exec获取并检出一个Git仓库。现在我想用git2go替换os/exec的调用。然而,我在认证方面遇到了问题。我有以下代码:

package main

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

func main() {

	_, Cred := git.NewCredSshKey("git","~/.ssh/id_rsa.pub","~/.ssh/id_rsa","")
	log.Println(Cred.Type())
	gitH,err := git.OpenRepository(".")
	if (err != nil) {
		log.Fatalln(err)
	}
	remotes,err := gitH.ListRemotes()
	if (err != nil) {
		log.Fatalln(err)
	}
	log.Println(remotes)
	origin,err := gitH.LoadRemote("origin")
	if (err != nil) {
		log.Fatalln(err)
	}
	err = origin.Fetch(nil,"")
	if (err != nil) {
		log.Fatalln(err)
	}
}

当我运行这段代码时,会出现authentication required but no callback set的错误。

根据[文档],看起来我需要添加一个对origin.SetCallbacks()的调用,它需要一个RemoteCallbacks结构体。RemoteCallbacks有一个名为CredentialsCallback的函数,它返回一个整数和一个Cred指针。由于NewCredSshKey返回相同的值,我尝试添加以下代码:

var cb git.RemoteCallbacks
cb.CredentialsCallback = git.NewCredSshKey("git","~/.ssh/id_rsa.pub","~/.ssh/id_rsa","")
origin.SetCallbacks(cb)

但是会出现错误multiple-value git.NewCredSshKey() in single-value contextcannot use cb (type git.RemoteCallbacks) as type *git.RemoteCallbacks in function argument

我想我完全误解了这个工作原理,并且我找不到使用这个库的任何示例。如果有任何提示或指向示例的指针,将不胜感激。

英文:

I'm working on learning Go as my first compiled language (coming from php/python). My first project was a small POST hook listener for Bitbucket, which fetches and then checks out a Git repository via os/exec. I'm now trying to replace the os/exec calls with git2go. I'm running into a snag with the authentication, though. I have the following code:

package main

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

func main() {

	_, Cred := git.NewCredSshKey("git","~/.ssh/id_rsa.pub","~/.ssh/id_rsa","")
	log.Println(Cred.Type())
	gitH,err := git.OpenRepository(".")
	if (err != nil) {
		log.Fatalln(err)
	}
	remotes,err := gitH.ListRemotes()
	if (err != nil) {
		log.Fatalln(err)
	}
	log.Println(remotes)
	origin,err := gitH.LoadRemote("origin")
	if (err != nil) {
		log.Fatalln(err)
	}
	err = origin.Fetch(nil,"")
	if (err != nil) {
		log.Fatalln(err)
	}
}

When I run this I get authentication required but no callback set.

Looking at the docs, it looks like I need to add a call to origin.SetCallbacks() which expects a RemoteCallbacks struct. RemoteCallbacks has the function CredentialsCallback which returns an int and a Cred pointer. Since NewCredSshKey returns the same values, I tried adding the following:

var cb git.RemoteCallbacks
cb.CredentialsCallback = git.NewCredSshKey("git","~/.ssh/id_rsa.pub","~/.ssh/id_rsa","")
origin.SetCallbacks(cb)

which gives the errors multiple-value git.NewCredSshKey() in single-value context and
cannot use cb (type git.RemoteCallbacks) as type *git.RemoteCallbacks in function argument.

I think I'm completely misunderstanding how this works, and I haven't been able to find any examples using this library. Tips or pointers to some examples would be much appreciated.

答案1

得分: 3

一些事情:

CredentialsCallback 需要设置为与其签名匹配的函数,而不是该函数的输出。然而,NewCredSshKey 的签名本身就不正确,只有其返回值匹配。正确的签名应该是:

func(url string, username_from_url string, allowed_types CredType) (int, *Cred)

第二个错误 cannot use cb (type git.RemoteCallbacks) as type *git.RemoteCallbacks 是因为你需要一个指向 RemoteCallbacks 的指针。

要么声明并初始化为指针:

cb := &git.RemoteCallbacks{}
// 或者
// cb := new(git.RemoteCallbacks)

要么在传递参数时取地址:

origin.SetCallbacks(&cb)
英文:

A Couple of things:

CredentialsCallback needs to be set to a function that matches it's signature, not the output of such a function. However, the signature for NewCredSshKey isn't correct in the first place, only its return values match. The correct signature is:

func(url string, username_from_url string, allowed_types CredType) (int, *Cred)

The second error cannot use cb (type git.RemoteCallbacks) as type *git.RemoteCallbacks is because you need a pointer to a RemoteCallbacks.

Either declare and initialize it as a pointer:

cb := &git.RemoteCallbacks{}
// or alternatively 
// cb := new(git.RemoteCallbacks)

or take the address of when passing it as an argument:

origin.SetCallbacks(&cb)

huangapple
  • 本文由 发表于 2014年8月2日 01:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/25085436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定